2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.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_init(void);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_init(int, char *[]);
124 static const struct got_error* cmd_import(int, char *[]);
125 static const struct got_error* cmd_clone(int, char *[]);
126 static const struct got_error* cmd_fetch(int, char *[]);
127 static const struct got_error* cmd_checkout(int, char *[]);
128 static const struct got_error* cmd_update(int, char *[]);
129 static const struct got_error* cmd_log(int, char *[]);
130 static const struct got_error* cmd_diff(int, char *[]);
131 static const struct got_error* cmd_blame(int, char *[]);
132 static const struct got_error* cmd_tree(int, char *[]);
133 static const struct got_error* cmd_status(int, char *[]);
134 static const struct got_error* cmd_ref(int, char *[]);
135 static const struct got_error* cmd_branch(int, char *[]);
136 static const struct got_error* cmd_tag(int, char *[]);
137 static const struct got_error* cmd_add(int, char *[]);
138 static const struct got_error* cmd_remove(int, char *[]);
139 static const struct got_error* cmd_patch(int, char *[]);
140 static const struct got_error* cmd_revert(int, char *[]);
141 static const struct got_error* cmd_commit(int, char *[]);
142 static const struct got_error* cmd_send(int, char *[]);
143 static const struct got_error* cmd_cherrypick(int, char *[]);
144 static const struct got_error* cmd_backout(int, char *[]);
145 static const struct got_error* cmd_rebase(int, char *[]);
146 static const struct got_error* cmd_histedit(int, char *[]);
147 static const struct got_error* cmd_integrate(int, char *[]);
148 static const struct got_error* cmd_merge(int, char *[]);
149 static const struct got_error* cmd_stage(int, char *[]);
150 static const struct got_error* cmd_unstage(int, char *[]);
151 static const struct got_error* cmd_cat(int, char *[]);
152 static const struct got_error* cmd_info(int, char *[]);
154 static const struct got_cmd got_commands[] = {
155 { "init", cmd_init, usage_init, "" },
156 { "import", cmd_import, usage_import, "im" },
157 { "clone", cmd_clone, usage_clone, "cl" },
158 { "fetch", cmd_fetch, usage_fetch, "fe" },
159 { "checkout", cmd_checkout, usage_checkout, "co" },
160 { "update", cmd_update, usage_update, "up" },
161 { "log", cmd_log, usage_log, "" },
162 { "diff", cmd_diff, usage_diff, "di" },
163 { "blame", cmd_blame, usage_blame, "bl" },
164 { "tree", cmd_tree, usage_tree, "tr" },
165 { "status", cmd_status, usage_status, "st" },
166 { "ref", cmd_ref, usage_ref, "" },
167 { "branch", cmd_branch, usage_branch, "br" },
168 { "tag", cmd_tag, usage_tag, "" },
169 { "add", cmd_add, usage_add, "" },
170 { "remove", cmd_remove, usage_remove, "rm" },
171 { "patch", cmd_patch, usage_patch, "pa" },
172 { "revert", cmd_revert, usage_revert, "rv" },
173 { "commit", cmd_commit, usage_commit, "ci" },
174 { "send", cmd_send, usage_send, "se" },
175 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
176 { "backout", cmd_backout, usage_backout, "bo" },
177 { "rebase", cmd_rebase, usage_rebase, "rb" },
178 { "histedit", cmd_histedit, usage_histedit, "he" },
179 { "integrate", cmd_integrate, usage_integrate,"ig" },
180 { "merge", cmd_merge, usage_merge, "mg" },
181 { "stage", cmd_stage, usage_stage, "sg" },
182 { "unstage", cmd_unstage, usage_unstage, "ug" },
183 { "cat", cmd_cat, usage_cat, "" },
184 { "info", cmd_info, usage_info, "" },
188 list_commands(FILE *fp)
192 fprintf(fp, "commands:");
193 for (i = 0; i < nitems(got_commands); i++) {
194 const struct got_cmd *cmd = &got_commands[i];
195 fprintf(fp, " %s", cmd->cmd_name);
201 option_conflict(char a, char b)
203 errx(1, "-%c and -%c options are mutually exclusive", a, b);
207 main(int argc, char *argv[])
209 const struct got_cmd *cmd;
212 int hflag = 0, Vflag = 0;
213 static const struct option longopts[] = {
214 { "version", no_argument, NULL, 'V' },
218 setlocale(LC_CTYPE, "");
220 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
240 got_version_print_str();
245 usage(hflag, hflag ? 0 : 1);
247 signal(SIGINT, catch_sigint);
248 signal(SIGPIPE, catch_sigpipe);
250 for (i = 0; i < nitems(got_commands); i++) {
251 const struct got_error *error;
253 cmd = &got_commands[i];
255 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
256 strcmp(cmd->cmd_alias, argv[0]) != 0)
262 error = cmd->cmd_main(argc, argv);
263 if (error && error->code != GOT_ERR_CANCELLED &&
264 error->code != GOT_ERR_PRIVSEP_EXIT &&
265 !(sigpipe_received &&
266 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
268 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
269 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
276 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
277 list_commands(stderr);
282 usage(int hflag, int status)
284 FILE *fp = (status == 0) ? stdout : stderr;
286 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
293 static const struct got_error *
294 get_editor(char **abspath)
296 const struct got_error *err = NULL;
301 editor = getenv("VISUAL");
303 editor = getenv("EDITOR");
306 err = got_path_find_prog(abspath, editor);
311 if (*abspath == NULL) {
312 *abspath = strdup("/bin/ed");
313 if (*abspath == NULL)
314 return got_error_from_errno("strdup");
320 static const struct got_error *
321 apply_unveil(const char *repo_path, int repo_read_only,
322 const char *worktree_path)
324 const struct got_error *err;
327 if (unveil("gmon.out", "rwc") != 0)
328 return got_error_from_errno2("unveil", "gmon.out");
330 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
331 return got_error_from_errno2("unveil", repo_path);
333 if (worktree_path && unveil(worktree_path, "rwc") != 0)
334 return got_error_from_errno2("unveil", worktree_path);
336 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
337 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
339 err = got_privsep_unveil_exec_helpers();
343 if (unveil(NULL, NULL) != 0)
344 return got_error_from_errno("unveil");
352 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
356 static const struct got_error *
357 cmd_init(int argc, char *argv[])
359 const struct got_error *error = NULL;
360 char *repo_path = NULL;
363 while ((ch = getopt(argc, argv, "")) != -1) {
375 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
381 repo_path = strdup(argv[0]);
382 if (repo_path == NULL)
383 return got_error_from_errno("strdup");
385 got_path_strip_trailing_slashes(repo_path);
387 error = got_path_mkdir(repo_path);
389 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
392 error = apply_unveil(repo_path, 0, NULL);
396 error = got_repo_init(repo_path);
405 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
406 "[-r repository-path] [-I pattern] path\n", getprogname());
411 spawn_editor(const char *editor, const char *file)
414 sig_t sighup, sigint, sigquit;
417 sighup = signal(SIGHUP, SIG_IGN);
418 sigint = signal(SIGINT, SIG_IGN);
419 sigquit = signal(SIGQUIT, SIG_IGN);
421 switch (pid = fork()) {
425 execl(editor, editor, file, (char *)NULL);
429 while (waitpid(pid, &st, 0) == -1)
434 (void)signal(SIGHUP, sighup);
435 (void)signal(SIGINT, sigint);
436 (void)signal(SIGQUIT, sigquit);
438 if (!WIFEXITED(st)) {
443 return WEXITSTATUS(st);
446 static const struct got_error *
447 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
448 const char *initial_content, size_t initial_content_len,
449 int require_modification)
451 const struct got_error *err = NULL;
457 size_t len, logmsg_len;
458 char *initial_content_stripped = NULL, *buf = NULL, *s;
462 if (stat(logmsg_path, &st) == -1)
463 return got_error_from_errno2("stat", logmsg_path);
465 if (spawn_editor(editor, logmsg_path) == -1)
466 return got_error_from_errno("failed spawning editor");
468 if (stat(logmsg_path, &st2) == -1)
469 return got_error_from_errno("stat");
471 if (require_modification &&
472 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
473 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
474 "no changes made to commit message, aborting");
477 * Set up a stripped version of the initial content without comments
478 * and blank lines. We need this in order to check if the message
479 * has in fact been edited.
481 initial_content_stripped = malloc(initial_content_len + 1);
482 if (initial_content_stripped == NULL)
483 return got_error_from_errno("malloc");
484 initial_content_stripped[0] = '\0';
486 buf = strdup(initial_content);
488 err = got_error_from_errno("strdup");
493 while ((line = strsep(&s, "\n")) != NULL) {
494 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
495 continue; /* remove comments and leading empty lines */
496 len = strlcat(initial_content_stripped, line,
497 initial_content_len + 1);
498 if (len >= initial_content_len + 1) {
499 err = got_error(GOT_ERR_NO_SPACE);
503 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
504 initial_content_stripped[len - 1] = '\0';
508 logmsg_len = st2.st_size;
509 *logmsg = malloc(logmsg_len + 1);
511 return got_error_from_errno("malloc");
514 fp = fopen(logmsg_path, "re");
516 err = got_error_from_errno("fopen");
521 while ((linelen = getline(&line, &linesize, fp)) != -1) {
522 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
523 continue; /* remove comments and leading empty lines */
524 len = strlcat(*logmsg, line, logmsg_len + 1);
525 if (len >= logmsg_len + 1) {
526 err = got_error(GOT_ERR_NO_SPACE);
532 err = got_ferror(fp, GOT_ERR_IO);
535 while (len > 0 && (*logmsg)[len - 1] == '\n') {
536 (*logmsg)[len - 1] = '\0';
541 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
542 "commit message cannot be empty, aborting");
545 if (require_modification &&
546 strcmp(*logmsg, initial_content_stripped) == 0)
547 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
548 "no changes made to commit message, aborting");
550 free(initial_content_stripped);
552 if (fp && fclose(fp) == EOF && err == NULL)
553 err = got_error_from_errno("fclose");
561 static const struct got_error *
562 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
563 const char *path_dir, const char *branch_name)
565 char *initial_content = NULL;
566 const struct got_error *err = NULL;
567 int initial_content_len;
570 initial_content_len = asprintf(&initial_content,
571 "\n# %s to be imported to branch %s\n", path_dir,
573 if (initial_content_len == -1)
574 return got_error_from_errno("asprintf");
576 err = got_opentemp_named_fd(logmsg_path, &fd,
577 GOT_TMPDIR_STR "/got-importmsg");
581 if (write(fd, initial_content, initial_content_len) == -1) {
582 err = got_error_from_errno2("write", *logmsg_path);
586 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
587 initial_content_len, 1);
589 if (fd != -1 && close(fd) == -1 && err == NULL)
590 err = got_error_from_errno2("close", *logmsg_path);
591 free(initial_content);
599 static const struct got_error *
600 import_progress(void *arg, const char *path)
602 printf("A %s\n", path);
607 valid_author(const char *author)
610 * Really dumb email address check; we're only doing this to
611 * avoid git's object parser breaking on commits we create.
613 while (*author && *author != '<')
617 while (*author && *author != '@')
621 while (*author && *author != '>')
623 return *author == '>';
626 static const struct got_error *
627 get_author(char **author, struct got_repository *repo,
628 struct got_worktree *worktree)
630 const struct got_error *err = NULL;
631 const char *got_author = NULL, *name, *email;
632 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
637 worktree_conf = got_worktree_get_gotconfig(worktree);
638 repo_conf = got_repo_get_gotconfig(repo);
641 * Priority of potential author information sources, from most
642 * significant to least significant:
643 * 1) work tree's .got/got.conf file
644 * 2) repository's got.conf file
645 * 3) repository's git config file
646 * 4) environment variables
647 * 5) global git config files (in user's home directory or /etc)
651 got_author = got_gotconfig_get_author(worktree_conf);
652 if (got_author == NULL)
653 got_author = got_gotconfig_get_author(repo_conf);
654 if (got_author == NULL) {
655 name = got_repo_get_gitconfig_author_name(repo);
656 email = got_repo_get_gitconfig_author_email(repo);
658 if (asprintf(author, "%s <%s>", name, email) == -1)
659 return got_error_from_errno("asprintf");
663 got_author = getenv("GOT_AUTHOR");
664 if (got_author == NULL) {
665 name = got_repo_get_global_gitconfig_author_name(repo);
666 email = got_repo_get_global_gitconfig_author_email(
669 if (asprintf(author, "%s <%s>", name, email)
671 return got_error_from_errno("asprintf");
674 /* TODO: Look up user in password database? */
675 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
679 *author = strdup(got_author);
681 return got_error_from_errno("strdup");
683 if (!valid_author(*author)) {
684 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
691 static const struct got_error *
692 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
693 struct got_worktree *worktree)
695 const char *got_allowed_signers = NULL;
696 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
698 *allowed_signers = NULL;
701 worktree_conf = got_worktree_get_gotconfig(worktree);
702 repo_conf = got_repo_get_gotconfig(repo);
705 * Priority of potential author information sources, from most
706 * significant to least significant:
707 * 1) work tree's .got/got.conf file
708 * 2) repository's got.conf file
712 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
714 if (got_allowed_signers == NULL)
715 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
718 if (got_allowed_signers) {
719 *allowed_signers = strdup(got_allowed_signers);
720 if (*allowed_signers == NULL)
721 return got_error_from_errno("strdup");
726 static const struct got_error *
727 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
728 struct got_worktree *worktree)
730 const char *got_revoked_signers = NULL;
731 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
733 *revoked_signers = NULL;
736 worktree_conf = got_worktree_get_gotconfig(worktree);
737 repo_conf = got_repo_get_gotconfig(repo);
740 * Priority of potential author information sources, from most
741 * significant to least significant:
742 * 1) work tree's .got/got.conf file
743 * 2) repository's got.conf file
747 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
749 if (got_revoked_signers == NULL)
750 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
753 if (got_revoked_signers) {
754 *revoked_signers = strdup(got_revoked_signers);
755 if (*revoked_signers == NULL)
756 return got_error_from_errno("strdup");
761 static const struct got_error *
762 get_gitconfig_path(char **gitconfig_path)
764 const char *homedir = getenv("HOME");
766 *gitconfig_path = NULL;
768 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
769 return got_error_from_errno("asprintf");
775 static const struct got_error *
776 cmd_import(int argc, char *argv[])
778 const struct got_error *error = NULL;
779 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
780 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
781 const char *branch_name = "main";
782 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
783 struct got_repository *repo = NULL;
784 struct got_reference *branch_ref = NULL, *head_ref = NULL;
785 struct got_object_id *new_commit_id = NULL;
787 struct got_pathlist_head ignores;
788 struct got_pathlist_entry *pe;
789 int preserve_logmsg = 0;
790 int *pack_fds = NULL;
792 TAILQ_INIT(&ignores);
794 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
797 branch_name = optarg;
800 logmsg = strdup(optarg);
801 if (logmsg == NULL) {
802 error = got_error_from_errno("strdup");
807 repo_path = realpath(optarg, NULL);
808 if (repo_path == NULL) {
809 error = got_error_from_errno2("realpath",
815 if (optarg[0] == '\0')
817 error = got_pathlist_insert(&pe, &ignores, optarg,
832 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
840 if (repo_path == NULL) {
841 repo_path = getcwd(NULL, 0);
842 if (repo_path == NULL)
843 return got_error_from_errno("getcwd");
845 got_path_strip_trailing_slashes(repo_path);
846 error = get_gitconfig_path(&gitconfig_path);
849 error = got_repo_pack_fds_open(&pack_fds);
852 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
856 error = get_author(&author, repo, NULL);
861 * Don't let the user create a branch name with a leading '-'.
862 * While technically a valid reference name, this case is usually
863 * an unintended typo.
865 if (branch_name[0] == '-')
866 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
868 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
869 error = got_error_from_errno("asprintf");
873 error = got_ref_open(&branch_ref, repo, refname, 0);
875 if (error->code != GOT_ERR_NOT_REF)
878 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
879 "import target branch already exists");
883 path_dir = realpath(argv[0], NULL);
884 if (path_dir == NULL) {
885 error = got_error_from_errno2("realpath", argv[0]);
888 got_path_strip_trailing_slashes(path_dir);
891 * unveil(2) traverses exec(2); if an editor is used we have
892 * to apply unveil after the log message has been written.
894 if (logmsg == NULL || strlen(logmsg) == 0) {
895 error = get_editor(&editor);
899 error = collect_import_msg(&logmsg, &logmsg_path, editor,
902 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
909 if (unveil(path_dir, "r") != 0) {
910 error = got_error_from_errno2("unveil", path_dir);
916 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
923 error = got_repo_import(&new_commit_id, path_dir, logmsg,
924 author, &ignores, repo, import_progress, NULL);
931 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
938 error = got_ref_write(branch_ref, repo);
945 error = got_object_id_str(&id_str, new_commit_id);
952 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
954 if (error->code != GOT_ERR_NOT_REF) {
960 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
968 error = got_ref_write(head_ref, repo);
976 printf("Created branch %s with commit %s\n",
977 got_ref_get_name(branch_ref), id_str);
980 const struct got_error *pack_err =
981 got_repo_pack_fds_close(pack_fds);
985 if (preserve_logmsg) {
986 fprintf(stderr, "%s: log message preserved in %s\n",
987 getprogname(), logmsg_path);
988 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
989 error = got_error_from_errno2("unlink", logmsg_path);
998 free(gitconfig_path);
1000 got_ref_close(branch_ref);
1002 got_ref_close(head_ref);
1009 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
1010 "[-R reference] repository-url [directory]\n", getprogname());
1014 struct got_fetch_progress_arg {
1015 char last_scaled_size[FMT_SCALED_STRSIZE];
1017 int last_p_resolved;
1020 struct got_repository *repo;
1023 int configs_created;
1025 struct got_pathlist_head *symrefs;
1026 struct got_pathlist_head *wanted_branches;
1027 struct got_pathlist_head *wanted_refs;
1031 const char *remote_repo_path;
1032 const char *git_url;
1033 int fetch_all_branches;
1034 int mirror_references;
1038 /* XXX forward declaration */
1039 static const struct got_error *
1040 create_config_files(const char *proto, const char *host, const char *port,
1041 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1042 int mirror_references, struct got_pathlist_head *symrefs,
1043 struct got_pathlist_head *wanted_branches,
1044 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1046 static const struct got_error *
1047 fetch_progress(void *arg, const char *message, off_t packfile_size,
1048 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1050 const struct got_error *err = NULL;
1051 struct got_fetch_progress_arg *a = arg;
1052 char scaled_size[FMT_SCALED_STRSIZE];
1053 int p_indexed, p_resolved;
1054 int print_size = 0, print_indexed = 0, print_resolved = 0;
1057 * In order to allow a failed clone to be resumed with 'got fetch'
1058 * we try to create configuration files as soon as possible.
1059 * Once the server has sent information about its default branch
1060 * we have all required information.
1062 if (a->create_configs && !a->configs_created &&
1063 !TAILQ_EMPTY(a->config_info.symrefs)) {
1064 err = create_config_files(a->config_info.proto,
1065 a->config_info.host, a->config_info.port,
1066 a->config_info.remote_repo_path,
1067 a->config_info.git_url,
1068 a->config_info.fetch_all_branches,
1069 a->config_info.mirror_references,
1070 a->config_info.symrefs,
1071 a->config_info.wanted_branches,
1072 a->config_info.wanted_refs, a->repo);
1075 a->configs_created = 1;
1078 if (a->verbosity < 0)
1081 if (message && message[0] != '\0') {
1082 printf("\rserver: %s", message);
1087 if (packfile_size > 0 || nobj_indexed > 0) {
1088 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1089 (a->last_scaled_size[0] == '\0' ||
1090 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1092 if (strlcpy(a->last_scaled_size, scaled_size,
1093 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1094 return got_error(GOT_ERR_NO_SPACE);
1096 if (nobj_indexed > 0) {
1097 p_indexed = (nobj_indexed * 100) / nobj_total;
1098 if (p_indexed != a->last_p_indexed) {
1099 a->last_p_indexed = p_indexed;
1104 if (nobj_resolved > 0) {
1105 p_resolved = (nobj_resolved * 100) /
1106 (nobj_total - nobj_loose);
1107 if (p_resolved != a->last_p_resolved) {
1108 a->last_p_resolved = p_resolved;
1116 if (print_size || print_indexed || print_resolved)
1119 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1121 printf("; indexing %d%%", p_indexed);
1123 printf("; resolving deltas %d%%", p_resolved);
1124 if (print_size || print_indexed || print_resolved)
1130 static const struct got_error *
1131 create_symref(const char *refname, struct got_reference *target_ref,
1132 int verbosity, struct got_repository *repo)
1134 const struct got_error *err;
1135 struct got_reference *head_symref;
1137 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1141 err = got_ref_write(head_symref, repo);
1142 if (err == NULL && verbosity > 0) {
1143 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1144 got_ref_get_name(target_ref));
1146 got_ref_close(head_symref);
1150 static const struct got_error *
1151 list_remote_refs(struct got_pathlist_head *symrefs,
1152 struct got_pathlist_head *refs)
1154 const struct got_error *err;
1155 struct got_pathlist_entry *pe;
1157 TAILQ_FOREACH(pe, symrefs, entry) {
1158 const char *refname = pe->path;
1159 const char *targetref = pe->data;
1161 printf("%s: %s\n", refname, targetref);
1164 TAILQ_FOREACH(pe, refs, entry) {
1165 const char *refname = pe->path;
1166 struct got_object_id *id = pe->data;
1169 err = got_object_id_str(&id_str, id);
1172 printf("%s: %s\n", refname, id_str);
1179 static const struct got_error *
1180 create_ref(const char *refname, struct got_object_id *id,
1181 int verbosity, struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 struct got_reference *ref;
1187 err = got_object_id_str(&id_str, id);
1191 err = got_ref_alloc(&ref, refname, id);
1195 err = got_ref_write(ref, repo);
1198 if (err == NULL && verbosity >= 0)
1199 printf("Created reference %s: %s\n", refname, id_str);
1206 match_wanted_ref(const char *refname, const char *wanted_ref)
1208 if (strncmp(refname, "refs/", 5) != 0)
1213 * Prevent fetching of references that won't make any
1214 * sense outside of the remote repository's context.
1216 if (strncmp(refname, "got/", 4) == 0)
1218 if (strncmp(refname, "remotes/", 8) == 0)
1221 if (strncmp(wanted_ref, "refs/", 5) == 0)
1224 /* Allow prefix match. */
1225 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1228 /* Allow exact match. */
1229 return (strcmp(refname, wanted_ref) == 0);
1233 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1235 struct got_pathlist_entry *pe;
1237 TAILQ_FOREACH(pe, wanted_refs, entry) {
1238 if (match_wanted_ref(refname, pe->path))
1245 static const struct got_error *
1246 create_wanted_ref(const char *refname, struct got_object_id *id,
1247 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1249 const struct got_error *err;
1250 char *remote_refname;
1252 if (strncmp("refs/", refname, 5) == 0)
1255 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1256 remote_repo_name, refname) == -1)
1257 return got_error_from_errno("asprintf");
1259 err = create_ref(remote_refname, id, verbosity, repo);
1260 free(remote_refname);
1264 static const struct got_error *
1265 create_gotconfig(const char *proto, const char *host, const char *port,
1266 const char *remote_repo_path, const char *default_branch,
1267 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1268 struct got_pathlist_head *wanted_refs, int mirror_references,
1269 struct got_repository *repo)
1271 const struct got_error *err = NULL;
1272 char *gotconfig_path = NULL;
1273 char *gotconfig = NULL;
1274 FILE *gotconfig_file = NULL;
1275 const char *branchname = NULL;
1276 char *branches = NULL, *refs = NULL;
1279 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1280 struct got_pathlist_entry *pe;
1281 TAILQ_FOREACH(pe, wanted_branches, entry) {
1283 branchname = pe->path;
1284 if (strncmp(branchname, "refs/heads/", 11) == 0)
1286 if (asprintf(&s, "%s\"%s\" ",
1287 branches ? branches : "", branchname) == -1) {
1288 err = got_error_from_errno("asprintf");
1294 } else if (!fetch_all_branches && default_branch) {
1295 branchname = default_branch;
1296 if (strncmp(branchname, "refs/heads/", 11) == 0)
1298 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1299 err = got_error_from_errno("asprintf");
1303 if (!TAILQ_EMPTY(wanted_refs)) {
1304 struct got_pathlist_entry *pe;
1305 TAILQ_FOREACH(pe, wanted_refs, entry) {
1307 const char *refname = pe->path;
1308 if (strncmp(refname, "refs/", 5) == 0)
1310 if (asprintf(&s, "%s\"%s\" ",
1311 refs ? refs : "", refname) == -1) {
1312 err = got_error_from_errno("asprintf");
1320 /* Create got.conf(5). */
1321 gotconfig_path = got_repo_get_path_gotconfig(repo);
1322 if (gotconfig_path == NULL) {
1323 err = got_error_from_errno("got_repo_get_path_gotconfig");
1326 gotconfig_file = fopen(gotconfig_path, "ae");
1327 if (gotconfig_file == NULL) {
1328 err = got_error_from_errno2("fopen", gotconfig_path);
1331 if (asprintf(&gotconfig,
1336 "\trepository \"%s\"\n"
1342 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1343 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1344 remote_repo_path, branches ? "\tbranch { " : "",
1345 branches ? branches : "", branches ? "}\n" : "",
1346 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1347 mirror_references ? "\tmirror_references yes\n" : "",
1348 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1349 err = got_error_from_errno("asprintf");
1352 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1353 if (n != strlen(gotconfig)) {
1354 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1359 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1360 err = got_error_from_errno2("fclose", gotconfig_path);
1361 free(gotconfig_path);
1366 static const struct got_error *
1367 create_gitconfig(const char *git_url, const char *default_branch,
1368 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1369 struct got_pathlist_head *wanted_refs, int mirror_references,
1370 struct got_repository *repo)
1372 const struct got_error *err = NULL;
1373 char *gitconfig_path = NULL;
1374 char *gitconfig = NULL;
1375 FILE *gitconfig_file = NULL;
1376 char *branches = NULL, *refs = NULL;
1377 const char *branchname;
1380 /* Create a config file Git can understand. */
1381 gitconfig_path = got_repo_get_path_gitconfig(repo);
1382 if (gitconfig_path == NULL) {
1383 err = got_error_from_errno("got_repo_get_path_gitconfig");
1386 gitconfig_file = fopen(gitconfig_path, "ae");
1387 if (gitconfig_file == NULL) {
1388 err = got_error_from_errno2("fopen", gitconfig_path);
1391 if (fetch_all_branches) {
1392 if (mirror_references) {
1393 if (asprintf(&branches,
1394 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1395 err = got_error_from_errno("asprintf");
1398 } else if (asprintf(&branches,
1399 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1400 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1401 err = got_error_from_errno("asprintf");
1404 } else if (!TAILQ_EMPTY(wanted_branches)) {
1405 struct got_pathlist_entry *pe;
1406 TAILQ_FOREACH(pe, wanted_branches, entry) {
1408 branchname = pe->path;
1409 if (strncmp(branchname, "refs/heads/", 11) == 0)
1411 if (mirror_references) {
1413 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1414 branches ? branches : "",
1415 branchname, branchname) == -1) {
1416 err = got_error_from_errno("asprintf");
1419 } else if (asprintf(&s,
1420 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1421 branches ? branches : "",
1422 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1423 branchname) == -1) {
1424 err = got_error_from_errno("asprintf");
1432 * If the server specified a default branch, use just that one.
1433 * Otherwise fall back to fetching all branches on next fetch.
1435 if (default_branch) {
1436 branchname = default_branch;
1437 if (strncmp(branchname, "refs/heads/", 11) == 0)
1440 branchname = "*"; /* fall back to all branches */
1441 if (mirror_references) {
1442 if (asprintf(&branches,
1443 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1444 branchname, branchname) == -1) {
1445 err = got_error_from_errno("asprintf");
1448 } else if (asprintf(&branches,
1449 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1450 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1451 branchname) == -1) {
1452 err = got_error_from_errno("asprintf");
1456 if (!TAILQ_EMPTY(wanted_refs)) {
1457 struct got_pathlist_entry *pe;
1458 TAILQ_FOREACH(pe, wanted_refs, entry) {
1460 const char *refname = pe->path;
1461 if (strncmp(refname, "refs/", 5) == 0)
1463 if (mirror_references) {
1465 "%s\tfetch = refs/%s:refs/%s\n",
1466 refs ? refs : "", refname, refname) == -1) {
1467 err = got_error_from_errno("asprintf");
1470 } else if (asprintf(&s,
1471 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1473 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1475 err = got_error_from_errno("asprintf");
1483 if (asprintf(&gitconfig,
1488 "\tfetch = refs/tags/*:refs/tags/*\n",
1489 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1490 refs ? refs : "") == -1) {
1491 err = got_error_from_errno("asprintf");
1494 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1495 if (n != strlen(gitconfig)) {
1496 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1500 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1501 err = got_error_from_errno2("fclose", gitconfig_path);
1502 free(gitconfig_path);
1507 static const struct got_error *
1508 create_config_files(const char *proto, const char *host, const char *port,
1509 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1510 int mirror_references, struct got_pathlist_head *symrefs,
1511 struct got_pathlist_head *wanted_branches,
1512 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1514 const struct got_error *err = NULL;
1515 const char *default_branch = NULL;
1516 struct got_pathlist_entry *pe;
1519 * If we asked for a set of wanted branches then use the first
1522 if (!TAILQ_EMPTY(wanted_branches)) {
1523 pe = TAILQ_FIRST(wanted_branches);
1524 default_branch = pe->path;
1526 /* First HEAD ref listed by server is the default branch. */
1527 TAILQ_FOREACH(pe, symrefs, entry) {
1528 const char *refname = pe->path;
1529 const char *target = pe->data;
1531 if (strcmp(refname, GOT_REF_HEAD) != 0)
1534 default_branch = target;
1539 /* Create got.conf(5). */
1540 err = create_gotconfig(proto, host, port, remote_repo_path,
1541 default_branch, fetch_all_branches, wanted_branches,
1542 wanted_refs, mirror_references, repo);
1546 /* Create a config file Git can understand. */
1547 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1548 wanted_branches, wanted_refs, mirror_references, repo);
1551 static const struct got_error *
1552 cmd_clone(int argc, char *argv[])
1554 const struct got_error *error = NULL;
1555 const char *uri, *dirname;
1556 char *proto, *host, *port, *repo_name, *server_path;
1557 char *default_destdir = NULL, *id_str = NULL;
1558 const char *repo_path;
1559 struct got_repository *repo = NULL;
1560 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1561 struct got_pathlist_entry *pe;
1562 struct got_object_id *pack_hash = NULL;
1563 int ch, fetchfd = -1, fetchstatus;
1564 pid_t fetchpid = -1;
1565 struct got_fetch_progress_arg fpa;
1566 char *git_url = NULL;
1567 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1568 int list_refs_only = 0;
1569 int *pack_fds = NULL;
1572 TAILQ_INIT(&symrefs);
1573 TAILQ_INIT(&wanted_branches);
1574 TAILQ_INIT(&wanted_refs);
1576 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1579 fetch_all_branches = 1;
1582 error = got_pathlist_append(&wanted_branches,
1591 mirror_references = 1;
1596 else if (verbosity < 3)
1603 error = got_pathlist_append(&wanted_refs,
1616 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1617 option_conflict('a', 'b');
1618 if (list_refs_only) {
1619 if (!TAILQ_EMPTY(&wanted_branches))
1620 option_conflict('l', 'b');
1621 if (fetch_all_branches)
1622 option_conflict('l', 'a');
1623 if (mirror_references)
1624 option_conflict('l', 'm');
1625 if (!TAILQ_EMPTY(&wanted_refs))
1626 option_conflict('l', 'R');
1638 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1643 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1644 host, port ? ":" : "", port ? port : "",
1645 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1646 error = got_error_from_errno("asprintf");
1650 if (strcmp(proto, "git") == 0) {
1652 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1653 "sendfd dns inet unveil", NULL) == -1)
1656 } else if (strcmp(proto, "git+ssh") == 0 ||
1657 strcmp(proto, "ssh") == 0) {
1659 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1660 "sendfd unveil", NULL) == -1)
1663 } else if (strcmp(proto, "http") == 0 ||
1664 strcmp(proto, "git+http") == 0) {
1665 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1668 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1671 if (dirname == NULL) {
1672 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1673 error = got_error_from_errno("asprintf");
1676 repo_path = default_destdir;
1678 repo_path = dirname;
1680 if (!list_refs_only) {
1681 error = got_path_mkdir(repo_path);
1683 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1684 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1686 if (!got_path_dir_is_empty(repo_path)) {
1687 error = got_error_path(repo_path,
1688 GOT_ERR_DIR_NOT_EMPTY);
1693 error = got_dial_apply_unveil(proto);
1697 error = apply_unveil(repo_path, 0, NULL);
1702 printf("Connecting to %s%s%s\n", host,
1703 port ? ":" : "", port ? port : "");
1705 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1706 server_path, verbosity);
1710 if (!list_refs_only) {
1711 error = got_repo_init(repo_path);
1714 error = got_repo_pack_fds_open(&pack_fds);
1717 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1722 fpa.last_scaled_size[0] = '\0';
1723 fpa.last_p_indexed = -1;
1724 fpa.last_p_resolved = -1;
1725 fpa.verbosity = verbosity;
1726 fpa.create_configs = 1;
1727 fpa.configs_created = 0;
1729 fpa.config_info.symrefs = &symrefs;
1730 fpa.config_info.wanted_branches = &wanted_branches;
1731 fpa.config_info.wanted_refs = &wanted_refs;
1732 fpa.config_info.proto = proto;
1733 fpa.config_info.host = host;
1734 fpa.config_info.port = port;
1735 fpa.config_info.remote_repo_path = server_path;
1736 fpa.config_info.git_url = git_url;
1737 fpa.config_info.fetch_all_branches = fetch_all_branches;
1738 fpa.config_info.mirror_references = mirror_references;
1739 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1740 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1741 fetch_all_branches, &wanted_branches, &wanted_refs,
1742 list_refs_only, verbosity, fetchfd, repo,
1743 fetch_progress, &fpa);
1747 if (list_refs_only) {
1748 error = list_remote_refs(&symrefs, &refs);
1752 if (pack_hash == NULL) {
1753 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1754 "server sent an empty pack file");
1757 error = got_object_id_str(&id_str, pack_hash);
1761 printf("\nFetched %s.pack\n", id_str);
1764 /* Set up references provided with the pack file. */
1765 TAILQ_FOREACH(pe, &refs, entry) {
1766 const char *refname = pe->path;
1767 struct got_object_id *id = pe->data;
1768 char *remote_refname;
1770 if (is_wanted_ref(&wanted_refs, refname) &&
1771 !mirror_references) {
1772 error = create_wanted_ref(refname, id,
1773 GOT_FETCH_DEFAULT_REMOTE_NAME,
1774 verbosity - 1, repo);
1780 error = create_ref(refname, id, verbosity - 1, repo);
1784 if (mirror_references)
1787 if (strncmp("refs/heads/", refname, 11) != 0)
1790 if (asprintf(&remote_refname,
1791 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1792 refname + 11) == -1) {
1793 error = got_error_from_errno("asprintf");
1796 error = create_ref(remote_refname, id, verbosity - 1, repo);
1797 free(remote_refname);
1802 /* Set the HEAD reference if the server provided one. */
1803 TAILQ_FOREACH(pe, &symrefs, entry) {
1804 struct got_reference *target_ref;
1805 const char *refname = pe->path;
1806 const char *target = pe->data;
1807 char *remote_refname = NULL, *remote_target = NULL;
1809 if (strcmp(refname, GOT_REF_HEAD) != 0)
1812 error = got_ref_open(&target_ref, repo, target, 0);
1814 if (error->code == GOT_ERR_NOT_REF) {
1821 error = create_symref(refname, target_ref, verbosity, repo);
1822 got_ref_close(target_ref);
1826 if (mirror_references)
1829 if (strncmp("refs/heads/", target, 11) != 0)
1832 if (asprintf(&remote_refname,
1833 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1835 error = got_error_from_errno("asprintf");
1838 if (asprintf(&remote_target,
1839 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1840 target + 11) == -1) {
1841 error = got_error_from_errno("asprintf");
1842 free(remote_refname);
1845 error = got_ref_open(&target_ref, repo, remote_target, 0);
1847 free(remote_refname);
1848 free(remote_target);
1849 if (error->code == GOT_ERR_NOT_REF) {
1855 error = create_symref(remote_refname, target_ref,
1856 verbosity - 1, repo);
1857 free(remote_refname);
1858 free(remote_target);
1859 got_ref_close(target_ref);
1865 * We failed to set the HEAD reference. If we asked for
1866 * a set of wanted branches use the first of one of those
1867 * which could be fetched instead.
1869 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1870 const char *target = pe->path;
1871 struct got_reference *target_ref;
1873 error = got_ref_open(&target_ref, repo, target, 0);
1875 if (error->code == GOT_ERR_NOT_REF) {
1882 error = create_symref(GOT_REF_HEAD, target_ref,
1884 got_ref_close(target_ref);
1892 printf("Created %s repository '%s'\n",
1893 mirror_references ? "mirrored" : "cloned", repo_path);
1896 const struct got_error *pack_err =
1897 got_repo_pack_fds_close(pack_fds);
1902 if (kill(fetchpid, SIGTERM) == -1)
1903 error = got_error_from_errno("kill");
1904 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1905 error = got_error_from_errno("waitpid");
1907 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1908 error = got_error_from_errno("close");
1910 const struct got_error *close_err = got_repo_close(repo);
1914 TAILQ_FOREACH(pe, &refs, entry) {
1915 free((void *)pe->path);
1918 got_pathlist_free(&refs);
1919 TAILQ_FOREACH(pe, &symrefs, entry) {
1920 free((void *)pe->path);
1923 got_pathlist_free(&symrefs);
1924 got_pathlist_free(&wanted_branches);
1925 got_pathlist_free(&wanted_refs);
1932 free(default_destdir);
1937 static const struct got_error *
1938 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1939 int replace_tags, int verbosity, struct got_repository *repo)
1941 const struct got_error *err = NULL;
1942 char *new_id_str = NULL;
1943 struct got_object_id *old_id = NULL;
1945 err = got_object_id_str(&new_id_str, new_id);
1949 if (!replace_tags &&
1950 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1951 err = got_ref_resolve(&old_id, repo, ref);
1954 if (got_object_id_cmp(old_id, new_id) == 0)
1956 if (verbosity >= 0) {
1957 printf("Rejecting update of existing tag %s: %s\n",
1958 got_ref_get_name(ref), new_id_str);
1963 if (got_ref_is_symbolic(ref)) {
1964 if (verbosity >= 0) {
1965 printf("Replacing reference %s: %s\n",
1966 got_ref_get_name(ref),
1967 got_ref_get_symref_target(ref));
1969 err = got_ref_change_symref_to_ref(ref, new_id);
1972 err = got_ref_write(ref, repo);
1976 err = got_ref_resolve(&old_id, repo, ref);
1979 if (got_object_id_cmp(old_id, new_id) == 0)
1982 err = got_ref_change_ref(ref, new_id);
1985 err = got_ref_write(ref, repo);
1991 printf("Updated %s: %s\n", got_ref_get_name(ref),
1999 static const struct got_error *
2000 update_symref(const char *refname, struct got_reference *target_ref,
2001 int verbosity, struct got_repository *repo)
2003 const struct got_error *err = NULL, *unlock_err;
2004 struct got_reference *symref;
2005 int symref_is_locked = 0;
2007 err = got_ref_open(&symref, repo, refname, 1);
2009 if (err->code != GOT_ERR_NOT_REF)
2011 err = got_ref_alloc_symref(&symref, refname, target_ref);
2015 err = got_ref_write(symref, repo);
2020 printf("Created reference %s: %s\n",
2021 got_ref_get_name(symref),
2022 got_ref_get_symref_target(symref));
2024 symref_is_locked = 1;
2026 if (strcmp(got_ref_get_symref_target(symref),
2027 got_ref_get_name(target_ref)) == 0)
2030 err = got_ref_change_symref(symref,
2031 got_ref_get_name(target_ref));
2035 err = got_ref_write(symref, repo);
2040 printf("Updated %s: %s\n", got_ref_get_name(symref),
2041 got_ref_get_symref_target(symref));
2045 if (symref_is_locked) {
2046 unlock_err = got_ref_unlock(symref);
2047 if (unlock_err && err == NULL)
2050 got_ref_close(symref);
2057 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2058 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2059 "[remote-repository-name]\n",
2064 static const struct got_error *
2065 delete_missing_ref(struct got_reference *ref,
2066 int verbosity, struct got_repository *repo)
2068 const struct got_error *err = NULL;
2069 struct got_object_id *id = NULL;
2070 char *id_str = NULL;
2072 if (got_ref_is_symbolic(ref)) {
2073 err = got_ref_delete(ref, repo);
2076 if (verbosity >= 0) {
2077 printf("Deleted %s: %s\n",
2078 got_ref_get_name(ref),
2079 got_ref_get_symref_target(ref));
2082 err = got_ref_resolve(&id, repo, ref);
2085 err = got_object_id_str(&id_str, id);
2089 err = got_ref_delete(ref, repo);
2092 if (verbosity >= 0) {
2093 printf("Deleted %s: %s\n",
2094 got_ref_get_name(ref), id_str);
2103 static const struct got_error *
2104 delete_missing_refs(struct got_pathlist_head *their_refs,
2105 struct got_pathlist_head *their_symrefs,
2106 const struct got_remote_repo *remote,
2107 int verbosity, struct got_repository *repo)
2109 const struct got_error *err = NULL, *unlock_err;
2110 struct got_reflist_head my_refs;
2111 struct got_reflist_entry *re;
2112 struct got_pathlist_entry *pe;
2113 char *remote_namespace = NULL;
2114 char *local_refname = NULL;
2116 TAILQ_INIT(&my_refs);
2118 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2120 return got_error_from_errno("asprintf");
2122 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2126 TAILQ_FOREACH(re, &my_refs, entry) {
2127 const char *refname = got_ref_get_name(re->ref);
2128 const char *their_refname;
2130 if (remote->mirror_references) {
2131 their_refname = refname;
2133 if (strncmp(refname, remote_namespace,
2134 strlen(remote_namespace)) == 0) {
2135 if (strcmp(refname + strlen(remote_namespace),
2138 if (asprintf(&local_refname, "refs/heads/%s",
2139 refname + strlen(remote_namespace)) == -1) {
2140 err = got_error_from_errno("asprintf");
2143 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2146 their_refname = local_refname;
2149 TAILQ_FOREACH(pe, their_refs, entry) {
2150 if (strcmp(their_refname, pe->path) == 0)
2156 TAILQ_FOREACH(pe, their_symrefs, entry) {
2157 if (strcmp(their_refname, pe->path) == 0)
2163 err = delete_missing_ref(re->ref, verbosity, repo);
2167 if (local_refname) {
2168 struct got_reference *ref;
2169 err = got_ref_open(&ref, repo, local_refname, 1);
2171 if (err->code != GOT_ERR_NOT_REF)
2173 free(local_refname);
2174 local_refname = NULL;
2177 err = delete_missing_ref(ref, verbosity, repo);
2180 unlock_err = got_ref_unlock(ref);
2182 if (unlock_err && err == NULL) {
2187 free(local_refname);
2188 local_refname = NULL;
2192 free(remote_namespace);
2193 free(local_refname);
2197 static const struct got_error *
2198 update_wanted_ref(const char *refname, struct got_object_id *id,
2199 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2201 const struct got_error *err, *unlock_err;
2202 char *remote_refname;
2203 struct got_reference *ref;
2205 if (strncmp("refs/", refname, 5) == 0)
2208 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2209 remote_repo_name, refname) == -1)
2210 return got_error_from_errno("asprintf");
2212 err = got_ref_open(&ref, repo, remote_refname, 1);
2214 if (err->code != GOT_ERR_NOT_REF)
2216 err = create_ref(remote_refname, id, verbosity, repo);
2218 err = update_ref(ref, id, 0, verbosity, repo);
2219 unlock_err = got_ref_unlock(ref);
2220 if (unlock_err && err == NULL)
2225 free(remote_refname);
2229 static const struct got_error *
2230 delete_ref(struct got_repository *repo, struct got_reference *ref)
2232 const struct got_error *err = NULL;
2233 struct got_object_id *id = NULL;
2234 char *id_str = NULL;
2237 if (got_ref_is_symbolic(ref)) {
2238 target = got_ref_get_symref_target(ref);
2240 err = got_ref_resolve(&id, repo, ref);
2243 err = got_object_id_str(&id_str, id);
2249 err = got_ref_delete(ref, repo);
2253 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2260 static const struct got_error *
2261 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2263 const struct got_error *err = NULL;
2264 struct got_reflist_head refs;
2265 struct got_reflist_entry *re;
2270 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2271 err = got_error_from_errno("asprintf");
2274 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2278 TAILQ_FOREACH(re, &refs, entry)
2279 delete_ref(repo, re->ref);
2281 got_ref_list_free(&refs);
2285 static const struct got_error *
2286 cmd_fetch(int argc, char *argv[])
2288 const struct got_error *error = NULL, *unlock_err;
2289 char *cwd = NULL, *repo_path = NULL;
2290 const char *remote_name;
2291 char *proto = NULL, *host = NULL, *port = NULL;
2292 char *repo_name = NULL, *server_path = NULL;
2293 const struct got_remote_repo *remotes, *remote = NULL;
2295 char *id_str = NULL;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2299 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2300 struct got_pathlist_entry *pe;
2301 struct got_object_id *pack_hash = NULL;
2302 int i, ch, fetchfd = -1, fetchstatus;
2303 pid_t fetchpid = -1;
2304 struct got_fetch_progress_arg fpa;
2305 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2306 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2307 int *pack_fds = NULL;
2310 TAILQ_INIT(&symrefs);
2311 TAILQ_INIT(&wanted_branches);
2312 TAILQ_INIT(&wanted_refs);
2314 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2317 fetch_all_branches = 1;
2320 error = got_pathlist_append(&wanted_branches,
2332 repo_path = realpath(optarg, NULL);
2333 if (repo_path == NULL)
2334 return got_error_from_errno2("realpath",
2336 got_path_strip_trailing_slashes(repo_path);
2344 else if (verbosity < 3)
2351 error = got_pathlist_append(&wanted_refs,
2367 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2368 option_conflict('a', 'b');
2369 if (list_refs_only) {
2370 if (!TAILQ_EMPTY(&wanted_branches))
2371 option_conflict('l', 'b');
2372 if (fetch_all_branches)
2373 option_conflict('l', 'a');
2375 option_conflict('l', 'd');
2377 option_conflict('l', 'X');
2379 if (delete_remote) {
2380 if (fetch_all_branches)
2381 option_conflict('X', 'a');
2382 if (!TAILQ_EMPTY(&wanted_branches))
2383 option_conflict('X', 'b');
2385 option_conflict('X', 'd');
2387 option_conflict('X', 't');
2388 if (!TAILQ_EMPTY(&wanted_refs))
2389 option_conflict('X', 'R');
2394 errx(1, "-X option requires a remote name");
2395 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2396 } else if (argc == 1)
2397 remote_name = argv[0];
2401 cwd = getcwd(NULL, 0);
2403 error = got_error_from_errno("getcwd");
2407 error = got_repo_pack_fds_open(&pack_fds);
2411 if (repo_path == NULL) {
2412 error = got_worktree_open(&worktree, cwd);
2413 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2419 strdup(got_worktree_get_repo_path(worktree));
2420 if (repo_path == NULL)
2421 error = got_error_from_errno("strdup");
2425 repo_path = strdup(cwd);
2426 if (repo_path == NULL) {
2427 error = got_error_from_errno("strdup");
2433 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2437 if (delete_remote) {
2438 error = delete_refs_for_remote(repo, remote_name);
2439 goto done; /* nothing else to do */
2443 worktree_conf = got_worktree_get_gotconfig(worktree);
2444 if (worktree_conf) {
2445 got_gotconfig_get_remotes(&nremotes, &remotes,
2447 for (i = 0; i < nremotes; i++) {
2448 if (strcmp(remotes[i].name, remote_name) == 0) {
2449 remote = &remotes[i];
2455 if (remote == NULL) {
2456 repo_conf = got_repo_get_gotconfig(repo);
2458 got_gotconfig_get_remotes(&nremotes, &remotes,
2460 for (i = 0; i < nremotes; i++) {
2461 if (strcmp(remotes[i].name, remote_name) == 0) {
2462 remote = &remotes[i];
2468 if (remote == NULL) {
2469 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2470 for (i = 0; i < nremotes; i++) {
2471 if (strcmp(remotes[i].name, remote_name) == 0) {
2472 remote = &remotes[i];
2477 if (remote == NULL) {
2478 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2482 if (TAILQ_EMPTY(&wanted_branches)) {
2483 if (!fetch_all_branches)
2484 fetch_all_branches = remote->fetch_all_branches;
2485 for (i = 0; i < remote->nfetch_branches; i++) {
2486 got_pathlist_append(&wanted_branches,
2487 remote->fetch_branches[i], NULL);
2490 if (TAILQ_EMPTY(&wanted_refs)) {
2491 for (i = 0; i < remote->nfetch_refs; i++) {
2492 got_pathlist_append(&wanted_refs,
2493 remote->fetch_refs[i], NULL);
2497 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2498 &repo_name, remote->fetch_url);
2502 if (strcmp(proto, "git") == 0) {
2504 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2505 "sendfd dns inet unveil", NULL) == -1)
2508 } else if (strcmp(proto, "git+ssh") == 0 ||
2509 strcmp(proto, "ssh") == 0) {
2511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2512 "sendfd unveil", NULL) == -1)
2515 } else if (strcmp(proto, "http") == 0 ||
2516 strcmp(proto, "git+http") == 0) {
2517 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2520 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2524 error = got_dial_apply_unveil(proto);
2528 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2533 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2534 port ? ":" : "", port ? port : "");
2536 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2537 server_path, verbosity);
2541 fpa.last_scaled_size[0] = '\0';
2542 fpa.last_p_indexed = -1;
2543 fpa.last_p_resolved = -1;
2544 fpa.verbosity = verbosity;
2546 fpa.create_configs = 0;
2547 fpa.configs_created = 0;
2548 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2549 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2550 remote->mirror_references, fetch_all_branches, &wanted_branches,
2551 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2552 fetch_progress, &fpa);
2556 if (list_refs_only) {
2557 error = list_remote_refs(&symrefs, &refs);
2561 if (pack_hash == NULL) {
2563 printf("Already up-to-date\n");
2564 } else if (verbosity >= 0) {
2565 error = got_object_id_str(&id_str, pack_hash);
2568 printf("\nFetched %s.pack\n", id_str);
2573 /* Update references provided with the pack file. */
2574 TAILQ_FOREACH(pe, &refs, entry) {
2575 const char *refname = pe->path;
2576 struct got_object_id *id = pe->data;
2577 struct got_reference *ref;
2578 char *remote_refname;
2580 if (is_wanted_ref(&wanted_refs, refname) &&
2581 !remote->mirror_references) {
2582 error = update_wanted_ref(refname, id,
2583 remote->name, verbosity, repo);
2589 if (remote->mirror_references ||
2590 strncmp("refs/tags/", refname, 10) == 0) {
2591 error = got_ref_open(&ref, repo, refname, 1);
2593 if (error->code != GOT_ERR_NOT_REF)
2595 error = create_ref(refname, id, verbosity,
2600 error = update_ref(ref, id, replace_tags,
2602 unlock_err = got_ref_unlock(ref);
2603 if (unlock_err && error == NULL)
2609 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2610 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2611 remote_name, refname + 11) == -1) {
2612 error = got_error_from_errno("asprintf");
2616 error = got_ref_open(&ref, repo, remote_refname, 1);
2618 if (error->code != GOT_ERR_NOT_REF)
2620 error = create_ref(remote_refname, id,
2625 error = update_ref(ref, id, replace_tags,
2627 unlock_err = got_ref_unlock(ref);
2628 if (unlock_err && error == NULL)
2635 /* Also create a local branch if none exists yet. */
2636 error = got_ref_open(&ref, repo, refname, 1);
2638 if (error->code != GOT_ERR_NOT_REF)
2640 error = create_ref(refname, id, verbosity,
2645 unlock_err = got_ref_unlock(ref);
2646 if (unlock_err && error == NULL)
2653 error = delete_missing_refs(&refs, &symrefs, remote,
2659 if (!remote->mirror_references) {
2660 /* Update remote HEAD reference if the server provided one. */
2661 TAILQ_FOREACH(pe, &symrefs, entry) {
2662 struct got_reference *target_ref;
2663 const char *refname = pe->path;
2664 const char *target = pe->data;
2665 char *remote_refname = NULL, *remote_target = NULL;
2667 if (strcmp(refname, GOT_REF_HEAD) != 0)
2670 if (strncmp("refs/heads/", target, 11) != 0)
2673 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2674 remote->name, refname) == -1) {
2675 error = got_error_from_errno("asprintf");
2678 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2679 remote->name, target + 11) == -1) {
2680 error = got_error_from_errno("asprintf");
2681 free(remote_refname);
2685 error = got_ref_open(&target_ref, repo, remote_target,
2688 free(remote_refname);
2689 free(remote_target);
2690 if (error->code == GOT_ERR_NOT_REF) {
2696 error = update_symref(remote_refname, target_ref,
2698 free(remote_refname);
2699 free(remote_target);
2700 got_ref_close(target_ref);
2707 if (kill(fetchpid, SIGTERM) == -1)
2708 error = got_error_from_errno("kill");
2709 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2710 error = got_error_from_errno("waitpid");
2712 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2713 error = got_error_from_errno("close");
2715 const struct got_error *close_err = got_repo_close(repo);
2720 got_worktree_close(worktree);
2722 const struct got_error *pack_err =
2723 got_repo_pack_fds_close(pack_fds);
2727 TAILQ_FOREACH(pe, &refs, entry) {
2728 free((void *)pe->path);
2731 got_pathlist_free(&refs);
2732 TAILQ_FOREACH(pe, &symrefs, entry) {
2733 free((void *)pe->path);
2736 got_pathlist_free(&symrefs);
2737 got_pathlist_free(&wanted_branches);
2738 got_pathlist_free(&wanted_refs);
2753 usage_checkout(void)
2755 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2756 "[-p prefix] [-q] repository-path [worktree-path]\n",
2762 show_worktree_base_ref_warning(void)
2764 fprintf(stderr, "%s: warning: could not create a reference "
2765 "to the work tree's base commit; the commit could be "
2766 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2767 "repository writable and running 'got update' will prevent this\n",
2771 struct got_checkout_progress_arg {
2772 const char *worktree_path;
2773 int had_base_commit_ref_error;
2777 static const struct got_error *
2778 checkout_progress(void *arg, unsigned char status, const char *path)
2780 struct got_checkout_progress_arg *a = arg;
2782 /* Base commit bump happens silently. */
2783 if (status == GOT_STATUS_BUMP_BASE)
2786 if (status == GOT_STATUS_BASE_REF_ERR) {
2787 a->had_base_commit_ref_error = 1;
2791 while (path[0] == '/')
2794 if (a->verbosity >= 0)
2795 printf("%c %s/%s\n", status, a->worktree_path, path);
2800 static const struct got_error *
2801 check_cancelled(void *arg)
2803 if (sigint_received || sigpipe_received)
2804 return got_error(GOT_ERR_CANCELLED);
2808 static const struct got_error *
2809 check_linear_ancestry(struct got_object_id *commit_id,
2810 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2811 struct got_repository *repo)
2813 const struct got_error *err = NULL;
2814 struct got_object_id *yca_id;
2816 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2817 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2822 return got_error(GOT_ERR_ANCESTRY);
2825 * Require a straight line of history between the target commit
2826 * and the work tree's base commit.
2828 * Non-linear situations such as this require a rebase:
2830 * (commit) D F (base_commit)
2838 * 'got update' only handles linear cases:
2839 * Update forwards in time: A (base/yca) - B - C - D (commit)
2840 * Update backwards in time: D (base) - C - B - A (commit/yca)
2842 if (allow_forwards_in_time_only) {
2843 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2844 return got_error(GOT_ERR_ANCESTRY);
2845 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2846 got_object_id_cmp(base_commit_id, yca_id) != 0)
2847 return got_error(GOT_ERR_ANCESTRY);
2853 static const struct got_error *
2854 check_same_branch(struct got_object_id *commit_id,
2855 struct got_reference *head_ref, struct got_object_id *yca_id,
2856 struct got_repository *repo)
2858 const struct got_error *err = NULL;
2859 struct got_commit_graph *graph = NULL;
2860 struct got_object_id *head_commit_id = NULL;
2861 int is_same_branch = 0;
2863 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2867 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2871 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2876 err = got_commit_graph_open(&graph, "/", 1);
2880 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2881 check_cancelled, NULL);
2886 struct got_object_id *id;
2887 err = got_commit_graph_iter_next(&id, graph, repo,
2888 check_cancelled, NULL);
2890 if (err->code == GOT_ERR_ITER_COMPLETED)
2896 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2898 if (got_object_id_cmp(id, commit_id) == 0) {
2906 got_commit_graph_close(graph);
2907 free(head_commit_id);
2908 if (!err && !is_same_branch)
2909 err = got_error(GOT_ERR_ANCESTRY);
2913 static const struct got_error *
2914 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2916 static char msg[512];
2917 const char *branch_name;
2919 if (got_ref_is_symbolic(ref))
2920 branch_name = got_ref_get_symref_target(ref);
2922 branch_name = got_ref_get_name(ref);
2924 if (strncmp("refs/heads/", branch_name, 11) == 0)
2927 snprintf(msg, sizeof(msg),
2928 "target commit is not contained in branch '%s'; "
2929 "the branch to use must be specified with -b; "
2930 "if necessary a new branch can be created for "
2931 "this commit with 'got branch -c %s BRANCH_NAME'",
2932 branch_name, commit_id_str);
2934 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2937 static const struct got_error *
2938 cmd_checkout(int argc, char *argv[])
2940 const struct got_error *error = NULL;
2941 struct got_repository *repo = NULL;
2942 struct got_reference *head_ref = NULL, *ref = NULL;
2943 struct got_worktree *worktree = NULL;
2944 char *repo_path = NULL;
2945 char *worktree_path = NULL;
2946 const char *path_prefix = "";
2947 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2948 char *commit_id_str = NULL;
2949 struct got_object_id *commit_id = NULL;
2951 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2952 struct got_pathlist_head paths;
2953 struct got_checkout_progress_arg cpa;
2954 int *pack_fds = NULL;
2958 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2961 branch_name = optarg;
2964 commit_id_str = strdup(optarg);
2965 if (commit_id_str == NULL)
2966 return got_error_from_errno("strdup");
2972 path_prefix = optarg;
2987 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2988 "unveil", NULL) == -1)
2992 char *base, *dotgit;
2994 repo_path = realpath(argv[0], NULL);
2995 if (repo_path == NULL)
2996 return got_error_from_errno2("realpath", argv[0]);
2997 cwd = getcwd(NULL, 0);
2999 error = got_error_from_errno("getcwd");
3006 error = got_path_basename(&base, path);
3009 dotgit = strstr(base, ".git");
3012 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3013 error = got_error_from_errno("asprintf");
3018 } else if (argc == 2) {
3019 repo_path = realpath(argv[0], NULL);
3020 if (repo_path == NULL) {
3021 error = got_error_from_errno2("realpath", argv[0]);
3024 worktree_path = realpath(argv[1], NULL);
3025 if (worktree_path == NULL) {
3026 if (errno != ENOENT) {
3027 error = got_error_from_errno2("realpath",
3031 worktree_path = strdup(argv[1]);
3032 if (worktree_path == NULL) {
3033 error = got_error_from_errno("strdup");
3040 got_path_strip_trailing_slashes(repo_path);
3041 got_path_strip_trailing_slashes(worktree_path);
3043 error = got_repo_pack_fds_open(&pack_fds);
3047 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3051 /* Pre-create work tree path for unveil(2) */
3052 error = got_path_mkdir(worktree_path);
3054 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3055 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3057 if (!allow_nonempty &&
3058 !got_path_dir_is_empty(worktree_path)) {
3059 error = got_error_path(worktree_path,
3060 GOT_ERR_DIR_NOT_EMPTY);
3065 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3069 error = got_ref_open(&head_ref, repo, branch_name, 0);
3073 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3074 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3077 error = got_worktree_open(&worktree, worktree_path);
3081 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3085 if (!same_path_prefix) {
3086 error = got_error(GOT_ERR_PATH_PREFIX);
3090 if (commit_id_str) {
3091 struct got_reflist_head refs;
3093 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3097 error = got_repo_match_object_id(&commit_id, NULL,
3098 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3099 got_ref_list_free(&refs);
3102 error = check_linear_ancestry(commit_id,
3103 got_worktree_get_base_commit_id(worktree), 0, repo);
3104 if (error != NULL) {
3105 if (error->code == GOT_ERR_ANCESTRY) {
3106 error = checkout_ancestry_error(
3107 head_ref, commit_id_str);
3111 error = check_same_branch(commit_id, head_ref, NULL, repo);
3113 if (error->code == GOT_ERR_ANCESTRY) {
3114 error = checkout_ancestry_error(
3115 head_ref, commit_id_str);
3119 error = got_worktree_set_base_commit_id(worktree, repo,
3123 /* Expand potentially abbreviated commit ID string. */
3124 free(commit_id_str);
3125 error = got_object_id_str(&commit_id_str, commit_id);
3129 commit_id = got_object_id_dup(
3130 got_worktree_get_base_commit_id(worktree));
3131 if (commit_id == NULL) {
3132 error = got_error_from_errno("got_object_id_dup");
3135 error = got_object_id_str(&commit_id_str, commit_id);
3140 error = got_pathlist_append(&paths, "", NULL);
3143 cpa.worktree_path = worktree_path;
3144 cpa.had_base_commit_ref_error = 0;
3145 cpa.verbosity = verbosity;
3146 error = got_worktree_checkout_files(worktree, &paths, repo,
3147 checkout_progress, &cpa, check_cancelled, NULL);
3151 if (got_ref_is_symbolic(head_ref)) {
3152 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3155 refname = got_ref_get_name(ref);
3157 refname = got_ref_get_name(head_ref);
3158 printf("Checked out %s: %s\n", refname, commit_id_str);
3159 printf("Now shut up and hack\n");
3160 if (cpa.had_base_commit_ref_error)
3161 show_worktree_base_ref_warning();
3164 const struct got_error *pack_err =
3165 got_repo_pack_fds_close(pack_fds);
3170 got_ref_close(head_ref);
3173 got_pathlist_free(&paths);
3174 free(commit_id_str);
3177 free(worktree_path);
3182 struct got_update_progress_arg {
3194 print_update_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->not_updated > 0)
3205 printf("Files not updated because of existing merge "
3206 "conflicts: %d\n", upa->not_updated);
3210 * The meaning of some status codes differs between merge-style operations and
3211 * update operations. For example, the ! status code means "file was missing"
3212 * if changes were merged into the work tree, and "missing file was restored"
3213 * if the work tree was updated. This function should be used by any operation
3214 * which merges changes into the work tree without updating the work tree.
3217 print_merge_progress_stats(struct got_update_progress_arg *upa)
3219 if (!upa->did_something)
3222 if (upa->conflicts > 0)
3223 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3224 if (upa->obstructed > 0)
3225 printf("File paths obstructed by a non-regular file: %d\n",
3227 if (upa->missing > 0)
3228 printf("Files which had incoming changes but could not be "
3229 "found in the work tree: %d\n", upa->missing);
3230 if (upa->not_deleted > 0)
3231 printf("Files not deleted due to differences in deleted "
3232 "content: %d\n", upa->not_deleted);
3233 if (upa->unversioned > 0)
3234 printf("Files not merged because an unversioned file was "
3235 "found in the work tree: %d\n", upa->unversioned);
3241 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3247 static const struct got_error *
3248 update_progress(void *arg, unsigned char status, const char *path)
3250 struct got_update_progress_arg *upa = arg;
3252 if (status == GOT_STATUS_EXISTS ||
3253 status == GOT_STATUS_BASE_REF_ERR)
3256 upa->did_something = 1;
3258 /* Base commit bump happens silently. */
3259 if (status == GOT_STATUS_BUMP_BASE)
3262 if (status == GOT_STATUS_CONFLICT)
3264 if (status == GOT_STATUS_OBSTRUCTED)
3266 if (status == GOT_STATUS_CANNOT_UPDATE)
3268 if (status == GOT_STATUS_MISSING)
3270 if (status == GOT_STATUS_CANNOT_DELETE)
3272 if (status == GOT_STATUS_UNVERSIONED)
3275 while (path[0] == '/')
3277 if (upa->verbosity >= 0)
3278 printf("%c %s\n", status, path);
3283 static const struct got_error *
3284 switch_head_ref(struct got_reference *head_ref,
3285 struct got_object_id *commit_id, struct got_worktree *worktree,
3286 struct got_repository *repo)
3288 const struct got_error *err = NULL;
3290 int ref_has_moved = 0;
3292 /* Trivial case: switching between two different references. */
3293 if (strcmp(got_ref_get_name(head_ref),
3294 got_worktree_get_head_ref_name(worktree)) != 0) {
3295 printf("Switching work tree from %s to %s\n",
3296 got_worktree_get_head_ref_name(worktree),
3297 got_ref_get_name(head_ref));
3298 return got_worktree_set_head_ref(worktree, head_ref);
3301 err = check_linear_ancestry(commit_id,
3302 got_worktree_get_base_commit_id(worktree), 0, repo);
3304 if (err->code != GOT_ERR_ANCESTRY)
3311 /* Switching to a rebased branch with the same reference name. */
3312 err = got_object_id_str(&base_id_str,
3313 got_worktree_get_base_commit_id(worktree));
3316 printf("Reference %s now points at a different branch\n",
3317 got_worktree_get_head_ref_name(worktree));
3318 printf("Switching work tree from %s to %s\n", base_id_str,
3319 got_worktree_get_head_ref_name(worktree));
3323 static const struct got_error *
3324 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3326 const struct got_error *err;
3329 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3333 return got_error(GOT_ERR_REBASING);
3335 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3339 return got_error(GOT_ERR_HISTEDIT_BUSY);
3344 static const struct got_error *
3345 check_merge_in_progress(struct got_worktree *worktree,
3346 struct got_repository *repo)
3348 const struct got_error *err;
3351 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3355 return got_error(GOT_ERR_MERGE_BUSY);
3360 static const struct got_error *
3361 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3362 char *argv[], struct got_worktree *worktree)
3364 const struct got_error *err = NULL;
3366 struct got_pathlist_entry *new;
3372 return got_error_from_errno("strdup");
3373 return got_pathlist_append(paths, path, NULL);
3376 for (i = 0; i < argc; i++) {
3377 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3380 err = got_pathlist_insert(&new, paths, path, NULL);
3381 if (err || new == NULL /* duplicate */) {
3391 static const struct got_error *
3392 wrap_not_worktree_error(const struct got_error *orig_err,
3393 const char *cmdname, const char *path)
3395 const struct got_error *err;
3396 struct got_repository *repo;
3397 static char msg[512];
3398 int *pack_fds = NULL;
3400 err = got_repo_pack_fds_open(&pack_fds);
3404 err = got_repo_open(&repo, path, NULL, pack_fds);
3408 snprintf(msg, sizeof(msg),
3409 "'got %s' needs a work tree in addition to a git repository\n"
3410 "Work trees can be checked out from this Git repository with "
3412 "The got(1) manual page contains more information.", cmdname);
3413 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3414 got_repo_close(repo);
3416 const struct got_error *pack_err =
3417 got_repo_pack_fds_close(pack_fds);
3424 static const struct got_error *
3425 cmd_update(int argc, char *argv[])
3427 const struct got_error *error = NULL;
3428 struct got_repository *repo = NULL;
3429 struct got_worktree *worktree = NULL;
3430 char *worktree_path = NULL;
3431 struct got_object_id *commit_id = NULL;
3432 char *commit_id_str = NULL;
3433 const char *branch_name = NULL;
3434 struct got_reference *head_ref = NULL;
3435 struct got_pathlist_head paths;
3436 struct got_pathlist_entry *pe;
3437 int ch, verbosity = 0;
3438 struct got_update_progress_arg upa;
3439 int *pack_fds = NULL;
3443 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3446 branch_name = optarg;
3449 commit_id_str = strdup(optarg);
3450 if (commit_id_str == NULL)
3451 return got_error_from_errno("strdup");
3466 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3467 "unveil", NULL) == -1)
3470 worktree_path = getcwd(NULL, 0);
3471 if (worktree_path == NULL) {
3472 error = got_error_from_errno("getcwd");
3476 error = got_repo_pack_fds_open(&pack_fds);
3480 error = got_worktree_open(&worktree, worktree_path);
3482 if (error->code == GOT_ERR_NOT_WORKTREE)
3483 error = wrap_not_worktree_error(error, "update",
3488 error = check_rebase_or_histedit_in_progress(worktree);
3492 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3497 error = apply_unveil(got_repo_get_path(repo), 0,
3498 got_worktree_get_root_path(worktree));
3502 error = check_merge_in_progress(worktree, repo);
3506 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3510 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3511 got_worktree_get_head_ref_name(worktree), 0);
3514 if (commit_id_str == NULL) {
3515 error = got_ref_resolve(&commit_id, repo, head_ref);
3518 error = got_object_id_str(&commit_id_str, commit_id);
3522 struct got_reflist_head refs;
3524 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3528 error = got_repo_match_object_id(&commit_id, NULL,
3529 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3530 got_ref_list_free(&refs);
3531 free(commit_id_str);
3532 commit_id_str = NULL;
3535 error = got_object_id_str(&commit_id_str, commit_id);
3541 struct got_object_id *head_commit_id;
3542 TAILQ_FOREACH(pe, &paths, entry) {
3543 if (pe->path_len == 0)
3545 error = got_error_msg(GOT_ERR_BAD_PATH,
3546 "switching between branches requires that "
3547 "the entire work tree gets updated");
3550 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3553 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3555 free(head_commit_id);
3558 error = check_same_branch(commit_id, head_ref, NULL, repo);
3561 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3565 error = check_linear_ancestry(commit_id,
3566 got_worktree_get_base_commit_id(worktree), 0, repo);
3567 if (error != NULL) {
3568 if (error->code == GOT_ERR_ANCESTRY)
3569 error = got_error(GOT_ERR_BRANCH_MOVED);
3572 error = check_same_branch(commit_id, head_ref, NULL, repo);
3577 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3579 error = got_worktree_set_base_commit_id(worktree, repo,
3585 memset(&upa, 0, sizeof(upa));
3586 upa.verbosity = verbosity;
3587 error = got_worktree_checkout_files(worktree, &paths, repo,
3588 update_progress, &upa, check_cancelled, NULL);
3592 if (upa.did_something) {
3593 printf("Updated to %s: %s\n",
3594 got_worktree_get_head_ref_name(worktree), commit_id_str);
3596 printf("Already up-to-date\n");
3598 print_update_progress_stats(&upa);
3601 const struct got_error *pack_err =
3602 got_repo_pack_fds_close(pack_fds);
3606 free(worktree_path);
3607 TAILQ_FOREACH(pe, &paths, entry)
3608 free((char *)pe->path);
3609 got_pathlist_free(&paths);
3611 free(commit_id_str);
3615 static const struct got_error *
3616 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3617 const char *path, int diff_context, int ignore_whitespace,
3618 int force_text_diff, struct got_repository *repo, FILE *outfile)
3620 const struct got_error *err = NULL;
3621 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3622 FILE *f1 = NULL, *f2 = NULL;
3623 int fd1 = -1, fd2 = -1;
3625 fd1 = got_opentempfd();
3627 return got_error_from_errno("got_opentempfd");
3628 fd2 = got_opentempfd();
3630 err = got_error_from_errno("got_opentempfd");
3635 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3641 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3645 f1 = got_opentemp();
3647 err = got_error_from_errno("got_opentemp");
3650 f2 = got_opentemp();
3652 err = got_error_from_errno("got_opentemp");
3656 while (path[0] == '/')
3658 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3659 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3660 force_text_diff, outfile);
3662 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3663 err = got_error_from_errno("close");
3665 got_object_blob_close(blob1);
3666 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3667 err = got_error_from_errno("close");
3668 got_object_blob_close(blob2);
3669 if (f1 && fclose(f1) == EOF && err == NULL)
3670 err = got_error_from_errno("fclose");
3671 if (f2 && fclose(f2) == EOF && err == NULL)
3672 err = got_error_from_errno("fclose");
3676 static const struct got_error *
3677 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3678 const char *path, int diff_context, int ignore_whitespace,
3679 int force_text_diff, struct got_repository *repo, FILE *outfile)
3681 const struct got_error *err = NULL;
3682 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3683 struct got_diff_blob_output_unidiff_arg arg;
3684 FILE *f1 = NULL, *f2 = NULL;
3685 int fd1 = -1, fd2 = -1;
3688 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3691 fd1 = got_opentempfd();
3693 err = got_error_from_errno("got_opentempfd");
3698 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3702 f1 = got_opentemp();
3704 err = got_error_from_errno("got_opentemp");
3708 f2 = got_opentemp();
3710 err = got_error_from_errno("got_opentemp");
3713 fd2 = got_opentempfd();
3715 err = got_error_from_errno("got_opentempfd");
3718 arg.diff_context = diff_context;
3719 arg.ignore_whitespace = ignore_whitespace;
3720 arg.force_text_diff = force_text_diff;
3721 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3722 arg.outfile = outfile;
3723 arg.line_offsets = NULL;
3725 while (path[0] == '/')
3727 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3728 got_diff_blob_output_unidiff, &arg, 1);
3731 got_object_tree_close(tree1);
3733 got_object_tree_close(tree2);
3734 if (f1 && fclose(f1) == EOF && err == NULL)
3735 err = got_error_from_errno("fclose");
3736 if (f2 && fclose(f2) == EOF && err == NULL)
3737 err = got_error_from_errno("fclose");
3738 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3739 err = got_error_from_errno("close");
3740 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3741 err = got_error_from_errno("close");
3745 static const struct got_error *
3746 get_changed_paths(struct got_pathlist_head *paths,
3747 struct got_commit_object *commit, struct got_repository *repo)
3749 const struct got_error *err = NULL;
3750 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3751 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3752 struct got_object_qid *qid;
3754 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3756 struct got_commit_object *pcommit;
3757 err = got_object_open_as_commit(&pcommit, repo,
3762 tree_id1 = got_object_id_dup(
3763 got_object_commit_get_tree_id(pcommit));
3764 if (tree_id1 == NULL) {
3765 got_object_commit_close(pcommit);
3766 return got_error_from_errno("got_object_id_dup");
3768 got_object_commit_close(pcommit);
3773 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3778 tree_id2 = got_object_commit_get_tree_id(commit);
3779 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3783 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3784 got_diff_tree_collect_changed_paths, paths, 0);
3787 got_object_tree_close(tree1);
3789 got_object_tree_close(tree2);
3794 static const struct got_error *
3795 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3796 const char *path, int diff_context, struct got_repository *repo,
3799 const struct got_error *err = NULL;
3800 struct got_commit_object *pcommit = NULL;
3801 char *id_str1 = NULL, *id_str2 = NULL;
3802 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3803 struct got_object_qid *qid;
3805 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3807 err = got_object_open_as_commit(&pcommit, repo,
3811 err = got_object_id_str(&id_str1, &qid->id);
3816 err = got_object_id_str(&id_str2, id);
3820 if (path && path[0] != '\0') {
3822 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3826 err = got_object_id_by_path(&obj_id1, repo,
3829 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3835 err = got_object_get_type(&obj_type, repo, obj_id2);
3841 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3842 fprintf(outfile, "commit - %s\n",
3843 id_str1 ? id_str1 : "/dev/null");
3844 fprintf(outfile, "commit + %s\n", id_str2);
3846 case GOT_OBJ_TYPE_BLOB:
3847 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3848 0, 0, repo, outfile);
3850 case GOT_OBJ_TYPE_TREE:
3851 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3852 0, 0, repo, outfile);
3855 err = got_error(GOT_ERR_OBJ_TYPE);
3861 obj_id2 = got_object_commit_get_tree_id(commit);
3863 obj_id1 = got_object_commit_get_tree_id(pcommit);
3865 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3866 fprintf(outfile, "commit - %s\n",
3867 id_str1 ? id_str1 : "/dev/null");
3868 fprintf(outfile, "commit + %s\n", id_str2);
3869 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3876 got_object_commit_close(pcommit);
3881 get_datestr(time_t *time, char *datebuf)
3883 struct tm mytm, *tm;
3886 tm = gmtime_r(time, &mytm);
3889 s = asctime_r(tm, datebuf);
3892 p = strchr(s, '\n');
3898 static const struct got_error *
3899 match_commit(int *have_match, struct got_object_id *id,
3900 struct got_commit_object *commit, regex_t *regex)
3902 const struct got_error *err = NULL;
3903 regmatch_t regmatch;
3904 char *id_str = NULL, *logmsg = NULL;
3908 err = got_object_id_str(&id_str, id);
3912 err = got_object_commit_get_logmsg(&logmsg, commit);
3916 if (regexec(regex, got_object_commit_get_author(commit), 1,
3917 ®match, 0) == 0 ||
3918 regexec(regex, got_object_commit_get_committer(commit), 1,
3919 ®match, 0) == 0 ||
3920 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3921 regexec(regex, logmsg, 1, ®match, 0) == 0)
3930 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3933 regmatch_t regmatch;
3934 struct got_pathlist_entry *pe;
3938 TAILQ_FOREACH(pe, changed_paths, entry) {
3939 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3946 static const struct got_error *
3947 match_patch(int *have_match, struct got_commit_object *commit,
3948 struct got_object_id *id, const char *path, int diff_context,
3949 struct got_repository *repo, regex_t *regex, FILE *f)
3951 const struct got_error *err = NULL;
3953 size_t linesize = 0;
3955 regmatch_t regmatch;
3959 err = got_opentemp_truncate(f);
3963 err = print_patch(commit, id, path, diff_context, repo, f);
3967 if (fseeko(f, 0L, SEEK_SET) == -1) {
3968 err = got_error_from_errno("fseeko");
3972 while ((linelen = getline(&line, &linesize, f)) != -1) {
3973 if (regexec(regex, line, 1, ®match, 0) == 0) {
3983 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3985 static const struct got_error*
3986 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3987 struct got_object_id *id, struct got_repository *repo,
3990 static const struct got_error *err = NULL;
3991 struct got_reflist_entry *re;
3997 TAILQ_FOREACH(re, refs, entry) {
3998 struct got_tag_object *tag = NULL;
3999 struct got_object_id *ref_id;
4002 name = got_ref_get_name(re->ref);
4003 if (strcmp(name, GOT_REF_HEAD) == 0)
4005 if (strncmp(name, "refs/", 5) == 0)
4007 if (strncmp(name, "got/", 4) == 0)
4009 if (strncmp(name, "heads/", 6) == 0)
4011 if (strncmp(name, "remotes/", 8) == 0) {
4015 s = strstr(name, "/" GOT_REF_HEAD);
4016 if (s != NULL && s[strlen(s)] == '\0')
4019 err = got_ref_resolve(&ref_id, repo, re->ref);
4022 if (strncmp(name, "tags/", 5) == 0) {
4023 err = got_object_open_as_tag(&tag, repo, ref_id);
4025 if (err->code != GOT_ERR_OBJ_TYPE) {
4029 /* Ref points at something other than a tag. */
4034 cmp = got_object_id_cmp(tag ?
4035 got_object_tag_get_object_id(tag) : ref_id, id);
4038 got_object_tag_close(tag);
4042 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4043 s ? ", " : "", name) == -1) {
4044 err = got_error_from_errno("asprintf");
4055 static const struct got_error *
4056 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4057 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4059 const struct got_error *err = NULL;
4060 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4061 char *comma, *s, *nl;
4062 struct got_reflist_head *refs;
4063 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4065 time_t committer_time;
4067 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4069 err = build_refs_str(&ref_str, refs, id, repo, 1);
4073 /* Display the first matching ref only. */
4074 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4078 if (ref_str == NULL) {
4079 err = got_object_id_str(&id_str, id);
4084 committer_time = got_object_commit_get_committer_time(commit);
4085 if (gmtime_r(&committer_time, &tm) == NULL) {
4086 err = got_error_from_errno("gmtime_r");
4089 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4090 err = got_error(GOT_ERR_NO_SPACE);
4094 err = got_object_commit_get_logmsg(&logmsg0, commit);
4099 while (isspace((unsigned char)s[0]))
4102 nl = strchr(s, '\n');
4108 printf("%s%-7s %s\n", datebuf, ref_str, s);
4110 printf("%s%.7s %s\n", datebuf, id_str, s);
4112 if (fflush(stdout) != 0 && err == NULL)
4113 err = got_error_from_errno("fflush");
4121 static const struct got_error *
4122 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4123 struct got_repository *repo, const char *path,
4124 struct got_pathlist_head *changed_paths, int show_patch,
4125 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4126 const char *custom_refs_str)
4128 const struct got_error *err = NULL;
4129 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4131 time_t committer_time;
4132 const char *author, *committer;
4133 char *refs_str = NULL;
4135 err = got_object_id_str(&id_str, id);
4139 if (custom_refs_str == NULL) {
4140 struct got_reflist_head *refs;
4141 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4143 err = build_refs_str(&refs_str, refs, id, repo, 0);
4149 printf(GOT_COMMIT_SEP_STR);
4150 if (custom_refs_str)
4151 printf("commit %s (%s)\n", id_str, custom_refs_str);
4153 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4154 refs_str ? refs_str : "", refs_str ? ")" : "");
4159 printf("from: %s\n", got_object_commit_get_author(commit));
4160 committer_time = got_object_commit_get_committer_time(commit);
4161 datestr = get_datestr(&committer_time, datebuf);
4163 printf("date: %s UTC\n", datestr);
4164 author = got_object_commit_get_author(commit);
4165 committer = got_object_commit_get_committer(commit);
4166 if (strcmp(author, committer) != 0)
4167 printf("via: %s\n", committer);
4168 if (got_object_commit_get_nparents(commit) > 1) {
4169 const struct got_object_id_queue *parent_ids;
4170 struct got_object_qid *qid;
4172 parent_ids = got_object_commit_get_parent_ids(commit);
4173 STAILQ_FOREACH(qid, parent_ids, entry) {
4174 err = got_object_id_str(&id_str, &qid->id);
4177 printf("parent %d: %s\n", n++, id_str);
4183 err = got_object_commit_get_logmsg(&logmsg0, commit);
4189 line = strsep(&logmsg, "\n");
4191 printf(" %s\n", line);
4195 if (changed_paths) {
4196 struct got_pathlist_entry *pe;
4197 TAILQ_FOREACH(pe, changed_paths, entry) {
4198 struct got_diff_changed_path *cp = pe->data;
4199 printf(" %c %s\n", cp->status, pe->path);
4204 err = print_patch(commit, id, path, diff_context, repo, stdout);
4209 if (fflush(stdout) != 0 && err == NULL)
4210 err = got_error_from_errno("fflush");
4217 static const struct got_error *
4218 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4219 struct got_repository *repo, const char *path, int show_changed_paths,
4220 int show_patch, const char *search_pattern, int diff_context, int limit,
4221 int log_branches, int reverse_display_order,
4222 struct got_reflist_object_id_map *refs_idmap, int one_line,
4225 const struct got_error *err;
4226 struct got_commit_graph *graph;
4229 struct got_object_id_queue reversed_commits;
4230 struct got_object_qid *qid;
4231 struct got_commit_object *commit;
4232 struct got_pathlist_head changed_paths;
4233 struct got_pathlist_entry *pe;
4235 STAILQ_INIT(&reversed_commits);
4236 TAILQ_INIT(&changed_paths);
4238 if (search_pattern && regcomp(®ex, search_pattern,
4239 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4240 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4242 err = got_commit_graph_open(&graph, path, !log_branches);
4245 err = got_commit_graph_iter_start(graph, root_id, repo,
4246 check_cancelled, NULL);
4250 struct got_object_id *id;
4252 if (sigint_received || sigpipe_received)
4255 err = got_commit_graph_iter_next(&id, graph, repo,
4256 check_cancelled, NULL);
4258 if (err->code == GOT_ERR_ITER_COMPLETED)
4265 err = got_object_open_as_commit(&commit, repo, id);
4269 if (show_changed_paths && !reverse_display_order) {
4270 err = get_changed_paths(&changed_paths, commit, repo);
4275 if (search_pattern) {
4276 err = match_commit(&have_match, id, commit, ®ex);
4278 got_object_commit_close(commit);
4281 if (have_match == 0 && show_changed_paths)
4282 match_changed_paths(&have_match,
4283 &changed_paths, ®ex);
4284 if (have_match == 0 && show_patch) {
4285 err = match_patch(&have_match, commit, id,
4286 path, diff_context, repo, ®ex,
4291 if (have_match == 0) {
4292 got_object_commit_close(commit);
4293 TAILQ_FOREACH(pe, &changed_paths, entry) {
4294 free((char *)pe->path);
4297 got_pathlist_free(&changed_paths);
4302 if (reverse_display_order) {
4303 err = got_object_qid_alloc(&qid, id);
4306 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4307 got_object_commit_close(commit);
4310 err = print_commit_oneline(commit, id,
4313 err = print_commit(commit, id, repo, path,
4314 show_changed_paths ? &changed_paths : NULL,
4315 show_patch, diff_context, refs_idmap, NULL);
4316 got_object_commit_close(commit);
4320 if ((limit && --limit == 0) ||
4321 (end_id && got_object_id_cmp(id, end_id) == 0))
4324 TAILQ_FOREACH(pe, &changed_paths, entry) {
4325 free((char *)pe->path);
4328 got_pathlist_free(&changed_paths);
4330 if (reverse_display_order) {
4331 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4332 err = got_object_open_as_commit(&commit, repo,
4336 if (show_changed_paths) {
4337 err = get_changed_paths(&changed_paths,
4343 err = print_commit_oneline(commit, &qid->id,
4346 err = print_commit(commit, &qid->id, repo, path,
4347 show_changed_paths ? &changed_paths : NULL,
4348 show_patch, diff_context, refs_idmap, NULL);
4349 got_object_commit_close(commit);
4352 TAILQ_FOREACH(pe, &changed_paths, entry) {
4353 free((char *)pe->path);
4356 got_pathlist_free(&changed_paths);
4360 while (!STAILQ_EMPTY(&reversed_commits)) {
4361 qid = STAILQ_FIRST(&reversed_commits);
4362 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4363 got_object_qid_free(qid);
4365 TAILQ_FOREACH(pe, &changed_paths, entry) {
4366 free((char *)pe->path);
4369 got_pathlist_free(&changed_paths);
4372 got_commit_graph_close(graph);
4379 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4380 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4381 "[-r repository-path] [-R] [path]\n", getprogname());
4386 get_default_log_limit(void)
4388 const char *got_default_log_limit;
4392 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4393 if (got_default_log_limit == NULL)
4395 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4401 static const struct got_error *
4402 cmd_log(int argc, char *argv[])
4404 const struct got_error *error;
4405 struct got_repository *repo = NULL;
4406 struct got_worktree *worktree = NULL;
4407 struct got_object_id *start_id = NULL, *end_id = NULL;
4408 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4409 const char *start_commit = NULL, *end_commit = NULL;
4410 const char *search_pattern = NULL;
4411 int diff_context = -1, ch;
4412 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4413 int reverse_display_order = 0, one_line = 0;
4415 struct got_reflist_head refs;
4416 struct got_reflist_object_id_map *refs_idmap = NULL;
4417 FILE *tmpfile = NULL;
4418 int *pack_fds = NULL;
4423 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4429 limit = get_default_log_limit();
4431 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4437 show_changed_paths = 1;
4440 start_commit = optarg;
4443 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4446 errx(1, "number of context lines is %s: %s",
4450 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4452 errx(1, "number of commits is %s: %s",
4459 repo_path = realpath(optarg, NULL);
4460 if (repo_path == NULL)
4461 return got_error_from_errno2("realpath",
4463 got_path_strip_trailing_slashes(repo_path);
4466 reverse_display_order = 1;
4472 search_pattern = optarg;
4475 end_commit = optarg;
4486 if (diff_context == -1)
4488 else if (!show_patch)
4489 errx(1, "-C requires -p");
4491 if (one_line && (show_patch || show_changed_paths))
4492 errx(1, "cannot use -s with -p or -P");
4494 cwd = getcwd(NULL, 0);
4496 error = got_error_from_errno("getcwd");
4500 error = got_repo_pack_fds_open(&pack_fds);
4504 if (repo_path == NULL) {
4505 error = got_worktree_open(&worktree, cwd);
4506 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4513 error = got_worktree_resolve_path(&path, worktree,
4518 path = strdup(argv[0]);
4520 error = got_error_from_errno("strdup");
4524 } else if (argc != 0)
4527 if (repo_path == NULL) {
4528 repo_path = worktree ?
4529 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4531 if (repo_path == NULL) {
4532 error = got_error_from_errno("strdup");
4536 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4540 error = apply_unveil(got_repo_get_path(repo), 1,
4541 worktree ? got_worktree_get_root_path(worktree) : NULL);
4545 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4549 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4553 if (start_commit == NULL) {
4554 struct got_reference *head_ref;
4555 struct got_commit_object *commit = NULL;
4556 error = got_ref_open(&head_ref, repo,
4557 worktree ? got_worktree_get_head_ref_name(worktree)
4561 error = got_ref_resolve(&start_id, repo, head_ref);
4562 got_ref_close(head_ref);
4565 error = got_object_open_as_commit(&commit, repo,
4569 got_object_commit_close(commit);
4571 error = got_repo_match_object_id(&start_id, NULL,
4572 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4576 if (end_commit != NULL) {
4577 error = got_repo_match_object_id(&end_id, NULL,
4578 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4585 * If a path was specified on the command line it was resolved
4586 * to a path in the work tree above. Prepend the work tree's
4587 * path prefix to obtain the corresponding in-repository path.
4591 prefix = got_worktree_get_path_prefix(worktree);
4592 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4593 (path[0] != '\0') ? "/" : "", path) == -1) {
4594 error = got_error_from_errno("asprintf");
4599 error = got_repo_map_path(&in_repo_path, repo,
4605 path = in_repo_path;
4609 /* Release work tree lock. */
4610 got_worktree_close(worktree);
4614 if (search_pattern && show_patch) {
4615 tmpfile = got_opentemp();
4616 if (tmpfile == NULL) {
4617 error = got_error_from_errno("got_opentemp");
4622 error = print_commits(start_id, end_id, repo, path ? path : "",
4623 show_changed_paths, show_patch, search_pattern, diff_context,
4624 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4631 got_worktree_close(worktree);
4633 const struct got_error *close_err = got_repo_close(repo);
4638 const struct got_error *pack_err =
4639 got_repo_pack_fds_close(pack_fds);
4644 got_reflist_object_id_map_free(refs_idmap);
4645 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4646 error = got_error_from_errno("fclose");
4647 got_ref_list_free(&refs);
4654 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4655 "[-r repository-path] [-s] [-w] [-P] "
4656 "[object1 object2 | path ...]\n", getprogname());
4660 struct print_diff_arg {
4661 struct got_repository *repo;
4662 struct got_worktree *worktree;
4667 enum got_diff_algorithm diff_algo;
4668 int ignore_whitespace;
4669 int force_text_diff;
4675 * Create a file which contains the target path of a symlink so we can feed
4676 * it as content to the diff engine.
4678 static const struct got_error *
4679 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4680 const char *abspath)
4682 const struct got_error *err = NULL;
4683 char target_path[PATH_MAX];
4684 ssize_t target_len, outlen;
4689 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4690 if (target_len == -1)
4691 return got_error_from_errno2("readlinkat", abspath);
4693 target_len = readlink(abspath, target_path, PATH_MAX);
4694 if (target_len == -1)
4695 return got_error_from_errno2("readlink", abspath);
4698 *fd = got_opentempfd();
4700 return got_error_from_errno("got_opentempfd");
4702 outlen = write(*fd, target_path, target_len);
4704 err = got_error_from_errno("got_opentempfd");
4708 if (lseek(*fd, 0, SEEK_SET) == -1) {
4709 err = got_error_from_errno2("lseek", abspath);
4720 static const struct got_error *
4721 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4722 const char *path, struct got_object_id *blob_id,
4723 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4724 int dirfd, const char *de_name)
4726 struct print_diff_arg *a = arg;
4727 const struct got_error *err = NULL;
4728 struct got_blob_object *blob1 = NULL;
4729 int fd = -1, fd1 = -1, fd2 = -1;
4731 char *abspath = NULL, *label1 = NULL;
4736 if (a->diff_staged) {
4737 if (staged_status != GOT_STATUS_MODIFY &&
4738 staged_status != GOT_STATUS_ADD &&
4739 staged_status != GOT_STATUS_DELETE)
4742 if (staged_status == GOT_STATUS_DELETE)
4744 if (status == GOT_STATUS_NONEXISTENT)
4745 return got_error_set_errno(ENOENT, path);
4746 if (status != GOT_STATUS_MODIFY &&
4747 status != GOT_STATUS_ADD &&
4748 status != GOT_STATUS_DELETE &&
4749 status != GOT_STATUS_CONFLICT)
4753 err = got_opentemp_truncate(a->f1);
4755 return got_error_from_errno("got_opentemp_truncate");
4756 err = got_opentemp_truncate(a->f2);
4758 return got_error_from_errno("got_opentemp_truncate");
4760 if (!a->header_shown) {
4761 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4762 got_worktree_get_root_path(a->worktree));
4763 printf("commit - %s\n", a->id_str);
4764 printf("path + %s%s\n",
4765 got_worktree_get_root_path(a->worktree),
4766 a->diff_staged ? " (staged changes)" : "");
4767 a->header_shown = 1;
4770 if (a->diff_staged) {
4771 const char *label1 = NULL, *label2 = NULL;
4772 switch (staged_status) {
4773 case GOT_STATUS_MODIFY:
4777 case GOT_STATUS_ADD:
4780 case GOT_STATUS_DELETE:
4784 return got_error(GOT_ERR_FILE_STATUS);
4786 fd1 = got_opentempfd();
4788 err = got_error_from_errno("got_opentempfd");
4791 fd2 = got_opentempfd();
4793 err = got_error_from_errno("got_opentempfd");
4796 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4797 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4798 a->diff_algo, a->diff_context, a->ignore_whitespace,
4799 a->force_text_diff, a->repo, stdout);
4803 fd1 = got_opentempfd();
4805 err = got_error_from_errno("got_opentempfd");
4809 if (staged_status == GOT_STATUS_ADD ||
4810 staged_status == GOT_STATUS_MODIFY) {
4812 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4816 err = got_object_id_str(&id_str, staged_blob_id);
4819 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4820 err = got_error_from_errno("asprintf");
4825 } else if (status != GOT_STATUS_ADD) {
4826 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4832 if (status != GOT_STATUS_DELETE) {
4833 if (asprintf(&abspath, "%s/%s",
4834 got_worktree_get_root_path(a->worktree), path) == -1) {
4835 err = got_error_from_errno("asprintf");
4840 fd = openat(dirfd, de_name,
4841 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4843 if (!got_err_open_nofollow_on_symlink()) {
4844 err = got_error_from_errno2("openat",
4848 err = get_symlink_target_file(&fd, dirfd,
4854 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4856 if (!got_err_open_nofollow_on_symlink()) {
4857 err = got_error_from_errno2("open",
4861 err = get_symlink_target_file(&fd, dirfd,
4867 if (fstat(fd, &sb) == -1) {
4868 err = got_error_from_errno2("fstat", abspath);
4871 f2 = fdopen(fd, "r");
4873 err = got_error_from_errno2("fdopen", abspath);
4883 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4889 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4890 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4891 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4893 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4894 err = got_error_from_errno("close");
4895 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4896 err = got_error_from_errno("close");
4898 got_object_blob_close(blob1);
4899 if (fd != -1 && close(fd) == -1 && err == NULL)
4900 err = got_error_from_errno("close");
4901 if (f2 && fclose(f2) == EOF && err == NULL)
4902 err = got_error_from_errno("fclose");
4907 static const struct got_error *
4908 cmd_diff(int argc, char *argv[])
4910 const struct got_error *error;
4911 struct got_repository *repo = NULL;
4912 struct got_worktree *worktree = NULL;
4913 char *cwd = NULL, *repo_path = NULL;
4914 const char *commit_args[2] = { NULL, NULL };
4915 int ncommit_args = 0;
4916 struct got_object_id *ids[2] = { NULL, NULL };
4917 char *labels[2] = { NULL, NULL };
4918 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4919 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4920 int force_text_diff = 0, force_path = 0, rflag = 0;
4922 struct got_reflist_head refs;
4923 struct got_pathlist_head paths;
4924 struct got_pathlist_entry *pe;
4925 FILE *f1 = NULL, *f2 = NULL;
4926 int fd1 = -1, fd2 = -1;
4927 int *pack_fds = NULL;
4933 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4938 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4941 force_text_diff = 1;
4944 if (ncommit_args >= 2)
4945 errx(1, "too many -c options used");
4946 commit_args[ncommit_args++] = optarg;
4949 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4952 errx(1, "number of context lines is %s: %s",
4956 repo_path = realpath(optarg, NULL);
4957 if (repo_path == NULL)
4958 return got_error_from_errno2("realpath",
4960 got_path_strip_trailing_slashes(repo_path);
4967 ignore_whitespace = 1;
4981 cwd = getcwd(NULL, 0);
4983 error = got_error_from_errno("getcwd");
4987 error = got_repo_pack_fds_open(&pack_fds);
4991 if (repo_path == NULL) {
4992 error = got_worktree_open(&worktree, cwd);
4993 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4999 strdup(got_worktree_get_repo_path(worktree));
5000 if (repo_path == NULL) {
5001 error = got_error_from_errno("strdup");
5005 repo_path = strdup(cwd);
5006 if (repo_path == NULL) {
5007 error = got_error_from_errno("strdup");
5013 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5018 if (rflag || worktree == NULL || ncommit_args > 0) {
5020 error = got_error_msg(GOT_ERR_NOT_IMPL,
5021 "-P option can only be used when diffing "
5026 error = got_error_msg(GOT_ERR_NOT_IMPL,
5027 "-s option can only be used when diffing "
5033 error = apply_unveil(got_repo_get_path(repo), 1,
5034 worktree ? got_worktree_get_root_path(worktree) : NULL);
5038 if ((!force_path && argc == 2) || ncommit_args > 0) {
5039 int obj_type = (ncommit_args > 0 ?
5040 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5041 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5045 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5047 if (ncommit_args > 0)
5048 arg = commit_args[i];
5051 error = got_repo_match_object_id(&ids[i], &labels[i],
5052 arg, obj_type, &refs, repo);
5054 if (error->code != GOT_ERR_NOT_REF &&
5055 error->code != GOT_ERR_NO_OBJ)
5057 if (ncommit_args > 0)
5065 f1 = got_opentemp();
5067 error = got_error_from_errno("got_opentemp");
5071 f2 = got_opentemp();
5073 error = got_error_from_errno("got_opentemp");
5077 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5078 struct print_diff_arg arg;
5081 if (worktree == NULL) {
5082 if (argc == 2 && ids[0] == NULL) {
5083 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5085 } else if (argc == 2 && ids[1] == NULL) {
5086 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5088 } else if (argc > 0) {
5089 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5090 "%s", "specified paths cannot be resolved");
5093 error = got_error(GOT_ERR_NOT_WORKTREE);
5098 error = get_worktree_paths_from_argv(&paths, argc, argv,
5103 error = got_object_id_str(&id_str,
5104 got_worktree_get_base_commit_id(worktree));
5108 arg.worktree = worktree;
5109 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5110 arg.diff_context = diff_context;
5111 arg.id_str = id_str;
5112 arg.header_shown = 0;
5113 arg.diff_staged = diff_staged;
5114 arg.ignore_whitespace = ignore_whitespace;
5115 arg.force_text_diff = force_text_diff;
5119 error = got_worktree_status(worktree, &paths, repo, 0,
5120 print_diff, &arg, check_cancelled, NULL);
5125 if (ncommit_args == 1) {
5126 struct got_commit_object *commit;
5127 error = got_object_open_as_commit(&commit, repo, ids[0]);
5131 labels[1] = labels[0];
5133 if (got_object_commit_get_nparents(commit) > 0) {
5134 const struct got_object_id_queue *pids;
5135 struct got_object_qid *pid;
5136 pids = got_object_commit_get_parent_ids(commit);
5137 pid = STAILQ_FIRST(pids);
5138 ids[0] = got_object_id_dup(&pid->id);
5139 if (ids[0] == NULL) {
5140 error = got_error_from_errno(
5141 "got_object_id_dup");
5142 got_object_commit_close(commit);
5145 error = got_object_id_str(&labels[0], ids[0]);
5147 got_object_commit_close(commit);
5152 labels[0] = strdup("/dev/null");
5153 if (labels[0] == NULL) {
5154 error = got_error_from_errno("strdup");
5155 got_object_commit_close(commit);
5160 got_object_commit_close(commit);
5163 if (ncommit_args == 0 && argc > 2) {
5164 error = got_error_msg(GOT_ERR_BAD_PATH,
5165 "path arguments cannot be used when diffing two objects");
5170 error = got_object_get_type(&type1, repo, ids[0]);
5175 error = got_object_get_type(&type2, repo, ids[1]);
5178 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5179 error = got_error(GOT_ERR_OBJ_TYPE);
5182 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5183 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5184 "path arguments cannot be used when diffing blobs");
5188 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5190 struct got_pathlist_entry *new;
5194 error = got_worktree_resolve_path(&p, worktree,
5198 prefix = got_worktree_get_path_prefix(worktree);
5199 while (prefix[0] == '/')
5201 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5202 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5204 error = got_error_from_errno("asprintf");
5210 char *mapped_path, *s;
5211 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5217 in_repo_path = strdup(s);
5218 if (in_repo_path == NULL) {
5219 error = got_error_from_errno("asprintf");
5226 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5227 if (error || new == NULL /* duplicate */)
5234 /* Release work tree lock. */
5235 got_worktree_close(worktree);
5239 fd1 = got_opentempfd();
5241 error = got_error_from_errno("got_opentempfd");
5245 fd2 = got_opentempfd();
5247 error = got_error_from_errno("got_opentempfd");
5251 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5252 case GOT_OBJ_TYPE_BLOB:
5253 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5254 fd1, fd2, ids[0], ids[1], NULL, NULL,
5255 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5256 ignore_whitespace, force_text_diff, repo, stdout);
5258 case GOT_OBJ_TYPE_TREE:
5259 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5260 ids[0], ids[1], &paths, "", "",
5261 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5262 ignore_whitespace, force_text_diff, repo, stdout);
5264 case GOT_OBJ_TYPE_COMMIT:
5265 printf("diff %s %s\n", labels[0], labels[1]);
5266 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5267 fd1, fd2, ids[0], ids[1], &paths,
5268 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5269 ignore_whitespace, force_text_diff, repo, stdout);
5272 error = got_error(GOT_ERR_OBJ_TYPE);
5280 got_worktree_close(worktree);
5282 const struct got_error *close_err = got_repo_close(repo);
5287 const struct got_error *pack_err =
5288 got_repo_pack_fds_close(pack_fds);
5292 TAILQ_FOREACH(pe, &paths, entry)
5293 free((char *)pe->path);
5294 got_pathlist_free(&paths);
5295 got_ref_list_free(&refs);
5296 if (f1 && fclose(f1) == EOF && error == NULL)
5297 error = got_error_from_errno("fclose");
5298 if (f2 && fclose(f2) == EOF && error == NULL)
5299 error = got_error_from_errno("fclose");
5300 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5301 error = got_error_from_errno("close");
5302 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5303 error = got_error_from_errno("close");
5311 "usage: %s blame [-c commit] [-r repository-path] path\n",
5320 char datebuf[11]; /* YYYY-MM-DD + NUL */
5323 struct blame_cb_args {
5324 struct blame_line *lines;
5328 off_t *line_offsets;
5330 struct got_repository *repo;
5333 static const struct got_error *
5334 blame_cb(void *arg, int nlines, int lineno,
5335 struct got_commit_object *commit, struct got_object_id *id)
5337 const struct got_error *err = NULL;
5338 struct blame_cb_args *a = arg;
5339 struct blame_line *bline;
5341 size_t linesize = 0;
5344 time_t committer_time;
5346 if (nlines != a->nlines ||
5347 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5348 return got_error(GOT_ERR_RANGE);
5350 if (sigint_received)
5351 return got_error(GOT_ERR_ITER_COMPLETED);
5354 return NULL; /* no change in this commit */
5356 /* Annotate this line. */
5357 bline = &a->lines[lineno - 1];
5358 if (bline->annotated)
5360 err = got_object_id_str(&bline->id_str, id);
5364 bline->committer = strdup(got_object_commit_get_committer(commit));
5365 if (bline->committer == NULL) {
5366 err = got_error_from_errno("strdup");
5370 committer_time = got_object_commit_get_committer_time(commit);
5371 if (gmtime_r(&committer_time, &tm) == NULL)
5372 return got_error_from_errno("gmtime_r");
5373 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5375 err = got_error(GOT_ERR_NO_SPACE);
5378 bline->annotated = 1;
5380 /* Print lines annotated so far. */
5381 bline = &a->lines[a->lineno_cur - 1];
5382 if (!bline->annotated)
5385 offset = a->line_offsets[a->lineno_cur - 1];
5386 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5387 err = got_error_from_errno("fseeko");
5391 while (bline->annotated) {
5392 char *smallerthan, *at, *nl, *committer;
5395 if (getline(&line, &linesize, a->f) == -1) {
5397 err = got_error_from_errno("getline");
5401 committer = bline->committer;
5402 smallerthan = strchr(committer, '<');
5403 if (smallerthan && smallerthan[1] != '\0')
5404 committer = smallerthan + 1;
5405 at = strchr(committer, '@');
5408 len = strlen(committer);
5410 committer[8] = '\0';
5412 nl = strchr(line, '\n');
5415 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5416 bline->id_str, bline->datebuf, committer, line);
5419 bline = &a->lines[a->lineno_cur - 1];
5426 static const struct got_error *
5427 cmd_blame(int argc, char *argv[])
5429 const struct got_error *error;
5430 struct got_repository *repo = NULL;
5431 struct got_worktree *worktree = NULL;
5432 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5433 char *link_target = NULL;
5434 struct got_object_id *obj_id = NULL;
5435 struct got_object_id *commit_id = NULL;
5436 struct got_commit_object *commit = NULL;
5437 struct got_blob_object *blob = NULL;
5438 char *commit_id_str = NULL;
5439 struct blame_cb_args bca;
5440 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5442 int *pack_fds = NULL;
5443 FILE *f1 = NULL, *f2 = NULL;
5445 fd1 = got_opentempfd();
5447 return got_error_from_errno("got_opentempfd");
5449 memset(&bca, 0, sizeof(bca));
5452 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5457 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5460 commit_id_str = optarg;
5463 repo_path = realpath(optarg, NULL);
5464 if (repo_path == NULL)
5465 return got_error_from_errno2("realpath",
5467 got_path_strip_trailing_slashes(repo_path);
5483 cwd = getcwd(NULL, 0);
5485 error = got_error_from_errno("getcwd");
5489 error = got_repo_pack_fds_open(&pack_fds);
5493 if (repo_path == NULL) {
5494 error = got_worktree_open(&worktree, cwd);
5495 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5501 strdup(got_worktree_get_repo_path(worktree));
5502 if (repo_path == NULL) {
5503 error = got_error_from_errno("strdup");
5508 repo_path = strdup(cwd);
5509 if (repo_path == NULL) {
5510 error = got_error_from_errno("strdup");
5516 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5521 const char *prefix = got_worktree_get_path_prefix(worktree);
5524 error = got_worktree_resolve_path(&p, worktree, path);
5527 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5528 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5530 error = got_error_from_errno("asprintf");
5535 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5537 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5540 error = got_repo_map_path(&in_repo_path, repo, path);
5545 if (commit_id_str == NULL) {
5546 struct got_reference *head_ref;
5547 error = got_ref_open(&head_ref, repo, worktree ?
5548 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5551 error = got_ref_resolve(&commit_id, repo, head_ref);
5552 got_ref_close(head_ref);
5556 struct got_reflist_head refs;
5558 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5564 got_ref_list_free(&refs);
5570 /* Release work tree lock. */
5571 got_worktree_close(worktree);
5575 error = got_object_open_as_commit(&commit, repo, commit_id);
5579 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5584 error = got_object_id_by_path(&obj_id, repo, commit,
5585 link_target ? link_target : in_repo_path);
5589 error = got_object_get_type(&obj_type, repo, obj_id);
5593 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5594 error = got_error_path(link_target ? link_target : in_repo_path,
5599 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5602 bca.f = got_opentemp();
5603 if (bca.f == NULL) {
5604 error = got_error_from_errno("got_opentemp");
5607 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5608 &bca.line_offsets, bca.f, blob);
5609 if (error || bca.nlines == 0)
5612 /* Don't include \n at EOF in the blame line count. */
5613 if (bca.line_offsets[bca.nlines - 1] == filesize)
5616 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5617 if (bca.lines == NULL) {
5618 error = got_error_from_errno("calloc");
5622 bca.nlines_prec = 0;
5630 fd2 = got_opentempfd();
5632 error = got_error_from_errno("got_opentempfd");
5635 fd3 = got_opentempfd();
5637 error = got_error_from_errno("got_opentempfd");
5640 f1 = got_opentemp();
5642 error = got_error_from_errno("got_opentemp");
5645 f2 = got_opentemp();
5647 error = got_error_from_errno("got_opentemp");
5650 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5651 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5652 check_cancelled, NULL, fd2, fd3, f1, f2);
5661 got_object_commit_close(commit);
5663 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5664 error = got_error_from_errno("close");
5665 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5666 error = got_error_from_errno("close");
5667 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5668 error = got_error_from_errno("close");
5669 if (f1 && fclose(f1) == EOF && error == NULL)
5670 error = got_error_from_errno("fclose");
5671 if (f2 && fclose(f2) == EOF && error == NULL)
5672 error = got_error_from_errno("fclose");
5675 got_object_blob_close(blob);
5677 got_worktree_close(worktree);
5679 const struct got_error *close_err = got_repo_close(repo);
5684 const struct got_error *pack_err =
5685 got_repo_pack_fds_close(pack_fds);
5690 for (i = 0; i < bca.nlines; i++) {
5691 struct blame_line *bline = &bca.lines[i];
5692 free(bline->id_str);
5693 free(bline->committer);
5697 free(bca.line_offsets);
5698 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5699 error = got_error_from_errno("fclose");
5707 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5712 static const struct got_error *
5713 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5714 const char *root_path, struct got_repository *repo)
5716 const struct got_error *err = NULL;
5717 int is_root_path = (strcmp(path, root_path) == 0);
5718 const char *modestr = "";
5719 mode_t mode = got_tree_entry_get_mode(te);
5720 char *link_target = NULL;
5722 path += strlen(root_path);
5723 while (path[0] == '/')
5726 if (got_object_tree_entry_is_submodule(te))
5728 else if (S_ISLNK(mode)) {
5731 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5734 for (i = 0; i < strlen(link_target); i++) {
5735 if (!isprint((unsigned char)link_target[i]))
5736 link_target[i] = '?';
5741 else if (S_ISDIR(mode))
5743 else if (mode & S_IXUSR)
5746 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5747 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5748 link_target ? " -> ": "", link_target ? link_target : "");
5754 static const struct got_error *
5755 print_tree(const char *path, struct got_commit_object *commit,
5756 int show_ids, int recurse, const char *root_path,
5757 struct got_repository *repo)
5759 const struct got_error *err = NULL;
5760 struct got_object_id *tree_id = NULL;
5761 struct got_tree_object *tree = NULL;
5764 err = got_object_id_by_path(&tree_id, repo, commit, path);
5768 err = got_object_open_as_tree(&tree, repo, tree_id);
5771 nentries = got_object_tree_get_nentries(tree);
5772 for (i = 0; i < nentries; i++) {
5773 struct got_tree_entry *te;
5776 if (sigint_received || sigpipe_received)
5779 te = got_object_tree_get_entry(tree, i);
5782 err = got_object_id_str(&id_str,
5783 got_tree_entry_get_id(te));
5786 if (asprintf(&id, "%s ", id_str) == -1) {
5787 err = got_error_from_errno("asprintf");
5793 err = print_entry(te, id, path, root_path, repo);
5798 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5800 if (asprintf(&child_path, "%s%s%s", path,
5801 path[0] == '/' && path[1] == '\0' ? "" : "/",
5802 got_tree_entry_get_name(te)) == -1) {
5803 err = got_error_from_errno("asprintf");
5806 err = print_tree(child_path, commit, show_ids, 1,
5815 got_object_tree_close(tree);
5820 static const struct got_error *
5821 cmd_tree(int argc, char *argv[])
5823 const struct got_error *error;
5824 struct got_repository *repo = NULL;
5825 struct got_worktree *worktree = NULL;
5826 const char *path, *refname = NULL;
5827 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5828 struct got_object_id *commit_id = NULL;
5829 struct got_commit_object *commit = NULL;
5830 char *commit_id_str = NULL;
5831 int show_ids = 0, recurse = 0;
5833 int *pack_fds = NULL;
5836 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5841 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5844 commit_id_str = optarg;
5847 repo_path = realpath(optarg, NULL);
5848 if (repo_path == NULL)
5849 return got_error_from_errno2("realpath",
5851 got_path_strip_trailing_slashes(repo_path);
5875 cwd = getcwd(NULL, 0);
5877 error = got_error_from_errno("getcwd");
5881 error = got_repo_pack_fds_open(&pack_fds);
5885 if (repo_path == NULL) {
5886 error = got_worktree_open(&worktree, cwd);
5887 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5893 strdup(got_worktree_get_repo_path(worktree));
5894 if (repo_path == NULL)
5895 error = got_error_from_errno("strdup");
5899 repo_path = strdup(cwd);
5900 if (repo_path == NULL) {
5901 error = got_error_from_errno("strdup");
5907 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5912 const char *prefix = got_worktree_get_path_prefix(worktree);
5917 error = got_worktree_resolve_path(&p, worktree, path);
5920 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5921 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5923 error = got_error_from_errno("asprintf");
5928 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5932 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5937 error = got_repo_map_path(&in_repo_path, repo, path);
5942 if (commit_id_str == NULL) {
5943 struct got_reference *head_ref;
5945 refname = got_worktree_get_head_ref_name(worktree);
5947 refname = GOT_REF_HEAD;
5948 error = got_ref_open(&head_ref, repo, refname, 0);
5951 error = got_ref_resolve(&commit_id, repo, head_ref);
5952 got_ref_close(head_ref);
5956 struct got_reflist_head refs;
5958 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5962 error = got_repo_match_object_id(&commit_id, NULL,
5963 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5964 got_ref_list_free(&refs);
5970 /* Release work tree lock. */
5971 got_worktree_close(worktree);
5975 error = got_object_open_as_commit(&commit, repo, commit_id);
5979 error = print_tree(in_repo_path, commit, show_ids, recurse,
5980 in_repo_path, repo);
5987 got_object_commit_close(commit);
5989 got_worktree_close(worktree);
5991 const struct got_error *close_err = got_repo_close(repo);
5996 const struct got_error *pack_err =
5997 got_repo_pack_fds_close(pack_fds);
6007 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
6008 "[-S status-codes] [path ...]\n", getprogname());
6012 struct got_status_arg {
6017 static const struct got_error *
6018 print_status(void *arg, unsigned char status, unsigned char staged_status,
6019 const char *path, struct got_object_id *blob_id,
6020 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6021 int dirfd, const char *de_name)
6023 struct got_status_arg *st = arg;
6025 if (status == staged_status && (status == GOT_STATUS_DELETE))
6026 status = GOT_STATUS_NO_CHANGE;
6027 if (st != NULL && st->status_codes) {
6028 size_t ncodes = strlen(st->status_codes);
6031 for (i = 0; i < ncodes ; i++) {
6033 if (status == st->status_codes[i] ||
6034 staged_status == st->status_codes[i]) {
6039 if (status == st->status_codes[i] ||
6040 staged_status == st->status_codes[i])
6045 if (st->suppress && j == 0)
6052 printf("%c%c %s\n", status, staged_status, path);
6056 static const struct got_error *
6057 cmd_status(int argc, char *argv[])
6059 const struct got_error *error = NULL;
6060 struct got_repository *repo = NULL;
6061 struct got_worktree *worktree = NULL;
6062 struct got_status_arg st;
6064 struct got_pathlist_head paths;
6065 struct got_pathlist_entry *pe;
6066 int ch, i, no_ignores = 0;
6067 int *pack_fds = NULL;
6071 memset(&st, 0, sizeof(st));
6072 st.status_codes = NULL;
6075 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6081 if (st.status_codes != NULL && st.suppress == 0)
6082 option_conflict('S', 's');
6086 for (i = 0; i < strlen(optarg); i++) {
6087 switch (optarg[i]) {
6088 case GOT_STATUS_MODIFY:
6089 case GOT_STATUS_ADD:
6090 case GOT_STATUS_DELETE:
6091 case GOT_STATUS_CONFLICT:
6092 case GOT_STATUS_MISSING:
6093 case GOT_STATUS_OBSTRUCTED:
6094 case GOT_STATUS_UNVERSIONED:
6095 case GOT_STATUS_MODE_CHANGE:
6096 case GOT_STATUS_NONEXISTENT:
6099 errx(1, "invalid status code '%c'",
6103 if (ch == 's' && st.suppress)
6104 option_conflict('s', 'S');
6105 st.status_codes = optarg;
6117 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6121 cwd = getcwd(NULL, 0);
6123 error = got_error_from_errno("getcwd");
6127 error = got_repo_pack_fds_open(&pack_fds);
6131 error = got_worktree_open(&worktree, cwd);
6133 if (error->code == GOT_ERR_NOT_WORKTREE)
6134 error = wrap_not_worktree_error(error, "status", cwd);
6138 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6143 error = apply_unveil(got_repo_get_path(repo), 1,
6144 got_worktree_get_root_path(worktree));
6148 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6152 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6153 print_status, &st, check_cancelled, NULL);
6156 const struct got_error *pack_err =
6157 got_repo_pack_fds_close(pack_fds);
6162 TAILQ_FOREACH(pe, &paths, entry)
6163 free((char *)pe->path);
6164 got_pathlist_free(&paths);
6173 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6174 "[-s reference] [-d] [name]\n",
6179 static const struct got_error *
6180 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6182 static const struct got_error *err = NULL;
6183 struct got_reflist_head refs;
6184 struct got_reflist_entry *re;
6187 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6188 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6193 TAILQ_FOREACH(re, &refs, entry) {
6195 refstr = got_ref_to_str(re->ref);
6196 if (refstr == NULL) {
6197 err = got_error_from_errno("got_ref_to_str");
6200 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6204 got_ref_list_free(&refs);
6208 static const struct got_error *
6209 delete_ref_by_name(struct got_repository *repo, const char *refname)
6211 const struct got_error *err;
6212 struct got_reference *ref;
6214 err = got_ref_open(&ref, repo, refname, 0);
6218 err = delete_ref(repo, ref);
6223 static const struct got_error *
6224 add_ref(struct got_repository *repo, const char *refname, const char *target)
6226 const struct got_error *err = NULL;
6227 struct got_object_id *id = NULL;
6228 struct got_reference *ref = NULL;
6229 struct got_reflist_head refs;
6232 * Don't let the user create a reference name with a leading '-'.
6233 * While technically a valid reference name, this case is usually
6234 * an unintended typo.
6236 if (refname[0] == '-')
6237 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6240 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6243 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6245 got_ref_list_free(&refs);
6249 err = got_ref_alloc(&ref, refname, id);
6253 err = got_ref_write(ref, repo);
6261 static const struct got_error *
6262 add_symref(struct got_repository *repo, const char *refname, const char *target)
6264 const struct got_error *err = NULL;
6265 struct got_reference *ref = NULL;
6266 struct got_reference *target_ref = NULL;
6269 * Don't let the user create a reference name with a leading '-'.
6270 * While technically a valid reference name, this case is usually
6271 * an unintended typo.
6273 if (refname[0] == '-')
6274 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6276 err = got_ref_open(&target_ref, repo, target, 0);
6280 err = got_ref_alloc_symref(&ref, refname, target_ref);
6284 err = got_ref_write(ref, repo);
6287 got_ref_close(target_ref);
6293 static const struct got_error *
6294 cmd_ref(int argc, char *argv[])
6296 const struct got_error *error = NULL;
6297 struct got_repository *repo = NULL;
6298 struct got_worktree *worktree = NULL;
6299 char *cwd = NULL, *repo_path = NULL;
6300 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6301 const char *obj_arg = NULL, *symref_target= NULL;
6302 char *refname = NULL;
6303 int *pack_fds = NULL;
6305 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6314 repo_path = realpath(optarg, NULL);
6315 if (repo_path == NULL)
6316 return got_error_from_errno2("realpath",
6318 got_path_strip_trailing_slashes(repo_path);
6324 symref_target = optarg;
6335 if (obj_arg && do_list)
6336 option_conflict('c', 'l');
6337 if (obj_arg && do_delete)
6338 option_conflict('c', 'd');
6339 if (obj_arg && symref_target)
6340 option_conflict('c', 's');
6341 if (symref_target && do_delete)
6342 option_conflict('s', 'd');
6343 if (symref_target && do_list)
6344 option_conflict('s', 'l');
6345 if (do_delete && do_list)
6346 option_conflict('d', 'l');
6347 if (sort_by_time && !do_list)
6348 errx(1, "-t option requires -l option");
6354 if (argc != 0 && argc != 1)
6357 refname = strdup(argv[0]);
6358 if (refname == NULL) {
6359 error = got_error_from_errno("strdup");
6366 refname = strdup(argv[0]);
6367 if (refname == NULL) {
6368 error = got_error_from_errno("strdup");
6374 got_path_strip_trailing_slashes(refname);
6377 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6378 "sendfd unveil", NULL) == -1)
6381 cwd = getcwd(NULL, 0);
6383 error = got_error_from_errno("getcwd");
6387 error = got_repo_pack_fds_open(&pack_fds);
6391 if (repo_path == NULL) {
6392 error = got_worktree_open(&worktree, cwd);
6393 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6399 strdup(got_worktree_get_repo_path(worktree));
6400 if (repo_path == NULL)
6401 error = got_error_from_errno("strdup");
6405 repo_path = strdup(cwd);
6406 if (repo_path == NULL) {
6407 error = got_error_from_errno("strdup");
6413 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6419 /* Remove "cpath" promise. */
6420 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6426 error = apply_unveil(got_repo_get_path(repo), do_list,
6427 worktree ? got_worktree_get_root_path(worktree) : NULL);
6432 error = list_refs(repo, refname, sort_by_time);
6434 error = delete_ref_by_name(repo, refname);
6435 else if (symref_target)
6436 error = add_symref(repo, refname, symref_target);
6438 if (obj_arg == NULL)
6440 error = add_ref(repo, refname, obj_arg);
6445 const struct got_error *close_err = got_repo_close(repo);
6450 got_worktree_close(worktree);
6452 const struct got_error *pack_err =
6453 got_repo_pack_fds_close(pack_fds);
6466 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6467 "[-n] [name]\n", getprogname());
6471 static const struct got_error *
6472 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6473 struct got_reference *ref)
6475 const struct got_error *err = NULL;
6476 const char *refname, *marker = " ";
6479 refname = got_ref_get_name(ref);
6480 if (worktree && strcmp(refname,
6481 got_worktree_get_head_ref_name(worktree)) == 0) {
6482 struct got_object_id *id = NULL;
6484 err = got_ref_resolve(&id, repo, ref);
6487 if (got_object_id_cmp(id,
6488 got_worktree_get_base_commit_id(worktree)) == 0)
6495 if (strncmp(refname, "refs/heads/", 11) == 0)
6497 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6499 if (strncmp(refname, "refs/remotes/", 13) == 0)
6502 refstr = got_ref_to_str(ref);
6504 return got_error_from_errno("got_ref_to_str");
6506 printf("%s%s: %s\n", marker, refname, refstr);
6511 static const struct got_error *
6512 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6514 const char *refname;
6516 if (worktree == NULL)
6517 return got_error(GOT_ERR_NOT_WORKTREE);
6519 refname = got_worktree_get_head_ref_name(worktree);
6521 if (strncmp(refname, "refs/heads/", 11) == 0)
6523 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6526 printf("%s\n", refname);
6531 static const struct got_error *
6532 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6535 static const struct got_error *err = NULL;
6536 struct got_reflist_head refs;
6537 struct got_reflist_entry *re;
6538 struct got_reference *temp_ref = NULL;
6539 int rebase_in_progress, histedit_in_progress;
6544 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6549 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6554 if (rebase_in_progress || histedit_in_progress) {
6555 err = got_ref_open(&temp_ref, repo,
6556 got_worktree_get_head_ref_name(worktree), 0);
6559 list_branch(repo, worktree, temp_ref);
6560 got_ref_close(temp_ref);
6564 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6565 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6570 TAILQ_FOREACH(re, &refs, entry)
6571 list_branch(repo, worktree, re->ref);
6573 got_ref_list_free(&refs);
6575 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6576 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6581 TAILQ_FOREACH(re, &refs, entry)
6582 list_branch(repo, worktree, re->ref);
6584 got_ref_list_free(&refs);
6589 static const struct got_error *
6590 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6591 const char *branch_name)
6593 const struct got_error *err = NULL;
6594 struct got_reference *ref = NULL;
6595 char *refname, *remote_refname = NULL;
6597 if (strncmp(branch_name, "refs/", 5) == 0)
6599 if (strncmp(branch_name, "heads/", 6) == 0)
6601 else if (strncmp(branch_name, "remotes/", 8) == 0)
6604 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6605 return got_error_from_errno("asprintf");
6607 if (asprintf(&remote_refname, "refs/remotes/%s",
6608 branch_name) == -1) {
6609 err = got_error_from_errno("asprintf");
6613 err = got_ref_open(&ref, repo, refname, 0);
6615 const struct got_error *err2;
6616 if (err->code != GOT_ERR_NOT_REF)
6619 * Keep 'err' intact such that if neither branch exists
6620 * we report "refs/heads" rather than "refs/remotes" in
6621 * our error message.
6623 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6630 strcmp(got_worktree_get_head_ref_name(worktree),
6631 got_ref_get_name(ref)) == 0) {
6632 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6633 "will not delete this work tree's current branch");
6637 err = delete_ref(repo, ref);
6642 free(remote_refname);
6646 static const struct got_error *
6647 add_branch(struct got_repository *repo, const char *branch_name,
6648 struct got_object_id *base_commit_id)
6650 const struct got_error *err = NULL;
6651 struct got_reference *ref = NULL;
6652 char *base_refname = NULL, *refname = NULL;
6655 * Don't let the user create a branch name with a leading '-'.
6656 * While technically a valid reference name, this case is usually
6657 * an unintended typo.
6659 if (branch_name[0] == '-')
6660 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6662 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6665 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6666 err = got_error_from_errno("asprintf");
6670 err = got_ref_open(&ref, repo, refname, 0);
6672 err = got_error(GOT_ERR_BRANCH_EXISTS);
6674 } else if (err->code != GOT_ERR_NOT_REF)
6677 err = got_ref_alloc(&ref, refname, base_commit_id);
6681 err = got_ref_write(ref, repo);
6690 static const struct got_error *
6691 cmd_branch(int argc, char *argv[])
6693 const struct got_error *error = NULL;
6694 struct got_repository *repo = NULL;
6695 struct got_worktree *worktree = NULL;
6696 char *cwd = NULL, *repo_path = NULL;
6697 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6698 const char *delref = NULL, *commit_id_arg = NULL;
6699 struct got_reference *ref = NULL;
6700 struct got_pathlist_head paths;
6701 struct got_pathlist_entry *pe;
6702 struct got_object_id *commit_id = NULL;
6703 char *commit_id_str = NULL;
6704 int *pack_fds = NULL;
6708 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6711 commit_id_arg = optarg;
6717 repo_path = realpath(optarg, NULL);
6718 if (repo_path == NULL)
6719 return got_error_from_errno2("realpath",
6721 got_path_strip_trailing_slashes(repo_path);
6738 if (do_list && delref)
6739 option_conflict('l', 'd');
6740 if (sort_by_time && !do_list)
6741 errx(1, "-t option requires -l option");
6746 if (!do_list && !delref && argc == 0)
6749 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6750 errx(1, "-c option can only be used when creating a branch");
6752 if (do_list || delref) {
6755 } else if (!do_show && argc != 1)
6759 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6760 "sendfd unveil", NULL) == -1)
6763 cwd = getcwd(NULL, 0);
6765 error = got_error_from_errno("getcwd");
6769 error = got_repo_pack_fds_open(&pack_fds);
6773 if (repo_path == NULL) {
6774 error = got_worktree_open(&worktree, cwd);
6775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6781 strdup(got_worktree_get_repo_path(worktree));
6782 if (repo_path == NULL)
6783 error = got_error_from_errno("strdup");
6787 repo_path = strdup(cwd);
6788 if (repo_path == NULL) {
6789 error = got_error_from_errno("strdup");
6795 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6800 if (do_list || do_show) {
6801 /* Remove "cpath" promise. */
6802 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6808 error = apply_unveil(got_repo_get_path(repo), do_list,
6809 worktree ? got_worktree_get_root_path(worktree) : NULL);
6814 error = show_current_branch(repo, worktree);
6816 error = list_branches(repo, worktree, sort_by_time);
6818 error = delete_branch(repo, worktree, delref);
6820 struct got_reflist_head refs;
6822 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6826 if (commit_id_arg == NULL)
6827 commit_id_arg = worktree ?
6828 got_worktree_get_head_ref_name(worktree) :
6830 error = got_repo_match_object_id(&commit_id, NULL,
6831 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6832 got_ref_list_free(&refs);
6835 error = add_branch(repo, argv[0], commit_id);
6838 if (worktree && do_update) {
6839 struct got_update_progress_arg upa;
6840 char *branch_refname = NULL;
6842 error = got_object_id_str(&commit_id_str, commit_id);
6845 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6849 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6851 error = got_error_from_errno("asprintf");
6854 error = got_ref_open(&ref, repo, branch_refname, 0);
6855 free(branch_refname);
6858 error = switch_head_ref(ref, commit_id, worktree,
6862 error = got_worktree_set_base_commit_id(worktree, repo,
6866 memset(&upa, 0, sizeof(upa));
6867 error = got_worktree_checkout_files(worktree, &paths,
6868 repo, update_progress, &upa, check_cancelled,
6872 if (upa.did_something) {
6873 printf("Updated to %s: %s\n",
6874 got_worktree_get_head_ref_name(worktree),
6877 print_update_progress_stats(&upa);
6884 const struct got_error *close_err = got_repo_close(repo);
6889 got_worktree_close(worktree);
6891 const struct got_error *pack_err =
6892 got_repo_pack_fds_close(pack_fds);
6899 free(commit_id_str);
6900 TAILQ_FOREACH(pe, &paths, entry)
6901 free((char *)pe->path);
6902 got_pathlist_free(&paths);
6911 "usage: %s tag [-c commit] [-r repository] [-l] "
6912 "[-m message] [-s signer-id] [-v] [-V] name\n",
6918 static const struct got_error *
6919 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6921 const struct got_error *err = NULL;
6922 struct got_reflist_entry *re, *se, *new;
6923 struct got_object_id *re_id, *se_id;
6924 struct got_tag_object *re_tag, *se_tag;
6925 time_t re_time, se_time;
6927 STAILQ_FOREACH(re, tags, entry) {
6928 se = STAILQ_FIRST(sorted);
6930 err = got_reflist_entry_dup(&new, re);
6933 STAILQ_INSERT_HEAD(sorted, new, entry);
6936 err = got_ref_resolve(&re_id, repo, re->ref);
6939 err = got_object_open_as_tag(&re_tag, repo, re_id);
6943 re_time = got_object_tag_get_tagger_time(re_tag);
6944 got_object_tag_close(re_tag);
6948 err = got_ref_resolve(&se_id, repo, re->ref);
6951 err = got_object_open_as_tag(&se_tag, repo, se_id);
6955 se_time = got_object_tag_get_tagger_time(se_tag);
6956 got_object_tag_close(se_tag);
6958 if (se_time > re_time) {
6959 err = got_reflist_entry_dup(&new, re);
6962 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6965 se = STAILQ_NEXT(se, entry);
6974 static const struct got_error *
6975 get_tag_refname(char **refname, const char *tag_name)
6977 const struct got_error *err;
6979 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6980 *refname = strdup(tag_name);
6981 if (*refname == NULL)
6982 return got_error_from_errno("strdup");
6983 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6984 err = got_error_from_errno("asprintf");
6992 static const struct got_error *
6993 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6994 const char *allowed_signers, const char *revoked_signers, int verbosity)
6996 static const struct got_error *err = NULL;
6997 struct got_reflist_head refs;
6998 struct got_reflist_entry *re;
6999 char *wanted_refname = NULL;
7004 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7009 struct got_reference *ref;
7010 err = get_tag_refname(&wanted_refname, tag_name);
7013 /* Wanted tag reference should exist. */
7014 err = got_ref_open(&ref, repo, wanted_refname, 0);
7020 TAILQ_FOREACH(re, &refs, entry) {
7021 const char *refname;
7022 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7024 const char *tagger, *ssh_sig = NULL;
7025 char *sig_msg = NULL;
7027 struct got_object_id *id;
7028 struct got_tag_object *tag;
7029 struct got_commit_object *commit = NULL;
7031 refname = got_ref_get_name(re->ref);
7032 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7033 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7036 refstr = got_ref_to_str(re->ref);
7037 if (refstr == NULL) {
7038 err = got_error_from_errno("got_ref_to_str");
7042 err = got_ref_resolve(&id, repo, re->ref);
7045 err = got_object_open_as_tag(&tag, repo, id);
7047 if (err->code != GOT_ERR_OBJ_TYPE) {
7051 /* "lightweight" tag */
7052 err = got_object_open_as_commit(&commit, repo, id);
7057 tagger = got_object_commit_get_committer(commit);
7059 got_object_commit_get_committer_time(commit);
7060 err = got_object_id_str(&id_str, id);
7066 tagger = got_object_tag_get_tagger(tag);
7067 tagger_time = got_object_tag_get_tagger_time(tag);
7068 err = got_object_id_str(&id_str,
7069 got_object_tag_get_object_id(tag));
7075 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7076 got_object_tag_get_message(tag));
7077 if (ssh_sig && allowed_signers == NULL) {
7078 err = got_error_msg(
7079 GOT_ERR_VERIFY_TAG_SIGNATURE,
7080 "SSH signature verification requires "
7081 "setting allowed_signers in "
7087 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7089 printf("from: %s\n", tagger);
7090 datestr = get_datestr(&tagger_time, datebuf);
7092 printf("date: %s UTC\n", datestr);
7094 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7096 switch (got_object_tag_get_object_type(tag)) {
7097 case GOT_OBJ_TYPE_BLOB:
7098 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7101 case GOT_OBJ_TYPE_TREE:
7102 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7105 case GOT_OBJ_TYPE_COMMIT:
7106 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7109 case GOT_OBJ_TYPE_TAG:
7110 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7120 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7121 allowed_signers, revoked_signers, verbosity);
7122 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7126 printf("signature: %s", sig_msg);
7132 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7135 got_object_commit_close(commit);
7137 tagmsg0 = strdup(got_object_tag_get_message(tag));
7138 got_object_tag_close(tag);
7139 if (tagmsg0 == NULL) {
7140 err = got_error_from_errno("strdup");
7147 line = strsep(&tagmsg, "\n");
7149 printf(" %s\n", line);
7154 got_ref_list_free(&refs);
7155 free(wanted_refname);
7157 if (err == NULL && bad_sigs)
7158 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7162 static const struct got_error *
7163 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7164 const char *tag_name, const char *repo_path)
7166 const struct got_error *err = NULL;
7167 char *template = NULL, *initial_content = NULL;
7168 char *editor = NULL;
7169 int initial_content_len;
7172 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7173 err = got_error_from_errno("asprintf");
7177 initial_content_len = asprintf(&initial_content,
7178 "\n# tagging commit %s as %s\n",
7179 commit_id_str, tag_name);
7180 if (initial_content_len == -1) {
7181 err = got_error_from_errno("asprintf");
7185 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7189 if (write(fd, initial_content, initial_content_len) == -1) {
7190 err = got_error_from_errno2("write", *tagmsg_path);
7194 err = get_editor(&editor);
7197 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7198 initial_content_len, 1);
7200 free(initial_content);
7204 if (fd != -1 && close(fd) == -1 && err == NULL)
7205 err = got_error_from_errno2("close", *tagmsg_path);
7214 static const struct got_error *
7215 add_tag(struct got_repository *repo, const char *tagger,
7216 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7217 const char *signer_id, int verbosity)
7219 const struct got_error *err = NULL;
7220 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7221 char *label = NULL, *commit_id_str = NULL;
7222 struct got_reference *ref = NULL;
7223 char *refname = NULL, *tagmsg = NULL;
7224 char *tagmsg_path = NULL, *tag_id_str = NULL;
7225 int preserve_tagmsg = 0;
7226 struct got_reflist_head refs;
7231 * Don't let the user create a tag name with a leading '-'.
7232 * While technically a valid reference name, this case is usually
7233 * an unintended typo.
7235 if (tag_name[0] == '-')
7236 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7238 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7242 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7243 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7247 err = got_object_id_str(&commit_id_str, commit_id);
7251 err = get_tag_refname(&refname, tag_name);
7254 if (strncmp("refs/tags/", tag_name, 10) == 0)
7257 err = got_ref_open(&ref, repo, refname, 0);
7259 err = got_error(GOT_ERR_TAG_EXISTS);
7261 } else if (err->code != GOT_ERR_NOT_REF)
7264 if (tagmsg_arg == NULL) {
7265 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7266 tag_name, got_repo_get_path(repo));
7268 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7269 tagmsg_path != NULL)
7270 preserve_tagmsg = 1;
7273 /* Editor is done; we can now apply unveil(2) */
7274 err = got_sigs_apply_unveil();
7277 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7282 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7283 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7287 preserve_tagmsg = 1;
7291 err = got_ref_alloc(&ref, refname, tag_id);
7294 preserve_tagmsg = 1;
7298 err = got_ref_write(ref, repo);
7301 preserve_tagmsg = 1;
7305 err = got_object_id_str(&tag_id_str, tag_id);
7308 preserve_tagmsg = 1;
7311 printf("Created tag %s\n", tag_id_str);
7313 if (preserve_tagmsg) {
7314 fprintf(stderr, "%s: tag message preserved in %s\n",
7315 getprogname(), tagmsg_path);
7316 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7317 err = got_error_from_errno2("unlink", tagmsg_path);
7322 free(commit_id_str);
7326 got_ref_list_free(&refs);
7330 static const struct got_error *
7331 cmd_tag(int argc, char *argv[])
7333 const struct got_error *error = NULL;
7334 struct got_repository *repo = NULL;
7335 struct got_worktree *worktree = NULL;
7336 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7337 char *gitconfig_path = NULL, *tagger = NULL;
7338 char *allowed_signers = NULL, *revoked_signers = NULL;
7339 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7340 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7341 const char *signer_id = NULL;
7342 int *pack_fds = NULL;
7344 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7347 commit_id_arg = optarg;
7353 repo_path = realpath(optarg, NULL);
7354 if (repo_path == NULL)
7355 return got_error_from_errno2("realpath",
7357 got_path_strip_trailing_slashes(repo_path);
7371 else if (verbosity < 3)
7383 if (do_list || verify_tags) {
7384 if (commit_id_arg != NULL)
7386 "-c option can only be used when creating a tag");
7389 option_conflict('l', 'm');
7391 option_conflict('V', 'm');
7395 option_conflict('l', 's');
7397 option_conflict('V', 's');
7401 } else if (argc != 1)
7408 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7409 "sendfd unveil", NULL) == -1)
7412 cwd = getcwd(NULL, 0);
7414 error = got_error_from_errno("getcwd");
7418 error = got_repo_pack_fds_open(&pack_fds);
7422 if (repo_path == NULL) {
7423 error = got_worktree_open(&worktree, cwd);
7424 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7430 strdup(got_worktree_get_repo_path(worktree));
7431 if (repo_path == NULL)
7432 error = got_error_from_errno("strdup");
7436 repo_path = strdup(cwd);
7437 if (repo_path == NULL) {
7438 error = got_error_from_errno("strdup");
7444 if (do_list || verify_tags) {
7445 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7448 error = get_allowed_signers(&allowed_signers, repo, worktree);
7451 error = get_revoked_signers(&revoked_signers, repo, worktree);
7455 /* Release work tree lock. */
7456 got_worktree_close(worktree);
7461 * Remove "cpath" promise unless needed for signature tmpfile
7465 got_sigs_apply_unveil();
7468 if (pledge("stdio rpath wpath flock proc exec sendfd "
7469 "unveil", NULL) == -1)
7473 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7476 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7477 revoked_signers, verbosity);
7479 error = get_gitconfig_path(&gitconfig_path);
7482 error = got_repo_open(&repo, repo_path, gitconfig_path,
7487 error = get_author(&tagger, repo, worktree);
7491 /* Release work tree lock. */
7492 got_worktree_close(worktree);
7498 error = got_sigs_apply_unveil();
7502 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7507 if (commit_id_arg == NULL) {
7508 struct got_reference *head_ref;
7509 struct got_object_id *commit_id;
7510 error = got_ref_open(&head_ref, repo,
7511 worktree ? got_worktree_get_head_ref_name(worktree)
7515 error = got_ref_resolve(&commit_id, repo, head_ref);
7516 got_ref_close(head_ref);
7519 error = got_object_id_str(&commit_id_str, commit_id);
7525 error = add_tag(repo, tagger, tag_name,
7526 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7527 signer_id, verbosity);
7531 const struct got_error *close_err = got_repo_close(repo);
7536 got_worktree_close(worktree);
7538 const struct got_error *pack_err =
7539 got_repo_pack_fds_close(pack_fds);
7545 free(gitconfig_path);
7546 free(commit_id_str);
7548 free(allowed_signers);
7549 free(revoked_signers);
7556 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7561 static const struct got_error *
7562 add_progress(void *arg, unsigned char status, const char *path)
7564 while (path[0] == '/')
7566 printf("%c %s\n", status, path);
7570 static const struct got_error *
7571 cmd_add(int argc, char *argv[])
7573 const struct got_error *error = NULL;
7574 struct got_repository *repo = NULL;
7575 struct got_worktree *worktree = NULL;
7577 struct got_pathlist_head paths;
7578 struct got_pathlist_entry *pe;
7579 int ch, can_recurse = 0, no_ignores = 0;
7580 int *pack_fds = NULL;
7584 while ((ch = getopt(argc, argv, "IR")) != -1) {
7602 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7609 cwd = getcwd(NULL, 0);
7611 error = got_error_from_errno("getcwd");
7615 error = got_repo_pack_fds_open(&pack_fds);
7619 error = got_worktree_open(&worktree, cwd);
7621 if (error->code == GOT_ERR_NOT_WORKTREE)
7622 error = wrap_not_worktree_error(error, "add", cwd);
7626 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7631 error = apply_unveil(got_repo_get_path(repo), 1,
7632 got_worktree_get_root_path(worktree));
7636 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7643 TAILQ_FOREACH(pe, &paths, entry) {
7644 if (asprintf(&ondisk_path, "%s/%s",
7645 got_worktree_get_root_path(worktree),
7647 error = got_error_from_errno("asprintf");
7650 if (lstat(ondisk_path, &sb) == -1) {
7651 if (errno == ENOENT) {
7655 error = got_error_from_errno2("lstat",
7661 if (S_ISDIR(sb.st_mode)) {
7662 error = got_error_msg(GOT_ERR_BAD_PATH,
7663 "adding directories requires -R option");
7669 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7670 NULL, repo, no_ignores);
7673 const struct got_error *close_err = got_repo_close(repo);
7678 got_worktree_close(worktree);
7680 const struct got_error *pack_err =
7681 got_repo_pack_fds_close(pack_fds);
7685 TAILQ_FOREACH(pe, &paths, entry)
7686 free((char *)pe->path);
7687 got_pathlist_free(&paths);
7695 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7696 "path ...\n", getprogname());
7700 static const struct got_error *
7701 print_remove_status(void *arg, unsigned char status,
7702 unsigned char staged_status, const char *path)
7704 while (path[0] == '/')
7706 if (status == GOT_STATUS_NONEXISTENT)
7708 if (status == staged_status && (status == GOT_STATUS_DELETE))
7709 status = GOT_STATUS_NO_CHANGE;
7710 printf("%c%c %s\n", status, staged_status, path);
7714 static const struct got_error *
7715 cmd_remove(int argc, char *argv[])
7717 const struct got_error *error = NULL;
7718 struct got_worktree *worktree = NULL;
7719 struct got_repository *repo = NULL;
7720 const char *status_codes = NULL;
7722 struct got_pathlist_head paths;
7723 struct got_pathlist_entry *pe;
7724 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7725 int ignore_missing_paths = 0;
7726 int *pack_fds = NULL;
7730 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7733 delete_local_mods = 1;
7734 ignore_missing_paths = 1;
7743 for (i = 0; i < strlen(optarg); i++) {
7744 switch (optarg[i]) {
7745 case GOT_STATUS_MODIFY:
7746 delete_local_mods = 1;
7748 case GOT_STATUS_MISSING:
7749 ignore_missing_paths = 1;
7752 errx(1, "invalid status code '%c'",
7756 status_codes = optarg;
7768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7775 cwd = getcwd(NULL, 0);
7777 error = got_error_from_errno("getcwd");
7781 error = got_repo_pack_fds_open(&pack_fds);
7785 error = got_worktree_open(&worktree, cwd);
7787 if (error->code == GOT_ERR_NOT_WORKTREE)
7788 error = wrap_not_worktree_error(error, "remove", cwd);
7792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7797 error = apply_unveil(got_repo_get_path(repo), 1,
7798 got_worktree_get_root_path(worktree));
7802 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7809 TAILQ_FOREACH(pe, &paths, entry) {
7810 if (asprintf(&ondisk_path, "%s/%s",
7811 got_worktree_get_root_path(worktree),
7813 error = got_error_from_errno("asprintf");
7816 if (lstat(ondisk_path, &sb) == -1) {
7817 if (errno == ENOENT) {
7821 error = got_error_from_errno2("lstat",
7827 if (S_ISDIR(sb.st_mode)) {
7828 error = got_error_msg(GOT_ERR_BAD_PATH,
7829 "removing directories requires -R option");
7835 error = got_worktree_schedule_delete(worktree, &paths,
7836 delete_local_mods, status_codes, print_remove_status, NULL,
7837 repo, keep_on_disk, ignore_missing_paths);
7840 const struct got_error *close_err = got_repo_close(repo);
7845 got_worktree_close(worktree);
7847 const struct got_error *pack_err =
7848 got_repo_pack_fds_close(pack_fds);
7852 TAILQ_FOREACH(pe, &paths, entry)
7853 free((char *)pe->path);
7854 got_pathlist_free(&paths);
7862 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7863 "[-R] [patchfile]\n", getprogname());
7867 static const struct got_error *
7868 patch_from_stdin(int *patchfd)
7870 const struct got_error *err = NULL;
7872 char *path, buf[BUFSIZ];
7873 sig_t sighup, sigint, sigquit;
7875 err = got_opentemp_named_fd(&path, patchfd,
7876 GOT_TMPDIR_STR "/got-patch");
7882 sighup = signal(SIGHUP, SIG_DFL);
7883 sigint = signal(SIGINT, SIG_DFL);
7884 sigquit = signal(SIGQUIT, SIG_DFL);
7887 r = read(0, buf, sizeof(buf));
7889 err = got_error_from_errno("read");
7894 if (write(*patchfd, buf, r) == -1) {
7895 err = got_error_from_errno("write");
7900 signal(SIGHUP, sighup);
7901 signal(SIGINT, sigint);
7902 signal(SIGQUIT, sigquit);
7904 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7905 err = got_error_from_errno("lseek");
7915 static const struct got_error *
7916 patch_progress(void *arg, const char *old, const char *new,
7917 unsigned char status, const struct got_error *error, int old_from,
7918 int old_lines, int new_from, int new_lines, int offset,
7919 int ws_mangled, const struct got_error *hunk_err)
7921 const char *path = new == NULL ? old : new;
7923 while (*path == '/')
7927 printf("%c %s\n", status, path);
7930 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7932 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7933 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7934 old_lines, new_from, new_lines);
7935 if (hunk_err != NULL)
7936 printf("%s\n", hunk_err->msg);
7937 else if (offset != 0)
7938 printf("applied with offset %d\n", offset);
7940 printf("hunk contains mangled whitespace\n");
7946 static const struct got_error *
7947 cmd_patch(int argc, char *argv[])
7949 const struct got_error *error = NULL, *close_error = NULL;
7950 struct got_worktree *worktree = NULL;
7951 struct got_repository *repo = NULL;
7954 int ch, nop = 0, strip = -1, reverse = 0;
7956 int *pack_fds = NULL;
7958 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7964 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7966 errx(1, "pathname strip count is %s: %s",
7982 error = patch_from_stdin(&patchfd);
7985 } else if (argc == 1) {
7986 patchfd = open(argv[0], O_RDONLY);
7987 if (patchfd == -1) {
7988 error = got_error_from_errno2("open", argv[0]);
7994 if ((cwd = getcwd(NULL, 0)) == NULL) {
7995 error = got_error_from_errno("getcwd");
7999 error = got_repo_pack_fds_open(&pack_fds);
8003 error = got_worktree_open(&worktree, cwd);
8007 const char *repo_path = got_worktree_get_repo_path(worktree);
8008 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8012 error = apply_unveil(got_repo_get_path(repo), 0,
8013 got_worktree_get_root_path(worktree));
8018 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8023 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8024 &patch_progress, NULL, check_cancelled, NULL);
8028 close_error = got_repo_close(repo);
8030 error = close_error;
8032 if (worktree != NULL) {
8033 close_error = got_worktree_close(worktree);
8035 error = close_error;
8038 const struct got_error *pack_err =
8039 got_repo_pack_fds_close(pack_fds);
8050 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8051 "path ...\n", getprogname());
8055 static const struct got_error *
8056 revert_progress(void *arg, unsigned char status, const char *path)
8058 if (status == GOT_STATUS_UNVERSIONED)
8061 while (path[0] == '/')
8063 printf("%c %s\n", status, path);
8067 struct choose_patch_arg {
8068 FILE *patch_script_file;
8072 static const struct got_error *
8073 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8074 int nchanges, const char *action)
8076 const struct got_error *err;
8078 size_t linesize = 0;
8082 case GOT_STATUS_ADD:
8083 printf("A %s\n%s this addition? [y/n] ", path, action);
8085 case GOT_STATUS_DELETE:
8086 printf("D %s\n%s this deletion? [y/n] ", path, action);
8088 case GOT_STATUS_MODIFY:
8089 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8090 return got_error_from_errno("fseek");
8091 printf(GOT_COMMIT_SEP_STR);
8092 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8094 if (linelen == -1 && ferror(patch_file)) {
8095 err = got_error_from_errno("getline");
8100 printf(GOT_COMMIT_SEP_STR);
8101 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8102 path, n, nchanges, action);
8105 return got_error_path(path, GOT_ERR_FILE_STATUS);
8111 static const struct got_error *
8112 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8113 FILE *patch_file, int n, int nchanges)
8115 const struct got_error *err = NULL;
8117 size_t linesize = 0;
8120 struct choose_patch_arg *a = arg;
8122 *choice = GOT_PATCH_CHOICE_NONE;
8124 if (a->patch_script_file) {
8126 err = show_change(status, path, patch_file, n, nchanges,
8130 linelen = getline(&line, &linesize, a->patch_script_file);
8131 if (linelen == -1) {
8132 if (ferror(a->patch_script_file))
8133 return got_error_from_errno("getline");
8136 nl = strchr(line, '\n');
8139 if (strcmp(line, "y") == 0) {
8140 *choice = GOT_PATCH_CHOICE_YES;
8142 } else if (strcmp(line, "n") == 0) {
8143 *choice = GOT_PATCH_CHOICE_NO;
8145 } else if (strcmp(line, "q") == 0 &&
8146 status == GOT_STATUS_MODIFY) {
8147 *choice = GOT_PATCH_CHOICE_QUIT;
8150 printf("invalid response '%s'\n", line);
8155 while (resp != 'y' && resp != 'n' && resp != 'q') {
8156 err = show_change(status, path, patch_file, n, nchanges,
8163 if (status == GOT_STATUS_MODIFY) {
8164 if (resp != 'y' && resp != 'n' && resp != 'q') {
8165 printf("invalid response '%c'\n", resp);
8168 } else if (resp != 'y' && resp != 'n') {
8169 printf("invalid response '%c'\n", resp);
8175 *choice = GOT_PATCH_CHOICE_YES;
8176 else if (resp == 'n')
8177 *choice = GOT_PATCH_CHOICE_NO;
8178 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8179 *choice = GOT_PATCH_CHOICE_QUIT;
8184 static const struct got_error *
8185 cmd_revert(int argc, char *argv[])
8187 const struct got_error *error = NULL;
8188 struct got_worktree *worktree = NULL;
8189 struct got_repository *repo = NULL;
8190 char *cwd = NULL, *path = NULL;
8191 struct got_pathlist_head paths;
8192 struct got_pathlist_entry *pe;
8193 int ch, can_recurse = 0, pflag = 0;
8194 FILE *patch_script_file = NULL;
8195 const char *patch_script_path = NULL;
8196 struct choose_patch_arg cpa;
8197 int *pack_fds = NULL;
8201 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8207 patch_script_path = optarg;
8222 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8223 "unveil", NULL) == -1)
8228 if (patch_script_path && !pflag)
8229 errx(1, "-F option can only be used together with -p option");
8231 cwd = getcwd(NULL, 0);
8233 error = got_error_from_errno("getcwd");
8237 error = got_repo_pack_fds_open(&pack_fds);
8241 error = got_worktree_open(&worktree, cwd);
8243 if (error->code == GOT_ERR_NOT_WORKTREE)
8244 error = wrap_not_worktree_error(error, "revert", cwd);
8248 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8253 if (patch_script_path) {
8254 patch_script_file = fopen(patch_script_path, "re");
8255 if (patch_script_file == NULL) {
8256 error = got_error_from_errno2("fopen",
8261 error = apply_unveil(got_repo_get_path(repo), 1,
8262 got_worktree_get_root_path(worktree));
8266 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8273 TAILQ_FOREACH(pe, &paths, entry) {
8274 if (asprintf(&ondisk_path, "%s/%s",
8275 got_worktree_get_root_path(worktree),
8277 error = got_error_from_errno("asprintf");
8280 if (lstat(ondisk_path, &sb) == -1) {
8281 if (errno == ENOENT) {
8285 error = got_error_from_errno2("lstat",
8291 if (S_ISDIR(sb.st_mode)) {
8292 error = got_error_msg(GOT_ERR_BAD_PATH,
8293 "reverting directories requires -R option");
8299 cpa.patch_script_file = patch_script_file;
8300 cpa.action = "revert";
8301 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8302 pflag ? choose_patch : NULL, &cpa, repo);
8304 if (patch_script_file && fclose(patch_script_file) == EOF &&
8306 error = got_error_from_errno2("fclose", patch_script_path);
8308 const struct got_error *close_err = got_repo_close(repo);
8313 got_worktree_close(worktree);
8315 const struct got_error *pack_err =
8316 got_repo_pack_fds_close(pack_fds);
8328 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8329 "[path ...]\n", getprogname());
8333 struct collect_commit_logmsg_arg {
8334 const char *cmdline_log;
8335 const char *prepared_log;
8336 int non_interactive;
8338 const char *worktree_path;
8339 const char *branch_name;
8340 const char *repo_path;
8345 static const struct got_error *
8346 read_prepared_logmsg(char **logmsg, const char *path)
8348 const struct got_error *err = NULL;
8354 memset(&sb, 0, sizeof(sb));
8356 f = fopen(path, "re");
8358 return got_error_from_errno2("fopen", path);
8360 if (fstat(fileno(f), &sb) == -1) {
8361 err = got_error_from_errno2("fstat", path);
8364 if (sb.st_size == 0) {
8365 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8369 *logmsg = malloc(sb.st_size + 1);
8370 if (*logmsg == NULL) {
8371 err = got_error_from_errno("malloc");
8375 r = fread(*logmsg, 1, sb.st_size, f);
8376 if (r != sb.st_size) {
8378 err = got_error_from_errno2("fread", path);
8380 err = got_error(GOT_ERR_IO);
8383 (*logmsg)[sb.st_size] = '\0';
8385 if (fclose(f) == EOF && err == NULL)
8386 err = got_error_from_errno2("fclose", path);
8395 static const struct got_error *
8396 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8399 char *initial_content = NULL;
8400 struct got_pathlist_entry *pe;
8401 const struct got_error *err = NULL;
8402 char *template = NULL;
8403 struct collect_commit_logmsg_arg *a = arg;
8404 int initial_content_len;
8408 /* if a message was specified on the command line, just use it */
8409 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8410 len = strlen(a->cmdline_log) + 1;
8411 *logmsg = malloc(len + 1);
8412 if (*logmsg == NULL)
8413 return got_error_from_errno("malloc");
8414 strlcpy(*logmsg, a->cmdline_log, len);
8416 } else if (a->prepared_log != NULL && a->non_interactive)
8417 return read_prepared_logmsg(logmsg, a->prepared_log);
8419 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8420 return got_error_from_errno("asprintf");
8422 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8426 if (a->prepared_log) {
8428 err = read_prepared_logmsg(&msg, a->prepared_log);
8431 if (write(fd, msg, strlen(msg)) == -1) {
8432 err = got_error_from_errno2("write", a->logmsg_path);
8439 initial_content_len = asprintf(&initial_content,
8440 "\n# changes to be committed on branch %s:\n",
8442 if (initial_content_len == -1) {
8443 err = got_error_from_errno("asprintf");
8447 if (write(fd, initial_content, initial_content_len) == -1) {
8448 err = got_error_from_errno2("write", a->logmsg_path);
8452 TAILQ_FOREACH(pe, commitable_paths, entry) {
8453 struct got_commitable *ct = pe->data;
8454 dprintf(fd, "# %c %s\n",
8455 got_commitable_get_status(ct),
8456 got_commitable_get_path(ct));
8459 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8460 initial_content_len, a->prepared_log ? 0 : 1);
8462 free(initial_content);
8465 if (fd != -1 && close(fd) == -1 && err == NULL)
8466 err = got_error_from_errno2("close", a->logmsg_path);
8468 /* Editor is done; we can now apply unveil(2) */
8470 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8478 static const struct got_error *
8479 cmd_commit(int argc, char *argv[])
8481 const struct got_error *error = NULL;
8482 struct got_worktree *worktree = NULL;
8483 struct got_repository *repo = NULL;
8484 char *cwd = NULL, *id_str = NULL;
8485 struct got_object_id *id = NULL;
8486 const char *logmsg = NULL;
8487 char *prepared_logmsg = NULL;
8488 struct collect_commit_logmsg_arg cl_arg;
8489 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8490 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8491 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8492 struct got_pathlist_head paths;
8493 int *pack_fds = NULL;
8496 cl_arg.logmsg_path = NULL;
8498 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8502 option_conflict('F', 'm');
8503 prepared_logmsg = realpath(optarg, NULL);
8504 if (prepared_logmsg == NULL)
8505 return got_error_from_errno2("realpath",
8509 if (prepared_logmsg)
8510 option_conflict('m', 'F');
8514 non_interactive = 1;
8517 allow_bad_symlinks = 1;
8529 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8530 "unveil", NULL) == -1)
8533 cwd = getcwd(NULL, 0);
8535 error = got_error_from_errno("getcwd");
8539 error = got_repo_pack_fds_open(&pack_fds);
8543 error = got_worktree_open(&worktree, cwd);
8545 if (error->code == GOT_ERR_NOT_WORKTREE)
8546 error = wrap_not_worktree_error(error, "commit", cwd);
8550 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8553 if (rebase_in_progress) {
8554 error = got_error(GOT_ERR_REBASING);
8558 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8563 error = get_gitconfig_path(&gitconfig_path);
8566 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8567 gitconfig_path, pack_fds);
8571 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8574 if (merge_in_progress) {
8575 error = got_error(GOT_ERR_MERGE_BUSY);
8579 error = get_author(&author, repo, worktree);
8584 * unveil(2) traverses exec(2); if an editor is used we have
8585 * to apply unveil after the log message has been written.
8587 if (logmsg == NULL || strlen(logmsg) == 0)
8588 error = get_editor(&editor);
8590 error = apply_unveil(got_repo_get_path(repo), 0,
8591 got_worktree_get_root_path(worktree));
8595 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8599 cl_arg.editor = editor;
8600 cl_arg.cmdline_log = logmsg;
8601 cl_arg.prepared_log = prepared_logmsg;
8602 cl_arg.non_interactive = non_interactive;
8603 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8604 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8605 if (!histedit_in_progress) {
8606 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8607 error = got_error(GOT_ERR_COMMIT_BRANCH);
8610 cl_arg.branch_name += 11;
8612 cl_arg.repo_path = got_repo_get_path(repo);
8613 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8614 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8615 print_status, NULL, repo);
8617 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8618 cl_arg.logmsg_path != NULL)
8619 preserve_logmsg = 1;
8623 error = got_object_id_str(&id_str, id);
8626 printf("Created commit %s\n", id_str);
8628 if (preserve_logmsg) {
8629 fprintf(stderr, "%s: log message preserved in %s\n",
8630 getprogname(), cl_arg.logmsg_path);
8631 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8633 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8634 free(cl_arg.logmsg_path);
8636 const struct got_error *close_err = got_repo_close(repo);
8641 got_worktree_close(worktree);
8643 const struct got_error *pack_err =
8644 got_repo_pack_fds_close(pack_fds);
8650 free(gitconfig_path);
8653 free(prepared_logmsg);
8660 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8661 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8662 "[remote-repository]\n", getprogname());
8667 print_load_info(int print_colored, int print_found, int print_trees,
8668 int ncolored, int nfound, int ntrees)
8670 if (print_colored) {
8671 printf("%d commit%s colored", ncolored,
8672 ncolored == 1 ? "" : "s");
8675 printf("%s%d object%s found",
8676 ncolored > 0 ? "; " : "",
8677 nfound, nfound == 1 ? "" : "s");
8680 printf("; %d tree%s scanned", ntrees,
8681 ntrees == 1 ? "" : "s");
8685 struct got_send_progress_arg {
8686 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8693 int last_nobj_total;
8697 int printed_something;
8699 struct got_pathlist_head *delete_branches;
8702 static const struct got_error *
8703 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8704 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8705 int nobj_written, off_t bytes_sent, const char *refname, int success)
8707 struct got_send_progress_arg *a = arg;
8708 char scaled_packsize[FMT_SCALED_STRSIZE];
8709 char scaled_sent[FMT_SCALED_STRSIZE];
8710 int p_deltify = 0, p_written = 0, p_sent = 0;
8711 int print_colored = 0, print_found = 0, print_trees = 0;
8712 int print_searching = 0, print_total = 0;
8713 int print_deltify = 0, print_written = 0, print_sent = 0;
8715 if (a->verbosity < 0)
8719 const char *status = success ? "accepted" : "rejected";
8722 struct got_pathlist_entry *pe;
8723 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8724 const char *branchname = pe->path;
8725 if (got_path_cmp(branchname, refname,
8726 strlen(branchname), strlen(refname)) == 0) {
8728 a->sent_something = 1;
8734 if (a->printed_something)
8736 printf("Server has %s %s", status, refname);
8737 a->printed_something = 1;
8741 if (a->last_ncolored != ncolored) {
8743 a->last_ncolored = ncolored;
8746 if (a->last_nfound != nfound) {
8749 a->last_nfound = nfound;
8752 if (a->last_ntrees != ntrees) {
8756 a->last_ntrees = ntrees;
8759 if ((print_colored || print_found || print_trees) &&
8762 print_load_info(print_colored, print_found, print_trees,
8763 ncolored, nfound, ntrees);
8764 a->printed_something = 1;
8767 } else if (!a->loading_done) {
8769 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8771 a->loading_done = 1;
8774 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8775 return got_error_from_errno("fmt_scaled");
8776 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8777 return got_error_from_errno("fmt_scaled");
8779 if (a->last_ncommits != ncommits) {
8780 print_searching = 1;
8781 a->last_ncommits = ncommits;
8784 if (a->last_nobj_total != nobj_total) {
8785 print_searching = 1;
8787 a->last_nobj_total = nobj_total;
8790 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8791 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8792 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8793 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8794 return got_error(GOT_ERR_NO_SPACE);
8797 if (nobj_deltify > 0 || nobj_written > 0) {
8798 if (nobj_deltify > 0) {
8799 p_deltify = (nobj_deltify * 100) / nobj_total;
8800 if (p_deltify != a->last_p_deltify) {
8801 a->last_p_deltify = p_deltify;
8802 print_searching = 1;
8807 if (nobj_written > 0) {
8808 p_written = (nobj_written * 100) / nobj_total;
8809 if (p_written != a->last_p_written) {
8810 a->last_p_written = p_written;
8811 print_searching = 1;
8819 if (bytes_sent > 0) {
8820 p_sent = (bytes_sent * 100) / packfile_size;
8821 if (p_sent != a->last_p_sent) {
8822 a->last_p_sent = p_sent;
8823 print_searching = 1;
8829 a->sent_something = 1;
8832 if (print_searching || print_total || print_deltify || print_written ||
8835 if (print_searching)
8836 printf("packing %d reference%s", ncommits,
8837 ncommits == 1 ? "" : "s");
8839 printf("; %d object%s", nobj_total,
8840 nobj_total == 1 ? "" : "s");
8842 printf("; deltify: %d%%", p_deltify);
8844 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8845 scaled_packsize, p_sent);
8846 else if (print_written)
8847 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8848 scaled_packsize, p_written);
8849 if (print_searching || print_total || print_deltify ||
8850 print_written || print_sent) {
8851 a->printed_something = 1;
8857 static const struct got_error *
8858 cmd_send(int argc, char *argv[])
8860 const struct got_error *error = NULL;
8861 char *cwd = NULL, *repo_path = NULL;
8862 const char *remote_name;
8863 char *proto = NULL, *host = NULL, *port = NULL;
8864 char *repo_name = NULL, *server_path = NULL;
8865 const struct got_remote_repo *remotes, *remote = NULL;
8866 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8867 struct got_repository *repo = NULL;
8868 struct got_worktree *worktree = NULL;
8869 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8870 struct got_pathlist_head branches;
8871 struct got_pathlist_head tags;
8872 struct got_reflist_head all_branches;
8873 struct got_reflist_head all_tags;
8874 struct got_pathlist_head delete_args;
8875 struct got_pathlist_head delete_branches;
8876 struct got_reflist_entry *re;
8877 struct got_pathlist_entry *pe;
8878 int i, ch, sendfd = -1, sendstatus;
8880 struct got_send_progress_arg spa;
8881 int verbosity = 0, overwrite_refs = 0;
8882 int send_all_branches = 0, send_all_tags = 0;
8883 struct got_reference *ref = NULL;
8884 int *pack_fds = NULL;
8886 TAILQ_INIT(&branches);
8888 TAILQ_INIT(&all_branches);
8889 TAILQ_INIT(&all_tags);
8890 TAILQ_INIT(&delete_args);
8891 TAILQ_INIT(&delete_branches);
8893 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8896 send_all_branches = 1;
8899 error = got_pathlist_append(&branches, optarg, NULL);
8905 error = got_pathlist_append(&delete_args, optarg, NULL);
8913 repo_path = realpath(optarg, NULL);
8914 if (repo_path == NULL)
8915 return got_error_from_errno2("realpath",
8917 got_path_strip_trailing_slashes(repo_path);
8920 error = got_pathlist_append(&tags, optarg, NULL);
8931 else if (verbosity < 3)
8945 if (send_all_branches && !TAILQ_EMPTY(&branches))
8946 option_conflict('a', 'b');
8947 if (send_all_tags && !TAILQ_EMPTY(&tags))
8948 option_conflict('T', 't');
8952 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8954 remote_name = argv[0];
8958 cwd = getcwd(NULL, 0);
8960 error = got_error_from_errno("getcwd");
8964 error = got_repo_pack_fds_open(&pack_fds);
8968 if (repo_path == NULL) {
8969 error = got_worktree_open(&worktree, cwd);
8970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8976 strdup(got_worktree_get_repo_path(worktree));
8977 if (repo_path == NULL)
8978 error = got_error_from_errno("strdup");
8982 repo_path = strdup(cwd);
8983 if (repo_path == NULL) {
8984 error = got_error_from_errno("strdup");
8990 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8995 worktree_conf = got_worktree_get_gotconfig(worktree);
8996 if (worktree_conf) {
8997 got_gotconfig_get_remotes(&nremotes, &remotes,
8999 for (i = 0; i < nremotes; i++) {
9000 if (strcmp(remotes[i].name, remote_name) == 0) {
9001 remote = &remotes[i];
9007 if (remote == NULL) {
9008 repo_conf = got_repo_get_gotconfig(repo);
9010 got_gotconfig_get_remotes(&nremotes, &remotes,
9012 for (i = 0; i < nremotes; i++) {
9013 if (strcmp(remotes[i].name, remote_name) == 0) {
9014 remote = &remotes[i];
9020 if (remote == NULL) {
9021 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9022 for (i = 0; i < nremotes; i++) {
9023 if (strcmp(remotes[i].name, remote_name) == 0) {
9024 remote = &remotes[i];
9029 if (remote == NULL) {
9030 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9034 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9035 &repo_name, remote->send_url);
9039 if (strcmp(proto, "git") == 0) {
9041 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9042 "sendfd dns inet unveil", NULL) == -1)
9045 } else if (strcmp(proto, "git+ssh") == 0 ||
9046 strcmp(proto, "ssh") == 0) {
9048 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9049 "sendfd unveil", NULL) == -1)
9052 } else if (strcmp(proto, "http") == 0 ||
9053 strcmp(proto, "git+http") == 0) {
9054 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9057 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9061 error = got_dial_apply_unveil(proto);
9065 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9069 if (send_all_branches) {
9070 error = got_ref_list(&all_branches, repo, "refs/heads",
9071 got_ref_cmp_by_name, NULL);
9074 TAILQ_FOREACH(re, &all_branches, entry) {
9075 const char *branchname = got_ref_get_name(re->ref);
9076 error = got_pathlist_append(&branches,
9082 } else if (nbranches == 0) {
9083 for (i = 0; i < remote->nsend_branches; i++) {
9084 got_pathlist_append(&branches,
9085 remote->send_branches[i], NULL);
9089 if (send_all_tags) {
9090 error = got_ref_list(&all_tags, repo, "refs/tags",
9091 got_ref_cmp_by_name, NULL);
9094 TAILQ_FOREACH(re, &all_tags, entry) {
9095 const char *tagname = got_ref_get_name(re->ref);
9096 error = got_pathlist_append(&tags,
9105 * To prevent accidents only branches in refs/heads/ can be deleted
9106 * with 'got send -d'.
9107 * Deleting anything else requires local repository access or Git.
9109 TAILQ_FOREACH(pe, &delete_args, entry) {
9110 const char *branchname = pe->path;
9112 struct got_pathlist_entry *new;
9113 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9114 s = strdup(branchname);
9116 error = got_error_from_errno("strdup");
9120 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9121 error = got_error_from_errno("asprintf");
9125 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9126 if (error || new == NULL /* duplicate */)
9133 if (nbranches == 0 && ndelete_branches == 0) {
9134 struct got_reference *head_ref;
9136 error = got_ref_open(&head_ref, repo,
9137 got_worktree_get_head_ref_name(worktree), 0);
9139 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9142 if (got_ref_is_symbolic(head_ref)) {
9143 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9144 got_ref_close(head_ref);
9149 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9157 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9158 port ? ":" : "", port ? port : "");
9160 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9161 server_path, verbosity);
9165 memset(&spa, 0, sizeof(spa));
9166 spa.last_scaled_packsize[0] = '\0';
9167 spa.last_p_deltify = -1;
9168 spa.last_p_written = -1;
9169 spa.verbosity = verbosity;
9170 spa.delete_branches = &delete_branches;
9171 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9172 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9173 check_cancelled, NULL);
9174 if (spa.printed_something)
9178 if (!spa.sent_something && verbosity >= 0)
9179 printf("Already up-to-date\n");
9182 if (kill(sendpid, SIGTERM) == -1)
9183 error = got_error_from_errno("kill");
9184 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9185 error = got_error_from_errno("waitpid");
9187 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9188 error = got_error_from_errno("close");
9190 const struct got_error *close_err = got_repo_close(repo);
9195 got_worktree_close(worktree);
9197 const struct got_error *pack_err =
9198 got_repo_pack_fds_close(pack_fds);
9204 got_pathlist_free(&branches);
9205 got_pathlist_free(&tags);
9206 got_ref_list_free(&all_branches);
9207 got_ref_list_free(&all_tags);
9208 got_pathlist_free(&delete_args);
9209 TAILQ_FOREACH(pe, &delete_branches, entry)
9210 free((char *)pe->path);
9211 got_pathlist_free(&delete_branches);
9223 usage_cherrypick(void)
9225 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9229 static const struct got_error *
9230 cmd_cherrypick(int argc, char *argv[])
9232 const struct got_error *error = NULL;
9233 struct got_worktree *worktree = NULL;
9234 struct got_repository *repo = NULL;
9235 char *cwd = NULL, *commit_id_str = NULL;
9236 struct got_object_id *commit_id = NULL;
9237 struct got_commit_object *commit = NULL;
9238 struct got_object_qid *pid;
9240 struct got_update_progress_arg upa;
9241 int *pack_fds = NULL;
9243 while ((ch = getopt(argc, argv, "")) != -1) {
9255 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9256 "unveil", NULL) == -1)
9262 cwd = getcwd(NULL, 0);
9264 error = got_error_from_errno("getcwd");
9268 error = got_repo_pack_fds_open(&pack_fds);
9272 error = got_worktree_open(&worktree, cwd);
9274 if (error->code == GOT_ERR_NOT_WORKTREE)
9275 error = wrap_not_worktree_error(error, "cherrypick",
9280 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9285 error = apply_unveil(got_repo_get_path(repo), 0,
9286 got_worktree_get_root_path(worktree));
9290 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9291 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9294 error = got_object_id_str(&commit_id_str, commit_id);
9298 error = got_object_open_as_commit(&commit, repo, commit_id);
9301 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9302 memset(&upa, 0, sizeof(upa));
9303 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9304 commit_id, repo, update_progress, &upa, check_cancelled,
9309 if (upa.did_something)
9310 printf("Merged commit %s\n", commit_id_str);
9311 print_merge_progress_stats(&upa);
9314 got_object_commit_close(commit);
9315 free(commit_id_str);
9317 got_worktree_close(worktree);
9319 const struct got_error *close_err = got_repo_close(repo);
9324 const struct got_error *pack_err =
9325 got_repo_pack_fds_close(pack_fds);
9336 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9340 static const struct got_error *
9341 cmd_backout(int argc, char *argv[])
9343 const struct got_error *error = NULL;
9344 struct got_worktree *worktree = NULL;
9345 struct got_repository *repo = NULL;
9346 char *cwd = NULL, *commit_id_str = NULL;
9347 struct got_object_id *commit_id = NULL;
9348 struct got_commit_object *commit = NULL;
9349 struct got_object_qid *pid;
9351 struct got_update_progress_arg upa;
9352 int *pack_fds = NULL;
9354 while ((ch = getopt(argc, argv, "")) != -1) {
9366 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9367 "unveil", NULL) == -1)
9373 cwd = getcwd(NULL, 0);
9375 error = got_error_from_errno("getcwd");
9379 error = got_repo_pack_fds_open(&pack_fds);
9383 error = got_worktree_open(&worktree, cwd);
9385 if (error->code == GOT_ERR_NOT_WORKTREE)
9386 error = wrap_not_worktree_error(error, "backout", cwd);
9390 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9395 error = apply_unveil(got_repo_get_path(repo), 0,
9396 got_worktree_get_root_path(worktree));
9400 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9401 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9404 error = got_object_id_str(&commit_id_str, commit_id);
9408 error = got_object_open_as_commit(&commit, repo, commit_id);
9411 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9413 error = got_error(GOT_ERR_ROOT_COMMIT);
9417 memset(&upa, 0, sizeof(upa));
9418 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9419 repo, update_progress, &upa, check_cancelled, NULL);
9423 if (upa.did_something)
9424 printf("Backed out commit %s\n", commit_id_str);
9425 print_merge_progress_stats(&upa);
9428 got_object_commit_close(commit);
9429 free(commit_id_str);
9431 got_worktree_close(worktree);
9433 const struct got_error *close_err = got_repo_close(repo);
9438 const struct got_error *pack_err =
9439 got_repo_pack_fds_close(pack_fds);
9449 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9455 trim_logmsg(char *logmsg, int limit)
9460 len = strlen(logmsg);
9464 nl = strchr(logmsg, '\n');
9469 static const struct got_error *
9470 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9472 const struct got_error *err;
9473 char *logmsg0 = NULL;
9476 err = got_object_commit_get_logmsg(&logmsg0, commit);
9481 while (isspace((unsigned char)s[0]))
9484 *logmsg = strdup(s);
9485 if (*logmsg == NULL) {
9486 err = got_error_from_errno("strdup");
9490 trim_logmsg(*logmsg, limit);
9496 static const struct got_error *
9497 show_rebase_merge_conflict(struct got_object_id *id,
9498 struct got_repository *repo)
9500 const struct got_error *err;
9501 struct got_commit_object *commit = NULL;
9502 char *id_str = NULL, *logmsg = NULL;
9504 err = got_object_open_as_commit(&commit, repo, id);
9508 err = got_object_id_str(&id_str, id);
9514 err = get_short_logmsg(&logmsg, 42, commit);
9518 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9521 got_object_commit_close(commit);
9526 static const struct got_error *
9527 show_rebase_progress(struct got_commit_object *commit,
9528 struct got_object_id *old_id, struct got_object_id *new_id)
9530 const struct got_error *err;
9531 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9533 err = got_object_id_str(&old_id_str, old_id);
9538 err = got_object_id_str(&new_id_str, new_id);
9543 old_id_str[12] = '\0';
9545 new_id_str[12] = '\0';
9547 err = get_short_logmsg(&logmsg, 42, commit);
9551 printf("%s -> %s: %s\n", old_id_str,
9552 new_id_str ? new_id_str : "no-op change", logmsg);
9560 static const struct got_error *
9561 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9562 struct got_reference *branch, struct got_reference *new_base_branch,
9563 struct got_reference *tmp_branch, struct got_repository *repo,
9566 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9567 return got_worktree_rebase_complete(worktree, fileindex,
9568 new_base_branch, tmp_branch, branch, repo, create_backup);
9571 static const struct got_error *
9572 rebase_commit(struct got_pathlist_head *merged_paths,
9573 struct got_worktree *worktree, struct got_fileindex *fileindex,
9574 struct got_reference *tmp_branch,
9575 struct got_object_id *commit_id, struct got_repository *repo)
9577 const struct got_error *error;
9578 struct got_commit_object *commit;
9579 struct got_object_id *new_commit_id;
9581 error = got_object_open_as_commit(&commit, repo, commit_id);
9585 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9586 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9588 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9590 error = show_rebase_progress(commit, commit_id, NULL);
9592 error = show_rebase_progress(commit, commit_id, new_commit_id);
9593 free(new_commit_id);
9596 got_object_commit_close(commit);
9600 struct check_path_prefix_arg {
9601 const char *path_prefix;
9606 static const struct got_error *
9607 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9608 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9609 struct got_object_id *id1, struct got_object_id *id2,
9610 const char *path1, const char *path2,
9611 mode_t mode1, mode_t mode2, struct got_repository *repo)
9613 struct check_path_prefix_arg *a = arg;
9615 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9616 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9617 return got_error(a->errcode);
9622 static const struct got_error *
9623 check_path_prefix(struct got_object_id *parent_id,
9624 struct got_object_id *commit_id, const char *path_prefix,
9625 int errcode, struct got_repository *repo)
9627 const struct got_error *err;
9628 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9629 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9630 struct check_path_prefix_arg cpp_arg;
9632 if (got_path_is_root_dir(path_prefix))
9635 err = got_object_open_as_commit(&commit, repo, commit_id);
9639 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9643 err = got_object_open_as_tree(&tree1, repo,
9644 got_object_commit_get_tree_id(parent_commit));
9648 err = got_object_open_as_tree(&tree2, repo,
9649 got_object_commit_get_tree_id(commit));
9653 cpp_arg.path_prefix = path_prefix;
9654 while (cpp_arg.path_prefix[0] == '/')
9655 cpp_arg.path_prefix++;
9656 cpp_arg.len = strlen(cpp_arg.path_prefix);
9657 cpp_arg.errcode = errcode;
9658 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9659 check_path_prefix_in_diff, &cpp_arg, 0);
9662 got_object_tree_close(tree1);
9664 got_object_tree_close(tree2);
9666 got_object_commit_close(commit);
9668 got_object_commit_close(parent_commit);
9672 static const struct got_error *
9673 collect_commits(struct got_object_id_queue *commits,
9674 struct got_object_id *initial_commit_id,
9675 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9676 const char *path_prefix, int path_prefix_errcode,
9677 struct got_repository *repo)
9679 const struct got_error *err = NULL;
9680 struct got_commit_graph *graph = NULL;
9681 struct got_object_id *parent_id = NULL;
9682 struct got_object_qid *qid;
9683 struct got_object_id *commit_id = initial_commit_id;
9685 err = got_commit_graph_open(&graph, "/", 1);
9689 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9690 check_cancelled, NULL);
9693 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9694 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9695 check_cancelled, NULL);
9697 if (err->code == GOT_ERR_ITER_COMPLETED) {
9698 err = got_error_msg(GOT_ERR_ANCESTRY,
9699 "ran out of commits to rebase before "
9700 "youngest common ancestor commit has "
9705 err = check_path_prefix(parent_id, commit_id,
9706 path_prefix, path_prefix_errcode, repo);
9710 err = got_object_qid_alloc(&qid, commit_id);
9713 STAILQ_INSERT_HEAD(commits, qid, entry);
9714 commit_id = parent_id;
9718 got_commit_graph_close(graph);
9722 static const struct got_error *
9723 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9725 const struct got_error *err = NULL;
9726 time_t committer_time;
9728 char datebuf[11]; /* YYYY-MM-DD + NUL */
9729 char *author0 = NULL, *author, *smallerthan;
9730 char *logmsg0 = NULL, *logmsg, *newline;
9732 committer_time = got_object_commit_get_committer_time(commit);
9733 if (gmtime_r(&committer_time, &tm) == NULL)
9734 return got_error_from_errno("gmtime_r");
9735 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9736 return got_error(GOT_ERR_NO_SPACE);
9738 author0 = strdup(got_object_commit_get_author(commit));
9739 if (author0 == NULL)
9740 return got_error_from_errno("strdup");
9742 smallerthan = strchr(author, '<');
9743 if (smallerthan && smallerthan[1] != '\0')
9744 author = smallerthan + 1;
9745 author[strcspn(author, "@>")] = '\0';
9747 err = got_object_commit_get_logmsg(&logmsg0, commit);
9751 while (*logmsg == '\n')
9753 newline = strchr(logmsg, '\n');
9757 if (asprintf(brief_str, "%s %s %s",
9758 datebuf, author, logmsg) == -1)
9759 err = got_error_from_errno("asprintf");
9766 static const struct got_error *
9767 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9768 struct got_repository *repo)
9770 const struct got_error *err;
9773 err = got_object_id_str(&id_str, id);
9777 err = got_ref_delete(ref, repo);
9781 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9787 static const struct got_error *
9788 print_backup_ref(const char *branch_name, const char *new_id_str,
9789 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9790 struct got_reflist_object_id_map *refs_idmap,
9791 struct got_repository *repo)
9793 const struct got_error *err = NULL;
9794 struct got_reflist_head *refs;
9795 char *refs_str = NULL;
9796 struct got_object_id *new_commit_id = NULL;
9797 struct got_commit_object *new_commit = NULL;
9798 char *new_commit_brief_str = NULL;
9799 struct got_object_id *yca_id = NULL;
9800 struct got_commit_object *yca_commit = NULL;
9801 char *yca_id_str = NULL, *yca_brief_str = NULL;
9802 char *custom_refs_str;
9804 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9805 return got_error_from_errno("asprintf");
9807 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9808 0, 0, refs_idmap, custom_refs_str);
9812 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9816 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9818 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9823 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9827 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9831 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9832 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9836 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9837 refs_str ? " (" : "", refs_str ? refs_str : "",
9838 refs_str ? ")" : "", new_commit_brief_str);
9839 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9840 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9844 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9848 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9852 err = got_object_id_str(&yca_id_str, yca_id);
9856 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9858 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9862 printf("history forked at %s%s%s%s\n %s\n",
9864 refs_str ? " (" : "", refs_str ? refs_str : "",
9865 refs_str ? ")" : "", yca_brief_str);
9868 free(custom_refs_str);
9869 free(new_commit_id);
9873 free(yca_brief_str);
9875 got_object_commit_close(new_commit);
9877 got_object_commit_close(yca_commit);
9882 static const struct got_error *
9883 process_backup_refs(const char *backup_ref_prefix,
9884 const char *wanted_branch_name,
9885 int delete, struct got_repository *repo)
9887 const struct got_error *err;
9888 struct got_reflist_head refs, backup_refs;
9889 struct got_reflist_entry *re;
9890 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9891 struct got_object_id *old_commit_id = NULL;
9892 char *branch_name = NULL;
9893 struct got_commit_object *old_commit = NULL;
9894 struct got_reflist_object_id_map *refs_idmap = NULL;
9895 int wanted_branch_found = 0;
9898 TAILQ_INIT(&backup_refs);
9900 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9904 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9908 if (wanted_branch_name) {
9909 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9910 wanted_branch_name += 11;
9913 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9914 got_ref_cmp_by_commit_timestamp_descending, repo);
9918 TAILQ_FOREACH(re, &backup_refs, entry) {
9919 const char *refname = got_ref_get_name(re->ref);
9922 err = check_cancelled(NULL);
9926 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9930 err = got_object_open_as_commit(&old_commit, repo,
9935 if (strncmp(backup_ref_prefix, refname,
9936 backup_ref_prefix_len) == 0)
9937 refname += backup_ref_prefix_len;
9939 while (refname[0] == '/')
9942 branch_name = strdup(refname);
9943 if (branch_name == NULL) {
9944 err = got_error_from_errno("strdup");
9947 slash = strrchr(branch_name, '/');
9950 refname += strlen(branch_name) + 1;
9953 if (wanted_branch_name == NULL ||
9954 strcmp(wanted_branch_name, branch_name) == 0) {
9955 wanted_branch_found = 1;
9957 err = delete_backup_ref(re->ref,
9958 old_commit_id, repo);
9960 err = print_backup_ref(branch_name, refname,
9961 old_commit_id, old_commit, refs_idmap,
9968 free(old_commit_id);
9969 old_commit_id = NULL;
9972 got_object_commit_close(old_commit);
9976 if (wanted_branch_name && !wanted_branch_found) {
9977 err = got_error_fmt(GOT_ERR_NOT_REF,
9978 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9982 got_reflist_object_id_map_free(refs_idmap);
9983 got_ref_list_free(&refs);
9984 got_ref_list_free(&backup_refs);
9985 free(old_commit_id);
9988 got_object_commit_close(old_commit);
9992 static const struct got_error *
9993 abort_progress(void *arg, unsigned char status, const char *path)
9996 * Unversioned files should not clutter progress output when
9997 * an operation is aborted.
9999 if (status == GOT_STATUS_UNVERSIONED)
10002 return update_progress(arg, status, path);
10005 static const struct got_error *
10006 cmd_rebase(int argc, char *argv[])
10008 const struct got_error *error = NULL;
10009 struct got_worktree *worktree = NULL;
10010 struct got_repository *repo = NULL;
10011 struct got_fileindex *fileindex = NULL;
10013 struct got_reference *branch = NULL;
10014 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10015 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10016 struct got_object_id *resume_commit_id = NULL;
10017 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10018 struct got_commit_object *commit = NULL;
10019 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10020 int histedit_in_progress = 0, merge_in_progress = 0;
10021 int create_backup = 1, list_backups = 0, delete_backups = 0;
10022 struct got_object_id_queue commits;
10023 struct got_pathlist_head merged_paths;
10024 const struct got_object_id_queue *parent_ids;
10025 struct got_object_qid *qid, *pid;
10026 struct got_update_progress_arg upa;
10027 int *pack_fds = NULL;
10029 STAILQ_INIT(&commits);
10030 TAILQ_INIT(&merged_paths);
10031 memset(&upa, 0, sizeof(upa));
10033 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10039 continue_rebase = 1;
10045 delete_backups = 1;
10057 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10058 "unveil", NULL) == -1)
10061 if (list_backups) {
10063 option_conflict('l', 'a');
10064 if (continue_rebase)
10065 option_conflict('l', 'c');
10066 if (delete_backups)
10067 option_conflict('l', 'X');
10068 if (argc != 0 && argc != 1)
10070 } else if (delete_backups) {
10072 option_conflict('X', 'a');
10073 if (continue_rebase)
10074 option_conflict('X', 'c');
10076 option_conflict('l', 'X');
10077 if (argc != 0 && argc != 1)
10080 if (abort_rebase && continue_rebase)
10082 else if (abort_rebase || continue_rebase) {
10085 } else if (argc != 1)
10089 cwd = getcwd(NULL, 0);
10091 error = got_error_from_errno("getcwd");
10095 error = got_repo_pack_fds_open(&pack_fds);
10099 error = got_worktree_open(&worktree, cwd);
10101 if (list_backups || delete_backups) {
10102 if (error->code != GOT_ERR_NOT_WORKTREE)
10105 if (error->code == GOT_ERR_NOT_WORKTREE)
10106 error = wrap_not_worktree_error(error,
10112 error = got_repo_open(&repo,
10113 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10118 error = apply_unveil(got_repo_get_path(repo), 0,
10119 worktree ? got_worktree_get_root_path(worktree) : NULL);
10123 if (list_backups || delete_backups) {
10124 error = process_backup_refs(
10125 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10126 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10127 goto done; /* nothing else to do */
10130 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10134 if (histedit_in_progress) {
10135 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10139 error = got_worktree_merge_in_progress(&merge_in_progress,
10143 if (merge_in_progress) {
10144 error = got_error(GOT_ERR_MERGE_BUSY);
10148 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10152 if (abort_rebase) {
10153 if (!rebase_in_progress) {
10154 error = got_error(GOT_ERR_NOT_REBASING);
10157 error = got_worktree_rebase_continue(&resume_commit_id,
10158 &new_base_branch, &tmp_branch, &branch, &fileindex,
10162 printf("Switching work tree to %s\n",
10163 got_ref_get_symref_target(new_base_branch));
10164 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10165 new_base_branch, abort_progress, &upa);
10168 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10169 print_merge_progress_stats(&upa);
10170 goto done; /* nothing else to do */
10173 if (continue_rebase) {
10174 if (!rebase_in_progress) {
10175 error = got_error(GOT_ERR_NOT_REBASING);
10178 error = got_worktree_rebase_continue(&resume_commit_id,
10179 &new_base_branch, &tmp_branch, &branch, &fileindex,
10184 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10185 resume_commit_id, repo);
10189 yca_id = got_object_id_dup(resume_commit_id);
10190 if (yca_id == NULL) {
10191 error = got_error_from_errno("got_object_id_dup");
10195 error = got_ref_open(&branch, repo, argv[0], 0);
10200 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10204 if (!continue_rebase) {
10205 struct got_object_id *base_commit_id;
10207 base_commit_id = got_worktree_get_base_commit_id(worktree);
10208 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10209 base_commit_id, branch_head_commit_id, 1, repo,
10210 check_cancelled, NULL);
10213 if (yca_id == NULL) {
10214 error = got_error_msg(GOT_ERR_ANCESTRY,
10215 "specified branch shares no common ancestry "
10216 "with work tree's branch");
10220 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10222 if (error->code != GOT_ERR_ANCESTRY)
10226 struct got_pathlist_head paths;
10227 printf("%s is already based on %s\n",
10228 got_ref_get_name(branch),
10229 got_worktree_get_head_ref_name(worktree));
10230 error = switch_head_ref(branch, branch_head_commit_id,
10234 error = got_worktree_set_base_commit_id(worktree, repo,
10235 branch_head_commit_id);
10238 TAILQ_INIT(&paths);
10239 error = got_pathlist_append(&paths, "", NULL);
10242 error = got_worktree_checkout_files(worktree,
10243 &paths, repo, update_progress, &upa,
10244 check_cancelled, NULL);
10245 got_pathlist_free(&paths);
10248 if (upa.did_something) {
10250 error = got_object_id_str(&id_str,
10251 branch_head_commit_id);
10254 printf("Updated to %s: %s\n",
10255 got_worktree_get_head_ref_name(worktree),
10259 printf("Already up-to-date\n");
10260 print_update_progress_stats(&upa);
10265 commit_id = branch_head_commit_id;
10266 error = got_object_open_as_commit(&commit, repo, commit_id);
10270 parent_ids = got_object_commit_get_parent_ids(commit);
10271 pid = STAILQ_FIRST(parent_ids);
10273 error = got_error(GOT_ERR_EMPTY_REBASE);
10276 error = collect_commits(&commits, commit_id, &pid->id,
10277 yca_id, got_worktree_get_path_prefix(worktree),
10278 GOT_ERR_REBASE_PATH, repo);
10279 got_object_commit_close(commit);
10284 if (!continue_rebase) {
10285 error = got_worktree_rebase_prepare(&new_base_branch,
10286 &tmp_branch, &fileindex, worktree, branch, repo);
10291 if (STAILQ_EMPTY(&commits)) {
10292 if (continue_rebase) {
10293 error = rebase_complete(worktree, fileindex,
10294 branch, new_base_branch, tmp_branch, repo,
10298 /* Fast-forward the reference of the branch. */
10299 struct got_object_id *new_head_commit_id;
10301 error = got_ref_resolve(&new_head_commit_id, repo,
10305 error = got_object_id_str(&id_str, new_head_commit_id);
10306 printf("Forwarding %s to commit %s\n",
10307 got_ref_get_name(branch), id_str);
10309 error = got_ref_change_ref(branch,
10310 new_head_commit_id);
10313 /* No backup needed since objects did not change. */
10319 STAILQ_FOREACH(qid, &commits, entry) {
10321 commit_id = &qid->id;
10322 parent_id = pid ? &pid->id : yca_id;
10325 memset(&upa, 0, sizeof(upa));
10326 error = got_worktree_rebase_merge_files(&merged_paths,
10327 worktree, fileindex, parent_id, commit_id, repo,
10328 update_progress, &upa, check_cancelled, NULL);
10332 print_merge_progress_stats(&upa);
10333 if (upa.conflicts > 0 || upa.missing > 0 ||
10334 upa.not_deleted > 0 || upa.unversioned > 0) {
10335 if (upa.conflicts > 0) {
10336 error = show_rebase_merge_conflict(&qid->id,
10341 got_worktree_rebase_pathlist_free(&merged_paths);
10345 error = rebase_commit(&merged_paths, worktree, fileindex,
10346 tmp_branch, commit_id, repo);
10347 got_worktree_rebase_pathlist_free(&merged_paths);
10352 if (upa.conflicts > 0 || upa.missing > 0 ||
10353 upa.not_deleted > 0 || upa.unversioned > 0) {
10354 error = got_worktree_rebase_postpone(worktree, fileindex);
10357 if (upa.conflicts > 0 && upa.missing == 0 &&
10358 upa.not_deleted == 0 && upa.unversioned == 0) {
10359 error = got_error_msg(GOT_ERR_CONFLICTS,
10360 "conflicts must be resolved before rebasing "
10362 } else if (upa.conflicts > 0) {
10363 error = got_error_msg(GOT_ERR_CONFLICTS,
10364 "conflicts must be resolved before rebasing "
10365 "can continue; changes destined for some "
10366 "files were not yet merged and should be "
10367 "merged manually if required before the "
10368 "rebase operation is continued");
10370 error = got_error_msg(GOT_ERR_CONFLICTS,
10371 "changes destined for some files were not "
10372 "yet merged and should be merged manually "
10373 "if required before the rebase operation "
10377 error = rebase_complete(worktree, fileindex, branch,
10378 new_base_branch, tmp_branch, repo, create_backup);
10380 got_object_id_queue_free(&commits);
10381 free(branch_head_commit_id);
10382 free(resume_commit_id);
10385 got_object_commit_close(commit);
10387 got_ref_close(branch);
10388 if (new_base_branch)
10389 got_ref_close(new_base_branch);
10391 got_ref_close(tmp_branch);
10393 got_worktree_close(worktree);
10395 const struct got_error *close_err = got_repo_close(repo);
10400 const struct got_error *pack_err =
10401 got_repo_pack_fds_close(pack_fds);
10409 usage_histedit(void)
10411 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10412 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10417 #define GOT_HISTEDIT_PICK 'p'
10418 #define GOT_HISTEDIT_EDIT 'e'
10419 #define GOT_HISTEDIT_FOLD 'f'
10420 #define GOT_HISTEDIT_DROP 'd'
10421 #define GOT_HISTEDIT_MESG 'm'
10423 static const struct got_histedit_cmd {
10424 unsigned char code;
10427 } got_histedit_cmds[] = {
10428 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10429 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10430 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10432 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10433 { GOT_HISTEDIT_MESG, "mesg",
10434 "single-line log message for commit above (open editor if empty)" },
10437 struct got_histedit_list_entry {
10438 TAILQ_ENTRY(got_histedit_list_entry) entry;
10439 struct got_object_id *commit_id;
10440 const struct got_histedit_cmd *cmd;
10443 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10445 static const struct got_error *
10446 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10447 FILE *f, struct got_repository *repo)
10449 const struct got_error *err = NULL;
10450 char *logmsg = NULL, *id_str = NULL;
10451 struct got_commit_object *commit = NULL;
10454 err = got_object_open_as_commit(&commit, repo, commit_id);
10458 err = get_short_logmsg(&logmsg, 34, commit);
10462 err = got_object_id_str(&id_str, commit_id);
10466 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10468 err = got_ferror(f, GOT_ERR_IO);
10471 got_object_commit_close(commit);
10477 static const struct got_error *
10478 histedit_write_commit_list(struct got_object_id_queue *commits,
10479 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10480 struct got_repository *repo)
10482 const struct got_error *err = NULL;
10483 struct got_object_qid *qid;
10484 const char *histedit_cmd = NULL;
10486 if (STAILQ_EMPTY(commits))
10487 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10489 STAILQ_FOREACH(qid, commits, entry) {
10490 histedit_cmd = got_histedit_cmds[0].name;
10492 histedit_cmd = "edit";
10493 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10494 histedit_cmd = "fold";
10495 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10498 if (edit_logmsg_only) {
10499 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10501 err = got_ferror(f, GOT_ERR_IO);
10510 static const struct got_error *
10511 write_cmd_list(FILE *f, const char *branch_name,
10512 struct got_object_id_queue *commits)
10514 const struct got_error *err = NULL;
10518 struct got_object_qid *qid;
10520 qid = STAILQ_FIRST(commits);
10521 err = got_object_id_str(&id_str, &qid->id);
10526 "# Editing the history of branch '%s' starting at\n"
10528 "# Commits will be processed in order from top to "
10529 "bottom of this file.\n", branch_name, id_str);
10531 err = got_ferror(f, GOT_ERR_IO);
10535 n = fprintf(f, "# Available histedit commands:\n");
10537 err = got_ferror(f, GOT_ERR_IO);
10541 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10542 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10543 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10546 err = got_ferror(f, GOT_ERR_IO);
10555 static const struct got_error *
10556 histedit_syntax_error(int lineno)
10558 static char msg[42];
10561 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10563 if (ret == -1 || ret >= sizeof(msg))
10564 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10566 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10569 static const struct got_error *
10570 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10571 char *logmsg, struct got_repository *repo)
10573 const struct got_error *err;
10574 struct got_commit_object *folded_commit = NULL;
10575 char *id_str, *folded_logmsg = NULL;
10577 err = got_object_id_str(&id_str, hle->commit_id);
10581 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10585 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10588 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10589 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10590 folded_logmsg) == -1) {
10591 err = got_error_from_errno("asprintf");
10595 got_object_commit_close(folded_commit);
10597 free(folded_logmsg);
10601 static struct got_histedit_list_entry *
10602 get_folded_commits(struct got_histedit_list_entry *hle)
10604 struct got_histedit_list_entry *prev, *folded = NULL;
10606 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10607 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10608 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10609 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10611 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10617 static const struct got_error *
10618 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10619 struct got_repository *repo)
10621 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10622 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10623 const struct got_error *err = NULL;
10624 struct got_commit_object *commit = NULL;
10627 struct got_histedit_list_entry *folded = NULL;
10629 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10633 folded = get_folded_commits(hle);
10635 while (folded != hle) {
10636 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10637 folded = TAILQ_NEXT(folded, entry);
10640 err = append_folded_commit_msg(&new_msg, folded,
10646 folded = TAILQ_NEXT(folded, entry);
10650 err = got_object_id_str(&id_str, hle->commit_id);
10653 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10656 logmsg_len = asprintf(&new_msg,
10657 "%s\n# original log message of commit %s: %s",
10658 logmsg ? logmsg : "", id_str, orig_logmsg);
10659 if (logmsg_len == -1) {
10660 err = got_error_from_errno("asprintf");
10666 err = got_object_id_str(&id_str, hle->commit_id);
10670 err = got_opentemp_named_fd(&logmsg_path, &fd,
10671 GOT_TMPDIR_STR "/got-logmsg");
10675 write(fd, logmsg, logmsg_len);
10678 err = get_editor(&editor);
10682 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10685 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10688 hle->logmsg = strdup(new_msg);
10689 if (hle->logmsg == NULL)
10690 err = got_error_from_errno("strdup");
10693 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10694 err = got_error_from_errno2("unlink", logmsg_path);
10700 got_object_commit_close(commit);
10704 static const struct got_error *
10705 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10706 FILE *f, struct got_repository *repo)
10708 const struct got_error *err = NULL;
10709 char *line = NULL, *p, *end;
10713 const struct got_histedit_cmd *cmd;
10714 struct got_object_id *commit_id = NULL;
10715 struct got_histedit_list_entry *hle = NULL;
10718 len = getline(&line, &size, f);
10720 const struct got_error *getline_err;
10723 getline_err = got_error_from_errno("getline");
10724 err = got_ferror(f, getline_err->code);
10729 while (isspace((unsigned char)p[0]))
10731 if (p[0] == '#' || p[0] == '\0') {
10737 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10738 cmd = &got_histedit_cmds[i];
10739 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10740 isspace((unsigned char)p[strlen(cmd->name)])) {
10741 p += strlen(cmd->name);
10744 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10749 if (i == nitems(got_histedit_cmds)) {
10750 err = histedit_syntax_error(lineno);
10753 while (isspace((unsigned char)p[0]))
10755 if (cmd->code == GOT_HISTEDIT_MESG) {
10756 if (hle == NULL || hle->logmsg != NULL) {
10757 err = got_error(GOT_ERR_HISTEDIT_CMD);
10760 if (p[0] == '\0') {
10761 err = histedit_edit_logmsg(hle, repo);
10765 hle->logmsg = strdup(p);
10766 if (hle->logmsg == NULL) {
10767 err = got_error_from_errno("strdup");
10776 while (end[0] && !isspace((unsigned char)end[0]))
10780 err = got_object_resolve_id_str(&commit_id, repo, p);
10782 /* override error code */
10783 err = histedit_syntax_error(lineno);
10787 hle = malloc(sizeof(*hle));
10789 err = got_error_from_errno("malloc");
10793 hle->commit_id = commit_id;
10794 hle->logmsg = NULL;
10798 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10806 static const struct got_error *
10807 histedit_check_script(struct got_histedit_list *histedit_cmds,
10808 struct got_object_id_queue *commits, struct got_repository *repo)
10810 const struct got_error *err = NULL;
10811 struct got_object_qid *qid;
10812 struct got_histedit_list_entry *hle;
10813 static char msg[92];
10816 if (TAILQ_EMPTY(histedit_cmds))
10817 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10818 "histedit script contains no commands");
10819 if (STAILQ_EMPTY(commits))
10820 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10822 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10823 struct got_histedit_list_entry *hle2;
10824 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10827 if (got_object_id_cmp(hle->commit_id,
10828 hle2->commit_id) != 0)
10830 err = got_object_id_str(&id_str, hle->commit_id);
10833 snprintf(msg, sizeof(msg), "commit %s is listed "
10834 "more than once in histedit script", id_str);
10836 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10840 STAILQ_FOREACH(qid, commits, entry) {
10841 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10842 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10846 err = got_object_id_str(&id_str, &qid->id);
10849 snprintf(msg, sizeof(msg),
10850 "commit %s missing from histedit script", id_str);
10852 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10856 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10857 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10858 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10859 "last commit in histedit script cannot be folded");
10864 static const struct got_error *
10865 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10866 const char *path, struct got_object_id_queue *commits,
10867 struct got_repository *repo)
10869 const struct got_error *err = NULL;
10873 err = get_editor(&editor);
10877 if (spawn_editor(editor, path) == -1) {
10878 err = got_error_from_errno("failed spawning editor");
10882 f = fopen(path, "re");
10884 err = got_error_from_errno("fopen");
10887 err = histedit_parse_list(histedit_cmds, f, repo);
10891 err = histedit_check_script(histedit_cmds, commits, repo);
10893 if (f && fclose(f) == EOF && err == NULL)
10894 err = got_error_from_errno("fclose");
10899 static const struct got_error *
10900 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10901 struct got_object_id_queue *, const char *, const char *,
10902 struct got_repository *);
10904 static const struct got_error *
10905 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10906 struct got_object_id_queue *commits, const char *branch_name,
10907 int edit_logmsg_only, int fold_only, int edit_only,
10908 struct got_repository *repo)
10910 const struct got_error *err;
10914 err = got_opentemp_named(&path, &f, "got-histedit");
10918 err = write_cmd_list(f, branch_name, commits);
10922 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10923 fold_only, edit_only, repo);
10927 if (edit_logmsg_only || fold_only || edit_only) {
10929 err = histedit_parse_list(histedit_cmds, f, repo);
10931 if (fclose(f) == EOF) {
10932 err = got_error_from_errno("fclose");
10936 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10938 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10939 err->code != GOT_ERR_HISTEDIT_CMD)
10941 err = histedit_edit_list_retry(histedit_cmds, err,
10942 commits, path, branch_name, repo);
10946 if (f && fclose(f) == EOF && err == NULL)
10947 err = got_error_from_errno("fclose");
10948 if (path && unlink(path) != 0 && err == NULL)
10949 err = got_error_from_errno2("unlink", path);
10954 static const struct got_error *
10955 histedit_save_list(struct got_histedit_list *histedit_cmds,
10956 struct got_worktree *worktree, struct got_repository *repo)
10958 const struct got_error *err = NULL;
10961 struct got_histedit_list_entry *hle;
10962 struct got_commit_object *commit = NULL;
10964 err = got_worktree_get_histedit_script_path(&path, worktree);
10968 f = fopen(path, "we");
10970 err = got_error_from_errno2("fopen", path);
10973 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10974 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10980 int n = fprintf(f, "%c %s\n",
10981 GOT_HISTEDIT_MESG, hle->logmsg);
10983 err = got_ferror(f, GOT_ERR_IO);
10989 if (f && fclose(f) == EOF && err == NULL)
10990 err = got_error_from_errno("fclose");
10993 got_object_commit_close(commit);
10998 histedit_free_list(struct got_histedit_list *histedit_cmds)
11000 struct got_histedit_list_entry *hle;
11002 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11003 TAILQ_REMOVE(histedit_cmds, hle, entry);
11008 static const struct got_error *
11009 histedit_load_list(struct got_histedit_list *histedit_cmds,
11010 const char *path, struct got_repository *repo)
11012 const struct got_error *err = NULL;
11015 f = fopen(path, "re");
11017 err = got_error_from_errno2("fopen", path);
11021 err = histedit_parse_list(histedit_cmds, f, repo);
11023 if (f && fclose(f) == EOF && err == NULL)
11024 err = got_error_from_errno("fclose");
11028 static const struct got_error *
11029 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11030 const struct got_error *edit_err, struct got_object_id_queue *commits,
11031 const char *path, const char *branch_name, struct got_repository *repo)
11033 const struct got_error *err = NULL, *prev_err = edit_err;
11036 while (resp != 'c' && resp != 'r' && resp != 'a') {
11037 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11038 "or (a)bort: ", getprogname(), prev_err->msg);
11043 histedit_free_list(histedit_cmds);
11044 err = histedit_run_editor(histedit_cmds, path, commits,
11047 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11048 err->code != GOT_ERR_HISTEDIT_CMD)
11055 } else if (resp == 'r') {
11056 histedit_free_list(histedit_cmds);
11057 err = histedit_edit_script(histedit_cmds,
11058 commits, branch_name, 0, 0, 0, repo);
11060 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11061 err->code != GOT_ERR_HISTEDIT_CMD)
11068 } else if (resp == 'a') {
11069 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11072 printf("invalid response '%c'\n", resp);
11078 static const struct got_error *
11079 histedit_complete(struct got_worktree *worktree,
11080 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11081 struct got_reference *branch, struct got_repository *repo)
11083 printf("Switching work tree to %s\n",
11084 got_ref_get_symref_target(branch));
11085 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11089 static const struct got_error *
11090 show_histedit_progress(struct got_commit_object *commit,
11091 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11093 const struct got_error *err;
11094 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11096 err = got_object_id_str(&old_id_str, hle->commit_id);
11101 err = got_object_id_str(&new_id_str, new_id);
11106 old_id_str[12] = '\0';
11108 new_id_str[12] = '\0';
11111 logmsg = strdup(hle->logmsg);
11112 if (logmsg == NULL) {
11113 err = got_error_from_errno("strdup");
11116 trim_logmsg(logmsg, 42);
11118 err = get_short_logmsg(&logmsg, 42, commit);
11123 switch (hle->cmd->code) {
11124 case GOT_HISTEDIT_PICK:
11125 case GOT_HISTEDIT_EDIT:
11126 printf("%s -> %s: %s\n", old_id_str,
11127 new_id_str ? new_id_str : "no-op change", logmsg);
11129 case GOT_HISTEDIT_DROP:
11130 case GOT_HISTEDIT_FOLD:
11131 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11143 static const struct got_error *
11144 histedit_commit(struct got_pathlist_head *merged_paths,
11145 struct got_worktree *worktree, struct got_fileindex *fileindex,
11146 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11147 struct got_repository *repo)
11149 const struct got_error *err;
11150 struct got_commit_object *commit;
11151 struct got_object_id *new_commit_id;
11153 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11154 && hle->logmsg == NULL) {
11155 err = histedit_edit_logmsg(hle, repo);
11160 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11164 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11165 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11166 hle->logmsg, repo);
11168 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11170 err = show_histedit_progress(commit, hle, NULL);
11172 err = show_histedit_progress(commit, hle, new_commit_id);
11173 free(new_commit_id);
11176 got_object_commit_close(commit);
11180 static const struct got_error *
11181 histedit_skip_commit(struct got_histedit_list_entry *hle,
11182 struct got_worktree *worktree, struct got_repository *repo)
11184 const struct got_error *error;
11185 struct got_commit_object *commit;
11187 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11192 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11196 error = show_histedit_progress(commit, hle, NULL);
11197 got_object_commit_close(commit);
11201 static const struct got_error *
11202 check_local_changes(void *arg, unsigned char status,
11203 unsigned char staged_status, const char *path,
11204 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11205 struct got_object_id *commit_id, int dirfd, const char *de_name)
11207 int *have_local_changes = arg;
11210 case GOT_STATUS_ADD:
11211 case GOT_STATUS_DELETE:
11212 case GOT_STATUS_MODIFY:
11213 case GOT_STATUS_CONFLICT:
11214 *have_local_changes = 1;
11215 return got_error(GOT_ERR_CANCELLED);
11220 switch (staged_status) {
11221 case GOT_STATUS_ADD:
11222 case GOT_STATUS_DELETE:
11223 case GOT_STATUS_MODIFY:
11224 *have_local_changes = 1;
11225 return got_error(GOT_ERR_CANCELLED);
11233 static const struct got_error *
11234 cmd_histedit(int argc, char *argv[])
11236 const struct got_error *error = NULL;
11237 struct got_worktree *worktree = NULL;
11238 struct got_fileindex *fileindex = NULL;
11239 struct got_repository *repo = NULL;
11241 struct got_reference *branch = NULL;
11242 struct got_reference *tmp_branch = NULL;
11243 struct got_object_id *resume_commit_id = NULL;
11244 struct got_object_id *base_commit_id = NULL;
11245 struct got_object_id *head_commit_id = NULL;
11246 struct got_commit_object *commit = NULL;
11247 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11248 struct got_update_progress_arg upa;
11249 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11250 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11251 int list_backups = 0, delete_backups = 0;
11252 const char *edit_script_path = NULL;
11253 struct got_object_id_queue commits;
11254 struct got_pathlist_head merged_paths;
11255 const struct got_object_id_queue *parent_ids;
11256 struct got_object_qid *pid;
11257 struct got_histedit_list histedit_cmds;
11258 struct got_histedit_list_entry *hle;
11259 int *pack_fds = NULL;
11261 STAILQ_INIT(&commits);
11262 TAILQ_INIT(&histedit_cmds);
11263 TAILQ_INIT(&merged_paths);
11264 memset(&upa, 0, sizeof(upa));
11266 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11281 edit_script_path = optarg;
11284 edit_logmsg_only = 1;
11290 delete_backups = 1;
11302 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11303 "unveil", NULL) == -1)
11306 if (abort_edit && continue_edit)
11307 option_conflict('a', 'c');
11308 if (edit_script_path && edit_logmsg_only)
11309 option_conflict('F', 'm');
11310 if (abort_edit && edit_logmsg_only)
11311 option_conflict('a', 'm');
11312 if (continue_edit && edit_logmsg_only)
11313 option_conflict('c', 'm');
11314 if (abort_edit && fold_only)
11315 option_conflict('a', 'f');
11316 if (continue_edit && fold_only)
11317 option_conflict('c', 'f');
11318 if (fold_only && edit_logmsg_only)
11319 option_conflict('f', 'm');
11320 if (edit_script_path && fold_only)
11321 option_conflict('F', 'f');
11322 if (abort_edit && edit_only)
11323 option_conflict('a', 'e');
11324 if (continue_edit && edit_only)
11325 option_conflict('c', 'e');
11326 if (edit_only && edit_logmsg_only)
11327 option_conflict('e', 'm');
11328 if (edit_script_path && edit_only)
11329 option_conflict('F', 'e');
11330 if (list_backups) {
11332 option_conflict('l', 'a');
11334 option_conflict('l', 'c');
11335 if (edit_script_path)
11336 option_conflict('l', 'F');
11337 if (edit_logmsg_only)
11338 option_conflict('l', 'm');
11340 option_conflict('l', 'f');
11342 option_conflict('l', 'e');
11343 if (delete_backups)
11344 option_conflict('l', 'X');
11345 if (argc != 0 && argc != 1)
11347 } else if (delete_backups) {
11349 option_conflict('X', 'a');
11351 option_conflict('X', 'c');
11352 if (edit_script_path)
11353 option_conflict('X', 'F');
11354 if (edit_logmsg_only)
11355 option_conflict('X', 'm');
11357 option_conflict('X', 'f');
11359 option_conflict('X', 'e');
11361 option_conflict('X', 'l');
11362 if (argc != 0 && argc != 1)
11364 } else if (argc != 0)
11368 * This command cannot apply unveil(2) in all cases because the
11369 * user may choose to run an editor to edit the histedit script
11370 * and to edit individual commit log messages.
11371 * unveil(2) traverses exec(2); if an editor is used we have to
11372 * apply unveil after edit script and log messages have been written.
11373 * XXX TODO: Make use of unveil(2) where possible.
11376 cwd = getcwd(NULL, 0);
11378 error = got_error_from_errno("getcwd");
11382 error = got_repo_pack_fds_open(&pack_fds);
11386 error = got_worktree_open(&worktree, cwd);
11388 if (list_backups || delete_backups) {
11389 if (error->code != GOT_ERR_NOT_WORKTREE)
11392 if (error->code == GOT_ERR_NOT_WORKTREE)
11393 error = wrap_not_worktree_error(error,
11399 if (list_backups || delete_backups) {
11400 error = got_repo_open(&repo,
11401 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11405 error = apply_unveil(got_repo_get_path(repo), 0,
11406 worktree ? got_worktree_get_root_path(worktree) : NULL);
11409 error = process_backup_refs(
11410 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11411 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11412 goto done; /* nothing else to do */
11415 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11420 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11423 if (rebase_in_progress) {
11424 error = got_error(GOT_ERR_REBASING);
11428 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11432 if (merge_in_progress) {
11433 error = got_error(GOT_ERR_MERGE_BUSY);
11437 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11441 if (edit_in_progress && edit_logmsg_only) {
11442 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11443 "histedit operation is in progress in this "
11444 "work tree and must be continued or aborted "
11445 "before the -m option can be used");
11448 if (edit_in_progress && fold_only) {
11449 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11450 "histedit operation is in progress in this "
11451 "work tree and must be continued or aborted "
11452 "before the -f option can be used");
11455 if (edit_in_progress && edit_only) {
11456 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11457 "histedit operation is in progress in this "
11458 "work tree and must be continued or aborted "
11459 "before the -e option can be used");
11463 if (edit_in_progress && abort_edit) {
11464 error = got_worktree_histedit_continue(&resume_commit_id,
11465 &tmp_branch, &branch, &base_commit_id, &fileindex,
11469 printf("Switching work tree to %s\n",
11470 got_ref_get_symref_target(branch));
11471 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11472 branch, base_commit_id, abort_progress, &upa);
11475 printf("Histedit of %s aborted\n",
11476 got_ref_get_symref_target(branch));
11477 print_merge_progress_stats(&upa);
11478 goto done; /* nothing else to do */
11479 } else if (abort_edit) {
11480 error = got_error(GOT_ERR_NOT_HISTEDIT);
11484 if (continue_edit) {
11487 if (!edit_in_progress) {
11488 error = got_error(GOT_ERR_NOT_HISTEDIT);
11492 error = got_worktree_get_histedit_script_path(&path, worktree);
11496 error = histedit_load_list(&histedit_cmds, path, repo);
11501 error = got_worktree_histedit_continue(&resume_commit_id,
11502 &tmp_branch, &branch, &base_commit_id, &fileindex,
11507 error = got_ref_resolve(&head_commit_id, repo, branch);
11511 error = got_object_open_as_commit(&commit, repo,
11515 parent_ids = got_object_commit_get_parent_ids(commit);
11516 pid = STAILQ_FIRST(parent_ids);
11518 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11521 error = collect_commits(&commits, head_commit_id, &pid->id,
11522 base_commit_id, got_worktree_get_path_prefix(worktree),
11523 GOT_ERR_HISTEDIT_PATH, repo);
11524 got_object_commit_close(commit);
11529 if (edit_in_progress) {
11530 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11534 error = got_ref_open(&branch, repo,
11535 got_worktree_get_head_ref_name(worktree), 0);
11539 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11540 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11541 "will not edit commit history of a branch outside "
11542 "the \"refs/heads/\" reference namespace");
11546 error = got_ref_resolve(&head_commit_id, repo, branch);
11547 got_ref_close(branch);
11552 error = got_object_open_as_commit(&commit, repo,
11556 parent_ids = got_object_commit_get_parent_ids(commit);
11557 pid = STAILQ_FIRST(parent_ids);
11559 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11562 error = collect_commits(&commits, head_commit_id, &pid->id,
11563 got_worktree_get_base_commit_id(worktree),
11564 got_worktree_get_path_prefix(worktree),
11565 GOT_ERR_HISTEDIT_PATH, repo);
11566 got_object_commit_close(commit);
11571 if (STAILQ_EMPTY(&commits)) {
11572 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11576 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11577 &base_commit_id, &fileindex, worktree, repo);
11581 if (edit_script_path) {
11582 error = histedit_load_list(&histedit_cmds,
11583 edit_script_path, repo);
11585 got_worktree_histedit_abort(worktree, fileindex,
11586 repo, branch, base_commit_id,
11587 abort_progress, &upa);
11588 print_merge_progress_stats(&upa);
11592 const char *branch_name;
11593 branch_name = got_ref_get_symref_target(branch);
11594 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11596 error = histedit_edit_script(&histedit_cmds, &commits,
11597 branch_name, edit_logmsg_only, fold_only,
11600 got_worktree_histedit_abort(worktree, fileindex,
11601 repo, branch, base_commit_id,
11602 abort_progress, &upa);
11603 print_merge_progress_stats(&upa);
11609 error = histedit_save_list(&histedit_cmds, worktree,
11612 got_worktree_histedit_abort(worktree, fileindex,
11613 repo, branch, base_commit_id,
11614 abort_progress, &upa);
11615 print_merge_progress_stats(&upa);
11621 error = histedit_check_script(&histedit_cmds, &commits, repo);
11625 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11626 if (resume_commit_id) {
11627 if (got_object_id_cmp(hle->commit_id,
11628 resume_commit_id) != 0)
11631 resume_commit_id = NULL;
11632 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11633 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11634 error = histedit_skip_commit(hle, worktree,
11639 struct got_pathlist_head paths;
11640 int have_changes = 0;
11642 TAILQ_INIT(&paths);
11643 error = got_pathlist_append(&paths, "", NULL);
11646 error = got_worktree_status(worktree, &paths,
11647 repo, 0, check_local_changes, &have_changes,
11648 check_cancelled, NULL);
11649 got_pathlist_free(&paths);
11651 if (error->code != GOT_ERR_CANCELLED)
11653 if (sigint_received || sigpipe_received)
11656 if (have_changes) {
11657 error = histedit_commit(NULL, worktree,
11658 fileindex, tmp_branch, hle, repo);
11662 error = got_object_open_as_commit(
11663 &commit, repo, hle->commit_id);
11666 error = show_histedit_progress(commit,
11668 got_object_commit_close(commit);
11677 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11678 error = histedit_skip_commit(hle, worktree, repo);
11684 error = got_object_open_as_commit(&commit, repo,
11688 parent_ids = got_object_commit_get_parent_ids(commit);
11689 pid = STAILQ_FIRST(parent_ids);
11691 error = got_worktree_histedit_merge_files(&merged_paths,
11692 worktree, fileindex, &pid->id, hle->commit_id, repo,
11693 update_progress, &upa, check_cancelled, NULL);
11696 got_object_commit_close(commit);
11699 print_merge_progress_stats(&upa);
11700 if (upa.conflicts > 0 || upa.missing > 0 ||
11701 upa.not_deleted > 0 || upa.unversioned > 0) {
11702 if (upa.conflicts > 0) {
11703 error = show_rebase_merge_conflict(
11704 hle->commit_id, repo);
11708 got_worktree_rebase_pathlist_free(&merged_paths);
11712 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11714 error = got_object_id_str(&id_str, hle->commit_id);
11717 printf("Stopping histedit for amending commit %s\n",
11720 got_worktree_rebase_pathlist_free(&merged_paths);
11721 error = got_worktree_histedit_postpone(worktree,
11726 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11727 error = histedit_skip_commit(hle, worktree, repo);
11733 error = histedit_commit(&merged_paths, worktree, fileindex,
11734 tmp_branch, hle, repo);
11735 got_worktree_rebase_pathlist_free(&merged_paths);
11740 if (upa.conflicts > 0 || upa.missing > 0 ||
11741 upa.not_deleted > 0 || upa.unversioned > 0) {
11742 error = got_worktree_histedit_postpone(worktree, fileindex);
11745 if (upa.conflicts > 0 && upa.missing == 0 &&
11746 upa.not_deleted == 0 && upa.unversioned == 0) {
11747 error = got_error_msg(GOT_ERR_CONFLICTS,
11748 "conflicts must be resolved before histedit "
11750 } else if (upa.conflicts > 0) {
11751 error = got_error_msg(GOT_ERR_CONFLICTS,
11752 "conflicts must be resolved before histedit "
11753 "can continue; changes destined for some "
11754 "files were not yet merged and should be "
11755 "merged manually if required before the "
11756 "histedit operation is continued");
11758 error = got_error_msg(GOT_ERR_CONFLICTS,
11759 "changes destined for some files were not "
11760 "yet merged and should be merged manually "
11761 "if required before the histedit operation "
11765 error = histedit_complete(worktree, fileindex, tmp_branch,
11768 got_object_id_queue_free(&commits);
11769 histedit_free_list(&histedit_cmds);
11770 free(head_commit_id);
11771 free(base_commit_id);
11772 free(resume_commit_id);
11774 got_object_commit_close(commit);
11776 got_ref_close(branch);
11778 got_ref_close(tmp_branch);
11780 got_worktree_close(worktree);
11782 const struct got_error *close_err = got_repo_close(repo);
11787 const struct got_error *pack_err =
11788 got_repo_pack_fds_close(pack_fds);
11796 usage_integrate(void)
11798 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11802 static const struct got_error *
11803 cmd_integrate(int argc, char *argv[])
11805 const struct got_error *error = NULL;
11806 struct got_repository *repo = NULL;
11807 struct got_worktree *worktree = NULL;
11808 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11809 const char *branch_arg = NULL;
11810 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11811 struct got_fileindex *fileindex = NULL;
11812 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11814 struct got_update_progress_arg upa;
11815 int *pack_fds = NULL;
11817 while ((ch = getopt(argc, argv, "")) != -1) {
11830 branch_arg = argv[0];
11832 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11833 "unveil", NULL) == -1)
11836 cwd = getcwd(NULL, 0);
11838 error = got_error_from_errno("getcwd");
11842 error = got_repo_pack_fds_open(&pack_fds);
11846 error = got_worktree_open(&worktree, cwd);
11848 if (error->code == GOT_ERR_NOT_WORKTREE)
11849 error = wrap_not_worktree_error(error, "integrate",
11854 error = check_rebase_or_histedit_in_progress(worktree);
11858 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11863 error = apply_unveil(got_repo_get_path(repo), 0,
11864 got_worktree_get_root_path(worktree));
11868 error = check_merge_in_progress(worktree, repo);
11872 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11873 error = got_error_from_errno("asprintf");
11877 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11878 &base_branch_ref, worktree, refname, repo);
11882 refname = strdup(got_ref_get_name(branch_ref));
11883 if (refname == NULL) {
11884 error = got_error_from_errno("strdup");
11885 got_worktree_integrate_abort(worktree, fileindex, repo,
11886 branch_ref, base_branch_ref);
11889 base_refname = strdup(got_ref_get_name(base_branch_ref));
11890 if (base_refname == NULL) {
11891 error = got_error_from_errno("strdup");
11892 got_worktree_integrate_abort(worktree, fileindex, repo,
11893 branch_ref, base_branch_ref);
11897 error = got_ref_resolve(&commit_id, repo, branch_ref);
11901 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11905 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11906 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11907 "specified branch has already been integrated");
11908 got_worktree_integrate_abort(worktree, fileindex, repo,
11909 branch_ref, base_branch_ref);
11913 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11915 if (error->code == GOT_ERR_ANCESTRY)
11916 error = got_error(GOT_ERR_REBASE_REQUIRED);
11917 got_worktree_integrate_abort(worktree, fileindex, repo,
11918 branch_ref, base_branch_ref);
11922 memset(&upa, 0, sizeof(upa));
11923 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11924 branch_ref, base_branch_ref, update_progress, &upa,
11925 check_cancelled, NULL);
11929 printf("Integrated %s into %s\n", refname, base_refname);
11930 print_update_progress_stats(&upa);
11933 const struct got_error *close_err = got_repo_close(repo);
11938 got_worktree_close(worktree);
11940 const struct got_error *pack_err =
11941 got_repo_pack_fds_close(pack_fds);
11946 free(base_commit_id);
11949 free(base_refname);
11956 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11961 static const struct got_error *
11962 cmd_merge(int argc, char *argv[])
11964 const struct got_error *error = NULL;
11965 struct got_worktree *worktree = NULL;
11966 struct got_repository *repo = NULL;
11967 struct got_fileindex *fileindex = NULL;
11968 char *cwd = NULL, *id_str = NULL, *author = NULL;
11969 struct got_reference *branch = NULL, *wt_branch = NULL;
11970 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11971 struct got_object_id *wt_branch_tip = NULL;
11972 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11973 int interrupt_merge = 0;
11974 struct got_update_progress_arg upa;
11975 struct got_object_id *merge_commit_id = NULL;
11976 char *branch_name = NULL;
11977 int *pack_fds = NULL;
11979 memset(&upa, 0, sizeof(upa));
11981 while ((ch = getopt(argc, argv, "acn")) != -1) {
11987 continue_merge = 1;
11990 interrupt_merge = 1;
12002 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12003 "unveil", NULL) == -1)
12007 if (abort_merge && continue_merge)
12008 option_conflict('a', 'c');
12009 if (abort_merge || continue_merge) {
12012 } else if (argc != 1)
12015 cwd = getcwd(NULL, 0);
12017 error = got_error_from_errno("getcwd");
12021 error = got_repo_pack_fds_open(&pack_fds);
12025 error = got_worktree_open(&worktree, cwd);
12027 if (error->code == GOT_ERR_NOT_WORKTREE)
12028 error = wrap_not_worktree_error(error,
12033 error = got_repo_open(&repo,
12034 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12039 error = apply_unveil(got_repo_get_path(repo), 0,
12040 worktree ? got_worktree_get_root_path(worktree) : NULL);
12044 error = check_rebase_or_histedit_in_progress(worktree);
12048 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12054 if (!merge_in_progress) {
12055 error = got_error(GOT_ERR_NOT_MERGING);
12058 error = got_worktree_merge_continue(&branch_name,
12059 &branch_tip, &fileindex, worktree, repo);
12062 error = got_worktree_merge_abort(worktree, fileindex, repo,
12063 abort_progress, &upa);
12066 printf("Merge of %s aborted\n", branch_name);
12067 goto done; /* nothing else to do */
12070 error = get_author(&author, repo, worktree);
12074 if (continue_merge) {
12075 if (!merge_in_progress) {
12076 error = got_error(GOT_ERR_NOT_MERGING);
12079 error = got_worktree_merge_continue(&branch_name,
12080 &branch_tip, &fileindex, worktree, repo);
12084 error = got_ref_open(&branch, repo, argv[0], 0);
12087 branch_name = strdup(got_ref_get_name(branch));
12088 if (branch_name == NULL) {
12089 error = got_error_from_errno("strdup");
12092 error = got_ref_resolve(&branch_tip, repo, branch);
12097 error = got_ref_open(&wt_branch, repo,
12098 got_worktree_get_head_ref_name(worktree), 0);
12101 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12104 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12105 wt_branch_tip, branch_tip, 0, repo,
12106 check_cancelled, NULL);
12107 if (error && error->code != GOT_ERR_ANCESTRY)
12110 if (!continue_merge) {
12111 error = check_path_prefix(wt_branch_tip, branch_tip,
12112 got_worktree_get_path_prefix(worktree),
12113 GOT_ERR_MERGE_PATH, repo);
12117 error = check_same_branch(wt_branch_tip, branch,
12120 if (error->code != GOT_ERR_ANCESTRY)
12124 static char msg[512];
12125 snprintf(msg, sizeof(msg),
12126 "cannot create a merge commit because "
12127 "%s is based on %s; %s can be integrated "
12128 "with 'got integrate' instead", branch_name,
12129 got_worktree_get_head_ref_name(worktree),
12131 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12135 error = got_worktree_merge_prepare(&fileindex, worktree,
12140 error = got_worktree_merge_branch(worktree, fileindex,
12141 yca_id, branch_tip, repo, update_progress, &upa,
12142 check_cancelled, NULL);
12145 print_merge_progress_stats(&upa);
12146 if (!upa.did_something) {
12147 error = got_worktree_merge_abort(worktree, fileindex,
12148 repo, abort_progress, &upa);
12151 printf("Already up-to-date\n");
12156 if (interrupt_merge) {
12157 error = got_worktree_merge_postpone(worktree, fileindex);
12160 printf("Merge of %s interrupted on request\n", branch_name);
12161 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12162 upa.not_deleted > 0 || upa.unversioned > 0) {
12163 error = got_worktree_merge_postpone(worktree, fileindex);
12166 if (upa.conflicts > 0 && upa.missing == 0 &&
12167 upa.not_deleted == 0 && upa.unversioned == 0) {
12168 error = got_error_msg(GOT_ERR_CONFLICTS,
12169 "conflicts must be resolved before merging "
12171 } else if (upa.conflicts > 0) {
12172 error = got_error_msg(GOT_ERR_CONFLICTS,
12173 "conflicts must be resolved before merging "
12174 "can continue; changes destined for some "
12175 "files were not yet merged and "
12176 "should be merged manually if required before the "
12177 "merge operation is continued");
12179 error = got_error_msg(GOT_ERR_CONFLICTS,
12180 "changes destined for some "
12181 "files were not yet merged and should be "
12182 "merged manually if required before the "
12183 "merge operation is continued");
12187 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12188 fileindex, author, NULL, 1, branch_tip, branch_name,
12189 repo, continue_merge ? print_status : NULL, NULL);
12192 error = got_worktree_merge_complete(worktree, fileindex, repo);
12195 error = got_object_id_str(&id_str, merge_commit_id);
12198 printf("Merged %s into %s: %s\n", branch_name,
12199 got_worktree_get_head_ref_name(worktree),
12205 free(merge_commit_id);
12211 got_ref_close(branch);
12213 got_ref_close(wt_branch);
12215 got_worktree_close(worktree);
12217 const struct got_error *close_err = got_repo_close(repo);
12222 const struct got_error *pack_err =
12223 got_repo_pack_fds_close(pack_fds);
12233 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12234 "[-S] [file-path ...]\n",
12239 static const struct got_error *
12240 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12241 const char *path, struct got_object_id *blob_id,
12242 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12243 int dirfd, const char *de_name)
12245 const struct got_error *err = NULL;
12246 char *id_str = NULL;
12248 if (staged_status != GOT_STATUS_ADD &&
12249 staged_status != GOT_STATUS_MODIFY &&
12250 staged_status != GOT_STATUS_DELETE)
12253 if (staged_status == GOT_STATUS_ADD ||
12254 staged_status == GOT_STATUS_MODIFY)
12255 err = got_object_id_str(&id_str, staged_blob_id);
12257 err = got_object_id_str(&id_str, blob_id);
12261 printf("%s %c %s\n", id_str, staged_status, path);
12266 static const struct got_error *
12267 cmd_stage(int argc, char *argv[])
12269 const struct got_error *error = NULL;
12270 struct got_repository *repo = NULL;
12271 struct got_worktree *worktree = NULL;
12273 struct got_pathlist_head paths;
12274 struct got_pathlist_entry *pe;
12275 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12276 FILE *patch_script_file = NULL;
12277 const char *patch_script_path = NULL;
12278 struct choose_patch_arg cpa;
12279 int *pack_fds = NULL;
12281 TAILQ_INIT(&paths);
12283 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12292 patch_script_path = optarg;
12295 allow_bad_symlinks = 1;
12307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12308 "unveil", NULL) == -1)
12311 if (list_stage && (pflag || patch_script_path))
12312 errx(1, "-l option cannot be used with other options");
12313 if (patch_script_path && !pflag)
12314 errx(1, "-F option can only be used together with -p option");
12316 cwd = getcwd(NULL, 0);
12318 error = got_error_from_errno("getcwd");
12322 error = got_repo_pack_fds_open(&pack_fds);
12326 error = got_worktree_open(&worktree, cwd);
12328 if (error->code == GOT_ERR_NOT_WORKTREE)
12329 error = wrap_not_worktree_error(error, "stage", cwd);
12333 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12338 if (patch_script_path) {
12339 patch_script_file = fopen(patch_script_path, "re");
12340 if (patch_script_file == NULL) {
12341 error = got_error_from_errno2("fopen",
12342 patch_script_path);
12346 error = apply_unveil(got_repo_get_path(repo), 0,
12347 got_worktree_get_root_path(worktree));
12351 error = check_merge_in_progress(worktree, repo);
12355 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12360 error = got_worktree_status(worktree, &paths, repo, 0,
12361 print_stage, NULL, check_cancelled, NULL);
12363 cpa.patch_script_file = patch_script_file;
12364 cpa.action = "stage";
12365 error = got_worktree_stage(worktree, &paths,
12366 pflag ? NULL : print_status, NULL,
12367 pflag ? choose_patch : NULL, &cpa,
12368 allow_bad_symlinks, repo);
12371 if (patch_script_file && fclose(patch_script_file) == EOF &&
12373 error = got_error_from_errno2("fclose", patch_script_path);
12375 const struct got_error *close_err = got_repo_close(repo);
12380 got_worktree_close(worktree);
12382 const struct got_error *pack_err =
12383 got_repo_pack_fds_close(pack_fds);
12387 TAILQ_FOREACH(pe, &paths, entry)
12388 free((char *)pe->path);
12389 got_pathlist_free(&paths);
12395 usage_unstage(void)
12397 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12398 "[file-path ...]\n",
12404 static const struct got_error *
12405 cmd_unstage(int argc, char *argv[])
12407 const struct got_error *error = NULL;
12408 struct got_repository *repo = NULL;
12409 struct got_worktree *worktree = NULL;
12411 struct got_pathlist_head paths;
12412 struct got_pathlist_entry *pe;
12414 struct got_update_progress_arg upa;
12415 FILE *patch_script_file = NULL;
12416 const char *patch_script_path = NULL;
12417 struct choose_patch_arg cpa;
12418 int *pack_fds = NULL;
12420 TAILQ_INIT(&paths);
12422 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12428 patch_script_path = optarg;
12440 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12441 "unveil", NULL) == -1)
12444 if (patch_script_path && !pflag)
12445 errx(1, "-F option can only be used together with -p option");
12447 cwd = getcwd(NULL, 0);
12449 error = got_error_from_errno("getcwd");
12453 error = got_repo_pack_fds_open(&pack_fds);
12457 error = got_worktree_open(&worktree, cwd);
12459 if (error->code == GOT_ERR_NOT_WORKTREE)
12460 error = wrap_not_worktree_error(error, "unstage", cwd);
12464 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12469 if (patch_script_path) {
12470 patch_script_file = fopen(patch_script_path, "re");
12471 if (patch_script_file == NULL) {
12472 error = got_error_from_errno2("fopen",
12473 patch_script_path);
12478 error = apply_unveil(got_repo_get_path(repo), 0,
12479 got_worktree_get_root_path(worktree));
12483 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12487 cpa.patch_script_file = patch_script_file;
12488 cpa.action = "unstage";
12489 memset(&upa, 0, sizeof(upa));
12490 error = got_worktree_unstage(worktree, &paths, update_progress,
12491 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12493 print_merge_progress_stats(&upa);
12495 if (patch_script_file && fclose(patch_script_file) == EOF &&
12497 error = got_error_from_errno2("fclose", patch_script_path);
12499 const struct got_error *close_err = got_repo_close(repo);
12504 got_worktree_close(worktree);
12506 const struct got_error *pack_err =
12507 got_repo_pack_fds_close(pack_fds);
12511 TAILQ_FOREACH(pe, &paths, entry)
12512 free((char *)pe->path);
12513 got_pathlist_free(&paths);
12521 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12522 "arg1 [arg2 ...]\n", getprogname());
12526 static const struct got_error *
12527 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12529 const struct got_error *err;
12530 struct got_blob_object *blob;
12533 fd = got_opentempfd();
12535 return got_error_from_errno("got_opentempfd");
12537 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12541 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12543 if (fd != -1 && close(fd) == -1 && err == NULL)
12544 err = got_error_from_errno("close");
12546 got_object_blob_close(blob);
12550 static const struct got_error *
12551 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12553 const struct got_error *err;
12554 struct got_tree_object *tree;
12557 err = got_object_open_as_tree(&tree, repo, id);
12561 nentries = got_object_tree_get_nentries(tree);
12562 for (i = 0; i < nentries; i++) {
12563 struct got_tree_entry *te;
12565 if (sigint_received || sigpipe_received)
12567 te = got_object_tree_get_entry(tree, i);
12568 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12571 fprintf(outfile, "%s %.7o %s\n", id_str,
12572 got_tree_entry_get_mode(te),
12573 got_tree_entry_get_name(te));
12577 got_object_tree_close(tree);
12581 static const struct got_error *
12582 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12584 const struct got_error *err;
12585 struct got_commit_object *commit;
12586 const struct got_object_id_queue *parent_ids;
12587 struct got_object_qid *pid;
12588 char *id_str = NULL;
12589 const char *logmsg = NULL;
12592 err = got_object_open_as_commit(&commit, repo, id);
12596 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12600 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12601 parent_ids = got_object_commit_get_parent_ids(commit);
12602 fprintf(outfile, "numparents %d\n",
12603 got_object_commit_get_nparents(commit));
12604 STAILQ_FOREACH(pid, parent_ids, entry) {
12606 err = got_object_id_str(&pid_str, &pid->id);
12609 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12612 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12613 got_object_commit_get_author_gmtoff(commit));
12614 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12615 got_object_commit_get_author(commit),
12616 (long long)got_object_commit_get_author_time(commit),
12619 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12620 got_object_commit_get_committer_gmtoff(commit));
12621 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12622 got_object_commit_get_author(commit),
12623 (long long)got_object_commit_get_committer_time(commit),
12626 logmsg = got_object_commit_get_logmsg_raw(commit);
12627 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12628 fprintf(outfile, "%s", logmsg);
12631 got_object_commit_close(commit);
12635 static const struct got_error *
12636 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12638 const struct got_error *err;
12639 struct got_tag_object *tag;
12640 char *id_str = NULL;
12641 const char *tagmsg = NULL;
12644 err = got_object_open_as_tag(&tag, repo, id);
12648 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12652 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12654 switch (got_object_tag_get_object_type(tag)) {
12655 case GOT_OBJ_TYPE_BLOB:
12656 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12657 GOT_OBJ_LABEL_BLOB);
12659 case GOT_OBJ_TYPE_TREE:
12660 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12661 GOT_OBJ_LABEL_TREE);
12663 case GOT_OBJ_TYPE_COMMIT:
12664 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12665 GOT_OBJ_LABEL_COMMIT);
12667 case GOT_OBJ_TYPE_TAG:
12668 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12669 GOT_OBJ_LABEL_TAG);
12675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12676 got_object_tag_get_name(tag));
12678 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12679 got_object_tag_get_tagger_gmtoff(tag));
12680 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12681 got_object_tag_get_tagger(tag),
12682 (long long)got_object_tag_get_tagger_time(tag),
12685 tagmsg = got_object_tag_get_message(tag);
12686 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12687 fprintf(outfile, "%s", tagmsg);
12690 got_object_tag_close(tag);
12694 static const struct got_error *
12695 cmd_cat(int argc, char *argv[])
12697 const struct got_error *error;
12698 struct got_repository *repo = NULL;
12699 struct got_worktree *worktree = NULL;
12700 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12701 const char *commit_id_str = NULL;
12702 struct got_object_id *id = NULL, *commit_id = NULL;
12703 struct got_commit_object *commit = NULL;
12704 int ch, obj_type, i, force_path = 0;
12705 struct got_reflist_head refs;
12706 int *pack_fds = NULL;
12711 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12716 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12719 commit_id_str = optarg;
12722 repo_path = realpath(optarg, NULL);
12723 if (repo_path == NULL)
12724 return got_error_from_errno2("realpath",
12726 got_path_strip_trailing_slashes(repo_path);
12740 cwd = getcwd(NULL, 0);
12742 error = got_error_from_errno("getcwd");
12746 error = got_repo_pack_fds_open(&pack_fds);
12750 if (repo_path == NULL) {
12751 error = got_worktree_open(&worktree, cwd);
12752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12755 repo_path = strdup(
12756 got_worktree_get_repo_path(worktree));
12757 if (repo_path == NULL) {
12758 error = got_error_from_errno("strdup");
12762 /* Release work tree lock. */
12763 got_worktree_close(worktree);
12768 if (repo_path == NULL) {
12769 repo_path = strdup(cwd);
12770 if (repo_path == NULL)
12771 return got_error_from_errno("strdup");
12774 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12779 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12783 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12787 if (commit_id_str == NULL)
12788 commit_id_str = GOT_REF_HEAD;
12789 error = got_repo_match_object_id(&commit_id, NULL,
12790 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12794 error = got_object_open_as_commit(&commit, repo, commit_id);
12798 for (i = 0; i < argc; i++) {
12800 error = got_object_id_by_path(&id, repo, commit,
12805 error = got_repo_match_object_id(&id, &label, argv[i],
12806 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12809 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12810 error->code != GOT_ERR_NOT_REF)
12812 error = got_object_id_by_path(&id, repo,
12819 error = got_object_get_type(&obj_type, repo, id);
12823 switch (obj_type) {
12824 case GOT_OBJ_TYPE_BLOB:
12825 error = cat_blob(id, repo, stdout);
12827 case GOT_OBJ_TYPE_TREE:
12828 error = cat_tree(id, repo, stdout);
12830 case GOT_OBJ_TYPE_COMMIT:
12831 error = cat_commit(id, repo, stdout);
12833 case GOT_OBJ_TYPE_TAG:
12834 error = cat_tag(id, repo, stdout);
12837 error = got_error(GOT_ERR_OBJ_TYPE);
12852 got_object_commit_close(commit);
12854 got_worktree_close(worktree);
12856 const struct got_error *close_err = got_repo_close(repo);
12861 const struct got_error *pack_err =
12862 got_repo_pack_fds_close(pack_fds);
12867 got_ref_list_free(&refs);
12874 fprintf(stderr, "usage: %s info [path ...]\n",
12879 static const struct got_error *
12880 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12881 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12882 struct got_object_id *commit_id)
12884 const struct got_error *err = NULL;
12885 char *id_str = NULL;
12887 struct tm mytm, *tm;
12888 struct got_pathlist_head *paths = arg;
12889 struct got_pathlist_entry *pe;
12892 * Clear error indication from any of the path arguments which
12893 * would cause this file index entry to be displayed.
12895 TAILQ_FOREACH(pe, paths, entry) {
12896 if (got_path_cmp(path, pe->path, strlen(path),
12897 pe->path_len) == 0 ||
12898 got_path_is_child(path, pe->path, pe->path_len))
12899 pe->data = NULL; /* no error */
12902 printf(GOT_COMMIT_SEP_STR);
12904 printf("symlink: %s\n", path);
12905 else if (S_ISREG(mode)) {
12906 printf("file: %s\n", path);
12907 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12908 } else if (S_ISDIR(mode))
12909 printf("directory: %s\n", path);
12911 printf("something: %s\n", path);
12913 tm = localtime_r(&mtime, &mytm);
12916 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12917 return got_error(GOT_ERR_NO_SPACE);
12918 printf("timestamp: %s\n", datebuf);
12921 err = got_object_id_str(&id_str, blob_id);
12924 printf("based on blob: %s\n", id_str);
12928 if (staged_blob_id) {
12929 err = got_object_id_str(&id_str, staged_blob_id);
12932 printf("based on staged blob: %s\n", id_str);
12937 err = got_object_id_str(&id_str, commit_id);
12940 printf("based on commit: %s\n", id_str);
12947 static const struct got_error *
12948 cmd_info(int argc, char *argv[])
12950 const struct got_error *error = NULL;
12951 struct got_worktree *worktree = NULL;
12952 char *cwd = NULL, *id_str = NULL;
12953 struct got_pathlist_head paths;
12954 struct got_pathlist_entry *pe;
12955 char *uuidstr = NULL;
12956 int ch, show_files = 0;
12957 int *pack_fds = NULL;
12959 TAILQ_INIT(&paths);
12961 while ((ch = getopt(argc, argv, "")) != -1) {
12973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12977 cwd = getcwd(NULL, 0);
12979 error = got_error_from_errno("getcwd");
12983 error = got_repo_pack_fds_open(&pack_fds);
12987 error = got_worktree_open(&worktree, cwd);
12989 if (error->code == GOT_ERR_NOT_WORKTREE)
12990 error = wrap_not_worktree_error(error, "info", cwd);
12995 /* Remove "cpath" promise. */
12996 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
13000 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13005 error = get_worktree_paths_from_argv(&paths, argc, argv,
13012 error = got_object_id_str(&id_str,
13013 got_worktree_get_base_commit_id(worktree));
13017 error = got_worktree_get_uuid(&uuidstr, worktree);
13021 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13022 printf("work tree base commit: %s\n", id_str);
13023 printf("work tree path prefix: %s\n",
13024 got_worktree_get_path_prefix(worktree));
13025 printf("work tree branch reference: %s\n",
13026 got_worktree_get_head_ref_name(worktree));
13027 printf("work tree UUID: %s\n", uuidstr);
13028 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13031 struct got_pathlist_entry *pe;
13032 TAILQ_FOREACH(pe, &paths, entry) {
13033 if (pe->path_len == 0)
13036 * Assume this path will fail. This will be corrected
13037 * in print_path_info() in case the path does suceeed.
13039 pe->data = (void *)got_error_path(pe->path,
13042 error = got_worktree_path_info(worktree, &paths,
13043 print_path_info, &paths, check_cancelled, NULL);
13046 TAILQ_FOREACH(pe, &paths, entry) {
13047 if (pe->data != NULL) {
13048 error = pe->data; /* bad path */
13055 const struct got_error *pack_err =
13056 got_repo_pack_fds_close(pack_fds);
13060 TAILQ_FOREACH(pe, &paths, entry)
13061 free((char *)pe->path);
13062 got_pathlist_free(&paths);