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/types.h>
41 #include "got_compat.h"
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
57 #include "got_opentemp.h"
58 #include "got_gotconfig.h"
60 #include "got_patch.h"
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 static volatile sig_atomic_t sigint_received;
67 static volatile sig_atomic_t sigpipe_received;
70 catch_sigint(int signo)
76 catch_sigpipe(int signo)
84 const struct got_error *(*cmd_main)(int, char *[]);
85 void (*cmd_usage)(void);
86 const char *cmd_alias;
89 __dead static void usage(int, int);
90 __dead static void usage_init(void);
91 __dead static void usage_import(void);
92 __dead static void usage_clone(void);
93 __dead static void usage_fetch(void);
94 __dead static void usage_checkout(void);
95 __dead static void usage_update(void);
96 __dead static void usage_log(void);
97 __dead static void usage_diff(void);
98 __dead static void usage_blame(void);
99 __dead static void usage_tree(void);
100 __dead static void usage_status(void);
101 __dead static void usage_ref(void);
102 __dead static void usage_branch(void);
103 __dead static void usage_tag(void);
104 __dead static void usage_add(void);
105 __dead static void usage_remove(void);
106 __dead static void usage_patch(void);
107 __dead static void usage_revert(void);
108 __dead static void usage_commit(void);
109 __dead static void usage_send(void);
110 __dead static void usage_cherrypick(void);
111 __dead static void usage_backout(void);
112 __dead static void usage_rebase(void);
113 __dead static void usage_histedit(void);
114 __dead static void usage_integrate(void);
115 __dead static void usage_merge(void);
116 __dead static void usage_stage(void);
117 __dead static void usage_unstage(void);
118 __dead static void usage_cat(void);
119 __dead static void usage_info(void);
121 static const struct got_error* cmd_init(int, char *[]);
122 static const struct got_error* cmd_import(int, char *[]);
123 static const struct got_error* cmd_clone(int, char *[]);
124 static const struct got_error* cmd_fetch(int, char *[]);
125 static const struct got_error* cmd_checkout(int, char *[]);
126 static const struct got_error* cmd_update(int, char *[]);
127 static const struct got_error* cmd_log(int, char *[]);
128 static const struct got_error* cmd_diff(int, char *[]);
129 static const struct got_error* cmd_blame(int, char *[]);
130 static const struct got_error* cmd_tree(int, char *[]);
131 static const struct got_error* cmd_status(int, char *[]);
132 static const struct got_error* cmd_ref(int, char *[]);
133 static const struct got_error* cmd_branch(int, char *[]);
134 static const struct got_error* cmd_tag(int, char *[]);
135 static const struct got_error* cmd_add(int, char *[]);
136 static const struct got_error* cmd_remove(int, char *[]);
137 static const struct got_error* cmd_patch(int, char *[]);
138 static const struct got_error* cmd_revert(int, char *[]);
139 static const struct got_error* cmd_commit(int, char *[]);
140 static const struct got_error* cmd_send(int, char *[]);
141 static const struct got_error* cmd_cherrypick(int, char *[]);
142 static const struct got_error* cmd_backout(int, char *[]);
143 static const struct got_error* cmd_rebase(int, char *[]);
144 static const struct got_error* cmd_histedit(int, char *[]);
145 static const struct got_error* cmd_integrate(int, char *[]);
146 static const struct got_error* cmd_merge(int, char *[]);
147 static const struct got_error* cmd_stage(int, char *[]);
148 static const struct got_error* cmd_unstage(int, char *[]);
149 static const struct got_error* cmd_cat(int, char *[]);
150 static const struct got_error* cmd_info(int, char *[]);
152 static const struct got_cmd got_commands[] = {
153 { "init", cmd_init, usage_init, "" },
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
354 static const struct got_error *
355 cmd_init(int argc, char *argv[])
357 const struct got_error *error = NULL;
358 char *repo_path = NULL;
361 while ((ch = getopt(argc, argv, "")) != -1) {
373 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
379 repo_path = strdup(argv[0]);
380 if (repo_path == NULL)
381 return got_error_from_errno("strdup");
383 got_path_strip_trailing_slashes(repo_path);
385 error = got_path_mkdir(repo_path);
387 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
390 error = apply_unveil(repo_path, 0, NULL);
394 error = got_repo_init(repo_path);
403 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
404 "[-r repository-path] [-I pattern] path\n", getprogname());
409 spawn_editor(const char *editor, const char *file)
412 sig_t sighup, sigint, sigquit;
415 sighup = signal(SIGHUP, SIG_IGN);
416 sigint = signal(SIGINT, SIG_IGN);
417 sigquit = signal(SIGQUIT, SIG_IGN);
419 switch (pid = fork()) {
423 execl(editor, editor, file, (char *)NULL);
427 while (waitpid(pid, &st, 0) == -1)
432 (void)signal(SIGHUP, sighup);
433 (void)signal(SIGINT, sigint);
434 (void)signal(SIGQUIT, sigquit);
436 if (!WIFEXITED(st)) {
441 return WEXITSTATUS(st);
444 static const struct got_error *
445 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
446 const char *initial_content, size_t initial_content_len,
447 int require_modification)
449 const struct got_error *err = NULL;
455 size_t len, logmsg_len;
456 char *initial_content_stripped = NULL, *buf = NULL, *s;
460 if (stat(logmsg_path, &st) == -1)
461 return got_error_from_errno2("stat", logmsg_path);
463 if (spawn_editor(editor, logmsg_path) == -1)
464 return got_error_from_errno("failed spawning editor");
466 if (stat(logmsg_path, &st2) == -1)
467 return got_error_from_errno("stat");
469 if (require_modification &&
470 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
471 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "no changes made to commit message, aborting");
475 * Set up a stripped version of the initial content without comments
476 * and blank lines. We need this in order to check if the message
477 * has in fact been edited.
479 initial_content_stripped = malloc(initial_content_len + 1);
480 if (initial_content_stripped == NULL)
481 return got_error_from_errno("malloc");
482 initial_content_stripped[0] = '\0';
484 buf = strdup(initial_content);
486 err = got_error_from_errno("strdup");
491 while ((line = strsep(&s, "\n")) != NULL) {
492 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
493 continue; /* remove comments and leading empty lines */
494 len = strlcat(initial_content_stripped, line,
495 initial_content_len + 1);
496 if (len >= initial_content_len + 1) {
497 err = got_error(GOT_ERR_NO_SPACE);
501 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
502 initial_content_stripped[len - 1] = '\0';
506 logmsg_len = st2.st_size;
507 *logmsg = malloc(logmsg_len + 1);
509 return got_error_from_errno("malloc");
512 fp = fopen(logmsg_path, "re");
514 err = got_error_from_errno("fopen");
519 while ((linelen = getline(&line, &linesize, fp)) != -1) {
520 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
521 continue; /* remove comments and leading empty lines */
522 len = strlcat(*logmsg, line, logmsg_len + 1);
523 if (len >= logmsg_len + 1) {
524 err = got_error(GOT_ERR_NO_SPACE);
530 err = got_ferror(fp, GOT_ERR_IO);
533 while (len > 0 && (*logmsg)[len - 1] == '\n') {
534 (*logmsg)[len - 1] = '\0';
539 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
540 "commit message cannot be empty, aborting");
543 if (require_modification &&
544 strcmp(*logmsg, initial_content_stripped) == 0)
545 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
546 "no changes made to commit message, aborting");
548 free(initial_content_stripped);
550 if (fp && fclose(fp) == EOF && err == NULL)
551 err = got_error_from_errno("fclose");
559 static const struct got_error *
560 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
561 const char *path_dir, const char *branch_name)
563 char *initial_content = NULL;
564 const struct got_error *err = NULL;
565 int initial_content_len;
568 initial_content_len = asprintf(&initial_content,
569 "\n# %s to be imported to branch %s\n", path_dir,
571 if (initial_content_len == -1)
572 return got_error_from_errno("asprintf");
574 err = got_opentemp_named_fd(logmsg_path, &fd,
575 GOT_TMPDIR_STR "/got-importmsg");
579 if (write(fd, initial_content, initial_content_len) == -1) {
580 err = got_error_from_errno2("write", *logmsg_path);
584 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
585 initial_content_len, 1);
587 if (fd != -1 && close(fd) == -1 && err == NULL)
588 err = got_error_from_errno2("close", *logmsg_path);
589 free(initial_content);
597 static const struct got_error *
598 import_progress(void *arg, const char *path)
600 printf("A %s\n", path);
605 valid_author(const char *author)
608 * Really dumb email address check; we're only doing this to
609 * avoid git's object parser breaking on commits we create.
611 while (*author && *author != '<')
615 while (*author && *author != '@')
619 while (*author && *author != '>')
621 return *author == '>';
624 static const struct got_error *
625 get_author(char **author, struct got_repository *repo,
626 struct got_worktree *worktree)
628 const struct got_error *err = NULL;
629 const char *got_author = NULL, *name, *email;
630 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
635 worktree_conf = got_worktree_get_gotconfig(worktree);
636 repo_conf = got_repo_get_gotconfig(repo);
639 * Priority of potential author information sources, from most
640 * significant to least significant:
641 * 1) work tree's .got/got.conf file
642 * 2) repository's got.conf file
643 * 3) repository's git config file
644 * 4) environment variables
645 * 5) global git config files (in user's home directory or /etc)
649 got_author = got_gotconfig_get_author(worktree_conf);
650 if (got_author == NULL)
651 got_author = got_gotconfig_get_author(repo_conf);
652 if (got_author == NULL) {
653 name = got_repo_get_gitconfig_author_name(repo);
654 email = got_repo_get_gitconfig_author_email(repo);
656 if (asprintf(author, "%s <%s>", name, email) == -1)
657 return got_error_from_errno("asprintf");
661 got_author = getenv("GOT_AUTHOR");
662 if (got_author == NULL) {
663 name = got_repo_get_global_gitconfig_author_name(repo);
664 email = got_repo_get_global_gitconfig_author_email(
667 if (asprintf(author, "%s <%s>", name, email)
669 return got_error_from_errno("asprintf");
672 /* TODO: Look up user in password database? */
673 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
677 *author = strdup(got_author);
679 return got_error_from_errno("strdup");
681 if (!valid_author(*author)) {
682 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
689 static const struct got_error *
690 get_gitconfig_path(char **gitconfig_path)
692 const char *homedir = getenv("HOME");
694 *gitconfig_path = NULL;
696 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
697 return got_error_from_errno("asprintf");
703 static const struct got_error *
704 cmd_import(int argc, char *argv[])
706 const struct got_error *error = NULL;
707 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
708 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
709 const char *branch_name = "main";
710 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
711 struct got_repository *repo = NULL;
712 struct got_reference *branch_ref = NULL, *head_ref = NULL;
713 struct got_object_id *new_commit_id = NULL;
715 struct got_pathlist_head ignores;
716 struct got_pathlist_entry *pe;
717 int preserve_logmsg = 0;
719 TAILQ_INIT(&ignores);
721 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
724 branch_name = optarg;
727 logmsg = strdup(optarg);
728 if (logmsg == NULL) {
729 error = got_error_from_errno("strdup");
734 repo_path = realpath(optarg, NULL);
735 if (repo_path == NULL) {
736 error = got_error_from_errno2("realpath",
742 if (optarg[0] == '\0')
744 error = got_pathlist_insert(&pe, &ignores, optarg,
759 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
767 if (repo_path == NULL) {
768 repo_path = getcwd(NULL, 0);
769 if (repo_path == NULL)
770 return got_error_from_errno("getcwd");
772 got_path_strip_trailing_slashes(repo_path);
773 error = get_gitconfig_path(&gitconfig_path);
776 error = got_repo_open(&repo, repo_path, gitconfig_path);
780 error = get_author(&author, repo, NULL);
785 * Don't let the user create a branch name with a leading '-'.
786 * While technically a valid reference name, this case is usually
787 * an unintended typo.
789 if (branch_name[0] == '-')
790 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
792 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
793 error = got_error_from_errno("asprintf");
797 error = got_ref_open(&branch_ref, repo, refname, 0);
799 if (error->code != GOT_ERR_NOT_REF)
802 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
803 "import target branch already exists");
807 path_dir = realpath(argv[0], NULL);
808 if (path_dir == NULL) {
809 error = got_error_from_errno2("realpath", argv[0]);
812 got_path_strip_trailing_slashes(path_dir);
815 * unveil(2) traverses exec(2); if an editor is used we have
816 * to apply unveil after the log message has been written.
818 if (logmsg == NULL || strlen(logmsg) == 0) {
819 error = get_editor(&editor);
823 error = collect_import_msg(&logmsg, &logmsg_path, editor,
826 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
833 if (unveil(path_dir, "r") != 0) {
834 error = got_error_from_errno2("unveil", path_dir);
840 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
847 error = got_repo_import(&new_commit_id, path_dir, logmsg,
848 author, &ignores, repo, import_progress, NULL);
855 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
862 error = got_ref_write(branch_ref, repo);
869 error = got_object_id_str(&id_str, new_commit_id);
876 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
878 if (error->code != GOT_ERR_NOT_REF) {
884 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
892 error = got_ref_write(head_ref, repo);
900 printf("Created branch %s with commit %s\n",
901 got_ref_get_name(branch_ref), id_str);
903 if (preserve_logmsg) {
904 fprintf(stderr, "%s: log message preserved in %s\n",
905 getprogname(), logmsg_path);
906 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
907 error = got_error_from_errno2("unlink", logmsg_path);
916 free(gitconfig_path);
918 got_ref_close(branch_ref);
920 got_ref_close(head_ref);
927 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
928 "[-R reference] repository-url [directory]\n", getprogname());
932 struct got_fetch_progress_arg {
933 char last_scaled_size[FMT_SCALED_STRSIZE];
938 struct got_repository *repo;
943 struct got_pathlist_head *symrefs;
944 struct got_pathlist_head *wanted_branches;
945 struct got_pathlist_head *wanted_refs;
949 const char *remote_repo_path;
951 int fetch_all_branches;
952 int mirror_references;
956 /* XXX forward declaration */
957 static const struct got_error *
958 create_config_files(const char *proto, const char *host, const char *port,
959 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
960 int mirror_references, struct got_pathlist_head *symrefs,
961 struct got_pathlist_head *wanted_branches,
962 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
964 static const struct got_error *
965 fetch_progress(void *arg, const char *message, off_t packfile_size,
966 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
968 const struct got_error *err = NULL;
969 struct got_fetch_progress_arg *a = arg;
970 char scaled_size[FMT_SCALED_STRSIZE];
971 int p_indexed, p_resolved;
972 int print_size = 0, print_indexed = 0, print_resolved = 0;
975 * In order to allow a failed clone to be resumed with 'got fetch'
976 * we try to create configuration files as soon as possible.
977 * Once the server has sent information about its default branch
978 * we have all required information.
980 if (a->create_configs && !a->configs_created &&
981 !TAILQ_EMPTY(a->config_info.symrefs)) {
982 err = create_config_files(a->config_info.proto,
983 a->config_info.host, a->config_info.port,
984 a->config_info.remote_repo_path,
985 a->config_info.git_url,
986 a->config_info.fetch_all_branches,
987 a->config_info.mirror_references,
988 a->config_info.symrefs,
989 a->config_info.wanted_branches,
990 a->config_info.wanted_refs, a->repo);
993 a->configs_created = 1;
996 if (a->verbosity < 0)
999 if (message && message[0] != '\0') {
1000 printf("\rserver: %s", message);
1005 if (packfile_size > 0 || nobj_indexed > 0) {
1006 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1007 (a->last_scaled_size[0] == '\0' ||
1008 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1010 if (strlcpy(a->last_scaled_size, scaled_size,
1011 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1012 return got_error(GOT_ERR_NO_SPACE);
1014 if (nobj_indexed > 0) {
1015 p_indexed = (nobj_indexed * 100) / nobj_total;
1016 if (p_indexed != a->last_p_indexed) {
1017 a->last_p_indexed = p_indexed;
1022 if (nobj_resolved > 0) {
1023 p_resolved = (nobj_resolved * 100) /
1024 (nobj_total - nobj_loose);
1025 if (p_resolved != a->last_p_resolved) {
1026 a->last_p_resolved = p_resolved;
1034 if (print_size || print_indexed || print_resolved)
1037 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1039 printf("; indexing %d%%", p_indexed);
1041 printf("; resolving deltas %d%%", p_resolved);
1042 if (print_size || print_indexed || print_resolved)
1048 static const struct got_error *
1049 create_symref(const char *refname, struct got_reference *target_ref,
1050 int verbosity, struct got_repository *repo)
1052 const struct got_error *err;
1053 struct got_reference *head_symref;
1055 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1059 err = got_ref_write(head_symref, repo);
1060 if (err == NULL && verbosity > 0) {
1061 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1062 got_ref_get_name(target_ref));
1064 got_ref_close(head_symref);
1068 static const struct got_error *
1069 list_remote_refs(struct got_pathlist_head *symrefs,
1070 struct got_pathlist_head *refs)
1072 const struct got_error *err;
1073 struct got_pathlist_entry *pe;
1075 TAILQ_FOREACH(pe, symrefs, entry) {
1076 const char *refname = pe->path;
1077 const char *targetref = pe->data;
1079 printf("%s: %s\n", refname, targetref);
1082 TAILQ_FOREACH(pe, refs, entry) {
1083 const char *refname = pe->path;
1084 struct got_object_id *id = pe->data;
1087 err = got_object_id_str(&id_str, id);
1090 printf("%s: %s\n", refname, id_str);
1097 static const struct got_error *
1098 create_ref(const char *refname, struct got_object_id *id,
1099 int verbosity, struct got_repository *repo)
1101 const struct got_error *err = NULL;
1102 struct got_reference *ref;
1105 err = got_object_id_str(&id_str, id);
1109 err = got_ref_alloc(&ref, refname, id);
1113 err = got_ref_write(ref, repo);
1116 if (err == NULL && verbosity >= 0)
1117 printf("Created reference %s: %s\n", refname, id_str);
1124 match_wanted_ref(const char *refname, const char *wanted_ref)
1126 if (strncmp(refname, "refs/", 5) != 0)
1131 * Prevent fetching of references that won't make any
1132 * sense outside of the remote repository's context.
1134 if (strncmp(refname, "got/", 4) == 0)
1136 if (strncmp(refname, "remotes/", 8) == 0)
1139 if (strncmp(wanted_ref, "refs/", 5) == 0)
1142 /* Allow prefix match. */
1143 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1146 /* Allow exact match. */
1147 return (strcmp(refname, wanted_ref) == 0);
1151 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1153 struct got_pathlist_entry *pe;
1155 TAILQ_FOREACH(pe, wanted_refs, entry) {
1156 if (match_wanted_ref(refname, pe->path))
1163 static const struct got_error *
1164 create_wanted_ref(const char *refname, struct got_object_id *id,
1165 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1167 const struct got_error *err;
1168 char *remote_refname;
1170 if (strncmp("refs/", refname, 5) == 0)
1173 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1174 remote_repo_name, refname) == -1)
1175 return got_error_from_errno("asprintf");
1177 err = create_ref(remote_refname, id, verbosity, repo);
1178 free(remote_refname);
1182 static const struct got_error *
1183 create_gotconfig(const char *proto, const char *host, const char *port,
1184 const char *remote_repo_path, const char *default_branch,
1185 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1186 struct got_pathlist_head *wanted_refs, int mirror_references,
1187 struct got_repository *repo)
1189 const struct got_error *err = NULL;
1190 char *gotconfig_path = NULL;
1191 char *gotconfig = NULL;
1192 FILE *gotconfig_file = NULL;
1193 const char *branchname = NULL;
1194 char *branches = NULL, *refs = NULL;
1197 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1198 struct got_pathlist_entry *pe;
1199 TAILQ_FOREACH(pe, wanted_branches, entry) {
1201 branchname = pe->path;
1202 if (strncmp(branchname, "refs/heads/", 11) == 0)
1204 if (asprintf(&s, "%s\"%s\" ",
1205 branches ? branches : "", branchname) == -1) {
1206 err = got_error_from_errno("asprintf");
1212 } else if (!fetch_all_branches && default_branch) {
1213 branchname = default_branch;
1214 if (strncmp(branchname, "refs/heads/", 11) == 0)
1216 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1217 err = got_error_from_errno("asprintf");
1221 if (!TAILQ_EMPTY(wanted_refs)) {
1222 struct got_pathlist_entry *pe;
1223 TAILQ_FOREACH(pe, wanted_refs, entry) {
1225 const char *refname = pe->path;
1226 if (strncmp(refname, "refs/", 5) == 0)
1228 if (asprintf(&s, "%s\"%s\" ",
1229 refs ? refs : "", refname) == -1) {
1230 err = got_error_from_errno("asprintf");
1238 /* Create got.conf(5). */
1239 gotconfig_path = got_repo_get_path_gotconfig(repo);
1240 if (gotconfig_path == NULL) {
1241 err = got_error_from_errno("got_repo_get_path_gotconfig");
1244 gotconfig_file = fopen(gotconfig_path, "ae");
1245 if (gotconfig_file == NULL) {
1246 err = got_error_from_errno2("fopen", gotconfig_path);
1249 if (asprintf(&gotconfig,
1254 "\trepository \"%s\"\n"
1260 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1261 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1262 remote_repo_path, branches ? "\tbranch { " : "",
1263 branches ? branches : "", branches ? "}\n" : "",
1264 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1265 mirror_references ? "\tmirror-references yes\n" : "",
1266 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1267 err = got_error_from_errno("asprintf");
1270 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1271 if (n != strlen(gotconfig)) {
1272 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1277 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1278 err = got_error_from_errno2("fclose", gotconfig_path);
1279 free(gotconfig_path);
1284 static const struct got_error *
1285 create_gitconfig(const char *git_url, const char *default_branch,
1286 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1287 struct got_pathlist_head *wanted_refs, int mirror_references,
1288 struct got_repository *repo)
1290 const struct got_error *err = NULL;
1291 char *gitconfig_path = NULL;
1292 char *gitconfig = NULL;
1293 FILE *gitconfig_file = NULL;
1294 char *branches = NULL, *refs = NULL;
1295 const char *branchname;
1298 /* Create a config file Git can understand. */
1299 gitconfig_path = got_repo_get_path_gitconfig(repo);
1300 if (gitconfig_path == NULL) {
1301 err = got_error_from_errno("got_repo_get_path_gitconfig");
1304 gitconfig_file = fopen(gitconfig_path, "ae");
1305 if (gitconfig_file == NULL) {
1306 err = got_error_from_errno2("fopen", gitconfig_path);
1309 if (fetch_all_branches) {
1310 if (mirror_references) {
1311 if (asprintf(&branches,
1312 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1313 err = got_error_from_errno("asprintf");
1316 } else if (asprintf(&branches,
1317 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1318 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1319 err = got_error_from_errno("asprintf");
1322 } else if (!TAILQ_EMPTY(wanted_branches)) {
1323 struct got_pathlist_entry *pe;
1324 TAILQ_FOREACH(pe, wanted_branches, entry) {
1326 branchname = pe->path;
1327 if (strncmp(branchname, "refs/heads/", 11) == 0)
1329 if (mirror_references) {
1331 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1332 branches ? branches : "",
1333 branchname, branchname) == -1) {
1334 err = got_error_from_errno("asprintf");
1337 } else if (asprintf(&s,
1338 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1339 branches ? branches : "",
1340 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1341 branchname) == -1) {
1342 err = got_error_from_errno("asprintf");
1350 * If the server specified a default branch, use just that one.
1351 * Otherwise fall back to fetching all branches on next fetch.
1353 if (default_branch) {
1354 branchname = default_branch;
1355 if (strncmp(branchname, "refs/heads/", 11) == 0)
1358 branchname = "*"; /* fall back to all branches */
1359 if (mirror_references) {
1360 if (asprintf(&branches,
1361 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1362 branchname, branchname) == -1) {
1363 err = got_error_from_errno("asprintf");
1366 } else if (asprintf(&branches,
1367 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1368 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1369 branchname) == -1) {
1370 err = got_error_from_errno("asprintf");
1374 if (!TAILQ_EMPTY(wanted_refs)) {
1375 struct got_pathlist_entry *pe;
1376 TAILQ_FOREACH(pe, wanted_refs, entry) {
1378 const char *refname = pe->path;
1379 if (strncmp(refname, "refs/", 5) == 0)
1381 if (mirror_references) {
1383 "%s\tfetch = refs/%s:refs/%s\n",
1384 refs ? refs : "", refname, refname) == -1) {
1385 err = got_error_from_errno("asprintf");
1388 } else if (asprintf(&s,
1389 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1391 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1393 err = got_error_from_errno("asprintf");
1401 if (asprintf(&gitconfig,
1406 "\tfetch = refs/tags/*:refs/tags/*\n",
1407 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1408 refs ? refs : "") == -1) {
1409 err = got_error_from_errno("asprintf");
1412 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1413 if (n != strlen(gitconfig)) {
1414 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1418 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1419 err = got_error_from_errno2("fclose", gitconfig_path);
1420 free(gitconfig_path);
1425 static const struct got_error *
1426 create_config_files(const char *proto, const char *host, const char *port,
1427 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1428 int mirror_references, struct got_pathlist_head *symrefs,
1429 struct got_pathlist_head *wanted_branches,
1430 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1432 const struct got_error *err = NULL;
1433 const char *default_branch = NULL;
1434 struct got_pathlist_entry *pe;
1437 * If we asked for a set of wanted branches then use the first
1440 if (!TAILQ_EMPTY(wanted_branches)) {
1441 pe = TAILQ_FIRST(wanted_branches);
1442 default_branch = pe->path;
1444 /* First HEAD ref listed by server is the default branch. */
1445 TAILQ_FOREACH(pe, symrefs, entry) {
1446 const char *refname = pe->path;
1447 const char *target = pe->data;
1449 if (strcmp(refname, GOT_REF_HEAD) != 0)
1452 default_branch = target;
1457 /* Create got.conf(5). */
1458 err = create_gotconfig(proto, host, port, remote_repo_path,
1459 default_branch, fetch_all_branches, wanted_branches,
1460 wanted_refs, mirror_references, repo);
1464 /* Create a config file Git can understand. */
1465 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1466 wanted_branches, wanted_refs, mirror_references, repo);
1469 static const struct got_error *
1470 cmd_clone(int argc, char *argv[])
1472 const struct got_error *error = NULL;
1473 const char *uri, *dirname;
1474 char *proto, *host, *port, *repo_name, *server_path;
1475 char *default_destdir = NULL, *id_str = NULL;
1476 const char *repo_path;
1477 struct got_repository *repo = NULL;
1478 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1479 struct got_pathlist_entry *pe;
1480 struct got_object_id *pack_hash = NULL;
1481 int ch, fetchfd = -1, fetchstatus;
1482 pid_t fetchpid = -1;
1483 struct got_fetch_progress_arg fpa;
1484 char *git_url = NULL;
1485 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1486 int list_refs_only = 0;
1489 TAILQ_INIT(&symrefs);
1490 TAILQ_INIT(&wanted_branches);
1491 TAILQ_INIT(&wanted_refs);
1493 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1496 fetch_all_branches = 1;
1499 error = got_pathlist_append(&wanted_branches,
1508 mirror_references = 1;
1513 else if (verbosity < 3)
1520 error = got_pathlist_append(&wanted_refs,
1533 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1534 option_conflict('a', 'b');
1535 if (list_refs_only) {
1536 if (!TAILQ_EMPTY(&wanted_branches))
1537 option_conflict('l', 'b');
1538 if (fetch_all_branches)
1539 option_conflict('l', 'a');
1540 if (mirror_references)
1541 option_conflict('l', 'm');
1542 if (!TAILQ_EMPTY(&wanted_refs))
1543 option_conflict('l', 'R');
1555 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1560 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1561 host, port ? ":" : "", port ? port : "",
1562 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1563 error = got_error_from_errno("asprintf");
1567 if (strcmp(proto, "git") == 0) {
1569 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1570 "sendfd dns inet unveil", NULL) == -1)
1573 } else if (strcmp(proto, "git+ssh") == 0 ||
1574 strcmp(proto, "ssh") == 0) {
1576 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1577 "sendfd unveil", NULL) == -1)
1580 } else if (strcmp(proto, "http") == 0 ||
1581 strcmp(proto, "git+http") == 0) {
1582 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1585 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1588 if (dirname == NULL) {
1589 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1590 error = got_error_from_errno("asprintf");
1593 repo_path = default_destdir;
1595 repo_path = dirname;
1597 if (!list_refs_only) {
1598 error = got_path_mkdir(repo_path);
1600 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1601 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1603 if (!got_path_dir_is_empty(repo_path)) {
1604 error = got_error_path(repo_path,
1605 GOT_ERR_DIR_NOT_EMPTY);
1610 error = got_dial_apply_unveil(proto);
1614 error = apply_unveil(repo_path, 0, NULL);
1619 printf("Connecting to %s%s%s\n", host,
1620 port ? ":" : "", port ? port : "");
1622 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1623 server_path, verbosity);
1627 if (!list_refs_only) {
1628 error = got_repo_init(repo_path);
1631 error = got_repo_open(&repo, repo_path, NULL);
1636 fpa.last_scaled_size[0] = '\0';
1637 fpa.last_p_indexed = -1;
1638 fpa.last_p_resolved = -1;
1639 fpa.verbosity = verbosity;
1640 fpa.create_configs = 1;
1641 fpa.configs_created = 0;
1643 fpa.config_info.symrefs = &symrefs;
1644 fpa.config_info.wanted_branches = &wanted_branches;
1645 fpa.config_info.wanted_refs = &wanted_refs;
1646 fpa.config_info.proto = proto;
1647 fpa.config_info.host = host;
1648 fpa.config_info.port = port;
1649 fpa.config_info.remote_repo_path = server_path;
1650 fpa.config_info.git_url = git_url;
1651 fpa.config_info.fetch_all_branches = fetch_all_branches;
1652 fpa.config_info.mirror_references = mirror_references;
1653 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1654 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1655 fetch_all_branches, &wanted_branches, &wanted_refs,
1656 list_refs_only, verbosity, fetchfd, repo,
1657 fetch_progress, &fpa);
1661 if (list_refs_only) {
1662 error = list_remote_refs(&symrefs, &refs);
1666 if (pack_hash == NULL) {
1667 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1668 "server sent an empty pack file");
1671 error = got_object_id_str(&id_str, pack_hash);
1675 printf("\nFetched %s.pack\n", id_str);
1678 /* Set up references provided with the pack file. */
1679 TAILQ_FOREACH(pe, &refs, entry) {
1680 const char *refname = pe->path;
1681 struct got_object_id *id = pe->data;
1682 char *remote_refname;
1684 if (is_wanted_ref(&wanted_refs, refname) &&
1685 !mirror_references) {
1686 error = create_wanted_ref(refname, id,
1687 GOT_FETCH_DEFAULT_REMOTE_NAME,
1688 verbosity - 1, repo);
1694 error = create_ref(refname, id, verbosity - 1, repo);
1698 if (mirror_references)
1701 if (strncmp("refs/heads/", refname, 11) != 0)
1704 if (asprintf(&remote_refname,
1705 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1706 refname + 11) == -1) {
1707 error = got_error_from_errno("asprintf");
1710 error = create_ref(remote_refname, id, verbosity - 1, repo);
1711 free(remote_refname);
1716 /* Set the HEAD reference if the server provided one. */
1717 TAILQ_FOREACH(pe, &symrefs, entry) {
1718 struct got_reference *target_ref;
1719 const char *refname = pe->path;
1720 const char *target = pe->data;
1721 char *remote_refname = NULL, *remote_target = NULL;
1723 if (strcmp(refname, GOT_REF_HEAD) != 0)
1726 error = got_ref_open(&target_ref, repo, target, 0);
1728 if (error->code == GOT_ERR_NOT_REF) {
1735 error = create_symref(refname, target_ref, verbosity, repo);
1736 got_ref_close(target_ref);
1740 if (mirror_references)
1743 if (strncmp("refs/heads/", target, 11) != 0)
1746 if (asprintf(&remote_refname,
1747 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1749 error = got_error_from_errno("asprintf");
1752 if (asprintf(&remote_target,
1753 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1754 target + 11) == -1) {
1755 error = got_error_from_errno("asprintf");
1756 free(remote_refname);
1759 error = got_ref_open(&target_ref, repo, remote_target, 0);
1761 free(remote_refname);
1762 free(remote_target);
1763 if (error->code == GOT_ERR_NOT_REF) {
1769 error = create_symref(remote_refname, target_ref,
1770 verbosity - 1, repo);
1771 free(remote_refname);
1772 free(remote_target);
1773 got_ref_close(target_ref);
1779 * We failed to set the HEAD reference. If we asked for
1780 * a set of wanted branches use the first of one of those
1781 * which could be fetched instead.
1783 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1784 const char *target = pe->path;
1785 struct got_reference *target_ref;
1787 error = got_ref_open(&target_ref, repo, target, 0);
1789 if (error->code == GOT_ERR_NOT_REF) {
1796 error = create_symref(GOT_REF_HEAD, target_ref,
1798 got_ref_close(target_ref);
1806 printf("Created %s repository '%s'\n",
1807 mirror_references ? "mirrored" : "cloned", repo_path);
1810 if (kill(fetchpid, SIGTERM) == -1)
1811 error = got_error_from_errno("kill");
1812 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1813 error = got_error_from_errno("waitpid");
1815 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1816 error = got_error_from_errno("close");
1818 const struct got_error *close_err = got_repo_close(repo);
1822 TAILQ_FOREACH(pe, &refs, entry) {
1823 free((void *)pe->path);
1826 got_pathlist_free(&refs);
1827 TAILQ_FOREACH(pe, &symrefs, entry) {
1828 free((void *)pe->path);
1831 got_pathlist_free(&symrefs);
1832 got_pathlist_free(&wanted_branches);
1833 got_pathlist_free(&wanted_refs);
1840 free(default_destdir);
1845 static const struct got_error *
1846 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1847 int replace_tags, int verbosity, struct got_repository *repo)
1849 const struct got_error *err = NULL;
1850 char *new_id_str = NULL;
1851 struct got_object_id *old_id = NULL;
1853 err = got_object_id_str(&new_id_str, new_id);
1857 if (!replace_tags &&
1858 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1859 err = got_ref_resolve(&old_id, repo, ref);
1862 if (got_object_id_cmp(old_id, new_id) == 0)
1864 if (verbosity >= 0) {
1865 printf("Rejecting update of existing tag %s: %s\n",
1866 got_ref_get_name(ref), new_id_str);
1871 if (got_ref_is_symbolic(ref)) {
1872 if (verbosity >= 0) {
1873 printf("Replacing reference %s: %s\n",
1874 got_ref_get_name(ref),
1875 got_ref_get_symref_target(ref));
1877 err = got_ref_change_symref_to_ref(ref, new_id);
1880 err = got_ref_write(ref, repo);
1884 err = got_ref_resolve(&old_id, repo, ref);
1887 if (got_object_id_cmp(old_id, new_id) == 0)
1890 err = got_ref_change_ref(ref, new_id);
1893 err = got_ref_write(ref, repo);
1899 printf("Updated %s: %s\n", got_ref_get_name(ref),
1907 static const struct got_error *
1908 update_symref(const char *refname, struct got_reference *target_ref,
1909 int verbosity, struct got_repository *repo)
1911 const struct got_error *err = NULL, *unlock_err;
1912 struct got_reference *symref;
1913 int symref_is_locked = 0;
1915 err = got_ref_open(&symref, repo, refname, 1);
1917 if (err->code != GOT_ERR_NOT_REF)
1919 err = got_ref_alloc_symref(&symref, refname, target_ref);
1923 err = got_ref_write(symref, repo);
1928 printf("Created reference %s: %s\n",
1929 got_ref_get_name(symref),
1930 got_ref_get_symref_target(symref));
1932 symref_is_locked = 1;
1934 if (strcmp(got_ref_get_symref_target(symref),
1935 got_ref_get_name(target_ref)) == 0)
1938 err = got_ref_change_symref(symref,
1939 got_ref_get_name(target_ref));
1943 err = got_ref_write(symref, repo);
1948 printf("Updated %s: %s\n", got_ref_get_name(symref),
1949 got_ref_get_symref_target(symref));
1953 if (symref_is_locked) {
1954 unlock_err = got_ref_unlock(symref);
1955 if (unlock_err && err == NULL)
1958 got_ref_close(symref);
1965 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1966 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1967 "[remote-repository-name]\n",
1972 static const struct got_error *
1973 delete_missing_ref(struct got_reference *ref,
1974 int verbosity, struct got_repository *repo)
1976 const struct got_error *err = NULL;
1977 struct got_object_id *id = NULL;
1978 char *id_str = NULL;
1980 if (got_ref_is_symbolic(ref)) {
1981 err = got_ref_delete(ref, repo);
1984 if (verbosity >= 0) {
1985 printf("Deleted %s: %s\n",
1986 got_ref_get_name(ref),
1987 got_ref_get_symref_target(ref));
1990 err = got_ref_resolve(&id, repo, ref);
1993 err = got_object_id_str(&id_str, id);
1997 err = got_ref_delete(ref, repo);
2000 if (verbosity >= 0) {
2001 printf("Deleted %s: %s\n",
2002 got_ref_get_name(ref), id_str);
2011 static const struct got_error *
2012 delete_missing_refs(struct got_pathlist_head *their_refs,
2013 struct got_pathlist_head *their_symrefs,
2014 const struct got_remote_repo *remote,
2015 int verbosity, struct got_repository *repo)
2017 const struct got_error *err = NULL, *unlock_err;
2018 struct got_reflist_head my_refs;
2019 struct got_reflist_entry *re;
2020 struct got_pathlist_entry *pe;
2021 char *remote_namespace = NULL;
2022 char *local_refname = NULL;
2024 TAILQ_INIT(&my_refs);
2026 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2028 return got_error_from_errno("asprintf");
2030 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2034 TAILQ_FOREACH(re, &my_refs, entry) {
2035 const char *refname = got_ref_get_name(re->ref);
2036 const char *their_refname;
2038 if (remote->mirror_references) {
2039 their_refname = refname;
2041 if (strncmp(refname, remote_namespace,
2042 strlen(remote_namespace)) == 0) {
2043 if (strcmp(refname + strlen(remote_namespace),
2046 if (asprintf(&local_refname, "refs/heads/%s",
2047 refname + strlen(remote_namespace)) == -1) {
2048 err = got_error_from_errno("asprintf");
2051 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2054 their_refname = local_refname;
2057 TAILQ_FOREACH(pe, their_refs, entry) {
2058 if (strcmp(their_refname, pe->path) == 0)
2064 TAILQ_FOREACH(pe, their_symrefs, entry) {
2065 if (strcmp(their_refname, pe->path) == 0)
2071 err = delete_missing_ref(re->ref, verbosity, repo);
2075 if (local_refname) {
2076 struct got_reference *ref;
2077 err = got_ref_open(&ref, repo, local_refname, 1);
2079 if (err->code != GOT_ERR_NOT_REF)
2081 free(local_refname);
2082 local_refname = NULL;
2085 err = delete_missing_ref(ref, verbosity, repo);
2088 unlock_err = got_ref_unlock(ref);
2090 if (unlock_err && err == NULL) {
2095 free(local_refname);
2096 local_refname = NULL;
2100 free(remote_namespace);
2101 free(local_refname);
2105 static const struct got_error *
2106 update_wanted_ref(const char *refname, struct got_object_id *id,
2107 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2109 const struct got_error *err, *unlock_err;
2110 char *remote_refname;
2111 struct got_reference *ref;
2113 if (strncmp("refs/", refname, 5) == 0)
2116 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2117 remote_repo_name, refname) == -1)
2118 return got_error_from_errno("asprintf");
2120 err = got_ref_open(&ref, repo, remote_refname, 1);
2122 if (err->code != GOT_ERR_NOT_REF)
2124 err = create_ref(remote_refname, id, verbosity, repo);
2126 err = update_ref(ref, id, 0, verbosity, repo);
2127 unlock_err = got_ref_unlock(ref);
2128 if (unlock_err && err == NULL)
2133 free(remote_refname);
2137 static const struct got_error *
2138 delete_ref(struct got_repository *repo, struct got_reference *ref)
2140 const struct got_error *err = NULL;
2141 struct got_object_id *id = NULL;
2142 char *id_str = NULL;
2145 if (got_ref_is_symbolic(ref)) {
2146 target = got_ref_get_symref_target(ref);
2148 err = got_ref_resolve(&id, repo, ref);
2151 err = got_object_id_str(&id_str, id);
2157 err = got_ref_delete(ref, repo);
2161 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2168 static const struct got_error *
2169 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2171 const struct got_error *err = NULL;
2172 struct got_reflist_head refs;
2173 struct got_reflist_entry *re;
2178 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2179 err = got_error_from_errno("asprintf");
2182 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2186 TAILQ_FOREACH(re, &refs, entry)
2187 delete_ref(repo, re->ref);
2189 got_ref_list_free(&refs);
2193 static const struct got_error *
2194 cmd_fetch(int argc, char *argv[])
2196 const struct got_error *error = NULL, *unlock_err;
2197 char *cwd = NULL, *repo_path = NULL;
2198 const char *remote_name;
2199 char *proto = NULL, *host = NULL, *port = NULL;
2200 char *repo_name = NULL, *server_path = NULL;
2201 const struct got_remote_repo *remotes, *remote = NULL;
2203 char *id_str = NULL;
2204 struct got_repository *repo = NULL;
2205 struct got_worktree *worktree = NULL;
2206 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2207 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2208 struct got_pathlist_entry *pe;
2209 struct got_object_id *pack_hash = NULL;
2210 int i, ch, fetchfd = -1, fetchstatus;
2211 pid_t fetchpid = -1;
2212 struct got_fetch_progress_arg fpa;
2213 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2214 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2217 TAILQ_INIT(&symrefs);
2218 TAILQ_INIT(&wanted_branches);
2219 TAILQ_INIT(&wanted_refs);
2221 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2224 fetch_all_branches = 1;
2227 error = got_pathlist_append(&wanted_branches,
2239 repo_path = realpath(optarg, NULL);
2240 if (repo_path == NULL)
2241 return got_error_from_errno2("realpath",
2243 got_path_strip_trailing_slashes(repo_path);
2251 else if (verbosity < 3)
2258 error = got_pathlist_append(&wanted_refs,
2274 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2275 option_conflict('a', 'b');
2276 if (list_refs_only) {
2277 if (!TAILQ_EMPTY(&wanted_branches))
2278 option_conflict('l', 'b');
2279 if (fetch_all_branches)
2280 option_conflict('l', 'a');
2282 option_conflict('l', 'd');
2284 option_conflict('l', 'X');
2286 if (delete_remote) {
2287 if (fetch_all_branches)
2288 option_conflict('X', 'a');
2289 if (!TAILQ_EMPTY(&wanted_branches))
2290 option_conflict('X', 'b');
2292 option_conflict('X', 'd');
2294 option_conflict('X', 't');
2295 if (!TAILQ_EMPTY(&wanted_refs))
2296 option_conflict('X', 'R');
2301 errx(1, "-X option requires a remote name");
2302 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2303 } else if (argc == 1)
2304 remote_name = argv[0];
2308 cwd = getcwd(NULL, 0);
2310 error = got_error_from_errno("getcwd");
2314 if (repo_path == NULL) {
2315 error = got_worktree_open(&worktree, cwd);
2316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2322 strdup(got_worktree_get_repo_path(worktree));
2323 if (repo_path == NULL)
2324 error = got_error_from_errno("strdup");
2328 repo_path = strdup(cwd);
2329 if (repo_path == NULL) {
2330 error = got_error_from_errno("strdup");
2336 error = got_repo_open(&repo, repo_path, NULL);
2340 if (delete_remote) {
2341 error = delete_refs_for_remote(repo, remote_name);
2342 goto done; /* nothing else to do */
2346 worktree_conf = got_worktree_get_gotconfig(worktree);
2347 if (worktree_conf) {
2348 got_gotconfig_get_remotes(&nremotes, &remotes,
2350 for (i = 0; i < nremotes; i++) {
2351 if (strcmp(remotes[i].name, remote_name) == 0) {
2352 remote = &remotes[i];
2358 if (remote == NULL) {
2359 repo_conf = got_repo_get_gotconfig(repo);
2361 got_gotconfig_get_remotes(&nremotes, &remotes,
2363 for (i = 0; i < nremotes; i++) {
2364 if (strcmp(remotes[i].name, remote_name) == 0) {
2365 remote = &remotes[i];
2371 if (remote == NULL) {
2372 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2373 for (i = 0; i < nremotes; i++) {
2374 if (strcmp(remotes[i].name, remote_name) == 0) {
2375 remote = &remotes[i];
2380 if (remote == NULL) {
2381 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2385 if (TAILQ_EMPTY(&wanted_branches)) {
2386 if (!fetch_all_branches)
2387 fetch_all_branches = remote->fetch_all_branches;
2388 for (i = 0; i < remote->nfetch_branches; i++) {
2389 got_pathlist_append(&wanted_branches,
2390 remote->fetch_branches[i], NULL);
2393 if (TAILQ_EMPTY(&wanted_refs)) {
2394 for (i = 0; i < remote->nfetch_refs; i++) {
2395 got_pathlist_append(&wanted_refs,
2396 remote->fetch_refs[i], NULL);
2400 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2401 &repo_name, remote->fetch_url);
2405 if (strcmp(proto, "git") == 0) {
2407 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2408 "sendfd dns inet unveil", NULL) == -1)
2411 } else if (strcmp(proto, "git+ssh") == 0 ||
2412 strcmp(proto, "ssh") == 0) {
2414 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2415 "sendfd unveil", NULL) == -1)
2418 } else if (strcmp(proto, "http") == 0 ||
2419 strcmp(proto, "git+http") == 0) {
2420 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2423 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2427 error = got_dial_apply_unveil(proto);
2431 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2436 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2437 port ? ":" : "", port ? port : "");
2439 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2440 server_path, verbosity);
2444 fpa.last_scaled_size[0] = '\0';
2445 fpa.last_p_indexed = -1;
2446 fpa.last_p_resolved = -1;
2447 fpa.verbosity = verbosity;
2449 fpa.create_configs = 0;
2450 fpa.configs_created = 0;
2451 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2452 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2453 remote->mirror_references, fetch_all_branches, &wanted_branches,
2454 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2455 fetch_progress, &fpa);
2459 if (list_refs_only) {
2460 error = list_remote_refs(&symrefs, &refs);
2464 if (pack_hash == NULL) {
2466 printf("Already up-to-date\n");
2467 } else if (verbosity >= 0) {
2468 error = got_object_id_str(&id_str, pack_hash);
2471 printf("\nFetched %s.pack\n", id_str);
2476 /* Update references provided with the pack file. */
2477 TAILQ_FOREACH(pe, &refs, entry) {
2478 const char *refname = pe->path;
2479 struct got_object_id *id = pe->data;
2480 struct got_reference *ref;
2481 char *remote_refname;
2483 if (is_wanted_ref(&wanted_refs, refname) &&
2484 !remote->mirror_references) {
2485 error = update_wanted_ref(refname, id,
2486 remote->name, verbosity, repo);
2492 if (remote->mirror_references ||
2493 strncmp("refs/tags/", refname, 10) == 0) {
2494 error = got_ref_open(&ref, repo, refname, 1);
2496 if (error->code != GOT_ERR_NOT_REF)
2498 error = create_ref(refname, id, verbosity,
2503 error = update_ref(ref, id, replace_tags,
2505 unlock_err = got_ref_unlock(ref);
2506 if (unlock_err && error == NULL)
2512 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2513 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2514 remote_name, refname + 11) == -1) {
2515 error = got_error_from_errno("asprintf");
2519 error = got_ref_open(&ref, repo, remote_refname, 1);
2521 if (error->code != GOT_ERR_NOT_REF)
2523 error = create_ref(remote_refname, id,
2528 error = update_ref(ref, id, replace_tags,
2530 unlock_err = got_ref_unlock(ref);
2531 if (unlock_err && error == NULL)
2538 /* Also create a local branch if none exists yet. */
2539 error = got_ref_open(&ref, repo, refname, 1);
2541 if (error->code != GOT_ERR_NOT_REF)
2543 error = create_ref(refname, id, verbosity,
2548 unlock_err = got_ref_unlock(ref);
2549 if (unlock_err && error == NULL)
2556 error = delete_missing_refs(&refs, &symrefs, remote,
2562 if (!remote->mirror_references) {
2563 /* Update remote HEAD reference if the server provided one. */
2564 TAILQ_FOREACH(pe, &symrefs, entry) {
2565 struct got_reference *target_ref;
2566 const char *refname = pe->path;
2567 const char *target = pe->data;
2568 char *remote_refname = NULL, *remote_target = NULL;
2570 if (strcmp(refname, GOT_REF_HEAD) != 0)
2573 if (strncmp("refs/heads/", target, 11) != 0)
2576 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2577 remote->name, refname) == -1) {
2578 error = got_error_from_errno("asprintf");
2581 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2582 remote->name, target + 11) == -1) {
2583 error = got_error_from_errno("asprintf");
2584 free(remote_refname);
2588 error = got_ref_open(&target_ref, repo, remote_target,
2591 free(remote_refname);
2592 free(remote_target);
2593 if (error->code == GOT_ERR_NOT_REF) {
2599 error = update_symref(remote_refname, target_ref,
2601 free(remote_refname);
2602 free(remote_target);
2603 got_ref_close(target_ref);
2610 if (kill(fetchpid, SIGTERM) == -1)
2611 error = got_error_from_errno("kill");
2612 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2613 error = got_error_from_errno("waitpid");
2615 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2616 error = got_error_from_errno("close");
2618 const struct got_error *close_err = got_repo_close(repo);
2623 got_worktree_close(worktree);
2624 TAILQ_FOREACH(pe, &refs, entry) {
2625 free((void *)pe->path);
2628 got_pathlist_free(&refs);
2629 TAILQ_FOREACH(pe, &symrefs, entry) {
2630 free((void *)pe->path);
2633 got_pathlist_free(&symrefs);
2634 got_pathlist_free(&wanted_branches);
2635 got_pathlist_free(&wanted_refs);
2650 usage_checkout(void)
2652 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2653 "[-p prefix] [-q] repository-path [worktree-path]\n",
2659 show_worktree_base_ref_warning(void)
2661 fprintf(stderr, "%s: warning: could not create a reference "
2662 "to the work tree's base commit; the commit could be "
2663 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2664 "repository writable and running 'got update' will prevent this\n",
2668 struct got_checkout_progress_arg {
2669 const char *worktree_path;
2670 int had_base_commit_ref_error;
2674 static const struct got_error *
2675 checkout_progress(void *arg, unsigned char status, const char *path)
2677 struct got_checkout_progress_arg *a = arg;
2679 /* Base commit bump happens silently. */
2680 if (status == GOT_STATUS_BUMP_BASE)
2683 if (status == GOT_STATUS_BASE_REF_ERR) {
2684 a->had_base_commit_ref_error = 1;
2688 while (path[0] == '/')
2691 if (a->verbosity >= 0)
2692 printf("%c %s/%s\n", status, a->worktree_path, path);
2697 static const struct got_error *
2698 check_cancelled(void *arg)
2700 if (sigint_received || sigpipe_received)
2701 return got_error(GOT_ERR_CANCELLED);
2705 static const struct got_error *
2706 check_linear_ancestry(struct got_object_id *commit_id,
2707 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2708 struct got_repository *repo)
2710 const struct got_error *err = NULL;
2711 struct got_object_id *yca_id;
2713 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2714 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2719 return got_error(GOT_ERR_ANCESTRY);
2722 * Require a straight line of history between the target commit
2723 * and the work tree's base commit.
2725 * Non-linear situations such as this require a rebase:
2727 * (commit) D F (base_commit)
2735 * 'got update' only handles linear cases:
2736 * Update forwards in time: A (base/yca) - B - C - D (commit)
2737 * Update backwards in time: D (base) - C - B - A (commit/yca)
2739 if (allow_forwards_in_time_only) {
2740 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2741 return got_error(GOT_ERR_ANCESTRY);
2742 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2743 got_object_id_cmp(base_commit_id, yca_id) != 0)
2744 return got_error(GOT_ERR_ANCESTRY);
2750 static const struct got_error *
2751 check_same_branch(struct got_object_id *commit_id,
2752 struct got_reference *head_ref, struct got_object_id *yca_id,
2753 struct got_repository *repo)
2755 const struct got_error *err = NULL;
2756 struct got_commit_graph *graph = NULL;
2757 struct got_object_id *head_commit_id = NULL;
2758 int is_same_branch = 0;
2760 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2764 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2768 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2773 err = got_commit_graph_open(&graph, "/", 1);
2777 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2778 check_cancelled, NULL);
2783 struct got_object_id *id;
2784 err = got_commit_graph_iter_next(&id, graph, repo,
2785 check_cancelled, NULL);
2787 if (err->code == GOT_ERR_ITER_COMPLETED)
2793 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2795 if (got_object_id_cmp(id, commit_id) == 0) {
2803 got_commit_graph_close(graph);
2804 free(head_commit_id);
2805 if (!err && !is_same_branch)
2806 err = got_error(GOT_ERR_ANCESTRY);
2810 static const struct got_error *
2811 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2813 static char msg[512];
2814 const char *branch_name;
2816 if (got_ref_is_symbolic(ref))
2817 branch_name = got_ref_get_symref_target(ref);
2819 branch_name = got_ref_get_name(ref);
2821 if (strncmp("refs/heads/", branch_name, 11) == 0)
2824 snprintf(msg, sizeof(msg),
2825 "target commit is not contained in branch '%s'; "
2826 "the branch to use must be specified with -b; "
2827 "if necessary a new branch can be created for "
2828 "this commit with 'got branch -c %s BRANCH_NAME'",
2829 branch_name, commit_id_str);
2831 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2834 static const struct got_error *
2835 cmd_checkout(int argc, char *argv[])
2837 const struct got_error *error = NULL;
2838 struct got_repository *repo = NULL;
2839 struct got_reference *head_ref = NULL, *ref = NULL;
2840 struct got_worktree *worktree = NULL;
2841 char *repo_path = NULL;
2842 char *worktree_path = NULL;
2843 const char *path_prefix = "";
2844 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2845 char *commit_id_str = NULL;
2846 struct got_object_id *commit_id = NULL;
2848 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2849 struct got_pathlist_head paths;
2850 struct got_checkout_progress_arg cpa;
2854 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2857 branch_name = optarg;
2860 commit_id_str = strdup(optarg);
2861 if (commit_id_str == NULL)
2862 return got_error_from_errno("strdup");
2868 path_prefix = optarg;
2883 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2884 "unveil", NULL) == -1)
2888 char *base, *dotgit;
2890 repo_path = realpath(argv[0], NULL);
2891 if (repo_path == NULL)
2892 return got_error_from_errno2("realpath", argv[0]);
2893 cwd = getcwd(NULL, 0);
2895 error = got_error_from_errno("getcwd");
2902 error = got_path_basename(&base, path);
2905 dotgit = strstr(base, ".git");
2908 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2909 error = got_error_from_errno("asprintf");
2914 } else if (argc == 2) {
2915 repo_path = realpath(argv[0], NULL);
2916 if (repo_path == NULL) {
2917 error = got_error_from_errno2("realpath", argv[0]);
2920 worktree_path = realpath(argv[1], NULL);
2921 if (worktree_path == NULL) {
2922 if (errno != ENOENT) {
2923 error = got_error_from_errno2("realpath",
2927 worktree_path = strdup(argv[1]);
2928 if (worktree_path == NULL) {
2929 error = got_error_from_errno("strdup");
2936 got_path_strip_trailing_slashes(repo_path);
2937 got_path_strip_trailing_slashes(worktree_path);
2939 error = got_repo_open(&repo, repo_path, NULL);
2943 /* Pre-create work tree path for unveil(2) */
2944 error = got_path_mkdir(worktree_path);
2946 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2947 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2949 if (!allow_nonempty &&
2950 !got_path_dir_is_empty(worktree_path)) {
2951 error = got_error_path(worktree_path,
2952 GOT_ERR_DIR_NOT_EMPTY);
2957 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2961 error = got_ref_open(&head_ref, repo, branch_name, 0);
2965 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2966 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2969 error = got_worktree_open(&worktree, worktree_path);
2973 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2977 if (!same_path_prefix) {
2978 error = got_error(GOT_ERR_PATH_PREFIX);
2982 if (commit_id_str) {
2983 struct got_reflist_head refs;
2985 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2989 error = got_repo_match_object_id(&commit_id, NULL,
2990 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2991 got_ref_list_free(&refs);
2994 error = check_linear_ancestry(commit_id,
2995 got_worktree_get_base_commit_id(worktree), 0, repo);
2996 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,
4014 if (show_changed_paths) {
4015 err = get_changed_paths(&changed_paths,
4020 err = print_commit(commit, &qid->id, repo, path,
4021 show_changed_paths ? &changed_paths : NULL,
4022 show_patch, diff_context, refs_idmap, NULL);
4023 got_object_commit_close(commit);
4026 TAILQ_FOREACH(pe, &changed_paths, entry) {
4027 free((char *)pe->path);
4030 got_pathlist_free(&changed_paths);
4034 while (!STAILQ_EMPTY(&reversed_commits)) {
4035 qid = STAILQ_FIRST(&reversed_commits);
4036 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4037 got_object_qid_free(qid);
4039 TAILQ_FOREACH(pe, &changed_paths, entry) {
4040 free((char *)pe->path);
4043 got_pathlist_free(&changed_paths);
4046 got_commit_graph_close(graph);
4053 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4054 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4055 "[-R] [path]\n", getprogname());
4060 get_default_log_limit(void)
4062 const char *got_default_log_limit;
4066 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4067 if (got_default_log_limit == NULL)
4069 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4075 static const struct got_error *
4076 cmd_log(int argc, char *argv[])
4078 const struct got_error *error;
4079 struct got_repository *repo = NULL;
4080 struct got_worktree *worktree = NULL;
4081 struct got_object_id *start_id = NULL, *end_id = NULL;
4082 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4083 const char *start_commit = NULL, *end_commit = NULL;
4084 const char *search_pattern = NULL;
4085 int diff_context = -1, ch;
4086 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4087 int reverse_display_order = 0;
4089 struct got_reflist_head refs;
4090 struct got_reflist_object_id_map *refs_idmap = NULL;
4095 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4101 limit = get_default_log_limit();
4103 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4109 show_changed_paths = 1;
4112 start_commit = optarg;
4115 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4118 errx(1, "number of context lines is %s: %s",
4122 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4124 errx(1, "number of commits is %s: %s",
4131 repo_path = realpath(optarg, NULL);
4132 if (repo_path == NULL)
4133 return got_error_from_errno2("realpath",
4135 got_path_strip_trailing_slashes(repo_path);
4138 reverse_display_order = 1;
4141 search_pattern = optarg;
4144 end_commit = optarg;
4155 if (diff_context == -1)
4157 else if (!show_patch)
4158 errx(1, "-C requires -p");
4160 cwd = getcwd(NULL, 0);
4162 error = got_error_from_errno("getcwd");
4166 if (repo_path == NULL) {
4167 error = got_worktree_open(&worktree, cwd);
4168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4175 error = got_worktree_resolve_path(&path, worktree,
4180 path = strdup(argv[0]);
4182 error = got_error_from_errno("strdup");
4186 } else if (argc != 0)
4189 if (repo_path == NULL) {
4190 repo_path = worktree ?
4191 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4193 if (repo_path == NULL) {
4194 error = got_error_from_errno("strdup");
4198 error = got_repo_open(&repo, repo_path, NULL);
4202 error = apply_unveil(got_repo_get_path(repo), 1,
4203 worktree ? got_worktree_get_root_path(worktree) : NULL);
4207 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4211 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4215 if (start_commit == NULL) {
4216 struct got_reference *head_ref;
4217 struct got_commit_object *commit = NULL;
4218 error = got_ref_open(&head_ref, repo,
4219 worktree ? got_worktree_get_head_ref_name(worktree)
4223 error = got_ref_resolve(&start_id, repo, head_ref);
4224 got_ref_close(head_ref);
4227 error = got_object_open_as_commit(&commit, repo,
4231 got_object_commit_close(commit);
4233 error = got_repo_match_object_id(&start_id, NULL,
4234 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4238 if (end_commit != NULL) {
4239 error = got_repo_match_object_id(&end_id, NULL,
4240 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4247 * If a path was specified on the command line it was resolved
4248 * to a path in the work tree above. Prepend the work tree's
4249 * path prefix to obtain the corresponding in-repository path.
4253 prefix = got_worktree_get_path_prefix(worktree);
4254 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4255 (path[0] != '\0') ? "/" : "", path) == -1) {
4256 error = got_error_from_errno("asprintf");
4261 error = got_repo_map_path(&in_repo_path, repo,
4267 path = in_repo_path;
4271 /* Release work tree lock. */
4272 got_worktree_close(worktree);
4276 error = print_commits(start_id, end_id, repo, path ? path : "",
4277 show_changed_paths, show_patch, search_pattern, diff_context,
4278 limit, log_branches, reverse_display_order, refs_idmap);
4284 got_worktree_close(worktree);
4286 const struct got_error *close_err = got_repo_close(repo);
4291 got_reflist_object_id_map_free(refs_idmap);
4292 got_ref_list_free(&refs);
4299 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4300 "[-r repository-path] [-s] [-w] [-P] "
4301 "[object1 object2 | path ...]\n", getprogname());
4305 struct print_diff_arg {
4306 struct got_repository *repo;
4307 struct got_worktree *worktree;
4312 int ignore_whitespace;
4313 int force_text_diff;
4317 * Create a file which contains the target path of a symlink so we can feed
4318 * it as content to the diff engine.
4320 static const struct got_error *
4321 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4322 const char *abspath)
4324 const struct got_error *err = NULL;
4325 char target_path[PATH_MAX];
4326 ssize_t target_len, outlen;
4331 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4332 if (target_len == -1)
4333 return got_error_from_errno2("readlinkat", abspath);
4335 target_len = readlink(abspath, target_path, PATH_MAX);
4336 if (target_len == -1)
4337 return got_error_from_errno2("readlink", abspath);
4340 *fd = got_opentempfd();
4342 return got_error_from_errno("got_opentempfd");
4344 outlen = write(*fd, target_path, target_len);
4346 err = got_error_from_errno("got_opentempfd");
4350 if (lseek(*fd, 0, SEEK_SET) == -1) {
4351 err = got_error_from_errno2("lseek", abspath);
4362 static const struct got_error *
4363 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4364 const char *path, struct got_object_id *blob_id,
4365 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4366 int dirfd, const char *de_name)
4368 struct print_diff_arg *a = arg;
4369 const struct got_error *err = NULL;
4370 struct got_blob_object *blob1 = NULL;
4373 char *abspath = NULL, *label1 = NULL;
4376 if (a->diff_staged) {
4377 if (staged_status != GOT_STATUS_MODIFY &&
4378 staged_status != GOT_STATUS_ADD &&
4379 staged_status != GOT_STATUS_DELETE)
4382 if (staged_status == GOT_STATUS_DELETE)
4384 if (status == GOT_STATUS_NONEXISTENT)
4385 return got_error_set_errno(ENOENT, path);
4386 if (status != GOT_STATUS_MODIFY &&
4387 status != GOT_STATUS_ADD &&
4388 status != GOT_STATUS_DELETE &&
4389 status != GOT_STATUS_CONFLICT)
4393 if (!a->header_shown) {
4394 printf("diff %s %s%s\n", a->id_str,
4395 got_worktree_get_root_path(a->worktree),
4396 a->diff_staged ? " (staged changes)" : "");
4397 a->header_shown = 1;
4400 if (a->diff_staged) {
4401 const char *label1 = NULL, *label2 = NULL;
4402 switch (staged_status) {
4403 case GOT_STATUS_MODIFY:
4407 case GOT_STATUS_ADD:
4410 case GOT_STATUS_DELETE:
4414 return got_error(GOT_ERR_FILE_STATUS);
4416 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4417 staged_blob_id, label1, label2, a->diff_context,
4418 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4421 if (staged_status == GOT_STATUS_ADD ||
4422 staged_status == GOT_STATUS_MODIFY) {
4424 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4428 err = got_object_id_str(&id_str, staged_blob_id);
4431 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4432 err = got_error_from_errno("asprintf");
4437 } else if (status != GOT_STATUS_ADD) {
4438 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4443 if (status != GOT_STATUS_DELETE) {
4444 if (asprintf(&abspath, "%s/%s",
4445 got_worktree_get_root_path(a->worktree), path) == -1) {
4446 err = got_error_from_errno("asprintf");
4451 fd = openat(dirfd, de_name,
4452 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4454 if (!got_err_open_nofollow_on_symlink()) {
4455 err = got_error_from_errno2("openat",
4459 err = get_symlink_target_file(&fd, dirfd,
4465 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4467 if (!got_err_open_nofollow_on_symlink()) {
4468 err = got_error_from_errno2("open",
4472 err = get_symlink_target_file(&fd, dirfd,
4478 if (fstat(fd, &sb) == -1) {
4479 err = got_error_from_errno2("fstat", abspath);
4482 f2 = fdopen(fd, "r");
4484 err = got_error_from_errno2("fdopen", abspath);
4491 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4492 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4495 got_object_blob_close(blob1);
4496 if (f2 && fclose(f2) == EOF && err == NULL)
4497 err = got_error_from_errno("fclose");
4498 if (fd != -1 && close(fd) == -1 && err == NULL)
4499 err = got_error_from_errno("close");
4504 static const struct got_error *
4505 cmd_diff(int argc, char *argv[])
4507 const struct got_error *error;
4508 struct got_repository *repo = NULL;
4509 struct got_worktree *worktree = NULL;
4510 char *cwd = NULL, *repo_path = NULL;
4511 const char *commit_args[2] = { NULL, NULL };
4512 int ncommit_args = 0;
4513 struct got_object_id *ids[2] = { NULL, NULL };
4514 char *labels[2] = { NULL, NULL };
4515 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4516 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4517 int force_text_diff = 0, force_path = 0, rflag = 0;
4519 struct got_reflist_head refs;
4520 struct got_pathlist_head paths;
4521 struct got_pathlist_entry *pe;
4527 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4532 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4535 force_text_diff = 1;
4538 if (ncommit_args >= 2)
4539 errx(1, "too many -c options used");
4540 commit_args[ncommit_args++] = optarg;
4543 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4546 errx(1, "number of context lines is %s: %s",
4550 repo_path = realpath(optarg, NULL);
4551 if (repo_path == NULL)
4552 return got_error_from_errno2("realpath",
4554 got_path_strip_trailing_slashes(repo_path);
4561 ignore_whitespace = 1;
4575 cwd = getcwd(NULL, 0);
4577 error = got_error_from_errno("getcwd");
4581 if (repo_path == NULL) {
4582 error = got_worktree_open(&worktree, cwd);
4583 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4589 strdup(got_worktree_get_repo_path(worktree));
4590 if (repo_path == NULL) {
4591 error = got_error_from_errno("strdup");
4595 repo_path = strdup(cwd);
4596 if (repo_path == NULL) {
4597 error = got_error_from_errno("strdup");
4603 error = got_repo_open(&repo, repo_path, NULL);
4608 if (rflag || worktree == NULL || ncommit_args > 0) {
4610 error = got_error_msg(GOT_ERR_NOT_IMPL,
4611 "-P option can only be used when diffing "
4616 error = got_error_msg(GOT_ERR_NOT_IMPL,
4617 "-s option can only be used when diffing "
4623 error = apply_unveil(got_repo_get_path(repo), 1,
4624 worktree ? got_worktree_get_root_path(worktree) : NULL);
4628 if ((!force_path && argc == 2) || ncommit_args > 0) {
4629 int obj_type = (ncommit_args > 0 ?
4630 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4631 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4635 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4637 if (ncommit_args > 0)
4638 arg = commit_args[i];
4641 error = got_repo_match_object_id(&ids[i], &labels[i],
4642 arg, obj_type, &refs, repo);
4644 if (error->code != GOT_ERR_NOT_REF &&
4645 error->code != GOT_ERR_NO_OBJ)
4647 if (ncommit_args > 0)
4655 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4656 struct print_diff_arg arg;
4659 if (worktree == NULL) {
4660 if (argc == 2 && ids[0] == NULL) {
4661 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4663 } else if (argc == 2 && ids[1] == NULL) {
4664 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4666 } else if (argc > 0) {
4667 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4668 "%s", "specified paths cannot be resolved");
4671 error = got_error(GOT_ERR_NOT_WORKTREE);
4676 error = get_worktree_paths_from_argv(&paths, argc, argv,
4681 error = got_object_id_str(&id_str,
4682 got_worktree_get_base_commit_id(worktree));
4686 arg.worktree = worktree;
4687 arg.diff_context = diff_context;
4688 arg.id_str = id_str;
4689 arg.header_shown = 0;
4690 arg.diff_staged = diff_staged;
4691 arg.ignore_whitespace = ignore_whitespace;
4692 arg.force_text_diff = force_text_diff;
4694 error = got_worktree_status(worktree, &paths, repo, 0,
4695 print_diff, &arg, check_cancelled, NULL);
4700 if (ncommit_args == 1) {
4701 struct got_commit_object *commit;
4702 error = got_object_open_as_commit(&commit, repo, ids[0]);
4706 labels[1] = labels[0];
4708 if (got_object_commit_get_nparents(commit) > 0) {
4709 const struct got_object_id_queue *pids;
4710 struct got_object_qid *pid;
4711 pids = got_object_commit_get_parent_ids(commit);
4712 pid = STAILQ_FIRST(pids);
4713 ids[0] = got_object_id_dup(&pid->id);
4714 if (ids[0] == NULL) {
4715 error = got_error_from_errno(
4716 "got_object_id_dup");
4717 got_object_commit_close(commit);
4720 error = got_object_id_str(&labels[0], ids[0]);
4722 got_object_commit_close(commit);
4727 labels[0] = strdup("/dev/null");
4728 if (labels[0] == NULL) {
4729 error = got_error_from_errno("strdup");
4730 got_object_commit_close(commit);
4735 got_object_commit_close(commit);
4738 if (ncommit_args == 0 && argc > 2) {
4739 error = got_error_msg(GOT_ERR_BAD_PATH,
4740 "path arguments cannot be used when diffing two objects");
4745 error = got_object_get_type(&type1, repo, ids[0]);
4750 error = got_object_get_type(&type2, repo, ids[1]);
4753 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4754 error = got_error(GOT_ERR_OBJ_TYPE);
4757 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4758 error = got_error_msg(GOT_ERR_OBJ_TYPE,
4759 "path arguments cannot be used when diffing blobs");
4763 for (i = 0; ncommit_args > 0 && i < argc; i++) {
4765 struct got_pathlist_entry *new;
4769 error = got_worktree_resolve_path(&p, worktree,
4773 prefix = got_worktree_get_path_prefix(worktree);
4774 while (prefix[0] == '/')
4776 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4777 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4779 error = got_error_from_errno("asprintf");
4785 char *mapped_path, *s;
4786 error = got_repo_map_path(&mapped_path, repo, argv[i]);
4792 in_repo_path = strdup(s);
4793 if (in_repo_path == NULL) {
4794 error = got_error_from_errno("asprintf");
4801 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4802 if (error || new == NULL /* duplicate */)
4809 /* Release work tree lock. */
4810 got_worktree_close(worktree);
4814 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4815 case GOT_OBJ_TYPE_BLOB:
4816 error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4817 NULL, NULL, diff_context, ignore_whitespace,
4818 force_text_diff, repo, stdout);
4820 case GOT_OBJ_TYPE_TREE:
4821 error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4822 &paths, "", "", diff_context, ignore_whitespace,
4823 force_text_diff, repo, stdout);
4825 case GOT_OBJ_TYPE_COMMIT:
4826 printf("diff %s %s\n", labels[0], labels[1]);
4827 error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4828 &paths, diff_context, ignore_whitespace, force_text_diff,
4832 error = got_error(GOT_ERR_OBJ_TYPE);
4840 got_worktree_close(worktree);
4842 const struct got_error *close_err = got_repo_close(repo);
4846 TAILQ_FOREACH(pe, &paths, entry)
4847 free((char *)pe->path);
4848 got_pathlist_free(&paths);
4849 got_ref_list_free(&refs);
4857 "usage: %s blame [-c commit] [-r repository-path] path\n",
4866 char datebuf[11]; /* YYYY-MM-DD + NUL */
4869 struct blame_cb_args {
4870 struct blame_line *lines;
4874 off_t *line_offsets;
4876 struct got_repository *repo;
4879 static const struct got_error *
4880 blame_cb(void *arg, int nlines, int lineno,
4881 struct got_commit_object *commit, struct got_object_id *id)
4883 const struct got_error *err = NULL;
4884 struct blame_cb_args *a = arg;
4885 struct blame_line *bline;
4887 size_t linesize = 0;
4890 time_t committer_time;
4892 if (nlines != a->nlines ||
4893 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4894 return got_error(GOT_ERR_RANGE);
4896 if (sigint_received)
4897 return got_error(GOT_ERR_ITER_COMPLETED);
4900 return NULL; /* no change in this commit */
4902 /* Annotate this line. */
4903 bline = &a->lines[lineno - 1];
4904 if (bline->annotated)
4906 err = got_object_id_str(&bline->id_str, id);
4910 bline->committer = strdup(got_object_commit_get_committer(commit));
4911 if (bline->committer == NULL) {
4912 err = got_error_from_errno("strdup");
4916 committer_time = got_object_commit_get_committer_time(commit);
4917 if (gmtime_r(&committer_time, &tm) == NULL)
4918 return got_error_from_errno("gmtime_r");
4919 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4921 err = got_error(GOT_ERR_NO_SPACE);
4924 bline->annotated = 1;
4926 /* Print lines annotated so far. */
4927 bline = &a->lines[a->lineno_cur - 1];
4928 if (!bline->annotated)
4931 offset = a->line_offsets[a->lineno_cur - 1];
4932 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4933 err = got_error_from_errno("fseeko");
4937 while (bline->annotated) {
4938 char *smallerthan, *at, *nl, *committer;
4941 if (getline(&line, &linesize, a->f) == -1) {
4943 err = got_error_from_errno("getline");
4947 committer = bline->committer;
4948 smallerthan = strchr(committer, '<');
4949 if (smallerthan && smallerthan[1] != '\0')
4950 committer = smallerthan + 1;
4951 at = strchr(committer, '@');
4954 len = strlen(committer);
4956 committer[8] = '\0';
4958 nl = strchr(line, '\n');
4961 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4962 bline->id_str, bline->datebuf, committer, line);
4965 bline = &a->lines[a->lineno_cur - 1];
4972 static const struct got_error *
4973 cmd_blame(int argc, char *argv[])
4975 const struct got_error *error;
4976 struct got_repository *repo = NULL;
4977 struct got_worktree *worktree = NULL;
4978 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4979 char *link_target = NULL;
4980 struct got_object_id *obj_id = NULL;
4981 struct got_object_id *commit_id = NULL;
4982 struct got_commit_object *commit = NULL;
4983 struct got_blob_object *blob = NULL;
4984 char *commit_id_str = NULL;
4985 struct blame_cb_args bca;
4986 int ch, obj_type, i;
4989 memset(&bca, 0, sizeof(bca));
4992 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4997 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5000 commit_id_str = optarg;
5003 repo_path = realpath(optarg, NULL);
5004 if (repo_path == NULL)
5005 return got_error_from_errno2("realpath",
5007 got_path_strip_trailing_slashes(repo_path);
5023 cwd = getcwd(NULL, 0);
5025 error = got_error_from_errno("getcwd");
5028 if (repo_path == NULL) {
5029 error = got_worktree_open(&worktree, cwd);
5030 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5036 strdup(got_worktree_get_repo_path(worktree));
5037 if (repo_path == NULL) {
5038 error = got_error_from_errno("strdup");
5043 repo_path = strdup(cwd);
5044 if (repo_path == NULL) {
5045 error = got_error_from_errno("strdup");
5051 error = got_repo_open(&repo, repo_path, NULL);
5056 const char *prefix = got_worktree_get_path_prefix(worktree);
5059 error = got_worktree_resolve_path(&p, worktree, path);
5062 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5063 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5065 error = got_error_from_errno("asprintf");
5070 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5072 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5075 error = got_repo_map_path(&in_repo_path, repo, path);
5080 if (commit_id_str == NULL) {
5081 struct got_reference *head_ref;
5082 error = got_ref_open(&head_ref, repo, worktree ?
5083 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5086 error = got_ref_resolve(&commit_id, repo, head_ref);
5087 got_ref_close(head_ref);
5091 struct got_reflist_head refs;
5093 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5097 error = got_repo_match_object_id(&commit_id, NULL,
5098 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5099 got_ref_list_free(&refs);
5105 /* Release work tree lock. */
5106 got_worktree_close(worktree);
5110 error = got_object_open_as_commit(&commit, repo, commit_id);
5114 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5119 error = got_object_id_by_path(&obj_id, repo, commit,
5120 link_target ? link_target : in_repo_path);
5124 error = got_object_get_type(&obj_type, repo, obj_id);
5128 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5129 error = got_error_path(link_target ? link_target : in_repo_path,
5134 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5137 bca.f = got_opentemp();
5138 if (bca.f == NULL) {
5139 error = got_error_from_errno("got_opentemp");
5142 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5143 &bca.line_offsets, bca.f, blob);
5144 if (error || bca.nlines == 0)
5147 /* Don't include \n at EOF in the blame line count. */
5148 if (bca.line_offsets[bca.nlines - 1] == filesize)
5151 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5152 if (bca.lines == NULL) {
5153 error = got_error_from_errno("calloc");
5157 bca.nlines_prec = 0;
5165 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5166 repo, blame_cb, &bca, check_cancelled, NULL);
5175 got_object_commit_close(commit);
5177 got_object_blob_close(blob);
5179 got_worktree_close(worktree);
5181 const struct got_error *close_err = got_repo_close(repo);
5186 for (i = 0; i < bca.nlines; i++) {
5187 struct blame_line *bline = &bca.lines[i];
5188 free(bline->id_str);
5189 free(bline->committer);
5193 free(bca.line_offsets);
5194 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5195 error = got_error_from_errno("fclose");
5203 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5208 static const struct got_error *
5209 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5210 const char *root_path, struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 int is_root_path = (strcmp(path, root_path) == 0);
5214 const char *modestr = "";
5215 mode_t mode = got_tree_entry_get_mode(te);
5216 char *link_target = NULL;
5218 path += strlen(root_path);
5219 while (path[0] == '/')
5222 if (got_object_tree_entry_is_submodule(te))
5224 else if (S_ISLNK(mode)) {
5227 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5230 for (i = 0; i < strlen(link_target); i++) {
5231 if (!isprint((unsigned char)link_target[i]))
5232 link_target[i] = '?';
5237 else if (S_ISDIR(mode))
5239 else if (mode & S_IXUSR)
5242 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5243 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5244 link_target ? " -> ": "", link_target ? link_target : "");
5250 static const struct got_error *
5251 print_tree(const char *path, struct got_commit_object *commit,
5252 int show_ids, int recurse, const char *root_path,
5253 struct got_repository *repo)
5255 const struct got_error *err = NULL;
5256 struct got_object_id *tree_id = NULL;
5257 struct got_tree_object *tree = NULL;
5260 err = got_object_id_by_path(&tree_id, repo, commit, path);
5264 err = got_object_open_as_tree(&tree, repo, tree_id);
5267 nentries = got_object_tree_get_nentries(tree);
5268 for (i = 0; i < nentries; i++) {
5269 struct got_tree_entry *te;
5272 if (sigint_received || sigpipe_received)
5275 te = got_object_tree_get_entry(tree, i);
5278 err = got_object_id_str(&id_str,
5279 got_tree_entry_get_id(te));
5282 if (asprintf(&id, "%s ", id_str) == -1) {
5283 err = got_error_from_errno("asprintf");
5289 err = print_entry(te, id, path, root_path, repo);
5294 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5296 if (asprintf(&child_path, "%s%s%s", path,
5297 path[0] == '/' && path[1] == '\0' ? "" : "/",
5298 got_tree_entry_get_name(te)) == -1) {
5299 err = got_error_from_errno("asprintf");
5302 err = print_tree(child_path, commit, show_ids, 1,
5311 got_object_tree_close(tree);
5316 static const struct got_error *
5317 cmd_tree(int argc, char *argv[])
5319 const struct got_error *error;
5320 struct got_repository *repo = NULL;
5321 struct got_worktree *worktree = NULL;
5322 const char *path, *refname = NULL;
5323 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5324 struct got_object_id *commit_id = NULL;
5325 struct got_commit_object *commit = NULL;
5326 char *commit_id_str = NULL;
5327 int show_ids = 0, recurse = 0;
5331 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5336 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5339 commit_id_str = optarg;
5342 repo_path = realpath(optarg, NULL);
5343 if (repo_path == NULL)
5344 return got_error_from_errno2("realpath",
5346 got_path_strip_trailing_slashes(repo_path);
5370 cwd = getcwd(NULL, 0);
5372 error = got_error_from_errno("getcwd");
5375 if (repo_path == NULL) {
5376 error = got_worktree_open(&worktree, cwd);
5377 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5383 strdup(got_worktree_get_repo_path(worktree));
5384 if (repo_path == NULL)
5385 error = got_error_from_errno("strdup");
5389 repo_path = strdup(cwd);
5390 if (repo_path == NULL) {
5391 error = got_error_from_errno("strdup");
5397 error = got_repo_open(&repo, repo_path, NULL);
5402 const char *prefix = got_worktree_get_path_prefix(worktree);
5407 error = got_worktree_resolve_path(&p, worktree, path);
5410 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5411 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5413 error = got_error_from_errno("asprintf");
5418 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5422 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5427 error = got_repo_map_path(&in_repo_path, repo, path);
5432 if (commit_id_str == NULL) {
5433 struct got_reference *head_ref;
5435 refname = got_worktree_get_head_ref_name(worktree);
5437 refname = GOT_REF_HEAD;
5438 error = got_ref_open(&head_ref, repo, refname, 0);
5441 error = got_ref_resolve(&commit_id, repo, head_ref);
5442 got_ref_close(head_ref);
5446 struct got_reflist_head refs;
5448 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5452 error = got_repo_match_object_id(&commit_id, NULL,
5453 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5454 got_ref_list_free(&refs);
5460 /* Release work tree lock. */
5461 got_worktree_close(worktree);
5465 error = got_object_open_as_commit(&commit, repo, commit_id);
5469 error = print_tree(in_repo_path, commit, show_ids, recurse,
5470 in_repo_path, repo);
5477 got_object_commit_close(commit);
5479 got_worktree_close(worktree);
5481 const struct got_error *close_err = got_repo_close(repo);
5491 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5492 "[-S status-codes] [path ...]\n", getprogname());
5496 struct got_status_arg {
5501 static const struct got_error *
5502 print_status(void *arg, unsigned char status, unsigned char staged_status,
5503 const char *path, struct got_object_id *blob_id,
5504 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5505 int dirfd, const char *de_name)
5507 struct got_status_arg *st = arg;
5509 if (status == staged_status && (status == GOT_STATUS_DELETE))
5510 status = GOT_STATUS_NO_CHANGE;
5511 if (st != NULL && st->status_codes) {
5512 size_t ncodes = strlen(st->status_codes);
5515 for (i = 0; i < ncodes ; i++) {
5517 if (status == st->status_codes[i] ||
5518 staged_status == st->status_codes[i]) {
5523 if (status == st->status_codes[i] ||
5524 staged_status == st->status_codes[i])
5529 if (st->suppress && j == 0)
5536 printf("%c%c %s\n", status, staged_status, path);
5540 static const struct got_error *
5541 cmd_status(int argc, char *argv[])
5543 const struct got_error *error = NULL;
5544 struct got_repository *repo = NULL;
5545 struct got_worktree *worktree = NULL;
5546 struct got_status_arg st;
5548 struct got_pathlist_head paths;
5549 struct got_pathlist_entry *pe;
5550 int ch, i, no_ignores = 0;
5554 memset(&st, 0, sizeof(st));
5555 st.status_codes = NULL;
5558 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5564 if (st.status_codes != NULL && st.suppress == 0)
5565 option_conflict('S', 's');
5569 for (i = 0; i < strlen(optarg); i++) {
5570 switch (optarg[i]) {
5571 case GOT_STATUS_MODIFY:
5572 case GOT_STATUS_ADD:
5573 case GOT_STATUS_DELETE:
5574 case GOT_STATUS_CONFLICT:
5575 case GOT_STATUS_MISSING:
5576 case GOT_STATUS_OBSTRUCTED:
5577 case GOT_STATUS_UNVERSIONED:
5578 case GOT_STATUS_MODE_CHANGE:
5579 case GOT_STATUS_NONEXISTENT:
5582 errx(1, "invalid status code '%c'",
5586 if (ch == 's' && st.suppress)
5587 option_conflict('s', 'S');
5588 st.status_codes = optarg;
5600 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5604 cwd = getcwd(NULL, 0);
5606 error = got_error_from_errno("getcwd");
5610 error = got_worktree_open(&worktree, cwd);
5612 if (error->code == GOT_ERR_NOT_WORKTREE)
5613 error = wrap_not_worktree_error(error, "status", cwd);
5617 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5622 error = apply_unveil(got_repo_get_path(repo), 1,
5623 got_worktree_get_root_path(worktree));
5627 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5631 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5632 print_status, &st, check_cancelled, NULL);
5634 TAILQ_FOREACH(pe, &paths, entry)
5635 free((char *)pe->path);
5636 got_pathlist_free(&paths);
5645 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5646 "[-s reference] [-d] [name]\n",
5651 static const struct got_error *
5652 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5654 static const struct got_error *err = NULL;
5655 struct got_reflist_head refs;
5656 struct got_reflist_entry *re;
5659 err = got_ref_list(&refs, repo, refname, sort_by_time ?
5660 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5665 TAILQ_FOREACH(re, &refs, entry) {
5667 refstr = got_ref_to_str(re->ref);
5668 if (refstr == NULL) {
5669 err = got_error_from_errno("got_ref_to_str");
5672 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5676 got_ref_list_free(&refs);
5680 static const struct got_error *
5681 delete_ref_by_name(struct got_repository *repo, const char *refname)
5683 const struct got_error *err;
5684 struct got_reference *ref;
5686 err = got_ref_open(&ref, repo, refname, 0);
5690 err = delete_ref(repo, ref);
5695 static const struct got_error *
5696 add_ref(struct got_repository *repo, const char *refname, const char *target)
5698 const struct got_error *err = NULL;
5699 struct got_object_id *id = NULL;
5700 struct got_reference *ref = NULL;
5701 struct got_reflist_head refs;
5704 * Don't let the user create a reference name with a leading '-'.
5705 * While technically a valid reference name, this case is usually
5706 * an unintended typo.
5708 if (refname[0] == '-')
5709 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5712 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5715 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
5717 got_ref_list_free(&refs);
5721 err = got_ref_alloc(&ref, refname, id);
5725 err = got_ref_write(ref, repo);
5733 static const struct got_error *
5734 add_symref(struct got_repository *repo, const char *refname, const char *target)
5736 const struct got_error *err = NULL;
5737 struct got_reference *ref = NULL;
5738 struct got_reference *target_ref = NULL;
5741 * Don't let the user create a reference name with a leading '-'.
5742 * While technically a valid reference name, this case is usually
5743 * an unintended typo.
5745 if (refname[0] == '-')
5746 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5748 err = got_ref_open(&target_ref, repo, target, 0);
5752 err = got_ref_alloc_symref(&ref, refname, target_ref);
5756 err = got_ref_write(ref, repo);
5759 got_ref_close(target_ref);
5765 static const struct got_error *
5766 cmd_ref(int argc, char *argv[])
5768 const struct got_error *error = NULL;
5769 struct got_repository *repo = NULL;
5770 struct got_worktree *worktree = NULL;
5771 char *cwd = NULL, *repo_path = NULL;
5772 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5773 const char *obj_arg = NULL, *symref_target= NULL;
5774 char *refname = NULL;
5776 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5785 repo_path = realpath(optarg, NULL);
5786 if (repo_path == NULL)
5787 return got_error_from_errno2("realpath",
5789 got_path_strip_trailing_slashes(repo_path);
5795 symref_target = optarg;
5806 if (obj_arg && do_list)
5807 option_conflict('c', 'l');
5808 if (obj_arg && do_delete)
5809 option_conflict('c', 'd');
5810 if (obj_arg && symref_target)
5811 option_conflict('c', 's');
5812 if (symref_target && do_delete)
5813 option_conflict('s', 'd');
5814 if (symref_target && do_list)
5815 option_conflict('s', 'l');
5816 if (do_delete && do_list)
5817 option_conflict('d', 'l');
5818 if (sort_by_time && !do_list)
5819 errx(1, "-t option requires -l option");
5825 if (argc != 0 && argc != 1)
5828 refname = strdup(argv[0]);
5829 if (refname == NULL) {
5830 error = got_error_from_errno("strdup");
5837 refname = strdup(argv[0]);
5838 if (refname == NULL) {
5839 error = got_error_from_errno("strdup");
5845 got_path_strip_trailing_slashes(refname);
5849 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5853 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5854 "sendfd unveil", NULL) == -1)
5858 cwd = getcwd(NULL, 0);
5860 error = got_error_from_errno("getcwd");
5864 if (repo_path == NULL) {
5865 error = got_worktree_open(&worktree, cwd);
5866 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5872 strdup(got_worktree_get_repo_path(worktree));
5873 if (repo_path == NULL)
5874 error = got_error_from_errno("strdup");
5878 repo_path = strdup(cwd);
5879 if (repo_path == NULL) {
5880 error = got_error_from_errno("strdup");
5886 error = got_repo_open(&repo, repo_path, NULL);
5890 error = apply_unveil(got_repo_get_path(repo), do_list,
5891 worktree ? got_worktree_get_root_path(worktree) : NULL);
5896 error = list_refs(repo, refname, sort_by_time);
5898 error = delete_ref_by_name(repo, refname);
5899 else if (symref_target)
5900 error = add_symref(repo, refname, symref_target);
5902 if (obj_arg == NULL)
5904 error = add_ref(repo, refname, obj_arg);
5909 const struct got_error *close_err = got_repo_close(repo);
5914 got_worktree_close(worktree);
5924 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5925 "[-n] [name]\n", getprogname());
5929 static const struct got_error *
5930 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5931 struct got_reference *ref)
5933 const struct got_error *err = NULL;
5934 const char *refname, *marker = " ";
5937 refname = got_ref_get_name(ref);
5938 if (worktree && strcmp(refname,
5939 got_worktree_get_head_ref_name(worktree)) == 0) {
5940 struct got_object_id *id = NULL;
5942 err = got_ref_resolve(&id, repo, ref);
5945 if (got_object_id_cmp(id,
5946 got_worktree_get_base_commit_id(worktree)) == 0)
5953 if (strncmp(refname, "refs/heads/", 11) == 0)
5955 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5957 if (strncmp(refname, "refs/remotes/", 13) == 0)
5960 refstr = got_ref_to_str(ref);
5962 return got_error_from_errno("got_ref_to_str");
5964 printf("%s%s: %s\n", marker, refname, refstr);
5969 static const struct got_error *
5970 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5972 const char *refname;
5974 if (worktree == NULL)
5975 return got_error(GOT_ERR_NOT_WORKTREE);
5977 refname = got_worktree_get_head_ref_name(worktree);
5979 if (strncmp(refname, "refs/heads/", 11) == 0)
5981 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5984 printf("%s\n", refname);
5989 static const struct got_error *
5990 list_branches(struct got_repository *repo, struct got_worktree *worktree,
5993 static const struct got_error *err = NULL;
5994 struct got_reflist_head refs;
5995 struct got_reflist_entry *re;
5996 struct got_reference *temp_ref = NULL;
5997 int rebase_in_progress, histedit_in_progress;
6002 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6007 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6012 if (rebase_in_progress || histedit_in_progress) {
6013 err = got_ref_open(&temp_ref, repo,
6014 got_worktree_get_head_ref_name(worktree), 0);
6017 list_branch(repo, worktree, temp_ref);
6018 got_ref_close(temp_ref);
6022 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6023 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6028 TAILQ_FOREACH(re, &refs, entry)
6029 list_branch(repo, worktree, re->ref);
6031 got_ref_list_free(&refs);
6033 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6034 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6039 TAILQ_FOREACH(re, &refs, entry)
6040 list_branch(repo, worktree, re->ref);
6042 got_ref_list_free(&refs);
6047 static const struct got_error *
6048 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6049 const char *branch_name)
6051 const struct got_error *err = NULL;
6052 struct got_reference *ref = NULL;
6053 char *refname, *remote_refname = NULL;
6055 if (strncmp(branch_name, "refs/", 5) == 0)
6057 if (strncmp(branch_name, "heads/", 6) == 0)
6059 else if (strncmp(branch_name, "remotes/", 8) == 0)
6062 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6063 return got_error_from_errno("asprintf");
6065 if (asprintf(&remote_refname, "refs/remotes/%s",
6066 branch_name) == -1) {
6067 err = got_error_from_errno("asprintf");
6071 err = got_ref_open(&ref, repo, refname, 0);
6073 const struct got_error *err2;
6074 if (err->code != GOT_ERR_NOT_REF)
6077 * Keep 'err' intact such that if neither branch exists
6078 * we report "refs/heads" rather than "refs/remotes" in
6079 * our error message.
6081 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6088 strcmp(got_worktree_get_head_ref_name(worktree),
6089 got_ref_get_name(ref)) == 0) {
6090 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6091 "will not delete this work tree's current branch");
6095 err = delete_ref(repo, ref);
6100 free(remote_refname);
6104 static const struct got_error *
6105 add_branch(struct got_repository *repo, const char *branch_name,
6106 struct got_object_id *base_commit_id)
6108 const struct got_error *err = NULL;
6109 struct got_reference *ref = NULL;
6110 char *base_refname = NULL, *refname = NULL;
6113 * Don't let the user create a branch name with a leading '-'.
6114 * While technically a valid reference name, this case is usually
6115 * an unintended typo.
6117 if (branch_name[0] == '-')
6118 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6120 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6123 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6124 err = got_error_from_errno("asprintf");
6128 err = got_ref_open(&ref, repo, refname, 0);
6130 err = got_error(GOT_ERR_BRANCH_EXISTS);
6132 } else if (err->code != GOT_ERR_NOT_REF)
6135 err = got_ref_alloc(&ref, refname, base_commit_id);
6139 err = got_ref_write(ref, repo);
6148 static const struct got_error *
6149 cmd_branch(int argc, char *argv[])
6151 const struct got_error *error = NULL;
6152 struct got_repository *repo = NULL;
6153 struct got_worktree *worktree = NULL;
6154 char *cwd = NULL, *repo_path = NULL;
6155 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6156 const char *delref = NULL, *commit_id_arg = NULL;
6157 struct got_reference *ref = NULL;
6158 struct got_pathlist_head paths;
6159 struct got_pathlist_entry *pe;
6160 struct got_object_id *commit_id = NULL;
6161 char *commit_id_str = NULL;
6165 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6168 commit_id_arg = optarg;
6174 repo_path = realpath(optarg, NULL);
6175 if (repo_path == NULL)
6176 return got_error_from_errno2("realpath",
6178 got_path_strip_trailing_slashes(repo_path);
6195 if (do_list && delref)
6196 option_conflict('l', 'd');
6197 if (sort_by_time && !do_list)
6198 errx(1, "-t option requires -l option");
6203 if (!do_list && !delref && argc == 0)
6206 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6207 errx(1, "-c option can only be used when creating a branch");
6209 if (do_list || delref) {
6212 } else if (!do_show && argc != 1)
6216 if (do_list || do_show) {
6217 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6221 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6222 "sendfd unveil", NULL) == -1)
6226 cwd = getcwd(NULL, 0);
6228 error = got_error_from_errno("getcwd");
6232 if (repo_path == NULL) {
6233 error = got_worktree_open(&worktree, cwd);
6234 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6240 strdup(got_worktree_get_repo_path(worktree));
6241 if (repo_path == NULL)
6242 error = got_error_from_errno("strdup");
6246 repo_path = strdup(cwd);
6247 if (repo_path == NULL) {
6248 error = got_error_from_errno("strdup");
6254 error = got_repo_open(&repo, repo_path, NULL);
6258 error = apply_unveil(got_repo_get_path(repo), do_list,
6259 worktree ? got_worktree_get_root_path(worktree) : NULL);
6264 error = show_current_branch(repo, worktree);
6266 error = list_branches(repo, worktree, sort_by_time);
6268 error = delete_branch(repo, worktree, delref);
6270 struct got_reflist_head refs;
6272 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6276 if (commit_id_arg == NULL)
6277 commit_id_arg = worktree ?
6278 got_worktree_get_head_ref_name(worktree) :
6280 error = got_repo_match_object_id(&commit_id, NULL,
6281 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6282 got_ref_list_free(&refs);
6285 error = add_branch(repo, argv[0], commit_id);
6288 if (worktree && do_update) {
6289 struct got_update_progress_arg upa;
6290 char *branch_refname = NULL;
6292 error = got_object_id_str(&commit_id_str, commit_id);
6295 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6299 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6301 error = got_error_from_errno("asprintf");
6304 error = got_ref_open(&ref, repo, branch_refname, 0);
6305 free(branch_refname);
6308 error = switch_head_ref(ref, commit_id, worktree,
6312 error = got_worktree_set_base_commit_id(worktree, repo,
6316 memset(&upa, 0, sizeof(upa));
6317 error = got_worktree_checkout_files(worktree, &paths,
6318 repo, update_progress, &upa, check_cancelled,
6322 if (upa.did_something) {
6323 printf("Updated to %s: %s\n",
6324 got_worktree_get_head_ref_name(worktree),
6327 print_update_progress_stats(&upa);
6334 const struct got_error *close_err = got_repo_close(repo);
6339 got_worktree_close(worktree);
6343 free(commit_id_str);
6344 TAILQ_FOREACH(pe, &paths, entry)
6345 free((char *)pe->path);
6346 got_pathlist_free(&paths);
6355 "usage: %s tag [-c commit] [-r repository] [-l] "
6356 "[-m message] name\n", getprogname());
6361 static const struct got_error *
6362 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6364 const struct got_error *err = NULL;
6365 struct got_reflist_entry *re, *se, *new;
6366 struct got_object_id *re_id, *se_id;
6367 struct got_tag_object *re_tag, *se_tag;
6368 time_t re_time, se_time;
6370 STAILQ_FOREACH(re, tags, entry) {
6371 se = STAILQ_FIRST(sorted);
6373 err = got_reflist_entry_dup(&new, re);
6376 STAILQ_INSERT_HEAD(sorted, new, entry);
6379 err = got_ref_resolve(&re_id, repo, re->ref);
6382 err = got_object_open_as_tag(&re_tag, repo, re_id);
6386 re_time = got_object_tag_get_tagger_time(re_tag);
6387 got_object_tag_close(re_tag);
6391 err = got_ref_resolve(&se_id, repo, re->ref);
6394 err = got_object_open_as_tag(&se_tag, repo, se_id);
6398 se_time = got_object_tag_get_tagger_time(se_tag);
6399 got_object_tag_close(se_tag);
6401 if (se_time > re_time) {
6402 err = got_reflist_entry_dup(&new, re);
6405 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6408 se = STAILQ_NEXT(se, entry);
6417 static const struct got_error *
6418 list_tags(struct got_repository *repo)
6420 static const struct got_error *err = NULL;
6421 struct got_reflist_head refs;
6422 struct got_reflist_entry *re;
6426 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6430 TAILQ_FOREACH(re, &refs, entry) {
6431 const char *refname;
6432 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6436 struct got_object_id *id;
6437 struct got_tag_object *tag;
6438 struct got_commit_object *commit = NULL;
6440 refname = got_ref_get_name(re->ref);
6441 if (strncmp(refname, "refs/tags/", 10) != 0)
6444 refstr = got_ref_to_str(re->ref);
6445 if (refstr == NULL) {
6446 err = got_error_from_errno("got_ref_to_str");
6449 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6452 err = got_ref_resolve(&id, repo, re->ref);
6455 err = got_object_open_as_tag(&tag, repo, id);
6457 if (err->code != GOT_ERR_OBJ_TYPE) {
6461 /* "lightweight" tag */
6462 err = got_object_open_as_commit(&commit, repo, id);
6467 tagger = got_object_commit_get_committer(commit);
6469 got_object_commit_get_committer_time(commit);
6470 err = got_object_id_str(&id_str, id);
6476 tagger = got_object_tag_get_tagger(tag);
6477 tagger_time = got_object_tag_get_tagger_time(tag);
6478 err = got_object_id_str(&id_str,
6479 got_object_tag_get_object_id(tag));
6483 printf("from: %s\n", tagger);
6484 datestr = get_datestr(&tagger_time, datebuf);
6486 printf("date: %s UTC\n", datestr);
6488 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6490 switch (got_object_tag_get_object_type(tag)) {
6491 case GOT_OBJ_TYPE_BLOB:
6492 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6495 case GOT_OBJ_TYPE_TREE:
6496 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6499 case GOT_OBJ_TYPE_COMMIT:
6500 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6503 case GOT_OBJ_TYPE_TAG:
6504 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6513 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6516 got_object_commit_close(commit);
6518 tagmsg0 = strdup(got_object_tag_get_message(tag));
6519 got_object_tag_close(tag);
6520 if (tagmsg0 == NULL) {
6521 err = got_error_from_errno("strdup");
6528 line = strsep(&tagmsg, "\n");
6530 printf(" %s\n", line);
6535 got_ref_list_free(&refs);
6539 static const struct got_error *
6540 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6541 const char *tag_name, const char *repo_path)
6543 const struct got_error *err = NULL;
6544 char *template = NULL, *initial_content = NULL;
6545 char *editor = NULL;
6546 int initial_content_len;
6549 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6550 err = got_error_from_errno("asprintf");
6554 initial_content_len = asprintf(&initial_content,
6555 "\n# tagging commit %s as %s\n",
6556 commit_id_str, tag_name);
6557 if (initial_content_len == -1) {
6558 err = got_error_from_errno("asprintf");
6562 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6566 if (write(fd, initial_content, initial_content_len) == -1) {
6567 err = got_error_from_errno2("write", *tagmsg_path);
6571 err = get_editor(&editor);
6574 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6575 initial_content_len, 1);
6577 free(initial_content);
6581 if (fd != -1 && close(fd) == -1 && err == NULL)
6582 err = got_error_from_errno2("close", *tagmsg_path);
6584 /* Editor is done; we can now apply unveil(2) */
6586 err = apply_unveil(repo_path, 0, NULL);
6594 static const struct got_error *
6595 add_tag(struct got_repository *repo, const char *tagger,
6596 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6598 const struct got_error *err = NULL;
6599 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6600 char *label = NULL, *commit_id_str = NULL;
6601 struct got_reference *ref = NULL;
6602 char *refname = NULL, *tagmsg = NULL;
6603 char *tagmsg_path = NULL, *tag_id_str = NULL;
6604 int preserve_tagmsg = 0;
6605 struct got_reflist_head refs;
6610 * Don't let the user create a tag name with a leading '-'.
6611 * While technically a valid reference name, this case is usually
6612 * an unintended typo.
6614 if (tag_name[0] == '-')
6615 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6617 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6621 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6622 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6626 err = got_object_id_str(&commit_id_str, commit_id);
6630 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6631 refname = strdup(tag_name);
6632 if (refname == NULL) {
6633 err = got_error_from_errno("strdup");
6637 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6638 err = got_error_from_errno("asprintf");
6642 err = got_ref_open(&ref, repo, refname, 0);
6644 err = got_error(GOT_ERR_TAG_EXISTS);
6646 } else if (err->code != GOT_ERR_NOT_REF)
6649 if (tagmsg_arg == NULL) {
6650 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6651 tag_name, got_repo_get_path(repo));
6653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6654 tagmsg_path != NULL)
6655 preserve_tagmsg = 1;
6660 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6661 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6664 preserve_tagmsg = 1;
6668 err = got_ref_alloc(&ref, refname, tag_id);
6671 preserve_tagmsg = 1;
6675 err = got_ref_write(ref, repo);
6678 preserve_tagmsg = 1;
6682 err = got_object_id_str(&tag_id_str, tag_id);
6685 preserve_tagmsg = 1;
6688 printf("Created tag %s\n", tag_id_str);
6690 if (preserve_tagmsg) {
6691 fprintf(stderr, "%s: tag message preserved in %s\n",
6692 getprogname(), tagmsg_path);
6693 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6694 err = got_error_from_errno2("unlink", tagmsg_path);
6699 free(commit_id_str);
6703 got_ref_list_free(&refs);
6707 static const struct got_error *
6708 cmd_tag(int argc, char *argv[])
6710 const struct got_error *error = NULL;
6711 struct got_repository *repo = NULL;
6712 struct got_worktree *worktree = NULL;
6713 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6714 char *gitconfig_path = NULL, *tagger = NULL;
6715 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6716 int ch, do_list = 0;
6718 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6721 commit_id_arg = optarg;
6727 repo_path = realpath(optarg, NULL);
6728 if (repo_path == NULL)
6729 return got_error_from_errno2("realpath",
6731 got_path_strip_trailing_slashes(repo_path);
6746 if (commit_id_arg != NULL)
6748 "-c option can only be used when creating a tag");
6750 option_conflict('l', 'm');
6753 } else if (argc != 1)
6760 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6764 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6765 "sendfd unveil", NULL) == -1)
6769 cwd = getcwd(NULL, 0);
6771 error = got_error_from_errno("getcwd");
6775 if (repo_path == NULL) {
6776 error = got_worktree_open(&worktree, cwd);
6777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6783 strdup(got_worktree_get_repo_path(worktree));
6784 if (repo_path == NULL)
6785 error = got_error_from_errno("strdup");
6789 repo_path = strdup(cwd);
6790 if (repo_path == NULL) {
6791 error = got_error_from_errno("strdup");
6799 /* Release work tree lock. */
6800 got_worktree_close(worktree);
6803 error = got_repo_open(&repo, repo_path, NULL);
6806 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6809 error = list_tags(repo);
6811 error = get_gitconfig_path(&gitconfig_path);
6814 error = got_repo_open(&repo, repo_path, gitconfig_path);
6818 error = get_author(&tagger, repo, worktree);
6822 /* Release work tree lock. */
6823 got_worktree_close(worktree);
6828 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6833 if (commit_id_arg == NULL) {
6834 struct got_reference *head_ref;
6835 struct got_object_id *commit_id;
6836 error = got_ref_open(&head_ref, repo,
6837 worktree ? got_worktree_get_head_ref_name(worktree)
6841 error = got_ref_resolve(&commit_id, repo, head_ref);
6842 got_ref_close(head_ref);
6845 error = got_object_id_str(&commit_id_str, commit_id);
6851 error = add_tag(repo, tagger, tag_name,
6852 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6856 const struct got_error *close_err = got_repo_close(repo);
6861 got_worktree_close(worktree);
6864 free(gitconfig_path);
6865 free(commit_id_str);
6873 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6878 static const struct got_error *
6879 add_progress(void *arg, unsigned char status, const char *path)
6881 while (path[0] == '/')
6883 printf("%c %s\n", status, path);
6887 static const struct got_error *
6888 cmd_add(int argc, char *argv[])
6890 const struct got_error *error = NULL;
6891 struct got_repository *repo = NULL;
6892 struct got_worktree *worktree = NULL;
6894 struct got_pathlist_head paths;
6895 struct got_pathlist_entry *pe;
6896 int ch, can_recurse = 0, no_ignores = 0;
6900 while ((ch = getopt(argc, argv, "IR")) != -1) {
6918 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6925 cwd = getcwd(NULL, 0);
6927 error = got_error_from_errno("getcwd");
6931 error = got_worktree_open(&worktree, cwd);
6933 if (error->code == GOT_ERR_NOT_WORKTREE)
6934 error = wrap_not_worktree_error(error, "add", cwd);
6938 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6943 error = apply_unveil(got_repo_get_path(repo), 1,
6944 got_worktree_get_root_path(worktree));
6948 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6955 TAILQ_FOREACH(pe, &paths, entry) {
6956 if (asprintf(&ondisk_path, "%s/%s",
6957 got_worktree_get_root_path(worktree),
6959 error = got_error_from_errno("asprintf");
6962 if (lstat(ondisk_path, &sb) == -1) {
6963 if (errno == ENOENT) {
6967 error = got_error_from_errno2("lstat",
6973 if (S_ISDIR(sb.st_mode)) {
6974 error = got_error_msg(GOT_ERR_BAD_PATH,
6975 "adding directories requires -R option");
6981 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6982 NULL, repo, no_ignores);
6985 const struct got_error *close_err = got_repo_close(repo);
6990 got_worktree_close(worktree);
6991 TAILQ_FOREACH(pe, &paths, entry)
6992 free((char *)pe->path);
6993 got_pathlist_free(&paths);
7001 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7002 "path ...\n", getprogname());
7006 static const struct got_error *
7007 print_remove_status(void *arg, unsigned char status,
7008 unsigned char staged_status, const char *path)
7010 while (path[0] == '/')
7012 if (status == GOT_STATUS_NONEXISTENT)
7014 if (status == staged_status && (status == GOT_STATUS_DELETE))
7015 status = GOT_STATUS_NO_CHANGE;
7016 printf("%c%c %s\n", status, staged_status, path);
7020 static const struct got_error *
7021 cmd_remove(int argc, char *argv[])
7023 const struct got_error *error = NULL;
7024 struct got_worktree *worktree = NULL;
7025 struct got_repository *repo = NULL;
7026 const char *status_codes = NULL;
7028 struct got_pathlist_head paths;
7029 struct got_pathlist_entry *pe;
7030 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7031 int ignore_missing_paths = 0;
7035 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7038 delete_local_mods = 1;
7039 ignore_missing_paths = 1;
7048 for (i = 0; i < strlen(optarg); i++) {
7049 switch (optarg[i]) {
7050 case GOT_STATUS_MODIFY:
7051 delete_local_mods = 1;
7053 case GOT_STATUS_MISSING:
7054 ignore_missing_paths = 1;
7057 errx(1, "invalid status code '%c'",
7061 status_codes = optarg;
7073 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7080 cwd = getcwd(NULL, 0);
7082 error = got_error_from_errno("getcwd");
7085 error = got_worktree_open(&worktree, cwd);
7087 if (error->code == GOT_ERR_NOT_WORKTREE)
7088 error = wrap_not_worktree_error(error, "remove", cwd);
7092 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7097 error = apply_unveil(got_repo_get_path(repo), 1,
7098 got_worktree_get_root_path(worktree));
7102 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7109 TAILQ_FOREACH(pe, &paths, entry) {
7110 if (asprintf(&ondisk_path, "%s/%s",
7111 got_worktree_get_root_path(worktree),
7113 error = got_error_from_errno("asprintf");
7116 if (lstat(ondisk_path, &sb) == -1) {
7117 if (errno == ENOENT) {
7121 error = got_error_from_errno2("lstat",
7127 if (S_ISDIR(sb.st_mode)) {
7128 error = got_error_msg(GOT_ERR_BAD_PATH,
7129 "removing directories requires -R option");
7135 error = got_worktree_schedule_delete(worktree, &paths,
7136 delete_local_mods, status_codes, print_remove_status, NULL,
7137 repo, keep_on_disk, ignore_missing_paths);
7140 const struct got_error *close_err = got_repo_close(repo);
7145 got_worktree_close(worktree);
7146 TAILQ_FOREACH(pe, &paths, entry)
7147 free((char *)pe->path);
7148 got_pathlist_free(&paths);
7156 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7157 "[-R] [patchfile]\n", getprogname());
7161 static const struct got_error *
7162 patch_from_stdin(int *patchfd)
7164 const struct got_error *err = NULL;
7166 char *path, buf[BUFSIZ];
7167 sig_t sighup, sigint, sigquit;
7169 err = got_opentemp_named_fd(&path, patchfd,
7170 GOT_TMPDIR_STR "/got-patch");
7176 sighup = signal(SIGHUP, SIG_DFL);
7177 sigint = signal(SIGINT, SIG_DFL);
7178 sigquit = signal(SIGQUIT, SIG_DFL);
7181 r = read(0, buf, sizeof(buf));
7183 err = got_error_from_errno("read");
7188 if (write(*patchfd, buf, r) == -1) {
7189 err = got_error_from_errno("write");
7194 signal(SIGHUP, sighup);
7195 signal(SIGINT, sigint);
7196 signal(SIGQUIT, sigquit);
7198 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7199 err = got_error_from_errno("lseek");
7209 static const struct got_error *
7210 patch_progress(void *arg, const char *old, const char *new,
7211 unsigned char status, const struct got_error *error, long old_from,
7212 long old_lines, long new_from, long new_lines, long offset,
7213 const struct got_error *hunk_err)
7215 const char *path = new == NULL ? old : new;
7217 while (*path == '/')
7221 printf("%c %s\n", status, path);
7224 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7226 if (offset != 0 || hunk_err != NULL) {
7227 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7228 old_lines, new_from, new_lines);
7229 if (hunk_err != NULL)
7230 printf("%s\n", hunk_err->msg);
7232 printf("applied with offset %ld\n", offset);
7238 static const struct got_error *
7239 cmd_patch(int argc, char *argv[])
7241 const struct got_error *error = NULL, *close_error = NULL;
7242 struct got_worktree *worktree = NULL;
7243 struct got_repository *repo = NULL;
7246 int ch, nop = 0, strip = -1, reverse = 0;
7249 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7255 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7257 errx(1, "pathname strip count is %s: %s",
7273 error = patch_from_stdin(&patchfd);
7276 } else if (argc == 1) {
7277 patchfd = open(argv[0], O_RDONLY);
7278 if (patchfd == -1) {
7279 error = got_error_from_errno2("open", argv[0]);
7285 if ((cwd = getcwd(NULL, 0)) == NULL) {
7286 error = got_error_from_errno("getcwd");
7290 error = got_worktree_open(&worktree, cwd);
7294 const char *repo_path = got_worktree_get_repo_path(worktree);
7295 error = got_repo_open(&repo, repo_path, NULL);
7299 error = apply_unveil(got_repo_get_path(repo), 0,
7300 got_worktree_get_root_path(worktree));
7305 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7310 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7311 &patch_progress, NULL, check_cancelled, NULL);
7315 close_error = got_repo_close(repo);
7317 error = close_error;
7319 if (worktree != NULL) {
7320 close_error = got_worktree_close(worktree);
7322 error = close_error;
7331 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7332 "path ...\n", getprogname());
7336 static const struct got_error *
7337 revert_progress(void *arg, unsigned char status, const char *path)
7339 if (status == GOT_STATUS_UNVERSIONED)
7342 while (path[0] == '/')
7344 printf("%c %s\n", status, path);
7348 struct choose_patch_arg {
7349 FILE *patch_script_file;
7353 static const struct got_error *
7354 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7355 int nchanges, const char *action)
7358 size_t linesize = 0;
7362 case GOT_STATUS_ADD:
7363 printf("A %s\n%s this addition? [y/n] ", path, action);
7365 case GOT_STATUS_DELETE:
7366 printf("D %s\n%s this deletion? [y/n] ", path, action);
7368 case GOT_STATUS_MODIFY:
7369 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7370 return got_error_from_errno("fseek");
7371 printf(GOT_COMMIT_SEP_STR);
7372 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7374 if (ferror(patch_file))
7375 return got_error_from_errno("getline");
7376 printf(GOT_COMMIT_SEP_STR);
7377 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7378 path, n, nchanges, action);
7381 return got_error_path(path, GOT_ERR_FILE_STATUS);
7387 static const struct got_error *
7388 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7389 FILE *patch_file, int n, int nchanges)
7391 const struct got_error *err = NULL;
7393 size_t linesize = 0;
7396 struct choose_patch_arg *a = arg;
7398 *choice = GOT_PATCH_CHOICE_NONE;
7400 if (a->patch_script_file) {
7402 err = show_change(status, path, patch_file, n, nchanges,
7406 linelen = getline(&line, &linesize, a->patch_script_file);
7407 if (linelen == -1) {
7408 if (ferror(a->patch_script_file))
7409 return got_error_from_errno("getline");
7412 nl = strchr(line, '\n');
7415 if (strcmp(line, "y") == 0) {
7416 *choice = GOT_PATCH_CHOICE_YES;
7418 } else if (strcmp(line, "n") == 0) {
7419 *choice = GOT_PATCH_CHOICE_NO;
7421 } else if (strcmp(line, "q") == 0 &&
7422 status == GOT_STATUS_MODIFY) {
7423 *choice = GOT_PATCH_CHOICE_QUIT;
7426 printf("invalid response '%s'\n", line);
7431 while (resp != 'y' && resp != 'n' && resp != 'q') {
7432 err = show_change(status, path, patch_file, n, nchanges,
7439 if (status == GOT_STATUS_MODIFY) {
7440 if (resp != 'y' && resp != 'n' && resp != 'q') {
7441 printf("invalid response '%c'\n", resp);
7444 } else if (resp != 'y' && resp != 'n') {
7445 printf("invalid response '%c'\n", resp);
7451 *choice = GOT_PATCH_CHOICE_YES;
7452 else if (resp == 'n')
7453 *choice = GOT_PATCH_CHOICE_NO;
7454 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7455 *choice = GOT_PATCH_CHOICE_QUIT;
7460 static const struct got_error *
7461 cmd_revert(int argc, char *argv[])
7463 const struct got_error *error = NULL;
7464 struct got_worktree *worktree = NULL;
7465 struct got_repository *repo = NULL;
7466 char *cwd = NULL, *path = NULL;
7467 struct got_pathlist_head paths;
7468 struct got_pathlist_entry *pe;
7469 int ch, can_recurse = 0, pflag = 0;
7470 FILE *patch_script_file = NULL;
7471 const char *patch_script_path = NULL;
7472 struct choose_patch_arg cpa;
7476 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7482 patch_script_path = optarg;
7497 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7498 "unveil", NULL) == -1)
7503 if (patch_script_path && !pflag)
7504 errx(1, "-F option can only be used together with -p option");
7506 cwd = getcwd(NULL, 0);
7508 error = got_error_from_errno("getcwd");
7511 error = got_worktree_open(&worktree, cwd);
7513 if (error->code == GOT_ERR_NOT_WORKTREE)
7514 error = wrap_not_worktree_error(error, "revert", cwd);
7518 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7523 if (patch_script_path) {
7524 patch_script_file = fopen(patch_script_path, "re");
7525 if (patch_script_file == NULL) {
7526 error = got_error_from_errno2("fopen",
7531 error = apply_unveil(got_repo_get_path(repo), 1,
7532 got_worktree_get_root_path(worktree));
7536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7543 TAILQ_FOREACH(pe, &paths, entry) {
7544 if (asprintf(&ondisk_path, "%s/%s",
7545 got_worktree_get_root_path(worktree),
7547 error = got_error_from_errno("asprintf");
7550 if (lstat(ondisk_path, &sb) == -1) {
7551 if (errno == ENOENT) {
7555 error = got_error_from_errno2("lstat",
7561 if (S_ISDIR(sb.st_mode)) {
7562 error = got_error_msg(GOT_ERR_BAD_PATH,
7563 "reverting directories requires -R option");
7569 cpa.patch_script_file = patch_script_file;
7570 cpa.action = "revert";
7571 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7572 pflag ? choose_patch : NULL, &cpa, repo);
7574 if (patch_script_file && fclose(patch_script_file) == EOF &&
7576 error = got_error_from_errno2("fclose", patch_script_path);
7578 const struct got_error *close_err = got_repo_close(repo);
7583 got_worktree_close(worktree);
7592 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7593 "[path ...]\n", getprogname());
7597 struct collect_commit_logmsg_arg {
7598 const char *cmdline_log;
7599 const char *prepared_log;
7600 int non_interactive;
7602 const char *worktree_path;
7603 const char *branch_name;
7604 const char *repo_path;
7609 static const struct got_error *
7610 read_prepared_logmsg(char **logmsg, const char *path)
7612 const struct got_error *err = NULL;
7618 memset(&sb, 0, sizeof(sb));
7620 f = fopen(path, "re");
7622 return got_error_from_errno2("fopen", path);
7624 if (fstat(fileno(f), &sb) == -1) {
7625 err = got_error_from_errno2("fstat", path);
7628 if (sb.st_size == 0) {
7629 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7633 *logmsg = malloc(sb.st_size + 1);
7634 if (*logmsg == NULL) {
7635 err = got_error_from_errno("malloc");
7639 r = fread(*logmsg, 1, sb.st_size, f);
7640 if (r != sb.st_size) {
7642 err = got_error_from_errno2("fread", path);
7644 err = got_error(GOT_ERR_IO);
7647 (*logmsg)[sb.st_size] = '\0';
7649 if (fclose(f) == EOF && err == NULL)
7650 err = got_error_from_errno2("fclose", path);
7659 static const struct got_error *
7660 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7663 char *initial_content = NULL;
7664 struct got_pathlist_entry *pe;
7665 const struct got_error *err = NULL;
7666 char *template = NULL;
7667 struct collect_commit_logmsg_arg *a = arg;
7668 int initial_content_len;
7672 /* if a message was specified on the command line, just use it */
7673 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7674 len = strlen(a->cmdline_log) + 1;
7675 *logmsg = malloc(len + 1);
7676 if (*logmsg == NULL)
7677 return got_error_from_errno("malloc");
7678 strlcpy(*logmsg, a->cmdline_log, len);
7680 } else if (a->prepared_log != NULL && a->non_interactive)
7681 return read_prepared_logmsg(logmsg, a->prepared_log);
7683 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7684 return got_error_from_errno("asprintf");
7686 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7690 if (a->prepared_log) {
7692 err = read_prepared_logmsg(&msg, a->prepared_log);
7695 if (write(fd, msg, strlen(msg)) == -1) {
7696 err = got_error_from_errno2("write", a->logmsg_path);
7703 initial_content_len = asprintf(&initial_content,
7704 "\n# changes to be committed on branch %s:\n",
7706 if (initial_content_len == -1) {
7707 err = got_error_from_errno("asprintf");
7711 if (write(fd, initial_content, initial_content_len) == -1) {
7712 err = got_error_from_errno2("write", a->logmsg_path);
7716 TAILQ_FOREACH(pe, commitable_paths, entry) {
7717 struct got_commitable *ct = pe->data;
7718 dprintf(fd, "# %c %s\n",
7719 got_commitable_get_status(ct),
7720 got_commitable_get_path(ct));
7723 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7724 initial_content_len, a->prepared_log ? 0 : 1);
7726 free(initial_content);
7729 if (fd != -1 && close(fd) == -1 && err == NULL)
7730 err = got_error_from_errno2("close", a->logmsg_path);
7732 /* Editor is done; we can now apply unveil(2) */
7734 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7742 static const struct got_error *
7743 cmd_commit(int argc, char *argv[])
7745 const struct got_error *error = NULL;
7746 struct got_worktree *worktree = NULL;
7747 struct got_repository *repo = NULL;
7748 char *cwd = NULL, *id_str = NULL;
7749 struct got_object_id *id = NULL;
7750 const char *logmsg = NULL;
7751 char *prepared_logmsg = NULL;
7752 struct collect_commit_logmsg_arg cl_arg;
7753 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7754 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7755 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7756 struct got_pathlist_head paths;
7759 cl_arg.logmsg_path = NULL;
7761 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7765 option_conflict('F', 'm');
7766 prepared_logmsg = realpath(optarg, NULL);
7767 if (prepared_logmsg == NULL)
7768 return got_error_from_errno2("realpath",
7772 if (prepared_logmsg)
7773 option_conflict('m', 'F');
7777 non_interactive = 1;
7780 allow_bad_symlinks = 1;
7792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7793 "unveil", NULL) == -1)
7796 cwd = getcwd(NULL, 0);
7798 error = got_error_from_errno("getcwd");
7801 error = got_worktree_open(&worktree, cwd);
7803 if (error->code == GOT_ERR_NOT_WORKTREE)
7804 error = wrap_not_worktree_error(error, "commit", cwd);
7808 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7811 if (rebase_in_progress) {
7812 error = got_error(GOT_ERR_REBASING);
7816 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7821 error = get_gitconfig_path(&gitconfig_path);
7824 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7829 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7832 if (merge_in_progress) {
7833 error = got_error(GOT_ERR_MERGE_BUSY);
7837 error = get_author(&author, repo, worktree);
7842 * unveil(2) traverses exec(2); if an editor is used we have
7843 * to apply unveil after the log message has been written.
7845 if (logmsg == NULL || strlen(logmsg) == 0)
7846 error = get_editor(&editor);
7848 error = apply_unveil(got_repo_get_path(repo), 0,
7849 got_worktree_get_root_path(worktree));
7853 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7857 cl_arg.editor = editor;
7858 cl_arg.cmdline_log = logmsg;
7859 cl_arg.prepared_log = prepared_logmsg;
7860 cl_arg.non_interactive = non_interactive;
7861 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7862 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7863 if (!histedit_in_progress) {
7864 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7865 error = got_error(GOT_ERR_COMMIT_BRANCH);
7868 cl_arg.branch_name += 11;
7870 cl_arg.repo_path = got_repo_get_path(repo);
7871 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7872 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7873 print_status, NULL, repo);
7875 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7876 cl_arg.logmsg_path != NULL)
7877 preserve_logmsg = 1;
7881 error = got_object_id_str(&id_str, id);
7884 printf("Created commit %s\n", id_str);
7886 if (preserve_logmsg) {
7887 fprintf(stderr, "%s: log message preserved in %s\n",
7888 getprogname(), cl_arg.logmsg_path);
7889 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7891 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7892 free(cl_arg.logmsg_path);
7894 const struct got_error *close_err = got_repo_close(repo);
7899 got_worktree_close(worktree);
7902 free(gitconfig_path);
7905 free(prepared_logmsg);
7912 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7913 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7914 "[remote-repository]\n", getprogname());
7919 print_load_info(int print_colored, int print_found, int print_trees,
7920 int ncolored, int nfound, int ntrees)
7922 if (print_colored) {
7923 printf("%d commit%s colored", ncolored,
7924 ncolored == 1 ? "" : "s");
7927 printf("%s%d object%s found",
7928 ncolored > 0 ? "; " : "",
7929 nfound, nfound == 1 ? "" : "s");
7932 printf("; %d tree%s scanned", ntrees,
7933 ntrees == 1 ? "" : "s");
7937 struct got_send_progress_arg {
7938 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7945 int last_nobj_total;
7949 int printed_something;
7951 struct got_pathlist_head *delete_branches;
7954 static const struct got_error *
7955 send_progress(void *arg, int ncolored, int nfound, int ntrees,
7956 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
7957 int nobj_written, off_t bytes_sent, const char *refname, int success)
7959 struct got_send_progress_arg *a = arg;
7960 char scaled_packsize[FMT_SCALED_STRSIZE];
7961 char scaled_sent[FMT_SCALED_STRSIZE];
7962 int p_deltify = 0, p_written = 0, p_sent = 0;
7963 int print_colored = 0, print_found = 0, print_trees = 0;
7964 int print_searching = 0, print_total = 0;
7965 int print_deltify = 0, print_written = 0, print_sent = 0;
7967 if (a->verbosity < 0)
7971 const char *status = success ? "accepted" : "rejected";
7974 struct got_pathlist_entry *pe;
7975 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7976 const char *branchname = pe->path;
7977 if (got_path_cmp(branchname, refname,
7978 strlen(branchname), strlen(refname)) == 0) {
7980 a->sent_something = 1;
7986 if (a->printed_something)
7988 printf("Server has %s %s", status, refname);
7989 a->printed_something = 1;
7993 if (a->last_ncolored != ncolored) {
7995 a->last_ncolored = ncolored;
7998 if (a->last_nfound != nfound) {
8001 a->last_nfound = nfound;
8004 if (a->last_ntrees != ntrees) {
8008 a->last_ntrees = ntrees;
8011 if ((print_colored || print_found || print_trees) &&
8014 print_load_info(print_colored, print_found, print_trees,
8015 ncolored, nfound, ntrees);
8016 a->printed_something = 1;
8019 } else if (!a->loading_done) {
8021 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8023 a->loading_done = 1;
8026 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8027 return got_error_from_errno("fmt_scaled");
8028 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8029 return got_error_from_errno("fmt_scaled");
8031 if (a->last_ncommits != ncommits) {
8032 print_searching = 1;
8033 a->last_ncommits = ncommits;
8036 if (a->last_nobj_total != nobj_total) {
8037 print_searching = 1;
8039 a->last_nobj_total = nobj_total;
8042 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8043 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8044 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8045 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8046 return got_error(GOT_ERR_NO_SPACE);
8049 if (nobj_deltify > 0 || nobj_written > 0) {
8050 if (nobj_deltify > 0) {
8051 p_deltify = (nobj_deltify * 100) / nobj_total;
8052 if (p_deltify != a->last_p_deltify) {
8053 a->last_p_deltify = p_deltify;
8054 print_searching = 1;
8059 if (nobj_written > 0) {
8060 p_written = (nobj_written * 100) / nobj_total;
8061 if (p_written != a->last_p_written) {
8062 a->last_p_written = p_written;
8063 print_searching = 1;
8071 if (bytes_sent > 0) {
8072 p_sent = (bytes_sent * 100) / packfile_size;
8073 if (p_sent != a->last_p_sent) {
8074 a->last_p_sent = p_sent;
8075 print_searching = 1;
8081 a->sent_something = 1;
8084 if (print_searching || print_total || print_deltify || print_written ||
8087 if (print_searching)
8088 printf("packing %d reference%s", ncommits,
8089 ncommits == 1 ? "" : "s");
8091 printf("; %d object%s", nobj_total,
8092 nobj_total == 1 ? "" : "s");
8094 printf("; deltify: %d%%", p_deltify);
8096 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8097 scaled_packsize, p_sent);
8098 else if (print_written)
8099 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8100 scaled_packsize, p_written);
8101 if (print_searching || print_total || print_deltify ||
8102 print_written || print_sent) {
8103 a->printed_something = 1;
8109 static const struct got_error *
8110 cmd_send(int argc, char *argv[])
8112 const struct got_error *error = NULL;
8113 char *cwd = NULL, *repo_path = NULL;
8114 const char *remote_name;
8115 char *proto = NULL, *host = NULL, *port = NULL;
8116 char *repo_name = NULL, *server_path = NULL;
8117 const struct got_remote_repo *remotes, *remote = NULL;
8118 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8119 struct got_repository *repo = NULL;
8120 struct got_worktree *worktree = NULL;
8121 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8122 struct got_pathlist_head branches;
8123 struct got_pathlist_head tags;
8124 struct got_reflist_head all_branches;
8125 struct got_reflist_head all_tags;
8126 struct got_pathlist_head delete_args;
8127 struct got_pathlist_head delete_branches;
8128 struct got_reflist_entry *re;
8129 struct got_pathlist_entry *pe;
8130 int i, ch, sendfd = -1, sendstatus;
8132 struct got_send_progress_arg spa;
8133 int verbosity = 0, overwrite_refs = 0;
8134 int send_all_branches = 0, send_all_tags = 0;
8135 struct got_reference *ref = NULL;
8137 TAILQ_INIT(&branches);
8139 TAILQ_INIT(&all_branches);
8140 TAILQ_INIT(&all_tags);
8141 TAILQ_INIT(&delete_args);
8142 TAILQ_INIT(&delete_branches);
8144 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8147 send_all_branches = 1;
8150 error = got_pathlist_append(&branches, optarg, NULL);
8156 error = got_pathlist_append(&delete_args, optarg, NULL);
8164 repo_path = realpath(optarg, NULL);
8165 if (repo_path == NULL)
8166 return got_error_from_errno2("realpath",
8168 got_path_strip_trailing_slashes(repo_path);
8171 error = got_pathlist_append(&tags, optarg, NULL);
8182 else if (verbosity < 3)
8196 if (send_all_branches && !TAILQ_EMPTY(&branches))
8197 option_conflict('a', 'b');
8198 if (send_all_tags && !TAILQ_EMPTY(&tags))
8199 option_conflict('T', 't');
8203 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8205 remote_name = argv[0];
8209 cwd = getcwd(NULL, 0);
8211 error = got_error_from_errno("getcwd");
8215 if (repo_path == NULL) {
8216 error = got_worktree_open(&worktree, cwd);
8217 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8223 strdup(got_worktree_get_repo_path(worktree));
8224 if (repo_path == NULL)
8225 error = got_error_from_errno("strdup");
8229 repo_path = strdup(cwd);
8230 if (repo_path == NULL) {
8231 error = got_error_from_errno("strdup");
8237 error = got_repo_open(&repo, repo_path, NULL);
8242 worktree_conf = got_worktree_get_gotconfig(worktree);
8243 if (worktree_conf) {
8244 got_gotconfig_get_remotes(&nremotes, &remotes,
8246 for (i = 0; i < nremotes; i++) {
8247 if (strcmp(remotes[i].name, remote_name) == 0) {
8248 remote = &remotes[i];
8254 if (remote == NULL) {
8255 repo_conf = got_repo_get_gotconfig(repo);
8257 got_gotconfig_get_remotes(&nremotes, &remotes,
8259 for (i = 0; i < nremotes; i++) {
8260 if (strcmp(remotes[i].name, remote_name) == 0) {
8261 remote = &remotes[i];
8267 if (remote == NULL) {
8268 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8269 for (i = 0; i < nremotes; i++) {
8270 if (strcmp(remotes[i].name, remote_name) == 0) {
8271 remote = &remotes[i];
8276 if (remote == NULL) {
8277 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8281 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8282 &repo_name, remote->send_url);
8286 if (strcmp(proto, "git") == 0) {
8288 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8289 "sendfd dns inet unveil", NULL) == -1)
8292 } else if (strcmp(proto, "git+ssh") == 0 ||
8293 strcmp(proto, "ssh") == 0) {
8295 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8296 "sendfd unveil", NULL) == -1)
8299 } else if (strcmp(proto, "http") == 0 ||
8300 strcmp(proto, "git+http") == 0) {
8301 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8304 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8308 error = got_dial_apply_unveil(proto);
8312 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8316 if (send_all_branches) {
8317 error = got_ref_list(&all_branches, repo, "refs/heads",
8318 got_ref_cmp_by_name, NULL);
8321 TAILQ_FOREACH(re, &all_branches, entry) {
8322 const char *branchname = got_ref_get_name(re->ref);
8323 error = got_pathlist_append(&branches,
8329 } else if (nbranches == 0) {
8330 for (i = 0; i < remote->nsend_branches; i++) {
8331 got_pathlist_append(&branches,
8332 remote->send_branches[i], NULL);
8336 if (send_all_tags) {
8337 error = got_ref_list(&all_tags, repo, "refs/tags",
8338 got_ref_cmp_by_name, NULL);
8341 TAILQ_FOREACH(re, &all_tags, entry) {
8342 const char *tagname = got_ref_get_name(re->ref);
8343 error = got_pathlist_append(&tags,
8352 * To prevent accidents only branches in refs/heads/ can be deleted
8353 * with 'got send -d'.
8354 * Deleting anything else requires local repository access or Git.
8356 TAILQ_FOREACH(pe, &delete_args, entry) {
8357 const char *branchname = pe->path;
8359 struct got_pathlist_entry *new;
8360 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8361 s = strdup(branchname);
8363 error = got_error_from_errno("strdup");
8367 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8368 error = got_error_from_errno("asprintf");
8372 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8373 if (error || new == NULL /* duplicate */)
8380 if (nbranches == 0 && ndelete_branches == 0) {
8381 struct got_reference *head_ref;
8383 error = got_ref_open(&head_ref, repo,
8384 got_worktree_get_head_ref_name(worktree), 0);
8386 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8389 if (got_ref_is_symbolic(head_ref)) {
8390 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8391 got_ref_close(head_ref);
8396 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8404 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8405 port ? ":" : "", port ? port : "");
8407 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8408 server_path, verbosity);
8412 memset(&spa, 0, sizeof(spa));
8413 spa.last_scaled_packsize[0] = '\0';
8414 spa.last_p_deltify = -1;
8415 spa.last_p_written = -1;
8416 spa.verbosity = verbosity;
8417 spa.delete_branches = &delete_branches;
8418 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8419 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8420 check_cancelled, NULL);
8421 if (spa.printed_something)
8425 if (!spa.sent_something && verbosity >= 0)
8426 printf("Already up-to-date\n");
8429 if (kill(sendpid, SIGTERM) == -1)
8430 error = got_error_from_errno("kill");
8431 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8432 error = got_error_from_errno("waitpid");
8434 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8435 error = got_error_from_errno("close");
8437 const struct got_error *close_err = got_repo_close(repo);
8442 got_worktree_close(worktree);
8445 got_pathlist_free(&branches);
8446 got_pathlist_free(&tags);
8447 got_ref_list_free(&all_branches);
8448 got_ref_list_free(&all_tags);
8449 got_pathlist_free(&delete_args);
8450 TAILQ_FOREACH(pe, &delete_branches, entry)
8451 free((char *)pe->path);
8452 got_pathlist_free(&delete_branches);
8464 usage_cherrypick(void)
8466 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8470 static const struct got_error *
8471 cmd_cherrypick(int argc, char *argv[])
8473 const struct got_error *error = NULL;
8474 struct got_worktree *worktree = NULL;
8475 struct got_repository *repo = NULL;
8476 char *cwd = NULL, *commit_id_str = NULL;
8477 struct got_object_id *commit_id = NULL;
8478 struct got_commit_object *commit = NULL;
8479 struct got_object_qid *pid;
8481 struct got_update_progress_arg upa;
8483 while ((ch = getopt(argc, argv, "")) != -1) {
8495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8496 "unveil", NULL) == -1)
8502 cwd = getcwd(NULL, 0);
8504 error = got_error_from_errno("getcwd");
8507 error = got_worktree_open(&worktree, cwd);
8509 if (error->code == GOT_ERR_NOT_WORKTREE)
8510 error = wrap_not_worktree_error(error, "cherrypick",
8515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8520 error = apply_unveil(got_repo_get_path(repo), 0,
8521 got_worktree_get_root_path(worktree));
8525 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8526 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8529 error = got_object_id_str(&commit_id_str, commit_id);
8533 error = got_object_open_as_commit(&commit, repo, commit_id);
8536 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8537 memset(&upa, 0, sizeof(upa));
8538 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
8539 commit_id, repo, update_progress, &upa, check_cancelled,
8544 if (upa.did_something)
8545 printf("Merged commit %s\n", commit_id_str);
8546 print_merge_progress_stats(&upa);
8549 got_object_commit_close(commit);
8550 free(commit_id_str);
8552 got_worktree_close(worktree);
8554 const struct got_error *close_err = got_repo_close(repo);
8564 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8568 static const struct got_error *
8569 cmd_backout(int argc, char *argv[])
8571 const struct got_error *error = NULL;
8572 struct got_worktree *worktree = NULL;
8573 struct got_repository *repo = NULL;
8574 char *cwd = NULL, *commit_id_str = NULL;
8575 struct got_object_id *commit_id = NULL;
8576 struct got_commit_object *commit = NULL;
8577 struct got_object_qid *pid;
8579 struct got_update_progress_arg upa;
8581 while ((ch = getopt(argc, argv, "")) != -1) {
8593 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8594 "unveil", NULL) == -1)
8600 cwd = getcwd(NULL, 0);
8602 error = got_error_from_errno("getcwd");
8605 error = got_worktree_open(&worktree, cwd);
8607 if (error->code == GOT_ERR_NOT_WORKTREE)
8608 error = wrap_not_worktree_error(error, "backout", cwd);
8612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8617 error = apply_unveil(got_repo_get_path(repo), 0,
8618 got_worktree_get_root_path(worktree));
8622 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8623 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8626 error = got_object_id_str(&commit_id_str, commit_id);
8630 error = got_object_open_as_commit(&commit, repo, commit_id);
8633 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8635 error = got_error(GOT_ERR_ROOT_COMMIT);
8639 memset(&upa, 0, sizeof(upa));
8640 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
8641 repo, update_progress, &upa, check_cancelled, NULL);
8645 if (upa.did_something)
8646 printf("Backed out commit %s\n", commit_id_str);
8647 print_merge_progress_stats(&upa);
8650 got_object_commit_close(commit);
8651 free(commit_id_str);
8653 got_worktree_close(worktree);
8655 const struct got_error *close_err = got_repo_close(repo);
8665 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8671 trim_logmsg(char *logmsg, int limit)
8676 len = strlen(logmsg);
8680 nl = strchr(logmsg, '\n');
8685 static const struct got_error *
8686 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8688 const struct got_error *err;
8689 char *logmsg0 = NULL;
8692 err = got_object_commit_get_logmsg(&logmsg0, commit);
8697 while (isspace((unsigned char)s[0]))
8700 *logmsg = strdup(s);
8701 if (*logmsg == NULL) {
8702 err = got_error_from_errno("strdup");
8706 trim_logmsg(*logmsg, limit);
8712 static const struct got_error *
8713 show_rebase_merge_conflict(struct got_object_id *id,
8714 struct got_repository *repo)
8716 const struct got_error *err;
8717 struct got_commit_object *commit = NULL;
8718 char *id_str = NULL, *logmsg = NULL;
8720 err = got_object_open_as_commit(&commit, repo, id);
8724 err = got_object_id_str(&id_str, id);
8730 err = get_short_logmsg(&logmsg, 42, commit);
8734 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8737 got_object_commit_close(commit);
8742 static const struct got_error *
8743 show_rebase_progress(struct got_commit_object *commit,
8744 struct got_object_id *old_id, struct got_object_id *new_id)
8746 const struct got_error *err;
8747 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8749 err = got_object_id_str(&old_id_str, old_id);
8754 err = got_object_id_str(&new_id_str, new_id);
8759 old_id_str[12] = '\0';
8761 new_id_str[12] = '\0';
8763 err = get_short_logmsg(&logmsg, 42, commit);
8767 printf("%s -> %s: %s\n", old_id_str,
8768 new_id_str ? new_id_str : "no-op change", logmsg);
8776 static const struct got_error *
8777 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8778 struct got_reference *branch, struct got_reference *new_base_branch,
8779 struct got_reference *tmp_branch, struct got_repository *repo,
8782 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8783 return got_worktree_rebase_complete(worktree, fileindex,
8784 new_base_branch, tmp_branch, branch, repo, create_backup);
8787 static const struct got_error *
8788 rebase_commit(struct got_pathlist_head *merged_paths,
8789 struct got_worktree *worktree, struct got_fileindex *fileindex,
8790 struct got_reference *tmp_branch,
8791 struct got_object_id *commit_id, struct got_repository *repo)
8793 const struct got_error *error;
8794 struct got_commit_object *commit;
8795 struct got_object_id *new_commit_id;
8797 error = got_object_open_as_commit(&commit, repo, commit_id);
8801 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8802 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8804 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8806 error = show_rebase_progress(commit, commit_id, NULL);
8808 error = show_rebase_progress(commit, commit_id, new_commit_id);
8809 free(new_commit_id);
8812 got_object_commit_close(commit);
8816 struct check_path_prefix_arg {
8817 const char *path_prefix;
8822 static const struct got_error *
8823 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8824 struct got_blob_object *blob2, struct got_object_id *id1,
8825 struct got_object_id *id2, const char *path1, const char *path2,
8826 mode_t mode1, mode_t mode2, struct got_repository *repo)
8828 struct check_path_prefix_arg *a = arg;
8830 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8831 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8832 return got_error(a->errcode);
8837 static const struct got_error *
8838 check_path_prefix(struct got_object_id *parent_id,
8839 struct got_object_id *commit_id, const char *path_prefix,
8840 int errcode, struct got_repository *repo)
8842 const struct got_error *err;
8843 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8844 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8845 struct check_path_prefix_arg cpp_arg;
8847 if (got_path_is_root_dir(path_prefix))
8850 err = got_object_open_as_commit(&commit, repo, commit_id);
8854 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8858 err = got_object_open_as_tree(&tree1, repo,
8859 got_object_commit_get_tree_id(parent_commit));
8863 err = got_object_open_as_tree(&tree2, repo,
8864 got_object_commit_get_tree_id(commit));
8868 cpp_arg.path_prefix = path_prefix;
8869 while (cpp_arg.path_prefix[0] == '/')
8870 cpp_arg.path_prefix++;
8871 cpp_arg.len = strlen(cpp_arg.path_prefix);
8872 cpp_arg.errcode = errcode;
8873 err = got_diff_tree(tree1, tree2, "", "", repo,
8874 check_path_prefix_in_diff, &cpp_arg, 0);
8877 got_object_tree_close(tree1);
8879 got_object_tree_close(tree2);
8881 got_object_commit_close(commit);
8883 got_object_commit_close(parent_commit);
8887 static const struct got_error *
8888 collect_commits(struct got_object_id_queue *commits,
8889 struct got_object_id *initial_commit_id,
8890 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8891 const char *path_prefix, int path_prefix_errcode,
8892 struct got_repository *repo)
8894 const struct got_error *err = NULL;
8895 struct got_commit_graph *graph = NULL;
8896 struct got_object_id *parent_id = NULL;
8897 struct got_object_qid *qid;
8898 struct got_object_id *commit_id = initial_commit_id;
8900 err = got_commit_graph_open(&graph, "/", 1);
8904 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8905 check_cancelled, NULL);
8908 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8909 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8910 check_cancelled, NULL);
8912 if (err->code == GOT_ERR_ITER_COMPLETED) {
8913 err = got_error_msg(GOT_ERR_ANCESTRY,
8914 "ran out of commits to rebase before "
8915 "youngest common ancestor commit has "
8920 err = check_path_prefix(parent_id, commit_id,
8921 path_prefix, path_prefix_errcode, repo);
8925 err = got_object_qid_alloc(&qid, commit_id);
8928 STAILQ_INSERT_HEAD(commits, qid, entry);
8929 commit_id = parent_id;
8933 got_commit_graph_close(graph);
8937 static const struct got_error *
8938 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8940 const struct got_error *err = NULL;
8941 time_t committer_time;
8943 char datebuf[11]; /* YYYY-MM-DD + NUL */
8944 char *author0 = NULL, *author, *smallerthan;
8945 char *logmsg0 = NULL, *logmsg, *newline;
8947 committer_time = got_object_commit_get_committer_time(commit);
8948 if (gmtime_r(&committer_time, &tm) == NULL)
8949 return got_error_from_errno("gmtime_r");
8950 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8951 return got_error(GOT_ERR_NO_SPACE);
8953 author0 = strdup(got_object_commit_get_author(commit));
8954 if (author0 == NULL)
8955 return got_error_from_errno("strdup");
8957 smallerthan = strchr(author, '<');
8958 if (smallerthan && smallerthan[1] != '\0')
8959 author = smallerthan + 1;
8960 author[strcspn(author, "@>")] = '\0';
8962 err = got_object_commit_get_logmsg(&logmsg0, commit);
8966 while (*logmsg == '\n')
8968 newline = strchr(logmsg, '\n');
8972 if (asprintf(brief_str, "%s %s %s",
8973 datebuf, author, logmsg) == -1)
8974 err = got_error_from_errno("asprintf");
8981 static const struct got_error *
8982 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8983 struct got_repository *repo)
8985 const struct got_error *err;
8988 err = got_object_id_str(&id_str, id);
8992 err = got_ref_delete(ref, repo);
8996 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9002 static const struct got_error *
9003 print_backup_ref(const char *branch_name, const char *new_id_str,
9004 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9005 struct got_reflist_object_id_map *refs_idmap,
9006 struct got_repository *repo)
9008 const struct got_error *err = NULL;
9009 struct got_reflist_head *refs;
9010 char *refs_str = NULL;
9011 struct got_object_id *new_commit_id = NULL;
9012 struct got_commit_object *new_commit = NULL;
9013 char *new_commit_brief_str = NULL;
9014 struct got_object_id *yca_id = NULL;
9015 struct got_commit_object *yca_commit = NULL;
9016 char *yca_id_str = NULL, *yca_brief_str = NULL;
9017 char *custom_refs_str;
9019 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9020 return got_error_from_errno("asprintf");
9022 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9023 0, 0, refs_idmap, custom_refs_str);
9027 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9031 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9033 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
9038 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9042 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9046 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9047 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9051 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9052 refs_str ? " (" : "", refs_str ? refs_str : "",
9053 refs_str ? ")" : "", new_commit_brief_str);
9054 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9055 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9059 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9063 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9067 err = got_object_id_str(&yca_id_str, yca_id);
9071 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9073 err = build_refs_str(&refs_str, refs, yca_id, repo);
9077 printf("history forked at %s%s%s%s\n %s\n",
9079 refs_str ? " (" : "", refs_str ? refs_str : "",
9080 refs_str ? ")" : "", yca_brief_str);
9083 free(custom_refs_str);
9084 free(new_commit_id);
9088 free(yca_brief_str);
9090 got_object_commit_close(new_commit);
9092 got_object_commit_close(yca_commit);
9097 static const struct got_error *
9098 process_backup_refs(const char *backup_ref_prefix,
9099 const char *wanted_branch_name,
9100 int delete, struct got_repository *repo)
9102 const struct got_error *err;
9103 struct got_reflist_head refs, backup_refs;
9104 struct got_reflist_entry *re;
9105 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9106 struct got_object_id *old_commit_id = NULL;
9107 char *branch_name = NULL;
9108 struct got_commit_object *old_commit = NULL;
9109 struct got_reflist_object_id_map *refs_idmap = NULL;
9110 int wanted_branch_found = 0;
9113 TAILQ_INIT(&backup_refs);
9115 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9119 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9123 if (wanted_branch_name) {
9124 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9125 wanted_branch_name += 11;
9128 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9129 got_ref_cmp_by_commit_timestamp_descending, repo);
9133 TAILQ_FOREACH(re, &backup_refs, entry) {
9134 const char *refname = got_ref_get_name(re->ref);
9137 err = check_cancelled(NULL);
9141 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9145 err = got_object_open_as_commit(&old_commit, repo,
9150 if (strncmp(backup_ref_prefix, refname,
9151 backup_ref_prefix_len) == 0)
9152 refname += backup_ref_prefix_len;
9154 while (refname[0] == '/')
9157 branch_name = strdup(refname);
9158 if (branch_name == NULL) {
9159 err = got_error_from_errno("strdup");
9162 slash = strrchr(branch_name, '/');
9165 refname += strlen(branch_name) + 1;
9168 if (wanted_branch_name == NULL ||
9169 strcmp(wanted_branch_name, branch_name) == 0) {
9170 wanted_branch_found = 1;
9172 err = delete_backup_ref(re->ref,
9173 old_commit_id, repo);
9175 err = print_backup_ref(branch_name, refname,
9176 old_commit_id, old_commit, refs_idmap,
9183 free(old_commit_id);
9184 old_commit_id = NULL;
9187 got_object_commit_close(old_commit);
9191 if (wanted_branch_name && !wanted_branch_found) {
9192 err = got_error_fmt(GOT_ERR_NOT_REF,
9193 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9197 got_reflist_object_id_map_free(refs_idmap);
9198 got_ref_list_free(&refs);
9199 got_ref_list_free(&backup_refs);
9200 free(old_commit_id);
9203 got_object_commit_close(old_commit);
9207 static const struct got_error *
9208 abort_progress(void *arg, unsigned char status, const char *path)
9211 * Unversioned files should not clutter progress output when
9212 * an operation is aborted.
9214 if (status == GOT_STATUS_UNVERSIONED)
9217 return update_progress(arg, status, path);
9220 static const struct got_error *
9221 cmd_rebase(int argc, char *argv[])
9223 const struct got_error *error = NULL;
9224 struct got_worktree *worktree = NULL;
9225 struct got_repository *repo = NULL;
9226 struct got_fileindex *fileindex = NULL;
9228 struct got_reference *branch = NULL;
9229 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9230 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9231 struct got_object_id *resume_commit_id = NULL;
9232 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9233 struct got_commit_object *commit = NULL;
9234 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9235 int histedit_in_progress = 0, merge_in_progress = 0;
9236 int create_backup = 1, list_backups = 0, delete_backups = 0;
9237 struct got_object_id_queue commits;
9238 struct got_pathlist_head merged_paths;
9239 const struct got_object_id_queue *parent_ids;
9240 struct got_object_qid *qid, *pid;
9241 struct got_update_progress_arg upa;
9243 STAILQ_INIT(&commits);
9244 TAILQ_INIT(&merged_paths);
9245 memset(&upa, 0, sizeof(upa));
9247 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9253 continue_rebase = 1;
9271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9272 "unveil", NULL) == -1)
9277 option_conflict('l', 'a');
9278 if (continue_rebase)
9279 option_conflict('l', 'c');
9281 option_conflict('l', 'X');
9282 if (argc != 0 && argc != 1)
9284 } else if (delete_backups) {
9286 option_conflict('X', 'a');
9287 if (continue_rebase)
9288 option_conflict('X', 'c');
9290 option_conflict('l', 'X');
9291 if (argc != 0 && argc != 1)
9294 if (abort_rebase && continue_rebase)
9296 else if (abort_rebase || continue_rebase) {
9299 } else if (argc != 1)
9303 cwd = getcwd(NULL, 0);
9305 error = got_error_from_errno("getcwd");
9308 error = got_worktree_open(&worktree, cwd);
9310 if (list_backups || delete_backups) {
9311 if (error->code != GOT_ERR_NOT_WORKTREE)
9314 if (error->code == GOT_ERR_NOT_WORKTREE)
9315 error = wrap_not_worktree_error(error,
9321 error = got_repo_open(&repo,
9322 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9326 error = apply_unveil(got_repo_get_path(repo), 0,
9327 worktree ? got_worktree_get_root_path(worktree) : NULL);
9331 if (list_backups || delete_backups) {
9332 error = process_backup_refs(
9333 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9334 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9335 goto done; /* nothing else to do */
9338 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9342 if (histedit_in_progress) {
9343 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9347 error = got_worktree_merge_in_progress(&merge_in_progress,
9351 if (merge_in_progress) {
9352 error = got_error(GOT_ERR_MERGE_BUSY);
9356 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9361 if (!rebase_in_progress) {
9362 error = got_error(GOT_ERR_NOT_REBASING);
9365 error = got_worktree_rebase_continue(&resume_commit_id,
9366 &new_base_branch, &tmp_branch, &branch, &fileindex,
9370 printf("Switching work tree to %s\n",
9371 got_ref_get_symref_target(new_base_branch));
9372 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9373 new_base_branch, abort_progress, &upa);
9376 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9377 print_merge_progress_stats(&upa);
9378 goto done; /* nothing else to do */
9381 if (continue_rebase) {
9382 if (!rebase_in_progress) {
9383 error = got_error(GOT_ERR_NOT_REBASING);
9386 error = got_worktree_rebase_continue(&resume_commit_id,
9387 &new_base_branch, &tmp_branch, &branch, &fileindex,
9392 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9393 resume_commit_id, repo);
9397 yca_id = got_object_id_dup(resume_commit_id);
9398 if (yca_id == NULL) {
9399 error = got_error_from_errno("got_object_id_dup");
9403 error = got_ref_open(&branch, repo, argv[0], 0);
9408 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9412 if (!continue_rebase) {
9413 struct got_object_id *base_commit_id;
9415 base_commit_id = got_worktree_get_base_commit_id(worktree);
9416 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9417 base_commit_id, branch_head_commit_id, 1, repo,
9418 check_cancelled, NULL);
9421 if (yca_id == NULL) {
9422 error = got_error_msg(GOT_ERR_ANCESTRY,
9423 "specified branch shares no common ancestry "
9424 "with work tree's branch");
9428 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9430 if (error->code != GOT_ERR_ANCESTRY)
9434 struct got_pathlist_head paths;
9435 printf("%s is already based on %s\n",
9436 got_ref_get_name(branch),
9437 got_worktree_get_head_ref_name(worktree));
9438 error = switch_head_ref(branch, branch_head_commit_id,
9442 error = got_worktree_set_base_commit_id(worktree, repo,
9443 branch_head_commit_id);
9447 error = got_pathlist_append(&paths, "", NULL);
9450 error = got_worktree_checkout_files(worktree,
9451 &paths, repo, update_progress, &upa,
9452 check_cancelled, NULL);
9453 got_pathlist_free(&paths);
9456 if (upa.did_something) {
9458 error = got_object_id_str(&id_str,
9459 branch_head_commit_id);
9462 printf("Updated to %s: %s\n",
9463 got_worktree_get_head_ref_name(worktree),
9467 printf("Already up-to-date\n");
9468 print_update_progress_stats(&upa);
9473 commit_id = branch_head_commit_id;
9474 error = got_object_open_as_commit(&commit, repo, commit_id);
9478 parent_ids = got_object_commit_get_parent_ids(commit);
9479 pid = STAILQ_FIRST(parent_ids);
9481 error = got_error(GOT_ERR_EMPTY_REBASE);
9484 error = collect_commits(&commits, commit_id, &pid->id,
9485 yca_id, got_worktree_get_path_prefix(worktree),
9486 GOT_ERR_REBASE_PATH, repo);
9487 got_object_commit_close(commit);
9492 if (!continue_rebase) {
9493 error = got_worktree_rebase_prepare(&new_base_branch,
9494 &tmp_branch, &fileindex, worktree, branch, repo);
9499 if (STAILQ_EMPTY(&commits)) {
9500 if (continue_rebase) {
9501 error = rebase_complete(worktree, fileindex,
9502 branch, new_base_branch, tmp_branch, repo,
9506 /* Fast-forward the reference of the branch. */
9507 struct got_object_id *new_head_commit_id;
9509 error = got_ref_resolve(&new_head_commit_id, repo,
9513 error = got_object_id_str(&id_str, new_head_commit_id);
9514 printf("Forwarding %s to commit %s\n",
9515 got_ref_get_name(branch), id_str);
9517 error = got_ref_change_ref(branch,
9518 new_head_commit_id);
9521 /* No backup needed since objects did not change. */
9527 STAILQ_FOREACH(qid, &commits, entry) {
9529 commit_id = &qid->id;
9530 parent_id = pid ? &pid->id : yca_id;
9533 memset(&upa, 0, sizeof(upa));
9534 error = got_worktree_rebase_merge_files(&merged_paths,
9535 worktree, fileindex, parent_id, commit_id, repo,
9536 update_progress, &upa, check_cancelled, NULL);
9540 print_merge_progress_stats(&upa);
9541 if (upa.conflicts > 0 || upa.missing > 0 ||
9542 upa.not_deleted > 0 || upa.unversioned > 0) {
9543 if (upa.conflicts > 0) {
9544 error = show_rebase_merge_conflict(&qid->id,
9549 got_worktree_rebase_pathlist_free(&merged_paths);
9553 error = rebase_commit(&merged_paths, worktree, fileindex,
9554 tmp_branch, commit_id, repo);
9555 got_worktree_rebase_pathlist_free(&merged_paths);
9560 if (upa.conflicts > 0 || upa.missing > 0 ||
9561 upa.not_deleted > 0 || upa.unversioned > 0) {
9562 error = got_worktree_rebase_postpone(worktree, fileindex);
9565 if (upa.conflicts > 0 && upa.missing == 0 &&
9566 upa.not_deleted == 0 && upa.unversioned == 0) {
9567 error = got_error_msg(GOT_ERR_CONFLICTS,
9568 "conflicts must be resolved before rebasing "
9570 } else if (upa.conflicts > 0) {
9571 error = got_error_msg(GOT_ERR_CONFLICTS,
9572 "conflicts must be resolved before rebasing "
9573 "can continue; changes destined for some "
9574 "files were not yet merged and should be "
9575 "merged manually if required before the "
9576 "rebase operation is continued");
9578 error = got_error_msg(GOT_ERR_CONFLICTS,
9579 "changes destined for some files were not "
9580 "yet merged and should be merged manually "
9581 "if required before the rebase operation "
9585 error = rebase_complete(worktree, fileindex, branch,
9586 new_base_branch, tmp_branch, repo, create_backup);
9588 got_object_id_queue_free(&commits);
9589 free(branch_head_commit_id);
9590 free(resume_commit_id);
9593 got_object_commit_close(commit);
9595 got_ref_close(branch);
9596 if (new_base_branch)
9597 got_ref_close(new_base_branch);
9599 got_ref_close(tmp_branch);
9601 got_worktree_close(worktree);
9603 const struct got_error *close_err = got_repo_close(repo);
9611 usage_histedit(void)
9613 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9614 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9619 #define GOT_HISTEDIT_PICK 'p'
9620 #define GOT_HISTEDIT_EDIT 'e'
9621 #define GOT_HISTEDIT_FOLD 'f'
9622 #define GOT_HISTEDIT_DROP 'd'
9623 #define GOT_HISTEDIT_MESG 'm'
9625 static const struct got_histedit_cmd {
9629 } got_histedit_cmds[] = {
9630 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9631 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9632 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9634 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9635 { GOT_HISTEDIT_MESG, "mesg",
9636 "single-line log message for commit above (open editor if empty)" },
9639 struct got_histedit_list_entry {
9640 TAILQ_ENTRY(got_histedit_list_entry) entry;
9641 struct got_object_id *commit_id;
9642 const struct got_histedit_cmd *cmd;
9645 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9647 static const struct got_error *
9648 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9649 FILE *f, struct got_repository *repo)
9651 const struct got_error *err = NULL;
9652 char *logmsg = NULL, *id_str = NULL;
9653 struct got_commit_object *commit = NULL;
9656 err = got_object_open_as_commit(&commit, repo, commit_id);
9660 err = get_short_logmsg(&logmsg, 34, commit);
9664 err = got_object_id_str(&id_str, commit_id);
9668 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9670 err = got_ferror(f, GOT_ERR_IO);
9673 got_object_commit_close(commit);
9679 static const struct got_error *
9680 histedit_write_commit_list(struct got_object_id_queue *commits,
9681 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9682 struct got_repository *repo)
9684 const struct got_error *err = NULL;
9685 struct got_object_qid *qid;
9686 const char *histedit_cmd = NULL;
9688 if (STAILQ_EMPTY(commits))
9689 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9691 STAILQ_FOREACH(qid, commits, entry) {
9692 histedit_cmd = got_histedit_cmds[0].name;
9694 histedit_cmd = "edit";
9695 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9696 histedit_cmd = "fold";
9697 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
9700 if (edit_logmsg_only) {
9701 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9703 err = got_ferror(f, GOT_ERR_IO);
9712 static const struct got_error *
9713 write_cmd_list(FILE *f, const char *branch_name,
9714 struct got_object_id_queue *commits)
9716 const struct got_error *err = NULL;
9720 struct got_object_qid *qid;
9722 qid = STAILQ_FIRST(commits);
9723 err = got_object_id_str(&id_str, &qid->id);
9728 "# Editing the history of branch '%s' starting at\n"
9730 "# Commits will be processed in order from top to "
9731 "bottom of this file.\n", branch_name, id_str);
9733 err = got_ferror(f, GOT_ERR_IO);
9737 n = fprintf(f, "# Available histedit commands:\n");
9739 err = got_ferror(f, GOT_ERR_IO);
9743 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9744 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9745 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9748 err = got_ferror(f, GOT_ERR_IO);
9757 static const struct got_error *
9758 histedit_syntax_error(int lineno)
9760 static char msg[42];
9763 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9765 if (ret == -1 || ret >= sizeof(msg))
9766 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9768 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9771 static const struct got_error *
9772 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9773 char *logmsg, struct got_repository *repo)
9775 const struct got_error *err;
9776 struct got_commit_object *folded_commit = NULL;
9777 char *id_str, *folded_logmsg = NULL;
9779 err = got_object_id_str(&id_str, hle->commit_id);
9783 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9787 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9790 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9791 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9792 folded_logmsg) == -1) {
9793 err = got_error_from_errno("asprintf");
9797 got_object_commit_close(folded_commit);
9799 free(folded_logmsg);
9803 static struct got_histedit_list_entry *
9804 get_folded_commits(struct got_histedit_list_entry *hle)
9806 struct got_histedit_list_entry *prev, *folded = NULL;
9808 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9809 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9810 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9811 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9813 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9819 static const struct got_error *
9820 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9821 struct got_repository *repo)
9823 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9824 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9825 const struct got_error *err = NULL;
9826 struct got_commit_object *commit = NULL;
9829 struct got_histedit_list_entry *folded = NULL;
9831 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9835 folded = get_folded_commits(hle);
9837 while (folded != hle) {
9838 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9839 folded = TAILQ_NEXT(folded, entry);
9842 err = append_folded_commit_msg(&new_msg, folded,
9848 folded = TAILQ_NEXT(folded, entry);
9852 err = got_object_id_str(&id_str, hle->commit_id);
9855 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9858 logmsg_len = asprintf(&new_msg,
9859 "%s\n# original log message of commit %s: %s",
9860 logmsg ? logmsg : "", id_str, orig_logmsg);
9861 if (logmsg_len == -1) {
9862 err = got_error_from_errno("asprintf");
9868 err = got_object_id_str(&id_str, hle->commit_id);
9872 err = got_opentemp_named_fd(&logmsg_path, &fd,
9873 GOT_TMPDIR_STR "/got-logmsg");
9877 write(fd, logmsg, logmsg_len);
9880 err = get_editor(&editor);
9884 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9887 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9890 hle->logmsg = strdup(new_msg);
9891 if (hle->logmsg == NULL)
9892 err = got_error_from_errno("strdup");
9895 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9896 err = got_error_from_errno2("unlink", logmsg_path);
9902 got_object_commit_close(commit);
9906 static const struct got_error *
9907 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9908 FILE *f, struct got_repository *repo)
9910 const struct got_error *err = NULL;
9911 char *line = NULL, *p, *end;
9915 const struct got_histedit_cmd *cmd;
9916 struct got_object_id *commit_id = NULL;
9917 struct got_histedit_list_entry *hle = NULL;
9920 len = getline(&line, &size, f);
9922 const struct got_error *getline_err;
9925 getline_err = got_error_from_errno("getline");
9926 err = got_ferror(f, getline_err->code);
9931 while (isspace((unsigned char)p[0]))
9933 if (p[0] == '#' || p[0] == '\0') {
9939 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9940 cmd = &got_histedit_cmds[i];
9941 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9942 isspace((unsigned char)p[strlen(cmd->name)])) {
9943 p += strlen(cmd->name);
9946 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9951 if (i == nitems(got_histedit_cmds)) {
9952 err = histedit_syntax_error(lineno);
9955 while (isspace((unsigned char)p[0]))
9957 if (cmd->code == GOT_HISTEDIT_MESG) {
9958 if (hle == NULL || hle->logmsg != NULL) {
9959 err = got_error(GOT_ERR_HISTEDIT_CMD);
9963 err = histedit_edit_logmsg(hle, repo);
9967 hle->logmsg = strdup(p);
9968 if (hle->logmsg == NULL) {
9969 err = got_error_from_errno("strdup");
9978 while (end[0] && !isspace((unsigned char)end[0]))
9982 err = got_object_resolve_id_str(&commit_id, repo, p);
9984 /* override error code */
9985 err = histedit_syntax_error(lineno);
9989 hle = malloc(sizeof(*hle));
9991 err = got_error_from_errno("malloc");
9995 hle->commit_id = commit_id;
10000 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10008 static const struct got_error *
10009 histedit_check_script(struct got_histedit_list *histedit_cmds,
10010 struct got_object_id_queue *commits, struct got_repository *repo)
10012 const struct got_error *err = NULL;
10013 struct got_object_qid *qid;
10014 struct got_histedit_list_entry *hle;
10015 static char msg[92];
10018 if (TAILQ_EMPTY(histedit_cmds))
10019 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10020 "histedit script contains no commands");
10021 if (STAILQ_EMPTY(commits))
10022 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10024 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10025 struct got_histedit_list_entry *hle2;
10026 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10029 if (got_object_id_cmp(hle->commit_id,
10030 hle2->commit_id) != 0)
10032 err = got_object_id_str(&id_str, hle->commit_id);
10035 snprintf(msg, sizeof(msg), "commit %s is listed "
10036 "more than once in histedit script", id_str);
10038 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10042 STAILQ_FOREACH(qid, commits, entry) {
10043 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10044 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10048 err = got_object_id_str(&id_str, &qid->id);
10051 snprintf(msg, sizeof(msg),
10052 "commit %s missing from histedit script", id_str);
10054 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10058 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10059 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10060 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10061 "last commit in histedit script cannot be folded");
10066 static const struct got_error *
10067 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10068 const char *path, struct got_object_id_queue *commits,
10069 struct got_repository *repo)
10071 const struct got_error *err = NULL;
10075 err = get_editor(&editor);
10079 if (spawn_editor(editor, path) == -1) {
10080 err = got_error_from_errno("failed spawning editor");
10084 f = fopen(path, "re");
10086 err = got_error_from_errno("fopen");
10089 err = histedit_parse_list(histedit_cmds, f, repo);
10093 err = histedit_check_script(histedit_cmds, commits, repo);
10095 if (f && fclose(f) == EOF && err == NULL)
10096 err = got_error_from_errno("fclose");
10101 static const struct got_error *
10102 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10103 struct got_object_id_queue *, const char *, const char *,
10104 struct got_repository *);
10106 static const struct got_error *
10107 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10108 struct got_object_id_queue *commits, const char *branch_name,
10109 int edit_logmsg_only, int fold_only, int edit_only,
10110 struct got_repository *repo)
10112 const struct got_error *err;
10116 err = got_opentemp_named(&path, &f, "got-histedit");
10120 err = write_cmd_list(f, branch_name, commits);
10124 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10125 fold_only, edit_only, repo);
10129 if (edit_logmsg_only || fold_only || edit_only) {
10131 err = histedit_parse_list(histedit_cmds, f, repo);
10133 if (fclose(f) == EOF) {
10134 err = got_error_from_errno("fclose");
10138 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10140 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10141 err->code != GOT_ERR_HISTEDIT_CMD)
10143 err = histedit_edit_list_retry(histedit_cmds, err,
10144 commits, path, branch_name, repo);
10148 if (f && fclose(f) == EOF && err == NULL)
10149 err = got_error_from_errno("fclose");
10150 if (path && unlink(path) != 0 && err == NULL)
10151 err = got_error_from_errno2("unlink", path);
10156 static const struct got_error *
10157 histedit_save_list(struct got_histedit_list *histedit_cmds,
10158 struct got_worktree *worktree, struct got_repository *repo)
10160 const struct got_error *err = NULL;
10163 struct got_histedit_list_entry *hle;
10164 struct got_commit_object *commit = NULL;
10166 err = got_worktree_get_histedit_script_path(&path, worktree);
10170 f = fopen(path, "we");
10172 err = got_error_from_errno2("fopen", path);
10175 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10176 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10182 int n = fprintf(f, "%c %s\n",
10183 GOT_HISTEDIT_MESG, hle->logmsg);
10185 err = got_ferror(f, GOT_ERR_IO);
10191 if (f && fclose(f) == EOF && err == NULL)
10192 err = got_error_from_errno("fclose");
10195 got_object_commit_close(commit);
10200 histedit_free_list(struct got_histedit_list *histedit_cmds)
10202 struct got_histedit_list_entry *hle;
10204 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10205 TAILQ_REMOVE(histedit_cmds, hle, entry);
10210 static const struct got_error *
10211 histedit_load_list(struct got_histedit_list *histedit_cmds,
10212 const char *path, struct got_repository *repo)
10214 const struct got_error *err = NULL;
10217 f = fopen(path, "re");
10219 err = got_error_from_errno2("fopen", path);
10223 err = histedit_parse_list(histedit_cmds, f, repo);
10225 if (f && fclose(f) == EOF && err == NULL)
10226 err = got_error_from_errno("fclose");
10230 static const struct got_error *
10231 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10232 const struct got_error *edit_err, struct got_object_id_queue *commits,
10233 const char *path, const char *branch_name, struct got_repository *repo)
10235 const struct got_error *err = NULL, *prev_err = edit_err;
10238 while (resp != 'c' && resp != 'r' && resp != 'a') {
10239 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10240 "or (a)bort: ", getprogname(), prev_err->msg);
10245 histedit_free_list(histedit_cmds);
10246 err = histedit_run_editor(histedit_cmds, path, commits,
10249 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10250 err->code != GOT_ERR_HISTEDIT_CMD)
10257 } else if (resp == 'r') {
10258 histedit_free_list(histedit_cmds);
10259 err = histedit_edit_script(histedit_cmds,
10260 commits, branch_name, 0, 0, 0, repo);
10262 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10263 err->code != GOT_ERR_HISTEDIT_CMD)
10270 } else if (resp == 'a') {
10271 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10274 printf("invalid response '%c'\n", resp);
10280 static const struct got_error *
10281 histedit_complete(struct got_worktree *worktree,
10282 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10283 struct got_reference *branch, struct got_repository *repo)
10285 printf("Switching work tree to %s\n",
10286 got_ref_get_symref_target(branch));
10287 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10291 static const struct got_error *
10292 show_histedit_progress(struct got_commit_object *commit,
10293 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10295 const struct got_error *err;
10296 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10298 err = got_object_id_str(&old_id_str, hle->commit_id);
10303 err = got_object_id_str(&new_id_str, new_id);
10308 old_id_str[12] = '\0';
10310 new_id_str[12] = '\0';
10313 logmsg = strdup(hle->logmsg);
10314 if (logmsg == NULL) {
10315 err = got_error_from_errno("strdup");
10318 trim_logmsg(logmsg, 42);
10320 err = get_short_logmsg(&logmsg, 42, commit);
10325 switch (hle->cmd->code) {
10326 case GOT_HISTEDIT_PICK:
10327 case GOT_HISTEDIT_EDIT:
10328 printf("%s -> %s: %s\n", old_id_str,
10329 new_id_str ? new_id_str : "no-op change", logmsg);
10331 case GOT_HISTEDIT_DROP:
10332 case GOT_HISTEDIT_FOLD:
10333 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10345 static const struct got_error *
10346 histedit_commit(struct got_pathlist_head *merged_paths,
10347 struct got_worktree *worktree, struct got_fileindex *fileindex,
10348 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10349 struct got_repository *repo)
10351 const struct got_error *err;
10352 struct got_commit_object *commit;
10353 struct got_object_id *new_commit_id;
10355 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10356 && hle->logmsg == NULL) {
10357 err = histedit_edit_logmsg(hle, repo);
10362 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10366 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10367 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10368 hle->logmsg, repo);
10370 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10372 err = show_histedit_progress(commit, hle, NULL);
10374 err = show_histedit_progress(commit, hle, new_commit_id);
10375 free(new_commit_id);
10378 got_object_commit_close(commit);
10382 static const struct got_error *
10383 histedit_skip_commit(struct got_histedit_list_entry *hle,
10384 struct got_worktree *worktree, struct got_repository *repo)
10386 const struct got_error *error;
10387 struct got_commit_object *commit;
10389 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10394 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10398 error = show_histedit_progress(commit, hle, NULL);
10399 got_object_commit_close(commit);
10403 static const struct got_error *
10404 check_local_changes(void *arg, unsigned char status,
10405 unsigned char staged_status, const char *path,
10406 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10407 struct got_object_id *commit_id, int dirfd, const char *de_name)
10409 int *have_local_changes = arg;
10412 case GOT_STATUS_ADD:
10413 case GOT_STATUS_DELETE:
10414 case GOT_STATUS_MODIFY:
10415 case GOT_STATUS_CONFLICT:
10416 *have_local_changes = 1;
10417 return got_error(GOT_ERR_CANCELLED);
10422 switch (staged_status) {
10423 case GOT_STATUS_ADD:
10424 case GOT_STATUS_DELETE:
10425 case GOT_STATUS_MODIFY:
10426 *have_local_changes = 1;
10427 return got_error(GOT_ERR_CANCELLED);
10435 static const struct got_error *
10436 cmd_histedit(int argc, char *argv[])
10438 const struct got_error *error = NULL;
10439 struct got_worktree *worktree = NULL;
10440 struct got_fileindex *fileindex = NULL;
10441 struct got_repository *repo = NULL;
10443 struct got_reference *branch = NULL;
10444 struct got_reference *tmp_branch = NULL;
10445 struct got_object_id *resume_commit_id = NULL;
10446 struct got_object_id *base_commit_id = NULL;
10447 struct got_object_id *head_commit_id = NULL;
10448 struct got_commit_object *commit = NULL;
10449 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10450 struct got_update_progress_arg upa;
10451 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10452 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10453 int list_backups = 0, delete_backups = 0;
10454 const char *edit_script_path = NULL;
10455 struct got_object_id_queue commits;
10456 struct got_pathlist_head merged_paths;
10457 const struct got_object_id_queue *parent_ids;
10458 struct got_object_qid *pid;
10459 struct got_histedit_list histedit_cmds;
10460 struct got_histedit_list_entry *hle;
10462 STAILQ_INIT(&commits);
10463 TAILQ_INIT(&histedit_cmds);
10464 TAILQ_INIT(&merged_paths);
10465 memset(&upa, 0, sizeof(upa));
10467 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10482 edit_script_path = optarg;
10485 edit_logmsg_only = 1;
10491 delete_backups = 1;
10503 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10504 "unveil", NULL) == -1)
10507 if (abort_edit && continue_edit)
10508 option_conflict('a', 'c');
10509 if (edit_script_path && edit_logmsg_only)
10510 option_conflict('F', 'm');
10511 if (abort_edit && edit_logmsg_only)
10512 option_conflict('a', 'm');
10513 if (continue_edit && edit_logmsg_only)
10514 option_conflict('c', 'm');
10515 if (abort_edit && fold_only)
10516 option_conflict('a', 'f');
10517 if (continue_edit && fold_only)
10518 option_conflict('c', 'f');
10519 if (fold_only && edit_logmsg_only)
10520 option_conflict('f', 'm');
10521 if (edit_script_path && fold_only)
10522 option_conflict('F', 'f');
10523 if (abort_edit && edit_only)
10524 option_conflict('a', 'e');
10525 if (continue_edit && edit_only)
10526 option_conflict('c', 'e');
10527 if (edit_only && edit_logmsg_only)
10528 option_conflict('e', 'm');
10529 if (edit_script_path && edit_only)
10530 option_conflict('F', 'e');
10531 if (list_backups) {
10533 option_conflict('l', 'a');
10535 option_conflict('l', 'c');
10536 if (edit_script_path)
10537 option_conflict('l', 'F');
10538 if (edit_logmsg_only)
10539 option_conflict('l', 'm');
10541 option_conflict('l', 'f');
10543 option_conflict('l', 'e');
10544 if (delete_backups)
10545 option_conflict('l', 'X');
10546 if (argc != 0 && argc != 1)
10548 } else if (delete_backups) {
10550 option_conflict('X', 'a');
10552 option_conflict('X', 'c');
10553 if (edit_script_path)
10554 option_conflict('X', 'F');
10555 if (edit_logmsg_only)
10556 option_conflict('X', 'm');
10558 option_conflict('X', 'f');
10560 option_conflict('X', 'e');
10562 option_conflict('X', 'l');
10563 if (argc != 0 && argc != 1)
10565 } else if (argc != 0)
10569 * This command cannot apply unveil(2) in all cases because the
10570 * user may choose to run an editor to edit the histedit script
10571 * and to edit individual commit log messages.
10572 * unveil(2) traverses exec(2); if an editor is used we have to
10573 * apply unveil after edit script and log messages have been written.
10574 * XXX TODO: Make use of unveil(2) where possible.
10577 cwd = getcwd(NULL, 0);
10579 error = got_error_from_errno("getcwd");
10582 error = got_worktree_open(&worktree, cwd);
10584 if (list_backups || delete_backups) {
10585 if (error->code != GOT_ERR_NOT_WORKTREE)
10588 if (error->code == GOT_ERR_NOT_WORKTREE)
10589 error = wrap_not_worktree_error(error,
10595 if (list_backups || delete_backups) {
10596 error = got_repo_open(&repo,
10597 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10601 error = apply_unveil(got_repo_get_path(repo), 0,
10602 worktree ? got_worktree_get_root_path(worktree) : NULL);
10605 error = process_backup_refs(
10606 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10607 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10608 goto done; /* nothing else to do */
10611 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10616 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10619 if (rebase_in_progress) {
10620 error = got_error(GOT_ERR_REBASING);
10624 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10628 if (merge_in_progress) {
10629 error = got_error(GOT_ERR_MERGE_BUSY);
10633 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10637 if (edit_in_progress && edit_logmsg_only) {
10638 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10639 "histedit operation is in progress in this "
10640 "work tree and must be continued or aborted "
10641 "before the -m option can be used");
10644 if (edit_in_progress && fold_only) {
10645 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10646 "histedit operation is in progress in this "
10647 "work tree and must be continued or aborted "
10648 "before the -f option can be used");
10651 if (edit_in_progress && edit_only) {
10652 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10653 "histedit operation is in progress in this "
10654 "work tree and must be continued or aborted "
10655 "before the -e option can be used");
10659 if (edit_in_progress && abort_edit) {
10660 error = got_worktree_histedit_continue(&resume_commit_id,
10661 &tmp_branch, &branch, &base_commit_id, &fileindex,
10665 printf("Switching work tree to %s\n",
10666 got_ref_get_symref_target(branch));
10667 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10668 branch, base_commit_id, abort_progress, &upa);
10671 printf("Histedit of %s aborted\n",
10672 got_ref_get_symref_target(branch));
10673 print_merge_progress_stats(&upa);
10674 goto done; /* nothing else to do */
10675 } else if (abort_edit) {
10676 error = got_error(GOT_ERR_NOT_HISTEDIT);
10680 if (continue_edit) {
10683 if (!edit_in_progress) {
10684 error = got_error(GOT_ERR_NOT_HISTEDIT);
10688 error = got_worktree_get_histedit_script_path(&path, worktree);
10692 error = histedit_load_list(&histedit_cmds, path, repo);
10697 error = got_worktree_histedit_continue(&resume_commit_id,
10698 &tmp_branch, &branch, &base_commit_id, &fileindex,
10703 error = got_ref_resolve(&head_commit_id, repo, branch);
10707 error = got_object_open_as_commit(&commit, repo,
10711 parent_ids = got_object_commit_get_parent_ids(commit);
10712 pid = STAILQ_FIRST(parent_ids);
10714 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10717 error = collect_commits(&commits, head_commit_id, &pid->id,
10718 base_commit_id, got_worktree_get_path_prefix(worktree),
10719 GOT_ERR_HISTEDIT_PATH, repo);
10720 got_object_commit_close(commit);
10725 if (edit_in_progress) {
10726 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10730 error = got_ref_open(&branch, repo,
10731 got_worktree_get_head_ref_name(worktree), 0);
10735 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10736 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10737 "will not edit commit history of a branch outside "
10738 "the \"refs/heads/\" reference namespace");
10742 error = got_ref_resolve(&head_commit_id, repo, branch);
10743 got_ref_close(branch);
10748 error = got_object_open_as_commit(&commit, repo,
10752 parent_ids = got_object_commit_get_parent_ids(commit);
10753 pid = STAILQ_FIRST(parent_ids);
10755 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10758 error = collect_commits(&commits, head_commit_id, &pid->id,
10759 got_worktree_get_base_commit_id(worktree),
10760 got_worktree_get_path_prefix(worktree),
10761 GOT_ERR_HISTEDIT_PATH, repo);
10762 got_object_commit_close(commit);
10767 if (STAILQ_EMPTY(&commits)) {
10768 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10772 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10773 &base_commit_id, &fileindex, worktree, repo);
10777 if (edit_script_path) {
10778 error = histedit_load_list(&histedit_cmds,
10779 edit_script_path, repo);
10781 got_worktree_histedit_abort(worktree, fileindex,
10782 repo, branch, base_commit_id,
10783 abort_progress, &upa);
10784 print_merge_progress_stats(&upa);
10788 const char *branch_name;
10789 branch_name = got_ref_get_symref_target(branch);
10790 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10792 error = histedit_edit_script(&histedit_cmds, &commits,
10793 branch_name, edit_logmsg_only, fold_only,
10796 got_worktree_histedit_abort(worktree, fileindex,
10797 repo, branch, base_commit_id,
10798 abort_progress, &upa);
10799 print_merge_progress_stats(&upa);
10805 error = histedit_save_list(&histedit_cmds, worktree,
10808 got_worktree_histedit_abort(worktree, fileindex,
10809 repo, branch, base_commit_id,
10810 abort_progress, &upa);
10811 print_merge_progress_stats(&upa);
10817 error = histedit_check_script(&histedit_cmds, &commits, repo);
10821 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10822 if (resume_commit_id) {
10823 if (got_object_id_cmp(hle->commit_id,
10824 resume_commit_id) != 0)
10827 resume_commit_id = NULL;
10828 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10829 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10830 error = histedit_skip_commit(hle, worktree,
10835 struct got_pathlist_head paths;
10836 int have_changes = 0;
10838 TAILQ_INIT(&paths);
10839 error = got_pathlist_append(&paths, "", NULL);
10842 error = got_worktree_status(worktree, &paths,
10843 repo, 0, check_local_changes, &have_changes,
10844 check_cancelled, NULL);
10845 got_pathlist_free(&paths);
10847 if (error->code != GOT_ERR_CANCELLED)
10849 if (sigint_received || sigpipe_received)
10852 if (have_changes) {
10853 error = histedit_commit(NULL, worktree,
10854 fileindex, tmp_branch, hle, repo);
10858 error = got_object_open_as_commit(
10859 &commit, repo, hle->commit_id);
10862 error = show_histedit_progress(commit,
10864 got_object_commit_close(commit);
10873 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10874 error = histedit_skip_commit(hle, worktree, repo);
10880 error = got_object_open_as_commit(&commit, repo,
10884 parent_ids = got_object_commit_get_parent_ids(commit);
10885 pid = STAILQ_FIRST(parent_ids);
10887 error = got_worktree_histedit_merge_files(&merged_paths,
10888 worktree, fileindex, &pid->id, hle->commit_id, repo,
10889 update_progress, &upa, check_cancelled, NULL);
10892 got_object_commit_close(commit);
10895 print_merge_progress_stats(&upa);
10896 if (upa.conflicts > 0 || upa.missing > 0 ||
10897 upa.not_deleted > 0 || upa.unversioned > 0) {
10898 if (upa.conflicts > 0) {
10899 error = show_rebase_merge_conflict(
10900 hle->commit_id, repo);
10904 got_worktree_rebase_pathlist_free(&merged_paths);
10908 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10910 error = got_object_id_str(&id_str, hle->commit_id);
10913 printf("Stopping histedit for amending commit %s\n",
10916 got_worktree_rebase_pathlist_free(&merged_paths);
10917 error = got_worktree_histedit_postpone(worktree,
10922 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10923 error = histedit_skip_commit(hle, worktree, repo);
10929 error = histedit_commit(&merged_paths, worktree, fileindex,
10930 tmp_branch, hle, repo);
10931 got_worktree_rebase_pathlist_free(&merged_paths);
10936 if (upa.conflicts > 0 || upa.missing > 0 ||
10937 upa.not_deleted > 0 || upa.unversioned > 0) {
10938 error = got_worktree_histedit_postpone(worktree, fileindex);
10941 if (upa.conflicts > 0 && upa.missing == 0 &&
10942 upa.not_deleted == 0 && upa.unversioned == 0) {
10943 error = got_error_msg(GOT_ERR_CONFLICTS,
10944 "conflicts must be resolved before histedit "
10946 } else if (upa.conflicts > 0) {
10947 error = got_error_msg(GOT_ERR_CONFLICTS,
10948 "conflicts must be resolved before histedit "
10949 "can continue; changes destined for some "
10950 "files were not yet merged and should be "
10951 "merged manually if required before the "
10952 "histedit operation is continued");
10954 error = got_error_msg(GOT_ERR_CONFLICTS,
10955 "changes destined for some files were not "
10956 "yet merged and should be merged manually "
10957 "if required before the histedit operation "
10961 error = histedit_complete(worktree, fileindex, tmp_branch,
10964 got_object_id_queue_free(&commits);
10965 histedit_free_list(&histedit_cmds);
10966 free(head_commit_id);
10967 free(base_commit_id);
10968 free(resume_commit_id);
10970 got_object_commit_close(commit);
10972 got_ref_close(branch);
10974 got_ref_close(tmp_branch);
10976 got_worktree_close(worktree);
10978 const struct got_error *close_err = got_repo_close(repo);
10986 usage_integrate(void)
10988 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10992 static const struct got_error *
10993 cmd_integrate(int argc, char *argv[])
10995 const struct got_error *error = NULL;
10996 struct got_repository *repo = NULL;
10997 struct got_worktree *worktree = NULL;
10998 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10999 const char *branch_arg = NULL;
11000 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11001 struct got_fileindex *fileindex = NULL;
11002 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11004 struct got_update_progress_arg upa;
11006 while ((ch = getopt(argc, argv, "")) != -1) {
11019 branch_arg = argv[0];
11021 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11022 "unveil", NULL) == -1)
11025 cwd = getcwd(NULL, 0);
11027 error = got_error_from_errno("getcwd");
11031 error = got_worktree_open(&worktree, cwd);
11033 if (error->code == GOT_ERR_NOT_WORKTREE)
11034 error = wrap_not_worktree_error(error, "integrate",
11039 error = check_rebase_or_histedit_in_progress(worktree);
11043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11048 error = apply_unveil(got_repo_get_path(repo), 0,
11049 got_worktree_get_root_path(worktree));
11053 error = check_merge_in_progress(worktree, repo);
11057 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11058 error = got_error_from_errno("asprintf");
11062 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11063 &base_branch_ref, worktree, refname, repo);
11067 refname = strdup(got_ref_get_name(branch_ref));
11068 if (refname == NULL) {
11069 error = got_error_from_errno("strdup");
11070 got_worktree_integrate_abort(worktree, fileindex, repo,
11071 branch_ref, base_branch_ref);
11074 base_refname = strdup(got_ref_get_name(base_branch_ref));
11075 if (base_refname == NULL) {
11076 error = got_error_from_errno("strdup");
11077 got_worktree_integrate_abort(worktree, fileindex, repo,
11078 branch_ref, base_branch_ref);
11082 error = got_ref_resolve(&commit_id, repo, branch_ref);
11086 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11090 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11091 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11092 "specified branch has already been integrated");
11093 got_worktree_integrate_abort(worktree, fileindex, repo,
11094 branch_ref, base_branch_ref);
11098 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11100 if (error->code == GOT_ERR_ANCESTRY)
11101 error = got_error(GOT_ERR_REBASE_REQUIRED);
11102 got_worktree_integrate_abort(worktree, fileindex, repo,
11103 branch_ref, base_branch_ref);
11107 memset(&upa, 0, sizeof(upa));
11108 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11109 branch_ref, base_branch_ref, update_progress, &upa,
11110 check_cancelled, NULL);
11114 printf("Integrated %s into %s\n", refname, base_refname);
11115 print_update_progress_stats(&upa);
11118 const struct got_error *close_err = got_repo_close(repo);
11123 got_worktree_close(worktree);
11125 free(base_commit_id);
11128 free(base_refname);
11135 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11140 static const struct got_error *
11141 cmd_merge(int argc, char *argv[])
11143 const struct got_error *error = NULL;
11144 struct got_worktree *worktree = NULL;
11145 struct got_repository *repo = NULL;
11146 struct got_fileindex *fileindex = NULL;
11147 char *cwd = NULL, *id_str = NULL, *author = NULL;
11148 struct got_reference *branch = NULL, *wt_branch = NULL;
11149 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11150 struct got_object_id *wt_branch_tip = NULL;
11151 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11152 int interrupt_merge = 0;
11153 struct got_update_progress_arg upa;
11154 struct got_object_id *merge_commit_id = NULL;
11155 char *branch_name = NULL;
11157 memset(&upa, 0, sizeof(upa));
11159 while ((ch = getopt(argc, argv, "acn")) != -1) {
11165 continue_merge = 1;
11168 interrupt_merge = 1;
11180 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11181 "unveil", NULL) == -1)
11185 if (abort_merge && continue_merge)
11186 option_conflict('a', 'c');
11187 if (abort_merge || continue_merge) {
11190 } else if (argc != 1)
11193 cwd = getcwd(NULL, 0);
11195 error = got_error_from_errno("getcwd");
11199 error = got_worktree_open(&worktree, cwd);
11201 if (error->code == GOT_ERR_NOT_WORKTREE)
11202 error = wrap_not_worktree_error(error,
11207 error = got_repo_open(&repo,
11208 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
11212 error = apply_unveil(got_repo_get_path(repo), 0,
11213 worktree ? got_worktree_get_root_path(worktree) : NULL);
11217 error = check_rebase_or_histedit_in_progress(worktree);
11221 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11227 if (!merge_in_progress) {
11228 error = got_error(GOT_ERR_NOT_MERGING);
11231 error = got_worktree_merge_continue(&branch_name,
11232 &branch_tip, &fileindex, worktree, repo);
11235 error = got_worktree_merge_abort(worktree, fileindex, repo,
11236 abort_progress, &upa);
11239 printf("Merge of %s aborted\n", branch_name);
11240 goto done; /* nothing else to do */
11243 error = get_author(&author, repo, worktree);
11247 if (continue_merge) {
11248 if (!merge_in_progress) {
11249 error = got_error(GOT_ERR_NOT_MERGING);
11252 error = got_worktree_merge_continue(&branch_name,
11253 &branch_tip, &fileindex, worktree, repo);
11257 error = got_ref_open(&branch, repo, argv[0], 0);
11260 branch_name = strdup(got_ref_get_name(branch));
11261 if (branch_name == NULL) {
11262 error = got_error_from_errno("strdup");
11265 error = got_ref_resolve(&branch_tip, repo, branch);
11270 error = got_ref_open(&wt_branch, repo,
11271 got_worktree_get_head_ref_name(worktree), 0);
11274 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11277 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11278 wt_branch_tip, branch_tip, 0, repo,
11279 check_cancelled, NULL);
11280 if (error && error->code != GOT_ERR_ANCESTRY)
11283 if (!continue_merge) {
11284 error = check_path_prefix(wt_branch_tip, branch_tip,
11285 got_worktree_get_path_prefix(worktree),
11286 GOT_ERR_MERGE_PATH, repo);
11290 error = check_same_branch(wt_branch_tip, branch,
11293 if (error->code != GOT_ERR_ANCESTRY)
11297 static char msg[512];
11298 snprintf(msg, sizeof(msg),
11299 "cannot create a merge commit because "
11300 "%s is based on %s; %s can be integrated "
11301 "with 'got integrate' instead", branch_name,
11302 got_worktree_get_head_ref_name(worktree),
11304 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11308 error = got_worktree_merge_prepare(&fileindex, worktree,
11313 error = got_worktree_merge_branch(worktree, fileindex,
11314 yca_id, branch_tip, repo, update_progress, &upa,
11315 check_cancelled, NULL);
11318 print_merge_progress_stats(&upa);
11319 if (!upa.did_something) {
11320 error = got_worktree_merge_abort(worktree, fileindex,
11321 repo, abort_progress, &upa);
11324 printf("Already up-to-date\n");
11329 if (interrupt_merge) {
11330 error = got_worktree_merge_postpone(worktree, fileindex);
11333 printf("Merge of %s interrupted on request\n", branch_name);
11334 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11335 upa.not_deleted > 0 || upa.unversioned > 0) {
11336 error = got_worktree_merge_postpone(worktree, fileindex);
11339 if (upa.conflicts > 0 && upa.missing == 0 &&
11340 upa.not_deleted == 0 && upa.unversioned == 0) {
11341 error = got_error_msg(GOT_ERR_CONFLICTS,
11342 "conflicts must be resolved before merging "
11344 } else if (upa.conflicts > 0) {
11345 error = got_error_msg(GOT_ERR_CONFLICTS,
11346 "conflicts must be resolved before merging "
11347 "can continue; changes destined for some "
11348 "files were not yet merged and "
11349 "should be merged manually if required before the "
11350 "merge operation is continued");
11352 error = got_error_msg(GOT_ERR_CONFLICTS,
11353 "changes destined for some "
11354 "files were not yet merged and should be "
11355 "merged manually if required before the "
11356 "merge operation is continued");
11360 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11361 fileindex, author, NULL, 1, branch_tip, branch_name,
11362 repo, continue_merge ? print_status : NULL, NULL);
11365 error = got_worktree_merge_complete(worktree, fileindex, repo);
11368 error = got_object_id_str(&id_str, merge_commit_id);
11371 printf("Merged %s into %s: %s\n", branch_name,
11372 got_worktree_get_head_ref_name(worktree),
11378 free(merge_commit_id);
11384 got_ref_close(branch);
11386 got_ref_close(wt_branch);
11388 got_worktree_close(worktree);
11390 const struct got_error *close_err = got_repo_close(repo);
11400 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11401 "[-S] [file-path ...]\n",
11406 static const struct got_error *
11407 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11408 const char *path, struct got_object_id *blob_id,
11409 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11410 int dirfd, const char *de_name)
11412 const struct got_error *err = NULL;
11413 char *id_str = NULL;
11415 if (staged_status != GOT_STATUS_ADD &&
11416 staged_status != GOT_STATUS_MODIFY &&
11417 staged_status != GOT_STATUS_DELETE)
11420 if (staged_status == GOT_STATUS_ADD ||
11421 staged_status == GOT_STATUS_MODIFY)
11422 err = got_object_id_str(&id_str, staged_blob_id);
11424 err = got_object_id_str(&id_str, blob_id);
11428 printf("%s %c %s\n", id_str, staged_status, path);
11433 static const struct got_error *
11434 cmd_stage(int argc, char *argv[])
11436 const struct got_error *error = NULL;
11437 struct got_repository *repo = NULL;
11438 struct got_worktree *worktree = NULL;
11440 struct got_pathlist_head paths;
11441 struct got_pathlist_entry *pe;
11442 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11443 FILE *patch_script_file = NULL;
11444 const char *patch_script_path = NULL;
11445 struct choose_patch_arg cpa;
11447 TAILQ_INIT(&paths);
11449 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11458 patch_script_path = optarg;
11461 allow_bad_symlinks = 1;
11473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11474 "unveil", NULL) == -1)
11477 if (list_stage && (pflag || patch_script_path))
11478 errx(1, "-l option cannot be used with other options");
11479 if (patch_script_path && !pflag)
11480 errx(1, "-F option can only be used together with -p option");
11482 cwd = getcwd(NULL, 0);
11484 error = got_error_from_errno("getcwd");
11488 error = got_worktree_open(&worktree, cwd);
11490 if (error->code == GOT_ERR_NOT_WORKTREE)
11491 error = wrap_not_worktree_error(error, "stage", cwd);
11495 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11500 if (patch_script_path) {
11501 patch_script_file = fopen(patch_script_path, "re");
11502 if (patch_script_file == NULL) {
11503 error = got_error_from_errno2("fopen",
11504 patch_script_path);
11508 error = apply_unveil(got_repo_get_path(repo), 0,
11509 got_worktree_get_root_path(worktree));
11513 error = check_merge_in_progress(worktree, repo);
11517 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11522 error = got_worktree_status(worktree, &paths, repo, 0,
11523 print_stage, NULL, check_cancelled, NULL);
11525 cpa.patch_script_file = patch_script_file;
11526 cpa.action = "stage";
11527 error = got_worktree_stage(worktree, &paths,
11528 pflag ? NULL : print_status, NULL,
11529 pflag ? choose_patch : NULL, &cpa,
11530 allow_bad_symlinks, repo);
11533 if (patch_script_file && fclose(patch_script_file) == EOF &&
11535 error = got_error_from_errno2("fclose", patch_script_path);
11537 const struct got_error *close_err = got_repo_close(repo);
11542 got_worktree_close(worktree);
11543 TAILQ_FOREACH(pe, &paths, entry)
11544 free((char *)pe->path);
11545 got_pathlist_free(&paths);
11551 usage_unstage(void)
11553 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11554 "[file-path ...]\n",
11560 static const struct got_error *
11561 cmd_unstage(int argc, char *argv[])
11563 const struct got_error *error = NULL;
11564 struct got_repository *repo = NULL;
11565 struct got_worktree *worktree = NULL;
11567 struct got_pathlist_head paths;
11568 struct got_pathlist_entry *pe;
11570 struct got_update_progress_arg upa;
11571 FILE *patch_script_file = NULL;
11572 const char *patch_script_path = NULL;
11573 struct choose_patch_arg cpa;
11575 TAILQ_INIT(&paths);
11577 while ((ch = getopt(argc, argv, "pF:")) != -1) {
11583 patch_script_path = optarg;
11595 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11596 "unveil", NULL) == -1)
11599 if (patch_script_path && !pflag)
11600 errx(1, "-F option can only be used together with -p option");
11602 cwd = getcwd(NULL, 0);
11604 error = got_error_from_errno("getcwd");
11608 error = got_worktree_open(&worktree, cwd);
11610 if (error->code == GOT_ERR_NOT_WORKTREE)
11611 error = wrap_not_worktree_error(error, "unstage", cwd);
11615 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11620 if (patch_script_path) {
11621 patch_script_file = fopen(patch_script_path, "re");
11622 if (patch_script_file == NULL) {
11623 error = got_error_from_errno2("fopen",
11624 patch_script_path);
11629 error = apply_unveil(got_repo_get_path(repo), 0,
11630 got_worktree_get_root_path(worktree));
11634 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11638 cpa.patch_script_file = patch_script_file;
11639 cpa.action = "unstage";
11640 memset(&upa, 0, sizeof(upa));
11641 error = got_worktree_unstage(worktree, &paths, update_progress,
11642 &upa, pflag ? choose_patch : NULL, &cpa, repo);
11644 print_merge_progress_stats(&upa);
11646 if (patch_script_file && fclose(patch_script_file) == EOF &&
11648 error = got_error_from_errno2("fclose", patch_script_path);
11650 const struct got_error *close_err = got_repo_close(repo);
11655 got_worktree_close(worktree);
11656 TAILQ_FOREACH(pe, &paths, entry)
11657 free((char *)pe->path);
11658 got_pathlist_free(&paths);
11666 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11667 "arg1 [arg2 ...]\n", getprogname());
11671 static const struct got_error *
11672 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11674 const struct got_error *err;
11675 struct got_blob_object *blob;
11677 err = got_object_open_as_blob(&blob, repo, id, 8192);
11681 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11682 got_object_blob_close(blob);
11686 static const struct got_error *
11687 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11689 const struct got_error *err;
11690 struct got_tree_object *tree;
11693 err = got_object_open_as_tree(&tree, repo, id);
11697 nentries = got_object_tree_get_nentries(tree);
11698 for (i = 0; i < nentries; i++) {
11699 struct got_tree_entry *te;
11701 if (sigint_received || sigpipe_received)
11703 te = got_object_tree_get_entry(tree, i);
11704 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11707 fprintf(outfile, "%s %.7o %s\n", id_str,
11708 got_tree_entry_get_mode(te),
11709 got_tree_entry_get_name(te));
11713 got_object_tree_close(tree);
11718 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
11728 h = (long long)gmtoff / 3600;
11729 m = ((long long)gmtoff - h*3600) / 60;
11730 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
11733 static const struct got_error *
11734 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11736 const struct got_error *err;
11737 struct got_commit_object *commit;
11738 const struct got_object_id_queue *parent_ids;
11739 struct got_object_qid *pid;
11740 char *id_str = NULL;
11741 const char *logmsg = NULL;
11744 err = got_object_open_as_commit(&commit, repo, id);
11748 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11752 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11753 parent_ids = got_object_commit_get_parent_ids(commit);
11754 fprintf(outfile, "numparents %d\n",
11755 got_object_commit_get_nparents(commit));
11756 STAILQ_FOREACH(pid, parent_ids, entry) {
11758 err = got_object_id_str(&pid_str, &pid->id);
11761 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11764 format_gmtoff(gmtoff, sizeof(gmtoff),
11765 got_object_commit_get_author_gmtoff(commit));
11766 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
11767 got_object_commit_get_author(commit),
11768 (long long)got_object_commit_get_author_time(commit),
11771 format_gmtoff(gmtoff, sizeof(gmtoff),
11772 got_object_commit_get_committer_gmtoff(commit));
11773 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
11774 got_object_commit_get_author(commit),
11775 (long long)got_object_commit_get_committer_time(commit),
11778 logmsg = got_object_commit_get_logmsg_raw(commit);
11779 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11780 fprintf(outfile, "%s", logmsg);
11783 got_object_commit_close(commit);
11787 static const struct got_error *
11788 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11790 const struct got_error *err;
11791 struct got_tag_object *tag;
11792 char *id_str = NULL;
11793 const char *tagmsg = NULL;
11796 err = got_object_open_as_tag(&tag, repo, id);
11800 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11804 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11806 switch (got_object_tag_get_object_type(tag)) {
11807 case GOT_OBJ_TYPE_BLOB:
11808 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11809 GOT_OBJ_LABEL_BLOB);
11811 case GOT_OBJ_TYPE_TREE:
11812 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11813 GOT_OBJ_LABEL_TREE);
11815 case GOT_OBJ_TYPE_COMMIT:
11816 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11817 GOT_OBJ_LABEL_COMMIT);
11819 case GOT_OBJ_TYPE_TAG:
11820 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11821 GOT_OBJ_LABEL_TAG);
11827 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11828 got_object_tag_get_name(tag));
11830 format_gmtoff(gmtoff, sizeof(gmtoff),
11831 got_object_tag_get_tagger_gmtoff(tag));
11832 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
11833 got_object_tag_get_tagger(tag),
11834 (long long)got_object_tag_get_tagger_time(tag),
11837 tagmsg = got_object_tag_get_message(tag);
11838 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11839 fprintf(outfile, "%s", tagmsg);
11842 got_object_tag_close(tag);
11846 static const struct got_error *
11847 cmd_cat(int argc, char *argv[])
11849 const struct got_error *error;
11850 struct got_repository *repo = NULL;
11851 struct got_worktree *worktree = NULL;
11852 char *cwd = NULL, *repo_path = NULL, *label = NULL;
11853 const char *commit_id_str = NULL;
11854 struct got_object_id *id = NULL, *commit_id = NULL;
11855 struct got_commit_object *commit = NULL;
11856 int ch, obj_type, i, force_path = 0;
11857 struct got_reflist_head refs;
11862 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11867 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11870 commit_id_str = optarg;
11873 repo_path = realpath(optarg, NULL);
11874 if (repo_path == NULL)
11875 return got_error_from_errno2("realpath",
11877 got_path_strip_trailing_slashes(repo_path);
11891 cwd = getcwd(NULL, 0);
11893 error = got_error_from_errno("getcwd");
11897 if (repo_path == NULL) {
11898 error = got_worktree_open(&worktree, cwd);
11899 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11902 repo_path = strdup(
11903 got_worktree_get_repo_path(worktree));
11904 if (repo_path == NULL) {
11905 error = got_error_from_errno("strdup");
11909 /* Release work tree lock. */
11910 got_worktree_close(worktree);
11915 if (repo_path == NULL) {
11916 repo_path = strdup(cwd);
11917 if (repo_path == NULL)
11918 return got_error_from_errno("strdup");
11921 error = got_repo_open(&repo, repo_path, NULL);
11926 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11930 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11934 if (commit_id_str == NULL)
11935 commit_id_str = GOT_REF_HEAD;
11936 error = got_repo_match_object_id(&commit_id, NULL,
11937 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11941 error = got_object_open_as_commit(&commit, repo, commit_id);
11945 for (i = 0; i < argc; i++) {
11947 error = got_object_id_by_path(&id, repo, commit,
11952 error = got_repo_match_object_id(&id, &label, argv[i],
11953 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11956 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11957 error->code != GOT_ERR_NOT_REF)
11959 error = got_object_id_by_path(&id, repo,
11966 error = got_object_get_type(&obj_type, repo, id);
11970 switch (obj_type) {
11971 case GOT_OBJ_TYPE_BLOB:
11972 error = cat_blob(id, repo, stdout);
11974 case GOT_OBJ_TYPE_TREE:
11975 error = cat_tree(id, repo, stdout);
11977 case GOT_OBJ_TYPE_COMMIT:
11978 error = cat_commit(id, repo, stdout);
11980 case GOT_OBJ_TYPE_TAG:
11981 error = cat_tag(id, repo, stdout);
11984 error = got_error(GOT_ERR_OBJ_TYPE);
11999 got_object_commit_close(commit);
12001 got_worktree_close(worktree);
12003 const struct got_error *close_err = got_repo_close(repo);
12007 got_ref_list_free(&refs);
12014 fprintf(stderr, "usage: %s info [path ...]\n",
12019 static const struct got_error *
12020 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12021 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12022 struct got_object_id *commit_id)
12024 const struct got_error *err = NULL;
12025 char *id_str = NULL;
12027 struct tm mytm, *tm;
12028 struct got_pathlist_head *paths = arg;
12029 struct got_pathlist_entry *pe;
12032 * Clear error indication from any of the path arguments which
12033 * would cause this file index entry to be displayed.
12035 TAILQ_FOREACH(pe, paths, entry) {
12036 if (got_path_cmp(path, pe->path, strlen(path),
12037 pe->path_len) == 0 ||
12038 got_path_is_child(path, pe->path, pe->path_len))
12039 pe->data = NULL; /* no error */
12042 printf(GOT_COMMIT_SEP_STR);
12044 printf("symlink: %s\n", path);
12045 else if (S_ISREG(mode)) {
12046 printf("file: %s\n", path);
12047 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12048 } else if (S_ISDIR(mode))
12049 printf("directory: %s\n", path);
12051 printf("something: %s\n", path);
12053 tm = localtime_r(&mtime, &mytm);
12056 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12057 return got_error(GOT_ERR_NO_SPACE);
12058 printf("timestamp: %s\n", datebuf);
12061 err = got_object_id_str(&id_str, blob_id);
12064 printf("based on blob: %s\n", id_str);
12068 if (staged_blob_id) {
12069 err = got_object_id_str(&id_str, staged_blob_id);
12072 printf("based on staged blob: %s\n", id_str);
12077 err = got_object_id_str(&id_str, commit_id);
12080 printf("based on commit: %s\n", id_str);
12087 static const struct got_error *
12088 cmd_info(int argc, char *argv[])
12090 const struct got_error *error = NULL;
12091 struct got_worktree *worktree = NULL;
12092 char *cwd = NULL, *id_str = NULL;
12093 struct got_pathlist_head paths;
12094 struct got_pathlist_entry *pe;
12095 char *uuidstr = NULL;
12096 int ch, show_files = 0;
12098 TAILQ_INIT(&paths);
12100 while ((ch = getopt(argc, argv, "")) != -1) {
12112 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12116 cwd = getcwd(NULL, 0);
12118 error = got_error_from_errno("getcwd");
12122 error = got_worktree_open(&worktree, cwd);
12124 if (error->code == GOT_ERR_NOT_WORKTREE)
12125 error = wrap_not_worktree_error(error, "info", cwd);
12129 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12134 error = get_worktree_paths_from_argv(&paths, argc, argv,
12141 error = got_object_id_str(&id_str,
12142 got_worktree_get_base_commit_id(worktree));
12146 error = got_worktree_get_uuid(&uuidstr, worktree);
12150 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12151 printf("work tree base commit: %s\n", id_str);
12152 printf("work tree path prefix: %s\n",
12153 got_worktree_get_path_prefix(worktree));
12154 printf("work tree branch reference: %s\n",
12155 got_worktree_get_head_ref_name(worktree));
12156 printf("work tree UUID: %s\n", uuidstr);
12157 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12160 struct got_pathlist_entry *pe;
12161 TAILQ_FOREACH(pe, &paths, entry) {
12162 if (pe->path_len == 0)
12165 * Assume this path will fail. This will be corrected
12166 * in print_path_info() in case the path does suceeed.
12168 pe->data = (void *)got_error_path(pe->path,
12171 error = got_worktree_path_info(worktree, &paths,
12172 print_path_info, &paths, check_cancelled, NULL);
12175 TAILQ_FOREACH(pe, &paths, entry) {
12176 if (pe->data != NULL) {
12177 error = pe->data; /* bad path */
12183 TAILQ_FOREACH(pe, &paths, entry)
12184 free((char *)pe->path);
12185 got_pathlist_free(&paths);