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>
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_worktree.h"
54 #include "got_commit_graph.h"
55 #include "got_fetch.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_opentemp.h"
60 #include "got_gotconfig.h"
62 #include "got_patch.h"
65 #include "got_keyword.h"
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #ifndef GOT_DEFAULT_EDITOR
72 #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
75 static volatile sig_atomic_t sigint_received;
76 static volatile sig_atomic_t sigpipe_received;
79 catch_sigint(int signo)
85 catch_sigpipe(int signo)
93 const struct got_error *(*cmd_main)(int, char *[]);
94 void (*cmd_usage)(void);
95 const char *cmd_alias;
98 __dead static void usage(int, int);
99 __dead static void usage_import(void);
100 __dead static void usage_clone(void);
101 __dead static void usage_fetch(void);
102 __dead static void usage_checkout(void);
103 __dead static void usage_update(void);
104 __dead static void usage_log(void);
105 __dead static void usage_diff(void);
106 __dead static void usage_blame(void);
107 __dead static void usage_tree(void);
108 __dead static void usage_status(void);
109 __dead static void usage_ref(void);
110 __dead static void usage_branch(void);
111 __dead static void usage_tag(void);
112 __dead static void usage_add(void);
113 __dead static void usage_remove(void);
114 __dead static void usage_patch(void);
115 __dead static void usage_revert(void);
116 __dead static void usage_commit(void);
117 __dead static void usage_send(void);
118 __dead static void usage_cherrypick(void);
119 __dead static void usage_backout(void);
120 __dead static void usage_rebase(void);
121 __dead static void usage_histedit(void);
122 __dead static void usage_integrate(void);
123 __dead static void usage_merge(void);
124 __dead static void usage_stage(void);
125 __dead static void usage_unstage(void);
126 __dead static void usage_cat(void);
127 __dead static void usage_info(void);
129 static const struct got_error* cmd_import(int, char *[]);
130 static const struct got_error* cmd_clone(int, char *[]);
131 static const struct got_error* cmd_fetch(int, char *[]);
132 static const struct got_error* cmd_checkout(int, char *[]);
133 static const struct got_error* cmd_update(int, char *[]);
134 static const struct got_error* cmd_log(int, char *[]);
135 static const struct got_error* cmd_diff(int, char *[]);
136 static const struct got_error* cmd_blame(int, char *[]);
137 static const struct got_error* cmd_tree(int, char *[]);
138 static const struct got_error* cmd_status(int, char *[]);
139 static const struct got_error* cmd_ref(int, char *[]);
140 static const struct got_error* cmd_branch(int, char *[]);
141 static const struct got_error* cmd_tag(int, char *[]);
142 static const struct got_error* cmd_add(int, char *[]);
143 static const struct got_error* cmd_remove(int, char *[]);
144 static const struct got_error* cmd_patch(int, char *[]);
145 static const struct got_error* cmd_revert(int, char *[]);
146 static const struct got_error* cmd_commit(int, char *[]);
147 static const struct got_error* cmd_send(int, char *[]);
148 static const struct got_error* cmd_cherrypick(int, char *[]);
149 static const struct got_error* cmd_backout(int, char *[]);
150 static const struct got_error* cmd_rebase(int, char *[]);
151 static const struct got_error* cmd_histedit(int, char *[]);
152 static const struct got_error* cmd_integrate(int, char *[]);
153 static const struct got_error* cmd_merge(int, char *[]);
154 static const struct got_error* cmd_stage(int, char *[]);
155 static const struct got_error* cmd_unstage(int, char *[]);
156 static const struct got_error* cmd_cat(int, char *[]);
157 static const struct got_error* cmd_info(int, char *[]);
159 static const struct got_cmd got_commands[] = {
160 { "import", cmd_import, usage_import, "im" },
161 { "clone", cmd_clone, usage_clone, "cl" },
162 { "fetch", cmd_fetch, usage_fetch, "fe" },
163 { "checkout", cmd_checkout, usage_checkout, "co" },
164 { "update", cmd_update, usage_update, "up" },
165 { "log", cmd_log, usage_log, "" },
166 { "diff", cmd_diff, usage_diff, "di" },
167 { "blame", cmd_blame, usage_blame, "bl" },
168 { "tree", cmd_tree, usage_tree, "tr" },
169 { "status", cmd_status, usage_status, "st" },
170 { "ref", cmd_ref, usage_ref, "" },
171 { "branch", cmd_branch, usage_branch, "br" },
172 { "tag", cmd_tag, usage_tag, "" },
173 { "add", cmd_add, usage_add, "" },
174 { "remove", cmd_remove, usage_remove, "rm" },
175 { "patch", cmd_patch, usage_patch, "pa" },
176 { "revert", cmd_revert, usage_revert, "rv" },
177 { "commit", cmd_commit, usage_commit, "ci" },
178 { "send", cmd_send, usage_send, "se" },
179 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
180 { "backout", cmd_backout, usage_backout, "bo" },
181 { "rebase", cmd_rebase, usage_rebase, "rb" },
182 { "histedit", cmd_histedit, usage_histedit, "he" },
183 { "integrate", cmd_integrate, usage_integrate,"ig" },
184 { "merge", cmd_merge, usage_merge, "mg" },
185 { "stage", cmd_stage, usage_stage, "sg" },
186 { "unstage", cmd_unstage, usage_unstage, "ug" },
187 { "cat", cmd_cat, usage_cat, "" },
188 { "info", cmd_info, usage_info, "" },
192 list_commands(FILE *fp)
196 fprintf(fp, "commands:");
197 for (i = 0; i < nitems(got_commands); i++) {
198 const struct got_cmd *cmd = &got_commands[i];
199 fprintf(fp, " %s", cmd->cmd_name);
205 option_conflict(char a, char b)
207 errx(1, "-%c and -%c options are mutually exclusive", a, b);
211 main(int argc, char *argv[])
213 const struct got_cmd *cmd;
216 int hflag = 0, Vflag = 0;
217 static const struct option longopts[] = {
218 { "version", no_argument, NULL, 'V' },
222 setlocale(LC_CTYPE, "");
224 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
244 got_version_print_str();
249 usage(hflag, hflag ? 0 : 1);
251 signal(SIGINT, catch_sigint);
252 signal(SIGPIPE, catch_sigpipe);
254 for (i = 0; i < nitems(got_commands); i++) {
255 const struct got_error *error;
257 cmd = &got_commands[i];
259 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
260 strcmp(cmd->cmd_alias, argv[0]) != 0)
266 error = cmd->cmd_main(argc, argv);
267 if (error && error->code != GOT_ERR_CANCELLED &&
268 error->code != GOT_ERR_PRIVSEP_EXIT &&
269 !(sigpipe_received &&
270 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
272 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
274 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
281 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
282 list_commands(stderr);
287 usage(int hflag, int status)
289 FILE *fp = (status == 0) ? stdout : stderr;
291 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
298 static const struct got_error *
299 get_editor(char **abspath)
301 const struct got_error *err = NULL;
306 editor = getenv("VISUAL");
308 editor = getenv("EDITOR");
311 err = got_path_find_prog(abspath, editor);
316 if (*abspath == NULL) {
317 *abspath = strdup(GOT_DEFAULT_EDITOR);
318 if (*abspath == NULL)
319 return got_error_from_errno("strdup");
325 static const struct got_error *
326 apply_unveil(const char *repo_path, int repo_read_only,
327 const char *worktree_path)
329 const struct got_error *err;
332 if (unveil("gmon.out", "rwc") != 0)
333 return got_error_from_errno2("unveil", "gmon.out");
335 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
336 return got_error_from_errno2("unveil", repo_path);
338 if (worktree_path && unveil(worktree_path, "rwc") != 0)
339 return got_error_from_errno2("unveil", worktree_path);
341 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
342 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
344 err = got_privsep_unveil_exec_helpers();
348 if (unveil(NULL, NULL) != 0)
349 return got_error_from_errno("unveil");
357 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
358 "[-r repository-path] directory\n", getprogname());
363 spawn_editor(const char *editor, const char *file)
366 sig_t sighup, sigint, sigquit;
369 sighup = signal(SIGHUP, SIG_IGN);
370 sigint = signal(SIGINT, SIG_IGN);
371 sigquit = signal(SIGQUIT, SIG_IGN);
373 switch (pid = fork()) {
377 execl(editor, editor, file, (char *)NULL);
381 while (waitpid(pid, &st, 0) == -1)
386 (void)signal(SIGHUP, sighup);
387 (void)signal(SIGINT, sigint);
388 (void)signal(SIGQUIT, sigquit);
390 if (!WIFEXITED(st)) {
395 return WEXITSTATUS(st);
398 static const struct got_error *
399 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
401 const struct got_error *err = NULL;
408 if (fseeko(fp, 0L, SEEK_SET) == -1)
409 return got_error_from_errno("fseeko");
411 *logmsg = malloc(filesize + 1);
413 return got_error_from_errno("malloc");
416 while (getline(&line, &linesize, fp) != -1) {
417 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
418 continue; /* remove comments and leading empty lines */
419 *len = strlcat(*logmsg, line, filesize + 1);
420 if (*len >= filesize + 1) {
421 err = got_error(GOT_ERR_NO_SPACE);
426 err = got_ferror(fp, GOT_ERR_IO);
430 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
431 (*logmsg)[*len - 1] = '\0';
444 static const struct got_error *
445 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
446 const char *initial_content, size_t initial_content_len,
447 int require_modification)
449 const struct got_error *err = NULL;
456 if (stat(logmsg_path, &st) == -1)
457 return got_error_from_errno2("stat", logmsg_path);
459 if (spawn_editor(editor, logmsg_path) == -1)
460 return got_error_from_errno("failed spawning editor");
462 if (require_modification) {
463 struct timespec timeout;
467 nanosleep(&timeout, NULL);
470 if (stat(logmsg_path, &st2) == -1)
471 return got_error_from_errno2("stat", logmsg_path);
473 if (require_modification && st.st_size == st2.st_size &&
474 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
475 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 "no changes made to commit message, aborting");
478 fp = fopen(logmsg_path, "re");
480 err = got_error_from_errno("fopen");
484 /* strip comments and leading/trailing newlines */
485 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
488 if (logmsg_len == 0) {
489 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
490 "commit message cannot be empty, aborting");
494 if (fp && fclose(fp) == EOF && err == NULL)
495 err = got_error_from_errno("fclose");
503 static const struct got_error *
504 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
505 const char *path_dir, const char *branch_name)
507 char *initial_content = NULL;
508 const struct got_error *err = NULL;
509 int initial_content_len;
512 initial_content_len = asprintf(&initial_content,
513 "\n# %s to be imported to branch %s\n", path_dir,
515 if (initial_content_len == -1)
516 return got_error_from_errno("asprintf");
518 err = got_opentemp_named_fd(logmsg_path, &fd,
519 GOT_TMPDIR_STR "/got-importmsg", "");
523 if (write(fd, initial_content, initial_content_len) == -1) {
524 err = got_error_from_errno2("write", *logmsg_path);
527 if (close(fd) == -1) {
528 err = got_error_from_errno2("close", *logmsg_path);
533 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
534 initial_content_len, 1);
536 if (fd != -1 && close(fd) == -1 && err == NULL)
537 err = got_error_from_errno2("close", *logmsg_path);
538 free(initial_content);
546 static const struct got_error *
547 import_progress(void *arg, const char *path)
549 printf("A %s\n", path);
553 static const struct got_error *
554 valid_author(const char *author)
556 const char *email = author;
559 * Git' expects the author (or committer) to be in the form
560 * "name <email>", which are mostly free form (see the
561 * "committer" description in git-fast-import(1)). We're only
562 * doing this to avoid git's object parser breaking on commits
566 while (*author && *author != '\n' && *author != '<' && *author != '>')
568 if (author != email && *author == '<' && *(author - 1) != ' ')
569 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
570 "between author name and email required", email);
571 if (*author++ != '<')
572 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
573 while (*author && *author != '\n' && *author != '<' && *author != '>')
575 if (strcmp(author, ">") != 0)
576 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
580 static const struct got_error *
581 get_author(char **author, struct got_repository *repo,
582 struct got_worktree *worktree)
584 const struct got_error *err = NULL;
585 const char *got_author = NULL, *name, *email;
586 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
591 worktree_conf = got_worktree_get_gotconfig(worktree);
592 repo_conf = got_repo_get_gotconfig(repo);
595 * Priority of potential author information sources, from most
596 * significant to least significant:
597 * 1) work tree's .got/got.conf file
598 * 2) repository's got.conf file
599 * 3) repository's git config file
600 * 4) environment variables
601 * 5) global git config files (in user's home directory or /etc)
605 got_author = got_gotconfig_get_author(worktree_conf);
606 if (got_author == NULL)
607 got_author = got_gotconfig_get_author(repo_conf);
608 if (got_author == NULL) {
609 name = got_repo_get_gitconfig_author_name(repo);
610 email = got_repo_get_gitconfig_author_email(repo);
612 if (asprintf(author, "%s <%s>", name, email) == -1)
613 return got_error_from_errno("asprintf");
617 got_author = getenv("GOT_AUTHOR");
618 if (got_author == NULL) {
619 name = got_repo_get_global_gitconfig_author_name(repo);
620 email = got_repo_get_global_gitconfig_author_email(
623 if (asprintf(author, "%s <%s>", name, email)
625 return got_error_from_errno("asprintf");
628 /* TODO: Look up user in password database? */
629 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
633 *author = strdup(got_author);
635 return got_error_from_errno("strdup");
637 err = valid_author(*author);
645 static const struct got_error *
646 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
647 struct got_worktree *worktree)
649 const char *got_allowed_signers = NULL;
650 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
652 *allowed_signers = NULL;
655 worktree_conf = got_worktree_get_gotconfig(worktree);
656 repo_conf = got_repo_get_gotconfig(repo);
659 * Priority of potential author information sources, from most
660 * significant to least significant:
661 * 1) work tree's .got/got.conf file
662 * 2) repository's got.conf file
666 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
668 if (got_allowed_signers == NULL)
669 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
672 if (got_allowed_signers) {
673 *allowed_signers = strdup(got_allowed_signers);
674 if (*allowed_signers == NULL)
675 return got_error_from_errno("strdup");
680 static const struct got_error *
681 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
682 struct got_worktree *worktree)
684 const char *got_revoked_signers = NULL;
685 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
687 *revoked_signers = NULL;
690 worktree_conf = got_worktree_get_gotconfig(worktree);
691 repo_conf = got_repo_get_gotconfig(repo);
694 * Priority of potential author information sources, from most
695 * significant to least significant:
696 * 1) work tree's .got/got.conf file
697 * 2) repository's got.conf file
701 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
703 if (got_revoked_signers == NULL)
704 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
707 if (got_revoked_signers) {
708 *revoked_signers = strdup(got_revoked_signers);
709 if (*revoked_signers == NULL)
710 return got_error_from_errno("strdup");
716 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
718 const char *got_signer_id = NULL;
719 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
722 worktree_conf = got_worktree_get_gotconfig(worktree);
723 repo_conf = got_repo_get_gotconfig(repo);
726 * Priority of potential author information sources, from most
727 * significant to least significant:
728 * 1) work tree's .got/got.conf file
729 * 2) repository's got.conf file
733 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
734 if (got_signer_id == NULL)
735 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
737 return got_signer_id;
740 static const struct got_error *
741 get_gitconfig_path(char **gitconfig_path)
743 const char *homedir = getenv("HOME");
745 *gitconfig_path = NULL;
747 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
748 return got_error_from_errno("asprintf");
754 static const struct got_error *
755 cmd_import(int argc, char *argv[])
757 const struct got_error *error = NULL;
758 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
759 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
760 const char *branch_name = NULL;
761 char *id_str = NULL, *logmsg_path = NULL;
762 char refname[PATH_MAX] = "refs/heads/";
763 struct got_repository *repo = NULL;
764 struct got_reference *branch_ref = NULL, *head_ref = NULL;
765 struct got_object_id *new_commit_id = NULL;
767 struct got_pathlist_head ignores;
768 struct got_pathlist_entry *pe;
769 int preserve_logmsg = 0;
770 int *pack_fds = NULL;
772 TAILQ_INIT(&ignores);
775 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
781 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
784 branch_name = optarg;
787 if (optarg[0] == '\0')
789 error = got_pathlist_insert(&pe, &ignores, optarg,
795 logmsg = strdup(optarg);
796 if (logmsg == NULL) {
797 error = got_error_from_errno("strdup");
802 repo_path = realpath(optarg, NULL);
803 if (repo_path == NULL) {
804 error = got_error_from_errno2("realpath",
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 path_dir = realpath(argv[0], NULL);
838 if (path_dir == NULL) {
839 error = got_error_from_errno2("realpath", argv[0]);
842 got_path_strip_trailing_slashes(path_dir);
844 error = get_editor(&editor);
848 if (unveil(path_dir, "r") != 0) {
849 error = got_error_from_errno2("unveil", path_dir);
852 if (unveil(editor, "x") != 0) {
853 error = got_error_from_errno2("unveil", editor);
856 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
860 error = get_author(&author, repo, NULL);
865 * Don't let the user create a branch name with a leading '-'.
866 * While technically a valid reference name, this case is usually
867 * an unintended typo.
869 if (branch_name && branch_name[0] == '-')
870 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
872 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
873 if (error && error->code != GOT_ERR_NOT_REF)
877 n = strlcat(refname, branch_name, sizeof(refname));
878 else if (head_ref && got_ref_is_symbolic(head_ref))
879 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
882 n = strlcat(refname, "main", sizeof(refname));
883 if (n >= sizeof(refname)) {
884 error = got_error(GOT_ERR_NO_SPACE);
888 error = got_ref_open(&branch_ref, repo, refname, 0);
890 if (error->code != GOT_ERR_NOT_REF)
893 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
894 "import target branch already exists");
898 if (logmsg == NULL || *logmsg == '\0') {
900 error = collect_import_msg(&logmsg, &logmsg_path, editor,
903 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
910 error = got_repo_import(&new_commit_id, path_dir, logmsg,
911 author, &ignores, repo, import_progress, NULL);
918 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
925 error = got_ref_write(branch_ref, repo);
932 error = got_object_id_str(&id_str, new_commit_id);
939 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
941 if (error->code != GOT_ERR_NOT_REF) {
947 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
955 error = got_ref_write(head_ref, repo);
963 printf("Created branch %s with commit %s\n",
964 got_ref_get_name(branch_ref), id_str);
967 const struct got_error *pack_err =
968 got_repo_pack_fds_close(pack_fds);
973 const struct got_error *close_err = got_repo_close(repo);
977 if (preserve_logmsg) {
978 fprintf(stderr, "%s: log message preserved in %s\n",
979 getprogname(), logmsg_path);
980 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
981 error = got_error_from_errno2("unlink", logmsg_path);
989 free(gitconfig_path);
991 got_ref_close(branch_ref);
993 got_ref_close(head_ref);
1000 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1001 "repository-URL [directory]\n", getprogname());
1005 struct got_fetch_progress_arg {
1006 char last_scaled_size[FMT_SCALED_STRSIZE];
1008 int last_p_resolved;
1011 struct got_repository *repo;
1014 int configs_created;
1016 struct got_pathlist_head *symrefs;
1017 struct got_pathlist_head *wanted_branches;
1018 struct got_pathlist_head *wanted_refs;
1022 const char *remote_repo_path;
1023 const char *git_url;
1024 int fetch_all_branches;
1025 int mirror_references;
1029 /* XXX forward declaration */
1030 static const struct got_error *
1031 create_config_files(const char *proto, const char *host, const char *port,
1032 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1033 int mirror_references, struct got_pathlist_head *symrefs,
1034 struct got_pathlist_head *wanted_branches,
1035 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1037 static const struct got_error *
1038 fetch_progress(void *arg, const char *message, off_t packfile_size,
1039 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1041 const struct got_error *err = NULL;
1042 struct got_fetch_progress_arg *a = arg;
1043 char scaled_size[FMT_SCALED_STRSIZE];
1044 int p_indexed, p_resolved;
1045 int print_size = 0, print_indexed = 0, print_resolved = 0;
1048 * In order to allow a failed clone to be resumed with 'got fetch'
1049 * we try to create configuration files as soon as possible.
1050 * Once the server has sent information about its default branch
1051 * we have all required information.
1053 if (a->create_configs && !a->configs_created &&
1054 !TAILQ_EMPTY(a->config_info.symrefs)) {
1055 err = create_config_files(a->config_info.proto,
1056 a->config_info.host, a->config_info.port,
1057 a->config_info.remote_repo_path,
1058 a->config_info.git_url,
1059 a->config_info.fetch_all_branches,
1060 a->config_info.mirror_references,
1061 a->config_info.symrefs,
1062 a->config_info.wanted_branches,
1063 a->config_info.wanted_refs, a->repo);
1066 a->configs_created = 1;
1069 if (a->verbosity < 0)
1072 if (message && message[0] != '\0') {
1073 printf("\rserver: %s", message);
1078 if (packfile_size > 0 || nobj_indexed > 0) {
1079 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1080 (a->last_scaled_size[0] == '\0' ||
1081 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1083 if (strlcpy(a->last_scaled_size, scaled_size,
1084 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1085 return got_error(GOT_ERR_NO_SPACE);
1087 if (nobj_indexed > 0) {
1088 p_indexed = (nobj_indexed * 100) / nobj_total;
1089 if (p_indexed != a->last_p_indexed) {
1090 a->last_p_indexed = p_indexed;
1095 if (nobj_resolved > 0) {
1096 p_resolved = (nobj_resolved * 100) /
1097 (nobj_total - nobj_loose);
1098 if (p_resolved != a->last_p_resolved) {
1099 a->last_p_resolved = p_resolved;
1107 if (print_size || print_indexed || print_resolved)
1110 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1112 printf("; indexing %d%%", p_indexed);
1114 printf("; resolving deltas %d%%", p_resolved);
1115 if (print_size || print_indexed || print_resolved)
1121 static const struct got_error *
1122 create_symref(const char *refname, struct got_reference *target_ref,
1123 int verbosity, struct got_repository *repo)
1125 const struct got_error *err;
1126 struct got_reference *head_symref;
1128 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1132 err = got_ref_write(head_symref, repo);
1133 if (err == NULL && verbosity > 0) {
1134 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1135 got_ref_get_name(target_ref));
1137 got_ref_close(head_symref);
1141 static const struct got_error *
1142 list_remote_refs(struct got_pathlist_head *symrefs,
1143 struct got_pathlist_head *refs)
1145 const struct got_error *err;
1146 struct got_pathlist_entry *pe;
1148 TAILQ_FOREACH(pe, symrefs, entry) {
1149 const char *refname = pe->path;
1150 const char *targetref = pe->data;
1152 printf("%s: %s\n", refname, targetref);
1155 TAILQ_FOREACH(pe, refs, entry) {
1156 const char *refname = pe->path;
1157 struct got_object_id *id = pe->data;
1160 err = got_object_id_str(&id_str, id);
1163 printf("%s: %s\n", refname, id_str);
1170 static const struct got_error *
1171 create_ref(const char *refname, struct got_object_id *id,
1172 int verbosity, struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_reference *ref;
1178 err = got_object_id_str(&id_str, id);
1182 err = got_ref_alloc(&ref, refname, id);
1186 err = got_ref_write(ref, repo);
1189 if (err == NULL && verbosity >= 0)
1190 printf("Created reference %s: %s\n", refname, id_str);
1197 match_wanted_ref(const char *refname, const char *wanted_ref)
1199 if (strncmp(refname, "refs/", 5) != 0)
1204 * Prevent fetching of references that won't make any
1205 * sense outside of the remote repository's context.
1207 if (strncmp(refname, "got/", 4) == 0)
1209 if (strncmp(refname, "remotes/", 8) == 0)
1212 if (strncmp(wanted_ref, "refs/", 5) == 0)
1215 /* Allow prefix match. */
1216 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1219 /* Allow exact match. */
1220 return (strcmp(refname, wanted_ref) == 0);
1224 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1226 struct got_pathlist_entry *pe;
1228 TAILQ_FOREACH(pe, wanted_refs, entry) {
1229 if (match_wanted_ref(refname, pe->path))
1236 static const struct got_error *
1237 create_wanted_ref(const char *refname, struct got_object_id *id,
1238 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1240 const struct got_error *err;
1241 char *remote_refname;
1243 if (strncmp("refs/", refname, 5) == 0)
1246 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1247 remote_repo_name, refname) == -1)
1248 return got_error_from_errno("asprintf");
1250 err = create_ref(remote_refname, id, verbosity, repo);
1251 free(remote_refname);
1255 static const struct got_error *
1256 create_gotconfig(const char *proto, const char *host, const char *port,
1257 const char *remote_repo_path, const char *default_branch,
1258 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1259 struct got_pathlist_head *wanted_refs, int mirror_references,
1260 struct got_repository *repo)
1262 const struct got_error *err = NULL;
1263 char *gotconfig_path = NULL;
1264 char *gotconfig = NULL;
1265 FILE *gotconfig_file = NULL;
1266 const char *branchname = NULL;
1267 char *branches = NULL, *refs = NULL;
1270 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1271 struct got_pathlist_entry *pe;
1272 TAILQ_FOREACH(pe, wanted_branches, entry) {
1274 branchname = pe->path;
1275 if (strncmp(branchname, "refs/heads/", 11) == 0)
1277 if (asprintf(&s, "%s\"%s\" ",
1278 branches ? branches : "", branchname) == -1) {
1279 err = got_error_from_errno("asprintf");
1285 } else if (!fetch_all_branches && default_branch) {
1286 branchname = default_branch;
1287 if (strncmp(branchname, "refs/heads/", 11) == 0)
1289 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1290 err = got_error_from_errno("asprintf");
1294 if (!TAILQ_EMPTY(wanted_refs)) {
1295 struct got_pathlist_entry *pe;
1296 TAILQ_FOREACH(pe, wanted_refs, entry) {
1298 const char *refname = pe->path;
1299 if (strncmp(refname, "refs/", 5) == 0)
1301 if (asprintf(&s, "%s\"%s\" ",
1302 refs ? refs : "", refname) == -1) {
1303 err = got_error_from_errno("asprintf");
1311 /* Create got.conf(5). */
1312 gotconfig_path = got_repo_get_path_gotconfig(repo);
1313 if (gotconfig_path == NULL) {
1314 err = got_error_from_errno("got_repo_get_path_gotconfig");
1317 gotconfig_file = fopen(gotconfig_path, "ae");
1318 if (gotconfig_file == NULL) {
1319 err = got_error_from_errno2("fopen", gotconfig_path);
1322 if (asprintf(&gotconfig,
1327 "\trepository \"%s\"\n"
1333 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1334 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1335 remote_repo_path, branches ? "\tbranch { " : "",
1336 branches ? branches : "", branches ? "}\n" : "",
1337 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1338 mirror_references ? "\tmirror_references yes\n" : "",
1339 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1340 err = got_error_from_errno("asprintf");
1343 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1344 if (n != strlen(gotconfig)) {
1345 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1350 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1351 err = got_error_from_errno2("fclose", gotconfig_path);
1352 free(gotconfig_path);
1357 static const struct got_error *
1358 create_gitconfig(const char *git_url, const char *default_branch,
1359 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1360 struct got_pathlist_head *wanted_refs, int mirror_references,
1361 struct got_repository *repo)
1363 const struct got_error *err = NULL;
1364 char *gitconfig_path = NULL;
1365 char *gitconfig = NULL;
1366 FILE *gitconfig_file = NULL;
1367 char *branches = NULL, *refs = NULL;
1368 const char *branchname;
1371 /* Create a config file Git can understand. */
1372 gitconfig_path = got_repo_get_path_gitconfig(repo);
1373 if (gitconfig_path == NULL) {
1374 err = got_error_from_errno("got_repo_get_path_gitconfig");
1377 gitconfig_file = fopen(gitconfig_path, "ae");
1378 if (gitconfig_file == NULL) {
1379 err = got_error_from_errno2("fopen", gitconfig_path);
1382 if (fetch_all_branches) {
1383 if (mirror_references) {
1384 if (asprintf(&branches,
1385 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1386 err = got_error_from_errno("asprintf");
1389 } else if (asprintf(&branches,
1390 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1391 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1392 err = got_error_from_errno("asprintf");
1395 } else if (!TAILQ_EMPTY(wanted_branches)) {
1396 struct got_pathlist_entry *pe;
1397 TAILQ_FOREACH(pe, wanted_branches, entry) {
1399 branchname = pe->path;
1400 if (strncmp(branchname, "refs/heads/", 11) == 0)
1402 if (mirror_references) {
1404 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1405 branches ? branches : "",
1406 branchname, branchname) == -1) {
1407 err = got_error_from_errno("asprintf");
1410 } else if (asprintf(&s,
1411 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1412 branches ? branches : "",
1413 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1414 branchname) == -1) {
1415 err = got_error_from_errno("asprintf");
1423 * If the server specified a default branch, use just that one.
1424 * Otherwise fall back to fetching all branches on next fetch.
1426 if (default_branch) {
1427 branchname = default_branch;
1428 if (strncmp(branchname, "refs/heads/", 11) == 0)
1431 branchname = "*"; /* fall back to all branches */
1432 if (mirror_references) {
1433 if (asprintf(&branches,
1434 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1435 branchname, branchname) == -1) {
1436 err = got_error_from_errno("asprintf");
1439 } else if (asprintf(&branches,
1440 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1441 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1442 branchname) == -1) {
1443 err = got_error_from_errno("asprintf");
1447 if (!TAILQ_EMPTY(wanted_refs)) {
1448 struct got_pathlist_entry *pe;
1449 TAILQ_FOREACH(pe, wanted_refs, entry) {
1451 const char *refname = pe->path;
1452 if (strncmp(refname, "refs/", 5) == 0)
1454 if (mirror_references) {
1456 "%s\tfetch = refs/%s:refs/%s\n",
1457 refs ? refs : "", refname, refname) == -1) {
1458 err = got_error_from_errno("asprintf");
1461 } else if (asprintf(&s,
1462 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1464 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1466 err = got_error_from_errno("asprintf");
1474 if (asprintf(&gitconfig,
1479 "\tfetch = refs/tags/*:refs/tags/*\n",
1480 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1481 refs ? refs : "") == -1) {
1482 err = got_error_from_errno("asprintf");
1485 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1486 if (n != strlen(gitconfig)) {
1487 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1491 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1492 err = got_error_from_errno2("fclose", gitconfig_path);
1493 free(gitconfig_path);
1498 static const struct got_error *
1499 create_config_files(const char *proto, const char *host, const char *port,
1500 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1501 int mirror_references, struct got_pathlist_head *symrefs,
1502 struct got_pathlist_head *wanted_branches,
1503 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1505 const struct got_error *err = NULL;
1506 const char *default_branch = NULL;
1507 struct got_pathlist_entry *pe;
1510 * If we asked for a set of wanted branches then use the first
1513 if (!TAILQ_EMPTY(wanted_branches)) {
1514 pe = TAILQ_FIRST(wanted_branches);
1515 default_branch = pe->path;
1517 /* First HEAD ref listed by server is the default branch. */
1518 TAILQ_FOREACH(pe, symrefs, entry) {
1519 const char *refname = pe->path;
1520 const char *target = pe->data;
1522 if (strcmp(refname, GOT_REF_HEAD) != 0)
1525 default_branch = target;
1530 /* Create got.conf(5). */
1531 err = create_gotconfig(proto, host, port, remote_repo_path,
1532 default_branch, fetch_all_branches, wanted_branches,
1533 wanted_refs, mirror_references, repo);
1537 /* Create a config file Git can understand. */
1538 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1539 wanted_branches, wanted_refs, mirror_references, repo);
1542 static const struct got_error *
1543 cmd_clone(int argc, char *argv[])
1545 const struct got_error *error = NULL;
1546 const char *uri, *dirname;
1547 char *proto, *host, *port, *repo_name, *server_path;
1548 char *default_destdir = NULL, *id_str = NULL;
1549 const char *repo_path;
1550 struct got_repository *repo = NULL;
1551 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1552 struct got_pathlist_entry *pe;
1553 struct got_object_id *pack_hash = NULL;
1554 int ch, fetchfd = -1, fetchstatus;
1555 pid_t fetchpid = -1;
1556 struct got_fetch_progress_arg fpa;
1557 char *git_url = NULL;
1558 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1559 int bflag = 0, list_refs_only = 0;
1560 int *pack_fds = NULL;
1563 TAILQ_INIT(&symrefs);
1564 TAILQ_INIT(&wanted_branches);
1565 TAILQ_INIT(&wanted_refs);
1567 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1570 fetch_all_branches = 1;
1573 error = got_pathlist_append(&wanted_branches,
1583 mirror_references = 1;
1589 error = got_pathlist_append(&wanted_refs,
1597 else if (verbosity < 3)
1608 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1609 option_conflict('a', 'b');
1610 if (list_refs_only) {
1611 if (!TAILQ_EMPTY(&wanted_branches))
1612 option_conflict('l', 'b');
1613 if (fetch_all_branches)
1614 option_conflict('l', 'a');
1615 if (mirror_references)
1616 option_conflict('l', 'm');
1617 if (!TAILQ_EMPTY(&wanted_refs))
1618 option_conflict('l', 'R');
1630 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1635 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1636 host, port ? ":" : "", port ? port : "",
1637 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1638 error = got_error_from_errno("asprintf");
1642 if (strcmp(proto, "git") == 0) {
1644 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1645 "sendfd dns inet unveil", NULL) == -1)
1648 } else if (strcmp(proto, "git+ssh") == 0 ||
1649 strcmp(proto, "ssh") == 0) {
1651 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1652 "sendfd unveil", NULL) == -1)
1655 } else if (strcmp(proto, "http") == 0 ||
1656 strcmp(proto, "git+http") == 0) {
1657 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1660 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1663 if (dirname == NULL) {
1664 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1665 error = got_error_from_errno("asprintf");
1668 repo_path = default_destdir;
1670 repo_path = dirname;
1672 if (!list_refs_only) {
1673 error = got_path_mkdir(repo_path);
1675 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1676 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1678 if (!got_path_dir_is_empty(repo_path)) {
1679 error = got_error_path(repo_path,
1680 GOT_ERR_DIR_NOT_EMPTY);
1685 error = got_dial_apply_unveil(proto);
1689 error = apply_unveil(repo_path, 0, NULL);
1694 printf("Connecting to %s\n", git_url);
1696 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1697 server_path, verbosity);
1701 if (!list_refs_only) {
1702 error = got_repo_init(repo_path, NULL);
1705 error = got_repo_pack_fds_open(&pack_fds);
1708 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1713 fpa.last_scaled_size[0] = '\0';
1714 fpa.last_p_indexed = -1;
1715 fpa.last_p_resolved = -1;
1716 fpa.verbosity = verbosity;
1717 fpa.create_configs = 1;
1718 fpa.configs_created = 0;
1720 fpa.config_info.symrefs = &symrefs;
1721 fpa.config_info.wanted_branches = &wanted_branches;
1722 fpa.config_info.wanted_refs = &wanted_refs;
1723 fpa.config_info.proto = proto;
1724 fpa.config_info.host = host;
1725 fpa.config_info.port = port;
1726 fpa.config_info.remote_repo_path = server_path;
1727 fpa.config_info.git_url = git_url;
1728 fpa.config_info.fetch_all_branches = fetch_all_branches;
1729 fpa.config_info.mirror_references = mirror_references;
1730 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1731 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1732 fetch_all_branches, &wanted_branches, &wanted_refs,
1733 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1734 fetch_progress, &fpa);
1738 if (list_refs_only) {
1739 error = list_remote_refs(&symrefs, &refs);
1743 if (pack_hash == NULL) {
1744 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1745 "server sent an empty pack file");
1748 error = got_object_id_str(&id_str, pack_hash);
1752 printf("\nFetched %s.pack\n", id_str);
1755 /* Set up references provided with the pack file. */
1756 TAILQ_FOREACH(pe, &refs, entry) {
1757 const char *refname = pe->path;
1758 struct got_object_id *id = pe->data;
1759 char *remote_refname;
1761 if (is_wanted_ref(&wanted_refs, refname) &&
1762 !mirror_references) {
1763 error = create_wanted_ref(refname, id,
1764 GOT_FETCH_DEFAULT_REMOTE_NAME,
1765 verbosity - 1, repo);
1771 error = create_ref(refname, id, verbosity - 1, repo);
1775 if (mirror_references)
1778 if (strncmp("refs/heads/", refname, 11) != 0)
1781 if (asprintf(&remote_refname,
1782 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1783 refname + 11) == -1) {
1784 error = got_error_from_errno("asprintf");
1787 error = create_ref(remote_refname, id, verbosity - 1, repo);
1788 free(remote_refname);
1793 /* Set the HEAD reference if the server provided one. */
1794 TAILQ_FOREACH(pe, &symrefs, entry) {
1795 struct got_reference *target_ref;
1796 const char *refname = pe->path;
1797 const char *target = pe->data;
1798 char *remote_refname = NULL, *remote_target = NULL;
1800 if (strcmp(refname, GOT_REF_HEAD) != 0)
1803 error = got_ref_open(&target_ref, repo, target, 0);
1805 if (error->code == GOT_ERR_NOT_REF) {
1812 error = create_symref(refname, target_ref, verbosity, repo);
1813 got_ref_close(target_ref);
1817 if (mirror_references)
1820 if (strncmp("refs/heads/", target, 11) != 0)
1823 if (asprintf(&remote_refname,
1824 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1826 error = got_error_from_errno("asprintf");
1829 if (asprintf(&remote_target,
1830 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1831 target + 11) == -1) {
1832 error = got_error_from_errno("asprintf");
1833 free(remote_refname);
1836 error = got_ref_open(&target_ref, repo, remote_target, 0);
1838 free(remote_refname);
1839 free(remote_target);
1840 if (error->code == GOT_ERR_NOT_REF) {
1846 error = create_symref(remote_refname, target_ref,
1847 verbosity - 1, repo);
1848 free(remote_refname);
1849 free(remote_target);
1850 got_ref_close(target_ref);
1856 * We failed to set the HEAD reference. If we asked for
1857 * a set of wanted branches use the first of one of those
1858 * which could be fetched instead.
1860 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1861 const char *target = pe->path;
1862 struct got_reference *target_ref;
1864 error = got_ref_open(&target_ref, repo, target, 0);
1866 if (error->code == GOT_ERR_NOT_REF) {
1873 error = create_symref(GOT_REF_HEAD, target_ref,
1875 got_ref_close(target_ref);
1881 if (!fpa.configs_created && pe != NULL) {
1882 error = create_config_files(fpa.config_info.proto,
1883 fpa.config_info.host, fpa.config_info.port,
1884 fpa.config_info.remote_repo_path,
1885 fpa.config_info.git_url,
1886 fpa.config_info.fetch_all_branches,
1887 fpa.config_info.mirror_references,
1888 fpa.config_info.symrefs,
1889 fpa.config_info.wanted_branches,
1890 fpa.config_info.wanted_refs, fpa.repo);
1897 printf("Created %s repository '%s'\n",
1898 mirror_references ? "mirrored" : "cloned", repo_path);
1901 const struct got_error *pack_err =
1902 got_repo_pack_fds_close(pack_fds);
1907 if (kill(fetchpid, SIGTERM) == -1)
1908 error = got_error_from_errno("kill");
1909 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1910 error = got_error_from_errno("waitpid");
1912 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1913 error = got_error_from_errno("close");
1915 const struct got_error *close_err = got_repo_close(repo);
1919 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1920 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1921 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1922 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1929 free(default_destdir);
1934 static const struct got_error *
1935 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1936 int replace_tags, int verbosity, struct got_repository *repo)
1938 const struct got_error *err = NULL;
1939 char *new_id_str = NULL;
1940 struct got_object_id *old_id = NULL;
1942 err = got_object_id_str(&new_id_str, new_id);
1946 if (!replace_tags &&
1947 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1948 err = got_ref_resolve(&old_id, repo, ref);
1951 if (got_object_id_cmp(old_id, new_id) == 0)
1953 if (verbosity >= 0) {
1954 printf("Rejecting update of existing tag %s: %s\n",
1955 got_ref_get_name(ref), new_id_str);
1960 if (got_ref_is_symbolic(ref)) {
1961 if (verbosity >= 0) {
1962 printf("Replacing reference %s: %s\n",
1963 got_ref_get_name(ref),
1964 got_ref_get_symref_target(ref));
1966 err = got_ref_change_symref_to_ref(ref, new_id);
1969 err = got_ref_write(ref, repo);
1973 err = got_ref_resolve(&old_id, repo, ref);
1976 if (got_object_id_cmp(old_id, new_id) == 0)
1979 err = got_ref_change_ref(ref, new_id);
1982 err = got_ref_write(ref, repo);
1988 printf("Updated %s: %s\n", got_ref_get_name(ref),
1996 static const struct got_error *
1997 update_symref(const char *refname, struct got_reference *target_ref,
1998 int verbosity, struct got_repository *repo)
2000 const struct got_error *err = NULL, *unlock_err;
2001 struct got_reference *symref;
2002 int symref_is_locked = 0;
2004 err = got_ref_open(&symref, repo, refname, 1);
2006 if (err->code != GOT_ERR_NOT_REF)
2008 err = got_ref_alloc_symref(&symref, refname, target_ref);
2012 err = got_ref_write(symref, repo);
2017 printf("Created reference %s: %s\n",
2018 got_ref_get_name(symref),
2019 got_ref_get_symref_target(symref));
2021 symref_is_locked = 1;
2023 if (strcmp(got_ref_get_symref_target(symref),
2024 got_ref_get_name(target_ref)) == 0)
2027 err = got_ref_change_symref(symref,
2028 got_ref_get_name(target_ref));
2032 err = got_ref_write(symref, repo);
2037 printf("Updated %s: %s\n", got_ref_get_name(symref),
2038 got_ref_get_symref_target(symref));
2042 if (symref_is_locked) {
2043 unlock_err = got_ref_unlock(symref);
2044 if (unlock_err && err == NULL)
2047 got_ref_close(symref);
2054 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2055 "[-R reference] [-r repository-path] [remote-repository]\n",
2060 static const struct got_error *
2061 delete_missing_ref(struct got_reference *ref,
2062 int verbosity, struct got_repository *repo)
2064 const struct got_error *err = NULL;
2065 struct got_object_id *id = NULL;
2066 char *id_str = NULL;
2068 if (got_ref_is_symbolic(ref)) {
2069 err = got_ref_delete(ref, repo);
2072 if (verbosity >= 0) {
2073 printf("Deleted %s: %s\n",
2074 got_ref_get_name(ref),
2075 got_ref_get_symref_target(ref));
2078 err = got_ref_resolve(&id, repo, ref);
2081 err = got_object_id_str(&id_str, id);
2085 err = got_ref_delete(ref, repo);
2088 if (verbosity >= 0) {
2089 printf("Deleted %s: %s\n",
2090 got_ref_get_name(ref), id_str);
2099 static const struct got_error *
2100 delete_missing_refs(struct got_pathlist_head *their_refs,
2101 struct got_pathlist_head *their_symrefs,
2102 const struct got_remote_repo *remote,
2103 int verbosity, struct got_repository *repo)
2105 const struct got_error *err = NULL, *unlock_err;
2106 struct got_reflist_head my_refs;
2107 struct got_reflist_entry *re;
2108 struct got_pathlist_entry *pe;
2109 char *remote_namespace = NULL;
2110 char *local_refname = NULL;
2112 TAILQ_INIT(&my_refs);
2114 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2116 return got_error_from_errno("asprintf");
2118 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2122 TAILQ_FOREACH(re, &my_refs, entry) {
2123 const char *refname = got_ref_get_name(re->ref);
2124 const char *their_refname;
2126 if (remote->mirror_references) {
2127 their_refname = refname;
2129 if (strncmp(refname, remote_namespace,
2130 strlen(remote_namespace)) == 0) {
2131 if (strcmp(refname + strlen(remote_namespace),
2134 if (asprintf(&local_refname, "refs/heads/%s",
2135 refname + strlen(remote_namespace)) == -1) {
2136 err = got_error_from_errno("asprintf");
2139 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2142 their_refname = local_refname;
2145 TAILQ_FOREACH(pe, their_refs, entry) {
2146 if (strcmp(their_refname, pe->path) == 0)
2152 TAILQ_FOREACH(pe, their_symrefs, entry) {
2153 if (strcmp(their_refname, pe->path) == 0)
2159 err = delete_missing_ref(re->ref, verbosity, repo);
2163 if (local_refname) {
2164 struct got_reference *ref;
2165 err = got_ref_open(&ref, repo, local_refname, 1);
2167 if (err->code != GOT_ERR_NOT_REF)
2169 free(local_refname);
2170 local_refname = NULL;
2173 err = delete_missing_ref(ref, verbosity, repo);
2176 unlock_err = got_ref_unlock(ref);
2178 if (unlock_err && err == NULL) {
2183 free(local_refname);
2184 local_refname = NULL;
2188 got_ref_list_free(&my_refs);
2189 free(remote_namespace);
2190 free(local_refname);
2194 static const struct got_error *
2195 update_wanted_ref(const char *refname, struct got_object_id *id,
2196 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2198 const struct got_error *err, *unlock_err;
2199 char *remote_refname;
2200 struct got_reference *ref;
2202 if (strncmp("refs/", refname, 5) == 0)
2205 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2206 remote_repo_name, refname) == -1)
2207 return got_error_from_errno("asprintf");
2209 err = got_ref_open(&ref, repo, remote_refname, 1);
2211 if (err->code != GOT_ERR_NOT_REF)
2213 err = create_ref(remote_refname, id, verbosity, repo);
2215 err = update_ref(ref, id, 0, verbosity, repo);
2216 unlock_err = got_ref_unlock(ref);
2217 if (unlock_err && err == NULL)
2222 free(remote_refname);
2226 static const struct got_error *
2227 delete_ref(struct got_repository *repo, struct got_reference *ref)
2229 const struct got_error *err = NULL;
2230 struct got_object_id *id = NULL;
2231 char *id_str = NULL;
2234 if (got_ref_is_symbolic(ref)) {
2235 target = got_ref_get_symref_target(ref);
2237 err = got_ref_resolve(&id, repo, ref);
2240 err = got_object_id_str(&id_str, id);
2246 err = got_ref_delete(ref, repo);
2250 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2257 static const struct got_error *
2258 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2260 const struct got_error *err = NULL;
2261 struct got_reflist_head refs;
2262 struct got_reflist_entry *re;
2267 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2268 err = got_error_from_errno("asprintf");
2271 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2275 TAILQ_FOREACH(re, &refs, entry)
2276 delete_ref(repo, re->ref);
2278 got_ref_list_free(&refs);
2282 static const struct got_error *
2283 cmd_fetch(int argc, char *argv[])
2285 const struct got_error *error = NULL, *unlock_err;
2286 char *cwd = NULL, *repo_path = NULL;
2287 const char *remote_name;
2288 char *proto = NULL, *host = NULL, *port = NULL;
2289 char *repo_name = NULL, *server_path = NULL;
2290 const struct got_remote_repo *remotes;
2291 struct got_remote_repo *remote = NULL;
2293 char *id_str = NULL;
2294 struct got_repository *repo = NULL;
2295 struct got_worktree *worktree = NULL;
2296 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2297 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2298 char *head_refname = NULL;
2299 struct got_pathlist_entry *pe;
2300 struct got_reflist_head remote_refs;
2301 struct got_reflist_entry *re;
2302 struct got_object_id *pack_hash = NULL;
2303 int i, ch, fetchfd = -1, fetchstatus;
2304 pid_t fetchpid = -1;
2305 struct got_fetch_progress_arg fpa;
2306 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2307 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2308 int *pack_fds = NULL, have_bflag = 0;
2309 const char *remote_head = NULL, *worktree_branch = NULL;
2312 TAILQ_INIT(&symrefs);
2313 TAILQ_INIT(&remote_refs);
2314 TAILQ_INIT(&wanted_branches);
2315 TAILQ_INIT(&wanted_refs);
2317 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2320 fetch_all_branches = 1;
2323 error = got_pathlist_append(&wanted_branches,
2339 error = got_pathlist_append(&wanted_refs,
2345 repo_path = realpath(optarg, NULL);
2346 if (repo_path == NULL)
2347 return got_error_from_errno2("realpath",
2349 got_path_strip_trailing_slashes(repo_path);
2357 else if (verbosity < 3)
2371 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2372 option_conflict('a', 'b');
2373 if (list_refs_only) {
2374 if (!TAILQ_EMPTY(&wanted_branches))
2375 option_conflict('l', 'b');
2376 if (fetch_all_branches)
2377 option_conflict('l', 'a');
2379 option_conflict('l', 'd');
2381 option_conflict('l', 'X');
2383 if (delete_remote) {
2384 if (fetch_all_branches)
2385 option_conflict('X', 'a');
2386 if (!TAILQ_EMPTY(&wanted_branches))
2387 option_conflict('X', 'b');
2389 option_conflict('X', 'd');
2391 option_conflict('X', 't');
2392 if (!TAILQ_EMPTY(&wanted_refs))
2393 option_conflict('X', 'R');
2398 errx(1, "-X option requires a remote name");
2399 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2400 } else if (argc == 1)
2401 remote_name = argv[0];
2405 cwd = getcwd(NULL, 0);
2407 error = got_error_from_errno("getcwd");
2411 error = got_repo_pack_fds_open(&pack_fds);
2415 if (repo_path == NULL) {
2416 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
2417 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2423 strdup(got_worktree_get_repo_path(worktree));
2424 if (repo_path == NULL)
2425 error = got_error_from_errno("strdup");
2429 repo_path = strdup(cwd);
2430 if (repo_path == NULL) {
2431 error = got_error_from_errno("strdup");
2437 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2441 if (delete_remote) {
2442 error = delete_refs_for_remote(repo, remote_name);
2443 goto done; /* nothing else to do */
2447 worktree_conf = got_worktree_get_gotconfig(worktree);
2448 if (worktree_conf) {
2449 got_gotconfig_get_remotes(&nremotes, &remotes,
2451 for (i = 0; i < nremotes; i++) {
2452 if (strcmp(remotes[i].name, remote_name) == 0) {
2453 error = got_repo_remote_repo_dup(&remote,
2462 if (remote == NULL) {
2463 repo_conf = got_repo_get_gotconfig(repo);
2465 got_gotconfig_get_remotes(&nremotes, &remotes,
2467 for (i = 0; i < nremotes; i++) {
2468 if (strcmp(remotes[i].name, remote_name) == 0) {
2469 error = got_repo_remote_repo_dup(&remote,
2478 if (remote == NULL) {
2479 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2480 for (i = 0; i < nremotes; i++) {
2481 if (strcmp(remotes[i].name, remote_name) == 0) {
2482 error = got_repo_remote_repo_dup(&remote,
2490 if (remote == NULL) {
2491 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2495 if (TAILQ_EMPTY(&wanted_branches)) {
2496 if (!fetch_all_branches)
2497 fetch_all_branches = remote->fetch_all_branches;
2498 for (i = 0; i < remote->nfetch_branches; i++) {
2499 error = got_pathlist_append(&wanted_branches,
2500 remote->fetch_branches[i], NULL);
2505 if (TAILQ_EMPTY(&wanted_refs)) {
2506 for (i = 0; i < remote->nfetch_refs; i++) {
2507 error = got_pathlist_append(&wanted_refs,
2508 remote->fetch_refs[i], NULL);
2514 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2515 &repo_name, remote->fetch_url);
2519 if (strcmp(proto, "git") == 0) {
2521 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2522 "sendfd dns inet unveil", NULL) == -1)
2525 } else if (strcmp(proto, "git+ssh") == 0 ||
2526 strcmp(proto, "ssh") == 0) {
2528 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2529 "sendfd unveil", NULL) == -1)
2532 } else if (strcmp(proto, "http") == 0 ||
2533 strcmp(proto, "git+http") == 0) {
2534 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2537 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2541 error = got_dial_apply_unveil(proto);
2545 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2550 head_refname = strdup(got_worktree_get_head_ref_name(worktree));
2551 if (head_refname == NULL) {
2552 error = got_error_from_errno("strdup");
2556 /* Release work tree lock. */
2557 got_worktree_close(worktree);
2561 if (verbosity >= 0) {
2562 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2563 remote->name, proto, host,
2564 port ? ":" : "", port ? port : "",
2565 *server_path == '/' ? "" : "/", server_path);
2568 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2569 server_path, verbosity);
2575 * If set, get this remote's HEAD ref target so
2576 * if it has changed on the server we can fetch it.
2578 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2579 got_ref_cmp_by_name, repo);
2583 TAILQ_FOREACH(re, &remote_refs, entry) {
2584 const char *remote_refname, *remote_target;
2585 size_t remote_name_len;
2587 if (!got_ref_is_symbolic(re->ref))
2590 remote_name_len = strlen(remote->name);
2591 remote_refname = got_ref_get_name(re->ref);
2593 /* we only want refs/remotes/$remote->name/HEAD */
2594 if (strncmp(remote_refname + 13, remote->name,
2595 remote_name_len) != 0)
2598 if (strcmp(remote_refname + remote_name_len + 14,
2603 * Take the name itself because we already
2604 * only match with refs/heads/ in fetch_pack().
2606 remote_target = got_ref_get_symref_target(re->ref);
2607 remote_head = remote_target + remote_name_len + 14;
2612 strncmp(head_refname, "refs/heads/", 11) == 0)
2613 worktree_branch = head_refname;
2616 fpa.last_scaled_size[0] = '\0';
2617 fpa.last_p_indexed = -1;
2618 fpa.last_p_resolved = -1;
2619 fpa.verbosity = verbosity;
2621 fpa.create_configs = 0;
2622 fpa.configs_created = 0;
2623 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2625 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2626 remote->mirror_references, fetch_all_branches, &wanted_branches,
2627 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2628 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2632 if (list_refs_only) {
2633 error = list_remote_refs(&symrefs, &refs);
2637 if (pack_hash == NULL) {
2639 printf("Already up-to-date\n");
2640 } else if (verbosity >= 0) {
2641 error = got_object_id_str(&id_str, pack_hash);
2644 printf("\nFetched %s.pack\n", id_str);
2649 /* Update references provided with the pack file. */
2650 TAILQ_FOREACH(pe, &refs, entry) {
2651 const char *refname = pe->path;
2652 struct got_object_id *id = pe->data;
2653 struct got_reference *ref;
2654 char *remote_refname;
2656 if (is_wanted_ref(&wanted_refs, refname) &&
2657 !remote->mirror_references) {
2658 error = update_wanted_ref(refname, id,
2659 remote->name, verbosity, repo);
2665 if (remote->mirror_references ||
2666 strncmp("refs/tags/", refname, 10) == 0) {
2667 error = got_ref_open(&ref, repo, refname, 1);
2669 if (error->code != GOT_ERR_NOT_REF)
2671 error = create_ref(refname, id, verbosity,
2676 error = update_ref(ref, id, replace_tags,
2678 unlock_err = got_ref_unlock(ref);
2679 if (unlock_err && error == NULL)
2685 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2686 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2687 remote_name, refname + 11) == -1) {
2688 error = got_error_from_errno("asprintf");
2692 error = got_ref_open(&ref, repo, remote_refname, 1);
2694 if (error->code != GOT_ERR_NOT_REF)
2696 error = create_ref(remote_refname, id,
2701 error = update_ref(ref, id, replace_tags,
2703 unlock_err = got_ref_unlock(ref);
2704 if (unlock_err && error == NULL)
2711 /* Also create a local branch if none exists yet. */
2712 error = got_ref_open(&ref, repo, refname, 1);
2714 if (error->code != GOT_ERR_NOT_REF)
2716 error = create_ref(refname, id, verbosity,
2721 unlock_err = got_ref_unlock(ref);
2722 if (unlock_err && error == NULL)
2729 error = delete_missing_refs(&refs, &symrefs, remote,
2735 if (!remote->mirror_references) {
2736 /* Update remote HEAD reference if the server provided one. */
2737 TAILQ_FOREACH(pe, &symrefs, entry) {
2738 struct got_reference *target_ref;
2739 const char *refname = pe->path;
2740 const char *target = pe->data;
2741 char *remote_refname = NULL, *remote_target = NULL;
2743 if (strcmp(refname, GOT_REF_HEAD) != 0)
2746 if (strncmp("refs/heads/", target, 11) != 0)
2749 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2750 remote->name, refname) == -1) {
2751 error = got_error_from_errno("asprintf");
2754 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2755 remote->name, target + 11) == -1) {
2756 error = got_error_from_errno("asprintf");
2757 free(remote_refname);
2761 error = got_ref_open(&target_ref, repo, remote_target,
2764 free(remote_refname);
2765 free(remote_target);
2766 if (error->code == GOT_ERR_NOT_REF) {
2772 error = update_symref(remote_refname, target_ref,
2774 free(remote_refname);
2775 free(remote_target);
2776 got_ref_close(target_ref);
2783 if (kill(fetchpid, SIGTERM) == -1)
2784 error = got_error_from_errno("kill");
2785 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2786 error = got_error_from_errno("waitpid");
2788 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2789 error = got_error_from_errno("close");
2791 const struct got_error *close_err = got_repo_close(repo);
2796 got_worktree_close(worktree);
2798 const struct got_error *pack_err =
2799 got_repo_pack_fds_close(pack_fds);
2803 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2804 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2805 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2806 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2807 got_ref_list_free(&remote_refs);
2808 got_repo_free_remote_repo_data(remote);
2825 usage_checkout(void)
2827 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2828 "[-p path-prefix] repository-path [work-tree-path]\n",
2834 show_worktree_base_ref_warning(void)
2836 fprintf(stderr, "%s: warning: could not create a reference "
2837 "to the work tree's base commit; the commit could be "
2838 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2839 "repository writable and running 'got update' will prevent this\n",
2843 struct got_checkout_progress_arg {
2844 const char *worktree_path;
2845 int had_base_commit_ref_error;
2849 static const struct got_error *
2850 checkout_progress(void *arg, unsigned char status, const char *path)
2852 struct got_checkout_progress_arg *a = arg;
2854 /* Base commit bump happens silently. */
2855 if (status == GOT_STATUS_BUMP_BASE)
2858 if (status == GOT_STATUS_BASE_REF_ERR) {
2859 a->had_base_commit_ref_error = 1;
2863 while (path[0] == '/')
2866 if (a->verbosity >= 0)
2867 printf("%c %s/%s\n", status, a->worktree_path, path);
2872 static const struct got_error *
2873 check_cancelled(void *arg)
2875 if (sigint_received || sigpipe_received)
2876 return got_error(GOT_ERR_CANCELLED);
2880 static const struct got_error *
2881 check_linear_ancestry(struct got_object_id *commit_id,
2882 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2883 struct got_repository *repo)
2885 const struct got_error *err = NULL;
2886 struct got_object_id *yca_id;
2888 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2889 commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2894 return got_error(GOT_ERR_ANCESTRY);
2897 * Require a straight line of history between the target commit
2898 * and the work tree's base commit.
2900 * Non-linear situations such as this require a rebase:
2902 * (commit) D F (base_commit)
2910 * 'got update' only handles linear cases:
2911 * Update forwards in time: A (base/yca) - B - C - D (commit)
2912 * Update backwards in time: D (base) - C - B - A (commit/yca)
2914 if (allow_forwards_in_time_only) {
2915 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2916 return got_error(GOT_ERR_ANCESTRY);
2917 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2918 got_object_id_cmp(base_commit_id, yca_id) != 0)
2919 return got_error(GOT_ERR_ANCESTRY);
2925 static const struct got_error *
2926 check_same_branch(struct got_object_id *commit_id,
2927 struct got_reference *head_ref, struct got_repository *repo)
2929 const struct got_error *err = NULL;
2930 struct got_commit_graph *graph = NULL;
2931 struct got_object_id *head_commit_id = NULL;
2933 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2937 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2940 err = got_commit_graph_open(&graph, "/", 1);
2944 err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2945 check_cancelled, NULL);
2950 struct got_object_id id;
2952 err = got_commit_graph_iter_next(&id, graph, repo,
2953 check_cancelled, NULL);
2955 if (err->code == GOT_ERR_ITER_COMPLETED)
2956 err = got_error(GOT_ERR_ANCESTRY);
2960 if (got_object_id_cmp(&id, commit_id) == 0)
2965 got_commit_graph_close(graph);
2966 free(head_commit_id);
2970 static const struct got_error *
2971 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2973 static char msg[512];
2974 const char *branch_name;
2976 if (got_ref_is_symbolic(ref))
2977 branch_name = got_ref_get_symref_target(ref);
2979 branch_name = got_ref_get_name(ref);
2981 if (strncmp("refs/heads/", branch_name, 11) == 0)
2984 snprintf(msg, sizeof(msg),
2985 "target commit is not contained in branch '%s'; "
2986 "the branch to use must be specified with -b; "
2987 "if necessary a new branch can be created for "
2988 "this commit with 'got branch -c %s BRANCH_NAME'",
2989 branch_name, commit_id_str);
2991 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2994 static const struct got_error *
2995 cmd_checkout(int argc, char *argv[])
2997 const struct got_error *close_err, *error = NULL;
2998 struct got_repository *repo = NULL;
2999 struct got_reference *head_ref = NULL, *ref = NULL;
3000 struct got_worktree *worktree = NULL;
3001 char *repo_path = NULL;
3002 char *worktree_path = NULL;
3003 const char *path_prefix = "";
3004 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
3005 char *commit_id_str = NULL, *keyword_idstr = NULL;
3006 struct got_object_id *commit_id = NULL;
3008 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
3009 struct got_pathlist_head paths;
3010 struct got_checkout_progress_arg cpa;
3011 int *pack_fds = NULL;
3016 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3017 "unveil", NULL) == -1)
3021 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3024 branch_name = optarg;
3027 commit_id_str = strdup(optarg);
3028 if (commit_id_str == NULL)
3029 return got_error_from_errno("strdup");
3035 path_prefix = optarg;
3050 char *base, *dotgit;
3052 repo_path = realpath(argv[0], NULL);
3053 if (repo_path == NULL)
3054 return got_error_from_errno2("realpath", argv[0]);
3055 cwd = getcwd(NULL, 0);
3057 error = got_error_from_errno("getcwd");
3064 error = got_path_basename(&base, path);
3067 dotgit = strstr(base, ".git");
3070 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3071 error = got_error_from_errno("asprintf");
3076 } else if (argc == 2) {
3077 repo_path = realpath(argv[0], NULL);
3078 if (repo_path == NULL) {
3079 error = got_error_from_errno2("realpath", argv[0]);
3082 worktree_path = realpath(argv[1], NULL);
3083 if (worktree_path == NULL) {
3084 if (errno != ENOENT) {
3085 error = got_error_from_errno2("realpath",
3089 worktree_path = strdup(argv[1]);
3090 if (worktree_path == NULL) {
3091 error = got_error_from_errno("strdup");
3098 got_path_strip_trailing_slashes(repo_path);
3099 got_path_strip_trailing_slashes(worktree_path);
3101 if (got_path_is_child(worktree_path, repo_path, strlen(repo_path)) ||
3102 got_path_is_child(repo_path, worktree_path,
3103 strlen(worktree_path))) {
3104 error = got_error_fmt(GOT_ERR_BAD_PATH,
3105 "work tree and repository paths may not overlap: %s",
3110 error = got_repo_pack_fds_open(&pack_fds);
3114 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3118 /* Pre-create work tree path for unveil(2) */
3119 error = got_path_mkdir(worktree_path);
3121 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3122 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3124 if (!allow_nonempty &&
3125 !got_path_dir_is_empty(worktree_path)) {
3126 error = got_error_path(worktree_path,
3127 GOT_ERR_DIR_NOT_EMPTY);
3132 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3136 error = got_ref_open(&head_ref, repo, branch_name, 0);
3140 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3141 GOT_WORKTREE_GOT_DIR, repo);
3142 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3145 error = got_worktree_open(&worktree, worktree_path,
3146 GOT_WORKTREE_GOT_DIR);
3150 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3154 if (!same_path_prefix) {
3155 error = got_error(GOT_ERR_PATH_PREFIX);
3159 if (commit_id_str) {
3160 struct got_reflist_head refs;
3162 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3167 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3171 if (keyword_idstr != NULL) {
3172 free(commit_id_str);
3173 commit_id_str = keyword_idstr;
3176 error = got_repo_match_object_id(&commit_id, NULL,
3177 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3178 got_ref_list_free(&refs);
3181 error = check_linear_ancestry(commit_id,
3182 got_worktree_get_base_commit_id(worktree), 0, repo);
3183 if (error != NULL) {
3184 if (error->code == GOT_ERR_ANCESTRY) {
3185 error = checkout_ancestry_error(
3186 head_ref, commit_id_str);
3190 error = check_same_branch(commit_id, head_ref, repo);
3192 if (error->code == GOT_ERR_ANCESTRY) {
3193 error = checkout_ancestry_error(
3194 head_ref, commit_id_str);
3198 error = got_worktree_set_base_commit_id(worktree, repo,
3202 /* Expand potentially abbreviated commit ID string. */
3203 free(commit_id_str);
3204 error = got_object_id_str(&commit_id_str, commit_id);
3208 commit_id = got_object_id_dup(
3209 got_worktree_get_base_commit_id(worktree));
3210 if (commit_id == NULL) {
3211 error = got_error_from_errno("got_object_id_dup");
3214 error = got_object_id_str(&commit_id_str, commit_id);
3219 error = got_pathlist_append(&paths, "", NULL);
3222 cpa.worktree_path = worktree_path;
3223 cpa.had_base_commit_ref_error = 0;
3224 cpa.verbosity = verbosity;
3225 error = got_worktree_checkout_files(worktree, &paths, repo,
3226 checkout_progress, &cpa, check_cancelled, NULL);
3230 if (got_ref_is_symbolic(head_ref)) {
3231 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3234 refname = got_ref_get_name(ref);
3236 refname = got_ref_get_name(head_ref);
3237 printf("Checked out %s: %s\n", refname, commit_id_str);
3238 printf("Now shut up and hack\n");
3239 if (cpa.had_base_commit_ref_error)
3240 show_worktree_base_ref_warning();
3243 const struct got_error *pack_err =
3244 got_repo_pack_fds_close(pack_fds);
3249 got_ref_close(head_ref);
3253 close_err = got_repo_close(repo);
3257 if (worktree != NULL) {
3258 close_err = got_worktree_close(worktree);
3262 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3263 free(commit_id_str);
3266 free(worktree_path);
3271 struct got_update_progress_arg {
3283 print_update_progress_stats(struct got_update_progress_arg *upa)
3285 if (!upa->did_something)
3288 if (upa->conflicts > 0)
3289 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3290 if (upa->obstructed > 0)
3291 printf("File paths obstructed by a non-regular file: %d\n",
3293 if (upa->not_updated > 0)
3294 printf("Files not updated because of existing merge "
3295 "conflicts: %d\n", upa->not_updated);
3299 * The meaning of some status codes differs between merge-style operations and
3300 * update operations. For example, the ! status code means "file was missing"
3301 * if changes were merged into the work tree, and "missing file was restored"
3302 * if the work tree was updated. This function should be used by any operation
3303 * which merges changes into the work tree without updating the work tree.
3306 print_merge_progress_stats(struct got_update_progress_arg *upa)
3308 if (!upa->did_something)
3311 if (upa->conflicts > 0)
3312 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3313 if (upa->obstructed > 0)
3314 printf("File paths obstructed by a non-regular file: %d\n",
3316 if (upa->missing > 0)
3317 printf("Files which had incoming changes but could not be "
3318 "found in the work tree: %d\n", upa->missing);
3319 if (upa->not_deleted > 0)
3320 printf("Files not deleted due to differences in deleted "
3321 "content: %d\n", upa->not_deleted);
3322 if (upa->unversioned > 0)
3323 printf("Files not merged because an unversioned file was "
3324 "found in the work tree: %d\n", upa->unversioned);
3330 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3331 "[path ...]\n", getprogname());
3335 static const struct got_error *
3336 update_progress(void *arg, unsigned char status, const char *path)
3338 struct got_update_progress_arg *upa = arg;
3340 if (status == GOT_STATUS_EXISTS ||
3341 status == GOT_STATUS_BASE_REF_ERR)
3344 upa->did_something = 1;
3346 /* Base commit bump happens silently. */
3347 if (status == GOT_STATUS_BUMP_BASE)
3350 if (status == GOT_STATUS_CONFLICT)
3352 if (status == GOT_STATUS_OBSTRUCTED)
3354 if (status == GOT_STATUS_CANNOT_UPDATE)
3356 if (status == GOT_STATUS_MISSING)
3358 if (status == GOT_STATUS_CANNOT_DELETE)
3360 if (status == GOT_STATUS_UNVERSIONED)
3363 while (path[0] == '/')
3365 if (upa->verbosity >= 0)
3366 printf("%c %s\n", status, path);
3371 static const struct got_error *
3372 switch_head_ref(struct got_reference *head_ref,
3373 struct got_object_id *commit_id, struct got_worktree *worktree,
3374 struct got_repository *repo)
3376 const struct got_error *err = NULL;
3378 int ref_has_moved = 0;
3380 /* Trivial case: switching between two different references. */
3381 if (strcmp(got_ref_get_name(head_ref),
3382 got_worktree_get_head_ref_name(worktree)) != 0) {
3383 printf("Switching work tree from %s to %s\n",
3384 got_worktree_get_head_ref_name(worktree),
3385 got_ref_get_name(head_ref));
3386 return got_worktree_set_head_ref(worktree, head_ref);
3389 err = check_linear_ancestry(commit_id,
3390 got_worktree_get_base_commit_id(worktree), 0, repo);
3392 if (err->code != GOT_ERR_ANCESTRY)
3399 /* Switching to a rebased branch with the same reference name. */
3400 err = got_object_id_str(&base_id_str,
3401 got_worktree_get_base_commit_id(worktree));
3404 printf("Reference %s now points at a different branch\n",
3405 got_worktree_get_head_ref_name(worktree));
3406 printf("Switching work tree from %s to %s\n", base_id_str,
3407 got_worktree_get_head_ref_name(worktree));
3411 static const struct got_error *
3412 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3414 const struct got_error *err;
3417 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3421 return got_error(GOT_ERR_REBASING);
3423 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3427 return got_error(GOT_ERR_HISTEDIT_BUSY);
3432 static const struct got_error *
3433 check_merge_in_progress(struct got_worktree *worktree,
3434 struct got_repository *repo)
3436 const struct got_error *err;
3439 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3443 return got_error(GOT_ERR_MERGE_BUSY);
3448 static const struct got_error *
3449 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3450 char *argv[], struct got_worktree *worktree)
3452 const struct got_error *err = NULL;
3454 struct got_pathlist_entry *new;
3460 return got_error_from_errno("strdup");
3461 return got_pathlist_append(paths, path, NULL);
3464 for (i = 0; i < argc; i++) {
3465 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3468 err = got_pathlist_insert(&new, paths, path, NULL);
3469 if (err || new == NULL /* duplicate */) {
3479 static const struct got_error *
3480 wrap_not_worktree_error(const struct got_error *orig_err,
3481 const char *cmdname, const char *path)
3483 const struct got_error *err;
3484 struct got_repository *repo;
3485 static char msg[512];
3486 int *pack_fds = NULL;
3488 err = got_repo_pack_fds_open(&pack_fds);
3492 err = got_repo_open(&repo, path, NULL, pack_fds);
3496 snprintf(msg, sizeof(msg),
3497 "'got %s' needs a work tree in addition to a git repository\n"
3498 "Work trees can be checked out from this Git repository with "
3500 "The got(1) manual page contains more information.", cmdname);
3501 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3503 const struct got_error *close_err = got_repo_close(repo);
3508 const struct got_error *pack_err =
3509 got_repo_pack_fds_close(pack_fds);
3516 static const struct got_error *
3517 cmd_update(int argc, char *argv[])
3519 const struct got_error *close_err, *error = NULL;
3520 struct got_repository *repo = NULL;
3521 struct got_worktree *worktree = NULL;
3522 char *worktree_path = NULL;
3523 struct got_object_id *commit_id = NULL;
3524 char *commit_id_str = NULL;
3525 const char *branch_name = NULL;
3526 struct got_reference *head_ref = NULL;
3527 struct got_pathlist_head paths;
3528 struct got_pathlist_entry *pe;
3529 int ch, verbosity = 0;
3530 struct got_update_progress_arg upa;
3531 int *pack_fds = NULL;
3536 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3537 "unveil", NULL) == -1)
3541 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3544 branch_name = optarg;
3547 commit_id_str = strdup(optarg);
3548 if (commit_id_str == NULL)
3549 return got_error_from_errno("strdup");
3563 worktree_path = getcwd(NULL, 0);
3564 if (worktree_path == NULL) {
3565 error = got_error_from_errno("getcwd");
3569 error = got_repo_pack_fds_open(&pack_fds);
3573 error = got_worktree_open(&worktree, worktree_path,
3574 GOT_WORKTREE_GOT_DIR);
3576 if (error->code == GOT_ERR_NOT_WORKTREE)
3577 error = wrap_not_worktree_error(error, "update",
3582 error = check_rebase_or_histedit_in_progress(worktree);
3586 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3591 error = apply_unveil(got_repo_get_path(repo), 0,
3592 got_worktree_get_root_path(worktree));
3596 error = check_merge_in_progress(worktree, repo);
3600 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3604 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3605 got_worktree_get_head_ref_name(worktree), 0);
3608 if (commit_id_str == NULL) {
3609 error = got_ref_resolve(&commit_id, repo, head_ref);
3612 error = got_object_id_str(&commit_id_str, commit_id);
3616 struct got_reflist_head refs;
3617 char *keyword_idstr = NULL;
3621 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3626 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3630 if (keyword_idstr != NULL) {
3631 free(commit_id_str);
3632 commit_id_str = keyword_idstr;
3635 error = got_repo_match_object_id(&commit_id, NULL,
3636 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3637 got_ref_list_free(&refs);
3638 free(commit_id_str);
3639 commit_id_str = NULL;
3642 error = got_object_id_str(&commit_id_str, commit_id);
3648 struct got_object_id *head_commit_id;
3649 TAILQ_FOREACH(pe, &paths, entry) {
3650 if (pe->path_len == 0)
3652 error = got_error_msg(GOT_ERR_BAD_PATH,
3653 "switching between branches requires that "
3654 "the entire work tree gets updated");
3657 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3660 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3662 free(head_commit_id);
3665 error = check_same_branch(commit_id, head_ref, repo);
3668 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3672 error = check_linear_ancestry(commit_id,
3673 got_worktree_get_base_commit_id(worktree), 0, repo);
3674 if (error != NULL) {
3675 if (error->code == GOT_ERR_ANCESTRY)
3676 error = got_error(GOT_ERR_BRANCH_MOVED);
3679 error = check_same_branch(commit_id, head_ref, repo);
3684 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3686 error = got_worktree_set_base_commit_id(worktree, repo,
3692 memset(&upa, 0, sizeof(upa));
3693 upa.verbosity = verbosity;
3694 error = got_worktree_checkout_files(worktree, &paths, repo,
3695 update_progress, &upa, check_cancelled, NULL);
3699 if (upa.did_something) {
3700 printf("Updated to %s: %s\n",
3701 got_worktree_get_head_ref_name(worktree), commit_id_str);
3703 printf("Already up-to-date\n");
3705 print_update_progress_stats(&upa);
3708 const struct got_error *pack_err =
3709 got_repo_pack_fds_close(pack_fds);
3714 close_err = got_repo_close(repo);
3718 if (worktree != NULL) {
3719 close_err = got_worktree_close(worktree);
3723 if (head_ref != NULL)
3724 got_ref_close(head_ref);
3725 free(worktree_path);
3726 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3728 free(commit_id_str);
3732 static const struct got_error *
3733 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3734 const char *path, int diff_context, int ignore_whitespace,
3735 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3736 struct got_repository *repo, FILE *outfile)
3738 const struct got_error *err = NULL;
3739 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3740 FILE *f1 = NULL, *f2 = NULL;
3741 int fd1 = -1, fd2 = -1;
3743 fd1 = got_opentempfd();
3745 return got_error_from_errno("got_opentempfd");
3746 fd2 = got_opentempfd();
3748 err = got_error_from_errno("got_opentempfd");
3753 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3759 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3763 f1 = got_opentemp();
3765 err = got_error_from_errno("got_opentemp");
3768 f2 = got_opentemp();
3770 err = got_error_from_errno("got_opentemp");
3774 while (path[0] == '/')
3776 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3777 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3778 force_text_diff, dsa, outfile);
3780 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3781 err = got_error_from_errno("close");
3783 got_object_blob_close(blob1);
3784 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3785 err = got_error_from_errno("close");
3787 got_object_blob_close(blob2);
3788 if (f1 && fclose(f1) == EOF && err == NULL)
3789 err = got_error_from_errno("fclose");
3790 if (f2 && fclose(f2) == EOF && err == NULL)
3791 err = got_error_from_errno("fclose");
3795 static const struct got_error *
3796 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3797 const char *path, int diff_context, int ignore_whitespace,
3798 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3799 struct got_repository *repo, FILE *outfile)
3801 const struct got_error *err = NULL;
3802 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3803 struct got_diff_blob_output_unidiff_arg arg;
3804 FILE *f1 = NULL, *f2 = NULL;
3805 int fd1 = -1, fd2 = -1;
3808 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3811 fd1 = got_opentempfd();
3813 err = got_error_from_errno("got_opentempfd");
3818 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3822 f1 = got_opentemp();
3824 err = got_error_from_errno("got_opentemp");
3828 f2 = got_opentemp();
3830 err = got_error_from_errno("got_opentemp");
3833 fd2 = got_opentempfd();
3835 err = got_error_from_errno("got_opentempfd");
3838 arg.diff_context = diff_context;
3839 arg.ignore_whitespace = ignore_whitespace;
3840 arg.force_text_diff = force_text_diff;
3842 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3843 arg.outfile = outfile;
3846 while (path[0] == '/')
3848 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3849 got_diff_blob_output_unidiff, &arg, 1);
3852 got_object_tree_close(tree1);
3854 got_object_tree_close(tree2);
3855 if (f1 && fclose(f1) == EOF && err == NULL)
3856 err = got_error_from_errno("fclose");
3857 if (f2 && fclose(f2) == EOF && err == NULL)
3858 err = got_error_from_errno("fclose");
3859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3860 err = got_error_from_errno("close");
3861 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3862 err = got_error_from_errno("close");
3866 static const struct got_error *
3867 get_changed_paths(struct got_pathlist_head *paths,
3868 struct got_commit_object *commit, struct got_repository *repo,
3869 struct got_diffstat_cb_arg *dsa)
3871 const struct got_error *err = NULL;
3872 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3873 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3874 struct got_object_qid *qid;
3875 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3876 FILE *f1 = NULL, *f2 = NULL;
3877 int fd1 = -1, fd2 = -1;
3880 cb = got_diff_tree_compute_diffstat;
3882 f1 = got_opentemp();
3884 err = got_error_from_errno("got_opentemp");
3887 f2 = got_opentemp();
3889 err = got_error_from_errno("got_opentemp");
3892 fd1 = got_opentempfd();
3894 err = got_error_from_errno("got_opentempfd");
3897 fd2 = got_opentempfd();
3899 err = got_error_from_errno("got_opentempfd");
3904 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3906 struct got_commit_object *pcommit;
3907 err = got_object_open_as_commit(&pcommit, repo,
3912 tree_id1 = got_object_id_dup(
3913 got_object_commit_get_tree_id(pcommit));
3914 if (tree_id1 == NULL) {
3915 got_object_commit_close(pcommit);
3916 return got_error_from_errno("got_object_id_dup");
3918 got_object_commit_close(pcommit);
3923 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3928 tree_id2 = got_object_commit_get_tree_id(commit);
3929 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3933 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3934 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3937 got_object_tree_close(tree1);
3939 got_object_tree_close(tree2);
3940 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3941 err = got_error_from_errno("close");
3942 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3943 err = got_error_from_errno("close");
3944 if (f1 && fclose(f1) == EOF && err == NULL)
3945 err = got_error_from_errno("fclose");
3946 if (f2 && fclose(f2) == EOF && err == NULL)
3947 err = got_error_from_errno("fclose");
3952 static const struct got_error *
3953 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3954 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3955 struct got_repository *repo, FILE *outfile)
3957 const struct got_error *err = NULL;
3958 struct got_commit_object *pcommit = NULL;
3959 char *id_str1 = NULL, *id_str2 = NULL;
3960 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3961 struct got_object_qid *qid;
3963 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3965 err = got_object_open_as_commit(&pcommit, repo,
3969 err = got_object_id_str(&id_str1, &qid->id);
3974 err = got_object_id_str(&id_str2, id);
3978 if (path && path[0] != '\0') {
3980 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3984 err = got_object_id_by_path(&obj_id1, repo,
3987 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3993 err = got_object_get_type(&obj_type, repo, obj_id2);
3999 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4000 fprintf(outfile, "commit - %s\n",
4001 id_str1 ? id_str1 : "/dev/null");
4002 fprintf(outfile, "commit + %s\n", id_str2);
4004 case GOT_OBJ_TYPE_BLOB:
4005 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
4006 0, 0, dsa, repo, outfile);
4008 case GOT_OBJ_TYPE_TREE:
4009 err = diff_trees(obj_id1, obj_id2, path, diff_context,
4010 0, 0, dsa, repo, outfile);
4013 err = got_error(GOT_ERR_OBJ_TYPE);
4019 obj_id2 = got_object_commit_get_tree_id(commit);
4021 obj_id1 = got_object_commit_get_tree_id(pcommit);
4023 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4024 fprintf(outfile, "commit - %s\n",
4025 id_str1 ? id_str1 : "/dev/null");
4026 fprintf(outfile, "commit + %s\n", id_str2);
4027 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
4028 dsa, repo, outfile);
4034 got_object_commit_close(pcommit);
4039 get_datestr(time_t *time, char *datebuf)
4041 struct tm mytm, *tm;
4044 tm = gmtime_r(time, &mytm);
4047 s = asctime_r(tm, datebuf);
4050 p = strchr(s, '\n');
4056 static const struct got_error *
4057 match_commit(int *have_match, struct got_object_id *id,
4058 struct got_commit_object *commit, regex_t *regex)
4060 const struct got_error *err = NULL;
4061 regmatch_t regmatch;
4062 char *id_str = NULL, *logmsg = NULL;
4066 err = got_object_id_str(&id_str, id);
4070 err = got_object_commit_get_logmsg(&logmsg, commit);
4074 if (regexec(regex, got_object_commit_get_author(commit), 1,
4075 ®match, 0) == 0 ||
4076 regexec(regex, got_object_commit_get_committer(commit), 1,
4077 ®match, 0) == 0 ||
4078 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4079 regexec(regex, logmsg, 1, ®match, 0) == 0)
4088 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4091 regmatch_t regmatch;
4092 struct got_pathlist_entry *pe;
4096 TAILQ_FOREACH(pe, changed_paths, entry) {
4097 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4104 static const struct got_error *
4105 match_patch(int *have_match, struct got_commit_object *commit,
4106 struct got_object_id *id, const char *path, int diff_context,
4107 struct got_repository *repo, regex_t *regex, FILE *f)
4109 const struct got_error *err = NULL;
4111 size_t linesize = 0;
4112 regmatch_t regmatch;
4116 err = got_opentemp_truncate(f);
4120 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4124 if (fseeko(f, 0L, SEEK_SET) == -1) {
4125 err = got_error_from_errno("fseeko");
4129 while (getline(&line, &linesize, f) != -1) {
4130 if (regexec(regex, line, 1, ®match, 0) == 0) {
4140 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4142 static const struct got_error*
4143 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4144 struct got_object_id *id, struct got_repository *repo,
4147 static const struct got_error *err = NULL;
4148 struct got_reflist_entry *re;
4154 TAILQ_FOREACH(re, refs, entry) {
4155 struct got_tag_object *tag = NULL;
4156 struct got_object_id *ref_id;
4159 name = got_ref_get_name(re->ref);
4160 if (strcmp(name, GOT_REF_HEAD) == 0)
4162 if (strncmp(name, "refs/", 5) == 0)
4164 if (strncmp(name, "got/", 4) == 0)
4166 if (strncmp(name, "heads/", 6) == 0)
4168 if (strncmp(name, "remotes/", 8) == 0) {
4172 s = strstr(name, "/" GOT_REF_HEAD);
4173 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4176 err = got_ref_resolve(&ref_id, repo, re->ref);
4179 if (strncmp(name, "tags/", 5) == 0) {
4180 err = got_object_open_as_tag(&tag, repo, ref_id);
4182 if (err->code != GOT_ERR_OBJ_TYPE) {
4186 /* Ref points at something other than a tag. */
4191 cmp = got_object_id_cmp(tag ?
4192 got_object_tag_get_object_id(tag) : ref_id, id);
4195 got_object_tag_close(tag);
4199 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4200 s ? ", " : "", name) == -1) {
4201 err = got_error_from_errno("asprintf");
4212 static const struct got_error *
4213 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4214 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4216 const struct got_error *err = NULL;
4217 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4218 char *comma, *s, *nl;
4219 struct got_reflist_head *refs;
4220 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4222 time_t committer_time;
4224 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4226 err = build_refs_str(&ref_str, refs, id, repo, 1);
4230 /* Display the first matching ref only. */
4231 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4235 if (ref_str == NULL) {
4236 err = got_object_id_str(&id_str, id);
4241 committer_time = got_object_commit_get_committer_time(commit);
4242 if (gmtime_r(&committer_time, &tm) == NULL) {
4243 err = got_error_from_errno("gmtime_r");
4246 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4247 err = got_error(GOT_ERR_NO_SPACE);
4251 err = got_object_commit_get_logmsg(&logmsg0, commit);
4256 while (isspace((unsigned char)s[0]))
4259 nl = strchr(s, '\n');
4265 printf("%s%-7s %s\n", datebuf, ref_str, s);
4267 printf("%s%.7s %s\n", datebuf, id_str, s);
4269 if (fflush(stdout) != 0 && err == NULL)
4270 err = got_error_from_errno("fflush");
4278 static const struct got_error *
4279 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4281 struct got_pathlist_entry *pe;
4284 printf("%s\n", header);
4286 TAILQ_FOREACH(pe, dsa->paths, entry) {
4287 struct got_diff_changed_path *cp = pe->data;
4288 int pad = dsa->max_path_len - pe->path_len + 1;
4290 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4291 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4293 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4294 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4295 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4297 if (fflush(stdout) != 0)
4298 return got_error_from_errno("fflush");
4303 static const struct got_error *
4309 if (fseeko(f, 0L, SEEK_SET) == -1)
4310 return got_error_from_errno("fseek");
4313 r = fread(buf, 1, sizeof(buf), f);
4316 return got_error_from_errno("fread");
4320 if (fwrite(buf, 1, r, stdout) != r)
4321 return got_ferror(stdout, GOT_ERR_IO);
4327 static const struct got_error *
4328 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4329 struct got_repository *repo, const char *path,
4330 struct got_pathlist_head *changed_paths,
4331 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4332 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4335 const struct got_error *err = NULL;
4337 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4339 time_t committer_time;
4340 const char *author, *committer;
4341 char *refs_str = NULL;
4343 err = got_object_id_str(&id_str, id);
4347 if (custom_refs_str == NULL) {
4348 struct got_reflist_head *refs;
4349 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4351 err = build_refs_str(&refs_str, refs, id, repo, 0);
4357 printf(GOT_COMMIT_SEP_STR);
4358 if (custom_refs_str)
4359 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4362 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4363 refs_str ? " (" : "", refs_str ? refs_str : "",
4364 refs_str ? ")" : "");
4369 printf("from: %s\n", got_object_commit_get_author(commit));
4370 author = got_object_commit_get_author(commit);
4371 committer = got_object_commit_get_committer(commit);
4372 if (strcmp(author, committer) != 0)
4373 printf("via: %s\n", committer);
4374 committer_time = got_object_commit_get_committer_time(commit);
4375 datestr = get_datestr(&committer_time, datebuf);
4377 printf("date: %s UTC\n", datestr);
4378 if (got_object_commit_get_nparents(commit) > 1) {
4379 const struct got_object_id_queue *parent_ids;
4380 struct got_object_qid *qid;
4382 parent_ids = got_object_commit_get_parent_ids(commit);
4383 STAILQ_FOREACH(qid, parent_ids, entry) {
4384 err = got_object_id_str(&id_str, &qid->id);
4387 printf("parent %d: %s\n", n++, id_str);
4393 err = got_object_commit_get_logmsg(&logmsg0, commit);
4399 line = strsep(&logmsg, "\n");
4401 printf(" %s\n", line);
4405 if (changed_paths && diffstat == NULL) {
4406 struct got_pathlist_entry *pe;
4408 TAILQ_FOREACH(pe, changed_paths, entry) {
4409 struct got_diff_changed_path *cp = pe->data;
4411 printf(" %c %s\n", cp->status, pe->path);
4419 err = got_error_from_errno("got_opentemp");
4424 err = print_patch(commit, id, path, diff_context, diffstat,
4425 repo, diffstat == NULL ? stdout : f);
4430 err = print_diffstat(diffstat, NULL);
4442 if (fflush(stdout) != 0 && err == NULL)
4443 err = got_error_from_errno("fflush");
4445 if (f && fclose(f) == EOF && err == NULL)
4446 err = got_error_from_errno("fclose");
4452 static const struct got_error *
4453 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4454 struct got_repository *repo, const char *path, int show_changed_paths,
4455 int show_diffstat, int show_patch, const char *search_pattern,
4456 int diff_context, int limit, int log_branches, int reverse_display_order,
4457 struct got_reflist_object_id_map *refs_idmap, int one_line, int toposort,
4460 const struct got_error *err;
4461 struct got_commit_graph *graph;
4464 struct got_object_id_queue reversed_commits;
4465 struct got_object_qid *qid;
4466 struct got_commit_object *commit;
4467 struct got_pathlist_head changed_paths;
4469 STAILQ_INIT(&reversed_commits);
4470 TAILQ_INIT(&changed_paths);
4472 if (search_pattern && regcomp(®ex, search_pattern,
4473 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4474 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4476 err = got_commit_graph_open(&graph, path, !log_branches);
4479 if (log_branches && toposort) {
4480 err = got_commit_graph_toposort(graph, root_id, repo,
4481 check_cancelled, NULL);
4483 err = got_commit_graph_bfsort(graph, root_id, repo,
4484 check_cancelled, NULL);
4489 struct got_object_id id;
4490 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4491 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4493 if (sigint_received || sigpipe_received)
4496 err = got_commit_graph_iter_next(&id, graph, repo,
4497 check_cancelled, NULL);
4499 if (err->code == GOT_ERR_ITER_COMPLETED)
4504 err = got_object_open_as_commit(&commit, repo, &id);
4508 if (((show_changed_paths && !show_diffstat) ||
4509 (show_diffstat && !show_patch))
4510 && !reverse_display_order) {
4511 err = get_changed_paths(&changed_paths, commit, repo,
4512 show_diffstat ? &dsa : NULL);
4517 if (search_pattern) {
4518 err = match_commit(&have_match, &id, commit, ®ex);
4520 got_object_commit_close(commit);
4523 if (have_match == 0 && show_changed_paths)
4524 match_changed_paths(&have_match,
4525 &changed_paths, ®ex);
4526 if (have_match == 0 && show_patch) {
4527 err = match_patch(&have_match, commit, &id,
4528 path, diff_context, repo, ®ex, tmpfile);
4532 if (have_match == 0) {
4533 got_object_commit_close(commit);
4534 got_pathlist_free(&changed_paths,
4535 GOT_PATHLIST_FREE_ALL);
4540 if (reverse_display_order) {
4541 err = got_object_qid_alloc(&qid, &id);
4544 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4545 got_object_commit_close(commit);
4548 err = print_commit_oneline(commit, &id,
4551 err = print_commit(commit, &id, repo, path,
4552 (show_changed_paths || show_diffstat) ?
4553 &changed_paths : NULL,
4554 show_diffstat ? &dsa : NULL, show_patch,
4555 diff_context, refs_idmap, NULL, NULL);
4556 got_object_commit_close(commit);
4560 if ((limit && --limit == 0) ||
4561 (end_id && got_object_id_cmp(&id, end_id) == 0))
4564 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4566 if (reverse_display_order) {
4567 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4568 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4569 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4571 err = got_object_open_as_commit(&commit, repo,
4575 if ((show_changed_paths && !show_diffstat) ||
4576 (show_diffstat && !show_patch)) {
4577 err = get_changed_paths(&changed_paths, commit,
4578 repo, show_diffstat ? &dsa : NULL);
4583 err = print_commit_oneline(commit, &qid->id,
4586 err = print_commit(commit, &qid->id, repo, path,
4587 (show_changed_paths || show_diffstat) ?
4588 &changed_paths : NULL,
4589 show_diffstat ? &dsa : NULL, show_patch,
4590 diff_context, refs_idmap, NULL, NULL);
4591 got_object_commit_close(commit);
4594 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4598 while (!STAILQ_EMPTY(&reversed_commits)) {
4599 qid = STAILQ_FIRST(&reversed_commits);
4600 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4601 got_object_qid_free(qid);
4603 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4606 got_commit_graph_close(graph);
4613 fprintf(stderr, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4614 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4615 "[path]\n", getprogname());
4620 get_default_log_limit(void)
4622 const char *got_default_log_limit;
4626 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4627 if (got_default_log_limit == NULL)
4629 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4635 static const struct got_error *
4636 cmd_log(int argc, char *argv[])
4638 const struct got_error *error;
4639 struct got_repository *repo = NULL;
4640 struct got_worktree *worktree = NULL;
4641 struct got_object_id *start_id = NULL, *end_id = NULL;
4642 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4643 char *keyword_idstr = NULL;
4644 const char *start_commit = NULL, *end_commit = NULL;
4645 const char *search_pattern = NULL;
4646 int diff_context = -1, ch;
4647 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4648 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4651 struct got_reflist_head refs;
4652 struct got_reflist_object_id_map *refs_idmap = NULL;
4653 FILE *tmpfile = NULL;
4654 int *pack_fds = NULL;
4659 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4665 limit = get_default_log_limit();
4667 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:stx:")) != -1) {
4673 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4676 errx(1, "number of context lines is %s: %s",
4680 start_commit = optarg;
4686 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4688 errx(1, "number of commits is %s: %s",
4692 show_changed_paths = 1;
4698 reverse_display_order = 1;
4701 repo_path = realpath(optarg, NULL);
4702 if (repo_path == NULL)
4703 return got_error_from_errno2("realpath",
4705 got_path_strip_trailing_slashes(repo_path);
4708 search_pattern = optarg;
4717 end_commit = optarg;
4728 if (diff_context == -1)
4730 else if (!show_patch)
4731 errx(1, "-C requires -p");
4733 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4734 errx(1, "cannot use -s with -d, -p or -P");
4736 cwd = getcwd(NULL, 0);
4738 error = got_error_from_errno("getcwd");
4742 error = got_repo_pack_fds_open(&pack_fds);
4746 if (repo_path == NULL) {
4747 error = got_worktree_open(&worktree, cwd,
4748 GOT_WORKTREE_GOT_DIR);
4749 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4756 error = got_worktree_resolve_path(&path, worktree,
4761 path = strdup(argv[0]);
4763 error = got_error_from_errno("strdup");
4767 } else if (argc != 0)
4770 if (repo_path == NULL) {
4771 repo_path = worktree ?
4772 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4774 if (repo_path == NULL) {
4775 error = got_error_from_errno("strdup");
4779 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4783 error = apply_unveil(got_repo_get_path(repo), 1,
4784 worktree ? got_worktree_get_root_path(worktree) : NULL);
4788 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4792 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4796 if (start_commit == NULL) {
4797 struct got_reference *head_ref;
4798 struct got_commit_object *commit = NULL;
4799 error = got_ref_open(&head_ref, repo,
4800 worktree ? got_worktree_get_head_ref_name(worktree)
4804 error = got_ref_resolve(&start_id, repo, head_ref);
4805 got_ref_close(head_ref);
4808 error = got_object_open_as_commit(&commit, repo,
4812 got_object_commit_close(commit);
4814 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4818 if (keyword_idstr != NULL)
4819 start_commit = keyword_idstr;
4821 error = got_repo_match_object_id(&start_id, NULL,
4822 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4826 if (end_commit != NULL) {
4827 error = got_keyword_to_idstr(&keyword_idstr, end_commit,
4831 if (keyword_idstr != NULL)
4832 end_commit = keyword_idstr;
4834 error = got_repo_match_object_id(&end_id, NULL,
4835 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4842 * If a path was specified on the command line it was resolved
4843 * to a path in the work tree above. Prepend the work tree's
4844 * path prefix to obtain the corresponding in-repository path.
4848 prefix = got_worktree_get_path_prefix(worktree);
4849 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4850 (path[0] != '\0') ? "/" : "", path) == -1) {
4851 error = got_error_from_errno("asprintf");
4856 error = got_repo_map_path(&in_repo_path, repo,
4862 path = in_repo_path;
4866 /* Release work tree lock. */
4867 got_worktree_close(worktree);
4871 if (search_pattern && show_patch) {
4872 tmpfile = got_opentemp();
4873 if (tmpfile == NULL) {
4874 error = got_error_from_errno("got_opentemp");
4879 error = print_commits(start_id, end_id, repo, path ? path : "",
4880 show_changed_paths, show_diffstat, show_patch, search_pattern,
4881 diff_context, limit, log_branches, reverse_display_order,
4882 refs_idmap, one_line, toposort, tmpfile);
4889 free(keyword_idstr);
4891 got_worktree_close(worktree);
4893 const struct got_error *close_err = got_repo_close(repo);
4898 const struct got_error *pack_err =
4899 got_repo_pack_fds_close(pack_fds);
4904 got_reflist_object_id_map_free(refs_idmap);
4905 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4906 error = got_error_from_errno("fclose");
4907 got_ref_list_free(&refs);
4914 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4915 "[-r repository-path] [object1 object2 | path ...]\n",
4920 struct print_diff_arg {
4921 struct got_repository *repo;
4922 struct got_worktree *worktree;
4923 struct got_diffstat_cb_arg *diffstat;
4928 enum got_diff_algorithm diff_algo;
4929 int ignore_whitespace;
4930 int force_text_diff;
4937 * Create a file which contains the target path of a symlink so we can feed
4938 * it as content to the diff engine.
4940 static const struct got_error *
4941 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4942 const char *abspath)
4944 const struct got_error *err = NULL;
4945 char target_path[PATH_MAX];
4946 ssize_t target_len, outlen;
4951 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4952 if (target_len == -1)
4953 return got_error_from_errno2("readlinkat", abspath);
4955 target_len = readlink(abspath, target_path, PATH_MAX);
4956 if (target_len == -1)
4957 return got_error_from_errno2("readlink", abspath);
4960 *fd = got_opentempfd();
4962 return got_error_from_errno("got_opentempfd");
4964 outlen = write(*fd, target_path, target_len);
4966 err = got_error_from_errno("got_opentempfd");
4970 if (lseek(*fd, 0, SEEK_SET) == -1) {
4971 err = got_error_from_errno2("lseek", abspath);
4982 static const struct got_error *
4983 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4984 const char *path, struct got_object_id *blob_id,
4985 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4986 int dirfd, const char *de_name)
4988 struct print_diff_arg *a = arg;
4989 const struct got_error *err = NULL;
4990 struct got_blob_object *blob1 = NULL;
4991 int fd = -1, fd1 = -1, fd2 = -1;
4993 char *abspath = NULL, *label1 = NULL;
4998 memset(&sb, 0, sizeof(sb));
5000 if (a->diff_staged) {
5001 if (staged_status != GOT_STATUS_MODIFY &&
5002 staged_status != GOT_STATUS_ADD &&
5003 staged_status != GOT_STATUS_DELETE)
5006 if (staged_status == GOT_STATUS_DELETE)
5008 if (status == GOT_STATUS_NONEXISTENT)
5009 return got_error_set_errno(ENOENT, path);
5010 if (status != GOT_STATUS_MODIFY &&
5011 status != GOT_STATUS_ADD &&
5012 status != GOT_STATUS_DELETE &&
5013 status != GOT_STATUS_CONFLICT)
5017 err = got_opentemp_truncate(a->f1);
5019 return got_error_from_errno("got_opentemp_truncate");
5020 err = got_opentemp_truncate(a->f2);
5022 return got_error_from_errno("got_opentemp_truncate");
5024 if (!a->header_shown) {
5025 if (fprintf(a->outfile, "diff %s%s\n",
5026 a->diff_staged ? "-s " : "",
5027 got_worktree_get_root_path(a->worktree)) < 0) {
5028 err = got_error_from_errno("fprintf");
5031 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
5032 err = got_error_from_errno("fprintf");
5035 if (fprintf(a->outfile, "path + %s%s\n",
5036 got_worktree_get_root_path(a->worktree),
5037 a->diff_staged ? " (staged changes)" : "") < 0) {
5038 err = got_error_from_errno("fprintf");
5041 a->header_shown = 1;
5044 if (a->diff_staged) {
5045 const char *label1 = NULL, *label2 = NULL;
5046 switch (staged_status) {
5047 case GOT_STATUS_MODIFY:
5051 case GOT_STATUS_ADD:
5054 case GOT_STATUS_DELETE:
5058 return got_error(GOT_ERR_FILE_STATUS);
5060 fd1 = got_opentempfd();
5062 err = got_error_from_errno("got_opentempfd");
5065 fd2 = got_opentempfd();
5067 err = got_error_from_errno("got_opentempfd");
5070 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5071 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5072 a->diff_algo, a->diff_context, a->ignore_whitespace,
5073 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5077 fd1 = got_opentempfd();
5079 err = got_error_from_errno("got_opentempfd");
5083 if (staged_status == GOT_STATUS_ADD ||
5084 staged_status == GOT_STATUS_MODIFY) {
5086 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5090 err = got_object_id_str(&id_str, staged_blob_id);
5093 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5094 err = got_error_from_errno("asprintf");
5099 } else if (status != GOT_STATUS_ADD) {
5100 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5106 if (status != GOT_STATUS_DELETE) {
5107 if (asprintf(&abspath, "%s/%s",
5108 got_worktree_get_root_path(a->worktree), path) == -1) {
5109 err = got_error_from_errno("asprintf");
5114 fd = openat(dirfd, de_name,
5115 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5117 if (!got_err_open_nofollow_on_symlink()) {
5118 err = got_error_from_errno2("openat",
5122 err = get_symlink_target_file(&fd, dirfd,
5128 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5130 if (!got_err_open_nofollow_on_symlink()) {
5131 err = got_error_from_errno2("open",
5135 err = get_symlink_target_file(&fd, dirfd,
5141 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5142 err = got_error_from_errno2("fstatat", abspath);
5145 f2 = fdopen(fd, "r");
5147 err = got_error_from_errno2("fdopen", abspath);
5155 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5161 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5162 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5163 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5165 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5166 err = got_error_from_errno("close");
5167 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5168 err = got_error_from_errno("close");
5170 got_object_blob_close(blob1);
5171 if (fd != -1 && close(fd) == -1 && err == NULL)
5172 err = got_error_from_errno("close");
5173 if (f2 && fclose(f2) == EOF && err == NULL)
5174 err = got_error_from_errno("fclose");
5179 static const struct got_error *
5180 cmd_diff(int argc, char *argv[])
5182 const struct got_error *error;
5183 struct got_repository *repo = NULL;
5184 struct got_worktree *worktree = NULL;
5185 char *cwd = NULL, *repo_path = NULL;
5186 const char *commit_args[2] = { NULL, NULL };
5187 int ncommit_args = 0;
5188 struct got_object_id *ids[2] = { NULL, NULL };
5189 char *labels[2] = { NULL, NULL };
5190 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5191 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5192 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5194 struct got_reflist_head refs;
5195 struct got_pathlist_head diffstat_paths, paths;
5196 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5197 int fd1 = -1, fd2 = -1;
5198 int *pack_fds = NULL;
5199 struct got_diffstat_cb_arg dsa;
5201 memset(&dsa, 0, sizeof(dsa));
5205 TAILQ_INIT(&diffstat_paths);
5208 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5213 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5216 force_text_diff = 1;
5219 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5222 errx(1, "number of context lines is %s: %s",
5226 if (ncommit_args >= 2)
5227 errx(1, "too many -c options used");
5228 commit_args[ncommit_args++] = optarg;
5237 repo_path = realpath(optarg, NULL);
5238 if (repo_path == NULL)
5239 return got_error_from_errno2("realpath",
5241 got_path_strip_trailing_slashes(repo_path);
5248 ignore_whitespace = 1;
5259 cwd = getcwd(NULL, 0);
5261 error = got_error_from_errno("getcwd");
5265 error = got_repo_pack_fds_open(&pack_fds);
5269 if (repo_path == NULL) {
5270 error = got_worktree_open(&worktree, cwd,
5271 GOT_WORKTREE_GOT_DIR);
5272 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5278 strdup(got_worktree_get_repo_path(worktree));
5279 if (repo_path == NULL) {
5280 error = got_error_from_errno("strdup");
5284 repo_path = strdup(cwd);
5285 if (repo_path == NULL) {
5286 error = got_error_from_errno("strdup");
5292 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5297 if (show_diffstat) {
5298 dsa.paths = &diffstat_paths;
5299 dsa.force_text = force_text_diff;
5300 dsa.ignore_ws = ignore_whitespace;
5301 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5304 if (rflag || worktree == NULL || ncommit_args > 0) {
5306 error = got_error_msg(GOT_ERR_NOT_IMPL,
5307 "-P option can only be used when diffing "
5312 error = got_error_msg(GOT_ERR_NOT_IMPL,
5313 "-s option can only be used when diffing "
5319 error = apply_unveil(got_repo_get_path(repo), 1,
5320 worktree ? got_worktree_get_root_path(worktree) : NULL);
5324 if ((!force_path && argc == 2) || ncommit_args > 0) {
5325 int obj_type = (ncommit_args > 0 ?
5326 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5327 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5331 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5333 char *keyword_idstr = NULL;
5335 if (ncommit_args > 0)
5336 arg = commit_args[i];
5340 error = got_keyword_to_idstr(&keyword_idstr, arg,
5344 if (keyword_idstr != NULL)
5345 arg = keyword_idstr;
5347 error = got_repo_match_object_id(&ids[i], &labels[i],
5348 arg, obj_type, &refs, repo);
5349 free(keyword_idstr);
5351 if (error->code != GOT_ERR_NOT_REF &&
5352 error->code != GOT_ERR_NO_OBJ)
5354 if (ncommit_args > 0)
5362 f1 = got_opentemp();
5364 error = got_error_from_errno("got_opentemp");
5368 f2 = got_opentemp();
5370 error = got_error_from_errno("got_opentemp");
5374 outfile = got_opentemp();
5375 if (outfile == NULL) {
5376 error = got_error_from_errno("got_opentemp");
5380 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5381 struct print_diff_arg arg;
5384 if (worktree == NULL) {
5385 if (argc == 2 && ids[0] == NULL) {
5386 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5388 } else if (argc == 2 && ids[1] == NULL) {
5389 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5391 } else if (argc > 0) {
5392 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5393 "%s", "specified paths cannot be resolved");
5396 error = got_error(GOT_ERR_NOT_WORKTREE);
5401 error = get_worktree_paths_from_argv(&paths, argc, argv,
5406 error = got_object_id_str(&id_str,
5407 got_worktree_get_base_commit_id(worktree));
5411 arg.worktree = worktree;
5412 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5413 arg.diff_context = diff_context;
5414 arg.id_str = id_str;
5415 arg.header_shown = 0;
5416 arg.diff_staged = diff_staged;
5417 arg.ignore_whitespace = ignore_whitespace;
5418 arg.force_text_diff = force_text_diff;
5419 arg.diffstat = show_diffstat ? &dsa : NULL;
5422 arg.outfile = outfile;
5424 error = got_worktree_status(worktree, &paths, repo, 0,
5425 print_diff, &arg, check_cancelled, NULL);
5430 if (show_diffstat && dsa.nfiles > 0) {
5433 if (asprintf(&header, "diffstat %s%s",
5434 diff_staged ? "-s " : "",
5435 got_worktree_get_root_path(worktree)) == -1) {
5436 error = got_error_from_errno("asprintf");
5440 error = print_diffstat(&dsa, header);
5446 error = printfile(outfile);
5450 if (ncommit_args == 1) {
5451 struct got_commit_object *commit;
5452 error = got_object_open_as_commit(&commit, repo, ids[0]);
5456 labels[1] = labels[0];
5458 if (got_object_commit_get_nparents(commit) > 0) {
5459 const struct got_object_id_queue *pids;
5460 struct got_object_qid *pid;
5461 pids = got_object_commit_get_parent_ids(commit);
5462 pid = STAILQ_FIRST(pids);
5463 ids[0] = got_object_id_dup(&pid->id);
5464 if (ids[0] == NULL) {
5465 error = got_error_from_errno(
5466 "got_object_id_dup");
5467 got_object_commit_close(commit);
5470 error = got_object_id_str(&labels[0], ids[0]);
5472 got_object_commit_close(commit);
5477 labels[0] = strdup("/dev/null");
5478 if (labels[0] == NULL) {
5479 error = got_error_from_errno("strdup");
5480 got_object_commit_close(commit);
5485 got_object_commit_close(commit);
5488 if (ncommit_args == 0 && argc > 2) {
5489 error = got_error_msg(GOT_ERR_BAD_PATH,
5490 "path arguments cannot be used when diffing two objects");
5495 error = got_object_get_type(&type1, repo, ids[0]);
5500 error = got_object_get_type(&type2, repo, ids[1]);
5503 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5504 error = got_error(GOT_ERR_OBJ_TYPE);
5507 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5508 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5509 "path arguments cannot be used when diffing blobs");
5513 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5515 struct got_pathlist_entry *new;
5519 error = got_worktree_resolve_path(&p, worktree,
5523 prefix = got_worktree_get_path_prefix(worktree);
5524 while (prefix[0] == '/')
5526 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5527 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5529 error = got_error_from_errno("asprintf");
5535 char *mapped_path, *s;
5536 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5542 in_repo_path = strdup(s);
5543 if (in_repo_path == NULL) {
5544 error = got_error_from_errno("asprintf");
5551 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5552 if (error || new == NULL /* duplicate */)
5559 /* Release work tree lock. */
5560 got_worktree_close(worktree);
5564 fd1 = got_opentempfd();
5566 error = got_error_from_errno("got_opentempfd");
5570 fd2 = got_opentempfd();
5572 error = got_error_from_errno("got_opentempfd");
5576 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5577 case GOT_OBJ_TYPE_BLOB:
5578 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5579 fd1, fd2, ids[0], ids[1], NULL, NULL,
5580 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5581 ignore_whitespace, force_text_diff,
5582 show_diffstat ? &dsa : NULL, repo, outfile);
5584 case GOT_OBJ_TYPE_TREE:
5585 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5586 ids[0], ids[1], &paths, "", "",
5587 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5588 ignore_whitespace, force_text_diff,
5589 show_diffstat ? &dsa : NULL, repo, outfile);
5591 case GOT_OBJ_TYPE_COMMIT:
5592 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5593 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5594 fd1, fd2, ids[0], ids[1], &paths,
5595 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5596 ignore_whitespace, force_text_diff,
5597 show_diffstat ? &dsa : NULL, repo, outfile);
5600 error = got_error(GOT_ERR_OBJ_TYPE);
5605 if (show_diffstat && dsa.nfiles > 0) {
5606 char *header = NULL;
5608 if (asprintf(&header, "diffstat %s %s",
5609 labels[0], labels[1]) == -1) {
5610 error = got_error_from_errno("asprintf");
5614 error = print_diffstat(&dsa, header);
5620 error = printfile(outfile);
5628 got_worktree_close(worktree);
5630 const struct got_error *close_err = got_repo_close(repo);
5635 const struct got_error *pack_err =
5636 got_repo_pack_fds_close(pack_fds);
5640 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5641 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5642 got_ref_list_free(&refs);
5643 if (outfile && fclose(outfile) == EOF && error == NULL)
5644 error = got_error_from_errno("fclose");
5645 if (f1 && fclose(f1) == EOF && error == NULL)
5646 error = got_error_from_errno("fclose");
5647 if (f2 && fclose(f2) == EOF && error == NULL)
5648 error = got_error_from_errno("fclose");
5649 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5650 error = got_error_from_errno("close");
5651 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5652 error = got_error_from_errno("close");
5660 "usage: %s blame [-c commit] [-r repository-path] path\n",
5669 char datebuf[11]; /* YYYY-MM-DD + NUL */
5672 struct blame_cb_args {
5673 struct blame_line *lines;
5677 off_t *line_offsets;
5679 struct got_repository *repo;
5682 static const struct got_error *
5683 blame_cb(void *arg, int nlines, int lineno,
5684 struct got_commit_object *commit, struct got_object_id *id)
5686 const struct got_error *err = NULL;
5687 struct blame_cb_args *a = arg;
5688 struct blame_line *bline;
5690 size_t linesize = 0;
5693 time_t committer_time;
5695 if (nlines != a->nlines ||
5696 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5697 return got_error(GOT_ERR_RANGE);
5699 if (sigint_received)
5700 return got_error(GOT_ERR_ITER_COMPLETED);
5703 return NULL; /* no change in this commit */
5705 /* Annotate this line. */
5706 bline = &a->lines[lineno - 1];
5707 if (bline->annotated)
5709 err = got_object_id_str(&bline->id_str, id);
5713 bline->committer = strdup(got_object_commit_get_committer(commit));
5714 if (bline->committer == NULL) {
5715 err = got_error_from_errno("strdup");
5719 committer_time = got_object_commit_get_committer_time(commit);
5720 if (gmtime_r(&committer_time, &tm) == NULL)
5721 return got_error_from_errno("gmtime_r");
5722 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5724 err = got_error(GOT_ERR_NO_SPACE);
5727 bline->annotated = 1;
5729 /* Print lines annotated so far. */
5730 bline = &a->lines[a->lineno_cur - 1];
5731 if (!bline->annotated)
5734 offset = a->line_offsets[a->lineno_cur - 1];
5735 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5736 err = got_error_from_errno("fseeko");
5740 while (a->lineno_cur <= a->nlines && bline->annotated) {
5741 char *smallerthan, *at, *nl, *committer;
5744 if (getline(&line, &linesize, a->f) == -1) {
5746 err = got_error_from_errno("getline");
5750 committer = bline->committer;
5751 smallerthan = strchr(committer, '<');
5752 if (smallerthan && smallerthan[1] != '\0')
5753 committer = smallerthan + 1;
5754 at = strchr(committer, '@');
5757 len = strlen(committer);
5759 committer[8] = '\0';
5761 nl = strchr(line, '\n');
5764 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5765 bline->id_str, bline->datebuf, committer, line);
5768 bline = &a->lines[a->lineno_cur - 1];
5775 static const struct got_error *
5776 cmd_blame(int argc, char *argv[])
5778 const struct got_error *error;
5779 struct got_repository *repo = NULL;
5780 struct got_worktree *worktree = NULL;
5781 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5782 char *link_target = NULL;
5783 struct got_object_id *obj_id = NULL;
5784 struct got_object_id *commit_id = NULL;
5785 struct got_commit_object *commit = NULL;
5786 struct got_blob_object *blob = NULL;
5787 char *commit_id_str = NULL, *keyword_idstr = NULL;
5788 struct blame_cb_args bca;
5789 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5791 int *pack_fds = NULL;
5792 FILE *f1 = NULL, *f2 = NULL;
5794 fd1 = got_opentempfd();
5796 return got_error_from_errno("got_opentempfd");
5798 memset(&bca, 0, sizeof(bca));
5801 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5806 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5809 commit_id_str = optarg;
5812 repo_path = realpath(optarg, NULL);
5813 if (repo_path == NULL)
5814 return got_error_from_errno2("realpath",
5816 got_path_strip_trailing_slashes(repo_path);
5832 cwd = getcwd(NULL, 0);
5834 error = got_error_from_errno("getcwd");
5838 error = got_repo_pack_fds_open(&pack_fds);
5842 if (repo_path == NULL) {
5843 error = got_worktree_open(&worktree, cwd,
5844 GOT_WORKTREE_GOT_DIR);
5845 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5851 strdup(got_worktree_get_repo_path(worktree));
5852 if (repo_path == NULL) {
5853 error = got_error_from_errno("strdup");
5858 repo_path = strdup(cwd);
5859 if (repo_path == NULL) {
5860 error = got_error_from_errno("strdup");
5866 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5871 const char *prefix = got_worktree_get_path_prefix(worktree);
5874 error = got_worktree_resolve_path(&p, worktree, path);
5877 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5878 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5880 error = got_error_from_errno("asprintf");
5885 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5887 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5890 error = got_repo_map_path(&in_repo_path, repo, path);
5895 if (commit_id_str == NULL) {
5896 struct got_reference *head_ref;
5897 error = got_ref_open(&head_ref, repo, worktree ?
5898 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5901 error = got_ref_resolve(&commit_id, repo, head_ref);
5902 got_ref_close(head_ref);
5906 struct got_reflist_head refs;
5909 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5914 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5918 if (keyword_idstr != NULL)
5919 commit_id_str = keyword_idstr;
5921 error = got_repo_match_object_id(&commit_id, NULL,
5922 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5923 got_ref_list_free(&refs);
5929 /* Release work tree lock. */
5930 got_worktree_close(worktree);
5934 error = got_object_open_as_commit(&commit, repo, commit_id);
5938 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5943 error = got_object_id_by_path(&obj_id, repo, commit,
5944 link_target ? link_target : in_repo_path);
5948 error = got_object_get_type(&obj_type, repo, obj_id);
5952 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5953 error = got_error_path(link_target ? link_target : in_repo_path,
5958 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5961 bca.f = got_opentemp();
5962 if (bca.f == NULL) {
5963 error = got_error_from_errno("got_opentemp");
5966 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5967 &bca.line_offsets, bca.f, blob);
5968 if (error || bca.nlines == 0)
5971 /* Don't include \n at EOF in the blame line count. */
5972 if (bca.line_offsets[bca.nlines - 1] == filesize)
5975 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5976 if (bca.lines == NULL) {
5977 error = got_error_from_errno("calloc");
5981 bca.nlines_prec = 0;
5989 fd2 = got_opentempfd();
5991 error = got_error_from_errno("got_opentempfd");
5994 fd3 = got_opentempfd();
5996 error = got_error_from_errno("got_opentempfd");
5999 f1 = got_opentemp();
6001 error = got_error_from_errno("got_opentemp");
6004 f2 = got_opentemp();
6006 error = got_error_from_errno("got_opentemp");
6009 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
6010 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
6011 check_cancelled, NULL, fd2, fd3, f1, f2);
6013 free(keyword_idstr);
6021 got_object_commit_close(commit);
6023 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
6024 error = got_error_from_errno("close");
6025 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
6026 error = got_error_from_errno("close");
6027 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
6028 error = got_error_from_errno("close");
6029 if (f1 && fclose(f1) == EOF && error == NULL)
6030 error = got_error_from_errno("fclose");
6031 if (f2 && fclose(f2) == EOF && error == NULL)
6032 error = got_error_from_errno("fclose");
6035 got_object_blob_close(blob);
6037 got_worktree_close(worktree);
6039 const struct got_error *close_err = got_repo_close(repo);
6044 const struct got_error *pack_err =
6045 got_repo_pack_fds_close(pack_fds);
6050 for (i = 0; i < bca.nlines; i++) {
6051 struct blame_line *bline = &bca.lines[i];
6052 free(bline->id_str);
6053 free(bline->committer);
6057 free(bca.line_offsets);
6058 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6059 error = got_error_from_errno("fclose");
6066 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6067 "[path]\n", getprogname());
6071 static const struct got_error *
6072 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6073 const char *root_path, struct got_repository *repo)
6075 const struct got_error *err = NULL;
6076 int is_root_path = (strcmp(path, root_path) == 0);
6077 const char *modestr = "";
6078 mode_t mode = got_tree_entry_get_mode(te);
6079 char *link_target = NULL;
6081 path += strlen(root_path);
6082 while (path[0] == '/')
6085 if (got_object_tree_entry_is_submodule(te))
6087 else if (S_ISLNK(mode)) {
6090 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6093 for (i = 0; link_target[i] != '\0'; i++) {
6094 if (!isprint((unsigned char)link_target[i]))
6095 link_target[i] = '?';
6100 else if (S_ISDIR(mode))
6102 else if (mode & S_IXUSR)
6105 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6106 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6107 link_target ? " -> ": "", link_target ? link_target : "");
6113 static const struct got_error *
6114 print_tree(const char *path, struct got_commit_object *commit,
6115 int show_ids, int recurse, const char *root_path,
6116 struct got_repository *repo)
6118 const struct got_error *err = NULL;
6119 struct got_object_id *tree_id = NULL;
6120 struct got_tree_object *tree = NULL;
6123 err = got_object_id_by_path(&tree_id, repo, commit, path);
6127 err = got_object_open_as_tree(&tree, repo, tree_id);
6130 nentries = got_object_tree_get_nentries(tree);
6131 for (i = 0; i < nentries; i++) {
6132 struct got_tree_entry *te;
6135 if (sigint_received || sigpipe_received)
6138 te = got_object_tree_get_entry(tree, i);
6141 err = got_object_id_str(&id_str,
6142 got_tree_entry_get_id(te));
6145 if (asprintf(&id, "%s ", id_str) == -1) {
6146 err = got_error_from_errno("asprintf");
6152 err = print_entry(te, id, path, root_path, repo);
6157 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6159 if (asprintf(&child_path, "%s%s%s", path,
6160 path[0] == '/' && path[1] == '\0' ? "" : "/",
6161 got_tree_entry_get_name(te)) == -1) {
6162 err = got_error_from_errno("asprintf");
6165 err = print_tree(child_path, commit, show_ids, 1,
6174 got_object_tree_close(tree);
6179 static const struct got_error *
6180 cmd_tree(int argc, char *argv[])
6182 const struct got_error *error;
6183 struct got_repository *repo = NULL;
6184 struct got_worktree *worktree = NULL;
6185 const char *path, *refname = NULL;
6186 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6187 struct got_object_id *commit_id = NULL;
6188 struct got_commit_object *commit = NULL;
6189 char *commit_id_str = NULL, *keyword_idstr = NULL;
6190 int show_ids = 0, recurse = 0;
6192 int *pack_fds = NULL;
6195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6200 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6203 commit_id_str = optarg;
6212 repo_path = realpath(optarg, NULL);
6213 if (repo_path == NULL)
6214 return got_error_from_errno2("realpath",
6216 got_path_strip_trailing_slashes(repo_path);
6234 cwd = getcwd(NULL, 0);
6236 error = got_error_from_errno("getcwd");
6240 error = got_repo_pack_fds_open(&pack_fds);
6244 if (repo_path == NULL) {
6245 error = got_worktree_open(&worktree, cwd,
6246 GOT_WORKTREE_GOT_DIR);
6247 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6253 strdup(got_worktree_get_repo_path(worktree));
6254 if (repo_path == NULL)
6255 error = got_error_from_errno("strdup");
6259 repo_path = strdup(cwd);
6260 if (repo_path == NULL) {
6261 error = got_error_from_errno("strdup");
6267 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6272 const char *prefix = got_worktree_get_path_prefix(worktree);
6275 if (path == NULL || got_path_is_root_dir(path))
6277 error = got_worktree_resolve_path(&p, worktree, path);
6280 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6281 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6283 error = got_error_from_errno("asprintf");
6288 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6292 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6297 error = got_repo_map_path(&in_repo_path, repo, path);
6302 if (commit_id_str == NULL) {
6303 struct got_reference *head_ref;
6305 refname = got_worktree_get_head_ref_name(worktree);
6307 refname = GOT_REF_HEAD;
6308 error = got_ref_open(&head_ref, repo, refname, 0);
6311 error = got_ref_resolve(&commit_id, repo, head_ref);
6312 got_ref_close(head_ref);
6316 struct got_reflist_head refs;
6319 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6324 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6328 if (keyword_idstr != NULL)
6329 commit_id_str = keyword_idstr;
6331 error = got_repo_match_object_id(&commit_id, NULL,
6332 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6333 got_ref_list_free(&refs);
6339 /* Release work tree lock. */
6340 got_worktree_close(worktree);
6344 error = got_object_open_as_commit(&commit, repo, commit_id);
6348 error = print_tree(in_repo_path, commit, show_ids, recurse,
6349 in_repo_path, repo);
6351 free(keyword_idstr);
6357 got_object_commit_close(commit);
6359 got_worktree_close(worktree);
6361 const struct got_error *close_err = got_repo_close(repo);
6366 const struct got_error *pack_err =
6367 got_repo_pack_fds_close(pack_fds);
6377 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6378 "[-s status-codes] [path ...]\n", getprogname());
6382 struct got_status_arg {
6387 static const struct got_error *
6388 print_status(void *arg, unsigned char status, unsigned char staged_status,
6389 const char *path, struct got_object_id *blob_id,
6390 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6391 int dirfd, const char *de_name)
6393 struct got_status_arg *st = arg;
6395 if (status == staged_status && (status == GOT_STATUS_DELETE))
6396 status = GOT_STATUS_NO_CHANGE;
6397 if (st != NULL && st->status_codes) {
6398 size_t ncodes = strlen(st->status_codes);
6401 for (i = 0; i < ncodes ; i++) {
6403 if (status == st->status_codes[i] ||
6404 staged_status == st->status_codes[i]) {
6409 if (status == st->status_codes[i] ||
6410 staged_status == st->status_codes[i])
6415 if (st->suppress && j == 0)
6422 printf("%c%c %s\n", status, staged_status, path);
6426 static const struct got_error *
6427 cmd_status(int argc, char *argv[])
6429 const struct got_error *close_err, *error = NULL;
6430 struct got_repository *repo = NULL;
6431 struct got_worktree *worktree = NULL;
6432 struct got_status_arg st;
6434 struct got_pathlist_head paths;
6435 int ch, i, no_ignores = 0;
6436 int *pack_fds = NULL;
6440 memset(&st, 0, sizeof(st));
6441 st.status_codes = NULL;
6445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6450 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6456 if (st.status_codes != NULL && st.suppress == 0)
6457 option_conflict('S', 's');
6461 for (i = 0; optarg[i] != '\0'; i++) {
6462 switch (optarg[i]) {
6463 case GOT_STATUS_MODIFY:
6464 case GOT_STATUS_ADD:
6465 case GOT_STATUS_DELETE:
6466 case GOT_STATUS_CONFLICT:
6467 case GOT_STATUS_MISSING:
6468 case GOT_STATUS_OBSTRUCTED:
6469 case GOT_STATUS_UNVERSIONED:
6470 case GOT_STATUS_MODE_CHANGE:
6471 case GOT_STATUS_NONEXISTENT:
6474 errx(1, "invalid status code '%c'",
6478 if (ch == 's' && st.suppress)
6479 option_conflict('s', 'S');
6480 st.status_codes = optarg;
6491 cwd = getcwd(NULL, 0);
6493 error = got_error_from_errno("getcwd");
6497 error = got_repo_pack_fds_open(&pack_fds);
6501 error = got_worktree_open(&worktree, cwd,
6502 GOT_WORKTREE_GOT_DIR);
6504 if (error->code == GOT_ERR_NOT_WORKTREE)
6505 error = wrap_not_worktree_error(error, "status", cwd);
6509 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6514 error = apply_unveil(got_repo_get_path(repo), 1,
6515 got_worktree_get_root_path(worktree));
6519 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6523 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6524 print_status, &st, check_cancelled, NULL);
6527 const struct got_error *pack_err =
6528 got_repo_pack_fds_close(pack_fds);
6533 close_err = got_repo_close(repo);
6537 if (worktree != NULL) {
6538 close_err = got_worktree_close(worktree);
6543 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6551 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6552 "[-s reference] [name]\n", getprogname());
6556 static const struct got_error *
6557 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6559 static const struct got_error *err = NULL;
6560 struct got_reflist_head refs;
6561 struct got_reflist_entry *re;
6564 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6565 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6570 TAILQ_FOREACH(re, &refs, entry) {
6572 refstr = got_ref_to_str(re->ref);
6573 if (refstr == NULL) {
6574 err = got_error_from_errno("got_ref_to_str");
6577 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6581 got_ref_list_free(&refs);
6585 static const struct got_error *
6586 delete_ref_by_name(struct got_repository *repo, const char *refname)
6588 const struct got_error *err;
6589 struct got_reference *ref;
6591 err = got_ref_open(&ref, repo, refname, 0);
6595 err = delete_ref(repo, ref);
6600 static const struct got_error *
6601 add_ref(struct got_repository *repo, const char *refname, const char *target)
6603 const struct got_error *err = NULL;
6604 struct got_object_id *id = NULL;
6605 struct got_reference *ref = NULL;
6606 struct got_reflist_head refs;
6609 * Don't let the user create a reference name with a leading '-'.
6610 * While technically a valid reference name, this case is usually
6611 * an unintended typo.
6613 if (refname[0] == '-')
6614 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6617 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6620 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6622 got_ref_list_free(&refs);
6626 err = got_ref_alloc(&ref, refname, id);
6630 err = got_ref_write(ref, repo);
6638 static const struct got_error *
6639 add_symref(struct got_repository *repo, const char *refname, const char *target)
6641 const struct got_error *err = NULL;
6642 struct got_reference *ref = NULL;
6643 struct got_reference *target_ref = NULL;
6646 * Don't let the user create a reference name with a leading '-'.
6647 * While technically a valid reference name, this case is usually
6648 * an unintended typo.
6650 if (refname[0] == '-')
6651 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6653 err = got_ref_open(&target_ref, repo, target, 0);
6657 err = got_ref_alloc_symref(&ref, refname, target_ref);
6661 err = got_ref_write(ref, repo);
6664 got_ref_close(target_ref);
6670 static const struct got_error *
6671 cmd_ref(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_delete = 0, sort_by_time = 0;
6678 const char *obj_arg = NULL, *symref_target= NULL;
6679 char *refname = NULL, *keyword_idstr = NULL;
6680 int *pack_fds = NULL;
6683 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6684 "sendfd unveil", NULL) == -1)
6688 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6700 repo_path = realpath(optarg, NULL);
6701 if (repo_path == NULL)
6702 return got_error_from_errno2("realpath",
6704 got_path_strip_trailing_slashes(repo_path);
6707 symref_target = optarg;
6718 if (obj_arg && do_list)
6719 option_conflict('c', 'l');
6720 if (obj_arg && do_delete)
6721 option_conflict('c', 'd');
6722 if (obj_arg && symref_target)
6723 option_conflict('c', 's');
6724 if (symref_target && do_delete)
6725 option_conflict('s', 'd');
6726 if (symref_target && do_list)
6727 option_conflict('s', 'l');
6728 if (do_delete && do_list)
6729 option_conflict('d', 'l');
6730 if (sort_by_time && !do_list)
6731 errx(1, "-t option requires -l option");
6737 if (argc != 0 && argc != 1)
6740 refname = strdup(argv[0]);
6741 if (refname == NULL) {
6742 error = got_error_from_errno("strdup");
6749 refname = strdup(argv[0]);
6750 if (refname == NULL) {
6751 error = got_error_from_errno("strdup");
6757 got_path_strip_trailing_slashes(refname);
6759 cwd = getcwd(NULL, 0);
6761 error = got_error_from_errno("getcwd");
6765 error = got_repo_pack_fds_open(&pack_fds);
6769 if (repo_path == NULL) {
6770 error = got_worktree_open(&worktree, cwd,
6771 GOT_WORKTREE_GOT_DIR);
6772 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6778 strdup(got_worktree_get_repo_path(worktree));
6779 if (repo_path == NULL)
6780 error = got_error_from_errno("strdup");
6784 repo_path = strdup(cwd);
6785 if (repo_path == NULL) {
6786 error = got_error_from_errno("strdup");
6792 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6798 /* Remove "cpath" promise. */
6799 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6805 error = apply_unveil(got_repo_get_path(repo), do_list,
6806 worktree ? got_worktree_get_root_path(worktree) : NULL);
6811 error = list_refs(repo, refname, sort_by_time);
6813 error = delete_ref_by_name(repo, refname);
6814 else if (symref_target)
6815 error = add_symref(repo, refname, symref_target);
6817 if (obj_arg == NULL)
6820 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6824 if (keyword_idstr != NULL)
6825 obj_arg = keyword_idstr;
6827 error = add_ref(repo, refname, obj_arg);
6832 const struct got_error *close_err = got_repo_close(repo);
6837 got_worktree_close(worktree);
6839 const struct got_error *pack_err =
6840 got_repo_pack_fds_close(pack_fds);
6846 free(keyword_idstr);
6853 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6854 "[-r repository-path] [name]\n", getprogname());
6858 static const struct got_error *
6859 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6860 struct got_reference *ref)
6862 const struct got_error *err = NULL;
6863 const char *refname;
6867 refname = got_ref_get_name(ref);
6868 if (worktree && strcmp(refname,
6869 got_worktree_get_head_ref_name(worktree)) == 0) {
6870 err = got_worktree_get_state(&marker, repo, worktree,
6871 check_cancelled, NULL);
6876 if (strncmp(refname, "refs/heads/", 11) == 0)
6878 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6880 if (strncmp(refname, "refs/remotes/", 13) == 0)
6883 refstr = got_ref_to_str(ref);
6885 return got_error_from_errno("got_ref_to_str");
6887 printf("%c %s: %s\n", marker, refname, refstr);
6892 static const struct got_error *
6893 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6895 const char *refname;
6897 if (worktree == NULL)
6898 return got_error(GOT_ERR_NOT_WORKTREE);
6900 refname = got_worktree_get_head_ref_name(worktree);
6902 if (strncmp(refname, "refs/heads/", 11) == 0)
6904 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6907 printf("%s\n", refname);
6912 static const struct got_error *
6913 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6916 static const struct got_error *err = NULL;
6917 struct got_reflist_head refs;
6918 struct got_reflist_entry *re;
6919 struct got_reference *temp_ref = NULL;
6920 int rebase_in_progress, histedit_in_progress;
6925 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6930 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6935 if (rebase_in_progress || histedit_in_progress) {
6936 err = got_ref_open(&temp_ref, repo,
6937 got_worktree_get_head_ref_name(worktree), 0);
6940 list_branch(repo, worktree, temp_ref);
6941 got_ref_close(temp_ref);
6945 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6946 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6951 TAILQ_FOREACH(re, &refs, entry)
6952 list_branch(repo, worktree, re->ref);
6954 got_ref_list_free(&refs);
6956 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6957 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6962 TAILQ_FOREACH(re, &refs, entry)
6963 list_branch(repo, worktree, re->ref);
6965 got_ref_list_free(&refs);
6970 static const struct got_error *
6971 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6972 const char *branch_name)
6974 const struct got_error *err = NULL;
6975 struct got_reference *ref = NULL;
6976 char *refname, *remote_refname = NULL;
6978 if (strncmp(branch_name, "refs/", 5) == 0)
6980 if (strncmp(branch_name, "heads/", 6) == 0)
6982 else if (strncmp(branch_name, "remotes/", 8) == 0)
6985 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6986 return got_error_from_errno("asprintf");
6988 if (asprintf(&remote_refname, "refs/remotes/%s",
6989 branch_name) == -1) {
6990 err = got_error_from_errno("asprintf");
6994 err = got_ref_open(&ref, repo, refname, 0);
6996 const struct got_error *err2;
6997 if (err->code != GOT_ERR_NOT_REF)
7000 * Keep 'err' intact such that if neither branch exists
7001 * we report "refs/heads" rather than "refs/remotes" in
7002 * our error message.
7004 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7011 strcmp(got_worktree_get_head_ref_name(worktree),
7012 got_ref_get_name(ref)) == 0) {
7013 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7014 "will not delete this work tree's current branch");
7018 err = delete_ref(repo, ref);
7023 free(remote_refname);
7027 static const struct got_error *
7028 add_branch(struct got_repository *repo, const char *branch_name,
7029 struct got_object_id *base_commit_id)
7031 const struct got_error *err = NULL;
7032 struct got_reference *ref = NULL;
7033 char *refname = NULL;
7036 * Don't let the user create a branch name with a leading '-'.
7037 * While technically a valid reference name, this case is usually
7038 * an unintended typo.
7040 if (branch_name[0] == '-')
7041 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7043 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7046 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7047 err = got_error_from_errno("asprintf");
7051 err = got_ref_open(&ref, repo, refname, 0);
7053 err = got_error(GOT_ERR_BRANCH_EXISTS);
7055 } else if (err->code != GOT_ERR_NOT_REF)
7058 err = got_ref_alloc(&ref, refname, base_commit_id);
7062 err = got_ref_write(ref, repo);
7070 static const struct got_error *
7071 cmd_branch(int argc, char *argv[])
7073 const struct got_error *error = NULL;
7074 struct got_repository *repo = NULL;
7075 struct got_worktree *worktree = NULL;
7076 char *cwd = NULL, *repo_path = NULL;
7077 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7078 const char *delref = NULL, *commit_id_arg = NULL;
7079 struct got_reference *ref = NULL;
7080 struct got_pathlist_head paths;
7081 struct got_object_id *commit_id = NULL;
7082 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7083 int *pack_fds = NULL;
7088 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7089 "sendfd unveil", NULL) == -1)
7093 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7096 commit_id_arg = optarg;
7108 repo_path = realpath(optarg, NULL);
7109 if (repo_path == NULL)
7110 return got_error_from_errno2("realpath",
7112 got_path_strip_trailing_slashes(repo_path);
7123 if (do_list && delref)
7124 option_conflict('l', 'd');
7125 if (sort_by_time && !do_list)
7126 errx(1, "-t option requires -l option");
7131 if (!do_list && !delref && argc == 0)
7134 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7135 errx(1, "-c option can only be used when creating a branch");
7137 if (do_list || delref) {
7140 } else if (!do_show && argc != 1)
7143 cwd = getcwd(NULL, 0);
7145 error = got_error_from_errno("getcwd");
7149 error = got_repo_pack_fds_open(&pack_fds);
7153 if (repo_path == NULL) {
7154 error = got_worktree_open(&worktree, cwd,
7155 GOT_WORKTREE_GOT_DIR);
7156 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7162 strdup(got_worktree_get_repo_path(worktree));
7163 if (repo_path == NULL)
7164 error = got_error_from_errno("strdup");
7168 repo_path = strdup(cwd);
7169 if (repo_path == NULL) {
7170 error = got_error_from_errno("strdup");
7176 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7181 if (do_list || do_show) {
7182 /* Remove "cpath" promise. */
7183 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7189 error = apply_unveil(got_repo_get_path(repo), do_list,
7190 worktree ? got_worktree_get_root_path(worktree) : NULL);
7195 error = show_current_branch(repo, worktree);
7197 error = list_branches(repo, worktree, sort_by_time);
7199 error = delete_branch(repo, worktree, delref);
7201 struct got_reflist_head refs;
7203 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7207 if (commit_id_arg == NULL)
7208 commit_id_arg = worktree ?
7209 got_worktree_get_head_ref_name(worktree) :
7212 error = got_keyword_to_idstr(&keyword_idstr,
7213 commit_id_arg, repo, worktree);
7216 if (keyword_idstr != NULL)
7217 commit_id_arg = keyword_idstr;
7219 error = got_repo_match_object_id(&commit_id, NULL,
7220 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7221 got_ref_list_free(&refs);
7224 error = add_branch(repo, argv[0], commit_id);
7227 if (worktree && do_update) {
7228 struct got_update_progress_arg upa;
7229 char *branch_refname = NULL;
7231 error = got_object_id_str(&commit_id_str, commit_id);
7234 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7238 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7240 error = got_error_from_errno("asprintf");
7243 error = got_ref_open(&ref, repo, branch_refname, 0);
7244 free(branch_refname);
7247 error = switch_head_ref(ref, commit_id, worktree,
7251 error = got_worktree_set_base_commit_id(worktree, repo,
7255 memset(&upa, 0, sizeof(upa));
7256 error = got_worktree_checkout_files(worktree, &paths,
7257 repo, update_progress, &upa, check_cancelled,
7261 if (upa.did_something) {
7262 printf("Updated to %s: %s\n",
7263 got_worktree_get_head_ref_name(worktree),
7266 print_update_progress_stats(&upa);
7270 free(keyword_idstr);
7274 const struct got_error *close_err = got_repo_close(repo);
7279 got_worktree_close(worktree);
7281 const struct got_error *pack_err =
7282 got_repo_pack_fds_close(pack_fds);
7289 free(commit_id_str);
7290 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7298 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7299 "[-r repository-path] [-s signer-id] name\n", getprogname());
7304 static const struct got_error *
7305 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7307 const struct got_error *err = NULL;
7308 struct got_reflist_entry *re, *se, *new;
7309 struct got_object_id *re_id, *se_id;
7310 struct got_tag_object *re_tag, *se_tag;
7311 time_t re_time, se_time;
7313 STAILQ_FOREACH(re, tags, entry) {
7314 se = STAILQ_FIRST(sorted);
7316 err = got_reflist_entry_dup(&new, re);
7319 STAILQ_INSERT_HEAD(sorted, new, entry);
7322 err = got_ref_resolve(&re_id, repo, re->ref);
7325 err = got_object_open_as_tag(&re_tag, repo, re_id);
7329 re_time = got_object_tag_get_tagger_time(re_tag);
7330 got_object_tag_close(re_tag);
7334 err = got_ref_resolve(&se_id, repo, re->ref);
7337 err = got_object_open_as_tag(&se_tag, repo, se_id);
7341 se_time = got_object_tag_get_tagger_time(se_tag);
7342 got_object_tag_close(se_tag);
7344 if (se_time > re_time) {
7345 err = got_reflist_entry_dup(&new, re);
7348 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7351 se = STAILQ_NEXT(se, entry);
7360 static const struct got_error *
7361 get_tag_refname(char **refname, const char *tag_name)
7363 const struct got_error *err;
7365 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7366 *refname = strdup(tag_name);
7367 if (*refname == NULL)
7368 return got_error_from_errno("strdup");
7369 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7370 err = got_error_from_errno("asprintf");
7378 static const struct got_error *
7379 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7380 const char *allowed_signers, const char *revoked_signers, int verbosity)
7382 static const struct got_error *err = NULL;
7383 struct got_reflist_head refs;
7384 struct got_reflist_entry *re;
7385 char *wanted_refname = NULL;
7390 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7395 struct got_reference *ref;
7396 err = get_tag_refname(&wanted_refname, tag_name);
7399 /* Wanted tag reference should exist. */
7400 err = got_ref_open(&ref, repo, wanted_refname, 0);
7406 TAILQ_FOREACH(re, &refs, entry) {
7407 const char *refname;
7408 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7410 const char *tagger, *ssh_sig = NULL;
7411 char *sig_msg = NULL;
7413 struct got_object_id *id;
7414 struct got_tag_object *tag;
7415 struct got_commit_object *commit = NULL;
7417 refname = got_ref_get_name(re->ref);
7418 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7419 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7422 refstr = got_ref_to_str(re->ref);
7423 if (refstr == NULL) {
7424 err = got_error_from_errno("got_ref_to_str");
7428 err = got_ref_resolve(&id, repo, re->ref);
7431 err = got_object_open_as_tag(&tag, repo, id);
7433 if (err->code != GOT_ERR_OBJ_TYPE) {
7437 /* "lightweight" tag */
7438 err = got_object_open_as_commit(&commit, repo, id);
7443 tagger = got_object_commit_get_committer(commit);
7445 got_object_commit_get_committer_time(commit);
7446 err = got_object_id_str(&id_str, id);
7452 tagger = got_object_tag_get_tagger(tag);
7453 tagger_time = got_object_tag_get_tagger_time(tag);
7454 err = got_object_id_str(&id_str,
7455 got_object_tag_get_object_id(tag));
7460 if (tag && verify_tags) {
7461 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7462 got_object_tag_get_message(tag));
7463 if (ssh_sig && allowed_signers == NULL) {
7464 err = got_error_msg(
7465 GOT_ERR_VERIFY_TAG_SIGNATURE,
7466 "SSH signature verification requires "
7467 "setting allowed_signers in "
7473 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7475 printf("from: %s\n", tagger);
7476 datestr = get_datestr(&tagger_time, datebuf);
7478 printf("date: %s UTC\n", datestr);
7480 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7482 switch (got_object_tag_get_object_type(tag)) {
7483 case GOT_OBJ_TYPE_BLOB:
7484 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7487 case GOT_OBJ_TYPE_TREE:
7488 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7491 case GOT_OBJ_TYPE_COMMIT:
7492 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7495 case GOT_OBJ_TYPE_TAG:
7496 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7506 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7507 allowed_signers, revoked_signers, verbosity);
7508 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7512 printf("signature: %s", sig_msg);
7518 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7521 got_object_commit_close(commit);
7523 tagmsg0 = strdup(got_object_tag_get_message(tag));
7524 got_object_tag_close(tag);
7525 if (tagmsg0 == NULL) {
7526 err = got_error_from_errno("strdup");
7533 line = strsep(&tagmsg, "\n");
7535 printf(" %s\n", line);
7540 got_ref_list_free(&refs);
7541 free(wanted_refname);
7543 if (err == NULL && bad_sigs)
7544 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7548 static const struct got_error *
7549 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7550 const char *tag_name, const char *editor, const char *repo_path)
7552 const struct got_error *err = NULL;
7553 char *template = NULL, *initial_content = NULL;
7554 int initial_content_len;
7557 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7558 err = got_error_from_errno("asprintf");
7562 initial_content_len = asprintf(&initial_content,
7563 "\n# tagging commit %s as %s\n",
7564 commit_id_str, tag_name);
7565 if (initial_content_len == -1) {
7566 err = got_error_from_errno("asprintf");
7570 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7574 if (write(fd, initial_content, initial_content_len) == -1) {
7575 err = got_error_from_errno2("write", *tagmsg_path);
7578 if (close(fd) == -1) {
7579 err = got_error_from_errno2("close", *tagmsg_path);
7584 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7585 initial_content_len, 1);
7587 free(initial_content);
7590 if (fd != -1 && close(fd) == -1 && err == NULL)
7591 err = got_error_from_errno2("close", *tagmsg_path);
7600 static const struct got_error *
7601 add_tag(struct got_repository *repo, const char *tagger,
7602 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7603 const char *signer_id, const char *editor, int verbosity)
7605 const struct got_error *err = NULL;
7606 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7607 char *label = NULL, *commit_id_str = NULL;
7608 struct got_reference *ref = NULL;
7609 char *refname = NULL, *tagmsg = NULL;
7610 char *tagmsg_path = NULL, *tag_id_str = NULL;
7611 int preserve_tagmsg = 0;
7612 struct got_reflist_head refs;
7617 * Don't let the user create a tag name with a leading '-'.
7618 * While technically a valid reference name, this case is usually
7619 * an unintended typo.
7621 if (tag_name[0] == '-')
7622 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7624 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7628 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7629 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7633 err = got_object_id_str(&commit_id_str, commit_id);
7637 err = get_tag_refname(&refname, tag_name);
7640 if (strncmp("refs/tags/", tag_name, 10) == 0)
7643 err = got_ref_open(&ref, repo, refname, 0);
7645 err = got_error(GOT_ERR_TAG_EXISTS);
7647 } else if (err->code != GOT_ERR_NOT_REF)
7650 if (tagmsg_arg == NULL) {
7651 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7652 tag_name, editor, got_repo_get_path(repo));
7654 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7655 tagmsg_path != NULL)
7656 preserve_tagmsg = 1;
7661 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7662 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7666 preserve_tagmsg = 1;
7670 err = got_ref_alloc(&ref, refname, tag_id);
7673 preserve_tagmsg = 1;
7677 err = got_ref_write(ref, repo);
7680 preserve_tagmsg = 1;
7684 err = got_object_id_str(&tag_id_str, tag_id);
7687 preserve_tagmsg = 1;
7690 printf("Created tag %s\n", tag_id_str);
7692 if (preserve_tagmsg) {
7693 fprintf(stderr, "%s: tag message preserved in %s\n",
7694 getprogname(), tagmsg_path);
7695 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink", tagmsg_path);
7701 free(commit_id_str);
7705 got_ref_list_free(&refs);
7709 static const struct got_error *
7710 cmd_tag(int argc, char *argv[])
7712 const struct got_error *error = NULL;
7713 struct got_repository *repo = NULL;
7714 struct got_worktree *worktree = NULL;
7715 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7716 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7717 char *allowed_signers = NULL, *revoked_signers = NULL, *editor = NULL;
7718 const char *signer_id = NULL;
7719 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7720 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7721 int *pack_fds = NULL;
7724 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7725 "sendfd unveil", NULL) == -1)
7729 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7732 commit_id_arg = optarg;
7741 repo_path = realpath(optarg, NULL);
7742 if (repo_path == NULL) {
7743 error = got_error_from_errno2("realpath",
7747 got_path_strip_trailing_slashes(repo_path);
7758 else if (verbosity < 3)
7770 if (do_list || verify_tags) {
7771 if (commit_id_arg != NULL)
7773 "-c option can only be used when creating a tag");
7776 option_conflict('l', 'm');
7778 option_conflict('V', 'm');
7782 option_conflict('l', 's');
7784 option_conflict('V', 's');
7788 } else if (argc != 1)
7794 cwd = getcwd(NULL, 0);
7796 error = got_error_from_errno("getcwd");
7800 error = got_repo_pack_fds_open(&pack_fds);
7804 if (repo_path == NULL) {
7805 error = got_worktree_open(&worktree, cwd,
7806 GOT_WORKTREE_GOT_DIR);
7807 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7813 strdup(got_worktree_get_repo_path(worktree));
7814 if (repo_path == NULL)
7815 error = got_error_from_errno("strdup");
7819 repo_path = strdup(cwd);
7820 if (repo_path == NULL) {
7821 error = got_error_from_errno("strdup");
7827 if (do_list || verify_tags) {
7828 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7831 error = get_allowed_signers(&allowed_signers, repo, worktree);
7834 error = get_revoked_signers(&revoked_signers, repo, worktree);
7838 /* Release work tree lock. */
7839 got_worktree_close(worktree);
7844 * Remove "cpath" promise unless needed for signature tmpfile
7848 got_sigs_apply_unveil();
7851 if (pledge("stdio rpath wpath flock proc exec sendfd "
7852 "unveil", NULL) == -1)
7856 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7859 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7860 revoked_signers, verbosity);
7862 error = get_gitconfig_path(&gitconfig_path);
7865 error = got_repo_open(&repo, repo_path, gitconfig_path,
7870 error = get_author(&tagger, repo, worktree);
7873 if (signer_id == NULL)
7874 signer_id = get_signer_id(repo, worktree);
7876 if (tagmsg == NULL) {
7877 error = get_editor(&editor);
7880 if (unveil(editor, "x") != 0) {
7881 error = got_error_from_errno2("unveil", editor);
7886 error = got_sigs_apply_unveil();
7890 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7894 if (commit_id_arg == NULL) {
7895 struct got_reference *head_ref;
7896 struct got_object_id *commit_id;
7897 error = got_ref_open(&head_ref, repo,
7898 worktree ? got_worktree_get_head_ref_name(worktree)
7902 error = got_ref_resolve(&commit_id, repo, head_ref);
7903 got_ref_close(head_ref);
7906 error = got_object_id_str(&commit_id_str, commit_id);
7911 error = got_keyword_to_idstr(&keyword_idstr,
7912 commit_id_arg, repo, worktree);
7915 commit_id_str = keyword_idstr;
7919 /* Release work tree lock. */
7920 got_worktree_close(worktree);
7924 error = add_tag(repo, tagger, tag_name,
7925 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7926 signer_id, editor, verbosity);
7930 const struct got_error *close_err = got_repo_close(repo);
7935 got_worktree_close(worktree);
7937 const struct got_error *pack_err =
7938 got_repo_pack_fds_close(pack_fds);
7945 free(gitconfig_path);
7946 free(commit_id_str);
7948 free(allowed_signers);
7949 free(revoked_signers);
7956 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7960 static const struct got_error *
7961 add_progress(void *arg, unsigned char status, const char *path)
7963 while (path[0] == '/')
7965 printf("%c %s\n", status, path);
7969 static const struct got_error *
7970 cmd_add(int argc, char *argv[])
7972 const struct got_error *error = NULL;
7973 struct got_repository *repo = NULL;
7974 struct got_worktree *worktree = NULL;
7976 struct got_pathlist_head paths;
7977 struct got_pathlist_entry *pe;
7978 int ch, can_recurse = 0, no_ignores = 0;
7979 int *pack_fds = NULL;
7984 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7989 while ((ch = getopt(argc, argv, "IR")) != -1) {
8009 cwd = getcwd(NULL, 0);
8011 error = got_error_from_errno("getcwd");
8015 error = got_repo_pack_fds_open(&pack_fds);
8019 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8021 if (error->code == GOT_ERR_NOT_WORKTREE)
8022 error = wrap_not_worktree_error(error, "add", cwd);
8026 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8031 error = apply_unveil(got_repo_get_path(repo), 1,
8032 got_worktree_get_root_path(worktree));
8036 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8043 TAILQ_FOREACH(pe, &paths, entry) {
8044 if (asprintf(&ondisk_path, "%s/%s",
8045 got_worktree_get_root_path(worktree),
8047 error = got_error_from_errno("asprintf");
8050 if (lstat(ondisk_path, &sb) == -1) {
8051 if (errno == ENOENT) {
8055 error = got_error_from_errno2("lstat",
8061 if (S_ISDIR(sb.st_mode)) {
8062 error = got_error_msg(GOT_ERR_BAD_PATH,
8063 "adding directories requires -R option");
8069 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8070 NULL, repo, no_ignores);
8073 const struct got_error *close_err = got_repo_close(repo);
8078 got_worktree_close(worktree);
8080 const struct got_error *pack_err =
8081 got_repo_pack_fds_close(pack_fds);
8085 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8093 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8098 static const struct got_error *
8099 print_remove_status(void *arg, unsigned char status,
8100 unsigned char staged_status, const char *path)
8102 while (path[0] == '/')
8104 if (status == GOT_STATUS_NONEXISTENT)
8106 if (status == staged_status && (status == GOT_STATUS_DELETE))
8107 status = GOT_STATUS_NO_CHANGE;
8108 printf("%c%c %s\n", status, staged_status, path);
8112 static const struct got_error *
8113 cmd_remove(int argc, char *argv[])
8115 const struct got_error *error = NULL;
8116 struct got_worktree *worktree = NULL;
8117 struct got_repository *repo = NULL;
8118 const char *status_codes = NULL;
8120 struct got_pathlist_head paths;
8121 struct got_pathlist_entry *pe;
8122 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8123 int ignore_missing_paths = 0;
8124 int *pack_fds = NULL;
8129 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8134 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8137 delete_local_mods = 1;
8138 ignore_missing_paths = 1;
8147 for (i = 0; optarg[i] != '\0'; i++) {
8148 switch (optarg[i]) {
8149 case GOT_STATUS_MODIFY:
8150 delete_local_mods = 1;
8152 case GOT_STATUS_MISSING:
8153 ignore_missing_paths = 1;
8156 errx(1, "invalid status code '%c'",
8160 status_codes = optarg;
8174 cwd = getcwd(NULL, 0);
8176 error = got_error_from_errno("getcwd");
8180 error = got_repo_pack_fds_open(&pack_fds);
8184 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8186 if (error->code == GOT_ERR_NOT_WORKTREE)
8187 error = wrap_not_worktree_error(error, "remove", cwd);
8191 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8196 error = apply_unveil(got_repo_get_path(repo), 1,
8197 got_worktree_get_root_path(worktree));
8201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8208 TAILQ_FOREACH(pe, &paths, entry) {
8209 if (asprintf(&ondisk_path, "%s/%s",
8210 got_worktree_get_root_path(worktree),
8212 error = got_error_from_errno("asprintf");
8215 if (lstat(ondisk_path, &sb) == -1) {
8216 if (errno == ENOENT) {
8220 error = got_error_from_errno2("lstat",
8226 if (S_ISDIR(sb.st_mode)) {
8227 error = got_error_msg(GOT_ERR_BAD_PATH,
8228 "removing directories requires -R option");
8234 error = got_worktree_schedule_delete(worktree, &paths,
8235 delete_local_mods, status_codes, print_remove_status, NULL,
8236 repo, keep_on_disk, ignore_missing_paths);
8239 const struct got_error *close_err = got_repo_close(repo);
8244 got_worktree_close(worktree);
8246 const struct got_error *pack_err =
8247 got_repo_pack_fds_close(pack_fds);
8251 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8259 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8260 "[patchfile]\n", getprogname());
8264 static const struct got_error *
8265 patch_from_stdin(int *patchfd)
8267 const struct got_error *err = NULL;
8270 sig_t sighup, sigint, sigquit;
8272 *patchfd = got_opentempfd();
8274 return got_error_from_errno("got_opentempfd");
8276 sighup = signal(SIGHUP, SIG_DFL);
8277 sigint = signal(SIGINT, SIG_DFL);
8278 sigquit = signal(SIGQUIT, SIG_DFL);
8281 r = read(0, buf, sizeof(buf));
8283 err = got_error_from_errno("read");
8288 if (write(*patchfd, buf, r) == -1) {
8289 err = got_error_from_errno("write");
8294 signal(SIGHUP, sighup);
8295 signal(SIGINT, sigint);
8296 signal(SIGQUIT, sigquit);
8298 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8299 err = got_error_from_errno("lseek");
8309 struct got_patch_progress_arg {
8315 static const struct got_error *
8316 patch_progress(void *arg, const char *old, const char *new,
8317 unsigned char status, const struct got_error *error, int old_from,
8318 int old_lines, int new_from, int new_lines, int offset,
8319 int ws_mangled, const struct got_error *hunk_err)
8321 const char *path = new == NULL ? old : new;
8322 struct got_patch_progress_arg *a = arg;
8324 while (*path == '/')
8327 if (status != GOT_STATUS_NO_CHANGE &&
8328 status != 0 /* per-hunk progress */) {
8329 printf("%c %s\n", status, path);
8330 a->did_something = 1;
8333 if (hunk_err == NULL) {
8334 if (status == GOT_STATUS_CANNOT_UPDATE)
8336 else if (status == GOT_STATUS_CONFLICT)
8341 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8343 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8344 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8345 old_lines, new_from, new_lines);
8346 if (hunk_err != NULL)
8347 printf("%s\n", hunk_err->msg);
8348 else if (offset != 0)
8349 printf("applied with offset %d\n", offset);
8351 printf("hunk contains mangled whitespace\n");
8358 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8360 if (!ppa->did_something)
8363 if (ppa->conflicts > 0)
8364 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8366 if (ppa->rejects > 0) {
8367 printf("Files where patch failed to apply: %d\n",
8372 static const struct got_error *
8373 cmd_patch(int argc, char *argv[])
8375 const struct got_error *error = NULL, *close_error = NULL;
8376 struct got_worktree *worktree = NULL;
8377 struct got_repository *repo = NULL;
8378 struct got_reflist_head refs;
8379 struct got_object_id *commit_id = NULL;
8380 const char *commit_id_str = NULL;
8383 char *cwd = NULL, *keyword_idstr = NULL;
8384 int ch, nop = 0, strip = -1, reverse = 0;
8386 int *pack_fds = NULL;
8387 struct got_patch_progress_arg ppa;
8392 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8393 "unveil", NULL) == -1)
8397 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8400 commit_id_str = optarg;
8406 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8408 errx(1, "pathname strip count is %s: %s",
8424 error = patch_from_stdin(&patchfd);
8427 } else if (argc == 1) {
8428 patchfd = open(argv[0], O_RDONLY);
8429 if (patchfd == -1) {
8430 error = got_error_from_errno2("open", argv[0]);
8433 if (fstat(patchfd, &sb) == -1) {
8434 error = got_error_from_errno2("fstat", argv[0]);
8437 if (!S_ISREG(sb.st_mode)) {
8438 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8444 if ((cwd = getcwd(NULL, 0)) == NULL) {
8445 error = got_error_from_errno("getcwd");
8449 error = got_repo_pack_fds_open(&pack_fds);
8453 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8457 const char *repo_path = got_worktree_get_repo_path(worktree);
8458 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8462 error = apply_unveil(got_repo_get_path(repo), 0,
8463 got_worktree_get_root_path(worktree));
8467 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8471 if (commit_id_str != NULL) {
8472 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8477 error = got_repo_match_object_id(&commit_id, NULL,
8478 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8479 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8484 memset(&ppa, 0, sizeof(ppa));
8485 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8486 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8487 print_patch_progress_stats(&ppa);
8489 got_ref_list_free(&refs);
8490 free(keyword_idstr);
8493 close_error = got_repo_close(repo);
8495 error = close_error;
8497 if (worktree != NULL) {
8498 close_error = got_worktree_close(worktree);
8500 error = close_error;
8503 const struct got_error *pack_err =
8504 got_repo_pack_fds_close(pack_fds);
8515 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8520 static const struct got_error *
8521 revert_progress(void *arg, unsigned char status, const char *path)
8523 if (status == GOT_STATUS_UNVERSIONED)
8526 while (path[0] == '/')
8528 printf("%c %s\n", status, path);
8532 struct choose_patch_arg {
8533 FILE *patch_script_file;
8537 static const struct got_error *
8538 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8539 int nchanges, const char *action)
8541 const struct got_error *err;
8543 size_t linesize = 0;
8547 case GOT_STATUS_ADD:
8548 printf("A %s\n%s this addition? [y/n] ", path, action);
8550 case GOT_STATUS_DELETE:
8551 printf("D %s\n%s this deletion? [y/n] ", path, action);
8553 case GOT_STATUS_MODIFY:
8554 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8555 return got_error_from_errno("fseek");
8556 printf(GOT_COMMIT_SEP_STR);
8557 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8559 if (linelen == -1 && ferror(patch_file)) {
8560 err = got_error_from_errno("getline");
8565 printf(GOT_COMMIT_SEP_STR);
8566 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8567 path, n, nchanges, action);
8570 return got_error_path(path, GOT_ERR_FILE_STATUS);
8577 static const struct got_error *
8578 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8579 FILE *patch_file, int n, int nchanges)
8581 const struct got_error *err = NULL;
8583 size_t linesize = 0;
8586 struct choose_patch_arg *a = arg;
8588 *choice = GOT_PATCH_CHOICE_NONE;
8590 if (a->patch_script_file) {
8592 err = show_change(status, path, patch_file, n, nchanges,
8596 linelen = getline(&line, &linesize, a->patch_script_file);
8597 if (linelen == -1) {
8598 if (ferror(a->patch_script_file))
8599 return got_error_from_errno("getline");
8602 nl = strchr(line, '\n');
8605 if (strcmp(line, "y") == 0) {
8606 *choice = GOT_PATCH_CHOICE_YES;
8608 } else if (strcmp(line, "n") == 0) {
8609 *choice = GOT_PATCH_CHOICE_NO;
8611 } else if (strcmp(line, "q") == 0 &&
8612 status == GOT_STATUS_MODIFY) {
8613 *choice = GOT_PATCH_CHOICE_QUIT;
8616 printf("invalid response '%s'\n", line);
8621 while (resp != 'y' && resp != 'n' && resp != 'q') {
8622 err = show_change(status, path, patch_file, n, nchanges,
8629 if (status == GOT_STATUS_MODIFY) {
8630 if (resp != 'y' && resp != 'n' && resp != 'q') {
8631 printf("invalid response '%c'\n", resp);
8634 } else if (resp != 'y' && resp != 'n') {
8635 printf("invalid response '%c'\n", resp);
8641 *choice = GOT_PATCH_CHOICE_YES;
8642 else if (resp == 'n')
8643 *choice = GOT_PATCH_CHOICE_NO;
8644 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8645 *choice = GOT_PATCH_CHOICE_QUIT;
8650 struct wt_commitable_path_arg {
8651 struct got_pathlist_head *commit_paths;
8656 * Shortcut work tree status callback to determine if the set of paths scanned
8657 * has at least one versioned path that is being modified and, if not NULL, is
8658 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8659 * soon as a path is passed with a status that satisfies this criteria.
8661 static const struct got_error *
8662 worktree_has_commitable_path(void *arg, unsigned char status,
8663 unsigned char staged_status, const char *path,
8664 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8665 struct got_object_id *commit_id, int dirfd, const char *de_name)
8667 struct wt_commitable_path_arg *a = arg;
8669 if (status == staged_status && (status == GOT_STATUS_DELETE))
8670 status = GOT_STATUS_NO_CHANGE;
8672 if (!(status == GOT_STATUS_NO_CHANGE ||
8673 status == GOT_STATUS_UNVERSIONED) ||
8674 staged_status != GOT_STATUS_NO_CHANGE) {
8675 if (a->commit_paths != NULL) {
8676 struct got_pathlist_entry *pe;
8678 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8679 if (strncmp(path, pe->path,
8680 pe->path_len) == 0) {
8681 *a->has_changes = 1;
8686 *a->has_changes = 1;
8688 if (*a->has_changes)
8689 return got_error(GOT_ERR_FILE_MODIFIED);
8696 * Check that the changeset of the commit identified by id is
8697 * comprised of at least one modified path that is being committed.
8699 static const struct got_error *
8700 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8701 struct got_object_id *id, struct got_worktree *worktree,
8702 struct got_repository *repo)
8704 const struct got_error *err;
8705 struct got_pathlist_head paths;
8706 struct got_commit_object *commit = NULL, *pcommit = NULL;
8707 struct got_tree_object *tree = NULL, *ptree = NULL;
8708 struct got_object_qid *pid;
8712 err = got_object_open_as_commit(&commit, repo, id);
8716 err = got_object_open_as_tree(&tree, repo,
8717 got_object_commit_get_tree_id(commit));
8721 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8723 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8727 err = got_object_open_as_tree(&ptree, repo,
8728 got_object_commit_get_tree_id(pcommit));
8733 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8734 got_diff_tree_collect_changed_paths, &paths, 0);
8738 err = got_worktree_status(worktree, &paths, repo, 0,
8739 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8740 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8742 * At least one changed path in the referenced commit is
8743 * modified in the work tree, that's all we need to know!
8749 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8751 got_object_commit_close(commit);
8753 got_object_commit_close(pcommit);
8755 got_object_tree_close(tree);
8757 got_object_tree_close(ptree);
8762 * Remove any "logmsg" reference comprised entirely of paths that have
8763 * been reverted in this work tree. If any path in the logmsg ref changeset
8764 * remains in a changed state in the worktree, do not remove the reference.
8766 static const struct got_error *
8767 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8769 const struct got_error *err;
8770 struct got_reflist_head refs;
8771 struct got_reflist_entry *re;
8772 struct got_commit_object *commit = NULL;
8773 struct got_object_id *commit_id = NULL;
8774 struct wt_commitable_path_arg wcpa;
8775 char *uuidstr = NULL;
8779 err = got_worktree_get_uuid(&uuidstr, worktree);
8783 err = got_ref_list(&refs, repo, "refs/got/worktree",
8784 got_ref_cmp_by_name, repo);
8788 TAILQ_FOREACH(re, &refs, entry) {
8789 const char *refname;
8790 int has_changes = 0;
8792 refname = got_ref_get_name(re->ref);
8794 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8795 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8796 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8797 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8798 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8799 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8803 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8804 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8808 err = got_repo_match_object_id(&commit_id, NULL, refname,
8809 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8813 err = got_object_open_as_commit(&commit, repo, commit_id);
8817 wcpa.commit_paths = NULL;
8818 wcpa.has_changes = &has_changes;
8820 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8826 err = got_ref_delete(re->ref, repo);
8831 got_object_commit_close(commit);
8840 got_ref_list_free(&refs);
8842 got_object_commit_close(commit);
8846 static const struct got_error *
8847 cmd_revert(int argc, char *argv[])
8849 const struct got_error *error = NULL;
8850 struct got_worktree *worktree = NULL;
8851 struct got_repository *repo = NULL;
8852 char *cwd = NULL, *path = NULL;
8853 struct got_pathlist_head paths;
8854 struct got_pathlist_entry *pe;
8855 int ch, can_recurse = 0, pflag = 0;
8856 FILE *patch_script_file = NULL;
8857 const char *patch_script_path = NULL;
8858 struct choose_patch_arg cpa;
8859 int *pack_fds = NULL;
8864 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8865 "unveil", NULL) == -1)
8869 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8872 patch_script_path = optarg;
8891 if (patch_script_path && !pflag)
8892 errx(1, "-F option can only be used together with -p option");
8894 cwd = getcwd(NULL, 0);
8896 error = got_error_from_errno("getcwd");
8900 error = got_repo_pack_fds_open(&pack_fds);
8904 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8906 if (error->code == GOT_ERR_NOT_WORKTREE)
8907 error = wrap_not_worktree_error(error, "revert", cwd);
8911 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8916 if (patch_script_path) {
8917 patch_script_file = fopen(patch_script_path, "re");
8918 if (patch_script_file == NULL) {
8919 error = got_error_from_errno2("fopen",
8926 * XXX "c" perm needed on repo dir to delete merge references.
8928 error = apply_unveil(got_repo_get_path(repo), 0,
8929 got_worktree_get_root_path(worktree));
8933 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8940 TAILQ_FOREACH(pe, &paths, entry) {
8941 if (asprintf(&ondisk_path, "%s/%s",
8942 got_worktree_get_root_path(worktree),
8944 error = got_error_from_errno("asprintf");
8947 if (lstat(ondisk_path, &sb) == -1) {
8948 if (errno == ENOENT) {
8952 error = got_error_from_errno2("lstat",
8958 if (S_ISDIR(sb.st_mode)) {
8959 error = got_error_msg(GOT_ERR_BAD_PATH,
8960 "reverting directories requires -R option");
8966 cpa.patch_script_file = patch_script_file;
8967 cpa.action = "revert";
8968 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8969 pflag ? choose_patch : NULL, &cpa, repo);
8971 error = rm_logmsg_ref(worktree, repo);
8973 if (patch_script_file && fclose(patch_script_file) == EOF &&
8975 error = got_error_from_errno2("fclose", patch_script_path);
8977 const struct got_error *close_err = got_repo_close(repo);
8982 got_worktree_close(worktree);
8984 const struct got_error *pack_err =
8985 got_repo_pack_fds_close(pack_fds);
8989 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8998 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8999 "[-m message] [path ...]\n", getprogname());
9003 struct collect_commit_logmsg_arg {
9004 const char *cmdline_log;
9005 const char *prepared_log;
9006 const char *merged_log;
9007 int non_interactive;
9009 const char *worktree_path;
9010 const char *branch_name;
9011 const char *repo_path;
9016 static const struct got_error *
9017 read_prepared_logmsg(char **logmsg, const char *path)
9019 const struct got_error *err = NULL;
9025 memset(&sb, 0, sizeof(sb));
9027 f = fopen(path, "re");
9029 return got_error_from_errno2("fopen", path);
9031 if (fstat(fileno(f), &sb) == -1) {
9032 err = got_error_from_errno2("fstat", path);
9035 if (sb.st_size == 0) {
9036 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
9040 *logmsg = malloc(sb.st_size + 1);
9041 if (*logmsg == NULL) {
9042 err = got_error_from_errno("malloc");
9046 r = fread(*logmsg, 1, sb.st_size, f);
9047 if (r != sb.st_size) {
9049 err = got_error_from_errno2("fread", path);
9051 err = got_error(GOT_ERR_IO);
9054 (*logmsg)[sb.st_size] = '\0';
9056 if (fclose(f) == EOF && err == NULL)
9057 err = got_error_from_errno2("fclose", path);
9065 static const struct got_error *
9066 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9067 const char *diff_path, char **logmsg, void *arg)
9069 char *initial_content = NULL;
9070 struct got_pathlist_entry *pe;
9071 const struct got_error *err = NULL;
9072 char *template = NULL;
9073 char *prepared_msg = NULL, *merged_msg = NULL;
9074 struct collect_commit_logmsg_arg *a = arg;
9075 int initial_content_len;
9079 /* if a message was specified on the command line, just use it */
9080 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9081 len = strlen(a->cmdline_log) + 1;
9082 *logmsg = malloc(len + 1);
9083 if (*logmsg == NULL)
9084 return got_error_from_errno("malloc");
9085 strlcpy(*logmsg, a->cmdline_log, len);
9087 } else if (a->prepared_log != NULL && a->non_interactive)
9088 return read_prepared_logmsg(logmsg, a->prepared_log);
9090 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9091 return got_error_from_errno("asprintf");
9093 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9097 if (a->prepared_log) {
9098 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9101 } else if (a->merged_log) {
9102 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9107 initial_content_len = asprintf(&initial_content,
9108 "%s%s\n# changes to be committed on branch %s:\n",
9109 prepared_msg ? prepared_msg : "",
9110 merged_msg ? merged_msg : "", a->branch_name);
9111 if (initial_content_len == -1) {
9112 err = got_error_from_errno("asprintf");
9116 if (write(fd, initial_content, initial_content_len) == -1) {
9117 err = got_error_from_errno2("write", a->logmsg_path);
9121 TAILQ_FOREACH(pe, commitable_paths, entry) {
9122 struct got_commitable *ct = pe->data;
9123 dprintf(fd, "# %c %s\n",
9124 got_commitable_get_status(ct),
9125 got_commitable_get_path(ct));
9129 dprintf(fd, "# detailed changes can be viewed in %s\n",
9133 if (close(fd) == -1) {
9134 err = got_error_from_errno2("close", a->logmsg_path);
9139 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9140 initial_content_len, a->prepared_log ? 0 : 1);
9142 free(initial_content);
9147 if (fd != -1 && close(fd) == -1 && err == NULL)
9148 err = got_error_from_errno2("close", a->logmsg_path);
9156 static const struct got_error *
9157 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9158 const char *type, int has_content)
9160 const struct got_error *err = NULL;
9161 char *logmsg = NULL;
9163 err = got_object_commit_get_logmsg(&logmsg, commit);
9167 if (fprintf(f, "%s# log message of %s commit %s:%s",
9168 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9169 err = got_ferror(f, GOT_ERR_IO);
9176 * Lookup "logmsg" references of backed-out and cherrypicked commits
9177 * belonging to the current work tree. If found, and the worktree has
9178 * at least one modified file that was changed in the referenced commit,
9179 * add its log message to a new temporary file at *logmsg_path.
9180 * Add all refs found to matched_refs to be scheduled for removal on
9181 * successful commit.
9183 static const struct got_error *
9184 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9185 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9186 struct got_repository *repo)
9188 const struct got_error *err;
9189 struct got_commit_object *commit = NULL;
9190 struct got_object_id *id = NULL;
9191 struct got_reflist_head refs;
9192 struct got_reflist_entry *re, *re_match;
9194 char *uuidstr = NULL;
9195 int added_logmsg = 0;
9199 *logmsg_path = NULL;
9201 err = got_worktree_get_uuid(&uuidstr, worktree);
9205 err = got_ref_list(&refs, repo, "refs/got/worktree",
9206 got_ref_cmp_by_name, repo);
9210 TAILQ_FOREACH(re, &refs, entry) {
9211 const char *refname, *type;
9212 struct wt_commitable_path_arg wcpa;
9215 refname = got_ref_get_name(re->ref);
9217 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9218 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9219 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9220 type = "cherrypicked";
9221 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9222 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9223 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9224 type = "backed-out";
9228 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9229 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9233 err = got_repo_match_object_id(&id, NULL, refname,
9234 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9238 err = got_object_open_as_commit(&commit, repo, id);
9242 wcpa.commit_paths = paths;
9243 wcpa.has_changes = &add_logmsg;
9245 err = commit_path_changed_in_worktree(&wcpa, id,
9252 err = got_opentemp_named(logmsg_path, &f,
9253 "got-commit-logmsg", "");
9257 err = cat_logmsg(f, commit, refname, type,
9264 err = got_reflist_entry_dup(&re_match, re);
9267 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9270 got_object_commit_close(commit);
9279 got_ref_list_free(&refs);
9281 got_object_commit_close(commit);
9282 if (f && fclose(f) == EOF && err == NULL)
9283 err = got_error_from_errno("fclose");
9284 if (!added_logmsg) {
9285 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9286 err = got_error_from_errno2("unlink", *logmsg_path);
9287 *logmsg_path = NULL;
9292 static const struct got_error *
9293 cmd_commit(int argc, char *argv[])
9295 const struct got_error *error = NULL;
9296 struct got_worktree *worktree = NULL;
9297 struct got_repository *repo = NULL;
9298 char *cwd = NULL, *id_str = NULL;
9299 struct got_object_id *id = NULL;
9300 const char *logmsg = NULL;
9301 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9302 struct collect_commit_logmsg_arg cl_arg;
9303 const char *author = NULL;
9304 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9305 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9306 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9307 int show_diff = 1, commit_conflicts = 0;
9308 struct got_pathlist_head paths;
9309 struct got_reflist_head refs;
9310 struct got_reflist_entry *re;
9311 int *pack_fds = NULL;
9315 cl_arg.logmsg_path = NULL;
9318 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9319 "unveil", NULL) == -1)
9323 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9327 error = valid_author(author);
9332 commit_conflicts = 1;
9336 option_conflict('F', 'm');
9337 prepared_logmsg = realpath(optarg, NULL);
9338 if (prepared_logmsg == NULL)
9339 return got_error_from_errno2("realpath",
9343 if (prepared_logmsg)
9344 option_conflict('m', 'F');
9348 non_interactive = 1;
9354 allow_bad_symlinks = 1;
9365 cwd = getcwd(NULL, 0);
9367 error = got_error_from_errno("getcwd");
9371 error = got_repo_pack_fds_open(&pack_fds);
9375 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9377 if (error->code == GOT_ERR_NOT_WORKTREE)
9378 error = wrap_not_worktree_error(error, "commit", cwd);
9382 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9385 if (rebase_in_progress) {
9386 error = got_error(GOT_ERR_REBASING);
9390 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9395 error = get_gitconfig_path(&gitconfig_path);
9398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9399 gitconfig_path, pack_fds);
9403 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9406 if (merge_in_progress) {
9407 error = got_error(GOT_ERR_MERGE_BUSY);
9411 error = get_author(&committer, repo, worktree);
9418 if (logmsg == NULL || strlen(logmsg) == 0) {
9419 error = get_editor(&editor);
9422 if (unveil(editor, "x") != 0) {
9423 error = got_error_from_errno2("unveil", editor);
9428 error = apply_unveil(got_repo_get_path(repo), 0,
9429 got_worktree_get_root_path(worktree));
9433 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9437 if (prepared_logmsg == NULL) {
9438 error = lookup_logmsg_ref(&merged_logmsg,
9439 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9444 cl_arg.editor = editor;
9445 cl_arg.cmdline_log = logmsg;
9446 cl_arg.prepared_log = prepared_logmsg;
9447 cl_arg.merged_log = merged_logmsg;
9448 cl_arg.non_interactive = non_interactive;
9449 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9450 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9451 if (!histedit_in_progress) {
9452 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9453 error = got_error(GOT_ERR_COMMIT_BRANCH);
9456 cl_arg.branch_name += 11;
9458 cl_arg.repo_path = got_repo_get_path(repo);
9459 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9460 allow_bad_symlinks, show_diff, commit_conflicts,
9461 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9463 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9464 cl_arg.logmsg_path != NULL)
9465 preserve_logmsg = 1;
9469 error = got_object_id_str(&id_str, id);
9472 printf("Created commit %s\n", id_str);
9474 TAILQ_FOREACH(re, &refs, entry) {
9475 error = got_ref_delete(re->ref, repo);
9481 if (preserve_logmsg) {
9482 fprintf(stderr, "%s: log message preserved in %s\n",
9483 getprogname(), cl_arg.logmsg_path);
9484 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9486 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9487 free(cl_arg.logmsg_path);
9488 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9489 error = got_error_from_errno2("unlink", merged_logmsg);
9490 free(merged_logmsg);
9492 const struct got_error *close_err = got_repo_close(repo);
9497 got_worktree_close(worktree);
9499 const struct got_error *pack_err =
9500 got_repo_pack_fds_close(pack_fds);
9504 got_ref_list_free(&refs);
9505 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9508 free(gitconfig_path);
9511 free(prepared_logmsg);
9518 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9519 "[-r repository-path] [-t tag] [remote-repository]\n",
9525 print_load_info(int print_colored, int print_found, int print_trees,
9526 int ncolored, int nfound, int ntrees)
9528 if (print_colored) {
9529 printf("%d commit%s colored", ncolored,
9530 ncolored == 1 ? "" : "s");
9533 printf("%s%d object%s found",
9534 ncolored > 0 ? "; " : "",
9535 nfound, nfound == 1 ? "" : "s");
9538 printf("; %d tree%s scanned", ntrees,
9539 ntrees == 1 ? "" : "s");
9543 struct got_send_progress_arg {
9544 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9551 int last_nobj_total;
9555 int printed_something;
9557 struct got_pathlist_head *delete_branches;
9560 static const struct got_error *
9561 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9562 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9563 int nobj_written, off_t bytes_sent, const char *refname,
9564 const char *errmsg, int success)
9566 struct got_send_progress_arg *a = arg;
9567 char scaled_packsize[FMT_SCALED_STRSIZE];
9568 char scaled_sent[FMT_SCALED_STRSIZE];
9569 int p_deltify = 0, p_written = 0, p_sent = 0;
9570 int print_colored = 0, print_found = 0, print_trees = 0;
9571 int print_searching = 0, print_total = 0;
9572 int print_deltify = 0, print_written = 0, print_sent = 0;
9574 if (a->verbosity < 0)
9578 const char *status = success ? "accepted" : "rejected";
9581 struct got_pathlist_entry *pe;
9582 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9583 const char *branchname = pe->path;
9584 if (got_path_cmp(branchname, refname,
9585 strlen(branchname), strlen(refname)) == 0) {
9587 a->sent_something = 1;
9593 if (a->printed_something)
9595 printf("Server has %s %s", status, refname);
9597 printf(": %s", errmsg);
9598 a->printed_something = 1;
9602 if (a->last_ncolored != ncolored) {
9604 a->last_ncolored = ncolored;
9607 if (a->last_nfound != nfound) {
9610 a->last_nfound = nfound;
9613 if (a->last_ntrees != ntrees) {
9617 a->last_ntrees = ntrees;
9620 if ((print_colored || print_found || print_trees) &&
9623 print_load_info(print_colored, print_found, print_trees,
9624 ncolored, nfound, ntrees);
9625 a->printed_something = 1;
9628 } else if (!a->loading_done) {
9630 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9632 a->loading_done = 1;
9635 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9636 return got_error_from_errno("fmt_scaled");
9637 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9638 return got_error_from_errno("fmt_scaled");
9640 if (a->last_ncommits != ncommits) {
9641 print_searching = 1;
9642 a->last_ncommits = ncommits;
9645 if (a->last_nobj_total != nobj_total) {
9646 print_searching = 1;
9648 a->last_nobj_total = nobj_total;
9651 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9652 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9653 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9654 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9655 return got_error(GOT_ERR_NO_SPACE);
9658 if (nobj_deltify > 0 || nobj_written > 0) {
9659 if (nobj_deltify > 0) {
9660 p_deltify = (nobj_deltify * 100) / nobj_total;
9661 if (p_deltify != a->last_p_deltify) {
9662 a->last_p_deltify = p_deltify;
9663 print_searching = 1;
9668 if (nobj_written > 0) {
9669 p_written = (nobj_written * 100) / nobj_total;
9670 if (p_written != a->last_p_written) {
9671 a->last_p_written = p_written;
9672 print_searching = 1;
9680 if (bytes_sent > 0) {
9681 p_sent = (bytes_sent * 100) / packfile_size;
9682 if (p_sent != a->last_p_sent) {
9683 a->last_p_sent = p_sent;
9684 print_searching = 1;
9690 a->sent_something = 1;
9693 if (print_searching || print_total || print_deltify || print_written ||
9696 if (print_searching)
9697 printf("packing %d reference%s", ncommits,
9698 ncommits == 1 ? "" : "s");
9700 printf("; %d object%s", nobj_total,
9701 nobj_total == 1 ? "" : "s");
9703 printf("; deltify: %d%%", p_deltify);
9705 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9706 scaled_packsize, p_sent);
9707 else if (print_written)
9708 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9709 scaled_packsize, p_written);
9710 if (print_searching || print_total || print_deltify ||
9711 print_written || print_sent) {
9712 a->printed_something = 1;
9718 static const struct got_error *
9719 cmd_send(int argc, char *argv[])
9721 const struct got_error *error = NULL;
9722 char *cwd = NULL, *repo_path = NULL;
9723 const char *remote_name;
9724 char *proto = NULL, *host = NULL, *port = NULL;
9725 char *repo_name = NULL, *server_path = NULL;
9726 const struct got_remote_repo *remotes;
9727 struct got_remote_repo *remote = NULL;
9728 int nremotes, nbranches = 0, ndelete_branches = 0;
9729 struct got_repository *repo = NULL;
9730 struct got_worktree *worktree = NULL;
9731 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9732 struct got_pathlist_head branches;
9733 struct got_pathlist_head tags;
9734 struct got_reflist_head all_branches;
9735 struct got_reflist_head all_tags;
9736 struct got_pathlist_head delete_args;
9737 struct got_pathlist_head delete_branches;
9738 struct got_reflist_entry *re;
9739 struct got_pathlist_entry *pe;
9740 int i, ch, sendfd = -1, sendstatus;
9742 struct got_send_progress_arg spa;
9743 int verbosity = 0, overwrite_refs = 0;
9744 int send_all_branches = 0, send_all_tags = 0;
9745 struct got_reference *ref = NULL;
9746 int *pack_fds = NULL;
9748 TAILQ_INIT(&branches);
9750 TAILQ_INIT(&all_branches);
9751 TAILQ_INIT(&all_tags);
9752 TAILQ_INIT(&delete_args);
9753 TAILQ_INIT(&delete_branches);
9755 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9758 send_all_branches = 1;
9761 error = got_pathlist_append(&branches, optarg, NULL);
9767 error = got_pathlist_append(&delete_args, optarg, NULL);
9778 repo_path = realpath(optarg, NULL);
9779 if (repo_path == NULL)
9780 return got_error_from_errno2("realpath",
9782 got_path_strip_trailing_slashes(repo_path);
9788 error = got_pathlist_append(&tags, optarg, NULL);
9795 else if (verbosity < 3)
9806 if (send_all_branches && !TAILQ_EMPTY(&branches))
9807 option_conflict('a', 'b');
9808 if (send_all_tags && !TAILQ_EMPTY(&tags))
9809 option_conflict('T', 't');
9813 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9815 remote_name = argv[0];
9819 cwd = getcwd(NULL, 0);
9821 error = got_error_from_errno("getcwd");
9825 error = got_repo_pack_fds_open(&pack_fds);
9829 if (repo_path == NULL) {
9830 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9831 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9837 strdup(got_worktree_get_repo_path(worktree));
9838 if (repo_path == NULL)
9839 error = got_error_from_errno("strdup");
9843 repo_path = strdup(cwd);
9844 if (repo_path == NULL) {
9845 error = got_error_from_errno("strdup");
9851 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9856 worktree_conf = got_worktree_get_gotconfig(worktree);
9857 if (worktree_conf) {
9858 got_gotconfig_get_remotes(&nremotes, &remotes,
9860 for (i = 0; i < nremotes; i++) {
9861 if (strcmp(remotes[i].name, remote_name) == 0) {
9862 error = got_repo_remote_repo_dup(&remote,
9871 if (remote == NULL) {
9872 repo_conf = got_repo_get_gotconfig(repo);
9874 got_gotconfig_get_remotes(&nremotes, &remotes,
9876 for (i = 0; i < nremotes; i++) {
9877 if (strcmp(remotes[i].name, remote_name) == 0) {
9878 error = got_repo_remote_repo_dup(&remote,
9887 if (remote == NULL) {
9888 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9889 for (i = 0; i < nremotes; i++) {
9890 if (strcmp(remotes[i].name, remote_name) == 0) {
9891 error = got_repo_remote_repo_dup(&remote,
9899 if (remote == NULL) {
9900 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9904 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9905 &repo_name, remote->send_url);
9909 if (strcmp(proto, "git") == 0) {
9911 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9912 "sendfd dns inet unveil", NULL) == -1)
9915 } else if (strcmp(proto, "git+ssh") == 0 ||
9916 strcmp(proto, "ssh") == 0) {
9918 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9919 "sendfd unveil", NULL) == -1)
9922 } else if (strcmp(proto, "http") == 0 ||
9923 strcmp(proto, "git+http") == 0) {
9924 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9927 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9931 error = got_dial_apply_unveil(proto);
9935 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9939 if (send_all_branches) {
9940 error = got_ref_list(&all_branches, repo, "refs/heads",
9941 got_ref_cmp_by_name, NULL);
9944 TAILQ_FOREACH(re, &all_branches, entry) {
9945 const char *branchname = got_ref_get_name(re->ref);
9946 error = got_pathlist_append(&branches,
9952 } else if (nbranches == 0) {
9953 for (i = 0; i < remote->nsend_branches; i++) {
9954 error = got_pathlist_append(&branches,
9955 remote->send_branches[i], NULL);
9961 if (send_all_tags) {
9962 error = got_ref_list(&all_tags, repo, "refs/tags",
9963 got_ref_cmp_by_name, NULL);
9966 TAILQ_FOREACH(re, &all_tags, entry) {
9967 const char *tagname = got_ref_get_name(re->ref);
9968 error = got_pathlist_append(&tags,
9976 * To prevent accidents only branches in refs/heads/ can be deleted
9977 * with 'got send -d'.
9978 * Deleting anything else requires local repository access or Git.
9980 TAILQ_FOREACH(pe, &delete_args, entry) {
9981 const char *branchname = pe->path;
9983 struct got_pathlist_entry *new;
9984 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9985 s = strdup(branchname);
9987 error = got_error_from_errno("strdup");
9991 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9992 error = got_error_from_errno("asprintf");
9996 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9997 if (error || new == NULL /* duplicate */)
10001 ndelete_branches++;
10004 if (nbranches == 0 && ndelete_branches == 0) {
10005 struct got_reference *head_ref;
10007 error = got_ref_open(&head_ref, repo,
10008 got_worktree_get_head_ref_name(worktree), 0);
10010 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
10013 if (got_ref_is_symbolic(head_ref)) {
10014 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
10015 got_ref_close(head_ref);
10020 error = got_pathlist_append(&branches, got_ref_get_name(ref),
10028 /* Release work tree lock. */
10029 got_worktree_close(worktree);
10033 if (verbosity >= 0) {
10034 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10035 remote->name, proto, host,
10036 port ? ":" : "", port ? port : "",
10037 *server_path == '/' ? "" : "/", server_path);
10040 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
10041 server_path, verbosity);
10045 memset(&spa, 0, sizeof(spa));
10046 spa.last_scaled_packsize[0] = '\0';
10047 spa.last_p_deltify = -1;
10048 spa.last_p_written = -1;
10049 spa.verbosity = verbosity;
10050 spa.delete_branches = &delete_branches;
10051 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
10052 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
10053 check_cancelled, NULL);
10054 if (spa.printed_something)
10058 if (!spa.sent_something && verbosity >= 0)
10059 printf("Already up-to-date\n");
10062 if (kill(sendpid, SIGTERM) == -1)
10063 error = got_error_from_errno("kill");
10064 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
10065 error = got_error_from_errno("waitpid");
10067 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
10068 error = got_error_from_errno("close");
10070 const struct got_error *close_err = got_repo_close(repo);
10075 got_worktree_close(worktree);
10077 const struct got_error *pack_err =
10078 got_repo_pack_fds_close(pack_fds);
10083 got_ref_close(ref);
10084 got_repo_free_remote_repo_data(remote);
10086 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10087 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10088 got_ref_list_free(&all_branches);
10089 got_ref_list_free(&all_tags);
10090 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10091 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10103 * Print and if delete is set delete all ref_prefix references.
10104 * If wanted_ref is not NULL, only print or delete this reference.
10106 static const struct got_error *
10107 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10108 const char *wanted_ref, int delete, struct got_worktree *worktree,
10109 struct got_repository *repo)
10111 const struct got_error *err;
10112 struct got_pathlist_head paths;
10113 struct got_reflist_head refs;
10114 struct got_reflist_entry *re;
10115 struct got_reflist_object_id_map *refs_idmap = NULL;
10116 struct got_commit_object *commit = NULL;
10117 struct got_object_id *id = NULL;
10118 const char *header_prefix;
10119 char *uuidstr = NULL;
10123 TAILQ_INIT(&paths);
10125 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10129 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10133 if (worktree != NULL) {
10134 err = got_worktree_get_uuid(&uuidstr, worktree);
10140 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10144 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10145 header_prefix = "backout";
10147 header_prefix = "cherrypick";
10149 TAILQ_FOREACH(re, &refs, entry) {
10150 const char *refname, *wt;
10152 refname = got_ref_get_name(re->ref);
10154 err = check_cancelled(NULL);
10158 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10159 refname += prefix_len + 1; /* skip '-' delimiter */
10165 if (worktree == NULL || strncmp(refname, uuidstr,
10166 GOT_WORKTREE_UUID_STRLEN) == 0)
10167 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10171 err = got_repo_match_object_id(&id, NULL, refname,
10172 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10176 err = got_object_open_as_commit(&commit, repo, id);
10181 found = strncmp(wanted_ref, refname,
10182 strlen(wanted_ref)) == 0;
10183 if (wanted_ref && !found) {
10184 struct got_reflist_head *ci_refs;
10186 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10190 char *refs_str = NULL;
10191 char const *r = NULL;
10193 err = build_refs_str(&refs_str, ci_refs, id,
10200 if (strncmp(r, wanted_ref,
10201 strlen(wanted_ref)) == 0) {
10205 r = strchr(r, ' ');
10213 if (wanted_ref == NULL || found) {
10215 err = got_ref_delete(re->ref, repo);
10218 printf("Deleted: ");
10219 err = print_commit_oneline(commit, id, repo,
10223 * Print paths modified by commit to help
10224 * associate commits with worktree changes.
10226 err = get_changed_paths(&paths, commit,
10231 err = print_commit(commit, id, repo, NULL,
10232 &paths, NULL, 0, 0, refs_idmap, NULL,
10234 got_pathlist_free(&paths,
10235 GOT_PATHLIST_FREE_ALL);
10237 if (worktree == NULL)
10238 printf("work tree: %.*s\n\n",
10239 GOT_WORKTREE_UUID_STRLEN, wt);
10245 got_object_commit_close(commit);
10251 if (wanted_ref != NULL && !found)
10252 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10257 got_ref_list_free(&refs);
10258 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10260 got_reflist_object_id_map_free(refs_idmap);
10262 got_object_commit_close(commit);
10267 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10268 * identified by id for log messages to prepopulate the editor on commit.
10270 static const struct got_error *
10271 logmsg_ref(struct got_object_id *id, const char *prefix,
10272 struct got_worktree *worktree, struct got_repository *repo)
10274 const struct got_error *err = NULL;
10275 char *idstr, *ref = NULL, *refname = NULL;
10276 int histedit_in_progress;
10277 int rebase_in_progress, merge_in_progress;
10280 * Silenty refuse to create merge reference if any histedit, merge,
10281 * or rebase operation is in progress.
10283 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10287 if (histedit_in_progress)
10290 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10293 if (rebase_in_progress)
10296 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10300 if (merge_in_progress)
10303 err = got_object_id_str(&idstr, id);
10307 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10311 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10312 err = got_error_from_errno("asprintf");
10316 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10326 usage_cherrypick(void)
10328 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10333 static const struct got_error *
10334 cmd_cherrypick(int argc, char *argv[])
10336 const struct got_error *error = NULL;
10337 struct got_worktree *worktree = NULL;
10338 struct got_repository *repo = NULL;
10339 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10340 struct got_object_id *commit_id = NULL;
10341 struct got_commit_object *commit = NULL;
10342 struct got_object_qid *pid;
10343 int ch, list_refs = 0, remove_refs = 0;
10344 struct got_update_progress_arg upa;
10345 int *pack_fds = NULL;
10348 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10349 "unveil", NULL) == -1)
10353 while ((ch = getopt(argc, argv, "lX")) != -1) {
10362 usage_cherrypick();
10370 if (list_refs || remove_refs) {
10371 if (argc != 0 && argc != 1)
10372 usage_cherrypick();
10373 } else if (argc != 1)
10374 usage_cherrypick();
10375 if (list_refs && remove_refs)
10376 option_conflict('l', 'X');
10378 cwd = getcwd(NULL, 0);
10380 error = got_error_from_errno("getcwd");
10384 error = got_repo_pack_fds_open(&pack_fds);
10388 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10390 if (list_refs || remove_refs) {
10391 if (error->code != GOT_ERR_NOT_WORKTREE)
10394 if (error->code == GOT_ERR_NOT_WORKTREE)
10395 error = wrap_not_worktree_error(error,
10396 "cherrypick", cwd);
10401 error = got_repo_open(&repo,
10402 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10407 error = apply_unveil(got_repo_get_path(repo), 0,
10408 worktree ? got_worktree_get_root_path(worktree) : NULL);
10412 if (list_refs || remove_refs) {
10413 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10414 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10415 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10419 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10423 error = got_repo_match_object_id(&commit_id, NULL,
10424 keyword_idstr != NULL ? keyword_idstr : argv[0],
10425 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10428 error = got_object_id_str(&commit_id_str, commit_id);
10432 error = got_object_open_as_commit(&commit, repo, commit_id);
10435 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10436 memset(&upa, 0, sizeof(upa));
10437 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10438 commit_id, repo, update_progress, &upa, check_cancelled,
10443 if (upa.did_something) {
10444 error = logmsg_ref(commit_id,
10445 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10448 printf("Merged commit %s\n", commit_id_str);
10450 print_merge_progress_stats(&upa);
10453 free(keyword_idstr);
10455 got_object_commit_close(commit);
10456 free(commit_id_str);
10458 got_worktree_close(worktree);
10460 const struct got_error *close_err = got_repo_close(repo);
10465 const struct got_error *pack_err =
10466 got_repo_pack_fds_close(pack_fds);
10475 usage_backout(void)
10477 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10481 static const struct got_error *
10482 cmd_backout(int argc, char *argv[])
10484 const struct got_error *error = NULL;
10485 struct got_worktree *worktree = NULL;
10486 struct got_repository *repo = NULL;
10487 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10488 struct got_object_id *commit_id = NULL;
10489 struct got_commit_object *commit = NULL;
10490 struct got_object_qid *pid;
10491 int ch, list_refs = 0, remove_refs = 0;
10492 struct got_update_progress_arg upa;
10493 int *pack_fds = NULL;
10496 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10497 "unveil", NULL) == -1)
10501 while ((ch = getopt(argc, argv, "lX")) != -1) {
10518 if (list_refs || remove_refs) {
10519 if (argc != 0 && argc != 1)
10521 } else if (argc != 1)
10523 if (list_refs && remove_refs)
10524 option_conflict('l', 'X');
10526 cwd = getcwd(NULL, 0);
10528 error = got_error_from_errno("getcwd");
10532 error = got_repo_pack_fds_open(&pack_fds);
10536 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10538 if (list_refs || remove_refs) {
10539 if (error->code != GOT_ERR_NOT_WORKTREE)
10542 if (error->code == GOT_ERR_NOT_WORKTREE)
10543 error = wrap_not_worktree_error(error,
10549 error = got_repo_open(&repo,
10550 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10555 error = apply_unveil(got_repo_get_path(repo), 0,
10556 worktree ? got_worktree_get_root_path(worktree) : NULL);
10560 if (list_refs || remove_refs) {
10561 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10562 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10563 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10567 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10571 error = got_repo_match_object_id(&commit_id, NULL,
10572 keyword_idstr != NULL ? keyword_idstr : argv[0],
10573 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10576 error = got_object_id_str(&commit_id_str, commit_id);
10580 error = got_object_open_as_commit(&commit, repo, commit_id);
10583 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10585 error = got_error(GOT_ERR_ROOT_COMMIT);
10589 memset(&upa, 0, sizeof(upa));
10590 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10591 repo, update_progress, &upa, check_cancelled, NULL);
10595 if (upa.did_something) {
10596 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10600 printf("Backed out commit %s\n", commit_id_str);
10602 print_merge_progress_stats(&upa);
10605 free(keyword_idstr);
10607 got_object_commit_close(commit);
10608 free(commit_id_str);
10610 got_worktree_close(worktree);
10612 const struct got_error *close_err = got_repo_close(repo);
10617 const struct got_error *pack_err =
10618 got_repo_pack_fds_close(pack_fds);
10628 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10633 trim_logmsg(char *logmsg, int limit)
10638 len = strlen(logmsg);
10641 logmsg[len] = '\0';
10642 nl = strchr(logmsg, '\n');
10647 static const struct got_error *
10648 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10650 const struct got_error *err;
10651 char *logmsg0 = NULL;
10654 err = got_object_commit_get_logmsg(&logmsg0, commit);
10659 while (isspace((unsigned char)s[0]))
10662 *logmsg = strdup(s);
10663 if (*logmsg == NULL) {
10664 err = got_error_from_errno("strdup");
10668 trim_logmsg(*logmsg, limit);
10674 static const struct got_error *
10675 show_rebase_merge_conflict(struct got_object_id *id,
10676 struct got_repository *repo)
10678 const struct got_error *err;
10679 struct got_commit_object *commit = NULL;
10680 char *id_str = NULL, *logmsg = NULL;
10682 err = got_object_open_as_commit(&commit, repo, id);
10686 err = got_object_id_str(&id_str, id);
10692 err = get_short_logmsg(&logmsg, 42, commit);
10696 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10699 got_object_commit_close(commit);
10704 static const struct got_error *
10705 show_rebase_progress(struct got_commit_object *commit,
10706 struct got_object_id *old_id, struct got_object_id *new_id)
10708 const struct got_error *err;
10709 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10711 err = got_object_id_str(&old_id_str, old_id);
10716 err = got_object_id_str(&new_id_str, new_id);
10721 old_id_str[12] = '\0';
10723 new_id_str[12] = '\0';
10725 err = get_short_logmsg(&logmsg, 42, commit);
10729 printf("%s -> %s: %s\n", old_id_str,
10730 new_id_str ? new_id_str : "no-op change", logmsg);
10738 static const struct got_error *
10739 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10740 struct got_reference *branch, struct got_reference *tmp_branch,
10741 struct got_repository *repo, int create_backup)
10743 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10744 return got_worktree_rebase_complete(worktree, fileindex,
10745 tmp_branch, branch, repo, create_backup);
10748 static const struct got_error *
10749 rebase_commit(struct got_pathlist_head *merged_paths,
10750 struct got_worktree *worktree, struct got_fileindex *fileindex,
10751 struct got_reference *tmp_branch, const char *committer,
10752 struct got_object_id *commit_id, int allow_conflict,
10753 struct got_repository *repo)
10755 const struct got_error *error;
10756 struct got_commit_object *commit;
10757 struct got_object_id *new_commit_id;
10759 error = got_object_open_as_commit(&commit, repo, commit_id);
10763 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10764 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10765 allow_conflict, repo);
10767 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10769 error = show_rebase_progress(commit, commit_id, NULL);
10771 error = show_rebase_progress(commit, commit_id, new_commit_id);
10772 free(new_commit_id);
10775 got_object_commit_close(commit);
10779 struct check_path_prefix_arg {
10780 const char *path_prefix;
10785 static const struct got_error *
10786 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10787 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10788 struct got_object_id *id1, struct got_object_id *id2,
10789 const char *path1, const char *path2,
10790 mode_t mode1, mode_t mode2, struct got_repository *repo)
10792 struct check_path_prefix_arg *a = arg;
10794 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10795 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10796 return got_error(a->errcode);
10801 static const struct got_error *
10802 check_path_prefix(struct got_object_id *parent_id,
10803 struct got_object_id *commit_id, const char *path_prefix,
10804 int errcode, struct got_repository *repo)
10806 const struct got_error *err;
10807 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10808 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10809 struct check_path_prefix_arg cpp_arg;
10811 if (got_path_is_root_dir(path_prefix))
10814 err = got_object_open_as_commit(&commit, repo, commit_id);
10818 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10822 err = got_object_open_as_tree(&tree1, repo,
10823 got_object_commit_get_tree_id(parent_commit));
10827 err = got_object_open_as_tree(&tree2, repo,
10828 got_object_commit_get_tree_id(commit));
10832 cpp_arg.path_prefix = path_prefix;
10833 while (cpp_arg.path_prefix[0] == '/')
10834 cpp_arg.path_prefix++;
10835 cpp_arg.len = strlen(cpp_arg.path_prefix);
10836 cpp_arg.errcode = errcode;
10837 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10838 check_path_prefix_in_diff, &cpp_arg, 0);
10841 got_object_tree_close(tree1);
10843 got_object_tree_close(tree2);
10845 got_object_commit_close(commit);
10847 got_object_commit_close(parent_commit);
10851 static const struct got_error *
10852 collect_commits(struct got_object_id_queue *commits,
10853 struct got_object_id *initial_commit_id,
10854 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10855 const char *path_prefix, int path_prefix_errcode,
10856 struct got_repository *repo)
10858 const struct got_error *err = NULL;
10859 struct got_commit_graph *graph = NULL;
10860 struct got_object_id parent_id, commit_id;
10861 struct got_object_qid *qid;
10863 err = got_commit_graph_open(&graph, "/", 1);
10867 err = got_commit_graph_bfsort(graph, iter_start_id, repo,
10868 check_cancelled, NULL);
10872 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10873 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10874 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10875 check_cancelled, NULL);
10877 if (err->code == GOT_ERR_ITER_COMPLETED) {
10878 err = got_error_msg(GOT_ERR_ANCESTRY,
10879 "ran out of commits to rebase before "
10880 "youngest common ancestor commit has "
10881 "been reached?!?");
10885 err = check_path_prefix(&parent_id, &commit_id,
10886 path_prefix, path_prefix_errcode, repo);
10890 err = got_object_qid_alloc(&qid, &commit_id);
10893 STAILQ_INSERT_HEAD(commits, qid, entry);
10895 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10899 got_commit_graph_close(graph);
10903 static const struct got_error *
10904 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10906 const struct got_error *err = NULL;
10907 time_t committer_time;
10909 char datebuf[11]; /* YYYY-MM-DD + NUL */
10910 char *author0 = NULL, *author, *smallerthan;
10911 char *logmsg0 = NULL, *logmsg, *newline;
10913 committer_time = got_object_commit_get_committer_time(commit);
10914 if (gmtime_r(&committer_time, &tm) == NULL)
10915 return got_error_from_errno("gmtime_r");
10916 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10917 return got_error(GOT_ERR_NO_SPACE);
10919 author0 = strdup(got_object_commit_get_author(commit));
10920 if (author0 == NULL)
10921 return got_error_from_errno("strdup");
10923 smallerthan = strchr(author, '<');
10924 if (smallerthan && smallerthan[1] != '\0')
10925 author = smallerthan + 1;
10926 author[strcspn(author, "@>")] = '\0';
10928 err = got_object_commit_get_logmsg(&logmsg0, commit);
10932 while (*logmsg == '\n')
10934 newline = strchr(logmsg, '\n');
10938 if (asprintf(brief_str, "%s %s %s",
10939 datebuf, author, logmsg) == -1)
10940 err = got_error_from_errno("asprintf");
10947 static const struct got_error *
10948 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10949 struct got_repository *repo)
10951 const struct got_error *err;
10954 err = got_object_id_str(&id_str, id);
10958 err = got_ref_delete(ref, repo);
10962 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10968 static const struct got_error *
10969 print_backup_ref(const char *branch_name, const char *new_id_str,
10970 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10971 struct got_reflist_object_id_map *refs_idmap,
10972 struct got_repository *repo)
10974 const struct got_error *err = NULL;
10975 struct got_reflist_head *refs;
10976 char *refs_str = NULL;
10977 struct got_object_id *new_commit_id = NULL;
10978 struct got_commit_object *new_commit = NULL;
10979 char *new_commit_brief_str = NULL;
10980 struct got_object_id *yca_id = NULL;
10981 struct got_commit_object *yca_commit = NULL;
10982 char *yca_id_str = NULL, *yca_brief_str = NULL;
10983 char *custom_refs_str;
10985 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10986 return got_error_from_errno("asprintf");
10988 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10989 0, 0, refs_idmap, custom_refs_str, NULL);
10993 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10997 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10999 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
11004 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
11008 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
11012 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11013 old_commit_id, new_commit_id, 1, 0, repo, check_cancelled, NULL);
11017 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
11018 refs_str ? " (" : "", refs_str ? refs_str : "",
11019 refs_str ? ")" : "", new_commit_brief_str);
11020 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
11021 got_object_id_cmp(yca_id, old_commit_id) != 0) {
11025 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
11029 err = get_commit_brief_str(&yca_brief_str, yca_commit);
11033 err = got_object_id_str(&yca_id_str, yca_id);
11037 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
11039 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
11043 printf("history forked at %s%s%s%s\n %s\n",
11045 refs_str ? " (" : "", refs_str ? refs_str : "",
11046 refs_str ? ")" : "", yca_brief_str);
11049 free(custom_refs_str);
11050 free(new_commit_id);
11054 free(yca_brief_str);
11056 got_object_commit_close(new_commit);
11058 got_object_commit_close(yca_commit);
11063 static const struct got_error *
11064 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
11065 struct got_repository *repo)
11067 const struct got_error *err;
11068 struct got_reflist_head refs;
11069 struct got_reflist_entry *re;
11070 char *uuidstr = NULL;
11071 static char msg[160];
11075 err = got_worktree_get_uuid(&uuidstr, worktree);
11079 err = got_ref_list(&refs, repo, "refs/got/worktree",
11080 got_ref_cmp_by_name, repo);
11084 TAILQ_FOREACH(re, &refs, entry) {
11085 const char *cmd, *refname, *type;
11087 refname = got_ref_get_name(re->ref);
11089 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11090 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11091 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11092 cmd = "cherrypick";
11093 type = "cherrypicked";
11094 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11095 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11096 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11098 type = "backed-out";
11102 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11105 snprintf(msg, sizeof(msg),
11106 "work tree has references created by %s commits which "
11107 "must be removed with 'got %s -X' before running the %s "
11108 "command", type, cmd, caller);
11109 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11115 got_ref_list_free(&refs);
11119 static const struct got_error *
11120 process_backup_refs(const char *backup_ref_prefix,
11121 const char *wanted_branch_name,
11122 int delete, struct got_repository *repo)
11124 const struct got_error *err;
11125 struct got_reflist_head refs, backup_refs;
11126 struct got_reflist_entry *re;
11127 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11128 struct got_object_id *old_commit_id = NULL;
11129 char *branch_name = NULL;
11130 struct got_commit_object *old_commit = NULL;
11131 struct got_reflist_object_id_map *refs_idmap = NULL;
11132 int wanted_branch_found = 0;
11135 TAILQ_INIT(&backup_refs);
11137 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11141 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11145 if (wanted_branch_name) {
11146 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11147 wanted_branch_name += 11;
11150 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11151 got_ref_cmp_by_commit_timestamp_descending, repo);
11155 TAILQ_FOREACH(re, &backup_refs, entry) {
11156 const char *refname = got_ref_get_name(re->ref);
11159 err = check_cancelled(NULL);
11163 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11167 err = got_object_open_as_commit(&old_commit, repo,
11172 if (strncmp(backup_ref_prefix, refname,
11173 backup_ref_prefix_len) == 0)
11174 refname += backup_ref_prefix_len;
11176 while (refname[0] == '/')
11179 branch_name = strdup(refname);
11180 if (branch_name == NULL) {
11181 err = got_error_from_errno("strdup");
11184 slash = strrchr(branch_name, '/');
11187 refname += strlen(branch_name) + 1;
11190 if (wanted_branch_name == NULL ||
11191 strcmp(wanted_branch_name, branch_name) == 0) {
11192 wanted_branch_found = 1;
11194 err = delete_backup_ref(re->ref,
11195 old_commit_id, repo);
11197 err = print_backup_ref(branch_name, refname,
11198 old_commit_id, old_commit, refs_idmap,
11205 free(old_commit_id);
11206 old_commit_id = NULL;
11208 branch_name = NULL;
11209 got_object_commit_close(old_commit);
11213 if (wanted_branch_name && !wanted_branch_found) {
11214 err = got_error_fmt(GOT_ERR_NOT_REF,
11215 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11219 got_reflist_object_id_map_free(refs_idmap);
11220 got_ref_list_free(&refs);
11221 got_ref_list_free(&backup_refs);
11222 free(old_commit_id);
11225 got_object_commit_close(old_commit);
11229 static const struct got_error *
11230 abort_progress(void *arg, unsigned char status, const char *path)
11233 * Unversioned files should not clutter progress output when
11234 * an operation is aborted.
11236 if (status == GOT_STATUS_UNVERSIONED)
11239 return update_progress(arg, status, path);
11242 static const struct got_error *
11243 find_merge_commit_yca(struct got_object_id **new_yca_id,
11244 struct got_object_id *branch_head_commit_id,
11245 struct got_object_id *yca_id,
11246 struct got_object_id *base_commit_id,
11247 struct got_repository *repo)
11249 const struct got_error *err = NULL;
11250 struct got_commit_graph *graph = NULL;
11251 struct got_commit_object *commit = NULL;
11253 *new_yca_id = NULL;
11255 err = got_commit_graph_open(&graph, "/", 1);
11259 err = got_commit_graph_bfsort(graph, base_commit_id,
11260 repo, check_cancelled, NULL);
11265 struct got_object_id id;
11267 err = got_commit_graph_iter_next(&id, graph, repo,
11268 check_cancelled, NULL);
11270 if (err->code == GOT_ERR_ITER_COMPLETED)
11275 err = got_object_open_as_commit(&commit, repo, &id);
11279 if (got_object_commit_get_nparents(commit) > 1) {
11280 /* Search for a better YCA using toposort. */
11281 err = got_commit_graph_find_youngest_common_ancestor(
11282 new_yca_id, base_commit_id, branch_head_commit_id,
11283 0, 1, repo, check_cancelled, NULL);
11287 if (got_object_id_cmp(&id, yca_id) == 0)
11289 got_object_commit_close(commit);
11293 got_commit_graph_close(graph);
11295 got_object_commit_close(commit);
11299 static const struct got_error *
11300 cmd_rebase(int argc, char *argv[])
11302 const struct got_error *error = NULL;
11303 struct got_worktree *worktree = NULL;
11304 struct got_repository *repo = NULL;
11305 struct got_fileindex *fileindex = NULL;
11306 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11307 struct got_reference *branch = NULL;
11308 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11309 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11310 struct got_object_id *resume_commit_id = NULL;
11311 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11312 struct got_object_id *head_commit_id = NULL;
11313 struct got_reference *head_ref = NULL;
11314 struct got_commit_object *commit = NULL;
11315 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11316 int histedit_in_progress = 0, merge_in_progress = 0;
11317 int create_backup = 1, list_backups = 0, delete_backups = 0;
11318 int allow_conflict = 0;
11319 struct got_object_id_queue commits;
11320 struct got_pathlist_head merged_paths;
11321 const struct got_object_id_queue *parent_ids;
11322 struct got_object_qid *qid, *pid;
11323 struct got_update_progress_arg upa;
11324 int *pack_fds = NULL;
11326 STAILQ_INIT(&commits);
11327 TAILQ_INIT(&merged_paths);
11328 memset(&upa, 0, sizeof(upa));
11331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11332 "unveil", NULL) == -1)
11336 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11342 allow_conflict = 1;
11345 continue_rebase = 1;
11351 delete_backups = 1;
11362 if (list_backups) {
11364 option_conflict('l', 'a');
11365 if (allow_conflict)
11366 option_conflict('l', 'C');
11367 if (continue_rebase)
11368 option_conflict('l', 'c');
11369 if (delete_backups)
11370 option_conflict('l', 'X');
11371 if (argc != 0 && argc != 1)
11373 } else if (delete_backups) {
11375 option_conflict('X', 'a');
11376 if (allow_conflict)
11377 option_conflict('X', 'C');
11378 if (continue_rebase)
11379 option_conflict('X', 'c');
11381 option_conflict('l', 'X');
11382 if (argc != 0 && argc != 1)
11384 } else if (allow_conflict) {
11386 option_conflict('C', 'a');
11387 if (!continue_rebase)
11388 errx(1, "-C option requires -c");
11390 if (abort_rebase && continue_rebase)
11392 else if (abort_rebase || continue_rebase) {
11395 } else if (argc != 1)
11399 cwd = getcwd(NULL, 0);
11401 error = got_error_from_errno("getcwd");
11405 error = got_repo_pack_fds_open(&pack_fds);
11409 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11411 if (list_backups || delete_backups) {
11412 if (error->code != GOT_ERR_NOT_WORKTREE)
11415 if (error->code == GOT_ERR_NOT_WORKTREE)
11416 error = wrap_not_worktree_error(error,
11422 error = get_gitconfig_path(&gitconfig_path);
11425 error = got_repo_open(&repo,
11426 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11427 gitconfig_path, pack_fds);
11431 if (worktree != NULL && !list_backups && !delete_backups) {
11432 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11437 error = get_author(&committer, repo, worktree);
11438 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11441 error = apply_unveil(got_repo_get_path(repo), 0,
11442 worktree ? got_worktree_get_root_path(worktree) : NULL);
11446 if (list_backups || delete_backups) {
11447 error = process_backup_refs(
11448 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11449 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11450 goto done; /* nothing else to do */
11453 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11457 if (histedit_in_progress) {
11458 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11462 error = got_worktree_merge_in_progress(&merge_in_progress,
11466 if (merge_in_progress) {
11467 error = got_error(GOT_ERR_MERGE_BUSY);
11471 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11475 if (abort_rebase) {
11476 if (!rebase_in_progress) {
11477 error = got_error(GOT_ERR_NOT_REBASING);
11480 error = got_worktree_rebase_continue(&resume_commit_id,
11481 &new_base_branch, &tmp_branch, &branch, &fileindex,
11485 printf("Switching work tree to %s\n",
11486 got_ref_get_symref_target(new_base_branch));
11487 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11488 new_base_branch, abort_progress, &upa);
11491 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11492 print_merge_progress_stats(&upa);
11493 goto done; /* nothing else to do */
11496 if (continue_rebase) {
11497 if (!rebase_in_progress) {
11498 error = got_error(GOT_ERR_NOT_REBASING);
11501 error = got_worktree_rebase_continue(&resume_commit_id,
11502 &new_base_branch, &tmp_branch, &branch, &fileindex,
11507 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11508 committer, resume_commit_id, allow_conflict, repo);
11512 yca_id = got_object_id_dup(resume_commit_id);
11513 if (yca_id == NULL) {
11514 error = got_error_from_errno("got_object_id_dup");
11518 error = got_ref_open(&branch, repo, argv[0], 0);
11521 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11522 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11523 "will not rebase a branch which lives outside "
11524 "the \"refs/heads/\" reference namespace");
11529 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11533 if (!continue_rebase) {
11534 struct got_object_id *base_commit_id;
11536 error = got_ref_open(&head_ref, repo,
11537 got_worktree_get_head_ref_name(worktree), 0);
11540 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11543 base_commit_id = got_worktree_get_base_commit_id(worktree);
11544 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11545 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11549 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11550 base_commit_id, branch_head_commit_id, 1, 0,
11551 repo, check_cancelled, NULL);
11553 if (error->code == GOT_ERR_ANCESTRY) {
11554 error = got_error_msg(GOT_ERR_ANCESTRY,
11555 "specified branch shares no common "
11556 "ancestry with work tree's branch");
11562 * If a merge commit appears between the new base branch tip
11563 * and a YCA found via first-parent traversal then we might
11564 * find a better YCA using topologically sorted commits.
11566 if (got_object_id_cmp(base_commit_id, yca_id) != 0) {
11567 struct got_object_id *better_yca_id;
11568 error = find_merge_commit_yca(&better_yca_id,
11569 branch_head_commit_id, yca_id,
11570 base_commit_id, repo);
11573 if (better_yca_id) {
11575 yca_id = better_yca_id;
11579 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11580 struct got_pathlist_head paths;
11581 printf("%s is already based on %s\n",
11582 got_ref_get_name(branch),
11583 got_worktree_get_head_ref_name(worktree));
11584 error = switch_head_ref(branch, branch_head_commit_id,
11588 error = got_worktree_set_base_commit_id(worktree, repo,
11589 branch_head_commit_id);
11592 TAILQ_INIT(&paths);
11593 error = got_pathlist_append(&paths, "", NULL);
11596 error = got_worktree_checkout_files(worktree,
11597 &paths, repo, update_progress, &upa,
11598 check_cancelled, NULL);
11599 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11602 if (upa.did_something) {
11604 error = got_object_id_str(&id_str,
11605 branch_head_commit_id);
11608 printf("Updated to %s: %s\n",
11609 got_worktree_get_head_ref_name(worktree),
11613 printf("Already up-to-date\n");
11614 print_update_progress_stats(&upa);
11619 commit_id = branch_head_commit_id;
11620 error = got_object_open_as_commit(&commit, repo, commit_id);
11624 parent_ids = got_object_commit_get_parent_ids(commit);
11625 pid = STAILQ_FIRST(parent_ids);
11627 error = collect_commits(&commits, commit_id, &pid->id,
11628 yca_id, got_worktree_get_path_prefix(worktree),
11629 GOT_ERR_REBASE_PATH, repo);
11634 got_object_commit_close(commit);
11637 if (!continue_rebase) {
11638 error = got_worktree_rebase_prepare(&new_base_branch,
11639 &tmp_branch, &fileindex, worktree, branch, repo);
11644 if (STAILQ_EMPTY(&commits)) {
11645 if (continue_rebase) {
11646 error = rebase_complete(worktree, fileindex,
11647 branch, tmp_branch, repo, create_backup);
11650 /* Fast-forward the reference of the branch. */
11651 struct got_object_id *new_head_commit_id;
11653 error = got_ref_resolve(&new_head_commit_id, repo,
11657 error = got_object_id_str(&id_str, new_head_commit_id);
11660 printf("Forwarding %s to commit %s\n",
11661 got_ref_get_name(branch), id_str);
11663 error = got_ref_change_ref(branch,
11664 new_head_commit_id);
11667 /* No backup needed since objects did not change. */
11673 STAILQ_FOREACH(qid, &commits, entry) {
11675 commit_id = &qid->id;
11676 parent_id = pid ? &pid->id : yca_id;
11679 memset(&upa, 0, sizeof(upa));
11680 error = got_worktree_rebase_merge_files(&merged_paths,
11681 worktree, fileindex, parent_id, commit_id, repo,
11682 update_progress, &upa, check_cancelled, NULL);
11686 print_merge_progress_stats(&upa);
11687 if (upa.conflicts > 0 || upa.missing > 0 ||
11688 upa.not_deleted > 0 || upa.unversioned > 0) {
11689 if (upa.conflicts > 0) {
11690 error = show_rebase_merge_conflict(&qid->id,
11695 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11699 error = rebase_commit(&merged_paths, worktree, fileindex,
11700 tmp_branch, committer, commit_id, 0, repo);
11701 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11706 if (upa.conflicts > 0 || upa.missing > 0 ||
11707 upa.not_deleted > 0 || upa.unversioned > 0) {
11708 error = got_worktree_rebase_postpone(worktree, fileindex);
11711 if (upa.conflicts > 0 && upa.missing == 0 &&
11712 upa.not_deleted == 0 && upa.unversioned == 0) {
11713 error = got_error_msg(GOT_ERR_CONFLICTS,
11714 "conflicts must be resolved before rebasing "
11716 } else if (upa.conflicts > 0) {
11717 error = got_error_msg(GOT_ERR_CONFLICTS,
11718 "conflicts must be resolved before rebasing "
11719 "can continue; changes destined for some "
11720 "files were not yet merged and should be "
11721 "merged manually if required before the "
11722 "rebase operation is continued");
11724 error = got_error_msg(GOT_ERR_CONFLICTS,
11725 "changes destined for some files were not "
11726 "yet merged and should be merged manually "
11727 "if required before the rebase operation "
11731 error = rebase_complete(worktree, fileindex, branch,
11732 tmp_branch, repo, create_backup);
11736 free(gitconfig_path);
11737 got_object_id_queue_free(&commits);
11738 free(branch_head_commit_id);
11739 free(resume_commit_id);
11740 free(head_commit_id);
11743 got_object_commit_close(commit);
11745 got_ref_close(branch);
11746 if (new_base_branch)
11747 got_ref_close(new_base_branch);
11749 got_ref_close(tmp_branch);
11751 got_ref_close(head_ref);
11753 got_worktree_close(worktree);
11755 const struct got_error *close_err = got_repo_close(repo);
11760 const struct got_error *pack_err =
11761 got_repo_pack_fds_close(pack_fds);
11769 usage_histedit(void)
11771 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11772 "[branch]\n", getprogname());
11776 #define GOT_HISTEDIT_PICK 'p'
11777 #define GOT_HISTEDIT_EDIT 'e'
11778 #define GOT_HISTEDIT_FOLD 'f'
11779 #define GOT_HISTEDIT_DROP 'd'
11780 #define GOT_HISTEDIT_MESG 'm'
11782 static const struct got_histedit_cmd {
11783 unsigned char code;
11786 } got_histedit_cmds[] = {
11787 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11788 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11789 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11791 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11792 { GOT_HISTEDIT_MESG, "mesg", "open editor to edit the log message" },
11795 struct got_histedit_list_entry {
11796 TAILQ_ENTRY(got_histedit_list_entry) entry;
11797 struct got_object_id *commit_id;
11798 const struct got_histedit_cmd *cmd;
11801 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11803 static const struct got_error *
11804 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11805 FILE *f, struct got_repository *repo)
11807 const struct got_error *err = NULL;
11808 char *logmsg = NULL, *id_str = NULL;
11809 struct got_commit_object *commit = NULL;
11812 err = got_object_open_as_commit(&commit, repo, commit_id);
11816 err = get_short_logmsg(&logmsg, 34, commit);
11820 err = got_object_id_str(&id_str, commit_id);
11824 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11826 err = got_ferror(f, GOT_ERR_IO);
11829 got_object_commit_close(commit);
11835 static const struct got_error *
11836 histedit_write_commit_list(struct got_object_id_queue *commits,
11837 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11838 int edit_only, struct got_repository *repo)
11840 const struct got_error *err = NULL;
11841 struct got_object_qid *qid;
11842 const char *histedit_cmd = NULL;
11844 if (STAILQ_EMPTY(commits))
11845 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11847 STAILQ_FOREACH(qid, commits, entry) {
11848 histedit_cmd = got_histedit_cmds[0].name;
11850 histedit_cmd = "drop";
11851 else if (edit_only)
11852 histedit_cmd = "edit";
11853 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11854 histedit_cmd = "fold";
11855 else if (edit_logmsg_only)
11856 histedit_cmd = "mesg";
11857 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11865 static const struct got_error *
11866 write_cmd_list(FILE *f, const char *branch_name,
11867 struct got_object_id_queue *commits)
11869 const struct got_error *err = NULL;
11873 struct got_object_qid *qid;
11875 qid = STAILQ_FIRST(commits);
11876 err = got_object_id_str(&id_str, &qid->id);
11881 "# Editing the history of branch '%s' starting at\n"
11883 "# Commits will be processed in order from top to "
11884 "bottom of this file.\n", branch_name, id_str);
11886 err = got_ferror(f, GOT_ERR_IO);
11890 n = fprintf(f, "# Available histedit commands:\n");
11892 err = got_ferror(f, GOT_ERR_IO);
11896 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11897 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11898 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11901 err = got_ferror(f, GOT_ERR_IO);
11910 static const struct got_error *
11911 histedit_syntax_error(int lineno)
11913 static char msg[42];
11916 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11918 if (ret < 0 || (size_t)ret >= sizeof(msg))
11919 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11921 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11924 static const struct got_error *
11925 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11926 char *logmsg, struct got_repository *repo)
11928 const struct got_error *err;
11929 struct got_commit_object *folded_commit = NULL;
11930 char *id_str, *folded_logmsg = NULL;
11932 err = got_object_id_str(&id_str, hle->commit_id);
11936 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11940 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11943 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11944 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11945 folded_logmsg) == -1) {
11946 err = got_error_from_errno("asprintf");
11950 got_object_commit_close(folded_commit);
11952 free(folded_logmsg);
11956 static struct got_histedit_list_entry *
11957 get_folded_commits(struct got_histedit_list_entry *hle)
11959 struct got_histedit_list_entry *prev, *folded = NULL;
11961 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11962 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11963 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11964 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11966 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11972 static const struct got_error *
11973 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11974 const char *editor, struct got_repository *repo)
11976 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11977 char *logmsg = NULL, *new_msg = NULL;
11978 const struct got_error *err = NULL;
11979 struct got_commit_object *commit = NULL;
11982 struct got_histedit_list_entry *folded = NULL;
11984 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11988 folded = get_folded_commits(hle);
11990 while (folded != hle) {
11991 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11992 folded = TAILQ_NEXT(folded, entry);
11995 err = append_folded_commit_msg(&new_msg, folded,
12001 folded = TAILQ_NEXT(folded, entry);
12005 err = got_object_id_str(&id_str, hle->commit_id);
12008 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
12011 logmsg_len = asprintf(&new_msg,
12012 "%s\n# original log message of commit %s: %s",
12013 logmsg ? logmsg : "", id_str, orig_logmsg);
12014 if (logmsg_len == -1) {
12015 err = got_error_from_errno("asprintf");
12021 err = got_object_id_str(&id_str, hle->commit_id);
12025 err = got_opentemp_named_fd(&logmsg_path, &fd,
12026 GOT_TMPDIR_STR "/got-logmsg", "");
12030 if (write(fd, logmsg, logmsg_len) == -1) {
12031 err = got_error_from_errno2("write", logmsg_path);
12034 if (close(fd) == -1) {
12035 err = got_error_from_errno2("close", logmsg_path);
12040 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
12043 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
12046 hle->logmsg = strdup(new_msg);
12047 if (hle->logmsg == NULL)
12048 err = got_error_from_errno("strdup");
12051 if (fd != -1 && close(fd) == -1 && err == NULL)
12052 err = got_error_from_errno2("close", logmsg_path);
12053 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
12054 err = got_error_from_errno2("unlink", logmsg_path);
12059 got_object_commit_close(commit);
12063 static const struct got_error *
12064 histedit_parse_list(struct got_histedit_list *histedit_cmds,
12065 FILE *f, struct got_repository *repo)
12067 const struct got_error *err = NULL;
12068 char *line = NULL, *p, *end;
12069 size_t i, linesize = 0;
12072 const struct got_histedit_cmd *cmd;
12073 struct got_object_id *commit_id = NULL;
12074 struct got_histedit_list_entry *hle = NULL;
12077 linelen = getline(&line, &linesize, f);
12078 if (linelen == -1) {
12079 const struct got_error *getline_err;
12082 getline_err = got_error_from_errno("getline");
12083 err = got_ferror(f, getline_err->code);
12088 while (isspace((unsigned char)p[0]))
12090 if (p[0] == '#' || p[0] == '\0')
12093 for (i = 0; i < nitems(got_histedit_cmds); i++) {
12094 cmd = &got_histedit_cmds[i];
12095 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
12096 isspace((unsigned char)p[strlen(cmd->name)])) {
12097 p += strlen(cmd->name);
12100 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
12105 if (i == nitems(got_histedit_cmds)) {
12106 err = histedit_syntax_error(lineno);
12109 while (isspace((unsigned char)p[0]))
12112 while (end[0] && !isspace((unsigned char)end[0]))
12115 err = got_object_resolve_id_str(&commit_id, repo, p);
12117 /* override error code */
12118 err = histedit_syntax_error(lineno);
12121 hle = malloc(sizeof(*hle));
12123 err = got_error_from_errno("malloc");
12127 hle->commit_id = commit_id;
12128 hle->logmsg = NULL;
12130 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12138 static const struct got_error *
12139 histedit_check_script(struct got_histedit_list *histedit_cmds,
12140 struct got_object_id_queue *commits, struct got_repository *repo)
12142 const struct got_error *err = NULL;
12143 struct got_object_qid *qid;
12144 struct got_histedit_list_entry *hle;
12145 static char msg[92];
12148 if (TAILQ_EMPTY(histedit_cmds))
12149 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12150 "histedit script contains no commands");
12151 if (STAILQ_EMPTY(commits))
12152 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12154 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12155 struct got_histedit_list_entry *hle2;
12156 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12159 if (got_object_id_cmp(hle->commit_id,
12160 hle2->commit_id) != 0)
12162 err = got_object_id_str(&id_str, hle->commit_id);
12165 snprintf(msg, sizeof(msg), "commit %s is listed "
12166 "more than once in histedit script", id_str);
12168 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12172 STAILQ_FOREACH(qid, commits, entry) {
12173 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12174 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12178 err = got_object_id_str(&id_str, &qid->id);
12181 snprintf(msg, sizeof(msg),
12182 "commit %s missing from histedit script", id_str);
12184 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12188 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12189 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12190 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12191 "last commit in histedit script cannot be folded");
12196 static const struct got_error *
12197 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12198 const char *editor, const char *path,
12199 struct got_object_id_queue *commits, struct got_repository *repo)
12201 const struct got_error *err = NULL;
12202 struct stat st, st2;
12203 struct timespec timeout;
12206 if (stat(path, &st) == -1) {
12207 err = got_error_from_errno2("stat", path);
12211 if (spawn_editor(editor, path) == -1) {
12212 err = got_error_from_errno("failed spawning editor");
12216 timeout.tv_sec = 0;
12217 timeout.tv_nsec = 1;
12218 nanosleep(&timeout, NULL);
12220 if (stat(path, &st2) == -1) {
12221 err = got_error_from_errno2("stat", path);
12225 if (st.st_size == st2.st_size &&
12226 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12227 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12228 "no changes made to histedit script, aborting");
12232 f = fopen(path, "re");
12234 err = got_error_from_errno("fopen");
12237 err = histedit_parse_list(histedit_cmds, f, repo);
12241 err = histedit_check_script(histedit_cmds, commits, repo);
12243 if (f && fclose(f) == EOF && err == NULL)
12244 err = got_error_from_errno("fclose");
12248 static const struct got_error *
12249 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12250 struct got_object_id_queue *, const char *, const char *, const char *,
12251 struct got_repository *);
12253 static const struct got_error *
12254 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12255 struct got_object_id_queue *commits, const char *branch_name,
12256 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12257 const char *editor, struct got_repository *repo)
12259 const struct got_error *err;
12263 err = got_opentemp_named(&path, &f, "got-histedit", "");
12267 err = write_cmd_list(f, branch_name, commits);
12271 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12272 fold_only, drop_only, edit_only, repo);
12276 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12278 err = histedit_parse_list(histedit_cmds, f, repo);
12280 if (fclose(f) == EOF) {
12281 err = got_error_from_errno("fclose");
12285 err = histedit_run_editor(histedit_cmds, editor, path,
12288 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12289 err->code != GOT_ERR_HISTEDIT_CMD)
12291 err = histedit_edit_list_retry(histedit_cmds, err,
12292 commits, editor, path, branch_name, repo);
12296 if (f && fclose(f) == EOF && err == NULL)
12297 err = got_error_from_errno("fclose");
12298 if (path && unlink(path) != 0 && err == NULL)
12299 err = got_error_from_errno2("unlink", path);
12304 static const struct got_error *
12305 histedit_save_list(struct got_histedit_list *histedit_cmds,
12306 struct got_worktree *worktree, struct got_repository *repo)
12308 const struct got_error *err = NULL;
12311 struct got_histedit_list_entry *hle;
12313 err = got_worktree_get_histedit_script_path(&path, worktree);
12317 f = fopen(path, "we");
12319 err = got_error_from_errno2("fopen", path);
12322 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12323 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12329 if (f && fclose(f) == EOF && err == NULL)
12330 err = got_error_from_errno("fclose");
12336 histedit_free_list(struct got_histedit_list *histedit_cmds)
12338 struct got_histedit_list_entry *hle;
12340 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12341 TAILQ_REMOVE(histedit_cmds, hle, entry);
12346 static const struct got_error *
12347 histedit_load_list(struct got_histedit_list *histedit_cmds,
12348 const char *path, struct got_repository *repo)
12350 const struct got_error *err = NULL;
12353 f = fopen(path, "re");
12355 err = got_error_from_errno2("fopen", path);
12359 err = histedit_parse_list(histedit_cmds, f, repo);
12361 if (f && fclose(f) == EOF && err == NULL)
12362 err = got_error_from_errno("fclose");
12366 static const struct got_error *
12367 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12368 const struct got_error *edit_err, struct got_object_id_queue *commits,
12369 const char *editor, const char *path, const char *branch_name,
12370 struct got_repository *repo)
12372 const struct got_error *err = NULL, *prev_err = edit_err;
12375 while (resp != 'c' && resp != 'r' && resp != 'a') {
12376 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12377 "or (a)bort: ", getprogname(), prev_err->msg);
12382 histedit_free_list(histedit_cmds);
12383 err = histedit_run_editor(histedit_cmds, editor, path,
12386 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12387 err->code != GOT_ERR_HISTEDIT_CMD)
12394 } else if (resp == 'r') {
12395 histedit_free_list(histedit_cmds);
12396 err = histedit_edit_script(histedit_cmds,
12397 commits, branch_name, 0, 0, 0, 0, editor, repo);
12399 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12400 err->code != GOT_ERR_HISTEDIT_CMD)
12407 } else if (resp == 'a') {
12408 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12411 printf("invalid response '%c'\n", resp);
12417 static const struct got_error *
12418 histedit_complete(struct got_worktree *worktree,
12419 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12420 struct got_reference *branch, struct got_repository *repo)
12422 printf("Switching work tree to %s\n",
12423 got_ref_get_symref_target(branch));
12424 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12428 static const struct got_error *
12429 show_histedit_progress(struct got_commit_object *commit,
12430 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12432 const struct got_error *err;
12433 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12435 err = got_object_id_str(&old_id_str, hle->commit_id);
12440 err = got_object_id_str(&new_id_str, new_id);
12445 old_id_str[12] = '\0';
12447 new_id_str[12] = '\0';
12450 logmsg = strdup(hle->logmsg);
12451 if (logmsg == NULL) {
12452 err = got_error_from_errno("strdup");
12455 trim_logmsg(logmsg, 42);
12457 err = get_short_logmsg(&logmsg, 42, commit);
12462 switch (hle->cmd->code) {
12463 case GOT_HISTEDIT_PICK:
12464 case GOT_HISTEDIT_EDIT:
12465 case GOT_HISTEDIT_MESG:
12466 printf("%s -> %s: %s\n", old_id_str,
12467 new_id_str ? new_id_str : "no-op change", logmsg);
12469 case GOT_HISTEDIT_DROP:
12470 case GOT_HISTEDIT_FOLD:
12471 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12483 static const struct got_error *
12484 histedit_commit(struct got_pathlist_head *merged_paths,
12485 struct got_worktree *worktree, struct got_fileindex *fileindex,
12486 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12487 const char *committer, int allow_conflict, const char *editor,
12488 struct got_repository *repo)
12490 const struct got_error *err;
12491 struct got_commit_object *commit;
12492 struct got_object_id *new_commit_id;
12494 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12495 && hle->logmsg == NULL) {
12496 err = histedit_edit_logmsg(hle, editor, repo);
12501 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12505 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12506 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12507 hle->logmsg, allow_conflict, repo);
12509 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12511 err = show_histedit_progress(commit, hle, NULL);
12513 err = show_histedit_progress(commit, hle, new_commit_id);
12514 free(new_commit_id);
12517 got_object_commit_close(commit);
12521 static const struct got_error *
12522 histedit_skip_commit(struct got_histedit_list_entry *hle,
12523 struct got_worktree *worktree, struct got_repository *repo)
12525 const struct got_error *error;
12526 struct got_commit_object *commit;
12528 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12533 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12537 error = show_histedit_progress(commit, hle, NULL);
12538 got_object_commit_close(commit);
12542 static const struct got_error *
12543 check_local_changes(void *arg, unsigned char status,
12544 unsigned char staged_status, const char *path,
12545 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12546 struct got_object_id *commit_id, int dirfd, const char *de_name)
12548 int *have_local_changes = arg;
12551 case GOT_STATUS_ADD:
12552 case GOT_STATUS_DELETE:
12553 case GOT_STATUS_MODIFY:
12554 case GOT_STATUS_CONFLICT:
12555 *have_local_changes = 1;
12556 return got_error(GOT_ERR_CANCELLED);
12561 switch (staged_status) {
12562 case GOT_STATUS_ADD:
12563 case GOT_STATUS_DELETE:
12564 case GOT_STATUS_MODIFY:
12565 *have_local_changes = 1;
12566 return got_error(GOT_ERR_CANCELLED);
12574 static const struct got_error *
12575 cmd_histedit(int argc, char *argv[])
12577 const struct got_error *error = NULL;
12578 struct got_worktree *worktree = NULL;
12579 struct got_fileindex *fileindex = NULL;
12580 struct got_repository *repo = NULL;
12581 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12582 struct got_reference *branch = NULL;
12583 struct got_reference *tmp_branch = NULL;
12584 struct got_object_id *resume_commit_id = NULL;
12585 struct got_object_id *base_commit_id = NULL;
12586 struct got_object_id *head_commit_id = NULL;
12587 struct got_commit_object *commit = NULL;
12588 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12589 struct got_update_progress_arg upa;
12590 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12591 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12592 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12593 const char *edit_script_path = NULL;
12594 char *editor = NULL;
12595 struct got_object_id_queue commits;
12596 struct got_pathlist_head merged_paths;
12597 const struct got_object_id_queue *parent_ids;
12598 struct got_object_qid *pid;
12599 struct got_histedit_list histedit_cmds;
12600 struct got_histedit_list_entry *hle;
12601 int *pack_fds = NULL;
12603 STAILQ_INIT(&commits);
12604 TAILQ_INIT(&histedit_cmds);
12605 TAILQ_INIT(&merged_paths);
12606 memset(&upa, 0, sizeof(upa));
12609 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12610 "unveil", NULL) == -1)
12614 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12620 allow_conflict = 1;
12632 edit_script_path = optarg;
12641 edit_logmsg_only = 1;
12644 delete_backups = 1;
12655 if (abort_edit && allow_conflict)
12656 option_conflict('a', 'C');
12657 if (abort_edit && continue_edit)
12658 option_conflict('a', 'c');
12659 if (edit_script_path && allow_conflict)
12660 option_conflict('F', 'C');
12661 if (edit_script_path && edit_logmsg_only)
12662 option_conflict('F', 'm');
12663 if (abort_edit && edit_logmsg_only)
12664 option_conflict('a', 'm');
12665 if (edit_logmsg_only && allow_conflict)
12666 option_conflict('m', 'C');
12667 if (continue_edit && edit_logmsg_only)
12668 option_conflict('c', 'm');
12669 if (abort_edit && fold_only)
12670 option_conflict('a', 'f');
12671 if (fold_only && allow_conflict)
12672 option_conflict('f', 'C');
12673 if (continue_edit && fold_only)
12674 option_conflict('c', 'f');
12675 if (fold_only && edit_logmsg_only)
12676 option_conflict('f', 'm');
12677 if (edit_script_path && fold_only)
12678 option_conflict('F', 'f');
12679 if (abort_edit && edit_only)
12680 option_conflict('a', 'e');
12681 if (continue_edit && edit_only)
12682 option_conflict('c', 'e');
12683 if (edit_only && edit_logmsg_only)
12684 option_conflict('e', 'm');
12685 if (edit_script_path && edit_only)
12686 option_conflict('F', 'e');
12687 if (fold_only && edit_only)
12688 option_conflict('f', 'e');
12689 if (drop_only && abort_edit)
12690 option_conflict('d', 'a');
12691 if (drop_only && allow_conflict)
12692 option_conflict('d', 'C');
12693 if (drop_only && continue_edit)
12694 option_conflict('d', 'c');
12695 if (drop_only && edit_logmsg_only)
12696 option_conflict('d', 'm');
12697 if (drop_only && edit_only)
12698 option_conflict('d', 'e');
12699 if (drop_only && edit_script_path)
12700 option_conflict('d', 'F');
12701 if (drop_only && fold_only)
12702 option_conflict('d', 'f');
12703 if (list_backups) {
12705 option_conflict('l', 'a');
12706 if (allow_conflict)
12707 option_conflict('l', 'C');
12709 option_conflict('l', 'c');
12710 if (edit_script_path)
12711 option_conflict('l', 'F');
12712 if (edit_logmsg_only)
12713 option_conflict('l', 'm');
12715 option_conflict('l', 'd');
12717 option_conflict('l', 'f');
12719 option_conflict('l', 'e');
12720 if (delete_backups)
12721 option_conflict('l', 'X');
12722 if (argc != 0 && argc != 1)
12724 } else if (delete_backups) {
12726 option_conflict('X', 'a');
12727 if (allow_conflict)
12728 option_conflict('X', 'C');
12730 option_conflict('X', 'c');
12732 option_conflict('X', 'd');
12733 if (edit_script_path)
12734 option_conflict('X', 'F');
12735 if (edit_logmsg_only)
12736 option_conflict('X', 'm');
12738 option_conflict('X', 'f');
12740 option_conflict('X', 'e');
12742 option_conflict('X', 'l');
12743 if (argc != 0 && argc != 1)
12745 } else if (allow_conflict && !continue_edit)
12746 errx(1, "-C option requires -c");
12747 else if (argc != 0)
12750 cwd = getcwd(NULL, 0);
12752 error = got_error_from_errno("getcwd");
12756 error = got_repo_pack_fds_open(&pack_fds);
12760 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12762 if (list_backups || delete_backups) {
12763 if (error->code != GOT_ERR_NOT_WORKTREE)
12766 if (error->code == GOT_ERR_NOT_WORKTREE)
12767 error = wrap_not_worktree_error(error,
12773 if (list_backups || delete_backups) {
12774 error = got_repo_open(&repo,
12775 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12779 error = apply_unveil(got_repo_get_path(repo), 0,
12780 worktree ? got_worktree_get_root_path(worktree) : NULL);
12783 error = process_backup_refs(
12784 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12785 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12786 goto done; /* nothing else to do */
12788 error = get_gitconfig_path(&gitconfig_path);
12791 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12792 gitconfig_path, pack_fds);
12795 error = get_editor(&editor);
12798 if (unveil(editor, "x") != 0) {
12799 error = got_error_from_errno2("unveil", editor);
12802 if (edit_script_path) {
12803 if (unveil(edit_script_path, "r") != 0) {
12804 error = got_error_from_errno2("unveil",
12809 error = apply_unveil(got_repo_get_path(repo), 0,
12810 got_worktree_get_root_path(worktree));
12815 if (worktree != NULL && !list_backups && !delete_backups) {
12816 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12821 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12824 if (rebase_in_progress) {
12825 error = got_error(GOT_ERR_REBASING);
12829 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12833 if (merge_in_progress) {
12834 error = got_error(GOT_ERR_MERGE_BUSY);
12838 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12842 if (edit_in_progress && edit_logmsg_only) {
12843 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12844 "histedit operation is in progress in this "
12845 "work tree and must be continued or aborted "
12846 "before the -m option can be used");
12849 if (edit_in_progress && drop_only) {
12850 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12851 "histedit operation is in progress in this "
12852 "work tree and must be continued or aborted "
12853 "before the -d option can be used");
12856 if (edit_in_progress && fold_only) {
12857 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12858 "histedit operation is in progress in this "
12859 "work tree and must be continued or aborted "
12860 "before the -f option can be used");
12863 if (edit_in_progress && edit_only) {
12864 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12865 "histedit operation is in progress in this "
12866 "work tree and must be continued or aborted "
12867 "before the -e option can be used");
12871 if (edit_in_progress && abort_edit) {
12872 error = got_worktree_histedit_continue(&resume_commit_id,
12873 &tmp_branch, &branch, &base_commit_id, &fileindex,
12877 printf("Switching work tree to %s\n",
12878 got_ref_get_symref_target(branch));
12879 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12880 branch, base_commit_id, abort_progress, &upa);
12883 printf("Histedit of %s aborted\n",
12884 got_ref_get_symref_target(branch));
12885 print_merge_progress_stats(&upa);
12886 goto done; /* nothing else to do */
12887 } else if (abort_edit) {
12888 error = got_error(GOT_ERR_NOT_HISTEDIT);
12892 error = get_author(&committer, repo, worktree);
12896 if (continue_edit) {
12899 if (!edit_in_progress) {
12900 error = got_error(GOT_ERR_NOT_HISTEDIT);
12904 error = got_worktree_get_histedit_script_path(&path, worktree);
12908 error = histedit_load_list(&histedit_cmds, path, repo);
12913 error = got_worktree_histedit_continue(&resume_commit_id,
12914 &tmp_branch, &branch, &base_commit_id, &fileindex,
12919 error = got_ref_resolve(&head_commit_id, repo, branch);
12923 error = got_object_open_as_commit(&commit, repo,
12927 parent_ids = got_object_commit_get_parent_ids(commit);
12928 pid = STAILQ_FIRST(parent_ids);
12930 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12933 error = collect_commits(&commits, head_commit_id, &pid->id,
12934 base_commit_id, got_worktree_get_path_prefix(worktree),
12935 GOT_ERR_HISTEDIT_PATH, repo);
12936 got_object_commit_close(commit);
12941 if (edit_in_progress) {
12942 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12946 error = got_ref_open(&branch, repo,
12947 got_worktree_get_head_ref_name(worktree), 0);
12951 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12952 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12953 "will not edit commit history of a branch outside "
12954 "the \"refs/heads/\" reference namespace");
12958 error = got_ref_resolve(&head_commit_id, repo, branch);
12959 got_ref_close(branch);
12964 error = got_object_open_as_commit(&commit, repo,
12968 parent_ids = got_object_commit_get_parent_ids(commit);
12969 pid = STAILQ_FIRST(parent_ids);
12971 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12974 error = collect_commits(&commits, head_commit_id, &pid->id,
12975 got_worktree_get_base_commit_id(worktree),
12976 got_worktree_get_path_prefix(worktree),
12977 GOT_ERR_HISTEDIT_PATH, repo);
12978 got_object_commit_close(commit);
12983 if (STAILQ_EMPTY(&commits)) {
12984 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12988 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12989 &base_commit_id, &fileindex, worktree, repo);
12993 if (edit_script_path) {
12994 error = histedit_load_list(&histedit_cmds,
12995 edit_script_path, repo);
12997 got_worktree_histedit_abort(worktree, fileindex,
12998 repo, branch, base_commit_id,
12999 abort_progress, &upa);
13000 print_merge_progress_stats(&upa);
13004 const char *branch_name;
13005 branch_name = got_ref_get_symref_target(branch);
13006 if (strncmp(branch_name, "refs/heads/", 11) == 0)
13008 error = histedit_edit_script(&histedit_cmds, &commits,
13009 branch_name, edit_logmsg_only, fold_only,
13010 drop_only, edit_only, editor, repo);
13012 got_worktree_histedit_abort(worktree, fileindex,
13013 repo, branch, base_commit_id,
13014 abort_progress, &upa);
13015 print_merge_progress_stats(&upa);
13021 error = histedit_save_list(&histedit_cmds, worktree,
13024 got_worktree_histedit_abort(worktree, fileindex,
13025 repo, branch, base_commit_id,
13026 abort_progress, &upa);
13027 print_merge_progress_stats(&upa);
13033 error = histedit_check_script(&histedit_cmds, &commits, repo);
13037 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
13038 if (resume_commit_id) {
13039 if (got_object_id_cmp(hle->commit_id,
13040 resume_commit_id) != 0)
13043 resume_commit_id = NULL;
13044 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
13045 hle->cmd->code == GOT_HISTEDIT_FOLD) {
13046 error = histedit_skip_commit(hle, worktree,
13051 struct got_pathlist_head paths;
13052 int have_changes = 0;
13054 TAILQ_INIT(&paths);
13055 error = got_pathlist_append(&paths, "", NULL);
13058 error = got_worktree_status(worktree, &paths,
13059 repo, 0, check_local_changes, &have_changes,
13060 check_cancelled, NULL);
13061 got_pathlist_free(&paths,
13062 GOT_PATHLIST_FREE_NONE);
13064 if (error->code != GOT_ERR_CANCELLED)
13066 if (sigint_received || sigpipe_received)
13069 if (have_changes) {
13070 error = histedit_commit(NULL, worktree,
13071 fileindex, tmp_branch, hle,
13072 committer, allow_conflict, editor, repo);
13076 error = got_object_open_as_commit(
13077 &commit, repo, hle->commit_id);
13080 error = show_histedit_progress(commit,
13082 got_object_commit_close(commit);
13091 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
13092 error = histedit_skip_commit(hle, worktree, repo);
13097 error = got_object_open_as_commit(&commit, repo,
13101 parent_ids = got_object_commit_get_parent_ids(commit);
13102 pid = STAILQ_FIRST(parent_ids);
13104 error = got_worktree_histedit_merge_files(&merged_paths,
13105 worktree, fileindex, &pid->id, hle->commit_id, repo,
13106 update_progress, &upa, check_cancelled, NULL);
13109 got_object_commit_close(commit);
13112 print_merge_progress_stats(&upa);
13113 if (upa.conflicts > 0 || upa.missing > 0 ||
13114 upa.not_deleted > 0 || upa.unversioned > 0) {
13115 if (upa.conflicts > 0) {
13116 error = show_rebase_merge_conflict(
13117 hle->commit_id, repo);
13121 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13125 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13127 error = got_object_id_str(&id_str, hle->commit_id);
13130 printf("Stopping histedit for amending commit %s\n",
13133 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13134 error = got_worktree_histedit_postpone(worktree,
13137 } else if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13138 error = histedit_skip_commit(hle, worktree, repo);
13142 } else if (hle->cmd->code == GOT_HISTEDIT_MESG) {
13143 error = histedit_edit_logmsg(hle, editor, repo);
13148 error = histedit_commit(&merged_paths, worktree, fileindex,
13149 tmp_branch, hle, committer, allow_conflict, editor, repo);
13150 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13155 if (upa.conflicts > 0 || upa.missing > 0 ||
13156 upa.not_deleted > 0 || upa.unversioned > 0) {
13157 error = got_worktree_histedit_postpone(worktree, fileindex);
13160 if (upa.conflicts > 0 && upa.missing == 0 &&
13161 upa.not_deleted == 0 && upa.unversioned == 0) {
13162 error = got_error_msg(GOT_ERR_CONFLICTS,
13163 "conflicts must be resolved before histedit "
13165 } else if (upa.conflicts > 0) {
13166 error = got_error_msg(GOT_ERR_CONFLICTS,
13167 "conflicts must be resolved before histedit "
13168 "can continue; changes destined for some "
13169 "files were not yet merged and should be "
13170 "merged manually if required before the "
13171 "histedit operation is continued");
13173 error = got_error_msg(GOT_ERR_CONFLICTS,
13174 "changes destined for some files were not "
13175 "yet merged and should be merged manually "
13176 "if required before the histedit operation "
13180 error = histedit_complete(worktree, fileindex, tmp_branch,
13186 free(gitconfig_path);
13187 got_object_id_queue_free(&commits);
13188 histedit_free_list(&histedit_cmds);
13189 free(head_commit_id);
13190 free(base_commit_id);
13191 free(resume_commit_id);
13193 got_object_commit_close(commit);
13195 got_ref_close(branch);
13197 got_ref_close(tmp_branch);
13199 got_worktree_close(worktree);
13201 const struct got_error *close_err = got_repo_close(repo);
13206 const struct got_error *pack_err =
13207 got_repo_pack_fds_close(pack_fds);
13215 usage_integrate(void)
13217 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13221 static const struct got_error *
13222 cmd_integrate(int argc, char *argv[])
13224 const struct got_error *error = NULL;
13225 struct got_repository *repo = NULL;
13226 struct got_worktree *worktree = NULL;
13227 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13228 const char *branch_arg = NULL;
13229 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13230 struct got_fileindex *fileindex = NULL;
13231 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13233 struct got_update_progress_arg upa;
13234 int *pack_fds = NULL;
13237 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13238 "unveil", NULL) == -1)
13242 while ((ch = getopt(argc, argv, "")) != -1) {
13255 branch_arg = argv[0];
13257 cwd = getcwd(NULL, 0);
13259 error = got_error_from_errno("getcwd");
13263 error = got_repo_pack_fds_open(&pack_fds);
13267 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13269 if (error->code == GOT_ERR_NOT_WORKTREE)
13270 error = wrap_not_worktree_error(error, "integrate",
13275 error = check_rebase_or_histedit_in_progress(worktree);
13279 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13284 error = apply_unveil(got_repo_get_path(repo), 0,
13285 got_worktree_get_root_path(worktree));
13289 error = check_merge_in_progress(worktree, repo);
13293 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13294 error = got_error_from_errno("asprintf");
13298 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13299 &base_branch_ref, worktree, refname, repo);
13303 refname = strdup(got_ref_get_name(branch_ref));
13304 if (refname == NULL) {
13305 error = got_error_from_errno("strdup");
13306 got_worktree_integrate_abort(worktree, fileindex, repo,
13307 branch_ref, base_branch_ref);
13310 base_refname = strdup(got_ref_get_name(base_branch_ref));
13311 if (base_refname == NULL) {
13312 error = got_error_from_errno("strdup");
13313 got_worktree_integrate_abort(worktree, fileindex, repo,
13314 branch_ref, base_branch_ref);
13317 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13318 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13319 got_worktree_integrate_abort(worktree, fileindex, repo,
13320 branch_ref, base_branch_ref);
13324 error = got_ref_resolve(&commit_id, repo, branch_ref);
13328 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13332 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13333 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13334 "specified branch has already been integrated");
13335 got_worktree_integrate_abort(worktree, fileindex, repo,
13336 branch_ref, base_branch_ref);
13340 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13342 if (error->code == GOT_ERR_ANCESTRY)
13343 error = got_error(GOT_ERR_REBASE_REQUIRED);
13344 got_worktree_integrate_abort(worktree, fileindex, repo,
13345 branch_ref, base_branch_ref);
13349 memset(&upa, 0, sizeof(upa));
13350 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13351 branch_ref, base_branch_ref, update_progress, &upa,
13352 check_cancelled, NULL);
13356 printf("Integrated %s into %s\n", refname, base_refname);
13357 print_update_progress_stats(&upa);
13360 const struct got_error *close_err = got_repo_close(repo);
13365 got_worktree_close(worktree);
13367 const struct got_error *pack_err =
13368 got_repo_pack_fds_close(pack_fds);
13373 free(base_commit_id);
13376 free(base_refname);
13383 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13387 static const struct got_error *
13388 cmd_merge(int argc, char *argv[])
13390 const struct got_error *error = NULL;
13391 struct got_worktree *worktree = NULL;
13392 struct got_repository *repo = NULL;
13393 struct got_fileindex *fileindex = NULL;
13394 char *cwd = NULL, *id_str = NULL, *author = NULL;
13395 char *gitconfig_path = NULL;
13396 struct got_reference *branch = NULL, *wt_branch = NULL;
13397 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13398 struct got_object_id *wt_branch_tip = NULL;
13399 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13400 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13401 struct got_update_progress_arg upa;
13402 struct got_object_id *merge_commit_id = NULL;
13403 char *branch_name = NULL;
13404 int *pack_fds = NULL;
13406 memset(&upa, 0, sizeof(upa));
13409 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13410 "unveil", NULL) == -1)
13414 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13420 allow_conflict = 1;
13423 continue_merge = 1;
13426 prefer_fast_forward = 0;
13429 interrupt_merge = 1;
13441 if (continue_merge)
13442 option_conflict('a', 'c');
13443 if (!prefer_fast_forward)
13444 option_conflict('a', 'M');
13445 if (interrupt_merge)
13446 option_conflict('a', 'n');
13447 } else if (continue_merge) {
13448 if (!prefer_fast_forward)
13449 option_conflict('c', 'M');
13450 if (interrupt_merge)
13451 option_conflict('c', 'n');
13453 if (allow_conflict) {
13454 if (!continue_merge)
13455 errx(1, "-C option requires -c");
13457 if (abort_merge || continue_merge) {
13460 } else if (argc != 1)
13463 cwd = getcwd(NULL, 0);
13465 error = got_error_from_errno("getcwd");
13469 error = got_repo_pack_fds_open(&pack_fds);
13473 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13475 if (error->code == GOT_ERR_NOT_WORKTREE)
13476 error = wrap_not_worktree_error(error,
13481 error = get_gitconfig_path(&gitconfig_path);
13484 error = got_repo_open(&repo,
13485 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13486 gitconfig_path, pack_fds);
13490 if (worktree != NULL) {
13491 error = worktree_has_logmsg_ref("merge", worktree, repo);
13496 error = apply_unveil(got_repo_get_path(repo), 0,
13497 worktree ? got_worktree_get_root_path(worktree) : NULL);
13501 error = check_rebase_or_histedit_in_progress(worktree);
13505 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13510 if (merge_in_progress && !(abort_merge || continue_merge)) {
13511 error = got_error(GOT_ERR_MERGE_BUSY);
13515 if (!merge_in_progress && (abort_merge || continue_merge)) {
13516 error = got_error(GOT_ERR_NOT_MERGING);
13521 error = got_worktree_merge_continue(&branch_name,
13522 &branch_tip, &fileindex, worktree, repo);
13525 error = got_worktree_merge_abort(worktree, fileindex, repo,
13526 abort_progress, &upa);
13529 printf("Merge of %s aborted\n", branch_name);
13530 goto done; /* nothing else to do */
13533 if (strncmp(got_worktree_get_head_ref_name(worktree),
13534 "refs/heads/", 11) != 0) {
13535 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13536 "work tree's current branch %s is outside the "
13537 "\"refs/heads/\" reference namespace; "
13538 "update -b required",
13539 got_worktree_get_head_ref_name(worktree));
13543 error = get_author(&author, repo, worktree);
13547 error = got_ref_open(&wt_branch, repo,
13548 got_worktree_get_head_ref_name(worktree), 0);
13551 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13555 if (continue_merge) {
13556 struct got_object_id *base_commit_id;
13557 base_commit_id = got_worktree_get_base_commit_id(worktree);
13558 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13559 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13562 error = got_worktree_merge_continue(&branch_name,
13563 &branch_tip, &fileindex, worktree, repo);
13567 error = got_ref_open(&branch, repo, argv[0], 0);
13570 branch_name = strdup(got_ref_get_name(branch));
13571 if (branch_name == NULL) {
13572 error = got_error_from_errno("strdup");
13575 error = got_ref_resolve(&branch_tip, repo, branch);
13580 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13581 wt_branch_tip, branch_tip, 0, 0, repo,
13582 check_cancelled, NULL);
13583 if (error && error->code != GOT_ERR_ANCESTRY)
13586 if (!continue_merge) {
13587 error = check_path_prefix(wt_branch_tip, branch_tip,
13588 got_worktree_get_path_prefix(worktree),
13589 GOT_ERR_MERGE_PATH, repo);
13592 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13595 if (prefer_fast_forward && yca_id &&
13596 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13597 struct got_pathlist_head paths;
13598 if (interrupt_merge) {
13599 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13600 "there are no changes to merge since %s "
13601 "is already based on %s; merge cannot be "
13602 "interrupted for amending; -n",
13603 branch_name, got_ref_get_name(wt_branch));
13606 printf("Forwarding %s to %s\n",
13607 got_ref_get_name(wt_branch), branch_name);
13608 error = got_ref_change_ref(wt_branch, branch_tip);
13611 error = got_ref_write(wt_branch, repo);
13614 error = got_worktree_set_base_commit_id(worktree, repo,
13618 TAILQ_INIT(&paths);
13619 error = got_pathlist_append(&paths, "", NULL);
13622 error = got_worktree_checkout_files(worktree,
13623 &paths, repo, update_progress, &upa,
13624 check_cancelled, NULL);
13625 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13628 if (upa.did_something) {
13630 error = got_object_id_str(&id_str, branch_tip);
13633 printf("Updated to commit %s\n", id_str);
13636 printf("Already up-to-date\n");
13637 print_update_progress_stats(&upa);
13640 error = got_worktree_merge_write_refs(worktree, branch, repo);
13644 error = got_worktree_merge_branch(worktree, fileindex,
13645 yca_id, branch_tip, repo, update_progress, &upa,
13646 check_cancelled, NULL);
13649 print_merge_progress_stats(&upa);
13650 if (!upa.did_something) {
13651 error = got_worktree_merge_abort(worktree, fileindex,
13652 repo, abort_progress, &upa);
13655 printf("Already up-to-date\n");
13660 if (interrupt_merge) {
13661 error = got_worktree_merge_postpone(worktree, fileindex);
13664 printf("Merge of %s interrupted on request\n", branch_name);
13665 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13666 upa.not_deleted > 0 || upa.unversioned > 0) {
13667 error = got_worktree_merge_postpone(worktree, fileindex);
13670 if (upa.conflicts > 0 && upa.missing == 0 &&
13671 upa.not_deleted == 0 && upa.unversioned == 0) {
13672 error = got_error_msg(GOT_ERR_CONFLICTS,
13673 "conflicts must be resolved before merging "
13675 } else if (upa.conflicts > 0) {
13676 error = got_error_msg(GOT_ERR_CONFLICTS,
13677 "conflicts must be resolved before merging "
13678 "can continue; changes destined for some "
13679 "files were not yet merged and "
13680 "should be merged manually if required before the "
13681 "merge operation is continued");
13683 error = got_error_msg(GOT_ERR_CONFLICTS,
13684 "changes destined for some "
13685 "files were not yet merged and should be "
13686 "merged manually if required before the "
13687 "merge operation is continued");
13691 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13692 fileindex, author, NULL, 1, branch_tip, branch_name,
13693 allow_conflict, repo, continue_merge ? print_status : NULL,
13697 error = got_worktree_merge_complete(worktree, fileindex, repo);
13700 error = got_object_id_str(&id_str, merge_commit_id);
13703 printf("Merged %s into %s: %s\n", branch_name,
13704 got_worktree_get_head_ref_name(worktree),
13709 free(gitconfig_path);
13711 free(merge_commit_id);
13717 got_ref_close(branch);
13719 got_ref_close(wt_branch);
13721 got_worktree_close(worktree);
13723 const struct got_error *close_err = got_repo_close(repo);
13728 const struct got_error *pack_err =
13729 got_repo_pack_fds_close(pack_fds);
13739 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13740 "[path ...]\n", getprogname());
13744 static const struct got_error *
13745 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13746 const char *path, struct got_object_id *blob_id,
13747 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13748 int dirfd, const char *de_name)
13750 const struct got_error *err = NULL;
13751 char *id_str = NULL;
13753 if (staged_status != GOT_STATUS_ADD &&
13754 staged_status != GOT_STATUS_MODIFY &&
13755 staged_status != GOT_STATUS_DELETE)
13758 if (staged_status == GOT_STATUS_ADD ||
13759 staged_status == GOT_STATUS_MODIFY)
13760 err = got_object_id_str(&id_str, staged_blob_id);
13762 err = got_object_id_str(&id_str, blob_id);
13766 printf("%s %c %s\n", id_str, staged_status, path);
13771 static const struct got_error *
13772 cmd_stage(int argc, char *argv[])
13774 const struct got_error *error = NULL;
13775 struct got_repository *repo = NULL;
13776 struct got_worktree *worktree = NULL;
13778 struct got_pathlist_head paths;
13779 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13780 FILE *patch_script_file = NULL;
13781 const char *patch_script_path = NULL;
13782 struct choose_patch_arg cpa;
13783 int *pack_fds = NULL;
13785 TAILQ_INIT(&paths);
13788 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13789 "unveil", NULL) == -1)
13793 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13796 patch_script_path = optarg;
13805 allow_bad_symlinks = 1;
13816 if (list_stage && (pflag || patch_script_path))
13817 errx(1, "-l option cannot be used with other options");
13818 if (patch_script_path && !pflag)
13819 errx(1, "-F option can only be used together with -p option");
13821 cwd = getcwd(NULL, 0);
13823 error = got_error_from_errno("getcwd");
13827 error = got_repo_pack_fds_open(&pack_fds);
13831 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13833 if (error->code == GOT_ERR_NOT_WORKTREE)
13834 error = wrap_not_worktree_error(error, "stage", cwd);
13838 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13843 if (patch_script_path) {
13844 patch_script_file = fopen(patch_script_path, "re");
13845 if (patch_script_file == NULL) {
13846 error = got_error_from_errno2("fopen",
13847 patch_script_path);
13851 error = apply_unveil(got_repo_get_path(repo), 0,
13852 got_worktree_get_root_path(worktree));
13856 error = check_merge_in_progress(worktree, repo);
13860 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13865 error = got_worktree_status(worktree, &paths, repo, 0,
13866 print_stage, NULL, check_cancelled, NULL);
13868 cpa.patch_script_file = patch_script_file;
13869 cpa.action = "stage";
13870 error = got_worktree_stage(worktree, &paths,
13871 pflag ? NULL : print_status, NULL,
13872 pflag ? choose_patch : NULL, &cpa,
13873 allow_bad_symlinks, repo);
13876 if (patch_script_file && fclose(patch_script_file) == EOF &&
13878 error = got_error_from_errno2("fclose", patch_script_path);
13880 const struct got_error *close_err = got_repo_close(repo);
13885 got_worktree_close(worktree);
13887 const struct got_error *pack_err =
13888 got_repo_pack_fds_close(pack_fds);
13892 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13898 usage_unstage(void)
13900 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13901 "[path ...]\n", getprogname());
13906 static const struct got_error *
13907 cmd_unstage(int argc, char *argv[])
13909 const struct got_error *error = NULL;
13910 struct got_repository *repo = NULL;
13911 struct got_worktree *worktree = NULL;
13913 struct got_pathlist_head paths;
13915 struct got_update_progress_arg upa;
13916 FILE *patch_script_file = NULL;
13917 const char *patch_script_path = NULL;
13918 struct choose_patch_arg cpa;
13919 int *pack_fds = NULL;
13921 TAILQ_INIT(&paths);
13924 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13925 "unveil", NULL) == -1)
13929 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13932 patch_script_path = optarg;
13946 if (patch_script_path && !pflag)
13947 errx(1, "-F option can only be used together with -p option");
13949 cwd = getcwd(NULL, 0);
13951 error = got_error_from_errno("getcwd");
13955 error = got_repo_pack_fds_open(&pack_fds);
13959 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13961 if (error->code == GOT_ERR_NOT_WORKTREE)
13962 error = wrap_not_worktree_error(error, "unstage", cwd);
13966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13971 if (patch_script_path) {
13972 patch_script_file = fopen(patch_script_path, "re");
13973 if (patch_script_file == NULL) {
13974 error = got_error_from_errno2("fopen",
13975 patch_script_path);
13980 error = apply_unveil(got_repo_get_path(repo), 0,
13981 got_worktree_get_root_path(worktree));
13985 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13989 cpa.patch_script_file = patch_script_file;
13990 cpa.action = "unstage";
13991 memset(&upa, 0, sizeof(upa));
13992 error = got_worktree_unstage(worktree, &paths, update_progress,
13993 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13995 print_merge_progress_stats(&upa);
13997 if (patch_script_file && fclose(patch_script_file) == EOF &&
13999 error = got_error_from_errno2("fclose", patch_script_path);
14001 const struct got_error *close_err = got_repo_close(repo);
14006 got_worktree_close(worktree);
14008 const struct got_error *pack_err =
14009 got_repo_pack_fds_close(pack_fds);
14013 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14021 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14022 "arg ...\n", getprogname());
14026 static const struct got_error *
14027 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14029 const struct got_error *err;
14030 struct got_blob_object *blob;
14033 fd = got_opentempfd();
14035 return got_error_from_errno("got_opentempfd");
14037 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
14041 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
14043 if (fd != -1 && close(fd) == -1 && err == NULL)
14044 err = got_error_from_errno("close");
14046 got_object_blob_close(blob);
14050 static const struct got_error *
14051 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14053 const struct got_error *err;
14054 struct got_tree_object *tree;
14057 err = got_object_open_as_tree(&tree, repo, id);
14061 nentries = got_object_tree_get_nentries(tree);
14062 for (i = 0; i < nentries; i++) {
14063 struct got_tree_entry *te;
14065 if (sigint_received || sigpipe_received)
14067 te = got_object_tree_get_entry(tree, i);
14068 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
14071 fprintf(outfile, "%s %.7o %s\n", id_str,
14072 got_tree_entry_get_mode(te),
14073 got_tree_entry_get_name(te));
14077 got_object_tree_close(tree);
14081 static const struct got_error *
14082 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14084 const struct got_error *err;
14085 struct got_commit_object *commit;
14086 const struct got_object_id_queue *parent_ids;
14087 struct got_object_qid *pid;
14088 char *id_str = NULL;
14089 const char *logmsg = NULL;
14092 err = got_object_open_as_commit(&commit, repo, id);
14096 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
14100 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
14101 parent_ids = got_object_commit_get_parent_ids(commit);
14102 fprintf(outfile, "numparents %d\n",
14103 got_object_commit_get_nparents(commit));
14104 STAILQ_FOREACH(pid, parent_ids, entry) {
14106 err = got_object_id_str(&pid_str, &pid->id);
14109 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
14112 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14113 got_object_commit_get_author_gmtoff(commit));
14114 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14115 got_object_commit_get_author(commit),
14116 (long long)got_object_commit_get_author_time(commit),
14119 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14120 got_object_commit_get_committer_gmtoff(commit));
14121 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14122 got_object_commit_get_committer(commit),
14123 (long long)got_object_commit_get_committer_time(commit),
14126 logmsg = got_object_commit_get_logmsg_raw(commit);
14127 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14128 fprintf(outfile, "%s", logmsg);
14131 got_object_commit_close(commit);
14135 static const struct got_error *
14136 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14138 const struct got_error *err;
14139 struct got_tag_object *tag;
14140 char *id_str = NULL;
14141 const char *tagmsg = NULL;
14144 err = got_object_open_as_tag(&tag, repo, id);
14148 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14152 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14154 switch (got_object_tag_get_object_type(tag)) {
14155 case GOT_OBJ_TYPE_BLOB:
14156 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14157 GOT_OBJ_LABEL_BLOB);
14159 case GOT_OBJ_TYPE_TREE:
14160 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14161 GOT_OBJ_LABEL_TREE);
14163 case GOT_OBJ_TYPE_COMMIT:
14164 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14165 GOT_OBJ_LABEL_COMMIT);
14167 case GOT_OBJ_TYPE_TAG:
14168 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14169 GOT_OBJ_LABEL_TAG);
14175 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14176 got_object_tag_get_name(tag));
14178 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14179 got_object_tag_get_tagger_gmtoff(tag));
14180 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14181 got_object_tag_get_tagger(tag),
14182 (long long)got_object_tag_get_tagger_time(tag),
14185 tagmsg = got_object_tag_get_message(tag);
14186 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14187 fprintf(outfile, "%s", tagmsg);
14190 got_object_tag_close(tag);
14194 static const struct got_error *
14195 cmd_cat(int argc, char *argv[])
14197 const struct got_error *error;
14198 struct got_repository *repo = NULL;
14199 struct got_worktree *worktree = NULL;
14200 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14201 char *keyword_idstr = NULL;
14202 const char *commit_id_str = NULL;
14203 struct got_object_id *id = NULL, *commit_id = NULL;
14204 struct got_commit_object *commit = NULL;
14205 int ch, obj_type, i, force_path = 0;
14206 struct got_reflist_head refs;
14207 int *pack_fds = NULL;
14212 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14217 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14220 commit_id_str = optarg;
14226 repo_path = realpath(optarg, NULL);
14227 if (repo_path == NULL)
14228 return got_error_from_errno2("realpath",
14230 got_path_strip_trailing_slashes(repo_path);
14241 cwd = getcwd(NULL, 0);
14243 error = got_error_from_errno("getcwd");
14247 error = got_repo_pack_fds_open(&pack_fds);
14251 if (repo_path == NULL) {
14252 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14253 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14256 repo_path = strdup(
14257 got_worktree_get_repo_path(worktree));
14258 if (repo_path == NULL) {
14259 error = got_error_from_errno("strdup");
14263 if (commit_id_str == NULL) {
14264 /* Release work tree lock. */
14265 got_worktree_close(worktree);
14271 if (repo_path == NULL) {
14272 repo_path = strdup(cwd);
14273 if (repo_path == NULL)
14274 return got_error_from_errno("strdup");
14277 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14282 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14286 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14290 if (commit_id_str != NULL) {
14291 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14295 if (keyword_idstr != NULL)
14296 commit_id_str = keyword_idstr;
14297 if (worktree != NULL) {
14298 got_worktree_close(worktree);
14302 commit_id_str = GOT_REF_HEAD;
14303 error = got_repo_match_object_id(&commit_id, NULL,
14304 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14308 error = got_object_open_as_commit(&commit, repo, commit_id);
14312 for (i = 0; i < argc; i++) {
14314 error = got_object_id_by_path(&id, repo, commit,
14319 error = got_repo_match_object_id(&id, &label, argv[i],
14320 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14323 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14324 error->code != GOT_ERR_NOT_REF)
14326 error = got_object_id_by_path(&id, repo,
14333 error = got_object_get_type(&obj_type, repo, id);
14337 switch (obj_type) {
14338 case GOT_OBJ_TYPE_BLOB:
14339 error = cat_blob(id, repo, stdout);
14341 case GOT_OBJ_TYPE_TREE:
14342 error = cat_tree(id, repo, stdout);
14344 case GOT_OBJ_TYPE_COMMIT:
14345 error = cat_commit(id, repo, stdout);
14347 case GOT_OBJ_TYPE_TAG:
14348 error = cat_tag(id, repo, stdout);
14351 error = got_error(GOT_ERR_OBJ_TYPE);
14365 free(keyword_idstr);
14367 got_object_commit_close(commit);
14369 got_worktree_close(worktree);
14371 const struct got_error *close_err = got_repo_close(repo);
14376 const struct got_error *pack_err =
14377 got_repo_pack_fds_close(pack_fds);
14382 got_ref_list_free(&refs);
14389 fprintf(stderr, "usage: %s info [path ...]\n",
14394 static const struct got_error *
14395 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14396 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14397 struct got_object_id *commit_id)
14399 const struct got_error *err = NULL;
14400 char *id_str = NULL;
14402 struct tm mytm, *tm;
14403 struct got_pathlist_head *paths = arg;
14404 struct got_pathlist_entry *pe;
14407 * Clear error indication from any of the path arguments which
14408 * would cause this file index entry to be displayed.
14410 TAILQ_FOREACH(pe, paths, entry) {
14411 if (got_path_cmp(path, pe->path, strlen(path),
14412 pe->path_len) == 0 ||
14413 got_path_is_child(path, pe->path, pe->path_len))
14414 pe->data = NULL; /* no error */
14417 printf(GOT_COMMIT_SEP_STR);
14419 printf("symlink: %s\n", path);
14420 else if (S_ISREG(mode)) {
14421 printf("file: %s\n", path);
14422 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14423 } else if (S_ISDIR(mode))
14424 printf("directory: %s\n", path);
14426 printf("something: %s\n", path);
14428 tm = localtime_r(&mtime, &mytm);
14431 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14432 return got_error(GOT_ERR_NO_SPACE);
14433 printf("timestamp: %s\n", datebuf);
14436 err = got_object_id_str(&id_str, blob_id);
14439 printf("based on blob: %s\n", id_str);
14443 if (staged_blob_id) {
14444 err = got_object_id_str(&id_str, staged_blob_id);
14447 printf("based on staged blob: %s\n", id_str);
14452 err = got_object_id_str(&id_str, commit_id);
14455 printf("based on commit: %s\n", id_str);
14462 static const struct got_error *
14463 cmd_info(int argc, char *argv[])
14465 const struct got_error *error = NULL;
14466 struct got_worktree *worktree = NULL;
14467 char *cwd = NULL, *id_str = NULL;
14468 struct got_pathlist_head paths;
14469 char *uuidstr = NULL;
14470 int ch, show_files = 0;
14472 TAILQ_INIT(&paths);
14475 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14480 while ((ch = getopt(argc, argv, "")) != -1) {
14491 cwd = getcwd(NULL, 0);
14493 error = got_error_from_errno("getcwd");
14497 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14499 if (error->code == GOT_ERR_NOT_WORKTREE)
14500 error = wrap_not_worktree_error(error, "info", cwd);
14505 /* Remove "wpath cpath proc exec sendfd" promises. */
14506 if (pledge("stdio rpath flock unveil", NULL) == -1)
14509 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14514 error = get_worktree_paths_from_argv(&paths, argc, argv,
14521 error = got_object_id_str(&id_str,
14522 got_worktree_get_base_commit_id(worktree));
14526 error = got_worktree_get_uuid(&uuidstr, worktree);
14530 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14531 printf("work tree base commit: %s\n", id_str);
14532 printf("work tree path prefix: %s\n",
14533 got_worktree_get_path_prefix(worktree));
14534 printf("work tree branch reference: %s\n",
14535 got_worktree_get_head_ref_name(worktree));
14536 printf("work tree UUID: %s\n", uuidstr);
14537 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14540 struct got_pathlist_entry *pe;
14541 TAILQ_FOREACH(pe, &paths, entry) {
14542 if (pe->path_len == 0)
14545 * Assume this path will fail. This will be corrected
14546 * in print_path_info() in case the path does suceeed.
14548 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14550 error = got_worktree_path_info(worktree, &paths,
14551 print_path_info, &paths, check_cancelled, NULL);
14554 TAILQ_FOREACH(pe, &paths, entry) {
14555 if (pe->data != NULL) {
14556 const struct got_error *perr;
14559 error = got_error_fmt(perr->code, "%s",
14567 got_worktree_close(worktree);
14568 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);