2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
42 #include "got_compat.h"
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
401 size_t len, logmsg_len;
402 char *initial_content_stripped = NULL, *buf = NULL, *s;
406 if (stat(logmsg_path, &st) == -1)
407 return got_error_from_errno2("stat", logmsg_path);
409 if (spawn_editor(editor, logmsg_path) == -1)
410 return got_error_from_errno("failed spawning editor");
412 if (stat(logmsg_path, &st2) == -1)
413 return got_error_from_errno("stat");
415 if (require_modification &&
416 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
417 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
418 "no changes made to commit message, aborting");
421 * Set up a stripped version of the initial content without comments
422 * and blank lines. We need this in order to check if the message
423 * has in fact been edited.
425 initial_content_stripped = malloc(initial_content_len + 1);
426 if (initial_content_stripped == NULL)
427 return got_error_from_errno("malloc");
428 initial_content_stripped[0] = '\0';
430 buf = strdup(initial_content);
432 err = got_error_from_errno("strdup");
437 while ((line = strsep(&s, "\n")) != NULL) {
438 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(initial_content_stripped, line,
441 initial_content_len + 1);
442 if (len >= initial_content_len + 1) {
443 err = got_error(GOT_ERR_NO_SPACE);
447 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
448 initial_content_stripped[len - 1] = '\0';
452 logmsg_len = st2.st_size;
453 *logmsg = malloc(logmsg_len + 1);
455 return got_error_from_errno("malloc");
458 fp = fopen(logmsg_path, "re");
460 err = got_error_from_errno("fopen");
465 while (getline(&line, &linesize, fp) != -1) {
466 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
467 continue; /* remove comments and leading empty lines */
468 len = strlcat(*logmsg, line, logmsg_len + 1);
469 if (len >= logmsg_len + 1) {
470 err = got_error(GOT_ERR_NO_SPACE);
476 err = got_ferror(fp, GOT_ERR_IO);
479 while (len > 0 && (*logmsg)[len - 1] == '\n') {
480 (*logmsg)[len - 1] = '\0';
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
489 if (require_modification &&
490 strcmp(*logmsg, initial_content_stripped) == 0)
491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
492 "no changes made to commit message, aborting");
494 free(initial_content_stripped);
496 if (fp && fclose(fp) == EOF && err == NULL)
497 err = got_error_from_errno("fclose");
505 static const struct got_error *
506 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
507 const char *path_dir, const char *branch_name)
509 char *initial_content = NULL;
510 const struct got_error *err = NULL;
511 int initial_content_len;
514 initial_content_len = asprintf(&initial_content,
515 "\n# %s to be imported to branch %s\n", path_dir,
517 if (initial_content_len == -1)
518 return got_error_from_errno("asprintf");
520 err = got_opentemp_named_fd(logmsg_path, &fd,
521 GOT_TMPDIR_STR "/got-importmsg");
525 if (write(fd, initial_content, initial_content_len) == -1) {
526 err = got_error_from_errno2("write", *logmsg_path);
530 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
531 initial_content_len, 1);
533 if (fd != -1 && close(fd) == -1 && err == NULL)
534 err = got_error_from_errno2("close", *logmsg_path);
535 free(initial_content);
543 static const struct got_error *
544 import_progress(void *arg, const char *path)
546 printf("A %s\n", path);
550 static const struct got_error *
551 valid_author(const char *author)
553 const char *email = author;
556 * Git' expects the author (or committer) to be in the form
557 * "name <email>", which are mostly free form (see the
558 * "committer" description in git-fast-import(1)). We're only
559 * doing this to avoid git's object parser breaking on commits
563 while (*author && *author != '\n' && *author != '<' && *author != '>')
565 if (*author++ != '<')
566 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
567 while (*author && *author != '\n' && *author != '<' && *author != '>')
569 if (strcmp(author, ">") != 0)
570 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
574 static const struct got_error *
575 get_author(char **author, struct got_repository *repo,
576 struct got_worktree *worktree)
578 const struct got_error *err = NULL;
579 const char *got_author = NULL, *name, *email;
580 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
585 worktree_conf = got_worktree_get_gotconfig(worktree);
586 repo_conf = got_repo_get_gotconfig(repo);
589 * Priority of potential author information sources, from most
590 * significant to least significant:
591 * 1) work tree's .got/got.conf file
592 * 2) repository's got.conf file
593 * 3) repository's git config file
594 * 4) environment variables
595 * 5) global git config files (in user's home directory or /etc)
599 got_author = got_gotconfig_get_author(worktree_conf);
600 if (got_author == NULL)
601 got_author = got_gotconfig_get_author(repo_conf);
602 if (got_author == NULL) {
603 name = got_repo_get_gitconfig_author_name(repo);
604 email = got_repo_get_gitconfig_author_email(repo);
606 if (asprintf(author, "%s <%s>", name, email) == -1)
607 return got_error_from_errno("asprintf");
611 got_author = getenv("GOT_AUTHOR");
612 if (got_author == NULL) {
613 name = got_repo_get_global_gitconfig_author_name(repo);
614 email = got_repo_get_global_gitconfig_author_email(
617 if (asprintf(author, "%s <%s>", name, email)
619 return got_error_from_errno("asprintf");
622 /* TODO: Look up user in password database? */
623 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
627 *author = strdup(got_author);
629 return got_error_from_errno("strdup");
631 err = valid_author(*author);
639 static const struct got_error *
640 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
641 struct got_worktree *worktree)
643 const char *got_allowed_signers = NULL;
644 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
646 *allowed_signers = NULL;
649 worktree_conf = got_worktree_get_gotconfig(worktree);
650 repo_conf = got_repo_get_gotconfig(repo);
653 * Priority of potential author information sources, from most
654 * significant to least significant:
655 * 1) work tree's .got/got.conf file
656 * 2) repository's got.conf file
660 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
662 if (got_allowed_signers == NULL)
663 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
666 if (got_allowed_signers) {
667 *allowed_signers = strdup(got_allowed_signers);
668 if (*allowed_signers == NULL)
669 return got_error_from_errno("strdup");
674 static const struct got_error *
675 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
676 struct got_worktree *worktree)
678 const char *got_revoked_signers = NULL;
679 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
681 *revoked_signers = NULL;
684 worktree_conf = got_worktree_get_gotconfig(worktree);
685 repo_conf = got_repo_get_gotconfig(repo);
688 * Priority of potential author information sources, from most
689 * significant to least significant:
690 * 1) work tree's .got/got.conf file
691 * 2) repository's got.conf file
695 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
697 if (got_revoked_signers == NULL)
698 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
701 if (got_revoked_signers) {
702 *revoked_signers = strdup(got_revoked_signers);
703 if (*revoked_signers == NULL)
704 return got_error_from_errno("strdup");
709 static const struct got_error *
710 get_signer_id(char **signer_id, struct got_repository *repo,
711 struct got_worktree *worktree)
713 const char *got_signer_id = NULL;
714 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
719 worktree_conf = got_worktree_get_gotconfig(worktree);
720 repo_conf = got_repo_get_gotconfig(repo);
723 * Priority of potential author information sources, from most
724 * significant to least significant:
725 * 1) work tree's .got/got.conf file
726 * 2) repository's got.conf file
730 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
731 if (got_signer_id == NULL)
732 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
735 *signer_id = strdup(got_signer_id);
736 if (*signer_id == NULL)
737 return got_error_from_errno("strdup");
742 static const struct got_error *
743 get_gitconfig_path(char **gitconfig_path)
745 const char *homedir = getenv("HOME");
747 *gitconfig_path = NULL;
749 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
750 return got_error_from_errno("asprintf");
756 static const struct got_error *
757 cmd_import(int argc, char *argv[])
759 const struct got_error *error = NULL;
760 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
761 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
762 const char *branch_name = "main";
763 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
764 struct got_repository *repo = NULL;
765 struct got_reference *branch_ref = NULL, *head_ref = NULL;
766 struct got_object_id *new_commit_id = NULL;
768 struct got_pathlist_head ignores;
769 struct got_pathlist_entry *pe;
770 int preserve_logmsg = 0;
771 int *pack_fds = NULL;
773 TAILQ_INIT(&ignores);
775 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
778 branch_name = optarg;
781 logmsg = strdup(optarg);
782 if (logmsg == NULL) {
783 error = got_error_from_errno("strdup");
788 repo_path = realpath(optarg, NULL);
789 if (repo_path == NULL) {
790 error = got_error_from_errno2("realpath",
796 if (optarg[0] == '\0')
798 error = got_pathlist_insert(&pe, &ignores, optarg,
813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
821 if (repo_path == NULL) {
822 repo_path = getcwd(NULL, 0);
823 if (repo_path == NULL)
824 return got_error_from_errno("getcwd");
826 got_path_strip_trailing_slashes(repo_path);
827 error = get_gitconfig_path(&gitconfig_path);
830 error = got_repo_pack_fds_open(&pack_fds);
833 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
837 error = get_author(&author, repo, NULL);
842 * Don't let the user create a branch name with a leading '-'.
843 * While technically a valid reference name, this case is usually
844 * an unintended typo.
846 if (branch_name[0] == '-')
847 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
849 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
850 error = got_error_from_errno("asprintf");
854 error = got_ref_open(&branch_ref, repo, refname, 0);
856 if (error->code != GOT_ERR_NOT_REF)
859 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
860 "import target branch already exists");
864 path_dir = realpath(argv[0], NULL);
865 if (path_dir == NULL) {
866 error = got_error_from_errno2("realpath", argv[0]);
869 got_path_strip_trailing_slashes(path_dir);
872 * unveil(2) traverses exec(2); if an editor is used we have
873 * to apply unveil after the log message has been written.
875 if (logmsg == NULL || strlen(logmsg) == 0) {
876 error = get_editor(&editor);
880 error = collect_import_msg(&logmsg, &logmsg_path, editor,
883 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
890 if (unveil(path_dir, "r") != 0) {
891 error = got_error_from_errno2("unveil", path_dir);
897 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
904 error = got_repo_import(&new_commit_id, path_dir, logmsg,
905 author, &ignores, repo, import_progress, NULL);
912 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
919 error = got_ref_write(branch_ref, repo);
926 error = got_object_id_str(&id_str, new_commit_id);
933 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
935 if (error->code != GOT_ERR_NOT_REF) {
941 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
949 error = got_ref_write(head_ref, repo);
957 printf("Created branch %s with commit %s\n",
958 got_ref_get_name(branch_ref), id_str);
961 const struct got_error *pack_err =
962 got_repo_pack_fds_close(pack_fds);
966 if (preserve_logmsg) {
967 fprintf(stderr, "%s: log message preserved in %s\n",
968 getprogname(), logmsg_path);
969 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
970 error = got_error_from_errno2("unlink", logmsg_path);
979 free(gitconfig_path);
981 got_ref_close(branch_ref);
983 got_ref_close(head_ref);
990 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
991 "[-R reference] repository-url [directory]\n", getprogname());
995 struct got_fetch_progress_arg {
996 char last_scaled_size[FMT_SCALED_STRSIZE];
1001 struct got_repository *repo;
1004 int configs_created;
1006 struct got_pathlist_head *symrefs;
1007 struct got_pathlist_head *wanted_branches;
1008 struct got_pathlist_head *wanted_refs;
1012 const char *remote_repo_path;
1013 const char *git_url;
1014 int fetch_all_branches;
1015 int mirror_references;
1019 /* XXX forward declaration */
1020 static const struct got_error *
1021 create_config_files(const char *proto, const char *host, const char *port,
1022 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1023 int mirror_references, struct got_pathlist_head *symrefs,
1024 struct got_pathlist_head *wanted_branches,
1025 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1027 static const struct got_error *
1028 fetch_progress(void *arg, const char *message, off_t packfile_size,
1029 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1031 const struct got_error *err = NULL;
1032 struct got_fetch_progress_arg *a = arg;
1033 char scaled_size[FMT_SCALED_STRSIZE];
1034 int p_indexed, p_resolved;
1035 int print_size = 0, print_indexed = 0, print_resolved = 0;
1038 * In order to allow a failed clone to be resumed with 'got fetch'
1039 * we try to create configuration files as soon as possible.
1040 * Once the server has sent information about its default branch
1041 * we have all required information.
1043 if (a->create_configs && !a->configs_created &&
1044 !TAILQ_EMPTY(a->config_info.symrefs)) {
1045 err = create_config_files(a->config_info.proto,
1046 a->config_info.host, a->config_info.port,
1047 a->config_info.remote_repo_path,
1048 a->config_info.git_url,
1049 a->config_info.fetch_all_branches,
1050 a->config_info.mirror_references,
1051 a->config_info.symrefs,
1052 a->config_info.wanted_branches,
1053 a->config_info.wanted_refs, a->repo);
1056 a->configs_created = 1;
1059 if (a->verbosity < 0)
1062 if (message && message[0] != '\0') {
1063 printf("\rserver: %s", message);
1068 if (packfile_size > 0 || nobj_indexed > 0) {
1069 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1070 (a->last_scaled_size[0] == '\0' ||
1071 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1073 if (strlcpy(a->last_scaled_size, scaled_size,
1074 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1075 return got_error(GOT_ERR_NO_SPACE);
1077 if (nobj_indexed > 0) {
1078 p_indexed = (nobj_indexed * 100) / nobj_total;
1079 if (p_indexed != a->last_p_indexed) {
1080 a->last_p_indexed = p_indexed;
1085 if (nobj_resolved > 0) {
1086 p_resolved = (nobj_resolved * 100) /
1087 (nobj_total - nobj_loose);
1088 if (p_resolved != a->last_p_resolved) {
1089 a->last_p_resolved = p_resolved;
1097 if (print_size || print_indexed || print_resolved)
1100 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1102 printf("; indexing %d%%", p_indexed);
1104 printf("; resolving deltas %d%%", p_resolved);
1105 if (print_size || print_indexed || print_resolved)
1111 static const struct got_error *
1112 create_symref(const char *refname, struct got_reference *target_ref,
1113 int verbosity, struct got_repository *repo)
1115 const struct got_error *err;
1116 struct got_reference *head_symref;
1118 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1122 err = got_ref_write(head_symref, repo);
1123 if (err == NULL && verbosity > 0) {
1124 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1125 got_ref_get_name(target_ref));
1127 got_ref_close(head_symref);
1131 static const struct got_error *
1132 list_remote_refs(struct got_pathlist_head *symrefs,
1133 struct got_pathlist_head *refs)
1135 const struct got_error *err;
1136 struct got_pathlist_entry *pe;
1138 TAILQ_FOREACH(pe, symrefs, entry) {
1139 const char *refname = pe->path;
1140 const char *targetref = pe->data;
1142 printf("%s: %s\n", refname, targetref);
1145 TAILQ_FOREACH(pe, refs, entry) {
1146 const char *refname = pe->path;
1147 struct got_object_id *id = pe->data;
1150 err = got_object_id_str(&id_str, id);
1153 printf("%s: %s\n", refname, id_str);
1160 static const struct got_error *
1161 create_ref(const char *refname, struct got_object_id *id,
1162 int verbosity, struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 struct got_reference *ref;
1168 err = got_object_id_str(&id_str, id);
1172 err = got_ref_alloc(&ref, refname, id);
1176 err = got_ref_write(ref, repo);
1179 if (err == NULL && verbosity >= 0)
1180 printf("Created reference %s: %s\n", refname, id_str);
1187 match_wanted_ref(const char *refname, const char *wanted_ref)
1189 if (strncmp(refname, "refs/", 5) != 0)
1194 * Prevent fetching of references that won't make any
1195 * sense outside of the remote repository's context.
1197 if (strncmp(refname, "got/", 4) == 0)
1199 if (strncmp(refname, "remotes/", 8) == 0)
1202 if (strncmp(wanted_ref, "refs/", 5) == 0)
1205 /* Allow prefix match. */
1206 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1209 /* Allow exact match. */
1210 return (strcmp(refname, wanted_ref) == 0);
1214 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1216 struct got_pathlist_entry *pe;
1218 TAILQ_FOREACH(pe, wanted_refs, entry) {
1219 if (match_wanted_ref(refname, pe->path))
1226 static const struct got_error *
1227 create_wanted_ref(const char *refname, struct got_object_id *id,
1228 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1230 const struct got_error *err;
1231 char *remote_refname;
1233 if (strncmp("refs/", refname, 5) == 0)
1236 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1237 remote_repo_name, refname) == -1)
1238 return got_error_from_errno("asprintf");
1240 err = create_ref(remote_refname, id, verbosity, repo);
1241 free(remote_refname);
1245 static const struct got_error *
1246 create_gotconfig(const char *proto, const char *host, const char *port,
1247 const char *remote_repo_path, const char *default_branch,
1248 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1249 struct got_pathlist_head *wanted_refs, int mirror_references,
1250 struct got_repository *repo)
1252 const struct got_error *err = NULL;
1253 char *gotconfig_path = NULL;
1254 char *gotconfig = NULL;
1255 FILE *gotconfig_file = NULL;
1256 const char *branchname = NULL;
1257 char *branches = NULL, *refs = NULL;
1260 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1261 struct got_pathlist_entry *pe;
1262 TAILQ_FOREACH(pe, wanted_branches, entry) {
1264 branchname = pe->path;
1265 if (strncmp(branchname, "refs/heads/", 11) == 0)
1267 if (asprintf(&s, "%s\"%s\" ",
1268 branches ? branches : "", branchname) == -1) {
1269 err = got_error_from_errno("asprintf");
1275 } else if (!fetch_all_branches && default_branch) {
1276 branchname = default_branch;
1277 if (strncmp(branchname, "refs/heads/", 11) == 0)
1279 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1280 err = got_error_from_errno("asprintf");
1284 if (!TAILQ_EMPTY(wanted_refs)) {
1285 struct got_pathlist_entry *pe;
1286 TAILQ_FOREACH(pe, wanted_refs, entry) {
1288 const char *refname = pe->path;
1289 if (strncmp(refname, "refs/", 5) == 0)
1291 if (asprintf(&s, "%s\"%s\" ",
1292 refs ? refs : "", refname) == -1) {
1293 err = got_error_from_errno("asprintf");
1301 /* Create got.conf(5). */
1302 gotconfig_path = got_repo_get_path_gotconfig(repo);
1303 if (gotconfig_path == NULL) {
1304 err = got_error_from_errno("got_repo_get_path_gotconfig");
1307 gotconfig_file = fopen(gotconfig_path, "ae");
1308 if (gotconfig_file == NULL) {
1309 err = got_error_from_errno2("fopen", gotconfig_path);
1312 if (asprintf(&gotconfig,
1317 "\trepository \"%s\"\n"
1323 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1324 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1325 remote_repo_path, branches ? "\tbranch { " : "",
1326 branches ? branches : "", branches ? "}\n" : "",
1327 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1328 mirror_references ? "\tmirror_references yes\n" : "",
1329 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1330 err = got_error_from_errno("asprintf");
1333 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1334 if (n != strlen(gotconfig)) {
1335 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1340 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1341 err = got_error_from_errno2("fclose", gotconfig_path);
1342 free(gotconfig_path);
1347 static const struct got_error *
1348 create_gitconfig(const char *git_url, const char *default_branch,
1349 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1350 struct got_pathlist_head *wanted_refs, int mirror_references,
1351 struct got_repository *repo)
1353 const struct got_error *err = NULL;
1354 char *gitconfig_path = NULL;
1355 char *gitconfig = NULL;
1356 FILE *gitconfig_file = NULL;
1357 char *branches = NULL, *refs = NULL;
1358 const char *branchname;
1361 /* Create a config file Git can understand. */
1362 gitconfig_path = got_repo_get_path_gitconfig(repo);
1363 if (gitconfig_path == NULL) {
1364 err = got_error_from_errno("got_repo_get_path_gitconfig");
1367 gitconfig_file = fopen(gitconfig_path, "ae");
1368 if (gitconfig_file == NULL) {
1369 err = got_error_from_errno2("fopen", gitconfig_path);
1372 if (fetch_all_branches) {
1373 if (mirror_references) {
1374 if (asprintf(&branches,
1375 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1376 err = got_error_from_errno("asprintf");
1379 } else if (asprintf(&branches,
1380 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1381 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1382 err = got_error_from_errno("asprintf");
1385 } else if (!TAILQ_EMPTY(wanted_branches)) {
1386 struct got_pathlist_entry *pe;
1387 TAILQ_FOREACH(pe, wanted_branches, entry) {
1389 branchname = pe->path;
1390 if (strncmp(branchname, "refs/heads/", 11) == 0)
1392 if (mirror_references) {
1394 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1395 branches ? branches : "",
1396 branchname, branchname) == -1) {
1397 err = got_error_from_errno("asprintf");
1400 } else if (asprintf(&s,
1401 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1402 branches ? branches : "",
1403 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1404 branchname) == -1) {
1405 err = got_error_from_errno("asprintf");
1413 * If the server specified a default branch, use just that one.
1414 * Otherwise fall back to fetching all branches on next fetch.
1416 if (default_branch) {
1417 branchname = default_branch;
1418 if (strncmp(branchname, "refs/heads/", 11) == 0)
1421 branchname = "*"; /* fall back to all branches */
1422 if (mirror_references) {
1423 if (asprintf(&branches,
1424 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1425 branchname, branchname) == -1) {
1426 err = got_error_from_errno("asprintf");
1429 } else if (asprintf(&branches,
1430 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1431 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1432 branchname) == -1) {
1433 err = got_error_from_errno("asprintf");
1437 if (!TAILQ_EMPTY(wanted_refs)) {
1438 struct got_pathlist_entry *pe;
1439 TAILQ_FOREACH(pe, wanted_refs, entry) {
1441 const char *refname = pe->path;
1442 if (strncmp(refname, "refs/", 5) == 0)
1444 if (mirror_references) {
1446 "%s\tfetch = refs/%s:refs/%s\n",
1447 refs ? refs : "", refname, refname) == -1) {
1448 err = got_error_from_errno("asprintf");
1451 } else if (asprintf(&s,
1452 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1454 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1456 err = got_error_from_errno("asprintf");
1464 if (asprintf(&gitconfig,
1469 "\tfetch = refs/tags/*:refs/tags/*\n",
1470 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1471 refs ? refs : "") == -1) {
1472 err = got_error_from_errno("asprintf");
1475 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1476 if (n != strlen(gitconfig)) {
1477 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1481 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1482 err = got_error_from_errno2("fclose", gitconfig_path);
1483 free(gitconfig_path);
1488 static const struct got_error *
1489 create_config_files(const char *proto, const char *host, const char *port,
1490 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1491 int mirror_references, struct got_pathlist_head *symrefs,
1492 struct got_pathlist_head *wanted_branches,
1493 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1495 const struct got_error *err = NULL;
1496 const char *default_branch = NULL;
1497 struct got_pathlist_entry *pe;
1500 * If we asked for a set of wanted branches then use the first
1503 if (!TAILQ_EMPTY(wanted_branches)) {
1504 pe = TAILQ_FIRST(wanted_branches);
1505 default_branch = pe->path;
1507 /* First HEAD ref listed by server is the default branch. */
1508 TAILQ_FOREACH(pe, symrefs, entry) {
1509 const char *refname = pe->path;
1510 const char *target = pe->data;
1512 if (strcmp(refname, GOT_REF_HEAD) != 0)
1515 default_branch = target;
1520 /* Create got.conf(5). */
1521 err = create_gotconfig(proto, host, port, remote_repo_path,
1522 default_branch, fetch_all_branches, wanted_branches,
1523 wanted_refs, mirror_references, repo);
1527 /* Create a config file Git can understand. */
1528 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1529 wanted_branches, wanted_refs, mirror_references, repo);
1532 static const struct got_error *
1533 cmd_clone(int argc, char *argv[])
1535 const struct got_error *error = NULL;
1536 const char *uri, *dirname;
1537 char *proto, *host, *port, *repo_name, *server_path;
1538 char *default_destdir = NULL, *id_str = NULL;
1539 const char *repo_path;
1540 struct got_repository *repo = NULL;
1541 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1542 struct got_pathlist_entry *pe;
1543 struct got_object_id *pack_hash = NULL;
1544 int ch, fetchfd = -1, fetchstatus;
1545 pid_t fetchpid = -1;
1546 struct got_fetch_progress_arg fpa;
1547 char *git_url = NULL;
1548 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1549 int list_refs_only = 0;
1550 int *pack_fds = NULL;
1553 TAILQ_INIT(&symrefs);
1554 TAILQ_INIT(&wanted_branches);
1555 TAILQ_INIT(&wanted_refs);
1557 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1560 fetch_all_branches = 1;
1563 error = got_pathlist_append(&wanted_branches,
1572 mirror_references = 1;
1577 else if (verbosity < 3)
1584 error = got_pathlist_append(&wanted_refs,
1597 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1598 option_conflict('a', 'b');
1599 if (list_refs_only) {
1600 if (!TAILQ_EMPTY(&wanted_branches))
1601 option_conflict('l', 'b');
1602 if (fetch_all_branches)
1603 option_conflict('l', 'a');
1604 if (mirror_references)
1605 option_conflict('l', 'm');
1606 if (!TAILQ_EMPTY(&wanted_refs))
1607 option_conflict('l', 'R');
1619 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1624 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1625 host, port ? ":" : "", port ? port : "",
1626 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1627 error = got_error_from_errno("asprintf");
1631 if (strcmp(proto, "git") == 0) {
1633 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1634 "sendfd dns inet unveil", NULL) == -1)
1637 } else if (strcmp(proto, "git+ssh") == 0 ||
1638 strcmp(proto, "ssh") == 0) {
1640 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1641 "sendfd unveil", NULL) == -1)
1644 } else if (strcmp(proto, "http") == 0 ||
1645 strcmp(proto, "git+http") == 0) {
1646 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1649 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1652 if (dirname == NULL) {
1653 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1654 error = got_error_from_errno("asprintf");
1657 repo_path = default_destdir;
1659 repo_path = dirname;
1661 if (!list_refs_only) {
1662 error = got_path_mkdir(repo_path);
1664 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1665 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1667 if (!got_path_dir_is_empty(repo_path)) {
1668 error = got_error_path(repo_path,
1669 GOT_ERR_DIR_NOT_EMPTY);
1674 error = got_dial_apply_unveil(proto);
1678 error = apply_unveil(repo_path, 0, NULL);
1683 printf("Connecting to %s%s%s\n", host,
1684 port ? ":" : "", port ? port : "");
1686 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1687 server_path, verbosity);
1691 if (!list_refs_only) {
1692 error = got_repo_init(repo_path);
1695 error = got_repo_pack_fds_open(&pack_fds);
1698 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1703 fpa.last_scaled_size[0] = '\0';
1704 fpa.last_p_indexed = -1;
1705 fpa.last_p_resolved = -1;
1706 fpa.verbosity = verbosity;
1707 fpa.create_configs = 1;
1708 fpa.configs_created = 0;
1710 fpa.config_info.symrefs = &symrefs;
1711 fpa.config_info.wanted_branches = &wanted_branches;
1712 fpa.config_info.wanted_refs = &wanted_refs;
1713 fpa.config_info.proto = proto;
1714 fpa.config_info.host = host;
1715 fpa.config_info.port = port;
1716 fpa.config_info.remote_repo_path = server_path;
1717 fpa.config_info.git_url = git_url;
1718 fpa.config_info.fetch_all_branches = fetch_all_branches;
1719 fpa.config_info.mirror_references = mirror_references;
1720 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1721 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1722 fetch_all_branches, &wanted_branches, &wanted_refs,
1723 list_refs_only, verbosity, fetchfd, repo,
1724 fetch_progress, &fpa);
1728 if (list_refs_only) {
1729 error = list_remote_refs(&symrefs, &refs);
1733 if (pack_hash == NULL) {
1734 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1735 "server sent an empty pack file");
1738 error = got_object_id_str(&id_str, pack_hash);
1742 printf("\nFetched %s.pack\n", id_str);
1745 /* Set up references provided with the pack file. */
1746 TAILQ_FOREACH(pe, &refs, entry) {
1747 const char *refname = pe->path;
1748 struct got_object_id *id = pe->data;
1749 char *remote_refname;
1751 if (is_wanted_ref(&wanted_refs, refname) &&
1752 !mirror_references) {
1753 error = create_wanted_ref(refname, id,
1754 GOT_FETCH_DEFAULT_REMOTE_NAME,
1755 verbosity - 1, repo);
1761 error = create_ref(refname, id, verbosity - 1, repo);
1765 if (mirror_references)
1768 if (strncmp("refs/heads/", refname, 11) != 0)
1771 if (asprintf(&remote_refname,
1772 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1773 refname + 11) == -1) {
1774 error = got_error_from_errno("asprintf");
1777 error = create_ref(remote_refname, id, verbosity - 1, repo);
1778 free(remote_refname);
1783 /* Set the HEAD reference if the server provided one. */
1784 TAILQ_FOREACH(pe, &symrefs, entry) {
1785 struct got_reference *target_ref;
1786 const char *refname = pe->path;
1787 const char *target = pe->data;
1788 char *remote_refname = NULL, *remote_target = NULL;
1790 if (strcmp(refname, GOT_REF_HEAD) != 0)
1793 error = got_ref_open(&target_ref, repo, target, 0);
1795 if (error->code == GOT_ERR_NOT_REF) {
1802 error = create_symref(refname, target_ref, verbosity, repo);
1803 got_ref_close(target_ref);
1807 if (mirror_references)
1810 if (strncmp("refs/heads/", target, 11) != 0)
1813 if (asprintf(&remote_refname,
1814 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1816 error = got_error_from_errno("asprintf");
1819 if (asprintf(&remote_target,
1820 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1821 target + 11) == -1) {
1822 error = got_error_from_errno("asprintf");
1823 free(remote_refname);
1826 error = got_ref_open(&target_ref, repo, remote_target, 0);
1828 free(remote_refname);
1829 free(remote_target);
1830 if (error->code == GOT_ERR_NOT_REF) {
1836 error = create_symref(remote_refname, target_ref,
1837 verbosity - 1, repo);
1838 free(remote_refname);
1839 free(remote_target);
1840 got_ref_close(target_ref);
1846 * We failed to set the HEAD reference. If we asked for
1847 * a set of wanted branches use the first of one of those
1848 * which could be fetched instead.
1850 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1851 const char *target = pe->path;
1852 struct got_reference *target_ref;
1854 error = got_ref_open(&target_ref, repo, target, 0);
1856 if (error->code == GOT_ERR_NOT_REF) {
1863 error = create_symref(GOT_REF_HEAD, target_ref,
1865 got_ref_close(target_ref);
1873 printf("Created %s repository '%s'\n",
1874 mirror_references ? "mirrored" : "cloned", repo_path);
1877 const struct got_error *pack_err =
1878 got_repo_pack_fds_close(pack_fds);
1883 if (kill(fetchpid, SIGTERM) == -1)
1884 error = got_error_from_errno("kill");
1885 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1886 error = got_error_from_errno("waitpid");
1888 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1889 error = got_error_from_errno("close");
1891 const struct got_error *close_err = got_repo_close(repo);
1895 TAILQ_FOREACH(pe, &refs, entry) {
1896 free((void *)pe->path);
1899 got_pathlist_free(&refs);
1900 TAILQ_FOREACH(pe, &symrefs, entry) {
1901 free((void *)pe->path);
1904 got_pathlist_free(&symrefs);
1905 got_pathlist_free(&wanted_branches);
1906 got_pathlist_free(&wanted_refs);
1913 free(default_destdir);
1918 static const struct got_error *
1919 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1920 int replace_tags, int verbosity, struct got_repository *repo)
1922 const struct got_error *err = NULL;
1923 char *new_id_str = NULL;
1924 struct got_object_id *old_id = NULL;
1926 err = got_object_id_str(&new_id_str, new_id);
1930 if (!replace_tags &&
1931 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1932 err = got_ref_resolve(&old_id, repo, ref);
1935 if (got_object_id_cmp(old_id, new_id) == 0)
1937 if (verbosity >= 0) {
1938 printf("Rejecting update of existing tag %s: %s\n",
1939 got_ref_get_name(ref), new_id_str);
1944 if (got_ref_is_symbolic(ref)) {
1945 if (verbosity >= 0) {
1946 printf("Replacing reference %s: %s\n",
1947 got_ref_get_name(ref),
1948 got_ref_get_symref_target(ref));
1950 err = got_ref_change_symref_to_ref(ref, new_id);
1953 err = got_ref_write(ref, repo);
1957 err = got_ref_resolve(&old_id, repo, ref);
1960 if (got_object_id_cmp(old_id, new_id) == 0)
1963 err = got_ref_change_ref(ref, new_id);
1966 err = got_ref_write(ref, repo);
1972 printf("Updated %s: %s\n", got_ref_get_name(ref),
1980 static const struct got_error *
1981 update_symref(const char *refname, struct got_reference *target_ref,
1982 int verbosity, struct got_repository *repo)
1984 const struct got_error *err = NULL, *unlock_err;
1985 struct got_reference *symref;
1986 int symref_is_locked = 0;
1988 err = got_ref_open(&symref, repo, refname, 1);
1990 if (err->code != GOT_ERR_NOT_REF)
1992 err = got_ref_alloc_symref(&symref, refname, target_ref);
1996 err = got_ref_write(symref, repo);
2001 printf("Created reference %s: %s\n",
2002 got_ref_get_name(symref),
2003 got_ref_get_symref_target(symref));
2005 symref_is_locked = 1;
2007 if (strcmp(got_ref_get_symref_target(symref),
2008 got_ref_get_name(target_ref)) == 0)
2011 err = got_ref_change_symref(symref,
2012 got_ref_get_name(target_ref));
2016 err = got_ref_write(symref, repo);
2021 printf("Updated %s: %s\n", got_ref_get_name(symref),
2022 got_ref_get_symref_target(symref));
2026 if (symref_is_locked) {
2027 unlock_err = got_ref_unlock(symref);
2028 if (unlock_err && err == NULL)
2031 got_ref_close(symref);
2038 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2039 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2040 "[remote-repository-name]\n",
2045 static const struct got_error *
2046 delete_missing_ref(struct got_reference *ref,
2047 int verbosity, struct got_repository *repo)
2049 const struct got_error *err = NULL;
2050 struct got_object_id *id = NULL;
2051 char *id_str = NULL;
2053 if (got_ref_is_symbolic(ref)) {
2054 err = got_ref_delete(ref, repo);
2057 if (verbosity >= 0) {
2058 printf("Deleted %s: %s\n",
2059 got_ref_get_name(ref),
2060 got_ref_get_symref_target(ref));
2063 err = got_ref_resolve(&id, repo, ref);
2066 err = got_object_id_str(&id_str, id);
2070 err = got_ref_delete(ref, repo);
2073 if (verbosity >= 0) {
2074 printf("Deleted %s: %s\n",
2075 got_ref_get_name(ref), id_str);
2084 static const struct got_error *
2085 delete_missing_refs(struct got_pathlist_head *their_refs,
2086 struct got_pathlist_head *their_symrefs,
2087 const struct got_remote_repo *remote,
2088 int verbosity, struct got_repository *repo)
2090 const struct got_error *err = NULL, *unlock_err;
2091 struct got_reflist_head my_refs;
2092 struct got_reflist_entry *re;
2093 struct got_pathlist_entry *pe;
2094 char *remote_namespace = NULL;
2095 char *local_refname = NULL;
2097 TAILQ_INIT(&my_refs);
2099 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2101 return got_error_from_errno("asprintf");
2103 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2107 TAILQ_FOREACH(re, &my_refs, entry) {
2108 const char *refname = got_ref_get_name(re->ref);
2109 const char *their_refname;
2111 if (remote->mirror_references) {
2112 their_refname = refname;
2114 if (strncmp(refname, remote_namespace,
2115 strlen(remote_namespace)) == 0) {
2116 if (strcmp(refname + strlen(remote_namespace),
2119 if (asprintf(&local_refname, "refs/heads/%s",
2120 refname + strlen(remote_namespace)) == -1) {
2121 err = got_error_from_errno("asprintf");
2124 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2127 their_refname = local_refname;
2130 TAILQ_FOREACH(pe, their_refs, entry) {
2131 if (strcmp(their_refname, pe->path) == 0)
2137 TAILQ_FOREACH(pe, their_symrefs, entry) {
2138 if (strcmp(their_refname, pe->path) == 0)
2144 err = delete_missing_ref(re->ref, verbosity, repo);
2148 if (local_refname) {
2149 struct got_reference *ref;
2150 err = got_ref_open(&ref, repo, local_refname, 1);
2152 if (err->code != GOT_ERR_NOT_REF)
2154 free(local_refname);
2155 local_refname = NULL;
2158 err = delete_missing_ref(ref, verbosity, repo);
2161 unlock_err = got_ref_unlock(ref);
2163 if (unlock_err && err == NULL) {
2168 free(local_refname);
2169 local_refname = NULL;
2173 free(remote_namespace);
2174 free(local_refname);
2178 static const struct got_error *
2179 update_wanted_ref(const char *refname, struct got_object_id *id,
2180 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2182 const struct got_error *err, *unlock_err;
2183 char *remote_refname;
2184 struct got_reference *ref;
2186 if (strncmp("refs/", refname, 5) == 0)
2189 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2190 remote_repo_name, refname) == -1)
2191 return got_error_from_errno("asprintf");
2193 err = got_ref_open(&ref, repo, remote_refname, 1);
2195 if (err->code != GOT_ERR_NOT_REF)
2197 err = create_ref(remote_refname, id, verbosity, repo);
2199 err = update_ref(ref, id, 0, verbosity, repo);
2200 unlock_err = got_ref_unlock(ref);
2201 if (unlock_err && err == NULL)
2206 free(remote_refname);
2210 static const struct got_error *
2211 delete_ref(struct got_repository *repo, struct got_reference *ref)
2213 const struct got_error *err = NULL;
2214 struct got_object_id *id = NULL;
2215 char *id_str = NULL;
2218 if (got_ref_is_symbolic(ref)) {
2219 target = got_ref_get_symref_target(ref);
2221 err = got_ref_resolve(&id, repo, ref);
2224 err = got_object_id_str(&id_str, id);
2230 err = got_ref_delete(ref, repo);
2234 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2241 static const struct got_error *
2242 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2244 const struct got_error *err = NULL;
2245 struct got_reflist_head refs;
2246 struct got_reflist_entry *re;
2251 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2252 err = got_error_from_errno("asprintf");
2255 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2259 TAILQ_FOREACH(re, &refs, entry)
2260 delete_ref(repo, re->ref);
2262 got_ref_list_free(&refs);
2266 static const struct got_error *
2267 cmd_fetch(int argc, char *argv[])
2269 const struct got_error *error = NULL, *unlock_err;
2270 char *cwd = NULL, *repo_path = NULL;
2271 const char *remote_name;
2272 char *proto = NULL, *host = NULL, *port = NULL;
2273 char *repo_name = NULL, *server_path = NULL;
2274 const struct got_remote_repo *remotes, *remote = NULL;
2276 char *id_str = NULL;
2277 struct got_repository *repo = NULL;
2278 struct got_worktree *worktree = NULL;
2279 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2280 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2281 struct got_pathlist_entry *pe;
2282 struct got_object_id *pack_hash = NULL;
2283 int i, ch, fetchfd = -1, fetchstatus;
2284 pid_t fetchpid = -1;
2285 struct got_fetch_progress_arg fpa;
2286 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2287 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2288 int *pack_fds = NULL;
2291 TAILQ_INIT(&symrefs);
2292 TAILQ_INIT(&wanted_branches);
2293 TAILQ_INIT(&wanted_refs);
2295 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2298 fetch_all_branches = 1;
2301 error = got_pathlist_append(&wanted_branches,
2313 repo_path = realpath(optarg, NULL);
2314 if (repo_path == NULL)
2315 return got_error_from_errno2("realpath",
2317 got_path_strip_trailing_slashes(repo_path);
2325 else if (verbosity < 3)
2332 error = got_pathlist_append(&wanted_refs,
2348 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2349 option_conflict('a', 'b');
2350 if (list_refs_only) {
2351 if (!TAILQ_EMPTY(&wanted_branches))
2352 option_conflict('l', 'b');
2353 if (fetch_all_branches)
2354 option_conflict('l', 'a');
2356 option_conflict('l', 'd');
2358 option_conflict('l', 'X');
2360 if (delete_remote) {
2361 if (fetch_all_branches)
2362 option_conflict('X', 'a');
2363 if (!TAILQ_EMPTY(&wanted_branches))
2364 option_conflict('X', 'b');
2366 option_conflict('X', 'd');
2368 option_conflict('X', 't');
2369 if (!TAILQ_EMPTY(&wanted_refs))
2370 option_conflict('X', 'R');
2375 errx(1, "-X option requires a remote name");
2376 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2377 } else if (argc == 1)
2378 remote_name = argv[0];
2382 cwd = getcwd(NULL, 0);
2384 error = got_error_from_errno("getcwd");
2388 error = got_repo_pack_fds_open(&pack_fds);
2392 if (repo_path == NULL) {
2393 error = got_worktree_open(&worktree, cwd);
2394 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2400 strdup(got_worktree_get_repo_path(worktree));
2401 if (repo_path == NULL)
2402 error = got_error_from_errno("strdup");
2406 repo_path = strdup(cwd);
2407 if (repo_path == NULL) {
2408 error = got_error_from_errno("strdup");
2414 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2418 if (delete_remote) {
2419 error = delete_refs_for_remote(repo, remote_name);
2420 goto done; /* nothing else to do */
2424 worktree_conf = got_worktree_get_gotconfig(worktree);
2425 if (worktree_conf) {
2426 got_gotconfig_get_remotes(&nremotes, &remotes,
2428 for (i = 0; i < nremotes; i++) {
2429 if (strcmp(remotes[i].name, remote_name) == 0) {
2430 remote = &remotes[i];
2436 if (remote == NULL) {
2437 repo_conf = got_repo_get_gotconfig(repo);
2439 got_gotconfig_get_remotes(&nremotes, &remotes,
2441 for (i = 0; i < nremotes; i++) {
2442 if (strcmp(remotes[i].name, remote_name) == 0) {
2443 remote = &remotes[i];
2449 if (remote == NULL) {
2450 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2451 for (i = 0; i < nremotes; i++) {
2452 if (strcmp(remotes[i].name, remote_name) == 0) {
2453 remote = &remotes[i];
2458 if (remote == NULL) {
2459 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2463 if (TAILQ_EMPTY(&wanted_branches)) {
2464 if (!fetch_all_branches)
2465 fetch_all_branches = remote->fetch_all_branches;
2466 for (i = 0; i < remote->nfetch_branches; i++) {
2467 got_pathlist_append(&wanted_branches,
2468 remote->fetch_branches[i], NULL);
2471 if (TAILQ_EMPTY(&wanted_refs)) {
2472 for (i = 0; i < remote->nfetch_refs; i++) {
2473 got_pathlist_append(&wanted_refs,
2474 remote->fetch_refs[i], NULL);
2478 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2479 &repo_name, remote->fetch_url);
2483 if (strcmp(proto, "git") == 0) {
2485 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2486 "sendfd dns inet unveil", NULL) == -1)
2489 } else if (strcmp(proto, "git+ssh") == 0 ||
2490 strcmp(proto, "ssh") == 0) {
2492 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2493 "sendfd unveil", NULL) == -1)
2496 } else if (strcmp(proto, "http") == 0 ||
2497 strcmp(proto, "git+http") == 0) {
2498 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2501 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2505 error = got_dial_apply_unveil(proto);
2509 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2514 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2515 port ? ":" : "", port ? port : "");
2517 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2518 server_path, verbosity);
2522 fpa.last_scaled_size[0] = '\0';
2523 fpa.last_p_indexed = -1;
2524 fpa.last_p_resolved = -1;
2525 fpa.verbosity = verbosity;
2527 fpa.create_configs = 0;
2528 fpa.configs_created = 0;
2529 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2530 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2531 remote->mirror_references, fetch_all_branches, &wanted_branches,
2532 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2533 fetch_progress, &fpa);
2537 if (list_refs_only) {
2538 error = list_remote_refs(&symrefs, &refs);
2542 if (pack_hash == NULL) {
2544 printf("Already up-to-date\n");
2545 } else if (verbosity >= 0) {
2546 error = got_object_id_str(&id_str, pack_hash);
2549 printf("\nFetched %s.pack\n", id_str);
2554 /* Update references provided with the pack file. */
2555 TAILQ_FOREACH(pe, &refs, entry) {
2556 const char *refname = pe->path;
2557 struct got_object_id *id = pe->data;
2558 struct got_reference *ref;
2559 char *remote_refname;
2561 if (is_wanted_ref(&wanted_refs, refname) &&
2562 !remote->mirror_references) {
2563 error = update_wanted_ref(refname, id,
2564 remote->name, verbosity, repo);
2570 if (remote->mirror_references ||
2571 strncmp("refs/tags/", refname, 10) == 0) {
2572 error = got_ref_open(&ref, repo, refname, 1);
2574 if (error->code != GOT_ERR_NOT_REF)
2576 error = create_ref(refname, id, verbosity,
2581 error = update_ref(ref, id, replace_tags,
2583 unlock_err = got_ref_unlock(ref);
2584 if (unlock_err && error == NULL)
2590 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2591 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2592 remote_name, refname + 11) == -1) {
2593 error = got_error_from_errno("asprintf");
2597 error = got_ref_open(&ref, repo, remote_refname, 1);
2599 if (error->code != GOT_ERR_NOT_REF)
2601 error = create_ref(remote_refname, id,
2606 error = update_ref(ref, id, replace_tags,
2608 unlock_err = got_ref_unlock(ref);
2609 if (unlock_err && error == NULL)
2616 /* Also create a local branch if none exists yet. */
2617 error = got_ref_open(&ref, repo, refname, 1);
2619 if (error->code != GOT_ERR_NOT_REF)
2621 error = create_ref(refname, id, verbosity,
2626 unlock_err = got_ref_unlock(ref);
2627 if (unlock_err && error == NULL)
2634 error = delete_missing_refs(&refs, &symrefs, remote,
2640 if (!remote->mirror_references) {
2641 /* Update remote HEAD reference if the server provided one. */
2642 TAILQ_FOREACH(pe, &symrefs, entry) {
2643 struct got_reference *target_ref;
2644 const char *refname = pe->path;
2645 const char *target = pe->data;
2646 char *remote_refname = NULL, *remote_target = NULL;
2648 if (strcmp(refname, GOT_REF_HEAD) != 0)
2651 if (strncmp("refs/heads/", target, 11) != 0)
2654 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2655 remote->name, refname) == -1) {
2656 error = got_error_from_errno("asprintf");
2659 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2660 remote->name, target + 11) == -1) {
2661 error = got_error_from_errno("asprintf");
2662 free(remote_refname);
2666 error = got_ref_open(&target_ref, repo, remote_target,
2669 free(remote_refname);
2670 free(remote_target);
2671 if (error->code == GOT_ERR_NOT_REF) {
2677 error = update_symref(remote_refname, target_ref,
2679 free(remote_refname);
2680 free(remote_target);
2681 got_ref_close(target_ref);
2688 if (kill(fetchpid, SIGTERM) == -1)
2689 error = got_error_from_errno("kill");
2690 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2691 error = got_error_from_errno("waitpid");
2693 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2694 error = got_error_from_errno("close");
2696 const struct got_error *close_err = got_repo_close(repo);
2701 got_worktree_close(worktree);
2703 const struct got_error *pack_err =
2704 got_repo_pack_fds_close(pack_fds);
2708 TAILQ_FOREACH(pe, &refs, entry) {
2709 free((void *)pe->path);
2712 got_pathlist_free(&refs);
2713 TAILQ_FOREACH(pe, &symrefs, entry) {
2714 free((void *)pe->path);
2717 got_pathlist_free(&symrefs);
2718 got_pathlist_free(&wanted_branches);
2719 got_pathlist_free(&wanted_refs);
2734 usage_checkout(void)
2736 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2737 "[-p prefix] [-q] repository-path [worktree-path]\n",
2743 show_worktree_base_ref_warning(void)
2745 fprintf(stderr, "%s: warning: could not create a reference "
2746 "to the work tree's base commit; the commit could be "
2747 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2748 "repository writable and running 'got update' will prevent this\n",
2752 struct got_checkout_progress_arg {
2753 const char *worktree_path;
2754 int had_base_commit_ref_error;
2758 static const struct got_error *
2759 checkout_progress(void *arg, unsigned char status, const char *path)
2761 struct got_checkout_progress_arg *a = arg;
2763 /* Base commit bump happens silently. */
2764 if (status == GOT_STATUS_BUMP_BASE)
2767 if (status == GOT_STATUS_BASE_REF_ERR) {
2768 a->had_base_commit_ref_error = 1;
2772 while (path[0] == '/')
2775 if (a->verbosity >= 0)
2776 printf("%c %s/%s\n", status, a->worktree_path, path);
2781 static const struct got_error *
2782 check_cancelled(void *arg)
2784 if (sigint_received || sigpipe_received)
2785 return got_error(GOT_ERR_CANCELLED);
2789 static const struct got_error *
2790 check_linear_ancestry(struct got_object_id *commit_id,
2791 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2792 struct got_repository *repo)
2794 const struct got_error *err = NULL;
2795 struct got_object_id *yca_id;
2797 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2798 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2803 return got_error(GOT_ERR_ANCESTRY);
2806 * Require a straight line of history between the target commit
2807 * and the work tree's base commit.
2809 * Non-linear situations such as this require a rebase:
2811 * (commit) D F (base_commit)
2819 * 'got update' only handles linear cases:
2820 * Update forwards in time: A (base/yca) - B - C - D (commit)
2821 * Update backwards in time: D (base) - C - B - A (commit/yca)
2823 if (allow_forwards_in_time_only) {
2824 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2825 return got_error(GOT_ERR_ANCESTRY);
2826 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2827 got_object_id_cmp(base_commit_id, yca_id) != 0)
2828 return got_error(GOT_ERR_ANCESTRY);
2834 static const struct got_error *
2835 check_same_branch(struct got_object_id *commit_id,
2836 struct got_reference *head_ref, struct got_object_id *yca_id,
2837 struct got_repository *repo)
2839 const struct got_error *err = NULL;
2840 struct got_commit_graph *graph = NULL;
2841 struct got_object_id *head_commit_id = NULL;
2842 int is_same_branch = 0;
2844 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2848 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2852 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2857 err = got_commit_graph_open(&graph, "/", 1);
2861 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2862 check_cancelled, NULL);
2867 struct got_object_id *id;
2868 err = got_commit_graph_iter_next(&id, graph, repo,
2869 check_cancelled, NULL);
2871 if (err->code == GOT_ERR_ITER_COMPLETED)
2877 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2879 if (got_object_id_cmp(id, commit_id) == 0) {
2887 got_commit_graph_close(graph);
2888 free(head_commit_id);
2889 if (!err && !is_same_branch)
2890 err = got_error(GOT_ERR_ANCESTRY);
2894 static const struct got_error *
2895 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2897 static char msg[512];
2898 const char *branch_name;
2900 if (got_ref_is_symbolic(ref))
2901 branch_name = got_ref_get_symref_target(ref);
2903 branch_name = got_ref_get_name(ref);
2905 if (strncmp("refs/heads/", branch_name, 11) == 0)
2908 snprintf(msg, sizeof(msg),
2909 "target commit is not contained in branch '%s'; "
2910 "the branch to use must be specified with -b; "
2911 "if necessary a new branch can be created for "
2912 "this commit with 'got branch -c %s BRANCH_NAME'",
2913 branch_name, commit_id_str);
2915 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2918 static const struct got_error *
2919 cmd_checkout(int argc, char *argv[])
2921 const struct got_error *error = NULL;
2922 struct got_repository *repo = NULL;
2923 struct got_reference *head_ref = NULL, *ref = NULL;
2924 struct got_worktree *worktree = NULL;
2925 char *repo_path = NULL;
2926 char *worktree_path = NULL;
2927 const char *path_prefix = "";
2928 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2929 char *commit_id_str = NULL;
2930 struct got_object_id *commit_id = NULL;
2932 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2933 struct got_pathlist_head paths;
2934 struct got_checkout_progress_arg cpa;
2935 int *pack_fds = NULL;
2939 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2942 branch_name = optarg;
2945 commit_id_str = strdup(optarg);
2946 if (commit_id_str == NULL)
2947 return got_error_from_errno("strdup");
2953 path_prefix = optarg;
2968 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2969 "unveil", NULL) == -1)
2973 char *base, *dotgit;
2975 repo_path = realpath(argv[0], NULL);
2976 if (repo_path == NULL)
2977 return got_error_from_errno2("realpath", argv[0]);
2978 cwd = getcwd(NULL, 0);
2980 error = got_error_from_errno("getcwd");
2987 error = got_path_basename(&base, path);
2990 dotgit = strstr(base, ".git");
2993 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2994 error = got_error_from_errno("asprintf");
2999 } else if (argc == 2) {
3000 repo_path = realpath(argv[0], NULL);
3001 if (repo_path == NULL) {
3002 error = got_error_from_errno2("realpath", argv[0]);
3005 worktree_path = realpath(argv[1], NULL);
3006 if (worktree_path == NULL) {
3007 if (errno != ENOENT) {
3008 error = got_error_from_errno2("realpath",
3012 worktree_path = strdup(argv[1]);
3013 if (worktree_path == NULL) {
3014 error = got_error_from_errno("strdup");
3021 got_path_strip_trailing_slashes(repo_path);
3022 got_path_strip_trailing_slashes(worktree_path);
3024 error = got_repo_pack_fds_open(&pack_fds);
3028 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3032 /* Pre-create work tree path for unveil(2) */
3033 error = got_path_mkdir(worktree_path);
3035 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3036 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3038 if (!allow_nonempty &&
3039 !got_path_dir_is_empty(worktree_path)) {
3040 error = got_error_path(worktree_path,
3041 GOT_ERR_DIR_NOT_EMPTY);
3046 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3050 error = got_ref_open(&head_ref, repo, branch_name, 0);
3054 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3055 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3058 error = got_worktree_open(&worktree, worktree_path);
3062 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3066 if (!same_path_prefix) {
3067 error = got_error(GOT_ERR_PATH_PREFIX);
3071 if (commit_id_str) {
3072 struct got_reflist_head refs;
3074 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3078 error = got_repo_match_object_id(&commit_id, NULL,
3079 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3080 got_ref_list_free(&refs);
3083 error = check_linear_ancestry(commit_id,
3084 got_worktree_get_base_commit_id(worktree), 0, repo);
3085 if (error != NULL) {
3086 if (error->code == GOT_ERR_ANCESTRY) {
3087 error = checkout_ancestry_error(
3088 head_ref, commit_id_str);
3092 error = check_same_branch(commit_id, head_ref, NULL, repo);
3094 if (error->code == GOT_ERR_ANCESTRY) {
3095 error = checkout_ancestry_error(
3096 head_ref, commit_id_str);
3100 error = got_worktree_set_base_commit_id(worktree, repo,
3104 /* Expand potentially abbreviated commit ID string. */
3105 free(commit_id_str);
3106 error = got_object_id_str(&commit_id_str, commit_id);
3110 commit_id = got_object_id_dup(
3111 got_worktree_get_base_commit_id(worktree));
3112 if (commit_id == NULL) {
3113 error = got_error_from_errno("got_object_id_dup");
3116 error = got_object_id_str(&commit_id_str, commit_id);
3121 error = got_pathlist_append(&paths, "", NULL);
3124 cpa.worktree_path = worktree_path;
3125 cpa.had_base_commit_ref_error = 0;
3126 cpa.verbosity = verbosity;
3127 error = got_worktree_checkout_files(worktree, &paths, repo,
3128 checkout_progress, &cpa, check_cancelled, NULL);
3132 if (got_ref_is_symbolic(head_ref)) {
3133 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3136 refname = got_ref_get_name(ref);
3138 refname = got_ref_get_name(head_ref);
3139 printf("Checked out %s: %s\n", refname, commit_id_str);
3140 printf("Now shut up and hack\n");
3141 if (cpa.had_base_commit_ref_error)
3142 show_worktree_base_ref_warning();
3145 const struct got_error *pack_err =
3146 got_repo_pack_fds_close(pack_fds);
3151 got_ref_close(head_ref);
3154 got_pathlist_free(&paths);
3155 free(commit_id_str);
3158 free(worktree_path);
3163 struct got_update_progress_arg {
3175 print_update_progress_stats(struct got_update_progress_arg *upa)
3177 if (!upa->did_something)
3180 if (upa->conflicts > 0)
3181 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3182 if (upa->obstructed > 0)
3183 printf("File paths obstructed by a non-regular file: %d\n",
3185 if (upa->not_updated > 0)
3186 printf("Files not updated because of existing merge "
3187 "conflicts: %d\n", upa->not_updated);
3191 * The meaning of some status codes differs between merge-style operations and
3192 * update operations. For example, the ! status code means "file was missing"
3193 * if changes were merged into the work tree, and "missing file was restored"
3194 * if the work tree was updated. This function should be used by any operation
3195 * which merges changes into the work tree without updating the work tree.
3198 print_merge_progress_stats(struct got_update_progress_arg *upa)
3200 if (!upa->did_something)
3203 if (upa->conflicts > 0)
3204 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3205 if (upa->obstructed > 0)
3206 printf("File paths obstructed by a non-regular file: %d\n",
3208 if (upa->missing > 0)
3209 printf("Files which had incoming changes but could not be "
3210 "found in the work tree: %d\n", upa->missing);
3211 if (upa->not_deleted > 0)
3212 printf("Files not deleted due to differences in deleted "
3213 "content: %d\n", upa->not_deleted);
3214 if (upa->unversioned > 0)
3215 printf("Files not merged because an unversioned file was "
3216 "found in the work tree: %d\n", upa->unversioned);
3222 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3228 static const struct got_error *
3229 update_progress(void *arg, unsigned char status, const char *path)
3231 struct got_update_progress_arg *upa = arg;
3233 if (status == GOT_STATUS_EXISTS ||
3234 status == GOT_STATUS_BASE_REF_ERR)
3237 upa->did_something = 1;
3239 /* Base commit bump happens silently. */
3240 if (status == GOT_STATUS_BUMP_BASE)
3243 if (status == GOT_STATUS_CONFLICT)
3245 if (status == GOT_STATUS_OBSTRUCTED)
3247 if (status == GOT_STATUS_CANNOT_UPDATE)
3249 if (status == GOT_STATUS_MISSING)
3251 if (status == GOT_STATUS_CANNOT_DELETE)
3253 if (status == GOT_STATUS_UNVERSIONED)
3256 while (path[0] == '/')
3258 if (upa->verbosity >= 0)
3259 printf("%c %s\n", status, path);
3264 static const struct got_error *
3265 switch_head_ref(struct got_reference *head_ref,
3266 struct got_object_id *commit_id, struct got_worktree *worktree,
3267 struct got_repository *repo)
3269 const struct got_error *err = NULL;
3271 int ref_has_moved = 0;
3273 /* Trivial case: switching between two different references. */
3274 if (strcmp(got_ref_get_name(head_ref),
3275 got_worktree_get_head_ref_name(worktree)) != 0) {
3276 printf("Switching work tree from %s to %s\n",
3277 got_worktree_get_head_ref_name(worktree),
3278 got_ref_get_name(head_ref));
3279 return got_worktree_set_head_ref(worktree, head_ref);
3282 err = check_linear_ancestry(commit_id,
3283 got_worktree_get_base_commit_id(worktree), 0, repo);
3285 if (err->code != GOT_ERR_ANCESTRY)
3292 /* Switching to a rebased branch with the same reference name. */
3293 err = got_object_id_str(&base_id_str,
3294 got_worktree_get_base_commit_id(worktree));
3297 printf("Reference %s now points at a different branch\n",
3298 got_worktree_get_head_ref_name(worktree));
3299 printf("Switching work tree from %s to %s\n", base_id_str,
3300 got_worktree_get_head_ref_name(worktree));
3304 static const struct got_error *
3305 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3307 const struct got_error *err;
3310 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3314 return got_error(GOT_ERR_REBASING);
3316 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3320 return got_error(GOT_ERR_HISTEDIT_BUSY);
3325 static const struct got_error *
3326 check_merge_in_progress(struct got_worktree *worktree,
3327 struct got_repository *repo)
3329 const struct got_error *err;
3332 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3336 return got_error(GOT_ERR_MERGE_BUSY);
3341 static const struct got_error *
3342 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3343 char *argv[], struct got_worktree *worktree)
3345 const struct got_error *err = NULL;
3347 struct got_pathlist_entry *new;
3353 return got_error_from_errno("strdup");
3354 return got_pathlist_append(paths, path, NULL);
3357 for (i = 0; i < argc; i++) {
3358 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3361 err = got_pathlist_insert(&new, paths, path, NULL);
3362 if (err || new == NULL /* duplicate */) {
3372 static const struct got_error *
3373 wrap_not_worktree_error(const struct got_error *orig_err,
3374 const char *cmdname, const char *path)
3376 const struct got_error *err;
3377 struct got_repository *repo;
3378 static char msg[512];
3379 int *pack_fds = NULL;
3381 err = got_repo_pack_fds_open(&pack_fds);
3385 err = got_repo_open(&repo, path, NULL, pack_fds);
3389 snprintf(msg, sizeof(msg),
3390 "'got %s' needs a work tree in addition to a git repository\n"
3391 "Work trees can be checked out from this Git repository with "
3393 "The got(1) manual page contains more information.", cmdname);
3394 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3395 got_repo_close(repo);
3397 const struct got_error *pack_err =
3398 got_repo_pack_fds_close(pack_fds);
3405 static const struct got_error *
3406 cmd_update(int argc, char *argv[])
3408 const struct got_error *error = NULL;
3409 struct got_repository *repo = NULL;
3410 struct got_worktree *worktree = NULL;
3411 char *worktree_path = NULL;
3412 struct got_object_id *commit_id = NULL;
3413 char *commit_id_str = NULL;
3414 const char *branch_name = NULL;
3415 struct got_reference *head_ref = NULL;
3416 struct got_pathlist_head paths;
3417 struct got_pathlist_entry *pe;
3418 int ch, verbosity = 0;
3419 struct got_update_progress_arg upa;
3420 int *pack_fds = NULL;
3424 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3427 branch_name = optarg;
3430 commit_id_str = strdup(optarg);
3431 if (commit_id_str == NULL)
3432 return got_error_from_errno("strdup");
3447 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3448 "unveil", NULL) == -1)
3451 worktree_path = getcwd(NULL, 0);
3452 if (worktree_path == NULL) {
3453 error = got_error_from_errno("getcwd");
3457 error = got_repo_pack_fds_open(&pack_fds);
3461 error = got_worktree_open(&worktree, worktree_path);
3463 if (error->code == GOT_ERR_NOT_WORKTREE)
3464 error = wrap_not_worktree_error(error, "update",
3469 error = check_rebase_or_histedit_in_progress(worktree);
3473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3478 error = apply_unveil(got_repo_get_path(repo), 0,
3479 got_worktree_get_root_path(worktree));
3483 error = check_merge_in_progress(worktree, repo);
3487 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3491 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3492 got_worktree_get_head_ref_name(worktree), 0);
3495 if (commit_id_str == NULL) {
3496 error = got_ref_resolve(&commit_id, repo, head_ref);
3499 error = got_object_id_str(&commit_id_str, commit_id);
3503 struct got_reflist_head refs;
3505 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3509 error = got_repo_match_object_id(&commit_id, NULL,
3510 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3511 got_ref_list_free(&refs);
3512 free(commit_id_str);
3513 commit_id_str = NULL;
3516 error = got_object_id_str(&commit_id_str, commit_id);
3522 struct got_object_id *head_commit_id;
3523 TAILQ_FOREACH(pe, &paths, entry) {
3524 if (pe->path_len == 0)
3526 error = got_error_msg(GOT_ERR_BAD_PATH,
3527 "switching between branches requires that "
3528 "the entire work tree gets updated");
3531 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3534 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3536 free(head_commit_id);
3539 error = check_same_branch(commit_id, head_ref, NULL, repo);
3542 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3546 error = check_linear_ancestry(commit_id,
3547 got_worktree_get_base_commit_id(worktree), 0, repo);
3548 if (error != NULL) {
3549 if (error->code == GOT_ERR_ANCESTRY)
3550 error = got_error(GOT_ERR_BRANCH_MOVED);
3553 error = check_same_branch(commit_id, head_ref, NULL, repo);
3558 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3560 error = got_worktree_set_base_commit_id(worktree, repo,
3566 memset(&upa, 0, sizeof(upa));
3567 upa.verbosity = verbosity;
3568 error = got_worktree_checkout_files(worktree, &paths, repo,
3569 update_progress, &upa, check_cancelled, NULL);
3573 if (upa.did_something) {
3574 printf("Updated to %s: %s\n",
3575 got_worktree_get_head_ref_name(worktree), commit_id_str);
3577 printf("Already up-to-date\n");
3579 print_update_progress_stats(&upa);
3582 const struct got_error *pack_err =
3583 got_repo_pack_fds_close(pack_fds);
3587 free(worktree_path);
3588 TAILQ_FOREACH(pe, &paths, entry)
3589 free((char *)pe->path);
3590 got_pathlist_free(&paths);
3592 free(commit_id_str);
3596 static const struct got_error *
3597 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3598 const char *path, int diff_context, int ignore_whitespace,
3599 int force_text_diff, struct got_repository *repo, FILE *outfile)
3601 const struct got_error *err = NULL;
3602 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3603 FILE *f1 = NULL, *f2 = NULL;
3604 int fd1 = -1, fd2 = -1;
3606 fd1 = got_opentempfd();
3608 return got_error_from_errno("got_opentempfd");
3609 fd2 = got_opentempfd();
3611 err = got_error_from_errno("got_opentempfd");
3616 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3622 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3626 f1 = got_opentemp();
3628 err = got_error_from_errno("got_opentemp");
3631 f2 = got_opentemp();
3633 err = got_error_from_errno("got_opentemp");
3637 while (path[0] == '/')
3639 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3640 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3641 force_text_diff, outfile);
3643 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3644 err = got_error_from_errno("close");
3646 got_object_blob_close(blob1);
3647 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3648 err = got_error_from_errno("close");
3649 got_object_blob_close(blob2);
3650 if (f1 && fclose(f1) == EOF && err == NULL)
3651 err = got_error_from_errno("fclose");
3652 if (f2 && fclose(f2) == EOF && err == NULL)
3653 err = got_error_from_errno("fclose");
3657 static const struct got_error *
3658 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3659 const char *path, int diff_context, int ignore_whitespace,
3660 int force_text_diff, struct got_repository *repo, FILE *outfile)
3662 const struct got_error *err = NULL;
3663 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3664 struct got_diff_blob_output_unidiff_arg arg;
3665 FILE *f1 = NULL, *f2 = NULL;
3666 int fd1 = -1, fd2 = -1;
3669 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3672 fd1 = got_opentempfd();
3674 err = got_error_from_errno("got_opentempfd");
3679 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3683 f1 = got_opentemp();
3685 err = got_error_from_errno("got_opentemp");
3689 f2 = got_opentemp();
3691 err = got_error_from_errno("got_opentemp");
3694 fd2 = got_opentempfd();
3696 err = got_error_from_errno("got_opentempfd");
3699 arg.diff_context = diff_context;
3700 arg.ignore_whitespace = ignore_whitespace;
3701 arg.force_text_diff = force_text_diff;
3702 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3703 arg.outfile = outfile;
3704 arg.line_offsets = NULL;
3706 while (path[0] == '/')
3708 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3709 got_diff_blob_output_unidiff, &arg, 1);
3712 got_object_tree_close(tree1);
3714 got_object_tree_close(tree2);
3715 if (f1 && fclose(f1) == EOF && err == NULL)
3716 err = got_error_from_errno("fclose");
3717 if (f2 && fclose(f2) == EOF && err == NULL)
3718 err = got_error_from_errno("fclose");
3719 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3720 err = got_error_from_errno("close");
3721 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3722 err = got_error_from_errno("close");
3726 static const struct got_error *
3727 get_changed_paths(struct got_pathlist_head *paths,
3728 struct got_commit_object *commit, struct got_repository *repo)
3730 const struct got_error *err = NULL;
3731 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3732 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3733 struct got_object_qid *qid;
3735 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3737 struct got_commit_object *pcommit;
3738 err = got_object_open_as_commit(&pcommit, repo,
3743 tree_id1 = got_object_id_dup(
3744 got_object_commit_get_tree_id(pcommit));
3745 if (tree_id1 == NULL) {
3746 got_object_commit_close(pcommit);
3747 return got_error_from_errno("got_object_id_dup");
3749 got_object_commit_close(pcommit);
3754 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3759 tree_id2 = got_object_commit_get_tree_id(commit);
3760 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3764 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3765 got_diff_tree_collect_changed_paths, paths, 0);
3768 got_object_tree_close(tree1);
3770 got_object_tree_close(tree2);
3775 static const struct got_error *
3776 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3777 const char *path, int diff_context, struct got_repository *repo,
3780 const struct got_error *err = NULL;
3781 struct got_commit_object *pcommit = NULL;
3782 char *id_str1 = NULL, *id_str2 = NULL;
3783 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3784 struct got_object_qid *qid;
3786 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3788 err = got_object_open_as_commit(&pcommit, repo,
3792 err = got_object_id_str(&id_str1, &qid->id);
3797 err = got_object_id_str(&id_str2, id);
3801 if (path && path[0] != '\0') {
3803 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3807 err = got_object_id_by_path(&obj_id1, repo,
3810 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3816 err = got_object_get_type(&obj_type, repo, obj_id2);
3822 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3823 fprintf(outfile, "commit - %s\n",
3824 id_str1 ? id_str1 : "/dev/null");
3825 fprintf(outfile, "commit + %s\n", id_str2);
3827 case GOT_OBJ_TYPE_BLOB:
3828 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3829 0, 0, repo, outfile);
3831 case GOT_OBJ_TYPE_TREE:
3832 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3833 0, 0, repo, outfile);
3836 err = got_error(GOT_ERR_OBJ_TYPE);
3842 obj_id2 = got_object_commit_get_tree_id(commit);
3844 obj_id1 = got_object_commit_get_tree_id(pcommit);
3846 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3847 fprintf(outfile, "commit - %s\n",
3848 id_str1 ? id_str1 : "/dev/null");
3849 fprintf(outfile, "commit + %s\n", id_str2);
3850 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3857 got_object_commit_close(pcommit);
3862 get_datestr(time_t *time, char *datebuf)
3864 struct tm mytm, *tm;
3867 tm = gmtime_r(time, &mytm);
3870 s = asctime_r(tm, datebuf);
3873 p = strchr(s, '\n');
3879 static const struct got_error *
3880 match_commit(int *have_match, struct got_object_id *id,
3881 struct got_commit_object *commit, regex_t *regex)
3883 const struct got_error *err = NULL;
3884 regmatch_t regmatch;
3885 char *id_str = NULL, *logmsg = NULL;
3889 err = got_object_id_str(&id_str, id);
3893 err = got_object_commit_get_logmsg(&logmsg, commit);
3897 if (regexec(regex, got_object_commit_get_author(commit), 1,
3898 ®match, 0) == 0 ||
3899 regexec(regex, got_object_commit_get_committer(commit), 1,
3900 ®match, 0) == 0 ||
3901 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3902 regexec(regex, logmsg, 1, ®match, 0) == 0)
3911 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3914 regmatch_t regmatch;
3915 struct got_pathlist_entry *pe;
3919 TAILQ_FOREACH(pe, changed_paths, entry) {
3920 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3927 static const struct got_error *
3928 match_patch(int *have_match, struct got_commit_object *commit,
3929 struct got_object_id *id, const char *path, int diff_context,
3930 struct got_repository *repo, regex_t *regex, FILE *f)
3932 const struct got_error *err = NULL;
3934 size_t linesize = 0;
3935 regmatch_t regmatch;
3939 err = got_opentemp_truncate(f);
3943 err = print_patch(commit, id, path, diff_context, repo, f);
3947 if (fseeko(f, 0L, SEEK_SET) == -1) {
3948 err = got_error_from_errno("fseeko");
3952 while (getline(&line, &linesize, f) != -1) {
3953 if (regexec(regex, line, 1, ®match, 0) == 0) {
3963 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3965 static const struct got_error*
3966 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3967 struct got_object_id *id, struct got_repository *repo,
3970 static const struct got_error *err = NULL;
3971 struct got_reflist_entry *re;
3977 TAILQ_FOREACH(re, refs, entry) {
3978 struct got_tag_object *tag = NULL;
3979 struct got_object_id *ref_id;
3982 name = got_ref_get_name(re->ref);
3983 if (strcmp(name, GOT_REF_HEAD) == 0)
3985 if (strncmp(name, "refs/", 5) == 0)
3987 if (strncmp(name, "got/", 4) == 0)
3989 if (strncmp(name, "heads/", 6) == 0)
3991 if (strncmp(name, "remotes/", 8) == 0) {
3995 s = strstr(name, "/" GOT_REF_HEAD);
3996 if (s != NULL && s[strlen(s)] == '\0')
3999 err = got_ref_resolve(&ref_id, repo, re->ref);
4002 if (strncmp(name, "tags/", 5) == 0) {
4003 err = got_object_open_as_tag(&tag, repo, ref_id);
4005 if (err->code != GOT_ERR_OBJ_TYPE) {
4009 /* Ref points at something other than a tag. */
4014 cmp = got_object_id_cmp(tag ?
4015 got_object_tag_get_object_id(tag) : ref_id, id);
4018 got_object_tag_close(tag);
4022 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4023 s ? ", " : "", name) == -1) {
4024 err = got_error_from_errno("asprintf");
4035 static const struct got_error *
4036 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4037 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4039 const struct got_error *err = NULL;
4040 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4041 char *comma, *s, *nl;
4042 struct got_reflist_head *refs;
4043 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4045 time_t committer_time;
4047 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4049 err = build_refs_str(&ref_str, refs, id, repo, 1);
4053 /* Display the first matching ref only. */
4054 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4058 if (ref_str == NULL) {
4059 err = got_object_id_str(&id_str, id);
4064 committer_time = got_object_commit_get_committer_time(commit);
4065 if (gmtime_r(&committer_time, &tm) == NULL) {
4066 err = got_error_from_errno("gmtime_r");
4069 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4070 err = got_error(GOT_ERR_NO_SPACE);
4074 err = got_object_commit_get_logmsg(&logmsg0, commit);
4079 while (isspace((unsigned char)s[0]))
4082 nl = strchr(s, '\n');
4088 printf("%s%-7s %s\n", datebuf, ref_str, s);
4090 printf("%s%.7s %s\n", datebuf, id_str, s);
4092 if (fflush(stdout) != 0 && err == NULL)
4093 err = got_error_from_errno("fflush");
4101 static const struct got_error *
4102 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4103 struct got_repository *repo, const char *path,
4104 struct got_pathlist_head *changed_paths, int show_patch,
4105 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4106 const char *custom_refs_str)
4108 const struct got_error *err = NULL;
4109 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4111 time_t committer_time;
4112 const char *author, *committer;
4113 char *refs_str = NULL;
4115 err = got_object_id_str(&id_str, id);
4119 if (custom_refs_str == NULL) {
4120 struct got_reflist_head *refs;
4121 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4123 err = build_refs_str(&refs_str, refs, id, repo, 0);
4129 printf(GOT_COMMIT_SEP_STR);
4130 if (custom_refs_str)
4131 printf("commit %s (%s)\n", id_str, custom_refs_str);
4133 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4134 refs_str ? refs_str : "", refs_str ? ")" : "");
4139 printf("from: %s\n", got_object_commit_get_author(commit));
4140 committer_time = got_object_commit_get_committer_time(commit);
4141 datestr = get_datestr(&committer_time, datebuf);
4143 printf("date: %s UTC\n", datestr);
4144 author = got_object_commit_get_author(commit);
4145 committer = got_object_commit_get_committer(commit);
4146 if (strcmp(author, committer) != 0)
4147 printf("via: %s\n", committer);
4148 if (got_object_commit_get_nparents(commit) > 1) {
4149 const struct got_object_id_queue *parent_ids;
4150 struct got_object_qid *qid;
4152 parent_ids = got_object_commit_get_parent_ids(commit);
4153 STAILQ_FOREACH(qid, parent_ids, entry) {
4154 err = got_object_id_str(&id_str, &qid->id);
4157 printf("parent %d: %s\n", n++, id_str);
4163 err = got_object_commit_get_logmsg(&logmsg0, commit);
4169 line = strsep(&logmsg, "\n");
4171 printf(" %s\n", line);
4175 if (changed_paths) {
4176 struct got_pathlist_entry *pe;
4177 TAILQ_FOREACH(pe, changed_paths, entry) {
4178 struct got_diff_changed_path *cp = pe->data;
4179 printf(" %c %s\n", cp->status, pe->path);
4184 err = print_patch(commit, id, path, diff_context, repo, stdout);
4189 if (fflush(stdout) != 0 && err == NULL)
4190 err = got_error_from_errno("fflush");
4197 static const struct got_error *
4198 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4199 struct got_repository *repo, const char *path, int show_changed_paths,
4200 int show_patch, const char *search_pattern, int diff_context, int limit,
4201 int log_branches, int reverse_display_order,
4202 struct got_reflist_object_id_map *refs_idmap, int one_line,
4205 const struct got_error *err;
4206 struct got_commit_graph *graph;
4209 struct got_object_id_queue reversed_commits;
4210 struct got_object_qid *qid;
4211 struct got_commit_object *commit;
4212 struct got_pathlist_head changed_paths;
4213 struct got_pathlist_entry *pe;
4215 STAILQ_INIT(&reversed_commits);
4216 TAILQ_INIT(&changed_paths);
4218 if (search_pattern && regcomp(®ex, search_pattern,
4219 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4220 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4222 err = got_commit_graph_open(&graph, path, !log_branches);
4225 err = got_commit_graph_iter_start(graph, root_id, repo,
4226 check_cancelled, NULL);
4230 struct got_object_id *id;
4232 if (sigint_received || sigpipe_received)
4235 err = got_commit_graph_iter_next(&id, graph, repo,
4236 check_cancelled, NULL);
4238 if (err->code == GOT_ERR_ITER_COMPLETED)
4245 err = got_object_open_as_commit(&commit, repo, id);
4249 if (show_changed_paths && !reverse_display_order) {
4250 err = get_changed_paths(&changed_paths, commit, repo);
4255 if (search_pattern) {
4256 err = match_commit(&have_match, id, commit, ®ex);
4258 got_object_commit_close(commit);
4261 if (have_match == 0 && show_changed_paths)
4262 match_changed_paths(&have_match,
4263 &changed_paths, ®ex);
4264 if (have_match == 0 && show_patch) {
4265 err = match_patch(&have_match, commit, id,
4266 path, diff_context, repo, ®ex,
4271 if (have_match == 0) {
4272 got_object_commit_close(commit);
4273 TAILQ_FOREACH(pe, &changed_paths, entry) {
4274 free((char *)pe->path);
4277 got_pathlist_free(&changed_paths);
4282 if (reverse_display_order) {
4283 err = got_object_qid_alloc(&qid, id);
4286 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4287 got_object_commit_close(commit);
4290 err = print_commit_oneline(commit, id,
4293 err = print_commit(commit, id, repo, path,
4294 show_changed_paths ? &changed_paths : NULL,
4295 show_patch, diff_context, refs_idmap, NULL);
4296 got_object_commit_close(commit);
4300 if ((limit && --limit == 0) ||
4301 (end_id && got_object_id_cmp(id, end_id) == 0))
4304 TAILQ_FOREACH(pe, &changed_paths, entry) {
4305 free((char *)pe->path);
4308 got_pathlist_free(&changed_paths);
4310 if (reverse_display_order) {
4311 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4312 err = got_object_open_as_commit(&commit, repo,
4316 if (show_changed_paths) {
4317 err = get_changed_paths(&changed_paths,
4323 err = print_commit_oneline(commit, &qid->id,
4326 err = print_commit(commit, &qid->id, repo, path,
4327 show_changed_paths ? &changed_paths : NULL,
4328 show_patch, diff_context, refs_idmap, NULL);
4329 got_object_commit_close(commit);
4332 TAILQ_FOREACH(pe, &changed_paths, entry) {
4333 free((char *)pe->path);
4336 got_pathlist_free(&changed_paths);
4340 while (!STAILQ_EMPTY(&reversed_commits)) {
4341 qid = STAILQ_FIRST(&reversed_commits);
4342 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4343 got_object_qid_free(qid);
4345 TAILQ_FOREACH(pe, &changed_paths, entry) {
4346 free((char *)pe->path);
4349 got_pathlist_free(&changed_paths);
4352 got_commit_graph_close(graph);
4359 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4360 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4361 "[-r repository-path] [-R] [path]\n", getprogname());
4366 get_default_log_limit(void)
4368 const char *got_default_log_limit;
4372 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4373 if (got_default_log_limit == NULL)
4375 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4381 static const struct got_error *
4382 cmd_log(int argc, char *argv[])
4384 const struct got_error *error;
4385 struct got_repository *repo = NULL;
4386 struct got_worktree *worktree = NULL;
4387 struct got_object_id *start_id = NULL, *end_id = NULL;
4388 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4389 const char *start_commit = NULL, *end_commit = NULL;
4390 const char *search_pattern = NULL;
4391 int diff_context = -1, ch;
4392 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4393 int reverse_display_order = 0, one_line = 0;
4395 struct got_reflist_head refs;
4396 struct got_reflist_object_id_map *refs_idmap = NULL;
4397 FILE *tmpfile = NULL;
4398 int *pack_fds = NULL;
4403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4409 limit = get_default_log_limit();
4411 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4417 show_changed_paths = 1;
4420 start_commit = optarg;
4423 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4426 errx(1, "number of context lines is %s: %s",
4430 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4432 errx(1, "number of commits is %s: %s",
4439 repo_path = realpath(optarg, NULL);
4440 if (repo_path == NULL)
4441 return got_error_from_errno2("realpath",
4443 got_path_strip_trailing_slashes(repo_path);
4446 reverse_display_order = 1;
4452 search_pattern = optarg;
4455 end_commit = optarg;
4466 if (diff_context == -1)
4468 else if (!show_patch)
4469 errx(1, "-C requires -p");
4471 if (one_line && (show_patch || show_changed_paths))
4472 errx(1, "cannot use -s with -p or -P");
4474 cwd = getcwd(NULL, 0);
4476 error = got_error_from_errno("getcwd");
4480 error = got_repo_pack_fds_open(&pack_fds);
4484 if (repo_path == NULL) {
4485 error = got_worktree_open(&worktree, cwd);
4486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4493 error = got_worktree_resolve_path(&path, worktree,
4498 path = strdup(argv[0]);
4500 error = got_error_from_errno("strdup");
4504 } else if (argc != 0)
4507 if (repo_path == NULL) {
4508 repo_path = worktree ?
4509 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4511 if (repo_path == NULL) {
4512 error = got_error_from_errno("strdup");
4516 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4520 error = apply_unveil(got_repo_get_path(repo), 1,
4521 worktree ? got_worktree_get_root_path(worktree) : NULL);
4525 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4529 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4533 if (start_commit == NULL) {
4534 struct got_reference *head_ref;
4535 struct got_commit_object *commit = NULL;
4536 error = got_ref_open(&head_ref, repo,
4537 worktree ? got_worktree_get_head_ref_name(worktree)
4541 error = got_ref_resolve(&start_id, repo, head_ref);
4542 got_ref_close(head_ref);
4545 error = got_object_open_as_commit(&commit, repo,
4549 got_object_commit_close(commit);
4551 error = got_repo_match_object_id(&start_id, NULL,
4552 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4556 if (end_commit != NULL) {
4557 error = got_repo_match_object_id(&end_id, NULL,
4558 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4565 * If a path was specified on the command line it was resolved
4566 * to a path in the work tree above. Prepend the work tree's
4567 * path prefix to obtain the corresponding in-repository path.
4571 prefix = got_worktree_get_path_prefix(worktree);
4572 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4573 (path[0] != '\0') ? "/" : "", path) == -1) {
4574 error = got_error_from_errno("asprintf");
4579 error = got_repo_map_path(&in_repo_path, repo,
4585 path = in_repo_path;
4589 /* Release work tree lock. */
4590 got_worktree_close(worktree);
4594 if (search_pattern && show_patch) {
4595 tmpfile = got_opentemp();
4596 if (tmpfile == NULL) {
4597 error = got_error_from_errno("got_opentemp");
4602 error = print_commits(start_id, end_id, repo, path ? path : "",
4603 show_changed_paths, show_patch, search_pattern, diff_context,
4604 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4611 got_worktree_close(worktree);
4613 const struct got_error *close_err = got_repo_close(repo);
4618 const struct got_error *pack_err =
4619 got_repo_pack_fds_close(pack_fds);
4624 got_reflist_object_id_map_free(refs_idmap);
4625 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4626 error = got_error_from_errno("fclose");
4627 got_ref_list_free(&refs);
4634 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4635 "[-r repository-path] [-s] [-w] [-P] "
4636 "[object1 object2 | path ...]\n", getprogname());
4640 struct print_diff_arg {
4641 struct got_repository *repo;
4642 struct got_worktree *worktree;
4647 enum got_diff_algorithm diff_algo;
4648 int ignore_whitespace;
4649 int force_text_diff;
4655 * Create a file which contains the target path of a symlink so we can feed
4656 * it as content to the diff engine.
4658 static const struct got_error *
4659 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4660 const char *abspath)
4662 const struct got_error *err = NULL;
4663 char target_path[PATH_MAX];
4664 ssize_t target_len, outlen;
4669 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4670 if (target_len == -1)
4671 return got_error_from_errno2("readlinkat", abspath);
4673 target_len = readlink(abspath, target_path, PATH_MAX);
4674 if (target_len == -1)
4675 return got_error_from_errno2("readlink", abspath);
4678 *fd = got_opentempfd();
4680 return got_error_from_errno("got_opentempfd");
4682 outlen = write(*fd, target_path, target_len);
4684 err = got_error_from_errno("got_opentempfd");
4688 if (lseek(*fd, 0, SEEK_SET) == -1) {
4689 err = got_error_from_errno2("lseek", abspath);
4700 static const struct got_error *
4701 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4702 const char *path, struct got_object_id *blob_id,
4703 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4704 int dirfd, const char *de_name)
4706 struct print_diff_arg *a = arg;
4707 const struct got_error *err = NULL;
4708 struct got_blob_object *blob1 = NULL;
4709 int fd = -1, fd1 = -1, fd2 = -1;
4711 char *abspath = NULL, *label1 = NULL;
4716 if (a->diff_staged) {
4717 if (staged_status != GOT_STATUS_MODIFY &&
4718 staged_status != GOT_STATUS_ADD &&
4719 staged_status != GOT_STATUS_DELETE)
4722 if (staged_status == GOT_STATUS_DELETE)
4724 if (status == GOT_STATUS_NONEXISTENT)
4725 return got_error_set_errno(ENOENT, path);
4726 if (status != GOT_STATUS_MODIFY &&
4727 status != GOT_STATUS_ADD &&
4728 status != GOT_STATUS_DELETE &&
4729 status != GOT_STATUS_CONFLICT)
4733 err = got_opentemp_truncate(a->f1);
4735 return got_error_from_errno("got_opentemp_truncate");
4736 err = got_opentemp_truncate(a->f2);
4738 return got_error_from_errno("got_opentemp_truncate");
4740 if (!a->header_shown) {
4741 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4742 got_worktree_get_root_path(a->worktree));
4743 printf("commit - %s\n", a->id_str);
4744 printf("path + %s%s\n",
4745 got_worktree_get_root_path(a->worktree),
4746 a->diff_staged ? " (staged changes)" : "");
4747 a->header_shown = 1;
4750 if (a->diff_staged) {
4751 const char *label1 = NULL, *label2 = NULL;
4752 switch (staged_status) {
4753 case GOT_STATUS_MODIFY:
4757 case GOT_STATUS_ADD:
4760 case GOT_STATUS_DELETE:
4764 return got_error(GOT_ERR_FILE_STATUS);
4766 fd1 = got_opentempfd();
4768 err = got_error_from_errno("got_opentempfd");
4771 fd2 = got_opentempfd();
4773 err = got_error_from_errno("got_opentempfd");
4776 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4777 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4778 a->diff_algo, a->diff_context, a->ignore_whitespace,
4779 a->force_text_diff, a->repo, stdout);
4783 fd1 = got_opentempfd();
4785 err = got_error_from_errno("got_opentempfd");
4789 if (staged_status == GOT_STATUS_ADD ||
4790 staged_status == GOT_STATUS_MODIFY) {
4792 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4796 err = got_object_id_str(&id_str, staged_blob_id);
4799 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4800 err = got_error_from_errno("asprintf");
4805 } else if (status != GOT_STATUS_ADD) {
4806 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4812 if (status != GOT_STATUS_DELETE) {
4813 if (asprintf(&abspath, "%s/%s",
4814 got_worktree_get_root_path(a->worktree), path) == -1) {
4815 err = got_error_from_errno("asprintf");
4820 fd = openat(dirfd, de_name,
4821 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4823 if (!got_err_open_nofollow_on_symlink()) {
4824 err = got_error_from_errno2("openat",
4828 err = get_symlink_target_file(&fd, dirfd,
4834 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4836 if (!got_err_open_nofollow_on_symlink()) {
4837 err = got_error_from_errno2("open",
4841 err = get_symlink_target_file(&fd, dirfd,
4847 if (fstat(fd, &sb) == -1) {
4848 err = got_error_from_errno2("fstat", abspath);
4851 f2 = fdopen(fd, "r");
4853 err = got_error_from_errno2("fdopen", abspath);
4863 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4869 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4870 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4871 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4874 err = got_error_from_errno("close");
4875 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4876 err = got_error_from_errno("close");
4878 got_object_blob_close(blob1);
4879 if (fd != -1 && close(fd) == -1 && err == NULL)
4880 err = got_error_from_errno("close");
4881 if (f2 && fclose(f2) == EOF && err == NULL)
4882 err = got_error_from_errno("fclose");
4887 static const struct got_error *
4888 cmd_diff(int argc, char *argv[])
4890 const struct got_error *error;
4891 struct got_repository *repo = NULL;
4892 struct got_worktree *worktree = NULL;
4893 char *cwd = NULL, *repo_path = NULL;
4894 const char *commit_args[2] = { NULL, NULL };
4895 int ncommit_args = 0;
4896 struct got_object_id *ids[2] = { NULL, NULL };
4897 char *labels[2] = { NULL, NULL };
4898 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4899 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4900 int force_text_diff = 0, force_path = 0, rflag = 0;
4902 struct got_reflist_head refs;
4903 struct got_pathlist_head paths;
4904 struct got_pathlist_entry *pe;
4905 FILE *f1 = NULL, *f2 = NULL;
4906 int fd1 = -1, fd2 = -1;
4907 int *pack_fds = NULL;
4913 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4918 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4921 force_text_diff = 1;
4924 if (ncommit_args >= 2)
4925 errx(1, "too many -c options used");
4926 commit_args[ncommit_args++] = optarg;
4929 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4932 errx(1, "number of context lines is %s: %s",
4936 repo_path = realpath(optarg, NULL);
4937 if (repo_path == NULL)
4938 return got_error_from_errno2("realpath",
4940 got_path_strip_trailing_slashes(repo_path);
4947 ignore_whitespace = 1;
4961 cwd = getcwd(NULL, 0);
4963 error = got_error_from_errno("getcwd");
4967 error = got_repo_pack_fds_open(&pack_fds);
4971 if (repo_path == NULL) {
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4979 strdup(got_worktree_get_repo_path(worktree));
4980 if (repo_path == NULL) {
4981 error = got_error_from_errno("strdup");
4985 repo_path = strdup(cwd);
4986 if (repo_path == NULL) {
4987 error = got_error_from_errno("strdup");
4993 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4998 if (rflag || worktree == NULL || ncommit_args > 0) {
5000 error = got_error_msg(GOT_ERR_NOT_IMPL,
5001 "-P option can only be used when diffing "
5006 error = got_error_msg(GOT_ERR_NOT_IMPL,
5007 "-s option can only be used when diffing "
5013 error = apply_unveil(got_repo_get_path(repo), 1,
5014 worktree ? got_worktree_get_root_path(worktree) : NULL);
5018 if ((!force_path && argc == 2) || ncommit_args > 0) {
5019 int obj_type = (ncommit_args > 0 ?
5020 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5021 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5025 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5027 if (ncommit_args > 0)
5028 arg = commit_args[i];
5031 error = got_repo_match_object_id(&ids[i], &labels[i],
5032 arg, obj_type, &refs, repo);
5034 if (error->code != GOT_ERR_NOT_REF &&
5035 error->code != GOT_ERR_NO_OBJ)
5037 if (ncommit_args > 0)
5045 f1 = got_opentemp();
5047 error = got_error_from_errno("got_opentemp");
5051 f2 = got_opentemp();
5053 error = got_error_from_errno("got_opentemp");
5057 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5058 struct print_diff_arg arg;
5061 if (worktree == NULL) {
5062 if (argc == 2 && ids[0] == NULL) {
5063 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5065 } else if (argc == 2 && ids[1] == NULL) {
5066 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5068 } else if (argc > 0) {
5069 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5070 "%s", "specified paths cannot be resolved");
5073 error = got_error(GOT_ERR_NOT_WORKTREE);
5078 error = get_worktree_paths_from_argv(&paths, argc, argv,
5083 error = got_object_id_str(&id_str,
5084 got_worktree_get_base_commit_id(worktree));
5088 arg.worktree = worktree;
5089 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5090 arg.diff_context = diff_context;
5091 arg.id_str = id_str;
5092 arg.header_shown = 0;
5093 arg.diff_staged = diff_staged;
5094 arg.ignore_whitespace = ignore_whitespace;
5095 arg.force_text_diff = force_text_diff;
5099 error = got_worktree_status(worktree, &paths, repo, 0,
5100 print_diff, &arg, check_cancelled, NULL);
5105 if (ncommit_args == 1) {
5106 struct got_commit_object *commit;
5107 error = got_object_open_as_commit(&commit, repo, ids[0]);
5111 labels[1] = labels[0];
5113 if (got_object_commit_get_nparents(commit) > 0) {
5114 const struct got_object_id_queue *pids;
5115 struct got_object_qid *pid;
5116 pids = got_object_commit_get_parent_ids(commit);
5117 pid = STAILQ_FIRST(pids);
5118 ids[0] = got_object_id_dup(&pid->id);
5119 if (ids[0] == NULL) {
5120 error = got_error_from_errno(
5121 "got_object_id_dup");
5122 got_object_commit_close(commit);
5125 error = got_object_id_str(&labels[0], ids[0]);
5127 got_object_commit_close(commit);
5132 labels[0] = strdup("/dev/null");
5133 if (labels[0] == NULL) {
5134 error = got_error_from_errno("strdup");
5135 got_object_commit_close(commit);
5140 got_object_commit_close(commit);
5143 if (ncommit_args == 0 && argc > 2) {
5144 error = got_error_msg(GOT_ERR_BAD_PATH,
5145 "path arguments cannot be used when diffing two objects");
5150 error = got_object_get_type(&type1, repo, ids[0]);
5155 error = got_object_get_type(&type2, repo, ids[1]);
5158 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5159 error = got_error(GOT_ERR_OBJ_TYPE);
5162 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5163 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5164 "path arguments cannot be used when diffing blobs");
5168 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5170 struct got_pathlist_entry *new;
5174 error = got_worktree_resolve_path(&p, worktree,
5178 prefix = got_worktree_get_path_prefix(worktree);
5179 while (prefix[0] == '/')
5181 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5182 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5184 error = got_error_from_errno("asprintf");
5190 char *mapped_path, *s;
5191 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5197 in_repo_path = strdup(s);
5198 if (in_repo_path == NULL) {
5199 error = got_error_from_errno("asprintf");
5206 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5207 if (error || new == NULL /* duplicate */)
5214 /* Release work tree lock. */
5215 got_worktree_close(worktree);
5219 fd1 = got_opentempfd();
5221 error = got_error_from_errno("got_opentempfd");
5225 fd2 = got_opentempfd();
5227 error = got_error_from_errno("got_opentempfd");
5231 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5232 case GOT_OBJ_TYPE_BLOB:
5233 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5234 fd1, fd2, ids[0], ids[1], NULL, NULL,
5235 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5236 ignore_whitespace, force_text_diff, repo, stdout);
5238 case GOT_OBJ_TYPE_TREE:
5239 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5240 ids[0], ids[1], &paths, "", "",
5241 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5242 ignore_whitespace, force_text_diff, repo, stdout);
5244 case GOT_OBJ_TYPE_COMMIT:
5245 printf("diff %s %s\n", labels[0], labels[1]);
5246 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5247 fd1, fd2, ids[0], ids[1], &paths,
5248 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5249 ignore_whitespace, force_text_diff, repo, stdout);
5252 error = got_error(GOT_ERR_OBJ_TYPE);
5260 got_worktree_close(worktree);
5262 const struct got_error *close_err = got_repo_close(repo);
5267 const struct got_error *pack_err =
5268 got_repo_pack_fds_close(pack_fds);
5272 TAILQ_FOREACH(pe, &paths, entry)
5273 free((char *)pe->path);
5274 got_pathlist_free(&paths);
5275 got_ref_list_free(&refs);
5276 if (f1 && fclose(f1) == EOF && error == NULL)
5277 error = got_error_from_errno("fclose");
5278 if (f2 && fclose(f2) == EOF && error == NULL)
5279 error = got_error_from_errno("fclose");
5280 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5281 error = got_error_from_errno("close");
5282 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5283 error = got_error_from_errno("close");
5291 "usage: %s blame [-c commit] [-r repository-path] path\n",
5300 char datebuf[11]; /* YYYY-MM-DD + NUL */
5303 struct blame_cb_args {
5304 struct blame_line *lines;
5308 off_t *line_offsets;
5310 struct got_repository *repo;
5313 static const struct got_error *
5314 blame_cb(void *arg, int nlines, int lineno,
5315 struct got_commit_object *commit, struct got_object_id *id)
5317 const struct got_error *err = NULL;
5318 struct blame_cb_args *a = arg;
5319 struct blame_line *bline;
5321 size_t linesize = 0;
5324 time_t committer_time;
5326 if (nlines != a->nlines ||
5327 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5328 return got_error(GOT_ERR_RANGE);
5330 if (sigint_received)
5331 return got_error(GOT_ERR_ITER_COMPLETED);
5334 return NULL; /* no change in this commit */
5336 /* Annotate this line. */
5337 bline = &a->lines[lineno - 1];
5338 if (bline->annotated)
5340 err = got_object_id_str(&bline->id_str, id);
5344 bline->committer = strdup(got_object_commit_get_committer(commit));
5345 if (bline->committer == NULL) {
5346 err = got_error_from_errno("strdup");
5350 committer_time = got_object_commit_get_committer_time(commit);
5351 if (gmtime_r(&committer_time, &tm) == NULL)
5352 return got_error_from_errno("gmtime_r");
5353 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5355 err = got_error(GOT_ERR_NO_SPACE);
5358 bline->annotated = 1;
5360 /* Print lines annotated so far. */
5361 bline = &a->lines[a->lineno_cur - 1];
5362 if (!bline->annotated)
5365 offset = a->line_offsets[a->lineno_cur - 1];
5366 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5367 err = got_error_from_errno("fseeko");
5371 while (bline->annotated) {
5372 char *smallerthan, *at, *nl, *committer;
5375 if (getline(&line, &linesize, a->f) == -1) {
5377 err = got_error_from_errno("getline");
5381 committer = bline->committer;
5382 smallerthan = strchr(committer, '<');
5383 if (smallerthan && smallerthan[1] != '\0')
5384 committer = smallerthan + 1;
5385 at = strchr(committer, '@');
5388 len = strlen(committer);
5390 committer[8] = '\0';
5392 nl = strchr(line, '\n');
5395 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5396 bline->id_str, bline->datebuf, committer, line);
5399 bline = &a->lines[a->lineno_cur - 1];
5406 static const struct got_error *
5407 cmd_blame(int argc, char *argv[])
5409 const struct got_error *error;
5410 struct got_repository *repo = NULL;
5411 struct got_worktree *worktree = NULL;
5412 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5413 char *link_target = NULL;
5414 struct got_object_id *obj_id = NULL;
5415 struct got_object_id *commit_id = NULL;
5416 struct got_commit_object *commit = NULL;
5417 struct got_blob_object *blob = NULL;
5418 char *commit_id_str = NULL;
5419 struct blame_cb_args bca;
5420 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5422 int *pack_fds = NULL;
5423 FILE *f1 = NULL, *f2 = NULL;
5425 fd1 = got_opentempfd();
5427 return got_error_from_errno("got_opentempfd");
5429 memset(&bca, 0, sizeof(bca));
5432 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5437 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5440 commit_id_str = optarg;
5443 repo_path = realpath(optarg, NULL);
5444 if (repo_path == NULL)
5445 return got_error_from_errno2("realpath",
5447 got_path_strip_trailing_slashes(repo_path);
5463 cwd = getcwd(NULL, 0);
5465 error = got_error_from_errno("getcwd");
5469 error = got_repo_pack_fds_open(&pack_fds);
5473 if (repo_path == NULL) {
5474 error = got_worktree_open(&worktree, cwd);
5475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5481 strdup(got_worktree_get_repo_path(worktree));
5482 if (repo_path == NULL) {
5483 error = got_error_from_errno("strdup");
5488 repo_path = strdup(cwd);
5489 if (repo_path == NULL) {
5490 error = got_error_from_errno("strdup");
5496 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5501 const char *prefix = got_worktree_get_path_prefix(worktree);
5504 error = got_worktree_resolve_path(&p, worktree, path);
5507 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5508 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5510 error = got_error_from_errno("asprintf");
5515 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5517 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5520 error = got_repo_map_path(&in_repo_path, repo, path);
5525 if (commit_id_str == NULL) {
5526 struct got_reference *head_ref;
5527 error = got_ref_open(&head_ref, repo, worktree ?
5528 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5531 error = got_ref_resolve(&commit_id, repo, head_ref);
5532 got_ref_close(head_ref);
5536 struct got_reflist_head refs;
5538 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5542 error = got_repo_match_object_id(&commit_id, NULL,
5543 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5544 got_ref_list_free(&refs);
5550 /* Release work tree lock. */
5551 got_worktree_close(worktree);
5555 error = got_object_open_as_commit(&commit, repo, commit_id);
5559 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5564 error = got_object_id_by_path(&obj_id, repo, commit,
5565 link_target ? link_target : in_repo_path);
5569 error = got_object_get_type(&obj_type, repo, obj_id);
5573 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5574 error = got_error_path(link_target ? link_target : in_repo_path,
5579 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5582 bca.f = got_opentemp();
5583 if (bca.f == NULL) {
5584 error = got_error_from_errno("got_opentemp");
5587 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5588 &bca.line_offsets, bca.f, blob);
5589 if (error || bca.nlines == 0)
5592 /* Don't include \n at EOF in the blame line count. */
5593 if (bca.line_offsets[bca.nlines - 1] == filesize)
5596 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5597 if (bca.lines == NULL) {
5598 error = got_error_from_errno("calloc");
5602 bca.nlines_prec = 0;
5610 fd2 = got_opentempfd();
5612 error = got_error_from_errno("got_opentempfd");
5615 fd3 = got_opentempfd();
5617 error = got_error_from_errno("got_opentempfd");
5620 f1 = got_opentemp();
5622 error = got_error_from_errno("got_opentemp");
5625 f2 = got_opentemp();
5627 error = got_error_from_errno("got_opentemp");
5630 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5631 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5632 check_cancelled, NULL, fd2, fd3, f1, f2);
5641 got_object_commit_close(commit);
5643 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5644 error = got_error_from_errno("close");
5645 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5646 error = got_error_from_errno("close");
5647 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5648 error = got_error_from_errno("close");
5649 if (f1 && fclose(f1) == EOF && error == NULL)
5650 error = got_error_from_errno("fclose");
5651 if (f2 && fclose(f2) == EOF && error == NULL)
5652 error = got_error_from_errno("fclose");
5655 got_object_blob_close(blob);
5657 got_worktree_close(worktree);
5659 const struct got_error *close_err = got_repo_close(repo);
5664 const struct got_error *pack_err =
5665 got_repo_pack_fds_close(pack_fds);
5670 for (i = 0; i < bca.nlines; i++) {
5671 struct blame_line *bline = &bca.lines[i];
5672 free(bline->id_str);
5673 free(bline->committer);
5677 free(bca.line_offsets);
5678 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5679 error = got_error_from_errno("fclose");
5687 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5692 static const struct got_error *
5693 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5694 const char *root_path, struct got_repository *repo)
5696 const struct got_error *err = NULL;
5697 int is_root_path = (strcmp(path, root_path) == 0);
5698 const char *modestr = "";
5699 mode_t mode = got_tree_entry_get_mode(te);
5700 char *link_target = NULL;
5702 path += strlen(root_path);
5703 while (path[0] == '/')
5706 if (got_object_tree_entry_is_submodule(te))
5708 else if (S_ISLNK(mode)) {
5711 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5714 for (i = 0; i < strlen(link_target); i++) {
5715 if (!isprint((unsigned char)link_target[i]))
5716 link_target[i] = '?';
5721 else if (S_ISDIR(mode))
5723 else if (mode & S_IXUSR)
5726 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5727 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5728 link_target ? " -> ": "", link_target ? link_target : "");
5734 static const struct got_error *
5735 print_tree(const char *path, struct got_commit_object *commit,
5736 int show_ids, int recurse, const char *root_path,
5737 struct got_repository *repo)
5739 const struct got_error *err = NULL;
5740 struct got_object_id *tree_id = NULL;
5741 struct got_tree_object *tree = NULL;
5744 err = got_object_id_by_path(&tree_id, repo, commit, path);
5748 err = got_object_open_as_tree(&tree, repo, tree_id);
5751 nentries = got_object_tree_get_nentries(tree);
5752 for (i = 0; i < nentries; i++) {
5753 struct got_tree_entry *te;
5756 if (sigint_received || sigpipe_received)
5759 te = got_object_tree_get_entry(tree, i);
5762 err = got_object_id_str(&id_str,
5763 got_tree_entry_get_id(te));
5766 if (asprintf(&id, "%s ", id_str) == -1) {
5767 err = got_error_from_errno("asprintf");
5773 err = print_entry(te, id, path, root_path, repo);
5778 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5780 if (asprintf(&child_path, "%s%s%s", path,
5781 path[0] == '/' && path[1] == '\0' ? "" : "/",
5782 got_tree_entry_get_name(te)) == -1) {
5783 err = got_error_from_errno("asprintf");
5786 err = print_tree(child_path, commit, show_ids, 1,
5795 got_object_tree_close(tree);
5800 static const struct got_error *
5801 cmd_tree(int argc, char *argv[])
5803 const struct got_error *error;
5804 struct got_repository *repo = NULL;
5805 struct got_worktree *worktree = NULL;
5806 const char *path, *refname = NULL;
5807 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5808 struct got_object_id *commit_id = NULL;
5809 struct got_commit_object *commit = NULL;
5810 char *commit_id_str = NULL;
5811 int show_ids = 0, recurse = 0;
5813 int *pack_fds = NULL;
5816 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5821 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5824 commit_id_str = optarg;
5827 repo_path = realpath(optarg, NULL);
5828 if (repo_path == NULL)
5829 return got_error_from_errno2("realpath",
5831 got_path_strip_trailing_slashes(repo_path);
5855 cwd = getcwd(NULL, 0);
5857 error = got_error_from_errno("getcwd");
5861 error = got_repo_pack_fds_open(&pack_fds);
5865 if (repo_path == NULL) {
5866 error = got_worktree_open(&worktree, cwd);
5867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5873 strdup(got_worktree_get_repo_path(worktree));
5874 if (repo_path == NULL)
5875 error = got_error_from_errno("strdup");
5879 repo_path = strdup(cwd);
5880 if (repo_path == NULL) {
5881 error = got_error_from_errno("strdup");
5887 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5892 const char *prefix = got_worktree_get_path_prefix(worktree);
5897 error = got_worktree_resolve_path(&p, worktree, path);
5900 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5901 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5903 error = got_error_from_errno("asprintf");
5908 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5912 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5917 error = got_repo_map_path(&in_repo_path, repo, path);
5922 if (commit_id_str == NULL) {
5923 struct got_reference *head_ref;
5925 refname = got_worktree_get_head_ref_name(worktree);
5927 refname = GOT_REF_HEAD;
5928 error = got_ref_open(&head_ref, repo, refname, 0);
5931 error = got_ref_resolve(&commit_id, repo, head_ref);
5932 got_ref_close(head_ref);
5936 struct got_reflist_head refs;
5938 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5942 error = got_repo_match_object_id(&commit_id, NULL,
5943 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5944 got_ref_list_free(&refs);
5950 /* Release work tree lock. */
5951 got_worktree_close(worktree);
5955 error = got_object_open_as_commit(&commit, repo, commit_id);
5959 error = print_tree(in_repo_path, commit, show_ids, recurse,
5960 in_repo_path, repo);
5967 got_object_commit_close(commit);
5969 got_worktree_close(worktree);
5971 const struct got_error *close_err = got_repo_close(repo);
5976 const struct got_error *pack_err =
5977 got_repo_pack_fds_close(pack_fds);
5987 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5988 "[-S status-codes] [path ...]\n", getprogname());
5992 struct got_status_arg {
5997 static const struct got_error *
5998 print_status(void *arg, unsigned char status, unsigned char staged_status,
5999 const char *path, struct got_object_id *blob_id,
6000 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6001 int dirfd, const char *de_name)
6003 struct got_status_arg *st = arg;
6005 if (status == staged_status && (status == GOT_STATUS_DELETE))
6006 status = GOT_STATUS_NO_CHANGE;
6007 if (st != NULL && st->status_codes) {
6008 size_t ncodes = strlen(st->status_codes);
6011 for (i = 0; i < ncodes ; i++) {
6013 if (status == st->status_codes[i] ||
6014 staged_status == st->status_codes[i]) {
6019 if (status == st->status_codes[i] ||
6020 staged_status == st->status_codes[i])
6025 if (st->suppress && j == 0)
6032 printf("%c%c %s\n", status, staged_status, path);
6036 static const struct got_error *
6037 cmd_status(int argc, char *argv[])
6039 const struct got_error *error = NULL;
6040 struct got_repository *repo = NULL;
6041 struct got_worktree *worktree = NULL;
6042 struct got_status_arg st;
6044 struct got_pathlist_head paths;
6045 struct got_pathlist_entry *pe;
6046 int ch, i, no_ignores = 0;
6047 int *pack_fds = NULL;
6051 memset(&st, 0, sizeof(st));
6052 st.status_codes = NULL;
6055 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6061 if (st.status_codes != NULL && st.suppress == 0)
6062 option_conflict('S', 's');
6066 for (i = 0; i < strlen(optarg); i++) {
6067 switch (optarg[i]) {
6068 case GOT_STATUS_MODIFY:
6069 case GOT_STATUS_ADD:
6070 case GOT_STATUS_DELETE:
6071 case GOT_STATUS_CONFLICT:
6072 case GOT_STATUS_MISSING:
6073 case GOT_STATUS_OBSTRUCTED:
6074 case GOT_STATUS_UNVERSIONED:
6075 case GOT_STATUS_MODE_CHANGE:
6076 case GOT_STATUS_NONEXISTENT:
6079 errx(1, "invalid status code '%c'",
6083 if (ch == 's' && st.suppress)
6084 option_conflict('s', 'S');
6085 st.status_codes = optarg;
6097 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6101 cwd = getcwd(NULL, 0);
6103 error = got_error_from_errno("getcwd");
6107 error = got_repo_pack_fds_open(&pack_fds);
6111 error = got_worktree_open(&worktree, cwd);
6113 if (error->code == GOT_ERR_NOT_WORKTREE)
6114 error = wrap_not_worktree_error(error, "status", cwd);
6118 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6123 error = apply_unveil(got_repo_get_path(repo), 1,
6124 got_worktree_get_root_path(worktree));
6128 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6132 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6133 print_status, &st, check_cancelled, NULL);
6136 const struct got_error *pack_err =
6137 got_repo_pack_fds_close(pack_fds);
6142 TAILQ_FOREACH(pe, &paths, entry)
6143 free((char *)pe->path);
6144 got_pathlist_free(&paths);
6153 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6154 "[-s reference] [-d] [name]\n",
6159 static const struct got_error *
6160 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6162 static const struct got_error *err = NULL;
6163 struct got_reflist_head refs;
6164 struct got_reflist_entry *re;
6167 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6168 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6173 TAILQ_FOREACH(re, &refs, entry) {
6175 refstr = got_ref_to_str(re->ref);
6176 if (refstr == NULL) {
6177 err = got_error_from_errno("got_ref_to_str");
6180 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6184 got_ref_list_free(&refs);
6188 static const struct got_error *
6189 delete_ref_by_name(struct got_repository *repo, const char *refname)
6191 const struct got_error *err;
6192 struct got_reference *ref;
6194 err = got_ref_open(&ref, repo, refname, 0);
6198 err = delete_ref(repo, ref);
6203 static const struct got_error *
6204 add_ref(struct got_repository *repo, const char *refname, const char *target)
6206 const struct got_error *err = NULL;
6207 struct got_object_id *id = NULL;
6208 struct got_reference *ref = NULL;
6209 struct got_reflist_head refs;
6212 * Don't let the user create a reference name with a leading '-'.
6213 * While technically a valid reference name, this case is usually
6214 * an unintended typo.
6216 if (refname[0] == '-')
6217 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6220 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6223 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6225 got_ref_list_free(&refs);
6229 err = got_ref_alloc(&ref, refname, id);
6233 err = got_ref_write(ref, repo);
6241 static const struct got_error *
6242 add_symref(struct got_repository *repo, const char *refname, const char *target)
6244 const struct got_error *err = NULL;
6245 struct got_reference *ref = NULL;
6246 struct got_reference *target_ref = NULL;
6249 * Don't let the user create a reference name with a leading '-'.
6250 * While technically a valid reference name, this case is usually
6251 * an unintended typo.
6253 if (refname[0] == '-')
6254 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6256 err = got_ref_open(&target_ref, repo, target, 0);
6260 err = got_ref_alloc_symref(&ref, refname, target_ref);
6264 err = got_ref_write(ref, repo);
6267 got_ref_close(target_ref);
6273 static const struct got_error *
6274 cmd_ref(int argc, char *argv[])
6276 const struct got_error *error = NULL;
6277 struct got_repository *repo = NULL;
6278 struct got_worktree *worktree = NULL;
6279 char *cwd = NULL, *repo_path = NULL;
6280 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6281 const char *obj_arg = NULL, *symref_target= NULL;
6282 char *refname = NULL;
6283 int *pack_fds = NULL;
6285 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6294 repo_path = realpath(optarg, NULL);
6295 if (repo_path == NULL)
6296 return got_error_from_errno2("realpath",
6298 got_path_strip_trailing_slashes(repo_path);
6304 symref_target = optarg;
6315 if (obj_arg && do_list)
6316 option_conflict('c', 'l');
6317 if (obj_arg && do_delete)
6318 option_conflict('c', 'd');
6319 if (obj_arg && symref_target)
6320 option_conflict('c', 's');
6321 if (symref_target && do_delete)
6322 option_conflict('s', 'd');
6323 if (symref_target && do_list)
6324 option_conflict('s', 'l');
6325 if (do_delete && do_list)
6326 option_conflict('d', 'l');
6327 if (sort_by_time && !do_list)
6328 errx(1, "-t option requires -l option");
6334 if (argc != 0 && argc != 1)
6337 refname = strdup(argv[0]);
6338 if (refname == NULL) {
6339 error = got_error_from_errno("strdup");
6346 refname = strdup(argv[0]);
6347 if (refname == NULL) {
6348 error = got_error_from_errno("strdup");
6354 got_path_strip_trailing_slashes(refname);
6357 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6358 "sendfd unveil", NULL) == -1)
6361 cwd = getcwd(NULL, 0);
6363 error = got_error_from_errno("getcwd");
6367 error = got_repo_pack_fds_open(&pack_fds);
6371 if (repo_path == NULL) {
6372 error = got_worktree_open(&worktree, cwd);
6373 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6379 strdup(got_worktree_get_repo_path(worktree));
6380 if (repo_path == NULL)
6381 error = got_error_from_errno("strdup");
6385 repo_path = strdup(cwd);
6386 if (repo_path == NULL) {
6387 error = got_error_from_errno("strdup");
6393 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6399 /* Remove "cpath" promise. */
6400 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6406 error = apply_unveil(got_repo_get_path(repo), do_list,
6407 worktree ? got_worktree_get_root_path(worktree) : NULL);
6412 error = list_refs(repo, refname, sort_by_time);
6414 error = delete_ref_by_name(repo, refname);
6415 else if (symref_target)
6416 error = add_symref(repo, refname, symref_target);
6418 if (obj_arg == NULL)
6420 error = add_ref(repo, refname, obj_arg);
6425 const struct got_error *close_err = got_repo_close(repo);
6430 got_worktree_close(worktree);
6432 const struct got_error *pack_err =
6433 got_repo_pack_fds_close(pack_fds);
6446 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6447 "[-n] [name]\n", getprogname());
6451 static const struct got_error *
6452 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6453 struct got_reference *ref)
6455 const struct got_error *err = NULL;
6456 const char *refname, *marker = " ";
6459 refname = got_ref_get_name(ref);
6460 if (worktree && strcmp(refname,
6461 got_worktree_get_head_ref_name(worktree)) == 0) {
6462 struct got_object_id *id = NULL;
6464 err = got_ref_resolve(&id, repo, ref);
6467 if (got_object_id_cmp(id,
6468 got_worktree_get_base_commit_id(worktree)) == 0)
6475 if (strncmp(refname, "refs/heads/", 11) == 0)
6477 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6479 if (strncmp(refname, "refs/remotes/", 13) == 0)
6482 refstr = got_ref_to_str(ref);
6484 return got_error_from_errno("got_ref_to_str");
6486 printf("%s%s: %s\n", marker, refname, refstr);
6491 static const struct got_error *
6492 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6494 const char *refname;
6496 if (worktree == NULL)
6497 return got_error(GOT_ERR_NOT_WORKTREE);
6499 refname = got_worktree_get_head_ref_name(worktree);
6501 if (strncmp(refname, "refs/heads/", 11) == 0)
6503 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6506 printf("%s\n", refname);
6511 static const struct got_error *
6512 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6515 static const struct got_error *err = NULL;
6516 struct got_reflist_head refs;
6517 struct got_reflist_entry *re;
6518 struct got_reference *temp_ref = NULL;
6519 int rebase_in_progress, histedit_in_progress;
6524 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6529 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6534 if (rebase_in_progress || histedit_in_progress) {
6535 err = got_ref_open(&temp_ref, repo,
6536 got_worktree_get_head_ref_name(worktree), 0);
6539 list_branch(repo, worktree, temp_ref);
6540 got_ref_close(temp_ref);
6544 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6545 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6550 TAILQ_FOREACH(re, &refs, entry)
6551 list_branch(repo, worktree, re->ref);
6553 got_ref_list_free(&refs);
6555 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6556 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6561 TAILQ_FOREACH(re, &refs, entry)
6562 list_branch(repo, worktree, re->ref);
6564 got_ref_list_free(&refs);
6569 static const struct got_error *
6570 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6571 const char *branch_name)
6573 const struct got_error *err = NULL;
6574 struct got_reference *ref = NULL;
6575 char *refname, *remote_refname = NULL;
6577 if (strncmp(branch_name, "refs/", 5) == 0)
6579 if (strncmp(branch_name, "heads/", 6) == 0)
6581 else if (strncmp(branch_name, "remotes/", 8) == 0)
6584 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6585 return got_error_from_errno("asprintf");
6587 if (asprintf(&remote_refname, "refs/remotes/%s",
6588 branch_name) == -1) {
6589 err = got_error_from_errno("asprintf");
6593 err = got_ref_open(&ref, repo, refname, 0);
6595 const struct got_error *err2;
6596 if (err->code != GOT_ERR_NOT_REF)
6599 * Keep 'err' intact such that if neither branch exists
6600 * we report "refs/heads" rather than "refs/remotes" in
6601 * our error message.
6603 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6610 strcmp(got_worktree_get_head_ref_name(worktree),
6611 got_ref_get_name(ref)) == 0) {
6612 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6613 "will not delete this work tree's current branch");
6617 err = delete_ref(repo, ref);
6622 free(remote_refname);
6626 static const struct got_error *
6627 add_branch(struct got_repository *repo, const char *branch_name,
6628 struct got_object_id *base_commit_id)
6630 const struct got_error *err = NULL;
6631 struct got_reference *ref = NULL;
6632 char *base_refname = NULL, *refname = NULL;
6635 * Don't let the user create a branch name with a leading '-'.
6636 * While technically a valid reference name, this case is usually
6637 * an unintended typo.
6639 if (branch_name[0] == '-')
6640 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6642 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6645 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6646 err = got_error_from_errno("asprintf");
6650 err = got_ref_open(&ref, repo, refname, 0);
6652 err = got_error(GOT_ERR_BRANCH_EXISTS);
6654 } else if (err->code != GOT_ERR_NOT_REF)
6657 err = got_ref_alloc(&ref, refname, base_commit_id);
6661 err = got_ref_write(ref, repo);
6670 static const struct got_error *
6671 cmd_branch(int argc, char *argv[])
6673 const struct got_error *error = NULL;
6674 struct got_repository *repo = NULL;
6675 struct got_worktree *worktree = NULL;
6676 char *cwd = NULL, *repo_path = NULL;
6677 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6678 const char *delref = NULL, *commit_id_arg = NULL;
6679 struct got_reference *ref = NULL;
6680 struct got_pathlist_head paths;
6681 struct got_pathlist_entry *pe;
6682 struct got_object_id *commit_id = NULL;
6683 char *commit_id_str = NULL;
6684 int *pack_fds = NULL;
6688 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6691 commit_id_arg = optarg;
6697 repo_path = realpath(optarg, NULL);
6698 if (repo_path == NULL)
6699 return got_error_from_errno2("realpath",
6701 got_path_strip_trailing_slashes(repo_path);
6718 if (do_list && delref)
6719 option_conflict('l', 'd');
6720 if (sort_by_time && !do_list)
6721 errx(1, "-t option requires -l option");
6726 if (!do_list && !delref && argc == 0)
6729 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6730 errx(1, "-c option can only be used when creating a branch");
6732 if (do_list || delref) {
6735 } else if (!do_show && argc != 1)
6739 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6740 "sendfd unveil", NULL) == -1)
6743 cwd = getcwd(NULL, 0);
6745 error = got_error_from_errno("getcwd");
6749 error = got_repo_pack_fds_open(&pack_fds);
6753 if (repo_path == NULL) {
6754 error = got_worktree_open(&worktree, cwd);
6755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6761 strdup(got_worktree_get_repo_path(worktree));
6762 if (repo_path == NULL)
6763 error = got_error_from_errno("strdup");
6767 repo_path = strdup(cwd);
6768 if (repo_path == NULL) {
6769 error = got_error_from_errno("strdup");
6775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6780 if (do_list || do_show) {
6781 /* Remove "cpath" promise. */
6782 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6788 error = apply_unveil(got_repo_get_path(repo), do_list,
6789 worktree ? got_worktree_get_root_path(worktree) : NULL);
6794 error = show_current_branch(repo, worktree);
6796 error = list_branches(repo, worktree, sort_by_time);
6798 error = delete_branch(repo, worktree, delref);
6800 struct got_reflist_head refs;
6802 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6806 if (commit_id_arg == NULL)
6807 commit_id_arg = worktree ?
6808 got_worktree_get_head_ref_name(worktree) :
6810 error = got_repo_match_object_id(&commit_id, NULL,
6811 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6812 got_ref_list_free(&refs);
6815 error = add_branch(repo, argv[0], commit_id);
6818 if (worktree && do_update) {
6819 struct got_update_progress_arg upa;
6820 char *branch_refname = NULL;
6822 error = got_object_id_str(&commit_id_str, commit_id);
6825 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6829 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6831 error = got_error_from_errno("asprintf");
6834 error = got_ref_open(&ref, repo, branch_refname, 0);
6835 free(branch_refname);
6838 error = switch_head_ref(ref, commit_id, worktree,
6842 error = got_worktree_set_base_commit_id(worktree, repo,
6846 memset(&upa, 0, sizeof(upa));
6847 error = got_worktree_checkout_files(worktree, &paths,
6848 repo, update_progress, &upa, check_cancelled,
6852 if (upa.did_something) {
6853 printf("Updated to %s: %s\n",
6854 got_worktree_get_head_ref_name(worktree),
6857 print_update_progress_stats(&upa);
6864 const struct got_error *close_err = got_repo_close(repo);
6869 got_worktree_close(worktree);
6871 const struct got_error *pack_err =
6872 got_repo_pack_fds_close(pack_fds);
6879 free(commit_id_str);
6880 TAILQ_FOREACH(pe, &paths, entry)
6881 free((char *)pe->path);
6882 got_pathlist_free(&paths);
6891 "usage: %s tag [-c commit] [-r repository] [-l] "
6892 "[-m message] [-s signer-id] [-V] name\n",
6898 static const struct got_error *
6899 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6901 const struct got_error *err = NULL;
6902 struct got_reflist_entry *re, *se, *new;
6903 struct got_object_id *re_id, *se_id;
6904 struct got_tag_object *re_tag, *se_tag;
6905 time_t re_time, se_time;
6907 STAILQ_FOREACH(re, tags, entry) {
6908 se = STAILQ_FIRST(sorted);
6910 err = got_reflist_entry_dup(&new, re);
6913 STAILQ_INSERT_HEAD(sorted, new, entry);
6916 err = got_ref_resolve(&re_id, repo, re->ref);
6919 err = got_object_open_as_tag(&re_tag, repo, re_id);
6923 re_time = got_object_tag_get_tagger_time(re_tag);
6924 got_object_tag_close(re_tag);
6928 err = got_ref_resolve(&se_id, repo, re->ref);
6931 err = got_object_open_as_tag(&se_tag, repo, se_id);
6935 se_time = got_object_tag_get_tagger_time(se_tag);
6936 got_object_tag_close(se_tag);
6938 if (se_time > re_time) {
6939 err = got_reflist_entry_dup(&new, re);
6942 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6945 se = STAILQ_NEXT(se, entry);
6954 static const struct got_error *
6955 get_tag_refname(char **refname, const char *tag_name)
6957 const struct got_error *err;
6959 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6960 *refname = strdup(tag_name);
6961 if (*refname == NULL)
6962 return got_error_from_errno("strdup");
6963 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6964 err = got_error_from_errno("asprintf");
6972 static const struct got_error *
6973 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6974 const char *allowed_signers, const char *revoked_signers, int verbosity)
6976 static const struct got_error *err = NULL;
6977 struct got_reflist_head refs;
6978 struct got_reflist_entry *re;
6979 char *wanted_refname = NULL;
6984 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6989 struct got_reference *ref;
6990 err = get_tag_refname(&wanted_refname, tag_name);
6993 /* Wanted tag reference should exist. */
6994 err = got_ref_open(&ref, repo, wanted_refname, 0);
7000 TAILQ_FOREACH(re, &refs, entry) {
7001 const char *refname;
7002 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7004 const char *tagger, *ssh_sig = NULL;
7005 char *sig_msg = NULL;
7007 struct got_object_id *id;
7008 struct got_tag_object *tag;
7009 struct got_commit_object *commit = NULL;
7011 refname = got_ref_get_name(re->ref);
7012 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7013 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7016 refstr = got_ref_to_str(re->ref);
7017 if (refstr == NULL) {
7018 err = got_error_from_errno("got_ref_to_str");
7022 err = got_ref_resolve(&id, repo, re->ref);
7025 err = got_object_open_as_tag(&tag, repo, id);
7027 if (err->code != GOT_ERR_OBJ_TYPE) {
7031 /* "lightweight" tag */
7032 err = got_object_open_as_commit(&commit, repo, id);
7037 tagger = got_object_commit_get_committer(commit);
7039 got_object_commit_get_committer_time(commit);
7040 err = got_object_id_str(&id_str, id);
7046 tagger = got_object_tag_get_tagger(tag);
7047 tagger_time = got_object_tag_get_tagger_time(tag);
7048 err = got_object_id_str(&id_str,
7049 got_object_tag_get_object_id(tag));
7055 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7056 got_object_tag_get_message(tag));
7057 if (ssh_sig && allowed_signers == NULL) {
7058 err = got_error_msg(
7059 GOT_ERR_VERIFY_TAG_SIGNATURE,
7060 "SSH signature verification requires "
7061 "setting allowed_signers in "
7067 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7069 printf("from: %s\n", tagger);
7070 datestr = get_datestr(&tagger_time, datebuf);
7072 printf("date: %s UTC\n", datestr);
7074 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7076 switch (got_object_tag_get_object_type(tag)) {
7077 case GOT_OBJ_TYPE_BLOB:
7078 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7081 case GOT_OBJ_TYPE_TREE:
7082 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7085 case GOT_OBJ_TYPE_COMMIT:
7086 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7089 case GOT_OBJ_TYPE_TAG:
7090 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7100 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7101 allowed_signers, revoked_signers, verbosity);
7102 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7106 printf("signature: %s", sig_msg);
7112 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7115 got_object_commit_close(commit);
7117 tagmsg0 = strdup(got_object_tag_get_message(tag));
7118 got_object_tag_close(tag);
7119 if (tagmsg0 == NULL) {
7120 err = got_error_from_errno("strdup");
7127 line = strsep(&tagmsg, "\n");
7129 printf(" %s\n", line);
7134 got_ref_list_free(&refs);
7135 free(wanted_refname);
7137 if (err == NULL && bad_sigs)
7138 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7142 static const struct got_error *
7143 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7144 const char *tag_name, const char *repo_path)
7146 const struct got_error *err = NULL;
7147 char *template = NULL, *initial_content = NULL;
7148 char *editor = NULL;
7149 int initial_content_len;
7152 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7153 err = got_error_from_errno("asprintf");
7157 initial_content_len = asprintf(&initial_content,
7158 "\n# tagging commit %s as %s\n",
7159 commit_id_str, tag_name);
7160 if (initial_content_len == -1) {
7161 err = got_error_from_errno("asprintf");
7165 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7169 if (write(fd, initial_content, initial_content_len) == -1) {
7170 err = got_error_from_errno2("write", *tagmsg_path);
7174 err = get_editor(&editor);
7177 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7178 initial_content_len, 1);
7180 free(initial_content);
7184 if (fd != -1 && close(fd) == -1 && err == NULL)
7185 err = got_error_from_errno2("close", *tagmsg_path);
7194 static const struct got_error *
7195 add_tag(struct got_repository *repo, const char *tagger,
7196 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7197 const char *signer_id, int verbosity)
7199 const struct got_error *err = NULL;
7200 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7201 char *label = NULL, *commit_id_str = NULL;
7202 struct got_reference *ref = NULL;
7203 char *refname = NULL, *tagmsg = NULL;
7204 char *tagmsg_path = NULL, *tag_id_str = NULL;
7205 int preserve_tagmsg = 0;
7206 struct got_reflist_head refs;
7211 * Don't let the user create a tag name with a leading '-'.
7212 * While technically a valid reference name, this case is usually
7213 * an unintended typo.
7215 if (tag_name[0] == '-')
7216 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7218 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7222 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7223 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7227 err = got_object_id_str(&commit_id_str, commit_id);
7231 err = get_tag_refname(&refname, tag_name);
7234 if (strncmp("refs/tags/", tag_name, 10) == 0)
7237 err = got_ref_open(&ref, repo, refname, 0);
7239 err = got_error(GOT_ERR_TAG_EXISTS);
7241 } else if (err->code != GOT_ERR_NOT_REF)
7244 if (tagmsg_arg == NULL) {
7245 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7246 tag_name, got_repo_get_path(repo));
7248 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7249 tagmsg_path != NULL)
7250 preserve_tagmsg = 1;
7253 /* Editor is done; we can now apply unveil(2) */
7254 err = got_sigs_apply_unveil();
7257 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7262 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7263 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7267 preserve_tagmsg = 1;
7271 err = got_ref_alloc(&ref, refname, tag_id);
7274 preserve_tagmsg = 1;
7278 err = got_ref_write(ref, repo);
7281 preserve_tagmsg = 1;
7285 err = got_object_id_str(&tag_id_str, tag_id);
7288 preserve_tagmsg = 1;
7291 printf("Created tag %s\n", tag_id_str);
7293 if (preserve_tagmsg) {
7294 fprintf(stderr, "%s: tag message preserved in %s\n",
7295 getprogname(), tagmsg_path);
7296 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7297 err = got_error_from_errno2("unlink", tagmsg_path);
7302 free(commit_id_str);
7306 got_ref_list_free(&refs);
7310 static const struct got_error *
7311 cmd_tag(int argc, char *argv[])
7313 const struct got_error *error = NULL;
7314 struct got_repository *repo = NULL;
7315 struct got_worktree *worktree = NULL;
7316 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7317 char *gitconfig_path = NULL, *tagger = NULL;
7318 char *allowed_signers = NULL, *revoked_signers = NULL;
7319 char *signer_id = NULL;
7320 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7321 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7322 int *pack_fds = NULL;
7324 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7327 commit_id_arg = optarg;
7333 repo_path = realpath(optarg, NULL);
7334 if (repo_path == NULL) {
7335 error = got_error_from_errno2("realpath",
7339 got_path_strip_trailing_slashes(repo_path);
7345 signer_id = strdup(optarg);
7346 if (signer_id == NULL) {
7347 error = got_error_from_errno("strdup");
7357 else if (verbosity < 3)
7369 if (do_list || verify_tags) {
7370 if (commit_id_arg != NULL)
7372 "-c option can only be used when creating a tag");
7375 option_conflict('l', 'm');
7377 option_conflict('V', 'm');
7381 option_conflict('l', 's');
7383 option_conflict('V', 's');
7387 } else if (argc != 1)
7394 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7395 "sendfd unveil", NULL) == -1)
7398 cwd = getcwd(NULL, 0);
7400 error = got_error_from_errno("getcwd");
7404 error = got_repo_pack_fds_open(&pack_fds);
7408 if (repo_path == NULL) {
7409 error = got_worktree_open(&worktree, cwd);
7410 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7416 strdup(got_worktree_get_repo_path(worktree));
7417 if (repo_path == NULL)
7418 error = got_error_from_errno("strdup");
7422 repo_path = strdup(cwd);
7423 if (repo_path == NULL) {
7424 error = got_error_from_errno("strdup");
7430 if (do_list || verify_tags) {
7431 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7434 error = get_allowed_signers(&allowed_signers, repo, worktree);
7437 error = get_revoked_signers(&revoked_signers, repo, worktree);
7441 /* Release work tree lock. */
7442 got_worktree_close(worktree);
7447 * Remove "cpath" promise unless needed for signature tmpfile
7451 got_sigs_apply_unveil();
7454 if (pledge("stdio rpath wpath flock proc exec sendfd "
7455 "unveil", NULL) == -1)
7459 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7462 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7463 revoked_signers, verbosity);
7465 error = get_gitconfig_path(&gitconfig_path);
7468 error = got_repo_open(&repo, repo_path, gitconfig_path,
7473 error = get_author(&tagger, repo, worktree);
7476 if (signer_id == NULL) {
7477 error = get_signer_id(&signer_id, repo, worktree);
7482 /* Release work tree lock. */
7483 got_worktree_close(worktree);
7489 error = got_sigs_apply_unveil();
7493 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7498 if (commit_id_arg == NULL) {
7499 struct got_reference *head_ref;
7500 struct got_object_id *commit_id;
7501 error = got_ref_open(&head_ref, repo,
7502 worktree ? got_worktree_get_head_ref_name(worktree)
7506 error = got_ref_resolve(&commit_id, repo, head_ref);
7507 got_ref_close(head_ref);
7510 error = got_object_id_str(&commit_id_str, commit_id);
7516 error = add_tag(repo, tagger, tag_name,
7517 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7518 signer_id, verbosity);
7522 const struct got_error *close_err = got_repo_close(repo);
7527 got_worktree_close(worktree);
7529 const struct got_error *pack_err =
7530 got_repo_pack_fds_close(pack_fds);
7536 free(gitconfig_path);
7537 free(commit_id_str);
7539 free(allowed_signers);
7540 free(revoked_signers);
7548 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7553 static const struct got_error *
7554 add_progress(void *arg, unsigned char status, const char *path)
7556 while (path[0] == '/')
7558 printf("%c %s\n", status, path);
7562 static const struct got_error *
7563 cmd_add(int argc, char *argv[])
7565 const struct got_error *error = NULL;
7566 struct got_repository *repo = NULL;
7567 struct got_worktree *worktree = NULL;
7569 struct got_pathlist_head paths;
7570 struct got_pathlist_entry *pe;
7571 int ch, can_recurse = 0, no_ignores = 0;
7572 int *pack_fds = NULL;
7576 while ((ch = getopt(argc, argv, "IR")) != -1) {
7594 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7601 cwd = getcwd(NULL, 0);
7603 error = got_error_from_errno("getcwd");
7607 error = got_repo_pack_fds_open(&pack_fds);
7611 error = got_worktree_open(&worktree, cwd);
7613 if (error->code == GOT_ERR_NOT_WORKTREE)
7614 error = wrap_not_worktree_error(error, "add", cwd);
7618 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7623 error = apply_unveil(got_repo_get_path(repo), 1,
7624 got_worktree_get_root_path(worktree));
7628 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7635 TAILQ_FOREACH(pe, &paths, entry) {
7636 if (asprintf(&ondisk_path, "%s/%s",
7637 got_worktree_get_root_path(worktree),
7639 error = got_error_from_errno("asprintf");
7642 if (lstat(ondisk_path, &sb) == -1) {
7643 if (errno == ENOENT) {
7647 error = got_error_from_errno2("lstat",
7653 if (S_ISDIR(sb.st_mode)) {
7654 error = got_error_msg(GOT_ERR_BAD_PATH,
7655 "adding directories requires -R option");
7661 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7662 NULL, repo, no_ignores);
7665 const struct got_error *close_err = got_repo_close(repo);
7670 got_worktree_close(worktree);
7672 const struct got_error *pack_err =
7673 got_repo_pack_fds_close(pack_fds);
7677 TAILQ_FOREACH(pe, &paths, entry)
7678 free((char *)pe->path);
7679 got_pathlist_free(&paths);
7687 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7688 "path ...\n", getprogname());
7692 static const struct got_error *
7693 print_remove_status(void *arg, unsigned char status,
7694 unsigned char staged_status, const char *path)
7696 while (path[0] == '/')
7698 if (status == GOT_STATUS_NONEXISTENT)
7700 if (status == staged_status && (status == GOT_STATUS_DELETE))
7701 status = GOT_STATUS_NO_CHANGE;
7702 printf("%c%c %s\n", status, staged_status, path);
7706 static const struct got_error *
7707 cmd_remove(int argc, char *argv[])
7709 const struct got_error *error = NULL;
7710 struct got_worktree *worktree = NULL;
7711 struct got_repository *repo = NULL;
7712 const char *status_codes = NULL;
7714 struct got_pathlist_head paths;
7715 struct got_pathlist_entry *pe;
7716 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7717 int ignore_missing_paths = 0;
7718 int *pack_fds = NULL;
7722 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7725 delete_local_mods = 1;
7726 ignore_missing_paths = 1;
7735 for (i = 0; i < strlen(optarg); i++) {
7736 switch (optarg[i]) {
7737 case GOT_STATUS_MODIFY:
7738 delete_local_mods = 1;
7740 case GOT_STATUS_MISSING:
7741 ignore_missing_paths = 1;
7744 errx(1, "invalid status code '%c'",
7748 status_codes = optarg;
7760 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7767 cwd = getcwd(NULL, 0);
7769 error = got_error_from_errno("getcwd");
7773 error = got_repo_pack_fds_open(&pack_fds);
7777 error = got_worktree_open(&worktree, cwd);
7779 if (error->code == GOT_ERR_NOT_WORKTREE)
7780 error = wrap_not_worktree_error(error, "remove", cwd);
7784 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7789 error = apply_unveil(got_repo_get_path(repo), 1,
7790 got_worktree_get_root_path(worktree));
7794 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7801 TAILQ_FOREACH(pe, &paths, entry) {
7802 if (asprintf(&ondisk_path, "%s/%s",
7803 got_worktree_get_root_path(worktree),
7805 error = got_error_from_errno("asprintf");
7808 if (lstat(ondisk_path, &sb) == -1) {
7809 if (errno == ENOENT) {
7813 error = got_error_from_errno2("lstat",
7819 if (S_ISDIR(sb.st_mode)) {
7820 error = got_error_msg(GOT_ERR_BAD_PATH,
7821 "removing directories requires -R option");
7827 error = got_worktree_schedule_delete(worktree, &paths,
7828 delete_local_mods, status_codes, print_remove_status, NULL,
7829 repo, keep_on_disk, ignore_missing_paths);
7832 const struct got_error *close_err = got_repo_close(repo);
7837 got_worktree_close(worktree);
7839 const struct got_error *pack_err =
7840 got_repo_pack_fds_close(pack_fds);
7844 TAILQ_FOREACH(pe, &paths, entry)
7845 free((char *)pe->path);
7846 got_pathlist_free(&paths);
7854 fprintf(stderr, "usage: %s patch [-c commit] [-n] [-p strip-count] "
7855 "[-R] [patchfile]\n", getprogname());
7859 static const struct got_error *
7860 patch_from_stdin(int *patchfd)
7862 const struct got_error *err = NULL;
7864 char *path, buf[BUFSIZ];
7865 sig_t sighup, sigint, sigquit;
7867 err = got_opentemp_named_fd(&path, patchfd,
7868 GOT_TMPDIR_STR "/got-patch");
7874 sighup = signal(SIGHUP, SIG_DFL);
7875 sigint = signal(SIGINT, SIG_DFL);
7876 sigquit = signal(SIGQUIT, SIG_DFL);
7879 r = read(0, buf, sizeof(buf));
7881 err = got_error_from_errno("read");
7886 if (write(*patchfd, buf, r) == -1) {
7887 err = got_error_from_errno("write");
7892 signal(SIGHUP, sighup);
7893 signal(SIGINT, sigint);
7894 signal(SIGQUIT, sigquit);
7896 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7897 err = got_error_from_errno("lseek");
7907 static const struct got_error *
7908 patch_progress(void *arg, const char *old, const char *new,
7909 unsigned char status, const struct got_error *error, int old_from,
7910 int old_lines, int new_from, int new_lines, int offset,
7911 int ws_mangled, const struct got_error *hunk_err)
7913 const char *path = new == NULL ? old : new;
7915 while (*path == '/')
7919 printf("%c %s\n", status, path);
7922 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7924 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7925 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7926 old_lines, new_from, new_lines);
7927 if (hunk_err != NULL)
7928 printf("%s\n", hunk_err->msg);
7929 else if (offset != 0)
7930 printf("applied with offset %d\n", offset);
7932 printf("hunk contains mangled whitespace\n");
7938 static const struct got_error *
7939 cmd_patch(int argc, char *argv[])
7941 const struct got_error *error = NULL, *close_error = NULL;
7942 struct got_worktree *worktree = NULL;
7943 struct got_repository *repo = NULL;
7944 struct got_reflist_head refs;
7945 struct got_object_id *commit_id = NULL;
7946 const char *commit_id_str = NULL;
7950 int ch, nop = 0, strip = -1, reverse = 0;
7952 int *pack_fds = NULL;
7957 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
7958 "unveil", NULL) == -1)
7962 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
7965 commit_id_str = optarg;
7971 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7973 errx(1, "pathname strip count is %s: %s",
7989 error = patch_from_stdin(&patchfd);
7992 } else if (argc == 1) {
7993 patchfd = open(argv[0], O_RDONLY);
7994 if (patchfd == -1) {
7995 error = got_error_from_errno2("open", argv[0]);
7998 if (fstat(patchfd, &sb) == -1) {
7999 error = got_error_from_errno2("fstat", argv[0]);
8002 if (!S_ISREG(sb.st_mode)) {
8003 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8009 if ((cwd = getcwd(NULL, 0)) == NULL) {
8010 error = got_error_from_errno("getcwd");
8014 error = got_repo_pack_fds_open(&pack_fds);
8018 error = got_worktree_open(&worktree, cwd);
8022 const char *repo_path = got_worktree_get_repo_path(worktree);
8023 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8027 error = apply_unveil(got_repo_get_path(repo), 0,
8028 got_worktree_get_root_path(worktree));
8032 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8036 if (commit_id_str != NULL) {
8037 error = got_repo_match_object_id(&commit_id, NULL,
8038 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8043 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8044 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8047 got_ref_list_free(&refs);
8050 close_error = got_repo_close(repo);
8052 error = close_error;
8054 if (worktree != NULL) {
8055 close_error = got_worktree_close(worktree);
8057 error = close_error;
8060 const struct got_error *pack_err =
8061 got_repo_pack_fds_close(pack_fds);
8072 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8073 "path ...\n", getprogname());
8077 static const struct got_error *
8078 revert_progress(void *arg, unsigned char status, const char *path)
8080 if (status == GOT_STATUS_UNVERSIONED)
8083 while (path[0] == '/')
8085 printf("%c %s\n", status, path);
8089 struct choose_patch_arg {
8090 FILE *patch_script_file;
8094 static const struct got_error *
8095 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8096 int nchanges, const char *action)
8098 const struct got_error *err;
8100 size_t linesize = 0;
8104 case GOT_STATUS_ADD:
8105 printf("A %s\n%s this addition? [y/n] ", path, action);
8107 case GOT_STATUS_DELETE:
8108 printf("D %s\n%s this deletion? [y/n] ", path, action);
8110 case GOT_STATUS_MODIFY:
8111 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8112 return got_error_from_errno("fseek");
8113 printf(GOT_COMMIT_SEP_STR);
8114 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8116 if (linelen == -1 && ferror(patch_file)) {
8117 err = got_error_from_errno("getline");
8122 printf(GOT_COMMIT_SEP_STR);
8123 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8124 path, n, nchanges, action);
8127 return got_error_path(path, GOT_ERR_FILE_STATUS);
8133 static const struct got_error *
8134 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8135 FILE *patch_file, int n, int nchanges)
8137 const struct got_error *err = NULL;
8139 size_t linesize = 0;
8142 struct choose_patch_arg *a = arg;
8144 *choice = GOT_PATCH_CHOICE_NONE;
8146 if (a->patch_script_file) {
8148 err = show_change(status, path, patch_file, n, nchanges,
8152 linelen = getline(&line, &linesize, a->patch_script_file);
8153 if (linelen == -1) {
8154 if (ferror(a->patch_script_file))
8155 return got_error_from_errno("getline");
8158 nl = strchr(line, '\n');
8161 if (strcmp(line, "y") == 0) {
8162 *choice = GOT_PATCH_CHOICE_YES;
8164 } else if (strcmp(line, "n") == 0) {
8165 *choice = GOT_PATCH_CHOICE_NO;
8167 } else if (strcmp(line, "q") == 0 &&
8168 status == GOT_STATUS_MODIFY) {
8169 *choice = GOT_PATCH_CHOICE_QUIT;
8172 printf("invalid response '%s'\n", line);
8177 while (resp != 'y' && resp != 'n' && resp != 'q') {
8178 err = show_change(status, path, patch_file, n, nchanges,
8185 if (status == GOT_STATUS_MODIFY) {
8186 if (resp != 'y' && resp != 'n' && resp != 'q') {
8187 printf("invalid response '%c'\n", resp);
8190 } else if (resp != 'y' && resp != 'n') {
8191 printf("invalid response '%c'\n", resp);
8197 *choice = GOT_PATCH_CHOICE_YES;
8198 else if (resp == 'n')
8199 *choice = GOT_PATCH_CHOICE_NO;
8200 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8201 *choice = GOT_PATCH_CHOICE_QUIT;
8206 static const struct got_error *
8207 cmd_revert(int argc, char *argv[])
8209 const struct got_error *error = NULL;
8210 struct got_worktree *worktree = NULL;
8211 struct got_repository *repo = NULL;
8212 char *cwd = NULL, *path = NULL;
8213 struct got_pathlist_head paths;
8214 struct got_pathlist_entry *pe;
8215 int ch, can_recurse = 0, pflag = 0;
8216 FILE *patch_script_file = NULL;
8217 const char *patch_script_path = NULL;
8218 struct choose_patch_arg cpa;
8219 int *pack_fds = NULL;
8223 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8229 patch_script_path = optarg;
8244 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8245 "unveil", NULL) == -1)
8250 if (patch_script_path && !pflag)
8251 errx(1, "-F option can only be used together with -p option");
8253 cwd = getcwd(NULL, 0);
8255 error = got_error_from_errno("getcwd");
8259 error = got_repo_pack_fds_open(&pack_fds);
8263 error = got_worktree_open(&worktree, cwd);
8265 if (error->code == GOT_ERR_NOT_WORKTREE)
8266 error = wrap_not_worktree_error(error, "revert", cwd);
8270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8275 if (patch_script_path) {
8276 patch_script_file = fopen(patch_script_path, "re");
8277 if (patch_script_file == NULL) {
8278 error = got_error_from_errno2("fopen",
8283 error = apply_unveil(got_repo_get_path(repo), 1,
8284 got_worktree_get_root_path(worktree));
8288 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8295 TAILQ_FOREACH(pe, &paths, entry) {
8296 if (asprintf(&ondisk_path, "%s/%s",
8297 got_worktree_get_root_path(worktree),
8299 error = got_error_from_errno("asprintf");
8302 if (lstat(ondisk_path, &sb) == -1) {
8303 if (errno == ENOENT) {
8307 error = got_error_from_errno2("lstat",
8313 if (S_ISDIR(sb.st_mode)) {
8314 error = got_error_msg(GOT_ERR_BAD_PATH,
8315 "reverting directories requires -R option");
8321 cpa.patch_script_file = patch_script_file;
8322 cpa.action = "revert";
8323 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8324 pflag ? choose_patch : NULL, &cpa, repo);
8326 if (patch_script_file && fclose(patch_script_file) == EOF &&
8328 error = got_error_from_errno2("fclose", patch_script_path);
8330 const struct got_error *close_err = got_repo_close(repo);
8335 got_worktree_close(worktree);
8337 const struct got_error *pack_err =
8338 got_repo_pack_fds_close(pack_fds);
8350 fprintf(stderr, "usage: %s commit [-A author] [-F path] [-m msg] "
8351 "[-N] [-S] [path ...]\n", getprogname());
8355 struct collect_commit_logmsg_arg {
8356 const char *cmdline_log;
8357 const char *prepared_log;
8358 int non_interactive;
8360 const char *worktree_path;
8361 const char *branch_name;
8362 const char *repo_path;
8367 static const struct got_error *
8368 read_prepared_logmsg(char **logmsg, const char *path)
8370 const struct got_error *err = NULL;
8376 memset(&sb, 0, sizeof(sb));
8378 f = fopen(path, "re");
8380 return got_error_from_errno2("fopen", path);
8382 if (fstat(fileno(f), &sb) == -1) {
8383 err = got_error_from_errno2("fstat", path);
8386 if (sb.st_size == 0) {
8387 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8391 *logmsg = malloc(sb.st_size + 1);
8392 if (*logmsg == NULL) {
8393 err = got_error_from_errno("malloc");
8397 r = fread(*logmsg, 1, sb.st_size, f);
8398 if (r != sb.st_size) {
8400 err = got_error_from_errno2("fread", path);
8402 err = got_error(GOT_ERR_IO);
8405 (*logmsg)[sb.st_size] = '\0';
8407 if (fclose(f) == EOF && err == NULL)
8408 err = got_error_from_errno2("fclose", path);
8417 static const struct got_error *
8418 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8421 char *initial_content = NULL;
8422 struct got_pathlist_entry *pe;
8423 const struct got_error *err = NULL;
8424 char *template = NULL;
8425 struct collect_commit_logmsg_arg *a = arg;
8426 int initial_content_len;
8430 /* if a message was specified on the command line, just use it */
8431 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8432 len = strlen(a->cmdline_log) + 1;
8433 *logmsg = malloc(len + 1);
8434 if (*logmsg == NULL)
8435 return got_error_from_errno("malloc");
8436 strlcpy(*logmsg, a->cmdline_log, len);
8438 } else if (a->prepared_log != NULL && a->non_interactive)
8439 return read_prepared_logmsg(logmsg, a->prepared_log);
8441 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8442 return got_error_from_errno("asprintf");
8444 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8448 if (a->prepared_log) {
8450 err = read_prepared_logmsg(&msg, a->prepared_log);
8453 if (write(fd, msg, strlen(msg)) == -1) {
8454 err = got_error_from_errno2("write", a->logmsg_path);
8461 initial_content_len = asprintf(&initial_content,
8462 "\n# changes to be committed on branch %s:\n",
8464 if (initial_content_len == -1) {
8465 err = got_error_from_errno("asprintf");
8469 if (write(fd, initial_content, initial_content_len) == -1) {
8470 err = got_error_from_errno2("write", a->logmsg_path);
8474 TAILQ_FOREACH(pe, commitable_paths, entry) {
8475 struct got_commitable *ct = pe->data;
8476 dprintf(fd, "# %c %s\n",
8477 got_commitable_get_status(ct),
8478 got_commitable_get_path(ct));
8481 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8482 initial_content_len, a->prepared_log ? 0 : 1);
8484 free(initial_content);
8487 if (fd != -1 && close(fd) == -1 && err == NULL)
8488 err = got_error_from_errno2("close", a->logmsg_path);
8490 /* Editor is done; we can now apply unveil(2) */
8492 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8500 static const struct got_error *
8501 cmd_commit(int argc, char *argv[])
8503 const struct got_error *error = NULL;
8504 struct got_worktree *worktree = NULL;
8505 struct got_repository *repo = NULL;
8506 char *cwd = NULL, *id_str = NULL;
8507 struct got_object_id *id = NULL;
8508 const char *logmsg = NULL;
8509 char *prepared_logmsg = NULL;
8510 struct collect_commit_logmsg_arg cl_arg;
8511 const char *author = NULL;
8512 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8513 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8514 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8515 struct got_pathlist_head paths;
8516 int *pack_fds = NULL;
8519 cl_arg.logmsg_path = NULL;
8521 while ((ch = getopt(argc, argv, "A:F:m:NS")) != -1) {
8525 error = valid_author(author);
8531 option_conflict('F', 'm');
8532 prepared_logmsg = realpath(optarg, NULL);
8533 if (prepared_logmsg == NULL)
8534 return got_error_from_errno2("realpath",
8538 if (prepared_logmsg)
8539 option_conflict('m', 'F');
8543 non_interactive = 1;
8546 allow_bad_symlinks = 1;
8558 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8559 "unveil", NULL) == -1)
8562 cwd = getcwd(NULL, 0);
8564 error = got_error_from_errno("getcwd");
8568 error = got_repo_pack_fds_open(&pack_fds);
8572 error = got_worktree_open(&worktree, cwd);
8574 if (error->code == GOT_ERR_NOT_WORKTREE)
8575 error = wrap_not_worktree_error(error, "commit", cwd);
8579 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8582 if (rebase_in_progress) {
8583 error = got_error(GOT_ERR_REBASING);
8587 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8592 error = get_gitconfig_path(&gitconfig_path);
8595 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8596 gitconfig_path, pack_fds);
8600 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8603 if (merge_in_progress) {
8604 error = got_error(GOT_ERR_MERGE_BUSY);
8608 error = get_author(&committer, repo, worktree);
8612 if (author != NULL && !strcmp(committer, author)) {
8613 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8621 * unveil(2) traverses exec(2); if an editor is used we have
8622 * to apply unveil after the log message has been written.
8624 if (logmsg == NULL || strlen(logmsg) == 0)
8625 error = get_editor(&editor);
8627 error = apply_unveil(got_repo_get_path(repo), 0,
8628 got_worktree_get_root_path(worktree));
8632 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8636 cl_arg.editor = editor;
8637 cl_arg.cmdline_log = logmsg;
8638 cl_arg.prepared_log = prepared_logmsg;
8639 cl_arg.non_interactive = non_interactive;
8640 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8641 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8642 if (!histedit_in_progress) {
8643 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8644 error = got_error(GOT_ERR_COMMIT_BRANCH);
8647 cl_arg.branch_name += 11;
8649 cl_arg.repo_path = got_repo_get_path(repo);
8650 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8651 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8652 print_status, NULL, repo);
8654 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8655 cl_arg.logmsg_path != NULL)
8656 preserve_logmsg = 1;
8660 error = got_object_id_str(&id_str, id);
8663 printf("Created commit %s\n", id_str);
8665 if (preserve_logmsg) {
8666 fprintf(stderr, "%s: log message preserved in %s\n",
8667 getprogname(), cl_arg.logmsg_path);
8668 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8670 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8671 free(cl_arg.logmsg_path);
8673 const struct got_error *close_err = got_repo_close(repo);
8678 got_worktree_close(worktree);
8680 const struct got_error *pack_err =
8681 got_repo_pack_fds_close(pack_fds);
8687 free(gitconfig_path);
8690 free(prepared_logmsg);
8697 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8698 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8699 "[remote-repository]\n", getprogname());
8704 print_load_info(int print_colored, int print_found, int print_trees,
8705 int ncolored, int nfound, int ntrees)
8707 if (print_colored) {
8708 printf("%d commit%s colored", ncolored,
8709 ncolored == 1 ? "" : "s");
8712 printf("%s%d object%s found",
8713 ncolored > 0 ? "; " : "",
8714 nfound, nfound == 1 ? "" : "s");
8717 printf("; %d tree%s scanned", ntrees,
8718 ntrees == 1 ? "" : "s");
8722 struct got_send_progress_arg {
8723 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8730 int last_nobj_total;
8734 int printed_something;
8736 struct got_pathlist_head *delete_branches;
8739 static const struct got_error *
8740 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8741 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8742 int nobj_written, off_t bytes_sent, const char *refname, int success)
8744 struct got_send_progress_arg *a = arg;
8745 char scaled_packsize[FMT_SCALED_STRSIZE];
8746 char scaled_sent[FMT_SCALED_STRSIZE];
8747 int p_deltify = 0, p_written = 0, p_sent = 0;
8748 int print_colored = 0, print_found = 0, print_trees = 0;
8749 int print_searching = 0, print_total = 0;
8750 int print_deltify = 0, print_written = 0, print_sent = 0;
8752 if (a->verbosity < 0)
8756 const char *status = success ? "accepted" : "rejected";
8759 struct got_pathlist_entry *pe;
8760 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8761 const char *branchname = pe->path;
8762 if (got_path_cmp(branchname, refname,
8763 strlen(branchname), strlen(refname)) == 0) {
8765 a->sent_something = 1;
8771 if (a->printed_something)
8773 printf("Server has %s %s", status, refname);
8774 a->printed_something = 1;
8778 if (a->last_ncolored != ncolored) {
8780 a->last_ncolored = ncolored;
8783 if (a->last_nfound != nfound) {
8786 a->last_nfound = nfound;
8789 if (a->last_ntrees != ntrees) {
8793 a->last_ntrees = ntrees;
8796 if ((print_colored || print_found || print_trees) &&
8799 print_load_info(print_colored, print_found, print_trees,
8800 ncolored, nfound, ntrees);
8801 a->printed_something = 1;
8804 } else if (!a->loading_done) {
8806 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8808 a->loading_done = 1;
8811 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8812 return got_error_from_errno("fmt_scaled");
8813 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8814 return got_error_from_errno("fmt_scaled");
8816 if (a->last_ncommits != ncommits) {
8817 print_searching = 1;
8818 a->last_ncommits = ncommits;
8821 if (a->last_nobj_total != nobj_total) {
8822 print_searching = 1;
8824 a->last_nobj_total = nobj_total;
8827 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8828 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8829 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8830 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8831 return got_error(GOT_ERR_NO_SPACE);
8834 if (nobj_deltify > 0 || nobj_written > 0) {
8835 if (nobj_deltify > 0) {
8836 p_deltify = (nobj_deltify * 100) / nobj_total;
8837 if (p_deltify != a->last_p_deltify) {
8838 a->last_p_deltify = p_deltify;
8839 print_searching = 1;
8844 if (nobj_written > 0) {
8845 p_written = (nobj_written * 100) / nobj_total;
8846 if (p_written != a->last_p_written) {
8847 a->last_p_written = p_written;
8848 print_searching = 1;
8856 if (bytes_sent > 0) {
8857 p_sent = (bytes_sent * 100) / packfile_size;
8858 if (p_sent != a->last_p_sent) {
8859 a->last_p_sent = p_sent;
8860 print_searching = 1;
8866 a->sent_something = 1;
8869 if (print_searching || print_total || print_deltify || print_written ||
8872 if (print_searching)
8873 printf("packing %d reference%s", ncommits,
8874 ncommits == 1 ? "" : "s");
8876 printf("; %d object%s", nobj_total,
8877 nobj_total == 1 ? "" : "s");
8879 printf("; deltify: %d%%", p_deltify);
8881 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8882 scaled_packsize, p_sent);
8883 else if (print_written)
8884 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8885 scaled_packsize, p_written);
8886 if (print_searching || print_total || print_deltify ||
8887 print_written || print_sent) {
8888 a->printed_something = 1;
8894 static const struct got_error *
8895 cmd_send(int argc, char *argv[])
8897 const struct got_error *error = NULL;
8898 char *cwd = NULL, *repo_path = NULL;
8899 const char *remote_name;
8900 char *proto = NULL, *host = NULL, *port = NULL;
8901 char *repo_name = NULL, *server_path = NULL;
8902 const struct got_remote_repo *remotes, *remote = NULL;
8903 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8904 struct got_repository *repo = NULL;
8905 struct got_worktree *worktree = NULL;
8906 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8907 struct got_pathlist_head branches;
8908 struct got_pathlist_head tags;
8909 struct got_reflist_head all_branches;
8910 struct got_reflist_head all_tags;
8911 struct got_pathlist_head delete_args;
8912 struct got_pathlist_head delete_branches;
8913 struct got_reflist_entry *re;
8914 struct got_pathlist_entry *pe;
8915 int i, ch, sendfd = -1, sendstatus;
8917 struct got_send_progress_arg spa;
8918 int verbosity = 0, overwrite_refs = 0;
8919 int send_all_branches = 0, send_all_tags = 0;
8920 struct got_reference *ref = NULL;
8921 int *pack_fds = NULL;
8923 TAILQ_INIT(&branches);
8925 TAILQ_INIT(&all_branches);
8926 TAILQ_INIT(&all_tags);
8927 TAILQ_INIT(&delete_args);
8928 TAILQ_INIT(&delete_branches);
8930 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8933 send_all_branches = 1;
8936 error = got_pathlist_append(&branches, optarg, NULL);
8942 error = got_pathlist_append(&delete_args, optarg, NULL);
8950 repo_path = realpath(optarg, NULL);
8951 if (repo_path == NULL)
8952 return got_error_from_errno2("realpath",
8954 got_path_strip_trailing_slashes(repo_path);
8957 error = got_pathlist_append(&tags, optarg, NULL);
8968 else if (verbosity < 3)
8982 if (send_all_branches && !TAILQ_EMPTY(&branches))
8983 option_conflict('a', 'b');
8984 if (send_all_tags && !TAILQ_EMPTY(&tags))
8985 option_conflict('T', 't');
8989 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8991 remote_name = argv[0];
8995 cwd = getcwd(NULL, 0);
8997 error = got_error_from_errno("getcwd");
9001 error = got_repo_pack_fds_open(&pack_fds);
9005 if (repo_path == NULL) {
9006 error = got_worktree_open(&worktree, cwd);
9007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9013 strdup(got_worktree_get_repo_path(worktree));
9014 if (repo_path == NULL)
9015 error = got_error_from_errno("strdup");
9019 repo_path = strdup(cwd);
9020 if (repo_path == NULL) {
9021 error = got_error_from_errno("strdup");
9027 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9032 worktree_conf = got_worktree_get_gotconfig(worktree);
9033 if (worktree_conf) {
9034 got_gotconfig_get_remotes(&nremotes, &remotes,
9036 for (i = 0; i < nremotes; i++) {
9037 if (strcmp(remotes[i].name, remote_name) == 0) {
9038 remote = &remotes[i];
9044 if (remote == NULL) {
9045 repo_conf = got_repo_get_gotconfig(repo);
9047 got_gotconfig_get_remotes(&nremotes, &remotes,
9049 for (i = 0; i < nremotes; i++) {
9050 if (strcmp(remotes[i].name, remote_name) == 0) {
9051 remote = &remotes[i];
9057 if (remote == NULL) {
9058 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9059 for (i = 0; i < nremotes; i++) {
9060 if (strcmp(remotes[i].name, remote_name) == 0) {
9061 remote = &remotes[i];
9066 if (remote == NULL) {
9067 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9071 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9072 &repo_name, remote->send_url);
9076 if (strcmp(proto, "git") == 0) {
9078 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9079 "sendfd dns inet unveil", NULL) == -1)
9082 } else if (strcmp(proto, "git+ssh") == 0 ||
9083 strcmp(proto, "ssh") == 0) {
9085 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9086 "sendfd unveil", NULL) == -1)
9089 } else if (strcmp(proto, "http") == 0 ||
9090 strcmp(proto, "git+http") == 0) {
9091 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9094 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9098 error = got_dial_apply_unveil(proto);
9102 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9106 if (send_all_branches) {
9107 error = got_ref_list(&all_branches, repo, "refs/heads",
9108 got_ref_cmp_by_name, NULL);
9111 TAILQ_FOREACH(re, &all_branches, entry) {
9112 const char *branchname = got_ref_get_name(re->ref);
9113 error = got_pathlist_append(&branches,
9119 } else if (nbranches == 0) {
9120 for (i = 0; i < remote->nsend_branches; i++) {
9121 got_pathlist_append(&branches,
9122 remote->send_branches[i], NULL);
9126 if (send_all_tags) {
9127 error = got_ref_list(&all_tags, repo, "refs/tags",
9128 got_ref_cmp_by_name, NULL);
9131 TAILQ_FOREACH(re, &all_tags, entry) {
9132 const char *tagname = got_ref_get_name(re->ref);
9133 error = got_pathlist_append(&tags,
9142 * To prevent accidents only branches in refs/heads/ can be deleted
9143 * with 'got send -d'.
9144 * Deleting anything else requires local repository access or Git.
9146 TAILQ_FOREACH(pe, &delete_args, entry) {
9147 const char *branchname = pe->path;
9149 struct got_pathlist_entry *new;
9150 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9151 s = strdup(branchname);
9153 error = got_error_from_errno("strdup");
9157 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9158 error = got_error_from_errno("asprintf");
9162 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9163 if (error || new == NULL /* duplicate */)
9170 if (nbranches == 0 && ndelete_branches == 0) {
9171 struct got_reference *head_ref;
9173 error = got_ref_open(&head_ref, repo,
9174 got_worktree_get_head_ref_name(worktree), 0);
9176 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9179 if (got_ref_is_symbolic(head_ref)) {
9180 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9181 got_ref_close(head_ref);
9186 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9194 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9195 port ? ":" : "", port ? port : "");
9197 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9198 server_path, verbosity);
9202 memset(&spa, 0, sizeof(spa));
9203 spa.last_scaled_packsize[0] = '\0';
9204 spa.last_p_deltify = -1;
9205 spa.last_p_written = -1;
9206 spa.verbosity = verbosity;
9207 spa.delete_branches = &delete_branches;
9208 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9209 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9210 check_cancelled, NULL);
9211 if (spa.printed_something)
9215 if (!spa.sent_something && verbosity >= 0)
9216 printf("Already up-to-date\n");
9219 if (kill(sendpid, SIGTERM) == -1)
9220 error = got_error_from_errno("kill");
9221 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9222 error = got_error_from_errno("waitpid");
9224 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9225 error = got_error_from_errno("close");
9227 const struct got_error *close_err = got_repo_close(repo);
9232 got_worktree_close(worktree);
9234 const struct got_error *pack_err =
9235 got_repo_pack_fds_close(pack_fds);
9241 got_pathlist_free(&branches);
9242 got_pathlist_free(&tags);
9243 got_ref_list_free(&all_branches);
9244 got_ref_list_free(&all_tags);
9245 got_pathlist_free(&delete_args);
9246 TAILQ_FOREACH(pe, &delete_branches, entry)
9247 free((char *)pe->path);
9248 got_pathlist_free(&delete_branches);
9260 usage_cherrypick(void)
9262 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9266 static const struct got_error *
9267 cmd_cherrypick(int argc, char *argv[])
9269 const struct got_error *error = NULL;
9270 struct got_worktree *worktree = NULL;
9271 struct got_repository *repo = NULL;
9272 char *cwd = NULL, *commit_id_str = NULL;
9273 struct got_object_id *commit_id = NULL;
9274 struct got_commit_object *commit = NULL;
9275 struct got_object_qid *pid;
9277 struct got_update_progress_arg upa;
9278 int *pack_fds = NULL;
9280 while ((ch = getopt(argc, argv, "")) != -1) {
9292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9293 "unveil", NULL) == -1)
9299 cwd = getcwd(NULL, 0);
9301 error = got_error_from_errno("getcwd");
9305 error = got_repo_pack_fds_open(&pack_fds);
9309 error = got_worktree_open(&worktree, cwd);
9311 if (error->code == GOT_ERR_NOT_WORKTREE)
9312 error = wrap_not_worktree_error(error, "cherrypick",
9317 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9322 error = apply_unveil(got_repo_get_path(repo), 0,
9323 got_worktree_get_root_path(worktree));
9327 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9328 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9331 error = got_object_id_str(&commit_id_str, commit_id);
9335 error = got_object_open_as_commit(&commit, repo, commit_id);
9338 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9339 memset(&upa, 0, sizeof(upa));
9340 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9341 commit_id, repo, update_progress, &upa, check_cancelled,
9346 if (upa.did_something)
9347 printf("Merged commit %s\n", commit_id_str);
9348 print_merge_progress_stats(&upa);
9351 got_object_commit_close(commit);
9352 free(commit_id_str);
9354 got_worktree_close(worktree);
9356 const struct got_error *close_err = got_repo_close(repo);
9361 const struct got_error *pack_err =
9362 got_repo_pack_fds_close(pack_fds);
9373 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9377 static const struct got_error *
9378 cmd_backout(int argc, char *argv[])
9380 const struct got_error *error = NULL;
9381 struct got_worktree *worktree = NULL;
9382 struct got_repository *repo = NULL;
9383 char *cwd = NULL, *commit_id_str = NULL;
9384 struct got_object_id *commit_id = NULL;
9385 struct got_commit_object *commit = NULL;
9386 struct got_object_qid *pid;
9388 struct got_update_progress_arg upa;
9389 int *pack_fds = NULL;
9391 while ((ch = getopt(argc, argv, "")) != -1) {
9403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9404 "unveil", NULL) == -1)
9410 cwd = getcwd(NULL, 0);
9412 error = got_error_from_errno("getcwd");
9416 error = got_repo_pack_fds_open(&pack_fds);
9420 error = got_worktree_open(&worktree, cwd);
9422 if (error->code == GOT_ERR_NOT_WORKTREE)
9423 error = wrap_not_worktree_error(error, "backout", cwd);
9427 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9432 error = apply_unveil(got_repo_get_path(repo), 0,
9433 got_worktree_get_root_path(worktree));
9437 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9438 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9441 error = got_object_id_str(&commit_id_str, commit_id);
9445 error = got_object_open_as_commit(&commit, repo, commit_id);
9448 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9450 error = got_error(GOT_ERR_ROOT_COMMIT);
9454 memset(&upa, 0, sizeof(upa));
9455 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9456 repo, update_progress, &upa, check_cancelled, NULL);
9460 if (upa.did_something)
9461 printf("Backed out commit %s\n", commit_id_str);
9462 print_merge_progress_stats(&upa);
9465 got_object_commit_close(commit);
9466 free(commit_id_str);
9468 got_worktree_close(worktree);
9470 const struct got_error *close_err = got_repo_close(repo);
9475 const struct got_error *pack_err =
9476 got_repo_pack_fds_close(pack_fds);
9486 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9492 trim_logmsg(char *logmsg, int limit)
9497 len = strlen(logmsg);
9501 nl = strchr(logmsg, '\n');
9506 static const struct got_error *
9507 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9509 const struct got_error *err;
9510 char *logmsg0 = NULL;
9513 err = got_object_commit_get_logmsg(&logmsg0, commit);
9518 while (isspace((unsigned char)s[0]))
9521 *logmsg = strdup(s);
9522 if (*logmsg == NULL) {
9523 err = got_error_from_errno("strdup");
9527 trim_logmsg(*logmsg, limit);
9533 static const struct got_error *
9534 show_rebase_merge_conflict(struct got_object_id *id,
9535 struct got_repository *repo)
9537 const struct got_error *err;
9538 struct got_commit_object *commit = NULL;
9539 char *id_str = NULL, *logmsg = NULL;
9541 err = got_object_open_as_commit(&commit, repo, id);
9545 err = got_object_id_str(&id_str, id);
9551 err = get_short_logmsg(&logmsg, 42, commit);
9555 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9558 got_object_commit_close(commit);
9563 static const struct got_error *
9564 show_rebase_progress(struct got_commit_object *commit,
9565 struct got_object_id *old_id, struct got_object_id *new_id)
9567 const struct got_error *err;
9568 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9570 err = got_object_id_str(&old_id_str, old_id);
9575 err = got_object_id_str(&new_id_str, new_id);
9580 old_id_str[12] = '\0';
9582 new_id_str[12] = '\0';
9584 err = get_short_logmsg(&logmsg, 42, commit);
9588 printf("%s -> %s: %s\n", old_id_str,
9589 new_id_str ? new_id_str : "no-op change", logmsg);
9597 static const struct got_error *
9598 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9599 struct got_reference *branch, struct got_reference *new_base_branch,
9600 struct got_reference *tmp_branch, struct got_repository *repo,
9603 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9604 return got_worktree_rebase_complete(worktree, fileindex,
9605 new_base_branch, tmp_branch, branch, repo, create_backup);
9608 static const struct got_error *
9609 rebase_commit(struct got_pathlist_head *merged_paths,
9610 struct got_worktree *worktree, struct got_fileindex *fileindex,
9611 struct got_reference *tmp_branch, const char *committer,
9612 struct got_object_id *commit_id, struct got_repository *repo)
9614 const struct got_error *error;
9615 struct got_commit_object *commit;
9616 struct got_object_id *new_commit_id;
9618 error = got_object_open_as_commit(&commit, repo, commit_id);
9622 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9623 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9626 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9628 error = show_rebase_progress(commit, commit_id, NULL);
9630 error = show_rebase_progress(commit, commit_id, new_commit_id);
9631 free(new_commit_id);
9634 got_object_commit_close(commit);
9638 struct check_path_prefix_arg {
9639 const char *path_prefix;
9644 static const struct got_error *
9645 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9646 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9647 struct got_object_id *id1, struct got_object_id *id2,
9648 const char *path1, const char *path2,
9649 mode_t mode1, mode_t mode2, struct got_repository *repo)
9651 struct check_path_prefix_arg *a = arg;
9653 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9654 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9655 return got_error(a->errcode);
9660 static const struct got_error *
9661 check_path_prefix(struct got_object_id *parent_id,
9662 struct got_object_id *commit_id, const char *path_prefix,
9663 int errcode, struct got_repository *repo)
9665 const struct got_error *err;
9666 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9667 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9668 struct check_path_prefix_arg cpp_arg;
9670 if (got_path_is_root_dir(path_prefix))
9673 err = got_object_open_as_commit(&commit, repo, commit_id);
9677 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9681 err = got_object_open_as_tree(&tree1, repo,
9682 got_object_commit_get_tree_id(parent_commit));
9686 err = got_object_open_as_tree(&tree2, repo,
9687 got_object_commit_get_tree_id(commit));
9691 cpp_arg.path_prefix = path_prefix;
9692 while (cpp_arg.path_prefix[0] == '/')
9693 cpp_arg.path_prefix++;
9694 cpp_arg.len = strlen(cpp_arg.path_prefix);
9695 cpp_arg.errcode = errcode;
9696 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9697 check_path_prefix_in_diff, &cpp_arg, 0);
9700 got_object_tree_close(tree1);
9702 got_object_tree_close(tree2);
9704 got_object_commit_close(commit);
9706 got_object_commit_close(parent_commit);
9710 static const struct got_error *
9711 collect_commits(struct got_object_id_queue *commits,
9712 struct got_object_id *initial_commit_id,
9713 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9714 const char *path_prefix, int path_prefix_errcode,
9715 struct got_repository *repo)
9717 const struct got_error *err = NULL;
9718 struct got_commit_graph *graph = NULL;
9719 struct got_object_id *parent_id = NULL;
9720 struct got_object_qid *qid;
9721 struct got_object_id *commit_id = initial_commit_id;
9723 err = got_commit_graph_open(&graph, "/", 1);
9727 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9728 check_cancelled, NULL);
9731 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9732 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9733 check_cancelled, NULL);
9735 if (err->code == GOT_ERR_ITER_COMPLETED) {
9736 err = got_error_msg(GOT_ERR_ANCESTRY,
9737 "ran out of commits to rebase before "
9738 "youngest common ancestor commit has "
9743 err = check_path_prefix(parent_id, commit_id,
9744 path_prefix, path_prefix_errcode, repo);
9748 err = got_object_qid_alloc(&qid, commit_id);
9751 STAILQ_INSERT_HEAD(commits, qid, entry);
9752 commit_id = parent_id;
9756 got_commit_graph_close(graph);
9760 static const struct got_error *
9761 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9763 const struct got_error *err = NULL;
9764 time_t committer_time;
9766 char datebuf[11]; /* YYYY-MM-DD + NUL */
9767 char *author0 = NULL, *author, *smallerthan;
9768 char *logmsg0 = NULL, *logmsg, *newline;
9770 committer_time = got_object_commit_get_committer_time(commit);
9771 if (gmtime_r(&committer_time, &tm) == NULL)
9772 return got_error_from_errno("gmtime_r");
9773 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9774 return got_error(GOT_ERR_NO_SPACE);
9776 author0 = strdup(got_object_commit_get_author(commit));
9777 if (author0 == NULL)
9778 return got_error_from_errno("strdup");
9780 smallerthan = strchr(author, '<');
9781 if (smallerthan && smallerthan[1] != '\0')
9782 author = smallerthan + 1;
9783 author[strcspn(author, "@>")] = '\0';
9785 err = got_object_commit_get_logmsg(&logmsg0, commit);
9789 while (*logmsg == '\n')
9791 newline = strchr(logmsg, '\n');
9795 if (asprintf(brief_str, "%s %s %s",
9796 datebuf, author, logmsg) == -1)
9797 err = got_error_from_errno("asprintf");
9804 static const struct got_error *
9805 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9806 struct got_repository *repo)
9808 const struct got_error *err;
9811 err = got_object_id_str(&id_str, id);
9815 err = got_ref_delete(ref, repo);
9819 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9825 static const struct got_error *
9826 print_backup_ref(const char *branch_name, const char *new_id_str,
9827 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9828 struct got_reflist_object_id_map *refs_idmap,
9829 struct got_repository *repo)
9831 const struct got_error *err = NULL;
9832 struct got_reflist_head *refs;
9833 char *refs_str = NULL;
9834 struct got_object_id *new_commit_id = NULL;
9835 struct got_commit_object *new_commit = NULL;
9836 char *new_commit_brief_str = NULL;
9837 struct got_object_id *yca_id = NULL;
9838 struct got_commit_object *yca_commit = NULL;
9839 char *yca_id_str = NULL, *yca_brief_str = NULL;
9840 char *custom_refs_str;
9842 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9843 return got_error_from_errno("asprintf");
9845 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9846 0, 0, refs_idmap, custom_refs_str);
9850 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9854 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9856 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9861 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9865 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9869 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9870 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9874 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9875 refs_str ? " (" : "", refs_str ? refs_str : "",
9876 refs_str ? ")" : "", new_commit_brief_str);
9877 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9878 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9882 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9886 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9890 err = got_object_id_str(&yca_id_str, yca_id);
9894 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9896 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9900 printf("history forked at %s%s%s%s\n %s\n",
9902 refs_str ? " (" : "", refs_str ? refs_str : "",
9903 refs_str ? ")" : "", yca_brief_str);
9906 free(custom_refs_str);
9907 free(new_commit_id);
9911 free(yca_brief_str);
9913 got_object_commit_close(new_commit);
9915 got_object_commit_close(yca_commit);
9920 static const struct got_error *
9921 process_backup_refs(const char *backup_ref_prefix,
9922 const char *wanted_branch_name,
9923 int delete, struct got_repository *repo)
9925 const struct got_error *err;
9926 struct got_reflist_head refs, backup_refs;
9927 struct got_reflist_entry *re;
9928 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9929 struct got_object_id *old_commit_id = NULL;
9930 char *branch_name = NULL;
9931 struct got_commit_object *old_commit = NULL;
9932 struct got_reflist_object_id_map *refs_idmap = NULL;
9933 int wanted_branch_found = 0;
9936 TAILQ_INIT(&backup_refs);
9938 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9942 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9946 if (wanted_branch_name) {
9947 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9948 wanted_branch_name += 11;
9951 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9952 got_ref_cmp_by_commit_timestamp_descending, repo);
9956 TAILQ_FOREACH(re, &backup_refs, entry) {
9957 const char *refname = got_ref_get_name(re->ref);
9960 err = check_cancelled(NULL);
9964 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9968 err = got_object_open_as_commit(&old_commit, repo,
9973 if (strncmp(backup_ref_prefix, refname,
9974 backup_ref_prefix_len) == 0)
9975 refname += backup_ref_prefix_len;
9977 while (refname[0] == '/')
9980 branch_name = strdup(refname);
9981 if (branch_name == NULL) {
9982 err = got_error_from_errno("strdup");
9985 slash = strrchr(branch_name, '/');
9988 refname += strlen(branch_name) + 1;
9991 if (wanted_branch_name == NULL ||
9992 strcmp(wanted_branch_name, branch_name) == 0) {
9993 wanted_branch_found = 1;
9995 err = delete_backup_ref(re->ref,
9996 old_commit_id, repo);
9998 err = print_backup_ref(branch_name, refname,
9999 old_commit_id, old_commit, refs_idmap,
10006 free(old_commit_id);
10007 old_commit_id = NULL;
10009 branch_name = NULL;
10010 got_object_commit_close(old_commit);
10014 if (wanted_branch_name && !wanted_branch_found) {
10015 err = got_error_fmt(GOT_ERR_NOT_REF,
10016 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10020 got_reflist_object_id_map_free(refs_idmap);
10021 got_ref_list_free(&refs);
10022 got_ref_list_free(&backup_refs);
10023 free(old_commit_id);
10026 got_object_commit_close(old_commit);
10030 static const struct got_error *
10031 abort_progress(void *arg, unsigned char status, const char *path)
10034 * Unversioned files should not clutter progress output when
10035 * an operation is aborted.
10037 if (status == GOT_STATUS_UNVERSIONED)
10040 return update_progress(arg, status, path);
10043 static const struct got_error *
10044 cmd_rebase(int argc, char *argv[])
10046 const struct got_error *error = NULL;
10047 struct got_worktree *worktree = NULL;
10048 struct got_repository *repo = NULL;
10049 struct got_fileindex *fileindex = NULL;
10050 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10051 struct got_reference *branch = NULL;
10052 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10053 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10054 struct got_object_id *resume_commit_id = NULL;
10055 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10056 struct got_commit_object *commit = NULL;
10057 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10058 int histedit_in_progress = 0, merge_in_progress = 0;
10059 int create_backup = 1, list_backups = 0, delete_backups = 0;
10060 struct got_object_id_queue commits;
10061 struct got_pathlist_head merged_paths;
10062 const struct got_object_id_queue *parent_ids;
10063 struct got_object_qid *qid, *pid;
10064 struct got_update_progress_arg upa;
10065 int *pack_fds = NULL;
10067 STAILQ_INIT(&commits);
10068 TAILQ_INIT(&merged_paths);
10069 memset(&upa, 0, sizeof(upa));
10071 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10077 continue_rebase = 1;
10083 delete_backups = 1;
10095 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10096 "unveil", NULL) == -1)
10099 if (list_backups) {
10101 option_conflict('l', 'a');
10102 if (continue_rebase)
10103 option_conflict('l', 'c');
10104 if (delete_backups)
10105 option_conflict('l', 'X');
10106 if (argc != 0 && argc != 1)
10108 } else if (delete_backups) {
10110 option_conflict('X', 'a');
10111 if (continue_rebase)
10112 option_conflict('X', 'c');
10114 option_conflict('l', 'X');
10115 if (argc != 0 && argc != 1)
10118 if (abort_rebase && continue_rebase)
10120 else if (abort_rebase || continue_rebase) {
10123 } else if (argc != 1)
10127 cwd = getcwd(NULL, 0);
10129 error = got_error_from_errno("getcwd");
10133 error = got_repo_pack_fds_open(&pack_fds);
10137 error = got_worktree_open(&worktree, cwd);
10139 if (list_backups || delete_backups) {
10140 if (error->code != GOT_ERR_NOT_WORKTREE)
10143 if (error->code == GOT_ERR_NOT_WORKTREE)
10144 error = wrap_not_worktree_error(error,
10150 error = get_gitconfig_path(&gitconfig_path);
10153 error = got_repo_open(&repo,
10154 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10155 gitconfig_path, pack_fds);
10159 error = get_author(&committer, repo, worktree);
10160 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10163 error = apply_unveil(got_repo_get_path(repo), 0,
10164 worktree ? got_worktree_get_root_path(worktree) : NULL);
10168 if (list_backups || delete_backups) {
10169 error = process_backup_refs(
10170 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10171 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10172 goto done; /* nothing else to do */
10175 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10179 if (histedit_in_progress) {
10180 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10184 error = got_worktree_merge_in_progress(&merge_in_progress,
10188 if (merge_in_progress) {
10189 error = got_error(GOT_ERR_MERGE_BUSY);
10193 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10197 if (abort_rebase) {
10198 if (!rebase_in_progress) {
10199 error = got_error(GOT_ERR_NOT_REBASING);
10202 error = got_worktree_rebase_continue(&resume_commit_id,
10203 &new_base_branch, &tmp_branch, &branch, &fileindex,
10207 printf("Switching work tree to %s\n",
10208 got_ref_get_symref_target(new_base_branch));
10209 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10210 new_base_branch, abort_progress, &upa);
10213 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10214 print_merge_progress_stats(&upa);
10215 goto done; /* nothing else to do */
10218 if (continue_rebase) {
10219 if (!rebase_in_progress) {
10220 error = got_error(GOT_ERR_NOT_REBASING);
10223 error = got_worktree_rebase_continue(&resume_commit_id,
10224 &new_base_branch, &tmp_branch, &branch, &fileindex,
10229 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10230 committer, resume_commit_id, repo);
10234 yca_id = got_object_id_dup(resume_commit_id);
10235 if (yca_id == NULL) {
10236 error = got_error_from_errno("got_object_id_dup");
10240 error = got_ref_open(&branch, repo, argv[0], 0);
10245 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10249 if (!continue_rebase) {
10250 struct got_object_id *base_commit_id;
10252 base_commit_id = got_worktree_get_base_commit_id(worktree);
10253 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10254 base_commit_id, branch_head_commit_id, 1, repo,
10255 check_cancelled, NULL);
10258 if (yca_id == NULL) {
10259 error = got_error_msg(GOT_ERR_ANCESTRY,
10260 "specified branch shares no common ancestry "
10261 "with work tree's branch");
10265 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10267 if (error->code != GOT_ERR_ANCESTRY)
10271 struct got_pathlist_head paths;
10272 printf("%s is already based on %s\n",
10273 got_ref_get_name(branch),
10274 got_worktree_get_head_ref_name(worktree));
10275 error = switch_head_ref(branch, branch_head_commit_id,
10279 error = got_worktree_set_base_commit_id(worktree, repo,
10280 branch_head_commit_id);
10283 TAILQ_INIT(&paths);
10284 error = got_pathlist_append(&paths, "", NULL);
10287 error = got_worktree_checkout_files(worktree,
10288 &paths, repo, update_progress, &upa,
10289 check_cancelled, NULL);
10290 got_pathlist_free(&paths);
10293 if (upa.did_something) {
10295 error = got_object_id_str(&id_str,
10296 branch_head_commit_id);
10299 printf("Updated to %s: %s\n",
10300 got_worktree_get_head_ref_name(worktree),
10304 printf("Already up-to-date\n");
10305 print_update_progress_stats(&upa);
10310 commit_id = branch_head_commit_id;
10311 error = got_object_open_as_commit(&commit, repo, commit_id);
10315 parent_ids = got_object_commit_get_parent_ids(commit);
10316 pid = STAILQ_FIRST(parent_ids);
10318 error = got_error(GOT_ERR_EMPTY_REBASE);
10321 error = collect_commits(&commits, commit_id, &pid->id,
10322 yca_id, got_worktree_get_path_prefix(worktree),
10323 GOT_ERR_REBASE_PATH, repo);
10324 got_object_commit_close(commit);
10329 if (!continue_rebase) {
10330 error = got_worktree_rebase_prepare(&new_base_branch,
10331 &tmp_branch, &fileindex, worktree, branch, repo);
10336 if (STAILQ_EMPTY(&commits)) {
10337 if (continue_rebase) {
10338 error = rebase_complete(worktree, fileindex,
10339 branch, new_base_branch, tmp_branch, repo,
10343 /* Fast-forward the reference of the branch. */
10344 struct got_object_id *new_head_commit_id;
10346 error = got_ref_resolve(&new_head_commit_id, repo,
10350 error = got_object_id_str(&id_str, new_head_commit_id);
10353 printf("Forwarding %s to commit %s\n",
10354 got_ref_get_name(branch), id_str);
10356 error = got_ref_change_ref(branch,
10357 new_head_commit_id);
10360 /* No backup needed since objects did not change. */
10366 STAILQ_FOREACH(qid, &commits, entry) {
10368 commit_id = &qid->id;
10369 parent_id = pid ? &pid->id : yca_id;
10372 memset(&upa, 0, sizeof(upa));
10373 error = got_worktree_rebase_merge_files(&merged_paths,
10374 worktree, fileindex, parent_id, commit_id, repo,
10375 update_progress, &upa, check_cancelled, NULL);
10379 print_merge_progress_stats(&upa);
10380 if (upa.conflicts > 0 || upa.missing > 0 ||
10381 upa.not_deleted > 0 || upa.unversioned > 0) {
10382 if (upa.conflicts > 0) {
10383 error = show_rebase_merge_conflict(&qid->id,
10388 got_worktree_rebase_pathlist_free(&merged_paths);
10392 error = rebase_commit(&merged_paths, worktree, fileindex,
10393 tmp_branch, committer, commit_id, repo);
10394 got_worktree_rebase_pathlist_free(&merged_paths);
10399 if (upa.conflicts > 0 || upa.missing > 0 ||
10400 upa.not_deleted > 0 || upa.unversioned > 0) {
10401 error = got_worktree_rebase_postpone(worktree, fileindex);
10404 if (upa.conflicts > 0 && upa.missing == 0 &&
10405 upa.not_deleted == 0 && upa.unversioned == 0) {
10406 error = got_error_msg(GOT_ERR_CONFLICTS,
10407 "conflicts must be resolved before rebasing "
10409 } else if (upa.conflicts > 0) {
10410 error = got_error_msg(GOT_ERR_CONFLICTS,
10411 "conflicts must be resolved before rebasing "
10412 "can continue; changes destined for some "
10413 "files were not yet merged and should be "
10414 "merged manually if required before the "
10415 "rebase operation is continued");
10417 error = got_error_msg(GOT_ERR_CONFLICTS,
10418 "changes destined for some files were not "
10419 "yet merged and should be merged manually "
10420 "if required before the rebase operation "
10424 error = rebase_complete(worktree, fileindex, branch,
10425 new_base_branch, tmp_branch, repo, create_backup);
10429 free(gitconfig_path);
10430 got_object_id_queue_free(&commits);
10431 free(branch_head_commit_id);
10432 free(resume_commit_id);
10435 got_object_commit_close(commit);
10437 got_ref_close(branch);
10438 if (new_base_branch)
10439 got_ref_close(new_base_branch);
10441 got_ref_close(tmp_branch);
10443 got_worktree_close(worktree);
10445 const struct got_error *close_err = got_repo_close(repo);
10450 const struct got_error *pack_err =
10451 got_repo_pack_fds_close(pack_fds);
10459 usage_histedit(void)
10461 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10462 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10467 #define GOT_HISTEDIT_PICK 'p'
10468 #define GOT_HISTEDIT_EDIT 'e'
10469 #define GOT_HISTEDIT_FOLD 'f'
10470 #define GOT_HISTEDIT_DROP 'd'
10471 #define GOT_HISTEDIT_MESG 'm'
10473 static const struct got_histedit_cmd {
10474 unsigned char code;
10477 } got_histedit_cmds[] = {
10478 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10479 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10480 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10482 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10483 { GOT_HISTEDIT_MESG, "mesg",
10484 "single-line log message for commit above (open editor if empty)" },
10487 struct got_histedit_list_entry {
10488 TAILQ_ENTRY(got_histedit_list_entry) entry;
10489 struct got_object_id *commit_id;
10490 const struct got_histedit_cmd *cmd;
10493 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10495 static const struct got_error *
10496 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10497 FILE *f, struct got_repository *repo)
10499 const struct got_error *err = NULL;
10500 char *logmsg = NULL, *id_str = NULL;
10501 struct got_commit_object *commit = NULL;
10504 err = got_object_open_as_commit(&commit, repo, commit_id);
10508 err = get_short_logmsg(&logmsg, 34, commit);
10512 err = got_object_id_str(&id_str, commit_id);
10516 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10518 err = got_ferror(f, GOT_ERR_IO);
10521 got_object_commit_close(commit);
10527 static const struct got_error *
10528 histedit_write_commit_list(struct got_object_id_queue *commits,
10529 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10530 struct got_repository *repo)
10532 const struct got_error *err = NULL;
10533 struct got_object_qid *qid;
10534 const char *histedit_cmd = NULL;
10536 if (STAILQ_EMPTY(commits))
10537 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10539 STAILQ_FOREACH(qid, commits, entry) {
10540 histedit_cmd = got_histedit_cmds[0].name;
10542 histedit_cmd = "edit";
10543 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10544 histedit_cmd = "fold";
10545 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10548 if (edit_logmsg_only) {
10549 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10551 err = got_ferror(f, GOT_ERR_IO);
10560 static const struct got_error *
10561 write_cmd_list(FILE *f, const char *branch_name,
10562 struct got_object_id_queue *commits)
10564 const struct got_error *err = NULL;
10568 struct got_object_qid *qid;
10570 qid = STAILQ_FIRST(commits);
10571 err = got_object_id_str(&id_str, &qid->id);
10576 "# Editing the history of branch '%s' starting at\n"
10578 "# Commits will be processed in order from top to "
10579 "bottom of this file.\n", branch_name, id_str);
10581 err = got_ferror(f, GOT_ERR_IO);
10585 n = fprintf(f, "# Available histedit commands:\n");
10587 err = got_ferror(f, GOT_ERR_IO);
10591 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10592 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10593 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10596 err = got_ferror(f, GOT_ERR_IO);
10605 static const struct got_error *
10606 histedit_syntax_error(int lineno)
10608 static char msg[42];
10611 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10613 if (ret == -1 || ret >= sizeof(msg))
10614 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10616 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10619 static const struct got_error *
10620 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10621 char *logmsg, struct got_repository *repo)
10623 const struct got_error *err;
10624 struct got_commit_object *folded_commit = NULL;
10625 char *id_str, *folded_logmsg = NULL;
10627 err = got_object_id_str(&id_str, hle->commit_id);
10631 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10635 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10638 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10639 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10640 folded_logmsg) == -1) {
10641 err = got_error_from_errno("asprintf");
10645 got_object_commit_close(folded_commit);
10647 free(folded_logmsg);
10651 static struct got_histedit_list_entry *
10652 get_folded_commits(struct got_histedit_list_entry *hle)
10654 struct got_histedit_list_entry *prev, *folded = NULL;
10656 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10657 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10658 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10659 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10661 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10667 static const struct got_error *
10668 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10669 struct got_repository *repo)
10671 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10672 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10673 const struct got_error *err = NULL;
10674 struct got_commit_object *commit = NULL;
10677 struct got_histedit_list_entry *folded = NULL;
10679 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10683 folded = get_folded_commits(hle);
10685 while (folded != hle) {
10686 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10687 folded = TAILQ_NEXT(folded, entry);
10690 err = append_folded_commit_msg(&new_msg, folded,
10696 folded = TAILQ_NEXT(folded, entry);
10700 err = got_object_id_str(&id_str, hle->commit_id);
10703 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10706 logmsg_len = asprintf(&new_msg,
10707 "%s\n# original log message of commit %s: %s",
10708 logmsg ? logmsg : "", id_str, orig_logmsg);
10709 if (logmsg_len == -1) {
10710 err = got_error_from_errno("asprintf");
10716 err = got_object_id_str(&id_str, hle->commit_id);
10720 err = got_opentemp_named_fd(&logmsg_path, &fd,
10721 GOT_TMPDIR_STR "/got-logmsg");
10725 write(fd, logmsg, logmsg_len);
10728 err = get_editor(&editor);
10732 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10735 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10738 hle->logmsg = strdup(new_msg);
10739 if (hle->logmsg == NULL)
10740 err = got_error_from_errno("strdup");
10743 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10744 err = got_error_from_errno2("unlink", logmsg_path);
10750 got_object_commit_close(commit);
10754 static const struct got_error *
10755 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10756 FILE *f, struct got_repository *repo)
10758 const struct got_error *err = NULL;
10759 char *line = NULL, *p, *end;
10762 int lineno = 0, lastcmd = -1;
10763 const struct got_histedit_cmd *cmd;
10764 struct got_object_id *commit_id = NULL;
10765 struct got_histedit_list_entry *hle = NULL;
10768 len = getline(&line, &size, f);
10770 const struct got_error *getline_err;
10773 getline_err = got_error_from_errno("getline");
10774 err = got_ferror(f, getline_err->code);
10779 while (isspace((unsigned char)p[0]))
10781 if (p[0] == '#' || p[0] == '\0') {
10787 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10788 cmd = &got_histedit_cmds[i];
10789 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10790 isspace((unsigned char)p[strlen(cmd->name)])) {
10791 p += strlen(cmd->name);
10794 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10799 if (i == nitems(got_histedit_cmds)) {
10800 err = histedit_syntax_error(lineno);
10803 while (isspace((unsigned char)p[0]))
10805 if (cmd->code == GOT_HISTEDIT_MESG) {
10806 if (lastcmd != GOT_HISTEDIT_PICK &&
10807 lastcmd != GOT_HISTEDIT_EDIT) {
10808 err = got_error(GOT_ERR_HISTEDIT_CMD);
10811 if (p[0] == '\0') {
10812 err = histedit_edit_logmsg(hle, repo);
10816 hle->logmsg = strdup(p);
10817 if (hle->logmsg == NULL) {
10818 err = got_error_from_errno("strdup");
10824 lastcmd = cmd->code;
10828 while (end[0] && !isspace((unsigned char)end[0]))
10832 err = got_object_resolve_id_str(&commit_id, repo, p);
10834 /* override error code */
10835 err = histedit_syntax_error(lineno);
10839 hle = malloc(sizeof(*hle));
10841 err = got_error_from_errno("malloc");
10845 hle->commit_id = commit_id;
10846 hle->logmsg = NULL;
10850 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10851 lastcmd = cmd->code;
10859 static const struct got_error *
10860 histedit_check_script(struct got_histedit_list *histedit_cmds,
10861 struct got_object_id_queue *commits, struct got_repository *repo)
10863 const struct got_error *err = NULL;
10864 struct got_object_qid *qid;
10865 struct got_histedit_list_entry *hle;
10866 static char msg[92];
10869 if (TAILQ_EMPTY(histedit_cmds))
10870 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10871 "histedit script contains no commands");
10872 if (STAILQ_EMPTY(commits))
10873 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10875 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10876 struct got_histedit_list_entry *hle2;
10877 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10880 if (got_object_id_cmp(hle->commit_id,
10881 hle2->commit_id) != 0)
10883 err = got_object_id_str(&id_str, hle->commit_id);
10886 snprintf(msg, sizeof(msg), "commit %s is listed "
10887 "more than once in histedit script", id_str);
10889 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10893 STAILQ_FOREACH(qid, commits, entry) {
10894 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10895 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10899 err = got_object_id_str(&id_str, &qid->id);
10902 snprintf(msg, sizeof(msg),
10903 "commit %s missing from histedit script", id_str);
10905 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10909 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10910 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10911 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10912 "last commit in histedit script cannot be folded");
10917 static const struct got_error *
10918 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10919 const char *path, struct got_object_id_queue *commits,
10920 struct got_repository *repo)
10922 const struct got_error *err = NULL;
10926 err = get_editor(&editor);
10930 if (spawn_editor(editor, path) == -1) {
10931 err = got_error_from_errno("failed spawning editor");
10935 f = fopen(path, "re");
10937 err = got_error_from_errno("fopen");
10940 err = histedit_parse_list(histedit_cmds, f, repo);
10944 err = histedit_check_script(histedit_cmds, commits, repo);
10946 if (f && fclose(f) == EOF && err == NULL)
10947 err = got_error_from_errno("fclose");
10952 static const struct got_error *
10953 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10954 struct got_object_id_queue *, const char *, const char *,
10955 struct got_repository *);
10957 static const struct got_error *
10958 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10959 struct got_object_id_queue *commits, const char *branch_name,
10960 int edit_logmsg_only, int fold_only, int edit_only,
10961 struct got_repository *repo)
10963 const struct got_error *err;
10967 err = got_opentemp_named(&path, &f, "got-histedit");
10971 err = write_cmd_list(f, branch_name, commits);
10975 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10976 fold_only, edit_only, repo);
10980 if (edit_logmsg_only || fold_only || edit_only) {
10982 err = histedit_parse_list(histedit_cmds, f, repo);
10984 if (fclose(f) == EOF) {
10985 err = got_error_from_errno("fclose");
10989 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10991 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10992 err->code != GOT_ERR_HISTEDIT_CMD)
10994 err = histedit_edit_list_retry(histedit_cmds, err,
10995 commits, path, branch_name, repo);
10999 if (f && fclose(f) == EOF && err == NULL)
11000 err = got_error_from_errno("fclose");
11001 if (path && unlink(path) != 0 && err == NULL)
11002 err = got_error_from_errno2("unlink", path);
11007 static const struct got_error *
11008 histedit_save_list(struct got_histedit_list *histedit_cmds,
11009 struct got_worktree *worktree, struct got_repository *repo)
11011 const struct got_error *err = NULL;
11014 struct got_histedit_list_entry *hle;
11015 struct got_commit_object *commit = NULL;
11017 err = got_worktree_get_histedit_script_path(&path, worktree);
11021 f = fopen(path, "we");
11023 err = got_error_from_errno2("fopen", path);
11026 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11027 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11033 int n = fprintf(f, "%c %s\n",
11034 GOT_HISTEDIT_MESG, hle->logmsg);
11036 err = got_ferror(f, GOT_ERR_IO);
11042 if (f && fclose(f) == EOF && err == NULL)
11043 err = got_error_from_errno("fclose");
11046 got_object_commit_close(commit);
11051 histedit_free_list(struct got_histedit_list *histedit_cmds)
11053 struct got_histedit_list_entry *hle;
11055 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11056 TAILQ_REMOVE(histedit_cmds, hle, entry);
11061 static const struct got_error *
11062 histedit_load_list(struct got_histedit_list *histedit_cmds,
11063 const char *path, struct got_repository *repo)
11065 const struct got_error *err = NULL;
11068 f = fopen(path, "re");
11070 err = got_error_from_errno2("fopen", path);
11074 err = histedit_parse_list(histedit_cmds, f, repo);
11076 if (f && fclose(f) == EOF && err == NULL)
11077 err = got_error_from_errno("fclose");
11081 static const struct got_error *
11082 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11083 const struct got_error *edit_err, struct got_object_id_queue *commits,
11084 const char *path, const char *branch_name, struct got_repository *repo)
11086 const struct got_error *err = NULL, *prev_err = edit_err;
11089 while (resp != 'c' && resp != 'r' && resp != 'a') {
11090 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11091 "or (a)bort: ", getprogname(), prev_err->msg);
11096 histedit_free_list(histedit_cmds);
11097 err = histedit_run_editor(histedit_cmds, path, commits,
11100 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11101 err->code != GOT_ERR_HISTEDIT_CMD)
11108 } else if (resp == 'r') {
11109 histedit_free_list(histedit_cmds);
11110 err = histedit_edit_script(histedit_cmds,
11111 commits, branch_name, 0, 0, 0, repo);
11113 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11114 err->code != GOT_ERR_HISTEDIT_CMD)
11121 } else if (resp == 'a') {
11122 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11125 printf("invalid response '%c'\n", resp);
11131 static const struct got_error *
11132 histedit_complete(struct got_worktree *worktree,
11133 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11134 struct got_reference *branch, struct got_repository *repo)
11136 printf("Switching work tree to %s\n",
11137 got_ref_get_symref_target(branch));
11138 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11142 static const struct got_error *
11143 show_histedit_progress(struct got_commit_object *commit,
11144 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11146 const struct got_error *err;
11147 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11149 err = got_object_id_str(&old_id_str, hle->commit_id);
11154 err = got_object_id_str(&new_id_str, new_id);
11159 old_id_str[12] = '\0';
11161 new_id_str[12] = '\0';
11164 logmsg = strdup(hle->logmsg);
11165 if (logmsg == NULL) {
11166 err = got_error_from_errno("strdup");
11169 trim_logmsg(logmsg, 42);
11171 err = get_short_logmsg(&logmsg, 42, commit);
11176 switch (hle->cmd->code) {
11177 case GOT_HISTEDIT_PICK:
11178 case GOT_HISTEDIT_EDIT:
11179 printf("%s -> %s: %s\n", old_id_str,
11180 new_id_str ? new_id_str : "no-op change", logmsg);
11182 case GOT_HISTEDIT_DROP:
11183 case GOT_HISTEDIT_FOLD:
11184 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11196 static const struct got_error *
11197 histedit_commit(struct got_pathlist_head *merged_paths,
11198 struct got_worktree *worktree, struct got_fileindex *fileindex,
11199 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11200 const char *committer, struct got_repository *repo)
11202 const struct got_error *err;
11203 struct got_commit_object *commit;
11204 struct got_object_id *new_commit_id;
11206 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11207 && hle->logmsg == NULL) {
11208 err = histedit_edit_logmsg(hle, repo);
11213 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11217 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11218 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11219 hle->logmsg, repo);
11221 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11223 err = show_histedit_progress(commit, hle, NULL);
11225 err = show_histedit_progress(commit, hle, new_commit_id);
11226 free(new_commit_id);
11229 got_object_commit_close(commit);
11233 static const struct got_error *
11234 histedit_skip_commit(struct got_histedit_list_entry *hle,
11235 struct got_worktree *worktree, struct got_repository *repo)
11237 const struct got_error *error;
11238 struct got_commit_object *commit;
11240 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11245 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11249 error = show_histedit_progress(commit, hle, NULL);
11250 got_object_commit_close(commit);
11254 static const struct got_error *
11255 check_local_changes(void *arg, unsigned char status,
11256 unsigned char staged_status, const char *path,
11257 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11258 struct got_object_id *commit_id, int dirfd, const char *de_name)
11260 int *have_local_changes = arg;
11263 case GOT_STATUS_ADD:
11264 case GOT_STATUS_DELETE:
11265 case GOT_STATUS_MODIFY:
11266 case GOT_STATUS_CONFLICT:
11267 *have_local_changes = 1;
11268 return got_error(GOT_ERR_CANCELLED);
11273 switch (staged_status) {
11274 case GOT_STATUS_ADD:
11275 case GOT_STATUS_DELETE:
11276 case GOT_STATUS_MODIFY:
11277 *have_local_changes = 1;
11278 return got_error(GOT_ERR_CANCELLED);
11286 static const struct got_error *
11287 cmd_histedit(int argc, char *argv[])
11289 const struct got_error *error = NULL;
11290 struct got_worktree *worktree = NULL;
11291 struct got_fileindex *fileindex = NULL;
11292 struct got_repository *repo = NULL;
11293 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11294 struct got_reference *branch = NULL;
11295 struct got_reference *tmp_branch = NULL;
11296 struct got_object_id *resume_commit_id = NULL;
11297 struct got_object_id *base_commit_id = NULL;
11298 struct got_object_id *head_commit_id = NULL;
11299 struct got_commit_object *commit = NULL;
11300 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11301 struct got_update_progress_arg upa;
11302 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11303 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11304 int list_backups = 0, delete_backups = 0;
11305 const char *edit_script_path = NULL;
11306 struct got_object_id_queue commits;
11307 struct got_pathlist_head merged_paths;
11308 const struct got_object_id_queue *parent_ids;
11309 struct got_object_qid *pid;
11310 struct got_histedit_list histedit_cmds;
11311 struct got_histedit_list_entry *hle;
11312 int *pack_fds = NULL;
11314 STAILQ_INIT(&commits);
11315 TAILQ_INIT(&histedit_cmds);
11316 TAILQ_INIT(&merged_paths);
11317 memset(&upa, 0, sizeof(upa));
11319 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11334 edit_script_path = optarg;
11337 edit_logmsg_only = 1;
11343 delete_backups = 1;
11355 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11356 "unveil", NULL) == -1)
11359 if (abort_edit && continue_edit)
11360 option_conflict('a', 'c');
11361 if (edit_script_path && edit_logmsg_only)
11362 option_conflict('F', 'm');
11363 if (abort_edit && edit_logmsg_only)
11364 option_conflict('a', 'm');
11365 if (continue_edit && edit_logmsg_only)
11366 option_conflict('c', 'm');
11367 if (abort_edit && fold_only)
11368 option_conflict('a', 'f');
11369 if (continue_edit && fold_only)
11370 option_conflict('c', 'f');
11371 if (fold_only && edit_logmsg_only)
11372 option_conflict('f', 'm');
11373 if (edit_script_path && fold_only)
11374 option_conflict('F', 'f');
11375 if (abort_edit && edit_only)
11376 option_conflict('a', 'e');
11377 if (continue_edit && edit_only)
11378 option_conflict('c', 'e');
11379 if (edit_only && edit_logmsg_only)
11380 option_conflict('e', 'm');
11381 if (edit_script_path && edit_only)
11382 option_conflict('F', 'e');
11383 if (list_backups) {
11385 option_conflict('l', 'a');
11387 option_conflict('l', 'c');
11388 if (edit_script_path)
11389 option_conflict('l', 'F');
11390 if (edit_logmsg_only)
11391 option_conflict('l', 'm');
11393 option_conflict('l', 'f');
11395 option_conflict('l', 'e');
11396 if (delete_backups)
11397 option_conflict('l', 'X');
11398 if (argc != 0 && argc != 1)
11400 } else if (delete_backups) {
11402 option_conflict('X', 'a');
11404 option_conflict('X', 'c');
11405 if (edit_script_path)
11406 option_conflict('X', 'F');
11407 if (edit_logmsg_only)
11408 option_conflict('X', 'm');
11410 option_conflict('X', 'f');
11412 option_conflict('X', 'e');
11414 option_conflict('X', 'l');
11415 if (argc != 0 && argc != 1)
11417 } else if (argc != 0)
11421 * This command cannot apply unveil(2) in all cases because the
11422 * user may choose to run an editor to edit the histedit script
11423 * and to edit individual commit log messages.
11424 * unveil(2) traverses exec(2); if an editor is used we have to
11425 * apply unveil after edit script and log messages have been written.
11426 * XXX TODO: Make use of unveil(2) where possible.
11429 cwd = getcwd(NULL, 0);
11431 error = got_error_from_errno("getcwd");
11435 error = got_repo_pack_fds_open(&pack_fds);
11439 error = got_worktree_open(&worktree, cwd);
11441 if (list_backups || delete_backups) {
11442 if (error->code != GOT_ERR_NOT_WORKTREE)
11445 if (error->code == GOT_ERR_NOT_WORKTREE)
11446 error = wrap_not_worktree_error(error,
11452 if (list_backups || delete_backups) {
11453 error = got_repo_open(&repo,
11454 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11458 error = apply_unveil(got_repo_get_path(repo), 0,
11459 worktree ? got_worktree_get_root_path(worktree) : NULL);
11462 error = process_backup_refs(
11463 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11464 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11465 goto done; /* nothing else to do */
11468 error = get_gitconfig_path(&gitconfig_path);
11471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11472 gitconfig_path, pack_fds);
11476 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11479 if (rebase_in_progress) {
11480 error = got_error(GOT_ERR_REBASING);
11484 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11488 if (merge_in_progress) {
11489 error = got_error(GOT_ERR_MERGE_BUSY);
11493 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11497 if (edit_in_progress && edit_logmsg_only) {
11498 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11499 "histedit operation is in progress in this "
11500 "work tree and must be continued or aborted "
11501 "before the -m option can be used");
11504 if (edit_in_progress && fold_only) {
11505 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11506 "histedit operation is in progress in this "
11507 "work tree and must be continued or aborted "
11508 "before the -f option can be used");
11511 if (edit_in_progress && edit_only) {
11512 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11513 "histedit operation is in progress in this "
11514 "work tree and must be continued or aborted "
11515 "before the -e option can be used");
11519 if (edit_in_progress && abort_edit) {
11520 error = got_worktree_histedit_continue(&resume_commit_id,
11521 &tmp_branch, &branch, &base_commit_id, &fileindex,
11525 printf("Switching work tree to %s\n",
11526 got_ref_get_symref_target(branch));
11527 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11528 branch, base_commit_id, abort_progress, &upa);
11531 printf("Histedit of %s aborted\n",
11532 got_ref_get_symref_target(branch));
11533 print_merge_progress_stats(&upa);
11534 goto done; /* nothing else to do */
11535 } else if (abort_edit) {
11536 error = got_error(GOT_ERR_NOT_HISTEDIT);
11540 error = get_author(&committer, repo, worktree);
11544 if (continue_edit) {
11547 if (!edit_in_progress) {
11548 error = got_error(GOT_ERR_NOT_HISTEDIT);
11552 error = got_worktree_get_histedit_script_path(&path, worktree);
11556 error = histedit_load_list(&histedit_cmds, path, repo);
11561 error = got_worktree_histedit_continue(&resume_commit_id,
11562 &tmp_branch, &branch, &base_commit_id, &fileindex,
11567 error = got_ref_resolve(&head_commit_id, repo, branch);
11571 error = got_object_open_as_commit(&commit, repo,
11575 parent_ids = got_object_commit_get_parent_ids(commit);
11576 pid = STAILQ_FIRST(parent_ids);
11578 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11581 error = collect_commits(&commits, head_commit_id, &pid->id,
11582 base_commit_id, got_worktree_get_path_prefix(worktree),
11583 GOT_ERR_HISTEDIT_PATH, repo);
11584 got_object_commit_close(commit);
11589 if (edit_in_progress) {
11590 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11594 error = got_ref_open(&branch, repo,
11595 got_worktree_get_head_ref_name(worktree), 0);
11599 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11600 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11601 "will not edit commit history of a branch outside "
11602 "the \"refs/heads/\" reference namespace");
11606 error = got_ref_resolve(&head_commit_id, repo, branch);
11607 got_ref_close(branch);
11612 error = got_object_open_as_commit(&commit, repo,
11616 parent_ids = got_object_commit_get_parent_ids(commit);
11617 pid = STAILQ_FIRST(parent_ids);
11619 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11622 error = collect_commits(&commits, head_commit_id, &pid->id,
11623 got_worktree_get_base_commit_id(worktree),
11624 got_worktree_get_path_prefix(worktree),
11625 GOT_ERR_HISTEDIT_PATH, repo);
11626 got_object_commit_close(commit);
11631 if (STAILQ_EMPTY(&commits)) {
11632 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11636 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11637 &base_commit_id, &fileindex, worktree, repo);
11641 if (edit_script_path) {
11642 error = histedit_load_list(&histedit_cmds,
11643 edit_script_path, repo);
11645 got_worktree_histedit_abort(worktree, fileindex,
11646 repo, branch, base_commit_id,
11647 abort_progress, &upa);
11648 print_merge_progress_stats(&upa);
11652 const char *branch_name;
11653 branch_name = got_ref_get_symref_target(branch);
11654 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11656 error = histedit_edit_script(&histedit_cmds, &commits,
11657 branch_name, edit_logmsg_only, fold_only,
11660 got_worktree_histedit_abort(worktree, fileindex,
11661 repo, branch, base_commit_id,
11662 abort_progress, &upa);
11663 print_merge_progress_stats(&upa);
11669 error = histedit_save_list(&histedit_cmds, worktree,
11672 got_worktree_histedit_abort(worktree, fileindex,
11673 repo, branch, base_commit_id,
11674 abort_progress, &upa);
11675 print_merge_progress_stats(&upa);
11681 error = histedit_check_script(&histedit_cmds, &commits, repo);
11685 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11686 if (resume_commit_id) {
11687 if (got_object_id_cmp(hle->commit_id,
11688 resume_commit_id) != 0)
11691 resume_commit_id = NULL;
11692 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11693 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11694 error = histedit_skip_commit(hle, worktree,
11699 struct got_pathlist_head paths;
11700 int have_changes = 0;
11702 TAILQ_INIT(&paths);
11703 error = got_pathlist_append(&paths, "", NULL);
11706 error = got_worktree_status(worktree, &paths,
11707 repo, 0, check_local_changes, &have_changes,
11708 check_cancelled, NULL);
11709 got_pathlist_free(&paths);
11711 if (error->code != GOT_ERR_CANCELLED)
11713 if (sigint_received || sigpipe_received)
11716 if (have_changes) {
11717 error = histedit_commit(NULL, worktree,
11718 fileindex, tmp_branch, hle,
11723 error = got_object_open_as_commit(
11724 &commit, repo, hle->commit_id);
11727 error = show_histedit_progress(commit,
11729 got_object_commit_close(commit);
11738 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11739 error = histedit_skip_commit(hle, worktree, repo);
11745 error = got_object_open_as_commit(&commit, repo,
11749 parent_ids = got_object_commit_get_parent_ids(commit);
11750 pid = STAILQ_FIRST(parent_ids);
11752 error = got_worktree_histedit_merge_files(&merged_paths,
11753 worktree, fileindex, &pid->id, hle->commit_id, repo,
11754 update_progress, &upa, check_cancelled, NULL);
11757 got_object_commit_close(commit);
11760 print_merge_progress_stats(&upa);
11761 if (upa.conflicts > 0 || upa.missing > 0 ||
11762 upa.not_deleted > 0 || upa.unversioned > 0) {
11763 if (upa.conflicts > 0) {
11764 error = show_rebase_merge_conflict(
11765 hle->commit_id, repo);
11769 got_worktree_rebase_pathlist_free(&merged_paths);
11773 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11775 error = got_object_id_str(&id_str, hle->commit_id);
11778 printf("Stopping histedit for amending commit %s\n",
11781 got_worktree_rebase_pathlist_free(&merged_paths);
11782 error = got_worktree_histedit_postpone(worktree,
11787 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11788 error = histedit_skip_commit(hle, worktree, repo);
11794 error = histedit_commit(&merged_paths, worktree, fileindex,
11795 tmp_branch, hle, committer, repo);
11796 got_worktree_rebase_pathlist_free(&merged_paths);
11801 if (upa.conflicts > 0 || upa.missing > 0 ||
11802 upa.not_deleted > 0 || upa.unversioned > 0) {
11803 error = got_worktree_histedit_postpone(worktree, fileindex);
11806 if (upa.conflicts > 0 && upa.missing == 0 &&
11807 upa.not_deleted == 0 && upa.unversioned == 0) {
11808 error = got_error_msg(GOT_ERR_CONFLICTS,
11809 "conflicts must be resolved before histedit "
11811 } else if (upa.conflicts > 0) {
11812 error = got_error_msg(GOT_ERR_CONFLICTS,
11813 "conflicts must be resolved before histedit "
11814 "can continue; changes destined for some "
11815 "files were not yet merged and should be "
11816 "merged manually if required before the "
11817 "histedit operation is continued");
11819 error = got_error_msg(GOT_ERR_CONFLICTS,
11820 "changes destined for some files were not "
11821 "yet merged and should be merged manually "
11822 "if required before the histedit operation "
11826 error = histedit_complete(worktree, fileindex, tmp_branch,
11831 free(gitconfig_path);
11832 got_object_id_queue_free(&commits);
11833 histedit_free_list(&histedit_cmds);
11834 free(head_commit_id);
11835 free(base_commit_id);
11836 free(resume_commit_id);
11838 got_object_commit_close(commit);
11840 got_ref_close(branch);
11842 got_ref_close(tmp_branch);
11844 got_worktree_close(worktree);
11846 const struct got_error *close_err = got_repo_close(repo);
11851 const struct got_error *pack_err =
11852 got_repo_pack_fds_close(pack_fds);
11860 usage_integrate(void)
11862 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11866 static const struct got_error *
11867 cmd_integrate(int argc, char *argv[])
11869 const struct got_error *error = NULL;
11870 struct got_repository *repo = NULL;
11871 struct got_worktree *worktree = NULL;
11872 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11873 const char *branch_arg = NULL;
11874 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11875 struct got_fileindex *fileindex = NULL;
11876 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11878 struct got_update_progress_arg upa;
11879 int *pack_fds = NULL;
11881 while ((ch = getopt(argc, argv, "")) != -1) {
11894 branch_arg = argv[0];
11896 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11897 "unveil", NULL) == -1)
11900 cwd = getcwd(NULL, 0);
11902 error = got_error_from_errno("getcwd");
11906 error = got_repo_pack_fds_open(&pack_fds);
11910 error = got_worktree_open(&worktree, cwd);
11912 if (error->code == GOT_ERR_NOT_WORKTREE)
11913 error = wrap_not_worktree_error(error, "integrate",
11918 error = check_rebase_or_histedit_in_progress(worktree);
11922 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11927 error = apply_unveil(got_repo_get_path(repo), 0,
11928 got_worktree_get_root_path(worktree));
11932 error = check_merge_in_progress(worktree, repo);
11936 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11937 error = got_error_from_errno("asprintf");
11941 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11942 &base_branch_ref, worktree, refname, repo);
11946 refname = strdup(got_ref_get_name(branch_ref));
11947 if (refname == NULL) {
11948 error = got_error_from_errno("strdup");
11949 got_worktree_integrate_abort(worktree, fileindex, repo,
11950 branch_ref, base_branch_ref);
11953 base_refname = strdup(got_ref_get_name(base_branch_ref));
11954 if (base_refname == NULL) {
11955 error = got_error_from_errno("strdup");
11956 got_worktree_integrate_abort(worktree, fileindex, repo,
11957 branch_ref, base_branch_ref);
11961 error = got_ref_resolve(&commit_id, repo, branch_ref);
11965 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11969 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11970 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11971 "specified branch has already been integrated");
11972 got_worktree_integrate_abort(worktree, fileindex, repo,
11973 branch_ref, base_branch_ref);
11977 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11979 if (error->code == GOT_ERR_ANCESTRY)
11980 error = got_error(GOT_ERR_REBASE_REQUIRED);
11981 got_worktree_integrate_abort(worktree, fileindex, repo,
11982 branch_ref, base_branch_ref);
11986 memset(&upa, 0, sizeof(upa));
11987 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11988 branch_ref, base_branch_ref, update_progress, &upa,
11989 check_cancelled, NULL);
11993 printf("Integrated %s into %s\n", refname, base_refname);
11994 print_update_progress_stats(&upa);
11997 const struct got_error *close_err = got_repo_close(repo);
12002 got_worktree_close(worktree);
12004 const struct got_error *pack_err =
12005 got_repo_pack_fds_close(pack_fds);
12010 free(base_commit_id);
12013 free(base_refname);
12020 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
12025 static const struct got_error *
12026 cmd_merge(int argc, char *argv[])
12028 const struct got_error *error = NULL;
12029 struct got_worktree *worktree = NULL;
12030 struct got_repository *repo = NULL;
12031 struct got_fileindex *fileindex = NULL;
12032 char *cwd = NULL, *id_str = NULL, *author = NULL;
12033 struct got_reference *branch = NULL, *wt_branch = NULL;
12034 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12035 struct got_object_id *wt_branch_tip = NULL;
12036 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12037 int interrupt_merge = 0;
12038 struct got_update_progress_arg upa;
12039 struct got_object_id *merge_commit_id = NULL;
12040 char *branch_name = NULL;
12041 int *pack_fds = NULL;
12043 memset(&upa, 0, sizeof(upa));
12045 while ((ch = getopt(argc, argv, "acn")) != -1) {
12051 continue_merge = 1;
12054 interrupt_merge = 1;
12066 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12067 "unveil", NULL) == -1)
12071 if (abort_merge && continue_merge)
12072 option_conflict('a', 'c');
12073 if (abort_merge || continue_merge) {
12076 } else if (argc != 1)
12079 cwd = getcwd(NULL, 0);
12081 error = got_error_from_errno("getcwd");
12085 error = got_repo_pack_fds_open(&pack_fds);
12089 error = got_worktree_open(&worktree, cwd);
12091 if (error->code == GOT_ERR_NOT_WORKTREE)
12092 error = wrap_not_worktree_error(error,
12097 error = got_repo_open(&repo,
12098 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12103 error = apply_unveil(got_repo_get_path(repo), 0,
12104 worktree ? got_worktree_get_root_path(worktree) : NULL);
12108 error = check_rebase_or_histedit_in_progress(worktree);
12112 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12118 if (!merge_in_progress) {
12119 error = got_error(GOT_ERR_NOT_MERGING);
12122 error = got_worktree_merge_continue(&branch_name,
12123 &branch_tip, &fileindex, worktree, repo);
12126 error = got_worktree_merge_abort(worktree, fileindex, repo,
12127 abort_progress, &upa);
12130 printf("Merge of %s aborted\n", branch_name);
12131 goto done; /* nothing else to do */
12134 error = get_author(&author, repo, worktree);
12138 if (continue_merge) {
12139 if (!merge_in_progress) {
12140 error = got_error(GOT_ERR_NOT_MERGING);
12143 error = got_worktree_merge_continue(&branch_name,
12144 &branch_tip, &fileindex, worktree, repo);
12148 error = got_ref_open(&branch, repo, argv[0], 0);
12151 branch_name = strdup(got_ref_get_name(branch));
12152 if (branch_name == NULL) {
12153 error = got_error_from_errno("strdup");
12156 error = got_ref_resolve(&branch_tip, repo, branch);
12161 error = got_ref_open(&wt_branch, repo,
12162 got_worktree_get_head_ref_name(worktree), 0);
12165 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12168 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12169 wt_branch_tip, branch_tip, 0, repo,
12170 check_cancelled, NULL);
12171 if (error && error->code != GOT_ERR_ANCESTRY)
12174 if (!continue_merge) {
12175 error = check_path_prefix(wt_branch_tip, branch_tip,
12176 got_worktree_get_path_prefix(worktree),
12177 GOT_ERR_MERGE_PATH, repo);
12181 error = check_same_branch(wt_branch_tip, branch,
12184 if (error->code != GOT_ERR_ANCESTRY)
12188 static char msg[512];
12189 snprintf(msg, sizeof(msg),
12190 "cannot create a merge commit because "
12191 "%s is based on %s; %s can be integrated "
12192 "with 'got integrate' instead", branch_name,
12193 got_worktree_get_head_ref_name(worktree),
12195 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12199 error = got_worktree_merge_prepare(&fileindex, worktree,
12204 error = got_worktree_merge_branch(worktree, fileindex,
12205 yca_id, branch_tip, repo, update_progress, &upa,
12206 check_cancelled, NULL);
12209 print_merge_progress_stats(&upa);
12210 if (!upa.did_something) {
12211 error = got_worktree_merge_abort(worktree, fileindex,
12212 repo, abort_progress, &upa);
12215 printf("Already up-to-date\n");
12220 if (interrupt_merge) {
12221 error = got_worktree_merge_postpone(worktree, fileindex);
12224 printf("Merge of %s interrupted on request\n", branch_name);
12225 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12226 upa.not_deleted > 0 || upa.unversioned > 0) {
12227 error = got_worktree_merge_postpone(worktree, fileindex);
12230 if (upa.conflicts > 0 && upa.missing == 0 &&
12231 upa.not_deleted == 0 && upa.unversioned == 0) {
12232 error = got_error_msg(GOT_ERR_CONFLICTS,
12233 "conflicts must be resolved before merging "
12235 } else if (upa.conflicts > 0) {
12236 error = got_error_msg(GOT_ERR_CONFLICTS,
12237 "conflicts must be resolved before merging "
12238 "can continue; changes destined for some "
12239 "files were not yet merged and "
12240 "should be merged manually if required before the "
12241 "merge operation is continued");
12243 error = got_error_msg(GOT_ERR_CONFLICTS,
12244 "changes destined for some "
12245 "files were not yet merged and should be "
12246 "merged manually if required before the "
12247 "merge operation is continued");
12251 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12252 fileindex, author, NULL, 1, branch_tip, branch_name,
12253 repo, continue_merge ? print_status : NULL, NULL);
12256 error = got_worktree_merge_complete(worktree, fileindex, repo);
12259 error = got_object_id_str(&id_str, merge_commit_id);
12262 printf("Merged %s into %s: %s\n", branch_name,
12263 got_worktree_get_head_ref_name(worktree),
12269 free(merge_commit_id);
12275 got_ref_close(branch);
12277 got_ref_close(wt_branch);
12279 got_worktree_close(worktree);
12281 const struct got_error *close_err = got_repo_close(repo);
12286 const struct got_error *pack_err =
12287 got_repo_pack_fds_close(pack_fds);
12297 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12298 "[-S] [file-path ...]\n",
12303 static const struct got_error *
12304 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12305 const char *path, struct got_object_id *blob_id,
12306 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12307 int dirfd, const char *de_name)
12309 const struct got_error *err = NULL;
12310 char *id_str = NULL;
12312 if (staged_status != GOT_STATUS_ADD &&
12313 staged_status != GOT_STATUS_MODIFY &&
12314 staged_status != GOT_STATUS_DELETE)
12317 if (staged_status == GOT_STATUS_ADD ||
12318 staged_status == GOT_STATUS_MODIFY)
12319 err = got_object_id_str(&id_str, staged_blob_id);
12321 err = got_object_id_str(&id_str, blob_id);
12325 printf("%s %c %s\n", id_str, staged_status, path);
12330 static const struct got_error *
12331 cmd_stage(int argc, char *argv[])
12333 const struct got_error *error = NULL;
12334 struct got_repository *repo = NULL;
12335 struct got_worktree *worktree = NULL;
12337 struct got_pathlist_head paths;
12338 struct got_pathlist_entry *pe;
12339 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12340 FILE *patch_script_file = NULL;
12341 const char *patch_script_path = NULL;
12342 struct choose_patch_arg cpa;
12343 int *pack_fds = NULL;
12345 TAILQ_INIT(&paths);
12347 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12356 patch_script_path = optarg;
12359 allow_bad_symlinks = 1;
12371 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12372 "unveil", NULL) == -1)
12375 if (list_stage && (pflag || patch_script_path))
12376 errx(1, "-l option cannot be used with other options");
12377 if (patch_script_path && !pflag)
12378 errx(1, "-F option can only be used together with -p option");
12380 cwd = getcwd(NULL, 0);
12382 error = got_error_from_errno("getcwd");
12386 error = got_repo_pack_fds_open(&pack_fds);
12390 error = got_worktree_open(&worktree, cwd);
12392 if (error->code == GOT_ERR_NOT_WORKTREE)
12393 error = wrap_not_worktree_error(error, "stage", cwd);
12397 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12402 if (patch_script_path) {
12403 patch_script_file = fopen(patch_script_path, "re");
12404 if (patch_script_file == NULL) {
12405 error = got_error_from_errno2("fopen",
12406 patch_script_path);
12410 error = apply_unveil(got_repo_get_path(repo), 0,
12411 got_worktree_get_root_path(worktree));
12415 error = check_merge_in_progress(worktree, repo);
12419 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12424 error = got_worktree_status(worktree, &paths, repo, 0,
12425 print_stage, NULL, check_cancelled, NULL);
12427 cpa.patch_script_file = patch_script_file;
12428 cpa.action = "stage";
12429 error = got_worktree_stage(worktree, &paths,
12430 pflag ? NULL : print_status, NULL,
12431 pflag ? choose_patch : NULL, &cpa,
12432 allow_bad_symlinks, repo);
12435 if (patch_script_file && fclose(patch_script_file) == EOF &&
12437 error = got_error_from_errno2("fclose", patch_script_path);
12439 const struct got_error *close_err = got_repo_close(repo);
12444 got_worktree_close(worktree);
12446 const struct got_error *pack_err =
12447 got_repo_pack_fds_close(pack_fds);
12451 TAILQ_FOREACH(pe, &paths, entry)
12452 free((char *)pe->path);
12453 got_pathlist_free(&paths);
12459 usage_unstage(void)
12461 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12462 "[file-path ...]\n",
12468 static const struct got_error *
12469 cmd_unstage(int argc, char *argv[])
12471 const struct got_error *error = NULL;
12472 struct got_repository *repo = NULL;
12473 struct got_worktree *worktree = NULL;
12475 struct got_pathlist_head paths;
12476 struct got_pathlist_entry *pe;
12478 struct got_update_progress_arg upa;
12479 FILE *patch_script_file = NULL;
12480 const char *patch_script_path = NULL;
12481 struct choose_patch_arg cpa;
12482 int *pack_fds = NULL;
12484 TAILQ_INIT(&paths);
12486 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12492 patch_script_path = optarg;
12504 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12505 "unveil", NULL) == -1)
12508 if (patch_script_path && !pflag)
12509 errx(1, "-F option can only be used together with -p option");
12511 cwd = getcwd(NULL, 0);
12513 error = got_error_from_errno("getcwd");
12517 error = got_repo_pack_fds_open(&pack_fds);
12521 error = got_worktree_open(&worktree, cwd);
12523 if (error->code == GOT_ERR_NOT_WORKTREE)
12524 error = wrap_not_worktree_error(error, "unstage", cwd);
12528 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12533 if (patch_script_path) {
12534 patch_script_file = fopen(patch_script_path, "re");
12535 if (patch_script_file == NULL) {
12536 error = got_error_from_errno2("fopen",
12537 patch_script_path);
12542 error = apply_unveil(got_repo_get_path(repo), 0,
12543 got_worktree_get_root_path(worktree));
12547 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12551 cpa.patch_script_file = patch_script_file;
12552 cpa.action = "unstage";
12553 memset(&upa, 0, sizeof(upa));
12554 error = got_worktree_unstage(worktree, &paths, update_progress,
12555 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12557 print_merge_progress_stats(&upa);
12559 if (patch_script_file && fclose(patch_script_file) == EOF &&
12561 error = got_error_from_errno2("fclose", patch_script_path);
12563 const struct got_error *close_err = got_repo_close(repo);
12568 got_worktree_close(worktree);
12570 const struct got_error *pack_err =
12571 got_repo_pack_fds_close(pack_fds);
12575 TAILQ_FOREACH(pe, &paths, entry)
12576 free((char *)pe->path);
12577 got_pathlist_free(&paths);
12585 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12586 "arg1 [arg2 ...]\n", getprogname());
12590 static const struct got_error *
12591 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12593 const struct got_error *err;
12594 struct got_blob_object *blob;
12597 fd = got_opentempfd();
12599 return got_error_from_errno("got_opentempfd");
12601 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12605 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12607 if (fd != -1 && close(fd) == -1 && err == NULL)
12608 err = got_error_from_errno("close");
12610 got_object_blob_close(blob);
12614 static const struct got_error *
12615 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12617 const struct got_error *err;
12618 struct got_tree_object *tree;
12621 err = got_object_open_as_tree(&tree, repo, id);
12625 nentries = got_object_tree_get_nentries(tree);
12626 for (i = 0; i < nentries; i++) {
12627 struct got_tree_entry *te;
12629 if (sigint_received || sigpipe_received)
12631 te = got_object_tree_get_entry(tree, i);
12632 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12635 fprintf(outfile, "%s %.7o %s\n", id_str,
12636 got_tree_entry_get_mode(te),
12637 got_tree_entry_get_name(te));
12641 got_object_tree_close(tree);
12645 static const struct got_error *
12646 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12648 const struct got_error *err;
12649 struct got_commit_object *commit;
12650 const struct got_object_id_queue *parent_ids;
12651 struct got_object_qid *pid;
12652 char *id_str = NULL;
12653 const char *logmsg = NULL;
12656 err = got_object_open_as_commit(&commit, repo, id);
12660 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12664 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12665 parent_ids = got_object_commit_get_parent_ids(commit);
12666 fprintf(outfile, "numparents %d\n",
12667 got_object_commit_get_nparents(commit));
12668 STAILQ_FOREACH(pid, parent_ids, entry) {
12670 err = got_object_id_str(&pid_str, &pid->id);
12673 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12676 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12677 got_object_commit_get_author_gmtoff(commit));
12678 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12679 got_object_commit_get_author(commit),
12680 (long long)got_object_commit_get_author_time(commit),
12683 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12684 got_object_commit_get_committer_gmtoff(commit));
12685 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12686 got_object_commit_get_committer(commit),
12687 (long long)got_object_commit_get_committer_time(commit),
12690 logmsg = got_object_commit_get_logmsg_raw(commit);
12691 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12692 fprintf(outfile, "%s", logmsg);
12695 got_object_commit_close(commit);
12699 static const struct got_error *
12700 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12702 const struct got_error *err;
12703 struct got_tag_object *tag;
12704 char *id_str = NULL;
12705 const char *tagmsg = NULL;
12708 err = got_object_open_as_tag(&tag, repo, id);
12712 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12716 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12718 switch (got_object_tag_get_object_type(tag)) {
12719 case GOT_OBJ_TYPE_BLOB:
12720 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12721 GOT_OBJ_LABEL_BLOB);
12723 case GOT_OBJ_TYPE_TREE:
12724 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12725 GOT_OBJ_LABEL_TREE);
12727 case GOT_OBJ_TYPE_COMMIT:
12728 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12729 GOT_OBJ_LABEL_COMMIT);
12731 case GOT_OBJ_TYPE_TAG:
12732 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12733 GOT_OBJ_LABEL_TAG);
12739 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12740 got_object_tag_get_name(tag));
12742 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12743 got_object_tag_get_tagger_gmtoff(tag));
12744 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12745 got_object_tag_get_tagger(tag),
12746 (long long)got_object_tag_get_tagger_time(tag),
12749 tagmsg = got_object_tag_get_message(tag);
12750 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12751 fprintf(outfile, "%s", tagmsg);
12754 got_object_tag_close(tag);
12758 static const struct got_error *
12759 cmd_cat(int argc, char *argv[])
12761 const struct got_error *error;
12762 struct got_repository *repo = NULL;
12763 struct got_worktree *worktree = NULL;
12764 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12765 const char *commit_id_str = NULL;
12766 struct got_object_id *id = NULL, *commit_id = NULL;
12767 struct got_commit_object *commit = NULL;
12768 int ch, obj_type, i, force_path = 0;
12769 struct got_reflist_head refs;
12770 int *pack_fds = NULL;
12775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12780 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12783 commit_id_str = optarg;
12786 repo_path = realpath(optarg, NULL);
12787 if (repo_path == NULL)
12788 return got_error_from_errno2("realpath",
12790 got_path_strip_trailing_slashes(repo_path);
12804 cwd = getcwd(NULL, 0);
12806 error = got_error_from_errno("getcwd");
12810 error = got_repo_pack_fds_open(&pack_fds);
12814 if (repo_path == NULL) {
12815 error = got_worktree_open(&worktree, cwd);
12816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12819 repo_path = strdup(
12820 got_worktree_get_repo_path(worktree));
12821 if (repo_path == NULL) {
12822 error = got_error_from_errno("strdup");
12826 /* Release work tree lock. */
12827 got_worktree_close(worktree);
12832 if (repo_path == NULL) {
12833 repo_path = strdup(cwd);
12834 if (repo_path == NULL)
12835 return got_error_from_errno("strdup");
12838 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12843 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12847 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12851 if (commit_id_str == NULL)
12852 commit_id_str = GOT_REF_HEAD;
12853 error = got_repo_match_object_id(&commit_id, NULL,
12854 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12858 error = got_object_open_as_commit(&commit, repo, commit_id);
12862 for (i = 0; i < argc; i++) {
12864 error = got_object_id_by_path(&id, repo, commit,
12869 error = got_repo_match_object_id(&id, &label, argv[i],
12870 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12873 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12874 error->code != GOT_ERR_NOT_REF)
12876 error = got_object_id_by_path(&id, repo,
12883 error = got_object_get_type(&obj_type, repo, id);
12887 switch (obj_type) {
12888 case GOT_OBJ_TYPE_BLOB:
12889 error = cat_blob(id, repo, stdout);
12891 case GOT_OBJ_TYPE_TREE:
12892 error = cat_tree(id, repo, stdout);
12894 case GOT_OBJ_TYPE_COMMIT:
12895 error = cat_commit(id, repo, stdout);
12897 case GOT_OBJ_TYPE_TAG:
12898 error = cat_tag(id, repo, stdout);
12901 error = got_error(GOT_ERR_OBJ_TYPE);
12916 got_object_commit_close(commit);
12918 got_worktree_close(worktree);
12920 const struct got_error *close_err = got_repo_close(repo);
12925 const struct got_error *pack_err =
12926 got_repo_pack_fds_close(pack_fds);
12931 got_ref_list_free(&refs);
12938 fprintf(stderr, "usage: %s info [path ...]\n",
12943 static const struct got_error *
12944 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12945 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12946 struct got_object_id *commit_id)
12948 const struct got_error *err = NULL;
12949 char *id_str = NULL;
12951 struct tm mytm, *tm;
12952 struct got_pathlist_head *paths = arg;
12953 struct got_pathlist_entry *pe;
12956 * Clear error indication from any of the path arguments which
12957 * would cause this file index entry to be displayed.
12959 TAILQ_FOREACH(pe, paths, entry) {
12960 if (got_path_cmp(path, pe->path, strlen(path),
12961 pe->path_len) == 0 ||
12962 got_path_is_child(path, pe->path, pe->path_len))
12963 pe->data = NULL; /* no error */
12966 printf(GOT_COMMIT_SEP_STR);
12968 printf("symlink: %s\n", path);
12969 else if (S_ISREG(mode)) {
12970 printf("file: %s\n", path);
12971 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12972 } else if (S_ISDIR(mode))
12973 printf("directory: %s\n", path);
12975 printf("something: %s\n", path);
12977 tm = localtime_r(&mtime, &mytm);
12980 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12981 return got_error(GOT_ERR_NO_SPACE);
12982 printf("timestamp: %s\n", datebuf);
12985 err = got_object_id_str(&id_str, blob_id);
12988 printf("based on blob: %s\n", id_str);
12992 if (staged_blob_id) {
12993 err = got_object_id_str(&id_str, staged_blob_id);
12996 printf("based on staged blob: %s\n", id_str);
13001 err = got_object_id_str(&id_str, commit_id);
13004 printf("based on commit: %s\n", id_str);
13011 static const struct got_error *
13012 cmd_info(int argc, char *argv[])
13014 const struct got_error *error = NULL;
13015 struct got_worktree *worktree = NULL;
13016 char *cwd = NULL, *id_str = NULL;
13017 struct got_pathlist_head paths;
13018 struct got_pathlist_entry *pe;
13019 char *uuidstr = NULL;
13020 int ch, show_files = 0;
13022 TAILQ_INIT(&paths);
13024 while ((ch = getopt(argc, argv, "")) != -1) {
13036 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13040 cwd = getcwd(NULL, 0);
13042 error = got_error_from_errno("getcwd");
13046 error = got_worktree_open(&worktree, cwd);
13048 if (error->code == GOT_ERR_NOT_WORKTREE)
13049 error = wrap_not_worktree_error(error, "info", cwd);
13054 /* Remove "wpath cpath proc exec sendfd" promises. */
13055 if (pledge("stdio rpath flock unveil", NULL) == -1)
13058 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13063 error = get_worktree_paths_from_argv(&paths, argc, argv,
13070 error = got_object_id_str(&id_str,
13071 got_worktree_get_base_commit_id(worktree));
13075 error = got_worktree_get_uuid(&uuidstr, worktree);
13079 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13080 printf("work tree base commit: %s\n", id_str);
13081 printf("work tree path prefix: %s\n",
13082 got_worktree_get_path_prefix(worktree));
13083 printf("work tree branch reference: %s\n",
13084 got_worktree_get_head_ref_name(worktree));
13085 printf("work tree UUID: %s\n", uuidstr);
13086 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13089 struct got_pathlist_entry *pe;
13090 TAILQ_FOREACH(pe, &paths, entry) {
13091 if (pe->path_len == 0)
13094 * Assume this path will fail. This will be corrected
13095 * in print_path_info() in case the path does suceeed.
13097 pe->data = (void *)got_error_path(pe->path,
13100 error = got_worktree_path_info(worktree, &paths,
13101 print_path_info, &paths, check_cancelled, NULL);
13104 TAILQ_FOREACH(pe, &paths, entry) {
13105 if (pe->data != NULL) {
13106 error = pe->data; /* bad path */
13113 got_worktree_close(worktree);
13114 TAILQ_FOREACH(pe, &paths, entry)
13115 free((char *)pe->path);
13116 got_pathlist_free(&paths);