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 error = get_author(&author, repo, NULL);
842 * Don't let the user create a branch name with a leading '-'.
843 * While technically a valid reference name, this case is usually
844 * an unintended typo.
846 if (branch_name && branch_name[0] == '-')
847 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
849 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
850 if (error && error->code != GOT_ERR_NOT_REF)
854 n = strlcat(refname, branch_name, sizeof(refname));
855 else if (head_ref && got_ref_is_symbolic(head_ref))
856 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
859 n = strlcat(refname, "main", sizeof(refname));
860 if (n >= sizeof(refname)) {
861 error = got_error(GOT_ERR_NO_SPACE);
865 error = got_ref_open(&branch_ref, repo, refname, 0);
867 if (error->code != GOT_ERR_NOT_REF)
870 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
871 "import target branch already exists");
875 path_dir = realpath(argv[0], NULL);
876 if (path_dir == NULL) {
877 error = got_error_from_errno2("realpath", argv[0]);
880 got_path_strip_trailing_slashes(path_dir);
883 * unveil(2) traverses exec(2); if an editor is used we have
884 * to apply unveil after the log message has been written.
886 if (logmsg == NULL || *logmsg == '\0') {
887 error = get_editor(&editor);
891 error = collect_import_msg(&logmsg, &logmsg_path, editor,
894 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
901 if (unveil(path_dir, "r") != 0) {
902 error = got_error_from_errno2("unveil", path_dir);
908 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
915 error = got_repo_import(&new_commit_id, path_dir, logmsg,
916 author, &ignores, repo, import_progress, NULL);
923 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
930 error = got_ref_write(branch_ref, repo);
937 error = got_object_id_str(&id_str, new_commit_id);
944 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
946 if (error->code != GOT_ERR_NOT_REF) {
952 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
960 error = got_ref_write(head_ref, repo);
968 printf("Created branch %s with commit %s\n",
969 got_ref_get_name(branch_ref), id_str);
972 const struct got_error *pack_err =
973 got_repo_pack_fds_close(pack_fds);
978 const struct got_error *close_err = got_repo_close(repo);
982 if (preserve_logmsg) {
983 fprintf(stderr, "%s: log message preserved in %s\n",
984 getprogname(), logmsg_path);
985 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
986 error = got_error_from_errno2("unlink", logmsg_path);
994 free(gitconfig_path);
996 got_ref_close(branch_ref);
998 got_ref_close(head_ref);
1005 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1006 "repository-URL [directory]\n", getprogname());
1010 struct got_fetch_progress_arg {
1011 char last_scaled_size[FMT_SCALED_STRSIZE];
1013 int last_p_resolved;
1016 struct got_repository *repo;
1019 int configs_created;
1021 struct got_pathlist_head *symrefs;
1022 struct got_pathlist_head *wanted_branches;
1023 struct got_pathlist_head *wanted_refs;
1027 const char *remote_repo_path;
1028 const char *git_url;
1029 int fetch_all_branches;
1030 int mirror_references;
1034 /* XXX forward declaration */
1035 static const struct got_error *
1036 create_config_files(const char *proto, const char *host, const char *port,
1037 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1038 int mirror_references, struct got_pathlist_head *symrefs,
1039 struct got_pathlist_head *wanted_branches,
1040 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1042 static const struct got_error *
1043 fetch_progress(void *arg, const char *message, off_t packfile_size,
1044 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1046 const struct got_error *err = NULL;
1047 struct got_fetch_progress_arg *a = arg;
1048 char scaled_size[FMT_SCALED_STRSIZE];
1049 int p_indexed, p_resolved;
1050 int print_size = 0, print_indexed = 0, print_resolved = 0;
1053 * In order to allow a failed clone to be resumed with 'got fetch'
1054 * we try to create configuration files as soon as possible.
1055 * Once the server has sent information about its default branch
1056 * we have all required information.
1058 if (a->create_configs && !a->configs_created &&
1059 !TAILQ_EMPTY(a->config_info.symrefs)) {
1060 err = create_config_files(a->config_info.proto,
1061 a->config_info.host, a->config_info.port,
1062 a->config_info.remote_repo_path,
1063 a->config_info.git_url,
1064 a->config_info.fetch_all_branches,
1065 a->config_info.mirror_references,
1066 a->config_info.symrefs,
1067 a->config_info.wanted_branches,
1068 a->config_info.wanted_refs, a->repo);
1071 a->configs_created = 1;
1074 if (a->verbosity < 0)
1077 if (message && message[0] != '\0') {
1078 printf("\rserver: %s", message);
1083 if (packfile_size > 0 || nobj_indexed > 0) {
1084 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1085 (a->last_scaled_size[0] == '\0' ||
1086 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1088 if (strlcpy(a->last_scaled_size, scaled_size,
1089 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1090 return got_error(GOT_ERR_NO_SPACE);
1092 if (nobj_indexed > 0) {
1093 p_indexed = (nobj_indexed * 100) / nobj_total;
1094 if (p_indexed != a->last_p_indexed) {
1095 a->last_p_indexed = p_indexed;
1100 if (nobj_resolved > 0) {
1101 p_resolved = (nobj_resolved * 100) /
1102 (nobj_total - nobj_loose);
1103 if (p_resolved != a->last_p_resolved) {
1104 a->last_p_resolved = p_resolved;
1112 if (print_size || print_indexed || print_resolved)
1115 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1117 printf("; indexing %d%%", p_indexed);
1119 printf("; resolving deltas %d%%", p_resolved);
1120 if (print_size || print_indexed || print_resolved)
1126 static const struct got_error *
1127 create_symref(const char *refname, struct got_reference *target_ref,
1128 int verbosity, struct got_repository *repo)
1130 const struct got_error *err;
1131 struct got_reference *head_symref;
1133 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1137 err = got_ref_write(head_symref, repo);
1138 if (err == NULL && verbosity > 0) {
1139 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1140 got_ref_get_name(target_ref));
1142 got_ref_close(head_symref);
1146 static const struct got_error *
1147 list_remote_refs(struct got_pathlist_head *symrefs,
1148 struct got_pathlist_head *refs)
1150 const struct got_error *err;
1151 struct got_pathlist_entry *pe;
1153 TAILQ_FOREACH(pe, symrefs, entry) {
1154 const char *refname = pe->path;
1155 const char *targetref = pe->data;
1157 printf("%s: %s\n", refname, targetref);
1160 TAILQ_FOREACH(pe, refs, entry) {
1161 const char *refname = pe->path;
1162 struct got_object_id *id = pe->data;
1165 err = got_object_id_str(&id_str, id);
1168 printf("%s: %s\n", refname, id_str);
1175 static const struct got_error *
1176 create_ref(const char *refname, struct got_object_id *id,
1177 int verbosity, struct got_repository *repo)
1179 const struct got_error *err = NULL;
1180 struct got_reference *ref;
1183 err = got_object_id_str(&id_str, id);
1187 err = got_ref_alloc(&ref, refname, id);
1191 err = got_ref_write(ref, repo);
1194 if (err == NULL && verbosity >= 0)
1195 printf("Created reference %s: %s\n", refname, id_str);
1202 match_wanted_ref(const char *refname, const char *wanted_ref)
1204 if (strncmp(refname, "refs/", 5) != 0)
1209 * Prevent fetching of references that won't make any
1210 * sense outside of the remote repository's context.
1212 if (strncmp(refname, "got/", 4) == 0)
1214 if (strncmp(refname, "remotes/", 8) == 0)
1217 if (strncmp(wanted_ref, "refs/", 5) == 0)
1220 /* Allow prefix match. */
1221 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1224 /* Allow exact match. */
1225 return (strcmp(refname, wanted_ref) == 0);
1229 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1231 struct got_pathlist_entry *pe;
1233 TAILQ_FOREACH(pe, wanted_refs, entry) {
1234 if (match_wanted_ref(refname, pe->path))
1241 static const struct got_error *
1242 create_wanted_ref(const char *refname, struct got_object_id *id,
1243 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1245 const struct got_error *err;
1246 char *remote_refname;
1248 if (strncmp("refs/", refname, 5) == 0)
1251 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1252 remote_repo_name, refname) == -1)
1253 return got_error_from_errno("asprintf");
1255 err = create_ref(remote_refname, id, verbosity, repo);
1256 free(remote_refname);
1260 static const struct got_error *
1261 create_gotconfig(const char *proto, const char *host, const char *port,
1262 const char *remote_repo_path, const char *default_branch,
1263 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1264 struct got_pathlist_head *wanted_refs, int mirror_references,
1265 struct got_repository *repo)
1267 const struct got_error *err = NULL;
1268 char *gotconfig_path = NULL;
1269 char *gotconfig = NULL;
1270 FILE *gotconfig_file = NULL;
1271 const char *branchname = NULL;
1272 char *branches = NULL, *refs = NULL;
1275 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1276 struct got_pathlist_entry *pe;
1277 TAILQ_FOREACH(pe, wanted_branches, entry) {
1279 branchname = pe->path;
1280 if (strncmp(branchname, "refs/heads/", 11) == 0)
1282 if (asprintf(&s, "%s\"%s\" ",
1283 branches ? branches : "", branchname) == -1) {
1284 err = got_error_from_errno("asprintf");
1290 } else if (!fetch_all_branches && default_branch) {
1291 branchname = default_branch;
1292 if (strncmp(branchname, "refs/heads/", 11) == 0)
1294 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1295 err = got_error_from_errno("asprintf");
1299 if (!TAILQ_EMPTY(wanted_refs)) {
1300 struct got_pathlist_entry *pe;
1301 TAILQ_FOREACH(pe, wanted_refs, entry) {
1303 const char *refname = pe->path;
1304 if (strncmp(refname, "refs/", 5) == 0)
1306 if (asprintf(&s, "%s\"%s\" ",
1307 refs ? refs : "", refname) == -1) {
1308 err = got_error_from_errno("asprintf");
1316 /* Create got.conf(5). */
1317 gotconfig_path = got_repo_get_path_gotconfig(repo);
1318 if (gotconfig_path == NULL) {
1319 err = got_error_from_errno("got_repo_get_path_gotconfig");
1322 gotconfig_file = fopen(gotconfig_path, "ae");
1323 if (gotconfig_file == NULL) {
1324 err = got_error_from_errno2("fopen", gotconfig_path);
1327 if (asprintf(&gotconfig,
1332 "\trepository \"%s\"\n"
1338 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1339 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1340 remote_repo_path, branches ? "\tbranch { " : "",
1341 branches ? branches : "", branches ? "}\n" : "",
1342 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1343 mirror_references ? "\tmirror_references yes\n" : "",
1344 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1345 err = got_error_from_errno("asprintf");
1348 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1349 if (n != strlen(gotconfig)) {
1350 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1355 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1356 err = got_error_from_errno2("fclose", gotconfig_path);
1357 free(gotconfig_path);
1362 static const struct got_error *
1363 create_gitconfig(const char *git_url, const char *default_branch,
1364 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1365 struct got_pathlist_head *wanted_refs, int mirror_references,
1366 struct got_repository *repo)
1368 const struct got_error *err = NULL;
1369 char *gitconfig_path = NULL;
1370 char *gitconfig = NULL;
1371 FILE *gitconfig_file = NULL;
1372 char *branches = NULL, *refs = NULL;
1373 const char *branchname;
1376 /* Create a config file Git can understand. */
1377 gitconfig_path = got_repo_get_path_gitconfig(repo);
1378 if (gitconfig_path == NULL) {
1379 err = got_error_from_errno("got_repo_get_path_gitconfig");
1382 gitconfig_file = fopen(gitconfig_path, "ae");
1383 if (gitconfig_file == NULL) {
1384 err = got_error_from_errno2("fopen", gitconfig_path);
1387 if (fetch_all_branches) {
1388 if (mirror_references) {
1389 if (asprintf(&branches,
1390 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1391 err = got_error_from_errno("asprintf");
1394 } else if (asprintf(&branches,
1395 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1396 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1397 err = got_error_from_errno("asprintf");
1400 } else if (!TAILQ_EMPTY(wanted_branches)) {
1401 struct got_pathlist_entry *pe;
1402 TAILQ_FOREACH(pe, wanted_branches, entry) {
1404 branchname = pe->path;
1405 if (strncmp(branchname, "refs/heads/", 11) == 0)
1407 if (mirror_references) {
1409 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1410 branches ? branches : "",
1411 branchname, branchname) == -1) {
1412 err = got_error_from_errno("asprintf");
1415 } else if (asprintf(&s,
1416 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1417 branches ? branches : "",
1418 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1419 branchname) == -1) {
1420 err = got_error_from_errno("asprintf");
1428 * If the server specified a default branch, use just that one.
1429 * Otherwise fall back to fetching all branches on next fetch.
1431 if (default_branch) {
1432 branchname = default_branch;
1433 if (strncmp(branchname, "refs/heads/", 11) == 0)
1436 branchname = "*"; /* fall back to all branches */
1437 if (mirror_references) {
1438 if (asprintf(&branches,
1439 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1440 branchname, branchname) == -1) {
1441 err = got_error_from_errno("asprintf");
1444 } else if (asprintf(&branches,
1445 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1446 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1447 branchname) == -1) {
1448 err = got_error_from_errno("asprintf");
1452 if (!TAILQ_EMPTY(wanted_refs)) {
1453 struct got_pathlist_entry *pe;
1454 TAILQ_FOREACH(pe, wanted_refs, entry) {
1456 const char *refname = pe->path;
1457 if (strncmp(refname, "refs/", 5) == 0)
1459 if (mirror_references) {
1461 "%s\tfetch = refs/%s:refs/%s\n",
1462 refs ? refs : "", refname, refname) == -1) {
1463 err = got_error_from_errno("asprintf");
1466 } else if (asprintf(&s,
1467 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1469 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1471 err = got_error_from_errno("asprintf");
1479 if (asprintf(&gitconfig,
1484 "\tfetch = refs/tags/*:refs/tags/*\n",
1485 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1486 refs ? refs : "") == -1) {
1487 err = got_error_from_errno("asprintf");
1490 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1491 if (n != strlen(gitconfig)) {
1492 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1496 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1497 err = got_error_from_errno2("fclose", gitconfig_path);
1498 free(gitconfig_path);
1503 static const struct got_error *
1504 create_config_files(const char *proto, const char *host, const char *port,
1505 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1506 int mirror_references, struct got_pathlist_head *symrefs,
1507 struct got_pathlist_head *wanted_branches,
1508 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1510 const struct got_error *err = NULL;
1511 const char *default_branch = NULL;
1512 struct got_pathlist_entry *pe;
1515 * If we asked for a set of wanted branches then use the first
1518 if (!TAILQ_EMPTY(wanted_branches)) {
1519 pe = TAILQ_FIRST(wanted_branches);
1520 default_branch = pe->path;
1522 /* First HEAD ref listed by server is the default branch. */
1523 TAILQ_FOREACH(pe, symrefs, entry) {
1524 const char *refname = pe->path;
1525 const char *target = pe->data;
1527 if (strcmp(refname, GOT_REF_HEAD) != 0)
1530 default_branch = target;
1535 /* Create got.conf(5). */
1536 err = create_gotconfig(proto, host, port, remote_repo_path,
1537 default_branch, fetch_all_branches, wanted_branches,
1538 wanted_refs, mirror_references, repo);
1542 /* Create a config file Git can understand. */
1543 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1544 wanted_branches, wanted_refs, mirror_references, repo);
1547 static const struct got_error *
1548 cmd_clone(int argc, char *argv[])
1550 const struct got_error *error = NULL;
1551 const char *uri, *dirname;
1552 char *proto, *host, *port, *repo_name, *server_path;
1553 char *default_destdir = NULL, *id_str = NULL;
1554 const char *repo_path;
1555 struct got_repository *repo = NULL;
1556 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1557 struct got_pathlist_entry *pe;
1558 struct got_object_id *pack_hash = NULL;
1559 int ch, fetchfd = -1, fetchstatus;
1560 pid_t fetchpid = -1;
1561 struct got_fetch_progress_arg fpa;
1562 char *git_url = NULL;
1563 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1564 int bflag = 0, list_refs_only = 0;
1565 int *pack_fds = NULL;
1568 TAILQ_INIT(&symrefs);
1569 TAILQ_INIT(&wanted_branches);
1570 TAILQ_INIT(&wanted_refs);
1572 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1575 fetch_all_branches = 1;
1578 error = got_pathlist_append(&wanted_branches,
1588 mirror_references = 1;
1594 error = got_pathlist_append(&wanted_refs,
1602 else if (verbosity < 3)
1613 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1614 option_conflict('a', 'b');
1615 if (list_refs_only) {
1616 if (!TAILQ_EMPTY(&wanted_branches))
1617 option_conflict('l', 'b');
1618 if (fetch_all_branches)
1619 option_conflict('l', 'a');
1620 if (mirror_references)
1621 option_conflict('l', 'm');
1622 if (!TAILQ_EMPTY(&wanted_refs))
1623 option_conflict('l', 'R');
1635 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1640 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1641 host, port ? ":" : "", port ? port : "",
1642 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1643 error = got_error_from_errno("asprintf");
1647 if (strcmp(proto, "git") == 0) {
1649 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1650 "sendfd dns inet unveil", NULL) == -1)
1653 } else if (strcmp(proto, "git+ssh") == 0 ||
1654 strcmp(proto, "ssh") == 0) {
1656 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1657 "sendfd unveil", NULL) == -1)
1660 } else if (strcmp(proto, "http") == 0 ||
1661 strcmp(proto, "git+http") == 0) {
1662 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1665 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1668 if (dirname == NULL) {
1669 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1670 error = got_error_from_errno("asprintf");
1673 repo_path = default_destdir;
1675 repo_path = dirname;
1677 if (!list_refs_only) {
1678 error = got_path_mkdir(repo_path);
1680 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1681 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1683 if (!got_path_dir_is_empty(repo_path)) {
1684 error = got_error_path(repo_path,
1685 GOT_ERR_DIR_NOT_EMPTY);
1690 error = got_dial_apply_unveil(proto);
1694 error = apply_unveil(repo_path, 0, NULL);
1699 printf("Connecting to %s\n", git_url);
1701 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1702 server_path, verbosity);
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) {
2533 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2534 "sendfd unveil", NULL) == -1)
2537 } else if (strcmp(proto, "http") == 0 ||
2538 strcmp(proto, "git+http") == 0) {
2539 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
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);
2580 * If set, get this remote's HEAD ref target so
2581 * if it has changed on the server we can fetch it.
2583 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2584 got_ref_cmp_by_name, repo);
2588 TAILQ_FOREACH(re, &remote_refs, entry) {
2589 const char *remote_refname, *remote_target;
2590 size_t remote_name_len;
2592 if (!got_ref_is_symbolic(re->ref))
2595 remote_name_len = strlen(remote->name);
2596 remote_refname = got_ref_get_name(re->ref);
2598 /* we only want refs/remotes/$remote->name/HEAD */
2599 if (strncmp(remote_refname + 13, remote->name,
2600 remote_name_len) != 0)
2603 if (strcmp(remote_refname + remote_name_len + 14,
2608 * Take the name itself because we already
2609 * only match with refs/heads/ in fetch_pack().
2611 remote_target = got_ref_get_symref_target(re->ref);
2612 remote_head = remote_target + remote_name_len + 14;
2617 strncmp(head_refname, "refs/heads/", 11) == 0)
2618 worktree_branch = head_refname;
2621 fpa.last_scaled_size[0] = '\0';
2622 fpa.last_p_indexed = -1;
2623 fpa.last_p_resolved = -1;
2624 fpa.verbosity = verbosity;
2626 fpa.create_configs = 0;
2627 fpa.configs_created = 0;
2628 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2630 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2631 remote->mirror_references, fetch_all_branches, &wanted_branches,
2632 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2633 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2637 if (list_refs_only) {
2638 error = list_remote_refs(&symrefs, &refs);
2642 if (pack_hash == NULL) {
2644 printf("Already up-to-date\n");
2645 } else if (verbosity >= 0) {
2646 error = got_object_id_str(&id_str, pack_hash);
2649 printf("\nFetched %s.pack\n", id_str);
2654 /* Update references provided with the pack file. */
2655 TAILQ_FOREACH(pe, &refs, entry) {
2656 const char *refname = pe->path;
2657 struct got_object_id *id = pe->data;
2658 struct got_reference *ref;
2659 char *remote_refname;
2661 if (is_wanted_ref(&wanted_refs, refname) &&
2662 !remote->mirror_references) {
2663 error = update_wanted_ref(refname, id,
2664 remote->name, verbosity, repo);
2670 if (remote->mirror_references ||
2671 strncmp("refs/tags/", refname, 10) == 0) {
2672 error = got_ref_open(&ref, repo, refname, 1);
2674 if (error->code != GOT_ERR_NOT_REF)
2676 error = create_ref(refname, id, verbosity,
2681 error = update_ref(ref, id, replace_tags,
2683 unlock_err = got_ref_unlock(ref);
2684 if (unlock_err && error == NULL)
2690 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2691 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2692 remote_name, refname + 11) == -1) {
2693 error = got_error_from_errno("asprintf");
2697 error = got_ref_open(&ref, repo, remote_refname, 1);
2699 if (error->code != GOT_ERR_NOT_REF)
2701 error = create_ref(remote_refname, id,
2706 error = update_ref(ref, id, replace_tags,
2708 unlock_err = got_ref_unlock(ref);
2709 if (unlock_err && error == NULL)
2716 /* Also create a local branch if none exists yet. */
2717 error = got_ref_open(&ref, repo, refname, 1);
2719 if (error->code != GOT_ERR_NOT_REF)
2721 error = create_ref(refname, id, verbosity,
2726 unlock_err = got_ref_unlock(ref);
2727 if (unlock_err && error == NULL)
2734 error = delete_missing_refs(&refs, &symrefs, remote,
2740 if (!remote->mirror_references) {
2741 /* Update remote HEAD reference if the server provided one. */
2742 TAILQ_FOREACH(pe, &symrefs, entry) {
2743 struct got_reference *target_ref;
2744 const char *refname = pe->path;
2745 const char *target = pe->data;
2746 char *remote_refname = NULL, *remote_target = NULL;
2748 if (strcmp(refname, GOT_REF_HEAD) != 0)
2751 if (strncmp("refs/heads/", target, 11) != 0)
2754 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2755 remote->name, refname) == -1) {
2756 error = got_error_from_errno("asprintf");
2759 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2760 remote->name, target + 11) == -1) {
2761 error = got_error_from_errno("asprintf");
2762 free(remote_refname);
2766 error = got_ref_open(&target_ref, repo, remote_target,
2769 free(remote_refname);
2770 free(remote_target);
2771 if (error->code == GOT_ERR_NOT_REF) {
2777 error = update_symref(remote_refname, target_ref,
2779 free(remote_refname);
2780 free(remote_target);
2781 got_ref_close(target_ref);
2788 if (kill(fetchpid, SIGTERM) == -1)
2789 error = got_error_from_errno("kill");
2790 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2791 error = got_error_from_errno("waitpid");
2793 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2794 error = got_error_from_errno("close");
2796 const struct got_error *close_err = got_repo_close(repo);
2801 got_worktree_close(worktree);
2803 const struct got_error *pack_err =
2804 got_repo_pack_fds_close(pack_fds);
2808 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2809 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2810 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2811 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2812 got_ref_list_free(&remote_refs);
2813 got_repo_free_remote_repo_data(remote);
2830 usage_checkout(void)
2832 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2833 "[-p path-prefix] repository-path [work-tree-path]\n",
2839 show_worktree_base_ref_warning(void)
2841 fprintf(stderr, "%s: warning: could not create a reference "
2842 "to the work tree's base commit; the commit could be "
2843 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2844 "repository writable and running 'got update' will prevent this\n",
2848 struct got_checkout_progress_arg {
2849 const char *worktree_path;
2850 int had_base_commit_ref_error;
2854 static const struct got_error *
2855 checkout_progress(void *arg, unsigned char status, const char *path)
2857 struct got_checkout_progress_arg *a = arg;
2859 /* Base commit bump happens silently. */
2860 if (status == GOT_STATUS_BUMP_BASE)
2863 if (status == GOT_STATUS_BASE_REF_ERR) {
2864 a->had_base_commit_ref_error = 1;
2868 while (path[0] == '/')
2871 if (a->verbosity >= 0)
2872 printf("%c %s/%s\n", status, a->worktree_path, path);
2877 static const struct got_error *
2878 check_cancelled(void *arg)
2880 if (sigint_received || sigpipe_received)
2881 return got_error(GOT_ERR_CANCELLED);
2885 static const struct got_error *
2886 check_linear_ancestry(struct got_object_id *commit_id,
2887 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2888 struct got_repository *repo)
2890 const struct got_error *err = NULL;
2891 struct got_object_id *yca_id;
2893 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2894 commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2899 return got_error(GOT_ERR_ANCESTRY);
2902 * Require a straight line of history between the target commit
2903 * and the work tree's base commit.
2905 * Non-linear situations such as this require a rebase:
2907 * (commit) D F (base_commit)
2915 * 'got update' only handles linear cases:
2916 * Update forwards in time: A (base/yca) - B - C - D (commit)
2917 * Update backwards in time: D (base) - C - B - A (commit/yca)
2919 if (allow_forwards_in_time_only) {
2920 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2921 return got_error(GOT_ERR_ANCESTRY);
2922 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2923 got_object_id_cmp(base_commit_id, yca_id) != 0)
2924 return got_error(GOT_ERR_ANCESTRY);
2930 static const struct got_error *
2931 check_same_branch(struct got_object_id *commit_id,
2932 struct got_reference *head_ref, struct got_repository *repo)
2934 const struct got_error *err = NULL;
2935 struct got_commit_graph *graph = NULL;
2936 struct got_object_id *head_commit_id = NULL;
2938 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2942 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2945 err = got_commit_graph_open(&graph, "/", 1);
2949 err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2950 check_cancelled, NULL);
2955 struct got_object_id id;
2957 err = got_commit_graph_iter_next(&id, graph, repo,
2958 check_cancelled, NULL);
2960 if (err->code == GOT_ERR_ITER_COMPLETED)
2961 err = got_error(GOT_ERR_ANCESTRY);
2965 if (got_object_id_cmp(&id, commit_id) == 0)
2970 got_commit_graph_close(graph);
2971 free(head_commit_id);
2975 static const struct got_error *
2976 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2978 static char msg[512];
2979 const char *branch_name;
2981 if (got_ref_is_symbolic(ref))
2982 branch_name = got_ref_get_symref_target(ref);
2984 branch_name = got_ref_get_name(ref);
2986 if (strncmp("refs/heads/", branch_name, 11) == 0)
2989 snprintf(msg, sizeof(msg),
2990 "target commit is not contained in branch '%s'; "
2991 "the branch to use must be specified with -b; "
2992 "if necessary a new branch can be created for "
2993 "this commit with 'got branch -c %s BRANCH_NAME'",
2994 branch_name, commit_id_str);
2996 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2999 static const struct got_error *
3000 cmd_checkout(int argc, char *argv[])
3002 const struct got_error *close_err, *error = NULL;
3003 struct got_repository *repo = NULL;
3004 struct got_reference *head_ref = NULL, *ref = NULL;
3005 struct got_worktree *worktree = NULL;
3006 char *repo_path = NULL;
3007 char *worktree_path = NULL;
3008 const char *path_prefix = "";
3009 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
3010 char *commit_id_str = NULL, *keyword_idstr = NULL;
3011 struct got_object_id *commit_id = NULL;
3013 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
3014 struct got_pathlist_head paths;
3015 struct got_checkout_progress_arg cpa;
3016 int *pack_fds = NULL;
3021 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3022 "unveil", NULL) == -1)
3026 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3029 branch_name = optarg;
3032 commit_id_str = strdup(optarg);
3033 if (commit_id_str == NULL)
3034 return got_error_from_errno("strdup");
3040 path_prefix = optarg;
3055 char *base, *dotgit;
3057 repo_path = realpath(argv[0], NULL);
3058 if (repo_path == NULL)
3059 return got_error_from_errno2("realpath", argv[0]);
3060 cwd = getcwd(NULL, 0);
3062 error = got_error_from_errno("getcwd");
3069 error = got_path_basename(&base, path);
3072 dotgit = strstr(base, ".git");
3075 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3076 error = got_error_from_errno("asprintf");
3081 } else if (argc == 2) {
3082 repo_path = realpath(argv[0], NULL);
3083 if (repo_path == NULL) {
3084 error = got_error_from_errno2("realpath", argv[0]);
3087 worktree_path = realpath(argv[1], NULL);
3088 if (worktree_path == NULL) {
3089 if (errno != ENOENT) {
3090 error = got_error_from_errno2("realpath",
3094 worktree_path = strdup(argv[1]);
3095 if (worktree_path == NULL) {
3096 error = got_error_from_errno("strdup");
3103 got_path_strip_trailing_slashes(repo_path);
3104 got_path_strip_trailing_slashes(worktree_path);
3106 if (got_path_is_child(worktree_path, repo_path, strlen(repo_path)) ||
3107 got_path_is_child(repo_path, worktree_path,
3108 strlen(worktree_path))) {
3109 error = got_error_fmt(GOT_ERR_BAD_PATH,
3110 "work tree and repository paths may not overlap: %s",
3115 error = got_repo_pack_fds_open(&pack_fds);
3119 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3123 /* Pre-create work tree path for unveil(2) */
3124 error = got_path_mkdir(worktree_path);
3126 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3127 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3129 if (!allow_nonempty &&
3130 !got_path_dir_is_empty(worktree_path)) {
3131 error = got_error_path(worktree_path,
3132 GOT_ERR_DIR_NOT_EMPTY);
3137 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3141 error = got_ref_open(&head_ref, repo, branch_name, 0);
3145 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3146 GOT_WORKTREE_GOT_DIR, repo);
3147 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3150 error = got_worktree_open(&worktree, worktree_path,
3151 GOT_WORKTREE_GOT_DIR);
3155 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3159 if (!same_path_prefix) {
3160 error = got_error(GOT_ERR_PATH_PREFIX);
3164 if (commit_id_str) {
3165 struct got_reflist_head refs;
3167 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3172 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3176 if (keyword_idstr != NULL) {
3177 free(commit_id_str);
3178 commit_id_str = keyword_idstr;
3181 error = got_repo_match_object_id(&commit_id, NULL,
3182 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3183 got_ref_list_free(&refs);
3186 error = check_linear_ancestry(commit_id,
3187 got_worktree_get_base_commit_id(worktree), 0, repo);
3188 if (error != NULL) {
3189 if (error->code == GOT_ERR_ANCESTRY) {
3190 error = checkout_ancestry_error(
3191 head_ref, commit_id_str);
3195 error = check_same_branch(commit_id, head_ref, repo);
3197 if (error->code == GOT_ERR_ANCESTRY) {
3198 error = checkout_ancestry_error(
3199 head_ref, commit_id_str);
3203 error = got_worktree_set_base_commit_id(worktree, repo,
3207 /* Expand potentially abbreviated commit ID string. */
3208 free(commit_id_str);
3209 error = got_object_id_str(&commit_id_str, commit_id);
3213 commit_id = got_object_id_dup(
3214 got_worktree_get_base_commit_id(worktree));
3215 if (commit_id == NULL) {
3216 error = got_error_from_errno("got_object_id_dup");
3219 error = got_object_id_str(&commit_id_str, commit_id);
3224 error = got_pathlist_append(&paths, "", NULL);
3227 cpa.worktree_path = worktree_path;
3228 cpa.had_base_commit_ref_error = 0;
3229 cpa.verbosity = verbosity;
3230 error = got_worktree_checkout_files(worktree, &paths, repo,
3231 checkout_progress, &cpa, check_cancelled, NULL);
3235 if (got_ref_is_symbolic(head_ref)) {
3236 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3239 refname = got_ref_get_name(ref);
3241 refname = got_ref_get_name(head_ref);
3242 printf("Checked out %s: %s\n", refname, commit_id_str);
3243 printf("Now shut up and hack\n");
3244 if (cpa.had_base_commit_ref_error)
3245 show_worktree_base_ref_warning();
3248 const struct got_error *pack_err =
3249 got_repo_pack_fds_close(pack_fds);
3254 got_ref_close(head_ref);
3258 close_err = got_repo_close(repo);
3262 if (worktree != NULL) {
3263 close_err = got_worktree_close(worktree);
3267 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3268 free(commit_id_str);
3271 free(worktree_path);
3276 struct got_update_progress_arg {
3288 print_update_progress_stats(struct got_update_progress_arg *upa)
3290 if (!upa->did_something)
3293 if (upa->conflicts > 0)
3294 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3295 if (upa->obstructed > 0)
3296 printf("File paths obstructed by a non-regular file: %d\n",
3298 if (upa->not_updated > 0)
3299 printf("Files not updated because of existing merge "
3300 "conflicts: %d\n", upa->not_updated);
3304 * The meaning of some status codes differs between merge-style operations and
3305 * update operations. For example, the ! status code means "file was missing"
3306 * if changes were merged into the work tree, and "missing file was restored"
3307 * if the work tree was updated. This function should be used by any operation
3308 * which merges changes into the work tree without updating the work tree.
3311 print_merge_progress_stats(struct got_update_progress_arg *upa)
3313 if (!upa->did_something)
3316 if (upa->conflicts > 0)
3317 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3318 if (upa->obstructed > 0)
3319 printf("File paths obstructed by a non-regular file: %d\n",
3321 if (upa->missing > 0)
3322 printf("Files which had incoming changes but could not be "
3323 "found in the work tree: %d\n", upa->missing);
3324 if (upa->not_deleted > 0)
3325 printf("Files not deleted due to differences in deleted "
3326 "content: %d\n", upa->not_deleted);
3327 if (upa->unversioned > 0)
3328 printf("Files not merged because an unversioned file was "
3329 "found in the work tree: %d\n", upa->unversioned);
3335 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3336 "[path ...]\n", getprogname());
3340 static const struct got_error *
3341 update_progress(void *arg, unsigned char status, const char *path)
3343 struct got_update_progress_arg *upa = arg;
3345 if (status == GOT_STATUS_EXISTS ||
3346 status == GOT_STATUS_BASE_REF_ERR)
3349 upa->did_something = 1;
3351 /* Base commit bump happens silently. */
3352 if (status == GOT_STATUS_BUMP_BASE)
3355 if (status == GOT_STATUS_CONFLICT)
3357 if (status == GOT_STATUS_OBSTRUCTED)
3359 if (status == GOT_STATUS_CANNOT_UPDATE)
3361 if (status == GOT_STATUS_MISSING)
3363 if (status == GOT_STATUS_CANNOT_DELETE)
3365 if (status == GOT_STATUS_UNVERSIONED)
3368 while (path[0] == '/')
3370 if (upa->verbosity >= 0)
3371 printf("%c %s\n", status, path);
3376 static const struct got_error *
3377 switch_head_ref(struct got_reference *head_ref,
3378 struct got_object_id *commit_id, struct got_worktree *worktree,
3379 struct got_repository *repo)
3381 const struct got_error *err = NULL;
3383 int ref_has_moved = 0;
3385 /* Trivial case: switching between two different references. */
3386 if (strcmp(got_ref_get_name(head_ref),
3387 got_worktree_get_head_ref_name(worktree)) != 0) {
3388 printf("Switching work tree from %s to %s\n",
3389 got_worktree_get_head_ref_name(worktree),
3390 got_ref_get_name(head_ref));
3391 return got_worktree_set_head_ref(worktree, head_ref);
3394 err = check_linear_ancestry(commit_id,
3395 got_worktree_get_base_commit_id(worktree), 0, repo);
3397 if (err->code != GOT_ERR_ANCESTRY)
3404 /* Switching to a rebased branch with the same reference name. */
3405 err = got_object_id_str(&base_id_str,
3406 got_worktree_get_base_commit_id(worktree));
3409 printf("Reference %s now points at a different branch\n",
3410 got_worktree_get_head_ref_name(worktree));
3411 printf("Switching work tree from %s to %s\n", base_id_str,
3412 got_worktree_get_head_ref_name(worktree));
3416 static const struct got_error *
3417 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3419 const struct got_error *err;
3422 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3426 return got_error(GOT_ERR_REBASING);
3428 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3432 return got_error(GOT_ERR_HISTEDIT_BUSY);
3437 static const struct got_error *
3438 check_merge_in_progress(struct got_worktree *worktree,
3439 struct got_repository *repo)
3441 const struct got_error *err;
3444 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3448 return got_error(GOT_ERR_MERGE_BUSY);
3453 static const struct got_error *
3454 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3455 char *argv[], struct got_worktree *worktree)
3457 const struct got_error *err = NULL;
3459 struct got_pathlist_entry *new;
3465 return got_error_from_errno("strdup");
3466 return got_pathlist_append(paths, path, NULL);
3469 for (i = 0; i < argc; i++) {
3470 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3473 err = got_pathlist_insert(&new, paths, path, NULL);
3474 if (err || new == NULL /* duplicate */) {
3484 static const struct got_error *
3485 wrap_not_worktree_error(const struct got_error *orig_err,
3486 const char *cmdname, const char *path)
3488 const struct got_error *err;
3489 struct got_repository *repo;
3490 static char msg[512];
3491 int *pack_fds = NULL;
3493 err = got_repo_pack_fds_open(&pack_fds);
3497 err = got_repo_open(&repo, path, NULL, pack_fds);
3501 snprintf(msg, sizeof(msg),
3502 "'got %s' needs a work tree in addition to a git repository\n"
3503 "Work trees can be checked out from this Git repository with "
3505 "The got(1) manual page contains more information.", cmdname);
3506 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3508 const struct got_error *close_err = got_repo_close(repo);
3513 const struct got_error *pack_err =
3514 got_repo_pack_fds_close(pack_fds);
3521 static const struct got_error *
3522 cmd_update(int argc, char *argv[])
3524 const struct got_error *close_err, *error = NULL;
3525 struct got_repository *repo = NULL;
3526 struct got_worktree *worktree = NULL;
3527 char *worktree_path = NULL;
3528 struct got_object_id *commit_id = NULL;
3529 char *commit_id_str = NULL;
3530 const char *branch_name = NULL;
3531 struct got_reference *head_ref = NULL;
3532 struct got_pathlist_head paths;
3533 struct got_pathlist_entry *pe;
3534 int ch, verbosity = 0;
3535 struct got_update_progress_arg upa;
3536 int *pack_fds = NULL;
3541 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3542 "unveil", NULL) == -1)
3546 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3549 branch_name = optarg;
3552 commit_id_str = strdup(optarg);
3553 if (commit_id_str == NULL)
3554 return got_error_from_errno("strdup");
3568 worktree_path = getcwd(NULL, 0);
3569 if (worktree_path == NULL) {
3570 error = got_error_from_errno("getcwd");
3574 error = got_repo_pack_fds_open(&pack_fds);
3578 error = got_worktree_open(&worktree, worktree_path,
3579 GOT_WORKTREE_GOT_DIR);
3581 if (error->code == GOT_ERR_NOT_WORKTREE)
3582 error = wrap_not_worktree_error(error, "update",
3587 error = check_rebase_or_histedit_in_progress(worktree);
3591 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3596 error = apply_unveil(got_repo_get_path(repo), 0,
3597 got_worktree_get_root_path(worktree));
3601 error = check_merge_in_progress(worktree, repo);
3605 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3609 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3610 got_worktree_get_head_ref_name(worktree), 0);
3613 if (commit_id_str == NULL) {
3614 error = got_ref_resolve(&commit_id, repo, head_ref);
3617 error = got_object_id_str(&commit_id_str, commit_id);
3621 struct got_reflist_head refs;
3622 char *keyword_idstr = NULL;
3626 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3631 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3635 if (keyword_idstr != NULL) {
3636 free(commit_id_str);
3637 commit_id_str = keyword_idstr;
3640 error = got_repo_match_object_id(&commit_id, NULL,
3641 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3642 got_ref_list_free(&refs);
3643 free(commit_id_str);
3644 commit_id_str = NULL;
3647 error = got_object_id_str(&commit_id_str, commit_id);
3653 struct got_object_id *head_commit_id;
3654 TAILQ_FOREACH(pe, &paths, entry) {
3655 if (pe->path_len == 0)
3657 error = got_error_msg(GOT_ERR_BAD_PATH,
3658 "switching between branches requires that "
3659 "the entire work tree gets updated");
3662 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3665 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3667 free(head_commit_id);
3670 error = check_same_branch(commit_id, head_ref, repo);
3673 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3677 error = check_linear_ancestry(commit_id,
3678 got_worktree_get_base_commit_id(worktree), 0, repo);
3679 if (error != NULL) {
3680 if (error->code == GOT_ERR_ANCESTRY)
3681 error = got_error(GOT_ERR_BRANCH_MOVED);
3684 error = check_same_branch(commit_id, head_ref, repo);
3689 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3691 error = got_worktree_set_base_commit_id(worktree, repo,
3697 memset(&upa, 0, sizeof(upa));
3698 upa.verbosity = verbosity;
3699 error = got_worktree_checkout_files(worktree, &paths, repo,
3700 update_progress, &upa, check_cancelled, NULL);
3704 if (upa.did_something) {
3705 printf("Updated to %s: %s\n",
3706 got_worktree_get_head_ref_name(worktree), commit_id_str);
3708 printf("Already up-to-date\n");
3710 print_update_progress_stats(&upa);
3713 const struct got_error *pack_err =
3714 got_repo_pack_fds_close(pack_fds);
3719 close_err = got_repo_close(repo);
3723 if (worktree != NULL) {
3724 close_err = got_worktree_close(worktree);
3728 if (head_ref != NULL)
3729 got_ref_close(head_ref);
3730 free(worktree_path);
3731 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3733 free(commit_id_str);
3737 static const struct got_error *
3738 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3739 const char *path, int diff_context, int ignore_whitespace,
3740 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3741 struct got_repository *repo, FILE *outfile)
3743 const struct got_error *err = NULL;
3744 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3745 FILE *f1 = NULL, *f2 = NULL;
3746 int fd1 = -1, fd2 = -1;
3748 fd1 = got_opentempfd();
3750 return got_error_from_errno("got_opentempfd");
3751 fd2 = got_opentempfd();
3753 err = got_error_from_errno("got_opentempfd");
3758 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3764 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3768 f1 = got_opentemp();
3770 err = got_error_from_errno("got_opentemp");
3773 f2 = got_opentemp();
3775 err = got_error_from_errno("got_opentemp");
3779 while (path[0] == '/')
3781 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3782 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3783 force_text_diff, dsa, outfile);
3785 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3786 err = got_error_from_errno("close");
3788 got_object_blob_close(blob1);
3789 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3790 err = got_error_from_errno("close");
3792 got_object_blob_close(blob2);
3793 if (f1 && fclose(f1) == EOF && err == NULL)
3794 err = got_error_from_errno("fclose");
3795 if (f2 && fclose(f2) == EOF && err == NULL)
3796 err = got_error_from_errno("fclose");
3800 static const struct got_error *
3801 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3802 const char *path, int diff_context, int ignore_whitespace,
3803 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3804 struct got_repository *repo, FILE *outfile)
3806 const struct got_error *err = NULL;
3807 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3808 struct got_diff_blob_output_unidiff_arg arg;
3809 FILE *f1 = NULL, *f2 = NULL;
3810 int fd1 = -1, fd2 = -1;
3813 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3816 fd1 = got_opentempfd();
3818 err = got_error_from_errno("got_opentempfd");
3823 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3827 f1 = got_opentemp();
3829 err = got_error_from_errno("got_opentemp");
3833 f2 = got_opentemp();
3835 err = got_error_from_errno("got_opentemp");
3838 fd2 = got_opentempfd();
3840 err = got_error_from_errno("got_opentempfd");
3843 arg.diff_context = diff_context;
3844 arg.ignore_whitespace = ignore_whitespace;
3845 arg.force_text_diff = force_text_diff;
3847 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3848 arg.outfile = outfile;
3851 while (path[0] == '/')
3853 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3854 got_diff_blob_output_unidiff, &arg, 1);
3857 got_object_tree_close(tree1);
3859 got_object_tree_close(tree2);
3860 if (f1 && fclose(f1) == EOF && err == NULL)
3861 err = got_error_from_errno("fclose");
3862 if (f2 && fclose(f2) == EOF && err == NULL)
3863 err = got_error_from_errno("fclose");
3864 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3865 err = got_error_from_errno("close");
3866 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3867 err = got_error_from_errno("close");
3871 static const struct got_error *
3872 get_changed_paths(struct got_pathlist_head *paths,
3873 struct got_commit_object *commit, struct got_repository *repo,
3874 struct got_diffstat_cb_arg *dsa)
3876 const struct got_error *err = NULL;
3877 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3878 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3879 struct got_object_qid *qid;
3880 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3881 FILE *f1 = NULL, *f2 = NULL;
3882 int fd1 = -1, fd2 = -1;
3885 cb = got_diff_tree_compute_diffstat;
3887 f1 = got_opentemp();
3889 err = got_error_from_errno("got_opentemp");
3892 f2 = got_opentemp();
3894 err = got_error_from_errno("got_opentemp");
3897 fd1 = got_opentempfd();
3899 err = got_error_from_errno("got_opentempfd");
3902 fd2 = got_opentempfd();
3904 err = got_error_from_errno("got_opentempfd");
3909 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3911 struct got_commit_object *pcommit;
3912 err = got_object_open_as_commit(&pcommit, repo,
3917 tree_id1 = got_object_id_dup(
3918 got_object_commit_get_tree_id(pcommit));
3919 if (tree_id1 == NULL) {
3920 got_object_commit_close(pcommit);
3921 return got_error_from_errno("got_object_id_dup");
3923 got_object_commit_close(pcommit);
3928 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3933 tree_id2 = got_object_commit_get_tree_id(commit);
3934 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3938 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3939 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3942 got_object_tree_close(tree1);
3944 got_object_tree_close(tree2);
3945 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3946 err = got_error_from_errno("close");
3947 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3948 err = got_error_from_errno("close");
3949 if (f1 && fclose(f1) == EOF && err == NULL)
3950 err = got_error_from_errno("fclose");
3951 if (f2 && fclose(f2) == EOF && err == NULL)
3952 err = got_error_from_errno("fclose");
3957 static const struct got_error *
3958 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3959 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3960 struct got_repository *repo, FILE *outfile)
3962 const struct got_error *err = NULL;
3963 struct got_commit_object *pcommit = NULL;
3964 char *id_str1 = NULL, *id_str2 = NULL;
3965 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3966 struct got_object_qid *qid;
3968 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3970 err = got_object_open_as_commit(&pcommit, repo,
3974 err = got_object_id_str(&id_str1, &qid->id);
3979 err = got_object_id_str(&id_str2, id);
3983 if (path && path[0] != '\0') {
3985 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3989 err = got_object_id_by_path(&obj_id1, repo,
3992 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3998 err = got_object_get_type(&obj_type, repo, obj_id2);
4004 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4005 fprintf(outfile, "commit - %s\n",
4006 id_str1 ? id_str1 : "/dev/null");
4007 fprintf(outfile, "commit + %s\n", id_str2);
4009 case GOT_OBJ_TYPE_BLOB:
4010 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
4011 0, 0, dsa, repo, outfile);
4013 case GOT_OBJ_TYPE_TREE:
4014 err = diff_trees(obj_id1, obj_id2, path, diff_context,
4015 0, 0, dsa, repo, outfile);
4018 err = got_error(GOT_ERR_OBJ_TYPE);
4024 obj_id2 = got_object_commit_get_tree_id(commit);
4026 obj_id1 = got_object_commit_get_tree_id(pcommit);
4028 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4029 fprintf(outfile, "commit - %s\n",
4030 id_str1 ? id_str1 : "/dev/null");
4031 fprintf(outfile, "commit + %s\n", id_str2);
4032 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
4033 dsa, repo, outfile);
4039 got_object_commit_close(pcommit);
4044 get_datestr(time_t *time, char *datebuf)
4046 struct tm mytm, *tm;
4049 tm = gmtime_r(time, &mytm);
4052 s = asctime_r(tm, datebuf);
4055 p = strchr(s, '\n');
4061 static const struct got_error *
4062 match_commit(int *have_match, struct got_object_id *id,
4063 struct got_commit_object *commit, regex_t *regex)
4065 const struct got_error *err = NULL;
4066 regmatch_t regmatch;
4067 char *id_str = NULL, *logmsg = NULL;
4071 err = got_object_id_str(&id_str, id);
4075 err = got_object_commit_get_logmsg(&logmsg, commit);
4079 if (regexec(regex, got_object_commit_get_author(commit), 1,
4080 ®match, 0) == 0 ||
4081 regexec(regex, got_object_commit_get_committer(commit), 1,
4082 ®match, 0) == 0 ||
4083 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4084 regexec(regex, logmsg, 1, ®match, 0) == 0)
4093 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4096 regmatch_t regmatch;
4097 struct got_pathlist_entry *pe;
4101 TAILQ_FOREACH(pe, changed_paths, entry) {
4102 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4109 static const struct got_error *
4110 match_patch(int *have_match, struct got_commit_object *commit,
4111 struct got_object_id *id, const char *path, int diff_context,
4112 struct got_repository *repo, regex_t *regex, FILE *f)
4114 const struct got_error *err = NULL;
4116 size_t linesize = 0;
4117 regmatch_t regmatch;
4121 err = got_opentemp_truncate(f);
4125 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4129 if (fseeko(f, 0L, SEEK_SET) == -1) {
4130 err = got_error_from_errno("fseeko");
4134 while (getline(&line, &linesize, f) != -1) {
4135 if (regexec(regex, line, 1, ®match, 0) == 0) {
4145 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4147 static const struct got_error*
4148 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4149 struct got_object_id *id, struct got_repository *repo,
4152 static const struct got_error *err = NULL;
4153 struct got_reflist_entry *re;
4159 TAILQ_FOREACH(re, refs, entry) {
4160 struct got_tag_object *tag = NULL;
4161 struct got_object_id *ref_id;
4164 name = got_ref_get_name(re->ref);
4165 if (strcmp(name, GOT_REF_HEAD) == 0)
4167 if (strncmp(name, "refs/", 5) == 0)
4169 if (strncmp(name, "got/", 4) == 0)
4171 if (strncmp(name, "heads/", 6) == 0)
4173 if (strncmp(name, "remotes/", 8) == 0) {
4177 s = strstr(name, "/" GOT_REF_HEAD);
4178 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4181 err = got_ref_resolve(&ref_id, repo, re->ref);
4184 if (strncmp(name, "tags/", 5) == 0) {
4185 err = got_object_open_as_tag(&tag, repo, ref_id);
4187 if (err->code != GOT_ERR_OBJ_TYPE) {
4191 /* Ref points at something other than a tag. */
4196 cmp = got_object_id_cmp(tag ?
4197 got_object_tag_get_object_id(tag) : ref_id, id);
4200 got_object_tag_close(tag);
4204 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4205 s ? ", " : "", name) == -1) {
4206 err = got_error_from_errno("asprintf");
4217 static const struct got_error *
4218 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4219 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4221 const struct got_error *err = NULL;
4222 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4223 char *comma, *s, *nl;
4224 struct got_reflist_head *refs;
4225 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4227 time_t committer_time;
4229 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4231 err = build_refs_str(&ref_str, refs, id, repo, 1);
4235 /* Display the first matching ref only. */
4236 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4240 if (ref_str == NULL) {
4241 err = got_object_id_str(&id_str, id);
4246 committer_time = got_object_commit_get_committer_time(commit);
4247 if (gmtime_r(&committer_time, &tm) == NULL) {
4248 err = got_error_from_errno("gmtime_r");
4251 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4252 err = got_error(GOT_ERR_NO_SPACE);
4256 err = got_object_commit_get_logmsg(&logmsg0, commit);
4261 while (isspace((unsigned char)s[0]))
4264 nl = strchr(s, '\n');
4270 printf("%s%-7s %s\n", datebuf, ref_str, s);
4272 printf("%s%.7s %s\n", datebuf, id_str, s);
4274 if (fflush(stdout) != 0 && err == NULL)
4275 err = got_error_from_errno("fflush");
4283 static const struct got_error *
4284 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4286 struct got_pathlist_entry *pe;
4289 printf("%s\n", header);
4291 TAILQ_FOREACH(pe, dsa->paths, entry) {
4292 struct got_diff_changed_path *cp = pe->data;
4293 int pad = dsa->max_path_len - pe->path_len + 1;
4295 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4296 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4298 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4299 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4300 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4302 if (fflush(stdout) != 0)
4303 return got_error_from_errno("fflush");
4308 static const struct got_error *
4314 if (fseeko(f, 0L, SEEK_SET) == -1)
4315 return got_error_from_errno("fseek");
4318 r = fread(buf, 1, sizeof(buf), f);
4321 return got_error_from_errno("fread");
4325 if (fwrite(buf, 1, r, stdout) != r)
4326 return got_ferror(stdout, GOT_ERR_IO);
4332 static const struct got_error *
4333 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4334 struct got_repository *repo, const char *path,
4335 struct got_pathlist_head *changed_paths,
4336 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4337 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4340 const struct got_error *err = NULL;
4342 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4344 time_t committer_time;
4345 const char *author, *committer;
4346 char *refs_str = NULL;
4348 err = got_object_id_str(&id_str, id);
4352 if (custom_refs_str == NULL) {
4353 struct got_reflist_head *refs;
4354 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4356 err = build_refs_str(&refs_str, refs, id, repo, 0);
4362 printf(GOT_COMMIT_SEP_STR);
4363 if (custom_refs_str)
4364 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4367 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4368 refs_str ? " (" : "", refs_str ? refs_str : "",
4369 refs_str ? ")" : "");
4374 printf("from: %s\n", got_object_commit_get_author(commit));
4375 author = got_object_commit_get_author(commit);
4376 committer = got_object_commit_get_committer(commit);
4377 if (strcmp(author, committer) != 0)
4378 printf("via: %s\n", committer);
4379 committer_time = got_object_commit_get_committer_time(commit);
4380 datestr = get_datestr(&committer_time, datebuf);
4382 printf("date: %s UTC\n", datestr);
4383 if (got_object_commit_get_nparents(commit) > 1) {
4384 const struct got_object_id_queue *parent_ids;
4385 struct got_object_qid *qid;
4387 parent_ids = got_object_commit_get_parent_ids(commit);
4388 STAILQ_FOREACH(qid, parent_ids, entry) {
4389 err = got_object_id_str(&id_str, &qid->id);
4392 printf("parent %d: %s\n", n++, id_str);
4398 err = got_object_commit_get_logmsg(&logmsg0, commit);
4404 line = strsep(&logmsg, "\n");
4406 printf(" %s\n", line);
4410 if (changed_paths && diffstat == NULL) {
4411 struct got_pathlist_entry *pe;
4413 TAILQ_FOREACH(pe, changed_paths, entry) {
4414 struct got_diff_changed_path *cp = pe->data;
4416 printf(" %c %s\n", cp->status, pe->path);
4424 err = got_error_from_errno("got_opentemp");
4429 err = print_patch(commit, id, path, diff_context, diffstat,
4430 repo, diffstat == NULL ? stdout : f);
4435 err = print_diffstat(diffstat, NULL);
4447 if (fflush(stdout) != 0 && err == NULL)
4448 err = got_error_from_errno("fflush");
4450 if (f && fclose(f) == EOF && err == NULL)
4451 err = got_error_from_errno("fclose");
4457 static const struct got_error *
4458 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4459 struct got_repository *repo, const char *path, int show_changed_paths,
4460 int show_diffstat, int show_patch, const char *search_pattern,
4461 int diff_context, int limit, int log_branches, int reverse_display_order,
4462 struct got_reflist_object_id_map *refs_idmap, int one_line, int toposort,
4465 const struct got_error *err;
4466 struct got_commit_graph *graph;
4469 struct got_object_id_queue reversed_commits;
4470 struct got_object_qid *qid;
4471 struct got_commit_object *commit;
4472 struct got_pathlist_head changed_paths;
4474 STAILQ_INIT(&reversed_commits);
4475 TAILQ_INIT(&changed_paths);
4477 if (search_pattern && regcomp(®ex, search_pattern,
4478 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4479 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4481 err = got_commit_graph_open(&graph, path, !log_branches);
4484 if (log_branches && toposort) {
4485 err = got_commit_graph_toposort(graph, root_id, repo,
4486 check_cancelled, NULL);
4488 err = got_commit_graph_bfsort(graph, root_id, repo,
4489 check_cancelled, NULL);
4494 struct got_object_id id;
4495 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4496 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4498 if (sigint_received || sigpipe_received)
4501 err = got_commit_graph_iter_next(&id, graph, repo,
4502 check_cancelled, NULL);
4504 if (err->code == GOT_ERR_ITER_COMPLETED)
4509 err = got_object_open_as_commit(&commit, repo, &id);
4513 if (((show_changed_paths && !show_diffstat) ||
4514 (show_diffstat && !show_patch))
4515 && !reverse_display_order) {
4516 err = get_changed_paths(&changed_paths, commit, repo,
4517 show_diffstat ? &dsa : NULL);
4522 if (search_pattern) {
4523 err = match_commit(&have_match, &id, commit, ®ex);
4525 got_object_commit_close(commit);
4528 if (have_match == 0 && show_changed_paths)
4529 match_changed_paths(&have_match,
4530 &changed_paths, ®ex);
4531 if (have_match == 0 && show_patch) {
4532 err = match_patch(&have_match, commit, &id,
4533 path, diff_context, repo, ®ex, tmpfile);
4537 if (have_match == 0) {
4538 got_object_commit_close(commit);
4539 got_pathlist_free(&changed_paths,
4540 GOT_PATHLIST_FREE_ALL);
4545 if (reverse_display_order) {
4546 err = got_object_qid_alloc(&qid, &id);
4549 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4550 got_object_commit_close(commit);
4553 err = print_commit_oneline(commit, &id,
4556 err = print_commit(commit, &id, repo, path,
4557 (show_changed_paths || show_diffstat) ?
4558 &changed_paths : NULL,
4559 show_diffstat ? &dsa : NULL, show_patch,
4560 diff_context, refs_idmap, NULL, NULL);
4561 got_object_commit_close(commit);
4565 if ((limit && --limit == 0) ||
4566 (end_id && got_object_id_cmp(&id, end_id) == 0))
4569 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4571 if (reverse_display_order) {
4572 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4573 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4574 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4576 err = got_object_open_as_commit(&commit, repo,
4580 if ((show_changed_paths && !show_diffstat) ||
4581 (show_diffstat && !show_patch)) {
4582 err = get_changed_paths(&changed_paths, commit,
4583 repo, show_diffstat ? &dsa : NULL);
4588 err = print_commit_oneline(commit, &qid->id,
4591 err = print_commit(commit, &qid->id, repo, path,
4592 (show_changed_paths || show_diffstat) ?
4593 &changed_paths : NULL,
4594 show_diffstat ? &dsa : NULL, show_patch,
4595 diff_context, refs_idmap, NULL, NULL);
4596 got_object_commit_close(commit);
4599 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4603 while (!STAILQ_EMPTY(&reversed_commits)) {
4604 qid = STAILQ_FIRST(&reversed_commits);
4605 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4606 got_object_qid_free(qid);
4608 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4611 got_commit_graph_close(graph);
4618 fprintf(stderr, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4619 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4620 "[path]\n", getprogname());
4625 get_default_log_limit(void)
4627 const char *got_default_log_limit;
4631 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4632 if (got_default_log_limit == NULL)
4634 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4640 static const struct got_error *
4641 cmd_log(int argc, char *argv[])
4643 const struct got_error *error;
4644 struct got_repository *repo = NULL;
4645 struct got_worktree *worktree = NULL;
4646 struct got_object_id *start_id = NULL, *end_id = NULL;
4647 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4648 char *keyword_idstr = NULL;
4649 const char *start_commit = NULL, *end_commit = NULL;
4650 const char *search_pattern = NULL;
4651 int diff_context = -1, ch;
4652 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4653 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4656 struct got_reflist_head refs;
4657 struct got_reflist_object_id_map *refs_idmap = NULL;
4658 FILE *tmpfile = NULL;
4659 int *pack_fds = NULL;
4664 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4670 limit = get_default_log_limit();
4672 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:stx:")) != -1) {
4678 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4681 errx(1, "number of context lines is %s: %s",
4685 start_commit = optarg;
4691 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4693 errx(1, "number of commits is %s: %s",
4697 show_changed_paths = 1;
4703 reverse_display_order = 1;
4706 repo_path = realpath(optarg, NULL);
4707 if (repo_path == NULL)
4708 return got_error_from_errno2("realpath",
4710 got_path_strip_trailing_slashes(repo_path);
4713 search_pattern = optarg;
4722 end_commit = optarg;
4733 if (diff_context == -1)
4735 else if (!show_patch)
4736 errx(1, "-C requires -p");
4738 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4739 errx(1, "cannot use -s with -d, -p or -P");
4741 cwd = getcwd(NULL, 0);
4743 error = got_error_from_errno("getcwd");
4747 error = got_repo_pack_fds_open(&pack_fds);
4751 if (repo_path == NULL) {
4752 error = got_worktree_open(&worktree, cwd,
4753 GOT_WORKTREE_GOT_DIR);
4754 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4761 error = got_worktree_resolve_path(&path, worktree,
4766 path = strdup(argv[0]);
4768 error = got_error_from_errno("strdup");
4772 } else if (argc != 0)
4775 if (repo_path == NULL) {
4776 repo_path = worktree ?
4777 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4779 if (repo_path == NULL) {
4780 error = got_error_from_errno("strdup");
4784 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4788 error = apply_unveil(got_repo_get_path(repo), 1,
4789 worktree ? got_worktree_get_root_path(worktree) : NULL);
4793 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4797 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4801 if (start_commit == NULL) {
4802 struct got_reference *head_ref;
4803 struct got_commit_object *commit = NULL;
4804 error = got_ref_open(&head_ref, repo,
4805 worktree ? got_worktree_get_head_ref_name(worktree)
4809 error = got_ref_resolve(&start_id, repo, head_ref);
4810 got_ref_close(head_ref);
4813 error = got_object_open_as_commit(&commit, repo,
4817 got_object_commit_close(commit);
4819 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4823 if (keyword_idstr != NULL)
4824 start_commit = keyword_idstr;
4826 error = got_repo_match_object_id(&start_id, NULL,
4827 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4831 if (end_commit != NULL) {
4832 error = got_keyword_to_idstr(&keyword_idstr, end_commit,
4836 if (keyword_idstr != NULL)
4837 end_commit = keyword_idstr;
4839 error = got_repo_match_object_id(&end_id, NULL,
4840 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4847 * If a path was specified on the command line it was resolved
4848 * to a path in the work tree above. Prepend the work tree's
4849 * path prefix to obtain the corresponding in-repository path.
4853 prefix = got_worktree_get_path_prefix(worktree);
4854 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4855 (path[0] != '\0') ? "/" : "", path) == -1) {
4856 error = got_error_from_errno("asprintf");
4861 error = got_repo_map_path(&in_repo_path, repo,
4867 path = in_repo_path;
4871 /* Release work tree lock. */
4872 got_worktree_close(worktree);
4876 if (search_pattern && show_patch) {
4877 tmpfile = got_opentemp();
4878 if (tmpfile == NULL) {
4879 error = got_error_from_errno("got_opentemp");
4884 error = print_commits(start_id, end_id, repo, path ? path : "",
4885 show_changed_paths, show_diffstat, show_patch, search_pattern,
4886 diff_context, limit, log_branches, reverse_display_order,
4887 refs_idmap, one_line, toposort, tmpfile);
4894 free(keyword_idstr);
4896 got_worktree_close(worktree);
4898 const struct got_error *close_err = got_repo_close(repo);
4903 const struct got_error *pack_err =
4904 got_repo_pack_fds_close(pack_fds);
4909 got_reflist_object_id_map_free(refs_idmap);
4910 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4911 error = got_error_from_errno("fclose");
4912 got_ref_list_free(&refs);
4919 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4920 "[-r repository-path] [object1 object2 | path ...]\n",
4925 struct print_diff_arg {
4926 struct got_repository *repo;
4927 struct got_worktree *worktree;
4928 struct got_diffstat_cb_arg *diffstat;
4933 enum got_diff_algorithm diff_algo;
4934 int ignore_whitespace;
4935 int force_text_diff;
4942 * Create a file which contains the target path of a symlink so we can feed
4943 * it as content to the diff engine.
4945 static const struct got_error *
4946 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4947 const char *abspath)
4949 const struct got_error *err = NULL;
4950 char target_path[PATH_MAX];
4951 ssize_t target_len, outlen;
4956 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4957 if (target_len == -1)
4958 return got_error_from_errno2("readlinkat", abspath);
4960 target_len = readlink(abspath, target_path, PATH_MAX);
4961 if (target_len == -1)
4962 return got_error_from_errno2("readlink", abspath);
4965 *fd = got_opentempfd();
4967 return got_error_from_errno("got_opentempfd");
4969 outlen = write(*fd, target_path, target_len);
4971 err = got_error_from_errno("got_opentempfd");
4975 if (lseek(*fd, 0, SEEK_SET) == -1) {
4976 err = got_error_from_errno2("lseek", abspath);
4987 static const struct got_error *
4988 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4989 const char *path, struct got_object_id *blob_id,
4990 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4991 int dirfd, const char *de_name)
4993 struct print_diff_arg *a = arg;
4994 const struct got_error *err = NULL;
4995 struct got_blob_object *blob1 = NULL;
4996 int fd = -1, fd1 = -1, fd2 = -1;
4998 char *abspath = NULL, *label1 = NULL;
5003 memset(&sb, 0, sizeof(sb));
5005 if (a->diff_staged) {
5006 if (staged_status != GOT_STATUS_MODIFY &&
5007 staged_status != GOT_STATUS_ADD &&
5008 staged_status != GOT_STATUS_DELETE)
5011 if (staged_status == GOT_STATUS_DELETE)
5013 if (status == GOT_STATUS_NONEXISTENT)
5014 return got_error_set_errno(ENOENT, path);
5015 if (status != GOT_STATUS_MODIFY &&
5016 status != GOT_STATUS_ADD &&
5017 status != GOT_STATUS_DELETE &&
5018 status != GOT_STATUS_CONFLICT)
5022 err = got_opentemp_truncate(a->f1);
5024 return got_error_from_errno("got_opentemp_truncate");
5025 err = got_opentemp_truncate(a->f2);
5027 return got_error_from_errno("got_opentemp_truncate");
5029 if (!a->header_shown) {
5030 if (fprintf(a->outfile, "diff %s%s\n",
5031 a->diff_staged ? "-s " : "",
5032 got_worktree_get_root_path(a->worktree)) < 0) {
5033 err = got_error_from_errno("fprintf");
5036 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
5037 err = got_error_from_errno("fprintf");
5040 if (fprintf(a->outfile, "path + %s%s\n",
5041 got_worktree_get_root_path(a->worktree),
5042 a->diff_staged ? " (staged changes)" : "") < 0) {
5043 err = got_error_from_errno("fprintf");
5046 a->header_shown = 1;
5049 if (a->diff_staged) {
5050 const char *label1 = NULL, *label2 = NULL;
5051 switch (staged_status) {
5052 case GOT_STATUS_MODIFY:
5056 case GOT_STATUS_ADD:
5059 case GOT_STATUS_DELETE:
5063 return got_error(GOT_ERR_FILE_STATUS);
5065 fd1 = got_opentempfd();
5067 err = got_error_from_errno("got_opentempfd");
5070 fd2 = got_opentempfd();
5072 err = got_error_from_errno("got_opentempfd");
5075 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5076 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5077 a->diff_algo, a->diff_context, a->ignore_whitespace,
5078 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5082 fd1 = got_opentempfd();
5084 err = got_error_from_errno("got_opentempfd");
5088 if (staged_status == GOT_STATUS_ADD ||
5089 staged_status == GOT_STATUS_MODIFY) {
5091 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5095 err = got_object_id_str(&id_str, staged_blob_id);
5098 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5099 err = got_error_from_errno("asprintf");
5104 } else if (status != GOT_STATUS_ADD) {
5105 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5111 if (status != GOT_STATUS_DELETE) {
5112 if (asprintf(&abspath, "%s/%s",
5113 got_worktree_get_root_path(a->worktree), path) == -1) {
5114 err = got_error_from_errno("asprintf");
5119 fd = openat(dirfd, de_name,
5120 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5122 if (!got_err_open_nofollow_on_symlink()) {
5123 err = got_error_from_errno2("openat",
5127 err = get_symlink_target_file(&fd, dirfd,
5133 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5135 if (!got_err_open_nofollow_on_symlink()) {
5136 err = got_error_from_errno2("open",
5140 err = get_symlink_target_file(&fd, dirfd,
5146 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5147 err = got_error_from_errno2("fstatat", abspath);
5150 f2 = fdopen(fd, "r");
5152 err = got_error_from_errno2("fdopen", abspath);
5160 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5166 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5167 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5168 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5170 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5171 err = got_error_from_errno("close");
5172 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5173 err = got_error_from_errno("close");
5175 got_object_blob_close(blob1);
5176 if (fd != -1 && close(fd) == -1 && err == NULL)
5177 err = got_error_from_errno("close");
5178 if (f2 && fclose(f2) == EOF && err == NULL)
5179 err = got_error_from_errno("fclose");
5184 static const struct got_error *
5185 cmd_diff(int argc, char *argv[])
5187 const struct got_error *error;
5188 struct got_repository *repo = NULL;
5189 struct got_worktree *worktree = NULL;
5190 char *cwd = NULL, *repo_path = NULL;
5191 const char *commit_args[2] = { NULL, NULL };
5192 int ncommit_args = 0;
5193 struct got_object_id *ids[2] = { NULL, NULL };
5194 char *labels[2] = { NULL, NULL };
5195 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5196 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5197 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5199 struct got_reflist_head refs;
5200 struct got_pathlist_head diffstat_paths, paths;
5201 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5202 int fd1 = -1, fd2 = -1;
5203 int *pack_fds = NULL;
5204 struct got_diffstat_cb_arg dsa;
5206 memset(&dsa, 0, sizeof(dsa));
5210 TAILQ_INIT(&diffstat_paths);
5213 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5218 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5221 force_text_diff = 1;
5224 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5227 errx(1, "number of context lines is %s: %s",
5231 if (ncommit_args >= 2)
5232 errx(1, "too many -c options used");
5233 commit_args[ncommit_args++] = optarg;
5242 repo_path = realpath(optarg, NULL);
5243 if (repo_path == NULL)
5244 return got_error_from_errno2("realpath",
5246 got_path_strip_trailing_slashes(repo_path);
5253 ignore_whitespace = 1;
5264 cwd = getcwd(NULL, 0);
5266 error = got_error_from_errno("getcwd");
5270 error = got_repo_pack_fds_open(&pack_fds);
5274 if (repo_path == NULL) {
5275 error = got_worktree_open(&worktree, cwd,
5276 GOT_WORKTREE_GOT_DIR);
5277 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5283 strdup(got_worktree_get_repo_path(worktree));
5284 if (repo_path == NULL) {
5285 error = got_error_from_errno("strdup");
5289 repo_path = strdup(cwd);
5290 if (repo_path == NULL) {
5291 error = got_error_from_errno("strdup");
5297 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5302 if (show_diffstat) {
5303 dsa.paths = &diffstat_paths;
5304 dsa.force_text = force_text_diff;
5305 dsa.ignore_ws = ignore_whitespace;
5306 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5309 if (rflag || worktree == NULL || ncommit_args > 0) {
5311 error = got_error_msg(GOT_ERR_NOT_IMPL,
5312 "-P option can only be used when diffing "
5317 error = got_error_msg(GOT_ERR_NOT_IMPL,
5318 "-s option can only be used when diffing "
5324 error = apply_unveil(got_repo_get_path(repo), 1,
5325 worktree ? got_worktree_get_root_path(worktree) : NULL);
5329 if ((!force_path && argc == 2) || ncommit_args > 0) {
5330 int obj_type = (ncommit_args > 0 ?
5331 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5332 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5336 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5338 char *keyword_idstr = NULL;
5340 if (ncommit_args > 0)
5341 arg = commit_args[i];
5345 error = got_keyword_to_idstr(&keyword_idstr, arg,
5349 if (keyword_idstr != NULL)
5350 arg = keyword_idstr;
5352 error = got_repo_match_object_id(&ids[i], &labels[i],
5353 arg, obj_type, &refs, repo);
5354 free(keyword_idstr);
5356 if (error->code != GOT_ERR_NOT_REF &&
5357 error->code != GOT_ERR_NO_OBJ)
5359 if (ncommit_args > 0)
5367 f1 = got_opentemp();
5369 error = got_error_from_errno("got_opentemp");
5373 f2 = got_opentemp();
5375 error = got_error_from_errno("got_opentemp");
5379 outfile = got_opentemp();
5380 if (outfile == NULL) {
5381 error = got_error_from_errno("got_opentemp");
5385 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5386 struct print_diff_arg arg;
5389 if (worktree == NULL) {
5390 if (argc == 2 && ids[0] == NULL) {
5391 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5393 } else if (argc == 2 && ids[1] == NULL) {
5394 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5396 } else if (argc > 0) {
5397 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5398 "%s", "specified paths cannot be resolved");
5401 error = got_error(GOT_ERR_NOT_WORKTREE);
5406 error = get_worktree_paths_from_argv(&paths, argc, argv,
5411 error = got_object_id_str(&id_str,
5412 got_worktree_get_base_commit_id(worktree));
5416 arg.worktree = worktree;
5417 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5418 arg.diff_context = diff_context;
5419 arg.id_str = id_str;
5420 arg.header_shown = 0;
5421 arg.diff_staged = diff_staged;
5422 arg.ignore_whitespace = ignore_whitespace;
5423 arg.force_text_diff = force_text_diff;
5424 arg.diffstat = show_diffstat ? &dsa : NULL;
5427 arg.outfile = outfile;
5429 error = got_worktree_status(worktree, &paths, repo, 0,
5430 print_diff, &arg, check_cancelled, NULL);
5435 if (show_diffstat && dsa.nfiles > 0) {
5438 if (asprintf(&header, "diffstat %s%s",
5439 diff_staged ? "-s " : "",
5440 got_worktree_get_root_path(worktree)) == -1) {
5441 error = got_error_from_errno("asprintf");
5445 error = print_diffstat(&dsa, header);
5451 error = printfile(outfile);
5455 if (ncommit_args == 1) {
5456 struct got_commit_object *commit;
5457 error = got_object_open_as_commit(&commit, repo, ids[0]);
5461 labels[1] = labels[0];
5463 if (got_object_commit_get_nparents(commit) > 0) {
5464 const struct got_object_id_queue *pids;
5465 struct got_object_qid *pid;
5466 pids = got_object_commit_get_parent_ids(commit);
5467 pid = STAILQ_FIRST(pids);
5468 ids[0] = got_object_id_dup(&pid->id);
5469 if (ids[0] == NULL) {
5470 error = got_error_from_errno(
5471 "got_object_id_dup");
5472 got_object_commit_close(commit);
5475 error = got_object_id_str(&labels[0], ids[0]);
5477 got_object_commit_close(commit);
5482 labels[0] = strdup("/dev/null");
5483 if (labels[0] == NULL) {
5484 error = got_error_from_errno("strdup");
5485 got_object_commit_close(commit);
5490 got_object_commit_close(commit);
5493 if (ncommit_args == 0 && argc > 2) {
5494 error = got_error_msg(GOT_ERR_BAD_PATH,
5495 "path arguments cannot be used when diffing two objects");
5500 error = got_object_get_type(&type1, repo, ids[0]);
5505 error = got_object_get_type(&type2, repo, ids[1]);
5508 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5509 error = got_error(GOT_ERR_OBJ_TYPE);
5512 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5513 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5514 "path arguments cannot be used when diffing blobs");
5518 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5520 struct got_pathlist_entry *new;
5524 error = got_worktree_resolve_path(&p, worktree,
5528 prefix = got_worktree_get_path_prefix(worktree);
5529 while (prefix[0] == '/')
5531 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5532 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5534 error = got_error_from_errno("asprintf");
5540 char *mapped_path, *s;
5541 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5547 in_repo_path = strdup(s);
5548 if (in_repo_path == NULL) {
5549 error = got_error_from_errno("asprintf");
5556 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5557 if (error || new == NULL /* duplicate */)
5564 /* Release work tree lock. */
5565 got_worktree_close(worktree);
5569 fd1 = got_opentempfd();
5571 error = got_error_from_errno("got_opentempfd");
5575 fd2 = got_opentempfd();
5577 error = got_error_from_errno("got_opentempfd");
5581 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5582 case GOT_OBJ_TYPE_BLOB:
5583 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5584 fd1, fd2, ids[0], ids[1], NULL, NULL,
5585 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5586 ignore_whitespace, force_text_diff,
5587 show_diffstat ? &dsa : NULL, repo, outfile);
5589 case GOT_OBJ_TYPE_TREE:
5590 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5591 ids[0], ids[1], &paths, "", "",
5592 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5593 ignore_whitespace, force_text_diff,
5594 show_diffstat ? &dsa : NULL, repo, outfile);
5596 case GOT_OBJ_TYPE_COMMIT:
5597 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5598 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5599 fd1, fd2, ids[0], ids[1], &paths,
5600 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5601 ignore_whitespace, force_text_diff,
5602 show_diffstat ? &dsa : NULL, repo, outfile);
5605 error = got_error(GOT_ERR_OBJ_TYPE);
5610 if (show_diffstat && dsa.nfiles > 0) {
5611 char *header = NULL;
5613 if (asprintf(&header, "diffstat %s %s",
5614 labels[0], labels[1]) == -1) {
5615 error = got_error_from_errno("asprintf");
5619 error = print_diffstat(&dsa, header);
5625 error = printfile(outfile);
5633 got_worktree_close(worktree);
5635 const struct got_error *close_err = got_repo_close(repo);
5640 const struct got_error *pack_err =
5641 got_repo_pack_fds_close(pack_fds);
5645 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5646 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5647 got_ref_list_free(&refs);
5648 if (outfile && fclose(outfile) == EOF && error == NULL)
5649 error = got_error_from_errno("fclose");
5650 if (f1 && fclose(f1) == EOF && error == NULL)
5651 error = got_error_from_errno("fclose");
5652 if (f2 && fclose(f2) == EOF && error == NULL)
5653 error = got_error_from_errno("fclose");
5654 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5655 error = got_error_from_errno("close");
5656 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5657 error = got_error_from_errno("close");
5665 "usage: %s blame [-c commit] [-r repository-path] path\n",
5674 char datebuf[11]; /* YYYY-MM-DD + NUL */
5677 struct blame_cb_args {
5678 struct blame_line *lines;
5682 off_t *line_offsets;
5684 struct got_repository *repo;
5687 static const struct got_error *
5688 blame_cb(void *arg, int nlines, int lineno,
5689 struct got_commit_object *commit, struct got_object_id *id)
5691 const struct got_error *err = NULL;
5692 struct blame_cb_args *a = arg;
5693 struct blame_line *bline;
5695 size_t linesize = 0;
5698 time_t committer_time;
5700 if (nlines != a->nlines ||
5701 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5702 return got_error(GOT_ERR_RANGE);
5704 if (sigint_received)
5705 return got_error(GOT_ERR_ITER_COMPLETED);
5708 return NULL; /* no change in this commit */
5710 /* Annotate this line. */
5711 bline = &a->lines[lineno - 1];
5712 if (bline->annotated)
5714 err = got_object_id_str(&bline->id_str, id);
5718 bline->committer = strdup(got_object_commit_get_committer(commit));
5719 if (bline->committer == NULL) {
5720 err = got_error_from_errno("strdup");
5724 committer_time = got_object_commit_get_committer_time(commit);
5725 if (gmtime_r(&committer_time, &tm) == NULL)
5726 return got_error_from_errno("gmtime_r");
5727 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5729 err = got_error(GOT_ERR_NO_SPACE);
5732 bline->annotated = 1;
5734 /* Print lines annotated so far. */
5735 bline = &a->lines[a->lineno_cur - 1];
5736 if (!bline->annotated)
5739 offset = a->line_offsets[a->lineno_cur - 1];
5740 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5741 err = got_error_from_errno("fseeko");
5745 while (a->lineno_cur <= a->nlines && bline->annotated) {
5746 char *smallerthan, *at, *nl, *committer;
5749 if (getline(&line, &linesize, a->f) == -1) {
5751 err = got_error_from_errno("getline");
5755 committer = bline->committer;
5756 smallerthan = strchr(committer, '<');
5757 if (smallerthan && smallerthan[1] != '\0')
5758 committer = smallerthan + 1;
5759 at = strchr(committer, '@');
5762 len = strlen(committer);
5764 committer[8] = '\0';
5766 nl = strchr(line, '\n');
5769 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5770 bline->id_str, bline->datebuf, committer, line);
5773 bline = &a->lines[a->lineno_cur - 1];
5780 static const struct got_error *
5781 cmd_blame(int argc, char *argv[])
5783 const struct got_error *error;
5784 struct got_repository *repo = NULL;
5785 struct got_worktree *worktree = NULL;
5786 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5787 char *link_target = NULL;
5788 struct got_object_id *obj_id = NULL;
5789 struct got_object_id *commit_id = NULL;
5790 struct got_commit_object *commit = NULL;
5791 struct got_blob_object *blob = NULL;
5792 char *commit_id_str = NULL, *keyword_idstr = NULL;
5793 struct blame_cb_args bca;
5794 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5796 int *pack_fds = NULL;
5797 FILE *f1 = NULL, *f2 = NULL;
5799 fd1 = got_opentempfd();
5801 return got_error_from_errno("got_opentempfd");
5803 memset(&bca, 0, sizeof(bca));
5806 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5811 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5814 commit_id_str = optarg;
5817 repo_path = realpath(optarg, NULL);
5818 if (repo_path == NULL)
5819 return got_error_from_errno2("realpath",
5821 got_path_strip_trailing_slashes(repo_path);
5837 cwd = getcwd(NULL, 0);
5839 error = got_error_from_errno("getcwd");
5843 error = got_repo_pack_fds_open(&pack_fds);
5847 if (repo_path == NULL) {
5848 error = got_worktree_open(&worktree, cwd,
5849 GOT_WORKTREE_GOT_DIR);
5850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5856 strdup(got_worktree_get_repo_path(worktree));
5857 if (repo_path == NULL) {
5858 error = got_error_from_errno("strdup");
5863 repo_path = strdup(cwd);
5864 if (repo_path == NULL) {
5865 error = got_error_from_errno("strdup");
5871 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5876 const char *prefix = got_worktree_get_path_prefix(worktree);
5879 error = got_worktree_resolve_path(&p, worktree, path);
5882 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5883 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5885 error = got_error_from_errno("asprintf");
5890 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5892 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5895 error = got_repo_map_path(&in_repo_path, repo, path);
5900 if (commit_id_str == NULL) {
5901 struct got_reference *head_ref;
5902 error = got_ref_open(&head_ref, repo, worktree ?
5903 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5906 error = got_ref_resolve(&commit_id, repo, head_ref);
5907 got_ref_close(head_ref);
5911 struct got_reflist_head refs;
5914 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5919 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5923 if (keyword_idstr != NULL)
5924 commit_id_str = keyword_idstr;
5926 error = got_repo_match_object_id(&commit_id, NULL,
5927 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5928 got_ref_list_free(&refs);
5934 /* Release work tree lock. */
5935 got_worktree_close(worktree);
5939 error = got_object_open_as_commit(&commit, repo, commit_id);
5943 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5948 error = got_object_id_by_path(&obj_id, repo, commit,
5949 link_target ? link_target : in_repo_path);
5953 error = got_object_get_type(&obj_type, repo, obj_id);
5957 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5958 error = got_error_path(link_target ? link_target : in_repo_path,
5963 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5966 bca.f = got_opentemp();
5967 if (bca.f == NULL) {
5968 error = got_error_from_errno("got_opentemp");
5971 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5972 &bca.line_offsets, bca.f, blob);
5973 if (error || bca.nlines == 0)
5976 /* Don't include \n at EOF in the blame line count. */
5977 if (bca.line_offsets[bca.nlines - 1] == filesize)
5980 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5981 if (bca.lines == NULL) {
5982 error = got_error_from_errno("calloc");
5986 bca.nlines_prec = 0;
5994 fd2 = got_opentempfd();
5996 error = got_error_from_errno("got_opentempfd");
5999 fd3 = got_opentempfd();
6001 error = got_error_from_errno("got_opentempfd");
6004 f1 = got_opentemp();
6006 error = got_error_from_errno("got_opentemp");
6009 f2 = got_opentemp();
6011 error = got_error_from_errno("got_opentemp");
6014 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
6015 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
6016 check_cancelled, NULL, fd2, fd3, f1, f2);
6018 free(keyword_idstr);
6026 got_object_commit_close(commit);
6028 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
6029 error = got_error_from_errno("close");
6030 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
6031 error = got_error_from_errno("close");
6032 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
6033 error = got_error_from_errno("close");
6034 if (f1 && fclose(f1) == EOF && error == NULL)
6035 error = got_error_from_errno("fclose");
6036 if (f2 && fclose(f2) == EOF && error == NULL)
6037 error = got_error_from_errno("fclose");
6040 got_object_blob_close(blob);
6042 got_worktree_close(worktree);
6044 const struct got_error *close_err = got_repo_close(repo);
6049 const struct got_error *pack_err =
6050 got_repo_pack_fds_close(pack_fds);
6055 for (i = 0; i < bca.nlines; i++) {
6056 struct blame_line *bline = &bca.lines[i];
6057 free(bline->id_str);
6058 free(bline->committer);
6062 free(bca.line_offsets);
6063 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6064 error = got_error_from_errno("fclose");
6071 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6072 "[path]\n", getprogname());
6076 static const struct got_error *
6077 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6078 const char *root_path, struct got_repository *repo)
6080 const struct got_error *err = NULL;
6081 int is_root_path = (strcmp(path, root_path) == 0);
6082 const char *modestr = "";
6083 mode_t mode = got_tree_entry_get_mode(te);
6084 char *link_target = NULL;
6086 path += strlen(root_path);
6087 while (path[0] == '/')
6090 if (got_object_tree_entry_is_submodule(te))
6092 else if (S_ISLNK(mode)) {
6095 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6098 for (i = 0; link_target[i] != '\0'; i++) {
6099 if (!isprint((unsigned char)link_target[i]))
6100 link_target[i] = '?';
6105 else if (S_ISDIR(mode))
6107 else if (mode & S_IXUSR)
6110 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6111 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6112 link_target ? " -> ": "", link_target ? link_target : "");
6118 static const struct got_error *
6119 print_tree(const char *path, struct got_commit_object *commit,
6120 int show_ids, int recurse, const char *root_path,
6121 struct got_repository *repo)
6123 const struct got_error *err = NULL;
6124 struct got_object_id *tree_id = NULL;
6125 struct got_tree_object *tree = NULL;
6128 err = got_object_id_by_path(&tree_id, repo, commit, path);
6132 err = got_object_open_as_tree(&tree, repo, tree_id);
6135 nentries = got_object_tree_get_nentries(tree);
6136 for (i = 0; i < nentries; i++) {
6137 struct got_tree_entry *te;
6140 if (sigint_received || sigpipe_received)
6143 te = got_object_tree_get_entry(tree, i);
6146 err = got_object_id_str(&id_str,
6147 got_tree_entry_get_id(te));
6150 if (asprintf(&id, "%s ", id_str) == -1) {
6151 err = got_error_from_errno("asprintf");
6157 err = print_entry(te, id, path, root_path, repo);
6162 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6164 if (asprintf(&child_path, "%s%s%s", path,
6165 path[0] == '/' && path[1] == '\0' ? "" : "/",
6166 got_tree_entry_get_name(te)) == -1) {
6167 err = got_error_from_errno("asprintf");
6170 err = print_tree(child_path, commit, show_ids, 1,
6179 got_object_tree_close(tree);
6184 static const struct got_error *
6185 cmd_tree(int argc, char *argv[])
6187 const struct got_error *error;
6188 struct got_repository *repo = NULL;
6189 struct got_worktree *worktree = NULL;
6190 const char *path, *refname = NULL;
6191 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6192 struct got_object_id *commit_id = NULL;
6193 struct got_commit_object *commit = NULL;
6194 char *commit_id_str = NULL, *keyword_idstr = NULL;
6195 int show_ids = 0, recurse = 0;
6197 int *pack_fds = NULL;
6200 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6205 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6208 commit_id_str = optarg;
6217 repo_path = realpath(optarg, NULL);
6218 if (repo_path == NULL)
6219 return got_error_from_errno2("realpath",
6221 got_path_strip_trailing_slashes(repo_path);
6239 cwd = getcwd(NULL, 0);
6241 error = got_error_from_errno("getcwd");
6245 error = got_repo_pack_fds_open(&pack_fds);
6249 if (repo_path == NULL) {
6250 error = got_worktree_open(&worktree, cwd,
6251 GOT_WORKTREE_GOT_DIR);
6252 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6258 strdup(got_worktree_get_repo_path(worktree));
6259 if (repo_path == NULL)
6260 error = got_error_from_errno("strdup");
6264 repo_path = strdup(cwd);
6265 if (repo_path == NULL) {
6266 error = got_error_from_errno("strdup");
6272 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6277 const char *prefix = got_worktree_get_path_prefix(worktree);
6280 if (path == NULL || got_path_is_root_dir(path))
6282 error = got_worktree_resolve_path(&p, worktree, path);
6285 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6286 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6288 error = got_error_from_errno("asprintf");
6293 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6297 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6302 error = got_repo_map_path(&in_repo_path, repo, path);
6307 if (commit_id_str == NULL) {
6308 struct got_reference *head_ref;
6310 refname = got_worktree_get_head_ref_name(worktree);
6312 refname = GOT_REF_HEAD;
6313 error = got_ref_open(&head_ref, repo, refname, 0);
6316 error = got_ref_resolve(&commit_id, repo, head_ref);
6317 got_ref_close(head_ref);
6321 struct got_reflist_head refs;
6324 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6329 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6333 if (keyword_idstr != NULL)
6334 commit_id_str = keyword_idstr;
6336 error = got_repo_match_object_id(&commit_id, NULL,
6337 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6338 got_ref_list_free(&refs);
6344 /* Release work tree lock. */
6345 got_worktree_close(worktree);
6349 error = got_object_open_as_commit(&commit, repo, commit_id);
6353 error = print_tree(in_repo_path, commit, show_ids, recurse,
6354 in_repo_path, repo);
6356 free(keyword_idstr);
6362 got_object_commit_close(commit);
6364 got_worktree_close(worktree);
6366 const struct got_error *close_err = got_repo_close(repo);
6371 const struct got_error *pack_err =
6372 got_repo_pack_fds_close(pack_fds);
6382 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6383 "[-s status-codes] [path ...]\n", getprogname());
6387 struct got_status_arg {
6392 static const struct got_error *
6393 print_status(void *arg, unsigned char status, unsigned char staged_status,
6394 const char *path, struct got_object_id *blob_id,
6395 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6396 int dirfd, const char *de_name)
6398 struct got_status_arg *st = arg;
6400 if (status == staged_status && (status == GOT_STATUS_DELETE))
6401 status = GOT_STATUS_NO_CHANGE;
6402 if (st != NULL && st->status_codes) {
6403 size_t ncodes = strlen(st->status_codes);
6406 for (i = 0; i < ncodes ; i++) {
6408 if (status == st->status_codes[i] ||
6409 staged_status == st->status_codes[i]) {
6414 if (status == st->status_codes[i] ||
6415 staged_status == st->status_codes[i])
6420 if (st->suppress && j == 0)
6427 printf("%c%c %s\n", status, staged_status, path);
6431 static const struct got_error *
6432 cmd_status(int argc, char *argv[])
6434 const struct got_error *close_err, *error = NULL;
6435 struct got_repository *repo = NULL;
6436 struct got_worktree *worktree = NULL;
6437 struct got_status_arg st;
6439 struct got_pathlist_head paths;
6440 int ch, i, no_ignores = 0;
6441 int *pack_fds = NULL;
6445 memset(&st, 0, sizeof(st));
6446 st.status_codes = NULL;
6450 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6455 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6461 if (st.status_codes != NULL && st.suppress == 0)
6462 option_conflict('S', 's');
6466 for (i = 0; optarg[i] != '\0'; i++) {
6467 switch (optarg[i]) {
6468 case GOT_STATUS_MODIFY:
6469 case GOT_STATUS_ADD:
6470 case GOT_STATUS_DELETE:
6471 case GOT_STATUS_CONFLICT:
6472 case GOT_STATUS_MISSING:
6473 case GOT_STATUS_OBSTRUCTED:
6474 case GOT_STATUS_UNVERSIONED:
6475 case GOT_STATUS_MODE_CHANGE:
6476 case GOT_STATUS_NONEXISTENT:
6479 errx(1, "invalid status code '%c'",
6483 if (ch == 's' && st.suppress)
6484 option_conflict('s', 'S');
6485 st.status_codes = optarg;
6496 cwd = getcwd(NULL, 0);
6498 error = got_error_from_errno("getcwd");
6502 error = got_repo_pack_fds_open(&pack_fds);
6506 error = got_worktree_open(&worktree, cwd,
6507 GOT_WORKTREE_GOT_DIR);
6509 if (error->code == GOT_ERR_NOT_WORKTREE)
6510 error = wrap_not_worktree_error(error, "status", cwd);
6514 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6519 error = apply_unveil(got_repo_get_path(repo), 1,
6520 got_worktree_get_root_path(worktree));
6524 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6528 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6529 print_status, &st, check_cancelled, NULL);
6532 const struct got_error *pack_err =
6533 got_repo_pack_fds_close(pack_fds);
6538 close_err = got_repo_close(repo);
6542 if (worktree != NULL) {
6543 close_err = got_worktree_close(worktree);
6548 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6556 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6557 "[-s reference] [name]\n", getprogname());
6561 static const struct got_error *
6562 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6564 static const struct got_error *err = NULL;
6565 struct got_reflist_head refs;
6566 struct got_reflist_entry *re;
6569 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6570 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6575 TAILQ_FOREACH(re, &refs, entry) {
6577 refstr = got_ref_to_str(re->ref);
6578 if (refstr == NULL) {
6579 err = got_error_from_errno("got_ref_to_str");
6582 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6586 got_ref_list_free(&refs);
6590 static const struct got_error *
6591 delete_ref_by_name(struct got_repository *repo, const char *refname)
6593 const struct got_error *err;
6594 struct got_reference *ref;
6596 err = got_ref_open(&ref, repo, refname, 0);
6600 err = delete_ref(repo, ref);
6605 static const struct got_error *
6606 add_ref(struct got_repository *repo, const char *refname, const char *target)
6608 const struct got_error *err = NULL;
6609 struct got_object_id *id = NULL;
6610 struct got_reference *ref = NULL;
6611 struct got_reflist_head refs;
6614 * Don't let the user create a reference name with a leading '-'.
6615 * While technically a valid reference name, this case is usually
6616 * an unintended typo.
6618 if (refname[0] == '-')
6619 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6622 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6625 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6627 got_ref_list_free(&refs);
6631 err = got_ref_alloc(&ref, refname, id);
6635 err = got_ref_write(ref, repo);
6643 static const struct got_error *
6644 add_symref(struct got_repository *repo, const char *refname, const char *target)
6646 const struct got_error *err = NULL;
6647 struct got_reference *ref = NULL;
6648 struct got_reference *target_ref = NULL;
6651 * Don't let the user create a reference name with a leading '-'.
6652 * While technically a valid reference name, this case is usually
6653 * an unintended typo.
6655 if (refname[0] == '-')
6656 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6658 err = got_ref_open(&target_ref, repo, target, 0);
6662 err = got_ref_alloc_symref(&ref, refname, target_ref);
6666 err = got_ref_write(ref, repo);
6669 got_ref_close(target_ref);
6675 static const struct got_error *
6676 cmd_ref(int argc, char *argv[])
6678 const struct got_error *error = NULL;
6679 struct got_repository *repo = NULL;
6680 struct got_worktree *worktree = NULL;
6681 char *cwd = NULL, *repo_path = NULL;
6682 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6683 const char *obj_arg = NULL, *symref_target= NULL;
6684 char *refname = NULL, *keyword_idstr = NULL;
6685 int *pack_fds = NULL;
6688 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6689 "sendfd unveil", NULL) == -1)
6693 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6705 repo_path = realpath(optarg, NULL);
6706 if (repo_path == NULL)
6707 return got_error_from_errno2("realpath",
6709 got_path_strip_trailing_slashes(repo_path);
6712 symref_target = optarg;
6723 if (obj_arg && do_list)
6724 option_conflict('c', 'l');
6725 if (obj_arg && do_delete)
6726 option_conflict('c', 'd');
6727 if (obj_arg && symref_target)
6728 option_conflict('c', 's');
6729 if (symref_target && do_delete)
6730 option_conflict('s', 'd');
6731 if (symref_target && do_list)
6732 option_conflict('s', 'l');
6733 if (do_delete && do_list)
6734 option_conflict('d', 'l');
6735 if (sort_by_time && !do_list)
6736 errx(1, "-t option requires -l option");
6742 if (argc != 0 && argc != 1)
6745 refname = strdup(argv[0]);
6746 if (refname == NULL) {
6747 error = got_error_from_errno("strdup");
6754 refname = strdup(argv[0]);
6755 if (refname == NULL) {
6756 error = got_error_from_errno("strdup");
6762 got_path_strip_trailing_slashes(refname);
6764 cwd = getcwd(NULL, 0);
6766 error = got_error_from_errno("getcwd");
6770 error = got_repo_pack_fds_open(&pack_fds);
6774 if (repo_path == NULL) {
6775 error = got_worktree_open(&worktree, cwd,
6776 GOT_WORKTREE_GOT_DIR);
6777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6783 strdup(got_worktree_get_repo_path(worktree));
6784 if (repo_path == NULL)
6785 error = got_error_from_errno("strdup");
6789 repo_path = strdup(cwd);
6790 if (repo_path == NULL) {
6791 error = got_error_from_errno("strdup");
6797 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6803 /* Remove "cpath" promise. */
6804 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6810 error = apply_unveil(got_repo_get_path(repo), do_list,
6811 worktree ? got_worktree_get_root_path(worktree) : NULL);
6816 error = list_refs(repo, refname, sort_by_time);
6818 error = delete_ref_by_name(repo, refname);
6819 else if (symref_target)
6820 error = add_symref(repo, refname, symref_target);
6822 if (obj_arg == NULL)
6825 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6829 if (keyword_idstr != NULL)
6830 obj_arg = keyword_idstr;
6832 error = add_ref(repo, refname, obj_arg);
6837 const struct got_error *close_err = got_repo_close(repo);
6842 got_worktree_close(worktree);
6844 const struct got_error *pack_err =
6845 got_repo_pack_fds_close(pack_fds);
6851 free(keyword_idstr);
6858 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6859 "[-r repository-path] [name]\n", getprogname());
6863 static const struct got_error *
6864 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6865 struct got_reference *ref)
6867 const struct got_error *err = NULL;
6868 const char *refname;
6872 refname = got_ref_get_name(ref);
6873 if (worktree && strcmp(refname,
6874 got_worktree_get_head_ref_name(worktree)) == 0) {
6875 err = got_worktree_get_state(&marker, repo, worktree,
6876 check_cancelled, NULL);
6881 if (strncmp(refname, "refs/heads/", 11) == 0)
6883 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6885 if (strncmp(refname, "refs/remotes/", 13) == 0)
6888 refstr = got_ref_to_str(ref);
6890 return got_error_from_errno("got_ref_to_str");
6892 printf("%c %s: %s\n", marker, refname, refstr);
6897 static const struct got_error *
6898 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6900 const char *refname;
6902 if (worktree == NULL)
6903 return got_error(GOT_ERR_NOT_WORKTREE);
6905 refname = got_worktree_get_head_ref_name(worktree);
6907 if (strncmp(refname, "refs/heads/", 11) == 0)
6909 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6912 printf("%s\n", refname);
6917 static const struct got_error *
6918 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6921 static const struct got_error *err = NULL;
6922 struct got_reflist_head refs;
6923 struct got_reflist_entry *re;
6924 struct got_reference *temp_ref = NULL;
6925 int rebase_in_progress, histedit_in_progress;
6930 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6935 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6940 if (rebase_in_progress || histedit_in_progress) {
6941 err = got_ref_open(&temp_ref, repo,
6942 got_worktree_get_head_ref_name(worktree), 0);
6945 list_branch(repo, worktree, temp_ref);
6946 got_ref_close(temp_ref);
6950 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6951 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6956 TAILQ_FOREACH(re, &refs, entry)
6957 list_branch(repo, worktree, re->ref);
6959 got_ref_list_free(&refs);
6961 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6962 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6967 TAILQ_FOREACH(re, &refs, entry)
6968 list_branch(repo, worktree, re->ref);
6970 got_ref_list_free(&refs);
6975 static const struct got_error *
6976 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6977 const char *branch_name)
6979 const struct got_error *err = NULL;
6980 struct got_reference *ref = NULL;
6981 char *refname, *remote_refname = NULL;
6983 if (strncmp(branch_name, "refs/", 5) == 0)
6985 if (strncmp(branch_name, "heads/", 6) == 0)
6987 else if (strncmp(branch_name, "remotes/", 8) == 0)
6990 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6991 return got_error_from_errno("asprintf");
6993 if (asprintf(&remote_refname, "refs/remotes/%s",
6994 branch_name) == -1) {
6995 err = got_error_from_errno("asprintf");
6999 err = got_ref_open(&ref, repo, refname, 0);
7001 const struct got_error *err2;
7002 if (err->code != GOT_ERR_NOT_REF)
7005 * Keep 'err' intact such that if neither branch exists
7006 * we report "refs/heads" rather than "refs/remotes" in
7007 * our error message.
7009 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7016 strcmp(got_worktree_get_head_ref_name(worktree),
7017 got_ref_get_name(ref)) == 0) {
7018 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7019 "will not delete this work tree's current branch");
7023 err = delete_ref(repo, ref);
7028 free(remote_refname);
7032 static const struct got_error *
7033 add_branch(struct got_repository *repo, const char *branch_name,
7034 struct got_object_id *base_commit_id)
7036 const struct got_error *err = NULL;
7037 struct got_reference *ref = NULL;
7038 char *refname = NULL;
7041 * Don't let the user create a branch name with a leading '-'.
7042 * While technically a valid reference name, this case is usually
7043 * an unintended typo.
7045 if (branch_name[0] == '-')
7046 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7048 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7051 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7052 err = got_error_from_errno("asprintf");
7056 err = got_ref_open(&ref, repo, refname, 0);
7058 err = got_error(GOT_ERR_BRANCH_EXISTS);
7060 } else if (err->code != GOT_ERR_NOT_REF)
7063 err = got_ref_alloc(&ref, refname, base_commit_id);
7067 err = got_ref_write(ref, repo);
7075 static const struct got_error *
7076 cmd_branch(int argc, char *argv[])
7078 const struct got_error *error = NULL;
7079 struct got_repository *repo = NULL;
7080 struct got_worktree *worktree = NULL;
7081 char *cwd = NULL, *repo_path = NULL;
7082 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7083 const char *delref = NULL, *commit_id_arg = NULL;
7084 struct got_reference *ref = NULL;
7085 struct got_pathlist_head paths;
7086 struct got_object_id *commit_id = NULL;
7087 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7088 int *pack_fds = NULL;
7093 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7094 "sendfd unveil", NULL) == -1)
7098 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7101 commit_id_arg = optarg;
7113 repo_path = realpath(optarg, NULL);
7114 if (repo_path == NULL)
7115 return got_error_from_errno2("realpath",
7117 got_path_strip_trailing_slashes(repo_path);
7128 if (do_list && delref)
7129 option_conflict('l', 'd');
7130 if (sort_by_time && !do_list)
7131 errx(1, "-t option requires -l option");
7136 if (!do_list && !delref && argc == 0)
7139 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7140 errx(1, "-c option can only be used when creating a branch");
7142 if (do_list || delref) {
7145 } else if (!do_show && argc != 1)
7148 cwd = getcwd(NULL, 0);
7150 error = got_error_from_errno("getcwd");
7154 error = got_repo_pack_fds_open(&pack_fds);
7158 if (repo_path == NULL) {
7159 error = got_worktree_open(&worktree, cwd,
7160 GOT_WORKTREE_GOT_DIR);
7161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7167 strdup(got_worktree_get_repo_path(worktree));
7168 if (repo_path == NULL)
7169 error = got_error_from_errno("strdup");
7173 repo_path = strdup(cwd);
7174 if (repo_path == NULL) {
7175 error = got_error_from_errno("strdup");
7181 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7186 if (do_list || do_show) {
7187 /* Remove "cpath" promise. */
7188 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7194 error = apply_unveil(got_repo_get_path(repo), do_list,
7195 worktree ? got_worktree_get_root_path(worktree) : NULL);
7200 error = show_current_branch(repo, worktree);
7202 error = list_branches(repo, worktree, sort_by_time);
7204 error = delete_branch(repo, worktree, delref);
7206 struct got_reflist_head refs;
7208 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7212 if (commit_id_arg == NULL)
7213 commit_id_arg = worktree ?
7214 got_worktree_get_head_ref_name(worktree) :
7217 error = got_keyword_to_idstr(&keyword_idstr,
7218 commit_id_arg, repo, worktree);
7221 if (keyword_idstr != NULL)
7222 commit_id_arg = keyword_idstr;
7224 error = got_repo_match_object_id(&commit_id, NULL,
7225 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7226 got_ref_list_free(&refs);
7229 error = add_branch(repo, argv[0], commit_id);
7232 if (worktree && do_update) {
7233 struct got_update_progress_arg upa;
7234 char *branch_refname = NULL;
7236 error = got_object_id_str(&commit_id_str, commit_id);
7239 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7243 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7245 error = got_error_from_errno("asprintf");
7248 error = got_ref_open(&ref, repo, branch_refname, 0);
7249 free(branch_refname);
7252 error = switch_head_ref(ref, commit_id, worktree,
7256 error = got_worktree_set_base_commit_id(worktree, repo,
7260 memset(&upa, 0, sizeof(upa));
7261 error = got_worktree_checkout_files(worktree, &paths,
7262 repo, update_progress, &upa, check_cancelled,
7266 if (upa.did_something) {
7267 printf("Updated to %s: %s\n",
7268 got_worktree_get_head_ref_name(worktree),
7271 print_update_progress_stats(&upa);
7275 free(keyword_idstr);
7279 const struct got_error *close_err = got_repo_close(repo);
7284 got_worktree_close(worktree);
7286 const struct got_error *pack_err =
7287 got_repo_pack_fds_close(pack_fds);
7294 free(commit_id_str);
7295 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7303 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7304 "[-r repository-path] [-s signer-id] name\n", getprogname());
7309 static const struct got_error *
7310 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7312 const struct got_error *err = NULL;
7313 struct got_reflist_entry *re, *se, *new;
7314 struct got_object_id *re_id, *se_id;
7315 struct got_tag_object *re_tag, *se_tag;
7316 time_t re_time, se_time;
7318 STAILQ_FOREACH(re, tags, entry) {
7319 se = STAILQ_FIRST(sorted);
7321 err = got_reflist_entry_dup(&new, re);
7324 STAILQ_INSERT_HEAD(sorted, new, entry);
7327 err = got_ref_resolve(&re_id, repo, re->ref);
7330 err = got_object_open_as_tag(&re_tag, repo, re_id);
7334 re_time = got_object_tag_get_tagger_time(re_tag);
7335 got_object_tag_close(re_tag);
7339 err = got_ref_resolve(&se_id, repo, re->ref);
7342 err = got_object_open_as_tag(&se_tag, repo, se_id);
7346 se_time = got_object_tag_get_tagger_time(se_tag);
7347 got_object_tag_close(se_tag);
7349 if (se_time > re_time) {
7350 err = got_reflist_entry_dup(&new, re);
7353 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7356 se = STAILQ_NEXT(se, entry);
7365 static const struct got_error *
7366 get_tag_refname(char **refname, const char *tag_name)
7368 const struct got_error *err;
7370 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7371 *refname = strdup(tag_name);
7372 if (*refname == NULL)
7373 return got_error_from_errno("strdup");
7374 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7375 err = got_error_from_errno("asprintf");
7383 static const struct got_error *
7384 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7385 const char *allowed_signers, const char *revoked_signers, int verbosity)
7387 static const struct got_error *err = NULL;
7388 struct got_reflist_head refs;
7389 struct got_reflist_entry *re;
7390 char *wanted_refname = NULL;
7395 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7400 struct got_reference *ref;
7401 err = get_tag_refname(&wanted_refname, tag_name);
7404 /* Wanted tag reference should exist. */
7405 err = got_ref_open(&ref, repo, wanted_refname, 0);
7411 TAILQ_FOREACH(re, &refs, entry) {
7412 const char *refname;
7413 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7415 const char *tagger, *ssh_sig = NULL;
7416 char *sig_msg = NULL;
7418 struct got_object_id *id;
7419 struct got_tag_object *tag;
7420 struct got_commit_object *commit = NULL;
7422 refname = got_ref_get_name(re->ref);
7423 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7424 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7427 refstr = got_ref_to_str(re->ref);
7428 if (refstr == NULL) {
7429 err = got_error_from_errno("got_ref_to_str");
7433 err = got_ref_resolve(&id, repo, re->ref);
7436 err = got_object_open_as_tag(&tag, repo, id);
7438 if (err->code != GOT_ERR_OBJ_TYPE) {
7442 /* "lightweight" tag */
7443 err = got_object_open_as_commit(&commit, repo, id);
7448 tagger = got_object_commit_get_committer(commit);
7450 got_object_commit_get_committer_time(commit);
7451 err = got_object_id_str(&id_str, id);
7457 tagger = got_object_tag_get_tagger(tag);
7458 tagger_time = got_object_tag_get_tagger_time(tag);
7459 err = got_object_id_str(&id_str,
7460 got_object_tag_get_object_id(tag));
7465 if (tag && verify_tags) {
7466 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7467 got_object_tag_get_message(tag));
7468 if (ssh_sig && allowed_signers == NULL) {
7469 err = got_error_msg(
7470 GOT_ERR_VERIFY_TAG_SIGNATURE,
7471 "SSH signature verification requires "
7472 "setting allowed_signers in "
7478 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7480 printf("from: %s\n", tagger);
7481 datestr = get_datestr(&tagger_time, datebuf);
7483 printf("date: %s UTC\n", datestr);
7485 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7487 switch (got_object_tag_get_object_type(tag)) {
7488 case GOT_OBJ_TYPE_BLOB:
7489 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7492 case GOT_OBJ_TYPE_TREE:
7493 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7496 case GOT_OBJ_TYPE_COMMIT:
7497 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7500 case GOT_OBJ_TYPE_TAG:
7501 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7511 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7512 allowed_signers, revoked_signers, verbosity);
7513 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7517 printf("signature: %s", sig_msg);
7523 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7526 got_object_commit_close(commit);
7528 tagmsg0 = strdup(got_object_tag_get_message(tag));
7529 got_object_tag_close(tag);
7530 if (tagmsg0 == NULL) {
7531 err = got_error_from_errno("strdup");
7538 line = strsep(&tagmsg, "\n");
7540 printf(" %s\n", line);
7545 got_ref_list_free(&refs);
7546 free(wanted_refname);
7548 if (err == NULL && bad_sigs)
7549 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7553 static const struct got_error *
7554 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7555 const char *tag_name, const char *repo_path)
7557 const struct got_error *err = NULL;
7558 char *template = NULL, *initial_content = NULL;
7559 char *editor = NULL;
7560 int initial_content_len;
7563 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7564 err = got_error_from_errno("asprintf");
7568 initial_content_len = asprintf(&initial_content,
7569 "\n# tagging commit %s as %s\n",
7570 commit_id_str, tag_name);
7571 if (initial_content_len == -1) {
7572 err = got_error_from_errno("asprintf");
7576 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7580 if (write(fd, initial_content, initial_content_len) == -1) {
7581 err = got_error_from_errno2("write", *tagmsg_path);
7584 if (close(fd) == -1) {
7585 err = got_error_from_errno2("close", *tagmsg_path);
7590 err = get_editor(&editor);
7593 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7594 initial_content_len, 1);
7596 free(initial_content);
7600 if (fd != -1 && close(fd) == -1 && err == NULL)
7601 err = got_error_from_errno2("close", *tagmsg_path);
7610 static const struct got_error *
7611 add_tag(struct got_repository *repo, const char *tagger,
7612 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7613 const char *signer_id, int verbosity)
7615 const struct got_error *err = NULL;
7616 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7617 char *label = NULL, *commit_id_str = NULL;
7618 struct got_reference *ref = NULL;
7619 char *refname = NULL, *tagmsg = NULL;
7620 char *tagmsg_path = NULL, *tag_id_str = NULL;
7621 int preserve_tagmsg = 0;
7622 struct got_reflist_head refs;
7627 * Don't let the user create a tag name with a leading '-'.
7628 * While technically a valid reference name, this case is usually
7629 * an unintended typo.
7631 if (tag_name[0] == '-')
7632 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7634 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7638 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7639 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7643 err = got_object_id_str(&commit_id_str, commit_id);
7647 err = get_tag_refname(&refname, tag_name);
7650 if (strncmp("refs/tags/", tag_name, 10) == 0)
7653 err = got_ref_open(&ref, repo, refname, 0);
7655 err = got_error(GOT_ERR_TAG_EXISTS);
7657 } else if (err->code != GOT_ERR_NOT_REF)
7660 if (tagmsg_arg == NULL) {
7661 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7662 tag_name, got_repo_get_path(repo));
7664 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7665 tagmsg_path != NULL)
7666 preserve_tagmsg = 1;
7669 /* Editor is done; we can now apply unveil(2) */
7670 err = got_sigs_apply_unveil();
7673 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7678 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7679 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7683 preserve_tagmsg = 1;
7687 err = got_ref_alloc(&ref, refname, tag_id);
7690 preserve_tagmsg = 1;
7694 err = got_ref_write(ref, repo);
7697 preserve_tagmsg = 1;
7701 err = got_object_id_str(&tag_id_str, tag_id);
7704 preserve_tagmsg = 1;
7707 printf("Created tag %s\n", tag_id_str);
7709 if (preserve_tagmsg) {
7710 fprintf(stderr, "%s: tag message preserved in %s\n",
7711 getprogname(), tagmsg_path);
7712 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7713 err = got_error_from_errno2("unlink", tagmsg_path);
7718 free(commit_id_str);
7722 got_ref_list_free(&refs);
7726 static const struct got_error *
7727 cmd_tag(int argc, char *argv[])
7729 const struct got_error *error = NULL;
7730 struct got_repository *repo = NULL;
7731 struct got_worktree *worktree = NULL;
7732 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7733 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7734 char *allowed_signers = NULL, *revoked_signers = NULL;
7735 const char *signer_id = NULL;
7736 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7737 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7738 int *pack_fds = NULL;
7741 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7742 "sendfd unveil", NULL) == -1)
7746 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7749 commit_id_arg = optarg;
7758 repo_path = realpath(optarg, NULL);
7759 if (repo_path == NULL) {
7760 error = got_error_from_errno2("realpath",
7764 got_path_strip_trailing_slashes(repo_path);
7775 else if (verbosity < 3)
7787 if (do_list || verify_tags) {
7788 if (commit_id_arg != NULL)
7790 "-c option can only be used when creating a tag");
7793 option_conflict('l', 'm');
7795 option_conflict('V', 'm');
7799 option_conflict('l', 's');
7801 option_conflict('V', 's');
7805 } else if (argc != 1)
7811 cwd = getcwd(NULL, 0);
7813 error = got_error_from_errno("getcwd");
7817 error = got_repo_pack_fds_open(&pack_fds);
7821 if (repo_path == NULL) {
7822 error = got_worktree_open(&worktree, cwd,
7823 GOT_WORKTREE_GOT_DIR);
7824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7830 strdup(got_worktree_get_repo_path(worktree));
7831 if (repo_path == NULL)
7832 error = got_error_from_errno("strdup");
7836 repo_path = strdup(cwd);
7837 if (repo_path == NULL) {
7838 error = got_error_from_errno("strdup");
7844 if (do_list || verify_tags) {
7845 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7848 error = get_allowed_signers(&allowed_signers, repo, worktree);
7851 error = get_revoked_signers(&revoked_signers, repo, worktree);
7855 /* Release work tree lock. */
7856 got_worktree_close(worktree);
7861 * Remove "cpath" promise unless needed for signature tmpfile
7865 got_sigs_apply_unveil();
7868 if (pledge("stdio rpath wpath flock proc exec sendfd "
7869 "unveil", NULL) == -1)
7873 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7876 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7877 revoked_signers, verbosity);
7879 error = get_gitconfig_path(&gitconfig_path);
7882 error = got_repo_open(&repo, repo_path, gitconfig_path,
7887 error = get_author(&tagger, repo, worktree);
7890 if (signer_id == NULL)
7891 signer_id = get_signer_id(repo, worktree);
7895 error = got_sigs_apply_unveil();
7899 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7904 if (commit_id_arg == NULL) {
7905 struct got_reference *head_ref;
7906 struct got_object_id *commit_id;
7907 error = got_ref_open(&head_ref, repo,
7908 worktree ? got_worktree_get_head_ref_name(worktree)
7912 error = got_ref_resolve(&commit_id, repo, head_ref);
7913 got_ref_close(head_ref);
7916 error = got_object_id_str(&commit_id_str, commit_id);
7921 error = got_keyword_to_idstr(&keyword_idstr,
7922 commit_id_arg, repo, worktree);
7925 commit_id_str = keyword_idstr;
7929 /* Release work tree lock. */
7930 got_worktree_close(worktree);
7934 error = add_tag(repo, tagger, tag_name,
7935 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7936 signer_id, verbosity);
7940 const struct got_error *close_err = got_repo_close(repo);
7945 got_worktree_close(worktree);
7947 const struct got_error *pack_err =
7948 got_repo_pack_fds_close(pack_fds);
7954 free(gitconfig_path);
7955 free(commit_id_str);
7957 free(allowed_signers);
7958 free(revoked_signers);
7965 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7969 static const struct got_error *
7970 add_progress(void *arg, unsigned char status, const char *path)
7972 while (path[0] == '/')
7974 printf("%c %s\n", status, path);
7978 static const struct got_error *
7979 cmd_add(int argc, char *argv[])
7981 const struct got_error *error = NULL;
7982 struct got_repository *repo = NULL;
7983 struct got_worktree *worktree = NULL;
7985 struct got_pathlist_head paths;
7986 struct got_pathlist_entry *pe;
7987 int ch, can_recurse = 0, no_ignores = 0;
7988 int *pack_fds = NULL;
7993 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7998 while ((ch = getopt(argc, argv, "IR")) != -1) {
8018 cwd = getcwd(NULL, 0);
8020 error = got_error_from_errno("getcwd");
8024 error = got_repo_pack_fds_open(&pack_fds);
8028 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8030 if (error->code == GOT_ERR_NOT_WORKTREE)
8031 error = wrap_not_worktree_error(error, "add", cwd);
8035 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8040 error = apply_unveil(got_repo_get_path(repo), 1,
8041 got_worktree_get_root_path(worktree));
8045 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8052 TAILQ_FOREACH(pe, &paths, entry) {
8053 if (asprintf(&ondisk_path, "%s/%s",
8054 got_worktree_get_root_path(worktree),
8056 error = got_error_from_errno("asprintf");
8059 if (lstat(ondisk_path, &sb) == -1) {
8060 if (errno == ENOENT) {
8064 error = got_error_from_errno2("lstat",
8070 if (S_ISDIR(sb.st_mode)) {
8071 error = got_error_msg(GOT_ERR_BAD_PATH,
8072 "adding directories requires -R option");
8078 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8079 NULL, repo, no_ignores);
8082 const struct got_error *close_err = got_repo_close(repo);
8087 got_worktree_close(worktree);
8089 const struct got_error *pack_err =
8090 got_repo_pack_fds_close(pack_fds);
8094 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8102 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8107 static const struct got_error *
8108 print_remove_status(void *arg, unsigned char status,
8109 unsigned char staged_status, const char *path)
8111 while (path[0] == '/')
8113 if (status == GOT_STATUS_NONEXISTENT)
8115 if (status == staged_status && (status == GOT_STATUS_DELETE))
8116 status = GOT_STATUS_NO_CHANGE;
8117 printf("%c%c %s\n", status, staged_status, path);
8121 static const struct got_error *
8122 cmd_remove(int argc, char *argv[])
8124 const struct got_error *error = NULL;
8125 struct got_worktree *worktree = NULL;
8126 struct got_repository *repo = NULL;
8127 const char *status_codes = NULL;
8129 struct got_pathlist_head paths;
8130 struct got_pathlist_entry *pe;
8131 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8132 int ignore_missing_paths = 0;
8133 int *pack_fds = NULL;
8138 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8143 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8146 delete_local_mods = 1;
8147 ignore_missing_paths = 1;
8156 for (i = 0; optarg[i] != '\0'; i++) {
8157 switch (optarg[i]) {
8158 case GOT_STATUS_MODIFY:
8159 delete_local_mods = 1;
8161 case GOT_STATUS_MISSING:
8162 ignore_missing_paths = 1;
8165 errx(1, "invalid status code '%c'",
8169 status_codes = optarg;
8183 cwd = getcwd(NULL, 0);
8185 error = got_error_from_errno("getcwd");
8189 error = got_repo_pack_fds_open(&pack_fds);
8193 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8195 if (error->code == GOT_ERR_NOT_WORKTREE)
8196 error = wrap_not_worktree_error(error, "remove", cwd);
8200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8205 error = apply_unveil(got_repo_get_path(repo), 1,
8206 got_worktree_get_root_path(worktree));
8210 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8217 TAILQ_FOREACH(pe, &paths, entry) {
8218 if (asprintf(&ondisk_path, "%s/%s",
8219 got_worktree_get_root_path(worktree),
8221 error = got_error_from_errno("asprintf");
8224 if (lstat(ondisk_path, &sb) == -1) {
8225 if (errno == ENOENT) {
8229 error = got_error_from_errno2("lstat",
8235 if (S_ISDIR(sb.st_mode)) {
8236 error = got_error_msg(GOT_ERR_BAD_PATH,
8237 "removing directories requires -R option");
8243 error = got_worktree_schedule_delete(worktree, &paths,
8244 delete_local_mods, status_codes, print_remove_status, NULL,
8245 repo, keep_on_disk, ignore_missing_paths);
8248 const struct got_error *close_err = got_repo_close(repo);
8253 got_worktree_close(worktree);
8255 const struct got_error *pack_err =
8256 got_repo_pack_fds_close(pack_fds);
8260 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8268 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8269 "[patchfile]\n", getprogname());
8273 static const struct got_error *
8274 patch_from_stdin(int *patchfd)
8276 const struct got_error *err = NULL;
8279 sig_t sighup, sigint, sigquit;
8281 *patchfd = got_opentempfd();
8283 return got_error_from_errno("got_opentempfd");
8285 sighup = signal(SIGHUP, SIG_DFL);
8286 sigint = signal(SIGINT, SIG_DFL);
8287 sigquit = signal(SIGQUIT, SIG_DFL);
8290 r = read(0, buf, sizeof(buf));
8292 err = got_error_from_errno("read");
8297 if (write(*patchfd, buf, r) == -1) {
8298 err = got_error_from_errno("write");
8303 signal(SIGHUP, sighup);
8304 signal(SIGINT, sigint);
8305 signal(SIGQUIT, sigquit);
8307 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8308 err = got_error_from_errno("lseek");
8318 struct got_patch_progress_arg {
8324 static const struct got_error *
8325 patch_progress(void *arg, const char *old, const char *new,
8326 unsigned char status, const struct got_error *error, int old_from,
8327 int old_lines, int new_from, int new_lines, int offset,
8328 int ws_mangled, const struct got_error *hunk_err)
8330 const char *path = new == NULL ? old : new;
8331 struct got_patch_progress_arg *a = arg;
8333 while (*path == '/')
8336 if (status != GOT_STATUS_NO_CHANGE &&
8337 status != 0 /* per-hunk progress */) {
8338 printf("%c %s\n", status, path);
8339 a->did_something = 1;
8342 if (hunk_err == NULL) {
8343 if (status == GOT_STATUS_CANNOT_UPDATE)
8345 else if (status == GOT_STATUS_CONFLICT)
8350 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8352 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8353 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8354 old_lines, new_from, new_lines);
8355 if (hunk_err != NULL)
8356 printf("%s\n", hunk_err->msg);
8357 else if (offset != 0)
8358 printf("applied with offset %d\n", offset);
8360 printf("hunk contains mangled whitespace\n");
8367 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8369 if (!ppa->did_something)
8372 if (ppa->conflicts > 0)
8373 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8375 if (ppa->rejects > 0) {
8376 printf("Files where patch failed to apply: %d\n",
8381 static const struct got_error *
8382 cmd_patch(int argc, char *argv[])
8384 const struct got_error *error = NULL, *close_error = NULL;
8385 struct got_worktree *worktree = NULL;
8386 struct got_repository *repo = NULL;
8387 struct got_reflist_head refs;
8388 struct got_object_id *commit_id = NULL;
8389 const char *commit_id_str = NULL;
8392 char *cwd = NULL, *keyword_idstr = NULL;
8393 int ch, nop = 0, strip = -1, reverse = 0;
8395 int *pack_fds = NULL;
8396 struct got_patch_progress_arg ppa;
8401 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8402 "unveil", NULL) == -1)
8406 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8409 commit_id_str = optarg;
8415 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8417 errx(1, "pathname strip count is %s: %s",
8433 error = patch_from_stdin(&patchfd);
8436 } else if (argc == 1) {
8437 patchfd = open(argv[0], O_RDONLY);
8438 if (patchfd == -1) {
8439 error = got_error_from_errno2("open", argv[0]);
8442 if (fstat(patchfd, &sb) == -1) {
8443 error = got_error_from_errno2("fstat", argv[0]);
8446 if (!S_ISREG(sb.st_mode)) {
8447 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8453 if ((cwd = getcwd(NULL, 0)) == NULL) {
8454 error = got_error_from_errno("getcwd");
8458 error = got_repo_pack_fds_open(&pack_fds);
8462 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8466 const char *repo_path = got_worktree_get_repo_path(worktree);
8467 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8471 error = apply_unveil(got_repo_get_path(repo), 0,
8472 got_worktree_get_root_path(worktree));
8476 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8480 if (commit_id_str != NULL) {
8481 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8486 error = got_repo_match_object_id(&commit_id, NULL,
8487 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8488 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8493 memset(&ppa, 0, sizeof(ppa));
8494 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8495 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8496 print_patch_progress_stats(&ppa);
8498 got_ref_list_free(&refs);
8499 free(keyword_idstr);
8502 close_error = got_repo_close(repo);
8504 error = close_error;
8506 if (worktree != NULL) {
8507 close_error = got_worktree_close(worktree);
8509 error = close_error;
8512 const struct got_error *pack_err =
8513 got_repo_pack_fds_close(pack_fds);
8524 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8529 static const struct got_error *
8530 revert_progress(void *arg, unsigned char status, const char *path)
8532 if (status == GOT_STATUS_UNVERSIONED)
8535 while (path[0] == '/')
8537 printf("%c %s\n", status, path);
8541 struct choose_patch_arg {
8542 FILE *patch_script_file;
8546 static const struct got_error *
8547 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8548 int nchanges, const char *action)
8550 const struct got_error *err;
8552 size_t linesize = 0;
8556 case GOT_STATUS_ADD:
8557 printf("A %s\n%s this addition? [y/n] ", path, action);
8559 case GOT_STATUS_DELETE:
8560 printf("D %s\n%s this deletion? [y/n] ", path, action);
8562 case GOT_STATUS_MODIFY:
8563 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8564 return got_error_from_errno("fseek");
8565 printf(GOT_COMMIT_SEP_STR);
8566 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8568 if (linelen == -1 && ferror(patch_file)) {
8569 err = got_error_from_errno("getline");
8574 printf(GOT_COMMIT_SEP_STR);
8575 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8576 path, n, nchanges, action);
8579 return got_error_path(path, GOT_ERR_FILE_STATUS);
8586 static const struct got_error *
8587 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8588 FILE *patch_file, int n, int nchanges)
8590 const struct got_error *err = NULL;
8592 size_t linesize = 0;
8595 struct choose_patch_arg *a = arg;
8597 *choice = GOT_PATCH_CHOICE_NONE;
8599 if (a->patch_script_file) {
8601 err = show_change(status, path, patch_file, n, nchanges,
8605 linelen = getline(&line, &linesize, a->patch_script_file);
8606 if (linelen == -1) {
8607 if (ferror(a->patch_script_file))
8608 return got_error_from_errno("getline");
8611 nl = strchr(line, '\n');
8614 if (strcmp(line, "y") == 0) {
8615 *choice = GOT_PATCH_CHOICE_YES;
8617 } else if (strcmp(line, "n") == 0) {
8618 *choice = GOT_PATCH_CHOICE_NO;
8620 } else if (strcmp(line, "q") == 0 &&
8621 status == GOT_STATUS_MODIFY) {
8622 *choice = GOT_PATCH_CHOICE_QUIT;
8625 printf("invalid response '%s'\n", line);
8630 while (resp != 'y' && resp != 'n' && resp != 'q') {
8631 err = show_change(status, path, patch_file, n, nchanges,
8638 if (status == GOT_STATUS_MODIFY) {
8639 if (resp != 'y' && resp != 'n' && resp != 'q') {
8640 printf("invalid response '%c'\n", resp);
8643 } else if (resp != 'y' && resp != 'n') {
8644 printf("invalid response '%c'\n", resp);
8650 *choice = GOT_PATCH_CHOICE_YES;
8651 else if (resp == 'n')
8652 *choice = GOT_PATCH_CHOICE_NO;
8653 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8654 *choice = GOT_PATCH_CHOICE_QUIT;
8659 struct wt_commitable_path_arg {
8660 struct got_pathlist_head *commit_paths;
8665 * Shortcut work tree status callback to determine if the set of paths scanned
8666 * has at least one versioned path that is being modified and, if not NULL, is
8667 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8668 * soon as a path is passed with a status that satisfies this criteria.
8670 static const struct got_error *
8671 worktree_has_commitable_path(void *arg, unsigned char status,
8672 unsigned char staged_status, const char *path,
8673 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8674 struct got_object_id *commit_id, int dirfd, const char *de_name)
8676 struct wt_commitable_path_arg *a = arg;
8678 if (status == staged_status && (status == GOT_STATUS_DELETE))
8679 status = GOT_STATUS_NO_CHANGE;
8681 if (!(status == GOT_STATUS_NO_CHANGE ||
8682 status == GOT_STATUS_UNVERSIONED) ||
8683 staged_status != GOT_STATUS_NO_CHANGE) {
8684 if (a->commit_paths != NULL) {
8685 struct got_pathlist_entry *pe;
8687 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8688 if (strncmp(path, pe->path,
8689 pe->path_len) == 0) {
8690 *a->has_changes = 1;
8695 *a->has_changes = 1;
8697 if (*a->has_changes)
8698 return got_error(GOT_ERR_FILE_MODIFIED);
8705 * Check that the changeset of the commit identified by id is
8706 * comprised of at least one modified path that is being committed.
8708 static const struct got_error *
8709 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8710 struct got_object_id *id, struct got_worktree *worktree,
8711 struct got_repository *repo)
8713 const struct got_error *err;
8714 struct got_pathlist_head paths;
8715 struct got_commit_object *commit = NULL, *pcommit = NULL;
8716 struct got_tree_object *tree = NULL, *ptree = NULL;
8717 struct got_object_qid *pid;
8721 err = got_object_open_as_commit(&commit, repo, id);
8725 err = got_object_open_as_tree(&tree, repo,
8726 got_object_commit_get_tree_id(commit));
8730 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8732 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8736 err = got_object_open_as_tree(&ptree, repo,
8737 got_object_commit_get_tree_id(pcommit));
8742 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8743 got_diff_tree_collect_changed_paths, &paths, 0);
8747 err = got_worktree_status(worktree, &paths, repo, 0,
8748 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8749 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8751 * At least one changed path in the referenced commit is
8752 * modified in the work tree, that's all we need to know!
8758 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8760 got_object_commit_close(commit);
8762 got_object_commit_close(pcommit);
8764 got_object_tree_close(tree);
8766 got_object_tree_close(ptree);
8771 * Remove any "logmsg" reference comprised entirely of paths that have
8772 * been reverted in this work tree. If any path in the logmsg ref changeset
8773 * remains in a changed state in the worktree, do not remove the reference.
8775 static const struct got_error *
8776 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8778 const struct got_error *err;
8779 struct got_reflist_head refs;
8780 struct got_reflist_entry *re;
8781 struct got_commit_object *commit = NULL;
8782 struct got_object_id *commit_id = NULL;
8783 struct wt_commitable_path_arg wcpa;
8784 char *uuidstr = NULL;
8788 err = got_worktree_get_uuid(&uuidstr, worktree);
8792 err = got_ref_list(&refs, repo, "refs/got/worktree",
8793 got_ref_cmp_by_name, repo);
8797 TAILQ_FOREACH(re, &refs, entry) {
8798 const char *refname;
8799 int has_changes = 0;
8801 refname = got_ref_get_name(re->ref);
8803 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8804 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8805 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8806 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8807 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8808 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8812 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8813 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8817 err = got_repo_match_object_id(&commit_id, NULL, refname,
8818 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8822 err = got_object_open_as_commit(&commit, repo, commit_id);
8826 wcpa.commit_paths = NULL;
8827 wcpa.has_changes = &has_changes;
8829 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8835 err = got_ref_delete(re->ref, repo);
8840 got_object_commit_close(commit);
8849 got_ref_list_free(&refs);
8851 got_object_commit_close(commit);
8855 static const struct got_error *
8856 cmd_revert(int argc, char *argv[])
8858 const struct got_error *error = NULL;
8859 struct got_worktree *worktree = NULL;
8860 struct got_repository *repo = NULL;
8861 char *cwd = NULL, *path = NULL;
8862 struct got_pathlist_head paths;
8863 struct got_pathlist_entry *pe;
8864 int ch, can_recurse = 0, pflag = 0;
8865 FILE *patch_script_file = NULL;
8866 const char *patch_script_path = NULL;
8867 struct choose_patch_arg cpa;
8868 int *pack_fds = NULL;
8873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8874 "unveil", NULL) == -1)
8878 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8881 patch_script_path = optarg;
8900 if (patch_script_path && !pflag)
8901 errx(1, "-F option can only be used together with -p option");
8903 cwd = getcwd(NULL, 0);
8905 error = got_error_from_errno("getcwd");
8909 error = got_repo_pack_fds_open(&pack_fds);
8913 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8915 if (error->code == GOT_ERR_NOT_WORKTREE)
8916 error = wrap_not_worktree_error(error, "revert", cwd);
8920 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8925 if (patch_script_path) {
8926 patch_script_file = fopen(patch_script_path, "re");
8927 if (patch_script_file == NULL) {
8928 error = got_error_from_errno2("fopen",
8935 * XXX "c" perm needed on repo dir to delete merge references.
8937 error = apply_unveil(got_repo_get_path(repo), 0,
8938 got_worktree_get_root_path(worktree));
8942 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8949 TAILQ_FOREACH(pe, &paths, entry) {
8950 if (asprintf(&ondisk_path, "%s/%s",
8951 got_worktree_get_root_path(worktree),
8953 error = got_error_from_errno("asprintf");
8956 if (lstat(ondisk_path, &sb) == -1) {
8957 if (errno == ENOENT) {
8961 error = got_error_from_errno2("lstat",
8967 if (S_ISDIR(sb.st_mode)) {
8968 error = got_error_msg(GOT_ERR_BAD_PATH,
8969 "reverting directories requires -R option");
8975 cpa.patch_script_file = patch_script_file;
8976 cpa.action = "revert";
8977 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8978 pflag ? choose_patch : NULL, &cpa, repo);
8980 error = rm_logmsg_ref(worktree, repo);
8982 if (patch_script_file && fclose(patch_script_file) == EOF &&
8984 error = got_error_from_errno2("fclose", patch_script_path);
8986 const struct got_error *close_err = got_repo_close(repo);
8991 got_worktree_close(worktree);
8993 const struct got_error *pack_err =
8994 got_repo_pack_fds_close(pack_fds);
8998 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9007 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
9008 "[-m message] [path ...]\n", getprogname());
9012 struct collect_commit_logmsg_arg {
9013 const char *cmdline_log;
9014 const char *prepared_log;
9015 const char *merged_log;
9016 int non_interactive;
9018 const char *worktree_path;
9019 const char *branch_name;
9020 const char *repo_path;
9025 static const struct got_error *
9026 read_prepared_logmsg(char **logmsg, const char *path)
9028 const struct got_error *err = NULL;
9034 memset(&sb, 0, sizeof(sb));
9036 f = fopen(path, "re");
9038 return got_error_from_errno2("fopen", path);
9040 if (fstat(fileno(f), &sb) == -1) {
9041 err = got_error_from_errno2("fstat", path);
9044 if (sb.st_size == 0) {
9045 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
9049 *logmsg = malloc(sb.st_size + 1);
9050 if (*logmsg == NULL) {
9051 err = got_error_from_errno("malloc");
9055 r = fread(*logmsg, 1, sb.st_size, f);
9056 if (r != sb.st_size) {
9058 err = got_error_from_errno2("fread", path);
9060 err = got_error(GOT_ERR_IO);
9063 (*logmsg)[sb.st_size] = '\0';
9065 if (fclose(f) == EOF && err == NULL)
9066 err = got_error_from_errno2("fclose", path);
9074 static const struct got_error *
9075 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9076 const char *diff_path, char **logmsg, void *arg)
9078 char *initial_content = NULL;
9079 struct got_pathlist_entry *pe;
9080 const struct got_error *err = NULL;
9081 char *template = NULL;
9082 char *prepared_msg = NULL, *merged_msg = NULL;
9083 struct collect_commit_logmsg_arg *a = arg;
9084 int initial_content_len;
9088 /* if a message was specified on the command line, just use it */
9089 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9090 len = strlen(a->cmdline_log) + 1;
9091 *logmsg = malloc(len + 1);
9092 if (*logmsg == NULL)
9093 return got_error_from_errno("malloc");
9094 strlcpy(*logmsg, a->cmdline_log, len);
9096 } else if (a->prepared_log != NULL && a->non_interactive)
9097 return read_prepared_logmsg(logmsg, a->prepared_log);
9099 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9100 return got_error_from_errno("asprintf");
9102 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9106 if (a->prepared_log) {
9107 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9110 } else if (a->merged_log) {
9111 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9116 initial_content_len = asprintf(&initial_content,
9117 "%s%s\n# changes to be committed on branch %s:\n",
9118 prepared_msg ? prepared_msg : "",
9119 merged_msg ? merged_msg : "", a->branch_name);
9120 if (initial_content_len == -1) {
9121 err = got_error_from_errno("asprintf");
9125 if (write(fd, initial_content, initial_content_len) == -1) {
9126 err = got_error_from_errno2("write", a->logmsg_path);
9130 TAILQ_FOREACH(pe, commitable_paths, entry) {
9131 struct got_commitable *ct = pe->data;
9132 dprintf(fd, "# %c %s\n",
9133 got_commitable_get_status(ct),
9134 got_commitable_get_path(ct));
9138 dprintf(fd, "# detailed changes can be viewed in %s\n",
9142 if (close(fd) == -1) {
9143 err = got_error_from_errno2("close", a->logmsg_path);
9148 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9149 initial_content_len, a->prepared_log ? 0 : 1);
9151 free(initial_content);
9156 if (fd != -1 && close(fd) == -1 && err == NULL)
9157 err = got_error_from_errno2("close", a->logmsg_path);
9159 /* Editor is done; we can now apply unveil(2) */
9161 err = apply_unveil(a->repo_path, 0, a->worktree_path);
9169 static const struct got_error *
9170 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9171 const char *type, int has_content)
9173 const struct got_error *err = NULL;
9174 char *logmsg = NULL;
9176 err = got_object_commit_get_logmsg(&logmsg, commit);
9180 if (fprintf(f, "%s# log message of %s commit %s:%s",
9181 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9182 err = got_ferror(f, GOT_ERR_IO);
9189 * Lookup "logmsg" references of backed-out and cherrypicked commits
9190 * belonging to the current work tree. If found, and the worktree has
9191 * at least one modified file that was changed in the referenced commit,
9192 * add its log message to a new temporary file at *logmsg_path.
9193 * Add all refs found to matched_refs to be scheduled for removal on
9194 * successful commit.
9196 static const struct got_error *
9197 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9198 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9199 struct got_repository *repo)
9201 const struct got_error *err;
9202 struct got_commit_object *commit = NULL;
9203 struct got_object_id *id = NULL;
9204 struct got_reflist_head refs;
9205 struct got_reflist_entry *re, *re_match;
9207 char *uuidstr = NULL;
9208 int added_logmsg = 0;
9212 *logmsg_path = NULL;
9214 err = got_worktree_get_uuid(&uuidstr, worktree);
9218 err = got_ref_list(&refs, repo, "refs/got/worktree",
9219 got_ref_cmp_by_name, repo);
9223 TAILQ_FOREACH(re, &refs, entry) {
9224 const char *refname, *type;
9225 struct wt_commitable_path_arg wcpa;
9228 refname = got_ref_get_name(re->ref);
9230 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9231 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9232 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9233 type = "cherrypicked";
9234 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9235 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9236 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9237 type = "backed-out";
9241 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9242 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9246 err = got_repo_match_object_id(&id, NULL, refname,
9247 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9251 err = got_object_open_as_commit(&commit, repo, id);
9255 wcpa.commit_paths = paths;
9256 wcpa.has_changes = &add_logmsg;
9258 err = commit_path_changed_in_worktree(&wcpa, id,
9265 err = got_opentemp_named(logmsg_path, &f,
9266 "got-commit-logmsg", "");
9270 err = cat_logmsg(f, commit, refname, type,
9277 err = got_reflist_entry_dup(&re_match, re);
9280 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9283 got_object_commit_close(commit);
9292 got_ref_list_free(&refs);
9294 got_object_commit_close(commit);
9295 if (f && fclose(f) == EOF && err == NULL)
9296 err = got_error_from_errno("fclose");
9297 if (!added_logmsg) {
9298 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9299 err = got_error_from_errno2("unlink", *logmsg_path);
9300 *logmsg_path = NULL;
9305 static const struct got_error *
9306 cmd_commit(int argc, char *argv[])
9308 const struct got_error *error = NULL;
9309 struct got_worktree *worktree = NULL;
9310 struct got_repository *repo = NULL;
9311 char *cwd = NULL, *id_str = NULL;
9312 struct got_object_id *id = NULL;
9313 const char *logmsg = NULL;
9314 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9315 struct collect_commit_logmsg_arg cl_arg;
9316 const char *author = NULL;
9317 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9318 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9319 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9320 int show_diff = 1, commit_conflicts = 0;
9321 struct got_pathlist_head paths;
9322 struct got_reflist_head refs;
9323 struct got_reflist_entry *re;
9324 int *pack_fds = NULL;
9328 cl_arg.logmsg_path = NULL;
9331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9332 "unveil", NULL) == -1)
9336 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9340 error = valid_author(author);
9345 commit_conflicts = 1;
9349 option_conflict('F', 'm');
9350 prepared_logmsg = realpath(optarg, NULL);
9351 if (prepared_logmsg == NULL)
9352 return got_error_from_errno2("realpath",
9356 if (prepared_logmsg)
9357 option_conflict('m', 'F');
9361 non_interactive = 1;
9367 allow_bad_symlinks = 1;
9378 cwd = getcwd(NULL, 0);
9380 error = got_error_from_errno("getcwd");
9384 error = got_repo_pack_fds_open(&pack_fds);
9388 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9390 if (error->code == GOT_ERR_NOT_WORKTREE)
9391 error = wrap_not_worktree_error(error, "commit", cwd);
9395 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9398 if (rebase_in_progress) {
9399 error = got_error(GOT_ERR_REBASING);
9403 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9408 error = get_gitconfig_path(&gitconfig_path);
9411 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9412 gitconfig_path, pack_fds);
9416 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9419 if (merge_in_progress) {
9420 error = got_error(GOT_ERR_MERGE_BUSY);
9424 error = get_author(&committer, repo, worktree);
9432 * unveil(2) traverses exec(2); if an editor is used we have
9433 * to apply unveil after the log message has been written.
9435 if (logmsg == NULL || strlen(logmsg) == 0)
9436 error = get_editor(&editor);
9438 error = apply_unveil(got_repo_get_path(repo), 0,
9439 got_worktree_get_root_path(worktree));
9443 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9447 if (prepared_logmsg == NULL) {
9448 error = lookup_logmsg_ref(&merged_logmsg,
9449 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9454 cl_arg.editor = editor;
9455 cl_arg.cmdline_log = logmsg;
9456 cl_arg.prepared_log = prepared_logmsg;
9457 cl_arg.merged_log = merged_logmsg;
9458 cl_arg.non_interactive = non_interactive;
9459 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9460 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9461 if (!histedit_in_progress) {
9462 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9463 error = got_error(GOT_ERR_COMMIT_BRANCH);
9466 cl_arg.branch_name += 11;
9468 cl_arg.repo_path = got_repo_get_path(repo);
9469 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9470 allow_bad_symlinks, show_diff, commit_conflicts,
9471 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9473 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9474 cl_arg.logmsg_path != NULL)
9475 preserve_logmsg = 1;
9479 error = got_object_id_str(&id_str, id);
9482 printf("Created commit %s\n", id_str);
9484 TAILQ_FOREACH(re, &refs, entry) {
9485 error = got_ref_delete(re->ref, repo);
9491 if (preserve_logmsg) {
9492 fprintf(stderr, "%s: log message preserved in %s\n",
9493 getprogname(), cl_arg.logmsg_path);
9494 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9496 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9497 free(cl_arg.logmsg_path);
9498 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9499 error = got_error_from_errno2("unlink", merged_logmsg);
9500 free(merged_logmsg);
9502 const struct got_error *close_err = got_repo_close(repo);
9507 got_worktree_close(worktree);
9509 const struct got_error *pack_err =
9510 got_repo_pack_fds_close(pack_fds);
9514 got_ref_list_free(&refs);
9515 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9518 free(gitconfig_path);
9521 free(prepared_logmsg);
9528 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9529 "[-r repository-path] [-t tag] [remote-repository]\n",
9535 print_load_info(int print_colored, int print_found, int print_trees,
9536 int ncolored, int nfound, int ntrees)
9538 if (print_colored) {
9539 printf("%d commit%s colored", ncolored,
9540 ncolored == 1 ? "" : "s");
9543 printf("%s%d object%s found",
9544 ncolored > 0 ? "; " : "",
9545 nfound, nfound == 1 ? "" : "s");
9548 printf("; %d tree%s scanned", ntrees,
9549 ntrees == 1 ? "" : "s");
9553 struct got_send_progress_arg {
9554 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9561 int last_nobj_total;
9565 int printed_something;
9567 struct got_pathlist_head *delete_branches;
9570 static const struct got_error *
9571 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9572 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9573 int nobj_written, off_t bytes_sent, const char *refname,
9574 const char *errmsg, int success)
9576 struct got_send_progress_arg *a = arg;
9577 char scaled_packsize[FMT_SCALED_STRSIZE];
9578 char scaled_sent[FMT_SCALED_STRSIZE];
9579 int p_deltify = 0, p_written = 0, p_sent = 0;
9580 int print_colored = 0, print_found = 0, print_trees = 0;
9581 int print_searching = 0, print_total = 0;
9582 int print_deltify = 0, print_written = 0, print_sent = 0;
9584 if (a->verbosity < 0)
9588 const char *status = success ? "accepted" : "rejected";
9591 struct got_pathlist_entry *pe;
9592 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9593 const char *branchname = pe->path;
9594 if (got_path_cmp(branchname, refname,
9595 strlen(branchname), strlen(refname)) == 0) {
9597 a->sent_something = 1;
9603 if (a->printed_something)
9605 printf("Server has %s %s", status, refname);
9607 printf(": %s", errmsg);
9608 a->printed_something = 1;
9612 if (a->last_ncolored != ncolored) {
9614 a->last_ncolored = ncolored;
9617 if (a->last_nfound != nfound) {
9620 a->last_nfound = nfound;
9623 if (a->last_ntrees != ntrees) {
9627 a->last_ntrees = ntrees;
9630 if ((print_colored || print_found || print_trees) &&
9633 print_load_info(print_colored, print_found, print_trees,
9634 ncolored, nfound, ntrees);
9635 a->printed_something = 1;
9638 } else if (!a->loading_done) {
9640 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9642 a->loading_done = 1;
9645 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9646 return got_error_from_errno("fmt_scaled");
9647 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9648 return got_error_from_errno("fmt_scaled");
9650 if (a->last_ncommits != ncommits) {
9651 print_searching = 1;
9652 a->last_ncommits = ncommits;
9655 if (a->last_nobj_total != nobj_total) {
9656 print_searching = 1;
9658 a->last_nobj_total = nobj_total;
9661 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9662 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9663 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9664 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9665 return got_error(GOT_ERR_NO_SPACE);
9668 if (nobj_deltify > 0 || nobj_written > 0) {
9669 if (nobj_deltify > 0) {
9670 p_deltify = (nobj_deltify * 100) / nobj_total;
9671 if (p_deltify != a->last_p_deltify) {
9672 a->last_p_deltify = p_deltify;
9673 print_searching = 1;
9678 if (nobj_written > 0) {
9679 p_written = (nobj_written * 100) / nobj_total;
9680 if (p_written != a->last_p_written) {
9681 a->last_p_written = p_written;
9682 print_searching = 1;
9690 if (bytes_sent > 0) {
9691 p_sent = (bytes_sent * 100) / packfile_size;
9692 if (p_sent != a->last_p_sent) {
9693 a->last_p_sent = p_sent;
9694 print_searching = 1;
9700 a->sent_something = 1;
9703 if (print_searching || print_total || print_deltify || print_written ||
9706 if (print_searching)
9707 printf("packing %d reference%s", ncommits,
9708 ncommits == 1 ? "" : "s");
9710 printf("; %d object%s", nobj_total,
9711 nobj_total == 1 ? "" : "s");
9713 printf("; deltify: %d%%", p_deltify);
9715 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9716 scaled_packsize, p_sent);
9717 else if (print_written)
9718 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9719 scaled_packsize, p_written);
9720 if (print_searching || print_total || print_deltify ||
9721 print_written || print_sent) {
9722 a->printed_something = 1;
9728 static const struct got_error *
9729 cmd_send(int argc, char *argv[])
9731 const struct got_error *error = NULL;
9732 char *cwd = NULL, *repo_path = NULL;
9733 const char *remote_name;
9734 char *proto = NULL, *host = NULL, *port = NULL;
9735 char *repo_name = NULL, *server_path = NULL;
9736 const struct got_remote_repo *remotes;
9737 struct got_remote_repo *remote = NULL;
9738 int nremotes, nbranches = 0, ndelete_branches = 0;
9739 struct got_repository *repo = NULL;
9740 struct got_worktree *worktree = NULL;
9741 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9742 struct got_pathlist_head branches;
9743 struct got_pathlist_head tags;
9744 struct got_reflist_head all_branches;
9745 struct got_reflist_head all_tags;
9746 struct got_pathlist_head delete_args;
9747 struct got_pathlist_head delete_branches;
9748 struct got_reflist_entry *re;
9749 struct got_pathlist_entry *pe;
9750 int i, ch, sendfd = -1, sendstatus;
9752 struct got_send_progress_arg spa;
9753 int verbosity = 0, overwrite_refs = 0;
9754 int send_all_branches = 0, send_all_tags = 0;
9755 struct got_reference *ref = NULL;
9756 int *pack_fds = NULL;
9758 TAILQ_INIT(&branches);
9760 TAILQ_INIT(&all_branches);
9761 TAILQ_INIT(&all_tags);
9762 TAILQ_INIT(&delete_args);
9763 TAILQ_INIT(&delete_branches);
9765 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9768 send_all_branches = 1;
9771 error = got_pathlist_append(&branches, optarg, NULL);
9777 error = got_pathlist_append(&delete_args, optarg, NULL);
9788 repo_path = realpath(optarg, NULL);
9789 if (repo_path == NULL)
9790 return got_error_from_errno2("realpath",
9792 got_path_strip_trailing_slashes(repo_path);
9798 error = got_pathlist_append(&tags, optarg, NULL);
9805 else if (verbosity < 3)
9816 if (send_all_branches && !TAILQ_EMPTY(&branches))
9817 option_conflict('a', 'b');
9818 if (send_all_tags && !TAILQ_EMPTY(&tags))
9819 option_conflict('T', 't');
9823 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9825 remote_name = argv[0];
9829 cwd = getcwd(NULL, 0);
9831 error = got_error_from_errno("getcwd");
9835 error = got_repo_pack_fds_open(&pack_fds);
9839 if (repo_path == NULL) {
9840 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9847 strdup(got_worktree_get_repo_path(worktree));
9848 if (repo_path == NULL)
9849 error = got_error_from_errno("strdup");
9853 repo_path = strdup(cwd);
9854 if (repo_path == NULL) {
9855 error = got_error_from_errno("strdup");
9861 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9866 worktree_conf = got_worktree_get_gotconfig(worktree);
9867 if (worktree_conf) {
9868 got_gotconfig_get_remotes(&nremotes, &remotes,
9870 for (i = 0; i < nremotes; i++) {
9871 if (strcmp(remotes[i].name, remote_name) == 0) {
9872 error = got_repo_remote_repo_dup(&remote,
9881 if (remote == NULL) {
9882 repo_conf = got_repo_get_gotconfig(repo);
9884 got_gotconfig_get_remotes(&nremotes, &remotes,
9886 for (i = 0; i < nremotes; i++) {
9887 if (strcmp(remotes[i].name, remote_name) == 0) {
9888 error = got_repo_remote_repo_dup(&remote,
9897 if (remote == NULL) {
9898 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9899 for (i = 0; i < nremotes; i++) {
9900 if (strcmp(remotes[i].name, remote_name) == 0) {
9901 error = got_repo_remote_repo_dup(&remote,
9909 if (remote == NULL) {
9910 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9914 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9915 &repo_name, remote->send_url);
9919 if (strcmp(proto, "git") == 0) {
9921 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9922 "sendfd dns inet unveil", NULL) == -1)
9925 } else if (strcmp(proto, "git+ssh") == 0 ||
9926 strcmp(proto, "ssh") == 0) {
9928 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9929 "sendfd unveil", NULL) == -1)
9932 } else if (strcmp(proto, "http") == 0 ||
9933 strcmp(proto, "git+http") == 0) {
9934 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9937 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9941 error = got_dial_apply_unveil(proto);
9945 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9949 if (send_all_branches) {
9950 error = got_ref_list(&all_branches, repo, "refs/heads",
9951 got_ref_cmp_by_name, NULL);
9954 TAILQ_FOREACH(re, &all_branches, entry) {
9955 const char *branchname = got_ref_get_name(re->ref);
9956 error = got_pathlist_append(&branches,
9962 } else if (nbranches == 0) {
9963 for (i = 0; i < remote->nsend_branches; i++) {
9964 error = got_pathlist_append(&branches,
9965 remote->send_branches[i], NULL);
9971 if (send_all_tags) {
9972 error = got_ref_list(&all_tags, repo, "refs/tags",
9973 got_ref_cmp_by_name, NULL);
9976 TAILQ_FOREACH(re, &all_tags, entry) {
9977 const char *tagname = got_ref_get_name(re->ref);
9978 error = got_pathlist_append(&tags,
9986 * To prevent accidents only branches in refs/heads/ can be deleted
9987 * with 'got send -d'.
9988 * Deleting anything else requires local repository access or Git.
9990 TAILQ_FOREACH(pe, &delete_args, entry) {
9991 const char *branchname = pe->path;
9993 struct got_pathlist_entry *new;
9994 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9995 s = strdup(branchname);
9997 error = got_error_from_errno("strdup");
10001 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
10002 error = got_error_from_errno("asprintf");
10006 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
10007 if (error || new == NULL /* duplicate */)
10011 ndelete_branches++;
10014 if (nbranches == 0 && ndelete_branches == 0) {
10015 struct got_reference *head_ref;
10017 error = got_ref_open(&head_ref, repo,
10018 got_worktree_get_head_ref_name(worktree), 0);
10020 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
10023 if (got_ref_is_symbolic(head_ref)) {
10024 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
10025 got_ref_close(head_ref);
10030 error = got_pathlist_append(&branches, got_ref_get_name(ref),
10038 /* Release work tree lock. */
10039 got_worktree_close(worktree);
10043 if (verbosity >= 0) {
10044 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10045 remote->name, proto, host,
10046 port ? ":" : "", port ? port : "",
10047 *server_path == '/' ? "" : "/", server_path);
10050 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
10051 server_path, verbosity);
10055 memset(&spa, 0, sizeof(spa));
10056 spa.last_scaled_packsize[0] = '\0';
10057 spa.last_p_deltify = -1;
10058 spa.last_p_written = -1;
10059 spa.verbosity = verbosity;
10060 spa.delete_branches = &delete_branches;
10061 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
10062 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
10063 check_cancelled, NULL);
10064 if (spa.printed_something)
10068 if (!spa.sent_something && verbosity >= 0)
10069 printf("Already up-to-date\n");
10072 if (kill(sendpid, SIGTERM) == -1)
10073 error = got_error_from_errno("kill");
10074 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
10075 error = got_error_from_errno("waitpid");
10077 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
10078 error = got_error_from_errno("close");
10080 const struct got_error *close_err = got_repo_close(repo);
10085 got_worktree_close(worktree);
10087 const struct got_error *pack_err =
10088 got_repo_pack_fds_close(pack_fds);
10093 got_ref_close(ref);
10094 got_repo_free_remote_repo_data(remote);
10096 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10097 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10098 got_ref_list_free(&all_branches);
10099 got_ref_list_free(&all_tags);
10100 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10101 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10113 * Print and if delete is set delete all ref_prefix references.
10114 * If wanted_ref is not NULL, only print or delete this reference.
10116 static const struct got_error *
10117 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10118 const char *wanted_ref, int delete, struct got_worktree *worktree,
10119 struct got_repository *repo)
10121 const struct got_error *err;
10122 struct got_pathlist_head paths;
10123 struct got_reflist_head refs;
10124 struct got_reflist_entry *re;
10125 struct got_reflist_object_id_map *refs_idmap = NULL;
10126 struct got_commit_object *commit = NULL;
10127 struct got_object_id *id = NULL;
10128 const char *header_prefix;
10129 char *uuidstr = NULL;
10133 TAILQ_INIT(&paths);
10135 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10139 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10143 if (worktree != NULL) {
10144 err = got_worktree_get_uuid(&uuidstr, worktree);
10150 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10154 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10155 header_prefix = "backout";
10157 header_prefix = "cherrypick";
10159 TAILQ_FOREACH(re, &refs, entry) {
10160 const char *refname, *wt;
10162 refname = got_ref_get_name(re->ref);
10164 err = check_cancelled(NULL);
10168 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10169 refname += prefix_len + 1; /* skip '-' delimiter */
10175 if (worktree == NULL || strncmp(refname, uuidstr,
10176 GOT_WORKTREE_UUID_STRLEN) == 0)
10177 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10181 err = got_repo_match_object_id(&id, NULL, refname,
10182 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10186 err = got_object_open_as_commit(&commit, repo, id);
10191 found = strncmp(wanted_ref, refname,
10192 strlen(wanted_ref)) == 0;
10193 if (wanted_ref && !found) {
10194 struct got_reflist_head *ci_refs;
10196 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10200 char *refs_str = NULL;
10201 char const *r = NULL;
10203 err = build_refs_str(&refs_str, ci_refs, id,
10210 if (strncmp(r, wanted_ref,
10211 strlen(wanted_ref)) == 0) {
10215 r = strchr(r, ' ');
10223 if (wanted_ref == NULL || found) {
10225 err = got_ref_delete(re->ref, repo);
10228 printf("Deleted: ");
10229 err = print_commit_oneline(commit, id, repo,
10233 * Print paths modified by commit to help
10234 * associate commits with worktree changes.
10236 err = get_changed_paths(&paths, commit,
10241 err = print_commit(commit, id, repo, NULL,
10242 &paths, NULL, 0, 0, refs_idmap, NULL,
10244 got_pathlist_free(&paths,
10245 GOT_PATHLIST_FREE_ALL);
10247 if (worktree == NULL)
10248 printf("work tree: %.*s\n\n",
10249 GOT_WORKTREE_UUID_STRLEN, wt);
10255 got_object_commit_close(commit);
10261 if (wanted_ref != NULL && !found)
10262 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10267 got_ref_list_free(&refs);
10268 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10270 got_reflist_object_id_map_free(refs_idmap);
10272 got_object_commit_close(commit);
10277 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10278 * identified by id for log messages to prepopulate the editor on commit.
10280 static const struct got_error *
10281 logmsg_ref(struct got_object_id *id, const char *prefix,
10282 struct got_worktree *worktree, struct got_repository *repo)
10284 const struct got_error *err = NULL;
10285 char *idstr, *ref = NULL, *refname = NULL;
10286 int histedit_in_progress;
10287 int rebase_in_progress, merge_in_progress;
10290 * Silenty refuse to create merge reference if any histedit, merge,
10291 * or rebase operation is in progress.
10293 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10297 if (histedit_in_progress)
10300 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10303 if (rebase_in_progress)
10306 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10310 if (merge_in_progress)
10313 err = got_object_id_str(&idstr, id);
10317 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10321 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10322 err = got_error_from_errno("asprintf");
10326 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10336 usage_cherrypick(void)
10338 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10343 static const struct got_error *
10344 cmd_cherrypick(int argc, char *argv[])
10346 const struct got_error *error = NULL;
10347 struct got_worktree *worktree = NULL;
10348 struct got_repository *repo = NULL;
10349 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10350 struct got_object_id *commit_id = NULL;
10351 struct got_commit_object *commit = NULL;
10352 struct got_object_qid *pid;
10353 int ch, list_refs = 0, remove_refs = 0;
10354 struct got_update_progress_arg upa;
10355 int *pack_fds = NULL;
10358 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10359 "unveil", NULL) == -1)
10363 while ((ch = getopt(argc, argv, "lX")) != -1) {
10372 usage_cherrypick();
10380 if (list_refs || remove_refs) {
10381 if (argc != 0 && argc != 1)
10382 usage_cherrypick();
10383 } else if (argc != 1)
10384 usage_cherrypick();
10385 if (list_refs && remove_refs)
10386 option_conflict('l', 'X');
10388 cwd = getcwd(NULL, 0);
10390 error = got_error_from_errno("getcwd");
10394 error = got_repo_pack_fds_open(&pack_fds);
10398 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10400 if (list_refs || remove_refs) {
10401 if (error->code != GOT_ERR_NOT_WORKTREE)
10404 if (error->code == GOT_ERR_NOT_WORKTREE)
10405 error = wrap_not_worktree_error(error,
10406 "cherrypick", cwd);
10411 error = got_repo_open(&repo,
10412 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10417 error = apply_unveil(got_repo_get_path(repo), 0,
10418 worktree ? got_worktree_get_root_path(worktree) : NULL);
10422 if (list_refs || remove_refs) {
10423 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10424 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10425 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10429 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10433 error = got_repo_match_object_id(&commit_id, NULL,
10434 keyword_idstr != NULL ? keyword_idstr : argv[0],
10435 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10438 error = got_object_id_str(&commit_id_str, commit_id);
10442 error = got_object_open_as_commit(&commit, repo, commit_id);
10445 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10446 memset(&upa, 0, sizeof(upa));
10447 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10448 commit_id, repo, update_progress, &upa, check_cancelled,
10453 if (upa.did_something) {
10454 error = logmsg_ref(commit_id,
10455 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10458 printf("Merged commit %s\n", commit_id_str);
10460 print_merge_progress_stats(&upa);
10463 free(keyword_idstr);
10465 got_object_commit_close(commit);
10466 free(commit_id_str);
10468 got_worktree_close(worktree);
10470 const struct got_error *close_err = got_repo_close(repo);
10475 const struct got_error *pack_err =
10476 got_repo_pack_fds_close(pack_fds);
10485 usage_backout(void)
10487 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10491 static const struct got_error *
10492 cmd_backout(int argc, char *argv[])
10494 const struct got_error *error = NULL;
10495 struct got_worktree *worktree = NULL;
10496 struct got_repository *repo = NULL;
10497 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10498 struct got_object_id *commit_id = NULL;
10499 struct got_commit_object *commit = NULL;
10500 struct got_object_qid *pid;
10501 int ch, list_refs = 0, remove_refs = 0;
10502 struct got_update_progress_arg upa;
10503 int *pack_fds = NULL;
10506 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10507 "unveil", NULL) == -1)
10511 while ((ch = getopt(argc, argv, "lX")) != -1) {
10528 if (list_refs || remove_refs) {
10529 if (argc != 0 && argc != 1)
10531 } else if (argc != 1)
10533 if (list_refs && remove_refs)
10534 option_conflict('l', 'X');
10536 cwd = getcwd(NULL, 0);
10538 error = got_error_from_errno("getcwd");
10542 error = got_repo_pack_fds_open(&pack_fds);
10546 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10548 if (list_refs || remove_refs) {
10549 if (error->code != GOT_ERR_NOT_WORKTREE)
10552 if (error->code == GOT_ERR_NOT_WORKTREE)
10553 error = wrap_not_worktree_error(error,
10559 error = got_repo_open(&repo,
10560 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10565 error = apply_unveil(got_repo_get_path(repo), 0,
10566 worktree ? got_worktree_get_root_path(worktree) : NULL);
10570 if (list_refs || remove_refs) {
10571 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10572 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10573 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10577 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10581 error = got_repo_match_object_id(&commit_id, NULL,
10582 keyword_idstr != NULL ? keyword_idstr : argv[0],
10583 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10586 error = got_object_id_str(&commit_id_str, commit_id);
10590 error = got_object_open_as_commit(&commit, repo, commit_id);
10593 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10595 error = got_error(GOT_ERR_ROOT_COMMIT);
10599 memset(&upa, 0, sizeof(upa));
10600 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10601 repo, update_progress, &upa, check_cancelled, NULL);
10605 if (upa.did_something) {
10606 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10610 printf("Backed out commit %s\n", commit_id_str);
10612 print_merge_progress_stats(&upa);
10615 free(keyword_idstr);
10617 got_object_commit_close(commit);
10618 free(commit_id_str);
10620 got_worktree_close(worktree);
10622 const struct got_error *close_err = got_repo_close(repo);
10627 const struct got_error *pack_err =
10628 got_repo_pack_fds_close(pack_fds);
10638 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10643 trim_logmsg(char *logmsg, int limit)
10648 len = strlen(logmsg);
10651 logmsg[len] = '\0';
10652 nl = strchr(logmsg, '\n');
10657 static const struct got_error *
10658 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10660 const struct got_error *err;
10661 char *logmsg0 = NULL;
10664 err = got_object_commit_get_logmsg(&logmsg0, commit);
10669 while (isspace((unsigned char)s[0]))
10672 *logmsg = strdup(s);
10673 if (*logmsg == NULL) {
10674 err = got_error_from_errno("strdup");
10678 trim_logmsg(*logmsg, limit);
10684 static const struct got_error *
10685 show_rebase_merge_conflict(struct got_object_id *id,
10686 struct got_repository *repo)
10688 const struct got_error *err;
10689 struct got_commit_object *commit = NULL;
10690 char *id_str = NULL, *logmsg = NULL;
10692 err = got_object_open_as_commit(&commit, repo, id);
10696 err = got_object_id_str(&id_str, id);
10702 err = get_short_logmsg(&logmsg, 42, commit);
10706 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10709 got_object_commit_close(commit);
10714 static const struct got_error *
10715 show_rebase_progress(struct got_commit_object *commit,
10716 struct got_object_id *old_id, struct got_object_id *new_id)
10718 const struct got_error *err;
10719 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10721 err = got_object_id_str(&old_id_str, old_id);
10726 err = got_object_id_str(&new_id_str, new_id);
10731 old_id_str[12] = '\0';
10733 new_id_str[12] = '\0';
10735 err = get_short_logmsg(&logmsg, 42, commit);
10739 printf("%s -> %s: %s\n", old_id_str,
10740 new_id_str ? new_id_str : "no-op change", logmsg);
10748 static const struct got_error *
10749 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10750 struct got_reference *branch, struct got_reference *tmp_branch,
10751 struct got_repository *repo, int create_backup)
10753 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10754 return got_worktree_rebase_complete(worktree, fileindex,
10755 tmp_branch, branch, repo, create_backup);
10758 static const struct got_error *
10759 rebase_commit(struct got_pathlist_head *merged_paths,
10760 struct got_worktree *worktree, struct got_fileindex *fileindex,
10761 struct got_reference *tmp_branch, const char *committer,
10762 struct got_object_id *commit_id, int allow_conflict,
10763 struct got_repository *repo)
10765 const struct got_error *error;
10766 struct got_commit_object *commit;
10767 struct got_object_id *new_commit_id;
10769 error = got_object_open_as_commit(&commit, repo, commit_id);
10773 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10774 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10775 allow_conflict, repo);
10777 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10779 error = show_rebase_progress(commit, commit_id, NULL);
10781 error = show_rebase_progress(commit, commit_id, new_commit_id);
10782 free(new_commit_id);
10785 got_object_commit_close(commit);
10789 struct check_path_prefix_arg {
10790 const char *path_prefix;
10795 static const struct got_error *
10796 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10797 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10798 struct got_object_id *id1, struct got_object_id *id2,
10799 const char *path1, const char *path2,
10800 mode_t mode1, mode_t mode2, struct got_repository *repo)
10802 struct check_path_prefix_arg *a = arg;
10804 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10805 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10806 return got_error(a->errcode);
10811 static const struct got_error *
10812 check_path_prefix(struct got_object_id *parent_id,
10813 struct got_object_id *commit_id, const char *path_prefix,
10814 int errcode, struct got_repository *repo)
10816 const struct got_error *err;
10817 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10818 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10819 struct check_path_prefix_arg cpp_arg;
10821 if (got_path_is_root_dir(path_prefix))
10824 err = got_object_open_as_commit(&commit, repo, commit_id);
10828 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10832 err = got_object_open_as_tree(&tree1, repo,
10833 got_object_commit_get_tree_id(parent_commit));
10837 err = got_object_open_as_tree(&tree2, repo,
10838 got_object_commit_get_tree_id(commit));
10842 cpp_arg.path_prefix = path_prefix;
10843 while (cpp_arg.path_prefix[0] == '/')
10844 cpp_arg.path_prefix++;
10845 cpp_arg.len = strlen(cpp_arg.path_prefix);
10846 cpp_arg.errcode = errcode;
10847 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10848 check_path_prefix_in_diff, &cpp_arg, 0);
10851 got_object_tree_close(tree1);
10853 got_object_tree_close(tree2);
10855 got_object_commit_close(commit);
10857 got_object_commit_close(parent_commit);
10861 static const struct got_error *
10862 collect_commits(struct got_object_id_queue *commits,
10863 struct got_object_id *initial_commit_id,
10864 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10865 const char *path_prefix, int path_prefix_errcode,
10866 struct got_repository *repo)
10868 const struct got_error *err = NULL;
10869 struct got_commit_graph *graph = NULL;
10870 struct got_object_id parent_id, commit_id;
10871 struct got_object_qid *qid;
10873 err = got_commit_graph_open(&graph, "/", 1);
10877 err = got_commit_graph_bfsort(graph, iter_start_id, repo,
10878 check_cancelled, NULL);
10882 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10883 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10884 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10885 check_cancelled, NULL);
10887 if (err->code == GOT_ERR_ITER_COMPLETED) {
10888 err = got_error_msg(GOT_ERR_ANCESTRY,
10889 "ran out of commits to rebase before "
10890 "youngest common ancestor commit has "
10891 "been reached?!?");
10895 err = check_path_prefix(&parent_id, &commit_id,
10896 path_prefix, path_prefix_errcode, repo);
10900 err = got_object_qid_alloc(&qid, &commit_id);
10903 STAILQ_INSERT_HEAD(commits, qid, entry);
10905 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10909 got_commit_graph_close(graph);
10913 static const struct got_error *
10914 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10916 const struct got_error *err = NULL;
10917 time_t committer_time;
10919 char datebuf[11]; /* YYYY-MM-DD + NUL */
10920 char *author0 = NULL, *author, *smallerthan;
10921 char *logmsg0 = NULL, *logmsg, *newline;
10923 committer_time = got_object_commit_get_committer_time(commit);
10924 if (gmtime_r(&committer_time, &tm) == NULL)
10925 return got_error_from_errno("gmtime_r");
10926 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10927 return got_error(GOT_ERR_NO_SPACE);
10929 author0 = strdup(got_object_commit_get_author(commit));
10930 if (author0 == NULL)
10931 return got_error_from_errno("strdup");
10933 smallerthan = strchr(author, '<');
10934 if (smallerthan && smallerthan[1] != '\0')
10935 author = smallerthan + 1;
10936 author[strcspn(author, "@>")] = '\0';
10938 err = got_object_commit_get_logmsg(&logmsg0, commit);
10942 while (*logmsg == '\n')
10944 newline = strchr(logmsg, '\n');
10948 if (asprintf(brief_str, "%s %s %s",
10949 datebuf, author, logmsg) == -1)
10950 err = got_error_from_errno("asprintf");
10957 static const struct got_error *
10958 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10959 struct got_repository *repo)
10961 const struct got_error *err;
10964 err = got_object_id_str(&id_str, id);
10968 err = got_ref_delete(ref, repo);
10972 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10978 static const struct got_error *
10979 print_backup_ref(const char *branch_name, const char *new_id_str,
10980 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10981 struct got_reflist_object_id_map *refs_idmap,
10982 struct got_repository *repo)
10984 const struct got_error *err = NULL;
10985 struct got_reflist_head *refs;
10986 char *refs_str = NULL;
10987 struct got_object_id *new_commit_id = NULL;
10988 struct got_commit_object *new_commit = NULL;
10989 char *new_commit_brief_str = NULL;
10990 struct got_object_id *yca_id = NULL;
10991 struct got_commit_object *yca_commit = NULL;
10992 char *yca_id_str = NULL, *yca_brief_str = NULL;
10993 char *custom_refs_str;
10995 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10996 return got_error_from_errno("asprintf");
10998 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10999 0, 0, refs_idmap, custom_refs_str, NULL);
11003 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
11007 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
11009 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
11014 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
11018 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
11022 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11023 old_commit_id, new_commit_id, 1, 0, repo, check_cancelled, NULL);
11027 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
11028 refs_str ? " (" : "", refs_str ? refs_str : "",
11029 refs_str ? ")" : "", new_commit_brief_str);
11030 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
11031 got_object_id_cmp(yca_id, old_commit_id) != 0) {
11035 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
11039 err = get_commit_brief_str(&yca_brief_str, yca_commit);
11043 err = got_object_id_str(&yca_id_str, yca_id);
11047 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
11049 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
11053 printf("history forked at %s%s%s%s\n %s\n",
11055 refs_str ? " (" : "", refs_str ? refs_str : "",
11056 refs_str ? ")" : "", yca_brief_str);
11059 free(custom_refs_str);
11060 free(new_commit_id);
11064 free(yca_brief_str);
11066 got_object_commit_close(new_commit);
11068 got_object_commit_close(yca_commit);
11073 static const struct got_error *
11074 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
11075 struct got_repository *repo)
11077 const struct got_error *err;
11078 struct got_reflist_head refs;
11079 struct got_reflist_entry *re;
11080 char *uuidstr = NULL;
11081 static char msg[160];
11085 err = got_worktree_get_uuid(&uuidstr, worktree);
11089 err = got_ref_list(&refs, repo, "refs/got/worktree",
11090 got_ref_cmp_by_name, repo);
11094 TAILQ_FOREACH(re, &refs, entry) {
11095 const char *cmd, *refname, *type;
11097 refname = got_ref_get_name(re->ref);
11099 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11100 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11101 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11102 cmd = "cherrypick";
11103 type = "cherrypicked";
11104 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11105 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11106 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11108 type = "backed-out";
11112 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11115 snprintf(msg, sizeof(msg),
11116 "work tree has references created by %s commits which "
11117 "must be removed with 'got %s -X' before running the %s "
11118 "command", type, cmd, caller);
11119 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11125 got_ref_list_free(&refs);
11129 static const struct got_error *
11130 process_backup_refs(const char *backup_ref_prefix,
11131 const char *wanted_branch_name,
11132 int delete, struct got_repository *repo)
11134 const struct got_error *err;
11135 struct got_reflist_head refs, backup_refs;
11136 struct got_reflist_entry *re;
11137 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11138 struct got_object_id *old_commit_id = NULL;
11139 char *branch_name = NULL;
11140 struct got_commit_object *old_commit = NULL;
11141 struct got_reflist_object_id_map *refs_idmap = NULL;
11142 int wanted_branch_found = 0;
11145 TAILQ_INIT(&backup_refs);
11147 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11151 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11155 if (wanted_branch_name) {
11156 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11157 wanted_branch_name += 11;
11160 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11161 got_ref_cmp_by_commit_timestamp_descending, repo);
11165 TAILQ_FOREACH(re, &backup_refs, entry) {
11166 const char *refname = got_ref_get_name(re->ref);
11169 err = check_cancelled(NULL);
11173 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11177 err = got_object_open_as_commit(&old_commit, repo,
11182 if (strncmp(backup_ref_prefix, refname,
11183 backup_ref_prefix_len) == 0)
11184 refname += backup_ref_prefix_len;
11186 while (refname[0] == '/')
11189 branch_name = strdup(refname);
11190 if (branch_name == NULL) {
11191 err = got_error_from_errno("strdup");
11194 slash = strrchr(branch_name, '/');
11197 refname += strlen(branch_name) + 1;
11200 if (wanted_branch_name == NULL ||
11201 strcmp(wanted_branch_name, branch_name) == 0) {
11202 wanted_branch_found = 1;
11204 err = delete_backup_ref(re->ref,
11205 old_commit_id, repo);
11207 err = print_backup_ref(branch_name, refname,
11208 old_commit_id, old_commit, refs_idmap,
11215 free(old_commit_id);
11216 old_commit_id = NULL;
11218 branch_name = NULL;
11219 got_object_commit_close(old_commit);
11223 if (wanted_branch_name && !wanted_branch_found) {
11224 err = got_error_fmt(GOT_ERR_NOT_REF,
11225 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11229 got_reflist_object_id_map_free(refs_idmap);
11230 got_ref_list_free(&refs);
11231 got_ref_list_free(&backup_refs);
11232 free(old_commit_id);
11235 got_object_commit_close(old_commit);
11239 static const struct got_error *
11240 abort_progress(void *arg, unsigned char status, const char *path)
11243 * Unversioned files should not clutter progress output when
11244 * an operation is aborted.
11246 if (status == GOT_STATUS_UNVERSIONED)
11249 return update_progress(arg, status, path);
11252 static const struct got_error *
11253 find_merge_commit_yca(struct got_object_id **new_yca_id,
11254 struct got_object_id *branch_head_commit_id,
11255 struct got_object_id *yca_id,
11256 struct got_object_id *base_commit_id,
11257 struct got_repository *repo)
11259 const struct got_error *err = NULL;
11260 struct got_commit_graph *graph = NULL;
11261 struct got_commit_object *commit = NULL;
11263 *new_yca_id = NULL;
11265 err = got_commit_graph_open(&graph, "/", 1);
11269 err = got_commit_graph_bfsort(graph, base_commit_id,
11270 repo, check_cancelled, NULL);
11275 struct got_object_id id;
11277 err = got_commit_graph_iter_next(&id, graph, repo,
11278 check_cancelled, NULL);
11280 if (err->code == GOT_ERR_ITER_COMPLETED)
11285 err = got_object_open_as_commit(&commit, repo, &id);
11289 if (got_object_commit_get_nparents(commit) > 1) {
11290 /* Search for a better YCA using toposort. */
11291 err = got_commit_graph_find_youngest_common_ancestor(
11292 new_yca_id, base_commit_id, branch_head_commit_id,
11293 0, 1, repo, check_cancelled, NULL);
11297 if (got_object_id_cmp(&id, yca_id) == 0)
11299 got_object_commit_close(commit);
11303 got_commit_graph_close(graph);
11305 got_object_commit_close(commit);
11309 static const struct got_error *
11310 cmd_rebase(int argc, char *argv[])
11312 const struct got_error *error = NULL;
11313 struct got_worktree *worktree = NULL;
11314 struct got_repository *repo = NULL;
11315 struct got_fileindex *fileindex = NULL;
11316 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11317 struct got_reference *branch = NULL;
11318 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11319 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11320 struct got_object_id *resume_commit_id = NULL;
11321 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11322 struct got_object_id *head_commit_id = NULL;
11323 struct got_reference *head_ref = NULL;
11324 struct got_commit_object *commit = NULL;
11325 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11326 int histedit_in_progress = 0, merge_in_progress = 0;
11327 int create_backup = 1, list_backups = 0, delete_backups = 0;
11328 int allow_conflict = 0;
11329 struct got_object_id_queue commits;
11330 struct got_pathlist_head merged_paths;
11331 const struct got_object_id_queue *parent_ids;
11332 struct got_object_qid *qid, *pid;
11333 struct got_update_progress_arg upa;
11334 int *pack_fds = NULL;
11336 STAILQ_INIT(&commits);
11337 TAILQ_INIT(&merged_paths);
11338 memset(&upa, 0, sizeof(upa));
11341 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11342 "unveil", NULL) == -1)
11346 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11352 allow_conflict = 1;
11355 continue_rebase = 1;
11361 delete_backups = 1;
11372 if (list_backups) {
11374 option_conflict('l', 'a');
11375 if (allow_conflict)
11376 option_conflict('l', 'C');
11377 if (continue_rebase)
11378 option_conflict('l', 'c');
11379 if (delete_backups)
11380 option_conflict('l', 'X');
11381 if (argc != 0 && argc != 1)
11383 } else if (delete_backups) {
11385 option_conflict('X', 'a');
11386 if (allow_conflict)
11387 option_conflict('X', 'C');
11388 if (continue_rebase)
11389 option_conflict('X', 'c');
11391 option_conflict('l', 'X');
11392 if (argc != 0 && argc != 1)
11394 } else if (allow_conflict) {
11396 option_conflict('C', 'a');
11397 if (!continue_rebase)
11398 errx(1, "-C option requires -c");
11400 if (abort_rebase && continue_rebase)
11402 else if (abort_rebase || continue_rebase) {
11405 } else if (argc != 1)
11409 cwd = getcwd(NULL, 0);
11411 error = got_error_from_errno("getcwd");
11415 error = got_repo_pack_fds_open(&pack_fds);
11419 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11421 if (list_backups || delete_backups) {
11422 if (error->code != GOT_ERR_NOT_WORKTREE)
11425 if (error->code == GOT_ERR_NOT_WORKTREE)
11426 error = wrap_not_worktree_error(error,
11432 error = get_gitconfig_path(&gitconfig_path);
11435 error = got_repo_open(&repo,
11436 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11437 gitconfig_path, pack_fds);
11441 if (worktree != NULL && !list_backups && !delete_backups) {
11442 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11447 error = get_author(&committer, repo, worktree);
11448 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11451 error = apply_unveil(got_repo_get_path(repo), 0,
11452 worktree ? got_worktree_get_root_path(worktree) : NULL);
11456 if (list_backups || delete_backups) {
11457 error = process_backup_refs(
11458 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11459 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11460 goto done; /* nothing else to do */
11463 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11467 if (histedit_in_progress) {
11468 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11472 error = got_worktree_merge_in_progress(&merge_in_progress,
11476 if (merge_in_progress) {
11477 error = got_error(GOT_ERR_MERGE_BUSY);
11481 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11485 if (abort_rebase) {
11486 if (!rebase_in_progress) {
11487 error = got_error(GOT_ERR_NOT_REBASING);
11490 error = got_worktree_rebase_continue(&resume_commit_id,
11491 &new_base_branch, &tmp_branch, &branch, &fileindex,
11495 printf("Switching work tree to %s\n",
11496 got_ref_get_symref_target(new_base_branch));
11497 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11498 new_base_branch, abort_progress, &upa);
11501 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11502 print_merge_progress_stats(&upa);
11503 goto done; /* nothing else to do */
11506 if (continue_rebase) {
11507 if (!rebase_in_progress) {
11508 error = got_error(GOT_ERR_NOT_REBASING);
11511 error = got_worktree_rebase_continue(&resume_commit_id,
11512 &new_base_branch, &tmp_branch, &branch, &fileindex,
11517 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11518 committer, resume_commit_id, allow_conflict, repo);
11522 yca_id = got_object_id_dup(resume_commit_id);
11523 if (yca_id == NULL) {
11524 error = got_error_from_errno("got_object_id_dup");
11528 error = got_ref_open(&branch, repo, argv[0], 0);
11531 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11532 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11533 "will not rebase a branch which lives outside "
11534 "the \"refs/heads/\" reference namespace");
11539 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11543 if (!continue_rebase) {
11544 struct got_object_id *base_commit_id;
11546 error = got_ref_open(&head_ref, repo,
11547 got_worktree_get_head_ref_name(worktree), 0);
11550 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11553 base_commit_id = got_worktree_get_base_commit_id(worktree);
11554 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11555 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11559 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11560 base_commit_id, branch_head_commit_id, 1, 0,
11561 repo, check_cancelled, NULL);
11563 if (error->code == GOT_ERR_ANCESTRY) {
11564 error = got_error_msg(GOT_ERR_ANCESTRY,
11565 "specified branch shares no common "
11566 "ancestry with work tree's branch");
11572 * If a merge commit appears between the new base branch tip
11573 * and a YCA found via first-parent traversal then we might
11574 * find a better YCA using topologically sorted commits.
11576 if (got_object_id_cmp(base_commit_id, yca_id) != 0) {
11577 struct got_object_id *better_yca_id;
11578 error = find_merge_commit_yca(&better_yca_id,
11579 branch_head_commit_id, yca_id,
11580 base_commit_id, repo);
11583 if (better_yca_id) {
11585 yca_id = better_yca_id;
11589 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11590 struct got_pathlist_head paths;
11591 printf("%s is already based on %s\n",
11592 got_ref_get_name(branch),
11593 got_worktree_get_head_ref_name(worktree));
11594 error = switch_head_ref(branch, branch_head_commit_id,
11598 error = got_worktree_set_base_commit_id(worktree, repo,
11599 branch_head_commit_id);
11602 TAILQ_INIT(&paths);
11603 error = got_pathlist_append(&paths, "", NULL);
11606 error = got_worktree_checkout_files(worktree,
11607 &paths, repo, update_progress, &upa,
11608 check_cancelled, NULL);
11609 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11612 if (upa.did_something) {
11614 error = got_object_id_str(&id_str,
11615 branch_head_commit_id);
11618 printf("Updated to %s: %s\n",
11619 got_worktree_get_head_ref_name(worktree),
11623 printf("Already up-to-date\n");
11624 print_update_progress_stats(&upa);
11629 commit_id = branch_head_commit_id;
11630 error = got_object_open_as_commit(&commit, repo, commit_id);
11634 parent_ids = got_object_commit_get_parent_ids(commit);
11635 pid = STAILQ_FIRST(parent_ids);
11637 error = collect_commits(&commits, commit_id, &pid->id,
11638 yca_id, got_worktree_get_path_prefix(worktree),
11639 GOT_ERR_REBASE_PATH, repo);
11644 got_object_commit_close(commit);
11647 if (!continue_rebase) {
11648 error = got_worktree_rebase_prepare(&new_base_branch,
11649 &tmp_branch, &fileindex, worktree, branch, repo);
11654 if (STAILQ_EMPTY(&commits)) {
11655 if (continue_rebase) {
11656 error = rebase_complete(worktree, fileindex,
11657 branch, tmp_branch, repo, create_backup);
11660 /* Fast-forward the reference of the branch. */
11661 struct got_object_id *new_head_commit_id;
11663 error = got_ref_resolve(&new_head_commit_id, repo,
11667 error = got_object_id_str(&id_str, new_head_commit_id);
11670 printf("Forwarding %s to commit %s\n",
11671 got_ref_get_name(branch), id_str);
11673 error = got_ref_change_ref(branch,
11674 new_head_commit_id);
11677 /* No backup needed since objects did not change. */
11683 STAILQ_FOREACH(qid, &commits, entry) {
11685 commit_id = &qid->id;
11686 parent_id = pid ? &pid->id : yca_id;
11689 memset(&upa, 0, sizeof(upa));
11690 error = got_worktree_rebase_merge_files(&merged_paths,
11691 worktree, fileindex, parent_id, commit_id, repo,
11692 update_progress, &upa, check_cancelled, NULL);
11696 print_merge_progress_stats(&upa);
11697 if (upa.conflicts > 0 || upa.missing > 0 ||
11698 upa.not_deleted > 0 || upa.unversioned > 0) {
11699 if (upa.conflicts > 0) {
11700 error = show_rebase_merge_conflict(&qid->id,
11705 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11709 error = rebase_commit(&merged_paths, worktree, fileindex,
11710 tmp_branch, committer, commit_id, 0, repo);
11711 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11716 if (upa.conflicts > 0 || upa.missing > 0 ||
11717 upa.not_deleted > 0 || upa.unversioned > 0) {
11718 error = got_worktree_rebase_postpone(worktree, fileindex);
11721 if (upa.conflicts > 0 && upa.missing == 0 &&
11722 upa.not_deleted == 0 && upa.unversioned == 0) {
11723 error = got_error_msg(GOT_ERR_CONFLICTS,
11724 "conflicts must be resolved before rebasing "
11726 } else if (upa.conflicts > 0) {
11727 error = got_error_msg(GOT_ERR_CONFLICTS,
11728 "conflicts must be resolved before rebasing "
11729 "can continue; changes destined for some "
11730 "files were not yet merged and should be "
11731 "merged manually if required before the "
11732 "rebase operation is continued");
11734 error = got_error_msg(GOT_ERR_CONFLICTS,
11735 "changes destined for some files were not "
11736 "yet merged and should be merged manually "
11737 "if required before the rebase operation "
11741 error = rebase_complete(worktree, fileindex, branch,
11742 tmp_branch, repo, create_backup);
11746 free(gitconfig_path);
11747 got_object_id_queue_free(&commits);
11748 free(branch_head_commit_id);
11749 free(resume_commit_id);
11750 free(head_commit_id);
11753 got_object_commit_close(commit);
11755 got_ref_close(branch);
11756 if (new_base_branch)
11757 got_ref_close(new_base_branch);
11759 got_ref_close(tmp_branch);
11761 got_ref_close(head_ref);
11763 got_worktree_close(worktree);
11765 const struct got_error *close_err = got_repo_close(repo);
11770 const struct got_error *pack_err =
11771 got_repo_pack_fds_close(pack_fds);
11779 usage_histedit(void)
11781 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11782 "[branch]\n", getprogname());
11786 #define GOT_HISTEDIT_PICK 'p'
11787 #define GOT_HISTEDIT_EDIT 'e'
11788 #define GOT_HISTEDIT_FOLD 'f'
11789 #define GOT_HISTEDIT_DROP 'd'
11790 #define GOT_HISTEDIT_MESG 'm'
11792 static const struct got_histedit_cmd {
11793 unsigned char code;
11796 } got_histedit_cmds[] = {
11797 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11798 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11799 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11801 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11802 { GOT_HISTEDIT_MESG, "mesg", "open editor to edit the log message" },
11805 struct got_histedit_list_entry {
11806 TAILQ_ENTRY(got_histedit_list_entry) entry;
11807 struct got_object_id *commit_id;
11808 const struct got_histedit_cmd *cmd;
11811 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11813 static const struct got_error *
11814 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11815 FILE *f, struct got_repository *repo)
11817 const struct got_error *err = NULL;
11818 char *logmsg = NULL, *id_str = NULL;
11819 struct got_commit_object *commit = NULL;
11822 err = got_object_open_as_commit(&commit, repo, commit_id);
11826 err = get_short_logmsg(&logmsg, 34, commit);
11830 err = got_object_id_str(&id_str, commit_id);
11834 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11836 err = got_ferror(f, GOT_ERR_IO);
11839 got_object_commit_close(commit);
11845 static const struct got_error *
11846 histedit_write_commit_list(struct got_object_id_queue *commits,
11847 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11848 int edit_only, struct got_repository *repo)
11850 const struct got_error *err = NULL;
11851 struct got_object_qid *qid;
11852 const char *histedit_cmd = NULL;
11854 if (STAILQ_EMPTY(commits))
11855 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11857 STAILQ_FOREACH(qid, commits, entry) {
11858 histedit_cmd = got_histedit_cmds[0].name;
11860 histedit_cmd = "drop";
11861 else if (edit_only)
11862 histedit_cmd = "edit";
11863 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11864 histedit_cmd = "fold";
11865 else if (edit_logmsg_only)
11866 histedit_cmd = "mesg";
11867 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11875 static const struct got_error *
11876 write_cmd_list(FILE *f, const char *branch_name,
11877 struct got_object_id_queue *commits)
11879 const struct got_error *err = NULL;
11883 struct got_object_qid *qid;
11885 qid = STAILQ_FIRST(commits);
11886 err = got_object_id_str(&id_str, &qid->id);
11891 "# Editing the history of branch '%s' starting at\n"
11893 "# Commits will be processed in order from top to "
11894 "bottom of this file.\n", branch_name, id_str);
11896 err = got_ferror(f, GOT_ERR_IO);
11900 n = fprintf(f, "# Available histedit commands:\n");
11902 err = got_ferror(f, GOT_ERR_IO);
11906 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11907 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11908 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11911 err = got_ferror(f, GOT_ERR_IO);
11920 static const struct got_error *
11921 histedit_syntax_error(int lineno)
11923 static char msg[42];
11926 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11928 if (ret < 0 || (size_t)ret >= sizeof(msg))
11929 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11931 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11934 static const struct got_error *
11935 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11936 char *logmsg, struct got_repository *repo)
11938 const struct got_error *err;
11939 struct got_commit_object *folded_commit = NULL;
11940 char *id_str, *folded_logmsg = NULL;
11942 err = got_object_id_str(&id_str, hle->commit_id);
11946 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11950 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11953 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11954 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11955 folded_logmsg) == -1) {
11956 err = got_error_from_errno("asprintf");
11960 got_object_commit_close(folded_commit);
11962 free(folded_logmsg);
11966 static struct got_histedit_list_entry *
11967 get_folded_commits(struct got_histedit_list_entry *hle)
11969 struct got_histedit_list_entry *prev, *folded = NULL;
11971 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11972 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11973 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11974 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11976 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11982 static const struct got_error *
11983 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11984 struct got_repository *repo)
11986 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11987 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11988 const struct got_error *err = NULL;
11989 struct got_commit_object *commit = NULL;
11992 struct got_histedit_list_entry *folded = NULL;
11994 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11998 folded = get_folded_commits(hle);
12000 while (folded != hle) {
12001 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
12002 folded = TAILQ_NEXT(folded, entry);
12005 err = append_folded_commit_msg(&new_msg, folded,
12011 folded = TAILQ_NEXT(folded, entry);
12015 err = got_object_id_str(&id_str, hle->commit_id);
12018 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
12021 logmsg_len = asprintf(&new_msg,
12022 "%s\n# original log message of commit %s: %s",
12023 logmsg ? logmsg : "", id_str, orig_logmsg);
12024 if (logmsg_len == -1) {
12025 err = got_error_from_errno("asprintf");
12031 err = got_object_id_str(&id_str, hle->commit_id);
12035 err = got_opentemp_named_fd(&logmsg_path, &fd,
12036 GOT_TMPDIR_STR "/got-logmsg", "");
12040 if (write(fd, logmsg, logmsg_len) == -1) {
12041 err = got_error_from_errno2("write", logmsg_path);
12044 if (close(fd) == -1) {
12045 err = got_error_from_errno2("close", logmsg_path);
12050 err = get_editor(&editor);
12054 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
12057 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
12060 hle->logmsg = strdup(new_msg);
12061 if (hle->logmsg == NULL)
12062 err = got_error_from_errno("strdup");
12065 if (fd != -1 && close(fd) == -1 && err == NULL)
12066 err = got_error_from_errno2("close", logmsg_path);
12067 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
12068 err = got_error_from_errno2("unlink", logmsg_path);
12074 got_object_commit_close(commit);
12078 static const struct got_error *
12079 histedit_parse_list(struct got_histedit_list *histedit_cmds,
12080 FILE *f, struct got_repository *repo)
12082 const struct got_error *err = NULL;
12083 char *line = NULL, *p, *end;
12084 size_t i, linesize = 0;
12087 const struct got_histedit_cmd *cmd;
12088 struct got_object_id *commit_id = NULL;
12089 struct got_histedit_list_entry *hle = NULL;
12092 linelen = getline(&line, &linesize, f);
12093 if (linelen == -1) {
12094 const struct got_error *getline_err;
12097 getline_err = got_error_from_errno("getline");
12098 err = got_ferror(f, getline_err->code);
12103 while (isspace((unsigned char)p[0]))
12105 if (p[0] == '#' || p[0] == '\0')
12108 for (i = 0; i < nitems(got_histedit_cmds); i++) {
12109 cmd = &got_histedit_cmds[i];
12110 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
12111 isspace((unsigned char)p[strlen(cmd->name)])) {
12112 p += strlen(cmd->name);
12115 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
12120 if (i == nitems(got_histedit_cmds)) {
12121 err = histedit_syntax_error(lineno);
12124 while (isspace((unsigned char)p[0]))
12127 while (end[0] && !isspace((unsigned char)end[0]))
12130 err = got_object_resolve_id_str(&commit_id, repo, p);
12132 /* override error code */
12133 err = histedit_syntax_error(lineno);
12136 hle = malloc(sizeof(*hle));
12138 err = got_error_from_errno("malloc");
12142 hle->commit_id = commit_id;
12143 hle->logmsg = NULL;
12145 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12153 static const struct got_error *
12154 histedit_check_script(struct got_histedit_list *histedit_cmds,
12155 struct got_object_id_queue *commits, struct got_repository *repo)
12157 const struct got_error *err = NULL;
12158 struct got_object_qid *qid;
12159 struct got_histedit_list_entry *hle;
12160 static char msg[92];
12163 if (TAILQ_EMPTY(histedit_cmds))
12164 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12165 "histedit script contains no commands");
12166 if (STAILQ_EMPTY(commits))
12167 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12169 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12170 struct got_histedit_list_entry *hle2;
12171 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12174 if (got_object_id_cmp(hle->commit_id,
12175 hle2->commit_id) != 0)
12177 err = got_object_id_str(&id_str, hle->commit_id);
12180 snprintf(msg, sizeof(msg), "commit %s is listed "
12181 "more than once in histedit script", id_str);
12183 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12187 STAILQ_FOREACH(qid, commits, entry) {
12188 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12189 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12193 err = got_object_id_str(&id_str, &qid->id);
12196 snprintf(msg, sizeof(msg),
12197 "commit %s missing from histedit script", id_str);
12199 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12203 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12204 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12205 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12206 "last commit in histedit script cannot be folded");
12211 static const struct got_error *
12212 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12213 const char *path, struct got_object_id_queue *commits,
12214 struct got_repository *repo)
12216 const struct got_error *err = NULL;
12217 struct stat st, st2;
12218 struct timespec timeout;
12222 err = get_editor(&editor);
12226 if (stat(path, &st) == -1) {
12227 err = got_error_from_errno2("stat", path);
12231 if (spawn_editor(editor, path) == -1) {
12232 err = got_error_from_errno("failed spawning editor");
12236 timeout.tv_sec = 0;
12237 timeout.tv_nsec = 1;
12238 nanosleep(&timeout, NULL);
12240 if (stat(path, &st2) == -1) {
12241 err = got_error_from_errno2("stat", path);
12245 if (st.st_size == st2.st_size &&
12246 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12247 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12248 "no changes made to histedit script, aborting");
12252 f = fopen(path, "re");
12254 err = got_error_from_errno("fopen");
12257 err = histedit_parse_list(histedit_cmds, f, repo);
12261 err = histedit_check_script(histedit_cmds, commits, repo);
12263 if (f && fclose(f) == EOF && err == NULL)
12264 err = got_error_from_errno("fclose");
12269 static const struct got_error *
12270 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12271 struct got_object_id_queue *, const char *, const char *,
12272 struct got_repository *);
12274 static const struct got_error *
12275 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12276 struct got_object_id_queue *commits, const char *branch_name,
12277 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12278 struct got_repository *repo)
12280 const struct got_error *err;
12284 err = got_opentemp_named(&path, &f, "got-histedit", "");
12288 err = write_cmd_list(f, branch_name, commits);
12292 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12293 fold_only, drop_only, edit_only, repo);
12297 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12299 err = histedit_parse_list(histedit_cmds, f, repo);
12301 if (fclose(f) == EOF) {
12302 err = got_error_from_errno("fclose");
12306 err = histedit_run_editor(histedit_cmds, path, commits, repo);
12308 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12309 err->code != GOT_ERR_HISTEDIT_CMD)
12311 err = histedit_edit_list_retry(histedit_cmds, err,
12312 commits, path, branch_name, repo);
12316 if (f && fclose(f) == EOF && err == NULL)
12317 err = got_error_from_errno("fclose");
12318 if (path && unlink(path) != 0 && err == NULL)
12319 err = got_error_from_errno2("unlink", path);
12324 static const struct got_error *
12325 histedit_save_list(struct got_histedit_list *histedit_cmds,
12326 struct got_worktree *worktree, struct got_repository *repo)
12328 const struct got_error *err = NULL;
12331 struct got_histedit_list_entry *hle;
12333 err = got_worktree_get_histedit_script_path(&path, worktree);
12337 f = fopen(path, "we");
12339 err = got_error_from_errno2("fopen", path);
12342 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12343 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12349 if (f && fclose(f) == EOF && err == NULL)
12350 err = got_error_from_errno("fclose");
12356 histedit_free_list(struct got_histedit_list *histedit_cmds)
12358 struct got_histedit_list_entry *hle;
12360 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12361 TAILQ_REMOVE(histedit_cmds, hle, entry);
12366 static const struct got_error *
12367 histedit_load_list(struct got_histedit_list *histedit_cmds,
12368 const char *path, struct got_repository *repo)
12370 const struct got_error *err = NULL;
12373 f = fopen(path, "re");
12375 err = got_error_from_errno2("fopen", path);
12379 err = histedit_parse_list(histedit_cmds, f, repo);
12381 if (f && fclose(f) == EOF && err == NULL)
12382 err = got_error_from_errno("fclose");
12386 static const struct got_error *
12387 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12388 const struct got_error *edit_err, struct got_object_id_queue *commits,
12389 const char *path, const char *branch_name, struct got_repository *repo)
12391 const struct got_error *err = NULL, *prev_err = edit_err;
12394 while (resp != 'c' && resp != 'r' && resp != 'a') {
12395 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12396 "or (a)bort: ", getprogname(), prev_err->msg);
12401 histedit_free_list(histedit_cmds);
12402 err = histedit_run_editor(histedit_cmds, path, commits,
12405 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12406 err->code != GOT_ERR_HISTEDIT_CMD)
12413 } else if (resp == 'r') {
12414 histedit_free_list(histedit_cmds);
12415 err = histedit_edit_script(histedit_cmds,
12416 commits, branch_name, 0, 0, 0, 0, repo);
12418 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12419 err->code != GOT_ERR_HISTEDIT_CMD)
12426 } else if (resp == 'a') {
12427 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12430 printf("invalid response '%c'\n", resp);
12436 static const struct got_error *
12437 histedit_complete(struct got_worktree *worktree,
12438 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12439 struct got_reference *branch, struct got_repository *repo)
12441 printf("Switching work tree to %s\n",
12442 got_ref_get_symref_target(branch));
12443 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12447 static const struct got_error *
12448 show_histedit_progress(struct got_commit_object *commit,
12449 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12451 const struct got_error *err;
12452 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12454 err = got_object_id_str(&old_id_str, hle->commit_id);
12459 err = got_object_id_str(&new_id_str, new_id);
12464 old_id_str[12] = '\0';
12466 new_id_str[12] = '\0';
12469 logmsg = strdup(hle->logmsg);
12470 if (logmsg == NULL) {
12471 err = got_error_from_errno("strdup");
12474 trim_logmsg(logmsg, 42);
12476 err = get_short_logmsg(&logmsg, 42, commit);
12481 switch (hle->cmd->code) {
12482 case GOT_HISTEDIT_PICK:
12483 case GOT_HISTEDIT_EDIT:
12484 case GOT_HISTEDIT_MESG:
12485 printf("%s -> %s: %s\n", old_id_str,
12486 new_id_str ? new_id_str : "no-op change", logmsg);
12488 case GOT_HISTEDIT_DROP:
12489 case GOT_HISTEDIT_FOLD:
12490 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12502 static const struct got_error *
12503 histedit_commit(struct got_pathlist_head *merged_paths,
12504 struct got_worktree *worktree, struct got_fileindex *fileindex,
12505 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12506 const char *committer, int allow_conflict, struct got_repository *repo)
12508 const struct got_error *err;
12509 struct got_commit_object *commit;
12510 struct got_object_id *new_commit_id;
12512 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12513 && hle->logmsg == NULL) {
12514 err = histedit_edit_logmsg(hle, repo);
12519 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12523 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12524 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12525 hle->logmsg, allow_conflict, repo);
12527 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12529 err = show_histedit_progress(commit, hle, NULL);
12531 err = show_histedit_progress(commit, hle, new_commit_id);
12532 free(new_commit_id);
12535 got_object_commit_close(commit);
12539 static const struct got_error *
12540 histedit_skip_commit(struct got_histedit_list_entry *hle,
12541 struct got_worktree *worktree, struct got_repository *repo)
12543 const struct got_error *error;
12544 struct got_commit_object *commit;
12546 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12551 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12555 error = show_histedit_progress(commit, hle, NULL);
12556 got_object_commit_close(commit);
12560 static const struct got_error *
12561 check_local_changes(void *arg, unsigned char status,
12562 unsigned char staged_status, const char *path,
12563 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12564 struct got_object_id *commit_id, int dirfd, const char *de_name)
12566 int *have_local_changes = arg;
12569 case GOT_STATUS_ADD:
12570 case GOT_STATUS_DELETE:
12571 case GOT_STATUS_MODIFY:
12572 case GOT_STATUS_CONFLICT:
12573 *have_local_changes = 1;
12574 return got_error(GOT_ERR_CANCELLED);
12579 switch (staged_status) {
12580 case GOT_STATUS_ADD:
12581 case GOT_STATUS_DELETE:
12582 case GOT_STATUS_MODIFY:
12583 *have_local_changes = 1;
12584 return got_error(GOT_ERR_CANCELLED);
12592 static const struct got_error *
12593 cmd_histedit(int argc, char *argv[])
12595 const struct got_error *error = NULL;
12596 struct got_worktree *worktree = NULL;
12597 struct got_fileindex *fileindex = NULL;
12598 struct got_repository *repo = NULL;
12599 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12600 struct got_reference *branch = NULL;
12601 struct got_reference *tmp_branch = NULL;
12602 struct got_object_id *resume_commit_id = NULL;
12603 struct got_object_id *base_commit_id = NULL;
12604 struct got_object_id *head_commit_id = NULL;
12605 struct got_commit_object *commit = NULL;
12606 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12607 struct got_update_progress_arg upa;
12608 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12609 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12610 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12611 const char *edit_script_path = NULL;
12612 struct got_object_id_queue commits;
12613 struct got_pathlist_head merged_paths;
12614 const struct got_object_id_queue *parent_ids;
12615 struct got_object_qid *pid;
12616 struct got_histedit_list histedit_cmds;
12617 struct got_histedit_list_entry *hle;
12618 int *pack_fds = NULL;
12620 STAILQ_INIT(&commits);
12621 TAILQ_INIT(&histedit_cmds);
12622 TAILQ_INIT(&merged_paths);
12623 memset(&upa, 0, sizeof(upa));
12626 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12627 "unveil", NULL) == -1)
12631 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12637 allow_conflict = 1;
12649 edit_script_path = optarg;
12658 edit_logmsg_only = 1;
12661 delete_backups = 1;
12672 if (abort_edit && allow_conflict)
12673 option_conflict('a', 'C');
12674 if (abort_edit && continue_edit)
12675 option_conflict('a', 'c');
12676 if (edit_script_path && allow_conflict)
12677 option_conflict('F', 'C');
12678 if (edit_script_path && edit_logmsg_only)
12679 option_conflict('F', 'm');
12680 if (abort_edit && edit_logmsg_only)
12681 option_conflict('a', 'm');
12682 if (edit_logmsg_only && allow_conflict)
12683 option_conflict('m', 'C');
12684 if (continue_edit && edit_logmsg_only)
12685 option_conflict('c', 'm');
12686 if (abort_edit && fold_only)
12687 option_conflict('a', 'f');
12688 if (fold_only && allow_conflict)
12689 option_conflict('f', 'C');
12690 if (continue_edit && fold_only)
12691 option_conflict('c', 'f');
12692 if (fold_only && edit_logmsg_only)
12693 option_conflict('f', 'm');
12694 if (edit_script_path && fold_only)
12695 option_conflict('F', 'f');
12696 if (abort_edit && edit_only)
12697 option_conflict('a', 'e');
12698 if (continue_edit && edit_only)
12699 option_conflict('c', 'e');
12700 if (edit_only && edit_logmsg_only)
12701 option_conflict('e', 'm');
12702 if (edit_script_path && edit_only)
12703 option_conflict('F', 'e');
12704 if (fold_only && edit_only)
12705 option_conflict('f', 'e');
12706 if (drop_only && abort_edit)
12707 option_conflict('d', 'a');
12708 if (drop_only && allow_conflict)
12709 option_conflict('d', 'C');
12710 if (drop_only && continue_edit)
12711 option_conflict('d', 'c');
12712 if (drop_only && edit_logmsg_only)
12713 option_conflict('d', 'm');
12714 if (drop_only && edit_only)
12715 option_conflict('d', 'e');
12716 if (drop_only && edit_script_path)
12717 option_conflict('d', 'F');
12718 if (drop_only && fold_only)
12719 option_conflict('d', 'f');
12720 if (list_backups) {
12722 option_conflict('l', 'a');
12723 if (allow_conflict)
12724 option_conflict('l', 'C');
12726 option_conflict('l', 'c');
12727 if (edit_script_path)
12728 option_conflict('l', 'F');
12729 if (edit_logmsg_only)
12730 option_conflict('l', 'm');
12732 option_conflict('l', 'd');
12734 option_conflict('l', 'f');
12736 option_conflict('l', 'e');
12737 if (delete_backups)
12738 option_conflict('l', 'X');
12739 if (argc != 0 && argc != 1)
12741 } else if (delete_backups) {
12743 option_conflict('X', 'a');
12744 if (allow_conflict)
12745 option_conflict('X', 'C');
12747 option_conflict('X', 'c');
12749 option_conflict('X', 'd');
12750 if (edit_script_path)
12751 option_conflict('X', 'F');
12752 if (edit_logmsg_only)
12753 option_conflict('X', 'm');
12755 option_conflict('X', 'f');
12757 option_conflict('X', 'e');
12759 option_conflict('X', 'l');
12760 if (argc != 0 && argc != 1)
12762 } else if (allow_conflict && !continue_edit)
12763 errx(1, "-C option requires -c");
12764 else if (argc != 0)
12768 * This command cannot apply unveil(2) in all cases because the
12769 * user may choose to run an editor to edit the histedit script
12770 * and to edit individual commit log messages.
12771 * unveil(2) traverses exec(2); if an editor is used we have to
12772 * apply unveil after edit script and log messages have been written.
12773 * XXX TODO: Make use of unveil(2) where possible.
12776 cwd = getcwd(NULL, 0);
12778 error = got_error_from_errno("getcwd");
12782 error = got_repo_pack_fds_open(&pack_fds);
12786 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12788 if (list_backups || delete_backups) {
12789 if (error->code != GOT_ERR_NOT_WORKTREE)
12792 if (error->code == GOT_ERR_NOT_WORKTREE)
12793 error = wrap_not_worktree_error(error,
12799 if (list_backups || delete_backups) {
12800 error = got_repo_open(&repo,
12801 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12805 error = apply_unveil(got_repo_get_path(repo), 0,
12806 worktree ? got_worktree_get_root_path(worktree) : NULL);
12809 error = process_backup_refs(
12810 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12811 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12812 goto done; /* nothing else to do */
12815 error = get_gitconfig_path(&gitconfig_path);
12818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12819 gitconfig_path, pack_fds);
12823 if (worktree != NULL && !list_backups && !delete_backups) {
12824 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12829 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12832 if (rebase_in_progress) {
12833 error = got_error(GOT_ERR_REBASING);
12837 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12841 if (merge_in_progress) {
12842 error = got_error(GOT_ERR_MERGE_BUSY);
12846 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12850 if (edit_in_progress && edit_logmsg_only) {
12851 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12852 "histedit operation is in progress in this "
12853 "work tree and must be continued or aborted "
12854 "before the -m option can be used");
12857 if (edit_in_progress && drop_only) {
12858 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12859 "histedit operation is in progress in this "
12860 "work tree and must be continued or aborted "
12861 "before the -d option can be used");
12864 if (edit_in_progress && fold_only) {
12865 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12866 "histedit operation is in progress in this "
12867 "work tree and must be continued or aborted "
12868 "before the -f option can be used");
12871 if (edit_in_progress && edit_only) {
12872 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12873 "histedit operation is in progress in this "
12874 "work tree and must be continued or aborted "
12875 "before the -e option can be used");
12879 if (edit_in_progress && abort_edit) {
12880 error = got_worktree_histedit_continue(&resume_commit_id,
12881 &tmp_branch, &branch, &base_commit_id, &fileindex,
12885 printf("Switching work tree to %s\n",
12886 got_ref_get_symref_target(branch));
12887 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12888 branch, base_commit_id, abort_progress, &upa);
12891 printf("Histedit of %s aborted\n",
12892 got_ref_get_symref_target(branch));
12893 print_merge_progress_stats(&upa);
12894 goto done; /* nothing else to do */
12895 } else if (abort_edit) {
12896 error = got_error(GOT_ERR_NOT_HISTEDIT);
12900 error = get_author(&committer, repo, worktree);
12904 if (continue_edit) {
12907 if (!edit_in_progress) {
12908 error = got_error(GOT_ERR_NOT_HISTEDIT);
12912 error = got_worktree_get_histedit_script_path(&path, worktree);
12916 error = histedit_load_list(&histedit_cmds, path, repo);
12921 error = got_worktree_histedit_continue(&resume_commit_id,
12922 &tmp_branch, &branch, &base_commit_id, &fileindex,
12927 error = got_ref_resolve(&head_commit_id, repo, branch);
12931 error = got_object_open_as_commit(&commit, repo,
12935 parent_ids = got_object_commit_get_parent_ids(commit);
12936 pid = STAILQ_FIRST(parent_ids);
12938 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12941 error = collect_commits(&commits, head_commit_id, &pid->id,
12942 base_commit_id, got_worktree_get_path_prefix(worktree),
12943 GOT_ERR_HISTEDIT_PATH, repo);
12944 got_object_commit_close(commit);
12949 if (edit_in_progress) {
12950 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12954 error = got_ref_open(&branch, repo,
12955 got_worktree_get_head_ref_name(worktree), 0);
12959 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12960 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12961 "will not edit commit history of a branch outside "
12962 "the \"refs/heads/\" reference namespace");
12966 error = got_ref_resolve(&head_commit_id, repo, branch);
12967 got_ref_close(branch);
12972 error = got_object_open_as_commit(&commit, repo,
12976 parent_ids = got_object_commit_get_parent_ids(commit);
12977 pid = STAILQ_FIRST(parent_ids);
12979 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12982 error = collect_commits(&commits, head_commit_id, &pid->id,
12983 got_worktree_get_base_commit_id(worktree),
12984 got_worktree_get_path_prefix(worktree),
12985 GOT_ERR_HISTEDIT_PATH, repo);
12986 got_object_commit_close(commit);
12991 if (STAILQ_EMPTY(&commits)) {
12992 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12996 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12997 &base_commit_id, &fileindex, worktree, repo);
13001 if (edit_script_path) {
13002 error = histedit_load_list(&histedit_cmds,
13003 edit_script_path, repo);
13005 got_worktree_histedit_abort(worktree, fileindex,
13006 repo, branch, base_commit_id,
13007 abort_progress, &upa);
13008 print_merge_progress_stats(&upa);
13012 const char *branch_name;
13013 branch_name = got_ref_get_symref_target(branch);
13014 if (strncmp(branch_name, "refs/heads/", 11) == 0)
13016 error = histedit_edit_script(&histedit_cmds, &commits,
13017 branch_name, edit_logmsg_only, fold_only,
13018 drop_only, edit_only, repo);
13020 got_worktree_histedit_abort(worktree, fileindex,
13021 repo, branch, base_commit_id,
13022 abort_progress, &upa);
13023 print_merge_progress_stats(&upa);
13029 error = histedit_save_list(&histedit_cmds, worktree,
13032 got_worktree_histedit_abort(worktree, fileindex,
13033 repo, branch, base_commit_id,
13034 abort_progress, &upa);
13035 print_merge_progress_stats(&upa);
13041 error = histedit_check_script(&histedit_cmds, &commits, repo);
13045 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
13046 if (resume_commit_id) {
13047 if (got_object_id_cmp(hle->commit_id,
13048 resume_commit_id) != 0)
13051 resume_commit_id = NULL;
13052 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
13053 hle->cmd->code == GOT_HISTEDIT_FOLD) {
13054 error = histedit_skip_commit(hle, worktree,
13059 struct got_pathlist_head paths;
13060 int have_changes = 0;
13062 TAILQ_INIT(&paths);
13063 error = got_pathlist_append(&paths, "", NULL);
13066 error = got_worktree_status(worktree, &paths,
13067 repo, 0, check_local_changes, &have_changes,
13068 check_cancelled, NULL);
13069 got_pathlist_free(&paths,
13070 GOT_PATHLIST_FREE_NONE);
13072 if (error->code != GOT_ERR_CANCELLED)
13074 if (sigint_received || sigpipe_received)
13077 if (have_changes) {
13078 error = histedit_commit(NULL, worktree,
13079 fileindex, tmp_branch, hle,
13080 committer, allow_conflict, repo);
13084 error = got_object_open_as_commit(
13085 &commit, repo, hle->commit_id);
13088 error = show_histedit_progress(commit,
13090 got_object_commit_close(commit);
13099 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
13100 error = histedit_skip_commit(hle, worktree, repo);
13105 error = got_object_open_as_commit(&commit, repo,
13109 parent_ids = got_object_commit_get_parent_ids(commit);
13110 pid = STAILQ_FIRST(parent_ids);
13112 error = got_worktree_histedit_merge_files(&merged_paths,
13113 worktree, fileindex, &pid->id, hle->commit_id, repo,
13114 update_progress, &upa, check_cancelled, NULL);
13117 got_object_commit_close(commit);
13120 print_merge_progress_stats(&upa);
13121 if (upa.conflicts > 0 || upa.missing > 0 ||
13122 upa.not_deleted > 0 || upa.unversioned > 0) {
13123 if (upa.conflicts > 0) {
13124 error = show_rebase_merge_conflict(
13125 hle->commit_id, repo);
13129 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13133 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13135 error = got_object_id_str(&id_str, hle->commit_id);
13138 printf("Stopping histedit for amending commit %s\n",
13141 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13142 error = got_worktree_histedit_postpone(worktree,
13145 } else if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13146 error = histedit_skip_commit(hle, worktree, repo);
13150 } else if (hle->cmd->code == GOT_HISTEDIT_MESG) {
13151 error = histedit_edit_logmsg(hle, repo);
13156 error = histedit_commit(&merged_paths, worktree, fileindex,
13157 tmp_branch, hle, committer, allow_conflict, repo);
13158 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13163 if (upa.conflicts > 0 || upa.missing > 0 ||
13164 upa.not_deleted > 0 || upa.unversioned > 0) {
13165 error = got_worktree_histedit_postpone(worktree, fileindex);
13168 if (upa.conflicts > 0 && upa.missing == 0 &&
13169 upa.not_deleted == 0 && upa.unversioned == 0) {
13170 error = got_error_msg(GOT_ERR_CONFLICTS,
13171 "conflicts must be resolved before histedit "
13173 } else if (upa.conflicts > 0) {
13174 error = got_error_msg(GOT_ERR_CONFLICTS,
13175 "conflicts must be resolved before histedit "
13176 "can continue; changes destined for some "
13177 "files were not yet merged and should be "
13178 "merged manually if required before the "
13179 "histedit operation is continued");
13181 error = got_error_msg(GOT_ERR_CONFLICTS,
13182 "changes destined for some files were not "
13183 "yet merged and should be merged manually "
13184 "if required before the histedit operation "
13188 error = histedit_complete(worktree, fileindex, tmp_branch,
13193 free(gitconfig_path);
13194 got_object_id_queue_free(&commits);
13195 histedit_free_list(&histedit_cmds);
13196 free(head_commit_id);
13197 free(base_commit_id);
13198 free(resume_commit_id);
13200 got_object_commit_close(commit);
13202 got_ref_close(branch);
13204 got_ref_close(tmp_branch);
13206 got_worktree_close(worktree);
13208 const struct got_error *close_err = got_repo_close(repo);
13213 const struct got_error *pack_err =
13214 got_repo_pack_fds_close(pack_fds);
13222 usage_integrate(void)
13224 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13228 static const struct got_error *
13229 cmd_integrate(int argc, char *argv[])
13231 const struct got_error *error = NULL;
13232 struct got_repository *repo = NULL;
13233 struct got_worktree *worktree = NULL;
13234 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13235 const char *branch_arg = NULL;
13236 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13237 struct got_fileindex *fileindex = NULL;
13238 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13240 struct got_update_progress_arg upa;
13241 int *pack_fds = NULL;
13244 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13245 "unveil", NULL) == -1)
13249 while ((ch = getopt(argc, argv, "")) != -1) {
13262 branch_arg = argv[0];
13264 cwd = getcwd(NULL, 0);
13266 error = got_error_from_errno("getcwd");
13270 error = got_repo_pack_fds_open(&pack_fds);
13274 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13276 if (error->code == GOT_ERR_NOT_WORKTREE)
13277 error = wrap_not_worktree_error(error, "integrate",
13282 error = check_rebase_or_histedit_in_progress(worktree);
13286 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13291 error = apply_unveil(got_repo_get_path(repo), 0,
13292 got_worktree_get_root_path(worktree));
13296 error = check_merge_in_progress(worktree, repo);
13300 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13301 error = got_error_from_errno("asprintf");
13305 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13306 &base_branch_ref, worktree, refname, repo);
13310 refname = strdup(got_ref_get_name(branch_ref));
13311 if (refname == NULL) {
13312 error = got_error_from_errno("strdup");
13313 got_worktree_integrate_abort(worktree, fileindex, repo,
13314 branch_ref, base_branch_ref);
13317 base_refname = strdup(got_ref_get_name(base_branch_ref));
13318 if (base_refname == NULL) {
13319 error = got_error_from_errno("strdup");
13320 got_worktree_integrate_abort(worktree, fileindex, repo,
13321 branch_ref, base_branch_ref);
13324 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13325 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13326 got_worktree_integrate_abort(worktree, fileindex, repo,
13327 branch_ref, base_branch_ref);
13331 error = got_ref_resolve(&commit_id, repo, branch_ref);
13335 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13339 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13340 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13341 "specified branch has already been integrated");
13342 got_worktree_integrate_abort(worktree, fileindex, repo,
13343 branch_ref, base_branch_ref);
13347 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13349 if (error->code == GOT_ERR_ANCESTRY)
13350 error = got_error(GOT_ERR_REBASE_REQUIRED);
13351 got_worktree_integrate_abort(worktree, fileindex, repo,
13352 branch_ref, base_branch_ref);
13356 memset(&upa, 0, sizeof(upa));
13357 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13358 branch_ref, base_branch_ref, update_progress, &upa,
13359 check_cancelled, NULL);
13363 printf("Integrated %s into %s\n", refname, base_refname);
13364 print_update_progress_stats(&upa);
13367 const struct got_error *close_err = got_repo_close(repo);
13372 got_worktree_close(worktree);
13374 const struct got_error *pack_err =
13375 got_repo_pack_fds_close(pack_fds);
13380 free(base_commit_id);
13383 free(base_refname);
13390 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13394 static const struct got_error *
13395 cmd_merge(int argc, char *argv[])
13397 const struct got_error *error = NULL;
13398 struct got_worktree *worktree = NULL;
13399 struct got_repository *repo = NULL;
13400 struct got_fileindex *fileindex = NULL;
13401 char *cwd = NULL, *id_str = NULL, *author = NULL;
13402 char *gitconfig_path = NULL;
13403 struct got_reference *branch = NULL, *wt_branch = NULL;
13404 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13405 struct got_object_id *wt_branch_tip = NULL;
13406 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13407 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13408 struct got_update_progress_arg upa;
13409 struct got_object_id *merge_commit_id = NULL;
13410 char *branch_name = NULL;
13411 int *pack_fds = NULL;
13413 memset(&upa, 0, sizeof(upa));
13416 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13417 "unveil", NULL) == -1)
13421 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13427 allow_conflict = 1;
13430 continue_merge = 1;
13433 prefer_fast_forward = 0;
13436 interrupt_merge = 1;
13448 if (continue_merge)
13449 option_conflict('a', 'c');
13450 if (!prefer_fast_forward)
13451 option_conflict('a', 'M');
13452 if (interrupt_merge)
13453 option_conflict('a', 'n');
13454 } else if (continue_merge) {
13455 if (!prefer_fast_forward)
13456 option_conflict('c', 'M');
13457 if (interrupt_merge)
13458 option_conflict('c', 'n');
13460 if (allow_conflict) {
13461 if (!continue_merge)
13462 errx(1, "-C option requires -c");
13464 if (abort_merge || continue_merge) {
13467 } else if (argc != 1)
13470 cwd = getcwd(NULL, 0);
13472 error = got_error_from_errno("getcwd");
13476 error = got_repo_pack_fds_open(&pack_fds);
13480 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13482 if (error->code == GOT_ERR_NOT_WORKTREE)
13483 error = wrap_not_worktree_error(error,
13488 error = get_gitconfig_path(&gitconfig_path);
13491 error = got_repo_open(&repo,
13492 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13493 gitconfig_path, pack_fds);
13497 if (worktree != NULL) {
13498 error = worktree_has_logmsg_ref("merge", worktree, repo);
13503 error = apply_unveil(got_repo_get_path(repo), 0,
13504 worktree ? got_worktree_get_root_path(worktree) : NULL);
13508 error = check_rebase_or_histedit_in_progress(worktree);
13512 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13517 if (merge_in_progress && !(abort_merge || continue_merge)) {
13518 error = got_error(GOT_ERR_MERGE_BUSY);
13522 if (!merge_in_progress && (abort_merge || continue_merge)) {
13523 error = got_error(GOT_ERR_NOT_MERGING);
13528 error = got_worktree_merge_continue(&branch_name,
13529 &branch_tip, &fileindex, worktree, repo);
13532 error = got_worktree_merge_abort(worktree, fileindex, repo,
13533 abort_progress, &upa);
13536 printf("Merge of %s aborted\n", branch_name);
13537 goto done; /* nothing else to do */
13540 if (strncmp(got_worktree_get_head_ref_name(worktree),
13541 "refs/heads/", 11) != 0) {
13542 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13543 "work tree's current branch %s is outside the "
13544 "\"refs/heads/\" reference namespace; "
13545 "update -b required",
13546 got_worktree_get_head_ref_name(worktree));
13550 error = get_author(&author, repo, worktree);
13554 error = got_ref_open(&wt_branch, repo,
13555 got_worktree_get_head_ref_name(worktree), 0);
13558 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13562 if (continue_merge) {
13563 struct got_object_id *base_commit_id;
13564 base_commit_id = got_worktree_get_base_commit_id(worktree);
13565 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13566 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13569 error = got_worktree_merge_continue(&branch_name,
13570 &branch_tip, &fileindex, worktree, repo);
13574 error = got_ref_open(&branch, repo, argv[0], 0);
13577 branch_name = strdup(got_ref_get_name(branch));
13578 if (branch_name == NULL) {
13579 error = got_error_from_errno("strdup");
13582 error = got_ref_resolve(&branch_tip, repo, branch);
13587 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13588 wt_branch_tip, branch_tip, 0, 0, repo,
13589 check_cancelled, NULL);
13590 if (error && error->code != GOT_ERR_ANCESTRY)
13593 if (!continue_merge) {
13594 error = check_path_prefix(wt_branch_tip, branch_tip,
13595 got_worktree_get_path_prefix(worktree),
13596 GOT_ERR_MERGE_PATH, repo);
13599 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13602 if (prefer_fast_forward && yca_id &&
13603 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13604 struct got_pathlist_head paths;
13605 if (interrupt_merge) {
13606 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13607 "there are no changes to merge since %s "
13608 "is already based on %s; merge cannot be "
13609 "interrupted for amending; -n",
13610 branch_name, got_ref_get_name(wt_branch));
13613 printf("Forwarding %s to %s\n",
13614 got_ref_get_name(wt_branch), branch_name);
13615 error = got_ref_change_ref(wt_branch, branch_tip);
13618 error = got_ref_write(wt_branch, repo);
13621 error = got_worktree_set_base_commit_id(worktree, repo,
13625 TAILQ_INIT(&paths);
13626 error = got_pathlist_append(&paths, "", NULL);
13629 error = got_worktree_checkout_files(worktree,
13630 &paths, repo, update_progress, &upa,
13631 check_cancelled, NULL);
13632 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13635 if (upa.did_something) {
13637 error = got_object_id_str(&id_str, branch_tip);
13640 printf("Updated to commit %s\n", id_str);
13643 printf("Already up-to-date\n");
13644 print_update_progress_stats(&upa);
13647 error = got_worktree_merge_write_refs(worktree, branch, repo);
13651 error = got_worktree_merge_branch(worktree, fileindex,
13652 yca_id, branch_tip, repo, update_progress, &upa,
13653 check_cancelled, NULL);
13656 print_merge_progress_stats(&upa);
13657 if (!upa.did_something) {
13658 error = got_worktree_merge_abort(worktree, fileindex,
13659 repo, abort_progress, &upa);
13662 printf("Already up-to-date\n");
13667 if (interrupt_merge) {
13668 error = got_worktree_merge_postpone(worktree, fileindex);
13671 printf("Merge of %s interrupted on request\n", branch_name);
13672 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13673 upa.not_deleted > 0 || upa.unversioned > 0) {
13674 error = got_worktree_merge_postpone(worktree, fileindex);
13677 if (upa.conflicts > 0 && upa.missing == 0 &&
13678 upa.not_deleted == 0 && upa.unversioned == 0) {
13679 error = got_error_msg(GOT_ERR_CONFLICTS,
13680 "conflicts must be resolved before merging "
13682 } else if (upa.conflicts > 0) {
13683 error = got_error_msg(GOT_ERR_CONFLICTS,
13684 "conflicts must be resolved before merging "
13685 "can continue; changes destined for some "
13686 "files were not yet merged and "
13687 "should be merged manually if required before the "
13688 "merge operation is continued");
13690 error = got_error_msg(GOT_ERR_CONFLICTS,
13691 "changes destined for some "
13692 "files were not yet merged and should be "
13693 "merged manually if required before the "
13694 "merge operation is continued");
13698 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13699 fileindex, author, NULL, 1, branch_tip, branch_name,
13700 allow_conflict, repo, continue_merge ? print_status : NULL,
13704 error = got_worktree_merge_complete(worktree, fileindex, repo);
13707 error = got_object_id_str(&id_str, merge_commit_id);
13710 printf("Merged %s into %s: %s\n", branch_name,
13711 got_worktree_get_head_ref_name(worktree),
13716 free(gitconfig_path);
13718 free(merge_commit_id);
13724 got_ref_close(branch);
13726 got_ref_close(wt_branch);
13728 got_worktree_close(worktree);
13730 const struct got_error *close_err = got_repo_close(repo);
13735 const struct got_error *pack_err =
13736 got_repo_pack_fds_close(pack_fds);
13746 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13747 "[path ...]\n", getprogname());
13751 static const struct got_error *
13752 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13753 const char *path, struct got_object_id *blob_id,
13754 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13755 int dirfd, const char *de_name)
13757 const struct got_error *err = NULL;
13758 char *id_str = NULL;
13760 if (staged_status != GOT_STATUS_ADD &&
13761 staged_status != GOT_STATUS_MODIFY &&
13762 staged_status != GOT_STATUS_DELETE)
13765 if (staged_status == GOT_STATUS_ADD ||
13766 staged_status == GOT_STATUS_MODIFY)
13767 err = got_object_id_str(&id_str, staged_blob_id);
13769 err = got_object_id_str(&id_str, blob_id);
13773 printf("%s %c %s\n", id_str, staged_status, path);
13778 static const struct got_error *
13779 cmd_stage(int argc, char *argv[])
13781 const struct got_error *error = NULL;
13782 struct got_repository *repo = NULL;
13783 struct got_worktree *worktree = NULL;
13785 struct got_pathlist_head paths;
13786 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13787 FILE *patch_script_file = NULL;
13788 const char *patch_script_path = NULL;
13789 struct choose_patch_arg cpa;
13790 int *pack_fds = NULL;
13792 TAILQ_INIT(&paths);
13795 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13796 "unveil", NULL) == -1)
13800 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13803 patch_script_path = optarg;
13812 allow_bad_symlinks = 1;
13823 if (list_stage && (pflag || patch_script_path))
13824 errx(1, "-l option cannot be used with other options");
13825 if (patch_script_path && !pflag)
13826 errx(1, "-F option can only be used together with -p option");
13828 cwd = getcwd(NULL, 0);
13830 error = got_error_from_errno("getcwd");
13834 error = got_repo_pack_fds_open(&pack_fds);
13838 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13840 if (error->code == GOT_ERR_NOT_WORKTREE)
13841 error = wrap_not_worktree_error(error, "stage", cwd);
13845 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13850 if (patch_script_path) {
13851 patch_script_file = fopen(patch_script_path, "re");
13852 if (patch_script_file == NULL) {
13853 error = got_error_from_errno2("fopen",
13854 patch_script_path);
13858 error = apply_unveil(got_repo_get_path(repo), 0,
13859 got_worktree_get_root_path(worktree));
13863 error = check_merge_in_progress(worktree, repo);
13867 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13872 error = got_worktree_status(worktree, &paths, repo, 0,
13873 print_stage, NULL, check_cancelled, NULL);
13875 cpa.patch_script_file = patch_script_file;
13876 cpa.action = "stage";
13877 error = got_worktree_stage(worktree, &paths,
13878 pflag ? NULL : print_status, NULL,
13879 pflag ? choose_patch : NULL, &cpa,
13880 allow_bad_symlinks, repo);
13883 if (patch_script_file && fclose(patch_script_file) == EOF &&
13885 error = got_error_from_errno2("fclose", patch_script_path);
13887 const struct got_error *close_err = got_repo_close(repo);
13892 got_worktree_close(worktree);
13894 const struct got_error *pack_err =
13895 got_repo_pack_fds_close(pack_fds);
13899 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13905 usage_unstage(void)
13907 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13908 "[path ...]\n", getprogname());
13913 static const struct got_error *
13914 cmd_unstage(int argc, char *argv[])
13916 const struct got_error *error = NULL;
13917 struct got_repository *repo = NULL;
13918 struct got_worktree *worktree = NULL;
13920 struct got_pathlist_head paths;
13922 struct got_update_progress_arg upa;
13923 FILE *patch_script_file = NULL;
13924 const char *patch_script_path = NULL;
13925 struct choose_patch_arg cpa;
13926 int *pack_fds = NULL;
13928 TAILQ_INIT(&paths);
13931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13932 "unveil", NULL) == -1)
13936 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13939 patch_script_path = optarg;
13953 if (patch_script_path && !pflag)
13954 errx(1, "-F option can only be used together with -p option");
13956 cwd = getcwd(NULL, 0);
13958 error = got_error_from_errno("getcwd");
13962 error = got_repo_pack_fds_open(&pack_fds);
13966 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13968 if (error->code == GOT_ERR_NOT_WORKTREE)
13969 error = wrap_not_worktree_error(error, "unstage", cwd);
13973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13978 if (patch_script_path) {
13979 patch_script_file = fopen(patch_script_path, "re");
13980 if (patch_script_file == NULL) {
13981 error = got_error_from_errno2("fopen",
13982 patch_script_path);
13987 error = apply_unveil(got_repo_get_path(repo), 0,
13988 got_worktree_get_root_path(worktree));
13992 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13996 cpa.patch_script_file = patch_script_file;
13997 cpa.action = "unstage";
13998 memset(&upa, 0, sizeof(upa));
13999 error = got_worktree_unstage(worktree, &paths, update_progress,
14000 &upa, pflag ? choose_patch : NULL, &cpa, repo);
14002 print_merge_progress_stats(&upa);
14004 if (patch_script_file && fclose(patch_script_file) == EOF &&
14006 error = got_error_from_errno2("fclose", patch_script_path);
14008 const struct got_error *close_err = got_repo_close(repo);
14013 got_worktree_close(worktree);
14015 const struct got_error *pack_err =
14016 got_repo_pack_fds_close(pack_fds);
14020 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14028 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14029 "arg ...\n", getprogname());
14033 static const struct got_error *
14034 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14036 const struct got_error *err;
14037 struct got_blob_object *blob;
14040 fd = got_opentempfd();
14042 return got_error_from_errno("got_opentempfd");
14044 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
14048 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
14050 if (fd != -1 && close(fd) == -1 && err == NULL)
14051 err = got_error_from_errno("close");
14053 got_object_blob_close(blob);
14057 static const struct got_error *
14058 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14060 const struct got_error *err;
14061 struct got_tree_object *tree;
14064 err = got_object_open_as_tree(&tree, repo, id);
14068 nentries = got_object_tree_get_nentries(tree);
14069 for (i = 0; i < nentries; i++) {
14070 struct got_tree_entry *te;
14072 if (sigint_received || sigpipe_received)
14074 te = got_object_tree_get_entry(tree, i);
14075 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
14078 fprintf(outfile, "%s %.7o %s\n", id_str,
14079 got_tree_entry_get_mode(te),
14080 got_tree_entry_get_name(te));
14084 got_object_tree_close(tree);
14088 static const struct got_error *
14089 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14091 const struct got_error *err;
14092 struct got_commit_object *commit;
14093 const struct got_object_id_queue *parent_ids;
14094 struct got_object_qid *pid;
14095 char *id_str = NULL;
14096 const char *logmsg = NULL;
14099 err = got_object_open_as_commit(&commit, repo, id);
14103 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
14107 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
14108 parent_ids = got_object_commit_get_parent_ids(commit);
14109 fprintf(outfile, "numparents %d\n",
14110 got_object_commit_get_nparents(commit));
14111 STAILQ_FOREACH(pid, parent_ids, entry) {
14113 err = got_object_id_str(&pid_str, &pid->id);
14116 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
14119 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14120 got_object_commit_get_author_gmtoff(commit));
14121 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14122 got_object_commit_get_author(commit),
14123 (long long)got_object_commit_get_author_time(commit),
14126 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14127 got_object_commit_get_committer_gmtoff(commit));
14128 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14129 got_object_commit_get_committer(commit),
14130 (long long)got_object_commit_get_committer_time(commit),
14133 logmsg = got_object_commit_get_logmsg_raw(commit);
14134 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14135 fprintf(outfile, "%s", logmsg);
14138 got_object_commit_close(commit);
14142 static const struct got_error *
14143 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14145 const struct got_error *err;
14146 struct got_tag_object *tag;
14147 char *id_str = NULL;
14148 const char *tagmsg = NULL;
14151 err = got_object_open_as_tag(&tag, repo, id);
14155 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14161 switch (got_object_tag_get_object_type(tag)) {
14162 case GOT_OBJ_TYPE_BLOB:
14163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14164 GOT_OBJ_LABEL_BLOB);
14166 case GOT_OBJ_TYPE_TREE:
14167 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14168 GOT_OBJ_LABEL_TREE);
14170 case GOT_OBJ_TYPE_COMMIT:
14171 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14172 GOT_OBJ_LABEL_COMMIT);
14174 case GOT_OBJ_TYPE_TAG:
14175 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14176 GOT_OBJ_LABEL_TAG);
14182 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14183 got_object_tag_get_name(tag));
14185 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14186 got_object_tag_get_tagger_gmtoff(tag));
14187 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14188 got_object_tag_get_tagger(tag),
14189 (long long)got_object_tag_get_tagger_time(tag),
14192 tagmsg = got_object_tag_get_message(tag);
14193 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14194 fprintf(outfile, "%s", tagmsg);
14197 got_object_tag_close(tag);
14201 static const struct got_error *
14202 cmd_cat(int argc, char *argv[])
14204 const struct got_error *error;
14205 struct got_repository *repo = NULL;
14206 struct got_worktree *worktree = NULL;
14207 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14208 char *keyword_idstr = NULL;
14209 const char *commit_id_str = NULL;
14210 struct got_object_id *id = NULL, *commit_id = NULL;
14211 struct got_commit_object *commit = NULL;
14212 int ch, obj_type, i, force_path = 0;
14213 struct got_reflist_head refs;
14214 int *pack_fds = NULL;
14219 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14224 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14227 commit_id_str = optarg;
14233 repo_path = realpath(optarg, NULL);
14234 if (repo_path == NULL)
14235 return got_error_from_errno2("realpath",
14237 got_path_strip_trailing_slashes(repo_path);
14248 cwd = getcwd(NULL, 0);
14250 error = got_error_from_errno("getcwd");
14254 error = got_repo_pack_fds_open(&pack_fds);
14258 if (repo_path == NULL) {
14259 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14260 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14263 repo_path = strdup(
14264 got_worktree_get_repo_path(worktree));
14265 if (repo_path == NULL) {
14266 error = got_error_from_errno("strdup");
14270 if (commit_id_str == NULL) {
14271 /* Release work tree lock. */
14272 got_worktree_close(worktree);
14278 if (repo_path == NULL) {
14279 repo_path = strdup(cwd);
14280 if (repo_path == NULL)
14281 return got_error_from_errno("strdup");
14284 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14289 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14293 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14297 if (commit_id_str != NULL) {
14298 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14302 if (keyword_idstr != NULL)
14303 commit_id_str = keyword_idstr;
14304 if (worktree != NULL) {
14305 got_worktree_close(worktree);
14309 commit_id_str = GOT_REF_HEAD;
14310 error = got_repo_match_object_id(&commit_id, NULL,
14311 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14315 error = got_object_open_as_commit(&commit, repo, commit_id);
14319 for (i = 0; i < argc; i++) {
14321 error = got_object_id_by_path(&id, repo, commit,
14326 error = got_repo_match_object_id(&id, &label, argv[i],
14327 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14330 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14331 error->code != GOT_ERR_NOT_REF)
14333 error = got_object_id_by_path(&id, repo,
14340 error = got_object_get_type(&obj_type, repo, id);
14344 switch (obj_type) {
14345 case GOT_OBJ_TYPE_BLOB:
14346 error = cat_blob(id, repo, stdout);
14348 case GOT_OBJ_TYPE_TREE:
14349 error = cat_tree(id, repo, stdout);
14351 case GOT_OBJ_TYPE_COMMIT:
14352 error = cat_commit(id, repo, stdout);
14354 case GOT_OBJ_TYPE_TAG:
14355 error = cat_tag(id, repo, stdout);
14358 error = got_error(GOT_ERR_OBJ_TYPE);
14372 free(keyword_idstr);
14374 got_object_commit_close(commit);
14376 got_worktree_close(worktree);
14378 const struct got_error *close_err = got_repo_close(repo);
14383 const struct got_error *pack_err =
14384 got_repo_pack_fds_close(pack_fds);
14389 got_ref_list_free(&refs);
14396 fprintf(stderr, "usage: %s info [path ...]\n",
14401 static const struct got_error *
14402 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14403 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14404 struct got_object_id *commit_id)
14406 const struct got_error *err = NULL;
14407 char *id_str = NULL;
14409 struct tm mytm, *tm;
14410 struct got_pathlist_head *paths = arg;
14411 struct got_pathlist_entry *pe;
14414 * Clear error indication from any of the path arguments which
14415 * would cause this file index entry to be displayed.
14417 TAILQ_FOREACH(pe, paths, entry) {
14418 if (got_path_cmp(path, pe->path, strlen(path),
14419 pe->path_len) == 0 ||
14420 got_path_is_child(path, pe->path, pe->path_len))
14421 pe->data = NULL; /* no error */
14424 printf(GOT_COMMIT_SEP_STR);
14426 printf("symlink: %s\n", path);
14427 else if (S_ISREG(mode)) {
14428 printf("file: %s\n", path);
14429 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14430 } else if (S_ISDIR(mode))
14431 printf("directory: %s\n", path);
14433 printf("something: %s\n", path);
14435 tm = localtime_r(&mtime, &mytm);
14438 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14439 return got_error(GOT_ERR_NO_SPACE);
14440 printf("timestamp: %s\n", datebuf);
14443 err = got_object_id_str(&id_str, blob_id);
14446 printf("based on blob: %s\n", id_str);
14450 if (staged_blob_id) {
14451 err = got_object_id_str(&id_str, staged_blob_id);
14454 printf("based on staged blob: %s\n", id_str);
14459 err = got_object_id_str(&id_str, commit_id);
14462 printf("based on commit: %s\n", id_str);
14469 static const struct got_error *
14470 cmd_info(int argc, char *argv[])
14472 const struct got_error *error = NULL;
14473 struct got_worktree *worktree = NULL;
14474 char *cwd = NULL, *id_str = NULL;
14475 struct got_pathlist_head paths;
14476 char *uuidstr = NULL;
14477 int ch, show_files = 0;
14479 TAILQ_INIT(&paths);
14482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14487 while ((ch = getopt(argc, argv, "")) != -1) {
14498 cwd = getcwd(NULL, 0);
14500 error = got_error_from_errno("getcwd");
14504 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14506 if (error->code == GOT_ERR_NOT_WORKTREE)
14507 error = wrap_not_worktree_error(error, "info", cwd);
14512 /* Remove "wpath cpath proc exec sendfd" promises. */
14513 if (pledge("stdio rpath flock unveil", NULL) == -1)
14516 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14521 error = get_worktree_paths_from_argv(&paths, argc, argv,
14528 error = got_object_id_str(&id_str,
14529 got_worktree_get_base_commit_id(worktree));
14533 error = got_worktree_get_uuid(&uuidstr, worktree);
14537 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14538 printf("work tree base commit: %s\n", id_str);
14539 printf("work tree path prefix: %s\n",
14540 got_worktree_get_path_prefix(worktree));
14541 printf("work tree branch reference: %s\n",
14542 got_worktree_get_head_ref_name(worktree));
14543 printf("work tree UUID: %s\n", uuidstr);
14544 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14547 struct got_pathlist_entry *pe;
14548 TAILQ_FOREACH(pe, &paths, entry) {
14549 if (pe->path_len == 0)
14552 * Assume this path will fail. This will be corrected
14553 * in print_path_info() in case the path does suceeed.
14555 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14557 error = got_worktree_path_info(worktree, &paths,
14558 print_path_info, &paths, check_cancelled, NULL);
14561 TAILQ_FOREACH(pe, &paths, entry) {
14562 if (pe->data != NULL) {
14563 const struct got_error *perr;
14566 error = got_error_fmt(perr->code, "%s",
14574 got_worktree_close(worktree);
14575 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);