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 ||
1650 strcmp(proto, "git+http") == 0 ||
1651 strcmp(proto, "http") == 0 ||
1652 strcmp(proto, "git+https") == 0 ||
1653 strcmp(proto, "https") == 0) {
1655 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1656 "sendfd unveil", NULL) == -1)
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);
1702 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
1706 if (!list_refs_only) {
1707 error = got_repo_init(repo_path, NULL);
1710 error = got_repo_pack_fds_open(&pack_fds);
1713 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1718 fpa.last_scaled_size[0] = '\0';
1719 fpa.last_p_indexed = -1;
1720 fpa.last_p_resolved = -1;
1721 fpa.verbosity = verbosity;
1722 fpa.create_configs = 1;
1723 fpa.configs_created = 0;
1725 fpa.config_info.symrefs = &symrefs;
1726 fpa.config_info.wanted_branches = &wanted_branches;
1727 fpa.config_info.wanted_refs = &wanted_refs;
1728 fpa.config_info.proto = proto;
1729 fpa.config_info.host = host;
1730 fpa.config_info.port = port;
1731 fpa.config_info.remote_repo_path = server_path;
1732 fpa.config_info.git_url = git_url;
1733 fpa.config_info.fetch_all_branches = fetch_all_branches;
1734 fpa.config_info.mirror_references = mirror_references;
1735 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1736 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1737 fetch_all_branches, &wanted_branches, &wanted_refs,
1738 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1739 fetch_progress, &fpa);
1743 if (list_refs_only) {
1744 error = list_remote_refs(&symrefs, &refs);
1748 if (pack_hash == NULL) {
1749 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1750 "server sent an empty pack file");
1753 error = got_object_id_str(&id_str, pack_hash);
1757 printf("\nFetched %s.pack\n", id_str);
1760 /* Set up references provided with the pack file. */
1761 TAILQ_FOREACH(pe, &refs, entry) {
1762 const char *refname = pe->path;
1763 struct got_object_id *id = pe->data;
1764 char *remote_refname;
1766 if (is_wanted_ref(&wanted_refs, refname) &&
1767 !mirror_references) {
1768 error = create_wanted_ref(refname, id,
1769 GOT_FETCH_DEFAULT_REMOTE_NAME,
1770 verbosity - 1, repo);
1776 error = create_ref(refname, id, verbosity - 1, repo);
1780 if (mirror_references)
1783 if (strncmp("refs/heads/", refname, 11) != 0)
1786 if (asprintf(&remote_refname,
1787 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1788 refname + 11) == -1) {
1789 error = got_error_from_errno("asprintf");
1792 error = create_ref(remote_refname, id, verbosity - 1, repo);
1793 free(remote_refname);
1798 /* Set the HEAD reference if the server provided one. */
1799 TAILQ_FOREACH(pe, &symrefs, entry) {
1800 struct got_reference *target_ref;
1801 const char *refname = pe->path;
1802 const char *target = pe->data;
1803 char *remote_refname = NULL, *remote_target = NULL;
1805 if (strcmp(refname, GOT_REF_HEAD) != 0)
1808 error = got_ref_open(&target_ref, repo, target, 0);
1810 if (error->code == GOT_ERR_NOT_REF) {
1817 error = create_symref(refname, target_ref, verbosity, repo);
1818 got_ref_close(target_ref);
1822 if (mirror_references)
1825 if (strncmp("refs/heads/", target, 11) != 0)
1828 if (asprintf(&remote_refname,
1829 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1831 error = got_error_from_errno("asprintf");
1834 if (asprintf(&remote_target,
1835 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1836 target + 11) == -1) {
1837 error = got_error_from_errno("asprintf");
1838 free(remote_refname);
1841 error = got_ref_open(&target_ref, repo, remote_target, 0);
1843 free(remote_refname);
1844 free(remote_target);
1845 if (error->code == GOT_ERR_NOT_REF) {
1851 error = create_symref(remote_refname, target_ref,
1852 verbosity - 1, repo);
1853 free(remote_refname);
1854 free(remote_target);
1855 got_ref_close(target_ref);
1861 * We failed to set the HEAD reference. If we asked for
1862 * a set of wanted branches use the first of one of those
1863 * which could be fetched instead.
1865 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1866 const char *target = pe->path;
1867 struct got_reference *target_ref;
1869 error = got_ref_open(&target_ref, repo, target, 0);
1871 if (error->code == GOT_ERR_NOT_REF) {
1878 error = create_symref(GOT_REF_HEAD, target_ref,
1880 got_ref_close(target_ref);
1886 if (!fpa.configs_created && pe != NULL) {
1887 error = create_config_files(fpa.config_info.proto,
1888 fpa.config_info.host, fpa.config_info.port,
1889 fpa.config_info.remote_repo_path,
1890 fpa.config_info.git_url,
1891 fpa.config_info.fetch_all_branches,
1892 fpa.config_info.mirror_references,
1893 fpa.config_info.symrefs,
1894 fpa.config_info.wanted_branches,
1895 fpa.config_info.wanted_refs, fpa.repo);
1902 printf("Created %s repository '%s'\n",
1903 mirror_references ? "mirrored" : "cloned", repo_path);
1906 const struct got_error *pack_err =
1907 got_repo_pack_fds_close(pack_fds);
1912 if (kill(fetchpid, SIGTERM) == -1)
1913 error = got_error_from_errno("kill");
1914 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1915 error = got_error_from_errno("waitpid");
1917 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1918 error = got_error_from_errno("close");
1920 const struct got_error *close_err = got_repo_close(repo);
1924 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1925 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1926 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1927 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1934 free(default_destdir);
1939 static const struct got_error *
1940 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1941 int replace_tags, int verbosity, struct got_repository *repo)
1943 const struct got_error *err = NULL;
1944 char *new_id_str = NULL;
1945 struct got_object_id *old_id = NULL;
1947 err = got_object_id_str(&new_id_str, new_id);
1951 if (!replace_tags &&
1952 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1953 err = got_ref_resolve(&old_id, repo, ref);
1956 if (got_object_id_cmp(old_id, new_id) == 0)
1958 if (verbosity >= 0) {
1959 printf("Rejecting update of existing tag %s: %s\n",
1960 got_ref_get_name(ref), new_id_str);
1965 if (got_ref_is_symbolic(ref)) {
1966 if (verbosity >= 0) {
1967 printf("Replacing reference %s: %s\n",
1968 got_ref_get_name(ref),
1969 got_ref_get_symref_target(ref));
1971 err = got_ref_change_symref_to_ref(ref, new_id);
1974 err = got_ref_write(ref, repo);
1978 err = got_ref_resolve(&old_id, repo, ref);
1981 if (got_object_id_cmp(old_id, new_id) == 0)
1984 err = got_ref_change_ref(ref, new_id);
1987 err = got_ref_write(ref, repo);
1993 printf("Updated %s: %s\n", got_ref_get_name(ref),
2001 static const struct got_error *
2002 update_symref(const char *refname, struct got_reference *target_ref,
2003 int verbosity, struct got_repository *repo)
2005 const struct got_error *err = NULL, *unlock_err;
2006 struct got_reference *symref;
2007 int symref_is_locked = 0;
2009 err = got_ref_open(&symref, repo, refname, 1);
2011 if (err->code != GOT_ERR_NOT_REF)
2013 err = got_ref_alloc_symref(&symref, refname, target_ref);
2017 err = got_ref_write(symref, repo);
2022 printf("Created reference %s: %s\n",
2023 got_ref_get_name(symref),
2024 got_ref_get_symref_target(symref));
2026 symref_is_locked = 1;
2028 if (strcmp(got_ref_get_symref_target(symref),
2029 got_ref_get_name(target_ref)) == 0)
2032 err = got_ref_change_symref(symref,
2033 got_ref_get_name(target_ref));
2037 err = got_ref_write(symref, repo);
2042 printf("Updated %s: %s\n", got_ref_get_name(symref),
2043 got_ref_get_symref_target(symref));
2047 if (symref_is_locked) {
2048 unlock_err = got_ref_unlock(symref);
2049 if (unlock_err && err == NULL)
2052 got_ref_close(symref);
2059 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2060 "[-R reference] [-r repository-path] [remote-repository]\n",
2065 static const struct got_error *
2066 delete_missing_ref(struct got_reference *ref,
2067 int verbosity, struct got_repository *repo)
2069 const struct got_error *err = NULL;
2070 struct got_object_id *id = NULL;
2071 char *id_str = NULL;
2073 if (got_ref_is_symbolic(ref)) {
2074 err = got_ref_delete(ref, repo);
2077 if (verbosity >= 0) {
2078 printf("Deleted %s: %s\n",
2079 got_ref_get_name(ref),
2080 got_ref_get_symref_target(ref));
2083 err = got_ref_resolve(&id, repo, ref);
2086 err = got_object_id_str(&id_str, id);
2090 err = got_ref_delete(ref, repo);
2093 if (verbosity >= 0) {
2094 printf("Deleted %s: %s\n",
2095 got_ref_get_name(ref), id_str);
2104 static const struct got_error *
2105 delete_missing_refs(struct got_pathlist_head *their_refs,
2106 struct got_pathlist_head *their_symrefs,
2107 const struct got_remote_repo *remote,
2108 int verbosity, struct got_repository *repo)
2110 const struct got_error *err = NULL, *unlock_err;
2111 struct got_reflist_head my_refs;
2112 struct got_reflist_entry *re;
2113 struct got_pathlist_entry *pe;
2114 char *remote_namespace = NULL;
2115 char *local_refname = NULL;
2117 TAILQ_INIT(&my_refs);
2119 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2121 return got_error_from_errno("asprintf");
2123 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2127 TAILQ_FOREACH(re, &my_refs, entry) {
2128 const char *refname = got_ref_get_name(re->ref);
2129 const char *their_refname;
2131 if (remote->mirror_references) {
2132 their_refname = refname;
2134 if (strncmp(refname, remote_namespace,
2135 strlen(remote_namespace)) == 0) {
2136 if (strcmp(refname + strlen(remote_namespace),
2139 if (asprintf(&local_refname, "refs/heads/%s",
2140 refname + strlen(remote_namespace)) == -1) {
2141 err = got_error_from_errno("asprintf");
2144 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2147 their_refname = local_refname;
2150 TAILQ_FOREACH(pe, their_refs, entry) {
2151 if (strcmp(their_refname, pe->path) == 0)
2157 TAILQ_FOREACH(pe, their_symrefs, entry) {
2158 if (strcmp(their_refname, pe->path) == 0)
2164 err = delete_missing_ref(re->ref, verbosity, repo);
2168 if (local_refname) {
2169 struct got_reference *ref;
2170 err = got_ref_open(&ref, repo, local_refname, 1);
2172 if (err->code != GOT_ERR_NOT_REF)
2174 free(local_refname);
2175 local_refname = NULL;
2178 err = delete_missing_ref(ref, verbosity, repo);
2181 unlock_err = got_ref_unlock(ref);
2183 if (unlock_err && err == NULL) {
2188 free(local_refname);
2189 local_refname = NULL;
2193 got_ref_list_free(&my_refs);
2194 free(remote_namespace);
2195 free(local_refname);
2199 static const struct got_error *
2200 update_wanted_ref(const char *refname, struct got_object_id *id,
2201 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2203 const struct got_error *err, *unlock_err;
2204 char *remote_refname;
2205 struct got_reference *ref;
2207 if (strncmp("refs/", refname, 5) == 0)
2210 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2211 remote_repo_name, refname) == -1)
2212 return got_error_from_errno("asprintf");
2214 err = got_ref_open(&ref, repo, remote_refname, 1);
2216 if (err->code != GOT_ERR_NOT_REF)
2218 err = create_ref(remote_refname, id, verbosity, repo);
2220 err = update_ref(ref, id, 0, verbosity, repo);
2221 unlock_err = got_ref_unlock(ref);
2222 if (unlock_err && err == NULL)
2227 free(remote_refname);
2231 static const struct got_error *
2232 delete_ref(struct got_repository *repo, struct got_reference *ref)
2234 const struct got_error *err = NULL;
2235 struct got_object_id *id = NULL;
2236 char *id_str = NULL;
2239 if (got_ref_is_symbolic(ref)) {
2240 target = got_ref_get_symref_target(ref);
2242 err = got_ref_resolve(&id, repo, ref);
2245 err = got_object_id_str(&id_str, id);
2251 err = got_ref_delete(ref, repo);
2255 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2262 static const struct got_error *
2263 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2265 const struct got_error *err = NULL;
2266 struct got_reflist_head refs;
2267 struct got_reflist_entry *re;
2272 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2273 err = got_error_from_errno("asprintf");
2276 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2280 TAILQ_FOREACH(re, &refs, entry)
2281 delete_ref(repo, re->ref);
2283 got_ref_list_free(&refs);
2287 static const struct got_error *
2288 cmd_fetch(int argc, char *argv[])
2290 const struct got_error *error = NULL, *unlock_err;
2291 char *cwd = NULL, *repo_path = NULL;
2292 const char *remote_name;
2293 char *proto = NULL, *host = NULL, *port = NULL;
2294 char *repo_name = NULL, *server_path = NULL;
2295 const struct got_remote_repo *remotes;
2296 struct got_remote_repo *remote = NULL;
2298 char *id_str = NULL;
2299 struct got_repository *repo = NULL;
2300 struct got_worktree *worktree = NULL;
2301 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2302 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2303 char *head_refname = NULL;
2304 struct got_pathlist_entry *pe;
2305 struct got_reflist_head remote_refs;
2306 struct got_reflist_entry *re;
2307 struct got_object_id *pack_hash = NULL;
2308 int i, ch, fetchfd = -1, fetchstatus;
2309 pid_t fetchpid = -1;
2310 struct got_fetch_progress_arg fpa;
2311 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2312 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2313 int *pack_fds = NULL, have_bflag = 0;
2314 const char *remote_head = NULL, *worktree_branch = NULL;
2317 TAILQ_INIT(&symrefs);
2318 TAILQ_INIT(&remote_refs);
2319 TAILQ_INIT(&wanted_branches);
2320 TAILQ_INIT(&wanted_refs);
2322 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2325 fetch_all_branches = 1;
2328 error = got_pathlist_append(&wanted_branches,
2344 error = got_pathlist_append(&wanted_refs,
2350 repo_path = realpath(optarg, NULL);
2351 if (repo_path == NULL)
2352 return got_error_from_errno2("realpath",
2354 got_path_strip_trailing_slashes(repo_path);
2362 else if (verbosity < 3)
2376 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2377 option_conflict('a', 'b');
2378 if (list_refs_only) {
2379 if (!TAILQ_EMPTY(&wanted_branches))
2380 option_conflict('l', 'b');
2381 if (fetch_all_branches)
2382 option_conflict('l', 'a');
2384 option_conflict('l', 'd');
2386 option_conflict('l', 'X');
2388 if (delete_remote) {
2389 if (fetch_all_branches)
2390 option_conflict('X', 'a');
2391 if (!TAILQ_EMPTY(&wanted_branches))
2392 option_conflict('X', 'b');
2394 option_conflict('X', 'd');
2396 option_conflict('X', 't');
2397 if (!TAILQ_EMPTY(&wanted_refs))
2398 option_conflict('X', 'R');
2403 errx(1, "-X option requires a remote name");
2404 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2405 } else if (argc == 1)
2406 remote_name = argv[0];
2410 cwd = getcwd(NULL, 0);
2412 error = got_error_from_errno("getcwd");
2416 error = got_repo_pack_fds_open(&pack_fds);
2420 if (repo_path == NULL) {
2421 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
2422 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2428 strdup(got_worktree_get_repo_path(worktree));
2429 if (repo_path == NULL)
2430 error = got_error_from_errno("strdup");
2434 repo_path = strdup(cwd);
2435 if (repo_path == NULL) {
2436 error = got_error_from_errno("strdup");
2442 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2446 if (delete_remote) {
2447 error = delete_refs_for_remote(repo, remote_name);
2448 goto done; /* nothing else to do */
2452 worktree_conf = got_worktree_get_gotconfig(worktree);
2453 if (worktree_conf) {
2454 got_gotconfig_get_remotes(&nremotes, &remotes,
2456 for (i = 0; i < nremotes; i++) {
2457 if (strcmp(remotes[i].name, remote_name) == 0) {
2458 error = got_repo_remote_repo_dup(&remote,
2467 if (remote == NULL) {
2468 repo_conf = got_repo_get_gotconfig(repo);
2470 got_gotconfig_get_remotes(&nremotes, &remotes,
2472 for (i = 0; i < nremotes; i++) {
2473 if (strcmp(remotes[i].name, remote_name) == 0) {
2474 error = got_repo_remote_repo_dup(&remote,
2483 if (remote == NULL) {
2484 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2485 for (i = 0; i < nremotes; i++) {
2486 if (strcmp(remotes[i].name, remote_name) == 0) {
2487 error = got_repo_remote_repo_dup(&remote,
2495 if (remote == NULL) {
2496 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2500 if (TAILQ_EMPTY(&wanted_branches)) {
2501 if (!fetch_all_branches)
2502 fetch_all_branches = remote->fetch_all_branches;
2503 for (i = 0; i < remote->nfetch_branches; i++) {
2504 error = got_pathlist_append(&wanted_branches,
2505 remote->fetch_branches[i], NULL);
2510 if (TAILQ_EMPTY(&wanted_refs)) {
2511 for (i = 0; i < remote->nfetch_refs; i++) {
2512 error = got_pathlist_append(&wanted_refs,
2513 remote->fetch_refs[i], NULL);
2519 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2520 &repo_name, remote->fetch_url);
2524 if (strcmp(proto, "git") == 0) {
2526 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2527 "sendfd dns inet unveil", NULL) == -1)
2530 } else if (strcmp(proto, "git+ssh") == 0 ||
2531 strcmp(proto, "ssh") == 0 ||
2532 strcmp(proto, "git+http") == 0 ||
2533 strcmp(proto, "http") == 0 ||
2534 strcmp(proto, "git+https") == 0 ||
2535 strcmp(proto, "https") == 0) {
2537 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2538 "sendfd unveil", NULL) == -1)
2542 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2546 error = got_dial_apply_unveil(proto);
2550 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2555 head_refname = strdup(got_worktree_get_head_ref_name(worktree));
2556 if (head_refname == NULL) {
2557 error = got_error_from_errno("strdup");
2561 /* Release work tree lock. */
2562 got_worktree_close(worktree);
2566 if (verbosity >= 0) {
2567 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2568 remote->name, proto, host,
2569 port ? ":" : "", port ? port : "",
2570 *server_path == '/' ? "" : "/", server_path);
2573 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2574 server_path, verbosity);
2578 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
2584 * If set, get this remote's HEAD ref target so
2585 * if it has changed on the server we can fetch it.
2587 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2588 got_ref_cmp_by_name, repo);
2592 TAILQ_FOREACH(re, &remote_refs, entry) {
2593 const char *remote_refname, *remote_target;
2594 size_t remote_name_len;
2596 if (!got_ref_is_symbolic(re->ref))
2599 remote_name_len = strlen(remote->name);
2600 remote_refname = got_ref_get_name(re->ref);
2602 /* we only want refs/remotes/$remote->name/HEAD */
2603 if (strncmp(remote_refname + 13, remote->name,
2604 remote_name_len) != 0)
2607 if (strcmp(remote_refname + remote_name_len + 14,
2612 * Take the name itself because we already
2613 * only match with refs/heads/ in fetch_pack().
2615 remote_target = got_ref_get_symref_target(re->ref);
2616 remote_head = remote_target + remote_name_len + 14;
2621 strncmp(head_refname, "refs/heads/", 11) == 0)
2622 worktree_branch = head_refname;
2625 fpa.last_scaled_size[0] = '\0';
2626 fpa.last_p_indexed = -1;
2627 fpa.last_p_resolved = -1;
2628 fpa.verbosity = verbosity;
2630 fpa.create_configs = 0;
2631 fpa.configs_created = 0;
2632 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2634 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2635 remote->mirror_references, fetch_all_branches, &wanted_branches,
2636 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2637 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2641 if (list_refs_only) {
2642 error = list_remote_refs(&symrefs, &refs);
2646 if (pack_hash == NULL) {
2648 printf("Already up-to-date\n");
2649 } else if (verbosity >= 0) {
2650 error = got_object_id_str(&id_str, pack_hash);
2653 printf("\nFetched %s.pack\n", id_str);
2658 /* Update references provided with the pack file. */
2659 TAILQ_FOREACH(pe, &refs, entry) {
2660 const char *refname = pe->path;
2661 struct got_object_id *id = pe->data;
2662 struct got_reference *ref;
2663 char *remote_refname;
2665 if (is_wanted_ref(&wanted_refs, refname) &&
2666 !remote->mirror_references) {
2667 error = update_wanted_ref(refname, id,
2668 remote->name, verbosity, repo);
2674 if (remote->mirror_references ||
2675 strncmp("refs/tags/", refname, 10) == 0) {
2676 error = got_ref_open(&ref, repo, refname, 1);
2678 if (error->code != GOT_ERR_NOT_REF)
2680 error = create_ref(refname, id, verbosity,
2685 error = update_ref(ref, id, replace_tags,
2687 unlock_err = got_ref_unlock(ref);
2688 if (unlock_err && error == NULL)
2694 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2695 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2696 remote_name, refname + 11) == -1) {
2697 error = got_error_from_errno("asprintf");
2701 error = got_ref_open(&ref, repo, remote_refname, 1);
2703 if (error->code != GOT_ERR_NOT_REF)
2705 error = create_ref(remote_refname, id,
2710 error = update_ref(ref, id, replace_tags,
2712 unlock_err = got_ref_unlock(ref);
2713 if (unlock_err && error == NULL)
2720 /* Also create a local branch if none exists yet. */
2721 error = got_ref_open(&ref, repo, refname, 1);
2723 if (error->code != GOT_ERR_NOT_REF)
2725 error = create_ref(refname, id, verbosity,
2730 unlock_err = got_ref_unlock(ref);
2731 if (unlock_err && error == NULL)
2738 error = delete_missing_refs(&refs, &symrefs, remote,
2744 if (!remote->mirror_references) {
2745 /* Update remote HEAD reference if the server provided one. */
2746 TAILQ_FOREACH(pe, &symrefs, entry) {
2747 struct got_reference *target_ref;
2748 const char *refname = pe->path;
2749 const char *target = pe->data;
2750 char *remote_refname = NULL, *remote_target = NULL;
2752 if (strcmp(refname, GOT_REF_HEAD) != 0)
2755 if (strncmp("refs/heads/", target, 11) != 0)
2758 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2759 remote->name, refname) == -1) {
2760 error = got_error_from_errno("asprintf");
2763 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2764 remote->name, target + 11) == -1) {
2765 error = got_error_from_errno("asprintf");
2766 free(remote_refname);
2770 error = got_ref_open(&target_ref, repo, remote_target,
2773 free(remote_refname);
2774 free(remote_target);
2775 if (error->code == GOT_ERR_NOT_REF) {
2781 error = update_symref(remote_refname, target_ref,
2783 free(remote_refname);
2784 free(remote_target);
2785 got_ref_close(target_ref);
2792 if (kill(fetchpid, SIGTERM) == -1)
2793 error = got_error_from_errno("kill");
2794 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2795 error = got_error_from_errno("waitpid");
2797 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2798 error = got_error_from_errno("close");
2800 const struct got_error *close_err = got_repo_close(repo);
2805 got_worktree_close(worktree);
2807 const struct got_error *pack_err =
2808 got_repo_pack_fds_close(pack_fds);
2812 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2813 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2814 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2815 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2816 got_ref_list_free(&remote_refs);
2817 got_repo_free_remote_repo_data(remote);
2834 usage_checkout(void)
2836 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2837 "[-p path-prefix] repository-path [work-tree-path]\n",
2843 show_worktree_base_ref_warning(void)
2845 fprintf(stderr, "%s: warning: could not create a reference "
2846 "to the work tree's base commit; the commit could be "
2847 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2848 "repository writable and running 'got update' will prevent this\n",
2852 struct got_checkout_progress_arg {
2853 const char *worktree_path;
2854 int had_base_commit_ref_error;
2858 static const struct got_error *
2859 checkout_progress(void *arg, unsigned char status, const char *path)
2861 struct got_checkout_progress_arg *a = arg;
2863 /* Base commit bump happens silently. */
2864 if (status == GOT_STATUS_BUMP_BASE)
2867 if (status == GOT_STATUS_BASE_REF_ERR) {
2868 a->had_base_commit_ref_error = 1;
2872 while (path[0] == '/')
2875 if (a->verbosity >= 0)
2876 printf("%c %s/%s\n", status, a->worktree_path, path);
2881 static const struct got_error *
2882 check_cancelled(void *arg)
2884 if (sigint_received || sigpipe_received)
2885 return got_error(GOT_ERR_CANCELLED);
2889 static const struct got_error *
2890 check_linear_ancestry(struct got_object_id *commit_id,
2891 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2892 struct got_repository *repo)
2894 const struct got_error *err = NULL;
2895 struct got_object_id *yca_id;
2897 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2898 commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2903 return got_error(GOT_ERR_ANCESTRY);
2906 * Require a straight line of history between the target commit
2907 * and the work tree's base commit.
2909 * Non-linear situations such as this require a rebase:
2911 * (commit) D F (base_commit)
2919 * 'got update' only handles linear cases:
2920 * Update forwards in time: A (base/yca) - B - C - D (commit)
2921 * Update backwards in time: D (base) - C - B - A (commit/yca)
2923 if (allow_forwards_in_time_only) {
2924 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2925 return got_error(GOT_ERR_ANCESTRY);
2926 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2927 got_object_id_cmp(base_commit_id, yca_id) != 0)
2928 return got_error(GOT_ERR_ANCESTRY);
2934 static const struct got_error *
2935 check_same_branch(struct got_object_id *commit_id,
2936 struct got_reference *head_ref, struct got_repository *repo)
2938 const struct got_error *err = NULL;
2939 struct got_commit_graph *graph = NULL;
2940 struct got_object_id *head_commit_id = NULL;
2942 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2946 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2949 err = got_commit_graph_open(&graph, "/", 1);
2953 err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2954 check_cancelled, NULL);
2959 struct got_object_id id;
2961 err = got_commit_graph_iter_next(&id, graph, repo,
2962 check_cancelled, NULL);
2964 if (err->code == GOT_ERR_ITER_COMPLETED)
2965 err = got_error(GOT_ERR_ANCESTRY);
2969 if (got_object_id_cmp(&id, commit_id) == 0)
2974 got_commit_graph_close(graph);
2975 free(head_commit_id);
2979 static const struct got_error *
2980 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2982 static char msg[512];
2983 const char *branch_name;
2985 if (got_ref_is_symbolic(ref))
2986 branch_name = got_ref_get_symref_target(ref);
2988 branch_name = got_ref_get_name(ref);
2990 if (strncmp("refs/heads/", branch_name, 11) == 0)
2993 snprintf(msg, sizeof(msg),
2994 "target commit is not contained in branch '%s'; "
2995 "the branch to use must be specified with -b; "
2996 "if necessary a new branch can be created for "
2997 "this commit with 'got branch -c %s BRANCH_NAME'",
2998 branch_name, commit_id_str);
3000 return got_error_msg(GOT_ERR_ANCESTRY, msg);
3003 static const struct got_error *
3004 cmd_checkout(int argc, char *argv[])
3006 const struct got_error *close_err, *error = NULL;
3007 struct got_repository *repo = NULL;
3008 struct got_reference *head_ref = NULL, *ref = NULL;
3009 struct got_worktree *worktree = NULL;
3010 char *repo_path = NULL;
3011 char *worktree_path = NULL;
3012 const char *path_prefix = "";
3013 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
3014 char *commit_id_str = NULL, *keyword_idstr = NULL;
3015 struct got_object_id *commit_id = NULL;
3017 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
3018 struct got_pathlist_head paths;
3019 struct got_checkout_progress_arg cpa;
3020 int *pack_fds = NULL;
3025 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3026 "unveil", NULL) == -1)
3030 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3033 branch_name = optarg;
3036 commit_id_str = strdup(optarg);
3037 if (commit_id_str == NULL)
3038 return got_error_from_errno("strdup");
3044 path_prefix = optarg;
3059 char *base, *dotgit;
3061 repo_path = realpath(argv[0], NULL);
3062 if (repo_path == NULL)
3063 return got_error_from_errno2("realpath", argv[0]);
3064 cwd = getcwd(NULL, 0);
3066 error = got_error_from_errno("getcwd");
3073 error = got_path_basename(&base, path);
3076 dotgit = strstr(base, ".git");
3079 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3080 error = got_error_from_errno("asprintf");
3085 } else if (argc == 2) {
3086 repo_path = realpath(argv[0], NULL);
3087 if (repo_path == NULL) {
3088 error = got_error_from_errno2("realpath", argv[0]);
3091 worktree_path = realpath(argv[1], NULL);
3092 if (worktree_path == NULL) {
3093 if (errno != ENOENT) {
3094 error = got_error_from_errno2("realpath",
3098 worktree_path = strdup(argv[1]);
3099 if (worktree_path == NULL) {
3100 error = got_error_from_errno("strdup");
3107 got_path_strip_trailing_slashes(repo_path);
3108 got_path_strip_trailing_slashes(worktree_path);
3110 if (got_path_is_child(worktree_path, repo_path, strlen(repo_path)) ||
3111 got_path_is_child(repo_path, worktree_path,
3112 strlen(worktree_path))) {
3113 error = got_error_fmt(GOT_ERR_BAD_PATH,
3114 "work tree and repository paths may not overlap: %s",
3119 error = got_repo_pack_fds_open(&pack_fds);
3123 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3127 /* Pre-create work tree path for unveil(2) */
3128 error = got_path_mkdir(worktree_path);
3130 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3131 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3133 if (!allow_nonempty &&
3134 !got_path_dir_is_empty(worktree_path)) {
3135 error = got_error_path(worktree_path,
3136 GOT_ERR_DIR_NOT_EMPTY);
3141 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3145 error = got_ref_open(&head_ref, repo, branch_name, 0);
3149 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3150 GOT_WORKTREE_GOT_DIR, repo);
3151 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3154 error = got_worktree_open(&worktree, worktree_path,
3155 GOT_WORKTREE_GOT_DIR);
3159 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3163 if (!same_path_prefix) {
3164 error = got_error(GOT_ERR_PATH_PREFIX);
3168 if (commit_id_str) {
3169 struct got_reflist_head refs;
3171 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3176 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3180 if (keyword_idstr != NULL) {
3181 free(commit_id_str);
3182 commit_id_str = keyword_idstr;
3185 error = got_repo_match_object_id(&commit_id, NULL,
3186 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3187 got_ref_list_free(&refs);
3190 error = check_linear_ancestry(commit_id,
3191 got_worktree_get_base_commit_id(worktree), 0, repo);
3192 if (error != NULL) {
3193 if (error->code == GOT_ERR_ANCESTRY) {
3194 error = checkout_ancestry_error(
3195 head_ref, commit_id_str);
3199 error = check_same_branch(commit_id, head_ref, repo);
3201 if (error->code == GOT_ERR_ANCESTRY) {
3202 error = checkout_ancestry_error(
3203 head_ref, commit_id_str);
3207 error = got_worktree_set_base_commit_id(worktree, repo,
3211 /* Expand potentially abbreviated commit ID string. */
3212 free(commit_id_str);
3213 error = got_object_id_str(&commit_id_str, commit_id);
3217 commit_id = got_object_id_dup(
3218 got_worktree_get_base_commit_id(worktree));
3219 if (commit_id == NULL) {
3220 error = got_error_from_errno("got_object_id_dup");
3223 error = got_object_id_str(&commit_id_str, commit_id);
3228 error = got_pathlist_append(&paths, "", NULL);
3231 cpa.worktree_path = worktree_path;
3232 cpa.had_base_commit_ref_error = 0;
3233 cpa.verbosity = verbosity;
3234 error = got_worktree_checkout_files(worktree, &paths, repo,
3235 checkout_progress, &cpa, check_cancelled, NULL);
3239 if (got_ref_is_symbolic(head_ref)) {
3240 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3243 refname = got_ref_get_name(ref);
3245 refname = got_ref_get_name(head_ref);
3246 printf("Checked out %s: %s\n", refname, commit_id_str);
3247 printf("Now shut up and hack\n");
3248 if (cpa.had_base_commit_ref_error)
3249 show_worktree_base_ref_warning();
3252 const struct got_error *pack_err =
3253 got_repo_pack_fds_close(pack_fds);
3258 got_ref_close(head_ref);
3262 close_err = got_repo_close(repo);
3266 if (worktree != NULL) {
3267 close_err = got_worktree_close(worktree);
3271 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3272 free(commit_id_str);
3275 free(worktree_path);
3280 struct got_update_progress_arg {
3292 print_update_progress_stats(struct got_update_progress_arg *upa)
3294 if (!upa->did_something)
3297 if (upa->conflicts > 0)
3298 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3299 if (upa->obstructed > 0)
3300 printf("File paths obstructed by a non-regular file: %d\n",
3302 if (upa->not_updated > 0)
3303 printf("Files not updated because of existing merge "
3304 "conflicts: %d\n", upa->not_updated);
3308 * The meaning of some status codes differs between merge-style operations and
3309 * update operations. For example, the ! status code means "file was missing"
3310 * if changes were merged into the work tree, and "missing file was restored"
3311 * if the work tree was updated. This function should be used by any operation
3312 * which merges changes into the work tree without updating the work tree.
3315 print_merge_progress_stats(struct got_update_progress_arg *upa)
3317 if (!upa->did_something)
3320 if (upa->conflicts > 0)
3321 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3322 if (upa->obstructed > 0)
3323 printf("File paths obstructed by a non-regular file: %d\n",
3325 if (upa->missing > 0)
3326 printf("Files which had incoming changes but could not be "
3327 "found in the work tree: %d\n", upa->missing);
3328 if (upa->not_deleted > 0)
3329 printf("Files not deleted due to differences in deleted "
3330 "content: %d\n", upa->not_deleted);
3331 if (upa->unversioned > 0)
3332 printf("Files not merged because an unversioned file was "
3333 "found in the work tree: %d\n", upa->unversioned);
3339 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3340 "[path ...]\n", getprogname());
3344 static const struct got_error *
3345 update_progress(void *arg, unsigned char status, const char *path)
3347 struct got_update_progress_arg *upa = arg;
3349 if (status == GOT_STATUS_EXISTS ||
3350 status == GOT_STATUS_BASE_REF_ERR)
3353 upa->did_something = 1;
3355 /* Base commit bump happens silently. */
3356 if (status == GOT_STATUS_BUMP_BASE)
3359 if (status == GOT_STATUS_CONFLICT)
3361 if (status == GOT_STATUS_OBSTRUCTED)
3363 if (status == GOT_STATUS_CANNOT_UPDATE)
3365 if (status == GOT_STATUS_MISSING)
3367 if (status == GOT_STATUS_CANNOT_DELETE)
3369 if (status == GOT_STATUS_UNVERSIONED)
3372 while (path[0] == '/')
3374 if (upa->verbosity >= 0)
3375 printf("%c %s\n", status, path);
3380 static const struct got_error *
3381 switch_head_ref(struct got_reference *head_ref,
3382 struct got_object_id *commit_id, struct got_worktree *worktree,
3383 struct got_repository *repo)
3385 const struct got_error *err = NULL;
3387 int ref_has_moved = 0;
3389 /* Trivial case: switching between two different references. */
3390 if (strcmp(got_ref_get_name(head_ref),
3391 got_worktree_get_head_ref_name(worktree)) != 0) {
3392 printf("Switching work tree from %s to %s\n",
3393 got_worktree_get_head_ref_name(worktree),
3394 got_ref_get_name(head_ref));
3395 return got_worktree_set_head_ref(worktree, head_ref);
3398 err = check_linear_ancestry(commit_id,
3399 got_worktree_get_base_commit_id(worktree), 0, repo);
3401 if (err->code != GOT_ERR_ANCESTRY)
3408 /* Switching to a rebased branch with the same reference name. */
3409 err = got_object_id_str(&base_id_str,
3410 got_worktree_get_base_commit_id(worktree));
3413 printf("Reference %s now points at a different branch\n",
3414 got_worktree_get_head_ref_name(worktree));
3415 printf("Switching work tree from %s to %s\n", base_id_str,
3416 got_worktree_get_head_ref_name(worktree));
3420 static const struct got_error *
3421 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3423 const struct got_error *err;
3426 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3430 return got_error(GOT_ERR_REBASING);
3432 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3436 return got_error(GOT_ERR_HISTEDIT_BUSY);
3441 static const struct got_error *
3442 check_merge_in_progress(struct got_worktree *worktree,
3443 struct got_repository *repo)
3445 const struct got_error *err;
3448 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3452 return got_error(GOT_ERR_MERGE_BUSY);
3457 static const struct got_error *
3458 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3459 char *argv[], struct got_worktree *worktree)
3461 const struct got_error *err = NULL;
3463 struct got_pathlist_entry *new;
3469 return got_error_from_errno("strdup");
3470 return got_pathlist_append(paths, path, NULL);
3473 for (i = 0; i < argc; i++) {
3474 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3477 err = got_pathlist_insert(&new, paths, path, NULL);
3478 if (err || new == NULL /* duplicate */) {
3488 static const struct got_error *
3489 wrap_not_worktree_error(const struct got_error *orig_err,
3490 const char *cmdname, const char *path)
3492 const struct got_error *err;
3493 struct got_repository *repo;
3494 static char msg[512];
3495 int *pack_fds = NULL;
3497 err = got_repo_pack_fds_open(&pack_fds);
3501 err = got_repo_open(&repo, path, NULL, pack_fds);
3505 snprintf(msg, sizeof(msg),
3506 "'got %s' needs a work tree in addition to a git repository\n"
3507 "Work trees can be checked out from this Git repository with "
3509 "The got(1) manual page contains more information.", cmdname);
3510 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3512 const struct got_error *close_err = got_repo_close(repo);
3517 const struct got_error *pack_err =
3518 got_repo_pack_fds_close(pack_fds);
3525 static const struct got_error *
3526 cmd_update(int argc, char *argv[])
3528 const struct got_error *close_err, *error = NULL;
3529 struct got_repository *repo = NULL;
3530 struct got_worktree *worktree = NULL;
3531 char *worktree_path = NULL;
3532 struct got_object_id *commit_id = NULL;
3533 char *commit_id_str = NULL;
3534 const char *branch_name = NULL;
3535 struct got_reference *head_ref = NULL;
3536 struct got_pathlist_head paths;
3537 struct got_pathlist_entry *pe;
3538 int ch, verbosity = 0;
3539 struct got_update_progress_arg upa;
3540 int *pack_fds = NULL;
3545 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3546 "unveil", NULL) == -1)
3550 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3553 branch_name = optarg;
3556 commit_id_str = strdup(optarg);
3557 if (commit_id_str == NULL)
3558 return got_error_from_errno("strdup");
3572 worktree_path = getcwd(NULL, 0);
3573 if (worktree_path == NULL) {
3574 error = got_error_from_errno("getcwd");
3578 error = got_repo_pack_fds_open(&pack_fds);
3582 error = got_worktree_open(&worktree, worktree_path,
3583 GOT_WORKTREE_GOT_DIR);
3585 if (error->code == GOT_ERR_NOT_WORKTREE)
3586 error = wrap_not_worktree_error(error, "update",
3591 error = check_rebase_or_histedit_in_progress(worktree);
3595 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3600 error = apply_unveil(got_repo_get_path(repo), 0,
3601 got_worktree_get_root_path(worktree));
3605 error = check_merge_in_progress(worktree, repo);
3609 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3613 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3614 got_worktree_get_head_ref_name(worktree), 0);
3617 if (commit_id_str == NULL) {
3618 error = got_ref_resolve(&commit_id, repo, head_ref);
3621 error = got_object_id_str(&commit_id_str, commit_id);
3625 struct got_reflist_head refs;
3626 char *keyword_idstr = NULL;
3630 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3635 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3639 if (keyword_idstr != NULL) {
3640 free(commit_id_str);
3641 commit_id_str = keyword_idstr;
3644 error = got_repo_match_object_id(&commit_id, NULL,
3645 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3646 got_ref_list_free(&refs);
3647 free(commit_id_str);
3648 commit_id_str = NULL;
3651 error = got_object_id_str(&commit_id_str, commit_id);
3657 struct got_object_id *head_commit_id;
3658 TAILQ_FOREACH(pe, &paths, entry) {
3659 if (pe->path_len == 0)
3661 error = got_error_msg(GOT_ERR_BAD_PATH,
3662 "switching between branches requires that "
3663 "the entire work tree gets updated");
3666 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3669 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3671 free(head_commit_id);
3674 error = check_same_branch(commit_id, head_ref, repo);
3677 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3681 error = check_linear_ancestry(commit_id,
3682 got_worktree_get_base_commit_id(worktree), 0, repo);
3683 if (error != NULL) {
3684 if (error->code == GOT_ERR_ANCESTRY)
3685 error = got_error(GOT_ERR_BRANCH_MOVED);
3688 error = check_same_branch(commit_id, head_ref, repo);
3693 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3695 error = got_worktree_set_base_commit_id(worktree, repo,
3701 memset(&upa, 0, sizeof(upa));
3702 upa.verbosity = verbosity;
3703 error = got_worktree_checkout_files(worktree, &paths, repo,
3704 update_progress, &upa, check_cancelled, NULL);
3708 if (upa.did_something) {
3709 printf("Updated to %s: %s\n",
3710 got_worktree_get_head_ref_name(worktree), commit_id_str);
3712 printf("Already up-to-date\n");
3714 print_update_progress_stats(&upa);
3717 const struct got_error *pack_err =
3718 got_repo_pack_fds_close(pack_fds);
3723 close_err = got_repo_close(repo);
3727 if (worktree != NULL) {
3728 close_err = got_worktree_close(worktree);
3732 if (head_ref != NULL)
3733 got_ref_close(head_ref);
3734 free(worktree_path);
3735 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3737 free(commit_id_str);
3741 static const struct got_error *
3742 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3743 const char *path, int diff_context, int ignore_whitespace,
3744 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3745 struct got_repository *repo, FILE *outfile)
3747 const struct got_error *err = NULL;
3748 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3749 FILE *f1 = NULL, *f2 = NULL;
3750 int fd1 = -1, fd2 = -1;
3752 fd1 = got_opentempfd();
3754 return got_error_from_errno("got_opentempfd");
3755 fd2 = got_opentempfd();
3757 err = got_error_from_errno("got_opentempfd");
3762 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3768 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3772 f1 = got_opentemp();
3774 err = got_error_from_errno("got_opentemp");
3777 f2 = got_opentemp();
3779 err = got_error_from_errno("got_opentemp");
3783 while (path[0] == '/')
3785 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3786 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3787 force_text_diff, dsa, outfile);
3789 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3790 err = got_error_from_errno("close");
3792 got_object_blob_close(blob1);
3793 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3794 err = got_error_from_errno("close");
3796 got_object_blob_close(blob2);
3797 if (f1 && fclose(f1) == EOF && err == NULL)
3798 err = got_error_from_errno("fclose");
3799 if (f2 && fclose(f2) == EOF && err == NULL)
3800 err = got_error_from_errno("fclose");
3804 static const struct got_error *
3805 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3806 const char *path, int diff_context, int ignore_whitespace,
3807 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3808 struct got_repository *repo, FILE *outfile)
3810 const struct got_error *err = NULL;
3811 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3812 struct got_diff_blob_output_unidiff_arg arg;
3813 FILE *f1 = NULL, *f2 = NULL;
3814 int fd1 = -1, fd2 = -1;
3817 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3820 fd1 = got_opentempfd();
3822 err = got_error_from_errno("got_opentempfd");
3827 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3831 f1 = got_opentemp();
3833 err = got_error_from_errno("got_opentemp");
3837 f2 = got_opentemp();
3839 err = got_error_from_errno("got_opentemp");
3842 fd2 = got_opentempfd();
3844 err = got_error_from_errno("got_opentempfd");
3847 arg.diff_context = diff_context;
3848 arg.ignore_whitespace = ignore_whitespace;
3849 arg.force_text_diff = force_text_diff;
3851 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3852 arg.outfile = outfile;
3855 while (path[0] == '/')
3857 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3858 got_diff_blob_output_unidiff, &arg, 1);
3861 got_object_tree_close(tree1);
3863 got_object_tree_close(tree2);
3864 if (f1 && fclose(f1) == EOF && err == NULL)
3865 err = got_error_from_errno("fclose");
3866 if (f2 && fclose(f2) == EOF && err == NULL)
3867 err = got_error_from_errno("fclose");
3868 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3869 err = got_error_from_errno("close");
3870 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3871 err = got_error_from_errno("close");
3875 static const struct got_error *
3876 get_changed_paths(struct got_pathlist_head *paths,
3877 struct got_commit_object *commit, struct got_repository *repo,
3878 struct got_diffstat_cb_arg *dsa)
3880 const struct got_error *err = NULL;
3881 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3882 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3883 struct got_object_qid *qid;
3884 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3885 FILE *f1 = NULL, *f2 = NULL;
3886 int fd1 = -1, fd2 = -1;
3889 cb = got_diff_tree_compute_diffstat;
3891 f1 = got_opentemp();
3893 err = got_error_from_errno("got_opentemp");
3896 f2 = got_opentemp();
3898 err = got_error_from_errno("got_opentemp");
3901 fd1 = got_opentempfd();
3903 err = got_error_from_errno("got_opentempfd");
3906 fd2 = got_opentempfd();
3908 err = got_error_from_errno("got_opentempfd");
3913 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3915 struct got_commit_object *pcommit;
3916 err = got_object_open_as_commit(&pcommit, repo,
3921 tree_id1 = got_object_id_dup(
3922 got_object_commit_get_tree_id(pcommit));
3923 if (tree_id1 == NULL) {
3924 got_object_commit_close(pcommit);
3925 return got_error_from_errno("got_object_id_dup");
3927 got_object_commit_close(pcommit);
3932 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3937 tree_id2 = got_object_commit_get_tree_id(commit);
3938 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3942 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3943 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3946 got_object_tree_close(tree1);
3948 got_object_tree_close(tree2);
3949 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3950 err = got_error_from_errno("close");
3951 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3952 err = got_error_from_errno("close");
3953 if (f1 && fclose(f1) == EOF && err == NULL)
3954 err = got_error_from_errno("fclose");
3955 if (f2 && fclose(f2) == EOF && err == NULL)
3956 err = got_error_from_errno("fclose");
3961 static const struct got_error *
3962 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3963 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3964 struct got_repository *repo, FILE *outfile)
3966 const struct got_error *err = NULL;
3967 struct got_commit_object *pcommit = NULL;
3968 char *id_str1 = NULL, *id_str2 = NULL;
3969 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3970 struct got_object_qid *qid;
3972 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3974 err = got_object_open_as_commit(&pcommit, repo,
3978 err = got_object_id_str(&id_str1, &qid->id);
3983 err = got_object_id_str(&id_str2, id);
3987 if (path && path[0] != '\0') {
3989 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3993 err = got_object_id_by_path(&obj_id1, repo,
3996 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
4002 err = got_object_get_type(&obj_type, repo, obj_id2);
4008 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4009 fprintf(outfile, "commit - %s\n",
4010 id_str1 ? id_str1 : "/dev/null");
4011 fprintf(outfile, "commit + %s\n", id_str2);
4013 case GOT_OBJ_TYPE_BLOB:
4014 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
4015 0, 0, dsa, repo, outfile);
4017 case GOT_OBJ_TYPE_TREE:
4018 err = diff_trees(obj_id1, obj_id2, path, diff_context,
4019 0, 0, dsa, repo, outfile);
4022 err = got_error(GOT_ERR_OBJ_TYPE);
4028 obj_id2 = got_object_commit_get_tree_id(commit);
4030 obj_id1 = got_object_commit_get_tree_id(pcommit);
4032 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4033 fprintf(outfile, "commit - %s\n",
4034 id_str1 ? id_str1 : "/dev/null");
4035 fprintf(outfile, "commit + %s\n", id_str2);
4036 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
4037 dsa, repo, outfile);
4043 got_object_commit_close(pcommit);
4048 get_datestr(time_t *time, char *datebuf)
4050 struct tm mytm, *tm;
4053 tm = gmtime_r(time, &mytm);
4056 s = asctime_r(tm, datebuf);
4059 p = strchr(s, '\n');
4065 static const struct got_error *
4066 match_commit(int *have_match, struct got_object_id *id,
4067 struct got_commit_object *commit, regex_t *regex)
4069 const struct got_error *err = NULL;
4070 regmatch_t regmatch;
4071 char *id_str = NULL, *logmsg = NULL;
4075 err = got_object_id_str(&id_str, id);
4079 err = got_object_commit_get_logmsg(&logmsg, commit);
4083 if (regexec(regex, got_object_commit_get_author(commit), 1,
4084 ®match, 0) == 0 ||
4085 regexec(regex, got_object_commit_get_committer(commit), 1,
4086 ®match, 0) == 0 ||
4087 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4088 regexec(regex, logmsg, 1, ®match, 0) == 0)
4097 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4100 regmatch_t regmatch;
4101 struct got_pathlist_entry *pe;
4105 TAILQ_FOREACH(pe, changed_paths, entry) {
4106 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4113 static const struct got_error *
4114 match_patch(int *have_match, struct got_commit_object *commit,
4115 struct got_object_id *id, const char *path, int diff_context,
4116 struct got_repository *repo, regex_t *regex, FILE *f)
4118 const struct got_error *err = NULL;
4120 size_t linesize = 0;
4121 regmatch_t regmatch;
4125 err = got_opentemp_truncate(f);
4129 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4133 if (fseeko(f, 0L, SEEK_SET) == -1) {
4134 err = got_error_from_errno("fseeko");
4138 while (getline(&line, &linesize, f) != -1) {
4139 if (regexec(regex, line, 1, ®match, 0) == 0) {
4149 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4151 static const struct got_error*
4152 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4153 struct got_object_id *id, struct got_repository *repo,
4156 static const struct got_error *err = NULL;
4157 struct got_reflist_entry *re;
4163 TAILQ_FOREACH(re, refs, entry) {
4164 struct got_tag_object *tag = NULL;
4165 struct got_object_id *ref_id;
4168 name = got_ref_get_name(re->ref);
4169 if (strcmp(name, GOT_REF_HEAD) == 0)
4171 if (strncmp(name, "refs/", 5) == 0)
4173 if (strncmp(name, "got/", 4) == 0)
4175 if (strncmp(name, "heads/", 6) == 0)
4177 if (strncmp(name, "remotes/", 8) == 0) {
4181 s = strstr(name, "/" GOT_REF_HEAD);
4182 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4185 err = got_ref_resolve(&ref_id, repo, re->ref);
4188 if (strncmp(name, "tags/", 5) == 0) {
4189 err = got_object_open_as_tag(&tag, repo, ref_id);
4191 if (err->code != GOT_ERR_OBJ_TYPE) {
4195 /* Ref points at something other than a tag. */
4200 cmp = got_object_id_cmp(tag ?
4201 got_object_tag_get_object_id(tag) : ref_id, id);
4204 got_object_tag_close(tag);
4208 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4209 s ? ", " : "", name) == -1) {
4210 err = got_error_from_errno("asprintf");
4221 static const struct got_error *
4222 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4223 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4225 const struct got_error *err = NULL;
4226 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4227 char *comma, *s, *nl;
4228 struct got_reflist_head *refs;
4229 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4231 time_t committer_time;
4233 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4235 err = build_refs_str(&ref_str, refs, id, repo, 1);
4239 /* Display the first matching ref only. */
4240 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4244 if (ref_str == NULL) {
4245 err = got_object_id_str(&id_str, id);
4250 committer_time = got_object_commit_get_committer_time(commit);
4251 if (gmtime_r(&committer_time, &tm) == NULL) {
4252 err = got_error_from_errno("gmtime_r");
4255 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4256 err = got_error(GOT_ERR_NO_SPACE);
4260 err = got_object_commit_get_logmsg(&logmsg0, commit);
4265 while (isspace((unsigned char)s[0]))
4268 nl = strchr(s, '\n');
4274 printf("%s%-7s %s\n", datebuf, ref_str, s);
4276 printf("%s%.7s %s\n", datebuf, id_str, s);
4278 if (fflush(stdout) != 0 && err == NULL)
4279 err = got_error_from_errno("fflush");
4287 static const struct got_error *
4288 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4290 struct got_pathlist_entry *pe;
4293 printf("%s\n", header);
4295 TAILQ_FOREACH(pe, dsa->paths, entry) {
4296 struct got_diff_changed_path *cp = pe->data;
4297 int pad = dsa->max_path_len - pe->path_len + 1;
4299 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4300 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4302 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4303 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4304 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4306 if (fflush(stdout) != 0)
4307 return got_error_from_errno("fflush");
4312 static const struct got_error *
4318 if (fseeko(f, 0L, SEEK_SET) == -1)
4319 return got_error_from_errno("fseek");
4322 r = fread(buf, 1, sizeof(buf), f);
4325 return got_error_from_errno("fread");
4329 if (fwrite(buf, 1, r, stdout) != r)
4330 return got_ferror(stdout, GOT_ERR_IO);
4336 static const struct got_error *
4337 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4338 struct got_repository *repo, const char *path,
4339 struct got_pathlist_head *changed_paths,
4340 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4341 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4344 const struct got_error *err = NULL;
4346 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4348 time_t committer_time;
4349 const char *author, *committer;
4350 char *refs_str = NULL;
4352 err = got_object_id_str(&id_str, id);
4356 if (custom_refs_str == NULL) {
4357 struct got_reflist_head *refs;
4358 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4360 err = build_refs_str(&refs_str, refs, id, repo, 0);
4366 printf(GOT_COMMIT_SEP_STR);
4367 if (custom_refs_str)
4368 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4371 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4372 refs_str ? " (" : "", refs_str ? refs_str : "",
4373 refs_str ? ")" : "");
4378 printf("from: %s\n", got_object_commit_get_author(commit));
4379 author = got_object_commit_get_author(commit);
4380 committer = got_object_commit_get_committer(commit);
4381 if (strcmp(author, committer) != 0)
4382 printf("via: %s\n", committer);
4383 committer_time = got_object_commit_get_committer_time(commit);
4384 datestr = get_datestr(&committer_time, datebuf);
4386 printf("date: %s UTC\n", datestr);
4387 if (got_object_commit_get_nparents(commit) > 1) {
4388 const struct got_object_id_queue *parent_ids;
4389 struct got_object_qid *qid;
4391 parent_ids = got_object_commit_get_parent_ids(commit);
4392 STAILQ_FOREACH(qid, parent_ids, entry) {
4393 err = got_object_id_str(&id_str, &qid->id);
4396 printf("parent %d: %s\n", n++, id_str);
4402 err = got_object_commit_get_logmsg(&logmsg0, commit);
4408 line = strsep(&logmsg, "\n");
4410 printf(" %s\n", line);
4414 if (changed_paths && diffstat == NULL) {
4415 struct got_pathlist_entry *pe;
4417 TAILQ_FOREACH(pe, changed_paths, entry) {
4418 struct got_diff_changed_path *cp = pe->data;
4420 printf(" %c %s\n", cp->status, pe->path);
4428 err = got_error_from_errno("got_opentemp");
4433 err = print_patch(commit, id, path, diff_context, diffstat,
4434 repo, diffstat == NULL ? stdout : f);
4439 err = print_diffstat(diffstat, NULL);
4451 if (fflush(stdout) != 0 && err == NULL)
4452 err = got_error_from_errno("fflush");
4454 if (f && fclose(f) == EOF && err == NULL)
4455 err = got_error_from_errno("fclose");
4461 static const struct got_error *
4462 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4463 struct got_repository *repo, const char *path, int show_changed_paths,
4464 int show_diffstat, int show_patch, const char *search_pattern,
4465 int diff_context, int limit, int log_branches, int reverse_display_order,
4466 struct got_reflist_object_id_map *refs_idmap, int one_line, int toposort,
4469 const struct got_error *err;
4470 struct got_commit_graph *graph;
4473 struct got_object_id_queue reversed_commits;
4474 struct got_object_qid *qid;
4475 struct got_commit_object *commit;
4476 struct got_pathlist_head changed_paths;
4478 STAILQ_INIT(&reversed_commits);
4479 TAILQ_INIT(&changed_paths);
4481 if (search_pattern && regcomp(®ex, search_pattern,
4482 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4483 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4485 err = got_commit_graph_open(&graph, path, !log_branches);
4488 if (log_branches && toposort) {
4489 err = got_commit_graph_toposort(graph, root_id, repo,
4490 check_cancelled, NULL);
4492 err = got_commit_graph_bfsort(graph, root_id, repo,
4493 check_cancelled, NULL);
4498 struct got_object_id id;
4499 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4500 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4502 if (sigint_received || sigpipe_received)
4505 err = got_commit_graph_iter_next(&id, graph, repo,
4506 check_cancelled, NULL);
4508 if (err->code == GOT_ERR_ITER_COMPLETED)
4513 err = got_object_open_as_commit(&commit, repo, &id);
4517 if (((show_changed_paths && !show_diffstat) ||
4518 (show_diffstat && !show_patch))
4519 && !reverse_display_order) {
4520 err = get_changed_paths(&changed_paths, commit, repo,
4521 show_diffstat ? &dsa : NULL);
4526 if (search_pattern) {
4527 err = match_commit(&have_match, &id, commit, ®ex);
4529 got_object_commit_close(commit);
4532 if (have_match == 0 && show_changed_paths)
4533 match_changed_paths(&have_match,
4534 &changed_paths, ®ex);
4535 if (have_match == 0 && show_patch) {
4536 err = match_patch(&have_match, commit, &id,
4537 path, diff_context, repo, ®ex, tmpfile);
4541 if (have_match == 0) {
4542 got_object_commit_close(commit);
4543 got_pathlist_free(&changed_paths,
4544 GOT_PATHLIST_FREE_ALL);
4549 if (reverse_display_order) {
4550 err = got_object_qid_alloc(&qid, &id);
4553 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4554 got_object_commit_close(commit);
4557 err = print_commit_oneline(commit, &id,
4560 err = print_commit(commit, &id, repo, path,
4561 (show_changed_paths || show_diffstat) ?
4562 &changed_paths : NULL,
4563 show_diffstat ? &dsa : NULL, show_patch,
4564 diff_context, refs_idmap, NULL, NULL);
4565 got_object_commit_close(commit);
4569 if ((limit && --limit == 0) ||
4570 (end_id && got_object_id_cmp(&id, end_id) == 0))
4573 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4575 if (reverse_display_order) {
4576 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4577 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4578 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4580 err = got_object_open_as_commit(&commit, repo,
4584 if ((show_changed_paths && !show_diffstat) ||
4585 (show_diffstat && !show_patch)) {
4586 err = get_changed_paths(&changed_paths, commit,
4587 repo, show_diffstat ? &dsa : NULL);
4592 err = print_commit_oneline(commit, &qid->id,
4595 err = print_commit(commit, &qid->id, repo, path,
4596 (show_changed_paths || show_diffstat) ?
4597 &changed_paths : NULL,
4598 show_diffstat ? &dsa : NULL, show_patch,
4599 diff_context, refs_idmap, NULL, NULL);
4600 got_object_commit_close(commit);
4603 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4607 while (!STAILQ_EMPTY(&reversed_commits)) {
4608 qid = STAILQ_FIRST(&reversed_commits);
4609 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4610 got_object_qid_free(qid);
4612 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4615 got_commit_graph_close(graph);
4622 fprintf(stderr, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4623 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4624 "[path]\n", getprogname());
4629 get_default_log_limit(void)
4631 const char *got_default_log_limit;
4635 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4636 if (got_default_log_limit == NULL)
4638 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4644 static const struct got_error *
4645 cmd_log(int argc, char *argv[])
4647 const struct got_error *error;
4648 struct got_repository *repo = NULL;
4649 struct got_worktree *worktree = NULL;
4650 struct got_object_id *start_id = NULL, *end_id = NULL;
4651 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4652 char *keyword_idstr = NULL;
4653 const char *start_commit = NULL, *end_commit = NULL;
4654 const char *search_pattern = NULL;
4655 int diff_context = -1, ch;
4656 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4657 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4660 struct got_reflist_head refs;
4661 struct got_reflist_object_id_map *refs_idmap = NULL;
4662 FILE *tmpfile = NULL;
4663 int *pack_fds = NULL;
4668 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4674 limit = get_default_log_limit();
4676 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:stx:")) != -1) {
4682 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4685 errx(1, "number of context lines is %s: %s",
4689 start_commit = optarg;
4695 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4697 errx(1, "number of commits is %s: %s",
4701 show_changed_paths = 1;
4707 reverse_display_order = 1;
4710 repo_path = realpath(optarg, NULL);
4711 if (repo_path == NULL)
4712 return got_error_from_errno2("realpath",
4714 got_path_strip_trailing_slashes(repo_path);
4717 search_pattern = optarg;
4726 end_commit = optarg;
4737 if (diff_context == -1)
4739 else if (!show_patch)
4740 errx(1, "-C requires -p");
4742 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4743 errx(1, "cannot use -s with -d, -p or -P");
4745 cwd = getcwd(NULL, 0);
4747 error = got_error_from_errno("getcwd");
4751 error = got_repo_pack_fds_open(&pack_fds);
4755 if (repo_path == NULL) {
4756 error = got_worktree_open(&worktree, cwd,
4757 GOT_WORKTREE_GOT_DIR);
4758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4765 error = got_worktree_resolve_path(&path, worktree,
4770 path = strdup(argv[0]);
4772 error = got_error_from_errno("strdup");
4776 } else if (argc != 0)
4779 if (repo_path == NULL) {
4780 repo_path = worktree ?
4781 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4783 if (repo_path == NULL) {
4784 error = got_error_from_errno("strdup");
4788 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4792 error = apply_unveil(got_repo_get_path(repo), 1,
4793 worktree ? got_worktree_get_root_path(worktree) : NULL);
4797 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4801 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4805 if (start_commit == NULL) {
4806 struct got_reference *head_ref;
4807 struct got_commit_object *commit = NULL;
4808 error = got_ref_open(&head_ref, repo,
4809 worktree ? got_worktree_get_head_ref_name(worktree)
4813 error = got_ref_resolve(&start_id, repo, head_ref);
4814 got_ref_close(head_ref);
4817 error = got_object_open_as_commit(&commit, repo,
4821 got_object_commit_close(commit);
4823 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4827 if (keyword_idstr != NULL)
4828 start_commit = keyword_idstr;
4830 error = got_repo_match_object_id(&start_id, NULL,
4831 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4835 if (end_commit != NULL) {
4836 error = got_keyword_to_idstr(&keyword_idstr, end_commit,
4840 if (keyword_idstr != NULL)
4841 end_commit = keyword_idstr;
4843 error = got_repo_match_object_id(&end_id, NULL,
4844 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4851 * If a path was specified on the command line it was resolved
4852 * to a path in the work tree above. Prepend the work tree's
4853 * path prefix to obtain the corresponding in-repository path.
4857 prefix = got_worktree_get_path_prefix(worktree);
4858 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4859 (path[0] != '\0') ? "/" : "", path) == -1) {
4860 error = got_error_from_errno("asprintf");
4865 error = got_repo_map_path(&in_repo_path, repo,
4871 path = in_repo_path;
4875 /* Release work tree lock. */
4876 got_worktree_close(worktree);
4880 if (search_pattern && show_patch) {
4881 tmpfile = got_opentemp();
4882 if (tmpfile == NULL) {
4883 error = got_error_from_errno("got_opentemp");
4888 error = print_commits(start_id, end_id, repo, path ? path : "",
4889 show_changed_paths, show_diffstat, show_patch, search_pattern,
4890 diff_context, limit, log_branches, reverse_display_order,
4891 refs_idmap, one_line, toposort, tmpfile);
4898 free(keyword_idstr);
4900 got_worktree_close(worktree);
4902 const struct got_error *close_err = got_repo_close(repo);
4907 const struct got_error *pack_err =
4908 got_repo_pack_fds_close(pack_fds);
4913 got_reflist_object_id_map_free(refs_idmap);
4914 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4915 error = got_error_from_errno("fclose");
4916 got_ref_list_free(&refs);
4923 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4924 "[-r repository-path] [object1 object2 | path ...]\n",
4929 struct print_diff_arg {
4930 struct got_repository *repo;
4931 struct got_worktree *worktree;
4932 struct got_diffstat_cb_arg *diffstat;
4937 enum got_diff_algorithm diff_algo;
4938 int ignore_whitespace;
4939 int force_text_diff;
4946 * Create a file which contains the target path of a symlink so we can feed
4947 * it as content to the diff engine.
4949 static const struct got_error *
4950 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4951 const char *abspath)
4953 const struct got_error *err = NULL;
4954 char target_path[PATH_MAX];
4955 ssize_t target_len, outlen;
4960 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4961 if (target_len == -1)
4962 return got_error_from_errno2("readlinkat", abspath);
4964 target_len = readlink(abspath, target_path, PATH_MAX);
4965 if (target_len == -1)
4966 return got_error_from_errno2("readlink", abspath);
4969 *fd = got_opentempfd();
4971 return got_error_from_errno("got_opentempfd");
4973 outlen = write(*fd, target_path, target_len);
4975 err = got_error_from_errno("got_opentempfd");
4979 if (lseek(*fd, 0, SEEK_SET) == -1) {
4980 err = got_error_from_errno2("lseek", abspath);
4991 static const struct got_error *
4992 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4993 const char *path, struct got_object_id *blob_id,
4994 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4995 int dirfd, const char *de_name)
4997 struct print_diff_arg *a = arg;
4998 const struct got_error *err = NULL;
4999 struct got_blob_object *blob1 = NULL;
5000 int fd = -1, fd1 = -1, fd2 = -1;
5002 char *abspath = NULL, *label1 = NULL;
5007 memset(&sb, 0, sizeof(sb));
5009 if (a->diff_staged) {
5010 if (staged_status != GOT_STATUS_MODIFY &&
5011 staged_status != GOT_STATUS_ADD &&
5012 staged_status != GOT_STATUS_DELETE)
5015 if (staged_status == GOT_STATUS_DELETE)
5017 if (status == GOT_STATUS_NONEXISTENT)
5018 return got_error_set_errno(ENOENT, path);
5019 if (status != GOT_STATUS_MODIFY &&
5020 status != GOT_STATUS_ADD &&
5021 status != GOT_STATUS_DELETE &&
5022 status != GOT_STATUS_CONFLICT)
5026 err = got_opentemp_truncate(a->f1);
5028 return got_error_from_errno("got_opentemp_truncate");
5029 err = got_opentemp_truncate(a->f2);
5031 return got_error_from_errno("got_opentemp_truncate");
5033 if (!a->header_shown) {
5034 if (fprintf(a->outfile, "diff %s%s\n",
5035 a->diff_staged ? "-s " : "",
5036 got_worktree_get_root_path(a->worktree)) < 0) {
5037 err = got_error_from_errno("fprintf");
5040 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
5041 err = got_error_from_errno("fprintf");
5044 if (fprintf(a->outfile, "path + %s%s\n",
5045 got_worktree_get_root_path(a->worktree),
5046 a->diff_staged ? " (staged changes)" : "") < 0) {
5047 err = got_error_from_errno("fprintf");
5050 a->header_shown = 1;
5053 if (a->diff_staged) {
5054 const char *label1 = NULL, *label2 = NULL;
5055 switch (staged_status) {
5056 case GOT_STATUS_MODIFY:
5060 case GOT_STATUS_ADD:
5063 case GOT_STATUS_DELETE:
5067 return got_error(GOT_ERR_FILE_STATUS);
5069 fd1 = got_opentempfd();
5071 err = got_error_from_errno("got_opentempfd");
5074 fd2 = got_opentempfd();
5076 err = got_error_from_errno("got_opentempfd");
5079 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5080 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5081 a->diff_algo, a->diff_context, a->ignore_whitespace,
5082 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5086 fd1 = got_opentempfd();
5088 err = got_error_from_errno("got_opentempfd");
5092 if (staged_status == GOT_STATUS_ADD ||
5093 staged_status == GOT_STATUS_MODIFY) {
5095 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5099 err = got_object_id_str(&id_str, staged_blob_id);
5102 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5103 err = got_error_from_errno("asprintf");
5108 } else if (status != GOT_STATUS_ADD) {
5109 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5115 if (status != GOT_STATUS_DELETE) {
5116 if (asprintf(&abspath, "%s/%s",
5117 got_worktree_get_root_path(a->worktree), path) == -1) {
5118 err = got_error_from_errno("asprintf");
5123 fd = openat(dirfd, de_name,
5124 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5126 if (!got_err_open_nofollow_on_symlink()) {
5127 err = got_error_from_errno2("openat",
5131 err = get_symlink_target_file(&fd, dirfd,
5137 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5139 if (!got_err_open_nofollow_on_symlink()) {
5140 err = got_error_from_errno2("open",
5144 err = get_symlink_target_file(&fd, dirfd,
5150 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5151 err = got_error_from_errno2("fstatat", abspath);
5154 f2 = fdopen(fd, "r");
5156 err = got_error_from_errno2("fdopen", abspath);
5164 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5170 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5171 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5172 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5174 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5175 err = got_error_from_errno("close");
5176 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5177 err = got_error_from_errno("close");
5179 got_object_blob_close(blob1);
5180 if (fd != -1 && close(fd) == -1 && err == NULL)
5181 err = got_error_from_errno("close");
5182 if (f2 && fclose(f2) == EOF && err == NULL)
5183 err = got_error_from_errno("fclose");
5188 static const struct got_error *
5189 cmd_diff(int argc, char *argv[])
5191 const struct got_error *error;
5192 struct got_repository *repo = NULL;
5193 struct got_worktree *worktree = NULL;
5194 char *cwd = NULL, *repo_path = NULL;
5195 const char *commit_args[2] = { NULL, NULL };
5196 int ncommit_args = 0;
5197 struct got_object_id *ids[2] = { NULL, NULL };
5198 char *labels[2] = { NULL, NULL };
5199 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5200 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5201 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5203 struct got_reflist_head refs;
5204 struct got_pathlist_head diffstat_paths, paths;
5205 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5206 int fd1 = -1, fd2 = -1;
5207 int *pack_fds = NULL;
5208 struct got_diffstat_cb_arg dsa;
5210 memset(&dsa, 0, sizeof(dsa));
5214 TAILQ_INIT(&diffstat_paths);
5217 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5222 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5225 force_text_diff = 1;
5228 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5231 errx(1, "number of context lines is %s: %s",
5235 if (ncommit_args >= 2)
5236 errx(1, "too many -c options used");
5237 commit_args[ncommit_args++] = optarg;
5246 repo_path = realpath(optarg, NULL);
5247 if (repo_path == NULL)
5248 return got_error_from_errno2("realpath",
5250 got_path_strip_trailing_slashes(repo_path);
5257 ignore_whitespace = 1;
5268 cwd = getcwd(NULL, 0);
5270 error = got_error_from_errno("getcwd");
5274 error = got_repo_pack_fds_open(&pack_fds);
5278 if (repo_path == NULL) {
5279 error = got_worktree_open(&worktree, cwd,
5280 GOT_WORKTREE_GOT_DIR);
5281 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5287 strdup(got_worktree_get_repo_path(worktree));
5288 if (repo_path == NULL) {
5289 error = got_error_from_errno("strdup");
5293 repo_path = strdup(cwd);
5294 if (repo_path == NULL) {
5295 error = got_error_from_errno("strdup");
5301 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5306 if (show_diffstat) {
5307 dsa.paths = &diffstat_paths;
5308 dsa.force_text = force_text_diff;
5309 dsa.ignore_ws = ignore_whitespace;
5310 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5313 if (rflag || worktree == NULL || ncommit_args > 0) {
5315 error = got_error_msg(GOT_ERR_NOT_IMPL,
5316 "-P option can only be used when diffing "
5321 error = got_error_msg(GOT_ERR_NOT_IMPL,
5322 "-s option can only be used when diffing "
5328 error = apply_unveil(got_repo_get_path(repo), 1,
5329 worktree ? got_worktree_get_root_path(worktree) : NULL);
5333 if ((!force_path && argc == 2) || ncommit_args > 0) {
5334 int obj_type = (ncommit_args > 0 ?
5335 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5336 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5340 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5342 char *keyword_idstr = NULL;
5344 if (ncommit_args > 0)
5345 arg = commit_args[i];
5349 error = got_keyword_to_idstr(&keyword_idstr, arg,
5353 if (keyword_idstr != NULL)
5354 arg = keyword_idstr;
5356 error = got_repo_match_object_id(&ids[i], &labels[i],
5357 arg, obj_type, &refs, repo);
5358 free(keyword_idstr);
5360 if (error->code != GOT_ERR_NOT_REF &&
5361 error->code != GOT_ERR_NO_OBJ)
5363 if (ncommit_args > 0)
5371 f1 = got_opentemp();
5373 error = got_error_from_errno("got_opentemp");
5377 f2 = got_opentemp();
5379 error = got_error_from_errno("got_opentemp");
5383 outfile = got_opentemp();
5384 if (outfile == NULL) {
5385 error = got_error_from_errno("got_opentemp");
5389 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5390 struct print_diff_arg arg;
5393 if (worktree == NULL) {
5394 if (argc == 2 && ids[0] == NULL) {
5395 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5397 } else if (argc == 2 && ids[1] == NULL) {
5398 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5400 } else if (argc > 0) {
5401 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5402 "%s", "specified paths cannot be resolved");
5405 error = got_error(GOT_ERR_NOT_WORKTREE);
5410 error = get_worktree_paths_from_argv(&paths, argc, argv,
5415 error = got_object_id_str(&id_str,
5416 got_worktree_get_base_commit_id(worktree));
5420 arg.worktree = worktree;
5421 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5422 arg.diff_context = diff_context;
5423 arg.id_str = id_str;
5424 arg.header_shown = 0;
5425 arg.diff_staged = diff_staged;
5426 arg.ignore_whitespace = ignore_whitespace;
5427 arg.force_text_diff = force_text_diff;
5428 arg.diffstat = show_diffstat ? &dsa : NULL;
5431 arg.outfile = outfile;
5433 error = got_worktree_status(worktree, &paths, repo, 0,
5434 print_diff, &arg, check_cancelled, NULL);
5439 if (show_diffstat && dsa.nfiles > 0) {
5442 if (asprintf(&header, "diffstat %s%s",
5443 diff_staged ? "-s " : "",
5444 got_worktree_get_root_path(worktree)) == -1) {
5445 error = got_error_from_errno("asprintf");
5449 error = print_diffstat(&dsa, header);
5455 error = printfile(outfile);
5459 if (ncommit_args == 1) {
5460 struct got_commit_object *commit;
5461 error = got_object_open_as_commit(&commit, repo, ids[0]);
5465 labels[1] = labels[0];
5467 if (got_object_commit_get_nparents(commit) > 0) {
5468 const struct got_object_id_queue *pids;
5469 struct got_object_qid *pid;
5470 pids = got_object_commit_get_parent_ids(commit);
5471 pid = STAILQ_FIRST(pids);
5472 ids[0] = got_object_id_dup(&pid->id);
5473 if (ids[0] == NULL) {
5474 error = got_error_from_errno(
5475 "got_object_id_dup");
5476 got_object_commit_close(commit);
5479 error = got_object_id_str(&labels[0], ids[0]);
5481 got_object_commit_close(commit);
5486 labels[0] = strdup("/dev/null");
5487 if (labels[0] == NULL) {
5488 error = got_error_from_errno("strdup");
5489 got_object_commit_close(commit);
5494 got_object_commit_close(commit);
5497 if (ncommit_args == 0 && argc > 2) {
5498 error = got_error_msg(GOT_ERR_BAD_PATH,
5499 "path arguments cannot be used when diffing two objects");
5504 error = got_object_get_type(&type1, repo, ids[0]);
5509 error = got_object_get_type(&type2, repo, ids[1]);
5512 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5513 error = got_error(GOT_ERR_OBJ_TYPE);
5516 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5517 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5518 "path arguments cannot be used when diffing blobs");
5522 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5524 struct got_pathlist_entry *new;
5528 error = got_worktree_resolve_path(&p, worktree,
5532 prefix = got_worktree_get_path_prefix(worktree);
5533 while (prefix[0] == '/')
5535 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5536 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5538 error = got_error_from_errno("asprintf");
5544 char *mapped_path, *s;
5545 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5551 in_repo_path = strdup(s);
5552 if (in_repo_path == NULL) {
5553 error = got_error_from_errno("asprintf");
5560 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5561 if (error || new == NULL /* duplicate */)
5568 /* Release work tree lock. */
5569 got_worktree_close(worktree);
5573 fd1 = got_opentempfd();
5575 error = got_error_from_errno("got_opentempfd");
5579 fd2 = got_opentempfd();
5581 error = got_error_from_errno("got_opentempfd");
5585 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5586 case GOT_OBJ_TYPE_BLOB:
5587 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5588 fd1, fd2, ids[0], ids[1], NULL, NULL,
5589 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5590 ignore_whitespace, force_text_diff,
5591 show_diffstat ? &dsa : NULL, repo, outfile);
5593 case GOT_OBJ_TYPE_TREE:
5594 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5595 ids[0], ids[1], &paths, "", "",
5596 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5597 ignore_whitespace, force_text_diff,
5598 show_diffstat ? &dsa : NULL, repo, outfile);
5600 case GOT_OBJ_TYPE_COMMIT:
5601 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5602 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5603 fd1, fd2, ids[0], ids[1], &paths,
5604 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5605 ignore_whitespace, force_text_diff,
5606 show_diffstat ? &dsa : NULL, repo, outfile);
5609 error = got_error(GOT_ERR_OBJ_TYPE);
5614 if (show_diffstat && dsa.nfiles > 0) {
5615 char *header = NULL;
5617 if (asprintf(&header, "diffstat %s %s",
5618 labels[0], labels[1]) == -1) {
5619 error = got_error_from_errno("asprintf");
5623 error = print_diffstat(&dsa, header);
5629 error = printfile(outfile);
5637 got_worktree_close(worktree);
5639 const struct got_error *close_err = got_repo_close(repo);
5644 const struct got_error *pack_err =
5645 got_repo_pack_fds_close(pack_fds);
5649 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5650 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5651 got_ref_list_free(&refs);
5652 if (outfile && fclose(outfile) == EOF && error == NULL)
5653 error = got_error_from_errno("fclose");
5654 if (f1 && fclose(f1) == EOF && error == NULL)
5655 error = got_error_from_errno("fclose");
5656 if (f2 && fclose(f2) == EOF && error == NULL)
5657 error = got_error_from_errno("fclose");
5658 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5659 error = got_error_from_errno("close");
5660 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5661 error = got_error_from_errno("close");
5669 "usage: %s blame [-c commit] [-r repository-path] path\n",
5678 char datebuf[11]; /* YYYY-MM-DD + NUL */
5681 struct blame_cb_args {
5682 struct blame_line *lines;
5686 off_t *line_offsets;
5688 struct got_repository *repo;
5691 static const struct got_error *
5692 blame_cb(void *arg, int nlines, int lineno,
5693 struct got_commit_object *commit, struct got_object_id *id)
5695 const struct got_error *err = NULL;
5696 struct blame_cb_args *a = arg;
5697 struct blame_line *bline;
5699 size_t linesize = 0;
5702 time_t committer_time;
5704 if (nlines != a->nlines ||
5705 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5706 return got_error(GOT_ERR_RANGE);
5708 if (sigint_received)
5709 return got_error(GOT_ERR_ITER_COMPLETED);
5712 return NULL; /* no change in this commit */
5714 /* Annotate this line. */
5715 bline = &a->lines[lineno - 1];
5716 if (bline->annotated)
5718 err = got_object_id_str(&bline->id_str, id);
5722 bline->committer = strdup(got_object_commit_get_committer(commit));
5723 if (bline->committer == NULL) {
5724 err = got_error_from_errno("strdup");
5728 committer_time = got_object_commit_get_committer_time(commit);
5729 if (gmtime_r(&committer_time, &tm) == NULL)
5730 return got_error_from_errno("gmtime_r");
5731 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5733 err = got_error(GOT_ERR_NO_SPACE);
5736 bline->annotated = 1;
5738 /* Print lines annotated so far. */
5739 bline = &a->lines[a->lineno_cur - 1];
5740 if (!bline->annotated)
5743 offset = a->line_offsets[a->lineno_cur - 1];
5744 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5745 err = got_error_from_errno("fseeko");
5749 while (a->lineno_cur <= a->nlines && bline->annotated) {
5750 char *smallerthan, *at, *nl, *committer;
5753 if (getline(&line, &linesize, a->f) == -1) {
5755 err = got_error_from_errno("getline");
5759 committer = bline->committer;
5760 smallerthan = strchr(committer, '<');
5761 if (smallerthan && smallerthan[1] != '\0')
5762 committer = smallerthan + 1;
5763 at = strchr(committer, '@');
5766 len = strlen(committer);
5768 committer[8] = '\0';
5770 nl = strchr(line, '\n');
5773 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5774 bline->id_str, bline->datebuf, committer, line);
5777 bline = &a->lines[a->lineno_cur - 1];
5784 static const struct got_error *
5785 cmd_blame(int argc, char *argv[])
5787 const struct got_error *error;
5788 struct got_repository *repo = NULL;
5789 struct got_worktree *worktree = NULL;
5790 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5791 char *link_target = NULL;
5792 struct got_object_id *obj_id = NULL;
5793 struct got_object_id *commit_id = NULL;
5794 struct got_commit_object *commit = NULL;
5795 struct got_blob_object *blob = NULL;
5796 char *commit_id_str = NULL, *keyword_idstr = NULL;
5797 struct blame_cb_args bca;
5798 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5800 int *pack_fds = NULL;
5801 FILE *f1 = NULL, *f2 = NULL;
5803 fd1 = got_opentempfd();
5805 return got_error_from_errno("got_opentempfd");
5807 memset(&bca, 0, sizeof(bca));
5810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5815 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5818 commit_id_str = optarg;
5821 repo_path = realpath(optarg, NULL);
5822 if (repo_path == NULL)
5823 return got_error_from_errno2("realpath",
5825 got_path_strip_trailing_slashes(repo_path);
5841 cwd = getcwd(NULL, 0);
5843 error = got_error_from_errno("getcwd");
5847 error = got_repo_pack_fds_open(&pack_fds);
5851 if (repo_path == NULL) {
5852 error = got_worktree_open(&worktree, cwd,
5853 GOT_WORKTREE_GOT_DIR);
5854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5860 strdup(got_worktree_get_repo_path(worktree));
5861 if (repo_path == NULL) {
5862 error = got_error_from_errno("strdup");
5867 repo_path = strdup(cwd);
5868 if (repo_path == NULL) {
5869 error = got_error_from_errno("strdup");
5875 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5880 const char *prefix = got_worktree_get_path_prefix(worktree);
5883 error = got_worktree_resolve_path(&p, worktree, path);
5886 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5887 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5889 error = got_error_from_errno("asprintf");
5894 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5896 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5899 error = got_repo_map_path(&in_repo_path, repo, path);
5904 if (commit_id_str == NULL) {
5905 struct got_reference *head_ref;
5906 error = got_ref_open(&head_ref, repo, worktree ?
5907 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5910 error = got_ref_resolve(&commit_id, repo, head_ref);
5911 got_ref_close(head_ref);
5915 struct got_reflist_head refs;
5918 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5923 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5927 if (keyword_idstr != NULL)
5928 commit_id_str = keyword_idstr;
5930 error = got_repo_match_object_id(&commit_id, NULL,
5931 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5932 got_ref_list_free(&refs);
5938 /* Release work tree lock. */
5939 got_worktree_close(worktree);
5943 error = got_object_open_as_commit(&commit, repo, commit_id);
5947 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5952 error = got_object_id_by_path(&obj_id, repo, commit,
5953 link_target ? link_target : in_repo_path);
5957 error = got_object_get_type(&obj_type, repo, obj_id);
5961 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5962 error = got_error_path(link_target ? link_target : in_repo_path,
5967 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5970 bca.f = got_opentemp();
5971 if (bca.f == NULL) {
5972 error = got_error_from_errno("got_opentemp");
5975 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5976 &bca.line_offsets, bca.f, blob);
5977 if (error || bca.nlines == 0)
5980 /* Don't include \n at EOF in the blame line count. */
5981 if (bca.line_offsets[bca.nlines - 1] == filesize)
5984 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5985 if (bca.lines == NULL) {
5986 error = got_error_from_errno("calloc");
5990 bca.nlines_prec = 0;
5998 fd2 = got_opentempfd();
6000 error = got_error_from_errno("got_opentempfd");
6003 fd3 = got_opentempfd();
6005 error = got_error_from_errno("got_opentempfd");
6008 f1 = got_opentemp();
6010 error = got_error_from_errno("got_opentemp");
6013 f2 = got_opentemp();
6015 error = got_error_from_errno("got_opentemp");
6018 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
6019 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
6020 check_cancelled, NULL, fd2, fd3, f1, f2);
6022 free(keyword_idstr);
6030 got_object_commit_close(commit);
6032 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
6033 error = got_error_from_errno("close");
6034 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
6035 error = got_error_from_errno("close");
6036 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
6037 error = got_error_from_errno("close");
6038 if (f1 && fclose(f1) == EOF && error == NULL)
6039 error = got_error_from_errno("fclose");
6040 if (f2 && fclose(f2) == EOF && error == NULL)
6041 error = got_error_from_errno("fclose");
6044 got_object_blob_close(blob);
6046 got_worktree_close(worktree);
6048 const struct got_error *close_err = got_repo_close(repo);
6053 const struct got_error *pack_err =
6054 got_repo_pack_fds_close(pack_fds);
6059 for (i = 0; i < bca.nlines; i++) {
6060 struct blame_line *bline = &bca.lines[i];
6061 free(bline->id_str);
6062 free(bline->committer);
6066 free(bca.line_offsets);
6067 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6068 error = got_error_from_errno("fclose");
6075 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6076 "[path]\n", getprogname());
6080 static const struct got_error *
6081 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6082 const char *root_path, struct got_repository *repo)
6084 const struct got_error *err = NULL;
6085 int is_root_path = (strcmp(path, root_path) == 0);
6086 const char *modestr = "";
6087 mode_t mode = got_tree_entry_get_mode(te);
6088 char *link_target = NULL;
6090 path += strlen(root_path);
6091 while (path[0] == '/')
6094 if (got_object_tree_entry_is_submodule(te))
6096 else if (S_ISLNK(mode)) {
6099 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6102 for (i = 0; link_target[i] != '\0'; i++) {
6103 if (!isprint((unsigned char)link_target[i]))
6104 link_target[i] = '?';
6109 else if (S_ISDIR(mode))
6111 else if (mode & S_IXUSR)
6114 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6115 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6116 link_target ? " -> ": "", link_target ? link_target : "");
6122 static const struct got_error *
6123 print_tree(const char *path, struct got_commit_object *commit,
6124 int show_ids, int recurse, const char *root_path,
6125 struct got_repository *repo)
6127 const struct got_error *err = NULL;
6128 struct got_object_id *tree_id = NULL;
6129 struct got_tree_object *tree = NULL;
6132 err = got_object_id_by_path(&tree_id, repo, commit, path);
6136 err = got_object_open_as_tree(&tree, repo, tree_id);
6139 nentries = got_object_tree_get_nentries(tree);
6140 for (i = 0; i < nentries; i++) {
6141 struct got_tree_entry *te;
6144 if (sigint_received || sigpipe_received)
6147 te = got_object_tree_get_entry(tree, i);
6150 err = got_object_id_str(&id_str,
6151 got_tree_entry_get_id(te));
6154 if (asprintf(&id, "%s ", id_str) == -1) {
6155 err = got_error_from_errno("asprintf");
6161 err = print_entry(te, id, path, root_path, repo);
6166 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6168 if (asprintf(&child_path, "%s%s%s", path,
6169 path[0] == '/' && path[1] == '\0' ? "" : "/",
6170 got_tree_entry_get_name(te)) == -1) {
6171 err = got_error_from_errno("asprintf");
6174 err = print_tree(child_path, commit, show_ids, 1,
6183 got_object_tree_close(tree);
6188 static const struct got_error *
6189 cmd_tree(int argc, char *argv[])
6191 const struct got_error *error;
6192 struct got_repository *repo = NULL;
6193 struct got_worktree *worktree = NULL;
6194 const char *path, *refname = NULL;
6195 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6196 struct got_object_id *commit_id = NULL;
6197 struct got_commit_object *commit = NULL;
6198 char *commit_id_str = NULL, *keyword_idstr = NULL;
6199 int show_ids = 0, recurse = 0;
6201 int *pack_fds = NULL;
6204 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6209 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6212 commit_id_str = optarg;
6221 repo_path = realpath(optarg, NULL);
6222 if (repo_path == NULL)
6223 return got_error_from_errno2("realpath",
6225 got_path_strip_trailing_slashes(repo_path);
6243 cwd = getcwd(NULL, 0);
6245 error = got_error_from_errno("getcwd");
6249 error = got_repo_pack_fds_open(&pack_fds);
6253 if (repo_path == NULL) {
6254 error = got_worktree_open(&worktree, cwd,
6255 GOT_WORKTREE_GOT_DIR);
6256 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6262 strdup(got_worktree_get_repo_path(worktree));
6263 if (repo_path == NULL)
6264 error = got_error_from_errno("strdup");
6268 repo_path = strdup(cwd);
6269 if (repo_path == NULL) {
6270 error = got_error_from_errno("strdup");
6276 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6281 const char *prefix = got_worktree_get_path_prefix(worktree);
6284 if (path == NULL || got_path_is_root_dir(path))
6286 error = got_worktree_resolve_path(&p, worktree, path);
6289 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6290 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6292 error = got_error_from_errno("asprintf");
6297 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6301 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6306 error = got_repo_map_path(&in_repo_path, repo, path);
6311 if (commit_id_str == NULL) {
6312 struct got_reference *head_ref;
6314 refname = got_worktree_get_head_ref_name(worktree);
6316 refname = GOT_REF_HEAD;
6317 error = got_ref_open(&head_ref, repo, refname, 0);
6320 error = got_ref_resolve(&commit_id, repo, head_ref);
6321 got_ref_close(head_ref);
6325 struct got_reflist_head refs;
6328 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6333 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6337 if (keyword_idstr != NULL)
6338 commit_id_str = keyword_idstr;
6340 error = got_repo_match_object_id(&commit_id, NULL,
6341 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6342 got_ref_list_free(&refs);
6348 /* Release work tree lock. */
6349 got_worktree_close(worktree);
6353 error = got_object_open_as_commit(&commit, repo, commit_id);
6357 error = print_tree(in_repo_path, commit, show_ids, recurse,
6358 in_repo_path, repo);
6360 free(keyword_idstr);
6366 got_object_commit_close(commit);
6368 got_worktree_close(worktree);
6370 const struct got_error *close_err = got_repo_close(repo);
6375 const struct got_error *pack_err =
6376 got_repo_pack_fds_close(pack_fds);
6386 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6387 "[-s status-codes] [path ...]\n", getprogname());
6391 struct got_status_arg {
6396 static const struct got_error *
6397 print_status(void *arg, unsigned char status, unsigned char staged_status,
6398 const char *path, struct got_object_id *blob_id,
6399 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6400 int dirfd, const char *de_name)
6402 struct got_status_arg *st = arg;
6404 if (status == staged_status && (status == GOT_STATUS_DELETE))
6405 status = GOT_STATUS_NO_CHANGE;
6406 if (st != NULL && st->status_codes) {
6407 size_t ncodes = strlen(st->status_codes);
6410 for (i = 0; i < ncodes ; i++) {
6412 if (status == st->status_codes[i] ||
6413 staged_status == st->status_codes[i]) {
6418 if (status == st->status_codes[i] ||
6419 staged_status == st->status_codes[i])
6424 if (st->suppress && j == 0)
6431 printf("%c%c %s\n", status, staged_status, path);
6435 static const struct got_error *
6436 show_operation_in_progress(struct got_worktree *worktree,
6437 struct got_repository *repo)
6439 const struct got_error *err;
6440 char *new_base_branch_name = NULL;
6441 char *branch_name = NULL;
6442 int rebase_in_progress, histedit_in_progress, merge_in_progress;
6444 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6447 if (rebase_in_progress) {
6448 err = got_worktree_rebase_info(&new_base_branch_name,
6449 &branch_name, worktree, repo);
6452 printf("Work tree is rebasing %s onto %s\n",
6453 branch_name, new_base_branch_name);
6456 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6460 if (histedit_in_progress) {
6461 err = got_worktree_histedit_info(&branch_name, worktree, repo);
6464 printf("Work tree is editing the history of %s\n", branch_name);
6467 err = got_worktree_merge_in_progress(&merge_in_progress,
6471 if (merge_in_progress) {
6472 err = got_worktree_merge_info(&branch_name, worktree,
6476 printf("Work tree is merging %s into %s\n", branch_name,
6477 got_worktree_get_head_ref_name(worktree));
6480 free(new_base_branch_name);
6485 static const struct got_error *
6486 cmd_status(int argc, char *argv[])
6488 const struct got_error *close_err, *error = NULL;
6489 struct got_repository *repo = NULL;
6490 struct got_worktree *worktree = NULL;
6491 struct got_status_arg st;
6493 struct got_pathlist_head paths;
6494 int ch, i, no_ignores = 0;
6495 int *pack_fds = NULL;
6499 memset(&st, 0, sizeof(st));
6500 st.status_codes = NULL;
6504 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6509 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6515 if (st.status_codes != NULL && st.suppress == 0)
6516 option_conflict('S', 's');
6520 for (i = 0; optarg[i] != '\0'; i++) {
6521 switch (optarg[i]) {
6522 case GOT_STATUS_MODIFY:
6523 case GOT_STATUS_ADD:
6524 case GOT_STATUS_DELETE:
6525 case GOT_STATUS_CONFLICT:
6526 case GOT_STATUS_MISSING:
6527 case GOT_STATUS_OBSTRUCTED:
6528 case GOT_STATUS_UNVERSIONED:
6529 case GOT_STATUS_MODE_CHANGE:
6530 case GOT_STATUS_NONEXISTENT:
6533 errx(1, "invalid status code '%c'",
6537 if (ch == 's' && st.suppress)
6538 option_conflict('s', 'S');
6539 st.status_codes = optarg;
6550 cwd = getcwd(NULL, 0);
6552 error = got_error_from_errno("getcwd");
6556 error = got_repo_pack_fds_open(&pack_fds);
6560 error = got_worktree_open(&worktree, cwd,
6561 GOT_WORKTREE_GOT_DIR);
6563 if (error->code == GOT_ERR_NOT_WORKTREE)
6564 error = wrap_not_worktree_error(error, "status", cwd);
6568 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6573 error = apply_unveil(got_repo_get_path(repo), 1,
6574 got_worktree_get_root_path(worktree));
6578 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6582 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6583 print_status, &st, check_cancelled, NULL);
6587 error = show_operation_in_progress(worktree, repo);
6590 const struct got_error *pack_err =
6591 got_repo_pack_fds_close(pack_fds);
6596 close_err = got_repo_close(repo);
6600 if (worktree != NULL) {
6601 close_err = got_worktree_close(worktree);
6606 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6614 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6615 "[-s reference] [name]\n", getprogname());
6619 static const struct got_error *
6620 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6622 static const struct got_error *err = NULL;
6623 struct got_reflist_head refs;
6624 struct got_reflist_entry *re;
6627 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6628 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6633 TAILQ_FOREACH(re, &refs, entry) {
6635 refstr = got_ref_to_str(re->ref);
6636 if (refstr == NULL) {
6637 err = got_error_from_errno("got_ref_to_str");
6640 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6644 got_ref_list_free(&refs);
6648 static const struct got_error *
6649 delete_ref_by_name(struct got_repository *repo, const char *refname)
6651 const struct got_error *err;
6652 struct got_reference *ref;
6654 err = got_ref_open(&ref, repo, refname, 0);
6658 err = delete_ref(repo, ref);
6663 static const struct got_error *
6664 add_ref(struct got_repository *repo, const char *refname, const char *target)
6666 const struct got_error *err = NULL;
6667 struct got_object_id *id = NULL;
6668 struct got_reference *ref = NULL;
6669 struct got_reflist_head refs;
6672 * Don't let the user create a reference name with a leading '-'.
6673 * While technically a valid reference name, this case is usually
6674 * an unintended typo.
6676 if (refname[0] == '-')
6677 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6680 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6683 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6685 got_ref_list_free(&refs);
6689 err = got_ref_alloc(&ref, refname, id);
6693 err = got_ref_write(ref, repo);
6701 static const struct got_error *
6702 add_symref(struct got_repository *repo, const char *refname, const char *target)
6704 const struct got_error *err = NULL;
6705 struct got_reference *ref = NULL;
6706 struct got_reference *target_ref = NULL;
6709 * Don't let the user create a reference name with a leading '-'.
6710 * While technically a valid reference name, this case is usually
6711 * an unintended typo.
6713 if (refname[0] == '-')
6714 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6716 err = got_ref_open(&target_ref, repo, target, 0);
6720 err = got_ref_alloc_symref(&ref, refname, target_ref);
6724 err = got_ref_write(ref, repo);
6727 got_ref_close(target_ref);
6733 static const struct got_error *
6734 cmd_ref(int argc, char *argv[])
6736 const struct got_error *error = NULL;
6737 struct got_repository *repo = NULL;
6738 struct got_worktree *worktree = NULL;
6739 char *cwd = NULL, *repo_path = NULL;
6740 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6741 const char *obj_arg = NULL, *symref_target= NULL;
6742 char *refname = NULL, *keyword_idstr = NULL;
6743 int *pack_fds = NULL;
6746 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6747 "sendfd unveil", NULL) == -1)
6751 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6763 repo_path = realpath(optarg, NULL);
6764 if (repo_path == NULL)
6765 return got_error_from_errno2("realpath",
6767 got_path_strip_trailing_slashes(repo_path);
6770 symref_target = optarg;
6781 if (obj_arg && do_list)
6782 option_conflict('c', 'l');
6783 if (obj_arg && do_delete)
6784 option_conflict('c', 'd');
6785 if (obj_arg && symref_target)
6786 option_conflict('c', 's');
6787 if (symref_target && do_delete)
6788 option_conflict('s', 'd');
6789 if (symref_target && do_list)
6790 option_conflict('s', 'l');
6791 if (do_delete && do_list)
6792 option_conflict('d', 'l');
6793 if (sort_by_time && !do_list)
6794 errx(1, "-t option requires -l option");
6800 if (argc != 0 && argc != 1)
6803 refname = strdup(argv[0]);
6804 if (refname == NULL) {
6805 error = got_error_from_errno("strdup");
6812 refname = strdup(argv[0]);
6813 if (refname == NULL) {
6814 error = got_error_from_errno("strdup");
6820 got_path_strip_trailing_slashes(refname);
6822 cwd = getcwd(NULL, 0);
6824 error = got_error_from_errno("getcwd");
6828 error = got_repo_pack_fds_open(&pack_fds);
6832 if (repo_path == NULL) {
6833 error = got_worktree_open(&worktree, cwd,
6834 GOT_WORKTREE_GOT_DIR);
6835 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6841 strdup(got_worktree_get_repo_path(worktree));
6842 if (repo_path == NULL)
6843 error = got_error_from_errno("strdup");
6847 repo_path = strdup(cwd);
6848 if (repo_path == NULL) {
6849 error = got_error_from_errno("strdup");
6855 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6861 /* Remove "cpath" promise. */
6862 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6868 error = apply_unveil(got_repo_get_path(repo), do_list,
6869 worktree ? got_worktree_get_root_path(worktree) : NULL);
6874 error = list_refs(repo, refname, sort_by_time);
6876 error = delete_ref_by_name(repo, refname);
6877 else if (symref_target)
6878 error = add_symref(repo, refname, symref_target);
6880 if (obj_arg == NULL)
6883 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6887 if (keyword_idstr != NULL)
6888 obj_arg = keyword_idstr;
6890 error = add_ref(repo, refname, obj_arg);
6895 const struct got_error *close_err = got_repo_close(repo);
6900 got_worktree_close(worktree);
6902 const struct got_error *pack_err =
6903 got_repo_pack_fds_close(pack_fds);
6909 free(keyword_idstr);
6916 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6917 "[-r repository-path] [name]\n", getprogname());
6921 static const struct got_error *
6922 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6923 struct got_reference *ref)
6925 const struct got_error *err = NULL;
6926 const char *refname;
6930 refname = got_ref_get_name(ref);
6931 if (worktree && strcmp(refname,
6932 got_worktree_get_head_ref_name(worktree)) == 0) {
6933 err = got_worktree_get_state(&marker, repo, worktree,
6934 check_cancelled, NULL);
6939 if (strncmp(refname, "refs/heads/", 11) == 0)
6941 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6943 if (strncmp(refname, "refs/remotes/", 13) == 0)
6946 refstr = got_ref_to_str(ref);
6948 return got_error_from_errno("got_ref_to_str");
6950 printf("%c %s: %s\n", marker, refname, refstr);
6955 static const struct got_error *
6956 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6958 const char *refname;
6960 if (worktree == NULL)
6961 return got_error(GOT_ERR_NOT_WORKTREE);
6963 refname = got_worktree_get_head_ref_name(worktree);
6965 if (strncmp(refname, "refs/heads/", 11) == 0)
6967 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6970 printf("%s\n", refname);
6975 static const struct got_error *
6976 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6979 static const struct got_error *err = NULL;
6980 struct got_reflist_head refs;
6981 struct got_reflist_entry *re;
6982 struct got_reference *temp_ref = NULL;
6983 int rebase_in_progress, histedit_in_progress;
6988 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6993 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6998 if (rebase_in_progress || histedit_in_progress) {
6999 err = got_ref_open(&temp_ref, repo,
7000 got_worktree_get_head_ref_name(worktree), 0);
7003 list_branch(repo, worktree, temp_ref);
7004 got_ref_close(temp_ref);
7008 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
7009 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7014 TAILQ_FOREACH(re, &refs, entry)
7015 list_branch(repo, worktree, re->ref);
7017 got_ref_list_free(&refs);
7019 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
7020 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7025 TAILQ_FOREACH(re, &refs, entry)
7026 list_branch(repo, worktree, re->ref);
7028 got_ref_list_free(&refs);
7033 static const struct got_error *
7034 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
7035 const char *branch_name)
7037 const struct got_error *err = NULL;
7038 struct got_reference *ref = NULL;
7039 char *refname, *remote_refname = NULL;
7041 if (strncmp(branch_name, "refs/", 5) == 0)
7043 if (strncmp(branch_name, "heads/", 6) == 0)
7045 else if (strncmp(branch_name, "remotes/", 8) == 0)
7048 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
7049 return got_error_from_errno("asprintf");
7051 if (asprintf(&remote_refname, "refs/remotes/%s",
7052 branch_name) == -1) {
7053 err = got_error_from_errno("asprintf");
7057 err = got_ref_open(&ref, repo, refname, 0);
7059 const struct got_error *err2;
7060 if (err->code != GOT_ERR_NOT_REF)
7063 * Keep 'err' intact such that if neither branch exists
7064 * we report "refs/heads" rather than "refs/remotes" in
7065 * our error message.
7067 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7074 strcmp(got_worktree_get_head_ref_name(worktree),
7075 got_ref_get_name(ref)) == 0) {
7076 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7077 "will not delete this work tree's current branch");
7081 err = delete_ref(repo, ref);
7086 free(remote_refname);
7090 static const struct got_error *
7091 add_branch(struct got_repository *repo, const char *branch_name,
7092 struct got_object_id *base_commit_id)
7094 const struct got_error *err = NULL;
7095 struct got_reference *ref = NULL;
7096 char *refname = NULL;
7099 * Don't let the user create a branch name with a leading '-'.
7100 * While technically a valid reference name, this case is usually
7101 * an unintended typo.
7103 if (branch_name[0] == '-')
7104 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7106 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7109 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7110 err = got_error_from_errno("asprintf");
7114 err = got_ref_open(&ref, repo, refname, 0);
7116 err = got_error(GOT_ERR_BRANCH_EXISTS);
7118 } else if (err->code != GOT_ERR_NOT_REF)
7121 err = got_ref_alloc(&ref, refname, base_commit_id);
7125 err = got_ref_write(ref, repo);
7133 static const struct got_error *
7134 cmd_branch(int argc, char *argv[])
7136 const struct got_error *error = NULL;
7137 struct got_repository *repo = NULL;
7138 struct got_worktree *worktree = NULL;
7139 char *cwd = NULL, *repo_path = NULL;
7140 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7141 const char *delref = NULL, *commit_id_arg = NULL;
7142 struct got_reference *ref = NULL;
7143 struct got_pathlist_head paths;
7144 struct got_object_id *commit_id = NULL;
7145 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7146 int *pack_fds = NULL;
7151 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7152 "sendfd unveil", NULL) == -1)
7156 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7159 commit_id_arg = optarg;
7171 repo_path = realpath(optarg, NULL);
7172 if (repo_path == NULL)
7173 return got_error_from_errno2("realpath",
7175 got_path_strip_trailing_slashes(repo_path);
7186 if (do_list && delref)
7187 option_conflict('l', 'd');
7188 if (sort_by_time && !do_list)
7189 errx(1, "-t option requires -l option");
7194 if (!do_list && !delref && argc == 0)
7197 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7198 errx(1, "-c option can only be used when creating a branch");
7200 if (do_list || delref) {
7203 } else if (!do_show && argc != 1)
7206 cwd = getcwd(NULL, 0);
7208 error = got_error_from_errno("getcwd");
7212 error = got_repo_pack_fds_open(&pack_fds);
7216 if (repo_path == NULL) {
7217 error = got_worktree_open(&worktree, cwd,
7218 GOT_WORKTREE_GOT_DIR);
7219 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7225 strdup(got_worktree_get_repo_path(worktree));
7226 if (repo_path == NULL)
7227 error = got_error_from_errno("strdup");
7231 repo_path = strdup(cwd);
7232 if (repo_path == NULL) {
7233 error = got_error_from_errno("strdup");
7239 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7244 if (do_list || do_show) {
7245 /* Remove "cpath" promise. */
7246 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7252 error = apply_unveil(got_repo_get_path(repo), do_list,
7253 worktree ? got_worktree_get_root_path(worktree) : NULL);
7258 error = show_current_branch(repo, worktree);
7260 error = list_branches(repo, worktree, sort_by_time);
7262 error = delete_branch(repo, worktree, delref);
7264 struct got_reflist_head refs;
7266 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7270 if (commit_id_arg == NULL)
7271 commit_id_arg = worktree ?
7272 got_worktree_get_head_ref_name(worktree) :
7275 error = got_keyword_to_idstr(&keyword_idstr,
7276 commit_id_arg, repo, worktree);
7279 if (keyword_idstr != NULL)
7280 commit_id_arg = keyword_idstr;
7282 error = got_repo_match_object_id(&commit_id, NULL,
7283 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7284 got_ref_list_free(&refs);
7287 error = add_branch(repo, argv[0], commit_id);
7290 if (worktree && do_update) {
7291 struct got_update_progress_arg upa;
7292 char *branch_refname = NULL;
7294 error = got_object_id_str(&commit_id_str, commit_id);
7297 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7301 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7303 error = got_error_from_errno("asprintf");
7306 error = got_ref_open(&ref, repo, branch_refname, 0);
7307 free(branch_refname);
7310 error = switch_head_ref(ref, commit_id, worktree,
7314 error = got_worktree_set_base_commit_id(worktree, repo,
7318 memset(&upa, 0, sizeof(upa));
7319 error = got_worktree_checkout_files(worktree, &paths,
7320 repo, update_progress, &upa, check_cancelled,
7324 if (upa.did_something) {
7325 printf("Updated to %s: %s\n",
7326 got_worktree_get_head_ref_name(worktree),
7329 print_update_progress_stats(&upa);
7333 free(keyword_idstr);
7337 const struct got_error *close_err = got_repo_close(repo);
7342 got_worktree_close(worktree);
7344 const struct got_error *pack_err =
7345 got_repo_pack_fds_close(pack_fds);
7352 free(commit_id_str);
7353 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7361 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7362 "[-r repository-path] [-s signer-id] name\n", getprogname());
7367 static const struct got_error *
7368 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7370 const struct got_error *err = NULL;
7371 struct got_reflist_entry *re, *se, *new;
7372 struct got_object_id *re_id, *se_id;
7373 struct got_tag_object *re_tag, *se_tag;
7374 time_t re_time, se_time;
7376 STAILQ_FOREACH(re, tags, entry) {
7377 se = STAILQ_FIRST(sorted);
7379 err = got_reflist_entry_dup(&new, re);
7382 STAILQ_INSERT_HEAD(sorted, new, entry);
7385 err = got_ref_resolve(&re_id, repo, re->ref);
7388 err = got_object_open_as_tag(&re_tag, repo, re_id);
7392 re_time = got_object_tag_get_tagger_time(re_tag);
7393 got_object_tag_close(re_tag);
7397 err = got_ref_resolve(&se_id, repo, re->ref);
7400 err = got_object_open_as_tag(&se_tag, repo, se_id);
7404 se_time = got_object_tag_get_tagger_time(se_tag);
7405 got_object_tag_close(se_tag);
7407 if (se_time > re_time) {
7408 err = got_reflist_entry_dup(&new, re);
7411 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7414 se = STAILQ_NEXT(se, entry);
7423 static const struct got_error *
7424 get_tag_refname(char **refname, const char *tag_name)
7426 const struct got_error *err;
7428 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7429 *refname = strdup(tag_name);
7430 if (*refname == NULL)
7431 return got_error_from_errno("strdup");
7432 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7433 err = got_error_from_errno("asprintf");
7441 static const struct got_error *
7442 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7443 const char *allowed_signers, const char *revoked_signers, int verbosity)
7445 static const struct got_error *err = NULL;
7446 struct got_reflist_head refs;
7447 struct got_reflist_entry *re;
7448 char *wanted_refname = NULL;
7453 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7458 struct got_reference *ref;
7459 err = get_tag_refname(&wanted_refname, tag_name);
7462 /* Wanted tag reference should exist. */
7463 err = got_ref_open(&ref, repo, wanted_refname, 0);
7469 TAILQ_FOREACH(re, &refs, entry) {
7470 const char *refname;
7471 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7473 const char *tagger, *ssh_sig = NULL;
7474 char *sig_msg = NULL;
7476 struct got_object_id *id;
7477 struct got_tag_object *tag;
7478 struct got_commit_object *commit = NULL;
7480 refname = got_ref_get_name(re->ref);
7481 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7482 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7485 refstr = got_ref_to_str(re->ref);
7486 if (refstr == NULL) {
7487 err = got_error_from_errno("got_ref_to_str");
7491 err = got_ref_resolve(&id, repo, re->ref);
7494 err = got_object_open_as_tag(&tag, repo, id);
7496 if (err->code != GOT_ERR_OBJ_TYPE) {
7500 /* "lightweight" tag */
7501 err = got_object_open_as_commit(&commit, repo, id);
7506 tagger = got_object_commit_get_committer(commit);
7508 got_object_commit_get_committer_time(commit);
7509 err = got_object_id_str(&id_str, id);
7515 tagger = got_object_tag_get_tagger(tag);
7516 tagger_time = got_object_tag_get_tagger_time(tag);
7517 err = got_object_id_str(&id_str,
7518 got_object_tag_get_object_id(tag));
7523 if (tag && verify_tags) {
7524 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7525 got_object_tag_get_message(tag));
7526 if (ssh_sig && allowed_signers == NULL) {
7527 err = got_error_msg(
7528 GOT_ERR_VERIFY_TAG_SIGNATURE,
7529 "SSH signature verification requires "
7530 "setting allowed_signers in "
7536 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7538 printf("from: %s\n", tagger);
7539 datestr = get_datestr(&tagger_time, datebuf);
7541 printf("date: %s UTC\n", datestr);
7543 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7545 switch (got_object_tag_get_object_type(tag)) {
7546 case GOT_OBJ_TYPE_BLOB:
7547 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7550 case GOT_OBJ_TYPE_TREE:
7551 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7554 case GOT_OBJ_TYPE_COMMIT:
7555 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7558 case GOT_OBJ_TYPE_TAG:
7559 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7569 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7570 allowed_signers, revoked_signers, verbosity);
7571 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7575 printf("signature: %s", sig_msg);
7581 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7584 got_object_commit_close(commit);
7586 tagmsg0 = strdup(got_object_tag_get_message(tag));
7587 got_object_tag_close(tag);
7588 if (tagmsg0 == NULL) {
7589 err = got_error_from_errno("strdup");
7596 line = strsep(&tagmsg, "\n");
7598 printf(" %s\n", line);
7603 got_ref_list_free(&refs);
7604 free(wanted_refname);
7606 if (err == NULL && bad_sigs)
7607 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7611 static const struct got_error *
7612 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7613 const char *tag_name, const char *editor, const char *repo_path)
7615 const struct got_error *err = NULL;
7616 char *template = NULL, *initial_content = NULL;
7617 int initial_content_len;
7620 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7621 err = got_error_from_errno("asprintf");
7625 initial_content_len = asprintf(&initial_content,
7626 "\n# tagging commit %s as %s\n",
7627 commit_id_str, tag_name);
7628 if (initial_content_len == -1) {
7629 err = got_error_from_errno("asprintf");
7633 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7637 if (write(fd, initial_content, initial_content_len) == -1) {
7638 err = got_error_from_errno2("write", *tagmsg_path);
7641 if (close(fd) == -1) {
7642 err = got_error_from_errno2("close", *tagmsg_path);
7647 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7648 initial_content_len, 1);
7650 free(initial_content);
7653 if (fd != -1 && close(fd) == -1 && err == NULL)
7654 err = got_error_from_errno2("close", *tagmsg_path);
7663 static const struct got_error *
7664 add_tag(struct got_repository *repo, const char *tagger,
7665 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7666 const char *signer_id, const char *editor, int verbosity)
7668 const struct got_error *err = NULL;
7669 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7670 char *label = NULL, *commit_id_str = NULL;
7671 struct got_reference *ref = NULL;
7672 char *refname = NULL, *tagmsg = NULL;
7673 char *tagmsg_path = NULL, *tag_id_str = NULL;
7674 int preserve_tagmsg = 0;
7675 struct got_reflist_head refs;
7680 * Don't let the user create a tag name with a leading '-'.
7681 * While technically a valid reference name, this case is usually
7682 * an unintended typo.
7684 if (tag_name[0] == '-')
7685 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7687 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7691 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7692 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7696 err = got_object_id_str(&commit_id_str, commit_id);
7700 err = get_tag_refname(&refname, tag_name);
7703 if (strncmp("refs/tags/", tag_name, 10) == 0)
7706 err = got_ref_open(&ref, repo, refname, 0);
7708 err = got_error(GOT_ERR_TAG_EXISTS);
7710 } else if (err->code != GOT_ERR_NOT_REF)
7713 if (tagmsg_arg == NULL) {
7714 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7715 tag_name, editor, got_repo_get_path(repo));
7717 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7718 tagmsg_path != NULL)
7719 preserve_tagmsg = 1;
7724 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7725 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7729 preserve_tagmsg = 1;
7733 err = got_ref_alloc(&ref, refname, tag_id);
7736 preserve_tagmsg = 1;
7740 err = got_ref_write(ref, repo);
7743 preserve_tagmsg = 1;
7747 err = got_object_id_str(&tag_id_str, tag_id);
7750 preserve_tagmsg = 1;
7753 printf("Created tag %s\n", tag_id_str);
7755 if (preserve_tagmsg) {
7756 fprintf(stderr, "%s: tag message preserved in %s\n",
7757 getprogname(), tagmsg_path);
7758 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7759 err = got_error_from_errno2("unlink", tagmsg_path);
7764 free(commit_id_str);
7768 got_ref_list_free(&refs);
7772 static const struct got_error *
7773 cmd_tag(int argc, char *argv[])
7775 const struct got_error *error = NULL;
7776 struct got_repository *repo = NULL;
7777 struct got_worktree *worktree = NULL;
7778 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7779 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7780 char *allowed_signers = NULL, *revoked_signers = NULL, *editor = NULL;
7781 const char *signer_id = NULL;
7782 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7783 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7784 int *pack_fds = NULL;
7787 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7788 "sendfd unveil", NULL) == -1)
7792 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7795 commit_id_arg = optarg;
7804 repo_path = realpath(optarg, NULL);
7805 if (repo_path == NULL) {
7806 error = got_error_from_errno2("realpath",
7810 got_path_strip_trailing_slashes(repo_path);
7821 else if (verbosity < 3)
7833 if (do_list || verify_tags) {
7834 if (commit_id_arg != NULL)
7836 "-c option can only be used when creating a tag");
7839 option_conflict('l', 'm');
7841 option_conflict('V', 'm');
7845 option_conflict('l', 's');
7847 option_conflict('V', 's');
7851 } else if (argc != 1)
7857 cwd = getcwd(NULL, 0);
7859 error = got_error_from_errno("getcwd");
7863 error = got_repo_pack_fds_open(&pack_fds);
7867 if (repo_path == NULL) {
7868 error = got_worktree_open(&worktree, cwd,
7869 GOT_WORKTREE_GOT_DIR);
7870 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7876 strdup(got_worktree_get_repo_path(worktree));
7877 if (repo_path == NULL)
7878 error = got_error_from_errno("strdup");
7882 repo_path = strdup(cwd);
7883 if (repo_path == NULL) {
7884 error = got_error_from_errno("strdup");
7890 if (do_list || verify_tags) {
7891 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7894 error = get_allowed_signers(&allowed_signers, repo, worktree);
7897 error = get_revoked_signers(&revoked_signers, repo, worktree);
7901 /* Release work tree lock. */
7902 got_worktree_close(worktree);
7907 * Remove "cpath" promise unless needed for signature tmpfile
7911 got_sigs_apply_unveil();
7914 if (pledge("stdio rpath wpath flock proc exec sendfd "
7915 "unveil", NULL) == -1)
7919 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7922 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7923 revoked_signers, verbosity);
7925 error = get_gitconfig_path(&gitconfig_path);
7928 error = got_repo_open(&repo, repo_path, gitconfig_path,
7933 error = get_author(&tagger, repo, worktree);
7936 if (signer_id == NULL)
7937 signer_id = get_signer_id(repo, worktree);
7939 if (tagmsg == NULL) {
7940 error = get_editor(&editor);
7943 if (unveil(editor, "x") != 0) {
7944 error = got_error_from_errno2("unveil", editor);
7949 error = got_sigs_apply_unveil();
7953 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7957 if (commit_id_arg == NULL) {
7958 struct got_reference *head_ref;
7959 struct got_object_id *commit_id;
7960 error = got_ref_open(&head_ref, repo,
7961 worktree ? got_worktree_get_head_ref_name(worktree)
7965 error = got_ref_resolve(&commit_id, repo, head_ref);
7966 got_ref_close(head_ref);
7969 error = got_object_id_str(&commit_id_str, commit_id);
7974 error = got_keyword_to_idstr(&keyword_idstr,
7975 commit_id_arg, repo, worktree);
7978 commit_id_str = keyword_idstr;
7982 /* Release work tree lock. */
7983 got_worktree_close(worktree);
7987 error = add_tag(repo, tagger, tag_name,
7988 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7989 signer_id, editor, verbosity);
7993 const struct got_error *close_err = got_repo_close(repo);
7998 got_worktree_close(worktree);
8000 const struct got_error *pack_err =
8001 got_repo_pack_fds_close(pack_fds);
8008 free(gitconfig_path);
8009 free(commit_id_str);
8011 free(allowed_signers);
8012 free(revoked_signers);
8019 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
8023 static const struct got_error *
8024 add_progress(void *arg, unsigned char status, const char *path)
8026 while (path[0] == '/')
8028 printf("%c %s\n", status, path);
8032 static const struct got_error *
8033 cmd_add(int argc, char *argv[])
8035 const struct got_error *error = NULL;
8036 struct got_repository *repo = NULL;
8037 struct got_worktree *worktree = NULL;
8039 struct got_pathlist_head paths;
8040 struct got_pathlist_entry *pe;
8041 int ch, can_recurse = 0, no_ignores = 0;
8042 int *pack_fds = NULL;
8047 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8052 while ((ch = getopt(argc, argv, "IR")) != -1) {
8072 cwd = getcwd(NULL, 0);
8074 error = got_error_from_errno("getcwd");
8078 error = got_repo_pack_fds_open(&pack_fds);
8082 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8084 if (error->code == GOT_ERR_NOT_WORKTREE)
8085 error = wrap_not_worktree_error(error, "add", cwd);
8089 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8094 error = apply_unveil(got_repo_get_path(repo), 1,
8095 got_worktree_get_root_path(worktree));
8099 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8106 TAILQ_FOREACH(pe, &paths, entry) {
8107 if (asprintf(&ondisk_path, "%s/%s",
8108 got_worktree_get_root_path(worktree),
8110 error = got_error_from_errno("asprintf");
8113 if (lstat(ondisk_path, &sb) == -1) {
8114 if (errno == ENOENT) {
8118 error = got_error_from_errno2("lstat",
8124 if (S_ISDIR(sb.st_mode)) {
8125 error = got_error_msg(GOT_ERR_BAD_PATH,
8126 "adding directories requires -R option");
8132 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8133 NULL, repo, no_ignores);
8136 const struct got_error *close_err = got_repo_close(repo);
8141 got_worktree_close(worktree);
8143 const struct got_error *pack_err =
8144 got_repo_pack_fds_close(pack_fds);
8148 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8156 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8161 static const struct got_error *
8162 print_remove_status(void *arg, unsigned char status,
8163 unsigned char staged_status, const char *path)
8165 while (path[0] == '/')
8167 if (status == GOT_STATUS_NONEXISTENT)
8169 if (status == staged_status && (status == GOT_STATUS_DELETE))
8170 status = GOT_STATUS_NO_CHANGE;
8171 printf("%c%c %s\n", status, staged_status, path);
8175 static const struct got_error *
8176 cmd_remove(int argc, char *argv[])
8178 const struct got_error *error = NULL;
8179 struct got_worktree *worktree = NULL;
8180 struct got_repository *repo = NULL;
8181 const char *status_codes = NULL;
8183 struct got_pathlist_head paths;
8184 struct got_pathlist_entry *pe;
8185 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8186 int ignore_missing_paths = 0;
8187 int *pack_fds = NULL;
8192 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8197 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8200 delete_local_mods = 1;
8201 ignore_missing_paths = 1;
8210 for (i = 0; optarg[i] != '\0'; i++) {
8211 switch (optarg[i]) {
8212 case GOT_STATUS_MODIFY:
8213 delete_local_mods = 1;
8215 case GOT_STATUS_MISSING:
8216 ignore_missing_paths = 1;
8219 errx(1, "invalid status code '%c'",
8223 status_codes = optarg;
8237 cwd = getcwd(NULL, 0);
8239 error = got_error_from_errno("getcwd");
8243 error = got_repo_pack_fds_open(&pack_fds);
8247 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8249 if (error->code == GOT_ERR_NOT_WORKTREE)
8250 error = wrap_not_worktree_error(error, "remove", cwd);
8254 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8259 error = apply_unveil(got_repo_get_path(repo), 1,
8260 got_worktree_get_root_path(worktree));
8264 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8271 TAILQ_FOREACH(pe, &paths, entry) {
8272 if (asprintf(&ondisk_path, "%s/%s",
8273 got_worktree_get_root_path(worktree),
8275 error = got_error_from_errno("asprintf");
8278 if (lstat(ondisk_path, &sb) == -1) {
8279 if (errno == ENOENT) {
8283 error = got_error_from_errno2("lstat",
8289 if (S_ISDIR(sb.st_mode)) {
8290 error = got_error_msg(GOT_ERR_BAD_PATH,
8291 "removing directories requires -R option");
8297 error = got_worktree_schedule_delete(worktree, &paths,
8298 delete_local_mods, status_codes, print_remove_status, NULL,
8299 repo, keep_on_disk, ignore_missing_paths);
8302 const struct got_error *close_err = got_repo_close(repo);
8307 got_worktree_close(worktree);
8309 const struct got_error *pack_err =
8310 got_repo_pack_fds_close(pack_fds);
8314 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8322 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8323 "[patchfile]\n", getprogname());
8327 static const struct got_error *
8328 patch_from_stdin(int *patchfd)
8330 const struct got_error *err = NULL;
8333 sig_t sighup, sigint, sigquit;
8335 *patchfd = got_opentempfd();
8337 return got_error_from_errno("got_opentempfd");
8339 sighup = signal(SIGHUP, SIG_DFL);
8340 sigint = signal(SIGINT, SIG_DFL);
8341 sigquit = signal(SIGQUIT, SIG_DFL);
8344 r = read(0, buf, sizeof(buf));
8346 err = got_error_from_errno("read");
8351 if (write(*patchfd, buf, r) == -1) {
8352 err = got_error_from_errno("write");
8357 signal(SIGHUP, sighup);
8358 signal(SIGINT, sigint);
8359 signal(SIGQUIT, sigquit);
8361 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8362 err = got_error_from_errno("lseek");
8372 struct got_patch_progress_arg {
8378 static const struct got_error *
8379 patch_progress(void *arg, const char *old, const char *new,
8380 unsigned char status, const struct got_error *error, int old_from,
8381 int old_lines, int new_from, int new_lines, int offset,
8382 int ws_mangled, const struct got_error *hunk_err)
8384 const char *path = new == NULL ? old : new;
8385 struct got_patch_progress_arg *a = arg;
8387 while (*path == '/')
8390 if (status != GOT_STATUS_NO_CHANGE &&
8391 status != 0 /* per-hunk progress */) {
8392 printf("%c %s\n", status, path);
8393 a->did_something = 1;
8396 if (hunk_err == NULL) {
8397 if (status == GOT_STATUS_CANNOT_UPDATE)
8399 else if (status == GOT_STATUS_CONFLICT)
8404 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8406 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8407 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8408 old_lines, new_from, new_lines);
8409 if (hunk_err != NULL)
8410 printf("%s\n", hunk_err->msg);
8411 else if (offset != 0)
8412 printf("applied with offset %d\n", offset);
8414 printf("hunk contains mangled whitespace\n");
8421 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8423 if (!ppa->did_something)
8426 if (ppa->conflicts > 0)
8427 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8429 if (ppa->rejects > 0) {
8430 printf("Files where patch failed to apply: %d\n",
8435 static const struct got_error *
8436 cmd_patch(int argc, char *argv[])
8438 const struct got_error *error = NULL, *close_error = NULL;
8439 struct got_worktree *worktree = NULL;
8440 struct got_repository *repo = NULL;
8441 struct got_reflist_head refs;
8442 struct got_object_id *commit_id = NULL;
8443 const char *commit_id_str = NULL;
8446 char *cwd = NULL, *keyword_idstr = NULL;
8447 int ch, nop = 0, strip = -1, reverse = 0;
8449 int *pack_fds = NULL;
8450 struct got_patch_progress_arg ppa;
8455 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8456 "unveil", NULL) == -1)
8460 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8463 commit_id_str = optarg;
8469 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8471 errx(1, "pathname strip count is %s: %s",
8487 error = patch_from_stdin(&patchfd);
8490 } else if (argc == 1) {
8491 patchfd = open(argv[0], O_RDONLY);
8492 if (patchfd == -1) {
8493 error = got_error_from_errno2("open", argv[0]);
8496 if (fstat(patchfd, &sb) == -1) {
8497 error = got_error_from_errno2("fstat", argv[0]);
8500 if (!S_ISREG(sb.st_mode)) {
8501 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8507 if ((cwd = getcwd(NULL, 0)) == NULL) {
8508 error = got_error_from_errno("getcwd");
8512 error = got_repo_pack_fds_open(&pack_fds);
8516 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8520 const char *repo_path = got_worktree_get_repo_path(worktree);
8521 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8525 error = apply_unveil(got_repo_get_path(repo), 0,
8526 got_worktree_get_root_path(worktree));
8530 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8534 if (commit_id_str != NULL) {
8535 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8540 error = got_repo_match_object_id(&commit_id, NULL,
8541 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8542 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8547 memset(&ppa, 0, sizeof(ppa));
8548 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8549 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8550 print_patch_progress_stats(&ppa);
8552 got_ref_list_free(&refs);
8553 free(keyword_idstr);
8556 close_error = got_repo_close(repo);
8558 error = close_error;
8560 if (worktree != NULL) {
8561 close_error = got_worktree_close(worktree);
8563 error = close_error;
8566 const struct got_error *pack_err =
8567 got_repo_pack_fds_close(pack_fds);
8578 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8583 static const struct got_error *
8584 revert_progress(void *arg, unsigned char status, const char *path)
8586 if (status == GOT_STATUS_UNVERSIONED)
8589 while (path[0] == '/')
8591 printf("%c %s\n", status, path);
8595 struct choose_patch_arg {
8596 FILE *patch_script_file;
8600 static const struct got_error *
8601 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8602 int nchanges, const char *action)
8604 const struct got_error *err;
8606 size_t linesize = 0;
8610 case GOT_STATUS_ADD:
8611 printf("A %s\n%s this addition? [y/n] ", path, action);
8613 case GOT_STATUS_DELETE:
8614 printf("D %s\n%s this deletion? [y/n] ", path, action);
8616 case GOT_STATUS_MODIFY:
8617 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8618 return got_error_from_errno("fseek");
8619 printf(GOT_COMMIT_SEP_STR);
8620 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8622 if (linelen == -1 && ferror(patch_file)) {
8623 err = got_error_from_errno("getline");
8628 printf(GOT_COMMIT_SEP_STR);
8629 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8630 path, n, nchanges, action);
8633 return got_error_path(path, GOT_ERR_FILE_STATUS);
8640 static const struct got_error *
8641 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8642 FILE *patch_file, int n, int nchanges)
8644 const struct got_error *err = NULL;
8646 size_t linesize = 0;
8649 struct choose_patch_arg *a = arg;
8651 *choice = GOT_PATCH_CHOICE_NONE;
8653 if (a->patch_script_file) {
8655 err = show_change(status, path, patch_file, n, nchanges,
8659 linelen = getline(&line, &linesize, a->patch_script_file);
8660 if (linelen == -1) {
8661 if (ferror(a->patch_script_file))
8662 return got_error_from_errno("getline");
8665 nl = strchr(line, '\n');
8668 if (strcmp(line, "y") == 0) {
8669 *choice = GOT_PATCH_CHOICE_YES;
8671 } else if (strcmp(line, "n") == 0) {
8672 *choice = GOT_PATCH_CHOICE_NO;
8674 } else if (strcmp(line, "q") == 0 &&
8675 status == GOT_STATUS_MODIFY) {
8676 *choice = GOT_PATCH_CHOICE_QUIT;
8679 printf("invalid response '%s'\n", line);
8684 while (resp != 'y' && resp != 'n' && resp != 'q') {
8685 err = show_change(status, path, patch_file, n, nchanges,
8692 if (status == GOT_STATUS_MODIFY) {
8693 if (resp != 'y' && resp != 'n' && resp != 'q') {
8694 printf("invalid response '%c'\n", resp);
8697 } else if (resp != 'y' && resp != 'n') {
8698 printf("invalid response '%c'\n", resp);
8704 *choice = GOT_PATCH_CHOICE_YES;
8705 else if (resp == 'n')
8706 *choice = GOT_PATCH_CHOICE_NO;
8707 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8708 *choice = GOT_PATCH_CHOICE_QUIT;
8713 struct wt_commitable_path_arg {
8714 struct got_pathlist_head *commit_paths;
8719 * Shortcut work tree status callback to determine if the set of paths scanned
8720 * has at least one versioned path that is being modified and, if not NULL, is
8721 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8722 * soon as a path is passed with a status that satisfies this criteria.
8724 static const struct got_error *
8725 worktree_has_commitable_path(void *arg, unsigned char status,
8726 unsigned char staged_status, const char *path,
8727 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8728 struct got_object_id *commit_id, int dirfd, const char *de_name)
8730 struct wt_commitable_path_arg *a = arg;
8732 if (status == staged_status && (status == GOT_STATUS_DELETE))
8733 status = GOT_STATUS_NO_CHANGE;
8735 if (!(status == GOT_STATUS_NO_CHANGE ||
8736 status == GOT_STATUS_UNVERSIONED) ||
8737 staged_status != GOT_STATUS_NO_CHANGE) {
8738 if (a->commit_paths != NULL) {
8739 struct got_pathlist_entry *pe;
8741 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8742 if (strncmp(path, pe->path,
8743 pe->path_len) == 0) {
8744 *a->has_changes = 1;
8749 *a->has_changes = 1;
8751 if (*a->has_changes)
8752 return got_error(GOT_ERR_FILE_MODIFIED);
8759 * Check that the changeset of the commit identified by id is
8760 * comprised of at least one modified path that is being committed.
8762 static const struct got_error *
8763 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8764 struct got_object_id *id, struct got_worktree *worktree,
8765 struct got_repository *repo)
8767 const struct got_error *err;
8768 struct got_pathlist_head paths;
8769 struct got_commit_object *commit = NULL, *pcommit = NULL;
8770 struct got_tree_object *tree = NULL, *ptree = NULL;
8771 struct got_object_qid *pid;
8775 err = got_object_open_as_commit(&commit, repo, id);
8779 err = got_object_open_as_tree(&tree, repo,
8780 got_object_commit_get_tree_id(commit));
8784 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8786 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8790 err = got_object_open_as_tree(&ptree, repo,
8791 got_object_commit_get_tree_id(pcommit));
8796 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8797 got_diff_tree_collect_changed_paths, &paths, 0);
8801 err = got_worktree_status(worktree, &paths, repo, 0,
8802 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8803 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8805 * At least one changed path in the referenced commit is
8806 * modified in the work tree, that's all we need to know!
8812 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8814 got_object_commit_close(commit);
8816 got_object_commit_close(pcommit);
8818 got_object_tree_close(tree);
8820 got_object_tree_close(ptree);
8825 * Remove any "logmsg" reference comprised entirely of paths that have
8826 * been reverted in this work tree. If any path in the logmsg ref changeset
8827 * remains in a changed state in the worktree, do not remove the reference.
8829 static const struct got_error *
8830 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8832 const struct got_error *err;
8833 struct got_reflist_head refs;
8834 struct got_reflist_entry *re;
8835 struct got_commit_object *commit = NULL;
8836 struct got_object_id *commit_id = NULL;
8837 struct wt_commitable_path_arg wcpa;
8838 char *uuidstr = NULL;
8842 err = got_worktree_get_uuid(&uuidstr, worktree);
8846 err = got_ref_list(&refs, repo, "refs/got/worktree",
8847 got_ref_cmp_by_name, repo);
8851 TAILQ_FOREACH(re, &refs, entry) {
8852 const char *refname;
8853 int has_changes = 0;
8855 refname = got_ref_get_name(re->ref);
8857 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8858 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8859 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8860 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8861 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8862 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8866 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8867 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8871 err = got_repo_match_object_id(&commit_id, NULL, refname,
8872 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8876 err = got_object_open_as_commit(&commit, repo, commit_id);
8880 wcpa.commit_paths = NULL;
8881 wcpa.has_changes = &has_changes;
8883 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8889 err = got_ref_delete(re->ref, repo);
8894 got_object_commit_close(commit);
8903 got_ref_list_free(&refs);
8905 got_object_commit_close(commit);
8909 static const struct got_error *
8910 cmd_revert(int argc, char *argv[])
8912 const struct got_error *error = NULL;
8913 struct got_worktree *worktree = NULL;
8914 struct got_repository *repo = NULL;
8915 char *cwd = NULL, *path = NULL;
8916 struct got_pathlist_head paths;
8917 struct got_pathlist_entry *pe;
8918 int ch, can_recurse = 0, pflag = 0;
8919 FILE *patch_script_file = NULL;
8920 const char *patch_script_path = NULL;
8921 struct choose_patch_arg cpa;
8922 int *pack_fds = NULL;
8927 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8928 "unveil", NULL) == -1)
8932 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8935 patch_script_path = optarg;
8954 if (patch_script_path && !pflag)
8955 errx(1, "-F option can only be used together with -p option");
8957 cwd = getcwd(NULL, 0);
8959 error = got_error_from_errno("getcwd");
8963 error = got_repo_pack_fds_open(&pack_fds);
8967 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8969 if (error->code == GOT_ERR_NOT_WORKTREE)
8970 error = wrap_not_worktree_error(error, "revert", cwd);
8974 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8979 if (patch_script_path) {
8980 patch_script_file = fopen(patch_script_path, "re");
8981 if (patch_script_file == NULL) {
8982 error = got_error_from_errno2("fopen",
8989 * XXX "c" perm needed on repo dir to delete merge references.
8991 error = apply_unveil(got_repo_get_path(repo), 0,
8992 got_worktree_get_root_path(worktree));
8996 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9003 TAILQ_FOREACH(pe, &paths, entry) {
9004 if (asprintf(&ondisk_path, "%s/%s",
9005 got_worktree_get_root_path(worktree),
9007 error = got_error_from_errno("asprintf");
9010 if (lstat(ondisk_path, &sb) == -1) {
9011 if (errno == ENOENT) {
9015 error = got_error_from_errno2("lstat",
9021 if (S_ISDIR(sb.st_mode)) {
9022 error = got_error_msg(GOT_ERR_BAD_PATH,
9023 "reverting directories requires -R option");
9029 cpa.patch_script_file = patch_script_file;
9030 cpa.action = "revert";
9031 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
9032 pflag ? choose_patch : NULL, &cpa, repo);
9034 error = rm_logmsg_ref(worktree, repo);
9036 if (patch_script_file && fclose(patch_script_file) == EOF &&
9038 error = got_error_from_errno2("fclose", patch_script_path);
9040 const struct got_error *close_err = got_repo_close(repo);
9045 got_worktree_close(worktree);
9047 const struct got_error *pack_err =
9048 got_repo_pack_fds_close(pack_fds);
9052 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9061 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
9062 "[-m message] [path ...]\n", getprogname());
9066 struct collect_commit_logmsg_arg {
9067 const char *cmdline_log;
9068 const char *prepared_log;
9069 const char *merged_log;
9070 int non_interactive;
9072 const char *worktree_path;
9073 const char *branch_name;
9074 const char *repo_path;
9079 static const struct got_error *
9080 read_prepared_logmsg(char **logmsg, const char *path)
9082 const struct got_error *err = NULL;
9088 memset(&sb, 0, sizeof(sb));
9090 f = fopen(path, "re");
9092 return got_error_from_errno2("fopen", path);
9094 if (fstat(fileno(f), &sb) == -1) {
9095 err = got_error_from_errno2("fstat", path);
9098 if (sb.st_size == 0) {
9099 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
9103 *logmsg = malloc(sb.st_size + 1);
9104 if (*logmsg == NULL) {
9105 err = got_error_from_errno("malloc");
9109 r = fread(*logmsg, 1, sb.st_size, f);
9110 if (r != sb.st_size) {
9112 err = got_error_from_errno2("fread", path);
9114 err = got_error(GOT_ERR_IO);
9117 (*logmsg)[sb.st_size] = '\0';
9119 if (fclose(f) == EOF && err == NULL)
9120 err = got_error_from_errno2("fclose", path);
9128 static const struct got_error *
9129 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9130 const char *diff_path, char **logmsg, void *arg)
9132 char *initial_content = NULL;
9133 struct got_pathlist_entry *pe;
9134 const struct got_error *err = NULL;
9135 char *template = NULL;
9136 char *prepared_msg = NULL, *merged_msg = NULL;
9137 struct collect_commit_logmsg_arg *a = arg;
9138 int initial_content_len;
9142 /* if a message was specified on the command line, just use it */
9143 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9144 len = strlen(a->cmdline_log) + 1;
9145 *logmsg = malloc(len + 1);
9146 if (*logmsg == NULL)
9147 return got_error_from_errno("malloc");
9148 strlcpy(*logmsg, a->cmdline_log, len);
9150 } else if (a->prepared_log != NULL && a->non_interactive)
9151 return read_prepared_logmsg(logmsg, a->prepared_log);
9153 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9154 return got_error_from_errno("asprintf");
9156 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9160 if (a->prepared_log) {
9161 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9164 } else if (a->merged_log) {
9165 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9170 initial_content_len = asprintf(&initial_content,
9171 "%s%s\n# changes to be committed on branch %s:\n",
9172 prepared_msg ? prepared_msg : "",
9173 merged_msg ? merged_msg : "", a->branch_name);
9174 if (initial_content_len == -1) {
9175 err = got_error_from_errno("asprintf");
9179 if (write(fd, initial_content, initial_content_len) == -1) {
9180 err = got_error_from_errno2("write", a->logmsg_path);
9184 TAILQ_FOREACH(pe, commitable_paths, entry) {
9185 struct got_commitable *ct = pe->data;
9186 dprintf(fd, "# %c %s\n",
9187 got_commitable_get_status(ct),
9188 got_commitable_get_path(ct));
9192 dprintf(fd, "# detailed changes can be viewed in %s\n",
9196 if (close(fd) == -1) {
9197 err = got_error_from_errno2("close", a->logmsg_path);
9202 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9203 initial_content_len, a->prepared_log ? 0 : 1);
9205 free(initial_content);
9210 if (fd != -1 && close(fd) == -1 && err == NULL)
9211 err = got_error_from_errno2("close", a->logmsg_path);
9219 static const struct got_error *
9220 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9221 const char *type, int has_content)
9223 const struct got_error *err = NULL;
9224 char *logmsg = NULL;
9226 err = got_object_commit_get_logmsg(&logmsg, commit);
9230 if (fprintf(f, "%s# log message of %s commit %s:%s",
9231 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9232 err = got_ferror(f, GOT_ERR_IO);
9239 * Lookup "logmsg" references of backed-out and cherrypicked commits
9240 * belonging to the current work tree. If found, and the worktree has
9241 * at least one modified file that was changed in the referenced commit,
9242 * add its log message to a new temporary file at *logmsg_path.
9243 * Add all refs found to matched_refs to be scheduled for removal on
9244 * successful commit.
9246 static const struct got_error *
9247 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9248 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9249 struct got_repository *repo)
9251 const struct got_error *err;
9252 struct got_commit_object *commit = NULL;
9253 struct got_object_id *id = NULL;
9254 struct got_reflist_head refs;
9255 struct got_reflist_entry *re, *re_match;
9257 char *uuidstr = NULL;
9258 int added_logmsg = 0;
9262 *logmsg_path = NULL;
9264 err = got_worktree_get_uuid(&uuidstr, worktree);
9268 err = got_ref_list(&refs, repo, "refs/got/worktree",
9269 got_ref_cmp_by_name, repo);
9273 TAILQ_FOREACH(re, &refs, entry) {
9274 const char *refname, *type;
9275 struct wt_commitable_path_arg wcpa;
9278 refname = got_ref_get_name(re->ref);
9280 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9281 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9282 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9283 type = "cherrypicked";
9284 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9285 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9286 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9287 type = "backed-out";
9291 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9292 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9296 err = got_repo_match_object_id(&id, NULL, refname,
9297 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9301 err = got_object_open_as_commit(&commit, repo, id);
9305 wcpa.commit_paths = paths;
9306 wcpa.has_changes = &add_logmsg;
9308 err = commit_path_changed_in_worktree(&wcpa, id,
9315 err = got_opentemp_named(logmsg_path, &f,
9316 "got-commit-logmsg", "");
9320 err = cat_logmsg(f, commit, refname, type,
9327 err = got_reflist_entry_dup(&re_match, re);
9330 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9333 got_object_commit_close(commit);
9342 got_ref_list_free(&refs);
9344 got_object_commit_close(commit);
9345 if (f && fclose(f) == EOF && err == NULL)
9346 err = got_error_from_errno("fclose");
9347 if (!added_logmsg) {
9348 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9349 err = got_error_from_errno2("unlink", *logmsg_path);
9350 *logmsg_path = NULL;
9355 static const struct got_error *
9356 cmd_commit(int argc, char *argv[])
9358 const struct got_error *error = NULL;
9359 struct got_worktree *worktree = NULL;
9360 struct got_repository *repo = NULL;
9361 char *cwd = NULL, *id_str = NULL;
9362 struct got_object_id *id = NULL;
9363 const char *logmsg = NULL;
9364 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9365 struct collect_commit_logmsg_arg cl_arg;
9366 const char *author = NULL;
9367 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9368 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9369 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9370 int show_diff = 1, commit_conflicts = 0;
9371 struct got_pathlist_head paths;
9372 struct got_reflist_head refs;
9373 struct got_reflist_entry *re;
9374 int *pack_fds = NULL;
9378 cl_arg.logmsg_path = NULL;
9381 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9382 "unveil", NULL) == -1)
9386 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9390 error = valid_author(author);
9395 commit_conflicts = 1;
9399 option_conflict('F', 'm');
9400 prepared_logmsg = realpath(optarg, NULL);
9401 if (prepared_logmsg == NULL)
9402 return got_error_from_errno2("realpath",
9406 if (prepared_logmsg)
9407 option_conflict('m', 'F');
9411 non_interactive = 1;
9417 allow_bad_symlinks = 1;
9428 cwd = getcwd(NULL, 0);
9430 error = got_error_from_errno("getcwd");
9434 error = got_repo_pack_fds_open(&pack_fds);
9438 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9440 if (error->code == GOT_ERR_NOT_WORKTREE)
9441 error = wrap_not_worktree_error(error, "commit", cwd);
9445 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9448 if (rebase_in_progress) {
9449 error = got_error(GOT_ERR_REBASING);
9453 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9458 error = get_gitconfig_path(&gitconfig_path);
9461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9462 gitconfig_path, pack_fds);
9466 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9469 if (merge_in_progress) {
9470 error = got_error(GOT_ERR_MERGE_BUSY);
9474 error = get_author(&committer, repo, worktree);
9481 if (logmsg == NULL || strlen(logmsg) == 0) {
9482 error = get_editor(&editor);
9485 if (unveil(editor, "x") != 0) {
9486 error = got_error_from_errno2("unveil", editor);
9490 if (prepared_logmsg) {
9491 if (unveil(prepared_logmsg, "r") != 0) {
9492 error = got_error_from_errno2("unveil",
9498 error = apply_unveil(got_repo_get_path(repo), 0,
9499 got_worktree_get_root_path(worktree));
9503 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9507 if (prepared_logmsg == NULL) {
9508 error = lookup_logmsg_ref(&merged_logmsg,
9509 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9514 cl_arg.editor = editor;
9515 cl_arg.cmdline_log = logmsg;
9516 cl_arg.prepared_log = prepared_logmsg;
9517 cl_arg.merged_log = merged_logmsg;
9518 cl_arg.non_interactive = non_interactive;
9519 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9520 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9521 if (!histedit_in_progress) {
9522 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9523 error = got_error(GOT_ERR_COMMIT_BRANCH);
9526 cl_arg.branch_name += 11;
9528 cl_arg.repo_path = got_repo_get_path(repo);
9529 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9530 allow_bad_symlinks, show_diff, commit_conflicts,
9531 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9533 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9534 cl_arg.logmsg_path != NULL)
9535 preserve_logmsg = 1;
9539 error = got_object_id_str(&id_str, id);
9542 printf("Created commit %s\n", id_str);
9544 TAILQ_FOREACH(re, &refs, entry) {
9545 error = got_ref_delete(re->ref, repo);
9551 if (preserve_logmsg) {
9552 fprintf(stderr, "%s: log message preserved in %s\n",
9553 getprogname(), cl_arg.logmsg_path);
9554 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9556 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9557 free(cl_arg.logmsg_path);
9558 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9559 error = got_error_from_errno2("unlink", merged_logmsg);
9560 free(merged_logmsg);
9562 const struct got_error *close_err = got_repo_close(repo);
9567 got_worktree_close(worktree);
9569 const struct got_error *pack_err =
9570 got_repo_pack_fds_close(pack_fds);
9574 got_ref_list_free(&refs);
9575 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9578 free(gitconfig_path);
9581 free(prepared_logmsg);
9588 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9589 "[-r repository-path] [-t tag] [remote-repository]\n",
9595 print_load_info(int print_colored, int print_found, int print_trees,
9596 int ncolored, int nfound, int ntrees)
9598 if (print_colored) {
9599 printf("%d commit%s colored", ncolored,
9600 ncolored == 1 ? "" : "s");
9603 printf("%s%d object%s found",
9604 ncolored > 0 ? "; " : "",
9605 nfound, nfound == 1 ? "" : "s");
9608 printf("; %d tree%s scanned", ntrees,
9609 ntrees == 1 ? "" : "s");
9613 struct got_send_progress_arg {
9614 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9621 int last_nobj_total;
9625 int printed_something;
9627 struct got_pathlist_head *delete_branches;
9630 static const struct got_error *
9631 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9632 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9633 int nobj_written, off_t bytes_sent, const char *refname,
9634 const char *errmsg, int success)
9636 struct got_send_progress_arg *a = arg;
9637 char scaled_packsize[FMT_SCALED_STRSIZE];
9638 char scaled_sent[FMT_SCALED_STRSIZE];
9639 int p_deltify = 0, p_written = 0, p_sent = 0;
9640 int print_colored = 0, print_found = 0, print_trees = 0;
9641 int print_searching = 0, print_total = 0;
9642 int print_deltify = 0, print_written = 0, print_sent = 0;
9644 if (a->verbosity < 0)
9648 const char *status = success ? "accepted" : "rejected";
9651 struct got_pathlist_entry *pe;
9652 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9653 const char *branchname = pe->path;
9654 if (got_path_cmp(branchname, refname,
9655 strlen(branchname), strlen(refname)) == 0) {
9657 a->sent_something = 1;
9663 if (a->printed_something)
9665 printf("Server has %s %s", status, refname);
9667 printf(": %s", errmsg);
9668 a->printed_something = 1;
9672 if (a->last_ncolored != ncolored) {
9674 a->last_ncolored = ncolored;
9677 if (a->last_nfound != nfound) {
9680 a->last_nfound = nfound;
9683 if (a->last_ntrees != ntrees) {
9687 a->last_ntrees = ntrees;
9690 if ((print_colored || print_found || print_trees) &&
9693 print_load_info(print_colored, print_found, print_trees,
9694 ncolored, nfound, ntrees);
9695 a->printed_something = 1;
9698 } else if (!a->loading_done) {
9700 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9702 a->loading_done = 1;
9705 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9706 return got_error_from_errno("fmt_scaled");
9707 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9708 return got_error_from_errno("fmt_scaled");
9710 if (a->last_ncommits != ncommits) {
9711 print_searching = 1;
9712 a->last_ncommits = ncommits;
9715 if (a->last_nobj_total != nobj_total) {
9716 print_searching = 1;
9718 a->last_nobj_total = nobj_total;
9721 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9722 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9723 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9724 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9725 return got_error(GOT_ERR_NO_SPACE);
9728 if (nobj_deltify > 0 || nobj_written > 0) {
9729 if (nobj_deltify > 0) {
9730 p_deltify = (nobj_deltify * 100) / nobj_total;
9731 if (p_deltify != a->last_p_deltify) {
9732 a->last_p_deltify = p_deltify;
9733 print_searching = 1;
9738 if (nobj_written > 0) {
9739 p_written = (nobj_written * 100) / nobj_total;
9740 if (p_written != a->last_p_written) {
9741 a->last_p_written = p_written;
9742 print_searching = 1;
9750 if (bytes_sent > 0) {
9751 p_sent = (bytes_sent * 100) / packfile_size;
9752 if (p_sent != a->last_p_sent) {
9753 a->last_p_sent = p_sent;
9754 print_searching = 1;
9760 a->sent_something = 1;
9763 if (print_searching || print_total || print_deltify || print_written ||
9766 if (print_searching)
9767 printf("packing %d reference%s", ncommits,
9768 ncommits == 1 ? "" : "s");
9770 printf("; %d object%s", nobj_total,
9771 nobj_total == 1 ? "" : "s");
9773 printf("; deltify: %d%%", p_deltify);
9775 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9776 scaled_packsize, p_sent);
9777 else if (print_written)
9778 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9779 scaled_packsize, p_written);
9780 if (print_searching || print_total || print_deltify ||
9781 print_written || print_sent) {
9782 a->printed_something = 1;
9788 static const struct got_error *
9789 cmd_send(int argc, char *argv[])
9791 const struct got_error *error = NULL;
9792 char *cwd = NULL, *repo_path = NULL;
9793 const char *remote_name;
9794 char *proto = NULL, *host = NULL, *port = NULL;
9795 char *repo_name = NULL, *server_path = NULL;
9796 const struct got_remote_repo *remotes;
9797 struct got_remote_repo *remote = NULL;
9798 int nremotes, nbranches = 0, ndelete_branches = 0;
9799 struct got_repository *repo = NULL;
9800 struct got_worktree *worktree = NULL;
9801 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9802 struct got_pathlist_head branches;
9803 struct got_pathlist_head tags;
9804 struct got_reflist_head all_branches;
9805 struct got_reflist_head all_tags;
9806 struct got_pathlist_head delete_args;
9807 struct got_pathlist_head delete_branches;
9808 struct got_reflist_entry *re;
9809 struct got_pathlist_entry *pe;
9810 int i, ch, sendfd = -1, sendstatus;
9812 struct got_send_progress_arg spa;
9813 int verbosity = 0, overwrite_refs = 0;
9814 int send_all_branches = 0, send_all_tags = 0;
9815 struct got_reference *ref = NULL;
9816 int *pack_fds = NULL;
9818 TAILQ_INIT(&branches);
9820 TAILQ_INIT(&all_branches);
9821 TAILQ_INIT(&all_tags);
9822 TAILQ_INIT(&delete_args);
9823 TAILQ_INIT(&delete_branches);
9825 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9828 send_all_branches = 1;
9831 error = got_pathlist_append(&branches, optarg, NULL);
9837 error = got_pathlist_append(&delete_args, optarg, NULL);
9848 repo_path = realpath(optarg, NULL);
9849 if (repo_path == NULL)
9850 return got_error_from_errno2("realpath",
9852 got_path_strip_trailing_slashes(repo_path);
9858 error = got_pathlist_append(&tags, optarg, NULL);
9865 else if (verbosity < 3)
9876 if (send_all_branches && !TAILQ_EMPTY(&branches))
9877 option_conflict('a', 'b');
9878 if (send_all_tags && !TAILQ_EMPTY(&tags))
9879 option_conflict('T', 't');
9883 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9885 remote_name = argv[0];
9889 cwd = getcwd(NULL, 0);
9891 error = got_error_from_errno("getcwd");
9895 error = got_repo_pack_fds_open(&pack_fds);
9899 if (repo_path == NULL) {
9900 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9901 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9907 strdup(got_worktree_get_repo_path(worktree));
9908 if (repo_path == NULL)
9909 error = got_error_from_errno("strdup");
9913 repo_path = strdup(cwd);
9914 if (repo_path == NULL) {
9915 error = got_error_from_errno("strdup");
9921 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9926 worktree_conf = got_worktree_get_gotconfig(worktree);
9927 if (worktree_conf) {
9928 got_gotconfig_get_remotes(&nremotes, &remotes,
9930 for (i = 0; i < nremotes; i++) {
9931 if (strcmp(remotes[i].name, remote_name) == 0) {
9932 error = got_repo_remote_repo_dup(&remote,
9941 if (remote == NULL) {
9942 repo_conf = got_repo_get_gotconfig(repo);
9944 got_gotconfig_get_remotes(&nremotes, &remotes,
9946 for (i = 0; i < nremotes; i++) {
9947 if (strcmp(remotes[i].name, remote_name) == 0) {
9948 error = got_repo_remote_repo_dup(&remote,
9957 if (remote == NULL) {
9958 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9959 for (i = 0; i < nremotes; i++) {
9960 if (strcmp(remotes[i].name, remote_name) == 0) {
9961 error = got_repo_remote_repo_dup(&remote,
9969 if (remote == NULL) {
9970 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9974 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9975 &repo_name, remote->send_url);
9979 if (strcmp(proto, "git") == 0) {
9981 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9982 "sendfd dns inet unveil", NULL) == -1)
9985 } else if (strcmp(proto, "git+ssh") == 0 ||
9986 strcmp(proto, "ssh") == 0) {
9988 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9989 "sendfd unveil", NULL) == -1)
9992 } else if (strcmp(proto, "http") == 0 ||
9993 strcmp(proto, "git+http") == 0) {
9994 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9997 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
10001 error = got_dial_apply_unveil(proto);
10005 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
10009 if (send_all_branches) {
10010 error = got_ref_list(&all_branches, repo, "refs/heads",
10011 got_ref_cmp_by_name, NULL);
10014 TAILQ_FOREACH(re, &all_branches, entry) {
10015 const char *branchname = got_ref_get_name(re->ref);
10016 error = got_pathlist_append(&branches,
10022 } else if (nbranches == 0) {
10023 for (i = 0; i < remote->nsend_branches; i++) {
10024 error = got_pathlist_append(&branches,
10025 remote->send_branches[i], NULL);
10031 if (send_all_tags) {
10032 error = got_ref_list(&all_tags, repo, "refs/tags",
10033 got_ref_cmp_by_name, NULL);
10036 TAILQ_FOREACH(re, &all_tags, entry) {
10037 const char *tagname = got_ref_get_name(re->ref);
10038 error = got_pathlist_append(&tags,
10046 * To prevent accidents only branches in refs/heads/ can be deleted
10047 * with 'got send -d'.
10048 * Deleting anything else requires local repository access or Git.
10050 TAILQ_FOREACH(pe, &delete_args, entry) {
10051 const char *branchname = pe->path;
10053 struct got_pathlist_entry *new;
10054 if (strncmp(branchname, "refs/heads/", 11) == 0) {
10055 s = strdup(branchname);
10057 error = got_error_from_errno("strdup");
10061 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
10062 error = got_error_from_errno("asprintf");
10066 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
10067 if (error || new == NULL /* duplicate */)
10071 ndelete_branches++;
10074 if (nbranches == 0 && ndelete_branches == 0) {
10075 struct got_reference *head_ref;
10077 error = got_ref_open(&head_ref, repo,
10078 got_worktree_get_head_ref_name(worktree), 0);
10080 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
10083 if (got_ref_is_symbolic(head_ref)) {
10084 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
10085 got_ref_close(head_ref);
10090 error = got_pathlist_append(&branches, got_ref_get_name(ref),
10098 /* Release work tree lock. */
10099 got_worktree_close(worktree);
10103 if (verbosity >= 0) {
10104 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10105 remote->name, proto, host,
10106 port ? ":" : "", port ? port : "",
10107 *server_path == '/' ? "" : "/", server_path);
10110 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
10111 server_path, verbosity);
10115 memset(&spa, 0, sizeof(spa));
10116 spa.last_scaled_packsize[0] = '\0';
10117 spa.last_p_deltify = -1;
10118 spa.last_p_written = -1;
10119 spa.verbosity = verbosity;
10120 spa.delete_branches = &delete_branches;
10121 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
10122 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
10123 check_cancelled, NULL);
10124 if (spa.printed_something)
10128 if (!spa.sent_something && verbosity >= 0)
10129 printf("Already up-to-date\n");
10132 if (kill(sendpid, SIGTERM) == -1)
10133 error = got_error_from_errno("kill");
10134 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
10135 error = got_error_from_errno("waitpid");
10137 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
10138 error = got_error_from_errno("close");
10140 const struct got_error *close_err = got_repo_close(repo);
10145 got_worktree_close(worktree);
10147 const struct got_error *pack_err =
10148 got_repo_pack_fds_close(pack_fds);
10153 got_ref_close(ref);
10154 got_repo_free_remote_repo_data(remote);
10156 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10157 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10158 got_ref_list_free(&all_branches);
10159 got_ref_list_free(&all_tags);
10160 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10161 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10173 * Print and if delete is set delete all ref_prefix references.
10174 * If wanted_ref is not NULL, only print or delete this reference.
10176 static const struct got_error *
10177 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10178 const char *wanted_ref, int delete, struct got_worktree *worktree,
10179 struct got_repository *repo)
10181 const struct got_error *err;
10182 struct got_pathlist_head paths;
10183 struct got_reflist_head refs;
10184 struct got_reflist_entry *re;
10185 struct got_reflist_object_id_map *refs_idmap = NULL;
10186 struct got_commit_object *commit = NULL;
10187 struct got_object_id *id = NULL;
10188 const char *header_prefix;
10189 char *uuidstr = NULL;
10193 TAILQ_INIT(&paths);
10195 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10199 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10203 if (worktree != NULL) {
10204 err = got_worktree_get_uuid(&uuidstr, worktree);
10210 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10214 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10215 header_prefix = "backout";
10217 header_prefix = "cherrypick";
10219 TAILQ_FOREACH(re, &refs, entry) {
10220 const char *refname, *wt;
10222 refname = got_ref_get_name(re->ref);
10224 err = check_cancelled(NULL);
10228 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10229 refname += prefix_len + 1; /* skip '-' delimiter */
10235 if (worktree == NULL || strncmp(refname, uuidstr,
10236 GOT_WORKTREE_UUID_STRLEN) == 0)
10237 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10241 err = got_repo_match_object_id(&id, NULL, refname,
10242 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10246 err = got_object_open_as_commit(&commit, repo, id);
10251 found = strncmp(wanted_ref, refname,
10252 strlen(wanted_ref)) == 0;
10253 if (wanted_ref && !found) {
10254 struct got_reflist_head *ci_refs;
10256 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10260 char *refs_str = NULL;
10261 char const *r = NULL;
10263 err = build_refs_str(&refs_str, ci_refs, id,
10270 if (strncmp(r, wanted_ref,
10271 strlen(wanted_ref)) == 0) {
10275 r = strchr(r, ' ');
10283 if (wanted_ref == NULL || found) {
10285 err = got_ref_delete(re->ref, repo);
10288 printf("Deleted: ");
10289 err = print_commit_oneline(commit, id, repo,
10293 * Print paths modified by commit to help
10294 * associate commits with worktree changes.
10296 err = get_changed_paths(&paths, commit,
10301 err = print_commit(commit, id, repo, NULL,
10302 &paths, NULL, 0, 0, refs_idmap, NULL,
10304 got_pathlist_free(&paths,
10305 GOT_PATHLIST_FREE_ALL);
10307 if (worktree == NULL)
10308 printf("work tree: %.*s\n\n",
10309 GOT_WORKTREE_UUID_STRLEN, wt);
10315 got_object_commit_close(commit);
10321 if (wanted_ref != NULL && !found)
10322 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10327 got_ref_list_free(&refs);
10328 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10330 got_reflist_object_id_map_free(refs_idmap);
10332 got_object_commit_close(commit);
10337 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10338 * identified by id for log messages to prepopulate the editor on commit.
10340 static const struct got_error *
10341 logmsg_ref(struct got_object_id *id, const char *prefix,
10342 struct got_worktree *worktree, struct got_repository *repo)
10344 const struct got_error *err = NULL;
10345 char *idstr, *ref = NULL, *refname = NULL;
10346 int histedit_in_progress;
10347 int rebase_in_progress, merge_in_progress;
10350 * Silenty refuse to create merge reference if any histedit, merge,
10351 * or rebase operation is in progress.
10353 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10357 if (histedit_in_progress)
10360 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10363 if (rebase_in_progress)
10366 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10370 if (merge_in_progress)
10373 err = got_object_id_str(&idstr, id);
10377 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10381 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10382 err = got_error_from_errno("asprintf");
10386 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10396 usage_cherrypick(void)
10398 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10403 static const struct got_error *
10404 cmd_cherrypick(int argc, char *argv[])
10406 const struct got_error *error = NULL;
10407 struct got_worktree *worktree = NULL;
10408 struct got_repository *repo = NULL;
10409 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10410 struct got_object_id *commit_id = NULL;
10411 struct got_commit_object *commit = NULL;
10412 struct got_object_qid *pid;
10413 int ch, list_refs = 0, remove_refs = 0;
10414 struct got_update_progress_arg upa;
10415 int *pack_fds = NULL;
10418 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10419 "unveil", NULL) == -1)
10423 while ((ch = getopt(argc, argv, "lX")) != -1) {
10432 usage_cherrypick();
10440 if (list_refs || remove_refs) {
10441 if (argc != 0 && argc != 1)
10442 usage_cherrypick();
10443 } else if (argc != 1)
10444 usage_cherrypick();
10445 if (list_refs && remove_refs)
10446 option_conflict('l', 'X');
10448 cwd = getcwd(NULL, 0);
10450 error = got_error_from_errno("getcwd");
10454 error = got_repo_pack_fds_open(&pack_fds);
10458 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10460 if (list_refs || remove_refs) {
10461 if (error->code != GOT_ERR_NOT_WORKTREE)
10464 if (error->code == GOT_ERR_NOT_WORKTREE)
10465 error = wrap_not_worktree_error(error,
10466 "cherrypick", cwd);
10471 error = got_repo_open(&repo,
10472 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10477 error = apply_unveil(got_repo_get_path(repo), 0,
10478 worktree ? got_worktree_get_root_path(worktree) : NULL);
10482 if (list_refs || remove_refs) {
10483 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10484 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10485 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10489 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10493 error = got_repo_match_object_id(&commit_id, NULL,
10494 keyword_idstr != NULL ? keyword_idstr : argv[0],
10495 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10498 error = got_object_id_str(&commit_id_str, commit_id);
10502 error = got_object_open_as_commit(&commit, repo, commit_id);
10505 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10506 memset(&upa, 0, sizeof(upa));
10507 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10508 commit_id, repo, update_progress, &upa, check_cancelled,
10513 if (upa.did_something) {
10514 error = logmsg_ref(commit_id,
10515 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10518 printf("Merged commit %s\n", commit_id_str);
10520 print_merge_progress_stats(&upa);
10523 free(keyword_idstr);
10525 got_object_commit_close(commit);
10526 free(commit_id_str);
10528 got_worktree_close(worktree);
10530 const struct got_error *close_err = got_repo_close(repo);
10535 const struct got_error *pack_err =
10536 got_repo_pack_fds_close(pack_fds);
10545 usage_backout(void)
10547 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10551 static const struct got_error *
10552 cmd_backout(int argc, char *argv[])
10554 const struct got_error *error = NULL;
10555 struct got_worktree *worktree = NULL;
10556 struct got_repository *repo = NULL;
10557 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10558 struct got_object_id *commit_id = NULL;
10559 struct got_commit_object *commit = NULL;
10560 struct got_object_qid *pid;
10561 int ch, list_refs = 0, remove_refs = 0;
10562 struct got_update_progress_arg upa;
10563 int *pack_fds = NULL;
10566 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10567 "unveil", NULL) == -1)
10571 while ((ch = getopt(argc, argv, "lX")) != -1) {
10588 if (list_refs || remove_refs) {
10589 if (argc != 0 && argc != 1)
10591 } else if (argc != 1)
10593 if (list_refs && remove_refs)
10594 option_conflict('l', 'X');
10596 cwd = getcwd(NULL, 0);
10598 error = got_error_from_errno("getcwd");
10602 error = got_repo_pack_fds_open(&pack_fds);
10606 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10608 if (list_refs || remove_refs) {
10609 if (error->code != GOT_ERR_NOT_WORKTREE)
10612 if (error->code == GOT_ERR_NOT_WORKTREE)
10613 error = wrap_not_worktree_error(error,
10619 error = got_repo_open(&repo,
10620 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10625 error = apply_unveil(got_repo_get_path(repo), 0,
10626 worktree ? got_worktree_get_root_path(worktree) : NULL);
10630 if (list_refs || remove_refs) {
10631 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10632 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10633 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10637 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10641 error = got_repo_match_object_id(&commit_id, NULL,
10642 keyword_idstr != NULL ? keyword_idstr : argv[0],
10643 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10646 error = got_object_id_str(&commit_id_str, commit_id);
10650 error = got_object_open_as_commit(&commit, repo, commit_id);
10653 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10655 error = got_error(GOT_ERR_ROOT_COMMIT);
10659 memset(&upa, 0, sizeof(upa));
10660 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10661 repo, update_progress, &upa, check_cancelled, NULL);
10665 if (upa.did_something) {
10666 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10670 printf("Backed out commit %s\n", commit_id_str);
10672 print_merge_progress_stats(&upa);
10675 free(keyword_idstr);
10677 got_object_commit_close(commit);
10678 free(commit_id_str);
10680 got_worktree_close(worktree);
10682 const struct got_error *close_err = got_repo_close(repo);
10687 const struct got_error *pack_err =
10688 got_repo_pack_fds_close(pack_fds);
10698 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10703 trim_logmsg(char *logmsg, int limit)
10708 len = strlen(logmsg);
10711 logmsg[len] = '\0';
10712 nl = strchr(logmsg, '\n');
10717 static const struct got_error *
10718 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10720 const struct got_error *err;
10721 char *logmsg0 = NULL;
10724 err = got_object_commit_get_logmsg(&logmsg0, commit);
10729 while (isspace((unsigned char)s[0]))
10732 *logmsg = strdup(s);
10733 if (*logmsg == NULL) {
10734 err = got_error_from_errno("strdup");
10738 trim_logmsg(*logmsg, limit);
10744 static const struct got_error *
10745 show_rebase_merge_conflict(struct got_object_id *id,
10746 struct got_repository *repo)
10748 const struct got_error *err;
10749 struct got_commit_object *commit = NULL;
10750 char *id_str = NULL, *logmsg = NULL;
10752 err = got_object_open_as_commit(&commit, repo, id);
10756 err = got_object_id_str(&id_str, id);
10762 err = get_short_logmsg(&logmsg, 42, commit);
10766 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10769 got_object_commit_close(commit);
10774 static const struct got_error *
10775 show_rebase_progress(struct got_commit_object *commit,
10776 struct got_object_id *old_id, struct got_object_id *new_id)
10778 const struct got_error *err;
10779 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10781 err = got_object_id_str(&old_id_str, old_id);
10786 err = got_object_id_str(&new_id_str, new_id);
10791 old_id_str[12] = '\0';
10793 new_id_str[12] = '\0';
10795 err = get_short_logmsg(&logmsg, 42, commit);
10799 printf("%s -> %s: %s\n", old_id_str,
10800 new_id_str ? new_id_str : "no-op change", logmsg);
10808 static const struct got_error *
10809 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10810 struct got_reference *branch, struct got_reference *tmp_branch,
10811 struct got_repository *repo, int create_backup)
10813 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10814 return got_worktree_rebase_complete(worktree, fileindex,
10815 tmp_branch, branch, repo, create_backup);
10818 static const struct got_error *
10819 rebase_commit(struct got_pathlist_head *merged_paths,
10820 struct got_worktree *worktree, struct got_fileindex *fileindex,
10821 struct got_reference *tmp_branch, const char *committer,
10822 struct got_object_id *commit_id, int allow_conflict,
10823 struct got_repository *repo)
10825 const struct got_error *error;
10826 struct got_commit_object *commit;
10827 struct got_object_id *new_commit_id;
10829 error = got_object_open_as_commit(&commit, repo, commit_id);
10833 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10834 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10835 allow_conflict, repo);
10837 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10839 error = show_rebase_progress(commit, commit_id, NULL);
10841 error = show_rebase_progress(commit, commit_id, new_commit_id);
10842 free(new_commit_id);
10845 got_object_commit_close(commit);
10849 struct check_path_prefix_arg {
10850 const char *path_prefix;
10855 static const struct got_error *
10856 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10857 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10858 struct got_object_id *id1, struct got_object_id *id2,
10859 const char *path1, const char *path2,
10860 mode_t mode1, mode_t mode2, struct got_repository *repo)
10862 struct check_path_prefix_arg *a = arg;
10864 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10865 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10866 return got_error(a->errcode);
10871 static const struct got_error *
10872 check_path_prefix(struct got_object_id *parent_id,
10873 struct got_object_id *commit_id, const char *path_prefix,
10874 int errcode, struct got_repository *repo)
10876 const struct got_error *err;
10877 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10878 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10879 struct check_path_prefix_arg cpp_arg;
10881 if (got_path_is_root_dir(path_prefix))
10884 err = got_object_open_as_commit(&commit, repo, commit_id);
10888 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10892 err = got_object_open_as_tree(&tree1, repo,
10893 got_object_commit_get_tree_id(parent_commit));
10897 err = got_object_open_as_tree(&tree2, repo,
10898 got_object_commit_get_tree_id(commit));
10902 cpp_arg.path_prefix = path_prefix;
10903 while (cpp_arg.path_prefix[0] == '/')
10904 cpp_arg.path_prefix++;
10905 cpp_arg.len = strlen(cpp_arg.path_prefix);
10906 cpp_arg.errcode = errcode;
10907 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10908 check_path_prefix_in_diff, &cpp_arg, 0);
10911 got_object_tree_close(tree1);
10913 got_object_tree_close(tree2);
10915 got_object_commit_close(commit);
10917 got_object_commit_close(parent_commit);
10921 static const struct got_error *
10922 collect_commits(struct got_object_id_queue *commits,
10923 struct got_object_id *initial_commit_id,
10924 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10925 const char *path_prefix, int path_prefix_errcode,
10926 struct got_repository *repo)
10928 const struct got_error *err = NULL;
10929 struct got_commit_graph *graph = NULL;
10930 struct got_object_id parent_id, commit_id;
10931 struct got_object_qid *qid;
10933 err = got_commit_graph_open(&graph, "/", 1);
10937 err = got_commit_graph_bfsort(graph, iter_start_id, repo,
10938 check_cancelled, NULL);
10942 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10943 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10944 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10945 check_cancelled, NULL);
10947 if (err->code == GOT_ERR_ITER_COMPLETED) {
10948 err = got_error_msg(GOT_ERR_ANCESTRY,
10949 "ran out of commits to rebase before "
10950 "youngest common ancestor commit has "
10951 "been reached?!?");
10955 err = check_path_prefix(&parent_id, &commit_id,
10956 path_prefix, path_prefix_errcode, repo);
10960 err = got_object_qid_alloc(&qid, &commit_id);
10963 STAILQ_INSERT_HEAD(commits, qid, entry);
10965 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10969 got_commit_graph_close(graph);
10973 static const struct got_error *
10974 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10976 const struct got_error *err = NULL;
10977 time_t committer_time;
10979 char datebuf[11]; /* YYYY-MM-DD + NUL */
10980 char *author0 = NULL, *author, *smallerthan;
10981 char *logmsg0 = NULL, *logmsg, *newline;
10983 committer_time = got_object_commit_get_committer_time(commit);
10984 if (gmtime_r(&committer_time, &tm) == NULL)
10985 return got_error_from_errno("gmtime_r");
10986 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10987 return got_error(GOT_ERR_NO_SPACE);
10989 author0 = strdup(got_object_commit_get_author(commit));
10990 if (author0 == NULL)
10991 return got_error_from_errno("strdup");
10993 smallerthan = strchr(author, '<');
10994 if (smallerthan && smallerthan[1] != '\0')
10995 author = smallerthan + 1;
10996 author[strcspn(author, "@>")] = '\0';
10998 err = got_object_commit_get_logmsg(&logmsg0, commit);
11002 while (*logmsg == '\n')
11004 newline = strchr(logmsg, '\n');
11008 if (asprintf(brief_str, "%s %s %s",
11009 datebuf, author, logmsg) == -1)
11010 err = got_error_from_errno("asprintf");
11017 static const struct got_error *
11018 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
11019 struct got_repository *repo)
11021 const struct got_error *err;
11024 err = got_object_id_str(&id_str, id);
11028 err = got_ref_delete(ref, repo);
11032 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
11038 static const struct got_error *
11039 print_backup_ref(const char *branch_name, const char *new_id_str,
11040 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
11041 struct got_reflist_object_id_map *refs_idmap,
11042 struct got_repository *repo)
11044 const struct got_error *err = NULL;
11045 struct got_reflist_head *refs;
11046 char *refs_str = NULL;
11047 struct got_object_id *new_commit_id = NULL;
11048 struct got_commit_object *new_commit = NULL;
11049 char *new_commit_brief_str = NULL;
11050 struct got_object_id *yca_id = NULL;
11051 struct got_commit_object *yca_commit = NULL;
11052 char *yca_id_str = NULL, *yca_brief_str = NULL;
11053 char *custom_refs_str;
11055 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
11056 return got_error_from_errno("asprintf");
11058 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
11059 0, 0, refs_idmap, custom_refs_str, NULL);
11063 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
11067 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
11069 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
11074 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
11078 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
11082 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11083 old_commit_id, new_commit_id, 1, 0, repo, check_cancelled, NULL);
11087 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
11088 refs_str ? " (" : "", refs_str ? refs_str : "",
11089 refs_str ? ")" : "", new_commit_brief_str);
11090 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
11091 got_object_id_cmp(yca_id, old_commit_id) != 0) {
11095 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
11099 err = get_commit_brief_str(&yca_brief_str, yca_commit);
11103 err = got_object_id_str(&yca_id_str, yca_id);
11107 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
11109 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
11113 printf("history forked at %s%s%s%s\n %s\n",
11115 refs_str ? " (" : "", refs_str ? refs_str : "",
11116 refs_str ? ")" : "", yca_brief_str);
11119 free(custom_refs_str);
11120 free(new_commit_id);
11124 free(yca_brief_str);
11126 got_object_commit_close(new_commit);
11128 got_object_commit_close(yca_commit);
11133 static const struct got_error *
11134 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
11135 struct got_repository *repo)
11137 const struct got_error *err;
11138 struct got_reflist_head refs;
11139 struct got_reflist_entry *re;
11140 char *uuidstr = NULL;
11141 static char msg[160];
11145 err = got_worktree_get_uuid(&uuidstr, worktree);
11149 err = got_ref_list(&refs, repo, "refs/got/worktree",
11150 got_ref_cmp_by_name, repo);
11154 TAILQ_FOREACH(re, &refs, entry) {
11155 const char *cmd, *refname, *type;
11157 refname = got_ref_get_name(re->ref);
11159 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11160 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11161 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11162 cmd = "cherrypick";
11163 type = "cherrypicked";
11164 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11165 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11166 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11168 type = "backed-out";
11172 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11175 snprintf(msg, sizeof(msg),
11176 "work tree has references created by %s commits which "
11177 "must be removed with 'got %s -X' before running the %s "
11178 "command", type, cmd, caller);
11179 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11185 got_ref_list_free(&refs);
11189 static const struct got_error *
11190 process_backup_refs(const char *backup_ref_prefix,
11191 const char *wanted_branch_name,
11192 int delete, struct got_repository *repo)
11194 const struct got_error *err;
11195 struct got_reflist_head refs, backup_refs;
11196 struct got_reflist_entry *re;
11197 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11198 struct got_object_id *old_commit_id = NULL;
11199 char *branch_name = NULL;
11200 struct got_commit_object *old_commit = NULL;
11201 struct got_reflist_object_id_map *refs_idmap = NULL;
11202 int wanted_branch_found = 0;
11205 TAILQ_INIT(&backup_refs);
11207 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11211 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11215 if (wanted_branch_name) {
11216 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11217 wanted_branch_name += 11;
11220 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11221 got_ref_cmp_by_commit_timestamp_descending, repo);
11225 TAILQ_FOREACH(re, &backup_refs, entry) {
11226 const char *refname = got_ref_get_name(re->ref);
11229 err = check_cancelled(NULL);
11233 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11237 err = got_object_open_as_commit(&old_commit, repo,
11242 if (strncmp(backup_ref_prefix, refname,
11243 backup_ref_prefix_len) == 0)
11244 refname += backup_ref_prefix_len;
11246 while (refname[0] == '/')
11249 branch_name = strdup(refname);
11250 if (branch_name == NULL) {
11251 err = got_error_from_errno("strdup");
11254 slash = strrchr(branch_name, '/');
11257 refname += strlen(branch_name) + 1;
11260 if (wanted_branch_name == NULL ||
11261 strcmp(wanted_branch_name, branch_name) == 0) {
11262 wanted_branch_found = 1;
11264 err = delete_backup_ref(re->ref,
11265 old_commit_id, repo);
11267 err = print_backup_ref(branch_name, refname,
11268 old_commit_id, old_commit, refs_idmap,
11275 free(old_commit_id);
11276 old_commit_id = NULL;
11278 branch_name = NULL;
11279 got_object_commit_close(old_commit);
11283 if (wanted_branch_name && !wanted_branch_found) {
11284 err = got_error_fmt(GOT_ERR_NOT_REF,
11285 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11289 got_reflist_object_id_map_free(refs_idmap);
11290 got_ref_list_free(&refs);
11291 got_ref_list_free(&backup_refs);
11292 free(old_commit_id);
11295 got_object_commit_close(old_commit);
11299 static const struct got_error *
11300 abort_progress(void *arg, unsigned char status, const char *path)
11303 * Unversioned files should not clutter progress output when
11304 * an operation is aborted.
11306 if (status == GOT_STATUS_UNVERSIONED)
11309 return update_progress(arg, status, path);
11312 static const struct got_error *
11313 find_merge_commit_yca(struct got_object_id **new_yca_id,
11314 struct got_object_id *branch_head_commit_id,
11315 struct got_object_id *yca_id,
11316 struct got_object_id *base_commit_id,
11317 struct got_repository *repo)
11319 const struct got_error *err = NULL;
11320 struct got_commit_graph *graph = NULL;
11321 struct got_commit_object *commit = NULL;
11323 *new_yca_id = NULL;
11325 err = got_commit_graph_open(&graph, "/", 1);
11329 err = got_commit_graph_bfsort(graph, base_commit_id,
11330 repo, check_cancelled, NULL);
11335 struct got_object_id id;
11337 err = got_commit_graph_iter_next(&id, graph, repo,
11338 check_cancelled, NULL);
11340 if (err->code == GOT_ERR_ITER_COMPLETED)
11345 err = got_object_open_as_commit(&commit, repo, &id);
11349 if (got_object_commit_get_nparents(commit) > 1) {
11350 /* Search for a better YCA using toposort. */
11351 err = got_commit_graph_find_youngest_common_ancestor(
11352 new_yca_id, base_commit_id, branch_head_commit_id,
11353 0, 1, repo, check_cancelled, NULL);
11357 if (got_object_id_cmp(&id, yca_id) == 0)
11359 got_object_commit_close(commit);
11363 got_commit_graph_close(graph);
11365 got_object_commit_close(commit);
11369 static const struct got_error *
11370 cmd_rebase(int argc, char *argv[])
11372 const struct got_error *error = NULL;
11373 struct got_worktree *worktree = NULL;
11374 struct got_repository *repo = NULL;
11375 struct got_fileindex *fileindex = NULL;
11376 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11377 struct got_reference *branch = NULL;
11378 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11379 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11380 struct got_object_id *resume_commit_id = NULL;
11381 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11382 struct got_object_id *head_commit_id = NULL;
11383 struct got_reference *head_ref = NULL;
11384 struct got_commit_object *commit = NULL;
11385 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11386 int histedit_in_progress = 0, merge_in_progress = 0;
11387 int create_backup = 1, list_backups = 0, delete_backups = 0;
11388 int allow_conflict = 0;
11389 struct got_object_id_queue commits;
11390 struct got_pathlist_head merged_paths;
11391 const struct got_object_id_queue *parent_ids;
11392 struct got_object_qid *qid, *pid;
11393 struct got_update_progress_arg upa;
11394 int *pack_fds = NULL;
11396 STAILQ_INIT(&commits);
11397 TAILQ_INIT(&merged_paths);
11398 memset(&upa, 0, sizeof(upa));
11401 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11402 "unveil", NULL) == -1)
11406 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11412 allow_conflict = 1;
11415 continue_rebase = 1;
11421 delete_backups = 1;
11432 if (list_backups) {
11434 option_conflict('l', 'a');
11435 if (allow_conflict)
11436 option_conflict('l', 'C');
11437 if (continue_rebase)
11438 option_conflict('l', 'c');
11439 if (delete_backups)
11440 option_conflict('l', 'X');
11441 if (argc != 0 && argc != 1)
11443 } else if (delete_backups) {
11445 option_conflict('X', 'a');
11446 if (allow_conflict)
11447 option_conflict('X', 'C');
11448 if (continue_rebase)
11449 option_conflict('X', 'c');
11451 option_conflict('l', 'X');
11452 if (argc != 0 && argc != 1)
11454 } else if (allow_conflict) {
11456 option_conflict('C', 'a');
11457 if (!continue_rebase)
11458 errx(1, "-C option requires -c");
11460 if (abort_rebase && continue_rebase)
11462 else if (abort_rebase || continue_rebase) {
11465 } else if (argc != 1)
11469 cwd = getcwd(NULL, 0);
11471 error = got_error_from_errno("getcwd");
11475 error = got_repo_pack_fds_open(&pack_fds);
11479 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11481 if (list_backups || delete_backups) {
11482 if (error->code != GOT_ERR_NOT_WORKTREE)
11485 if (error->code == GOT_ERR_NOT_WORKTREE)
11486 error = wrap_not_worktree_error(error,
11492 error = get_gitconfig_path(&gitconfig_path);
11495 error = got_repo_open(&repo,
11496 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11497 gitconfig_path, pack_fds);
11501 if (worktree != NULL && !list_backups && !delete_backups) {
11502 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11507 error = get_author(&committer, repo, worktree);
11508 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11511 error = apply_unveil(got_repo_get_path(repo), 0,
11512 worktree ? got_worktree_get_root_path(worktree) : NULL);
11516 if (list_backups || delete_backups) {
11517 error = process_backup_refs(
11518 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11519 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11520 goto done; /* nothing else to do */
11523 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11527 if (histedit_in_progress) {
11528 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11532 error = got_worktree_merge_in_progress(&merge_in_progress,
11536 if (merge_in_progress) {
11537 error = got_error(GOT_ERR_MERGE_BUSY);
11541 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11545 if (abort_rebase) {
11546 if (!rebase_in_progress) {
11547 error = got_error(GOT_ERR_NOT_REBASING);
11550 error = got_worktree_rebase_continue(&resume_commit_id,
11551 &new_base_branch, &tmp_branch, &branch, &fileindex,
11555 printf("Switching work tree to %s\n",
11556 got_ref_get_symref_target(new_base_branch));
11557 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11558 new_base_branch, abort_progress, &upa);
11561 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11562 print_merge_progress_stats(&upa);
11563 goto done; /* nothing else to do */
11566 if (continue_rebase) {
11567 if (!rebase_in_progress) {
11568 error = got_error(GOT_ERR_NOT_REBASING);
11571 error = got_worktree_rebase_continue(&resume_commit_id,
11572 &new_base_branch, &tmp_branch, &branch, &fileindex,
11577 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11578 committer, resume_commit_id, allow_conflict, repo);
11582 yca_id = got_object_id_dup(resume_commit_id);
11583 if (yca_id == NULL) {
11584 error = got_error_from_errno("got_object_id_dup");
11588 error = got_ref_open(&branch, repo, argv[0], 0);
11591 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11592 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11593 "will not rebase a branch which lives outside "
11594 "the \"refs/heads/\" reference namespace");
11599 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11603 if (!continue_rebase) {
11604 struct got_object_id *base_commit_id;
11606 error = got_ref_open(&head_ref, repo,
11607 got_worktree_get_head_ref_name(worktree), 0);
11610 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11613 base_commit_id = got_worktree_get_base_commit_id(worktree);
11614 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11615 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11619 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11620 base_commit_id, branch_head_commit_id, 1, 0,
11621 repo, check_cancelled, NULL);
11623 if (error->code == GOT_ERR_ANCESTRY) {
11624 error = got_error_msg(GOT_ERR_ANCESTRY,
11625 "specified branch shares no common "
11626 "ancestry with work tree's branch");
11632 * If a merge commit appears between the new base branch tip
11633 * and a YCA found via first-parent traversal then we might
11634 * find a better YCA using topologically sorted commits.
11636 if (got_object_id_cmp(base_commit_id, yca_id) != 0) {
11637 struct got_object_id *better_yca_id;
11638 error = find_merge_commit_yca(&better_yca_id,
11639 branch_head_commit_id, yca_id,
11640 base_commit_id, repo);
11643 if (better_yca_id) {
11645 yca_id = better_yca_id;
11649 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11650 struct got_pathlist_head paths;
11651 printf("%s is already based on %s\n",
11652 got_ref_get_name(branch),
11653 got_worktree_get_head_ref_name(worktree));
11654 error = switch_head_ref(branch, branch_head_commit_id,
11658 error = got_worktree_set_base_commit_id(worktree, repo,
11659 branch_head_commit_id);
11662 TAILQ_INIT(&paths);
11663 error = got_pathlist_append(&paths, "", NULL);
11666 error = got_worktree_checkout_files(worktree,
11667 &paths, repo, update_progress, &upa,
11668 check_cancelled, NULL);
11669 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11672 if (upa.did_something) {
11674 error = got_object_id_str(&id_str,
11675 branch_head_commit_id);
11678 printf("Updated to %s: %s\n",
11679 got_worktree_get_head_ref_name(worktree),
11683 printf("Already up-to-date\n");
11684 print_update_progress_stats(&upa);
11689 commit_id = branch_head_commit_id;
11690 error = got_object_open_as_commit(&commit, repo, commit_id);
11694 parent_ids = got_object_commit_get_parent_ids(commit);
11695 pid = STAILQ_FIRST(parent_ids);
11697 error = collect_commits(&commits, commit_id, &pid->id,
11698 yca_id, got_worktree_get_path_prefix(worktree),
11699 GOT_ERR_REBASE_PATH, repo);
11704 got_object_commit_close(commit);
11707 if (!continue_rebase) {
11708 error = got_worktree_rebase_prepare(&new_base_branch,
11709 &tmp_branch, &fileindex, worktree, branch, repo);
11714 if (STAILQ_EMPTY(&commits)) {
11715 if (continue_rebase) {
11716 error = rebase_complete(worktree, fileindex,
11717 branch, tmp_branch, repo, create_backup);
11720 /* Fast-forward the reference of the branch. */
11721 struct got_object_id *new_head_commit_id;
11723 error = got_ref_resolve(&new_head_commit_id, repo,
11727 error = got_object_id_str(&id_str, new_head_commit_id);
11730 printf("Forwarding %s to commit %s\n",
11731 got_ref_get_name(branch), id_str);
11733 error = got_ref_change_ref(branch,
11734 new_head_commit_id);
11737 /* No backup needed since objects did not change. */
11743 STAILQ_FOREACH(qid, &commits, entry) {
11745 commit_id = &qid->id;
11746 parent_id = pid ? &pid->id : yca_id;
11749 memset(&upa, 0, sizeof(upa));
11750 error = got_worktree_rebase_merge_files(&merged_paths,
11751 worktree, fileindex, parent_id, commit_id, repo,
11752 update_progress, &upa, check_cancelled, NULL);
11756 print_merge_progress_stats(&upa);
11757 if (upa.conflicts > 0 || upa.missing > 0 ||
11758 upa.not_deleted > 0 || upa.unversioned > 0) {
11759 if (upa.conflicts > 0) {
11760 error = show_rebase_merge_conflict(&qid->id,
11765 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11769 error = rebase_commit(&merged_paths, worktree, fileindex,
11770 tmp_branch, committer, commit_id, 0, repo);
11771 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11776 if (upa.conflicts > 0 || upa.missing > 0 ||
11777 upa.not_deleted > 0 || upa.unversioned > 0) {
11778 error = got_worktree_rebase_postpone(worktree, fileindex);
11781 if (upa.conflicts > 0 && upa.missing == 0 &&
11782 upa.not_deleted == 0 && upa.unversioned == 0) {
11783 error = got_error_msg(GOT_ERR_CONFLICTS,
11784 "conflicts must be resolved before rebasing "
11786 } else if (upa.conflicts > 0) {
11787 error = got_error_msg(GOT_ERR_CONFLICTS,
11788 "conflicts must be resolved before rebasing "
11789 "can continue; changes destined for some "
11790 "files were not yet merged and should be "
11791 "merged manually if required before the "
11792 "rebase operation is continued");
11794 error = got_error_msg(GOT_ERR_CONFLICTS,
11795 "changes destined for some files were not "
11796 "yet merged and should be merged manually "
11797 "if required before the rebase operation "
11801 error = rebase_complete(worktree, fileindex, branch,
11802 tmp_branch, repo, create_backup);
11806 free(gitconfig_path);
11807 got_object_id_queue_free(&commits);
11808 free(branch_head_commit_id);
11809 free(resume_commit_id);
11810 free(head_commit_id);
11813 got_object_commit_close(commit);
11815 got_ref_close(branch);
11816 if (new_base_branch)
11817 got_ref_close(new_base_branch);
11819 got_ref_close(tmp_branch);
11821 got_ref_close(head_ref);
11823 got_worktree_close(worktree);
11825 const struct got_error *close_err = got_repo_close(repo);
11830 const struct got_error *pack_err =
11831 got_repo_pack_fds_close(pack_fds);
11839 usage_histedit(void)
11841 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11842 "[branch]\n", getprogname());
11846 #define GOT_HISTEDIT_PICK 'p'
11847 #define GOT_HISTEDIT_EDIT 'e'
11848 #define GOT_HISTEDIT_FOLD 'f'
11849 #define GOT_HISTEDIT_DROP 'd'
11850 #define GOT_HISTEDIT_MESG 'm'
11852 static const struct got_histedit_cmd {
11853 unsigned char code;
11856 } got_histedit_cmds[] = {
11857 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11858 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11859 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11861 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11862 { GOT_HISTEDIT_MESG, "mesg", "open editor to edit the log message" },
11865 struct got_histedit_list_entry {
11866 TAILQ_ENTRY(got_histedit_list_entry) entry;
11867 struct got_object_id *commit_id;
11868 const struct got_histedit_cmd *cmd;
11871 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11873 static const struct got_error *
11874 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11875 FILE *f, struct got_repository *repo)
11877 const struct got_error *err = NULL;
11878 char *logmsg = NULL, *id_str = NULL;
11879 struct got_commit_object *commit = NULL;
11882 err = got_object_open_as_commit(&commit, repo, commit_id);
11886 err = get_short_logmsg(&logmsg, 34, commit);
11890 err = got_object_id_str(&id_str, commit_id);
11894 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11896 err = got_ferror(f, GOT_ERR_IO);
11899 got_object_commit_close(commit);
11905 static const struct got_error *
11906 histedit_write_commit_list(struct got_object_id_queue *commits,
11907 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11908 int edit_only, struct got_repository *repo)
11910 const struct got_error *err = NULL;
11911 struct got_object_qid *qid;
11912 const char *histedit_cmd = NULL;
11914 if (STAILQ_EMPTY(commits))
11915 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11917 STAILQ_FOREACH(qid, commits, entry) {
11918 histedit_cmd = got_histedit_cmds[0].name;
11920 histedit_cmd = "drop";
11921 else if (edit_only)
11922 histedit_cmd = "edit";
11923 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11924 histedit_cmd = "fold";
11925 else if (edit_logmsg_only)
11926 histedit_cmd = "mesg";
11927 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11935 static const struct got_error *
11936 write_cmd_list(FILE *f, const char *branch_name,
11937 struct got_object_id_queue *commits)
11939 const struct got_error *err = NULL;
11943 struct got_object_qid *qid;
11945 qid = STAILQ_FIRST(commits);
11946 err = got_object_id_str(&id_str, &qid->id);
11951 "# Editing the history of branch '%s' starting at\n"
11953 "# Commits will be processed in order from top to "
11954 "bottom of this file.\n", branch_name, id_str);
11956 err = got_ferror(f, GOT_ERR_IO);
11960 n = fprintf(f, "# Available histedit commands:\n");
11962 err = got_ferror(f, GOT_ERR_IO);
11966 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11967 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11968 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11971 err = got_ferror(f, GOT_ERR_IO);
11980 static const struct got_error *
11981 histedit_syntax_error(int lineno)
11983 static char msg[42];
11986 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11988 if (ret < 0 || (size_t)ret >= sizeof(msg))
11989 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11991 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11994 static const struct got_error *
11995 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11996 char *logmsg, struct got_repository *repo)
11998 const struct got_error *err;
11999 struct got_commit_object *folded_commit = NULL;
12000 char *id_str, *folded_logmsg = NULL;
12002 err = got_object_id_str(&id_str, hle->commit_id);
12006 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
12010 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
12013 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
12014 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
12015 folded_logmsg) == -1) {
12016 err = got_error_from_errno("asprintf");
12020 got_object_commit_close(folded_commit);
12022 free(folded_logmsg);
12026 static struct got_histedit_list_entry *
12027 get_folded_commits(struct got_histedit_list_entry *hle)
12029 struct got_histedit_list_entry *prev, *folded = NULL;
12031 prev = TAILQ_PREV(hle, got_histedit_list, entry);
12032 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
12033 prev->cmd->code == GOT_HISTEDIT_DROP)) {
12034 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
12036 prev = TAILQ_PREV(prev, got_histedit_list, entry);
12042 static const struct got_error *
12043 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
12044 const char *editor, struct got_repository *repo)
12046 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
12047 char *logmsg = NULL, *new_msg = NULL;
12048 const struct got_error *err = NULL;
12049 struct got_commit_object *commit = NULL;
12052 struct got_histedit_list_entry *folded = NULL;
12054 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12058 folded = get_folded_commits(hle);
12060 while (folded != hle) {
12061 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
12062 folded = TAILQ_NEXT(folded, entry);
12065 err = append_folded_commit_msg(&new_msg, folded,
12071 folded = TAILQ_NEXT(folded, entry);
12075 err = got_object_id_str(&id_str, hle->commit_id);
12078 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
12081 logmsg_len = asprintf(&new_msg,
12082 "%s\n# original log message of commit %s: %s",
12083 logmsg ? logmsg : "", id_str, orig_logmsg);
12084 if (logmsg_len == -1) {
12085 err = got_error_from_errno("asprintf");
12091 err = got_object_id_str(&id_str, hle->commit_id);
12095 err = got_opentemp_named_fd(&logmsg_path, &fd,
12096 GOT_TMPDIR_STR "/got-logmsg", "");
12100 if (write(fd, logmsg, logmsg_len) == -1) {
12101 err = got_error_from_errno2("write", logmsg_path);
12104 if (close(fd) == -1) {
12105 err = got_error_from_errno2("close", logmsg_path);
12110 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
12113 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
12116 hle->logmsg = strdup(new_msg);
12117 if (hle->logmsg == NULL)
12118 err = got_error_from_errno("strdup");
12121 if (fd != -1 && close(fd) == -1 && err == NULL)
12122 err = got_error_from_errno2("close", logmsg_path);
12123 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
12124 err = got_error_from_errno2("unlink", logmsg_path);
12129 got_object_commit_close(commit);
12133 static const struct got_error *
12134 histedit_parse_list(struct got_histedit_list *histedit_cmds,
12135 FILE *f, struct got_repository *repo)
12137 const struct got_error *err = NULL;
12138 char *line = NULL, *p, *end;
12139 size_t i, linesize = 0;
12142 const struct got_histedit_cmd *cmd;
12143 struct got_object_id *commit_id = NULL;
12144 struct got_histedit_list_entry *hle = NULL;
12147 linelen = getline(&line, &linesize, f);
12148 if (linelen == -1) {
12149 const struct got_error *getline_err;
12152 getline_err = got_error_from_errno("getline");
12153 err = got_ferror(f, getline_err->code);
12158 while (isspace((unsigned char)p[0]))
12160 if (p[0] == '#' || p[0] == '\0')
12163 for (i = 0; i < nitems(got_histedit_cmds); i++) {
12164 cmd = &got_histedit_cmds[i];
12165 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
12166 isspace((unsigned char)p[strlen(cmd->name)])) {
12167 p += strlen(cmd->name);
12170 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
12175 if (i == nitems(got_histedit_cmds)) {
12176 err = histedit_syntax_error(lineno);
12179 while (isspace((unsigned char)p[0]))
12182 while (end[0] && !isspace((unsigned char)end[0]))
12185 err = got_object_resolve_id_str(&commit_id, repo, p);
12187 /* override error code */
12188 err = histedit_syntax_error(lineno);
12191 hle = malloc(sizeof(*hle));
12193 err = got_error_from_errno("malloc");
12197 hle->commit_id = commit_id;
12198 hle->logmsg = NULL;
12200 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12208 static const struct got_error *
12209 histedit_check_script(struct got_histedit_list *histedit_cmds,
12210 struct got_object_id_queue *commits, struct got_repository *repo)
12212 const struct got_error *err = NULL;
12213 struct got_object_qid *qid;
12214 struct got_histedit_list_entry *hle;
12215 static char msg[92];
12218 if (TAILQ_EMPTY(histedit_cmds))
12219 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12220 "histedit script contains no commands");
12221 if (STAILQ_EMPTY(commits))
12222 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12224 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12225 struct got_histedit_list_entry *hle2;
12226 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12229 if (got_object_id_cmp(hle->commit_id,
12230 hle2->commit_id) != 0)
12232 err = got_object_id_str(&id_str, hle->commit_id);
12235 snprintf(msg, sizeof(msg), "commit %s is listed "
12236 "more than once in histedit script", id_str);
12238 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12242 STAILQ_FOREACH(qid, commits, entry) {
12243 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12244 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12248 err = got_object_id_str(&id_str, &qid->id);
12251 snprintf(msg, sizeof(msg),
12252 "commit %s missing from histedit script", id_str);
12254 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12258 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12259 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12260 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12261 "last commit in histedit script cannot be folded");
12266 static const struct got_error *
12267 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12268 const char *editor, const char *path,
12269 struct got_object_id_queue *commits, struct got_repository *repo)
12271 const struct got_error *err = NULL;
12272 struct stat st, st2;
12273 struct timespec timeout;
12276 if (stat(path, &st) == -1) {
12277 err = got_error_from_errno2("stat", path);
12281 if (spawn_editor(editor, path) == -1) {
12282 err = got_error_from_errno("failed spawning editor");
12286 timeout.tv_sec = 0;
12287 timeout.tv_nsec = 1;
12288 nanosleep(&timeout, NULL);
12290 if (stat(path, &st2) == -1) {
12291 err = got_error_from_errno2("stat", path);
12295 if (st.st_size == st2.st_size &&
12296 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12297 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12298 "no changes made to histedit script, aborting");
12302 f = fopen(path, "re");
12304 err = got_error_from_errno("fopen");
12307 err = histedit_parse_list(histedit_cmds, f, repo);
12311 err = histedit_check_script(histedit_cmds, commits, repo);
12313 if (f && fclose(f) == EOF && err == NULL)
12314 err = got_error_from_errno("fclose");
12318 static const struct got_error *
12319 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12320 struct got_object_id_queue *, const char *, const char *, const char *,
12321 struct got_repository *);
12323 static const struct got_error *
12324 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12325 struct got_object_id_queue *commits, const char *branch_name,
12326 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12327 const char *editor, struct got_repository *repo)
12329 const struct got_error *err;
12333 err = got_opentemp_named(&path, &f, "got-histedit", "");
12337 err = write_cmd_list(f, branch_name, commits);
12341 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12342 fold_only, drop_only, edit_only, repo);
12346 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12348 err = histedit_parse_list(histedit_cmds, f, repo);
12350 if (fclose(f) == EOF) {
12351 err = got_error_from_errno("fclose");
12355 err = histedit_run_editor(histedit_cmds, editor, path,
12358 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12359 err->code != GOT_ERR_HISTEDIT_CMD)
12361 err = histedit_edit_list_retry(histedit_cmds, err,
12362 commits, editor, path, branch_name, repo);
12366 if (f && fclose(f) == EOF && err == NULL)
12367 err = got_error_from_errno("fclose");
12368 if (path && unlink(path) != 0 && err == NULL)
12369 err = got_error_from_errno2("unlink", path);
12374 static const struct got_error *
12375 histedit_save_list(struct got_histedit_list *histedit_cmds,
12376 struct got_worktree *worktree, struct got_repository *repo)
12378 const struct got_error *err = NULL;
12381 struct got_histedit_list_entry *hle;
12383 err = got_worktree_get_histedit_script_path(&path, worktree);
12387 f = fopen(path, "we");
12389 err = got_error_from_errno2("fopen", path);
12392 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12393 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12399 if (f && fclose(f) == EOF && err == NULL)
12400 err = got_error_from_errno("fclose");
12406 histedit_free_list(struct got_histedit_list *histedit_cmds)
12408 struct got_histedit_list_entry *hle;
12410 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12411 TAILQ_REMOVE(histedit_cmds, hle, entry);
12416 static const struct got_error *
12417 histedit_load_list(struct got_histedit_list *histedit_cmds,
12418 const char *path, struct got_repository *repo)
12420 const struct got_error *err = NULL;
12423 f = fopen(path, "re");
12425 err = got_error_from_errno2("fopen", path);
12429 err = histedit_parse_list(histedit_cmds, f, repo);
12431 if (f && fclose(f) == EOF && err == NULL)
12432 err = got_error_from_errno("fclose");
12436 static const struct got_error *
12437 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12438 const struct got_error *edit_err, struct got_object_id_queue *commits,
12439 const char *editor, const char *path, const char *branch_name,
12440 struct got_repository *repo)
12442 const struct got_error *err = NULL, *prev_err = edit_err;
12445 while (resp != 'c' && resp != 'r' && resp != 'a') {
12446 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12447 "or (a)bort: ", getprogname(), prev_err->msg);
12452 histedit_free_list(histedit_cmds);
12453 err = histedit_run_editor(histedit_cmds, editor, path,
12456 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12457 err->code != GOT_ERR_HISTEDIT_CMD)
12464 } else if (resp == 'r') {
12465 histedit_free_list(histedit_cmds);
12466 err = histedit_edit_script(histedit_cmds,
12467 commits, branch_name, 0, 0, 0, 0, editor, repo);
12469 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12470 err->code != GOT_ERR_HISTEDIT_CMD)
12477 } else if (resp == 'a') {
12478 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12481 printf("invalid response '%c'\n", resp);
12487 static const struct got_error *
12488 histedit_complete(struct got_worktree *worktree,
12489 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12490 struct got_reference *branch, struct got_repository *repo)
12492 printf("Switching work tree to %s\n",
12493 got_ref_get_symref_target(branch));
12494 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12498 static const struct got_error *
12499 show_histedit_progress(struct got_commit_object *commit,
12500 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12502 const struct got_error *err;
12503 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12505 err = got_object_id_str(&old_id_str, hle->commit_id);
12510 err = got_object_id_str(&new_id_str, new_id);
12515 old_id_str[12] = '\0';
12517 new_id_str[12] = '\0';
12520 logmsg = strdup(hle->logmsg);
12521 if (logmsg == NULL) {
12522 err = got_error_from_errno("strdup");
12525 trim_logmsg(logmsg, 42);
12527 err = get_short_logmsg(&logmsg, 42, commit);
12532 switch (hle->cmd->code) {
12533 case GOT_HISTEDIT_PICK:
12534 case GOT_HISTEDIT_EDIT:
12535 case GOT_HISTEDIT_MESG:
12536 printf("%s -> %s: %s\n", old_id_str,
12537 new_id_str ? new_id_str : "no-op change", logmsg);
12539 case GOT_HISTEDIT_DROP:
12540 case GOT_HISTEDIT_FOLD:
12541 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12553 static const struct got_error *
12554 histedit_commit(struct got_pathlist_head *merged_paths,
12555 struct got_worktree *worktree, struct got_fileindex *fileindex,
12556 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12557 const char *committer, int allow_conflict, const char *editor,
12558 struct got_repository *repo)
12560 const struct got_error *err;
12561 struct got_commit_object *commit;
12562 struct got_object_id *new_commit_id;
12564 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12565 && hle->logmsg == NULL) {
12566 err = histedit_edit_logmsg(hle, editor, repo);
12571 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12575 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12576 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12577 hle->logmsg, allow_conflict, repo);
12579 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12581 err = show_histedit_progress(commit, hle, NULL);
12583 err = show_histedit_progress(commit, hle, new_commit_id);
12584 free(new_commit_id);
12587 got_object_commit_close(commit);
12591 static const struct got_error *
12592 histedit_skip_commit(struct got_histedit_list_entry *hle,
12593 struct got_worktree *worktree, struct got_repository *repo)
12595 const struct got_error *error;
12596 struct got_commit_object *commit;
12598 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12603 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12607 error = show_histedit_progress(commit, hle, NULL);
12608 got_object_commit_close(commit);
12612 static const struct got_error *
12613 check_local_changes(void *arg, unsigned char status,
12614 unsigned char staged_status, const char *path,
12615 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12616 struct got_object_id *commit_id, int dirfd, const char *de_name)
12618 int *have_local_changes = arg;
12621 case GOT_STATUS_ADD:
12622 case GOT_STATUS_DELETE:
12623 case GOT_STATUS_MODIFY:
12624 case GOT_STATUS_CONFLICT:
12625 *have_local_changes = 1;
12626 return got_error(GOT_ERR_CANCELLED);
12631 switch (staged_status) {
12632 case GOT_STATUS_ADD:
12633 case GOT_STATUS_DELETE:
12634 case GOT_STATUS_MODIFY:
12635 *have_local_changes = 1;
12636 return got_error(GOT_ERR_CANCELLED);
12644 static const struct got_error *
12645 cmd_histedit(int argc, char *argv[])
12647 const struct got_error *error = NULL;
12648 struct got_worktree *worktree = NULL;
12649 struct got_fileindex *fileindex = NULL;
12650 struct got_repository *repo = NULL;
12651 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12652 struct got_reference *branch = NULL;
12653 struct got_reference *tmp_branch = NULL;
12654 struct got_object_id *resume_commit_id = NULL;
12655 struct got_object_id *base_commit_id = NULL;
12656 struct got_object_id *head_commit_id = NULL;
12657 struct got_commit_object *commit = NULL;
12658 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12659 struct got_update_progress_arg upa;
12660 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12661 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12662 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12663 const char *edit_script_path = NULL;
12664 char *editor = NULL;
12665 struct got_object_id_queue commits;
12666 struct got_pathlist_head merged_paths;
12667 const struct got_object_id_queue *parent_ids;
12668 struct got_object_qid *pid;
12669 struct got_histedit_list histedit_cmds;
12670 struct got_histedit_list_entry *hle;
12671 int *pack_fds = NULL;
12673 STAILQ_INIT(&commits);
12674 TAILQ_INIT(&histedit_cmds);
12675 TAILQ_INIT(&merged_paths);
12676 memset(&upa, 0, sizeof(upa));
12679 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12680 "unveil", NULL) == -1)
12684 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12690 allow_conflict = 1;
12702 edit_script_path = optarg;
12711 edit_logmsg_only = 1;
12714 delete_backups = 1;
12725 if (abort_edit && allow_conflict)
12726 option_conflict('a', 'C');
12727 if (abort_edit && continue_edit)
12728 option_conflict('a', 'c');
12729 if (edit_script_path && allow_conflict)
12730 option_conflict('F', 'C');
12731 if (edit_script_path && edit_logmsg_only)
12732 option_conflict('F', 'm');
12733 if (abort_edit && edit_logmsg_only)
12734 option_conflict('a', 'm');
12735 if (edit_logmsg_only && allow_conflict)
12736 option_conflict('m', 'C');
12737 if (continue_edit && edit_logmsg_only)
12738 option_conflict('c', 'm');
12739 if (abort_edit && fold_only)
12740 option_conflict('a', 'f');
12741 if (fold_only && allow_conflict)
12742 option_conflict('f', 'C');
12743 if (continue_edit && fold_only)
12744 option_conflict('c', 'f');
12745 if (fold_only && edit_logmsg_only)
12746 option_conflict('f', 'm');
12747 if (edit_script_path && fold_only)
12748 option_conflict('F', 'f');
12749 if (abort_edit && edit_only)
12750 option_conflict('a', 'e');
12751 if (continue_edit && edit_only)
12752 option_conflict('c', 'e');
12753 if (edit_only && edit_logmsg_only)
12754 option_conflict('e', 'm');
12755 if (edit_script_path && edit_only)
12756 option_conflict('F', 'e');
12757 if (fold_only && edit_only)
12758 option_conflict('f', 'e');
12759 if (drop_only && abort_edit)
12760 option_conflict('d', 'a');
12761 if (drop_only && allow_conflict)
12762 option_conflict('d', 'C');
12763 if (drop_only && continue_edit)
12764 option_conflict('d', 'c');
12765 if (drop_only && edit_logmsg_only)
12766 option_conflict('d', 'm');
12767 if (drop_only && edit_only)
12768 option_conflict('d', 'e');
12769 if (drop_only && edit_script_path)
12770 option_conflict('d', 'F');
12771 if (drop_only && fold_only)
12772 option_conflict('d', 'f');
12773 if (list_backups) {
12775 option_conflict('l', 'a');
12776 if (allow_conflict)
12777 option_conflict('l', 'C');
12779 option_conflict('l', 'c');
12780 if (edit_script_path)
12781 option_conflict('l', 'F');
12782 if (edit_logmsg_only)
12783 option_conflict('l', 'm');
12785 option_conflict('l', 'd');
12787 option_conflict('l', 'f');
12789 option_conflict('l', 'e');
12790 if (delete_backups)
12791 option_conflict('l', 'X');
12792 if (argc != 0 && argc != 1)
12794 } else if (delete_backups) {
12796 option_conflict('X', 'a');
12797 if (allow_conflict)
12798 option_conflict('X', 'C');
12800 option_conflict('X', 'c');
12802 option_conflict('X', 'd');
12803 if (edit_script_path)
12804 option_conflict('X', 'F');
12805 if (edit_logmsg_only)
12806 option_conflict('X', 'm');
12808 option_conflict('X', 'f');
12810 option_conflict('X', 'e');
12812 option_conflict('X', 'l');
12813 if (argc != 0 && argc != 1)
12815 } else if (allow_conflict && !continue_edit)
12816 errx(1, "-C option requires -c");
12817 else if (argc != 0)
12820 cwd = getcwd(NULL, 0);
12822 error = got_error_from_errno("getcwd");
12826 error = got_repo_pack_fds_open(&pack_fds);
12830 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12832 if (list_backups || delete_backups) {
12833 if (error->code != GOT_ERR_NOT_WORKTREE)
12836 if (error->code == GOT_ERR_NOT_WORKTREE)
12837 error = wrap_not_worktree_error(error,
12843 if (list_backups || delete_backups) {
12844 error = got_repo_open(&repo,
12845 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12849 error = apply_unveil(got_repo_get_path(repo), 0,
12850 worktree ? got_worktree_get_root_path(worktree) : NULL);
12853 error = process_backup_refs(
12854 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12855 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12856 goto done; /* nothing else to do */
12858 error = get_gitconfig_path(&gitconfig_path);
12861 error = got_repo_open(&repo,
12862 got_worktree_get_repo_path(worktree), gitconfig_path,
12866 error = get_editor(&editor);
12869 if (unveil(editor, "x") != 0) {
12870 error = got_error_from_errno2("unveil", editor);
12873 if (edit_script_path) {
12874 if (unveil(edit_script_path, "r") != 0) {
12875 error = got_error_from_errno2("unveil",
12880 error = apply_unveil(got_repo_get_path(repo), 0,
12881 got_worktree_get_root_path(worktree));
12886 if (worktree != NULL && !list_backups && !delete_backups) {
12887 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12892 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12895 if (rebase_in_progress) {
12896 error = got_error(GOT_ERR_REBASING);
12900 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12904 if (merge_in_progress) {
12905 error = got_error(GOT_ERR_MERGE_BUSY);
12909 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12913 if (edit_in_progress && edit_logmsg_only) {
12914 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12915 "histedit operation is in progress in this "
12916 "work tree and must be continued or aborted "
12917 "before the -m option can be used");
12920 if (edit_in_progress && drop_only) {
12921 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12922 "histedit operation is in progress in this "
12923 "work tree and must be continued or aborted "
12924 "before the -d option can be used");
12927 if (edit_in_progress && fold_only) {
12928 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12929 "histedit operation is in progress in this "
12930 "work tree and must be continued or aborted "
12931 "before the -f option can be used");
12934 if (edit_in_progress && edit_only) {
12935 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12936 "histedit operation is in progress in this "
12937 "work tree and must be continued or aborted "
12938 "before the -e option can be used");
12942 if (edit_in_progress && abort_edit) {
12943 error = got_worktree_histedit_continue(&resume_commit_id,
12944 &tmp_branch, &branch, &base_commit_id, &fileindex,
12948 printf("Switching work tree to %s\n",
12949 got_ref_get_symref_target(branch));
12950 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12951 branch, base_commit_id, abort_progress, &upa);
12954 printf("Histedit of %s aborted\n",
12955 got_ref_get_symref_target(branch));
12956 print_merge_progress_stats(&upa);
12957 goto done; /* nothing else to do */
12958 } else if (abort_edit) {
12959 error = got_error(GOT_ERR_NOT_HISTEDIT);
12963 error = get_author(&committer, repo, worktree);
12967 if (continue_edit) {
12970 if (!edit_in_progress) {
12971 error = got_error(GOT_ERR_NOT_HISTEDIT);
12975 error = got_worktree_get_histedit_script_path(&path, worktree);
12979 error = histedit_load_list(&histedit_cmds, path, repo);
12984 error = got_worktree_histedit_continue(&resume_commit_id,
12985 &tmp_branch, &branch, &base_commit_id, &fileindex,
12990 error = got_ref_resolve(&head_commit_id, repo, branch);
12994 error = got_object_open_as_commit(&commit, repo,
12998 parent_ids = got_object_commit_get_parent_ids(commit);
12999 pid = STAILQ_FIRST(parent_ids);
13001 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
13004 error = collect_commits(&commits, head_commit_id, &pid->id,
13005 base_commit_id, got_worktree_get_path_prefix(worktree),
13006 GOT_ERR_HISTEDIT_PATH, repo);
13007 got_object_commit_close(commit);
13012 if (edit_in_progress) {
13013 error = got_error(GOT_ERR_HISTEDIT_BUSY);
13017 error = got_ref_open(&branch, repo,
13018 got_worktree_get_head_ref_name(worktree), 0);
13022 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
13023 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
13024 "will not edit commit history of a branch outside "
13025 "the \"refs/heads/\" reference namespace");
13029 error = got_ref_resolve(&head_commit_id, repo, branch);
13030 got_ref_close(branch);
13035 error = got_object_open_as_commit(&commit, repo,
13039 parent_ids = got_object_commit_get_parent_ids(commit);
13040 pid = STAILQ_FIRST(parent_ids);
13042 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
13045 error = collect_commits(&commits, head_commit_id, &pid->id,
13046 got_worktree_get_base_commit_id(worktree),
13047 got_worktree_get_path_prefix(worktree),
13048 GOT_ERR_HISTEDIT_PATH, repo);
13049 got_object_commit_close(commit);
13054 if (STAILQ_EMPTY(&commits)) {
13055 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
13059 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
13060 &base_commit_id, &fileindex, worktree, repo);
13064 if (edit_script_path) {
13065 error = histedit_load_list(&histedit_cmds,
13066 edit_script_path, repo);
13068 got_worktree_histedit_abort(worktree, fileindex,
13069 repo, branch, base_commit_id,
13070 abort_progress, &upa);
13071 print_merge_progress_stats(&upa);
13075 const char *branch_name;
13076 branch_name = got_ref_get_symref_target(branch);
13077 if (strncmp(branch_name, "refs/heads/", 11) == 0)
13079 error = histedit_edit_script(&histedit_cmds, &commits,
13080 branch_name, edit_logmsg_only, fold_only,
13081 drop_only, edit_only, editor, repo);
13083 got_worktree_histedit_abort(worktree, fileindex,
13084 repo, branch, base_commit_id,
13085 abort_progress, &upa);
13086 print_merge_progress_stats(&upa);
13092 error = histedit_save_list(&histedit_cmds, worktree,
13095 got_worktree_histedit_abort(worktree, fileindex,
13096 repo, branch, base_commit_id,
13097 abort_progress, &upa);
13098 print_merge_progress_stats(&upa);
13104 error = histedit_check_script(&histedit_cmds, &commits, repo);
13108 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
13109 if (resume_commit_id) {
13110 if (got_object_id_cmp(hle->commit_id,
13111 resume_commit_id) != 0)
13114 resume_commit_id = NULL;
13115 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
13116 hle->cmd->code == GOT_HISTEDIT_FOLD) {
13117 error = histedit_skip_commit(hle, worktree,
13122 struct got_pathlist_head paths;
13123 int have_changes = 0;
13125 TAILQ_INIT(&paths);
13126 error = got_pathlist_append(&paths, "", NULL);
13129 error = got_worktree_status(worktree, &paths,
13130 repo, 0, check_local_changes, &have_changes,
13131 check_cancelled, NULL);
13132 got_pathlist_free(&paths,
13133 GOT_PATHLIST_FREE_NONE);
13135 if (error->code != GOT_ERR_CANCELLED)
13137 if (sigint_received || sigpipe_received)
13140 if (have_changes) {
13141 error = histedit_commit(NULL, worktree,
13142 fileindex, tmp_branch, hle,
13143 committer, allow_conflict, editor,
13148 error = got_object_open_as_commit(
13149 &commit, repo, hle->commit_id);
13152 error = show_histedit_progress(commit,
13154 got_object_commit_close(commit);
13163 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
13164 error = histedit_skip_commit(hle, worktree, repo);
13169 error = got_object_open_as_commit(&commit, repo,
13173 parent_ids = got_object_commit_get_parent_ids(commit);
13174 pid = STAILQ_FIRST(parent_ids);
13176 error = got_worktree_histedit_merge_files(&merged_paths,
13177 worktree, fileindex, &pid->id, hle->commit_id, repo,
13178 update_progress, &upa, check_cancelled, NULL);
13181 got_object_commit_close(commit);
13184 print_merge_progress_stats(&upa);
13185 if (upa.conflicts > 0 || upa.missing > 0 ||
13186 upa.not_deleted > 0 || upa.unversioned > 0) {
13187 if (upa.conflicts > 0) {
13188 error = show_rebase_merge_conflict(
13189 hle->commit_id, repo);
13193 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13197 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13199 error = got_object_id_str(&id_str, hle->commit_id);
13202 printf("Stopping histedit for amending commit %s\n",
13205 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13206 error = got_worktree_histedit_postpone(worktree,
13209 } else if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13210 error = histedit_skip_commit(hle, worktree, repo);
13214 } else if (hle->cmd->code == GOT_HISTEDIT_MESG) {
13215 error = histedit_edit_logmsg(hle, editor, repo);
13220 error = histedit_commit(&merged_paths, worktree, fileindex,
13221 tmp_branch, hle, committer, allow_conflict, editor, repo);
13222 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13227 if (upa.conflicts > 0 || upa.missing > 0 ||
13228 upa.not_deleted > 0 || upa.unversioned > 0) {
13229 error = got_worktree_histedit_postpone(worktree, fileindex);
13232 if (upa.conflicts > 0 && upa.missing == 0 &&
13233 upa.not_deleted == 0 && upa.unversioned == 0) {
13234 error = got_error_msg(GOT_ERR_CONFLICTS,
13235 "conflicts must be resolved before histedit "
13237 } else if (upa.conflicts > 0) {
13238 error = got_error_msg(GOT_ERR_CONFLICTS,
13239 "conflicts must be resolved before histedit "
13240 "can continue; changes destined for some "
13241 "files were not yet merged and should be "
13242 "merged manually if required before the "
13243 "histedit operation is continued");
13245 error = got_error_msg(GOT_ERR_CONFLICTS,
13246 "changes destined for some files were not "
13247 "yet merged and should be merged manually "
13248 "if required before the histedit operation "
13252 error = histedit_complete(worktree, fileindex, tmp_branch,
13258 free(gitconfig_path);
13259 got_object_id_queue_free(&commits);
13260 histedit_free_list(&histedit_cmds);
13261 free(head_commit_id);
13262 free(base_commit_id);
13263 free(resume_commit_id);
13265 got_object_commit_close(commit);
13267 got_ref_close(branch);
13269 got_ref_close(tmp_branch);
13271 got_worktree_close(worktree);
13273 const struct got_error *close_err = got_repo_close(repo);
13278 const struct got_error *pack_err =
13279 got_repo_pack_fds_close(pack_fds);
13287 usage_integrate(void)
13289 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13293 static const struct got_error *
13294 cmd_integrate(int argc, char *argv[])
13296 const struct got_error *error = NULL;
13297 struct got_repository *repo = NULL;
13298 struct got_worktree *worktree = NULL;
13299 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13300 const char *branch_arg = NULL;
13301 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13302 struct got_fileindex *fileindex = NULL;
13303 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13305 struct got_update_progress_arg upa;
13306 int *pack_fds = NULL;
13309 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13310 "unveil", NULL) == -1)
13314 while ((ch = getopt(argc, argv, "")) != -1) {
13327 branch_arg = argv[0];
13329 cwd = getcwd(NULL, 0);
13331 error = got_error_from_errno("getcwd");
13335 error = got_repo_pack_fds_open(&pack_fds);
13339 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13341 if (error->code == GOT_ERR_NOT_WORKTREE)
13342 error = wrap_not_worktree_error(error, "integrate",
13347 error = check_rebase_or_histedit_in_progress(worktree);
13351 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13356 error = apply_unveil(got_repo_get_path(repo), 0,
13357 got_worktree_get_root_path(worktree));
13361 error = check_merge_in_progress(worktree, repo);
13365 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13366 error = got_error_from_errno("asprintf");
13370 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13371 &base_branch_ref, worktree, refname, repo);
13375 refname = strdup(got_ref_get_name(branch_ref));
13376 if (refname == NULL) {
13377 error = got_error_from_errno("strdup");
13378 got_worktree_integrate_abort(worktree, fileindex, repo,
13379 branch_ref, base_branch_ref);
13382 base_refname = strdup(got_ref_get_name(base_branch_ref));
13383 if (base_refname == NULL) {
13384 error = got_error_from_errno("strdup");
13385 got_worktree_integrate_abort(worktree, fileindex, repo,
13386 branch_ref, base_branch_ref);
13389 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13390 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13391 got_worktree_integrate_abort(worktree, fileindex, repo,
13392 branch_ref, base_branch_ref);
13396 error = got_ref_resolve(&commit_id, repo, branch_ref);
13400 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13404 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13405 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13406 "specified branch has already been integrated");
13407 got_worktree_integrate_abort(worktree, fileindex, repo,
13408 branch_ref, base_branch_ref);
13412 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13414 if (error->code == GOT_ERR_ANCESTRY)
13415 error = got_error(GOT_ERR_REBASE_REQUIRED);
13416 got_worktree_integrate_abort(worktree, fileindex, repo,
13417 branch_ref, base_branch_ref);
13421 memset(&upa, 0, sizeof(upa));
13422 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13423 branch_ref, base_branch_ref, update_progress, &upa,
13424 check_cancelled, NULL);
13428 printf("Integrated %s into %s\n", refname, base_refname);
13429 print_update_progress_stats(&upa);
13432 const struct got_error *close_err = got_repo_close(repo);
13437 got_worktree_close(worktree);
13439 const struct got_error *pack_err =
13440 got_repo_pack_fds_close(pack_fds);
13445 free(base_commit_id);
13448 free(base_refname);
13455 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13459 static const struct got_error *
13460 cmd_merge(int argc, char *argv[])
13462 const struct got_error *error = NULL;
13463 struct got_worktree *worktree = NULL;
13464 struct got_repository *repo = NULL;
13465 struct got_fileindex *fileindex = NULL;
13466 char *cwd = NULL, *id_str = NULL, *author = NULL;
13467 char *gitconfig_path = NULL;
13468 struct got_reference *branch = NULL, *wt_branch = NULL;
13469 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13470 struct got_object_id *wt_branch_tip = NULL;
13471 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13472 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13473 struct got_update_progress_arg upa;
13474 struct got_object_id *merge_commit_id = NULL;
13475 char *branch_name = NULL;
13476 int *pack_fds = NULL;
13478 memset(&upa, 0, sizeof(upa));
13481 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13482 "unveil", NULL) == -1)
13486 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13492 allow_conflict = 1;
13495 continue_merge = 1;
13498 prefer_fast_forward = 0;
13501 interrupt_merge = 1;
13513 if (continue_merge)
13514 option_conflict('a', 'c');
13515 if (!prefer_fast_forward)
13516 option_conflict('a', 'M');
13517 if (interrupt_merge)
13518 option_conflict('a', 'n');
13519 } else if (continue_merge) {
13520 if (!prefer_fast_forward)
13521 option_conflict('c', 'M');
13522 if (interrupt_merge)
13523 option_conflict('c', 'n');
13525 if (allow_conflict) {
13526 if (!continue_merge)
13527 errx(1, "-C option requires -c");
13529 if (abort_merge || continue_merge) {
13532 } else if (argc != 1)
13535 cwd = getcwd(NULL, 0);
13537 error = got_error_from_errno("getcwd");
13541 error = got_repo_pack_fds_open(&pack_fds);
13545 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13547 if (error->code == GOT_ERR_NOT_WORKTREE)
13548 error = wrap_not_worktree_error(error,
13553 error = get_gitconfig_path(&gitconfig_path);
13556 error = got_repo_open(&repo,
13557 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13558 gitconfig_path, pack_fds);
13562 if (worktree != NULL) {
13563 error = worktree_has_logmsg_ref("merge", worktree, repo);
13568 error = apply_unveil(got_repo_get_path(repo), 0,
13569 worktree ? got_worktree_get_root_path(worktree) : NULL);
13573 error = check_rebase_or_histedit_in_progress(worktree);
13577 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13582 if (merge_in_progress && !(abort_merge || continue_merge)) {
13583 error = got_error(GOT_ERR_MERGE_BUSY);
13587 if (!merge_in_progress && (abort_merge || continue_merge)) {
13588 error = got_error(GOT_ERR_NOT_MERGING);
13593 error = got_worktree_merge_continue(&branch_name,
13594 &branch_tip, &fileindex, worktree, repo);
13597 error = got_worktree_merge_abort(worktree, fileindex, repo,
13598 abort_progress, &upa);
13601 printf("Merge of %s aborted\n", branch_name);
13602 goto done; /* nothing else to do */
13605 if (strncmp(got_worktree_get_head_ref_name(worktree),
13606 "refs/heads/", 11) != 0) {
13607 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13608 "work tree's current branch %s is outside the "
13609 "\"refs/heads/\" reference namespace; "
13610 "update -b required",
13611 got_worktree_get_head_ref_name(worktree));
13615 error = get_author(&author, repo, worktree);
13619 error = got_ref_open(&wt_branch, repo,
13620 got_worktree_get_head_ref_name(worktree), 0);
13623 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13627 if (continue_merge) {
13628 struct got_object_id *base_commit_id;
13629 base_commit_id = got_worktree_get_base_commit_id(worktree);
13630 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13631 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13634 error = got_worktree_merge_continue(&branch_name,
13635 &branch_tip, &fileindex, worktree, repo);
13639 error = got_ref_open(&branch, repo, argv[0], 0);
13642 branch_name = strdup(got_ref_get_name(branch));
13643 if (branch_name == NULL) {
13644 error = got_error_from_errno("strdup");
13647 error = got_ref_resolve(&branch_tip, repo, branch);
13652 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13653 wt_branch_tip, branch_tip, 0, 0, repo,
13654 check_cancelled, NULL);
13655 if (error && error->code != GOT_ERR_ANCESTRY)
13658 if (!continue_merge) {
13659 error = check_path_prefix(wt_branch_tip, branch_tip,
13660 got_worktree_get_path_prefix(worktree),
13661 GOT_ERR_MERGE_PATH, repo);
13664 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13667 if (prefer_fast_forward && yca_id &&
13668 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13669 struct got_pathlist_head paths;
13670 if (interrupt_merge) {
13671 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13672 "there are no changes to merge since %s "
13673 "is already based on %s; merge cannot be "
13674 "interrupted for amending; -n",
13675 branch_name, got_ref_get_name(wt_branch));
13678 printf("Forwarding %s to %s\n",
13679 got_ref_get_name(wt_branch), branch_name);
13680 error = got_ref_change_ref(wt_branch, branch_tip);
13683 error = got_ref_write(wt_branch, repo);
13686 error = got_worktree_set_base_commit_id(worktree, repo,
13690 TAILQ_INIT(&paths);
13691 error = got_pathlist_append(&paths, "", NULL);
13694 error = got_worktree_checkout_files(worktree,
13695 &paths, repo, update_progress, &upa,
13696 check_cancelled, NULL);
13697 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13700 if (upa.did_something) {
13702 error = got_object_id_str(&id_str, branch_tip);
13705 printf("Updated to commit %s\n", id_str);
13708 printf("Already up-to-date\n");
13709 print_update_progress_stats(&upa);
13712 error = got_worktree_merge_write_refs(worktree, branch, repo);
13716 error = got_worktree_merge_branch(worktree, fileindex,
13717 yca_id, branch_tip, repo, update_progress, &upa,
13718 check_cancelled, NULL);
13721 print_merge_progress_stats(&upa);
13722 if (!upa.did_something) {
13723 error = got_worktree_merge_abort(worktree, fileindex,
13724 repo, abort_progress, &upa);
13727 printf("Already up-to-date\n");
13732 if (interrupt_merge) {
13733 error = got_worktree_merge_postpone(worktree, fileindex);
13736 printf("Merge of %s interrupted on request\n", branch_name);
13737 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13738 upa.not_deleted > 0 || upa.unversioned > 0) {
13739 error = got_worktree_merge_postpone(worktree, fileindex);
13742 if (upa.conflicts > 0 && upa.missing == 0 &&
13743 upa.not_deleted == 0 && upa.unversioned == 0) {
13744 error = got_error_msg(GOT_ERR_CONFLICTS,
13745 "conflicts must be resolved before merging "
13747 } else if (upa.conflicts > 0) {
13748 error = got_error_msg(GOT_ERR_CONFLICTS,
13749 "conflicts must be resolved before merging "
13750 "can continue; changes destined for some "
13751 "files were not yet merged and "
13752 "should be merged manually if required before the "
13753 "merge operation is continued");
13755 error = got_error_msg(GOT_ERR_CONFLICTS,
13756 "changes destined for some "
13757 "files were not yet merged and should be "
13758 "merged manually if required before the "
13759 "merge operation is continued");
13763 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13764 fileindex, author, NULL, 1, branch_tip, branch_name,
13765 allow_conflict, repo, continue_merge ? print_status : NULL,
13769 error = got_worktree_merge_complete(worktree, fileindex, repo);
13772 error = got_object_id_str(&id_str, merge_commit_id);
13775 printf("Merged %s into %s: %s\n", branch_name,
13776 got_worktree_get_head_ref_name(worktree),
13781 free(gitconfig_path);
13783 free(merge_commit_id);
13789 got_ref_close(branch);
13791 got_ref_close(wt_branch);
13793 got_worktree_close(worktree);
13795 const struct got_error *close_err = got_repo_close(repo);
13800 const struct got_error *pack_err =
13801 got_repo_pack_fds_close(pack_fds);
13811 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13812 "[path ...]\n", getprogname());
13816 static const struct got_error *
13817 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13818 const char *path, struct got_object_id *blob_id,
13819 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13820 int dirfd, const char *de_name)
13822 const struct got_error *err = NULL;
13823 char *id_str = NULL;
13825 if (staged_status != GOT_STATUS_ADD &&
13826 staged_status != GOT_STATUS_MODIFY &&
13827 staged_status != GOT_STATUS_DELETE)
13830 if (staged_status == GOT_STATUS_ADD ||
13831 staged_status == GOT_STATUS_MODIFY)
13832 err = got_object_id_str(&id_str, staged_blob_id);
13834 err = got_object_id_str(&id_str, blob_id);
13838 printf("%s %c %s\n", id_str, staged_status, path);
13843 static const struct got_error *
13844 cmd_stage(int argc, char *argv[])
13846 const struct got_error *error = NULL;
13847 struct got_repository *repo = NULL;
13848 struct got_worktree *worktree = NULL;
13850 struct got_pathlist_head paths;
13851 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13852 FILE *patch_script_file = NULL;
13853 const char *patch_script_path = NULL;
13854 struct choose_patch_arg cpa;
13855 int *pack_fds = NULL;
13857 TAILQ_INIT(&paths);
13860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13861 "unveil", NULL) == -1)
13865 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13868 patch_script_path = optarg;
13877 allow_bad_symlinks = 1;
13888 if (list_stage && (pflag || patch_script_path))
13889 errx(1, "-l option cannot be used with other options");
13890 if (patch_script_path && !pflag)
13891 errx(1, "-F option can only be used together with -p option");
13893 cwd = getcwd(NULL, 0);
13895 error = got_error_from_errno("getcwd");
13899 error = got_repo_pack_fds_open(&pack_fds);
13903 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13905 if (error->code == GOT_ERR_NOT_WORKTREE)
13906 error = wrap_not_worktree_error(error, "stage", cwd);
13910 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13915 if (patch_script_path) {
13916 patch_script_file = fopen(patch_script_path, "re");
13917 if (patch_script_file == NULL) {
13918 error = got_error_from_errno2("fopen",
13919 patch_script_path);
13923 error = apply_unveil(got_repo_get_path(repo), 0,
13924 got_worktree_get_root_path(worktree));
13928 error = check_merge_in_progress(worktree, repo);
13932 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13937 error = got_worktree_status(worktree, &paths, repo, 0,
13938 print_stage, NULL, check_cancelled, NULL);
13940 cpa.patch_script_file = patch_script_file;
13941 cpa.action = "stage";
13942 error = got_worktree_stage(worktree, &paths,
13943 pflag ? NULL : print_status, NULL,
13944 pflag ? choose_patch : NULL, &cpa,
13945 allow_bad_symlinks, repo);
13948 if (patch_script_file && fclose(patch_script_file) == EOF &&
13950 error = got_error_from_errno2("fclose", patch_script_path);
13952 const struct got_error *close_err = got_repo_close(repo);
13957 got_worktree_close(worktree);
13959 const struct got_error *pack_err =
13960 got_repo_pack_fds_close(pack_fds);
13964 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13970 usage_unstage(void)
13972 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13973 "[path ...]\n", getprogname());
13978 static const struct got_error *
13979 cmd_unstage(int argc, char *argv[])
13981 const struct got_error *error = NULL;
13982 struct got_repository *repo = NULL;
13983 struct got_worktree *worktree = NULL;
13985 struct got_pathlist_head paths;
13987 struct got_update_progress_arg upa;
13988 FILE *patch_script_file = NULL;
13989 const char *patch_script_path = NULL;
13990 struct choose_patch_arg cpa;
13991 int *pack_fds = NULL;
13993 TAILQ_INIT(&paths);
13996 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13997 "unveil", NULL) == -1)
14001 while ((ch = getopt(argc, argv, "F:p")) != -1) {
14004 patch_script_path = optarg;
14018 if (patch_script_path && !pflag)
14019 errx(1, "-F option can only be used together with -p option");
14021 cwd = getcwd(NULL, 0);
14023 error = got_error_from_errno("getcwd");
14027 error = got_repo_pack_fds_open(&pack_fds);
14031 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14033 if (error->code == GOT_ERR_NOT_WORKTREE)
14034 error = wrap_not_worktree_error(error, "unstage", cwd);
14038 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
14043 if (patch_script_path) {
14044 patch_script_file = fopen(patch_script_path, "re");
14045 if (patch_script_file == NULL) {
14046 error = got_error_from_errno2("fopen",
14047 patch_script_path);
14052 error = apply_unveil(got_repo_get_path(repo), 0,
14053 got_worktree_get_root_path(worktree));
14057 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
14061 cpa.patch_script_file = patch_script_file;
14062 cpa.action = "unstage";
14063 memset(&upa, 0, sizeof(upa));
14064 error = got_worktree_unstage(worktree, &paths, update_progress,
14065 &upa, pflag ? choose_patch : NULL, &cpa, repo);
14067 print_merge_progress_stats(&upa);
14069 if (patch_script_file && fclose(patch_script_file) == EOF &&
14071 error = got_error_from_errno2("fclose", patch_script_path);
14073 const struct got_error *close_err = got_repo_close(repo);
14078 got_worktree_close(worktree);
14080 const struct got_error *pack_err =
14081 got_repo_pack_fds_close(pack_fds);
14085 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14093 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14094 "arg ...\n", getprogname());
14098 static const struct got_error *
14099 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14101 const struct got_error *err;
14102 struct got_blob_object *blob;
14105 fd = got_opentempfd();
14107 return got_error_from_errno("got_opentempfd");
14109 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
14113 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
14115 if (fd != -1 && close(fd) == -1 && err == NULL)
14116 err = got_error_from_errno("close");
14118 got_object_blob_close(blob);
14122 static const struct got_error *
14123 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14125 const struct got_error *err;
14126 struct got_tree_object *tree;
14129 err = got_object_open_as_tree(&tree, repo, id);
14133 nentries = got_object_tree_get_nentries(tree);
14134 for (i = 0; i < nentries; i++) {
14135 struct got_tree_entry *te;
14137 if (sigint_received || sigpipe_received)
14139 te = got_object_tree_get_entry(tree, i);
14140 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
14143 fprintf(outfile, "%s %.7o %s\n", id_str,
14144 got_tree_entry_get_mode(te),
14145 got_tree_entry_get_name(te));
14149 got_object_tree_close(tree);
14153 static const struct got_error *
14154 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14156 const struct got_error *err;
14157 struct got_commit_object *commit;
14158 const struct got_object_id_queue *parent_ids;
14159 struct got_object_qid *pid;
14160 char *id_str = NULL;
14161 const char *logmsg = NULL;
14164 err = got_object_open_as_commit(&commit, repo, id);
14168 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
14172 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
14173 parent_ids = got_object_commit_get_parent_ids(commit);
14174 fprintf(outfile, "numparents %d\n",
14175 got_object_commit_get_nparents(commit));
14176 STAILQ_FOREACH(pid, parent_ids, entry) {
14178 err = got_object_id_str(&pid_str, &pid->id);
14181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
14184 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14185 got_object_commit_get_author_gmtoff(commit));
14186 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14187 got_object_commit_get_author(commit),
14188 (long long)got_object_commit_get_author_time(commit),
14191 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14192 got_object_commit_get_committer_gmtoff(commit));
14193 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14194 got_object_commit_get_committer(commit),
14195 (long long)got_object_commit_get_committer_time(commit),
14198 logmsg = got_object_commit_get_logmsg_raw(commit);
14199 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14200 fprintf(outfile, "%s", logmsg);
14203 got_object_commit_close(commit);
14207 static const struct got_error *
14208 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14210 const struct got_error *err;
14211 struct got_tag_object *tag;
14212 char *id_str = NULL;
14213 const char *tagmsg = NULL;
14216 err = got_object_open_as_tag(&tag, repo, id);
14220 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14224 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14226 switch (got_object_tag_get_object_type(tag)) {
14227 case GOT_OBJ_TYPE_BLOB:
14228 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14229 GOT_OBJ_LABEL_BLOB);
14231 case GOT_OBJ_TYPE_TREE:
14232 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14233 GOT_OBJ_LABEL_TREE);
14235 case GOT_OBJ_TYPE_COMMIT:
14236 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14237 GOT_OBJ_LABEL_COMMIT);
14239 case GOT_OBJ_TYPE_TAG:
14240 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14241 GOT_OBJ_LABEL_TAG);
14247 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14248 got_object_tag_get_name(tag));
14250 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14251 got_object_tag_get_tagger_gmtoff(tag));
14252 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14253 got_object_tag_get_tagger(tag),
14254 (long long)got_object_tag_get_tagger_time(tag),
14257 tagmsg = got_object_tag_get_message(tag);
14258 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14259 fprintf(outfile, "%s", tagmsg);
14262 got_object_tag_close(tag);
14266 static const struct got_error *
14267 cmd_cat(int argc, char *argv[])
14269 const struct got_error *error;
14270 struct got_repository *repo = NULL;
14271 struct got_worktree *worktree = NULL;
14272 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14273 char *keyword_idstr = NULL;
14274 const char *commit_id_str = NULL;
14275 struct got_object_id *id = NULL, *commit_id = NULL;
14276 struct got_commit_object *commit = NULL;
14277 int ch, obj_type, i, force_path = 0;
14278 struct got_reflist_head refs;
14279 int *pack_fds = NULL;
14284 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14289 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14292 commit_id_str = optarg;
14298 repo_path = realpath(optarg, NULL);
14299 if (repo_path == NULL)
14300 return got_error_from_errno2("realpath",
14302 got_path_strip_trailing_slashes(repo_path);
14313 cwd = getcwd(NULL, 0);
14315 error = got_error_from_errno("getcwd");
14319 error = got_repo_pack_fds_open(&pack_fds);
14323 if (repo_path == NULL) {
14324 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14328 repo_path = strdup(
14329 got_worktree_get_repo_path(worktree));
14330 if (repo_path == NULL) {
14331 error = got_error_from_errno("strdup");
14335 if (commit_id_str == NULL) {
14336 /* Release work tree lock. */
14337 got_worktree_close(worktree);
14343 if (repo_path == NULL) {
14344 repo_path = strdup(cwd);
14345 if (repo_path == NULL)
14346 return got_error_from_errno("strdup");
14349 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14354 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14358 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14362 if (commit_id_str != NULL) {
14363 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14367 if (keyword_idstr != NULL)
14368 commit_id_str = keyword_idstr;
14369 if (worktree != NULL) {
14370 got_worktree_close(worktree);
14374 commit_id_str = GOT_REF_HEAD;
14375 error = got_repo_match_object_id(&commit_id, NULL,
14376 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14380 error = got_object_open_as_commit(&commit, repo, commit_id);
14384 for (i = 0; i < argc; i++) {
14386 error = got_object_id_by_path(&id, repo, commit,
14391 error = got_repo_match_object_id(&id, &label, argv[i],
14392 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14395 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14396 error->code != GOT_ERR_NOT_REF)
14398 error = got_object_id_by_path(&id, repo,
14405 error = got_object_get_type(&obj_type, repo, id);
14409 switch (obj_type) {
14410 case GOT_OBJ_TYPE_BLOB:
14411 error = cat_blob(id, repo, stdout);
14413 case GOT_OBJ_TYPE_TREE:
14414 error = cat_tree(id, repo, stdout);
14416 case GOT_OBJ_TYPE_COMMIT:
14417 error = cat_commit(id, repo, stdout);
14419 case GOT_OBJ_TYPE_TAG:
14420 error = cat_tag(id, repo, stdout);
14423 error = got_error(GOT_ERR_OBJ_TYPE);
14437 free(keyword_idstr);
14439 got_object_commit_close(commit);
14441 got_worktree_close(worktree);
14443 const struct got_error *close_err = got_repo_close(repo);
14448 const struct got_error *pack_err =
14449 got_repo_pack_fds_close(pack_fds);
14454 got_ref_list_free(&refs);
14461 fprintf(stderr, "usage: %s info [path ...]\n",
14466 static const struct got_error *
14467 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14468 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14469 struct got_object_id *commit_id)
14471 const struct got_error *err = NULL;
14472 char *id_str = NULL;
14474 struct tm mytm, *tm;
14475 struct got_pathlist_head *paths = arg;
14476 struct got_pathlist_entry *pe;
14479 * Clear error indication from any of the path arguments which
14480 * would cause this file index entry to be displayed.
14482 TAILQ_FOREACH(pe, paths, entry) {
14483 if (got_path_cmp(path, pe->path, strlen(path),
14484 pe->path_len) == 0 ||
14485 got_path_is_child(path, pe->path, pe->path_len))
14486 pe->data = NULL; /* no error */
14489 printf(GOT_COMMIT_SEP_STR);
14491 printf("symlink: %s\n", path);
14492 else if (S_ISREG(mode)) {
14493 printf("file: %s\n", path);
14494 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14495 } else if (S_ISDIR(mode))
14496 printf("directory: %s\n", path);
14498 printf("something: %s\n", path);
14500 tm = localtime_r(&mtime, &mytm);
14503 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14504 return got_error(GOT_ERR_NO_SPACE);
14505 printf("timestamp: %s\n", datebuf);
14508 err = got_object_id_str(&id_str, blob_id);
14511 printf("based on blob: %s\n", id_str);
14515 if (staged_blob_id) {
14516 err = got_object_id_str(&id_str, staged_blob_id);
14519 printf("based on staged blob: %s\n", id_str);
14524 err = got_object_id_str(&id_str, commit_id);
14527 printf("based on commit: %s\n", id_str);
14534 static const struct got_error *
14535 cmd_info(int argc, char *argv[])
14537 const struct got_error *error = NULL;
14538 struct got_worktree *worktree = NULL;
14539 char *cwd = NULL, *id_str = NULL;
14540 struct got_pathlist_head paths;
14541 char *uuidstr = NULL;
14542 int ch, show_files = 0;
14544 TAILQ_INIT(&paths);
14547 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14552 while ((ch = getopt(argc, argv, "")) != -1) {
14563 cwd = getcwd(NULL, 0);
14565 error = got_error_from_errno("getcwd");
14569 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14571 if (error->code == GOT_ERR_NOT_WORKTREE)
14572 error = wrap_not_worktree_error(error, "info", cwd);
14577 /* Remove "wpath cpath proc exec sendfd" promises. */
14578 if (pledge("stdio rpath flock unveil", NULL) == -1)
14581 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14586 error = get_worktree_paths_from_argv(&paths, argc, argv,
14593 error = got_object_id_str(&id_str,
14594 got_worktree_get_base_commit_id(worktree));
14598 error = got_worktree_get_uuid(&uuidstr, worktree);
14602 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14603 printf("work tree base commit: %s\n", id_str);
14604 printf("work tree path prefix: %s\n",
14605 got_worktree_get_path_prefix(worktree));
14606 printf("work tree branch reference: %s\n",
14607 got_worktree_get_head_ref_name(worktree));
14608 printf("work tree UUID: %s\n", uuidstr);
14609 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14612 struct got_pathlist_entry *pe;
14613 TAILQ_FOREACH(pe, &paths, entry) {
14614 if (pe->path_len == 0)
14617 * Assume this path will fail. This will be corrected
14618 * in print_path_info() in case the path does suceeed.
14620 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14622 error = got_worktree_path_info(worktree, &paths,
14623 print_path_info, &paths, check_cancelled, NULL);
14626 TAILQ_FOREACH(pe, &paths, entry) {
14627 if (pe->data != NULL) {
14628 const struct got_error *perr;
14631 error = got_error_fmt(perr->code, "%s",
14639 got_worktree_close(worktree);
14640 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);