2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
57 #include "got_opentemp.h"
58 #include "got_gotconfig.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static volatile sig_atomic_t sigint_received;
65 static volatile sig_atomic_t sigpipe_received;
68 catch_sigint(int signo)
74 catch_sigpipe(int signo)
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 const char *cmd_alias;
87 __dead static void usage(int, int);
88 __dead static void usage_init(void);
89 __dead static void usage_import(void);
90 __dead static void usage_clone(void);
91 __dead static void usage_fetch(void);
92 __dead static void usage_checkout(void);
93 __dead static void usage_update(void);
94 __dead static void usage_log(void);
95 __dead static void usage_diff(void);
96 __dead static void usage_blame(void);
97 __dead static void usage_tree(void);
98 __dead static void usage_status(void);
99 __dead static void usage_ref(void);
100 __dead static void usage_branch(void);
101 __dead static void usage_tag(void);
102 __dead static void usage_add(void);
103 __dead static void usage_remove(void);
104 __dead static void usage_revert(void);
105 __dead static void usage_commit(void);
106 __dead static void usage_send(void);
107 __dead static void usage_cherrypick(void);
108 __dead static void usage_backout(void);
109 __dead static void usage_rebase(void);
110 __dead static void usage_histedit(void);
111 __dead static void usage_integrate(void);
112 __dead static void usage_stage(void);
113 __dead static void usage_unstage(void);
114 __dead static void usage_cat(void);
115 __dead static void usage_info(void);
117 static const struct got_error* cmd_init(int, char *[]);
118 static const struct got_error* cmd_import(int, char *[]);
119 static const struct got_error* cmd_clone(int, char *[]);
120 static const struct got_error* cmd_fetch(int, char *[]);
121 static const struct got_error* cmd_checkout(int, char *[]);
122 static const struct got_error* cmd_update(int, char *[]);
123 static const struct got_error* cmd_log(int, char *[]);
124 static const struct got_error* cmd_diff(int, char *[]);
125 static const struct got_error* cmd_blame(int, char *[]);
126 static const struct got_error* cmd_tree(int, char *[]);
127 static const struct got_error* cmd_status(int, char *[]);
128 static const struct got_error* cmd_ref(int, char *[]);
129 static const struct got_error* cmd_branch(int, char *[]);
130 static const struct got_error* cmd_tag(int, char *[]);
131 static const struct got_error* cmd_add(int, char *[]);
132 static const struct got_error* cmd_remove(int, char *[]);
133 static const struct got_error* cmd_revert(int, char *[]);
134 static const struct got_error* cmd_commit(int, char *[]);
135 static const struct got_error* cmd_send(int, char *[]);
136 static const struct got_error* cmd_cherrypick(int, char *[]);
137 static const struct got_error* cmd_backout(int, char *[]);
138 static const struct got_error* cmd_rebase(int, char *[]);
139 static const struct got_error* cmd_histedit(int, char *[]);
140 static const struct got_error* cmd_integrate(int, char *[]);
141 static const struct got_error* cmd_stage(int, char *[]);
142 static const struct got_error* cmd_unstage(int, char *[]);
143 static const struct got_error* cmd_cat(int, char *[]);
144 static const struct got_error* cmd_info(int, char *[]);
146 static struct got_cmd got_commands[] = {
147 { "init", cmd_init, usage_init, "" },
148 { "import", cmd_import, usage_import, "im" },
149 { "clone", cmd_clone, usage_clone, "cl" },
150 { "fetch", cmd_fetch, usage_fetch, "fe" },
151 { "checkout", cmd_checkout, usage_checkout, "co" },
152 { "update", cmd_update, usage_update, "up" },
153 { "log", cmd_log, usage_log, "" },
154 { "diff", cmd_diff, usage_diff, "di" },
155 { "blame", cmd_blame, usage_blame, "bl" },
156 { "tree", cmd_tree, usage_tree, "tr" },
157 { "status", cmd_status, usage_status, "st" },
158 { "ref", cmd_ref, usage_ref, "" },
159 { "branch", cmd_branch, usage_branch, "br" },
160 { "tag", cmd_tag, usage_tag, "" },
161 { "add", cmd_add, usage_add, "" },
162 { "remove", cmd_remove, usage_remove, "rm" },
163 { "revert", cmd_revert, usage_revert, "rv" },
164 { "commit", cmd_commit, usage_commit, "ci" },
165 { "send", cmd_send, usage_send, "se" },
166 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
167 { "backout", cmd_backout, usage_backout, "bo" },
168 { "rebase", cmd_rebase, usage_rebase, "rb" },
169 { "histedit", cmd_histedit, usage_histedit, "he" },
170 { "integrate", cmd_integrate, usage_integrate,"ig" },
171 { "stage", cmd_stage, usage_stage, "sg" },
172 { "unstage", cmd_unstage, usage_unstage, "ug" },
173 { "cat", cmd_cat, usage_cat, "" },
174 { "info", cmd_info, usage_info, "" },
178 list_commands(FILE *fp)
182 fprintf(fp, "commands:");
183 for (i = 0; i < nitems(got_commands); i++) {
184 struct got_cmd *cmd = &got_commands[i];
185 fprintf(fp, " %s", cmd->cmd_name);
191 option_conflict(char a, char b)
193 errx(1, "-%c and -%c options are mutually exclusive", a, b);
197 main(int argc, char *argv[])
202 int hflag = 0, Vflag = 0;
203 static struct option longopts[] = {
204 { "version", no_argument, NULL, 'V' },
208 setlocale(LC_CTYPE, "");
210 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
230 got_version_print_str();
235 usage(hflag, hflag ? 0 : 1);
237 signal(SIGINT, catch_sigint);
238 signal(SIGPIPE, catch_sigpipe);
240 for (i = 0; i < nitems(got_commands); i++) {
241 const struct got_error *error;
243 cmd = &got_commands[i];
245 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
246 strcmp(cmd->cmd_alias, argv[0]) != 0)
250 got_commands[i].cmd_usage();
252 error = got_commands[i].cmd_main(argc, argv);
253 if (error && error->code != GOT_ERR_CANCELLED &&
254 error->code != GOT_ERR_PRIVSEP_EXIT &&
255 !(sigpipe_received &&
256 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
258 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
259 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
266 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
267 list_commands(stderr);
272 usage(int hflag, int status)
274 FILE *fp = (status == 0) ? stdout : stderr;
276 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
283 static const struct got_error *
284 get_editor(char **abspath)
286 const struct got_error *err = NULL;
291 editor = getenv("VISUAL");
293 editor = getenv("EDITOR");
296 err = got_path_find_prog(abspath, editor);
301 if (*abspath == NULL) {
302 *abspath = strdup("/bin/ed");
303 if (*abspath == NULL)
304 return got_error_from_errno("strdup");
310 static const struct got_error *
311 apply_unveil(const char *repo_path, int repo_read_only,
312 const char *worktree_path)
314 const struct got_error *err;
317 if (unveil("gmon.out", "rwc") != 0)
318 return got_error_from_errno2("unveil", "gmon.out");
320 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
321 return got_error_from_errno2("unveil", repo_path);
323 if (worktree_path && unveil(worktree_path, "rwc") != 0)
324 return got_error_from_errno2("unveil", worktree_path);
326 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
327 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
329 err = got_privsep_unveil_exec_helpers();
333 if (unveil(NULL, NULL) != 0)
334 return got_error_from_errno("unveil");
342 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
346 static const struct got_error *
347 cmd_init(int argc, char *argv[])
349 const struct got_error *error = NULL;
350 char *repo_path = NULL;
353 while ((ch = getopt(argc, argv, "")) != -1) {
365 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
371 repo_path = strdup(argv[0]);
372 if (repo_path == NULL)
373 return got_error_from_errno("strdup");
375 got_path_strip_trailing_slashes(repo_path);
377 error = got_path_mkdir(repo_path);
379 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
382 error = apply_unveil(repo_path, 0, NULL);
386 error = got_repo_init(repo_path);
395 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
396 "[-r repository-path] [-I pattern] path\n", getprogname());
401 spawn_editor(const char *editor, const char *file)
404 sig_t sighup, sigint, sigquit;
407 sighup = signal(SIGHUP, SIG_IGN);
408 sigint = signal(SIGINT, SIG_IGN);
409 sigquit = signal(SIGQUIT, SIG_IGN);
411 switch (pid = fork()) {
415 execl(editor, editor, file, (char *)NULL);
419 while (waitpid(pid, &st, 0) == -1)
424 (void)signal(SIGHUP, sighup);
425 (void)signal(SIGINT, sigint);
426 (void)signal(SIGQUIT, sigquit);
428 if (!WIFEXITED(st)) {
433 return WEXITSTATUS(st);
436 static const struct got_error *
437 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
438 const char *initial_content, size_t initial_content_len,
439 int require_modification)
441 const struct got_error *err = NULL;
447 size_t len, logmsg_len;
448 char *initial_content_stripped = NULL, *buf = NULL, *s;
452 if (stat(logmsg_path, &st) == -1)
453 return got_error_from_errno2("stat", logmsg_path);
455 if (spawn_editor(editor, logmsg_path) == -1)
456 return got_error_from_errno("failed spawning editor");
458 if (stat(logmsg_path, &st2) == -1)
459 return got_error_from_errno("stat");
461 if (require_modification &&
462 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
463 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
464 "no changes made to commit message, aborting");
467 * Set up a stripped version of the initial content without comments
468 * and blank lines. We need this in order to check if the message
469 * has in fact been edited.
471 initial_content_stripped = malloc(initial_content_len + 1);
472 if (initial_content_stripped == NULL)
473 return got_error_from_errno("malloc");
474 initial_content_stripped[0] = '\0';
476 buf = strdup(initial_content);
478 err = got_error_from_errno("strdup");
483 while ((line = strsep(&s, "\n")) != NULL) {
484 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
485 continue; /* remove comments and leading empty lines */
486 len = strlcat(initial_content_stripped, line,
487 initial_content_len + 1);
488 if (len >= initial_content_len + 1) {
489 err = got_error(GOT_ERR_NO_SPACE);
493 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
494 initial_content_stripped[len - 1] = '\0';
498 logmsg_len = st2.st_size;
499 *logmsg = malloc(logmsg_len + 1);
501 return got_error_from_errno("malloc");
504 fp = fopen(logmsg_path, "r");
506 err = got_error_from_errno("fopen");
511 while ((linelen = getline(&line, &linesize, fp)) != -1) {
512 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
513 continue; /* remove comments and leading empty lines */
514 len = strlcat(*logmsg, line, logmsg_len + 1);
515 if (len >= logmsg_len + 1) {
516 err = got_error(GOT_ERR_NO_SPACE);
522 err = got_ferror(fp, GOT_ERR_IO);
525 while (len > 0 && (*logmsg)[len - 1] == '\n') {
526 (*logmsg)[len - 1] = '\0';
531 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
532 "commit message cannot be empty, aborting");
535 if (require_modification &&
536 strcmp(*logmsg, initial_content_stripped) == 0)
537 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
538 "no changes made to commit message, aborting");
540 free(initial_content_stripped);
542 if (fp && fclose(fp) == EOF && err == NULL)
543 err = got_error_from_errno("fclose");
551 static const struct got_error *
552 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
553 const char *path_dir, const char *branch_name)
555 char *initial_content = NULL;
556 const struct got_error *err = NULL;
557 int initial_content_len;
560 initial_content_len = asprintf(&initial_content,
561 "\n# %s to be imported to branch %s\n", path_dir,
563 if (initial_content_len == -1)
564 return got_error_from_errno("asprintf");
566 err = got_opentemp_named_fd(logmsg_path, &fd,
567 GOT_TMPDIR_STR "/got-importmsg");
571 if (write(fd, initial_content, initial_content_len) == -1) {
572 err = got_error_from_errno2("write", *logmsg_path);
576 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
577 initial_content_len, 1);
579 if (fd != -1 && close(fd) == -1 && err == NULL)
580 err = got_error_from_errno2("close", *logmsg_path);
581 free(initial_content);
589 static const struct got_error *
590 import_progress(void *arg, const char *path)
592 printf("A %s\n", path);
596 static const struct got_error *
597 get_author(char **author, struct got_repository *repo,
598 struct got_worktree *worktree)
600 const struct got_error *err = NULL;
601 const char *got_author = NULL, *name, *email;
602 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
607 worktree_conf = got_worktree_get_gotconfig(worktree);
608 repo_conf = got_repo_get_gotconfig(repo);
611 * Priority of potential author information sources, from most
612 * significant to least significant:
613 * 1) work tree's .got/got.conf file
614 * 2) repository's got.conf file
615 * 3) repository's git config file
616 * 4) environment variables
617 * 5) global git config files (in user's home directory or /etc)
621 got_author = got_gotconfig_get_author(worktree_conf);
622 if (got_author == NULL)
623 got_author = got_gotconfig_get_author(repo_conf);
624 if (got_author == NULL) {
625 name = got_repo_get_gitconfig_author_name(repo);
626 email = got_repo_get_gitconfig_author_email(repo);
628 if (asprintf(author, "%s <%s>", name, email) == -1)
629 return got_error_from_errno("asprintf");
633 got_author = getenv("GOT_AUTHOR");
634 if (got_author == NULL) {
635 name = got_repo_get_global_gitconfig_author_name(repo);
636 email = got_repo_get_global_gitconfig_author_email(
639 if (asprintf(author, "%s <%s>", name, email)
641 return got_error_from_errno("asprintf");
644 /* TODO: Look up user in password database? */
645 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
649 *author = strdup(got_author);
651 return got_error_from_errno("strdup");
654 * Really dumb email address check; we're only doing this to
655 * avoid git's object parser breaking on commits we create.
657 while (*got_author && *got_author != '<')
659 if (*got_author != '<') {
660 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
663 while (*got_author && *got_author != '@')
665 if (*got_author != '@') {
666 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
669 while (*got_author && *got_author != '>')
671 if (*got_author != '>')
672 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
681 static const struct got_error *
682 get_gitconfig_path(char **gitconfig_path)
684 const char *homedir = getenv("HOME");
686 *gitconfig_path = NULL;
688 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
689 return got_error_from_errno("asprintf");
695 static const struct got_error *
696 cmd_import(int argc, char *argv[])
698 const struct got_error *error = NULL;
699 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
700 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
701 const char *branch_name = "main";
702 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
703 struct got_repository *repo = NULL;
704 struct got_reference *branch_ref = NULL, *head_ref = NULL;
705 struct got_object_id *new_commit_id = NULL;
707 struct got_pathlist_head ignores;
708 struct got_pathlist_entry *pe;
709 int preserve_logmsg = 0;
711 TAILQ_INIT(&ignores);
713 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
716 branch_name = optarg;
719 logmsg = strdup(optarg);
720 if (logmsg == NULL) {
721 error = got_error_from_errno("strdup");
726 repo_path = realpath(optarg, NULL);
727 if (repo_path == NULL) {
728 error = got_error_from_errno2("realpath",
734 if (optarg[0] == '\0')
736 error = got_pathlist_insert(&pe, &ignores, optarg,
751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
759 if (repo_path == NULL) {
760 repo_path = getcwd(NULL, 0);
761 if (repo_path == NULL)
762 return got_error_from_errno("getcwd");
764 got_path_strip_trailing_slashes(repo_path);
765 error = get_gitconfig_path(&gitconfig_path);
768 error = got_repo_open(&repo, repo_path, gitconfig_path);
772 error = get_author(&author, repo, NULL);
777 * Don't let the user create a branch name with a leading '-'.
778 * While technically a valid reference name, this case is usually
779 * an unintended typo.
781 if (branch_name[0] == '-')
782 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
784 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
785 error = got_error_from_errno("asprintf");
789 error = got_ref_open(&branch_ref, repo, refname, 0);
791 if (error->code != GOT_ERR_NOT_REF)
794 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
795 "import target branch already exists");
799 path_dir = realpath(argv[0], NULL);
800 if (path_dir == NULL) {
801 error = got_error_from_errno2("realpath", argv[0]);
804 got_path_strip_trailing_slashes(path_dir);
807 * unveil(2) traverses exec(2); if an editor is used we have
808 * to apply unveil after the log message has been written.
810 if (logmsg == NULL || strlen(logmsg) == 0) {
811 error = get_editor(&editor);
815 error = collect_import_msg(&logmsg, &logmsg_path, editor,
818 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
825 if (unveil(path_dir, "r") != 0) {
826 error = got_error_from_errno2("unveil", path_dir);
832 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
839 error = got_repo_import(&new_commit_id, path_dir, logmsg,
840 author, &ignores, repo, import_progress, NULL);
847 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
854 error = got_ref_write(branch_ref, repo);
861 error = got_object_id_str(&id_str, new_commit_id);
868 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
870 if (error->code != GOT_ERR_NOT_REF) {
876 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
884 error = got_ref_write(head_ref, repo);
892 printf("Created branch %s with commit %s\n",
893 got_ref_get_name(branch_ref), id_str);
895 if (preserve_logmsg) {
896 fprintf(stderr, "%s: log message preserved in %s\n",
897 getprogname(), logmsg_path);
898 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
899 error = got_error_from_errno2("unlink", logmsg_path);
908 free(gitconfig_path);
910 got_ref_close(branch_ref);
912 got_ref_close(head_ref);
919 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
920 "[-R reference] repository-url [directory]\n", getprogname());
924 struct got_fetch_progress_arg {
925 char last_scaled_size[FMT_SCALED_STRSIZE];
930 struct got_repository *repo;
935 struct got_pathlist_head *symrefs;
936 struct got_pathlist_head *wanted_branches;
937 struct got_pathlist_head *wanted_refs;
941 const char *remote_repo_path;
943 int fetch_all_branches;
944 int mirror_references;
948 /* XXX forward declaration */
949 static const struct got_error *
950 create_config_files(const char *proto, const char *host, const char *port,
951 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
952 int mirror_references, struct got_pathlist_head *symrefs,
953 struct got_pathlist_head *wanted_branches,
954 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
956 static const struct got_error *
957 fetch_progress(void *arg, const char *message, off_t packfile_size,
958 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
960 const struct got_error *err = NULL;
961 struct got_fetch_progress_arg *a = arg;
962 char scaled_size[FMT_SCALED_STRSIZE];
963 int p_indexed, p_resolved;
964 int print_size = 0, print_indexed = 0, print_resolved = 0;
967 * In order to allow a failed clone to be resumed with 'got fetch'
968 * we try to create configuration files as soon as possible.
969 * Once the server has sent information about its default branch
970 * we have all required information.
972 if (a->create_configs && !a->configs_created &&
973 !TAILQ_EMPTY(a->config_info.symrefs)) {
974 err = create_config_files(a->config_info.proto,
975 a->config_info.host, a->config_info.port,
976 a->config_info.remote_repo_path,
977 a->config_info.git_url,
978 a->config_info.fetch_all_branches,
979 a->config_info.mirror_references,
980 a->config_info.symrefs,
981 a->config_info.wanted_branches,
982 a->config_info.wanted_refs, a->repo);
985 a->configs_created = 1;
988 if (a->verbosity < 0)
991 if (message && message[0] != '\0') {
992 printf("\rserver: %s", message);
997 if (packfile_size > 0 || nobj_indexed > 0) {
998 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
999 (a->last_scaled_size[0] == '\0' ||
1000 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1002 if (strlcpy(a->last_scaled_size, scaled_size,
1003 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1004 return got_error(GOT_ERR_NO_SPACE);
1006 if (nobj_indexed > 0) {
1007 p_indexed = (nobj_indexed * 100) / nobj_total;
1008 if (p_indexed != a->last_p_indexed) {
1009 a->last_p_indexed = p_indexed;
1014 if (nobj_resolved > 0) {
1015 p_resolved = (nobj_resolved * 100) /
1016 (nobj_total - nobj_loose);
1017 if (p_resolved != a->last_p_resolved) {
1018 a->last_p_resolved = p_resolved;
1026 if (print_size || print_indexed || print_resolved)
1029 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1031 printf("; indexing %d%%", p_indexed);
1033 printf("; resolving deltas %d%%", p_resolved);
1034 if (print_size || print_indexed || print_resolved)
1040 static const struct got_error *
1041 create_symref(const char *refname, struct got_reference *target_ref,
1042 int verbosity, struct got_repository *repo)
1044 const struct got_error *err;
1045 struct got_reference *head_symref;
1047 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1051 err = got_ref_write(head_symref, repo);
1052 if (err == NULL && verbosity > 0) {
1053 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1054 got_ref_get_name(target_ref));
1056 got_ref_close(head_symref);
1060 static const struct got_error *
1061 list_remote_refs(struct got_pathlist_head *symrefs,
1062 struct got_pathlist_head *refs)
1064 const struct got_error *err;
1065 struct got_pathlist_entry *pe;
1067 TAILQ_FOREACH(pe, symrefs, entry) {
1068 const char *refname = pe->path;
1069 const char *targetref = pe->data;
1071 printf("%s: %s\n", refname, targetref);
1074 TAILQ_FOREACH(pe, refs, entry) {
1075 const char *refname = pe->path;
1076 struct got_object_id *id = pe->data;
1079 err = got_object_id_str(&id_str, id);
1082 printf("%s: %s\n", refname, id_str);
1089 static const struct got_error *
1090 create_ref(const char *refname, struct got_object_id *id,
1091 int verbosity, struct got_repository *repo)
1093 const struct got_error *err = NULL;
1094 struct got_reference *ref;
1097 err = got_object_id_str(&id_str, id);
1101 err = got_ref_alloc(&ref, refname, id);
1105 err = got_ref_write(ref, repo);
1108 if (err == NULL && verbosity >= 0)
1109 printf("Created reference %s: %s\n", refname, id_str);
1116 match_wanted_ref(const char *refname, const char *wanted_ref)
1118 if (strncmp(refname, "refs/", 5) != 0)
1123 * Prevent fetching of references that won't make any
1124 * sense outside of the remote repository's context.
1126 if (strncmp(refname, "got/", 4) == 0)
1128 if (strncmp(refname, "remotes/", 8) == 0)
1131 if (strncmp(wanted_ref, "refs/", 5) == 0)
1134 /* Allow prefix match. */
1135 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1138 /* Allow exact match. */
1139 return (strcmp(refname, wanted_ref) == 0);
1143 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1145 struct got_pathlist_entry *pe;
1147 TAILQ_FOREACH(pe, wanted_refs, entry) {
1148 if (match_wanted_ref(refname, pe->path))
1155 static const struct got_error *
1156 create_wanted_ref(const char *refname, struct got_object_id *id,
1157 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1159 const struct got_error *err;
1160 char *remote_refname;
1162 if (strncmp("refs/", refname, 5) == 0)
1165 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1166 remote_repo_name, refname) == -1)
1167 return got_error_from_errno("asprintf");
1169 err = create_ref(remote_refname, id, verbosity, repo);
1170 free(remote_refname);
1174 static const struct got_error *
1175 create_gotconfig(const char *proto, const char *host, const char *port,
1176 const char *remote_repo_path, const char *default_branch,
1177 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1178 struct got_pathlist_head *wanted_refs, int mirror_references,
1179 struct got_repository *repo)
1181 const struct got_error *err = NULL;
1182 char *gotconfig_path = NULL;
1183 char *gotconfig = NULL;
1184 FILE *gotconfig_file = NULL;
1185 const char *branchname = NULL;
1186 char *branches = NULL, *refs = NULL;
1189 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1190 struct got_pathlist_entry *pe;
1191 TAILQ_FOREACH(pe, wanted_branches, entry) {
1193 branchname = pe->path;
1194 if (strncmp(branchname, "refs/heads/", 11) == 0)
1196 if (asprintf(&s, "%s\"%s\" ",
1197 branches ? branches : "", branchname) == -1) {
1198 err = got_error_from_errno("asprintf");
1204 } else if (!fetch_all_branches && default_branch) {
1205 branchname = default_branch;
1206 if (strncmp(branchname, "refs/heads/", 11) == 0)
1208 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1209 err = got_error_from_errno("asprintf");
1213 if (!TAILQ_EMPTY(wanted_refs)) {
1214 struct got_pathlist_entry *pe;
1215 TAILQ_FOREACH(pe, wanted_refs, entry) {
1217 const char *refname = pe->path;
1218 if (strncmp(refname, "refs/", 5) == 0)
1220 if (asprintf(&s, "%s\"%s\" ",
1221 refs ? refs : "", refname) == -1) {
1222 err = got_error_from_errno("asprintf");
1230 /* Create got.conf(5). */
1231 gotconfig_path = got_repo_get_path_gotconfig(repo);
1232 if (gotconfig_path == NULL) {
1233 err = got_error_from_errno("got_repo_get_path_gotconfig");
1236 gotconfig_file = fopen(gotconfig_path, "a");
1237 if (gotconfig_file == NULL) {
1238 err = got_error_from_errno2("fopen", gotconfig_path);
1241 if (asprintf(&gotconfig,
1246 "\trepository \"%s\"\n"
1252 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1253 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1254 remote_repo_path, branches ? "\tbranch { " : "",
1255 branches ? branches : "", branches ? "}\n" : "",
1256 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1257 mirror_references ? "\tmirror-references yes\n" : "",
1258 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1259 err = got_error_from_errno("asprintf");
1262 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1263 if (n != strlen(gotconfig)) {
1264 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1269 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1270 err = got_error_from_errno2("fclose", gotconfig_path);
1271 free(gotconfig_path);
1276 static const struct got_error *
1277 create_gitconfig(const char *git_url, const char *default_branch,
1278 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1279 struct got_pathlist_head *wanted_refs, int mirror_references,
1280 struct got_repository *repo)
1282 const struct got_error *err = NULL;
1283 char *gitconfig_path = NULL;
1284 char *gitconfig = NULL;
1285 FILE *gitconfig_file = NULL;
1286 char *branches = NULL, *refs = NULL;
1287 const char *branchname;
1290 /* Create a config file Git can understand. */
1291 gitconfig_path = got_repo_get_path_gitconfig(repo);
1292 if (gitconfig_path == NULL) {
1293 err = got_error_from_errno("got_repo_get_path_gitconfig");
1296 gitconfig_file = fopen(gitconfig_path, "a");
1297 if (gitconfig_file == NULL) {
1298 err = got_error_from_errno2("fopen", gitconfig_path);
1301 if (fetch_all_branches) {
1302 if (mirror_references) {
1303 if (asprintf(&branches,
1304 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1305 err = got_error_from_errno("asprintf");
1308 } else if (asprintf(&branches,
1309 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1310 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1311 err = got_error_from_errno("asprintf");
1314 } else if (!TAILQ_EMPTY(wanted_branches)) {
1315 struct got_pathlist_entry *pe;
1316 TAILQ_FOREACH(pe, wanted_branches, entry) {
1318 branchname = pe->path;
1319 if (strncmp(branchname, "refs/heads/", 11) == 0)
1321 if (mirror_references) {
1323 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1324 branches ? branches : "",
1325 branchname, branchname) == -1) {
1326 err = got_error_from_errno("asprintf");
1329 } else if (asprintf(&s,
1330 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1331 branches ? branches : "",
1332 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1333 branchname) == -1) {
1334 err = got_error_from_errno("asprintf");
1342 * If the server specified a default branch, use just that one.
1343 * Otherwise fall back to fetching all branches on next fetch.
1345 if (default_branch) {
1346 branchname = default_branch;
1347 if (strncmp(branchname, "refs/heads/", 11) == 0)
1350 branchname = "*"; /* fall back to all branches */
1351 if (mirror_references) {
1352 if (asprintf(&branches,
1353 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1354 branchname, branchname) == -1) {
1355 err = got_error_from_errno("asprintf");
1358 } else if (asprintf(&branches,
1359 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1360 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1361 branchname) == -1) {
1362 err = got_error_from_errno("asprintf");
1366 if (!TAILQ_EMPTY(wanted_refs)) {
1367 struct got_pathlist_entry *pe;
1368 TAILQ_FOREACH(pe, wanted_refs, entry) {
1370 const char *refname = pe->path;
1371 if (strncmp(refname, "refs/", 5) == 0)
1373 if (mirror_references) {
1375 "%s\tfetch = refs/%s:refs/%s\n",
1376 refs ? refs : "", refname, refname) == -1) {
1377 err = got_error_from_errno("asprintf");
1380 } else if (asprintf(&s,
1381 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1383 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1385 err = got_error_from_errno("asprintf");
1393 if (asprintf(&gitconfig,
1398 "\tfetch = refs/tags/*:refs/tags/*\n",
1399 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1400 refs ? refs : "") == -1) {
1401 err = got_error_from_errno("asprintf");
1404 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1405 if (n != strlen(gitconfig)) {
1406 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1410 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1411 err = got_error_from_errno2("fclose", gitconfig_path);
1412 free(gitconfig_path);
1417 static const struct got_error *
1418 create_config_files(const char *proto, const char *host, const char *port,
1419 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1420 int mirror_references, struct got_pathlist_head *symrefs,
1421 struct got_pathlist_head *wanted_branches,
1422 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1424 const struct got_error *err = NULL;
1425 const char *default_branch = NULL;
1426 struct got_pathlist_entry *pe;
1429 * If we asked for a set of wanted branches then use the first
1432 if (!TAILQ_EMPTY(wanted_branches)) {
1433 pe = TAILQ_FIRST(wanted_branches);
1434 default_branch = pe->path;
1436 /* First HEAD ref listed by server is the default branch. */
1437 TAILQ_FOREACH(pe, symrefs, entry) {
1438 const char *refname = pe->path;
1439 const char *target = pe->data;
1441 if (strcmp(refname, GOT_REF_HEAD) != 0)
1444 default_branch = target;
1449 /* Create got.conf(5). */
1450 err = create_gotconfig(proto, host, port, remote_repo_path,
1451 default_branch, fetch_all_branches, wanted_branches,
1452 wanted_refs, mirror_references, repo);
1456 /* Create a config file Git can understand. */
1457 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1458 wanted_branches, wanted_refs, mirror_references, repo);
1461 static const struct got_error *
1462 cmd_clone(int argc, char *argv[])
1464 const struct got_error *error = NULL;
1465 const char *uri, *dirname;
1466 char *proto, *host, *port, *repo_name, *server_path;
1467 char *default_destdir = NULL, *id_str = NULL;
1468 const char *repo_path;
1469 struct got_repository *repo = NULL;
1470 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1471 struct got_pathlist_entry *pe;
1472 struct got_object_id *pack_hash = NULL;
1473 int ch, fetchfd = -1, fetchstatus;
1474 pid_t fetchpid = -1;
1475 struct got_fetch_progress_arg fpa;
1476 char *git_url = NULL;
1477 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1478 int list_refs_only = 0;
1481 TAILQ_INIT(&symrefs);
1482 TAILQ_INIT(&wanted_branches);
1483 TAILQ_INIT(&wanted_refs);
1485 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1488 fetch_all_branches = 1;
1491 error = got_pathlist_append(&wanted_branches,
1500 mirror_references = 1;
1505 else if (verbosity < 3)
1512 error = got_pathlist_append(&wanted_refs,
1525 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1526 option_conflict('a', 'b');
1527 if (list_refs_only) {
1528 if (!TAILQ_EMPTY(&wanted_branches))
1529 option_conflict('l', 'b');
1530 if (fetch_all_branches)
1531 option_conflict('l', 'a');
1532 if (mirror_references)
1533 option_conflict('l', 'm');
1534 if (!TAILQ_EMPTY(&wanted_refs))
1535 option_conflict('l', 'R');
1547 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1552 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1553 host, port ? ":" : "", port ? port : "",
1554 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1555 error = got_error_from_errno("asprintf");
1559 if (strcmp(proto, "git") == 0) {
1561 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 "sendfd dns inet unveil", NULL) == -1)
1565 } else if (strcmp(proto, "git+ssh") == 0 ||
1566 strcmp(proto, "ssh") == 0) {
1568 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 "sendfd unveil", NULL) == -1)
1572 } else if (strcmp(proto, "http") == 0 ||
1573 strcmp(proto, "git+http") == 0) {
1574 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1577 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1580 if (dirname == NULL) {
1581 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1582 error = got_error_from_errno("asprintf");
1585 repo_path = default_destdir;
1587 repo_path = dirname;
1589 if (!list_refs_only) {
1590 error = got_path_mkdir(repo_path);
1592 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1593 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1595 if (!got_path_dir_is_empty(repo_path)) {
1596 error = got_error_path(repo_path,
1597 GOT_ERR_DIR_NOT_EMPTY);
1602 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1603 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1604 error = got_error_from_errno2("unveil",
1605 GOT_FETCH_PATH_SSH);
1609 error = apply_unveil(repo_path, 0, NULL);
1614 printf("Connecting to %s%s%s\n", host,
1615 port ? ":" : "", port ? port : "");
1617 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 server_path, verbosity);
1622 if (!list_refs_only) {
1623 error = got_repo_init(repo_path);
1626 error = got_repo_open(&repo, repo_path, NULL);
1631 fpa.last_scaled_size[0] = '\0';
1632 fpa.last_p_indexed = -1;
1633 fpa.last_p_resolved = -1;
1634 fpa.verbosity = verbosity;
1635 fpa.create_configs = 1;
1636 fpa.configs_created = 0;
1638 fpa.config_info.symrefs = &symrefs;
1639 fpa.config_info.wanted_branches = &wanted_branches;
1640 fpa.config_info.wanted_refs = &wanted_refs;
1641 fpa.config_info.proto = proto;
1642 fpa.config_info.host = host;
1643 fpa.config_info.port = port;
1644 fpa.config_info.remote_repo_path = server_path;
1645 fpa.config_info.git_url = git_url;
1646 fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 fpa.config_info.mirror_references = mirror_references;
1648 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 fetch_all_branches, &wanted_branches, &wanted_refs,
1651 list_refs_only, verbosity, fetchfd, repo,
1652 fetch_progress, &fpa);
1656 if (list_refs_only) {
1657 error = list_remote_refs(&symrefs, &refs);
1661 error = got_object_id_str(&id_str, pack_hash);
1665 printf("\nFetched %s.pack\n", id_str);
1668 /* Set up references provided with the pack file. */
1669 TAILQ_FOREACH(pe, &refs, entry) {
1670 const char *refname = pe->path;
1671 struct got_object_id *id = pe->data;
1672 char *remote_refname;
1674 if (is_wanted_ref(&wanted_refs, refname) &&
1675 !mirror_references) {
1676 error = create_wanted_ref(refname, id,
1677 GOT_FETCH_DEFAULT_REMOTE_NAME,
1678 verbosity - 1, repo);
1684 error = create_ref(refname, id, verbosity - 1, repo);
1688 if (mirror_references)
1691 if (strncmp("refs/heads/", refname, 11) != 0)
1694 if (asprintf(&remote_refname,
1695 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1696 refname + 11) == -1) {
1697 error = got_error_from_errno("asprintf");
1700 error = create_ref(remote_refname, id, verbosity - 1, repo);
1701 free(remote_refname);
1706 /* Set the HEAD reference if the server provided one. */
1707 TAILQ_FOREACH(pe, &symrefs, entry) {
1708 struct got_reference *target_ref;
1709 const char *refname = pe->path;
1710 const char *target = pe->data;
1711 char *remote_refname = NULL, *remote_target = NULL;
1713 if (strcmp(refname, GOT_REF_HEAD) != 0)
1716 error = got_ref_open(&target_ref, repo, target, 0);
1718 if (error->code == GOT_ERR_NOT_REF) {
1725 error = create_symref(refname, target_ref, verbosity, repo);
1726 got_ref_close(target_ref);
1730 if (mirror_references)
1733 if (strncmp("refs/heads/", target, 11) != 0)
1736 if (asprintf(&remote_refname,
1737 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 error = got_error_from_errno("asprintf");
1742 if (asprintf(&remote_target,
1743 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 target + 11) == -1) {
1745 error = got_error_from_errno("asprintf");
1746 free(remote_refname);
1749 error = got_ref_open(&target_ref, repo, remote_target, 0);
1751 free(remote_refname);
1752 free(remote_target);
1753 if (error->code == GOT_ERR_NOT_REF) {
1759 error = create_symref(remote_refname, target_ref,
1760 verbosity - 1, repo);
1761 free(remote_refname);
1762 free(remote_target);
1763 got_ref_close(target_ref);
1769 * We failed to set the HEAD reference. If we asked for
1770 * a set of wanted branches use the first of one of those
1771 * which could be fetched instead.
1773 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1774 const char *target = pe->path;
1775 struct got_reference *target_ref;
1777 error = got_ref_open(&target_ref, repo, target, 0);
1779 if (error->code == GOT_ERR_NOT_REF) {
1786 error = create_symref(GOT_REF_HEAD, target_ref,
1788 got_ref_close(target_ref);
1796 printf("Created %s repository '%s'\n",
1797 mirror_references ? "mirrored" : "cloned", repo_path);
1800 if (kill(fetchpid, SIGTERM) == -1)
1801 error = got_error_from_errno("kill");
1802 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1803 error = got_error_from_errno("waitpid");
1805 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1806 error = got_error_from_errno("close");
1808 const struct got_error *close_err = got_repo_close(repo);
1812 TAILQ_FOREACH(pe, &refs, entry) {
1813 free((void *)pe->path);
1816 got_pathlist_free(&refs);
1817 TAILQ_FOREACH(pe, &symrefs, entry) {
1818 free((void *)pe->path);
1821 got_pathlist_free(&symrefs);
1822 got_pathlist_free(&wanted_branches);
1823 got_pathlist_free(&wanted_refs);
1830 free(default_destdir);
1835 static const struct got_error *
1836 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1837 int replace_tags, int verbosity, struct got_repository *repo)
1839 const struct got_error *err = NULL;
1840 char *new_id_str = NULL;
1841 struct got_object_id *old_id = NULL;
1843 err = got_object_id_str(&new_id_str, new_id);
1847 if (!replace_tags &&
1848 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1849 err = got_ref_resolve(&old_id, repo, ref);
1852 if (got_object_id_cmp(old_id, new_id) == 0)
1854 if (verbosity >= 0) {
1855 printf("Rejecting update of existing tag %s: %s\n",
1856 got_ref_get_name(ref), new_id_str);
1861 if (got_ref_is_symbolic(ref)) {
1862 if (verbosity >= 0) {
1863 printf("Replacing reference %s: %s\n",
1864 got_ref_get_name(ref),
1865 got_ref_get_symref_target(ref));
1867 err = got_ref_change_symref_to_ref(ref, new_id);
1870 err = got_ref_write(ref, repo);
1874 err = got_ref_resolve(&old_id, repo, ref);
1877 if (got_object_id_cmp(old_id, new_id) == 0)
1880 err = got_ref_change_ref(ref, new_id);
1883 err = got_ref_write(ref, repo);
1889 printf("Updated %s: %s\n", got_ref_get_name(ref),
1897 static const struct got_error *
1898 update_symref(const char *refname, struct got_reference *target_ref,
1899 int verbosity, struct got_repository *repo)
1901 const struct got_error *err = NULL, *unlock_err;
1902 struct got_reference *symref;
1903 int symref_is_locked = 0;
1905 err = got_ref_open(&symref, repo, refname, 1);
1907 if (err->code != GOT_ERR_NOT_REF)
1909 err = got_ref_alloc_symref(&symref, refname, target_ref);
1913 err = got_ref_write(symref, repo);
1918 printf("Created reference %s: %s\n",
1919 got_ref_get_name(symref),
1920 got_ref_get_symref_target(symref));
1922 symref_is_locked = 1;
1924 if (strcmp(got_ref_get_symref_target(symref),
1925 got_ref_get_name(target_ref)) == 0)
1928 err = got_ref_change_symref(symref,
1929 got_ref_get_name(target_ref));
1933 err = got_ref_write(symref, repo);
1938 printf("Updated %s: %s\n", got_ref_get_name(symref),
1939 got_ref_get_symref_target(symref));
1943 if (symref_is_locked) {
1944 unlock_err = got_ref_unlock(symref);
1945 if (unlock_err && err == NULL)
1948 got_ref_close(symref);
1955 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 "[remote-repository-name]\n",
1962 static const struct got_error *
1963 delete_missing_ref(struct got_reference *ref,
1964 int verbosity, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_object_id *id = NULL;
1968 char *id_str = NULL;
1970 if (got_ref_is_symbolic(ref)) {
1971 err = got_ref_delete(ref, repo);
1974 if (verbosity >= 0) {
1975 printf("Deleted %s: %s\n",
1976 got_ref_get_name(ref),
1977 got_ref_get_symref_target(ref));
1980 err = got_ref_resolve(&id, repo, ref);
1983 err = got_object_id_str(&id_str, id);
1987 err = got_ref_delete(ref, repo);
1990 if (verbosity >= 0) {
1991 printf("Deleted %s: %s\n",
1992 got_ref_get_name(ref), id_str);
2001 static const struct got_error *
2002 delete_missing_refs(struct got_pathlist_head *their_refs,
2003 struct got_pathlist_head *their_symrefs,
2004 const struct got_remote_repo *remote,
2005 int verbosity, struct got_repository *repo)
2007 const struct got_error *err = NULL, *unlock_err;
2008 struct got_reflist_head my_refs;
2009 struct got_reflist_entry *re;
2010 struct got_pathlist_entry *pe;
2011 char *remote_namespace = NULL;
2012 char *local_refname = NULL;
2014 TAILQ_INIT(&my_refs);
2016 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2018 return got_error_from_errno("asprintf");
2020 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2024 TAILQ_FOREACH(re, &my_refs, entry) {
2025 const char *refname = got_ref_get_name(re->ref);
2027 if (!remote->mirror_references) {
2028 if (strncmp(refname, remote_namespace,
2029 strlen(remote_namespace)) == 0) {
2030 if (strcmp(refname + strlen(remote_namespace),
2033 if (asprintf(&local_refname, "refs/heads/%s",
2034 refname + strlen(remote_namespace)) == -1) {
2035 err = got_error_from_errno("asprintf");
2038 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2042 TAILQ_FOREACH(pe, their_refs, entry) {
2043 if (strcmp(local_refname, pe->path) == 0)
2049 TAILQ_FOREACH(pe, their_symrefs, entry) {
2050 if (strcmp(local_refname, pe->path) == 0)
2056 err = delete_missing_ref(re->ref, verbosity, repo);
2060 if (local_refname) {
2061 struct got_reference *ref;
2062 err = got_ref_open(&ref, repo, local_refname, 1);
2064 if (err->code != GOT_ERR_NOT_REF)
2066 free(local_refname);
2067 local_refname = NULL;
2070 err = delete_missing_ref(ref, verbosity, repo);
2073 unlock_err = got_ref_unlock(ref);
2075 if (unlock_err && err == NULL) {
2080 free(local_refname);
2081 local_refname = NULL;
2085 free(remote_namespace);
2086 free(local_refname);
2090 static const struct got_error *
2091 update_wanted_ref(const char *refname, struct got_object_id *id,
2092 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2094 const struct got_error *err, *unlock_err;
2095 char *remote_refname;
2096 struct got_reference *ref;
2098 if (strncmp("refs/", refname, 5) == 0)
2101 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2102 remote_repo_name, refname) == -1)
2103 return got_error_from_errno("asprintf");
2105 err = got_ref_open(&ref, repo, remote_refname, 1);
2107 if (err->code != GOT_ERR_NOT_REF)
2109 err = create_ref(remote_refname, id, verbosity, repo);
2111 err = update_ref(ref, id, 0, verbosity, repo);
2112 unlock_err = got_ref_unlock(ref);
2113 if (unlock_err && err == NULL)
2118 free(remote_refname);
2122 static const struct got_error *
2123 delete_ref(struct got_repository *repo, struct got_reference *ref)
2125 const struct got_error *err = NULL;
2126 struct got_object_id *id = NULL;
2127 char *id_str = NULL;
2130 if (got_ref_is_symbolic(ref)) {
2131 target = got_ref_get_symref_target(ref);
2133 err = got_ref_resolve(&id, repo, ref);
2136 err = got_object_id_str(&id_str, id);
2142 err = got_ref_delete(ref, repo);
2146 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2153 static const struct got_error *
2154 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2156 const struct got_error *err = NULL;
2157 struct got_reflist_head refs;
2158 struct got_reflist_entry *re;
2163 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2164 err = got_error_from_errno("asprintf");
2167 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2171 TAILQ_FOREACH(re, &refs, entry)
2172 delete_ref(repo, re->ref);
2174 got_ref_list_free(&refs);
2178 static const struct got_error *
2179 cmd_fetch(int argc, char *argv[])
2181 const struct got_error *error = NULL, *unlock_err;
2182 char *cwd = NULL, *repo_path = NULL;
2183 const char *remote_name;
2184 char *proto = NULL, *host = NULL, *port = NULL;
2185 char *repo_name = NULL, *server_path = NULL;
2186 const struct got_remote_repo *remotes, *remote = NULL;
2188 char *id_str = NULL;
2189 struct got_repository *repo = NULL;
2190 struct got_worktree *worktree = NULL;
2191 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2192 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2193 struct got_pathlist_entry *pe;
2194 struct got_object_id *pack_hash = NULL;
2195 int i, ch, fetchfd = -1, fetchstatus;
2196 pid_t fetchpid = -1;
2197 struct got_fetch_progress_arg fpa;
2198 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2199 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2202 TAILQ_INIT(&symrefs);
2203 TAILQ_INIT(&wanted_branches);
2204 TAILQ_INIT(&wanted_refs);
2206 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2209 fetch_all_branches = 1;
2212 error = got_pathlist_append(&wanted_branches,
2224 repo_path = realpath(optarg, NULL);
2225 if (repo_path == NULL)
2226 return got_error_from_errno2("realpath",
2228 got_path_strip_trailing_slashes(repo_path);
2236 else if (verbosity < 3)
2243 error = got_pathlist_append(&wanted_refs,
2259 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2260 option_conflict('a', 'b');
2261 if (list_refs_only) {
2262 if (!TAILQ_EMPTY(&wanted_branches))
2263 option_conflict('l', 'b');
2264 if (fetch_all_branches)
2265 option_conflict('l', 'a');
2267 option_conflict('l', 'd');
2269 option_conflict('l', 'X');
2271 if (delete_remote) {
2272 if (fetch_all_branches)
2273 option_conflict('X', 'a');
2274 if (!TAILQ_EMPTY(&wanted_branches))
2275 option_conflict('X', 'b');
2277 option_conflict('X', 'd');
2279 option_conflict('X', 't');
2280 if (!TAILQ_EMPTY(&wanted_refs))
2281 option_conflict('X', 'R');
2286 errx(1, "-X option requires a remote name");
2287 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2288 } else if (argc == 1)
2289 remote_name = argv[0];
2293 cwd = getcwd(NULL, 0);
2295 error = got_error_from_errno("getcwd");
2299 if (repo_path == NULL) {
2300 error = got_worktree_open(&worktree, cwd);
2301 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2307 strdup(got_worktree_get_repo_path(worktree));
2308 if (repo_path == NULL)
2309 error = got_error_from_errno("strdup");
2313 repo_path = strdup(cwd);
2314 if (repo_path == NULL) {
2315 error = got_error_from_errno("strdup");
2321 error = got_repo_open(&repo, repo_path, NULL);
2325 if (delete_remote) {
2326 error = delete_refs_for_remote(repo, remote_name);
2327 goto done; /* nothing else to do */
2331 worktree_conf = got_worktree_get_gotconfig(worktree);
2332 if (worktree_conf) {
2333 got_gotconfig_get_remotes(&nremotes, &remotes,
2335 for (i = 0; i < nremotes; i++) {
2336 if (strcmp(remotes[i].name, remote_name) == 0) {
2337 remote = &remotes[i];
2343 if (remote == NULL) {
2344 repo_conf = got_repo_get_gotconfig(repo);
2346 got_gotconfig_get_remotes(&nremotes, &remotes,
2348 for (i = 0; i < nremotes; i++) {
2349 if (strcmp(remotes[i].name, remote_name) == 0) {
2350 remote = &remotes[i];
2356 if (remote == NULL) {
2357 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2358 for (i = 0; i < nremotes; i++) {
2359 if (strcmp(remotes[i].name, remote_name) == 0) {
2360 remote = &remotes[i];
2365 if (remote == NULL) {
2366 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2370 if (TAILQ_EMPTY(&wanted_branches)) {
2371 if (!fetch_all_branches)
2372 fetch_all_branches = remote->fetch_all_branches;
2373 for (i = 0; i < remote->nbranches; i++) {
2374 got_pathlist_append(&wanted_branches,
2375 remote->branches[i], NULL);
2378 if (TAILQ_EMPTY(&wanted_refs)) {
2379 for (i = 0; i < remote->nrefs; i++) {
2380 got_pathlist_append(&wanted_refs,
2381 remote->refs[i], NULL);
2385 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2386 &repo_name, remote->url);
2390 if (strcmp(proto, "git") == 0) {
2392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2393 "sendfd dns inet unveil", NULL) == -1)
2396 } else if (strcmp(proto, "git+ssh") == 0 ||
2397 strcmp(proto, "ssh") == 0) {
2399 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2400 "sendfd unveil", NULL) == -1)
2403 } else if (strcmp(proto, "http") == 0 ||
2404 strcmp(proto, "git+http") == 0) {
2405 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2408 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2412 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2413 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2414 error = got_error_from_errno2("unveil",
2415 GOT_FETCH_PATH_SSH);
2419 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2424 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2425 port ? ":" : "", port ? port : "");
2427 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2428 server_path, verbosity);
2432 fpa.last_scaled_size[0] = '\0';
2433 fpa.last_p_indexed = -1;
2434 fpa.last_p_resolved = -1;
2435 fpa.verbosity = verbosity;
2437 fpa.create_configs = 0;
2438 fpa.configs_created = 0;
2439 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2440 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2441 remote->mirror_references, fetch_all_branches, &wanted_branches,
2442 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2443 fetch_progress, &fpa);
2447 if (list_refs_only) {
2448 error = list_remote_refs(&symrefs, &refs);
2452 if (pack_hash == NULL) {
2454 printf("Already up-to-date\n");
2455 } else if (verbosity >= 0) {
2456 error = got_object_id_str(&id_str, pack_hash);
2459 printf("\nFetched %s.pack\n", id_str);
2464 /* Update references provided with the pack file. */
2465 TAILQ_FOREACH(pe, &refs, entry) {
2466 const char *refname = pe->path;
2467 struct got_object_id *id = pe->data;
2468 struct got_reference *ref;
2469 char *remote_refname;
2471 if (is_wanted_ref(&wanted_refs, refname) &&
2472 !remote->mirror_references) {
2473 error = update_wanted_ref(refname, id,
2474 remote->name, verbosity, repo);
2480 if (remote->mirror_references ||
2481 strncmp("refs/tags/", refname, 10) == 0) {
2482 error = got_ref_open(&ref, repo, refname, 1);
2484 if (error->code != GOT_ERR_NOT_REF)
2486 error = create_ref(refname, id, verbosity,
2491 error = update_ref(ref, id, replace_tags,
2493 unlock_err = got_ref_unlock(ref);
2494 if (unlock_err && error == NULL)
2500 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2501 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2502 remote_name, refname + 11) == -1) {
2503 error = got_error_from_errno("asprintf");
2507 error = got_ref_open(&ref, repo, remote_refname, 1);
2509 if (error->code != GOT_ERR_NOT_REF)
2511 error = create_ref(remote_refname, id,
2516 error = update_ref(ref, id, replace_tags,
2518 unlock_err = got_ref_unlock(ref);
2519 if (unlock_err && error == NULL)
2526 /* Also create a local branch if none exists yet. */
2527 error = got_ref_open(&ref, repo, refname, 1);
2529 if (error->code != GOT_ERR_NOT_REF)
2531 error = create_ref(refname, id, verbosity,
2536 unlock_err = got_ref_unlock(ref);
2537 if (unlock_err && error == NULL)
2544 error = delete_missing_refs(&refs, &symrefs, remote,
2550 if (!remote->mirror_references) {
2551 /* Update remote HEAD reference if the server provided one. */
2552 TAILQ_FOREACH(pe, &symrefs, entry) {
2553 struct got_reference *target_ref;
2554 const char *refname = pe->path;
2555 const char *target = pe->data;
2556 char *remote_refname = NULL, *remote_target = NULL;
2558 if (strcmp(refname, GOT_REF_HEAD) != 0)
2561 if (strncmp("refs/heads/", target, 11) != 0)
2564 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2565 remote->name, refname) == -1) {
2566 error = got_error_from_errno("asprintf");
2569 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2570 remote->name, target + 11) == -1) {
2571 error = got_error_from_errno("asprintf");
2572 free(remote_refname);
2576 error = got_ref_open(&target_ref, repo, remote_target,
2579 free(remote_refname);
2580 free(remote_target);
2581 if (error->code == GOT_ERR_NOT_REF) {
2587 error = update_symref(remote_refname, target_ref,
2589 free(remote_refname);
2590 free(remote_target);
2591 got_ref_close(target_ref);
2598 if (kill(fetchpid, SIGTERM) == -1)
2599 error = got_error_from_errno("kill");
2600 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2601 error = got_error_from_errno("waitpid");
2603 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2604 error = got_error_from_errno("close");
2606 const struct got_error *close_err = got_repo_close(repo);
2611 got_worktree_close(worktree);
2612 TAILQ_FOREACH(pe, &refs, entry) {
2613 free((void *)pe->path);
2616 got_pathlist_free(&refs);
2617 TAILQ_FOREACH(pe, &symrefs, entry) {
2618 free((void *)pe->path);
2621 got_pathlist_free(&symrefs);
2622 got_pathlist_free(&wanted_branches);
2623 got_pathlist_free(&wanted_refs);
2638 usage_checkout(void)
2640 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2641 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2646 show_worktree_base_ref_warning(void)
2648 fprintf(stderr, "%s: warning: could not create a reference "
2649 "to the work tree's base commit; the commit could be "
2650 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2651 "repository writable and running 'got update' will prevent this\n",
2655 struct got_checkout_progress_arg {
2656 const char *worktree_path;
2657 int had_base_commit_ref_error;
2660 static const struct got_error *
2661 checkout_progress(void *arg, unsigned char status, const char *path)
2663 struct got_checkout_progress_arg *a = arg;
2665 /* Base commit bump happens silently. */
2666 if (status == GOT_STATUS_BUMP_BASE)
2669 if (status == GOT_STATUS_BASE_REF_ERR) {
2670 a->had_base_commit_ref_error = 1;
2674 while (path[0] == '/')
2677 printf("%c %s/%s\n", status, a->worktree_path, path);
2681 static const struct got_error *
2682 check_cancelled(void *arg)
2684 if (sigint_received || sigpipe_received)
2685 return got_error(GOT_ERR_CANCELLED);
2689 static const struct got_error *
2690 check_linear_ancestry(struct got_object_id *commit_id,
2691 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2692 struct got_repository *repo)
2694 const struct got_error *err = NULL;
2695 struct got_object_id *yca_id;
2697 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2698 commit_id, base_commit_id, repo, check_cancelled, NULL);
2703 return got_error(GOT_ERR_ANCESTRY);
2706 * Require a straight line of history between the target commit
2707 * and the work tree's base commit.
2709 * Non-linear situations such as this require a rebase:
2711 * (commit) D F (base_commit)
2719 * 'got update' only handles linear cases:
2720 * Update forwards in time: A (base/yca) - B - C - D (commit)
2721 * Update backwards in time: D (base) - C - B - A (commit/yca)
2723 if (allow_forwards_in_time_only) {
2724 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2725 return got_error(GOT_ERR_ANCESTRY);
2726 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2727 got_object_id_cmp(base_commit_id, yca_id) != 0)
2728 return got_error(GOT_ERR_ANCESTRY);
2734 static const struct got_error *
2735 check_same_branch(struct got_object_id *commit_id,
2736 struct got_reference *head_ref, struct got_object_id *yca_id,
2737 struct got_repository *repo)
2739 const struct got_error *err = NULL;
2740 struct got_commit_graph *graph = NULL;
2741 struct got_object_id *head_commit_id = NULL;
2742 int is_same_branch = 0;
2744 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2748 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2752 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2757 err = got_commit_graph_open(&graph, "/", 1);
2761 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2762 check_cancelled, NULL);
2767 struct got_object_id *id;
2768 err = got_commit_graph_iter_next(&id, graph, repo,
2769 check_cancelled, NULL);
2771 if (err->code == GOT_ERR_ITER_COMPLETED)
2777 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2779 if (got_object_id_cmp(id, commit_id) == 0) {
2787 got_commit_graph_close(graph);
2788 free(head_commit_id);
2789 if (!err && !is_same_branch)
2790 err = got_error(GOT_ERR_ANCESTRY);
2794 static const struct got_error *
2795 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2797 static char msg[512];
2798 const char *branch_name;
2800 if (got_ref_is_symbolic(ref))
2801 branch_name = got_ref_get_symref_target(ref);
2803 branch_name = got_ref_get_name(ref);
2805 if (strncmp("refs/heads/", branch_name, 11) == 0)
2808 snprintf(msg, sizeof(msg),
2809 "target commit is not contained in branch '%s'; "
2810 "the branch to use must be specified with -b; "
2811 "if necessary a new branch can be created for "
2812 "this commit with 'got branch -c %s BRANCH_NAME'",
2813 branch_name, commit_id_str);
2815 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2818 static const struct got_error *
2819 cmd_checkout(int argc, char *argv[])
2821 const struct got_error *error = NULL;
2822 struct got_repository *repo = NULL;
2823 struct got_reference *head_ref = NULL;
2824 struct got_worktree *worktree = NULL;
2825 char *repo_path = NULL;
2826 char *worktree_path = NULL;
2827 const char *path_prefix = "";
2828 const char *branch_name = GOT_REF_HEAD;
2829 char *commit_id_str = NULL;
2831 int ch, same_path_prefix, allow_nonempty = 0;
2832 struct got_pathlist_head paths;
2833 struct got_checkout_progress_arg cpa;
2837 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2840 branch_name = optarg;
2843 commit_id_str = strdup(optarg);
2844 if (commit_id_str == NULL)
2845 return got_error_from_errno("strdup");
2851 path_prefix = optarg;
2863 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2864 "unveil", NULL) == -1)
2868 char *base, *dotgit;
2870 repo_path = realpath(argv[0], NULL);
2871 if (repo_path == NULL)
2872 return got_error_from_errno2("realpath", argv[0]);
2873 cwd = getcwd(NULL, 0);
2875 error = got_error_from_errno("getcwd");
2882 error = got_path_basename(&base, path);
2885 dotgit = strstr(base, ".git");
2888 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2889 error = got_error_from_errno("asprintf");
2894 } else if (argc == 2) {
2895 repo_path = realpath(argv[0], NULL);
2896 if (repo_path == NULL) {
2897 error = got_error_from_errno2("realpath", argv[0]);
2900 worktree_path = realpath(argv[1], NULL);
2901 if (worktree_path == NULL) {
2902 if (errno != ENOENT) {
2903 error = got_error_from_errno2("realpath",
2907 worktree_path = strdup(argv[1]);
2908 if (worktree_path == NULL) {
2909 error = got_error_from_errno("strdup");
2916 got_path_strip_trailing_slashes(repo_path);
2917 got_path_strip_trailing_slashes(worktree_path);
2919 error = got_repo_open(&repo, repo_path, NULL);
2923 /* Pre-create work tree path for unveil(2) */
2924 error = got_path_mkdir(worktree_path);
2926 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2927 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2929 if (!allow_nonempty &&
2930 !got_path_dir_is_empty(worktree_path)) {
2931 error = got_error_path(worktree_path,
2932 GOT_ERR_DIR_NOT_EMPTY);
2937 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2941 error = got_ref_open(&head_ref, repo, branch_name, 0);
2945 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2946 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2949 error = got_worktree_open(&worktree, worktree_path);
2953 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2957 if (!same_path_prefix) {
2958 error = got_error(GOT_ERR_PATH_PREFIX);
2962 if (commit_id_str) {
2963 struct got_object_id *commit_id;
2964 struct got_reflist_head refs;
2966 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2970 error = got_repo_match_object_id(&commit_id, NULL,
2971 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2972 got_ref_list_free(&refs);
2975 error = check_linear_ancestry(commit_id,
2976 got_worktree_get_base_commit_id(worktree), 0, repo);
2977 if (error != NULL) {
2979 if (error->code == GOT_ERR_ANCESTRY) {
2980 error = checkout_ancestry_error(
2981 head_ref, commit_id_str);
2985 error = check_same_branch(commit_id, head_ref, NULL, repo);
2987 if (error->code == GOT_ERR_ANCESTRY) {
2988 error = checkout_ancestry_error(
2989 head_ref, commit_id_str);
2993 error = got_worktree_set_base_commit_id(worktree, repo,
3000 error = got_pathlist_append(&paths, "", NULL);
3003 cpa.worktree_path = worktree_path;
3004 cpa.had_base_commit_ref_error = 0;
3005 error = got_worktree_checkout_files(worktree, &paths, repo,
3006 checkout_progress, &cpa, check_cancelled, NULL);
3010 printf("Now shut up and hack\n");
3011 if (cpa.had_base_commit_ref_error)
3012 show_worktree_base_ref_warning();
3014 got_pathlist_free(&paths);
3015 free(commit_id_str);
3017 free(worktree_path);
3022 struct got_update_progress_arg {
3030 print_update_progress_stats(struct got_update_progress_arg *upa)
3032 if (!upa->did_something)
3035 if (upa->conflicts > 0)
3036 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3037 if (upa->obstructed > 0)
3038 printf("File paths obstructed by a non-regular file: %d\n",
3040 if (upa->not_updated > 0)
3041 printf("Files not updated because of existing merge "
3042 "conflicts: %d\n", upa->not_updated);
3048 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3053 static const struct got_error *
3054 update_progress(void *arg, unsigned char status, const char *path)
3056 struct got_update_progress_arg *upa = arg;
3058 if (status == GOT_STATUS_EXISTS ||
3059 status == GOT_STATUS_BASE_REF_ERR)
3062 upa->did_something = 1;
3064 /* Base commit bump happens silently. */
3065 if (status == GOT_STATUS_BUMP_BASE)
3068 if (status == GOT_STATUS_CONFLICT)
3070 if (status == GOT_STATUS_OBSTRUCTED)
3072 if (status == GOT_STATUS_CANNOT_UPDATE)
3075 while (path[0] == '/')
3077 printf("%c %s\n", status, path);
3081 static const struct got_error *
3082 switch_head_ref(struct got_reference *head_ref,
3083 struct got_object_id *commit_id, struct got_worktree *worktree,
3084 struct got_repository *repo)
3086 const struct got_error *err = NULL;
3088 int ref_has_moved = 0;
3090 /* Trivial case: switching between two different references. */
3091 if (strcmp(got_ref_get_name(head_ref),
3092 got_worktree_get_head_ref_name(worktree)) != 0) {
3093 printf("Switching work tree from %s to %s\n",
3094 got_worktree_get_head_ref_name(worktree),
3095 got_ref_get_name(head_ref));
3096 return got_worktree_set_head_ref(worktree, head_ref);
3099 err = check_linear_ancestry(commit_id,
3100 got_worktree_get_base_commit_id(worktree), 0, repo);
3102 if (err->code != GOT_ERR_ANCESTRY)
3109 /* Switching to a rebased branch with the same reference name. */
3110 err = got_object_id_str(&base_id_str,
3111 got_worktree_get_base_commit_id(worktree));
3114 printf("Reference %s now points at a different branch\n",
3115 got_worktree_get_head_ref_name(worktree));
3116 printf("Switching work tree from %s to %s\n", base_id_str,
3117 got_worktree_get_head_ref_name(worktree));
3121 static const struct got_error *
3122 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3124 const struct got_error *err;
3127 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3131 return got_error(GOT_ERR_REBASING);
3133 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3137 return got_error(GOT_ERR_HISTEDIT_BUSY);
3142 static const struct got_error *
3143 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3144 char *argv[], struct got_worktree *worktree)
3146 const struct got_error *err = NULL;
3153 return got_error_from_errno("strdup");
3154 return got_pathlist_append(paths, path, NULL);
3157 for (i = 0; i < argc; i++) {
3158 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3161 err = got_pathlist_append(paths, path, NULL);
3171 static const struct got_error *
3172 wrap_not_worktree_error(const struct got_error *orig_err,
3173 const char *cmdname, const char *path)
3175 const struct got_error *err;
3176 struct got_repository *repo;
3177 static char msg[512];
3179 err = got_repo_open(&repo, path, NULL);
3183 snprintf(msg, sizeof(msg),
3184 "'got %s' needs a work tree in addition to a git repository\n"
3185 "Work trees can be checked out from this Git repository with "
3187 "The got(1) manual page contains more information.", cmdname);
3188 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3189 got_repo_close(repo);
3193 static const struct got_error *
3194 cmd_update(int argc, char *argv[])
3196 const struct got_error *error = NULL;
3197 struct got_repository *repo = NULL;
3198 struct got_worktree *worktree = NULL;
3199 char *worktree_path = NULL;
3200 struct got_object_id *commit_id = NULL;
3201 char *commit_id_str = NULL;
3202 const char *branch_name = NULL;
3203 struct got_reference *head_ref = NULL;
3204 struct got_pathlist_head paths;
3205 struct got_pathlist_entry *pe;
3207 struct got_update_progress_arg upa;
3211 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3214 branch_name = optarg;
3217 commit_id_str = strdup(optarg);
3218 if (commit_id_str == NULL)
3219 return got_error_from_errno("strdup");
3231 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3232 "unveil", NULL) == -1)
3235 worktree_path = getcwd(NULL, 0);
3236 if (worktree_path == NULL) {
3237 error = got_error_from_errno("getcwd");
3240 error = got_worktree_open(&worktree, worktree_path);
3242 if (error->code == GOT_ERR_NOT_WORKTREE)
3243 error = wrap_not_worktree_error(error, "update",
3248 error = check_rebase_or_histedit_in_progress(worktree);
3252 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3257 error = apply_unveil(got_repo_get_path(repo), 0,
3258 got_worktree_get_root_path(worktree));
3262 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3266 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3267 got_worktree_get_head_ref_name(worktree), 0);
3270 if (commit_id_str == NULL) {
3271 error = got_ref_resolve(&commit_id, repo, head_ref);
3274 error = got_object_id_str(&commit_id_str, commit_id);
3278 struct got_reflist_head refs;
3280 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3284 error = got_repo_match_object_id(&commit_id, NULL,
3285 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3286 got_ref_list_free(&refs);
3287 free(commit_id_str);
3288 commit_id_str = NULL;
3291 error = got_object_id_str(&commit_id_str, commit_id);
3297 struct got_object_id *head_commit_id;
3298 TAILQ_FOREACH(pe, &paths, entry) {
3299 if (pe->path_len == 0)
3301 error = got_error_msg(GOT_ERR_BAD_PATH,
3302 "switching between branches requires that "
3303 "the entire work tree gets updated");
3306 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3309 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3311 free(head_commit_id);
3314 error = check_same_branch(commit_id, head_ref, NULL, repo);
3317 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3321 error = check_linear_ancestry(commit_id,
3322 got_worktree_get_base_commit_id(worktree), 0, repo);
3323 if (error != NULL) {
3324 if (error->code == GOT_ERR_ANCESTRY)
3325 error = got_error(GOT_ERR_BRANCH_MOVED);
3328 error = check_same_branch(commit_id, head_ref, NULL, repo);
3333 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3335 error = got_worktree_set_base_commit_id(worktree, repo,
3341 memset(&upa, 0, sizeof(upa));
3342 error = got_worktree_checkout_files(worktree, &paths, repo,
3343 update_progress, &upa, check_cancelled, NULL);
3347 if (upa.did_something)
3348 printf("Updated to commit %s\n", commit_id_str);
3350 printf("Already up-to-date\n");
3351 print_update_progress_stats(&upa);
3353 free(worktree_path);
3354 TAILQ_FOREACH(pe, &paths, entry)
3355 free((char *)pe->path);
3356 got_pathlist_free(&paths);
3358 free(commit_id_str);
3362 static const struct got_error *
3363 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3364 const char *path, int diff_context, int ignore_whitespace,
3365 int force_text_diff, struct got_repository *repo)
3367 const struct got_error *err = NULL;
3368 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3371 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3376 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3380 while (path[0] == '/')
3382 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3383 diff_context, ignore_whitespace, force_text_diff, stdout);
3386 got_object_blob_close(blob1);
3387 got_object_blob_close(blob2);
3391 static const struct got_error *
3392 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3393 const char *path, int diff_context, int ignore_whitespace,
3394 int force_text_diff, struct got_repository *repo)
3396 const struct got_error *err = NULL;
3397 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3398 struct got_diff_blob_output_unidiff_arg arg;
3401 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3406 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3410 arg.diff_context = diff_context;
3411 arg.ignore_whitespace = ignore_whitespace;
3412 arg.force_text_diff = force_text_diff;
3413 arg.outfile = stdout;
3414 arg.line_offsets = NULL;
3416 while (path[0] == '/')
3418 err = got_diff_tree(tree1, tree2, path, path, repo,
3419 got_diff_blob_output_unidiff, &arg, 1);
3422 got_object_tree_close(tree1);
3424 got_object_tree_close(tree2);
3428 static const struct got_error *
3429 get_changed_paths(struct got_pathlist_head *paths,
3430 struct got_commit_object *commit, struct got_repository *repo)
3432 const struct got_error *err = NULL;
3433 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3434 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3435 struct got_object_qid *qid;
3437 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3439 struct got_commit_object *pcommit;
3440 err = got_object_open_as_commit(&pcommit, repo,
3445 tree_id1 = got_object_id_dup(
3446 got_object_commit_get_tree_id(pcommit));
3447 if (tree_id1 == NULL) {
3448 got_object_commit_close(pcommit);
3449 return got_error_from_errno("got_object_id_dup");
3451 got_object_commit_close(pcommit);
3456 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3461 tree_id2 = got_object_commit_get_tree_id(commit);
3462 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3466 err = got_diff_tree(tree1, tree2, "", "", repo,
3467 got_diff_tree_collect_changed_paths, paths, 0);
3470 got_object_tree_close(tree1);
3472 got_object_tree_close(tree2);
3477 static const struct got_error *
3478 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3479 const char *path, int diff_context, struct got_repository *repo)
3481 const struct got_error *err = NULL;
3482 struct got_commit_object *pcommit = NULL;
3483 char *id_str1 = NULL, *id_str2 = NULL;
3484 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3485 struct got_object_qid *qid;
3487 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3489 err = got_object_open_as_commit(&pcommit, repo,
3495 if (path && path[0] != '\0') {
3497 err = got_object_id_by_path(&obj_id2, repo, id, path);
3500 err = got_object_id_str(&id_str2, obj_id2);
3506 err = got_object_id_by_path(&obj_id1, repo,
3509 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3514 err = got_object_id_str(&id_str1, obj_id1);
3521 err = got_object_get_type(&obj_type, repo, obj_id2);
3526 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3528 case GOT_OBJ_TYPE_BLOB:
3529 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3532 case GOT_OBJ_TYPE_TREE:
3533 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3537 err = got_error(GOT_ERR_OBJ_TYPE);
3543 obj_id2 = got_object_commit_get_tree_id(commit);
3544 err = got_object_id_str(&id_str2, obj_id2);
3548 obj_id1 = got_object_commit_get_tree_id(pcommit);
3549 err = got_object_id_str(&id_str1, obj_id1);
3553 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3555 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3562 got_object_commit_close(pcommit);
3567 get_datestr(time_t *time, char *datebuf)
3569 struct tm mytm, *tm;
3572 tm = gmtime_r(time, &mytm);
3575 s = asctime_r(tm, datebuf);
3578 p = strchr(s, '\n');
3584 static const struct got_error *
3585 match_logmsg(int *have_match, struct got_object_id *id,
3586 struct got_commit_object *commit, regex_t *regex)
3588 const struct got_error *err = NULL;
3589 regmatch_t regmatch;
3590 char *id_str = NULL, *logmsg = NULL;
3594 err = got_object_id_str(&id_str, id);
3598 err = got_object_commit_get_logmsg(&logmsg, commit);
3602 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3611 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3614 regmatch_t regmatch;
3615 struct got_pathlist_entry *pe;
3619 TAILQ_FOREACH(pe, changed_paths, entry) {
3620 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3627 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3629 static const struct got_error*
3630 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3631 struct got_object_id *id, struct got_repository *repo)
3633 static const struct got_error *err = NULL;
3634 struct got_reflist_entry *re;
3640 TAILQ_FOREACH(re, refs, entry) {
3641 struct got_tag_object *tag = NULL;
3642 struct got_object_id *ref_id;
3645 name = got_ref_get_name(re->ref);
3646 if (strcmp(name, GOT_REF_HEAD) == 0)
3648 if (strncmp(name, "refs/", 5) == 0)
3650 if (strncmp(name, "got/", 4) == 0)
3652 if (strncmp(name, "heads/", 6) == 0)
3654 if (strncmp(name, "remotes/", 8) == 0) {
3656 s = strstr(name, "/" GOT_REF_HEAD);
3657 if (s != NULL && s[strlen(s)] == '\0')
3660 err = got_ref_resolve(&ref_id, repo, re->ref);
3663 if (strncmp(name, "tags/", 5) == 0) {
3664 err = got_object_open_as_tag(&tag, repo, ref_id);
3666 if (err->code != GOT_ERR_OBJ_TYPE) {
3670 /* Ref points at something other than a tag. */
3675 cmp = got_object_id_cmp(tag ?
3676 got_object_tag_get_object_id(tag) : ref_id, id);
3679 got_object_tag_close(tag);
3683 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3684 s ? ", " : "", name) == -1) {
3685 err = got_error_from_errno("asprintf");
3696 static const struct got_error *
3697 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3698 struct got_repository *repo, const char *path,
3699 struct got_pathlist_head *changed_paths, int show_patch,
3700 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3701 const char *custom_refs_str)
3703 const struct got_error *err = NULL;
3704 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3706 time_t committer_time;
3707 const char *author, *committer;
3708 char *refs_str = NULL;
3710 err = got_object_id_str(&id_str, id);
3714 if (custom_refs_str == NULL) {
3715 struct got_reflist_head *refs;
3716 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3718 err = build_refs_str(&refs_str, refs, id, repo);
3724 printf(GOT_COMMIT_SEP_STR);
3725 if (custom_refs_str)
3726 printf("commit %s (%s)\n", id_str, custom_refs_str);
3728 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3729 refs_str ? refs_str : "", refs_str ? ")" : "");
3734 printf("from: %s\n", got_object_commit_get_author(commit));
3735 committer_time = got_object_commit_get_committer_time(commit);
3736 datestr = get_datestr(&committer_time, datebuf);
3738 printf("date: %s UTC\n", datestr);
3739 author = got_object_commit_get_author(commit);
3740 committer = got_object_commit_get_committer(commit);
3741 if (strcmp(author, committer) != 0)
3742 printf("via: %s\n", committer);
3743 if (got_object_commit_get_nparents(commit) > 1) {
3744 const struct got_object_id_queue *parent_ids;
3745 struct got_object_qid *qid;
3747 parent_ids = got_object_commit_get_parent_ids(commit);
3748 STAILQ_FOREACH(qid, parent_ids, entry) {
3749 err = got_object_id_str(&id_str, qid->id);
3752 printf("parent %d: %s\n", n++, id_str);
3758 err = got_object_commit_get_logmsg(&logmsg0, commit);
3764 line = strsep(&logmsg, "\n");
3766 printf(" %s\n", line);
3770 if (changed_paths) {
3771 struct got_pathlist_entry *pe;
3772 TAILQ_FOREACH(pe, changed_paths, entry) {
3773 struct got_diff_changed_path *cp = pe->data;
3774 printf(" %c %s\n", cp->status, pe->path);
3779 err = print_patch(commit, id, path, diff_context, repo);
3784 if (fflush(stdout) != 0 && err == NULL)
3785 err = got_error_from_errno("fflush");
3792 static const struct got_error *
3793 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3794 struct got_repository *repo, const char *path, int show_changed_paths,
3795 int show_patch, const char *search_pattern, int diff_context, int limit,
3796 int log_branches, int reverse_display_order,
3797 struct got_reflist_object_id_map *refs_idmap)
3799 const struct got_error *err;
3800 struct got_commit_graph *graph;
3803 struct got_object_id_queue reversed_commits;
3804 struct got_object_qid *qid;
3805 struct got_commit_object *commit;
3806 struct got_pathlist_head changed_paths;
3807 struct got_pathlist_entry *pe;
3809 STAILQ_INIT(&reversed_commits);
3810 TAILQ_INIT(&changed_paths);
3812 if (search_pattern && regcomp(®ex, search_pattern,
3813 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3814 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3816 err = got_commit_graph_open(&graph, path, !log_branches);
3819 err = got_commit_graph_iter_start(graph, root_id, repo,
3820 check_cancelled, NULL);
3824 struct got_object_id *id;
3826 if (sigint_received || sigpipe_received)
3829 err = got_commit_graph_iter_next(&id, graph, repo,
3830 check_cancelled, NULL);
3832 if (err->code == GOT_ERR_ITER_COMPLETED)
3839 err = got_object_open_as_commit(&commit, repo, id);
3843 if (show_changed_paths && !reverse_display_order) {
3844 err = get_changed_paths(&changed_paths, commit, repo);
3849 if (search_pattern) {
3850 err = match_logmsg(&have_match, id, commit, ®ex);
3852 got_object_commit_close(commit);
3855 if (have_match == 0 && show_changed_paths)
3856 match_changed_paths(&have_match,
3857 &changed_paths, ®ex);
3858 if (have_match == 0) {
3859 got_object_commit_close(commit);
3860 TAILQ_FOREACH(pe, &changed_paths, entry) {
3861 free((char *)pe->path);
3864 got_pathlist_free(&changed_paths);
3869 if (reverse_display_order) {
3870 err = got_object_qid_alloc(&qid, id);
3873 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3874 got_object_commit_close(commit);
3876 err = print_commit(commit, id, repo, path,
3877 show_changed_paths ? &changed_paths : NULL,
3878 show_patch, diff_context, refs_idmap, NULL);
3879 got_object_commit_close(commit);
3883 if ((limit && --limit == 0) ||
3884 (end_id && got_object_id_cmp(id, end_id) == 0))
3887 TAILQ_FOREACH(pe, &changed_paths, entry) {
3888 free((char *)pe->path);
3891 got_pathlist_free(&changed_paths);
3893 if (reverse_display_order) {
3894 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3895 err = got_object_open_as_commit(&commit, repo, qid->id);
3898 if (show_changed_paths) {
3899 err = get_changed_paths(&changed_paths,
3904 err = print_commit(commit, qid->id, repo, path,
3905 show_changed_paths ? &changed_paths : NULL,
3906 show_patch, diff_context, refs_idmap, NULL);
3907 got_object_commit_close(commit);
3910 TAILQ_FOREACH(pe, &changed_paths, entry) {
3911 free((char *)pe->path);
3914 got_pathlist_free(&changed_paths);
3918 while (!STAILQ_EMPTY(&reversed_commits)) {
3919 qid = STAILQ_FIRST(&reversed_commits);
3920 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3921 got_object_qid_free(qid);
3923 TAILQ_FOREACH(pe, &changed_paths, entry) {
3924 free((char *)pe->path);
3927 got_pathlist_free(&changed_paths);
3930 got_commit_graph_close(graph);
3937 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3938 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3939 "[-R] [path]\n", getprogname());
3944 get_default_log_limit(void)
3946 const char *got_default_log_limit;
3950 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3951 if (got_default_log_limit == NULL)
3953 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3959 static const struct got_error *
3960 cmd_log(int argc, char *argv[])
3962 const struct got_error *error;
3963 struct got_repository *repo = NULL;
3964 struct got_worktree *worktree = NULL;
3965 struct got_object_id *start_id = NULL, *end_id = NULL;
3966 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3967 const char *start_commit = NULL, *end_commit = NULL;
3968 const char *search_pattern = NULL;
3969 int diff_context = -1, ch;
3970 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3971 int reverse_display_order = 0;
3973 struct got_reflist_head refs;
3974 struct got_reflist_object_id_map *refs_idmap = NULL;
3979 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3985 limit = get_default_log_limit();
3987 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3993 show_changed_paths = 1;
3996 start_commit = optarg;
3999 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4002 err(1, "-C option %s", errstr);
4005 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4007 err(1, "-l option %s", errstr);
4013 repo_path = realpath(optarg, NULL);
4014 if (repo_path == NULL)
4015 return got_error_from_errno2("realpath",
4017 got_path_strip_trailing_slashes(repo_path);
4020 reverse_display_order = 1;
4023 search_pattern = optarg;
4026 end_commit = optarg;
4037 if (diff_context == -1)
4039 else if (!show_patch)
4040 errx(1, "-C requires -p");
4042 cwd = getcwd(NULL, 0);
4044 error = got_error_from_errno("getcwd");
4048 if (repo_path == NULL) {
4049 error = got_worktree_open(&worktree, cwd);
4050 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4057 error = got_worktree_resolve_path(&path, worktree,
4062 path = strdup(argv[0]);
4064 error = got_error_from_errno("strdup");
4068 } else if (argc != 0)
4071 if (repo_path == NULL) {
4072 repo_path = worktree ?
4073 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4075 if (repo_path == NULL) {
4076 error = got_error_from_errno("strdup");
4080 error = got_repo_open(&repo, repo_path, NULL);
4084 error = apply_unveil(got_repo_get_path(repo), 1,
4085 worktree ? got_worktree_get_root_path(worktree) : NULL);
4089 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4093 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4097 if (start_commit == NULL) {
4098 struct got_reference *head_ref;
4099 struct got_commit_object *commit = NULL;
4100 error = got_ref_open(&head_ref, repo,
4101 worktree ? got_worktree_get_head_ref_name(worktree)
4105 error = got_ref_resolve(&start_id, repo, head_ref);
4106 got_ref_close(head_ref);
4109 error = got_object_open_as_commit(&commit, repo,
4113 got_object_commit_close(commit);
4115 error = got_repo_match_object_id(&start_id, NULL,
4116 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4120 if (end_commit != NULL) {
4121 error = got_repo_match_object_id(&end_id, NULL,
4122 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4129 * If a path was specified on the command line it was resolved
4130 * to a path in the work tree above. Prepend the work tree's
4131 * path prefix to obtain the corresponding in-repository path.
4135 prefix = got_worktree_get_path_prefix(worktree);
4136 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4137 (path[0] != '\0') ? "/" : "", path) == -1) {
4138 error = got_error_from_errno("asprintf");
4143 error = got_repo_map_path(&in_repo_path, repo,
4149 path = in_repo_path;
4152 error = print_commits(start_id, end_id, repo, path ? path : "",
4153 show_changed_paths, show_patch, search_pattern, diff_context,
4154 limit, log_branches, reverse_display_order, refs_idmap);
4160 got_worktree_close(worktree);
4162 const struct got_error *close_err = got_repo_close(repo);
4167 got_reflist_object_id_map_free(refs_idmap);
4168 got_ref_list_free(&refs);
4175 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4176 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4180 struct print_diff_arg {
4181 struct got_repository *repo;
4182 struct got_worktree *worktree;
4187 int ignore_whitespace;
4188 int force_text_diff;
4192 * Create a file which contains the target path of a symlink so we can feed
4193 * it as content to the diff engine.
4195 static const struct got_error *
4196 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4197 const char *abspath)
4199 const struct got_error *err = NULL;
4200 char target_path[PATH_MAX];
4201 ssize_t target_len, outlen;
4206 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4207 if (target_len == -1)
4208 return got_error_from_errno2("readlinkat", abspath);
4210 target_len = readlink(abspath, target_path, PATH_MAX);
4211 if (target_len == -1)
4212 return got_error_from_errno2("readlink", abspath);
4215 *fd = got_opentempfd();
4217 return got_error_from_errno("got_opentempfd");
4219 outlen = write(*fd, target_path, target_len);
4221 err = got_error_from_errno("got_opentempfd");
4225 if (lseek(*fd, 0, SEEK_SET) == -1) {
4226 err = got_error_from_errno2("lseek", abspath);
4237 static const struct got_error *
4238 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4239 const char *path, struct got_object_id *blob_id,
4240 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4241 int dirfd, const char *de_name)
4243 struct print_diff_arg *a = arg;
4244 const struct got_error *err = NULL;
4245 struct got_blob_object *blob1 = NULL;
4248 char *abspath = NULL, *label1 = NULL;
4251 if (a->diff_staged) {
4252 if (staged_status != GOT_STATUS_MODIFY &&
4253 staged_status != GOT_STATUS_ADD &&
4254 staged_status != GOT_STATUS_DELETE)
4257 if (staged_status == GOT_STATUS_DELETE)
4259 if (status == GOT_STATUS_NONEXISTENT)
4260 return got_error_set_errno(ENOENT, path);
4261 if (status != GOT_STATUS_MODIFY &&
4262 status != GOT_STATUS_ADD &&
4263 status != GOT_STATUS_DELETE &&
4264 status != GOT_STATUS_CONFLICT)
4268 if (!a->header_shown) {
4269 printf("diff %s %s%s\n", a->id_str,
4270 got_worktree_get_root_path(a->worktree),
4271 a->diff_staged ? " (staged changes)" : "");
4272 a->header_shown = 1;
4275 if (a->diff_staged) {
4276 const char *label1 = NULL, *label2 = NULL;
4277 switch (staged_status) {
4278 case GOT_STATUS_MODIFY:
4282 case GOT_STATUS_ADD:
4285 case GOT_STATUS_DELETE:
4289 return got_error(GOT_ERR_FILE_STATUS);
4291 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4292 staged_blob_id, label1, label2, a->diff_context,
4293 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4296 if (staged_status == GOT_STATUS_ADD ||
4297 staged_status == GOT_STATUS_MODIFY) {
4299 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4303 err = got_object_id_str(&id_str, staged_blob_id);
4306 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4307 err = got_error_from_errno("asprintf");
4312 } else if (status != GOT_STATUS_ADD) {
4313 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4318 if (status != GOT_STATUS_DELETE) {
4319 if (asprintf(&abspath, "%s/%s",
4320 got_worktree_get_root_path(a->worktree), path) == -1) {
4321 err = got_error_from_errno("asprintf");
4326 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4328 if (errno != ELOOP) {
4329 err = got_error_from_errno2("openat",
4333 err = get_symlink_target_file(&fd, dirfd,
4339 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4341 if (errno != ELOOP) {
4342 err = got_error_from_errno2("open",
4346 err = get_symlink_target_file(&fd, dirfd,
4352 if (fstat(fd, &sb) == -1) {
4353 err = got_error_from_errno2("fstat", abspath);
4356 f2 = fdopen(fd, "r");
4358 err = got_error_from_errno2("fdopen", abspath);
4365 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4366 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4369 got_object_blob_close(blob1);
4370 if (f2 && fclose(f2) == EOF && err == NULL)
4371 err = got_error_from_errno("fclose");
4372 if (fd != -1 && close(fd) == -1 && err == NULL)
4373 err = got_error_from_errno("close");
4378 static const struct got_error *
4379 cmd_diff(int argc, char *argv[])
4381 const struct got_error *error;
4382 struct got_repository *repo = NULL;
4383 struct got_worktree *worktree = NULL;
4384 char *cwd = NULL, *repo_path = NULL;
4385 struct got_object_id *id1 = NULL, *id2 = NULL;
4386 const char *id_str1 = NULL, *id_str2 = NULL;
4387 char *label1 = NULL, *label2 = NULL;
4389 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4390 int force_text_diff = 0;
4393 struct got_reflist_head refs;
4398 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4403 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4406 force_text_diff = 1;
4409 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4412 err(1, "-C option %s", errstr);
4415 repo_path = realpath(optarg, NULL);
4416 if (repo_path == NULL)
4417 return got_error_from_errno2("realpath",
4419 got_path_strip_trailing_slashes(repo_path);
4425 ignore_whitespace = 1;
4436 cwd = getcwd(NULL, 0);
4438 error = got_error_from_errno("getcwd");
4444 "-r option can't be used when diffing a work tree");
4445 error = got_worktree_open(&worktree, cwd);
4447 if (error->code == GOT_ERR_NOT_WORKTREE)
4448 error = wrap_not_worktree_error(error, "diff",
4452 repo_path = strdup(got_worktree_get_repo_path(worktree));
4453 if (repo_path == NULL) {
4454 error = got_error_from_errno("strdup");
4458 error = got_worktree_resolve_path(&path, worktree,
4465 error = got_error_from_errno("strdup");
4469 } else if (argc == 2) {
4471 errx(1, "-s option can't be used when diffing "
4472 "objects in repository");
4475 if (repo_path == NULL) {
4476 error = got_worktree_open(&worktree, cwd);
4477 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4479 repo_path = strdup(worktree ?
4480 got_worktree_get_repo_path(worktree) : cwd);
4481 if (repo_path == NULL) {
4482 error = got_error_from_errno("strdup");
4489 error = got_repo_open(&repo, repo_path, NULL);
4494 error = apply_unveil(got_repo_get_path(repo), 1,
4495 worktree ? got_worktree_get_root_path(worktree) : NULL);
4500 struct print_diff_arg arg;
4501 struct got_pathlist_head paths;
4506 error = got_object_id_str(&id_str,
4507 got_worktree_get_base_commit_id(worktree));
4511 arg.worktree = worktree;
4512 arg.diff_context = diff_context;
4513 arg.id_str = id_str;
4514 arg.header_shown = 0;
4515 arg.diff_staged = diff_staged;
4516 arg.ignore_whitespace = ignore_whitespace;
4517 arg.force_text_diff = force_text_diff;
4519 error = got_pathlist_append(&paths, path, NULL);
4523 error = got_worktree_status(worktree, &paths, repo, 0,
4524 print_diff, &arg, check_cancelled, NULL);
4526 got_pathlist_free(&paths);
4530 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4534 error = got_repo_match_object_id(&id1, &label1, id_str1,
4535 GOT_OBJ_TYPE_ANY, &refs, repo);
4539 error = got_repo_match_object_id(&id2, &label2, id_str2,
4540 GOT_OBJ_TYPE_ANY, &refs, repo);
4544 error = got_object_get_type(&type1, repo, id1);
4548 error = got_object_get_type(&type2, repo, id2);
4552 if (type1 != type2) {
4553 error = got_error(GOT_ERR_OBJ_TYPE);
4558 case GOT_OBJ_TYPE_BLOB:
4559 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4560 NULL, NULL, diff_context, ignore_whitespace,
4561 force_text_diff, repo, stdout);
4563 case GOT_OBJ_TYPE_TREE:
4564 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4565 "", "", diff_context, ignore_whitespace, force_text_diff,
4568 case GOT_OBJ_TYPE_COMMIT:
4569 printf("diff %s %s\n", label1, label2);
4570 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4571 diff_context, ignore_whitespace, force_text_diff, repo,
4575 error = got_error(GOT_ERR_OBJ_TYPE);
4584 got_worktree_close(worktree);
4586 const struct got_error *close_err = got_repo_close(repo);
4590 got_ref_list_free(&refs);
4598 "usage: %s blame [-c commit] [-r repository-path] path\n",
4607 char datebuf[11]; /* YYYY-MM-DD + NUL */
4610 struct blame_cb_args {
4611 struct blame_line *lines;
4615 off_t *line_offsets;
4617 struct got_repository *repo;
4620 static const struct got_error *
4621 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4623 const struct got_error *err = NULL;
4624 struct blame_cb_args *a = arg;
4625 struct blame_line *bline;
4627 size_t linesize = 0;
4628 struct got_commit_object *commit = NULL;
4631 time_t committer_time;
4633 if (nlines != a->nlines ||
4634 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4635 return got_error(GOT_ERR_RANGE);
4637 if (sigint_received)
4638 return got_error(GOT_ERR_ITER_COMPLETED);
4641 return NULL; /* no change in this commit */
4643 /* Annotate this line. */
4644 bline = &a->lines[lineno - 1];
4645 if (bline->annotated)
4647 err = got_object_id_str(&bline->id_str, id);
4651 err = got_object_open_as_commit(&commit, a->repo, id);
4655 bline->committer = strdup(got_object_commit_get_committer(commit));
4656 if (bline->committer == NULL) {
4657 err = got_error_from_errno("strdup");
4661 committer_time = got_object_commit_get_committer_time(commit);
4662 if (localtime_r(&committer_time, &tm) == NULL)
4663 return got_error_from_errno("localtime_r");
4664 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4666 err = got_error(GOT_ERR_NO_SPACE);
4669 bline->annotated = 1;
4671 /* Print lines annotated so far. */
4672 bline = &a->lines[a->lineno_cur - 1];
4673 if (!bline->annotated)
4676 offset = a->line_offsets[a->lineno_cur - 1];
4677 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4678 err = got_error_from_errno("fseeko");
4682 while (bline->annotated) {
4683 char *smallerthan, *at, *nl, *committer;
4686 if (getline(&line, &linesize, a->f) == -1) {
4688 err = got_error_from_errno("getline");
4692 committer = bline->committer;
4693 smallerthan = strchr(committer, '<');
4694 if (smallerthan && smallerthan[1] != '\0')
4695 committer = smallerthan + 1;
4696 at = strchr(committer, '@');
4699 len = strlen(committer);
4701 committer[8] = '\0';
4703 nl = strchr(line, '\n');
4706 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4707 bline->id_str, bline->datebuf, committer, line);
4710 bline = &a->lines[a->lineno_cur - 1];
4714 got_object_commit_close(commit);
4719 static const struct got_error *
4720 cmd_blame(int argc, char *argv[])
4722 const struct got_error *error;
4723 struct got_repository *repo = NULL;
4724 struct got_worktree *worktree = NULL;
4725 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4726 char *link_target = NULL;
4727 struct got_object_id *obj_id = NULL;
4728 struct got_object_id *commit_id = NULL;
4729 struct got_blob_object *blob = NULL;
4730 char *commit_id_str = NULL;
4731 struct blame_cb_args bca;
4732 int ch, obj_type, i;
4735 memset(&bca, 0, sizeof(bca));
4738 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4743 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4746 commit_id_str = optarg;
4749 repo_path = realpath(optarg, NULL);
4750 if (repo_path == NULL)
4751 return got_error_from_errno2("realpath",
4753 got_path_strip_trailing_slashes(repo_path);
4769 cwd = getcwd(NULL, 0);
4771 error = got_error_from_errno("getcwd");
4774 if (repo_path == NULL) {
4775 error = got_worktree_open(&worktree, cwd);
4776 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4782 strdup(got_worktree_get_repo_path(worktree));
4783 if (repo_path == NULL) {
4784 error = got_error_from_errno("strdup");
4789 repo_path = strdup(cwd);
4790 if (repo_path == NULL) {
4791 error = got_error_from_errno("strdup");
4797 error = got_repo_open(&repo, repo_path, NULL);
4802 const char *prefix = got_worktree_get_path_prefix(worktree);
4805 error = got_worktree_resolve_path(&p, worktree, path);
4808 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4809 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4811 error = got_error_from_errno("asprintf");
4816 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4818 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4821 error = got_repo_map_path(&in_repo_path, repo, path);
4826 if (commit_id_str == NULL) {
4827 struct got_reference *head_ref;
4828 error = got_ref_open(&head_ref, repo, worktree ?
4829 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4832 error = got_ref_resolve(&commit_id, repo, head_ref);
4833 got_ref_close(head_ref);
4837 struct got_reflist_head refs;
4839 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4843 error = got_repo_match_object_id(&commit_id, NULL,
4844 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4845 got_ref_list_free(&refs);
4850 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4855 error = got_object_id_by_path(&obj_id, repo, commit_id,
4856 link_target ? link_target : in_repo_path);
4860 error = got_object_get_type(&obj_type, repo, obj_id);
4864 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4865 error = got_error_path(link_target ? link_target : in_repo_path,
4870 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4873 bca.f = got_opentemp();
4874 if (bca.f == NULL) {
4875 error = got_error_from_errno("got_opentemp");
4878 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4879 &bca.line_offsets, bca.f, blob);
4880 if (error || bca.nlines == 0)
4883 /* Don't include \n at EOF in the blame line count. */
4884 if (bca.line_offsets[bca.nlines - 1] == filesize)
4887 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4888 if (bca.lines == NULL) {
4889 error = got_error_from_errno("calloc");
4893 bca.nlines_prec = 0;
4901 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4902 repo, blame_cb, &bca, check_cancelled, NULL);
4911 got_object_blob_close(blob);
4913 got_worktree_close(worktree);
4915 const struct got_error *close_err = got_repo_close(repo);
4920 for (i = 0; i < bca.nlines; i++) {
4921 struct blame_line *bline = &bca.lines[i];
4922 free(bline->id_str);
4923 free(bline->committer);
4927 free(bca.line_offsets);
4928 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4929 error = got_error_from_errno("fclose");
4937 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4942 static const struct got_error *
4943 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4944 const char *root_path, struct got_repository *repo)
4946 const struct got_error *err = NULL;
4947 int is_root_path = (strcmp(path, root_path) == 0);
4948 const char *modestr = "";
4949 mode_t mode = got_tree_entry_get_mode(te);
4950 char *link_target = NULL;
4952 path += strlen(root_path);
4953 while (path[0] == '/')
4956 if (got_object_tree_entry_is_submodule(te))
4958 else if (S_ISLNK(mode)) {
4961 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4964 for (i = 0; i < strlen(link_target); i++) {
4965 if (!isprint((unsigned char)link_target[i]))
4966 link_target[i] = '?';
4971 else if (S_ISDIR(mode))
4973 else if (mode & S_IXUSR)
4976 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4977 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4978 link_target ? " -> ": "", link_target ? link_target : "");
4984 static const struct got_error *
4985 print_tree(const char *path, struct got_object_id *commit_id,
4986 int show_ids, int recurse, const char *root_path,
4987 struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_object_id *tree_id = NULL;
4991 struct got_tree_object *tree = NULL;
4994 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4998 err = got_object_open_as_tree(&tree, repo, tree_id);
5001 nentries = got_object_tree_get_nentries(tree);
5002 for (i = 0; i < nentries; i++) {
5003 struct got_tree_entry *te;
5006 if (sigint_received || sigpipe_received)
5009 te = got_object_tree_get_entry(tree, i);
5012 err = got_object_id_str(&id_str,
5013 got_tree_entry_get_id(te));
5016 if (asprintf(&id, "%s ", id_str) == -1) {
5017 err = got_error_from_errno("asprintf");
5023 err = print_entry(te, id, path, root_path, repo);
5028 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5030 if (asprintf(&child_path, "%s%s%s", path,
5031 path[0] == '/' && path[1] == '\0' ? "" : "/",
5032 got_tree_entry_get_name(te)) == -1) {
5033 err = got_error_from_errno("asprintf");
5036 err = print_tree(child_path, commit_id, show_ids, 1,
5045 got_object_tree_close(tree);
5050 static const struct got_error *
5051 cmd_tree(int argc, char *argv[])
5053 const struct got_error *error;
5054 struct got_repository *repo = NULL;
5055 struct got_worktree *worktree = NULL;
5056 const char *path, *refname = NULL;
5057 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5058 struct got_object_id *commit_id = NULL;
5059 char *commit_id_str = NULL;
5060 int show_ids = 0, recurse = 0;
5064 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5069 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5072 commit_id_str = optarg;
5075 repo_path = realpath(optarg, NULL);
5076 if (repo_path == NULL)
5077 return got_error_from_errno2("realpath",
5079 got_path_strip_trailing_slashes(repo_path);
5103 cwd = getcwd(NULL, 0);
5105 error = got_error_from_errno("getcwd");
5108 if (repo_path == NULL) {
5109 error = got_worktree_open(&worktree, cwd);
5110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5116 strdup(got_worktree_get_repo_path(worktree));
5117 if (repo_path == NULL)
5118 error = got_error_from_errno("strdup");
5122 repo_path = strdup(cwd);
5123 if (repo_path == NULL) {
5124 error = got_error_from_errno("strdup");
5130 error = got_repo_open(&repo, repo_path, NULL);
5135 const char *prefix = got_worktree_get_path_prefix(worktree);
5140 error = got_worktree_resolve_path(&p, worktree, path);
5143 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5144 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5146 error = got_error_from_errno("asprintf");
5151 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5155 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5160 error = got_repo_map_path(&in_repo_path, repo, path);
5165 if (commit_id_str == NULL) {
5166 struct got_reference *head_ref;
5168 refname = got_worktree_get_head_ref_name(worktree);
5170 refname = GOT_REF_HEAD;
5171 error = got_ref_open(&head_ref, repo, refname, 0);
5174 error = got_ref_resolve(&commit_id, repo, head_ref);
5175 got_ref_close(head_ref);
5179 struct got_reflist_head refs;
5181 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5185 error = got_repo_match_object_id(&commit_id, NULL,
5186 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5187 got_ref_list_free(&refs);
5192 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5193 in_repo_path, repo);
5200 got_worktree_close(worktree);
5202 const struct got_error *close_err = got_repo_close(repo);
5212 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5217 static const struct got_error *
5218 print_status(void *arg, unsigned char status, unsigned char staged_status,
5219 const char *path, struct got_object_id *blob_id,
5220 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5221 int dirfd, const char *de_name)
5223 if (status == staged_status && (status == GOT_STATUS_DELETE))
5224 status = GOT_STATUS_NO_CHANGE;
5226 char *status_codes = arg;
5227 size_t ncodes = strlen(status_codes);
5229 for (i = 0; i < ncodes ; i++) {
5230 if (status == status_codes[i] ||
5231 staged_status == status_codes[i])
5237 printf("%c%c %s\n", status, staged_status, path);
5241 static const struct got_error *
5242 cmd_status(int argc, char *argv[])
5244 const struct got_error *error = NULL;
5245 struct got_repository *repo = NULL;
5246 struct got_worktree *worktree = NULL;
5247 char *cwd = NULL, *status_codes = NULL;;
5248 struct got_pathlist_head paths;
5249 struct got_pathlist_entry *pe;
5250 int ch, i, no_ignores = 0;
5254 while ((ch = getopt(argc, argv, "Is:")) != -1) {
5260 for (i = 0; i < strlen(optarg); i++) {
5261 switch (optarg[i]) {
5262 case GOT_STATUS_MODIFY:
5263 case GOT_STATUS_ADD:
5264 case GOT_STATUS_DELETE:
5265 case GOT_STATUS_CONFLICT:
5266 case GOT_STATUS_MISSING:
5267 case GOT_STATUS_OBSTRUCTED:
5268 case GOT_STATUS_UNVERSIONED:
5269 case GOT_STATUS_MODE_CHANGE:
5270 case GOT_STATUS_NONEXISTENT:
5273 errx(1, "invalid status code '%c'",
5277 status_codes = optarg;
5289 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5293 cwd = getcwd(NULL, 0);
5295 error = got_error_from_errno("getcwd");
5299 error = got_worktree_open(&worktree, cwd);
5301 if (error->code == GOT_ERR_NOT_WORKTREE)
5302 error = wrap_not_worktree_error(error, "status", cwd);
5306 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5311 error = apply_unveil(got_repo_get_path(repo), 1,
5312 got_worktree_get_root_path(worktree));
5316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5320 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5321 print_status, status_codes, check_cancelled, NULL);
5323 TAILQ_FOREACH(pe, &paths, entry)
5324 free((char *)pe->path);
5325 got_pathlist_free(&paths);
5334 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5340 static const struct got_error *
5341 list_refs(struct got_repository *repo, const char *refname)
5343 static const struct got_error *err = NULL;
5344 struct got_reflist_head refs;
5345 struct got_reflist_entry *re;
5348 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5352 TAILQ_FOREACH(re, &refs, entry) {
5354 refstr = got_ref_to_str(re->ref);
5356 return got_error_from_errno("got_ref_to_str");
5357 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5361 got_ref_list_free(&refs);
5365 static const struct got_error *
5366 delete_ref_by_name(struct got_repository *repo, const char *refname)
5368 const struct got_error *err;
5369 struct got_reference *ref;
5371 err = got_ref_open(&ref, repo, refname, 0);
5375 err = delete_ref(repo, ref);
5380 static const struct got_error *
5381 add_ref(struct got_repository *repo, const char *refname, const char *target)
5383 const struct got_error *err = NULL;
5384 struct got_object_id *id;
5385 struct got_reference *ref = NULL;
5388 * Don't let the user create a reference name with a leading '-'.
5389 * While technically a valid reference name, this case is usually
5390 * an unintended typo.
5392 if (refname[0] == '-')
5393 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5395 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5398 struct got_reference *target_ref;
5400 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5402 err = got_ref_open(&target_ref, repo, target, 0);
5405 err = got_ref_resolve(&id, repo, target_ref);
5406 got_ref_close(target_ref);
5411 err = got_ref_alloc(&ref, refname, id);
5415 err = got_ref_write(ref, repo);
5423 static const struct got_error *
5424 add_symref(struct got_repository *repo, const char *refname, const char *target)
5426 const struct got_error *err = NULL;
5427 struct got_reference *ref = NULL;
5428 struct got_reference *target_ref = NULL;
5431 * Don't let the user create a reference name with a leading '-'.
5432 * While technically a valid reference name, this case is usually
5433 * an unintended typo.
5435 if (refname[0] == '-')
5436 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5438 err = got_ref_open(&target_ref, repo, target, 0);
5442 err = got_ref_alloc_symref(&ref, refname, target_ref);
5446 err = got_ref_write(ref, repo);
5449 got_ref_close(target_ref);
5455 static const struct got_error *
5456 cmd_ref(int argc, char *argv[])
5458 const struct got_error *error = NULL;
5459 struct got_repository *repo = NULL;
5460 struct got_worktree *worktree = NULL;
5461 char *cwd = NULL, *repo_path = NULL;
5462 int ch, do_list = 0, do_delete = 0;
5463 const char *obj_arg = NULL, *symref_target= NULL;
5464 char *refname = NULL;
5466 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5475 repo_path = realpath(optarg, NULL);
5476 if (repo_path == NULL)
5477 return got_error_from_errno2("realpath",
5479 got_path_strip_trailing_slashes(repo_path);
5485 symref_target = optarg;
5493 if (obj_arg && do_list)
5494 option_conflict('c', 'l');
5495 if (obj_arg && do_delete)
5496 option_conflict('c', 'd');
5497 if (obj_arg && symref_target)
5498 option_conflict('c', 's');
5499 if (symref_target && do_delete)
5500 option_conflict('s', 'd');
5501 if (symref_target && do_list)
5502 option_conflict('s', 'l');
5503 if (do_delete && do_list)
5504 option_conflict('d', 'l');
5510 if (argc != 0 && argc != 1)
5513 refname = strdup(argv[0]);
5514 if (refname == NULL) {
5515 error = got_error_from_errno("strdup");
5522 refname = strdup(argv[0]);
5523 if (refname == NULL) {
5524 error = got_error_from_errno("strdup");
5530 got_path_strip_trailing_slashes(refname);
5534 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5538 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5539 "sendfd unveil", NULL) == -1)
5543 cwd = getcwd(NULL, 0);
5545 error = got_error_from_errno("getcwd");
5549 if (repo_path == NULL) {
5550 error = got_worktree_open(&worktree, cwd);
5551 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5557 strdup(got_worktree_get_repo_path(worktree));
5558 if (repo_path == NULL)
5559 error = got_error_from_errno("strdup");
5563 repo_path = strdup(cwd);
5564 if (repo_path == NULL) {
5565 error = got_error_from_errno("strdup");
5571 error = got_repo_open(&repo, repo_path, NULL);
5575 error = apply_unveil(got_repo_get_path(repo), do_list,
5576 worktree ? got_worktree_get_root_path(worktree) : NULL);
5581 error = list_refs(repo, refname);
5583 error = delete_ref_by_name(repo, refname);
5584 else if (symref_target)
5585 error = add_symref(repo, refname, symref_target);
5587 if (obj_arg == NULL)
5589 error = add_ref(repo, refname, obj_arg);
5594 const struct got_error *close_err = got_repo_close(repo);
5599 got_worktree_close(worktree);
5609 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5610 "[name]\n", getprogname());
5614 static const struct got_error *
5615 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5616 struct got_reference *ref)
5618 const struct got_error *err = NULL;
5619 const char *refname, *marker = " ";
5622 refname = got_ref_get_name(ref);
5623 if (worktree && strcmp(refname,
5624 got_worktree_get_head_ref_name(worktree)) == 0) {
5625 struct got_object_id *id = NULL;
5627 err = got_ref_resolve(&id, repo, ref);
5630 if (got_object_id_cmp(id,
5631 got_worktree_get_base_commit_id(worktree)) == 0)
5638 if (strncmp(refname, "refs/heads/", 11) == 0)
5640 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5642 if (strncmp(refname, "refs/remotes/", 13) == 0)
5645 refstr = got_ref_to_str(ref);
5647 return got_error_from_errno("got_ref_to_str");
5649 printf("%s%s: %s\n", marker, refname, refstr);
5654 static const struct got_error *
5655 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5657 const char *refname;
5659 if (worktree == NULL)
5660 return got_error(GOT_ERR_NOT_WORKTREE);
5662 refname = got_worktree_get_head_ref_name(worktree);
5664 if (strncmp(refname, "refs/heads/", 11) == 0)
5666 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5669 printf("%s\n", refname);
5674 static const struct got_error *
5675 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5677 static const struct got_error *err = NULL;
5678 struct got_reflist_head refs;
5679 struct got_reflist_entry *re;
5680 struct got_reference *temp_ref = NULL;
5681 int rebase_in_progress, histedit_in_progress;
5686 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5691 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5696 if (rebase_in_progress || histedit_in_progress) {
5697 err = got_ref_open(&temp_ref, repo,
5698 got_worktree_get_head_ref_name(worktree), 0);
5701 list_branch(repo, worktree, temp_ref);
5702 got_ref_close(temp_ref);
5706 err = got_ref_list(&refs, repo, "refs/heads",
5707 got_ref_cmp_by_name, NULL);
5711 TAILQ_FOREACH(re, &refs, entry)
5712 list_branch(repo, worktree, re->ref);
5714 got_ref_list_free(&refs);
5716 err = got_ref_list(&refs, repo, "refs/remotes",
5717 got_ref_cmp_by_name, NULL);
5721 TAILQ_FOREACH(re, &refs, entry)
5722 list_branch(repo, worktree, re->ref);
5724 got_ref_list_free(&refs);
5729 static const struct got_error *
5730 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5731 const char *branch_name)
5733 const struct got_error *err = NULL;
5734 struct got_reference *ref = NULL;
5735 char *refname, *remote_refname = NULL;
5737 if (strncmp(branch_name, "refs/", 5) == 0)
5739 if (strncmp(branch_name, "heads/", 6) == 0)
5741 else if (strncmp(branch_name, "remotes/", 8) == 0)
5744 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5745 return got_error_from_errno("asprintf");
5747 if (asprintf(&remote_refname, "refs/remotes/%s",
5748 branch_name) == -1) {
5749 err = got_error_from_errno("asprintf");
5753 err = got_ref_open(&ref, repo, refname, 0);
5755 const struct got_error *err2;
5756 if (err->code != GOT_ERR_NOT_REF)
5759 * Keep 'err' intact such that if neither branch exists
5760 * we report "refs/heads" rather than "refs/remotes" in
5761 * our error message.
5763 err2 = got_ref_open(&ref, repo, remote_refname, 0);
5770 strcmp(got_worktree_get_head_ref_name(worktree),
5771 got_ref_get_name(ref)) == 0) {
5772 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5773 "will not delete this work tree's current branch");
5777 err = got_ref_delete(ref, repo);
5782 free(remote_refname);
5786 static const struct got_error *
5787 add_branch(struct got_repository *repo, const char *branch_name,
5788 struct got_object_id *base_commit_id)
5790 const struct got_error *err = NULL;
5791 struct got_reference *ref = NULL;
5792 char *base_refname = NULL, *refname = NULL;
5795 * Don't let the user create a branch name with a leading '-'.
5796 * While technically a valid reference name, this case is usually
5797 * an unintended typo.
5799 if (branch_name[0] == '-')
5800 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5802 if (strncmp(branch_name, "refs/heads/", 11) == 0)
5805 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5806 err = got_error_from_errno("asprintf");
5810 err = got_ref_open(&ref, repo, refname, 0);
5812 err = got_error(GOT_ERR_BRANCH_EXISTS);
5814 } else if (err->code != GOT_ERR_NOT_REF)
5817 err = got_ref_alloc(&ref, refname, base_commit_id);
5821 err = got_ref_write(ref, repo);
5830 static const struct got_error *
5831 cmd_branch(int argc, char *argv[])
5833 const struct got_error *error = NULL;
5834 struct got_repository *repo = NULL;
5835 struct got_worktree *worktree = NULL;
5836 char *cwd = NULL, *repo_path = NULL;
5837 int ch, do_list = 0, do_show = 0, do_update = 1;
5838 const char *delref = NULL, *commit_id_arg = NULL;
5839 struct got_reference *ref = NULL;
5840 struct got_pathlist_head paths;
5841 struct got_pathlist_entry *pe;
5842 struct got_object_id *commit_id = NULL;
5843 char *commit_id_str = NULL;
5847 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5850 commit_id_arg = optarg;
5856 repo_path = realpath(optarg, NULL);
5857 if (repo_path == NULL)
5858 return got_error_from_errno2("realpath",
5860 got_path_strip_trailing_slashes(repo_path);
5874 if (do_list && delref)
5875 option_conflict('l', 'd');
5880 if (!do_list && !delref && argc == 0)
5883 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5884 errx(1, "-c option can only be used when creating a branch");
5886 if (do_list || delref) {
5889 } else if (!do_show && argc != 1)
5893 if (do_list || do_show) {
5894 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5898 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5899 "sendfd unveil", NULL) == -1)
5903 cwd = getcwd(NULL, 0);
5905 error = got_error_from_errno("getcwd");
5909 if (repo_path == NULL) {
5910 error = got_worktree_open(&worktree, cwd);
5911 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5917 strdup(got_worktree_get_repo_path(worktree));
5918 if (repo_path == NULL)
5919 error = got_error_from_errno("strdup");
5923 repo_path = strdup(cwd);
5924 if (repo_path == NULL) {
5925 error = got_error_from_errno("strdup");
5931 error = got_repo_open(&repo, repo_path, NULL);
5935 error = apply_unveil(got_repo_get_path(repo), do_list,
5936 worktree ? got_worktree_get_root_path(worktree) : NULL);
5941 error = show_current_branch(repo, worktree);
5943 error = list_branches(repo, worktree);
5945 error = delete_branch(repo, worktree, delref);
5947 struct got_reflist_head refs;
5949 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5953 if (commit_id_arg == NULL)
5954 commit_id_arg = worktree ?
5955 got_worktree_get_head_ref_name(worktree) :
5957 error = got_repo_match_object_id(&commit_id, NULL,
5958 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5959 got_ref_list_free(&refs);
5962 error = add_branch(repo, argv[0], commit_id);
5965 if (worktree && do_update) {
5966 struct got_update_progress_arg upa;
5967 char *branch_refname = NULL;
5969 error = got_object_id_str(&commit_id_str, commit_id);
5972 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5976 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5978 error = got_error_from_errno("asprintf");
5981 error = got_ref_open(&ref, repo, branch_refname, 0);
5982 free(branch_refname);
5985 error = switch_head_ref(ref, commit_id, worktree,
5989 error = got_worktree_set_base_commit_id(worktree, repo,
5993 memset(&upa, 0, sizeof(upa));
5994 error = got_worktree_checkout_files(worktree, &paths,
5995 repo, update_progress, &upa, check_cancelled,
5999 if (upa.did_something)
6000 printf("Updated to commit %s\n", commit_id_str);
6001 print_update_progress_stats(&upa);
6008 const struct got_error *close_err = got_repo_close(repo);
6013 got_worktree_close(worktree);
6017 free(commit_id_str);
6018 TAILQ_FOREACH(pe, &paths, entry)
6019 free((char *)pe->path);
6020 got_pathlist_free(&paths);
6029 "usage: %s tag [-c commit] [-r repository] [-l] "
6030 "[-m message] name\n", getprogname());
6035 static const struct got_error *
6036 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6038 const struct got_error *err = NULL;
6039 struct got_reflist_entry *re, *se, *new;
6040 struct got_object_id *re_id, *se_id;
6041 struct got_tag_object *re_tag, *se_tag;
6042 time_t re_time, se_time;
6044 STAILQ_FOREACH(re, tags, entry) {
6045 se = STAILQ_FIRST(sorted);
6047 err = got_reflist_entry_dup(&new, re);
6050 STAILQ_INSERT_HEAD(sorted, new, entry);
6053 err = got_ref_resolve(&re_id, repo, re->ref);
6056 err = got_object_open_as_tag(&re_tag, repo, re_id);
6060 re_time = got_object_tag_get_tagger_time(re_tag);
6061 got_object_tag_close(re_tag);
6065 err = got_ref_resolve(&se_id, repo, re->ref);
6068 err = got_object_open_as_tag(&se_tag, repo, se_id);
6072 se_time = got_object_tag_get_tagger_time(se_tag);
6073 got_object_tag_close(se_tag);
6075 if (se_time > re_time) {
6076 err = got_reflist_entry_dup(&new, re);
6079 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6082 se = STAILQ_NEXT(se, entry);
6091 static const struct got_error *
6092 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6094 static const struct got_error *err = NULL;
6095 struct got_reflist_head refs;
6096 struct got_reflist_entry *re;
6100 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6104 TAILQ_FOREACH(re, &refs, entry) {
6105 const char *refname;
6106 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6110 struct got_object_id *id;
6111 struct got_tag_object *tag;
6112 struct got_commit_object *commit = NULL;
6114 refname = got_ref_get_name(re->ref);
6115 if (strncmp(refname, "refs/tags/", 10) != 0)
6118 refstr = got_ref_to_str(re->ref);
6119 if (refstr == NULL) {
6120 err = got_error_from_errno("got_ref_to_str");
6123 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6126 err = got_ref_resolve(&id, repo, re->ref);
6129 err = got_object_open_as_tag(&tag, repo, id);
6131 if (err->code != GOT_ERR_OBJ_TYPE) {
6135 /* "lightweight" tag */
6136 err = got_object_open_as_commit(&commit, repo, id);
6141 tagger = got_object_commit_get_committer(commit);
6143 got_object_commit_get_committer_time(commit);
6144 err = got_object_id_str(&id_str, id);
6150 tagger = got_object_tag_get_tagger(tag);
6151 tagger_time = got_object_tag_get_tagger_time(tag);
6152 err = got_object_id_str(&id_str,
6153 got_object_tag_get_object_id(tag));
6157 printf("from: %s\n", tagger);
6158 datestr = get_datestr(&tagger_time, datebuf);
6160 printf("date: %s UTC\n", datestr);
6162 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6164 switch (got_object_tag_get_object_type(tag)) {
6165 case GOT_OBJ_TYPE_BLOB:
6166 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6169 case GOT_OBJ_TYPE_TREE:
6170 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6173 case GOT_OBJ_TYPE_COMMIT:
6174 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6177 case GOT_OBJ_TYPE_TAG:
6178 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6187 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6190 got_object_commit_close(commit);
6192 tagmsg0 = strdup(got_object_tag_get_message(tag));
6193 got_object_tag_close(tag);
6194 if (tagmsg0 == NULL) {
6195 err = got_error_from_errno("strdup");
6202 line = strsep(&tagmsg, "\n");
6204 printf(" %s\n", line);
6209 got_ref_list_free(&refs);
6213 static const struct got_error *
6214 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6215 const char *tag_name, const char *repo_path)
6217 const struct got_error *err = NULL;
6218 char *template = NULL, *initial_content = NULL;
6219 char *editor = NULL;
6220 int initial_content_len;
6223 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6224 err = got_error_from_errno("asprintf");
6228 initial_content_len = asprintf(&initial_content,
6229 "\n# tagging commit %s as %s\n",
6230 commit_id_str, tag_name);
6231 if (initial_content_len == -1) {
6232 err = got_error_from_errno("asprintf");
6236 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6240 if (write(fd, initial_content, initial_content_len) == -1) {
6241 err = got_error_from_errno2("write", *tagmsg_path);
6245 err = get_editor(&editor);
6248 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6249 initial_content_len, 1);
6251 free(initial_content);
6255 if (fd != -1 && close(fd) == -1 && err == NULL)
6256 err = got_error_from_errno2("close", *tagmsg_path);
6258 /* Editor is done; we can now apply unveil(2) */
6260 err = apply_unveil(repo_path, 0, NULL);
6268 static const struct got_error *
6269 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6270 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6272 const struct got_error *err = NULL;
6273 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6274 char *label = NULL, *commit_id_str = NULL;
6275 struct got_reference *ref = NULL;
6276 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6277 char *tagmsg_path = NULL, *tag_id_str = NULL;
6278 int preserve_tagmsg = 0;
6279 struct got_reflist_head refs;
6284 * Don't let the user create a tag name with a leading '-'.
6285 * While technically a valid reference name, this case is usually
6286 * an unintended typo.
6288 if (tag_name[0] == '-')
6289 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6291 err = get_author(&tagger, repo, worktree);
6295 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6299 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6300 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6304 err = got_object_id_str(&commit_id_str, commit_id);
6308 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6309 refname = strdup(tag_name);
6310 if (refname == NULL) {
6311 err = got_error_from_errno("strdup");
6315 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6316 err = got_error_from_errno("asprintf");
6320 err = got_ref_open(&ref, repo, refname, 0);
6322 err = got_error(GOT_ERR_TAG_EXISTS);
6324 } else if (err->code != GOT_ERR_NOT_REF)
6327 if (tagmsg_arg == NULL) {
6328 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6329 tag_name, got_repo_get_path(repo));
6331 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6332 tagmsg_path != NULL)
6333 preserve_tagmsg = 1;
6338 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6339 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6342 preserve_tagmsg = 1;
6346 err = got_ref_alloc(&ref, refname, tag_id);
6349 preserve_tagmsg = 1;
6353 err = got_ref_write(ref, repo);
6356 preserve_tagmsg = 1;
6360 err = got_object_id_str(&tag_id_str, tag_id);
6363 preserve_tagmsg = 1;
6366 printf("Created tag %s\n", tag_id_str);
6368 if (preserve_tagmsg) {
6369 fprintf(stderr, "%s: tag message preserved in %s\n",
6370 getprogname(), tagmsg_path);
6371 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6372 err = got_error_from_errno2("unlink", tagmsg_path);
6377 free(commit_id_str);
6382 got_ref_list_free(&refs);
6386 static const struct got_error *
6387 cmd_tag(int argc, char *argv[])
6389 const struct got_error *error = NULL;
6390 struct got_repository *repo = NULL;
6391 struct got_worktree *worktree = NULL;
6392 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6393 char *gitconfig_path = NULL;
6394 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6395 int ch, do_list = 0;
6397 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6400 commit_id_arg = optarg;
6406 repo_path = realpath(optarg, NULL);
6407 if (repo_path == NULL)
6408 return got_error_from_errno2("realpath",
6410 got_path_strip_trailing_slashes(repo_path);
6425 if (commit_id_arg != NULL)
6427 "-c option can only be used when creating a tag");
6429 option_conflict('l', 'm');
6432 } else if (argc != 1)
6439 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6443 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6444 "sendfd unveil", NULL) == -1)
6448 cwd = getcwd(NULL, 0);
6450 error = got_error_from_errno("getcwd");
6454 if (repo_path == NULL) {
6455 error = got_worktree_open(&worktree, cwd);
6456 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6462 strdup(got_worktree_get_repo_path(worktree));
6463 if (repo_path == NULL)
6464 error = got_error_from_errno("strdup");
6468 repo_path = strdup(cwd);
6469 if (repo_path == NULL) {
6470 error = got_error_from_errno("strdup");
6477 error = got_repo_open(&repo, repo_path, NULL);
6480 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6483 error = list_tags(repo, worktree);
6485 error = get_gitconfig_path(&gitconfig_path);
6488 error = got_repo_open(&repo, repo_path, gitconfig_path);
6493 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6498 if (commit_id_arg == NULL) {
6499 struct got_reference *head_ref;
6500 struct got_object_id *commit_id;
6501 error = got_ref_open(&head_ref, repo,
6502 worktree ? got_worktree_get_head_ref_name(worktree)
6506 error = got_ref_resolve(&commit_id, repo, head_ref);
6507 got_ref_close(head_ref);
6510 error = got_object_id_str(&commit_id_str, commit_id);
6516 error = add_tag(repo, worktree, tag_name,
6517 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6521 const struct got_error *close_err = got_repo_close(repo);
6526 got_worktree_close(worktree);
6529 free(gitconfig_path);
6530 free(commit_id_str);
6537 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6542 static const struct got_error *
6543 add_progress(void *arg, unsigned char status, const char *path)
6545 while (path[0] == '/')
6547 printf("%c %s\n", status, path);
6551 static const struct got_error *
6552 cmd_add(int argc, char *argv[])
6554 const struct got_error *error = NULL;
6555 struct got_repository *repo = NULL;
6556 struct got_worktree *worktree = NULL;
6558 struct got_pathlist_head paths;
6559 struct got_pathlist_entry *pe;
6560 int ch, can_recurse = 0, no_ignores = 0;
6564 while ((ch = getopt(argc, argv, "IR")) != -1) {
6582 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6589 cwd = getcwd(NULL, 0);
6591 error = got_error_from_errno("getcwd");
6595 error = got_worktree_open(&worktree, cwd);
6597 if (error->code == GOT_ERR_NOT_WORKTREE)
6598 error = wrap_not_worktree_error(error, "add", cwd);
6602 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6607 error = apply_unveil(got_repo_get_path(repo), 1,
6608 got_worktree_get_root_path(worktree));
6612 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6619 TAILQ_FOREACH(pe, &paths, entry) {
6620 if (asprintf(&ondisk_path, "%s/%s",
6621 got_worktree_get_root_path(worktree),
6623 error = got_error_from_errno("asprintf");
6626 if (lstat(ondisk_path, &sb) == -1) {
6627 if (errno == ENOENT) {
6631 error = got_error_from_errno2("lstat",
6637 if (S_ISDIR(sb.st_mode)) {
6638 error = got_error_msg(GOT_ERR_BAD_PATH,
6639 "adding directories requires -R option");
6645 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6646 NULL, repo, no_ignores);
6649 const struct got_error *close_err = got_repo_close(repo);
6654 got_worktree_close(worktree);
6655 TAILQ_FOREACH(pe, &paths, entry)
6656 free((char *)pe->path);
6657 got_pathlist_free(&paths);
6665 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6666 "path ...\n", getprogname());
6670 static const struct got_error *
6671 print_remove_status(void *arg, unsigned char status,
6672 unsigned char staged_status, const char *path)
6674 while (path[0] == '/')
6676 if (status == GOT_STATUS_NONEXISTENT)
6678 if (status == staged_status && (status == GOT_STATUS_DELETE))
6679 status = GOT_STATUS_NO_CHANGE;
6680 printf("%c%c %s\n", status, staged_status, path);
6684 static const struct got_error *
6685 cmd_remove(int argc, char *argv[])
6687 const struct got_error *error = NULL;
6688 struct got_worktree *worktree = NULL;
6689 struct got_repository *repo = NULL;
6690 const char *status_codes = NULL;
6692 struct got_pathlist_head paths;
6693 struct got_pathlist_entry *pe;
6694 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6698 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6701 delete_local_mods = 1;
6710 for (i = 0; i < strlen(optarg); i++) {
6711 switch (optarg[i]) {
6712 case GOT_STATUS_MODIFY:
6713 delete_local_mods = 1;
6715 case GOT_STATUS_MISSING:
6718 errx(1, "invalid status code '%c'",
6722 status_codes = optarg;
6734 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6741 cwd = getcwd(NULL, 0);
6743 error = got_error_from_errno("getcwd");
6746 error = got_worktree_open(&worktree, cwd);
6748 if (error->code == GOT_ERR_NOT_WORKTREE)
6749 error = wrap_not_worktree_error(error, "remove", cwd);
6753 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6758 error = apply_unveil(got_repo_get_path(repo), 1,
6759 got_worktree_get_root_path(worktree));
6763 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6770 TAILQ_FOREACH(pe, &paths, entry) {
6771 if (asprintf(&ondisk_path, "%s/%s",
6772 got_worktree_get_root_path(worktree),
6774 error = got_error_from_errno("asprintf");
6777 if (lstat(ondisk_path, &sb) == -1) {
6778 if (errno == ENOENT) {
6782 error = got_error_from_errno2("lstat",
6788 if (S_ISDIR(sb.st_mode)) {
6789 error = got_error_msg(GOT_ERR_BAD_PATH,
6790 "removing directories requires -R option");
6796 error = got_worktree_schedule_delete(worktree, &paths,
6797 delete_local_mods, status_codes, print_remove_status, NULL,
6798 repo, keep_on_disk);
6801 const struct got_error *close_err = got_repo_close(repo);
6806 got_worktree_close(worktree);
6807 TAILQ_FOREACH(pe, &paths, entry)
6808 free((char *)pe->path);
6809 got_pathlist_free(&paths);
6817 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6818 "path ...\n", getprogname());
6822 static const struct got_error *
6823 revert_progress(void *arg, unsigned char status, const char *path)
6825 if (status == GOT_STATUS_UNVERSIONED)
6828 while (path[0] == '/')
6830 printf("%c %s\n", status, path);
6834 struct choose_patch_arg {
6835 FILE *patch_script_file;
6839 static const struct got_error *
6840 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6841 int nchanges, const char *action)
6844 size_t linesize = 0;
6848 case GOT_STATUS_ADD:
6849 printf("A %s\n%s this addition? [y/n] ", path, action);
6851 case GOT_STATUS_DELETE:
6852 printf("D %s\n%s this deletion? [y/n] ", path, action);
6854 case GOT_STATUS_MODIFY:
6855 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6856 return got_error_from_errno("fseek");
6857 printf(GOT_COMMIT_SEP_STR);
6858 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6860 if (ferror(patch_file))
6861 return got_error_from_errno("getline");
6862 printf(GOT_COMMIT_SEP_STR);
6863 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6864 path, n, nchanges, action);
6867 return got_error_path(path, GOT_ERR_FILE_STATUS);
6873 static const struct got_error *
6874 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6875 FILE *patch_file, int n, int nchanges)
6877 const struct got_error *err = NULL;
6879 size_t linesize = 0;
6882 struct choose_patch_arg *a = arg;
6884 *choice = GOT_PATCH_CHOICE_NONE;
6886 if (a->patch_script_file) {
6888 err = show_change(status, path, patch_file, n, nchanges,
6892 linelen = getline(&line, &linesize, a->patch_script_file);
6893 if (linelen == -1) {
6894 if (ferror(a->patch_script_file))
6895 return got_error_from_errno("getline");
6898 nl = strchr(line, '\n');
6901 if (strcmp(line, "y") == 0) {
6902 *choice = GOT_PATCH_CHOICE_YES;
6904 } else if (strcmp(line, "n") == 0) {
6905 *choice = GOT_PATCH_CHOICE_NO;
6907 } else if (strcmp(line, "q") == 0 &&
6908 status == GOT_STATUS_MODIFY) {
6909 *choice = GOT_PATCH_CHOICE_QUIT;
6912 printf("invalid response '%s'\n", line);
6917 while (resp != 'y' && resp != 'n' && resp != 'q') {
6918 err = show_change(status, path, patch_file, n, nchanges,
6925 if (status == GOT_STATUS_MODIFY) {
6926 if (resp != 'y' && resp != 'n' && resp != 'q') {
6927 printf("invalid response '%c'\n", resp);
6930 } else if (resp != 'y' && resp != 'n') {
6931 printf("invalid response '%c'\n", resp);
6937 *choice = GOT_PATCH_CHOICE_YES;
6938 else if (resp == 'n')
6939 *choice = GOT_PATCH_CHOICE_NO;
6940 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6941 *choice = GOT_PATCH_CHOICE_QUIT;
6947 static const struct got_error *
6948 cmd_revert(int argc, char *argv[])
6950 const struct got_error *error = NULL;
6951 struct got_worktree *worktree = NULL;
6952 struct got_repository *repo = NULL;
6953 char *cwd = NULL, *path = NULL;
6954 struct got_pathlist_head paths;
6955 struct got_pathlist_entry *pe;
6956 int ch, can_recurse = 0, pflag = 0;
6957 FILE *patch_script_file = NULL;
6958 const char *patch_script_path = NULL;
6959 struct choose_patch_arg cpa;
6963 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6969 patch_script_path = optarg;
6984 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6985 "unveil", NULL) == -1)
6990 if (patch_script_path && !pflag)
6991 errx(1, "-F option can only be used together with -p option");
6993 cwd = getcwd(NULL, 0);
6995 error = got_error_from_errno("getcwd");
6998 error = got_worktree_open(&worktree, cwd);
7000 if (error->code == GOT_ERR_NOT_WORKTREE)
7001 error = wrap_not_worktree_error(error, "revert", cwd);
7005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7010 if (patch_script_path) {
7011 patch_script_file = fopen(patch_script_path, "r");
7012 if (patch_script_file == NULL) {
7013 error = got_error_from_errno2("fopen",
7018 error = apply_unveil(got_repo_get_path(repo), 1,
7019 got_worktree_get_root_path(worktree));
7023 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7030 TAILQ_FOREACH(pe, &paths, entry) {
7031 if (asprintf(&ondisk_path, "%s/%s",
7032 got_worktree_get_root_path(worktree),
7034 error = got_error_from_errno("asprintf");
7037 if (lstat(ondisk_path, &sb) == -1) {
7038 if (errno == ENOENT) {
7042 error = got_error_from_errno2("lstat",
7048 if (S_ISDIR(sb.st_mode)) {
7049 error = got_error_msg(GOT_ERR_BAD_PATH,
7050 "reverting directories requires -R option");
7056 cpa.patch_script_file = patch_script_file;
7057 cpa.action = "revert";
7058 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7059 pflag ? choose_patch : NULL, &cpa, repo);
7061 if (patch_script_file && fclose(patch_script_file) == EOF &&
7063 error = got_error_from_errno2("fclose", patch_script_path);
7065 const struct got_error *close_err = got_repo_close(repo);
7070 got_worktree_close(worktree);
7079 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7080 "[path ...]\n", getprogname());
7084 struct collect_commit_logmsg_arg {
7085 const char *cmdline_log;
7086 const char *prepared_log;
7087 int non_interactive;
7089 const char *worktree_path;
7090 const char *branch_name;
7091 const char *repo_path;
7096 static const struct got_error *
7097 read_prepared_logmsg(char **logmsg, const char *path)
7099 const struct got_error *err = NULL;
7105 memset(&sb, 0, sizeof(sb));
7107 f = fopen(path, "r");
7109 return got_error_from_errno2("fopen", path);
7111 if (fstat(fileno(f), &sb) == -1) {
7112 err = got_error_from_errno2("fstat", path);
7115 if (sb.st_size == 0) {
7116 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7120 *logmsg = malloc(sb.st_size + 1);
7121 if (*logmsg == NULL) {
7122 err = got_error_from_errno("malloc");
7126 r = fread(*logmsg, 1, sb.st_size, f);
7127 if (r != sb.st_size) {
7129 err = got_error_from_errno2("fread", path);
7131 err = got_error(GOT_ERR_IO);
7134 (*logmsg)[sb.st_size] = '\0';
7136 if (fclose(f) == EOF && err == NULL)
7137 err = got_error_from_errno2("fclose", path);
7146 static const struct got_error *
7147 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7150 char *initial_content = NULL;
7151 struct got_pathlist_entry *pe;
7152 const struct got_error *err = NULL;
7153 char *template = NULL;
7154 struct collect_commit_logmsg_arg *a = arg;
7155 int initial_content_len;
7159 /* if a message was specified on the command line, just use it */
7160 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7161 len = strlen(a->cmdline_log) + 1;
7162 *logmsg = malloc(len + 1);
7163 if (*logmsg == NULL)
7164 return got_error_from_errno("malloc");
7165 strlcpy(*logmsg, a->cmdline_log, len);
7167 } else if (a->prepared_log != NULL && a->non_interactive)
7168 return read_prepared_logmsg(logmsg, a->prepared_log);
7170 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7171 return got_error_from_errno("asprintf");
7173 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7177 if (a->prepared_log) {
7179 err = read_prepared_logmsg(&msg, a->prepared_log);
7182 if (write(fd, msg, strlen(msg)) == -1) {
7183 err = got_error_from_errno2("write", a->logmsg_path);
7190 initial_content_len = asprintf(&initial_content,
7191 "\n# changes to be committed on branch %s:\n",
7193 if (initial_content_len == -1) {
7194 err = got_error_from_errno("asprintf");
7198 if (write(fd, initial_content, initial_content_len) == -1) {
7199 err = got_error_from_errno2("write", a->logmsg_path);
7203 TAILQ_FOREACH(pe, commitable_paths, entry) {
7204 struct got_commitable *ct = pe->data;
7205 dprintf(fd, "# %c %s\n",
7206 got_commitable_get_status(ct),
7207 got_commitable_get_path(ct));
7210 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7211 initial_content_len, a->prepared_log ? 0 : 1);
7213 free(initial_content);
7216 if (fd != -1 && close(fd) == -1 && err == NULL)
7217 err = got_error_from_errno2("close", a->logmsg_path);
7219 /* Editor is done; we can now apply unveil(2) */
7221 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7229 static const struct got_error *
7230 cmd_commit(int argc, char *argv[])
7232 const struct got_error *error = NULL;
7233 struct got_worktree *worktree = NULL;
7234 struct got_repository *repo = NULL;
7235 char *cwd = NULL, *id_str = NULL;
7236 struct got_object_id *id = NULL;
7237 const char *logmsg = NULL;
7238 char *prepared_logmsg = NULL;
7239 struct collect_commit_logmsg_arg cl_arg;
7240 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7241 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7242 int allow_bad_symlinks = 0, non_interactive = 0;
7243 struct got_pathlist_head paths;
7246 cl_arg.logmsg_path = NULL;
7248 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7252 option_conflict('F', 'm');
7253 prepared_logmsg = realpath(optarg, NULL);
7254 if (prepared_logmsg == NULL)
7255 return got_error_from_errno2("realpath",
7259 if (prepared_logmsg)
7260 option_conflict('m', 'F');
7264 non_interactive = 1;
7267 allow_bad_symlinks = 1;
7279 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7280 "unveil", NULL) == -1)
7283 cwd = getcwd(NULL, 0);
7285 error = got_error_from_errno("getcwd");
7288 error = got_worktree_open(&worktree, cwd);
7290 if (error->code == GOT_ERR_NOT_WORKTREE)
7291 error = wrap_not_worktree_error(error, "commit", cwd);
7295 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7298 if (rebase_in_progress) {
7299 error = got_error(GOT_ERR_REBASING);
7303 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7308 error = get_gitconfig_path(&gitconfig_path);
7311 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7316 error = get_author(&author, repo, worktree);
7321 * unveil(2) traverses exec(2); if an editor is used we have
7322 * to apply unveil after the log message has been written.
7324 if (logmsg == NULL || strlen(logmsg) == 0)
7325 error = get_editor(&editor);
7327 error = apply_unveil(got_repo_get_path(repo), 0,
7328 got_worktree_get_root_path(worktree));
7332 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7336 cl_arg.editor = editor;
7337 cl_arg.cmdline_log = logmsg;
7338 cl_arg.prepared_log = prepared_logmsg;
7339 cl_arg.non_interactive = non_interactive;
7340 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7341 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7342 if (!histedit_in_progress) {
7343 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7344 error = got_error(GOT_ERR_COMMIT_BRANCH);
7347 cl_arg.branch_name += 11;
7349 cl_arg.repo_path = got_repo_get_path(repo);
7350 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7351 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7352 print_status, NULL, repo);
7354 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7355 cl_arg.logmsg_path != NULL)
7356 preserve_logmsg = 1;
7360 error = got_object_id_str(&id_str, id);
7363 printf("Created commit %s\n", id_str);
7365 if (preserve_logmsg) {
7366 fprintf(stderr, "%s: log message preserved in %s\n",
7367 getprogname(), cl_arg.logmsg_path);
7368 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7370 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7371 free(cl_arg.logmsg_path);
7373 const struct got_error *close_err = got_repo_close(repo);
7378 got_worktree_close(worktree);
7381 free(gitconfig_path);
7384 free(prepared_logmsg);
7391 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7392 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7393 "[remote-repository]\n", getprogname());
7397 struct got_send_progress_arg {
7398 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7401 int last_nobj_total;
7405 int printed_something;
7407 struct got_pathlist_head *delete_branches;
7410 static const struct got_error *
7411 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7412 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7415 struct got_send_progress_arg *a = arg;
7416 char scaled_packsize[FMT_SCALED_STRSIZE];
7417 char scaled_sent[FMT_SCALED_STRSIZE];
7418 int p_deltify = 0, p_written = 0, p_sent = 0;
7419 int print_searching = 0, print_total = 0;
7420 int print_deltify = 0, print_written = 0, print_sent = 0;
7422 if (a->verbosity < 0)
7426 const char *status = success ? "accepted" : "rejected";
7429 struct got_pathlist_entry *pe;
7430 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7431 const char *branchname = pe->path;
7432 if (got_path_cmp(branchname, refname,
7433 strlen(branchname), strlen(refname)) == 0) {
7440 printf("\nServer has %s %s", status, refname);
7441 a->printed_something = 1;
7445 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7446 return got_error_from_errno("fmt_scaled");
7447 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7448 return got_error_from_errno("fmt_scaled");
7450 if (a->last_ncommits != ncommits) {
7451 print_searching = 1;
7452 a->last_ncommits = ncommits;
7455 if (a->last_nobj_total != nobj_total) {
7456 print_searching = 1;
7458 a->last_nobj_total = nobj_total;
7461 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7462 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7463 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7464 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7465 return got_error(GOT_ERR_NO_SPACE);
7468 if (nobj_deltify > 0 || nobj_written > 0) {
7469 if (nobj_deltify > 0) {
7470 p_deltify = (nobj_deltify * 100) / nobj_total;
7471 if (p_deltify != a->last_p_deltify) {
7472 a->last_p_deltify = p_deltify;
7473 print_searching = 1;
7478 if (nobj_written > 0) {
7479 p_written = (nobj_written * 100) / nobj_total;
7480 if (p_written != a->last_p_written) {
7481 a->last_p_written = p_written;
7482 print_searching = 1;
7490 if (bytes_sent > 0) {
7491 p_sent = (bytes_sent * 100) / packfile_size;
7492 if (p_sent != a->last_p_sent) {
7493 a->last_p_sent = p_sent;
7494 print_searching = 1;
7500 a->sent_something = 1;
7503 if (print_searching || print_total || print_deltify || print_written ||
7506 if (print_searching)
7507 printf("packing %d reference%s", ncommits,
7508 ncommits == 1 ? "" : "s");
7510 printf("; %d object%s", nobj_total,
7511 nobj_total == 1 ? "" : "s");
7513 printf("; deltify: %d%%", p_deltify);
7515 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7516 scaled_packsize, p_sent);
7517 else if (print_written)
7518 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7519 scaled_packsize, p_written);
7520 if (print_searching || print_total || print_deltify ||
7521 print_written || print_sent) {
7522 a->printed_something = 1;
7528 static const struct got_error *
7529 cmd_send(int argc, char *argv[])
7531 const struct got_error *error = NULL;
7532 char *cwd = NULL, *repo_path = NULL;
7533 const char *remote_name;
7534 char *proto = NULL, *host = NULL, *port = NULL;
7535 char *repo_name = NULL, *server_path = NULL;
7536 const struct got_remote_repo *remotes, *remote = NULL;
7537 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7538 struct got_repository *repo = NULL;
7539 struct got_worktree *worktree = NULL;
7540 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7541 struct got_pathlist_head branches;
7542 struct got_pathlist_head tags;
7543 struct got_reflist_head all_branches;
7544 struct got_reflist_head all_tags;
7545 struct got_pathlist_head delete_args;
7546 struct got_pathlist_head delete_branches;
7547 struct got_reflist_entry *re;
7548 struct got_pathlist_entry *pe;
7549 int i, ch, sendfd = -1, sendstatus;
7551 struct got_send_progress_arg spa;
7552 int verbosity = 0, overwrite_refs = 0;
7553 int send_all_branches = 0, send_all_tags = 0;
7554 struct got_reference *ref = NULL;
7556 TAILQ_INIT(&branches);
7558 TAILQ_INIT(&all_branches);
7559 TAILQ_INIT(&all_tags);
7560 TAILQ_INIT(&delete_args);
7561 TAILQ_INIT(&delete_branches);
7563 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7566 send_all_branches = 1;
7569 error = got_pathlist_append(&branches, optarg, NULL);
7575 error = got_pathlist_append(&delete_args, optarg, NULL);
7583 repo_path = realpath(optarg, NULL);
7584 if (repo_path == NULL)
7585 return got_error_from_errno2("realpath",
7587 got_path_strip_trailing_slashes(repo_path);
7590 error = got_pathlist_append(&tags, optarg, NULL);
7601 else if (verbosity < 3)
7615 if (send_all_branches && !TAILQ_EMPTY(&branches))
7616 option_conflict('a', 'b');
7617 if (send_all_tags && !TAILQ_EMPTY(&tags))
7618 option_conflict('T', 't');
7622 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7624 remote_name = argv[0];
7628 cwd = getcwd(NULL, 0);
7630 error = got_error_from_errno("getcwd");
7634 if (repo_path == NULL) {
7635 error = got_worktree_open(&worktree, cwd);
7636 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7642 strdup(got_worktree_get_repo_path(worktree));
7643 if (repo_path == NULL)
7644 error = got_error_from_errno("strdup");
7648 repo_path = strdup(cwd);
7649 if (repo_path == NULL) {
7650 error = got_error_from_errno("strdup");
7656 error = got_repo_open(&repo, repo_path, NULL);
7661 worktree_conf = got_worktree_get_gotconfig(worktree);
7662 if (worktree_conf) {
7663 got_gotconfig_get_remotes(&nremotes, &remotes,
7665 for (i = 0; i < nremotes; i++) {
7666 if (strcmp(remotes[i].name, remote_name) == 0) {
7667 remote = &remotes[i];
7673 if (remote == NULL) {
7674 repo_conf = got_repo_get_gotconfig(repo);
7676 got_gotconfig_get_remotes(&nremotes, &remotes,
7678 for (i = 0; i < nremotes; i++) {
7679 if (strcmp(remotes[i].name, remote_name) == 0) {
7680 remote = &remotes[i];
7686 if (remote == NULL) {
7687 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7688 for (i = 0; i < nremotes; i++) {
7689 if (strcmp(remotes[i].name, remote_name) == 0) {
7690 remote = &remotes[i];
7695 if (remote == NULL) {
7696 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7700 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
7701 &repo_name, remote->url);
7705 if (strcmp(proto, "git") == 0) {
7707 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7708 "sendfd dns inet unveil", NULL) == -1)
7711 } else if (strcmp(proto, "git+ssh") == 0 ||
7712 strcmp(proto, "ssh") == 0) {
7714 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7715 "sendfd unveil", NULL) == -1)
7718 } else if (strcmp(proto, "http") == 0 ||
7719 strcmp(proto, "git+http") == 0) {
7720 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7723 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7727 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
7728 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
7729 error = got_error_from_errno2("unveil",
7730 GOT_FETCH_PATH_SSH);
7734 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7738 if (send_all_branches) {
7739 error = got_ref_list(&all_branches, repo, "refs/heads",
7740 got_ref_cmp_by_name, NULL);
7743 TAILQ_FOREACH(re, &all_branches, entry) {
7744 const char *branchname = got_ref_get_name(re->ref);
7745 error = got_pathlist_append(&branches,
7753 if (send_all_tags) {
7754 error = got_ref_list(&all_tags, repo, "refs/tags",
7755 got_ref_cmp_by_name, NULL);
7758 TAILQ_FOREACH(re, &all_tags, entry) {
7759 const char *tagname = got_ref_get_name(re->ref);
7760 error = got_pathlist_append(&tags,
7769 * To prevent accidents only branches in refs/heads/ can be deleted
7770 * with 'got send -d'.
7771 * Deleting anything else requires local repository access or Git.
7773 TAILQ_FOREACH(pe, &delete_args, entry) {
7774 const char *branchname = pe->path;
7776 struct got_pathlist_entry *new;
7777 if (strncmp(branchname, "refs/heads/", 11) == 0) {
7778 s = strdup(branchname);
7780 error = got_error_from_errno("strdup");
7784 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7785 error = got_error_from_errno("asprintf");
7789 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7790 if (error || new == NULL /* duplicate */)
7797 if (nbranches == 0 && ndelete_branches == 0) {
7798 struct got_reference *head_ref;
7800 error = got_ref_open(&head_ref, repo,
7801 got_worktree_get_head_ref_name(worktree), 0);
7803 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7806 if (got_ref_is_symbolic(head_ref)) {
7807 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7808 got_ref_close(head_ref);
7813 error = got_pathlist_append(&branches, got_ref_get_name(ref),
7821 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7822 port ? ":" : "", port ? port : "");
7824 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7825 server_path, verbosity);
7829 memset(&spa, 0, sizeof(spa));
7830 spa.last_scaled_packsize[0] = '\0';
7831 spa.last_p_deltify = -1;
7832 spa.last_p_written = -1;
7833 spa.verbosity = verbosity;
7834 spa.delete_branches = &delete_branches;
7835 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7836 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7837 check_cancelled, NULL);
7838 if (spa.printed_something)
7842 if (!spa.sent_something && verbosity >= 0)
7843 printf("Already up-to-date\n");
7846 if (kill(sendpid, SIGTERM) == -1)
7847 error = got_error_from_errno("kill");
7848 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7849 error = got_error_from_errno("waitpid");
7851 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7852 error = got_error_from_errno("close");
7854 const struct got_error *close_err = got_repo_close(repo);
7859 got_worktree_close(worktree);
7862 got_pathlist_free(&branches);
7863 got_pathlist_free(&tags);
7864 got_ref_list_free(&all_branches);
7865 got_ref_list_free(&all_tags);
7866 got_pathlist_free(&delete_args);
7867 TAILQ_FOREACH(pe, &delete_branches, entry)
7868 free((char *)pe->path);
7869 got_pathlist_free(&delete_branches);
7881 usage_cherrypick(void)
7883 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7887 static const struct got_error *
7888 cmd_cherrypick(int argc, char *argv[])
7890 const struct got_error *error = NULL;
7891 struct got_worktree *worktree = NULL;
7892 struct got_repository *repo = NULL;
7893 char *cwd = NULL, *commit_id_str = NULL;
7894 struct got_object_id *commit_id = NULL;
7895 struct got_commit_object *commit = NULL;
7896 struct got_object_qid *pid;
7897 struct got_reference *head_ref = NULL;
7899 struct got_update_progress_arg upa;
7901 while ((ch = getopt(argc, argv, "")) != -1) {
7913 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7914 "unveil", NULL) == -1)
7920 cwd = getcwd(NULL, 0);
7922 error = got_error_from_errno("getcwd");
7925 error = got_worktree_open(&worktree, cwd);
7927 if (error->code == GOT_ERR_NOT_WORKTREE)
7928 error = wrap_not_worktree_error(error, "cherrypick",
7933 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7938 error = apply_unveil(got_repo_get_path(repo), 0,
7939 got_worktree_get_root_path(worktree));
7943 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7944 GOT_OBJ_TYPE_COMMIT, repo);
7945 if (error != NULL) {
7946 struct got_reference *ref;
7947 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7949 error = got_ref_open(&ref, repo, argv[0], 0);
7952 error = got_ref_resolve(&commit_id, repo, ref);
7957 error = got_object_id_str(&commit_id_str, commit_id);
7961 error = got_ref_open(&head_ref, repo,
7962 got_worktree_get_head_ref_name(worktree), 0);
7966 error = check_same_branch(commit_id, head_ref, NULL, repo);
7968 if (error->code != GOT_ERR_ANCESTRY)
7972 error = got_error(GOT_ERR_SAME_BRANCH);
7976 error = got_object_open_as_commit(&commit, repo, commit_id);
7979 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7980 memset(&upa, 0, sizeof(upa));
7981 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7982 commit_id, repo, update_progress, &upa, check_cancelled,
7987 if (upa.did_something)
7988 printf("Merged commit %s\n", commit_id_str);
7989 print_update_progress_stats(&upa);
7992 got_object_commit_close(commit);
7993 free(commit_id_str);
7995 got_ref_close(head_ref);
7997 got_worktree_close(worktree);
7999 const struct got_error *close_err = got_repo_close(repo);
8009 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8013 static const struct got_error *
8014 cmd_backout(int argc, char *argv[])
8016 const struct got_error *error = NULL;
8017 struct got_worktree *worktree = NULL;
8018 struct got_repository *repo = NULL;
8019 char *cwd = NULL, *commit_id_str = NULL;
8020 struct got_object_id *commit_id = NULL;
8021 struct got_commit_object *commit = NULL;
8022 struct got_object_qid *pid;
8023 struct got_reference *head_ref = NULL;
8025 struct got_update_progress_arg upa;
8027 while ((ch = getopt(argc, argv, "")) != -1) {
8039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8040 "unveil", NULL) == -1)
8046 cwd = getcwd(NULL, 0);
8048 error = got_error_from_errno("getcwd");
8051 error = got_worktree_open(&worktree, cwd);
8053 if (error->code == GOT_ERR_NOT_WORKTREE)
8054 error = wrap_not_worktree_error(error, "backout", cwd);
8058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8063 error = apply_unveil(got_repo_get_path(repo), 0,
8064 got_worktree_get_root_path(worktree));
8068 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8069 GOT_OBJ_TYPE_COMMIT, repo);
8070 if (error != NULL) {
8071 struct got_reference *ref;
8072 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8074 error = got_ref_open(&ref, repo, argv[0], 0);
8077 error = got_ref_resolve(&commit_id, repo, ref);
8082 error = got_object_id_str(&commit_id_str, commit_id);
8086 error = got_ref_open(&head_ref, repo,
8087 got_worktree_get_head_ref_name(worktree), 0);
8091 error = check_same_branch(commit_id, head_ref, NULL, repo);
8095 error = got_object_open_as_commit(&commit, repo, commit_id);
8098 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8100 error = got_error(GOT_ERR_ROOT_COMMIT);
8104 memset(&upa, 0, sizeof(upa));
8105 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8106 update_progress, &upa, check_cancelled, NULL);
8110 if (upa.did_something)
8111 printf("Backed out commit %s\n", commit_id_str);
8112 print_update_progress_stats(&upa);
8115 got_object_commit_close(commit);
8116 free(commit_id_str);
8118 got_ref_close(head_ref);
8120 got_worktree_close(worktree);
8122 const struct got_error *close_err = got_repo_close(repo);
8132 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8138 trim_logmsg(char *logmsg, int limit)
8143 len = strlen(logmsg);
8147 nl = strchr(logmsg, '\n');
8152 static const struct got_error *
8153 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8155 const struct got_error *err;
8156 char *logmsg0 = NULL;
8159 err = got_object_commit_get_logmsg(&logmsg0, commit);
8164 while (isspace((unsigned char)s[0]))
8167 *logmsg = strdup(s);
8168 if (*logmsg == NULL) {
8169 err = got_error_from_errno("strdup");
8173 trim_logmsg(*logmsg, limit);
8179 static const struct got_error *
8180 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8182 const struct got_error *err;
8183 struct got_commit_object *commit = NULL;
8184 char *id_str = NULL, *logmsg = NULL;
8186 err = got_object_open_as_commit(&commit, repo, id);
8190 err = got_object_id_str(&id_str, id);
8196 err = get_short_logmsg(&logmsg, 42, commit);
8200 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8203 got_object_commit_close(commit);
8208 static const struct got_error *
8209 show_rebase_progress(struct got_commit_object *commit,
8210 struct got_object_id *old_id, struct got_object_id *new_id)
8212 const struct got_error *err;
8213 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8215 err = got_object_id_str(&old_id_str, old_id);
8220 err = got_object_id_str(&new_id_str, new_id);
8225 old_id_str[12] = '\0';
8227 new_id_str[12] = '\0';
8229 err = get_short_logmsg(&logmsg, 42, commit);
8233 printf("%s -> %s: %s\n", old_id_str,
8234 new_id_str ? new_id_str : "no-op change", logmsg);
8242 static const struct got_error *
8243 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8244 struct got_reference *branch, struct got_reference *new_base_branch,
8245 struct got_reference *tmp_branch, struct got_repository *repo,
8248 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8249 return got_worktree_rebase_complete(worktree, fileindex,
8250 new_base_branch, tmp_branch, branch, repo, create_backup);
8253 static const struct got_error *
8254 rebase_commit(struct got_pathlist_head *merged_paths,
8255 struct got_worktree *worktree, struct got_fileindex *fileindex,
8256 struct got_reference *tmp_branch,
8257 struct got_object_id *commit_id, struct got_repository *repo)
8259 const struct got_error *error;
8260 struct got_commit_object *commit;
8261 struct got_object_id *new_commit_id;
8263 error = got_object_open_as_commit(&commit, repo, commit_id);
8267 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8268 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8270 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8272 error = show_rebase_progress(commit, commit_id, NULL);
8274 error = show_rebase_progress(commit, commit_id, new_commit_id);
8275 free(new_commit_id);
8278 got_object_commit_close(commit);
8282 struct check_path_prefix_arg {
8283 const char *path_prefix;
8288 static const struct got_error *
8289 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8290 struct got_blob_object *blob2, struct got_object_id *id1,
8291 struct got_object_id *id2, const char *path1, const char *path2,
8292 mode_t mode1, mode_t mode2, struct got_repository *repo)
8294 struct check_path_prefix_arg *a = arg;
8296 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8297 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8298 return got_error(a->errcode);
8303 static const struct got_error *
8304 check_path_prefix(struct got_object_id *parent_id,
8305 struct got_object_id *commit_id, const char *path_prefix,
8306 int errcode, struct got_repository *repo)
8308 const struct got_error *err;
8309 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8310 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8311 struct check_path_prefix_arg cpp_arg;
8313 if (got_path_is_root_dir(path_prefix))
8316 err = got_object_open_as_commit(&commit, repo, commit_id);
8320 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8324 err = got_object_open_as_tree(&tree1, repo,
8325 got_object_commit_get_tree_id(parent_commit));
8329 err = got_object_open_as_tree(&tree2, repo,
8330 got_object_commit_get_tree_id(commit));
8334 cpp_arg.path_prefix = path_prefix;
8335 while (cpp_arg.path_prefix[0] == '/')
8336 cpp_arg.path_prefix++;
8337 cpp_arg.len = strlen(cpp_arg.path_prefix);
8338 cpp_arg.errcode = errcode;
8339 err = got_diff_tree(tree1, tree2, "", "", repo,
8340 check_path_prefix_in_diff, &cpp_arg, 0);
8343 got_object_tree_close(tree1);
8345 got_object_tree_close(tree2);
8347 got_object_commit_close(commit);
8349 got_object_commit_close(parent_commit);
8353 static const struct got_error *
8354 collect_commits(struct got_object_id_queue *commits,
8355 struct got_object_id *initial_commit_id,
8356 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8357 const char *path_prefix, int path_prefix_errcode,
8358 struct got_repository *repo)
8360 const struct got_error *err = NULL;
8361 struct got_commit_graph *graph = NULL;
8362 struct got_object_id *parent_id = NULL;
8363 struct got_object_qid *qid;
8364 struct got_object_id *commit_id = initial_commit_id;
8366 err = got_commit_graph_open(&graph, "/", 1);
8370 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8371 check_cancelled, NULL);
8374 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8375 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8376 check_cancelled, NULL);
8378 if (err->code == GOT_ERR_ITER_COMPLETED) {
8379 err = got_error_msg(GOT_ERR_ANCESTRY,
8380 "ran out of commits to rebase before "
8381 "youngest common ancestor commit has "
8386 err = check_path_prefix(parent_id, commit_id,
8387 path_prefix, path_prefix_errcode, repo);
8391 err = got_object_qid_alloc(&qid, commit_id);
8394 STAILQ_INSERT_HEAD(commits, qid, entry);
8395 commit_id = parent_id;
8399 got_commit_graph_close(graph);
8403 static const struct got_error *
8404 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8406 const struct got_error *err = NULL;
8407 time_t committer_time;
8409 char datebuf[11]; /* YYYY-MM-DD + NUL */
8410 char *author0 = NULL, *author, *smallerthan;
8411 char *logmsg0 = NULL, *logmsg, *newline;
8413 committer_time = got_object_commit_get_committer_time(commit);
8414 if (localtime_r(&committer_time, &tm) == NULL)
8415 return got_error_from_errno("localtime_r");
8416 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8417 return got_error(GOT_ERR_NO_SPACE);
8419 author0 = strdup(got_object_commit_get_author(commit));
8420 if (author0 == NULL)
8421 return got_error_from_errno("strdup");
8423 smallerthan = strchr(author, '<');
8424 if (smallerthan && smallerthan[1] != '\0')
8425 author = smallerthan + 1;
8426 author[strcspn(author, "@>")] = '\0';
8428 err = got_object_commit_get_logmsg(&logmsg0, commit);
8432 while (*logmsg == '\n')
8434 newline = strchr(logmsg, '\n');
8438 if (asprintf(brief_str, "%s %s %s",
8439 datebuf, author, logmsg) == -1)
8440 err = got_error_from_errno("asprintf");
8447 static const struct got_error *
8448 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8449 struct got_repository *repo)
8451 const struct got_error *err;
8454 err = got_object_id_str(&id_str, id);
8458 err = got_ref_delete(ref, repo);
8462 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8468 static const struct got_error *
8469 print_backup_ref(const char *branch_name, const char *new_id_str,
8470 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8471 struct got_reflist_object_id_map *refs_idmap,
8472 struct got_repository *repo)
8474 const struct got_error *err = NULL;
8475 struct got_reflist_head *refs;
8476 char *refs_str = NULL;
8477 struct got_object_id *new_commit_id = NULL;
8478 struct got_commit_object *new_commit = NULL;
8479 char *new_commit_brief_str = NULL;
8480 struct got_object_id *yca_id = NULL;
8481 struct got_commit_object *yca_commit = NULL;
8482 char *yca_id_str = NULL, *yca_brief_str = NULL;
8483 char *custom_refs_str;
8485 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8486 return got_error_from_errno("asprintf");
8488 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8489 0, 0, refs_idmap, custom_refs_str);
8493 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8497 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8499 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8504 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8508 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8512 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8513 old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8517 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8518 refs_str ? " (" : "", refs_str ? refs_str : "",
8519 refs_str ? ")" : "", new_commit_brief_str);
8520 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8521 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8525 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8529 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8533 err = got_object_id_str(&yca_id_str, yca_id);
8537 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8539 err = build_refs_str(&refs_str, refs, yca_id, repo);
8543 printf("history forked at %s%s%s%s\n %s\n",
8545 refs_str ? " (" : "", refs_str ? refs_str : "",
8546 refs_str ? ")" : "", yca_brief_str);
8549 free(custom_refs_str);
8550 free(new_commit_id);
8554 free(yca_brief_str);
8556 got_object_commit_close(new_commit);
8558 got_object_commit_close(yca_commit);
8563 static const struct got_error *
8564 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8565 int delete, struct got_repository *repo)
8567 const struct got_error *err;
8568 struct got_reflist_head refs, backup_refs;
8569 struct got_reflist_entry *re;
8570 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8571 struct got_object_id *old_commit_id = NULL;
8572 char *branch_name = NULL;
8573 struct got_commit_object *old_commit = NULL;
8574 struct got_reflist_object_id_map *refs_idmap = NULL;
8575 int wanted_branch_found = 0;
8578 TAILQ_INIT(&backup_refs);
8580 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8584 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8588 if (wanted_branch_name) {
8589 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8590 wanted_branch_name += 11;
8593 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8594 got_ref_cmp_by_commit_timestamp_descending, repo);
8598 TAILQ_FOREACH(re, &backup_refs, entry) {
8599 const char *refname = got_ref_get_name(re->ref);
8602 err = check_cancelled(NULL);
8606 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8610 err = got_object_open_as_commit(&old_commit, repo,
8615 if (strncmp(backup_ref_prefix, refname,
8616 backup_ref_prefix_len) == 0)
8617 refname += backup_ref_prefix_len;
8619 while (refname[0] == '/')
8622 branch_name = strdup(refname);
8623 if (branch_name == NULL) {
8624 err = got_error_from_errno("strdup");
8627 slash = strrchr(branch_name, '/');
8630 refname += strlen(branch_name) + 1;
8633 if (wanted_branch_name == NULL ||
8634 strcmp(wanted_branch_name, branch_name) == 0) {
8635 wanted_branch_found = 1;
8637 err = delete_backup_ref(re->ref,
8638 old_commit_id, repo);
8640 err = print_backup_ref(branch_name, refname,
8641 old_commit_id, old_commit, refs_idmap,
8648 free(old_commit_id);
8649 old_commit_id = NULL;
8652 got_object_commit_close(old_commit);
8656 if (wanted_branch_name && !wanted_branch_found) {
8657 err = got_error_fmt(GOT_ERR_NOT_REF,
8658 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8662 got_reflist_object_id_map_free(refs_idmap);
8663 got_ref_list_free(&refs);
8664 got_ref_list_free(&backup_refs);
8665 free(old_commit_id);
8668 got_object_commit_close(old_commit);
8672 static const struct got_error *
8673 cmd_rebase(int argc, char *argv[])
8675 const struct got_error *error = NULL;
8676 struct got_worktree *worktree = NULL;
8677 struct got_repository *repo = NULL;
8678 struct got_fileindex *fileindex = NULL;
8680 struct got_reference *branch = NULL;
8681 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8682 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8683 struct got_object_id *resume_commit_id = NULL;
8684 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8685 struct got_commit_object *commit = NULL;
8686 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8687 int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8688 int delete_backups = 0;
8689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8690 struct got_object_id_queue commits;
8691 struct got_pathlist_head merged_paths;
8692 const struct got_object_id_queue *parent_ids;
8693 struct got_object_qid *qid, *pid;
8695 STAILQ_INIT(&commits);
8696 TAILQ_INIT(&merged_paths);
8698 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8704 continue_rebase = 1;
8722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8723 "unveil", NULL) == -1)
8728 option_conflict('l', 'a');
8729 if (continue_rebase)
8730 option_conflict('l', 'c');
8732 option_conflict('l', 'X');
8733 if (argc != 0 && argc != 1)
8735 } else if (delete_backups) {
8737 option_conflict('X', 'a');
8738 if (continue_rebase)
8739 option_conflict('X', 'c');
8741 option_conflict('l', 'X');
8742 if (argc != 0 && argc != 1)
8745 if (abort_rebase && continue_rebase)
8747 else if (abort_rebase || continue_rebase) {
8750 } else if (argc != 1)
8754 cwd = getcwd(NULL, 0);
8756 error = got_error_from_errno("getcwd");
8759 error = got_worktree_open(&worktree, cwd);
8761 if (list_backups || delete_backups) {
8762 if (error->code != GOT_ERR_NOT_WORKTREE)
8765 if (error->code == GOT_ERR_NOT_WORKTREE)
8766 error = wrap_not_worktree_error(error,
8772 error = got_repo_open(&repo,
8773 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8777 error = apply_unveil(got_repo_get_path(repo), 0,
8778 worktree ? got_worktree_get_root_path(worktree) : NULL);
8782 if (list_backups || delete_backups) {
8783 error = process_backup_refs(
8784 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8785 argc == 1 ? argv[0] : NULL, delete_backups, repo);
8786 goto done; /* nothing else to do */
8789 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8793 if (histedit_in_progress) {
8794 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8798 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8803 struct got_update_progress_arg upa;
8804 if (!rebase_in_progress) {
8805 error = got_error(GOT_ERR_NOT_REBASING);
8808 error = got_worktree_rebase_continue(&resume_commit_id,
8809 &new_base_branch, &tmp_branch, &branch, &fileindex,
8813 printf("Switching work tree to %s\n",
8814 got_ref_get_symref_target(new_base_branch));
8815 memset(&upa, 0, sizeof(upa));
8816 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8817 new_base_branch, update_progress, &upa);
8820 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8821 print_update_progress_stats(&upa);
8822 goto done; /* nothing else to do */
8825 if (continue_rebase) {
8826 if (!rebase_in_progress) {
8827 error = got_error(GOT_ERR_NOT_REBASING);
8830 error = got_worktree_rebase_continue(&resume_commit_id,
8831 &new_base_branch, &tmp_branch, &branch, &fileindex,
8836 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8837 resume_commit_id, repo);
8841 yca_id = got_object_id_dup(resume_commit_id);
8842 if (yca_id == NULL) {
8843 error = got_error_from_errno("got_object_id_dup");
8847 error = got_ref_open(&branch, repo, argv[0], 0);
8852 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8856 if (!continue_rebase) {
8857 struct got_object_id *base_commit_id;
8859 base_commit_id = got_worktree_get_base_commit_id(worktree);
8860 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8861 base_commit_id, branch_head_commit_id, repo,
8862 check_cancelled, NULL);
8865 if (yca_id == NULL) {
8866 error = got_error_msg(GOT_ERR_ANCESTRY,
8867 "specified branch shares no common ancestry "
8868 "with work tree's branch");
8872 error = check_same_branch(base_commit_id, branch, yca_id, repo);
8874 if (error->code != GOT_ERR_ANCESTRY)
8878 static char msg[128];
8879 snprintf(msg, sizeof(msg),
8880 "%s is already based on %s",
8881 got_ref_get_name(branch),
8882 got_worktree_get_head_ref_name(worktree));
8883 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8886 error = got_worktree_rebase_prepare(&new_base_branch,
8887 &tmp_branch, &fileindex, worktree, branch, repo);
8892 commit_id = branch_head_commit_id;
8893 error = got_object_open_as_commit(&commit, repo, commit_id);
8897 parent_ids = got_object_commit_get_parent_ids(commit);
8898 pid = STAILQ_FIRST(parent_ids);
8900 if (!continue_rebase) {
8901 struct got_update_progress_arg upa;
8902 memset(&upa, 0, sizeof(upa));
8903 error = got_worktree_rebase_abort(worktree, fileindex,
8904 repo, new_base_branch, update_progress, &upa);
8907 printf("Rebase of %s aborted\n",
8908 got_ref_get_name(branch));
8909 print_update_progress_stats(&upa);
8912 error = got_error(GOT_ERR_EMPTY_REBASE);
8915 error = collect_commits(&commits, commit_id, pid->id,
8916 yca_id, got_worktree_get_path_prefix(worktree),
8917 GOT_ERR_REBASE_PATH, repo);
8918 got_object_commit_close(commit);
8923 if (STAILQ_EMPTY(&commits)) {
8924 if (continue_rebase) {
8925 error = rebase_complete(worktree, fileindex,
8926 branch, new_base_branch, tmp_branch, repo,
8930 /* Fast-forward the reference of the branch. */
8931 struct got_object_id *new_head_commit_id;
8933 error = got_ref_resolve(&new_head_commit_id, repo,
8937 error = got_object_id_str(&id_str, new_head_commit_id);
8938 printf("Forwarding %s to commit %s\n",
8939 got_ref_get_name(branch), id_str);
8941 error = got_ref_change_ref(branch,
8942 new_head_commit_id);
8945 /* No backup needed since objects did not change. */
8951 STAILQ_FOREACH(qid, &commits, entry) {
8952 struct got_update_progress_arg upa;
8954 commit_id = qid->id;
8955 parent_id = pid ? pid->id : yca_id;
8958 memset(&upa, 0, sizeof(upa));
8959 error = got_worktree_rebase_merge_files(&merged_paths,
8960 worktree, fileindex, parent_id, commit_id, repo,
8961 update_progress, &upa, check_cancelled, NULL);
8965 print_update_progress_stats(&upa);
8966 if (upa.conflicts > 0)
8967 rebase_status = GOT_STATUS_CONFLICT;
8969 if (rebase_status == GOT_STATUS_CONFLICT) {
8970 error = show_rebase_merge_conflict(qid->id, repo);
8973 got_worktree_rebase_pathlist_free(&merged_paths);
8977 error = rebase_commit(&merged_paths, worktree, fileindex,
8978 tmp_branch, commit_id, repo);
8979 got_worktree_rebase_pathlist_free(&merged_paths);
8984 if (rebase_status == GOT_STATUS_CONFLICT) {
8985 error = got_worktree_rebase_postpone(worktree, fileindex);
8988 error = got_error_msg(GOT_ERR_CONFLICTS,
8989 "conflicts must be resolved before rebasing can continue");
8991 error = rebase_complete(worktree, fileindex, branch,
8992 new_base_branch, tmp_branch, repo, create_backup);
8994 got_object_id_queue_free(&commits);
8995 free(branch_head_commit_id);
8996 free(resume_commit_id);
8999 got_object_commit_close(commit);
9001 got_ref_close(branch);
9002 if (new_base_branch)
9003 got_ref_close(new_base_branch);
9005 got_ref_close(tmp_branch);
9007 got_worktree_close(worktree);
9009 const struct got_error *close_err = got_repo_close(repo);
9017 usage_histedit(void)
9019 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9020 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9025 #define GOT_HISTEDIT_PICK 'p'
9026 #define GOT_HISTEDIT_EDIT 'e'
9027 #define GOT_HISTEDIT_FOLD 'f'
9028 #define GOT_HISTEDIT_DROP 'd'
9029 #define GOT_HISTEDIT_MESG 'm'
9031 static struct got_histedit_cmd {
9035 } got_histedit_cmds[] = {
9036 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9037 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9038 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9040 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9041 { GOT_HISTEDIT_MESG, "mesg",
9042 "single-line log message for commit above (open editor if empty)" },
9045 struct got_histedit_list_entry {
9046 TAILQ_ENTRY(got_histedit_list_entry) entry;
9047 struct got_object_id *commit_id;
9048 const struct got_histedit_cmd *cmd;
9051 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9053 static const struct got_error *
9054 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9055 FILE *f, struct got_repository *repo)
9057 const struct got_error *err = NULL;
9058 char *logmsg = NULL, *id_str = NULL;
9059 struct got_commit_object *commit = NULL;
9062 err = got_object_open_as_commit(&commit, repo, commit_id);
9066 err = get_short_logmsg(&logmsg, 34, commit);
9070 err = got_object_id_str(&id_str, commit_id);
9074 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9076 err = got_ferror(f, GOT_ERR_IO);
9079 got_object_commit_close(commit);
9085 static const struct got_error *
9086 histedit_write_commit_list(struct got_object_id_queue *commits,
9087 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9089 const struct got_error *err = NULL;
9090 struct got_object_qid *qid;
9091 const char *histedit_cmd = NULL;
9093 if (STAILQ_EMPTY(commits))
9094 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9096 STAILQ_FOREACH(qid, commits, entry) {
9097 histedit_cmd = got_histedit_cmds[0].name;
9098 if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9099 histedit_cmd = "fold";
9100 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9103 if (edit_logmsg_only) {
9104 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9106 err = got_ferror(f, GOT_ERR_IO);
9115 static const struct got_error *
9116 write_cmd_list(FILE *f, const char *branch_name,
9117 struct got_object_id_queue *commits)
9119 const struct got_error *err = NULL;
9123 struct got_object_qid *qid;
9125 qid = STAILQ_FIRST(commits);
9126 err = got_object_id_str(&id_str, qid->id);
9131 "# Editing the history of branch '%s' starting at\n"
9133 "# Commits will be processed in order from top to "
9134 "bottom of this file.\n", branch_name, id_str);
9136 err = got_ferror(f, GOT_ERR_IO);
9140 n = fprintf(f, "# Available histedit commands:\n");
9142 err = got_ferror(f, GOT_ERR_IO);
9146 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9147 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9148 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9151 err = got_ferror(f, GOT_ERR_IO);
9160 static const struct got_error *
9161 histedit_syntax_error(int lineno)
9163 static char msg[42];
9166 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9168 if (ret == -1 || ret >= sizeof(msg))
9169 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9171 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9174 static const struct got_error *
9175 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9176 char *logmsg, struct got_repository *repo)
9178 const struct got_error *err;
9179 struct got_commit_object *folded_commit = NULL;
9180 char *id_str, *folded_logmsg = NULL;
9182 err = got_object_id_str(&id_str, hle->commit_id);
9186 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9190 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9193 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9194 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9195 folded_logmsg) == -1) {
9196 err = got_error_from_errno("asprintf");
9200 got_object_commit_close(folded_commit);
9202 free(folded_logmsg);
9206 static struct got_histedit_list_entry *
9207 get_folded_commits(struct got_histedit_list_entry *hle)
9209 struct got_histedit_list_entry *prev, *folded = NULL;
9211 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9212 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9213 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9214 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9216 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9222 static const struct got_error *
9223 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9224 struct got_repository *repo)
9226 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9227 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9228 const struct got_error *err = NULL;
9229 struct got_commit_object *commit = NULL;
9232 struct got_histedit_list_entry *folded = NULL;
9234 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9238 folded = get_folded_commits(hle);
9240 while (folded != hle) {
9241 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9242 folded = TAILQ_NEXT(folded, entry);
9245 err = append_folded_commit_msg(&new_msg, folded,
9251 folded = TAILQ_NEXT(folded, entry);
9255 err = got_object_id_str(&id_str, hle->commit_id);
9258 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9261 logmsg_len = asprintf(&new_msg,
9262 "%s\n# original log message of commit %s: %s",
9263 logmsg ? logmsg : "", id_str, orig_logmsg);
9264 if (logmsg_len == -1) {
9265 err = got_error_from_errno("asprintf");
9271 err = got_object_id_str(&id_str, hle->commit_id);
9275 err = got_opentemp_named_fd(&logmsg_path, &fd,
9276 GOT_TMPDIR_STR "/got-logmsg");
9280 write(fd, logmsg, logmsg_len);
9283 err = get_editor(&editor);
9287 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9290 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9293 hle->logmsg = strdup(new_msg);
9294 if (hle->logmsg == NULL)
9295 err = got_error_from_errno("strdup");
9298 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9299 err = got_error_from_errno2("unlink", logmsg_path);
9305 got_object_commit_close(commit);
9309 static const struct got_error *
9310 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9311 FILE *f, struct got_repository *repo)
9313 const struct got_error *err = NULL;
9314 char *line = NULL, *p, *end;
9318 const struct got_histedit_cmd *cmd;
9319 struct got_object_id *commit_id = NULL;
9320 struct got_histedit_list_entry *hle = NULL;
9323 len = getline(&line, &size, f);
9325 const struct got_error *getline_err;
9328 getline_err = got_error_from_errno("getline");
9329 err = got_ferror(f, getline_err->code);
9334 while (isspace((unsigned char)p[0]))
9336 if (p[0] == '#' || p[0] == '\0') {
9342 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9343 cmd = &got_histedit_cmds[i];
9344 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9345 isspace((unsigned char)p[strlen(cmd->name)])) {
9346 p += strlen(cmd->name);
9349 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9354 if (i == nitems(got_histedit_cmds)) {
9355 err = histedit_syntax_error(lineno);
9358 while (isspace((unsigned char)p[0]))
9360 if (cmd->code == GOT_HISTEDIT_MESG) {
9361 if (hle == NULL || hle->logmsg != NULL) {
9362 err = got_error(GOT_ERR_HISTEDIT_CMD);
9366 err = histedit_edit_logmsg(hle, repo);
9370 hle->logmsg = strdup(p);
9371 if (hle->logmsg == NULL) {
9372 err = got_error_from_errno("strdup");
9381 while (end[0] && !isspace((unsigned char)end[0]))
9385 err = got_object_resolve_id_str(&commit_id, repo, p);
9387 /* override error code */
9388 err = histedit_syntax_error(lineno);
9392 hle = malloc(sizeof(*hle));
9394 err = got_error_from_errno("malloc");
9398 hle->commit_id = commit_id;
9403 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9411 static const struct got_error *
9412 histedit_check_script(struct got_histedit_list *histedit_cmds,
9413 struct got_object_id_queue *commits, struct got_repository *repo)
9415 const struct got_error *err = NULL;
9416 struct got_object_qid *qid;
9417 struct got_histedit_list_entry *hle;
9418 static char msg[92];
9421 if (TAILQ_EMPTY(histedit_cmds))
9422 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9423 "histedit script contains no commands");
9424 if (STAILQ_EMPTY(commits))
9425 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9427 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9428 struct got_histedit_list_entry *hle2;
9429 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9432 if (got_object_id_cmp(hle->commit_id,
9433 hle2->commit_id) != 0)
9435 err = got_object_id_str(&id_str, hle->commit_id);
9438 snprintf(msg, sizeof(msg), "commit %s is listed "
9439 "more than once in histedit script", id_str);
9441 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9445 STAILQ_FOREACH(qid, commits, entry) {
9446 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9447 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9451 err = got_object_id_str(&id_str, qid->id);
9454 snprintf(msg, sizeof(msg),
9455 "commit %s missing from histedit script", id_str);
9457 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9461 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9462 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9463 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9464 "last commit in histedit script cannot be folded");
9469 static const struct got_error *
9470 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9471 const char *path, struct got_object_id_queue *commits,
9472 struct got_repository *repo)
9474 const struct got_error *err = NULL;
9478 err = get_editor(&editor);
9482 if (spawn_editor(editor, path) == -1) {
9483 err = got_error_from_errno("failed spawning editor");
9487 f = fopen(path, "r");
9489 err = got_error_from_errno("fopen");
9492 err = histedit_parse_list(histedit_cmds, f, repo);
9496 err = histedit_check_script(histedit_cmds, commits, repo);
9498 if (f && fclose(f) == EOF && err == NULL)
9499 err = got_error_from_errno("fclose");
9504 static const struct got_error *
9505 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9506 struct got_object_id_queue *, const char *, const char *,
9507 struct got_repository *);
9509 static const struct got_error *
9510 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9511 struct got_object_id_queue *commits, const char *branch_name,
9512 int edit_logmsg_only, int fold_only, struct got_repository *repo)
9514 const struct got_error *err;
9518 err = got_opentemp_named(&path, &f, "got-histedit");
9522 err = write_cmd_list(f, branch_name, commits);
9526 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9531 if (edit_logmsg_only || fold_only) {
9533 err = histedit_parse_list(histedit_cmds, f, repo);
9535 if (fclose(f) == EOF) {
9536 err = got_error_from_errno("fclose");
9540 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9542 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9543 err->code != GOT_ERR_HISTEDIT_CMD)
9545 err = histedit_edit_list_retry(histedit_cmds, err,
9546 commits, path, branch_name, repo);
9550 if (f && fclose(f) == EOF && err == NULL)
9551 err = got_error_from_errno("fclose");
9552 if (path && unlink(path) != 0 && err == NULL)
9553 err = got_error_from_errno2("unlink", path);
9558 static const struct got_error *
9559 histedit_save_list(struct got_histedit_list *histedit_cmds,
9560 struct got_worktree *worktree, struct got_repository *repo)
9562 const struct got_error *err = NULL;
9565 struct got_histedit_list_entry *hle;
9566 struct got_commit_object *commit = NULL;
9568 err = got_worktree_get_histedit_script_path(&path, worktree);
9572 f = fopen(path, "w");
9574 err = got_error_from_errno2("fopen", path);
9577 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9578 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9584 int n = fprintf(f, "%c %s\n",
9585 GOT_HISTEDIT_MESG, hle->logmsg);
9587 err = got_ferror(f, GOT_ERR_IO);
9593 if (f && fclose(f) == EOF && err == NULL)
9594 err = got_error_from_errno("fclose");
9597 got_object_commit_close(commit);
9602 histedit_free_list(struct got_histedit_list *histedit_cmds)
9604 struct got_histedit_list_entry *hle;
9606 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9607 TAILQ_REMOVE(histedit_cmds, hle, entry);
9612 static const struct got_error *
9613 histedit_load_list(struct got_histedit_list *histedit_cmds,
9614 const char *path, struct got_repository *repo)
9616 const struct got_error *err = NULL;
9619 f = fopen(path, "r");
9621 err = got_error_from_errno2("fopen", path);
9625 err = histedit_parse_list(histedit_cmds, f, repo);
9627 if (f && fclose(f) == EOF && err == NULL)
9628 err = got_error_from_errno("fclose");
9632 static const struct got_error *
9633 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9634 const struct got_error *edit_err, struct got_object_id_queue *commits,
9635 const char *path, const char *branch_name, struct got_repository *repo)
9637 const struct got_error *err = NULL, *prev_err = edit_err;
9640 while (resp != 'c' && resp != 'r' && resp != 'a') {
9641 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9642 "or (a)bort: ", getprogname(), prev_err->msg);
9647 histedit_free_list(histedit_cmds);
9648 err = histedit_run_editor(histedit_cmds, path, commits,
9651 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9652 err->code != GOT_ERR_HISTEDIT_CMD)
9659 } else if (resp == 'r') {
9660 histedit_free_list(histedit_cmds);
9661 err = histedit_edit_script(histedit_cmds,
9662 commits, branch_name, 0, 0, repo);
9664 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9665 err->code != GOT_ERR_HISTEDIT_CMD)
9672 } else if (resp == 'a') {
9673 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9676 printf("invalid response '%c'\n", resp);
9682 static const struct got_error *
9683 histedit_complete(struct got_worktree *worktree,
9684 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9685 struct got_reference *branch, struct got_repository *repo)
9687 printf("Switching work tree to %s\n",
9688 got_ref_get_symref_target(branch));
9689 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9693 static const struct got_error *
9694 show_histedit_progress(struct got_commit_object *commit,
9695 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9697 const struct got_error *err;
9698 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9700 err = got_object_id_str(&old_id_str, hle->commit_id);
9705 err = got_object_id_str(&new_id_str, new_id);
9710 old_id_str[12] = '\0';
9712 new_id_str[12] = '\0';
9715 logmsg = strdup(hle->logmsg);
9716 if (logmsg == NULL) {
9717 err = got_error_from_errno("strdup");
9720 trim_logmsg(logmsg, 42);
9722 err = get_short_logmsg(&logmsg, 42, commit);
9727 switch (hle->cmd->code) {
9728 case GOT_HISTEDIT_PICK:
9729 case GOT_HISTEDIT_EDIT:
9730 printf("%s -> %s: %s\n", old_id_str,
9731 new_id_str ? new_id_str : "no-op change", logmsg);
9733 case GOT_HISTEDIT_DROP:
9734 case GOT_HISTEDIT_FOLD:
9735 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9747 static const struct got_error *
9748 histedit_commit(struct got_pathlist_head *merged_paths,
9749 struct got_worktree *worktree, struct got_fileindex *fileindex,
9750 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9751 struct got_repository *repo)
9753 const struct got_error *err;
9754 struct got_commit_object *commit;
9755 struct got_object_id *new_commit_id;
9757 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9758 && hle->logmsg == NULL) {
9759 err = histedit_edit_logmsg(hle, repo);
9764 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9768 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9769 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9772 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9774 err = show_histedit_progress(commit, hle, NULL);
9776 err = show_histedit_progress(commit, hle, new_commit_id);
9777 free(new_commit_id);
9780 got_object_commit_close(commit);
9784 static const struct got_error *
9785 histedit_skip_commit(struct got_histedit_list_entry *hle,
9786 struct got_worktree *worktree, struct got_repository *repo)
9788 const struct got_error *error;
9789 struct got_commit_object *commit;
9791 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9796 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9800 error = show_histedit_progress(commit, hle, NULL);
9801 got_object_commit_close(commit);
9805 static const struct got_error *
9806 check_local_changes(void *arg, unsigned char status,
9807 unsigned char staged_status, const char *path,
9808 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9809 struct got_object_id *commit_id, int dirfd, const char *de_name)
9811 int *have_local_changes = arg;
9814 case GOT_STATUS_ADD:
9815 case GOT_STATUS_DELETE:
9816 case GOT_STATUS_MODIFY:
9817 case GOT_STATUS_CONFLICT:
9818 *have_local_changes = 1;
9819 return got_error(GOT_ERR_CANCELLED);
9824 switch (staged_status) {
9825 case GOT_STATUS_ADD:
9826 case GOT_STATUS_DELETE:
9827 case GOT_STATUS_MODIFY:
9828 *have_local_changes = 1;
9829 return got_error(GOT_ERR_CANCELLED);
9837 static const struct got_error *
9838 cmd_histedit(int argc, char *argv[])
9840 const struct got_error *error = NULL;
9841 struct got_worktree *worktree = NULL;
9842 struct got_fileindex *fileindex = NULL;
9843 struct got_repository *repo = NULL;
9845 struct got_reference *branch = NULL;
9846 struct got_reference *tmp_branch = NULL;
9847 struct got_object_id *resume_commit_id = NULL;
9848 struct got_object_id *base_commit_id = NULL;
9849 struct got_object_id *head_commit_id = NULL;
9850 struct got_commit_object *commit = NULL;
9851 int ch, rebase_in_progress = 0;
9852 struct got_update_progress_arg upa;
9853 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9854 int edit_logmsg_only = 0, fold_only = 0;
9855 int list_backups = 0, delete_backups = 0;
9856 const char *edit_script_path = NULL;
9857 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9858 struct got_object_id_queue commits;
9859 struct got_pathlist_head merged_paths;
9860 const struct got_object_id_queue *parent_ids;
9861 struct got_object_qid *pid;
9862 struct got_histedit_list histedit_cmds;
9863 struct got_histedit_list_entry *hle;
9865 STAILQ_INIT(&commits);
9866 TAILQ_INIT(&histedit_cmds);
9867 TAILQ_INIT(&merged_paths);
9868 memset(&upa, 0, sizeof(upa));
9870 while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9882 edit_script_path = optarg;
9885 edit_logmsg_only = 1;
9903 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9904 "unveil", NULL) == -1)
9907 if (abort_edit && continue_edit)
9908 option_conflict('a', 'c');
9909 if (edit_script_path && edit_logmsg_only)
9910 option_conflict('F', 'm');
9911 if (abort_edit && edit_logmsg_only)
9912 option_conflict('a', 'm');
9913 if (continue_edit && edit_logmsg_only)
9914 option_conflict('c', 'm');
9915 if (abort_edit && fold_only)
9916 option_conflict('a', 'f');
9917 if (continue_edit && fold_only)
9918 option_conflict('c', 'f');
9919 if (fold_only && edit_logmsg_only)
9920 option_conflict('f', 'm');
9921 if (edit_script_path && fold_only)
9922 option_conflict('F', 'f');
9925 option_conflict('l', 'a');
9927 option_conflict('l', 'c');
9928 if (edit_script_path)
9929 option_conflict('l', 'F');
9930 if (edit_logmsg_only)
9931 option_conflict('l', 'm');
9933 option_conflict('l', 'f');
9935 option_conflict('l', 'X');
9936 if (argc != 0 && argc != 1)
9938 } else if (delete_backups) {
9940 option_conflict('X', 'a');
9942 option_conflict('X', 'c');
9943 if (edit_script_path)
9944 option_conflict('X', 'F');
9945 if (edit_logmsg_only)
9946 option_conflict('X', 'm');
9948 option_conflict('X', 'f');
9950 option_conflict('X', 'l');
9951 if (argc != 0 && argc != 1)
9953 } else if (argc != 0)
9957 * This command cannot apply unveil(2) in all cases because the
9958 * user may choose to run an editor to edit the histedit script
9959 * and to edit individual commit log messages.
9960 * unveil(2) traverses exec(2); if an editor is used we have to
9961 * apply unveil after edit script and log messages have been written.
9962 * XXX TODO: Make use of unveil(2) where possible.
9965 cwd = getcwd(NULL, 0);
9967 error = got_error_from_errno("getcwd");
9970 error = got_worktree_open(&worktree, cwd);
9972 if (list_backups || delete_backups) {
9973 if (error->code != GOT_ERR_NOT_WORKTREE)
9976 if (error->code == GOT_ERR_NOT_WORKTREE)
9977 error = wrap_not_worktree_error(error,
9983 if (list_backups || delete_backups) {
9984 error = got_repo_open(&repo,
9985 worktree ? got_worktree_get_repo_path(worktree) : cwd,
9989 error = apply_unveil(got_repo_get_path(repo), 0,
9990 worktree ? got_worktree_get_root_path(worktree) : NULL);
9993 error = process_backup_refs(
9994 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9995 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9996 goto done; /* nothing else to do */
9999 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10004 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10007 if (rebase_in_progress) {
10008 error = got_error(GOT_ERR_REBASING);
10012 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10016 if (edit_in_progress && edit_logmsg_only) {
10017 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10018 "histedit operation is in progress in this "
10019 "work tree and must be continued or aborted "
10020 "before the -m option can be used");
10023 if (edit_in_progress && fold_only) {
10024 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10025 "histedit operation is in progress in this "
10026 "work tree and must be continued or aborted "
10027 "before the -f option can be used");
10031 if (edit_in_progress && abort_edit) {
10032 error = got_worktree_histedit_continue(&resume_commit_id,
10033 &tmp_branch, &branch, &base_commit_id, &fileindex,
10037 printf("Switching work tree to %s\n",
10038 got_ref_get_symref_target(branch));
10039 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10040 branch, base_commit_id, update_progress, &upa);
10043 printf("Histedit of %s aborted\n",
10044 got_ref_get_symref_target(branch));
10045 print_update_progress_stats(&upa);
10046 goto done; /* nothing else to do */
10047 } else if (abort_edit) {
10048 error = got_error(GOT_ERR_NOT_HISTEDIT);
10052 if (continue_edit) {
10055 if (!edit_in_progress) {
10056 error = got_error(GOT_ERR_NOT_HISTEDIT);
10060 error = got_worktree_get_histedit_script_path(&path, worktree);
10064 error = histedit_load_list(&histedit_cmds, path, repo);
10069 error = got_worktree_histedit_continue(&resume_commit_id,
10070 &tmp_branch, &branch, &base_commit_id, &fileindex,
10075 error = got_ref_resolve(&head_commit_id, repo, branch);
10079 error = got_object_open_as_commit(&commit, repo,
10083 parent_ids = got_object_commit_get_parent_ids(commit);
10084 pid = STAILQ_FIRST(parent_ids);
10086 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10089 error = collect_commits(&commits, head_commit_id, pid->id,
10090 base_commit_id, got_worktree_get_path_prefix(worktree),
10091 GOT_ERR_HISTEDIT_PATH, repo);
10092 got_object_commit_close(commit);
10097 if (edit_in_progress) {
10098 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10102 error = got_ref_open(&branch, repo,
10103 got_worktree_get_head_ref_name(worktree), 0);
10107 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10108 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10109 "will not edit commit history of a branch outside "
10110 "the \"refs/heads/\" reference namespace");
10114 error = got_ref_resolve(&head_commit_id, repo, branch);
10115 got_ref_close(branch);
10120 error = got_object_open_as_commit(&commit, repo,
10124 parent_ids = got_object_commit_get_parent_ids(commit);
10125 pid = STAILQ_FIRST(parent_ids);
10127 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10130 error = collect_commits(&commits, head_commit_id, pid->id,
10131 got_worktree_get_base_commit_id(worktree),
10132 got_worktree_get_path_prefix(worktree),
10133 GOT_ERR_HISTEDIT_PATH, repo);
10134 got_object_commit_close(commit);
10139 if (STAILQ_EMPTY(&commits)) {
10140 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10144 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10145 &base_commit_id, &fileindex, worktree, repo);
10149 if (edit_script_path) {
10150 error = histedit_load_list(&histedit_cmds,
10151 edit_script_path, repo);
10153 got_worktree_histedit_abort(worktree, fileindex,
10154 repo, branch, base_commit_id,
10155 update_progress, &upa);
10156 print_update_progress_stats(&upa);
10160 const char *branch_name;
10161 branch_name = got_ref_get_symref_target(branch);
10162 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10164 error = histedit_edit_script(&histedit_cmds, &commits,
10165 branch_name, edit_logmsg_only, fold_only, repo);
10167 got_worktree_histedit_abort(worktree, fileindex,
10168 repo, branch, base_commit_id,
10169 update_progress, &upa);
10170 print_update_progress_stats(&upa);
10176 error = histedit_save_list(&histedit_cmds, worktree,
10179 got_worktree_histedit_abort(worktree, fileindex,
10180 repo, branch, base_commit_id,
10181 update_progress, &upa);
10182 print_update_progress_stats(&upa);
10188 error = histedit_check_script(&histedit_cmds, &commits, repo);
10192 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10193 if (resume_commit_id) {
10194 if (got_object_id_cmp(hle->commit_id,
10195 resume_commit_id) != 0)
10198 resume_commit_id = NULL;
10199 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10200 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10201 error = histedit_skip_commit(hle, worktree,
10206 struct got_pathlist_head paths;
10207 int have_changes = 0;
10209 TAILQ_INIT(&paths);
10210 error = got_pathlist_append(&paths, "", NULL);
10213 error = got_worktree_status(worktree, &paths,
10214 repo, 0, check_local_changes, &have_changes,
10215 check_cancelled, NULL);
10216 got_pathlist_free(&paths);
10218 if (error->code != GOT_ERR_CANCELLED)
10220 if (sigint_received || sigpipe_received)
10223 if (have_changes) {
10224 error = histedit_commit(NULL, worktree,
10225 fileindex, tmp_branch, hle, repo);
10229 error = got_object_open_as_commit(
10230 &commit, repo, hle->commit_id);
10233 error = show_histedit_progress(commit,
10235 got_object_commit_close(commit);
10244 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10245 error = histedit_skip_commit(hle, worktree, repo);
10251 error = got_object_open_as_commit(&commit, repo,
10255 parent_ids = got_object_commit_get_parent_ids(commit);
10256 pid = STAILQ_FIRST(parent_ids);
10258 error = got_worktree_histedit_merge_files(&merged_paths,
10259 worktree, fileindex, pid->id, hle->commit_id, repo,
10260 update_progress, &upa, check_cancelled, NULL);
10263 got_object_commit_close(commit);
10266 print_update_progress_stats(&upa);
10267 if (upa.conflicts > 0)
10268 rebase_status = GOT_STATUS_CONFLICT;
10270 if (rebase_status == GOT_STATUS_CONFLICT) {
10271 error = show_rebase_merge_conflict(hle->commit_id,
10275 got_worktree_rebase_pathlist_free(&merged_paths);
10279 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10281 error = got_object_id_str(&id_str, hle->commit_id);
10284 printf("Stopping histedit for amending commit %s\n",
10287 got_worktree_rebase_pathlist_free(&merged_paths);
10288 error = got_worktree_histedit_postpone(worktree,
10293 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10294 error = histedit_skip_commit(hle, worktree, repo);
10300 error = histedit_commit(&merged_paths, worktree, fileindex,
10301 tmp_branch, hle, repo);
10302 got_worktree_rebase_pathlist_free(&merged_paths);
10307 if (rebase_status == GOT_STATUS_CONFLICT) {
10308 error = got_worktree_histedit_postpone(worktree, fileindex);
10311 error = got_error_msg(GOT_ERR_CONFLICTS,
10312 "conflicts must be resolved before histedit can continue");
10314 error = histedit_complete(worktree, fileindex, tmp_branch,
10317 got_object_id_queue_free(&commits);
10318 histedit_free_list(&histedit_cmds);
10319 free(head_commit_id);
10320 free(base_commit_id);
10321 free(resume_commit_id);
10323 got_object_commit_close(commit);
10325 got_ref_close(branch);
10327 got_ref_close(tmp_branch);
10329 got_worktree_close(worktree);
10331 const struct got_error *close_err = got_repo_close(repo);
10339 usage_integrate(void)
10341 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10345 static const struct got_error *
10346 cmd_integrate(int argc, char *argv[])
10348 const struct got_error *error = NULL;
10349 struct got_repository *repo = NULL;
10350 struct got_worktree *worktree = NULL;
10351 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10352 const char *branch_arg = NULL;
10353 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10354 struct got_fileindex *fileindex = NULL;
10355 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10357 struct got_update_progress_arg upa;
10359 while ((ch = getopt(argc, argv, "")) != -1) {
10372 branch_arg = argv[0];
10374 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10375 "unveil", NULL) == -1)
10378 cwd = getcwd(NULL, 0);
10380 error = got_error_from_errno("getcwd");
10384 error = got_worktree_open(&worktree, cwd);
10386 if (error->code == GOT_ERR_NOT_WORKTREE)
10387 error = wrap_not_worktree_error(error, "integrate",
10392 error = check_rebase_or_histedit_in_progress(worktree);
10396 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10401 error = apply_unveil(got_repo_get_path(repo), 0,
10402 got_worktree_get_root_path(worktree));
10406 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10407 error = got_error_from_errno("asprintf");
10411 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10412 &base_branch_ref, worktree, refname, repo);
10416 refname = strdup(got_ref_get_name(branch_ref));
10417 if (refname == NULL) {
10418 error = got_error_from_errno("strdup");
10419 got_worktree_integrate_abort(worktree, fileindex, repo,
10420 branch_ref, base_branch_ref);
10423 base_refname = strdup(got_ref_get_name(base_branch_ref));
10424 if (base_refname == NULL) {
10425 error = got_error_from_errno("strdup");
10426 got_worktree_integrate_abort(worktree, fileindex, repo,
10427 branch_ref, base_branch_ref);
10431 error = got_ref_resolve(&commit_id, repo, branch_ref);
10435 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10439 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10440 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10441 "specified branch has already been integrated");
10442 got_worktree_integrate_abort(worktree, fileindex, repo,
10443 branch_ref, base_branch_ref);
10447 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10449 if (error->code == GOT_ERR_ANCESTRY)
10450 error = got_error(GOT_ERR_REBASE_REQUIRED);
10451 got_worktree_integrate_abort(worktree, fileindex, repo,
10452 branch_ref, base_branch_ref);
10456 memset(&upa, 0, sizeof(upa));
10457 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10458 branch_ref, base_branch_ref, update_progress, &upa,
10459 check_cancelled, NULL);
10463 printf("Integrated %s into %s\n", refname, base_refname);
10464 print_update_progress_stats(&upa);
10467 const struct got_error *close_err = got_repo_close(repo);
10472 got_worktree_close(worktree);
10474 free(base_commit_id);
10477 free(base_refname);
10484 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10485 "[-S] [file-path ...]\n",
10490 static const struct got_error *
10491 print_stage(void *arg, unsigned char status, unsigned char staged_status,
10492 const char *path, struct got_object_id *blob_id,
10493 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10494 int dirfd, const char *de_name)
10496 const struct got_error *err = NULL;
10497 char *id_str = NULL;
10499 if (staged_status != GOT_STATUS_ADD &&
10500 staged_status != GOT_STATUS_MODIFY &&
10501 staged_status != GOT_STATUS_DELETE)
10504 if (staged_status == GOT_STATUS_ADD ||
10505 staged_status == GOT_STATUS_MODIFY)
10506 err = got_object_id_str(&id_str, staged_blob_id);
10508 err = got_object_id_str(&id_str, blob_id);
10512 printf("%s %c %s\n", id_str, staged_status, path);
10517 static const struct got_error *
10518 cmd_stage(int argc, char *argv[])
10520 const struct got_error *error = NULL;
10521 struct got_repository *repo = NULL;
10522 struct got_worktree *worktree = NULL;
10524 struct got_pathlist_head paths;
10525 struct got_pathlist_entry *pe;
10526 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10527 FILE *patch_script_file = NULL;
10528 const char *patch_script_path = NULL;
10529 struct choose_patch_arg cpa;
10531 TAILQ_INIT(&paths);
10533 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10542 patch_script_path = optarg;
10545 allow_bad_symlinks = 1;
10557 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10558 "unveil", NULL) == -1)
10561 if (list_stage && (pflag || patch_script_path))
10562 errx(1, "-l option cannot be used with other options");
10563 if (patch_script_path && !pflag)
10564 errx(1, "-F option can only be used together with -p option");
10566 cwd = getcwd(NULL, 0);
10568 error = got_error_from_errno("getcwd");
10572 error = got_worktree_open(&worktree, cwd);
10574 if (error->code == GOT_ERR_NOT_WORKTREE)
10575 error = wrap_not_worktree_error(error, "stage", cwd);
10579 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10584 if (patch_script_path) {
10585 patch_script_file = fopen(patch_script_path, "r");
10586 if (patch_script_file == NULL) {
10587 error = got_error_from_errno2("fopen",
10588 patch_script_path);
10592 error = apply_unveil(got_repo_get_path(repo), 0,
10593 got_worktree_get_root_path(worktree));
10597 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10602 error = got_worktree_status(worktree, &paths, repo, 0,
10603 print_stage, NULL, check_cancelled, NULL);
10605 cpa.patch_script_file = patch_script_file;
10606 cpa.action = "stage";
10607 error = got_worktree_stage(worktree, &paths,
10608 pflag ? NULL : print_status, NULL,
10609 pflag ? choose_patch : NULL, &cpa,
10610 allow_bad_symlinks, repo);
10613 if (patch_script_file && fclose(patch_script_file) == EOF &&
10615 error = got_error_from_errno2("fclose", patch_script_path);
10617 const struct got_error *close_err = got_repo_close(repo);
10622 got_worktree_close(worktree);
10623 TAILQ_FOREACH(pe, &paths, entry)
10624 free((char *)pe->path);
10625 got_pathlist_free(&paths);
10631 usage_unstage(void)
10633 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10634 "[file-path ...]\n",
10640 static const struct got_error *
10641 cmd_unstage(int argc, char *argv[])
10643 const struct got_error *error = NULL;
10644 struct got_repository *repo = NULL;
10645 struct got_worktree *worktree = NULL;
10647 struct got_pathlist_head paths;
10648 struct got_pathlist_entry *pe;
10650 struct got_update_progress_arg upa;
10651 FILE *patch_script_file = NULL;
10652 const char *patch_script_path = NULL;
10653 struct choose_patch_arg cpa;
10655 TAILQ_INIT(&paths);
10657 while ((ch = getopt(argc, argv, "pF:")) != -1) {
10663 patch_script_path = optarg;
10675 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10676 "unveil", NULL) == -1)
10679 if (patch_script_path && !pflag)
10680 errx(1, "-F option can only be used together with -p option");
10682 cwd = getcwd(NULL, 0);
10684 error = got_error_from_errno("getcwd");
10688 error = got_worktree_open(&worktree, cwd);
10690 if (error->code == GOT_ERR_NOT_WORKTREE)
10691 error = wrap_not_worktree_error(error, "unstage", cwd);
10695 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10700 if (patch_script_path) {
10701 patch_script_file = fopen(patch_script_path, "r");
10702 if (patch_script_file == NULL) {
10703 error = got_error_from_errno2("fopen",
10704 patch_script_path);
10709 error = apply_unveil(got_repo_get_path(repo), 0,
10710 got_worktree_get_root_path(worktree));
10714 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10718 cpa.patch_script_file = patch_script_file;
10719 cpa.action = "unstage";
10720 memset(&upa, 0, sizeof(upa));
10721 error = got_worktree_unstage(worktree, &paths, update_progress,
10722 &upa, pflag ? choose_patch : NULL, &cpa, repo);
10724 print_update_progress_stats(&upa);
10726 if (patch_script_file && fclose(patch_script_file) == EOF &&
10728 error = got_error_from_errno2("fclose", patch_script_path);
10730 const struct got_error *close_err = got_repo_close(repo);
10735 got_worktree_close(worktree);
10736 TAILQ_FOREACH(pe, &paths, entry)
10737 free((char *)pe->path);
10738 got_pathlist_free(&paths);
10746 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10747 "arg1 [arg2 ...]\n", getprogname());
10751 static const struct got_error *
10752 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10754 const struct got_error *err;
10755 struct got_blob_object *blob;
10757 err = got_object_open_as_blob(&blob, repo, id, 8192);
10761 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10762 got_object_blob_close(blob);
10766 static const struct got_error *
10767 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10769 const struct got_error *err;
10770 struct got_tree_object *tree;
10773 err = got_object_open_as_tree(&tree, repo, id);
10777 nentries = got_object_tree_get_nentries(tree);
10778 for (i = 0; i < nentries; i++) {
10779 struct got_tree_entry *te;
10781 if (sigint_received || sigpipe_received)
10783 te = got_object_tree_get_entry(tree, i);
10784 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10787 fprintf(outfile, "%s %.7o %s\n", id_str,
10788 got_tree_entry_get_mode(te),
10789 got_tree_entry_get_name(te));
10793 got_object_tree_close(tree);
10797 static const struct got_error *
10798 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10800 const struct got_error *err;
10801 struct got_commit_object *commit;
10802 const struct got_object_id_queue *parent_ids;
10803 struct got_object_qid *pid;
10804 char *id_str = NULL;
10805 const char *logmsg = NULL;
10807 err = got_object_open_as_commit(&commit, repo, id);
10811 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10815 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10816 parent_ids = got_object_commit_get_parent_ids(commit);
10817 fprintf(outfile, "numparents %d\n",
10818 got_object_commit_get_nparents(commit));
10819 STAILQ_FOREACH(pid, parent_ids, entry) {
10821 err = got_object_id_str(&pid_str, pid->id);
10824 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10827 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10828 got_object_commit_get_author(commit),
10829 (long long)got_object_commit_get_author_time(commit));
10831 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10832 got_object_commit_get_author(commit),
10833 (long long)got_object_commit_get_committer_time(commit));
10835 logmsg = got_object_commit_get_logmsg_raw(commit);
10836 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10837 fprintf(outfile, "%s", logmsg);
10840 got_object_commit_close(commit);
10844 static const struct got_error *
10845 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10847 const struct got_error *err;
10848 struct got_tag_object *tag;
10849 char *id_str = NULL;
10850 const char *tagmsg = NULL;
10852 err = got_object_open_as_tag(&tag, repo, id);
10856 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10860 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10862 switch (got_object_tag_get_object_type(tag)) {
10863 case GOT_OBJ_TYPE_BLOB:
10864 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10865 GOT_OBJ_LABEL_BLOB);
10867 case GOT_OBJ_TYPE_TREE:
10868 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10869 GOT_OBJ_LABEL_TREE);
10871 case GOT_OBJ_TYPE_COMMIT:
10872 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10873 GOT_OBJ_LABEL_COMMIT);
10875 case GOT_OBJ_TYPE_TAG:
10876 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10877 GOT_OBJ_LABEL_TAG);
10883 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10884 got_object_tag_get_name(tag));
10886 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10887 got_object_tag_get_tagger(tag),
10888 (long long)got_object_tag_get_tagger_time(tag));
10890 tagmsg = got_object_tag_get_message(tag);
10891 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10892 fprintf(outfile, "%s", tagmsg);
10895 got_object_tag_close(tag);
10899 static const struct got_error *
10900 cmd_cat(int argc, char *argv[])
10902 const struct got_error *error;
10903 struct got_repository *repo = NULL;
10904 struct got_worktree *worktree = NULL;
10905 char *cwd = NULL, *repo_path = NULL, *label = NULL;
10906 const char *commit_id_str = NULL;
10907 struct got_object_id *id = NULL, *commit_id = NULL;
10908 int ch, obj_type, i, force_path = 0;
10909 struct got_reflist_head refs;
10914 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10919 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10922 commit_id_str = optarg;
10925 repo_path = realpath(optarg, NULL);
10926 if (repo_path == NULL)
10927 return got_error_from_errno2("realpath",
10929 got_path_strip_trailing_slashes(repo_path);
10943 cwd = getcwd(NULL, 0);
10945 error = got_error_from_errno("getcwd");
10948 error = got_worktree_open(&worktree, cwd);
10949 if (error && error->code != GOT_ERR_NOT_WORKTREE)
10952 if (repo_path == NULL) {
10953 repo_path = strdup(
10954 got_worktree_get_repo_path(worktree));
10955 if (repo_path == NULL) {
10956 error = got_error_from_errno("strdup");
10962 if (repo_path == NULL) {
10963 repo_path = getcwd(NULL, 0);
10964 if (repo_path == NULL)
10965 return got_error_from_errno("getcwd");
10968 error = got_repo_open(&repo, repo_path, NULL);
10973 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10977 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10981 if (commit_id_str == NULL)
10982 commit_id_str = GOT_REF_HEAD;
10983 error = got_repo_match_object_id(&commit_id, NULL,
10984 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10988 for (i = 0; i < argc; i++) {
10990 error = got_object_id_by_path(&id, repo, commit_id,
10995 error = got_repo_match_object_id(&id, &label, argv[i],
10996 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10999 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11000 error->code != GOT_ERR_NOT_REF)
11002 error = got_object_id_by_path(&id, repo,
11003 commit_id, argv[i]);
11009 error = got_object_get_type(&obj_type, repo, id);
11013 switch (obj_type) {
11014 case GOT_OBJ_TYPE_BLOB:
11015 error = cat_blob(id, repo, stdout);
11017 case GOT_OBJ_TYPE_TREE:
11018 error = cat_tree(id, repo, stdout);
11020 case GOT_OBJ_TYPE_COMMIT:
11021 error = cat_commit(id, repo, stdout);
11023 case GOT_OBJ_TYPE_TAG:
11024 error = cat_tag(id, repo, stdout);
11027 error = got_error(GOT_ERR_OBJ_TYPE);
11042 got_worktree_close(worktree);
11044 const struct got_error *close_err = got_repo_close(repo);
11048 got_ref_list_free(&refs);
11055 fprintf(stderr, "usage: %s info [path ...]\n",
11060 static const struct got_error *
11061 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11062 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11063 struct got_object_id *commit_id)
11065 const struct got_error *err = NULL;
11066 char *id_str = NULL;
11068 struct tm mytm, *tm;
11069 struct got_pathlist_head *paths = arg;
11070 struct got_pathlist_entry *pe;
11073 * Clear error indication from any of the path arguments which
11074 * would cause this file index entry to be displayed.
11076 TAILQ_FOREACH(pe, paths, entry) {
11077 if (got_path_cmp(path, pe->path, strlen(path),
11078 pe->path_len) == 0 ||
11079 got_path_is_child(path, pe->path, pe->path_len))
11080 pe->data = NULL; /* no error */
11083 printf(GOT_COMMIT_SEP_STR);
11085 printf("symlink: %s\n", path);
11086 else if (S_ISREG(mode)) {
11087 printf("file: %s\n", path);
11088 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11089 } else if (S_ISDIR(mode))
11090 printf("directory: %s\n", path);
11092 printf("something: %s\n", path);
11094 tm = localtime_r(&mtime, &mytm);
11097 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11098 return got_error(GOT_ERR_NO_SPACE);
11099 printf("timestamp: %s\n", datebuf);
11102 err = got_object_id_str(&id_str, blob_id);
11105 printf("based on blob: %s\n", id_str);
11109 if (staged_blob_id) {
11110 err = got_object_id_str(&id_str, staged_blob_id);
11113 printf("based on staged blob: %s\n", id_str);
11118 err = got_object_id_str(&id_str, commit_id);
11121 printf("based on commit: %s\n", id_str);
11128 static const struct got_error *
11129 cmd_info(int argc, char *argv[])
11131 const struct got_error *error = NULL;
11132 struct got_worktree *worktree = NULL;
11133 char *cwd = NULL, *id_str = NULL;
11134 struct got_pathlist_head paths;
11135 struct got_pathlist_entry *pe;
11136 char *uuidstr = NULL;
11137 int ch, show_files = 0;
11139 TAILQ_INIT(&paths);
11141 while ((ch = getopt(argc, argv, "")) != -1) {
11153 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11157 cwd = getcwd(NULL, 0);
11159 error = got_error_from_errno("getcwd");
11163 error = got_worktree_open(&worktree, cwd);
11165 if (error->code == GOT_ERR_NOT_WORKTREE)
11166 error = wrap_not_worktree_error(error, "info", cwd);
11170 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11175 error = get_worktree_paths_from_argv(&paths, argc, argv,
11182 error = got_object_id_str(&id_str,
11183 got_worktree_get_base_commit_id(worktree));
11187 error = got_worktree_get_uuid(&uuidstr, worktree);
11191 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11192 printf("work tree base commit: %s\n", id_str);
11193 printf("work tree path prefix: %s\n",
11194 got_worktree_get_path_prefix(worktree));
11195 printf("work tree branch reference: %s\n",
11196 got_worktree_get_head_ref_name(worktree));
11197 printf("work tree UUID: %s\n", uuidstr);
11198 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11201 struct got_pathlist_entry *pe;
11202 TAILQ_FOREACH(pe, &paths, entry) {
11203 if (pe->path_len == 0)
11206 * Assume this path will fail. This will be corrected
11207 * in print_path_info() in case the path does suceeed.
11209 pe->data = (void *)got_error_path(pe->path,
11212 error = got_worktree_path_info(worktree, &paths,
11213 print_path_info, &paths, check_cancelled, NULL);
11216 TAILQ_FOREACH(pe, &paths, entry) {
11217 if (pe->data != NULL) {
11218 error = pe->data; /* bad path */
11224 TAILQ_FOREACH(pe, &paths, entry)
11225 free((char *)pe->path);
11226 got_pathlist_free(&paths);