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>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
59 #include "got_patch.h"
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 static volatile sig_atomic_t sigint_received;
66 static volatile sig_atomic_t sigpipe_received;
69 catch_sigint(int signo)
75 catch_sigpipe(int signo)
83 const struct got_error *(*cmd_main)(int, char *[]);
84 void (*cmd_usage)(void);
85 const char *cmd_alias;
88 __dead static void usage(int, int);
89 __dead static void usage_init(void);
90 __dead static void usage_import(void);
91 __dead static void usage_clone(void);
92 __dead static void usage_fetch(void);
93 __dead static void usage_checkout(void);
94 __dead static void usage_update(void);
95 __dead static void usage_log(void);
96 __dead static void usage_diff(void);
97 __dead static void usage_blame(void);
98 __dead static void usage_tree(void);
99 __dead static void usage_status(void);
100 __dead static void usage_ref(void);
101 __dead static void usage_branch(void);
102 __dead static void usage_tag(void);
103 __dead static void usage_add(void);
104 __dead static void usage_remove(void);
105 __dead static void usage_patch(void);
106 __dead static void usage_revert(void);
107 __dead static void usage_commit(void);
108 __dead static void usage_send(void);
109 __dead static void usage_cherrypick(void);
110 __dead static void usage_backout(void);
111 __dead static void usage_rebase(void);
112 __dead static void usage_histedit(void);
113 __dead static void usage_integrate(void);
114 __dead static void usage_merge(void);
115 __dead static void usage_stage(void);
116 __dead static void usage_unstage(void);
117 __dead static void usage_cat(void);
118 __dead static void usage_info(void);
120 static const struct got_error* cmd_init(int, char *[]);
121 static const struct got_error* cmd_import(int, char *[]);
122 static const struct got_error* cmd_clone(int, char *[]);
123 static const struct got_error* cmd_fetch(int, char *[]);
124 static const struct got_error* cmd_checkout(int, char *[]);
125 static const struct got_error* cmd_update(int, char *[]);
126 static const struct got_error* cmd_log(int, char *[]);
127 static const struct got_error* cmd_diff(int, char *[]);
128 static const struct got_error* cmd_blame(int, char *[]);
129 static const struct got_error* cmd_tree(int, char *[]);
130 static const struct got_error* cmd_status(int, char *[]);
131 static const struct got_error* cmd_ref(int, char *[]);
132 static const struct got_error* cmd_branch(int, char *[]);
133 static const struct got_error* cmd_tag(int, char *[]);
134 static const struct got_error* cmd_add(int, char *[]);
135 static const struct got_error* cmd_remove(int, char *[]);
136 static const struct got_error* cmd_patch(int, char *[]);
137 static const struct got_error* cmd_revert(int, char *[]);
138 static const struct got_error* cmd_commit(int, char *[]);
139 static const struct got_error* cmd_send(int, char *[]);
140 static const struct got_error* cmd_cherrypick(int, char *[]);
141 static const struct got_error* cmd_backout(int, char *[]);
142 static const struct got_error* cmd_rebase(int, char *[]);
143 static const struct got_error* cmd_histedit(int, char *[]);
144 static const struct got_error* cmd_integrate(int, char *[]);
145 static const struct got_error* cmd_merge(int, char *[]);
146 static const struct got_error* cmd_stage(int, char *[]);
147 static const struct got_error* cmd_unstage(int, char *[]);
148 static const struct got_error* cmd_cat(int, char *[]);
149 static const struct got_error* cmd_info(int, char *[]);
151 static const struct got_cmd got_commands[] = {
152 { "init", cmd_init, usage_init, "" },
153 { "import", cmd_import, usage_import, "im" },
154 { "clone", cmd_clone, usage_clone, "cl" },
155 { "fetch", cmd_fetch, usage_fetch, "fe" },
156 { "checkout", cmd_checkout, usage_checkout, "co" },
157 { "update", cmd_update, usage_update, "up" },
158 { "log", cmd_log, usage_log, "" },
159 { "diff", cmd_diff, usage_diff, "di" },
160 { "blame", cmd_blame, usage_blame, "bl" },
161 { "tree", cmd_tree, usage_tree, "tr" },
162 { "status", cmd_status, usage_status, "st" },
163 { "ref", cmd_ref, usage_ref, "" },
164 { "branch", cmd_branch, usage_branch, "br" },
165 { "tag", cmd_tag, usage_tag, "" },
166 { "add", cmd_add, usage_add, "" },
167 { "remove", cmd_remove, usage_remove, "rm" },
168 { "patch", cmd_patch, usage_patch, "pa" },
169 { "revert", cmd_revert, usage_revert, "rv" },
170 { "commit", cmd_commit, usage_commit, "ci" },
171 { "send", cmd_send, usage_send, "se" },
172 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
173 { "backout", cmd_backout, usage_backout, "bo" },
174 { "rebase", cmd_rebase, usage_rebase, "rb" },
175 { "histedit", cmd_histedit, usage_histedit, "he" },
176 { "integrate", cmd_integrate, usage_integrate,"ig" },
177 { "merge", cmd_merge, usage_merge, "mg" },
178 { "stage", cmd_stage, usage_stage, "sg" },
179 { "unstage", cmd_unstage, usage_unstage, "ug" },
180 { "cat", cmd_cat, usage_cat, "" },
181 { "info", cmd_info, usage_info, "" },
185 list_commands(FILE *fp)
189 fprintf(fp, "commands:");
190 for (i = 0; i < nitems(got_commands); i++) {
191 const struct got_cmd *cmd = &got_commands[i];
192 fprintf(fp, " %s", cmd->cmd_name);
198 option_conflict(char a, char b)
200 errx(1, "-%c and -%c options are mutually exclusive", a, b);
204 main(int argc, char *argv[])
206 const struct got_cmd *cmd;
209 int hflag = 0, Vflag = 0;
210 static const struct option longopts[] = {
211 { "version", no_argument, NULL, 'V' },
215 setlocale(LC_CTYPE, "");
217 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
237 got_version_print_str();
242 usage(hflag, hflag ? 0 : 1);
244 signal(SIGINT, catch_sigint);
245 signal(SIGPIPE, catch_sigpipe);
247 for (i = 0; i < nitems(got_commands); i++) {
248 const struct got_error *error;
250 cmd = &got_commands[i];
252 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
253 strcmp(cmd->cmd_alias, argv[0]) != 0)
259 error = cmd->cmd_main(argc, argv);
260 if (error && error->code != GOT_ERR_CANCELLED &&
261 error->code != GOT_ERR_PRIVSEP_EXIT &&
262 !(sigpipe_received &&
263 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
265 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
266 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
273 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
274 list_commands(stderr);
279 usage(int hflag, int status)
281 FILE *fp = (status == 0) ? stdout : stderr;
283 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
290 static const struct got_error *
291 get_editor(char **abspath)
293 const struct got_error *err = NULL;
298 editor = getenv("VISUAL");
300 editor = getenv("EDITOR");
303 err = got_path_find_prog(abspath, editor);
308 if (*abspath == NULL) {
309 *abspath = strdup("/bin/ed");
310 if (*abspath == NULL)
311 return got_error_from_errno("strdup");
317 static const struct got_error *
318 apply_unveil(const char *repo_path, int repo_read_only,
319 const char *worktree_path)
321 const struct got_error *err;
324 if (unveil("gmon.out", "rwc") != 0)
325 return got_error_from_errno2("unveil", "gmon.out");
327 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
328 return got_error_from_errno2("unveil", repo_path);
330 if (worktree_path && unveil(worktree_path, "rwc") != 0)
331 return got_error_from_errno2("unveil", worktree_path);
333 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
334 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
336 err = got_privsep_unveil_exec_helpers();
340 if (unveil(NULL, NULL) != 0)
341 return got_error_from_errno("unveil");
349 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
353 static const struct got_error *
354 cmd_init(int argc, char *argv[])
356 const struct got_error *error = NULL;
357 char *repo_path = NULL;
360 while ((ch = getopt(argc, argv, "")) != -1) {
372 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
378 repo_path = strdup(argv[0]);
379 if (repo_path == NULL)
380 return got_error_from_errno("strdup");
382 got_path_strip_trailing_slashes(repo_path);
384 error = got_path_mkdir(repo_path);
386 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
389 error = apply_unveil(repo_path, 0, NULL);
393 error = got_repo_init(repo_path);
402 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
403 "[-r repository-path] [-I pattern] path\n", getprogname());
408 spawn_editor(const char *editor, const char *file)
411 sig_t sighup, sigint, sigquit;
414 sighup = signal(SIGHUP, SIG_IGN);
415 sigint = signal(SIGINT, SIG_IGN);
416 sigquit = signal(SIGQUIT, SIG_IGN);
418 switch (pid = fork()) {
422 execl(editor, editor, file, (char *)NULL);
426 while (waitpid(pid, &st, 0) == -1)
431 (void)signal(SIGHUP, sighup);
432 (void)signal(SIGINT, sigint);
433 (void)signal(SIGQUIT, sigquit);
435 if (!WIFEXITED(st)) {
440 return WEXITSTATUS(st);
443 static const struct got_error *
444 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
445 const char *initial_content, size_t initial_content_len,
446 int require_modification)
448 const struct got_error *err = NULL;
454 size_t len, logmsg_len;
455 char *initial_content_stripped = NULL, *buf = NULL, *s;
459 if (stat(logmsg_path, &st) == -1)
460 return got_error_from_errno2("stat", logmsg_path);
462 if (spawn_editor(editor, logmsg_path) == -1)
463 return got_error_from_errno("failed spawning editor");
465 if (stat(logmsg_path, &st2) == -1)
466 return got_error_from_errno("stat");
468 if (require_modification &&
469 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
470 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 "no changes made to commit message, aborting");
474 * Set up a stripped version of the initial content without comments
475 * and blank lines. We need this in order to check if the message
476 * has in fact been edited.
478 initial_content_stripped = malloc(initial_content_len + 1);
479 if (initial_content_stripped == NULL)
480 return got_error_from_errno("malloc");
481 initial_content_stripped[0] = '\0';
483 buf = strdup(initial_content);
485 err = got_error_from_errno("strdup");
490 while ((line = strsep(&s, "\n")) != NULL) {
491 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
492 continue; /* remove comments and leading empty lines */
493 len = strlcat(initial_content_stripped, line,
494 initial_content_len + 1);
495 if (len >= initial_content_len + 1) {
496 err = got_error(GOT_ERR_NO_SPACE);
500 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
501 initial_content_stripped[len - 1] = '\0';
505 logmsg_len = st2.st_size;
506 *logmsg = malloc(logmsg_len + 1);
508 return got_error_from_errno("malloc");
511 fp = fopen(logmsg_path, "re");
513 err = got_error_from_errno("fopen");
518 while ((linelen = getline(&line, &linesize, fp)) != -1) {
519 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
520 continue; /* remove comments and leading empty lines */
521 len = strlcat(*logmsg, line, logmsg_len + 1);
522 if (len >= logmsg_len + 1) {
523 err = got_error(GOT_ERR_NO_SPACE);
529 err = got_ferror(fp, GOT_ERR_IO);
532 while (len > 0 && (*logmsg)[len - 1] == '\n') {
533 (*logmsg)[len - 1] = '\0';
538 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
539 "commit message cannot be empty, aborting");
542 if (require_modification &&
543 strcmp(*logmsg, initial_content_stripped) == 0)
544 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
545 "no changes made to commit message, aborting");
547 free(initial_content_stripped);
549 if (fp && fclose(fp) == EOF && err == NULL)
550 err = got_error_from_errno("fclose");
558 static const struct got_error *
559 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
560 const char *path_dir, const char *branch_name)
562 char *initial_content = NULL;
563 const struct got_error *err = NULL;
564 int initial_content_len;
567 initial_content_len = asprintf(&initial_content,
568 "\n# %s to be imported to branch %s\n", path_dir,
570 if (initial_content_len == -1)
571 return got_error_from_errno("asprintf");
573 err = got_opentemp_named_fd(logmsg_path, &fd,
574 GOT_TMPDIR_STR "/got-importmsg");
578 if (write(fd, initial_content, initial_content_len) == -1) {
579 err = got_error_from_errno2("write", *logmsg_path);
583 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
584 initial_content_len, 1);
586 if (fd != -1 && close(fd) == -1 && err == NULL)
587 err = got_error_from_errno2("close", *logmsg_path);
588 free(initial_content);
596 static const struct got_error *
597 import_progress(void *arg, const char *path)
599 printf("A %s\n", path);
604 valid_author(const char *author)
607 * Really dumb email address check; we're only doing this to
608 * avoid git's object parser breaking on commits we create.
610 while (*author && *author != '<')
614 while (*author && *author != '@')
618 while (*author && *author != '>')
620 return *author == '>';
623 static const struct got_error *
624 get_author(char **author, struct got_repository *repo,
625 struct got_worktree *worktree)
627 const struct got_error *err = NULL;
628 const char *got_author = NULL, *name, *email;
629 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
634 worktree_conf = got_worktree_get_gotconfig(worktree);
635 repo_conf = got_repo_get_gotconfig(repo);
638 * Priority of potential author information sources, from most
639 * significant to least significant:
640 * 1) work tree's .got/got.conf file
641 * 2) repository's got.conf file
642 * 3) repository's git config file
643 * 4) environment variables
644 * 5) global git config files (in user's home directory or /etc)
648 got_author = got_gotconfig_get_author(worktree_conf);
649 if (got_author == NULL)
650 got_author = got_gotconfig_get_author(repo_conf);
651 if (got_author == NULL) {
652 name = got_repo_get_gitconfig_author_name(repo);
653 email = got_repo_get_gitconfig_author_email(repo);
655 if (asprintf(author, "%s <%s>", name, email) == -1)
656 return got_error_from_errno("asprintf");
660 got_author = getenv("GOT_AUTHOR");
661 if (got_author == NULL) {
662 name = got_repo_get_global_gitconfig_author_name(repo);
663 email = got_repo_get_global_gitconfig_author_email(
666 if (asprintf(author, "%s <%s>", name, email)
668 return got_error_from_errno("asprintf");
671 /* TODO: Look up user in password database? */
672 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
676 *author = strdup(got_author);
678 return got_error_from_errno("strdup");
680 if (!valid_author(*author)) {
681 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
688 static const struct got_error *
689 get_gitconfig_path(char **gitconfig_path)
691 const char *homedir = getenv("HOME");
693 *gitconfig_path = NULL;
695 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
696 return got_error_from_errno("asprintf");
702 static const struct got_error *
703 cmd_import(int argc, char *argv[])
705 const struct got_error *error = NULL;
706 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
707 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
708 const char *branch_name = "main";
709 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
710 struct got_repository *repo = NULL;
711 struct got_reference *branch_ref = NULL, *head_ref = NULL;
712 struct got_object_id *new_commit_id = NULL;
714 struct got_pathlist_head ignores;
715 struct got_pathlist_entry *pe;
716 int preserve_logmsg = 0;
718 TAILQ_INIT(&ignores);
720 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
723 branch_name = optarg;
726 logmsg = strdup(optarg);
727 if (logmsg == NULL) {
728 error = got_error_from_errno("strdup");
733 repo_path = realpath(optarg, NULL);
734 if (repo_path == NULL) {
735 error = got_error_from_errno2("realpath",
741 if (optarg[0] == '\0')
743 error = got_pathlist_insert(&pe, &ignores, optarg,
758 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
766 if (repo_path == NULL) {
767 repo_path = getcwd(NULL, 0);
768 if (repo_path == NULL)
769 return got_error_from_errno("getcwd");
771 got_path_strip_trailing_slashes(repo_path);
772 error = get_gitconfig_path(&gitconfig_path);
775 error = got_repo_open(&repo, repo_path, gitconfig_path);
779 error = get_author(&author, repo, NULL);
784 * Don't let the user create a branch name with a leading '-'.
785 * While technically a valid reference name, this case is usually
786 * an unintended typo.
788 if (branch_name[0] == '-')
789 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
791 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
792 error = got_error_from_errno("asprintf");
796 error = got_ref_open(&branch_ref, repo, refname, 0);
798 if (error->code != GOT_ERR_NOT_REF)
801 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
802 "import target branch already exists");
806 path_dir = realpath(argv[0], NULL);
807 if (path_dir == NULL) {
808 error = got_error_from_errno2("realpath", argv[0]);
811 got_path_strip_trailing_slashes(path_dir);
814 * unveil(2) traverses exec(2); if an editor is used we have
815 * to apply unveil after the log message has been written.
817 if (logmsg == NULL || strlen(logmsg) == 0) {
818 error = get_editor(&editor);
822 error = collect_import_msg(&logmsg, &logmsg_path, editor,
825 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
832 if (unveil(path_dir, "r") != 0) {
833 error = got_error_from_errno2("unveil", path_dir);
839 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
846 error = got_repo_import(&new_commit_id, path_dir, logmsg,
847 author, &ignores, repo, import_progress, NULL);
854 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
861 error = got_ref_write(branch_ref, repo);
868 error = got_object_id_str(&id_str, new_commit_id);
875 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
877 if (error->code != GOT_ERR_NOT_REF) {
883 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
891 error = got_ref_write(head_ref, repo);
899 printf("Created branch %s with commit %s\n",
900 got_ref_get_name(branch_ref), id_str);
902 if (preserve_logmsg) {
903 fprintf(stderr, "%s: log message preserved in %s\n",
904 getprogname(), logmsg_path);
905 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
906 error = got_error_from_errno2("unlink", logmsg_path);
915 free(gitconfig_path);
917 got_ref_close(branch_ref);
919 got_ref_close(head_ref);
926 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
927 "[-R reference] repository-url [directory]\n", getprogname());
931 struct got_fetch_progress_arg {
932 char last_scaled_size[FMT_SCALED_STRSIZE];
937 struct got_repository *repo;
942 struct got_pathlist_head *symrefs;
943 struct got_pathlist_head *wanted_branches;
944 struct got_pathlist_head *wanted_refs;
948 const char *remote_repo_path;
950 int fetch_all_branches;
951 int mirror_references;
955 /* XXX forward declaration */
956 static const struct got_error *
957 create_config_files(const char *proto, const char *host, const char *port,
958 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
959 int mirror_references, struct got_pathlist_head *symrefs,
960 struct got_pathlist_head *wanted_branches,
961 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
963 static const struct got_error *
964 fetch_progress(void *arg, const char *message, off_t packfile_size,
965 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
967 const struct got_error *err = NULL;
968 struct got_fetch_progress_arg *a = arg;
969 char scaled_size[FMT_SCALED_STRSIZE];
970 int p_indexed, p_resolved;
971 int print_size = 0, print_indexed = 0, print_resolved = 0;
974 * In order to allow a failed clone to be resumed with 'got fetch'
975 * we try to create configuration files as soon as possible.
976 * Once the server has sent information about its default branch
977 * we have all required information.
979 if (a->create_configs && !a->configs_created &&
980 !TAILQ_EMPTY(a->config_info.symrefs)) {
981 err = create_config_files(a->config_info.proto,
982 a->config_info.host, a->config_info.port,
983 a->config_info.remote_repo_path,
984 a->config_info.git_url,
985 a->config_info.fetch_all_branches,
986 a->config_info.mirror_references,
987 a->config_info.symrefs,
988 a->config_info.wanted_branches,
989 a->config_info.wanted_refs, a->repo);
992 a->configs_created = 1;
995 if (a->verbosity < 0)
998 if (message && message[0] != '\0') {
999 printf("\rserver: %s", message);
1004 if (packfile_size > 0 || nobj_indexed > 0) {
1005 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1006 (a->last_scaled_size[0] == '\0' ||
1007 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1009 if (strlcpy(a->last_scaled_size, scaled_size,
1010 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1011 return got_error(GOT_ERR_NO_SPACE);
1013 if (nobj_indexed > 0) {
1014 p_indexed = (nobj_indexed * 100) / nobj_total;
1015 if (p_indexed != a->last_p_indexed) {
1016 a->last_p_indexed = p_indexed;
1021 if (nobj_resolved > 0) {
1022 p_resolved = (nobj_resolved * 100) /
1023 (nobj_total - nobj_loose);
1024 if (p_resolved != a->last_p_resolved) {
1025 a->last_p_resolved = p_resolved;
1033 if (print_size || print_indexed || print_resolved)
1036 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1038 printf("; indexing %d%%", p_indexed);
1040 printf("; resolving deltas %d%%", p_resolved);
1041 if (print_size || print_indexed || print_resolved)
1047 static const struct got_error *
1048 create_symref(const char *refname, struct got_reference *target_ref,
1049 int verbosity, struct got_repository *repo)
1051 const struct got_error *err;
1052 struct got_reference *head_symref;
1054 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1058 err = got_ref_write(head_symref, repo);
1059 if (err == NULL && verbosity > 0) {
1060 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1061 got_ref_get_name(target_ref));
1063 got_ref_close(head_symref);
1067 static const struct got_error *
1068 list_remote_refs(struct got_pathlist_head *symrefs,
1069 struct got_pathlist_head *refs)
1071 const struct got_error *err;
1072 struct got_pathlist_entry *pe;
1074 TAILQ_FOREACH(pe, symrefs, entry) {
1075 const char *refname = pe->path;
1076 const char *targetref = pe->data;
1078 printf("%s: %s\n", refname, targetref);
1081 TAILQ_FOREACH(pe, refs, entry) {
1082 const char *refname = pe->path;
1083 struct got_object_id *id = pe->data;
1086 err = got_object_id_str(&id_str, id);
1089 printf("%s: %s\n", refname, id_str);
1096 static const struct got_error *
1097 create_ref(const char *refname, struct got_object_id *id,
1098 int verbosity, struct got_repository *repo)
1100 const struct got_error *err = NULL;
1101 struct got_reference *ref;
1104 err = got_object_id_str(&id_str, id);
1108 err = got_ref_alloc(&ref, refname, id);
1112 err = got_ref_write(ref, repo);
1115 if (err == NULL && verbosity >= 0)
1116 printf("Created reference %s: %s\n", refname, id_str);
1123 match_wanted_ref(const char *refname, const char *wanted_ref)
1125 if (strncmp(refname, "refs/", 5) != 0)
1130 * Prevent fetching of references that won't make any
1131 * sense outside of the remote repository's context.
1133 if (strncmp(refname, "got/", 4) == 0)
1135 if (strncmp(refname, "remotes/", 8) == 0)
1138 if (strncmp(wanted_ref, "refs/", 5) == 0)
1141 /* Allow prefix match. */
1142 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1145 /* Allow exact match. */
1146 return (strcmp(refname, wanted_ref) == 0);
1150 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1152 struct got_pathlist_entry *pe;
1154 TAILQ_FOREACH(pe, wanted_refs, entry) {
1155 if (match_wanted_ref(refname, pe->path))
1162 static const struct got_error *
1163 create_wanted_ref(const char *refname, struct got_object_id *id,
1164 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1166 const struct got_error *err;
1167 char *remote_refname;
1169 if (strncmp("refs/", refname, 5) == 0)
1172 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1173 remote_repo_name, refname) == -1)
1174 return got_error_from_errno("asprintf");
1176 err = create_ref(remote_refname, id, verbosity, repo);
1177 free(remote_refname);
1181 static const struct got_error *
1182 create_gotconfig(const char *proto, const char *host, const char *port,
1183 const char *remote_repo_path, const char *default_branch,
1184 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1185 struct got_pathlist_head *wanted_refs, int mirror_references,
1186 struct got_repository *repo)
1188 const struct got_error *err = NULL;
1189 char *gotconfig_path = NULL;
1190 char *gotconfig = NULL;
1191 FILE *gotconfig_file = NULL;
1192 const char *branchname = NULL;
1193 char *branches = NULL, *refs = NULL;
1196 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1197 struct got_pathlist_entry *pe;
1198 TAILQ_FOREACH(pe, wanted_branches, entry) {
1200 branchname = pe->path;
1201 if (strncmp(branchname, "refs/heads/", 11) == 0)
1203 if (asprintf(&s, "%s\"%s\" ",
1204 branches ? branches : "", branchname) == -1) {
1205 err = got_error_from_errno("asprintf");
1211 } else if (!fetch_all_branches && default_branch) {
1212 branchname = default_branch;
1213 if (strncmp(branchname, "refs/heads/", 11) == 0)
1215 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1216 err = got_error_from_errno("asprintf");
1220 if (!TAILQ_EMPTY(wanted_refs)) {
1221 struct got_pathlist_entry *pe;
1222 TAILQ_FOREACH(pe, wanted_refs, entry) {
1224 const char *refname = pe->path;
1225 if (strncmp(refname, "refs/", 5) == 0)
1227 if (asprintf(&s, "%s\"%s\" ",
1228 refs ? refs : "", refname) == -1) {
1229 err = got_error_from_errno("asprintf");
1237 /* Create got.conf(5). */
1238 gotconfig_path = got_repo_get_path_gotconfig(repo);
1239 if (gotconfig_path == NULL) {
1240 err = got_error_from_errno("got_repo_get_path_gotconfig");
1243 gotconfig_file = fopen(gotconfig_path, "ae");
1244 if (gotconfig_file == NULL) {
1245 err = got_error_from_errno2("fopen", gotconfig_path);
1248 if (asprintf(&gotconfig,
1253 "\trepository \"%s\"\n"
1259 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1260 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1261 remote_repo_path, branches ? "\tbranch { " : "",
1262 branches ? branches : "", branches ? "}\n" : "",
1263 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1264 mirror_references ? "\tmirror-references yes\n" : "",
1265 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1266 err = got_error_from_errno("asprintf");
1269 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1270 if (n != strlen(gotconfig)) {
1271 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1276 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1277 err = got_error_from_errno2("fclose", gotconfig_path);
1278 free(gotconfig_path);
1283 static const struct got_error *
1284 create_gitconfig(const char *git_url, const char *default_branch,
1285 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1286 struct got_pathlist_head *wanted_refs, int mirror_references,
1287 struct got_repository *repo)
1289 const struct got_error *err = NULL;
1290 char *gitconfig_path = NULL;
1291 char *gitconfig = NULL;
1292 FILE *gitconfig_file = NULL;
1293 char *branches = NULL, *refs = NULL;
1294 const char *branchname;
1297 /* Create a config file Git can understand. */
1298 gitconfig_path = got_repo_get_path_gitconfig(repo);
1299 if (gitconfig_path == NULL) {
1300 err = got_error_from_errno("got_repo_get_path_gitconfig");
1303 gitconfig_file = fopen(gitconfig_path, "ae");
1304 if (gitconfig_file == NULL) {
1305 err = got_error_from_errno2("fopen", gitconfig_path);
1308 if (fetch_all_branches) {
1309 if (mirror_references) {
1310 if (asprintf(&branches,
1311 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1312 err = got_error_from_errno("asprintf");
1315 } else if (asprintf(&branches,
1316 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1317 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1318 err = got_error_from_errno("asprintf");
1321 } else if (!TAILQ_EMPTY(wanted_branches)) {
1322 struct got_pathlist_entry *pe;
1323 TAILQ_FOREACH(pe, wanted_branches, entry) {
1325 branchname = pe->path;
1326 if (strncmp(branchname, "refs/heads/", 11) == 0)
1328 if (mirror_references) {
1330 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1331 branches ? branches : "",
1332 branchname, branchname) == -1) {
1333 err = got_error_from_errno("asprintf");
1336 } else if (asprintf(&s,
1337 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1338 branches ? branches : "",
1339 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1340 branchname) == -1) {
1341 err = got_error_from_errno("asprintf");
1349 * If the server specified a default branch, use just that one.
1350 * Otherwise fall back to fetching all branches on next fetch.
1352 if (default_branch) {
1353 branchname = default_branch;
1354 if (strncmp(branchname, "refs/heads/", 11) == 0)
1357 branchname = "*"; /* fall back to all branches */
1358 if (mirror_references) {
1359 if (asprintf(&branches,
1360 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1361 branchname, branchname) == -1) {
1362 err = got_error_from_errno("asprintf");
1365 } else if (asprintf(&branches,
1366 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1367 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1368 branchname) == -1) {
1369 err = got_error_from_errno("asprintf");
1373 if (!TAILQ_EMPTY(wanted_refs)) {
1374 struct got_pathlist_entry *pe;
1375 TAILQ_FOREACH(pe, wanted_refs, entry) {
1377 const char *refname = pe->path;
1378 if (strncmp(refname, "refs/", 5) == 0)
1380 if (mirror_references) {
1382 "%s\tfetch = refs/%s:refs/%s\n",
1383 refs ? refs : "", refname, refname) == -1) {
1384 err = got_error_from_errno("asprintf");
1387 } else if (asprintf(&s,
1388 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1390 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1392 err = got_error_from_errno("asprintf");
1400 if (asprintf(&gitconfig,
1405 "\tfetch = refs/tags/*:refs/tags/*\n",
1406 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1407 refs ? refs : "") == -1) {
1408 err = got_error_from_errno("asprintf");
1411 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1412 if (n != strlen(gitconfig)) {
1413 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1417 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1418 err = got_error_from_errno2("fclose", gitconfig_path);
1419 free(gitconfig_path);
1424 static const struct got_error *
1425 create_config_files(const char *proto, const char *host, const char *port,
1426 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1427 int mirror_references, struct got_pathlist_head *symrefs,
1428 struct got_pathlist_head *wanted_branches,
1429 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1431 const struct got_error *err = NULL;
1432 const char *default_branch = NULL;
1433 struct got_pathlist_entry *pe;
1436 * If we asked for a set of wanted branches then use the first
1439 if (!TAILQ_EMPTY(wanted_branches)) {
1440 pe = TAILQ_FIRST(wanted_branches);
1441 default_branch = pe->path;
1443 /* First HEAD ref listed by server is the default branch. */
1444 TAILQ_FOREACH(pe, symrefs, entry) {
1445 const char *refname = pe->path;
1446 const char *target = pe->data;
1448 if (strcmp(refname, GOT_REF_HEAD) != 0)
1451 default_branch = target;
1456 /* Create got.conf(5). */
1457 err = create_gotconfig(proto, host, port, remote_repo_path,
1458 default_branch, fetch_all_branches, wanted_branches,
1459 wanted_refs, mirror_references, repo);
1463 /* Create a config file Git can understand. */
1464 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1465 wanted_branches, wanted_refs, mirror_references, repo);
1468 static const struct got_error *
1469 cmd_clone(int argc, char *argv[])
1471 const struct got_error *error = NULL;
1472 const char *uri, *dirname;
1473 char *proto, *host, *port, *repo_name, *server_path;
1474 char *default_destdir = NULL, *id_str = NULL;
1475 const char *repo_path;
1476 struct got_repository *repo = NULL;
1477 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1478 struct got_pathlist_entry *pe;
1479 struct got_object_id *pack_hash = NULL;
1480 int ch, fetchfd = -1, fetchstatus;
1481 pid_t fetchpid = -1;
1482 struct got_fetch_progress_arg fpa;
1483 char *git_url = NULL;
1484 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1485 int list_refs_only = 0;
1488 TAILQ_INIT(&symrefs);
1489 TAILQ_INIT(&wanted_branches);
1490 TAILQ_INIT(&wanted_refs);
1492 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1495 fetch_all_branches = 1;
1498 error = got_pathlist_append(&wanted_branches,
1507 mirror_references = 1;
1512 else if (verbosity < 3)
1519 error = got_pathlist_append(&wanted_refs,
1532 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1533 option_conflict('a', 'b');
1534 if (list_refs_only) {
1535 if (!TAILQ_EMPTY(&wanted_branches))
1536 option_conflict('l', 'b');
1537 if (fetch_all_branches)
1538 option_conflict('l', 'a');
1539 if (mirror_references)
1540 option_conflict('l', 'm');
1541 if (!TAILQ_EMPTY(&wanted_refs))
1542 option_conflict('l', 'R');
1554 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1559 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1560 host, port ? ":" : "", port ? port : "",
1561 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1562 error = got_error_from_errno("asprintf");
1566 if (strcmp(proto, "git") == 0) {
1568 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 "sendfd dns inet unveil", NULL) == -1)
1572 } else if (strcmp(proto, "git+ssh") == 0 ||
1573 strcmp(proto, "ssh") == 0) {
1575 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1576 "sendfd unveil", NULL) == -1)
1579 } else if (strcmp(proto, "http") == 0 ||
1580 strcmp(proto, "git+http") == 0) {
1581 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1584 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1587 if (dirname == NULL) {
1588 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1589 error = got_error_from_errno("asprintf");
1592 repo_path = default_destdir;
1594 repo_path = dirname;
1596 if (!list_refs_only) {
1597 error = got_path_mkdir(repo_path);
1599 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1600 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1602 if (!got_path_dir_is_empty(repo_path)) {
1603 error = got_error_path(repo_path,
1604 GOT_ERR_DIR_NOT_EMPTY);
1609 error = got_dial_apply_unveil(proto);
1613 error = apply_unveil(repo_path, 0, NULL);
1618 printf("Connecting to %s%s%s\n", host,
1619 port ? ":" : "", port ? port : "");
1621 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1622 server_path, verbosity);
1626 if (!list_refs_only) {
1627 error = got_repo_init(repo_path);
1630 error = got_repo_open(&repo, repo_path, NULL);
1635 fpa.last_scaled_size[0] = '\0';
1636 fpa.last_p_indexed = -1;
1637 fpa.last_p_resolved = -1;
1638 fpa.verbosity = verbosity;
1639 fpa.create_configs = 1;
1640 fpa.configs_created = 0;
1642 fpa.config_info.symrefs = &symrefs;
1643 fpa.config_info.wanted_branches = &wanted_branches;
1644 fpa.config_info.wanted_refs = &wanted_refs;
1645 fpa.config_info.proto = proto;
1646 fpa.config_info.host = host;
1647 fpa.config_info.port = port;
1648 fpa.config_info.remote_repo_path = server_path;
1649 fpa.config_info.git_url = git_url;
1650 fpa.config_info.fetch_all_branches = fetch_all_branches;
1651 fpa.config_info.mirror_references = mirror_references;
1652 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1653 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1654 fetch_all_branches, &wanted_branches, &wanted_refs,
1655 list_refs_only, verbosity, fetchfd, repo,
1656 fetch_progress, &fpa);
1660 if (list_refs_only) {
1661 error = list_remote_refs(&symrefs, &refs);
1665 if (pack_hash == NULL) {
1666 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1667 "server sent an empty pack file");
1670 error = got_object_id_str(&id_str, pack_hash);
1674 printf("\nFetched %s.pack\n", id_str);
1677 /* Set up references provided with the pack file. */
1678 TAILQ_FOREACH(pe, &refs, entry) {
1679 const char *refname = pe->path;
1680 struct got_object_id *id = pe->data;
1681 char *remote_refname;
1683 if (is_wanted_ref(&wanted_refs, refname) &&
1684 !mirror_references) {
1685 error = create_wanted_ref(refname, id,
1686 GOT_FETCH_DEFAULT_REMOTE_NAME,
1687 verbosity - 1, repo);
1693 error = create_ref(refname, id, verbosity - 1, repo);
1697 if (mirror_references)
1700 if (strncmp("refs/heads/", refname, 11) != 0)
1703 if (asprintf(&remote_refname,
1704 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1705 refname + 11) == -1) {
1706 error = got_error_from_errno("asprintf");
1709 error = create_ref(remote_refname, id, verbosity - 1, repo);
1710 free(remote_refname);
1715 /* Set the HEAD reference if the server provided one. */
1716 TAILQ_FOREACH(pe, &symrefs, entry) {
1717 struct got_reference *target_ref;
1718 const char *refname = pe->path;
1719 const char *target = pe->data;
1720 char *remote_refname = NULL, *remote_target = NULL;
1722 if (strcmp(refname, GOT_REF_HEAD) != 0)
1725 error = got_ref_open(&target_ref, repo, target, 0);
1727 if (error->code == GOT_ERR_NOT_REF) {
1734 error = create_symref(refname, target_ref, verbosity, repo);
1735 got_ref_close(target_ref);
1739 if (mirror_references)
1742 if (strncmp("refs/heads/", target, 11) != 0)
1745 if (asprintf(&remote_refname,
1746 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1748 error = got_error_from_errno("asprintf");
1751 if (asprintf(&remote_target,
1752 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 target + 11) == -1) {
1754 error = got_error_from_errno("asprintf");
1755 free(remote_refname);
1758 error = got_ref_open(&target_ref, repo, remote_target, 0);
1760 free(remote_refname);
1761 free(remote_target);
1762 if (error->code == GOT_ERR_NOT_REF) {
1768 error = create_symref(remote_refname, target_ref,
1769 verbosity - 1, repo);
1770 free(remote_refname);
1771 free(remote_target);
1772 got_ref_close(target_ref);
1778 * We failed to set the HEAD reference. If we asked for
1779 * a set of wanted branches use the first of one of those
1780 * which could be fetched instead.
1782 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1783 const char *target = pe->path;
1784 struct got_reference *target_ref;
1786 error = got_ref_open(&target_ref, repo, target, 0);
1788 if (error->code == GOT_ERR_NOT_REF) {
1795 error = create_symref(GOT_REF_HEAD, target_ref,
1797 got_ref_close(target_ref);
1805 printf("Created %s repository '%s'\n",
1806 mirror_references ? "mirrored" : "cloned", repo_path);
1809 if (kill(fetchpid, SIGTERM) == -1)
1810 error = got_error_from_errno("kill");
1811 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1812 error = got_error_from_errno("waitpid");
1814 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1815 error = got_error_from_errno("close");
1817 const struct got_error *close_err = got_repo_close(repo);
1821 TAILQ_FOREACH(pe, &refs, entry) {
1822 free((void *)pe->path);
1825 got_pathlist_free(&refs);
1826 TAILQ_FOREACH(pe, &symrefs, entry) {
1827 free((void *)pe->path);
1830 got_pathlist_free(&symrefs);
1831 got_pathlist_free(&wanted_branches);
1832 got_pathlist_free(&wanted_refs);
1839 free(default_destdir);
1844 static const struct got_error *
1845 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1846 int replace_tags, int verbosity, struct got_repository *repo)
1848 const struct got_error *err = NULL;
1849 char *new_id_str = NULL;
1850 struct got_object_id *old_id = NULL;
1852 err = got_object_id_str(&new_id_str, new_id);
1856 if (!replace_tags &&
1857 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1858 err = got_ref_resolve(&old_id, repo, ref);
1861 if (got_object_id_cmp(old_id, new_id) == 0)
1863 if (verbosity >= 0) {
1864 printf("Rejecting update of existing tag %s: %s\n",
1865 got_ref_get_name(ref), new_id_str);
1870 if (got_ref_is_symbolic(ref)) {
1871 if (verbosity >= 0) {
1872 printf("Replacing reference %s: %s\n",
1873 got_ref_get_name(ref),
1874 got_ref_get_symref_target(ref));
1876 err = got_ref_change_symref_to_ref(ref, new_id);
1879 err = got_ref_write(ref, repo);
1883 err = got_ref_resolve(&old_id, repo, ref);
1886 if (got_object_id_cmp(old_id, new_id) == 0)
1889 err = got_ref_change_ref(ref, new_id);
1892 err = got_ref_write(ref, repo);
1898 printf("Updated %s: %s\n", got_ref_get_name(ref),
1906 static const struct got_error *
1907 update_symref(const char *refname, struct got_reference *target_ref,
1908 int verbosity, struct got_repository *repo)
1910 const struct got_error *err = NULL, *unlock_err;
1911 struct got_reference *symref;
1912 int symref_is_locked = 0;
1914 err = got_ref_open(&symref, repo, refname, 1);
1916 if (err->code != GOT_ERR_NOT_REF)
1918 err = got_ref_alloc_symref(&symref, refname, target_ref);
1922 err = got_ref_write(symref, repo);
1927 printf("Created reference %s: %s\n",
1928 got_ref_get_name(symref),
1929 got_ref_get_symref_target(symref));
1931 symref_is_locked = 1;
1933 if (strcmp(got_ref_get_symref_target(symref),
1934 got_ref_get_name(target_ref)) == 0)
1937 err = got_ref_change_symref(symref,
1938 got_ref_get_name(target_ref));
1942 err = got_ref_write(symref, repo);
1947 printf("Updated %s: %s\n", got_ref_get_name(symref),
1948 got_ref_get_symref_target(symref));
1952 if (symref_is_locked) {
1953 unlock_err = got_ref_unlock(symref);
1954 if (unlock_err && err == NULL)
1957 got_ref_close(symref);
1964 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1965 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1966 "[remote-repository-name]\n",
1971 static const struct got_error *
1972 delete_missing_ref(struct got_reference *ref,
1973 int verbosity, struct got_repository *repo)
1975 const struct got_error *err = NULL;
1976 struct got_object_id *id = NULL;
1977 char *id_str = NULL;
1979 if (got_ref_is_symbolic(ref)) {
1980 err = got_ref_delete(ref, repo);
1983 if (verbosity >= 0) {
1984 printf("Deleted %s: %s\n",
1985 got_ref_get_name(ref),
1986 got_ref_get_symref_target(ref));
1989 err = got_ref_resolve(&id, repo, ref);
1992 err = got_object_id_str(&id_str, id);
1996 err = got_ref_delete(ref, repo);
1999 if (verbosity >= 0) {
2000 printf("Deleted %s: %s\n",
2001 got_ref_get_name(ref), id_str);
2010 static const struct got_error *
2011 delete_missing_refs(struct got_pathlist_head *their_refs,
2012 struct got_pathlist_head *their_symrefs,
2013 const struct got_remote_repo *remote,
2014 int verbosity, struct got_repository *repo)
2016 const struct got_error *err = NULL, *unlock_err;
2017 struct got_reflist_head my_refs;
2018 struct got_reflist_entry *re;
2019 struct got_pathlist_entry *pe;
2020 char *remote_namespace = NULL;
2021 char *local_refname = NULL;
2023 TAILQ_INIT(&my_refs);
2025 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2027 return got_error_from_errno("asprintf");
2029 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2033 TAILQ_FOREACH(re, &my_refs, entry) {
2034 const char *refname = got_ref_get_name(re->ref);
2035 const char *their_refname;
2037 if (remote->mirror_references) {
2038 their_refname = refname;
2040 if (strncmp(refname, remote_namespace,
2041 strlen(remote_namespace)) == 0) {
2042 if (strcmp(refname + strlen(remote_namespace),
2045 if (asprintf(&local_refname, "refs/heads/%s",
2046 refname + strlen(remote_namespace)) == -1) {
2047 err = got_error_from_errno("asprintf");
2050 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2053 their_refname = local_refname;
2056 TAILQ_FOREACH(pe, their_refs, entry) {
2057 if (strcmp(their_refname, pe->path) == 0)
2063 TAILQ_FOREACH(pe, their_symrefs, entry) {
2064 if (strcmp(their_refname, pe->path) == 0)
2070 err = delete_missing_ref(re->ref, verbosity, repo);
2074 if (local_refname) {
2075 struct got_reference *ref;
2076 err = got_ref_open(&ref, repo, local_refname, 1);
2078 if (err->code != GOT_ERR_NOT_REF)
2080 free(local_refname);
2081 local_refname = NULL;
2084 err = delete_missing_ref(ref, verbosity, repo);
2087 unlock_err = got_ref_unlock(ref);
2089 if (unlock_err && err == NULL) {
2094 free(local_refname);
2095 local_refname = NULL;
2099 free(remote_namespace);
2100 free(local_refname);
2104 static const struct got_error *
2105 update_wanted_ref(const char *refname, struct got_object_id *id,
2106 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2108 const struct got_error *err, *unlock_err;
2109 char *remote_refname;
2110 struct got_reference *ref;
2112 if (strncmp("refs/", refname, 5) == 0)
2115 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2116 remote_repo_name, refname) == -1)
2117 return got_error_from_errno("asprintf");
2119 err = got_ref_open(&ref, repo, remote_refname, 1);
2121 if (err->code != GOT_ERR_NOT_REF)
2123 err = create_ref(remote_refname, id, verbosity, repo);
2125 err = update_ref(ref, id, 0, verbosity, repo);
2126 unlock_err = got_ref_unlock(ref);
2127 if (unlock_err && err == NULL)
2132 free(remote_refname);
2136 static const struct got_error *
2137 delete_ref(struct got_repository *repo, struct got_reference *ref)
2139 const struct got_error *err = NULL;
2140 struct got_object_id *id = NULL;
2141 char *id_str = NULL;
2144 if (got_ref_is_symbolic(ref)) {
2145 target = got_ref_get_symref_target(ref);
2147 err = got_ref_resolve(&id, repo, ref);
2150 err = got_object_id_str(&id_str, id);
2156 err = got_ref_delete(ref, repo);
2160 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2167 static const struct got_error *
2168 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2170 const struct got_error *err = NULL;
2171 struct got_reflist_head refs;
2172 struct got_reflist_entry *re;
2177 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2178 err = got_error_from_errno("asprintf");
2181 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2185 TAILQ_FOREACH(re, &refs, entry)
2186 delete_ref(repo, re->ref);
2188 got_ref_list_free(&refs);
2192 static const struct got_error *
2193 cmd_fetch(int argc, char *argv[])
2195 const struct got_error *error = NULL, *unlock_err;
2196 char *cwd = NULL, *repo_path = NULL;
2197 const char *remote_name;
2198 char *proto = NULL, *host = NULL, *port = NULL;
2199 char *repo_name = NULL, *server_path = NULL;
2200 const struct got_remote_repo *remotes, *remote = NULL;
2202 char *id_str = NULL;
2203 struct got_repository *repo = NULL;
2204 struct got_worktree *worktree = NULL;
2205 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2206 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2207 struct got_pathlist_entry *pe;
2208 struct got_object_id *pack_hash = NULL;
2209 int i, ch, fetchfd = -1, fetchstatus;
2210 pid_t fetchpid = -1;
2211 struct got_fetch_progress_arg fpa;
2212 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2213 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2216 TAILQ_INIT(&symrefs);
2217 TAILQ_INIT(&wanted_branches);
2218 TAILQ_INIT(&wanted_refs);
2220 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2223 fetch_all_branches = 1;
2226 error = got_pathlist_append(&wanted_branches,
2238 repo_path = realpath(optarg, NULL);
2239 if (repo_path == NULL)
2240 return got_error_from_errno2("realpath",
2242 got_path_strip_trailing_slashes(repo_path);
2250 else if (verbosity < 3)
2257 error = got_pathlist_append(&wanted_refs,
2273 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2274 option_conflict('a', 'b');
2275 if (list_refs_only) {
2276 if (!TAILQ_EMPTY(&wanted_branches))
2277 option_conflict('l', 'b');
2278 if (fetch_all_branches)
2279 option_conflict('l', 'a');
2281 option_conflict('l', 'd');
2283 option_conflict('l', 'X');
2285 if (delete_remote) {
2286 if (fetch_all_branches)
2287 option_conflict('X', 'a');
2288 if (!TAILQ_EMPTY(&wanted_branches))
2289 option_conflict('X', 'b');
2291 option_conflict('X', 'd');
2293 option_conflict('X', 't');
2294 if (!TAILQ_EMPTY(&wanted_refs))
2295 option_conflict('X', 'R');
2300 errx(1, "-X option requires a remote name");
2301 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2302 } else if (argc == 1)
2303 remote_name = argv[0];
2307 cwd = getcwd(NULL, 0);
2309 error = got_error_from_errno("getcwd");
2313 if (repo_path == NULL) {
2314 error = got_worktree_open(&worktree, cwd);
2315 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2321 strdup(got_worktree_get_repo_path(worktree));
2322 if (repo_path == NULL)
2323 error = got_error_from_errno("strdup");
2327 repo_path = strdup(cwd);
2328 if (repo_path == NULL) {
2329 error = got_error_from_errno("strdup");
2335 error = got_repo_open(&repo, repo_path, NULL);
2339 if (delete_remote) {
2340 error = delete_refs_for_remote(repo, remote_name);
2341 goto done; /* nothing else to do */
2345 worktree_conf = got_worktree_get_gotconfig(worktree);
2346 if (worktree_conf) {
2347 got_gotconfig_get_remotes(&nremotes, &remotes,
2349 for (i = 0; i < nremotes; i++) {
2350 if (strcmp(remotes[i].name, remote_name) == 0) {
2351 remote = &remotes[i];
2357 if (remote == NULL) {
2358 repo_conf = got_repo_get_gotconfig(repo);
2360 got_gotconfig_get_remotes(&nremotes, &remotes,
2362 for (i = 0; i < nremotes; i++) {
2363 if (strcmp(remotes[i].name, remote_name) == 0) {
2364 remote = &remotes[i];
2370 if (remote == NULL) {
2371 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2372 for (i = 0; i < nremotes; i++) {
2373 if (strcmp(remotes[i].name, remote_name) == 0) {
2374 remote = &remotes[i];
2379 if (remote == NULL) {
2380 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2384 if (TAILQ_EMPTY(&wanted_branches)) {
2385 if (!fetch_all_branches)
2386 fetch_all_branches = remote->fetch_all_branches;
2387 for (i = 0; i < remote->nfetch_branches; i++) {
2388 got_pathlist_append(&wanted_branches,
2389 remote->fetch_branches[i], NULL);
2392 if (TAILQ_EMPTY(&wanted_refs)) {
2393 for (i = 0; i < remote->nfetch_refs; i++) {
2394 got_pathlist_append(&wanted_refs,
2395 remote->fetch_refs[i], NULL);
2399 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2400 &repo_name, remote->fetch_url);
2404 if (strcmp(proto, "git") == 0) {
2406 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2407 "sendfd dns inet unveil", NULL) == -1)
2410 } else if (strcmp(proto, "git+ssh") == 0 ||
2411 strcmp(proto, "ssh") == 0) {
2413 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2414 "sendfd unveil", NULL) == -1)
2417 } else if (strcmp(proto, "http") == 0 ||
2418 strcmp(proto, "git+http") == 0) {
2419 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2422 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2426 error = got_dial_apply_unveil(proto);
2430 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2435 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2436 port ? ":" : "", port ? port : "");
2438 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2439 server_path, verbosity);
2443 fpa.last_scaled_size[0] = '\0';
2444 fpa.last_p_indexed = -1;
2445 fpa.last_p_resolved = -1;
2446 fpa.verbosity = verbosity;
2448 fpa.create_configs = 0;
2449 fpa.configs_created = 0;
2450 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2451 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2452 remote->mirror_references, fetch_all_branches, &wanted_branches,
2453 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2454 fetch_progress, &fpa);
2458 if (list_refs_only) {
2459 error = list_remote_refs(&symrefs, &refs);
2463 if (pack_hash == NULL) {
2465 printf("Already up-to-date\n");
2466 } else if (verbosity >= 0) {
2467 error = got_object_id_str(&id_str, pack_hash);
2470 printf("\nFetched %s.pack\n", id_str);
2475 /* Update references provided with the pack file. */
2476 TAILQ_FOREACH(pe, &refs, entry) {
2477 const char *refname = pe->path;
2478 struct got_object_id *id = pe->data;
2479 struct got_reference *ref;
2480 char *remote_refname;
2482 if (is_wanted_ref(&wanted_refs, refname) &&
2483 !remote->mirror_references) {
2484 error = update_wanted_ref(refname, id,
2485 remote->name, verbosity, repo);
2491 if (remote->mirror_references ||
2492 strncmp("refs/tags/", refname, 10) == 0) {
2493 error = got_ref_open(&ref, repo, refname, 1);
2495 if (error->code != GOT_ERR_NOT_REF)
2497 error = create_ref(refname, id, verbosity,
2502 error = update_ref(ref, id, replace_tags,
2504 unlock_err = got_ref_unlock(ref);
2505 if (unlock_err && error == NULL)
2511 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2512 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2513 remote_name, refname + 11) == -1) {
2514 error = got_error_from_errno("asprintf");
2518 error = got_ref_open(&ref, repo, remote_refname, 1);
2520 if (error->code != GOT_ERR_NOT_REF)
2522 error = create_ref(remote_refname, id,
2527 error = update_ref(ref, id, replace_tags,
2529 unlock_err = got_ref_unlock(ref);
2530 if (unlock_err && error == NULL)
2537 /* Also create a local branch if none exists yet. */
2538 error = got_ref_open(&ref, repo, refname, 1);
2540 if (error->code != GOT_ERR_NOT_REF)
2542 error = create_ref(refname, id, verbosity,
2547 unlock_err = got_ref_unlock(ref);
2548 if (unlock_err && error == NULL)
2555 error = delete_missing_refs(&refs, &symrefs, remote,
2561 if (!remote->mirror_references) {
2562 /* Update remote HEAD reference if the server provided one. */
2563 TAILQ_FOREACH(pe, &symrefs, entry) {
2564 struct got_reference *target_ref;
2565 const char *refname = pe->path;
2566 const char *target = pe->data;
2567 char *remote_refname = NULL, *remote_target = NULL;
2569 if (strcmp(refname, GOT_REF_HEAD) != 0)
2572 if (strncmp("refs/heads/", target, 11) != 0)
2575 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2576 remote->name, refname) == -1) {
2577 error = got_error_from_errno("asprintf");
2580 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2581 remote->name, target + 11) == -1) {
2582 error = got_error_from_errno("asprintf");
2583 free(remote_refname);
2587 error = got_ref_open(&target_ref, repo, remote_target,
2590 free(remote_refname);
2591 free(remote_target);
2592 if (error->code == GOT_ERR_NOT_REF) {
2598 error = update_symref(remote_refname, target_ref,
2600 free(remote_refname);
2601 free(remote_target);
2602 got_ref_close(target_ref);
2609 if (kill(fetchpid, SIGTERM) == -1)
2610 error = got_error_from_errno("kill");
2611 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2612 error = got_error_from_errno("waitpid");
2614 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2615 error = got_error_from_errno("close");
2617 const struct got_error *close_err = got_repo_close(repo);
2622 got_worktree_close(worktree);
2623 TAILQ_FOREACH(pe, &refs, entry) {
2624 free((void *)pe->path);
2627 got_pathlist_free(&refs);
2628 TAILQ_FOREACH(pe, &symrefs, entry) {
2629 free((void *)pe->path);
2632 got_pathlist_free(&symrefs);
2633 got_pathlist_free(&wanted_branches);
2634 got_pathlist_free(&wanted_refs);
2649 usage_checkout(void)
2651 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2652 "[-p prefix] [-q] repository-path [worktree-path]\n",
2658 show_worktree_base_ref_warning(void)
2660 fprintf(stderr, "%s: warning: could not create a reference "
2661 "to the work tree's base commit; the commit could be "
2662 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2663 "repository writable and running 'got update' will prevent this\n",
2667 struct got_checkout_progress_arg {
2668 const char *worktree_path;
2669 int had_base_commit_ref_error;
2673 static const struct got_error *
2674 checkout_progress(void *arg, unsigned char status, const char *path)
2676 struct got_checkout_progress_arg *a = arg;
2678 /* Base commit bump happens silently. */
2679 if (status == GOT_STATUS_BUMP_BASE)
2682 if (status == GOT_STATUS_BASE_REF_ERR) {
2683 a->had_base_commit_ref_error = 1;
2687 while (path[0] == '/')
2690 if (a->verbosity >= 0)
2691 printf("%c %s/%s\n", status, a->worktree_path, path);
2696 static const struct got_error *
2697 check_cancelled(void *arg)
2699 if (sigint_received || sigpipe_received)
2700 return got_error(GOT_ERR_CANCELLED);
2704 static const struct got_error *
2705 check_linear_ancestry(struct got_object_id *commit_id,
2706 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2707 struct got_repository *repo)
2709 const struct got_error *err = NULL;
2710 struct got_object_id *yca_id;
2712 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2713 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2718 return got_error(GOT_ERR_ANCESTRY);
2721 * Require a straight line of history between the target commit
2722 * and the work tree's base commit.
2724 * Non-linear situations such as this require a rebase:
2726 * (commit) D F (base_commit)
2734 * 'got update' only handles linear cases:
2735 * Update forwards in time: A (base/yca) - B - C - D (commit)
2736 * Update backwards in time: D (base) - C - B - A (commit/yca)
2738 if (allow_forwards_in_time_only) {
2739 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2740 return got_error(GOT_ERR_ANCESTRY);
2741 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2742 got_object_id_cmp(base_commit_id, yca_id) != 0)
2743 return got_error(GOT_ERR_ANCESTRY);
2749 static const struct got_error *
2750 check_same_branch(struct got_object_id *commit_id,
2751 struct got_reference *head_ref, struct got_object_id *yca_id,
2752 struct got_repository *repo)
2754 const struct got_error *err = NULL;
2755 struct got_commit_graph *graph = NULL;
2756 struct got_object_id *head_commit_id = NULL;
2757 int is_same_branch = 0;
2759 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2763 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2767 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2772 err = got_commit_graph_open(&graph, "/", 1);
2776 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2777 check_cancelled, NULL);
2782 struct got_object_id *id;
2783 err = got_commit_graph_iter_next(&id, graph, repo,
2784 check_cancelled, NULL);
2786 if (err->code == GOT_ERR_ITER_COMPLETED)
2792 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2794 if (got_object_id_cmp(id, commit_id) == 0) {
2802 got_commit_graph_close(graph);
2803 free(head_commit_id);
2804 if (!err && !is_same_branch)
2805 err = got_error(GOT_ERR_ANCESTRY);
2809 static const struct got_error *
2810 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2812 static char msg[512];
2813 const char *branch_name;
2815 if (got_ref_is_symbolic(ref))
2816 branch_name = got_ref_get_symref_target(ref);
2818 branch_name = got_ref_get_name(ref);
2820 if (strncmp("refs/heads/", branch_name, 11) == 0)
2823 snprintf(msg, sizeof(msg),
2824 "target commit is not contained in branch '%s'; "
2825 "the branch to use must be specified with -b; "
2826 "if necessary a new branch can be created for "
2827 "this commit with 'got branch -c %s BRANCH_NAME'",
2828 branch_name, commit_id_str);
2830 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2833 static const struct got_error *
2834 cmd_checkout(int argc, char *argv[])
2836 const struct got_error *error = NULL;
2837 struct got_repository *repo = NULL;
2838 struct got_reference *head_ref = NULL, *ref = NULL;
2839 struct got_worktree *worktree = NULL;
2840 char *repo_path = NULL;
2841 char *worktree_path = NULL;
2842 const char *path_prefix = "";
2843 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2844 char *commit_id_str = NULL;
2845 struct got_object_id *commit_id = NULL;
2847 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2848 struct got_pathlist_head paths;
2849 struct got_checkout_progress_arg cpa;
2853 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2856 branch_name = optarg;
2859 commit_id_str = strdup(optarg);
2860 if (commit_id_str == NULL)
2861 return got_error_from_errno("strdup");
2867 path_prefix = optarg;
2882 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2883 "unveil", NULL) == -1)
2887 char *base, *dotgit;
2889 repo_path = realpath(argv[0], NULL);
2890 if (repo_path == NULL)
2891 return got_error_from_errno2("realpath", argv[0]);
2892 cwd = getcwd(NULL, 0);
2894 error = got_error_from_errno("getcwd");
2901 error = got_path_basename(&base, path);
2904 dotgit = strstr(base, ".git");
2907 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2908 error = got_error_from_errno("asprintf");
2913 } else if (argc == 2) {
2914 repo_path = realpath(argv[0], NULL);
2915 if (repo_path == NULL) {
2916 error = got_error_from_errno2("realpath", argv[0]);
2919 worktree_path = realpath(argv[1], NULL);
2920 if (worktree_path == NULL) {
2921 if (errno != ENOENT) {
2922 error = got_error_from_errno2("realpath",
2926 worktree_path = strdup(argv[1]);
2927 if (worktree_path == NULL) {
2928 error = got_error_from_errno("strdup");
2935 got_path_strip_trailing_slashes(repo_path);
2936 got_path_strip_trailing_slashes(worktree_path);
2938 error = got_repo_open(&repo, repo_path, NULL);
2942 /* Pre-create work tree path for unveil(2) */
2943 error = got_path_mkdir(worktree_path);
2945 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2946 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2948 if (!allow_nonempty &&
2949 !got_path_dir_is_empty(worktree_path)) {
2950 error = got_error_path(worktree_path,
2951 GOT_ERR_DIR_NOT_EMPTY);
2956 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2960 error = got_ref_open(&head_ref, repo, branch_name, 0);
2964 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2965 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2968 error = got_worktree_open(&worktree, worktree_path);
2972 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2976 if (!same_path_prefix) {
2977 error = got_error(GOT_ERR_PATH_PREFIX);
2981 if (commit_id_str) {
2982 struct got_reflist_head refs;
2984 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2988 error = got_repo_match_object_id(&commit_id, NULL,
2989 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2990 got_ref_list_free(&refs);
2993 error = check_linear_ancestry(commit_id,
2994 got_worktree_get_base_commit_id(worktree), 0, repo);
2995 if (error != NULL) {
2997 if (error->code == GOT_ERR_ANCESTRY) {
2998 error = checkout_ancestry_error(
2999 head_ref, commit_id_str);
3003 error = check_same_branch(commit_id, head_ref, NULL, repo);
3005 if (error->code == GOT_ERR_ANCESTRY) {
3006 error = checkout_ancestry_error(
3007 head_ref, commit_id_str);
3011 error = got_worktree_set_base_commit_id(worktree, repo,
3015 /* Expand potentially abbreviated commit ID string. */
3016 free(commit_id_str);
3017 error = got_object_id_str(&commit_id_str, commit_id);
3021 commit_id = got_object_id_dup(
3022 got_worktree_get_base_commit_id(worktree));
3023 if (commit_id == NULL) {
3024 error = got_error_from_errno("got_object_id_dup");
3027 error = got_object_id_str(&commit_id_str, commit_id);
3032 error = got_pathlist_append(&paths, "", NULL);
3035 cpa.worktree_path = worktree_path;
3036 cpa.had_base_commit_ref_error = 0;
3037 cpa.verbosity = verbosity;
3038 error = got_worktree_checkout_files(worktree, &paths, repo,
3039 checkout_progress, &cpa, check_cancelled, NULL);
3043 if (got_ref_is_symbolic(head_ref)) {
3044 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3047 refname = got_ref_get_name(ref);
3049 refname = got_ref_get_name(head_ref);
3050 printf("Checked out %s: %s\n", refname, commit_id_str);
3051 printf("Now shut up and hack\n");
3052 if (cpa.had_base_commit_ref_error)
3053 show_worktree_base_ref_warning();
3056 got_ref_close(head_ref);
3059 got_pathlist_free(&paths);
3060 free(commit_id_str);
3063 free(worktree_path);
3068 struct got_update_progress_arg {
3080 print_update_progress_stats(struct got_update_progress_arg *upa)
3082 if (!upa->did_something)
3085 if (upa->conflicts > 0)
3086 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3087 if (upa->obstructed > 0)
3088 printf("File paths obstructed by a non-regular file: %d\n",
3090 if (upa->not_updated > 0)
3091 printf("Files not updated because of existing merge "
3092 "conflicts: %d\n", upa->not_updated);
3096 * The meaning of some status codes differs between merge-style operations and
3097 * update operations. For example, the ! status code means "file was missing"
3098 * if changes were merged into the work tree, and "missing file was restored"
3099 * if the work tree was updated. This function should be used by any operation
3100 * which merges changes into the work tree without updating the work tree.
3103 print_merge_progress_stats(struct got_update_progress_arg *upa)
3105 if (!upa->did_something)
3108 if (upa->conflicts > 0)
3109 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3110 if (upa->obstructed > 0)
3111 printf("File paths obstructed by a non-regular file: %d\n",
3113 if (upa->missing > 0)
3114 printf("Files which had incoming changes but could not be "
3115 "found in the work tree: %d\n", upa->missing);
3116 if (upa->not_deleted > 0)
3117 printf("Files not deleted due to differences in deleted "
3118 "content: %d\n", upa->not_deleted);
3119 if (upa->unversioned > 0)
3120 printf("Files not merged because an unversioned file was "
3121 "found in the work tree: %d\n", upa->unversioned);
3127 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3133 static const struct got_error *
3134 update_progress(void *arg, unsigned char status, const char *path)
3136 struct got_update_progress_arg *upa = arg;
3138 if (status == GOT_STATUS_EXISTS ||
3139 status == GOT_STATUS_BASE_REF_ERR)
3142 upa->did_something = 1;
3144 /* Base commit bump happens silently. */
3145 if (status == GOT_STATUS_BUMP_BASE)
3148 if (status == GOT_STATUS_CONFLICT)
3150 if (status == GOT_STATUS_OBSTRUCTED)
3152 if (status == GOT_STATUS_CANNOT_UPDATE)
3154 if (status == GOT_STATUS_MISSING)
3156 if (status == GOT_STATUS_CANNOT_DELETE)
3158 if (status == GOT_STATUS_UNVERSIONED)
3161 while (path[0] == '/')
3163 if (upa->verbosity >= 0)
3164 printf("%c %s\n", status, path);
3169 static const struct got_error *
3170 switch_head_ref(struct got_reference *head_ref,
3171 struct got_object_id *commit_id, struct got_worktree *worktree,
3172 struct got_repository *repo)
3174 const struct got_error *err = NULL;
3176 int ref_has_moved = 0;
3178 /* Trivial case: switching between two different references. */
3179 if (strcmp(got_ref_get_name(head_ref),
3180 got_worktree_get_head_ref_name(worktree)) != 0) {
3181 printf("Switching work tree from %s to %s\n",
3182 got_worktree_get_head_ref_name(worktree),
3183 got_ref_get_name(head_ref));
3184 return got_worktree_set_head_ref(worktree, head_ref);
3187 err = check_linear_ancestry(commit_id,
3188 got_worktree_get_base_commit_id(worktree), 0, repo);
3190 if (err->code != GOT_ERR_ANCESTRY)
3197 /* Switching to a rebased branch with the same reference name. */
3198 err = got_object_id_str(&base_id_str,
3199 got_worktree_get_base_commit_id(worktree));
3202 printf("Reference %s now points at a different branch\n",
3203 got_worktree_get_head_ref_name(worktree));
3204 printf("Switching work tree from %s to %s\n", base_id_str,
3205 got_worktree_get_head_ref_name(worktree));
3209 static const struct got_error *
3210 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3212 const struct got_error *err;
3215 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3219 return got_error(GOT_ERR_REBASING);
3221 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3225 return got_error(GOT_ERR_HISTEDIT_BUSY);
3230 static const struct got_error *
3231 check_merge_in_progress(struct got_worktree *worktree,
3232 struct got_repository *repo)
3234 const struct got_error *err;
3237 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3241 return got_error(GOT_ERR_MERGE_BUSY);
3246 static const struct got_error *
3247 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3248 char *argv[], struct got_worktree *worktree)
3250 const struct got_error *err = NULL;
3252 struct got_pathlist_entry *new;
3258 return got_error_from_errno("strdup");
3259 return got_pathlist_append(paths, path, NULL);
3262 for (i = 0; i < argc; i++) {
3263 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3266 err = got_pathlist_insert(&new, paths, path, NULL);
3267 if (err || new == NULL /* duplicate */) {
3277 static const struct got_error *
3278 wrap_not_worktree_error(const struct got_error *orig_err,
3279 const char *cmdname, const char *path)
3281 const struct got_error *err;
3282 struct got_repository *repo;
3283 static char msg[512];
3285 err = got_repo_open(&repo, path, NULL);
3289 snprintf(msg, sizeof(msg),
3290 "'got %s' needs a work tree in addition to a git repository\n"
3291 "Work trees can be checked out from this Git repository with "
3293 "The got(1) manual page contains more information.", cmdname);
3294 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3295 got_repo_close(repo);
3299 static const struct got_error *
3300 cmd_update(int argc, char *argv[])
3302 const struct got_error *error = NULL;
3303 struct got_repository *repo = NULL;
3304 struct got_worktree *worktree = NULL;
3305 char *worktree_path = NULL;
3306 struct got_object_id *commit_id = NULL;
3307 char *commit_id_str = NULL;
3308 const char *branch_name = NULL;
3309 struct got_reference *head_ref = NULL;
3310 struct got_pathlist_head paths;
3311 struct got_pathlist_entry *pe;
3312 int ch, verbosity = 0;
3313 struct got_update_progress_arg upa;
3317 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3320 branch_name = optarg;
3323 commit_id_str = strdup(optarg);
3324 if (commit_id_str == NULL)
3325 return got_error_from_errno("strdup");
3340 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3341 "unveil", NULL) == -1)
3344 worktree_path = getcwd(NULL, 0);
3345 if (worktree_path == NULL) {
3346 error = got_error_from_errno("getcwd");
3349 error = got_worktree_open(&worktree, worktree_path);
3351 if (error->code == GOT_ERR_NOT_WORKTREE)
3352 error = wrap_not_worktree_error(error, "update",
3357 error = check_rebase_or_histedit_in_progress(worktree);
3361 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3366 error = apply_unveil(got_repo_get_path(repo), 0,
3367 got_worktree_get_root_path(worktree));
3371 error = check_merge_in_progress(worktree, repo);
3375 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3379 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3380 got_worktree_get_head_ref_name(worktree), 0);
3383 if (commit_id_str == NULL) {
3384 error = got_ref_resolve(&commit_id, repo, head_ref);
3387 error = got_object_id_str(&commit_id_str, commit_id);
3391 struct got_reflist_head refs;
3393 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3397 error = got_repo_match_object_id(&commit_id, NULL,
3398 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3399 got_ref_list_free(&refs);
3400 free(commit_id_str);
3401 commit_id_str = NULL;
3404 error = got_object_id_str(&commit_id_str, commit_id);
3410 struct got_object_id *head_commit_id;
3411 TAILQ_FOREACH(pe, &paths, entry) {
3412 if (pe->path_len == 0)
3414 error = got_error_msg(GOT_ERR_BAD_PATH,
3415 "switching between branches requires that "
3416 "the entire work tree gets updated");
3419 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3422 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3424 free(head_commit_id);
3427 error = check_same_branch(commit_id, head_ref, NULL, repo);
3430 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3434 error = check_linear_ancestry(commit_id,
3435 got_worktree_get_base_commit_id(worktree), 0, repo);
3436 if (error != NULL) {
3437 if (error->code == GOT_ERR_ANCESTRY)
3438 error = got_error(GOT_ERR_BRANCH_MOVED);
3441 error = check_same_branch(commit_id, head_ref, NULL, repo);
3446 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3448 error = got_worktree_set_base_commit_id(worktree, repo,
3454 memset(&upa, 0, sizeof(upa));
3455 upa.verbosity = verbosity;
3456 error = got_worktree_checkout_files(worktree, &paths, repo,
3457 update_progress, &upa, check_cancelled, NULL);
3461 if (upa.did_something) {
3462 printf("Updated to %s: %s\n",
3463 got_worktree_get_head_ref_name(worktree), commit_id_str);
3465 printf("Already up-to-date\n");
3466 print_update_progress_stats(&upa);
3468 free(worktree_path);
3469 TAILQ_FOREACH(pe, &paths, entry)
3470 free((char *)pe->path);
3471 got_pathlist_free(&paths);
3473 free(commit_id_str);
3477 static const struct got_error *
3478 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3479 const char *path, int diff_context, int ignore_whitespace,
3480 int force_text_diff, struct got_repository *repo)
3482 const struct got_error *err = NULL;
3483 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3486 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3491 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3495 while (path[0] == '/')
3497 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3498 diff_context, ignore_whitespace, force_text_diff, stdout);
3501 got_object_blob_close(blob1);
3502 got_object_blob_close(blob2);
3506 static const struct got_error *
3507 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3508 const char *path, int diff_context, int ignore_whitespace,
3509 int force_text_diff, struct got_repository *repo)
3511 const struct got_error *err = NULL;
3512 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3513 struct got_diff_blob_output_unidiff_arg arg;
3516 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3521 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3525 arg.diff_context = diff_context;
3526 arg.ignore_whitespace = ignore_whitespace;
3527 arg.force_text_diff = force_text_diff;
3528 arg.outfile = stdout;
3529 arg.line_offsets = NULL;
3531 while (path[0] == '/')
3533 err = got_diff_tree(tree1, tree2, path, path, repo,
3534 got_diff_blob_output_unidiff, &arg, 1);
3537 got_object_tree_close(tree1);
3539 got_object_tree_close(tree2);
3543 static const struct got_error *
3544 get_changed_paths(struct got_pathlist_head *paths,
3545 struct got_commit_object *commit, struct got_repository *repo)
3547 const struct got_error *err = NULL;
3548 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3549 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3550 struct got_object_qid *qid;
3552 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3554 struct got_commit_object *pcommit;
3555 err = got_object_open_as_commit(&pcommit, repo,
3560 tree_id1 = got_object_id_dup(
3561 got_object_commit_get_tree_id(pcommit));
3562 if (tree_id1 == NULL) {
3563 got_object_commit_close(pcommit);
3564 return got_error_from_errno("got_object_id_dup");
3566 got_object_commit_close(pcommit);
3571 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3576 tree_id2 = got_object_commit_get_tree_id(commit);
3577 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3581 err = got_diff_tree(tree1, tree2, "", "", repo,
3582 got_diff_tree_collect_changed_paths, paths, 0);
3585 got_object_tree_close(tree1);
3587 got_object_tree_close(tree2);
3592 static const struct got_error *
3593 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3594 const char *path, int diff_context, struct got_repository *repo)
3596 const struct got_error *err = NULL;
3597 struct got_commit_object *pcommit = NULL;
3598 char *id_str1 = NULL, *id_str2 = NULL;
3599 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3600 struct got_object_qid *qid;
3602 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3604 err = got_object_open_as_commit(&pcommit, repo,
3610 if (path && path[0] != '\0') {
3612 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3615 err = got_object_id_str(&id_str2, obj_id2);
3621 err = got_object_id_by_path(&obj_id1, repo,
3624 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3629 err = got_object_id_str(&id_str1, obj_id1);
3636 err = got_object_get_type(&obj_type, repo, obj_id2);
3641 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3643 case GOT_OBJ_TYPE_BLOB:
3644 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3647 case GOT_OBJ_TYPE_TREE:
3648 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3652 err = got_error(GOT_ERR_OBJ_TYPE);
3658 obj_id2 = got_object_commit_get_tree_id(commit);
3659 err = got_object_id_str(&id_str2, obj_id2);
3663 obj_id1 = got_object_commit_get_tree_id(pcommit);
3664 err = got_object_id_str(&id_str1, obj_id1);
3668 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3670 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3677 got_object_commit_close(pcommit);
3682 get_datestr(time_t *time, char *datebuf)
3684 struct tm mytm, *tm;
3687 tm = gmtime_r(time, &mytm);
3690 s = asctime_r(tm, datebuf);
3693 p = strchr(s, '\n');
3699 static const struct got_error *
3700 match_logmsg(int *have_match, struct got_object_id *id,
3701 struct got_commit_object *commit, regex_t *regex)
3703 const struct got_error *err = NULL;
3704 regmatch_t regmatch;
3705 char *id_str = NULL, *logmsg = NULL;
3709 err = got_object_id_str(&id_str, id);
3713 err = got_object_commit_get_logmsg(&logmsg, commit);
3717 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3726 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3729 regmatch_t regmatch;
3730 struct got_pathlist_entry *pe;
3734 TAILQ_FOREACH(pe, changed_paths, entry) {
3735 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3742 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3744 static const struct got_error*
3745 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3746 struct got_object_id *id, struct got_repository *repo)
3748 static const struct got_error *err = NULL;
3749 struct got_reflist_entry *re;
3755 TAILQ_FOREACH(re, refs, entry) {
3756 struct got_tag_object *tag = NULL;
3757 struct got_object_id *ref_id;
3760 name = got_ref_get_name(re->ref);
3761 if (strcmp(name, GOT_REF_HEAD) == 0)
3763 if (strncmp(name, "refs/", 5) == 0)
3765 if (strncmp(name, "got/", 4) == 0)
3767 if (strncmp(name, "heads/", 6) == 0)
3769 if (strncmp(name, "remotes/", 8) == 0) {
3771 s = strstr(name, "/" GOT_REF_HEAD);
3772 if (s != NULL && s[strlen(s)] == '\0')
3775 err = got_ref_resolve(&ref_id, repo, re->ref);
3778 if (strncmp(name, "tags/", 5) == 0) {
3779 err = got_object_open_as_tag(&tag, repo, ref_id);
3781 if (err->code != GOT_ERR_OBJ_TYPE) {
3785 /* Ref points at something other than a tag. */
3790 cmp = got_object_id_cmp(tag ?
3791 got_object_tag_get_object_id(tag) : ref_id, id);
3794 got_object_tag_close(tag);
3798 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3799 s ? ", " : "", name) == -1) {
3800 err = got_error_from_errno("asprintf");
3811 static const struct got_error *
3812 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3813 struct got_repository *repo, const char *path,
3814 struct got_pathlist_head *changed_paths, int show_patch,
3815 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3816 const char *custom_refs_str)
3818 const struct got_error *err = NULL;
3819 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3821 time_t committer_time;
3822 const char *author, *committer;
3823 char *refs_str = NULL;
3825 err = got_object_id_str(&id_str, id);
3829 if (custom_refs_str == NULL) {
3830 struct got_reflist_head *refs;
3831 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3833 err = build_refs_str(&refs_str, refs, id, repo);
3839 printf(GOT_COMMIT_SEP_STR);
3840 if (custom_refs_str)
3841 printf("commit %s (%s)\n", id_str, custom_refs_str);
3843 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3844 refs_str ? refs_str : "", refs_str ? ")" : "");
3849 printf("from: %s\n", got_object_commit_get_author(commit));
3850 committer_time = got_object_commit_get_committer_time(commit);
3851 datestr = get_datestr(&committer_time, datebuf);
3853 printf("date: %s UTC\n", datestr);
3854 author = got_object_commit_get_author(commit);
3855 committer = got_object_commit_get_committer(commit);
3856 if (strcmp(author, committer) != 0)
3857 printf("via: %s\n", committer);
3858 if (got_object_commit_get_nparents(commit) > 1) {
3859 const struct got_object_id_queue *parent_ids;
3860 struct got_object_qid *qid;
3862 parent_ids = got_object_commit_get_parent_ids(commit);
3863 STAILQ_FOREACH(qid, parent_ids, entry) {
3864 err = got_object_id_str(&id_str, qid->id);
3867 printf("parent %d: %s\n", n++, id_str);
3873 err = got_object_commit_get_logmsg(&logmsg0, commit);
3879 line = strsep(&logmsg, "\n");
3881 printf(" %s\n", line);
3885 if (changed_paths) {
3886 struct got_pathlist_entry *pe;
3887 TAILQ_FOREACH(pe, changed_paths, entry) {
3888 struct got_diff_changed_path *cp = pe->data;
3889 printf(" %c %s\n", cp->status, pe->path);
3894 err = print_patch(commit, id, path, diff_context, repo);
3899 if (fflush(stdout) != 0 && err == NULL)
3900 err = got_error_from_errno("fflush");
3907 static const struct got_error *
3908 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3909 struct got_repository *repo, const char *path, int show_changed_paths,
3910 int show_patch, const char *search_pattern, int diff_context, int limit,
3911 int log_branches, int reverse_display_order,
3912 struct got_reflist_object_id_map *refs_idmap)
3914 const struct got_error *err;
3915 struct got_commit_graph *graph;
3918 struct got_object_id_queue reversed_commits;
3919 struct got_object_qid *qid;
3920 struct got_commit_object *commit;
3921 struct got_pathlist_head changed_paths;
3922 struct got_pathlist_entry *pe;
3924 STAILQ_INIT(&reversed_commits);
3925 TAILQ_INIT(&changed_paths);
3927 if (search_pattern && regcomp(®ex, search_pattern,
3928 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3929 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3931 err = got_commit_graph_open(&graph, path, !log_branches);
3934 err = got_commit_graph_iter_start(graph, root_id, repo,
3935 check_cancelled, NULL);
3939 struct got_object_id *id;
3941 if (sigint_received || sigpipe_received)
3944 err = got_commit_graph_iter_next(&id, graph, repo,
3945 check_cancelled, NULL);
3947 if (err->code == GOT_ERR_ITER_COMPLETED)
3954 err = got_object_open_as_commit(&commit, repo, id);
3958 if (show_changed_paths && !reverse_display_order) {
3959 err = get_changed_paths(&changed_paths, commit, repo);
3964 if (search_pattern) {
3965 err = match_logmsg(&have_match, id, commit, ®ex);
3967 got_object_commit_close(commit);
3970 if (have_match == 0 && show_changed_paths)
3971 match_changed_paths(&have_match,
3972 &changed_paths, ®ex);
3973 if (have_match == 0) {
3974 got_object_commit_close(commit);
3975 TAILQ_FOREACH(pe, &changed_paths, entry) {
3976 free((char *)pe->path);
3979 got_pathlist_free(&changed_paths);
3984 if (reverse_display_order) {
3985 err = got_object_qid_alloc(&qid, id);
3988 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3989 got_object_commit_close(commit);
3991 err = print_commit(commit, id, repo, path,
3992 show_changed_paths ? &changed_paths : NULL,
3993 show_patch, diff_context, refs_idmap, NULL);
3994 got_object_commit_close(commit);
3998 if ((limit && --limit == 0) ||
3999 (end_id && got_object_id_cmp(id, end_id) == 0))
4002 TAILQ_FOREACH(pe, &changed_paths, entry) {
4003 free((char *)pe->path);
4006 got_pathlist_free(&changed_paths);
4008 if (reverse_display_order) {
4009 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4010 err = got_object_open_as_commit(&commit, repo, qid->id);
4013 if (show_changed_paths) {
4014 err = get_changed_paths(&changed_paths,
4019 err = print_commit(commit, qid->id, repo, path,
4020 show_changed_paths ? &changed_paths : NULL,
4021 show_patch, diff_context, refs_idmap, NULL);
4022 got_object_commit_close(commit);
4025 TAILQ_FOREACH(pe, &changed_paths, entry) {
4026 free((char *)pe->path);
4029 got_pathlist_free(&changed_paths);
4033 while (!STAILQ_EMPTY(&reversed_commits)) {
4034 qid = STAILQ_FIRST(&reversed_commits);
4035 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4036 got_object_qid_free(qid);
4038 TAILQ_FOREACH(pe, &changed_paths, entry) {
4039 free((char *)pe->path);
4042 got_pathlist_free(&changed_paths);
4045 got_commit_graph_close(graph);
4052 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4053 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4054 "[-R] [path]\n", getprogname());
4059 get_default_log_limit(void)
4061 const char *got_default_log_limit;
4065 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4066 if (got_default_log_limit == NULL)
4068 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4074 static const struct got_error *
4075 cmd_log(int argc, char *argv[])
4077 const struct got_error *error;
4078 struct got_repository *repo = NULL;
4079 struct got_worktree *worktree = NULL;
4080 struct got_object_id *start_id = NULL, *end_id = NULL;
4081 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4082 const char *start_commit = NULL, *end_commit = NULL;
4083 const char *search_pattern = NULL;
4084 int diff_context = -1, ch;
4085 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4086 int reverse_display_order = 0;
4088 struct got_reflist_head refs;
4089 struct got_reflist_object_id_map *refs_idmap = NULL;
4094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4100 limit = get_default_log_limit();
4102 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4108 show_changed_paths = 1;
4111 start_commit = optarg;
4114 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4117 errx(1, "number of context lines is %s: %s",
4121 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4123 errx(1, "number of commits is %s: %s",
4130 repo_path = realpath(optarg, NULL);
4131 if (repo_path == NULL)
4132 return got_error_from_errno2("realpath",
4134 got_path_strip_trailing_slashes(repo_path);
4137 reverse_display_order = 1;
4140 search_pattern = optarg;
4143 end_commit = optarg;
4154 if (diff_context == -1)
4156 else if (!show_patch)
4157 errx(1, "-C requires -p");
4159 cwd = getcwd(NULL, 0);
4161 error = got_error_from_errno("getcwd");
4165 if (repo_path == NULL) {
4166 error = got_worktree_open(&worktree, cwd);
4167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4174 error = got_worktree_resolve_path(&path, worktree,
4179 path = strdup(argv[0]);
4181 error = got_error_from_errno("strdup");
4185 } else if (argc != 0)
4188 if (repo_path == NULL) {
4189 repo_path = worktree ?
4190 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4192 if (repo_path == NULL) {
4193 error = got_error_from_errno("strdup");
4197 error = got_repo_open(&repo, repo_path, NULL);
4201 error = apply_unveil(got_repo_get_path(repo), 1,
4202 worktree ? got_worktree_get_root_path(worktree) : NULL);
4206 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4210 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4214 if (start_commit == NULL) {
4215 struct got_reference *head_ref;
4216 struct got_commit_object *commit = NULL;
4217 error = got_ref_open(&head_ref, repo,
4218 worktree ? got_worktree_get_head_ref_name(worktree)
4222 error = got_ref_resolve(&start_id, repo, head_ref);
4223 got_ref_close(head_ref);
4226 error = got_object_open_as_commit(&commit, repo,
4230 got_object_commit_close(commit);
4232 error = got_repo_match_object_id(&start_id, NULL,
4233 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4237 if (end_commit != NULL) {
4238 error = got_repo_match_object_id(&end_id, NULL,
4239 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4246 * If a path was specified on the command line it was resolved
4247 * to a path in the work tree above. Prepend the work tree's
4248 * path prefix to obtain the corresponding in-repository path.
4252 prefix = got_worktree_get_path_prefix(worktree);
4253 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4254 (path[0] != '\0') ? "/" : "", path) == -1) {
4255 error = got_error_from_errno("asprintf");
4260 error = got_repo_map_path(&in_repo_path, repo,
4266 path = in_repo_path;
4270 /* Release work tree lock. */
4271 got_worktree_close(worktree);
4275 error = print_commits(start_id, end_id, repo, path ? path : "",
4276 show_changed_paths, show_patch, search_pattern, diff_context,
4277 limit, log_branches, reverse_display_order, refs_idmap);
4283 got_worktree_close(worktree);
4285 const struct got_error *close_err = got_repo_close(repo);
4290 got_reflist_object_id_map_free(refs_idmap);
4291 got_ref_list_free(&refs);
4298 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4299 "[-r repository-path] [-s] [-w] [-P] "
4300 "[object1 object2 | path ...]\n", getprogname());
4304 struct print_diff_arg {
4305 struct got_repository *repo;
4306 struct got_worktree *worktree;
4311 int ignore_whitespace;
4312 int force_text_diff;
4316 * Create a file which contains the target path of a symlink so we can feed
4317 * it as content to the diff engine.
4319 static const struct got_error *
4320 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4321 const char *abspath)
4323 const struct got_error *err = NULL;
4324 char target_path[PATH_MAX];
4325 ssize_t target_len, outlen;
4330 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4331 if (target_len == -1)
4332 return got_error_from_errno2("readlinkat", abspath);
4334 target_len = readlink(abspath, target_path, PATH_MAX);
4335 if (target_len == -1)
4336 return got_error_from_errno2("readlink", abspath);
4339 *fd = got_opentempfd();
4341 return got_error_from_errno("got_opentempfd");
4343 outlen = write(*fd, target_path, target_len);
4345 err = got_error_from_errno("got_opentempfd");
4349 if (lseek(*fd, 0, SEEK_SET) == -1) {
4350 err = got_error_from_errno2("lseek", abspath);
4361 static const struct got_error *
4362 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4363 const char *path, struct got_object_id *blob_id,
4364 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4365 int dirfd, const char *de_name)
4367 struct print_diff_arg *a = arg;
4368 const struct got_error *err = NULL;
4369 struct got_blob_object *blob1 = NULL;
4372 char *abspath = NULL, *label1 = NULL;
4375 if (a->diff_staged) {
4376 if (staged_status != GOT_STATUS_MODIFY &&
4377 staged_status != GOT_STATUS_ADD &&
4378 staged_status != GOT_STATUS_DELETE)
4381 if (staged_status == GOT_STATUS_DELETE)
4383 if (status == GOT_STATUS_NONEXISTENT)
4384 return got_error_set_errno(ENOENT, path);
4385 if (status != GOT_STATUS_MODIFY &&
4386 status != GOT_STATUS_ADD &&
4387 status != GOT_STATUS_DELETE &&
4388 status != GOT_STATUS_CONFLICT)
4392 if (!a->header_shown) {
4393 printf("diff %s %s%s\n", a->id_str,
4394 got_worktree_get_root_path(a->worktree),
4395 a->diff_staged ? " (staged changes)" : "");
4396 a->header_shown = 1;
4399 if (a->diff_staged) {
4400 const char *label1 = NULL, *label2 = NULL;
4401 switch (staged_status) {
4402 case GOT_STATUS_MODIFY:
4406 case GOT_STATUS_ADD:
4409 case GOT_STATUS_DELETE:
4413 return got_error(GOT_ERR_FILE_STATUS);
4415 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4416 staged_blob_id, label1, label2, a->diff_context,
4417 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4420 if (staged_status == GOT_STATUS_ADD ||
4421 staged_status == GOT_STATUS_MODIFY) {
4423 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4427 err = got_object_id_str(&id_str, staged_blob_id);
4430 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4431 err = got_error_from_errno("asprintf");
4436 } else if (status != GOT_STATUS_ADD) {
4437 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4442 if (status != GOT_STATUS_DELETE) {
4443 if (asprintf(&abspath, "%s/%s",
4444 got_worktree_get_root_path(a->worktree), path) == -1) {
4445 err = got_error_from_errno("asprintf");
4450 fd = openat(dirfd, de_name,
4451 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4453 if (!got_err_open_nofollow_on_symlink()) {
4454 err = got_error_from_errno2("openat",
4458 err = get_symlink_target_file(&fd, dirfd,
4464 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4466 if (!got_err_open_nofollow_on_symlink()) {
4467 err = got_error_from_errno2("open",
4471 err = get_symlink_target_file(&fd, dirfd,
4477 if (fstat(fd, &sb) == -1) {
4478 err = got_error_from_errno2("fstat", abspath);
4481 f2 = fdopen(fd, "r");
4483 err = got_error_from_errno2("fdopen", abspath);
4490 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4491 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4494 got_object_blob_close(blob1);
4495 if (f2 && fclose(f2) == EOF && err == NULL)
4496 err = got_error_from_errno("fclose");
4497 if (fd != -1 && close(fd) == -1 && err == NULL)
4498 err = got_error_from_errno("close");
4503 static const struct got_error *
4504 cmd_diff(int argc, char *argv[])
4506 const struct got_error *error;
4507 struct got_repository *repo = NULL;
4508 struct got_worktree *worktree = NULL;
4509 char *cwd = NULL, *repo_path = NULL;
4510 const char *commit_args[2] = { NULL, NULL };
4511 int ncommit_args = 0;
4512 struct got_object_id *ids[2] = { NULL, NULL };
4513 char *labels[2] = { NULL, NULL };
4514 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4515 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4516 int force_text_diff = 0, force_path = 0, rflag = 0;
4518 struct got_reflist_head refs;
4519 struct got_pathlist_head paths;
4520 struct got_pathlist_entry *pe;
4526 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4531 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4534 force_text_diff = 1;
4537 if (ncommit_args >= 2)
4538 errx(1, "too many -c options used");
4539 commit_args[ncommit_args++] = optarg;
4542 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4545 errx(1, "number of context lines is %s: %s",
4549 repo_path = realpath(optarg, NULL);
4550 if (repo_path == NULL)
4551 return got_error_from_errno2("realpath",
4553 got_path_strip_trailing_slashes(repo_path);
4560 ignore_whitespace = 1;
4574 cwd = getcwd(NULL, 0);
4576 error = got_error_from_errno("getcwd");
4580 if (repo_path == NULL) {
4581 error = got_worktree_open(&worktree, cwd);
4582 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4588 strdup(got_worktree_get_repo_path(worktree));
4589 if (repo_path == NULL) {
4590 error = got_error_from_errno("strdup");
4594 repo_path = strdup(cwd);
4595 if (repo_path == NULL) {
4596 error = got_error_from_errno("strdup");
4602 error = got_repo_open(&repo, repo_path, NULL);
4607 if (rflag || worktree == NULL || ncommit_args > 0) {
4609 error = got_error_msg(GOT_ERR_NOT_IMPL,
4610 "-P option can only be used when diffing "
4615 error = got_error_msg(GOT_ERR_NOT_IMPL,
4616 "-s option can only be used when diffing "
4622 error = apply_unveil(got_repo_get_path(repo), 1,
4623 worktree ? got_worktree_get_root_path(worktree) : NULL);
4627 if ((!force_path && argc == 2) || ncommit_args > 0) {
4628 int obj_type = (ncommit_args > 0 ?
4629 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4630 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4634 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4636 if (ncommit_args > 0)
4637 arg = commit_args[i];
4640 error = got_repo_match_object_id(&ids[i], &labels[i],
4641 arg, obj_type, &refs, repo);
4643 if (error->code != GOT_ERR_NOT_REF &&
4644 error->code != GOT_ERR_NO_OBJ)
4646 if (ncommit_args > 0)
4654 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4655 struct print_diff_arg arg;
4658 if (worktree == NULL) {
4659 if (argc == 2 && ids[0] == NULL) {
4660 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4662 } else if (argc == 2 && ids[1] == NULL) {
4663 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4665 } else if (argc > 0) {
4666 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4667 "%s", "specified paths cannot be resolved");
4670 error = got_error(GOT_ERR_NOT_WORKTREE);
4675 error = get_worktree_paths_from_argv(&paths, argc, argv,
4680 error = got_object_id_str(&id_str,
4681 got_worktree_get_base_commit_id(worktree));
4685 arg.worktree = worktree;
4686 arg.diff_context = diff_context;
4687 arg.id_str = id_str;
4688 arg.header_shown = 0;
4689 arg.diff_staged = diff_staged;
4690 arg.ignore_whitespace = ignore_whitespace;
4691 arg.force_text_diff = force_text_diff;
4693 error = got_worktree_status(worktree, &paths, repo, 0,
4694 print_diff, &arg, check_cancelled, NULL);
4699 if (ncommit_args == 1) {
4700 struct got_commit_object *commit;
4701 error = got_object_open_as_commit(&commit, repo, ids[0]);
4705 labels[1] = labels[0];
4707 if (got_object_commit_get_nparents(commit) > 0) {
4708 const struct got_object_id_queue *pids;
4709 struct got_object_qid *pid;
4710 pids = got_object_commit_get_parent_ids(commit);
4711 pid = STAILQ_FIRST(pids);
4712 ids[0] = got_object_id_dup(pid->id);
4713 if (ids[0] == NULL) {
4714 error = got_error_from_errno(
4715 "got_object_id_dup");
4716 got_object_commit_close(commit);
4719 error = got_object_id_str(&labels[0], ids[0]);
4721 got_object_commit_close(commit);
4726 labels[0] = strdup("/dev/null");
4727 if (labels[0] == NULL) {
4728 error = got_error_from_errno("strdup");
4729 got_object_commit_close(commit);
4734 got_object_commit_close(commit);
4737 if (ncommit_args == 0 && argc > 2) {
4738 error = got_error_msg(GOT_ERR_BAD_PATH,
4739 "path arguments cannot be used when diffing two objects");
4744 error = got_object_get_type(&type1, repo, ids[0]);
4749 error = got_object_get_type(&type2, repo, ids[1]);
4752 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4753 error = got_error(GOT_ERR_OBJ_TYPE);
4756 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4757 error = got_error_msg(GOT_ERR_OBJ_TYPE,
4758 "path arguments cannot be used when diffing blobs");
4762 for (i = 0; ncommit_args > 0 && i < argc; i++) {
4764 struct got_pathlist_entry *new;
4768 error = got_worktree_resolve_path(&p, worktree,
4772 prefix = got_worktree_get_path_prefix(worktree);
4773 while (prefix[0] == '/')
4775 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4776 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4778 error = got_error_from_errno("asprintf");
4784 char *mapped_path, *s;
4785 error = got_repo_map_path(&mapped_path, repo, argv[i]);
4791 in_repo_path = strdup(s);
4792 if (in_repo_path == NULL) {
4793 error = got_error_from_errno("asprintf");
4800 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4801 if (error || new == NULL /* duplicate */)
4808 /* Release work tree lock. */
4809 got_worktree_close(worktree);
4813 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4814 case GOT_OBJ_TYPE_BLOB:
4815 error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4816 NULL, NULL, diff_context, ignore_whitespace,
4817 force_text_diff, repo, stdout);
4819 case GOT_OBJ_TYPE_TREE:
4820 error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4821 &paths, "", "", diff_context, ignore_whitespace,
4822 force_text_diff, repo, stdout);
4824 case GOT_OBJ_TYPE_COMMIT:
4825 printf("diff %s %s\n", labels[0], labels[1]);
4826 error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4827 &paths, diff_context, ignore_whitespace, force_text_diff,
4831 error = got_error(GOT_ERR_OBJ_TYPE);
4839 got_worktree_close(worktree);
4841 const struct got_error *close_err = got_repo_close(repo);
4845 TAILQ_FOREACH(pe, &paths, entry)
4846 free((char *)pe->path);
4847 got_pathlist_free(&paths);
4848 got_ref_list_free(&refs);
4856 "usage: %s blame [-c commit] [-r repository-path] path\n",
4865 char datebuf[11]; /* YYYY-MM-DD + NUL */
4868 struct blame_cb_args {
4869 struct blame_line *lines;
4873 off_t *line_offsets;
4875 struct got_repository *repo;
4878 static const struct got_error *
4879 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4881 const struct got_error *err = NULL;
4882 struct blame_cb_args *a = arg;
4883 struct blame_line *bline;
4885 size_t linesize = 0;
4886 struct got_commit_object *commit = NULL;
4889 time_t committer_time;
4891 if (nlines != a->nlines ||
4892 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4893 return got_error(GOT_ERR_RANGE);
4895 if (sigint_received)
4896 return got_error(GOT_ERR_ITER_COMPLETED);
4899 return NULL; /* no change in this commit */
4901 /* Annotate this line. */
4902 bline = &a->lines[lineno - 1];
4903 if (bline->annotated)
4905 err = got_object_id_str(&bline->id_str, id);
4909 err = got_object_open_as_commit(&commit, a->repo, id);
4913 bline->committer = strdup(got_object_commit_get_committer(commit));
4914 if (bline->committer == NULL) {
4915 err = got_error_from_errno("strdup");
4919 committer_time = got_object_commit_get_committer_time(commit);
4920 if (gmtime_r(&committer_time, &tm) == NULL)
4921 return got_error_from_errno("gmtime_r");
4922 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4924 err = got_error(GOT_ERR_NO_SPACE);
4927 bline->annotated = 1;
4929 /* Print lines annotated so far. */
4930 bline = &a->lines[a->lineno_cur - 1];
4931 if (!bline->annotated)
4934 offset = a->line_offsets[a->lineno_cur - 1];
4935 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4936 err = got_error_from_errno("fseeko");
4940 while (bline->annotated) {
4941 char *smallerthan, *at, *nl, *committer;
4944 if (getline(&line, &linesize, a->f) == -1) {
4946 err = got_error_from_errno("getline");
4950 committer = bline->committer;
4951 smallerthan = strchr(committer, '<');
4952 if (smallerthan && smallerthan[1] != '\0')
4953 committer = smallerthan + 1;
4954 at = strchr(committer, '@');
4957 len = strlen(committer);
4959 committer[8] = '\0';
4961 nl = strchr(line, '\n');
4964 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4965 bline->id_str, bline->datebuf, committer, line);
4968 bline = &a->lines[a->lineno_cur - 1];
4972 got_object_commit_close(commit);
4977 static const struct got_error *
4978 cmd_blame(int argc, char *argv[])
4980 const struct got_error *error;
4981 struct got_repository *repo = NULL;
4982 struct got_worktree *worktree = NULL;
4983 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4984 char *link_target = NULL;
4985 struct got_object_id *obj_id = NULL;
4986 struct got_object_id *commit_id = NULL;
4987 struct got_commit_object *commit = NULL;
4988 struct got_blob_object *blob = NULL;
4989 char *commit_id_str = NULL;
4990 struct blame_cb_args bca;
4991 int ch, obj_type, i;
4994 memset(&bca, 0, sizeof(bca));
4997 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5002 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5005 commit_id_str = optarg;
5008 repo_path = realpath(optarg, NULL);
5009 if (repo_path == NULL)
5010 return got_error_from_errno2("realpath",
5012 got_path_strip_trailing_slashes(repo_path);
5028 cwd = getcwd(NULL, 0);
5030 error = got_error_from_errno("getcwd");
5033 if (repo_path == NULL) {
5034 error = got_worktree_open(&worktree, cwd);
5035 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5041 strdup(got_worktree_get_repo_path(worktree));
5042 if (repo_path == NULL) {
5043 error = got_error_from_errno("strdup");
5048 repo_path = strdup(cwd);
5049 if (repo_path == NULL) {
5050 error = got_error_from_errno("strdup");
5056 error = got_repo_open(&repo, repo_path, NULL);
5061 const char *prefix = got_worktree_get_path_prefix(worktree);
5064 error = got_worktree_resolve_path(&p, worktree, path);
5067 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5068 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5070 error = got_error_from_errno("asprintf");
5075 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5077 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5080 error = got_repo_map_path(&in_repo_path, repo, path);
5085 if (commit_id_str == NULL) {
5086 struct got_reference *head_ref;
5087 error = got_ref_open(&head_ref, repo, worktree ?
5088 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5091 error = got_ref_resolve(&commit_id, repo, head_ref);
5092 got_ref_close(head_ref);
5096 struct got_reflist_head refs;
5098 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5102 error = got_repo_match_object_id(&commit_id, NULL,
5103 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5104 got_ref_list_free(&refs);
5110 /* Release work tree lock. */
5111 got_worktree_close(worktree);
5115 error = got_object_open_as_commit(&commit, repo, commit_id);
5119 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5124 error = got_object_id_by_path(&obj_id, repo, commit,
5125 link_target ? link_target : in_repo_path);
5129 error = got_object_get_type(&obj_type, repo, obj_id);
5133 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5134 error = got_error_path(link_target ? link_target : in_repo_path,
5139 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5142 bca.f = got_opentemp();
5143 if (bca.f == NULL) {
5144 error = got_error_from_errno("got_opentemp");
5147 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5148 &bca.line_offsets, bca.f, blob);
5149 if (error || bca.nlines == 0)
5152 /* Don't include \n at EOF in the blame line count. */
5153 if (bca.line_offsets[bca.nlines - 1] == filesize)
5156 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5157 if (bca.lines == NULL) {
5158 error = got_error_from_errno("calloc");
5162 bca.nlines_prec = 0;
5170 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5171 repo, blame_cb, &bca, check_cancelled, NULL);
5180 got_object_commit_close(commit);
5182 got_object_blob_close(blob);
5184 got_worktree_close(worktree);
5186 const struct got_error *close_err = got_repo_close(repo);
5191 for (i = 0; i < bca.nlines; i++) {
5192 struct blame_line *bline = &bca.lines[i];
5193 free(bline->id_str);
5194 free(bline->committer);
5198 free(bca.line_offsets);
5199 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5200 error = got_error_from_errno("fclose");
5208 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5213 static const struct got_error *
5214 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5215 const char *root_path, struct got_repository *repo)
5217 const struct got_error *err = NULL;
5218 int is_root_path = (strcmp(path, root_path) == 0);
5219 const char *modestr = "";
5220 mode_t mode = got_tree_entry_get_mode(te);
5221 char *link_target = NULL;
5223 path += strlen(root_path);
5224 while (path[0] == '/')
5227 if (got_object_tree_entry_is_submodule(te))
5229 else if (S_ISLNK(mode)) {
5232 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5235 for (i = 0; i < strlen(link_target); i++) {
5236 if (!isprint((unsigned char)link_target[i]))
5237 link_target[i] = '?';
5242 else if (S_ISDIR(mode))
5244 else if (mode & S_IXUSR)
5247 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5248 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5249 link_target ? " -> ": "", link_target ? link_target : "");
5255 static const struct got_error *
5256 print_tree(const char *path, struct got_commit_object *commit,
5257 int show_ids, int recurse, const char *root_path,
5258 struct got_repository *repo)
5260 const struct got_error *err = NULL;
5261 struct got_object_id *tree_id = NULL;
5262 struct got_tree_object *tree = NULL;
5265 err = got_object_id_by_path(&tree_id, repo, commit, path);
5269 err = got_object_open_as_tree(&tree, repo, tree_id);
5272 nentries = got_object_tree_get_nentries(tree);
5273 for (i = 0; i < nentries; i++) {
5274 struct got_tree_entry *te;
5277 if (sigint_received || sigpipe_received)
5280 te = got_object_tree_get_entry(tree, i);
5283 err = got_object_id_str(&id_str,
5284 got_tree_entry_get_id(te));
5287 if (asprintf(&id, "%s ", id_str) == -1) {
5288 err = got_error_from_errno("asprintf");
5294 err = print_entry(te, id, path, root_path, repo);
5299 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5301 if (asprintf(&child_path, "%s%s%s", path,
5302 path[0] == '/' && path[1] == '\0' ? "" : "/",
5303 got_tree_entry_get_name(te)) == -1) {
5304 err = got_error_from_errno("asprintf");
5307 err = print_tree(child_path, commit, show_ids, 1,
5316 got_object_tree_close(tree);
5321 static const struct got_error *
5322 cmd_tree(int argc, char *argv[])
5324 const struct got_error *error;
5325 struct got_repository *repo = NULL;
5326 struct got_worktree *worktree = NULL;
5327 const char *path, *refname = NULL;
5328 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5329 struct got_object_id *commit_id = NULL;
5330 struct got_commit_object *commit = NULL;
5331 char *commit_id_str = NULL;
5332 int show_ids = 0, recurse = 0;
5336 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5341 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5344 commit_id_str = optarg;
5347 repo_path = realpath(optarg, NULL);
5348 if (repo_path == NULL)
5349 return got_error_from_errno2("realpath",
5351 got_path_strip_trailing_slashes(repo_path);
5375 cwd = getcwd(NULL, 0);
5377 error = got_error_from_errno("getcwd");
5380 if (repo_path == NULL) {
5381 error = got_worktree_open(&worktree, cwd);
5382 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5388 strdup(got_worktree_get_repo_path(worktree));
5389 if (repo_path == NULL)
5390 error = got_error_from_errno("strdup");
5394 repo_path = strdup(cwd);
5395 if (repo_path == NULL) {
5396 error = got_error_from_errno("strdup");
5402 error = got_repo_open(&repo, repo_path, NULL);
5407 const char *prefix = got_worktree_get_path_prefix(worktree);
5412 error = got_worktree_resolve_path(&p, worktree, path);
5415 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5416 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5418 error = got_error_from_errno("asprintf");
5423 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5427 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5432 error = got_repo_map_path(&in_repo_path, repo, path);
5437 if (commit_id_str == NULL) {
5438 struct got_reference *head_ref;
5440 refname = got_worktree_get_head_ref_name(worktree);
5442 refname = GOT_REF_HEAD;
5443 error = got_ref_open(&head_ref, repo, refname, 0);
5446 error = got_ref_resolve(&commit_id, repo, head_ref);
5447 got_ref_close(head_ref);
5451 struct got_reflist_head refs;
5453 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5457 error = got_repo_match_object_id(&commit_id, NULL,
5458 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5459 got_ref_list_free(&refs);
5465 /* Release work tree lock. */
5466 got_worktree_close(worktree);
5470 error = got_object_open_as_commit(&commit, repo, commit_id);
5474 error = print_tree(in_repo_path, commit, show_ids, recurse,
5475 in_repo_path, repo);
5482 got_object_commit_close(commit);
5484 got_worktree_close(worktree);
5486 const struct got_error *close_err = got_repo_close(repo);
5496 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5497 "[-S status-codes] [path ...]\n", getprogname());
5501 struct got_status_arg {
5506 static const struct got_error *
5507 print_status(void *arg, unsigned char status, unsigned char staged_status,
5508 const char *path, struct got_object_id *blob_id,
5509 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5510 int dirfd, const char *de_name)
5512 struct got_status_arg *st = arg;
5514 if (status == staged_status && (status == GOT_STATUS_DELETE))
5515 status = GOT_STATUS_NO_CHANGE;
5516 if (st != NULL && st->status_codes) {
5517 size_t ncodes = strlen(st->status_codes);
5520 for (i = 0; i < ncodes ; i++) {
5522 if (status == st->status_codes[i] ||
5523 staged_status == st->status_codes[i]) {
5528 if (status == st->status_codes[i] ||
5529 staged_status == st->status_codes[i])
5534 if (st->suppress && j == 0)
5541 printf("%c%c %s\n", status, staged_status, path);
5545 static const struct got_error *
5546 cmd_status(int argc, char *argv[])
5548 const struct got_error *error = NULL;
5549 struct got_repository *repo = NULL;
5550 struct got_worktree *worktree = NULL;
5551 struct got_status_arg st;
5553 struct got_pathlist_head paths;
5554 struct got_pathlist_entry *pe;
5555 int ch, i, no_ignores = 0;
5559 memset(&st, 0, sizeof(st));
5560 st.status_codes = NULL;
5563 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5569 if (st.status_codes != NULL && st.suppress == 0)
5570 option_conflict('S', 's');
5574 for (i = 0; i < strlen(optarg); i++) {
5575 switch (optarg[i]) {
5576 case GOT_STATUS_MODIFY:
5577 case GOT_STATUS_ADD:
5578 case GOT_STATUS_DELETE:
5579 case GOT_STATUS_CONFLICT:
5580 case GOT_STATUS_MISSING:
5581 case GOT_STATUS_OBSTRUCTED:
5582 case GOT_STATUS_UNVERSIONED:
5583 case GOT_STATUS_MODE_CHANGE:
5584 case GOT_STATUS_NONEXISTENT:
5587 errx(1, "invalid status code '%c'",
5591 if (ch == 's' && st.suppress)
5592 option_conflict('s', 'S');
5593 st.status_codes = optarg;
5605 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5609 cwd = getcwd(NULL, 0);
5611 error = got_error_from_errno("getcwd");
5615 error = got_worktree_open(&worktree, cwd);
5617 if (error->code == GOT_ERR_NOT_WORKTREE)
5618 error = wrap_not_worktree_error(error, "status", cwd);
5622 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5627 error = apply_unveil(got_repo_get_path(repo), 1,
5628 got_worktree_get_root_path(worktree));
5632 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5636 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5637 print_status, &st, check_cancelled, NULL);
5639 TAILQ_FOREACH(pe, &paths, entry)
5640 free((char *)pe->path);
5641 got_pathlist_free(&paths);
5650 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5651 "[-s reference] [-d] [name]\n",
5656 static const struct got_error *
5657 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5659 static const struct got_error *err = NULL;
5660 struct got_reflist_head refs;
5661 struct got_reflist_entry *re;
5664 err = got_ref_list(&refs, repo, refname, sort_by_time ?
5665 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5670 TAILQ_FOREACH(re, &refs, entry) {
5672 refstr = got_ref_to_str(re->ref);
5673 if (refstr == NULL) {
5674 err = got_error_from_errno("got_ref_to_str");
5677 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5681 got_ref_list_free(&refs);
5685 static const struct got_error *
5686 delete_ref_by_name(struct got_repository *repo, const char *refname)
5688 const struct got_error *err;
5689 struct got_reference *ref;
5691 err = got_ref_open(&ref, repo, refname, 0);
5695 err = delete_ref(repo, ref);
5700 static const struct got_error *
5701 add_ref(struct got_repository *repo, const char *refname, const char *target)
5703 const struct got_error *err = NULL;
5704 struct got_object_id *id = NULL;
5705 struct got_reference *ref = NULL;
5706 struct got_reflist_head refs;
5709 * Don't let the user create a reference name with a leading '-'.
5710 * While technically a valid reference name, this case is usually
5711 * an unintended typo.
5713 if (refname[0] == '-')
5714 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5717 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5720 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
5722 got_ref_list_free(&refs);
5726 err = got_ref_alloc(&ref, refname, id);
5730 err = got_ref_write(ref, repo);
5738 static const struct got_error *
5739 add_symref(struct got_repository *repo, const char *refname, const char *target)
5741 const struct got_error *err = NULL;
5742 struct got_reference *ref = NULL;
5743 struct got_reference *target_ref = NULL;
5746 * Don't let the user create a reference name with a leading '-'.
5747 * While technically a valid reference name, this case is usually
5748 * an unintended typo.
5750 if (refname[0] == '-')
5751 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5753 err = got_ref_open(&target_ref, repo, target, 0);
5757 err = got_ref_alloc_symref(&ref, refname, target_ref);
5761 err = got_ref_write(ref, repo);
5764 got_ref_close(target_ref);
5770 static const struct got_error *
5771 cmd_ref(int argc, char *argv[])
5773 const struct got_error *error = NULL;
5774 struct got_repository *repo = NULL;
5775 struct got_worktree *worktree = NULL;
5776 char *cwd = NULL, *repo_path = NULL;
5777 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5778 const char *obj_arg = NULL, *symref_target= NULL;
5779 char *refname = NULL;
5781 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5790 repo_path = realpath(optarg, NULL);
5791 if (repo_path == NULL)
5792 return got_error_from_errno2("realpath",
5794 got_path_strip_trailing_slashes(repo_path);
5800 symref_target = optarg;
5811 if (obj_arg && do_list)
5812 option_conflict('c', 'l');
5813 if (obj_arg && do_delete)
5814 option_conflict('c', 'd');
5815 if (obj_arg && symref_target)
5816 option_conflict('c', 's');
5817 if (symref_target && do_delete)
5818 option_conflict('s', 'd');
5819 if (symref_target && do_list)
5820 option_conflict('s', 'l');
5821 if (do_delete && do_list)
5822 option_conflict('d', 'l');
5823 if (sort_by_time && !do_list)
5824 errx(1, "-t option requires -l option");
5830 if (argc != 0 && argc != 1)
5833 refname = strdup(argv[0]);
5834 if (refname == NULL) {
5835 error = got_error_from_errno("strdup");
5842 refname = strdup(argv[0]);
5843 if (refname == NULL) {
5844 error = got_error_from_errno("strdup");
5850 got_path_strip_trailing_slashes(refname);
5854 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5858 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5859 "sendfd unveil", NULL) == -1)
5863 cwd = getcwd(NULL, 0);
5865 error = got_error_from_errno("getcwd");
5869 if (repo_path == NULL) {
5870 error = got_worktree_open(&worktree, cwd);
5871 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5877 strdup(got_worktree_get_repo_path(worktree));
5878 if (repo_path == NULL)
5879 error = got_error_from_errno("strdup");
5883 repo_path = strdup(cwd);
5884 if (repo_path == NULL) {
5885 error = got_error_from_errno("strdup");
5891 error = got_repo_open(&repo, repo_path, NULL);
5895 error = apply_unveil(got_repo_get_path(repo), do_list,
5896 worktree ? got_worktree_get_root_path(worktree) : NULL);
5901 error = list_refs(repo, refname, sort_by_time);
5903 error = delete_ref_by_name(repo, refname);
5904 else if (symref_target)
5905 error = add_symref(repo, refname, symref_target);
5907 if (obj_arg == NULL)
5909 error = add_ref(repo, refname, obj_arg);
5914 const struct got_error *close_err = got_repo_close(repo);
5919 got_worktree_close(worktree);
5929 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5930 "[-n] [name]\n", getprogname());
5934 static const struct got_error *
5935 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5936 struct got_reference *ref)
5938 const struct got_error *err = NULL;
5939 const char *refname, *marker = " ";
5942 refname = got_ref_get_name(ref);
5943 if (worktree && strcmp(refname,
5944 got_worktree_get_head_ref_name(worktree)) == 0) {
5945 struct got_object_id *id = NULL;
5947 err = got_ref_resolve(&id, repo, ref);
5950 if (got_object_id_cmp(id,
5951 got_worktree_get_base_commit_id(worktree)) == 0)
5958 if (strncmp(refname, "refs/heads/", 11) == 0)
5960 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5962 if (strncmp(refname, "refs/remotes/", 13) == 0)
5965 refstr = got_ref_to_str(ref);
5967 return got_error_from_errno("got_ref_to_str");
5969 printf("%s%s: %s\n", marker, refname, refstr);
5974 static const struct got_error *
5975 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5977 const char *refname;
5979 if (worktree == NULL)
5980 return got_error(GOT_ERR_NOT_WORKTREE);
5982 refname = got_worktree_get_head_ref_name(worktree);
5984 if (strncmp(refname, "refs/heads/", 11) == 0)
5986 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5989 printf("%s\n", refname);
5994 static const struct got_error *
5995 list_branches(struct got_repository *repo, struct got_worktree *worktree,
5998 static const struct got_error *err = NULL;
5999 struct got_reflist_head refs;
6000 struct got_reflist_entry *re;
6001 struct got_reference *temp_ref = NULL;
6002 int rebase_in_progress, histedit_in_progress;
6007 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6012 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6017 if (rebase_in_progress || histedit_in_progress) {
6018 err = got_ref_open(&temp_ref, repo,
6019 got_worktree_get_head_ref_name(worktree), 0);
6022 list_branch(repo, worktree, temp_ref);
6023 got_ref_close(temp_ref);
6027 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6028 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6033 TAILQ_FOREACH(re, &refs, entry)
6034 list_branch(repo, worktree, re->ref);
6036 got_ref_list_free(&refs);
6038 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6039 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6044 TAILQ_FOREACH(re, &refs, entry)
6045 list_branch(repo, worktree, re->ref);
6047 got_ref_list_free(&refs);
6052 static const struct got_error *
6053 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6054 const char *branch_name)
6056 const struct got_error *err = NULL;
6057 struct got_reference *ref = NULL;
6058 char *refname, *remote_refname = NULL;
6060 if (strncmp(branch_name, "refs/", 5) == 0)
6062 if (strncmp(branch_name, "heads/", 6) == 0)
6064 else if (strncmp(branch_name, "remotes/", 8) == 0)
6067 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6068 return got_error_from_errno("asprintf");
6070 if (asprintf(&remote_refname, "refs/remotes/%s",
6071 branch_name) == -1) {
6072 err = got_error_from_errno("asprintf");
6076 err = got_ref_open(&ref, repo, refname, 0);
6078 const struct got_error *err2;
6079 if (err->code != GOT_ERR_NOT_REF)
6082 * Keep 'err' intact such that if neither branch exists
6083 * we report "refs/heads" rather than "refs/remotes" in
6084 * our error message.
6086 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6093 strcmp(got_worktree_get_head_ref_name(worktree),
6094 got_ref_get_name(ref)) == 0) {
6095 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6096 "will not delete this work tree's current branch");
6100 err = delete_ref(repo, ref);
6105 free(remote_refname);
6109 static const struct got_error *
6110 add_branch(struct got_repository *repo, const char *branch_name,
6111 struct got_object_id *base_commit_id)
6113 const struct got_error *err = NULL;
6114 struct got_reference *ref = NULL;
6115 char *base_refname = NULL, *refname = NULL;
6118 * Don't let the user create a branch name with a leading '-'.
6119 * While technically a valid reference name, this case is usually
6120 * an unintended typo.
6122 if (branch_name[0] == '-')
6123 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6125 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6128 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6129 err = got_error_from_errno("asprintf");
6133 err = got_ref_open(&ref, repo, refname, 0);
6135 err = got_error(GOT_ERR_BRANCH_EXISTS);
6137 } else if (err->code != GOT_ERR_NOT_REF)
6140 err = got_ref_alloc(&ref, refname, base_commit_id);
6144 err = got_ref_write(ref, repo);
6153 static const struct got_error *
6154 cmd_branch(int argc, char *argv[])
6156 const struct got_error *error = NULL;
6157 struct got_repository *repo = NULL;
6158 struct got_worktree *worktree = NULL;
6159 char *cwd = NULL, *repo_path = NULL;
6160 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6161 const char *delref = NULL, *commit_id_arg = NULL;
6162 struct got_reference *ref = NULL;
6163 struct got_pathlist_head paths;
6164 struct got_pathlist_entry *pe;
6165 struct got_object_id *commit_id = NULL;
6166 char *commit_id_str = NULL;
6170 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6173 commit_id_arg = optarg;
6179 repo_path = realpath(optarg, NULL);
6180 if (repo_path == NULL)
6181 return got_error_from_errno2("realpath",
6183 got_path_strip_trailing_slashes(repo_path);
6200 if (do_list && delref)
6201 option_conflict('l', 'd');
6202 if (sort_by_time && !do_list)
6203 errx(1, "-t option requires -l option");
6208 if (!do_list && !delref && argc == 0)
6211 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6212 errx(1, "-c option can only be used when creating a branch");
6214 if (do_list || delref) {
6217 } else if (!do_show && argc != 1)
6221 if (do_list || do_show) {
6222 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6226 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6227 "sendfd unveil", NULL) == -1)
6231 cwd = getcwd(NULL, 0);
6233 error = got_error_from_errno("getcwd");
6237 if (repo_path == NULL) {
6238 error = got_worktree_open(&worktree, cwd);
6239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6245 strdup(got_worktree_get_repo_path(worktree));
6246 if (repo_path == NULL)
6247 error = got_error_from_errno("strdup");
6251 repo_path = strdup(cwd);
6252 if (repo_path == NULL) {
6253 error = got_error_from_errno("strdup");
6259 error = got_repo_open(&repo, repo_path, NULL);
6263 error = apply_unveil(got_repo_get_path(repo), do_list,
6264 worktree ? got_worktree_get_root_path(worktree) : NULL);
6269 error = show_current_branch(repo, worktree);
6271 error = list_branches(repo, worktree, sort_by_time);
6273 error = delete_branch(repo, worktree, delref);
6275 struct got_reflist_head refs;
6277 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6281 if (commit_id_arg == NULL)
6282 commit_id_arg = worktree ?
6283 got_worktree_get_head_ref_name(worktree) :
6285 error = got_repo_match_object_id(&commit_id, NULL,
6286 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6287 got_ref_list_free(&refs);
6290 error = add_branch(repo, argv[0], commit_id);
6293 if (worktree && do_update) {
6294 struct got_update_progress_arg upa;
6295 char *branch_refname = NULL;
6297 error = got_object_id_str(&commit_id_str, commit_id);
6300 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6304 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6306 error = got_error_from_errno("asprintf");
6309 error = got_ref_open(&ref, repo, branch_refname, 0);
6310 free(branch_refname);
6313 error = switch_head_ref(ref, commit_id, worktree,
6317 error = got_worktree_set_base_commit_id(worktree, repo,
6321 memset(&upa, 0, sizeof(upa));
6322 error = got_worktree_checkout_files(worktree, &paths,
6323 repo, update_progress, &upa, check_cancelled,
6327 if (upa.did_something) {
6328 printf("Updated to %s: %s\n",
6329 got_worktree_get_head_ref_name(worktree),
6332 print_update_progress_stats(&upa);
6339 const struct got_error *close_err = got_repo_close(repo);
6344 got_worktree_close(worktree);
6348 free(commit_id_str);
6349 TAILQ_FOREACH(pe, &paths, entry)
6350 free((char *)pe->path);
6351 got_pathlist_free(&paths);
6360 "usage: %s tag [-c commit] [-r repository] [-l] "
6361 "[-m message] name\n", getprogname());
6366 static const struct got_error *
6367 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6369 const struct got_error *err = NULL;
6370 struct got_reflist_entry *re, *se, *new;
6371 struct got_object_id *re_id, *se_id;
6372 struct got_tag_object *re_tag, *se_tag;
6373 time_t re_time, se_time;
6375 STAILQ_FOREACH(re, tags, entry) {
6376 se = STAILQ_FIRST(sorted);
6378 err = got_reflist_entry_dup(&new, re);
6381 STAILQ_INSERT_HEAD(sorted, new, entry);
6384 err = got_ref_resolve(&re_id, repo, re->ref);
6387 err = got_object_open_as_tag(&re_tag, repo, re_id);
6391 re_time = got_object_tag_get_tagger_time(re_tag);
6392 got_object_tag_close(re_tag);
6396 err = got_ref_resolve(&se_id, repo, re->ref);
6399 err = got_object_open_as_tag(&se_tag, repo, se_id);
6403 se_time = got_object_tag_get_tagger_time(se_tag);
6404 got_object_tag_close(se_tag);
6406 if (se_time > re_time) {
6407 err = got_reflist_entry_dup(&new, re);
6410 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6413 se = STAILQ_NEXT(se, entry);
6422 static const struct got_error *
6423 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6425 static const struct got_error *err = NULL;
6426 struct got_reflist_head refs;
6427 struct got_reflist_entry *re;
6431 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6435 TAILQ_FOREACH(re, &refs, entry) {
6436 const char *refname;
6437 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6441 struct got_object_id *id;
6442 struct got_tag_object *tag;
6443 struct got_commit_object *commit = NULL;
6445 refname = got_ref_get_name(re->ref);
6446 if (strncmp(refname, "refs/tags/", 10) != 0)
6449 refstr = got_ref_to_str(re->ref);
6450 if (refstr == NULL) {
6451 err = got_error_from_errno("got_ref_to_str");
6454 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6457 err = got_ref_resolve(&id, repo, re->ref);
6460 err = got_object_open_as_tag(&tag, repo, id);
6462 if (err->code != GOT_ERR_OBJ_TYPE) {
6466 /* "lightweight" tag */
6467 err = got_object_open_as_commit(&commit, repo, id);
6472 tagger = got_object_commit_get_committer(commit);
6474 got_object_commit_get_committer_time(commit);
6475 err = got_object_id_str(&id_str, id);
6481 tagger = got_object_tag_get_tagger(tag);
6482 tagger_time = got_object_tag_get_tagger_time(tag);
6483 err = got_object_id_str(&id_str,
6484 got_object_tag_get_object_id(tag));
6488 printf("from: %s\n", tagger);
6489 datestr = get_datestr(&tagger_time, datebuf);
6491 printf("date: %s UTC\n", datestr);
6493 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6495 switch (got_object_tag_get_object_type(tag)) {
6496 case GOT_OBJ_TYPE_BLOB:
6497 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6500 case GOT_OBJ_TYPE_TREE:
6501 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6504 case GOT_OBJ_TYPE_COMMIT:
6505 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6508 case GOT_OBJ_TYPE_TAG:
6509 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6518 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6521 got_object_commit_close(commit);
6523 tagmsg0 = strdup(got_object_tag_get_message(tag));
6524 got_object_tag_close(tag);
6525 if (tagmsg0 == NULL) {
6526 err = got_error_from_errno("strdup");
6533 line = strsep(&tagmsg, "\n");
6535 printf(" %s\n", line);
6540 got_ref_list_free(&refs);
6544 static const struct got_error *
6545 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6546 const char *tag_name, const char *repo_path)
6548 const struct got_error *err = NULL;
6549 char *template = NULL, *initial_content = NULL;
6550 char *editor = NULL;
6551 int initial_content_len;
6554 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6555 err = got_error_from_errno("asprintf");
6559 initial_content_len = asprintf(&initial_content,
6560 "\n# tagging commit %s as %s\n",
6561 commit_id_str, tag_name);
6562 if (initial_content_len == -1) {
6563 err = got_error_from_errno("asprintf");
6567 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6571 if (write(fd, initial_content, initial_content_len) == -1) {
6572 err = got_error_from_errno2("write", *tagmsg_path);
6576 err = get_editor(&editor);
6579 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6580 initial_content_len, 1);
6582 free(initial_content);
6586 if (fd != -1 && close(fd) == -1 && err == NULL)
6587 err = got_error_from_errno2("close", *tagmsg_path);
6589 /* Editor is done; we can now apply unveil(2) */
6591 err = apply_unveil(repo_path, 0, NULL);
6599 static const struct got_error *
6600 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6601 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6603 const struct got_error *err = NULL;
6604 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6605 char *label = NULL, *commit_id_str = NULL;
6606 struct got_reference *ref = NULL;
6607 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6608 char *tagmsg_path = NULL, *tag_id_str = NULL;
6609 int preserve_tagmsg = 0;
6610 struct got_reflist_head refs;
6615 * Don't let the user create a tag name with a leading '-'.
6616 * While technically a valid reference name, this case is usually
6617 * an unintended typo.
6619 if (tag_name[0] == '-')
6620 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6622 err = get_author(&tagger, repo, worktree);
6626 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6630 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6631 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6635 err = got_object_id_str(&commit_id_str, commit_id);
6639 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6640 refname = strdup(tag_name);
6641 if (refname == NULL) {
6642 err = got_error_from_errno("strdup");
6646 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6647 err = got_error_from_errno("asprintf");
6651 err = got_ref_open(&ref, repo, refname, 0);
6653 err = got_error(GOT_ERR_TAG_EXISTS);
6655 } else if (err->code != GOT_ERR_NOT_REF)
6658 if (tagmsg_arg == NULL) {
6659 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6660 tag_name, got_repo_get_path(repo));
6662 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6663 tagmsg_path != NULL)
6664 preserve_tagmsg = 1;
6669 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6670 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6673 preserve_tagmsg = 1;
6677 err = got_ref_alloc(&ref, refname, tag_id);
6680 preserve_tagmsg = 1;
6684 err = got_ref_write(ref, repo);
6687 preserve_tagmsg = 1;
6691 err = got_object_id_str(&tag_id_str, tag_id);
6694 preserve_tagmsg = 1;
6697 printf("Created tag %s\n", tag_id_str);
6699 if (preserve_tagmsg) {
6700 fprintf(stderr, "%s: tag message preserved in %s\n",
6701 getprogname(), tagmsg_path);
6702 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6703 err = got_error_from_errno2("unlink", tagmsg_path);
6708 free(commit_id_str);
6713 got_ref_list_free(&refs);
6717 static const struct got_error *
6718 cmd_tag(int argc, char *argv[])
6720 const struct got_error *error = NULL;
6721 struct got_repository *repo = NULL;
6722 struct got_worktree *worktree = NULL;
6723 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6724 char *gitconfig_path = NULL;
6725 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6726 int ch, do_list = 0;
6728 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6731 commit_id_arg = optarg;
6737 repo_path = realpath(optarg, NULL);
6738 if (repo_path == NULL)
6739 return got_error_from_errno2("realpath",
6741 got_path_strip_trailing_slashes(repo_path);
6756 if (commit_id_arg != NULL)
6758 "-c option can only be used when creating a tag");
6760 option_conflict('l', 'm');
6763 } else if (argc != 1)
6770 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6774 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6775 "sendfd unveil", NULL) == -1)
6779 cwd = getcwd(NULL, 0);
6781 error = got_error_from_errno("getcwd");
6785 if (repo_path == NULL) {
6786 error = got_worktree_open(&worktree, cwd);
6787 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6793 strdup(got_worktree_get_repo_path(worktree));
6794 if (repo_path == NULL)
6795 error = got_error_from_errno("strdup");
6799 repo_path = strdup(cwd);
6800 if (repo_path == NULL) {
6801 error = got_error_from_errno("strdup");
6808 error = got_repo_open(&repo, repo_path, NULL);
6811 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6814 error = list_tags(repo, worktree);
6816 error = get_gitconfig_path(&gitconfig_path);
6819 error = got_repo_open(&repo, repo_path, gitconfig_path);
6824 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6829 if (commit_id_arg == NULL) {
6830 struct got_reference *head_ref;
6831 struct got_object_id *commit_id;
6832 error = got_ref_open(&head_ref, repo,
6833 worktree ? got_worktree_get_head_ref_name(worktree)
6837 error = got_ref_resolve(&commit_id, repo, head_ref);
6838 got_ref_close(head_ref);
6841 error = got_object_id_str(&commit_id_str, commit_id);
6847 error = add_tag(repo, worktree, tag_name,
6848 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6852 const struct got_error *close_err = got_repo_close(repo);
6857 got_worktree_close(worktree);
6860 free(gitconfig_path);
6861 free(commit_id_str);
6868 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6873 static const struct got_error *
6874 add_progress(void *arg, unsigned char status, const char *path)
6876 while (path[0] == '/')
6878 printf("%c %s\n", status, path);
6882 static const struct got_error *
6883 cmd_add(int argc, char *argv[])
6885 const struct got_error *error = NULL;
6886 struct got_repository *repo = NULL;
6887 struct got_worktree *worktree = NULL;
6889 struct got_pathlist_head paths;
6890 struct got_pathlist_entry *pe;
6891 int ch, can_recurse = 0, no_ignores = 0;
6895 while ((ch = getopt(argc, argv, "IR")) != -1) {
6913 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6920 cwd = getcwd(NULL, 0);
6922 error = got_error_from_errno("getcwd");
6926 error = got_worktree_open(&worktree, cwd);
6928 if (error->code == GOT_ERR_NOT_WORKTREE)
6929 error = wrap_not_worktree_error(error, "add", cwd);
6933 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6938 error = apply_unveil(got_repo_get_path(repo), 1,
6939 got_worktree_get_root_path(worktree));
6943 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6950 TAILQ_FOREACH(pe, &paths, entry) {
6951 if (asprintf(&ondisk_path, "%s/%s",
6952 got_worktree_get_root_path(worktree),
6954 error = got_error_from_errno("asprintf");
6957 if (lstat(ondisk_path, &sb) == -1) {
6958 if (errno == ENOENT) {
6962 error = got_error_from_errno2("lstat",
6968 if (S_ISDIR(sb.st_mode)) {
6969 error = got_error_msg(GOT_ERR_BAD_PATH,
6970 "adding directories requires -R option");
6976 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6977 NULL, repo, no_ignores);
6980 const struct got_error *close_err = got_repo_close(repo);
6985 got_worktree_close(worktree);
6986 TAILQ_FOREACH(pe, &paths, entry)
6987 free((char *)pe->path);
6988 got_pathlist_free(&paths);
6996 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6997 "path ...\n", getprogname());
7001 static const struct got_error *
7002 print_remove_status(void *arg, unsigned char status,
7003 unsigned char staged_status, const char *path)
7005 while (path[0] == '/')
7007 if (status == GOT_STATUS_NONEXISTENT)
7009 if (status == staged_status && (status == GOT_STATUS_DELETE))
7010 status = GOT_STATUS_NO_CHANGE;
7011 printf("%c%c %s\n", status, staged_status, path);
7015 static const struct got_error *
7016 cmd_remove(int argc, char *argv[])
7018 const struct got_error *error = NULL;
7019 struct got_worktree *worktree = NULL;
7020 struct got_repository *repo = NULL;
7021 const char *status_codes = NULL;
7023 struct got_pathlist_head paths;
7024 struct got_pathlist_entry *pe;
7025 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7026 int ignore_missing_paths = 0;
7030 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7033 delete_local_mods = 1;
7034 ignore_missing_paths = 1;
7043 for (i = 0; i < strlen(optarg); i++) {
7044 switch (optarg[i]) {
7045 case GOT_STATUS_MODIFY:
7046 delete_local_mods = 1;
7048 case GOT_STATUS_MISSING:
7049 ignore_missing_paths = 1;
7052 errx(1, "invalid status code '%c'",
7056 status_codes = optarg;
7068 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7075 cwd = getcwd(NULL, 0);
7077 error = got_error_from_errno("getcwd");
7080 error = got_worktree_open(&worktree, cwd);
7082 if (error->code == GOT_ERR_NOT_WORKTREE)
7083 error = wrap_not_worktree_error(error, "remove", cwd);
7087 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7092 error = apply_unveil(got_repo_get_path(repo), 1,
7093 got_worktree_get_root_path(worktree));
7097 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7104 TAILQ_FOREACH(pe, &paths, entry) {
7105 if (asprintf(&ondisk_path, "%s/%s",
7106 got_worktree_get_root_path(worktree),
7108 error = got_error_from_errno("asprintf");
7111 if (lstat(ondisk_path, &sb) == -1) {
7112 if (errno == ENOENT) {
7116 error = got_error_from_errno2("lstat",
7122 if (S_ISDIR(sb.st_mode)) {
7123 error = got_error_msg(GOT_ERR_BAD_PATH,
7124 "removing directories requires -R option");
7130 error = got_worktree_schedule_delete(worktree, &paths,
7131 delete_local_mods, status_codes, print_remove_status, NULL,
7132 repo, keep_on_disk, ignore_missing_paths);
7135 const struct got_error *close_err = got_repo_close(repo);
7140 got_worktree_close(worktree);
7141 TAILQ_FOREACH(pe, &paths, entry)
7142 free((char *)pe->path);
7143 got_pathlist_free(&paths);
7151 fprintf(stderr, "usage: %s patch [-n] [patchfile]\n",
7156 static const struct got_error *
7157 patch_from_stdin(int *patchfd)
7159 const struct got_error *err = NULL;
7161 char *path, buf[BUFSIZ];
7162 sig_t sighup, sigint, sigquit;
7164 err = got_opentemp_named_fd(&path, patchfd,
7165 GOT_TMPDIR_STR "/got-patch");
7171 sighup = signal(SIGHUP, SIG_DFL);
7172 sigint = signal(SIGINT, SIG_DFL);
7173 sigquit = signal(SIGQUIT, SIG_DFL);
7176 r = read(0, buf, sizeof(buf));
7178 err = got_error_from_errno("read");
7183 if (write(*patchfd, buf, r) == -1) {
7184 err = got_error_from_errno("write");
7189 signal(SIGHUP, sighup);
7190 signal(SIGINT, sigint);
7191 signal(SIGQUIT, sigquit);
7193 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7194 err = got_error_from_errno("lseek");
7204 static const struct got_error *
7205 patch_progress(void *arg, const char *old, const char *new,
7206 unsigned char status, const struct got_error *error, long old_from,
7207 long old_lines, long new_from, long new_lines, long offset,
7208 const struct got_error *hunk_err)
7210 const char *path = new == NULL ? old : new;
7212 while (*path == '/')
7216 printf("%c %s\n", status, path);
7219 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7221 if (offset != 0 || hunk_err != NULL) {
7222 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7223 old_lines, new_from, new_lines);
7224 if (hunk_err != NULL)
7225 printf("%s\n", hunk_err->msg);
7227 printf("applied with offset %ld\n", offset);
7233 static const struct got_error *
7234 cmd_patch(int argc, char *argv[])
7236 const struct got_error *error = NULL, *close_error = NULL;
7237 struct got_worktree *worktree = NULL;
7238 struct got_repository *repo = NULL;
7241 int ch, nop = 0, strip = -1;
7244 while ((ch = getopt(argc, argv, "np:")) != -1) {
7250 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7252 errx(1, "pathname strip count is %s: %s",
7265 error = patch_from_stdin(&patchfd);
7268 } else if (argc == 1) {
7269 patchfd = open(argv[0], O_RDONLY);
7270 if (patchfd == -1) {
7271 error = got_error_from_errno2("open", argv[0]);
7277 if ((cwd = getcwd(NULL, 0)) == NULL) {
7278 error = got_error_from_errno("getcwd");
7282 error = got_worktree_open(&worktree, cwd);
7286 const char *repo_path = got_worktree_get_repo_path(worktree);
7287 error = got_repo_open(&repo, repo_path, NULL);
7291 error = apply_unveil(got_repo_get_path(repo), 0,
7292 worktree ? got_worktree_get_root_path(worktree) : NULL);
7297 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7302 error = got_patch(patchfd, worktree, repo, nop, strip,
7303 &patch_progress, NULL, check_cancelled, NULL);
7307 close_error = got_repo_close(repo);
7309 error = close_error;
7311 if (worktree != NULL) {
7312 close_error = got_worktree_close(worktree);
7314 error = close_error;
7323 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7324 "path ...\n", getprogname());
7328 static const struct got_error *
7329 revert_progress(void *arg, unsigned char status, const char *path)
7331 if (status == GOT_STATUS_UNVERSIONED)
7334 while (path[0] == '/')
7336 printf("%c %s\n", status, path);
7340 struct choose_patch_arg {
7341 FILE *patch_script_file;
7345 static const struct got_error *
7346 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7347 int nchanges, const char *action)
7350 size_t linesize = 0;
7354 case GOT_STATUS_ADD:
7355 printf("A %s\n%s this addition? [y/n] ", path, action);
7357 case GOT_STATUS_DELETE:
7358 printf("D %s\n%s this deletion? [y/n] ", path, action);
7360 case GOT_STATUS_MODIFY:
7361 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7362 return got_error_from_errno("fseek");
7363 printf(GOT_COMMIT_SEP_STR);
7364 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7366 if (ferror(patch_file))
7367 return got_error_from_errno("getline");
7368 printf(GOT_COMMIT_SEP_STR);
7369 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7370 path, n, nchanges, action);
7373 return got_error_path(path, GOT_ERR_FILE_STATUS);
7379 static const struct got_error *
7380 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7381 FILE *patch_file, int n, int nchanges)
7383 const struct got_error *err = NULL;
7385 size_t linesize = 0;
7388 struct choose_patch_arg *a = arg;
7390 *choice = GOT_PATCH_CHOICE_NONE;
7392 if (a->patch_script_file) {
7394 err = show_change(status, path, patch_file, n, nchanges,
7398 linelen = getline(&line, &linesize, a->patch_script_file);
7399 if (linelen == -1) {
7400 if (ferror(a->patch_script_file))
7401 return got_error_from_errno("getline");
7404 nl = strchr(line, '\n');
7407 if (strcmp(line, "y") == 0) {
7408 *choice = GOT_PATCH_CHOICE_YES;
7410 } else if (strcmp(line, "n") == 0) {
7411 *choice = GOT_PATCH_CHOICE_NO;
7413 } else if (strcmp(line, "q") == 0 &&
7414 status == GOT_STATUS_MODIFY) {
7415 *choice = GOT_PATCH_CHOICE_QUIT;
7418 printf("invalid response '%s'\n", line);
7423 while (resp != 'y' && resp != 'n' && resp != 'q') {
7424 err = show_change(status, path, patch_file, n, nchanges,
7431 if (status == GOT_STATUS_MODIFY) {
7432 if (resp != 'y' && resp != 'n' && resp != 'q') {
7433 printf("invalid response '%c'\n", resp);
7436 } else if (resp != 'y' && resp != 'n') {
7437 printf("invalid response '%c'\n", resp);
7443 *choice = GOT_PATCH_CHOICE_YES;
7444 else if (resp == 'n')
7445 *choice = GOT_PATCH_CHOICE_NO;
7446 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7447 *choice = GOT_PATCH_CHOICE_QUIT;
7452 static const struct got_error *
7453 cmd_revert(int argc, char *argv[])
7455 const struct got_error *error = NULL;
7456 struct got_worktree *worktree = NULL;
7457 struct got_repository *repo = NULL;
7458 char *cwd = NULL, *path = NULL;
7459 struct got_pathlist_head paths;
7460 struct got_pathlist_entry *pe;
7461 int ch, can_recurse = 0, pflag = 0;
7462 FILE *patch_script_file = NULL;
7463 const char *patch_script_path = NULL;
7464 struct choose_patch_arg cpa;
7468 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7474 patch_script_path = optarg;
7489 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7490 "unveil", NULL) == -1)
7495 if (patch_script_path && !pflag)
7496 errx(1, "-F option can only be used together with -p option");
7498 cwd = getcwd(NULL, 0);
7500 error = got_error_from_errno("getcwd");
7503 error = got_worktree_open(&worktree, cwd);
7505 if (error->code == GOT_ERR_NOT_WORKTREE)
7506 error = wrap_not_worktree_error(error, "revert", cwd);
7510 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7515 if (patch_script_path) {
7516 patch_script_file = fopen(patch_script_path, "re");
7517 if (patch_script_file == NULL) {
7518 error = got_error_from_errno2("fopen",
7523 error = apply_unveil(got_repo_get_path(repo), 1,
7524 got_worktree_get_root_path(worktree));
7528 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7535 TAILQ_FOREACH(pe, &paths, entry) {
7536 if (asprintf(&ondisk_path, "%s/%s",
7537 got_worktree_get_root_path(worktree),
7539 error = got_error_from_errno("asprintf");
7542 if (lstat(ondisk_path, &sb) == -1) {
7543 if (errno == ENOENT) {
7547 error = got_error_from_errno2("lstat",
7553 if (S_ISDIR(sb.st_mode)) {
7554 error = got_error_msg(GOT_ERR_BAD_PATH,
7555 "reverting directories requires -R option");
7561 cpa.patch_script_file = patch_script_file;
7562 cpa.action = "revert";
7563 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7564 pflag ? choose_patch : NULL, &cpa, repo);
7566 if (patch_script_file && fclose(patch_script_file) == EOF &&
7568 error = got_error_from_errno2("fclose", patch_script_path);
7570 const struct got_error *close_err = got_repo_close(repo);
7575 got_worktree_close(worktree);
7584 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7585 "[path ...]\n", getprogname());
7589 struct collect_commit_logmsg_arg {
7590 const char *cmdline_log;
7591 const char *prepared_log;
7592 int non_interactive;
7594 const char *worktree_path;
7595 const char *branch_name;
7596 const char *repo_path;
7601 static const struct got_error *
7602 read_prepared_logmsg(char **logmsg, const char *path)
7604 const struct got_error *err = NULL;
7610 memset(&sb, 0, sizeof(sb));
7612 f = fopen(path, "re");
7614 return got_error_from_errno2("fopen", path);
7616 if (fstat(fileno(f), &sb) == -1) {
7617 err = got_error_from_errno2("fstat", path);
7620 if (sb.st_size == 0) {
7621 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7625 *logmsg = malloc(sb.st_size + 1);
7626 if (*logmsg == NULL) {
7627 err = got_error_from_errno("malloc");
7631 r = fread(*logmsg, 1, sb.st_size, f);
7632 if (r != sb.st_size) {
7634 err = got_error_from_errno2("fread", path);
7636 err = got_error(GOT_ERR_IO);
7639 (*logmsg)[sb.st_size] = '\0';
7641 if (fclose(f) == EOF && err == NULL)
7642 err = got_error_from_errno2("fclose", path);
7651 static const struct got_error *
7652 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7655 char *initial_content = NULL;
7656 struct got_pathlist_entry *pe;
7657 const struct got_error *err = NULL;
7658 char *template = NULL;
7659 struct collect_commit_logmsg_arg *a = arg;
7660 int initial_content_len;
7664 /* if a message was specified on the command line, just use it */
7665 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7666 len = strlen(a->cmdline_log) + 1;
7667 *logmsg = malloc(len + 1);
7668 if (*logmsg == NULL)
7669 return got_error_from_errno("malloc");
7670 strlcpy(*logmsg, a->cmdline_log, len);
7672 } else if (a->prepared_log != NULL && a->non_interactive)
7673 return read_prepared_logmsg(logmsg, a->prepared_log);
7675 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7676 return got_error_from_errno("asprintf");
7678 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7682 if (a->prepared_log) {
7684 err = read_prepared_logmsg(&msg, a->prepared_log);
7687 if (write(fd, msg, strlen(msg)) == -1) {
7688 err = got_error_from_errno2("write", a->logmsg_path);
7695 initial_content_len = asprintf(&initial_content,
7696 "\n# changes to be committed on branch %s:\n",
7698 if (initial_content_len == -1) {
7699 err = got_error_from_errno("asprintf");
7703 if (write(fd, initial_content, initial_content_len) == -1) {
7704 err = got_error_from_errno2("write", a->logmsg_path);
7708 TAILQ_FOREACH(pe, commitable_paths, entry) {
7709 struct got_commitable *ct = pe->data;
7710 dprintf(fd, "# %c %s\n",
7711 got_commitable_get_status(ct),
7712 got_commitable_get_path(ct));
7715 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7716 initial_content_len, a->prepared_log ? 0 : 1);
7718 free(initial_content);
7721 if (fd != -1 && close(fd) == -1 && err == NULL)
7722 err = got_error_from_errno2("close", a->logmsg_path);
7724 /* Editor is done; we can now apply unveil(2) */
7726 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7734 static const struct got_error *
7735 cmd_commit(int argc, char *argv[])
7737 const struct got_error *error = NULL;
7738 struct got_worktree *worktree = NULL;
7739 struct got_repository *repo = NULL;
7740 char *cwd = NULL, *id_str = NULL;
7741 struct got_object_id *id = NULL;
7742 const char *logmsg = NULL;
7743 char *prepared_logmsg = NULL;
7744 struct collect_commit_logmsg_arg cl_arg;
7745 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7746 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7747 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7748 struct got_pathlist_head paths;
7751 cl_arg.logmsg_path = NULL;
7753 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7757 option_conflict('F', 'm');
7758 prepared_logmsg = realpath(optarg, NULL);
7759 if (prepared_logmsg == NULL)
7760 return got_error_from_errno2("realpath",
7764 if (prepared_logmsg)
7765 option_conflict('m', 'F');
7769 non_interactive = 1;
7772 allow_bad_symlinks = 1;
7784 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7785 "unveil", NULL) == -1)
7788 cwd = getcwd(NULL, 0);
7790 error = got_error_from_errno("getcwd");
7793 error = got_worktree_open(&worktree, cwd);
7795 if (error->code == GOT_ERR_NOT_WORKTREE)
7796 error = wrap_not_worktree_error(error, "commit", cwd);
7800 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7803 if (rebase_in_progress) {
7804 error = got_error(GOT_ERR_REBASING);
7808 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7813 error = get_gitconfig_path(&gitconfig_path);
7816 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7821 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7824 if (merge_in_progress) {
7825 error = got_error(GOT_ERR_MERGE_BUSY);
7829 error = get_author(&author, repo, worktree);
7834 * unveil(2) traverses exec(2); if an editor is used we have
7835 * to apply unveil after the log message has been written.
7837 if (logmsg == NULL || strlen(logmsg) == 0)
7838 error = get_editor(&editor);
7840 error = apply_unveil(got_repo_get_path(repo), 0,
7841 got_worktree_get_root_path(worktree));
7845 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7849 cl_arg.editor = editor;
7850 cl_arg.cmdline_log = logmsg;
7851 cl_arg.prepared_log = prepared_logmsg;
7852 cl_arg.non_interactive = non_interactive;
7853 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7854 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7855 if (!histedit_in_progress) {
7856 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7857 error = got_error(GOT_ERR_COMMIT_BRANCH);
7860 cl_arg.branch_name += 11;
7862 cl_arg.repo_path = got_repo_get_path(repo);
7863 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7864 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7865 print_status, NULL, repo);
7867 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7868 cl_arg.logmsg_path != NULL)
7869 preserve_logmsg = 1;
7873 error = got_object_id_str(&id_str, id);
7876 printf("Created commit %s\n", id_str);
7878 if (preserve_logmsg) {
7879 fprintf(stderr, "%s: log message preserved in %s\n",
7880 getprogname(), cl_arg.logmsg_path);
7881 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7883 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7884 free(cl_arg.logmsg_path);
7886 const struct got_error *close_err = got_repo_close(repo);
7891 got_worktree_close(worktree);
7894 free(gitconfig_path);
7897 free(prepared_logmsg);
7904 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7905 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7906 "[remote-repository]\n", getprogname());
7911 print_load_info(int print_colored, int print_found, int print_trees,
7912 int ncolored, int nfound, int ntrees)
7914 if (print_colored) {
7915 printf("%d commit%s colored", ncolored,
7916 ncolored == 1 ? "" : "s");
7919 printf("%s%d object%s found",
7920 ncolored > 0 ? "; " : "",
7921 nfound, nfound == 1 ? "" : "s");
7924 printf("; %d tree%s scanned", ntrees,
7925 ntrees == 1 ? "" : "s");
7929 struct got_send_progress_arg {
7930 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7937 int last_nobj_total;
7941 int printed_something;
7943 struct got_pathlist_head *delete_branches;
7946 static const struct got_error *
7947 send_progress(void *arg, int ncolored, int nfound, int ntrees,
7948 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
7949 int nobj_written, off_t bytes_sent, const char *refname, int success)
7951 struct got_send_progress_arg *a = arg;
7952 char scaled_packsize[FMT_SCALED_STRSIZE];
7953 char scaled_sent[FMT_SCALED_STRSIZE];
7954 int p_deltify = 0, p_written = 0, p_sent = 0;
7955 int print_colored = 0, print_found = 0, print_trees = 0;
7956 int print_searching = 0, print_total = 0;
7957 int print_deltify = 0, print_written = 0, print_sent = 0;
7959 if (a->verbosity < 0)
7963 const char *status = success ? "accepted" : "rejected";
7966 struct got_pathlist_entry *pe;
7967 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7968 const char *branchname = pe->path;
7969 if (got_path_cmp(branchname, refname,
7970 strlen(branchname), strlen(refname)) == 0) {
7972 a->sent_something = 1;
7978 if (a->printed_something)
7980 printf("Server has %s %s", status, refname);
7981 a->printed_something = 1;
7985 if (a->last_ncolored != ncolored) {
7987 a->last_ncolored = ncolored;
7990 if (a->last_nfound != nfound) {
7993 a->last_nfound = nfound;
7996 if (a->last_ntrees != ntrees) {
8000 a->last_ntrees = ntrees;
8003 if ((print_colored || print_found || print_trees) &&
8006 print_load_info(print_colored, print_found, print_trees,
8007 ncolored, nfound, ntrees);
8008 a->printed_something = 1;
8011 } else if (!a->loading_done) {
8013 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8015 a->loading_done = 1;
8018 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8019 return got_error_from_errno("fmt_scaled");
8020 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8021 return got_error_from_errno("fmt_scaled");
8023 if (a->last_ncommits != ncommits) {
8024 print_searching = 1;
8025 a->last_ncommits = ncommits;
8028 if (a->last_nobj_total != nobj_total) {
8029 print_searching = 1;
8031 a->last_nobj_total = nobj_total;
8034 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8035 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8036 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8037 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8038 return got_error(GOT_ERR_NO_SPACE);
8041 if (nobj_deltify > 0 || nobj_written > 0) {
8042 if (nobj_deltify > 0) {
8043 p_deltify = (nobj_deltify * 100) / nobj_total;
8044 if (p_deltify != a->last_p_deltify) {
8045 a->last_p_deltify = p_deltify;
8046 print_searching = 1;
8051 if (nobj_written > 0) {
8052 p_written = (nobj_written * 100) / nobj_total;
8053 if (p_written != a->last_p_written) {
8054 a->last_p_written = p_written;
8055 print_searching = 1;
8063 if (bytes_sent > 0) {
8064 p_sent = (bytes_sent * 100) / packfile_size;
8065 if (p_sent != a->last_p_sent) {
8066 a->last_p_sent = p_sent;
8067 print_searching = 1;
8073 a->sent_something = 1;
8076 if (print_searching || print_total || print_deltify || print_written ||
8079 if (print_searching)
8080 printf("packing %d reference%s", ncommits,
8081 ncommits == 1 ? "" : "s");
8083 printf("; %d object%s", nobj_total,
8084 nobj_total == 1 ? "" : "s");
8086 printf("; deltify: %d%%", p_deltify);
8088 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8089 scaled_packsize, p_sent);
8090 else if (print_written)
8091 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8092 scaled_packsize, p_written);
8093 if (print_searching || print_total || print_deltify ||
8094 print_written || print_sent) {
8095 a->printed_something = 1;
8101 static const struct got_error *
8102 cmd_send(int argc, char *argv[])
8104 const struct got_error *error = NULL;
8105 char *cwd = NULL, *repo_path = NULL;
8106 const char *remote_name;
8107 char *proto = NULL, *host = NULL, *port = NULL;
8108 char *repo_name = NULL, *server_path = NULL;
8109 const struct got_remote_repo *remotes, *remote = NULL;
8110 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8111 struct got_repository *repo = NULL;
8112 struct got_worktree *worktree = NULL;
8113 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8114 struct got_pathlist_head branches;
8115 struct got_pathlist_head tags;
8116 struct got_reflist_head all_branches;
8117 struct got_reflist_head all_tags;
8118 struct got_pathlist_head delete_args;
8119 struct got_pathlist_head delete_branches;
8120 struct got_reflist_entry *re;
8121 struct got_pathlist_entry *pe;
8122 int i, ch, sendfd = -1, sendstatus;
8124 struct got_send_progress_arg spa;
8125 int verbosity = 0, overwrite_refs = 0;
8126 int send_all_branches = 0, send_all_tags = 0;
8127 struct got_reference *ref = NULL;
8129 TAILQ_INIT(&branches);
8131 TAILQ_INIT(&all_branches);
8132 TAILQ_INIT(&all_tags);
8133 TAILQ_INIT(&delete_args);
8134 TAILQ_INIT(&delete_branches);
8136 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8139 send_all_branches = 1;
8142 error = got_pathlist_append(&branches, optarg, NULL);
8148 error = got_pathlist_append(&delete_args, optarg, NULL);
8156 repo_path = realpath(optarg, NULL);
8157 if (repo_path == NULL)
8158 return got_error_from_errno2("realpath",
8160 got_path_strip_trailing_slashes(repo_path);
8163 error = got_pathlist_append(&tags, optarg, NULL);
8174 else if (verbosity < 3)
8188 if (send_all_branches && !TAILQ_EMPTY(&branches))
8189 option_conflict('a', 'b');
8190 if (send_all_tags && !TAILQ_EMPTY(&tags))
8191 option_conflict('T', 't');
8195 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8197 remote_name = argv[0];
8201 cwd = getcwd(NULL, 0);
8203 error = got_error_from_errno("getcwd");
8207 if (repo_path == NULL) {
8208 error = got_worktree_open(&worktree, cwd);
8209 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8215 strdup(got_worktree_get_repo_path(worktree));
8216 if (repo_path == NULL)
8217 error = got_error_from_errno("strdup");
8221 repo_path = strdup(cwd);
8222 if (repo_path == NULL) {
8223 error = got_error_from_errno("strdup");
8229 error = got_repo_open(&repo, repo_path, NULL);
8234 worktree_conf = got_worktree_get_gotconfig(worktree);
8235 if (worktree_conf) {
8236 got_gotconfig_get_remotes(&nremotes, &remotes,
8238 for (i = 0; i < nremotes; i++) {
8239 if (strcmp(remotes[i].name, remote_name) == 0) {
8240 remote = &remotes[i];
8246 if (remote == NULL) {
8247 repo_conf = got_repo_get_gotconfig(repo);
8249 got_gotconfig_get_remotes(&nremotes, &remotes,
8251 for (i = 0; i < nremotes; i++) {
8252 if (strcmp(remotes[i].name, remote_name) == 0) {
8253 remote = &remotes[i];
8259 if (remote == NULL) {
8260 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8261 for (i = 0; i < nremotes; i++) {
8262 if (strcmp(remotes[i].name, remote_name) == 0) {
8263 remote = &remotes[i];
8268 if (remote == NULL) {
8269 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8273 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8274 &repo_name, remote->send_url);
8278 if (strcmp(proto, "git") == 0) {
8280 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8281 "sendfd dns inet unveil", NULL) == -1)
8284 } else if (strcmp(proto, "git+ssh") == 0 ||
8285 strcmp(proto, "ssh") == 0) {
8287 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8288 "sendfd unveil", NULL) == -1)
8291 } else if (strcmp(proto, "http") == 0 ||
8292 strcmp(proto, "git+http") == 0) {
8293 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8296 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8300 error = got_dial_apply_unveil(proto);
8304 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8308 if (send_all_branches) {
8309 error = got_ref_list(&all_branches, repo, "refs/heads",
8310 got_ref_cmp_by_name, NULL);
8313 TAILQ_FOREACH(re, &all_branches, entry) {
8314 const char *branchname = got_ref_get_name(re->ref);
8315 error = got_pathlist_append(&branches,
8321 } else if (nbranches == 0) {
8322 for (i = 0; i < remote->nsend_branches; i++) {
8323 got_pathlist_append(&branches,
8324 remote->send_branches[i], NULL);
8328 if (send_all_tags) {
8329 error = got_ref_list(&all_tags, repo, "refs/tags",
8330 got_ref_cmp_by_name, NULL);
8333 TAILQ_FOREACH(re, &all_tags, entry) {
8334 const char *tagname = got_ref_get_name(re->ref);
8335 error = got_pathlist_append(&tags,
8344 * To prevent accidents only branches in refs/heads/ can be deleted
8345 * with 'got send -d'.
8346 * Deleting anything else requires local repository access or Git.
8348 TAILQ_FOREACH(pe, &delete_args, entry) {
8349 const char *branchname = pe->path;
8351 struct got_pathlist_entry *new;
8352 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8353 s = strdup(branchname);
8355 error = got_error_from_errno("strdup");
8359 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8360 error = got_error_from_errno("asprintf");
8364 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8365 if (error || new == NULL /* duplicate */)
8372 if (nbranches == 0 && ndelete_branches == 0) {
8373 struct got_reference *head_ref;
8375 error = got_ref_open(&head_ref, repo,
8376 got_worktree_get_head_ref_name(worktree), 0);
8378 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8381 if (got_ref_is_symbolic(head_ref)) {
8382 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8383 got_ref_close(head_ref);
8388 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8396 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8397 port ? ":" : "", port ? port : "");
8399 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8400 server_path, verbosity);
8404 memset(&spa, 0, sizeof(spa));
8405 spa.last_scaled_packsize[0] = '\0';
8406 spa.last_p_deltify = -1;
8407 spa.last_p_written = -1;
8408 spa.verbosity = verbosity;
8409 spa.delete_branches = &delete_branches;
8410 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8411 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8412 check_cancelled, NULL);
8413 if (spa.printed_something)
8417 if (!spa.sent_something && verbosity >= 0)
8418 printf("Already up-to-date\n");
8421 if (kill(sendpid, SIGTERM) == -1)
8422 error = got_error_from_errno("kill");
8423 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8424 error = got_error_from_errno("waitpid");
8426 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8427 error = got_error_from_errno("close");
8429 const struct got_error *close_err = got_repo_close(repo);
8434 got_worktree_close(worktree);
8437 got_pathlist_free(&branches);
8438 got_pathlist_free(&tags);
8439 got_ref_list_free(&all_branches);
8440 got_ref_list_free(&all_tags);
8441 got_pathlist_free(&delete_args);
8442 TAILQ_FOREACH(pe, &delete_branches, entry)
8443 free((char *)pe->path);
8444 got_pathlist_free(&delete_branches);
8456 usage_cherrypick(void)
8458 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8462 static const struct got_error *
8463 cmd_cherrypick(int argc, char *argv[])
8465 const struct got_error *error = NULL;
8466 struct got_worktree *worktree = NULL;
8467 struct got_repository *repo = NULL;
8468 char *cwd = NULL, *commit_id_str = NULL;
8469 struct got_object_id *commit_id = NULL;
8470 struct got_commit_object *commit = NULL;
8471 struct got_object_qid *pid;
8473 struct got_update_progress_arg upa;
8475 while ((ch = getopt(argc, argv, "")) != -1) {
8487 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8488 "unveil", NULL) == -1)
8494 cwd = getcwd(NULL, 0);
8496 error = got_error_from_errno("getcwd");
8499 error = got_worktree_open(&worktree, cwd);
8501 if (error->code == GOT_ERR_NOT_WORKTREE)
8502 error = wrap_not_worktree_error(error, "cherrypick",
8507 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8512 error = apply_unveil(got_repo_get_path(repo), 0,
8513 got_worktree_get_root_path(worktree));
8517 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8518 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8521 error = got_object_id_str(&commit_id_str, commit_id);
8525 error = got_object_open_as_commit(&commit, repo, commit_id);
8528 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8529 memset(&upa, 0, sizeof(upa));
8530 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8531 commit_id, repo, update_progress, &upa, check_cancelled,
8536 if (upa.did_something)
8537 printf("Merged commit %s\n", commit_id_str);
8538 print_merge_progress_stats(&upa);
8541 got_object_commit_close(commit);
8542 free(commit_id_str);
8544 got_worktree_close(worktree);
8546 const struct got_error *close_err = got_repo_close(repo);
8556 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8560 static const struct got_error *
8561 cmd_backout(int argc, char *argv[])
8563 const struct got_error *error = NULL;
8564 struct got_worktree *worktree = NULL;
8565 struct got_repository *repo = NULL;
8566 char *cwd = NULL, *commit_id_str = NULL;
8567 struct got_object_id *commit_id = NULL;
8568 struct got_commit_object *commit = NULL;
8569 struct got_object_qid *pid;
8571 struct got_update_progress_arg upa;
8573 while ((ch = getopt(argc, argv, "")) != -1) {
8585 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8586 "unveil", NULL) == -1)
8592 cwd = getcwd(NULL, 0);
8594 error = got_error_from_errno("getcwd");
8597 error = got_worktree_open(&worktree, cwd);
8599 if (error->code == GOT_ERR_NOT_WORKTREE)
8600 error = wrap_not_worktree_error(error, "backout", cwd);
8604 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8609 error = apply_unveil(got_repo_get_path(repo), 0,
8610 got_worktree_get_root_path(worktree));
8614 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8615 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8618 error = got_object_id_str(&commit_id_str, commit_id);
8622 error = got_object_open_as_commit(&commit, repo, commit_id);
8625 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8627 error = got_error(GOT_ERR_ROOT_COMMIT);
8631 memset(&upa, 0, sizeof(upa));
8632 error = got_worktree_merge_files(worktree, commit_id, pid->id,
8633 repo, update_progress, &upa, check_cancelled, NULL);
8637 if (upa.did_something)
8638 printf("Backed out commit %s\n", commit_id_str);
8639 print_merge_progress_stats(&upa);
8642 got_object_commit_close(commit);
8643 free(commit_id_str);
8645 got_worktree_close(worktree);
8647 const struct got_error *close_err = got_repo_close(repo);
8657 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8663 trim_logmsg(char *logmsg, int limit)
8668 len = strlen(logmsg);
8672 nl = strchr(logmsg, '\n');
8677 static const struct got_error *
8678 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8680 const struct got_error *err;
8681 char *logmsg0 = NULL;
8684 err = got_object_commit_get_logmsg(&logmsg0, commit);
8689 while (isspace((unsigned char)s[0]))
8692 *logmsg = strdup(s);
8693 if (*logmsg == NULL) {
8694 err = got_error_from_errno("strdup");
8698 trim_logmsg(*logmsg, limit);
8704 static const struct got_error *
8705 show_rebase_merge_conflict(struct got_object_id *id,
8706 struct got_repository *repo)
8708 const struct got_error *err;
8709 struct got_commit_object *commit = NULL;
8710 char *id_str = NULL, *logmsg = NULL;
8712 err = got_object_open_as_commit(&commit, repo, id);
8716 err = got_object_id_str(&id_str, id);
8722 err = get_short_logmsg(&logmsg, 42, commit);
8726 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8729 got_object_commit_close(commit);
8734 static const struct got_error *
8735 show_rebase_progress(struct got_commit_object *commit,
8736 struct got_object_id *old_id, struct got_object_id *new_id)
8738 const struct got_error *err;
8739 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8741 err = got_object_id_str(&old_id_str, old_id);
8746 err = got_object_id_str(&new_id_str, new_id);
8751 old_id_str[12] = '\0';
8753 new_id_str[12] = '\0';
8755 err = get_short_logmsg(&logmsg, 42, commit);
8759 printf("%s -> %s: %s\n", old_id_str,
8760 new_id_str ? new_id_str : "no-op change", logmsg);
8768 static const struct got_error *
8769 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8770 struct got_reference *branch, struct got_reference *new_base_branch,
8771 struct got_reference *tmp_branch, struct got_repository *repo,
8774 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8775 return got_worktree_rebase_complete(worktree, fileindex,
8776 new_base_branch, tmp_branch, branch, repo, create_backup);
8779 static const struct got_error *
8780 rebase_commit(struct got_pathlist_head *merged_paths,
8781 struct got_worktree *worktree, struct got_fileindex *fileindex,
8782 struct got_reference *tmp_branch,
8783 struct got_object_id *commit_id, struct got_repository *repo)
8785 const struct got_error *error;
8786 struct got_commit_object *commit;
8787 struct got_object_id *new_commit_id;
8789 error = got_object_open_as_commit(&commit, repo, commit_id);
8793 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8794 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8796 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8798 error = show_rebase_progress(commit, commit_id, NULL);
8800 error = show_rebase_progress(commit, commit_id, new_commit_id);
8801 free(new_commit_id);
8804 got_object_commit_close(commit);
8808 struct check_path_prefix_arg {
8809 const char *path_prefix;
8814 static const struct got_error *
8815 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8816 struct got_blob_object *blob2, struct got_object_id *id1,
8817 struct got_object_id *id2, const char *path1, const char *path2,
8818 mode_t mode1, mode_t mode2, struct got_repository *repo)
8820 struct check_path_prefix_arg *a = arg;
8822 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8823 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8824 return got_error(a->errcode);
8829 static const struct got_error *
8830 check_path_prefix(struct got_object_id *parent_id,
8831 struct got_object_id *commit_id, const char *path_prefix,
8832 int errcode, struct got_repository *repo)
8834 const struct got_error *err;
8835 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8836 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8837 struct check_path_prefix_arg cpp_arg;
8839 if (got_path_is_root_dir(path_prefix))
8842 err = got_object_open_as_commit(&commit, repo, commit_id);
8846 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8850 err = got_object_open_as_tree(&tree1, repo,
8851 got_object_commit_get_tree_id(parent_commit));
8855 err = got_object_open_as_tree(&tree2, repo,
8856 got_object_commit_get_tree_id(commit));
8860 cpp_arg.path_prefix = path_prefix;
8861 while (cpp_arg.path_prefix[0] == '/')
8862 cpp_arg.path_prefix++;
8863 cpp_arg.len = strlen(cpp_arg.path_prefix);
8864 cpp_arg.errcode = errcode;
8865 err = got_diff_tree(tree1, tree2, "", "", repo,
8866 check_path_prefix_in_diff, &cpp_arg, 0);
8869 got_object_tree_close(tree1);
8871 got_object_tree_close(tree2);
8873 got_object_commit_close(commit);
8875 got_object_commit_close(parent_commit);
8879 static const struct got_error *
8880 collect_commits(struct got_object_id_queue *commits,
8881 struct got_object_id *initial_commit_id,
8882 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8883 const char *path_prefix, int path_prefix_errcode,
8884 struct got_repository *repo)
8886 const struct got_error *err = NULL;
8887 struct got_commit_graph *graph = NULL;
8888 struct got_object_id *parent_id = NULL;
8889 struct got_object_qid *qid;
8890 struct got_object_id *commit_id = initial_commit_id;
8892 err = got_commit_graph_open(&graph, "/", 1);
8896 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8897 check_cancelled, NULL);
8900 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8901 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8902 check_cancelled, NULL);
8904 if (err->code == GOT_ERR_ITER_COMPLETED) {
8905 err = got_error_msg(GOT_ERR_ANCESTRY,
8906 "ran out of commits to rebase before "
8907 "youngest common ancestor commit has "
8912 err = check_path_prefix(parent_id, commit_id,
8913 path_prefix, path_prefix_errcode, repo);
8917 err = got_object_qid_alloc(&qid, commit_id);
8920 STAILQ_INSERT_HEAD(commits, qid, entry);
8921 commit_id = parent_id;
8925 got_commit_graph_close(graph);
8929 static const struct got_error *
8930 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8932 const struct got_error *err = NULL;
8933 time_t committer_time;
8935 char datebuf[11]; /* YYYY-MM-DD + NUL */
8936 char *author0 = NULL, *author, *smallerthan;
8937 char *logmsg0 = NULL, *logmsg, *newline;
8939 committer_time = got_object_commit_get_committer_time(commit);
8940 if (gmtime_r(&committer_time, &tm) == NULL)
8941 return got_error_from_errno("gmtime_r");
8942 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8943 return got_error(GOT_ERR_NO_SPACE);
8945 author0 = strdup(got_object_commit_get_author(commit));
8946 if (author0 == NULL)
8947 return got_error_from_errno("strdup");
8949 smallerthan = strchr(author, '<');
8950 if (smallerthan && smallerthan[1] != '\0')
8951 author = smallerthan + 1;
8952 author[strcspn(author, "@>")] = '\0';
8954 err = got_object_commit_get_logmsg(&logmsg0, commit);
8958 while (*logmsg == '\n')
8960 newline = strchr(logmsg, '\n');
8964 if (asprintf(brief_str, "%s %s %s",
8965 datebuf, author, logmsg) == -1)
8966 err = got_error_from_errno("asprintf");
8973 static const struct got_error *
8974 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8975 struct got_repository *repo)
8977 const struct got_error *err;
8980 err = got_object_id_str(&id_str, id);
8984 err = got_ref_delete(ref, repo);
8988 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8994 static const struct got_error *
8995 print_backup_ref(const char *branch_name, const char *new_id_str,
8996 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8997 struct got_reflist_object_id_map *refs_idmap,
8998 struct got_repository *repo)
9000 const struct got_error *err = NULL;
9001 struct got_reflist_head *refs;
9002 char *refs_str = NULL;
9003 struct got_object_id *new_commit_id = NULL;
9004 struct got_commit_object *new_commit = NULL;
9005 char *new_commit_brief_str = NULL;
9006 struct got_object_id *yca_id = NULL;
9007 struct got_commit_object *yca_commit = NULL;
9008 char *yca_id_str = NULL, *yca_brief_str = NULL;
9009 char *custom_refs_str;
9011 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9012 return got_error_from_errno("asprintf");
9014 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9015 0, 0, refs_idmap, custom_refs_str);
9019 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9023 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9025 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
9030 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9034 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9038 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9039 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9043 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9044 refs_str ? " (" : "", refs_str ? refs_str : "",
9045 refs_str ? ")" : "", new_commit_brief_str);
9046 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9047 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9051 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9055 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9059 err = got_object_id_str(&yca_id_str, yca_id);
9063 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9065 err = build_refs_str(&refs_str, refs, yca_id, repo);
9069 printf("history forked at %s%s%s%s\n %s\n",
9071 refs_str ? " (" : "", refs_str ? refs_str : "",
9072 refs_str ? ")" : "", yca_brief_str);
9075 free(custom_refs_str);
9076 free(new_commit_id);
9080 free(yca_brief_str);
9082 got_object_commit_close(new_commit);
9084 got_object_commit_close(yca_commit);
9089 static const struct got_error *
9090 process_backup_refs(const char *backup_ref_prefix,
9091 const char *wanted_branch_name,
9092 int delete, struct got_repository *repo)
9094 const struct got_error *err;
9095 struct got_reflist_head refs, backup_refs;
9096 struct got_reflist_entry *re;
9097 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9098 struct got_object_id *old_commit_id = NULL;
9099 char *branch_name = NULL;
9100 struct got_commit_object *old_commit = NULL;
9101 struct got_reflist_object_id_map *refs_idmap = NULL;
9102 int wanted_branch_found = 0;
9105 TAILQ_INIT(&backup_refs);
9107 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9111 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9115 if (wanted_branch_name) {
9116 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9117 wanted_branch_name += 11;
9120 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9121 got_ref_cmp_by_commit_timestamp_descending, repo);
9125 TAILQ_FOREACH(re, &backup_refs, entry) {
9126 const char *refname = got_ref_get_name(re->ref);
9129 err = check_cancelled(NULL);
9133 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9137 err = got_object_open_as_commit(&old_commit, repo,
9142 if (strncmp(backup_ref_prefix, refname,
9143 backup_ref_prefix_len) == 0)
9144 refname += backup_ref_prefix_len;
9146 while (refname[0] == '/')
9149 branch_name = strdup(refname);
9150 if (branch_name == NULL) {
9151 err = got_error_from_errno("strdup");
9154 slash = strrchr(branch_name, '/');
9157 refname += strlen(branch_name) + 1;
9160 if (wanted_branch_name == NULL ||
9161 strcmp(wanted_branch_name, branch_name) == 0) {
9162 wanted_branch_found = 1;
9164 err = delete_backup_ref(re->ref,
9165 old_commit_id, repo);
9167 err = print_backup_ref(branch_name, refname,
9168 old_commit_id, old_commit, refs_idmap,
9175 free(old_commit_id);
9176 old_commit_id = NULL;
9179 got_object_commit_close(old_commit);
9183 if (wanted_branch_name && !wanted_branch_found) {
9184 err = got_error_fmt(GOT_ERR_NOT_REF,
9185 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9189 got_reflist_object_id_map_free(refs_idmap);
9190 got_ref_list_free(&refs);
9191 got_ref_list_free(&backup_refs);
9192 free(old_commit_id);
9195 got_object_commit_close(old_commit);
9199 static const struct got_error *
9200 abort_progress(void *arg, unsigned char status, const char *path)
9203 * Unversioned files should not clutter progress output when
9204 * an operation is aborted.
9206 if (status == GOT_STATUS_UNVERSIONED)
9209 return update_progress(arg, status, path);
9212 static const struct got_error *
9213 cmd_rebase(int argc, char *argv[])
9215 const struct got_error *error = NULL;
9216 struct got_worktree *worktree = NULL;
9217 struct got_repository *repo = NULL;
9218 struct got_fileindex *fileindex = NULL;
9220 struct got_reference *branch = NULL;
9221 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9222 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9223 struct got_object_id *resume_commit_id = NULL;
9224 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9225 struct got_commit_object *commit = NULL;
9226 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9227 int histedit_in_progress = 0, merge_in_progress = 0;
9228 int create_backup = 1, list_backups = 0, delete_backups = 0;
9229 struct got_object_id_queue commits;
9230 struct got_pathlist_head merged_paths;
9231 const struct got_object_id_queue *parent_ids;
9232 struct got_object_qid *qid, *pid;
9233 struct got_update_progress_arg upa;
9235 STAILQ_INIT(&commits);
9236 TAILQ_INIT(&merged_paths);
9237 memset(&upa, 0, sizeof(upa));
9239 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9245 continue_rebase = 1;
9263 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9264 "unveil", NULL) == -1)
9269 option_conflict('l', 'a');
9270 if (continue_rebase)
9271 option_conflict('l', 'c');
9273 option_conflict('l', 'X');
9274 if (argc != 0 && argc != 1)
9276 } else if (delete_backups) {
9278 option_conflict('X', 'a');
9279 if (continue_rebase)
9280 option_conflict('X', 'c');
9282 option_conflict('l', 'X');
9283 if (argc != 0 && argc != 1)
9286 if (abort_rebase && continue_rebase)
9288 else if (abort_rebase || continue_rebase) {
9291 } else if (argc != 1)
9295 cwd = getcwd(NULL, 0);
9297 error = got_error_from_errno("getcwd");
9300 error = got_worktree_open(&worktree, cwd);
9302 if (list_backups || delete_backups) {
9303 if (error->code != GOT_ERR_NOT_WORKTREE)
9306 if (error->code == GOT_ERR_NOT_WORKTREE)
9307 error = wrap_not_worktree_error(error,
9313 error = got_repo_open(&repo,
9314 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9318 error = apply_unveil(got_repo_get_path(repo), 0,
9319 worktree ? got_worktree_get_root_path(worktree) : NULL);
9323 if (list_backups || delete_backups) {
9324 error = process_backup_refs(
9325 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9326 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9327 goto done; /* nothing else to do */
9330 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9334 if (histedit_in_progress) {
9335 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9339 error = got_worktree_merge_in_progress(&merge_in_progress,
9343 if (merge_in_progress) {
9344 error = got_error(GOT_ERR_MERGE_BUSY);
9348 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9353 if (!rebase_in_progress) {
9354 error = got_error(GOT_ERR_NOT_REBASING);
9357 error = got_worktree_rebase_continue(&resume_commit_id,
9358 &new_base_branch, &tmp_branch, &branch, &fileindex,
9362 printf("Switching work tree to %s\n",
9363 got_ref_get_symref_target(new_base_branch));
9364 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9365 new_base_branch, abort_progress, &upa);
9368 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9369 print_merge_progress_stats(&upa);
9370 goto done; /* nothing else to do */
9373 if (continue_rebase) {
9374 if (!rebase_in_progress) {
9375 error = got_error(GOT_ERR_NOT_REBASING);
9378 error = got_worktree_rebase_continue(&resume_commit_id,
9379 &new_base_branch, &tmp_branch, &branch, &fileindex,
9384 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9385 resume_commit_id, repo);
9389 yca_id = got_object_id_dup(resume_commit_id);
9390 if (yca_id == NULL) {
9391 error = got_error_from_errno("got_object_id_dup");
9395 error = got_ref_open(&branch, repo, argv[0], 0);
9400 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9404 if (!continue_rebase) {
9405 struct got_object_id *base_commit_id;
9407 base_commit_id = got_worktree_get_base_commit_id(worktree);
9408 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9409 base_commit_id, branch_head_commit_id, 1, repo,
9410 check_cancelled, NULL);
9413 if (yca_id == NULL) {
9414 error = got_error_msg(GOT_ERR_ANCESTRY,
9415 "specified branch shares no common ancestry "
9416 "with work tree's branch");
9420 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9422 if (error->code != GOT_ERR_ANCESTRY)
9426 struct got_pathlist_head paths;
9427 printf("%s is already based on %s\n",
9428 got_ref_get_name(branch),
9429 got_worktree_get_head_ref_name(worktree));
9430 error = switch_head_ref(branch, branch_head_commit_id,
9434 error = got_worktree_set_base_commit_id(worktree, repo,
9435 branch_head_commit_id);
9439 error = got_pathlist_append(&paths, "", NULL);
9442 error = got_worktree_checkout_files(worktree,
9443 &paths, repo, update_progress, &upa,
9444 check_cancelled, NULL);
9445 got_pathlist_free(&paths);
9448 if (upa.did_something) {
9450 error = got_object_id_str(&id_str,
9451 branch_head_commit_id);
9454 printf("Updated to %s: %s\n",
9455 got_worktree_get_head_ref_name(worktree),
9459 printf("Already up-to-date\n");
9460 print_update_progress_stats(&upa);
9463 error = got_worktree_rebase_prepare(&new_base_branch,
9464 &tmp_branch, &fileindex, worktree, branch, repo);
9469 commit_id = branch_head_commit_id;
9470 error = got_object_open_as_commit(&commit, repo, commit_id);
9474 parent_ids = got_object_commit_get_parent_ids(commit);
9475 pid = STAILQ_FIRST(parent_ids);
9477 if (!continue_rebase) {
9478 error = got_worktree_rebase_abort(worktree, fileindex,
9479 repo, new_base_branch, abort_progress, &upa);
9482 printf("Rebase of %s aborted\n",
9483 got_ref_get_name(branch));
9484 print_merge_progress_stats(&upa);
9487 error = got_error(GOT_ERR_EMPTY_REBASE);
9490 error = collect_commits(&commits, commit_id, pid->id,
9491 yca_id, got_worktree_get_path_prefix(worktree),
9492 GOT_ERR_REBASE_PATH, repo);
9493 got_object_commit_close(commit);
9498 if (STAILQ_EMPTY(&commits)) {
9499 if (continue_rebase) {
9500 error = rebase_complete(worktree, fileindex,
9501 branch, new_base_branch, tmp_branch, repo,
9505 /* Fast-forward the reference of the branch. */
9506 struct got_object_id *new_head_commit_id;
9508 error = got_ref_resolve(&new_head_commit_id, repo,
9512 error = got_object_id_str(&id_str, new_head_commit_id);
9513 printf("Forwarding %s to commit %s\n",
9514 got_ref_get_name(branch), id_str);
9516 error = got_ref_change_ref(branch,
9517 new_head_commit_id);
9520 /* No backup needed since objects did not change. */
9526 STAILQ_FOREACH(qid, &commits, entry) {
9528 commit_id = qid->id;
9529 parent_id = pid ? pid->id : yca_id;
9532 memset(&upa, 0, sizeof(upa));
9533 error = got_worktree_rebase_merge_files(&merged_paths,
9534 worktree, fileindex, parent_id, commit_id, repo,
9535 update_progress, &upa, check_cancelled, NULL);
9539 print_merge_progress_stats(&upa);
9540 if (upa.conflicts > 0 || upa.missing > 0 ||
9541 upa.not_deleted > 0 || upa.unversioned > 0) {
9542 if (upa.conflicts > 0) {
9543 error = show_rebase_merge_conflict(qid->id,
9548 got_worktree_rebase_pathlist_free(&merged_paths);
9552 error = rebase_commit(&merged_paths, worktree, fileindex,
9553 tmp_branch, commit_id, repo);
9554 got_worktree_rebase_pathlist_free(&merged_paths);
9559 if (upa.conflicts > 0 || upa.missing > 0 ||
9560 upa.not_deleted > 0 || upa.unversioned > 0) {
9561 error = got_worktree_rebase_postpone(worktree, fileindex);
9564 if (upa.conflicts > 0 && upa.missing == 0 &&
9565 upa.not_deleted == 0 && upa.unversioned == 0) {
9566 error = got_error_msg(GOT_ERR_CONFLICTS,
9567 "conflicts must be resolved before rebasing "
9569 } else if (upa.conflicts > 0) {
9570 error = got_error_msg(GOT_ERR_CONFLICTS,
9571 "conflicts must be resolved before rebasing "
9572 "can continue; changes destined for some "
9573 "files were not yet merged and should be "
9574 "merged manually if required before the "
9575 "rebase operation is continued");
9577 error = got_error_msg(GOT_ERR_CONFLICTS,
9578 "changes destined for some files were not "
9579 "yet merged and should be merged manually "
9580 "if required before the rebase operation "
9584 error = rebase_complete(worktree, fileindex, branch,
9585 new_base_branch, tmp_branch, repo, create_backup);
9587 got_object_id_queue_free(&commits);
9588 free(branch_head_commit_id);
9589 free(resume_commit_id);
9592 got_object_commit_close(commit);
9594 got_ref_close(branch);
9595 if (new_base_branch)
9596 got_ref_close(new_base_branch);
9598 got_ref_close(tmp_branch);
9600 got_worktree_close(worktree);
9602 const struct got_error *close_err = got_repo_close(repo);
9610 usage_histedit(void)
9612 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9613 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9618 #define GOT_HISTEDIT_PICK 'p'
9619 #define GOT_HISTEDIT_EDIT 'e'
9620 #define GOT_HISTEDIT_FOLD 'f'
9621 #define GOT_HISTEDIT_DROP 'd'
9622 #define GOT_HISTEDIT_MESG 'm'
9624 static const struct got_histedit_cmd {
9628 } got_histedit_cmds[] = {
9629 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9630 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9631 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9633 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9634 { GOT_HISTEDIT_MESG, "mesg",
9635 "single-line log message for commit above (open editor if empty)" },
9638 struct got_histedit_list_entry {
9639 TAILQ_ENTRY(got_histedit_list_entry) entry;
9640 struct got_object_id *commit_id;
9641 const struct got_histedit_cmd *cmd;
9644 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9646 static const struct got_error *
9647 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9648 FILE *f, struct got_repository *repo)
9650 const struct got_error *err = NULL;
9651 char *logmsg = NULL, *id_str = NULL;
9652 struct got_commit_object *commit = NULL;
9655 err = got_object_open_as_commit(&commit, repo, commit_id);
9659 err = get_short_logmsg(&logmsg, 34, commit);
9663 err = got_object_id_str(&id_str, commit_id);
9667 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9669 err = got_ferror(f, GOT_ERR_IO);
9672 got_object_commit_close(commit);
9678 static const struct got_error *
9679 histedit_write_commit_list(struct got_object_id_queue *commits,
9680 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9681 struct got_repository *repo)
9683 const struct got_error *err = NULL;
9684 struct got_object_qid *qid;
9685 const char *histedit_cmd = NULL;
9687 if (STAILQ_EMPTY(commits))
9688 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9690 STAILQ_FOREACH(qid, commits, entry) {
9691 histedit_cmd = got_histedit_cmds[0].name;
9693 histedit_cmd = "edit";
9694 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9695 histedit_cmd = "fold";
9696 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9699 if (edit_logmsg_only) {
9700 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9702 err = got_ferror(f, GOT_ERR_IO);
9711 static const struct got_error *
9712 write_cmd_list(FILE *f, const char *branch_name,
9713 struct got_object_id_queue *commits)
9715 const struct got_error *err = NULL;
9719 struct got_object_qid *qid;
9721 qid = STAILQ_FIRST(commits);
9722 err = got_object_id_str(&id_str, qid->id);
9727 "# Editing the history of branch '%s' starting at\n"
9729 "# Commits will be processed in order from top to "
9730 "bottom of this file.\n", branch_name, id_str);
9732 err = got_ferror(f, GOT_ERR_IO);
9736 n = fprintf(f, "# Available histedit commands:\n");
9738 err = got_ferror(f, GOT_ERR_IO);
9742 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9743 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9744 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9747 err = got_ferror(f, GOT_ERR_IO);
9756 static const struct got_error *
9757 histedit_syntax_error(int lineno)
9759 static char msg[42];
9762 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9764 if (ret == -1 || ret >= sizeof(msg))
9765 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9767 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9770 static const struct got_error *
9771 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9772 char *logmsg, struct got_repository *repo)
9774 const struct got_error *err;
9775 struct got_commit_object *folded_commit = NULL;
9776 char *id_str, *folded_logmsg = NULL;
9778 err = got_object_id_str(&id_str, hle->commit_id);
9782 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9786 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9789 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9790 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9791 folded_logmsg) == -1) {
9792 err = got_error_from_errno("asprintf");
9796 got_object_commit_close(folded_commit);
9798 free(folded_logmsg);
9802 static struct got_histedit_list_entry *
9803 get_folded_commits(struct got_histedit_list_entry *hle)
9805 struct got_histedit_list_entry *prev, *folded = NULL;
9807 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9808 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9809 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9810 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9812 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9818 static const struct got_error *
9819 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9820 struct got_repository *repo)
9822 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9823 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9824 const struct got_error *err = NULL;
9825 struct got_commit_object *commit = NULL;
9828 struct got_histedit_list_entry *folded = NULL;
9830 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9834 folded = get_folded_commits(hle);
9836 while (folded != hle) {
9837 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9838 folded = TAILQ_NEXT(folded, entry);
9841 err = append_folded_commit_msg(&new_msg, folded,
9847 folded = TAILQ_NEXT(folded, entry);
9851 err = got_object_id_str(&id_str, hle->commit_id);
9854 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9857 logmsg_len = asprintf(&new_msg,
9858 "%s\n# original log message of commit %s: %s",
9859 logmsg ? logmsg : "", id_str, orig_logmsg);
9860 if (logmsg_len == -1) {
9861 err = got_error_from_errno("asprintf");
9867 err = got_object_id_str(&id_str, hle->commit_id);
9871 err = got_opentemp_named_fd(&logmsg_path, &fd,
9872 GOT_TMPDIR_STR "/got-logmsg");
9876 write(fd, logmsg, logmsg_len);
9879 err = get_editor(&editor);
9883 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9886 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9889 hle->logmsg = strdup(new_msg);
9890 if (hle->logmsg == NULL)
9891 err = got_error_from_errno("strdup");
9894 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9895 err = got_error_from_errno2("unlink", logmsg_path);
9901 got_object_commit_close(commit);
9905 static const struct got_error *
9906 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9907 FILE *f, struct got_repository *repo)
9909 const struct got_error *err = NULL;
9910 char *line = NULL, *p, *end;
9914 const struct got_histedit_cmd *cmd;
9915 struct got_object_id *commit_id = NULL;
9916 struct got_histedit_list_entry *hle = NULL;
9919 len = getline(&line, &size, f);
9921 const struct got_error *getline_err;
9924 getline_err = got_error_from_errno("getline");
9925 err = got_ferror(f, getline_err->code);
9930 while (isspace((unsigned char)p[0]))
9932 if (p[0] == '#' || p[0] == '\0') {
9938 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9939 cmd = &got_histedit_cmds[i];
9940 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9941 isspace((unsigned char)p[strlen(cmd->name)])) {
9942 p += strlen(cmd->name);
9945 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9950 if (i == nitems(got_histedit_cmds)) {
9951 err = histedit_syntax_error(lineno);
9954 while (isspace((unsigned char)p[0]))
9956 if (cmd->code == GOT_HISTEDIT_MESG) {
9957 if (hle == NULL || hle->logmsg != NULL) {
9958 err = got_error(GOT_ERR_HISTEDIT_CMD);
9962 err = histedit_edit_logmsg(hle, repo);
9966 hle->logmsg = strdup(p);
9967 if (hle->logmsg == NULL) {
9968 err = got_error_from_errno("strdup");
9977 while (end[0] && !isspace((unsigned char)end[0]))
9981 err = got_object_resolve_id_str(&commit_id, repo, p);
9983 /* override error code */
9984 err = histedit_syntax_error(lineno);
9988 hle = malloc(sizeof(*hle));
9990 err = got_error_from_errno("malloc");
9994 hle->commit_id = commit_id;
9999 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10007 static const struct got_error *
10008 histedit_check_script(struct got_histedit_list *histedit_cmds,
10009 struct got_object_id_queue *commits, struct got_repository *repo)
10011 const struct got_error *err = NULL;
10012 struct got_object_qid *qid;
10013 struct got_histedit_list_entry *hle;
10014 static char msg[92];
10017 if (TAILQ_EMPTY(histedit_cmds))
10018 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10019 "histedit script contains no commands");
10020 if (STAILQ_EMPTY(commits))
10021 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10023 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10024 struct got_histedit_list_entry *hle2;
10025 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10028 if (got_object_id_cmp(hle->commit_id,
10029 hle2->commit_id) != 0)
10031 err = got_object_id_str(&id_str, hle->commit_id);
10034 snprintf(msg, sizeof(msg), "commit %s is listed "
10035 "more than once in histedit script", id_str);
10037 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10041 STAILQ_FOREACH(qid, commits, entry) {
10042 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10043 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
10047 err = got_object_id_str(&id_str, qid->id);
10050 snprintf(msg, sizeof(msg),
10051 "commit %s missing from histedit script", id_str);
10053 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10057 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10058 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10059 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10060 "last commit in histedit script cannot be folded");
10065 static const struct got_error *
10066 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10067 const char *path, struct got_object_id_queue *commits,
10068 struct got_repository *repo)
10070 const struct got_error *err = NULL;
10074 err = get_editor(&editor);
10078 if (spawn_editor(editor, path) == -1) {
10079 err = got_error_from_errno("failed spawning editor");
10083 f = fopen(path, "re");
10085 err = got_error_from_errno("fopen");
10088 err = histedit_parse_list(histedit_cmds, f, repo);
10092 err = histedit_check_script(histedit_cmds, commits, repo);
10094 if (f && fclose(f) == EOF && err == NULL)
10095 err = got_error_from_errno("fclose");
10100 static const struct got_error *
10101 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10102 struct got_object_id_queue *, const char *, const char *,
10103 struct got_repository *);
10105 static const struct got_error *
10106 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10107 struct got_object_id_queue *commits, const char *branch_name,
10108 int edit_logmsg_only, int fold_only, int edit_only,
10109 struct got_repository *repo)
10111 const struct got_error *err;
10115 err = got_opentemp_named(&path, &f, "got-histedit");
10119 err = write_cmd_list(f, branch_name, commits);
10123 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10124 fold_only, edit_only, repo);
10128 if (edit_logmsg_only || fold_only || edit_only) {
10130 err = histedit_parse_list(histedit_cmds, f, repo);
10132 if (fclose(f) == EOF) {
10133 err = got_error_from_errno("fclose");
10137 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10139 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10140 err->code != GOT_ERR_HISTEDIT_CMD)
10142 err = histedit_edit_list_retry(histedit_cmds, err,
10143 commits, path, branch_name, repo);
10147 if (f && fclose(f) == EOF && err == NULL)
10148 err = got_error_from_errno("fclose");
10149 if (path && unlink(path) != 0 && err == NULL)
10150 err = got_error_from_errno2("unlink", path);
10155 static const struct got_error *
10156 histedit_save_list(struct got_histedit_list *histedit_cmds,
10157 struct got_worktree *worktree, struct got_repository *repo)
10159 const struct got_error *err = NULL;
10162 struct got_histedit_list_entry *hle;
10163 struct got_commit_object *commit = NULL;
10165 err = got_worktree_get_histedit_script_path(&path, worktree);
10169 f = fopen(path, "we");
10171 err = got_error_from_errno2("fopen", path);
10174 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10175 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10181 int n = fprintf(f, "%c %s\n",
10182 GOT_HISTEDIT_MESG, hle->logmsg);
10184 err = got_ferror(f, GOT_ERR_IO);
10190 if (f && fclose(f) == EOF && err == NULL)
10191 err = got_error_from_errno("fclose");
10194 got_object_commit_close(commit);
10199 histedit_free_list(struct got_histedit_list *histedit_cmds)
10201 struct got_histedit_list_entry *hle;
10203 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10204 TAILQ_REMOVE(histedit_cmds, hle, entry);
10209 static const struct got_error *
10210 histedit_load_list(struct got_histedit_list *histedit_cmds,
10211 const char *path, struct got_repository *repo)
10213 const struct got_error *err = NULL;
10216 f = fopen(path, "re");
10218 err = got_error_from_errno2("fopen", path);
10222 err = histedit_parse_list(histedit_cmds, f, repo);
10224 if (f && fclose(f) == EOF && err == NULL)
10225 err = got_error_from_errno("fclose");
10229 static const struct got_error *
10230 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10231 const struct got_error *edit_err, struct got_object_id_queue *commits,
10232 const char *path, const char *branch_name, struct got_repository *repo)
10234 const struct got_error *err = NULL, *prev_err = edit_err;
10237 while (resp != 'c' && resp != 'r' && resp != 'a') {
10238 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10239 "or (a)bort: ", getprogname(), prev_err->msg);
10244 histedit_free_list(histedit_cmds);
10245 err = histedit_run_editor(histedit_cmds, path, commits,
10248 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10249 err->code != GOT_ERR_HISTEDIT_CMD)
10256 } else if (resp == 'r') {
10257 histedit_free_list(histedit_cmds);
10258 err = histedit_edit_script(histedit_cmds,
10259 commits, branch_name, 0, 0, 0, repo);
10261 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10262 err->code != GOT_ERR_HISTEDIT_CMD)
10269 } else if (resp == 'a') {
10270 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10273 printf("invalid response '%c'\n", resp);
10279 static const struct got_error *
10280 histedit_complete(struct got_worktree *worktree,
10281 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10282 struct got_reference *branch, struct got_repository *repo)
10284 printf("Switching work tree to %s\n",
10285 got_ref_get_symref_target(branch));
10286 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10290 static const struct got_error *
10291 show_histedit_progress(struct got_commit_object *commit,
10292 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10294 const struct got_error *err;
10295 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10297 err = got_object_id_str(&old_id_str, hle->commit_id);
10302 err = got_object_id_str(&new_id_str, new_id);
10307 old_id_str[12] = '\0';
10309 new_id_str[12] = '\0';
10312 logmsg = strdup(hle->logmsg);
10313 if (logmsg == NULL) {
10314 err = got_error_from_errno("strdup");
10317 trim_logmsg(logmsg, 42);
10319 err = get_short_logmsg(&logmsg, 42, commit);
10324 switch (hle->cmd->code) {
10325 case GOT_HISTEDIT_PICK:
10326 case GOT_HISTEDIT_EDIT:
10327 printf("%s -> %s: %s\n", old_id_str,
10328 new_id_str ? new_id_str : "no-op change", logmsg);
10330 case GOT_HISTEDIT_DROP:
10331 case GOT_HISTEDIT_FOLD:
10332 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10344 static const struct got_error *
10345 histedit_commit(struct got_pathlist_head *merged_paths,
10346 struct got_worktree *worktree, struct got_fileindex *fileindex,
10347 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10348 struct got_repository *repo)
10350 const struct got_error *err;
10351 struct got_commit_object *commit;
10352 struct got_object_id *new_commit_id;
10354 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10355 && hle->logmsg == NULL) {
10356 err = histedit_edit_logmsg(hle, repo);
10361 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10365 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10366 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10367 hle->logmsg, repo);
10369 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10371 err = show_histedit_progress(commit, hle, NULL);
10373 err = show_histedit_progress(commit, hle, new_commit_id);
10374 free(new_commit_id);
10377 got_object_commit_close(commit);
10381 static const struct got_error *
10382 histedit_skip_commit(struct got_histedit_list_entry *hle,
10383 struct got_worktree *worktree, struct got_repository *repo)
10385 const struct got_error *error;
10386 struct got_commit_object *commit;
10388 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10393 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10397 error = show_histedit_progress(commit, hle, NULL);
10398 got_object_commit_close(commit);
10402 static const struct got_error *
10403 check_local_changes(void *arg, unsigned char status,
10404 unsigned char staged_status, const char *path,
10405 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10406 struct got_object_id *commit_id, int dirfd, const char *de_name)
10408 int *have_local_changes = arg;
10411 case GOT_STATUS_ADD:
10412 case GOT_STATUS_DELETE:
10413 case GOT_STATUS_MODIFY:
10414 case GOT_STATUS_CONFLICT:
10415 *have_local_changes = 1;
10416 return got_error(GOT_ERR_CANCELLED);
10421 switch (staged_status) {
10422 case GOT_STATUS_ADD:
10423 case GOT_STATUS_DELETE:
10424 case GOT_STATUS_MODIFY:
10425 *have_local_changes = 1;
10426 return got_error(GOT_ERR_CANCELLED);
10434 static const struct got_error *
10435 cmd_histedit(int argc, char *argv[])
10437 const struct got_error *error = NULL;
10438 struct got_worktree *worktree = NULL;
10439 struct got_fileindex *fileindex = NULL;
10440 struct got_repository *repo = NULL;
10442 struct got_reference *branch = NULL;
10443 struct got_reference *tmp_branch = NULL;
10444 struct got_object_id *resume_commit_id = NULL;
10445 struct got_object_id *base_commit_id = NULL;
10446 struct got_object_id *head_commit_id = NULL;
10447 struct got_commit_object *commit = NULL;
10448 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10449 struct got_update_progress_arg upa;
10450 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10451 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10452 int list_backups = 0, delete_backups = 0;
10453 const char *edit_script_path = NULL;
10454 struct got_object_id_queue commits;
10455 struct got_pathlist_head merged_paths;
10456 const struct got_object_id_queue *parent_ids;
10457 struct got_object_qid *pid;
10458 struct got_histedit_list histedit_cmds;
10459 struct got_histedit_list_entry *hle;
10461 STAILQ_INIT(&commits);
10462 TAILQ_INIT(&histedit_cmds);
10463 TAILQ_INIT(&merged_paths);
10464 memset(&upa, 0, sizeof(upa));
10466 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10481 edit_script_path = optarg;
10484 edit_logmsg_only = 1;
10490 delete_backups = 1;
10502 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10503 "unveil", NULL) == -1)
10506 if (abort_edit && continue_edit)
10507 option_conflict('a', 'c');
10508 if (edit_script_path && edit_logmsg_only)
10509 option_conflict('F', 'm');
10510 if (abort_edit && edit_logmsg_only)
10511 option_conflict('a', 'm');
10512 if (continue_edit && edit_logmsg_only)
10513 option_conflict('c', 'm');
10514 if (abort_edit && fold_only)
10515 option_conflict('a', 'f');
10516 if (continue_edit && fold_only)
10517 option_conflict('c', 'f');
10518 if (fold_only && edit_logmsg_only)
10519 option_conflict('f', 'm');
10520 if (edit_script_path && fold_only)
10521 option_conflict('F', 'f');
10522 if (abort_edit && edit_only)
10523 option_conflict('a', 'e');
10524 if (continue_edit && edit_only)
10525 option_conflict('c', 'e');
10526 if (edit_only && edit_logmsg_only)
10527 option_conflict('e', 'm');
10528 if (edit_script_path && edit_only)
10529 option_conflict('F', 'e');
10530 if (list_backups) {
10532 option_conflict('l', 'a');
10534 option_conflict('l', 'c');
10535 if (edit_script_path)
10536 option_conflict('l', 'F');
10537 if (edit_logmsg_only)
10538 option_conflict('l', 'm');
10540 option_conflict('l', 'f');
10542 option_conflict('l', 'e');
10543 if (delete_backups)
10544 option_conflict('l', 'X');
10545 if (argc != 0 && argc != 1)
10547 } else if (delete_backups) {
10549 option_conflict('X', 'a');
10551 option_conflict('X', 'c');
10552 if (edit_script_path)
10553 option_conflict('X', 'F');
10554 if (edit_logmsg_only)
10555 option_conflict('X', 'm');
10557 option_conflict('X', 'f');
10559 option_conflict('X', 'e');
10561 option_conflict('X', 'l');
10562 if (argc != 0 && argc != 1)
10564 } else if (argc != 0)
10568 * This command cannot apply unveil(2) in all cases because the
10569 * user may choose to run an editor to edit the histedit script
10570 * and to edit individual commit log messages.
10571 * unveil(2) traverses exec(2); if an editor is used we have to
10572 * apply unveil after edit script and log messages have been written.
10573 * XXX TODO: Make use of unveil(2) where possible.
10576 cwd = getcwd(NULL, 0);
10578 error = got_error_from_errno("getcwd");
10581 error = got_worktree_open(&worktree, cwd);
10583 if (list_backups || delete_backups) {
10584 if (error->code != GOT_ERR_NOT_WORKTREE)
10587 if (error->code == GOT_ERR_NOT_WORKTREE)
10588 error = wrap_not_worktree_error(error,
10594 if (list_backups || delete_backups) {
10595 error = got_repo_open(&repo,
10596 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10600 error = apply_unveil(got_repo_get_path(repo), 0,
10601 worktree ? got_worktree_get_root_path(worktree) : NULL);
10604 error = process_backup_refs(
10605 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10606 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10607 goto done; /* nothing else to do */
10610 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10615 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10618 if (rebase_in_progress) {
10619 error = got_error(GOT_ERR_REBASING);
10623 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10627 if (merge_in_progress) {
10628 error = got_error(GOT_ERR_MERGE_BUSY);
10632 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10636 if (edit_in_progress && edit_logmsg_only) {
10637 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10638 "histedit operation is in progress in this "
10639 "work tree and must be continued or aborted "
10640 "before the -m option can be used");
10643 if (edit_in_progress && fold_only) {
10644 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10645 "histedit operation is in progress in this "
10646 "work tree and must be continued or aborted "
10647 "before the -f option can be used");
10650 if (edit_in_progress && edit_only) {
10651 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10652 "histedit operation is in progress in this "
10653 "work tree and must be continued or aborted "
10654 "before the -e option can be used");
10658 if (edit_in_progress && abort_edit) {
10659 error = got_worktree_histedit_continue(&resume_commit_id,
10660 &tmp_branch, &branch, &base_commit_id, &fileindex,
10664 printf("Switching work tree to %s\n",
10665 got_ref_get_symref_target(branch));
10666 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10667 branch, base_commit_id, abort_progress, &upa);
10670 printf("Histedit of %s aborted\n",
10671 got_ref_get_symref_target(branch));
10672 print_merge_progress_stats(&upa);
10673 goto done; /* nothing else to do */
10674 } else if (abort_edit) {
10675 error = got_error(GOT_ERR_NOT_HISTEDIT);
10679 if (continue_edit) {
10682 if (!edit_in_progress) {
10683 error = got_error(GOT_ERR_NOT_HISTEDIT);
10687 error = got_worktree_get_histedit_script_path(&path, worktree);
10691 error = histedit_load_list(&histedit_cmds, path, repo);
10696 error = got_worktree_histedit_continue(&resume_commit_id,
10697 &tmp_branch, &branch, &base_commit_id, &fileindex,
10702 error = got_ref_resolve(&head_commit_id, repo, branch);
10706 error = got_object_open_as_commit(&commit, repo,
10710 parent_ids = got_object_commit_get_parent_ids(commit);
10711 pid = STAILQ_FIRST(parent_ids);
10713 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10716 error = collect_commits(&commits, head_commit_id, pid->id,
10717 base_commit_id, got_worktree_get_path_prefix(worktree),
10718 GOT_ERR_HISTEDIT_PATH, repo);
10719 got_object_commit_close(commit);
10724 if (edit_in_progress) {
10725 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10729 error = got_ref_open(&branch, repo,
10730 got_worktree_get_head_ref_name(worktree), 0);
10734 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10735 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10736 "will not edit commit history of a branch outside "
10737 "the \"refs/heads/\" reference namespace");
10741 error = got_ref_resolve(&head_commit_id, repo, branch);
10742 got_ref_close(branch);
10747 error = got_object_open_as_commit(&commit, repo,
10751 parent_ids = got_object_commit_get_parent_ids(commit);
10752 pid = STAILQ_FIRST(parent_ids);
10754 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10757 error = collect_commits(&commits, head_commit_id, pid->id,
10758 got_worktree_get_base_commit_id(worktree),
10759 got_worktree_get_path_prefix(worktree),
10760 GOT_ERR_HISTEDIT_PATH, repo);
10761 got_object_commit_close(commit);
10766 if (STAILQ_EMPTY(&commits)) {
10767 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10771 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10772 &base_commit_id, &fileindex, worktree, repo);
10776 if (edit_script_path) {
10777 error = histedit_load_list(&histedit_cmds,
10778 edit_script_path, repo);
10780 got_worktree_histedit_abort(worktree, fileindex,
10781 repo, branch, base_commit_id,
10782 abort_progress, &upa);
10783 print_merge_progress_stats(&upa);
10787 const char *branch_name;
10788 branch_name = got_ref_get_symref_target(branch);
10789 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10791 error = histedit_edit_script(&histedit_cmds, &commits,
10792 branch_name, edit_logmsg_only, fold_only,
10795 got_worktree_histedit_abort(worktree, fileindex,
10796 repo, branch, base_commit_id,
10797 abort_progress, &upa);
10798 print_merge_progress_stats(&upa);
10804 error = histedit_save_list(&histedit_cmds, worktree,
10807 got_worktree_histedit_abort(worktree, fileindex,
10808 repo, branch, base_commit_id,
10809 abort_progress, &upa);
10810 print_merge_progress_stats(&upa);
10816 error = histedit_check_script(&histedit_cmds, &commits, repo);
10820 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10821 if (resume_commit_id) {
10822 if (got_object_id_cmp(hle->commit_id,
10823 resume_commit_id) != 0)
10826 resume_commit_id = NULL;
10827 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10828 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10829 error = histedit_skip_commit(hle, worktree,
10834 struct got_pathlist_head paths;
10835 int have_changes = 0;
10837 TAILQ_INIT(&paths);
10838 error = got_pathlist_append(&paths, "", NULL);
10841 error = got_worktree_status(worktree, &paths,
10842 repo, 0, check_local_changes, &have_changes,
10843 check_cancelled, NULL);
10844 got_pathlist_free(&paths);
10846 if (error->code != GOT_ERR_CANCELLED)
10848 if (sigint_received || sigpipe_received)
10851 if (have_changes) {
10852 error = histedit_commit(NULL, worktree,
10853 fileindex, tmp_branch, hle, repo);
10857 error = got_object_open_as_commit(
10858 &commit, repo, hle->commit_id);
10861 error = show_histedit_progress(commit,
10863 got_object_commit_close(commit);
10872 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10873 error = histedit_skip_commit(hle, worktree, repo);
10879 error = got_object_open_as_commit(&commit, repo,
10883 parent_ids = got_object_commit_get_parent_ids(commit);
10884 pid = STAILQ_FIRST(parent_ids);
10886 error = got_worktree_histedit_merge_files(&merged_paths,
10887 worktree, fileindex, pid->id, hle->commit_id, repo,
10888 update_progress, &upa, check_cancelled, NULL);
10891 got_object_commit_close(commit);
10894 print_merge_progress_stats(&upa);
10895 if (upa.conflicts > 0 || upa.missing > 0 ||
10896 upa.not_deleted > 0 || upa.unversioned > 0) {
10897 if (upa.conflicts > 0) {
10898 error = show_rebase_merge_conflict(
10899 hle->commit_id, repo);
10903 got_worktree_rebase_pathlist_free(&merged_paths);
10907 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10909 error = got_object_id_str(&id_str, hle->commit_id);
10912 printf("Stopping histedit for amending commit %s\n",
10915 got_worktree_rebase_pathlist_free(&merged_paths);
10916 error = got_worktree_histedit_postpone(worktree,
10921 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10922 error = histedit_skip_commit(hle, worktree, repo);
10928 error = histedit_commit(&merged_paths, worktree, fileindex,
10929 tmp_branch, hle, repo);
10930 got_worktree_rebase_pathlist_free(&merged_paths);
10935 if (upa.conflicts > 0 || upa.missing > 0 ||
10936 upa.not_deleted > 0 || upa.unversioned > 0) {
10937 error = got_worktree_histedit_postpone(worktree, fileindex);
10940 if (upa.conflicts > 0 && upa.missing == 0 &&
10941 upa.not_deleted == 0 && upa.unversioned == 0) {
10942 error = got_error_msg(GOT_ERR_CONFLICTS,
10943 "conflicts must be resolved before histedit "
10945 } else if (upa.conflicts > 0) {
10946 error = got_error_msg(GOT_ERR_CONFLICTS,
10947 "conflicts must be resolved before histedit "
10948 "can continue; changes destined for some "
10949 "files were not yet merged and should be "
10950 "merged manually if required before the "
10951 "histedit operation is continued");
10953 error = got_error_msg(GOT_ERR_CONFLICTS,
10954 "changes destined for some files were not "
10955 "yet merged and should be merged manually "
10956 "if required before the histedit operation "
10960 error = histedit_complete(worktree, fileindex, tmp_branch,
10963 got_object_id_queue_free(&commits);
10964 histedit_free_list(&histedit_cmds);
10965 free(head_commit_id);
10966 free(base_commit_id);
10967 free(resume_commit_id);
10969 got_object_commit_close(commit);
10971 got_ref_close(branch);
10973 got_ref_close(tmp_branch);
10975 got_worktree_close(worktree);
10977 const struct got_error *close_err = got_repo_close(repo);
10985 usage_integrate(void)
10987 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10991 static const struct got_error *
10992 cmd_integrate(int argc, char *argv[])
10994 const struct got_error *error = NULL;
10995 struct got_repository *repo = NULL;
10996 struct got_worktree *worktree = NULL;
10997 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10998 const char *branch_arg = NULL;
10999 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11000 struct got_fileindex *fileindex = NULL;
11001 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11003 struct got_update_progress_arg upa;
11005 while ((ch = getopt(argc, argv, "")) != -1) {
11018 branch_arg = argv[0];
11020 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11021 "unveil", NULL) == -1)
11024 cwd = getcwd(NULL, 0);
11026 error = got_error_from_errno("getcwd");
11030 error = got_worktree_open(&worktree, cwd);
11032 if (error->code == GOT_ERR_NOT_WORKTREE)
11033 error = wrap_not_worktree_error(error, "integrate",
11038 error = check_rebase_or_histedit_in_progress(worktree);
11042 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11047 error = apply_unveil(got_repo_get_path(repo), 0,
11048 got_worktree_get_root_path(worktree));
11052 error = check_merge_in_progress(worktree, repo);
11056 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11057 error = got_error_from_errno("asprintf");
11061 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11062 &base_branch_ref, worktree, refname, repo);
11066 refname = strdup(got_ref_get_name(branch_ref));
11067 if (refname == NULL) {
11068 error = got_error_from_errno("strdup");
11069 got_worktree_integrate_abort(worktree, fileindex, repo,
11070 branch_ref, base_branch_ref);
11073 base_refname = strdup(got_ref_get_name(base_branch_ref));
11074 if (base_refname == NULL) {
11075 error = got_error_from_errno("strdup");
11076 got_worktree_integrate_abort(worktree, fileindex, repo,
11077 branch_ref, base_branch_ref);
11081 error = got_ref_resolve(&commit_id, repo, branch_ref);
11085 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11089 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11090 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11091 "specified branch has already been integrated");
11092 got_worktree_integrate_abort(worktree, fileindex, repo,
11093 branch_ref, base_branch_ref);
11097 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11099 if (error->code == GOT_ERR_ANCESTRY)
11100 error = got_error(GOT_ERR_REBASE_REQUIRED);
11101 got_worktree_integrate_abort(worktree, fileindex, repo,
11102 branch_ref, base_branch_ref);
11106 memset(&upa, 0, sizeof(upa));
11107 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11108 branch_ref, base_branch_ref, update_progress, &upa,
11109 check_cancelled, NULL);
11113 printf("Integrated %s into %s\n", refname, base_refname);
11114 print_update_progress_stats(&upa);
11117 const struct got_error *close_err = got_repo_close(repo);
11122 got_worktree_close(worktree);
11124 free(base_commit_id);
11127 free(base_refname);
11134 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11139 static const struct got_error *
11140 cmd_merge(int argc, char *argv[])
11142 const struct got_error *error = NULL;
11143 struct got_worktree *worktree = NULL;
11144 struct got_repository *repo = NULL;
11145 struct got_fileindex *fileindex = NULL;
11146 char *cwd = NULL, *id_str = NULL, *author = NULL;
11147 struct got_reference *branch = NULL, *wt_branch = NULL;
11148 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11149 struct got_object_id *wt_branch_tip = NULL;
11150 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11151 int interrupt_merge = 0;
11152 struct got_update_progress_arg upa;
11153 struct got_object_id *merge_commit_id = NULL;
11154 char *branch_name = NULL;
11156 memset(&upa, 0, sizeof(upa));
11158 while ((ch = getopt(argc, argv, "acn")) != -1) {
11164 continue_merge = 1;
11167 interrupt_merge = 1;
11179 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11180 "unveil", NULL) == -1)
11184 if (abort_merge && continue_merge)
11185 option_conflict('a', 'c');
11186 if (abort_merge || continue_merge) {
11189 } else if (argc != 1)
11192 cwd = getcwd(NULL, 0);
11194 error = got_error_from_errno("getcwd");
11198 error = got_worktree_open(&worktree, cwd);
11200 if (error->code == GOT_ERR_NOT_WORKTREE)
11201 error = wrap_not_worktree_error(error,
11206 error = got_repo_open(&repo,
11207 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
11211 error = apply_unveil(got_repo_get_path(repo), 0,
11212 worktree ? got_worktree_get_root_path(worktree) : NULL);
11216 error = check_rebase_or_histedit_in_progress(worktree);
11220 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11226 if (!merge_in_progress) {
11227 error = got_error(GOT_ERR_NOT_MERGING);
11230 error = got_worktree_merge_continue(&branch_name,
11231 &branch_tip, &fileindex, worktree, repo);
11234 error = got_worktree_merge_abort(worktree, fileindex, repo,
11235 abort_progress, &upa);
11238 printf("Merge of %s aborted\n", branch_name);
11239 goto done; /* nothing else to do */
11242 error = get_author(&author, repo, worktree);
11246 if (continue_merge) {
11247 if (!merge_in_progress) {
11248 error = got_error(GOT_ERR_NOT_MERGING);
11251 error = got_worktree_merge_continue(&branch_name,
11252 &branch_tip, &fileindex, worktree, repo);
11256 error = got_ref_open(&branch, repo, argv[0], 0);
11259 branch_name = strdup(got_ref_get_name(branch));
11260 if (branch_name == NULL) {
11261 error = got_error_from_errno("strdup");
11264 error = got_ref_resolve(&branch_tip, repo, branch);
11269 error = got_ref_open(&wt_branch, repo,
11270 got_worktree_get_head_ref_name(worktree), 0);
11273 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11276 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11277 wt_branch_tip, branch_tip, 0, repo,
11278 check_cancelled, NULL);
11279 if (error && error->code != GOT_ERR_ANCESTRY)
11282 if (!continue_merge) {
11283 error = check_path_prefix(wt_branch_tip, branch_tip,
11284 got_worktree_get_path_prefix(worktree),
11285 GOT_ERR_MERGE_PATH, repo);
11289 error = check_same_branch(wt_branch_tip, branch,
11292 if (error->code != GOT_ERR_ANCESTRY)
11296 static char msg[512];
11297 snprintf(msg, sizeof(msg),
11298 "cannot create a merge commit because "
11299 "%s is based on %s; %s can be integrated "
11300 "with 'got integrate' instead", branch_name,
11301 got_worktree_get_head_ref_name(worktree),
11303 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11307 error = got_worktree_merge_prepare(&fileindex, worktree,
11312 error = got_worktree_merge_branch(worktree, fileindex,
11313 yca_id, branch_tip, repo, update_progress, &upa,
11314 check_cancelled, NULL);
11317 print_merge_progress_stats(&upa);
11318 if (!upa.did_something) {
11319 error = got_worktree_merge_abort(worktree, fileindex,
11320 repo, abort_progress, &upa);
11323 printf("Already up-to-date\n");
11328 if (interrupt_merge) {
11329 error = got_worktree_merge_postpone(worktree, fileindex);
11332 printf("Merge of %s interrupted on request\n", branch_name);
11333 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11334 upa.not_deleted > 0 || upa.unversioned > 0) {
11335 error = got_worktree_merge_postpone(worktree, fileindex);
11338 if (upa.conflicts > 0 && upa.missing == 0 &&
11339 upa.not_deleted == 0 && upa.unversioned == 0) {
11340 error = got_error_msg(GOT_ERR_CONFLICTS,
11341 "conflicts must be resolved before merging "
11343 } else if (upa.conflicts > 0) {
11344 error = got_error_msg(GOT_ERR_CONFLICTS,
11345 "conflicts must be resolved before merging "
11346 "can continue; changes destined for some "
11347 "files were not yet merged and "
11348 "should be merged manually if required before the "
11349 "merge operation is continued");
11351 error = got_error_msg(GOT_ERR_CONFLICTS,
11352 "changes destined for some "
11353 "files were not yet merged and should be "
11354 "merged manually if required before the "
11355 "merge operation is continued");
11359 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11360 fileindex, author, NULL, 1, branch_tip, branch_name,
11361 repo, continue_merge ? print_status : NULL, NULL);
11364 error = got_worktree_merge_complete(worktree, fileindex, repo);
11367 error = got_object_id_str(&id_str, merge_commit_id);
11370 printf("Merged %s into %s: %s\n", branch_name,
11371 got_worktree_get_head_ref_name(worktree),
11377 free(merge_commit_id);
11383 got_ref_close(branch);
11385 got_ref_close(wt_branch);
11387 got_worktree_close(worktree);
11389 const struct got_error *close_err = got_repo_close(repo);
11399 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11400 "[-S] [file-path ...]\n",
11405 static const struct got_error *
11406 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11407 const char *path, struct got_object_id *blob_id,
11408 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11409 int dirfd, const char *de_name)
11411 const struct got_error *err = NULL;
11412 char *id_str = NULL;
11414 if (staged_status != GOT_STATUS_ADD &&
11415 staged_status != GOT_STATUS_MODIFY &&
11416 staged_status != GOT_STATUS_DELETE)
11419 if (staged_status == GOT_STATUS_ADD ||
11420 staged_status == GOT_STATUS_MODIFY)
11421 err = got_object_id_str(&id_str, staged_blob_id);
11423 err = got_object_id_str(&id_str, blob_id);
11427 printf("%s %c %s\n", id_str, staged_status, path);
11432 static const struct got_error *
11433 cmd_stage(int argc, char *argv[])
11435 const struct got_error *error = NULL;
11436 struct got_repository *repo = NULL;
11437 struct got_worktree *worktree = NULL;
11439 struct got_pathlist_head paths;
11440 struct got_pathlist_entry *pe;
11441 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11442 FILE *patch_script_file = NULL;
11443 const char *patch_script_path = NULL;
11444 struct choose_patch_arg cpa;
11446 TAILQ_INIT(&paths);
11448 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11457 patch_script_path = optarg;
11460 allow_bad_symlinks = 1;
11472 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11473 "unveil", NULL) == -1)
11476 if (list_stage && (pflag || patch_script_path))
11477 errx(1, "-l option cannot be used with other options");
11478 if (patch_script_path && !pflag)
11479 errx(1, "-F option can only be used together with -p option");
11481 cwd = getcwd(NULL, 0);
11483 error = got_error_from_errno("getcwd");
11487 error = got_worktree_open(&worktree, cwd);
11489 if (error->code == GOT_ERR_NOT_WORKTREE)
11490 error = wrap_not_worktree_error(error, "stage", cwd);
11494 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11499 if (patch_script_path) {
11500 patch_script_file = fopen(patch_script_path, "re");
11501 if (patch_script_file == NULL) {
11502 error = got_error_from_errno2("fopen",
11503 patch_script_path);
11507 error = apply_unveil(got_repo_get_path(repo), 0,
11508 got_worktree_get_root_path(worktree));
11512 error = check_merge_in_progress(worktree, repo);
11516 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11521 error = got_worktree_status(worktree, &paths, repo, 0,
11522 print_stage, NULL, check_cancelled, NULL);
11524 cpa.patch_script_file = patch_script_file;
11525 cpa.action = "stage";
11526 error = got_worktree_stage(worktree, &paths,
11527 pflag ? NULL : print_status, NULL,
11528 pflag ? choose_patch : NULL, &cpa,
11529 allow_bad_symlinks, repo);
11532 if (patch_script_file && fclose(patch_script_file) == EOF &&
11534 error = got_error_from_errno2("fclose", patch_script_path);
11536 const struct got_error *close_err = got_repo_close(repo);
11541 got_worktree_close(worktree);
11542 TAILQ_FOREACH(pe, &paths, entry)
11543 free((char *)pe->path);
11544 got_pathlist_free(&paths);
11550 usage_unstage(void)
11552 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11553 "[file-path ...]\n",
11559 static const struct got_error *
11560 cmd_unstage(int argc, char *argv[])
11562 const struct got_error *error = NULL;
11563 struct got_repository *repo = NULL;
11564 struct got_worktree *worktree = NULL;
11566 struct got_pathlist_head paths;
11567 struct got_pathlist_entry *pe;
11569 struct got_update_progress_arg upa;
11570 FILE *patch_script_file = NULL;
11571 const char *patch_script_path = NULL;
11572 struct choose_patch_arg cpa;
11574 TAILQ_INIT(&paths);
11576 while ((ch = getopt(argc, argv, "pF:")) != -1) {
11582 patch_script_path = optarg;
11594 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11595 "unveil", NULL) == -1)
11598 if (patch_script_path && !pflag)
11599 errx(1, "-F option can only be used together with -p option");
11601 cwd = getcwd(NULL, 0);
11603 error = got_error_from_errno("getcwd");
11607 error = got_worktree_open(&worktree, cwd);
11609 if (error->code == GOT_ERR_NOT_WORKTREE)
11610 error = wrap_not_worktree_error(error, "unstage", cwd);
11614 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11619 if (patch_script_path) {
11620 patch_script_file = fopen(patch_script_path, "re");
11621 if (patch_script_file == NULL) {
11622 error = got_error_from_errno2("fopen",
11623 patch_script_path);
11628 error = apply_unveil(got_repo_get_path(repo), 0,
11629 got_worktree_get_root_path(worktree));
11633 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11637 cpa.patch_script_file = patch_script_file;
11638 cpa.action = "unstage";
11639 memset(&upa, 0, sizeof(upa));
11640 error = got_worktree_unstage(worktree, &paths, update_progress,
11641 &upa, pflag ? choose_patch : NULL, &cpa, repo);
11643 print_merge_progress_stats(&upa);
11645 if (patch_script_file && fclose(patch_script_file) == EOF &&
11647 error = got_error_from_errno2("fclose", patch_script_path);
11649 const struct got_error *close_err = got_repo_close(repo);
11654 got_worktree_close(worktree);
11655 TAILQ_FOREACH(pe, &paths, entry)
11656 free((char *)pe->path);
11657 got_pathlist_free(&paths);
11665 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11666 "arg1 [arg2 ...]\n", getprogname());
11670 static const struct got_error *
11671 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11673 const struct got_error *err;
11674 struct got_blob_object *blob;
11676 err = got_object_open_as_blob(&blob, repo, id, 8192);
11680 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11681 got_object_blob_close(blob);
11685 static const struct got_error *
11686 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11688 const struct got_error *err;
11689 struct got_tree_object *tree;
11692 err = got_object_open_as_tree(&tree, repo, id);
11696 nentries = got_object_tree_get_nentries(tree);
11697 for (i = 0; i < nentries; i++) {
11698 struct got_tree_entry *te;
11700 if (sigint_received || sigpipe_received)
11702 te = got_object_tree_get_entry(tree, i);
11703 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11706 fprintf(outfile, "%s %.7o %s\n", id_str,
11707 got_tree_entry_get_mode(te),
11708 got_tree_entry_get_name(te));
11712 got_object_tree_close(tree);
11717 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
11727 h = (long long)gmtoff / 3600;
11728 m = ((long long)gmtoff - h*3600) / 60;
11729 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
11732 static const struct got_error *
11733 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11735 const struct got_error *err;
11736 struct got_commit_object *commit;
11737 const struct got_object_id_queue *parent_ids;
11738 struct got_object_qid *pid;
11739 char *id_str = NULL;
11740 const char *logmsg = NULL;
11743 err = got_object_open_as_commit(&commit, repo, id);
11747 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11751 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11752 parent_ids = got_object_commit_get_parent_ids(commit);
11753 fprintf(outfile, "numparents %d\n",
11754 got_object_commit_get_nparents(commit));
11755 STAILQ_FOREACH(pid, parent_ids, entry) {
11757 err = got_object_id_str(&pid_str, pid->id);
11760 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11763 format_gmtoff(gmtoff, sizeof(gmtoff),
11764 got_object_commit_get_author_gmtoff(commit));
11765 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
11766 got_object_commit_get_author(commit),
11767 (long long)got_object_commit_get_author_time(commit),
11770 format_gmtoff(gmtoff, sizeof(gmtoff),
11771 got_object_commit_get_committer_gmtoff(commit));
11772 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
11773 got_object_commit_get_author(commit),
11774 (long long)got_object_commit_get_committer_time(commit),
11777 logmsg = got_object_commit_get_logmsg_raw(commit);
11778 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11779 fprintf(outfile, "%s", logmsg);
11782 got_object_commit_close(commit);
11786 static const struct got_error *
11787 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11789 const struct got_error *err;
11790 struct got_tag_object *tag;
11791 char *id_str = NULL;
11792 const char *tagmsg = NULL;
11795 err = got_object_open_as_tag(&tag, repo, id);
11799 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11803 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11805 switch (got_object_tag_get_object_type(tag)) {
11806 case GOT_OBJ_TYPE_BLOB:
11807 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11808 GOT_OBJ_LABEL_BLOB);
11810 case GOT_OBJ_TYPE_TREE:
11811 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11812 GOT_OBJ_LABEL_TREE);
11814 case GOT_OBJ_TYPE_COMMIT:
11815 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11816 GOT_OBJ_LABEL_COMMIT);
11818 case GOT_OBJ_TYPE_TAG:
11819 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11820 GOT_OBJ_LABEL_TAG);
11826 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11827 got_object_tag_get_name(tag));
11829 format_gmtoff(gmtoff, sizeof(gmtoff),
11830 got_object_tag_get_tagger_gmtoff(tag));
11831 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
11832 got_object_tag_get_tagger(tag),
11833 (long long)got_object_tag_get_tagger_time(tag),
11836 tagmsg = got_object_tag_get_message(tag);
11837 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11838 fprintf(outfile, "%s", tagmsg);
11841 got_object_tag_close(tag);
11845 static const struct got_error *
11846 cmd_cat(int argc, char *argv[])
11848 const struct got_error *error;
11849 struct got_repository *repo = NULL;
11850 struct got_worktree *worktree = NULL;
11851 char *cwd = NULL, *repo_path = NULL, *label = NULL;
11852 const char *commit_id_str = NULL;
11853 struct got_object_id *id = NULL, *commit_id = NULL;
11854 struct got_commit_object *commit = NULL;
11855 int ch, obj_type, i, force_path = 0;
11856 struct got_reflist_head refs;
11861 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11866 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11869 commit_id_str = optarg;
11872 repo_path = realpath(optarg, NULL);
11873 if (repo_path == NULL)
11874 return got_error_from_errno2("realpath",
11876 got_path_strip_trailing_slashes(repo_path);
11890 cwd = getcwd(NULL, 0);
11892 error = got_error_from_errno("getcwd");
11895 error = got_worktree_open(&worktree, cwd);
11896 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11899 if (repo_path == NULL) {
11900 repo_path = strdup(
11901 got_worktree_get_repo_path(worktree));
11902 if (repo_path == NULL) {
11903 error = got_error_from_errno("strdup");
11908 /* Release work tree lock. */
11909 got_worktree_close(worktree);
11913 if (repo_path == NULL) {
11914 repo_path = getcwd(NULL, 0);
11915 if (repo_path == NULL)
11916 return got_error_from_errno("getcwd");
11919 error = got_repo_open(&repo, repo_path, NULL);
11924 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11928 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11932 if (commit_id_str == NULL)
11933 commit_id_str = GOT_REF_HEAD;
11934 error = got_repo_match_object_id(&commit_id, NULL,
11935 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11939 error = got_object_open_as_commit(&commit, repo, commit_id);
11943 for (i = 0; i < argc; i++) {
11945 error = got_object_id_by_path(&id, repo, commit,
11950 error = got_repo_match_object_id(&id, &label, argv[i],
11951 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11954 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11955 error->code != GOT_ERR_NOT_REF)
11957 error = got_object_id_by_path(&id, repo,
11964 error = got_object_get_type(&obj_type, repo, id);
11968 switch (obj_type) {
11969 case GOT_OBJ_TYPE_BLOB:
11970 error = cat_blob(id, repo, stdout);
11972 case GOT_OBJ_TYPE_TREE:
11973 error = cat_tree(id, repo, stdout);
11975 case GOT_OBJ_TYPE_COMMIT:
11976 error = cat_commit(id, repo, stdout);
11978 case GOT_OBJ_TYPE_TAG:
11979 error = cat_tag(id, repo, stdout);
11982 error = got_error(GOT_ERR_OBJ_TYPE);
11997 got_object_commit_close(commit);
11999 got_worktree_close(worktree);
12001 const struct got_error *close_err = got_repo_close(repo);
12005 got_ref_list_free(&refs);
12012 fprintf(stderr, "usage: %s info [path ...]\n",
12017 static const struct got_error *
12018 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12019 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12020 struct got_object_id *commit_id)
12022 const struct got_error *err = NULL;
12023 char *id_str = NULL;
12025 struct tm mytm, *tm;
12026 struct got_pathlist_head *paths = arg;
12027 struct got_pathlist_entry *pe;
12030 * Clear error indication from any of the path arguments which
12031 * would cause this file index entry to be displayed.
12033 TAILQ_FOREACH(pe, paths, entry) {
12034 if (got_path_cmp(path, pe->path, strlen(path),
12035 pe->path_len) == 0 ||
12036 got_path_is_child(path, pe->path, pe->path_len))
12037 pe->data = NULL; /* no error */
12040 printf(GOT_COMMIT_SEP_STR);
12042 printf("symlink: %s\n", path);
12043 else if (S_ISREG(mode)) {
12044 printf("file: %s\n", path);
12045 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12046 } else if (S_ISDIR(mode))
12047 printf("directory: %s\n", path);
12049 printf("something: %s\n", path);
12051 tm = localtime_r(&mtime, &mytm);
12054 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12055 return got_error(GOT_ERR_NO_SPACE);
12056 printf("timestamp: %s\n", datebuf);
12059 err = got_object_id_str(&id_str, blob_id);
12062 printf("based on blob: %s\n", id_str);
12066 if (staged_blob_id) {
12067 err = got_object_id_str(&id_str, staged_blob_id);
12070 printf("based on staged blob: %s\n", id_str);
12075 err = got_object_id_str(&id_str, commit_id);
12078 printf("based on commit: %s\n", id_str);
12085 static const struct got_error *
12086 cmd_info(int argc, char *argv[])
12088 const struct got_error *error = NULL;
12089 struct got_worktree *worktree = NULL;
12090 char *cwd = NULL, *id_str = NULL;
12091 struct got_pathlist_head paths;
12092 struct got_pathlist_entry *pe;
12093 char *uuidstr = NULL;
12094 int ch, show_files = 0;
12096 TAILQ_INIT(&paths);
12098 while ((ch = getopt(argc, argv, "")) != -1) {
12110 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12114 cwd = getcwd(NULL, 0);
12116 error = got_error_from_errno("getcwd");
12120 error = got_worktree_open(&worktree, cwd);
12122 if (error->code == GOT_ERR_NOT_WORKTREE)
12123 error = wrap_not_worktree_error(error, "info", cwd);
12127 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12132 error = get_worktree_paths_from_argv(&paths, argc, argv,
12139 error = got_object_id_str(&id_str,
12140 got_worktree_get_base_commit_id(worktree));
12144 error = got_worktree_get_uuid(&uuidstr, worktree);
12148 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12149 printf("work tree base commit: %s\n", id_str);
12150 printf("work tree path prefix: %s\n",
12151 got_worktree_get_path_prefix(worktree));
12152 printf("work tree branch reference: %s\n",
12153 got_worktree_get_head_ref_name(worktree));
12154 printf("work tree UUID: %s\n", uuidstr);
12155 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12158 struct got_pathlist_entry *pe;
12159 TAILQ_FOREACH(pe, &paths, entry) {
12160 if (pe->path_len == 0)
12163 * Assume this path will fail. This will be corrected
12164 * in print_path_info() in case the path does suceeed.
12166 pe->data = (void *)got_error_path(pe->path,
12169 error = got_worktree_path_info(worktree, &paths,
12170 print_path_info, &paths, check_cancelled, NULL);
12173 TAILQ_FOREACH(pe, &paths, entry) {
12174 if (pe->data != NULL) {
12175 error = pe->data; /* bad path */
12181 TAILQ_FOREACH(pe, &paths, entry)
12182 free((char *)pe->path);
12183 got_pathlist_free(&paths);