2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static volatile sig_atomic_t sigint_received;
64 static volatile sig_atomic_t sigpipe_received;
67 catch_sigint(int signo)
73 catch_sigpipe(int signo)
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
83 const char *cmd_alias;
86 __dead static void usage(int, int);
87 __dead static void usage_init(void);
88 __dead static void usage_import(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_fetch(void);
91 __dead static void usage_checkout(void);
92 __dead static void usage_update(void);
93 __dead static void usage_log(void);
94 __dead static void usage_diff(void);
95 __dead static void usage_blame(void);
96 __dead static void usage_tree(void);
97 __dead static void usage_status(void);
98 __dead static void usage_ref(void);
99 __dead static void usage_branch(void);
100 __dead static void usage_tag(void);
101 __dead static void usage_add(void);
102 __dead static void usage_remove(void);
103 __dead static void usage_revert(void);
104 __dead static void usage_commit(void);
105 __dead static void usage_cherrypick(void);
106 __dead static void usage_backout(void);
107 __dead static void usage_rebase(void);
108 __dead static void usage_histedit(void);
109 __dead static void usage_integrate(void);
110 __dead static void usage_stage(void);
111 __dead static void usage_unstage(void);
112 __dead static void usage_cat(void);
113 __dead static void usage_info(void);
115 static const struct got_error* cmd_init(int, char *[]);
116 static const struct got_error* cmd_import(int, char *[]);
117 static const struct got_error* cmd_clone(int, char *[]);
118 static const struct got_error* cmd_fetch(int, char *[]);
119 static const struct got_error* cmd_checkout(int, char *[]);
120 static const struct got_error* cmd_update(int, char *[]);
121 static const struct got_error* cmd_log(int, char *[]);
122 static const struct got_error* cmd_diff(int, char *[]);
123 static const struct got_error* cmd_blame(int, char *[]);
124 static const struct got_error* cmd_tree(int, char *[]);
125 static const struct got_error* cmd_status(int, char *[]);
126 static const struct got_error* cmd_ref(int, char *[]);
127 static const struct got_error* cmd_branch(int, char *[]);
128 static const struct got_error* cmd_tag(int, char *[]);
129 static const struct got_error* cmd_add(int, char *[]);
130 static const struct got_error* cmd_remove(int, char *[]);
131 static const struct got_error* cmd_revert(int, char *[]);
132 static const struct got_error* cmd_commit(int, char *[]);
133 static const struct got_error* cmd_cherrypick(int, char *[]);
134 static const struct got_error* cmd_backout(int, char *[]);
135 static const struct got_error* cmd_rebase(int, char *[]);
136 static const struct got_error* cmd_histedit(int, char *[]);
137 static const struct got_error* cmd_integrate(int, char *[]);
138 static const struct got_error* cmd_stage(int, char *[]);
139 static const struct got_error* cmd_unstage(int, char *[]);
140 static const struct got_error* cmd_cat(int, char *[]);
141 static const struct got_error* cmd_info(int, char *[]);
143 static struct got_cmd got_commands[] = {
144 { "init", cmd_init, usage_init, "" },
145 { "import", cmd_import, usage_import, "im" },
146 { "clone", cmd_clone, usage_clone, "cl" },
147 { "fetch", cmd_fetch, usage_fetch, "fe" },
148 { "checkout", cmd_checkout, usage_checkout, "co" },
149 { "update", cmd_update, usage_update, "up" },
150 { "log", cmd_log, usage_log, "" },
151 { "diff", cmd_diff, usage_diff, "di" },
152 { "blame", cmd_blame, usage_blame, "bl" },
153 { "tree", cmd_tree, usage_tree, "tr" },
154 { "status", cmd_status, usage_status, "st" },
155 { "ref", cmd_ref, usage_ref, "" },
156 { "branch", cmd_branch, usage_branch, "br" },
157 { "tag", cmd_tag, usage_tag, "" },
158 { "add", cmd_add, usage_add, "" },
159 { "remove", cmd_remove, usage_remove, "rm" },
160 { "revert", cmd_revert, usage_revert, "rv" },
161 { "commit", cmd_commit, usage_commit, "ci" },
162 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 { "backout", cmd_backout, usage_backout, "bo" },
164 { "rebase", cmd_rebase, usage_rebase, "rb" },
165 { "histedit", cmd_histedit, usage_histedit, "he" },
166 { "integrate", cmd_integrate, usage_integrate,"ig" },
167 { "stage", cmd_stage, usage_stage, "sg" },
168 { "unstage", cmd_unstage, usage_unstage, "ug" },
169 { "cat", cmd_cat, usage_cat, "" },
170 { "info", cmd_info, usage_info, "" },
174 list_commands(FILE *fp)
178 fprintf(fp, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(fp, " %s", cmd->cmd_name);
187 option_conflict(char a, char b)
189 errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 main(int argc, char *argv[])
198 int hflag = 0, Vflag = 0;
199 static struct option longopts[] = {
200 { "version", no_argument, NULL, 'V' },
204 setlocale(LC_CTYPE, "");
206 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
226 got_version_print_str();
231 usage(hflag, hflag ? 0 : 1);
233 signal(SIGINT, catch_sigint);
234 signal(SIGPIPE, catch_sigpipe);
236 for (i = 0; i < nitems(got_commands); i++) {
237 const struct got_error *error;
239 cmd = &got_commands[i];
241 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 strcmp(cmd->cmd_alias, argv[0]) != 0)
246 got_commands[i].cmd_usage();
248 error = got_commands[i].cmd_main(argc, argv);
249 if (error && error->code != GOT_ERR_CANCELLED &&
250 error->code != GOT_ERR_PRIVSEP_EXIT &&
251 !(sigpipe_received &&
252 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
254 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
262 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 list_commands(stderr);
268 usage(int hflag, int status)
270 FILE *fp = (status == 0) ? stdout : stderr;
272 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
279 static const struct got_error *
280 get_editor(char **abspath)
282 const struct got_error *err = NULL;
287 editor = getenv("VISUAL");
289 editor = getenv("EDITOR");
292 err = got_path_find_prog(abspath, editor);
297 if (*abspath == NULL) {
298 *abspath = strdup("/bin/ed");
299 if (*abspath == NULL)
300 return got_error_from_errno("strdup");
306 static const struct got_error *
307 apply_unveil(const char *repo_path, int repo_read_only,
308 const char *worktree_path)
310 const struct got_error *err;
313 if (unveil("gmon.out", "rwc") != 0)
314 return got_error_from_errno2("unveil", "gmon.out");
316 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 return got_error_from_errno2("unveil", repo_path);
319 if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 return got_error_from_errno2("unveil", worktree_path);
322 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 err = got_privsep_unveil_exec_helpers();
329 if (unveil(NULL, NULL) != 0)
330 return got_error_from_errno("unveil");
338 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 static const struct got_error *
343 cmd_init(int argc, char *argv[])
345 const struct got_error *error = NULL;
346 char *repo_path = NULL;
349 while ((ch = getopt(argc, argv, "")) != -1) {
361 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
367 repo_path = strdup(argv[0]);
368 if (repo_path == NULL)
369 return got_error_from_errno("strdup");
371 got_path_strip_trailing_slashes(repo_path);
373 error = got_path_mkdir(repo_path);
375 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 error = apply_unveil(repo_path, 0, NULL);
382 error = got_repo_init(repo_path);
391 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 "[-r repository-path] [-I pattern] path\n", getprogname());
397 spawn_editor(const char *editor, const char *file)
400 sig_t sighup, sigint, sigquit;
403 sighup = signal(SIGHUP, SIG_IGN);
404 sigint = signal(SIGINT, SIG_IGN);
405 sigquit = signal(SIGQUIT, SIG_IGN);
407 switch (pid = fork()) {
411 execl(editor, editor, file, (char *)NULL);
415 while (waitpid(pid, &st, 0) == -1)
420 (void)signal(SIGHUP, sighup);
421 (void)signal(SIGINT, sigint);
422 (void)signal(SIGQUIT, sigquit);
424 if (!WIFEXITED(st)) {
429 return WEXITSTATUS(st);
432 static const struct got_error *
433 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 const char *initial_content, size_t initial_content_len, int check_comments)
436 const struct got_error *err = NULL;
442 size_t len, logmsg_len;
443 char *initial_content_stripped = NULL, *buf = NULL, *s;
447 if (stat(logmsg_path, &st) == -1)
448 return got_error_from_errno2("stat", logmsg_path);
450 if (spawn_editor(editor, logmsg_path) == -1)
451 return got_error_from_errno("failed spawning editor");
453 if (stat(logmsg_path, &st2) == -1)
454 return got_error_from_errno("stat");
456 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
457 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
458 "no changes made to commit message, aborting");
461 * Set up a stripped version of the initial content without comments
462 * and blank lines. We need this in order to check if the message
463 * has in fact been edited.
465 initial_content_stripped = malloc(initial_content_len + 1);
466 if (initial_content_stripped == NULL)
467 return got_error_from_errno("malloc");
468 initial_content_stripped[0] = '\0';
470 buf = strdup(initial_content);
472 err = got_error_from_errno("strdup");
477 while ((line = strsep(&s, "\n")) != NULL) {
478 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
479 continue; /* remove comments and leading empty lines */
480 len = strlcat(initial_content_stripped, line,
481 initial_content_len + 1);
482 if (len >= initial_content_len + 1) {
483 err = got_error(GOT_ERR_NO_SPACE);
487 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
488 initial_content_stripped[len - 1] = '\0';
492 logmsg_len = st2.st_size;
493 *logmsg = malloc(logmsg_len + 1);
495 return got_error_from_errno("malloc");
498 fp = fopen(logmsg_path, "r");
500 err = got_error_from_errno("fopen");
505 while ((linelen = getline(&line, &linesize, fp)) != -1) {
506 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
507 continue; /* remove comments and leading empty lines */
508 len = strlcat(*logmsg, line, logmsg_len + 1);
509 if (len >= logmsg_len + 1) {
510 err = got_error(GOT_ERR_NO_SPACE);
516 err = got_ferror(fp, GOT_ERR_IO);
519 while (len > 0 && (*logmsg)[len - 1] == '\n') {
520 (*logmsg)[len - 1] = '\0';
525 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
526 "commit message cannot be empty, aborting");
529 if (check_comments && strcmp(*logmsg, initial_content_stripped) == 0)
530 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 "no changes made to commit message, aborting");
533 free(initial_content_stripped);
535 if (fp && fclose(fp) == EOF && err == NULL)
536 err = got_error_from_errno("fclose");
544 static const struct got_error *
545 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
546 const char *path_dir, const char *branch_name)
548 char *initial_content = NULL;
549 const struct got_error *err = NULL;
550 int initial_content_len;
553 initial_content_len = asprintf(&initial_content,
554 "\n# %s to be imported to branch %s\n", path_dir,
556 if (initial_content_len == -1)
557 return got_error_from_errno("asprintf");
559 err = got_opentemp_named_fd(logmsg_path, &fd,
560 GOT_TMPDIR_STR "/got-importmsg");
564 if (write(fd, initial_content, initial_content_len) == -1) {
565 err = got_error_from_errno2("write", *logmsg_path);
569 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
570 initial_content_len, 1);
572 if (fd != -1 && close(fd) == -1 && err == NULL)
573 err = got_error_from_errno2("close", *logmsg_path);
574 free(initial_content);
582 static const struct got_error *
583 import_progress(void *arg, const char *path)
585 printf("A %s\n", path);
589 static const struct got_error *
590 get_author(char **author, struct got_repository *repo,
591 struct got_worktree *worktree)
593 const struct got_error *err = NULL;
594 const char *got_author = NULL, *name, *email;
595 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
600 worktree_conf = got_worktree_get_gotconfig(worktree);
601 repo_conf = got_repo_get_gotconfig(repo);
604 * Priority of potential author information sources, from most
605 * significant to least significant:
606 * 1) work tree's .got/got.conf file
607 * 2) repository's got.conf file
608 * 3) repository's git config file
609 * 4) environment variables
610 * 5) global git config files (in user's home directory or /etc)
614 got_author = got_gotconfig_get_author(worktree_conf);
615 if (got_author == NULL)
616 got_author = got_gotconfig_get_author(repo_conf);
617 if (got_author == NULL) {
618 name = got_repo_get_gitconfig_author_name(repo);
619 email = got_repo_get_gitconfig_author_email(repo);
621 if (asprintf(author, "%s <%s>", name, email) == -1)
622 return got_error_from_errno("asprintf");
626 got_author = getenv("GOT_AUTHOR");
627 if (got_author == NULL) {
628 name = got_repo_get_global_gitconfig_author_name(repo);
629 email = got_repo_get_global_gitconfig_author_email(
632 if (asprintf(author, "%s <%s>", name, email)
634 return got_error_from_errno("asprintf");
637 /* TODO: Look up user in password database? */
638 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
642 *author = strdup(got_author);
644 return got_error_from_errno("strdup");
647 * Really dumb email address check; we're only doing this to
648 * avoid git's object parser breaking on commits we create.
650 while (*got_author && *got_author != '<')
652 if (*got_author != '<') {
653 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
656 while (*got_author && *got_author != '@')
658 if (*got_author != '@') {
659 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
662 while (*got_author && *got_author != '>')
664 if (*got_author != '>')
665 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
674 static const struct got_error *
675 get_gitconfig_path(char **gitconfig_path)
677 const char *homedir = getenv("HOME");
679 *gitconfig_path = NULL;
681 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
682 return got_error_from_errno("asprintf");
688 static const struct got_error *
689 cmd_import(int argc, char *argv[])
691 const struct got_error *error = NULL;
692 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
693 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
694 const char *branch_name = "main";
695 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
696 struct got_repository *repo = NULL;
697 struct got_reference *branch_ref = NULL, *head_ref = NULL;
698 struct got_object_id *new_commit_id = NULL;
700 struct got_pathlist_head ignores;
701 struct got_pathlist_entry *pe;
702 int preserve_logmsg = 0;
704 TAILQ_INIT(&ignores);
706 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
709 branch_name = optarg;
712 logmsg = strdup(optarg);
713 if (logmsg == NULL) {
714 error = got_error_from_errno("strdup");
719 repo_path = realpath(optarg, NULL);
720 if (repo_path == NULL) {
721 error = got_error_from_errno2("realpath",
727 if (optarg[0] == '\0')
729 error = got_pathlist_insert(&pe, &ignores, optarg,
744 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
752 if (repo_path == NULL) {
753 repo_path = getcwd(NULL, 0);
754 if (repo_path == NULL)
755 return got_error_from_errno("getcwd");
757 got_path_strip_trailing_slashes(repo_path);
758 error = get_gitconfig_path(&gitconfig_path);
761 error = got_repo_open(&repo, repo_path, gitconfig_path);
765 error = get_author(&author, repo, NULL);
770 * Don't let the user create a branch name with a leading '-'.
771 * While technically a valid reference name, this case is usually
772 * an unintended typo.
774 if (branch_name[0] == '-')
775 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
777 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
778 error = got_error_from_errno("asprintf");
782 error = got_ref_open(&branch_ref, repo, refname, 0);
784 if (error->code != GOT_ERR_NOT_REF)
787 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
788 "import target branch already exists");
792 path_dir = realpath(argv[0], NULL);
793 if (path_dir == NULL) {
794 error = got_error_from_errno2("realpath", argv[0]);
797 got_path_strip_trailing_slashes(path_dir);
800 * unveil(2) traverses exec(2); if an editor is used we have
801 * to apply unveil after the log message has been written.
803 if (logmsg == NULL || strlen(logmsg) == 0) {
804 error = get_editor(&editor);
808 error = collect_import_msg(&logmsg, &logmsg_path, editor,
811 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
818 if (unveil(path_dir, "r") != 0) {
819 error = got_error_from_errno2("unveil", path_dir);
825 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
832 error = got_repo_import(&new_commit_id, path_dir, logmsg,
833 author, &ignores, repo, import_progress, NULL);
840 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
847 error = got_ref_write(branch_ref, repo);
854 error = got_object_id_str(&id_str, new_commit_id);
861 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
863 if (error->code != GOT_ERR_NOT_REF) {
869 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
877 error = got_ref_write(head_ref, repo);
885 printf("Created branch %s with commit %s\n",
886 got_ref_get_name(branch_ref), id_str);
888 if (preserve_logmsg) {
889 fprintf(stderr, "%s: log message preserved in %s\n",
890 getprogname(), logmsg_path);
891 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
892 error = got_error_from_errno2("unlink", logmsg_path);
901 free(gitconfig_path);
903 got_ref_close(branch_ref);
905 got_ref_close(head_ref);
912 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
913 "[-R reference] repository-url [directory]\n", getprogname());
917 struct got_fetch_progress_arg {
918 char last_scaled_size[FMT_SCALED_STRSIZE];
923 struct got_repository *repo;
928 struct got_pathlist_head *symrefs;
929 struct got_pathlist_head *wanted_branches;
930 struct got_pathlist_head *wanted_refs;
934 const char *remote_repo_path;
936 int fetch_all_branches;
937 int mirror_references;
941 /* XXX forward declaration */
942 static const struct got_error *
943 create_config_files(const char *proto, const char *host, const char *port,
944 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
945 int mirror_references, struct got_pathlist_head *symrefs,
946 struct got_pathlist_head *wanted_branches,
947 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
949 static const struct got_error *
950 fetch_progress(void *arg, const char *message, off_t packfile_size,
951 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
953 const struct got_error *err = NULL;
954 struct got_fetch_progress_arg *a = arg;
955 char scaled_size[FMT_SCALED_STRSIZE];
956 int p_indexed, p_resolved;
957 int print_size = 0, print_indexed = 0, print_resolved = 0;
960 * In order to allow a failed clone to be resumed with 'got fetch'
961 * we try to create configuration files as soon as possible.
962 * Once the server has sent information about its default branch
963 * we have all required information.
965 if (a->create_configs && !a->configs_created &&
966 !TAILQ_EMPTY(a->config_info.symrefs)) {
967 err = create_config_files(a->config_info.proto,
968 a->config_info.host, a->config_info.port,
969 a->config_info.remote_repo_path,
970 a->config_info.git_url,
971 a->config_info.fetch_all_branches,
972 a->config_info.mirror_references,
973 a->config_info.symrefs,
974 a->config_info.wanted_branches,
975 a->config_info.wanted_refs, a->repo);
978 a->configs_created = 1;
981 if (a->verbosity < 0)
984 if (message && message[0] != '\0') {
985 printf("\rserver: %s", message);
990 if (packfile_size > 0 || nobj_indexed > 0) {
991 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
992 (a->last_scaled_size[0] == '\0' ||
993 strcmp(scaled_size, a->last_scaled_size)) != 0) {
995 if (strlcpy(a->last_scaled_size, scaled_size,
996 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
997 return got_error(GOT_ERR_NO_SPACE);
999 if (nobj_indexed > 0) {
1000 p_indexed = (nobj_indexed * 100) / nobj_total;
1001 if (p_indexed != a->last_p_indexed) {
1002 a->last_p_indexed = p_indexed;
1007 if (nobj_resolved > 0) {
1008 p_resolved = (nobj_resolved * 100) /
1009 (nobj_total - nobj_loose);
1010 if (p_resolved != a->last_p_resolved) {
1011 a->last_p_resolved = p_resolved;
1019 if (print_size || print_indexed || print_resolved)
1022 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1024 printf("; indexing %d%%", p_indexed);
1026 printf("; resolving deltas %d%%", p_resolved);
1027 if (print_size || print_indexed || print_resolved)
1033 static const struct got_error *
1034 create_symref(const char *refname, struct got_reference *target_ref,
1035 int verbosity, struct got_repository *repo)
1037 const struct got_error *err;
1038 struct got_reference *head_symref;
1040 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1044 err = got_ref_write(head_symref, repo);
1045 if (err == NULL && verbosity > 0) {
1046 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1047 got_ref_get_name(target_ref));
1049 got_ref_close(head_symref);
1053 static const struct got_error *
1054 list_remote_refs(struct got_pathlist_head *symrefs,
1055 struct got_pathlist_head *refs)
1057 const struct got_error *err;
1058 struct got_pathlist_entry *pe;
1060 TAILQ_FOREACH(pe, symrefs, entry) {
1061 const char *refname = pe->path;
1062 const char *targetref = pe->data;
1064 printf("%s: %s\n", refname, targetref);
1067 TAILQ_FOREACH(pe, refs, entry) {
1068 const char *refname = pe->path;
1069 struct got_object_id *id = pe->data;
1072 err = got_object_id_str(&id_str, id);
1075 printf("%s: %s\n", refname, id_str);
1082 static const struct got_error *
1083 create_ref(const char *refname, struct got_object_id *id,
1084 int verbosity, struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_reference *ref;
1090 err = got_object_id_str(&id_str, id);
1094 err = got_ref_alloc(&ref, refname, id);
1098 err = got_ref_write(ref, repo);
1101 if (err == NULL && verbosity >= 0)
1102 printf("Created reference %s: %s\n", refname, id_str);
1109 match_wanted_ref(const char *refname, const char *wanted_ref)
1111 if (strncmp(refname, "refs/", 5) != 0)
1116 * Prevent fetching of references that won't make any
1117 * sense outside of the remote repository's context.
1119 if (strncmp(refname, "got/", 4) == 0)
1121 if (strncmp(refname, "remotes/", 8) == 0)
1124 if (strncmp(wanted_ref, "refs/", 5) == 0)
1127 /* Allow prefix match. */
1128 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1131 /* Allow exact match. */
1132 return (strcmp(refname, wanted_ref) == 0);
1136 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1138 struct got_pathlist_entry *pe;
1140 TAILQ_FOREACH(pe, wanted_refs, entry) {
1141 if (match_wanted_ref(refname, pe->path))
1148 static const struct got_error *
1149 create_wanted_ref(const char *refname, struct got_object_id *id,
1150 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1152 const struct got_error *err;
1153 char *remote_refname;
1155 if (strncmp("refs/", refname, 5) == 0)
1158 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1159 remote_repo_name, refname) == -1)
1160 return got_error_from_errno("asprintf");
1162 err = create_ref(remote_refname, id, verbosity, repo);
1163 free(remote_refname);
1167 static const struct got_error *
1168 create_gotconfig(const char *proto, const char *host, const char *port,
1169 const char *remote_repo_path, const char *default_branch,
1170 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1171 struct got_pathlist_head *wanted_refs, int mirror_references,
1172 struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 char *gotconfig_path = NULL;
1176 char *gotconfig = NULL;
1177 FILE *gotconfig_file = NULL;
1178 const char *branchname = NULL;
1179 char *branches = NULL, *refs = NULL;
1182 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1183 struct got_pathlist_entry *pe;
1184 TAILQ_FOREACH(pe, wanted_branches, entry) {
1186 branchname = pe->path;
1187 if (strncmp(branchname, "refs/heads/", 11) == 0)
1189 if (asprintf(&s, "%s\"%s\" ",
1190 branches ? branches : "", branchname) == -1) {
1191 err = got_error_from_errno("asprintf");
1197 } else if (!fetch_all_branches && default_branch) {
1198 branchname = default_branch;
1199 if (strncmp(branchname, "refs/heads/", 11) == 0)
1201 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1202 err = got_error_from_errno("asprintf");
1206 if (!TAILQ_EMPTY(wanted_refs)) {
1207 struct got_pathlist_entry *pe;
1208 TAILQ_FOREACH(pe, wanted_refs, entry) {
1210 const char *refname = pe->path;
1211 if (strncmp(refname, "refs/", 5) == 0)
1213 if (asprintf(&s, "%s\"%s\" ",
1214 refs ? refs : "", refname) == -1) {
1215 err = got_error_from_errno("asprintf");
1223 /* Create got.conf(5). */
1224 gotconfig_path = got_repo_get_path_gotconfig(repo);
1225 if (gotconfig_path == NULL) {
1226 err = got_error_from_errno("got_repo_get_path_gotconfig");
1229 gotconfig_file = fopen(gotconfig_path, "a");
1230 if (gotconfig_file == NULL) {
1231 err = got_error_from_errno2("fopen", gotconfig_path);
1234 if (asprintf(&gotconfig,
1239 "\trepository \"%s\"\n"
1245 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1246 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1247 remote_repo_path, branches ? "\tbranch { " : "",
1248 branches ? branches : "", branches ? "}\n" : "",
1249 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1250 mirror_references ? "\tmirror-references yes\n" : "",
1251 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1252 err = got_error_from_errno("asprintf");
1255 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1256 if (n != strlen(gotconfig)) {
1257 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1262 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1263 err = got_error_from_errno2("fclose", gotconfig_path);
1264 free(gotconfig_path);
1269 static const struct got_error *
1270 create_gitconfig(const char *git_url, const char *default_branch,
1271 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1272 struct got_pathlist_head *wanted_refs, int mirror_references,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 char *gitconfig_path = NULL;
1277 char *gitconfig = NULL;
1278 FILE *gitconfig_file = NULL;
1279 char *branches = NULL, *refs = NULL;
1280 const char *branchname;
1283 /* Create a config file Git can understand. */
1284 gitconfig_path = got_repo_get_path_gitconfig(repo);
1285 if (gitconfig_path == NULL) {
1286 err = got_error_from_errno("got_repo_get_path_gitconfig");
1289 gitconfig_file = fopen(gitconfig_path, "a");
1290 if (gitconfig_file == NULL) {
1291 err = got_error_from_errno2("fopen", gitconfig_path);
1294 if (fetch_all_branches) {
1295 if (mirror_references) {
1296 if (asprintf(&branches,
1297 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1298 err = got_error_from_errno("asprintf");
1301 } else if (asprintf(&branches,
1302 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1303 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1304 err = got_error_from_errno("asprintf");
1307 } else if (!TAILQ_EMPTY(wanted_branches)) {
1308 struct got_pathlist_entry *pe;
1309 TAILQ_FOREACH(pe, wanted_branches, entry) {
1311 branchname = pe->path;
1312 if (strncmp(branchname, "refs/heads/", 11) == 0)
1314 if (mirror_references) {
1316 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1317 branches ? branches : "",
1318 branchname, branchname) == -1) {
1319 err = got_error_from_errno("asprintf");
1322 } else if (asprintf(&s,
1323 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1324 branches ? branches : "",
1325 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1326 branchname) == -1) {
1327 err = got_error_from_errno("asprintf");
1335 * If the server specified a default branch, use just that one.
1336 * Otherwise fall back to fetching all branches on next fetch.
1338 if (default_branch) {
1339 branchname = default_branch;
1340 if (strncmp(branchname, "refs/heads/", 11) == 0)
1343 branchname = "*"; /* fall back to all branches */
1344 if (mirror_references) {
1345 if (asprintf(&branches,
1346 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1347 branchname, branchname) == -1) {
1348 err = got_error_from_errno("asprintf");
1351 } else if (asprintf(&branches,
1352 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1353 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1354 branchname) == -1) {
1355 err = got_error_from_errno("asprintf");
1359 if (!TAILQ_EMPTY(wanted_refs)) {
1360 struct got_pathlist_entry *pe;
1361 TAILQ_FOREACH(pe, wanted_refs, entry) {
1363 const char *refname = pe->path;
1364 if (strncmp(refname, "refs/", 5) == 0)
1366 if (mirror_references) {
1368 "%s\tfetch = refs/%s:refs/%s\n",
1369 refs ? refs : "", refname, refname) == -1) {
1370 err = got_error_from_errno("asprintf");
1373 } else if (asprintf(&s,
1374 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1376 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1378 err = got_error_from_errno("asprintf");
1386 if (asprintf(&gitconfig,
1391 "\tfetch = refs/tags/*:refs/tags/*\n",
1392 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1393 refs ? refs : "") == -1) {
1394 err = got_error_from_errno("asprintf");
1397 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1398 if (n != strlen(gitconfig)) {
1399 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1403 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1404 err = got_error_from_errno2("fclose", gitconfig_path);
1405 free(gitconfig_path);
1410 static const struct got_error *
1411 create_config_files(const char *proto, const char *host, const char *port,
1412 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1413 int mirror_references, struct got_pathlist_head *symrefs,
1414 struct got_pathlist_head *wanted_branches,
1415 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 const char *default_branch = NULL;
1419 struct got_pathlist_entry *pe;
1422 * If we asked for a set of wanted branches then use the first
1425 if (!TAILQ_EMPTY(wanted_branches)) {
1426 pe = TAILQ_FIRST(wanted_branches);
1427 default_branch = pe->path;
1429 /* First HEAD ref listed by server is the default branch. */
1430 TAILQ_FOREACH(pe, symrefs, entry) {
1431 const char *refname = pe->path;
1432 const char *target = pe->data;
1434 if (strcmp(refname, GOT_REF_HEAD) != 0)
1437 default_branch = target;
1442 /* Create got.conf(5). */
1443 err = create_gotconfig(proto, host, port, remote_repo_path,
1444 default_branch, fetch_all_branches, wanted_branches,
1445 wanted_refs, mirror_references, repo);
1449 /* Create a config file Git can understand. */
1450 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1451 wanted_branches, wanted_refs, mirror_references, repo);
1454 static const struct got_error *
1455 cmd_clone(int argc, char *argv[])
1457 const struct got_error *error = NULL;
1458 const char *uri, *dirname;
1459 char *proto, *host, *port, *repo_name, *server_path;
1460 char *default_destdir = NULL, *id_str = NULL;
1461 const char *repo_path;
1462 struct got_repository *repo = NULL;
1463 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1464 struct got_pathlist_entry *pe;
1465 struct got_object_id *pack_hash = NULL;
1466 int ch, fetchfd = -1, fetchstatus;
1467 pid_t fetchpid = -1;
1468 struct got_fetch_progress_arg fpa;
1469 char *git_url = NULL;
1470 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1471 int list_refs_only = 0;
1474 TAILQ_INIT(&symrefs);
1475 TAILQ_INIT(&wanted_branches);
1476 TAILQ_INIT(&wanted_refs);
1478 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1481 fetch_all_branches = 1;
1484 error = got_pathlist_append(&wanted_branches,
1493 mirror_references = 1;
1498 else if (verbosity < 3)
1505 error = got_pathlist_append(&wanted_refs,
1518 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1519 option_conflict('a', 'b');
1520 if (list_refs_only) {
1521 if (!TAILQ_EMPTY(&wanted_branches))
1522 option_conflict('l', 'b');
1523 if (fetch_all_branches)
1524 option_conflict('l', 'a');
1525 if (mirror_references)
1526 option_conflict('l', 'm');
1527 if (verbosity == -1)
1528 option_conflict('l', 'q');
1529 if (!TAILQ_EMPTY(&wanted_refs))
1530 option_conflict('l', 'R');
1542 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1547 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1548 host, port ? ":" : "", port ? port : "",
1549 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1550 error = got_error_from_errno("asprintf");
1554 if (strcmp(proto, "git") == 0) {
1556 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1557 "sendfd dns inet unveil", NULL) == -1)
1560 } else if (strcmp(proto, "git+ssh") == 0 ||
1561 strcmp(proto, "ssh") == 0) {
1563 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1564 "sendfd unveil", NULL) == -1)
1567 } else if (strcmp(proto, "http") == 0 ||
1568 strcmp(proto, "git+http") == 0) {
1569 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1572 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1575 if (dirname == NULL) {
1576 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1577 error = got_error_from_errno("asprintf");
1580 repo_path = default_destdir;
1582 repo_path = dirname;
1584 if (!list_refs_only) {
1585 error = got_path_mkdir(repo_path);
1587 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1588 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1590 if (!got_path_dir_is_empty(repo_path)) {
1591 error = got_error_path(repo_path,
1592 GOT_ERR_DIR_NOT_EMPTY);
1597 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1598 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1599 error = got_error_from_errno2("unveil",
1600 GOT_FETCH_PATH_SSH);
1604 error = apply_unveil(repo_path, 0, NULL);
1609 printf("Connecting to %s%s%s\n", host,
1610 port ? ":" : "", port ? port : "");
1612 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1613 server_path, verbosity);
1617 if (!list_refs_only) {
1618 error = got_repo_init(repo_path);
1621 error = got_repo_open(&repo, repo_path, NULL);
1626 fpa.last_scaled_size[0] = '\0';
1627 fpa.last_p_indexed = -1;
1628 fpa.last_p_resolved = -1;
1629 fpa.verbosity = verbosity;
1630 fpa.create_configs = 1;
1631 fpa.configs_created = 0;
1633 fpa.config_info.symrefs = &symrefs;
1634 fpa.config_info.wanted_branches = &wanted_branches;
1635 fpa.config_info.wanted_refs = &wanted_refs;
1636 fpa.config_info.proto = proto;
1637 fpa.config_info.host = host;
1638 fpa.config_info.port = port;
1639 fpa.config_info.remote_repo_path = server_path;
1640 fpa.config_info.git_url = git_url;
1641 fpa.config_info.fetch_all_branches = fetch_all_branches;
1642 fpa.config_info.mirror_references = mirror_references;
1643 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1644 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1645 fetch_all_branches, &wanted_branches, &wanted_refs,
1646 list_refs_only, verbosity, fetchfd, repo,
1647 fetch_progress, &fpa);
1651 if (list_refs_only) {
1652 error = list_remote_refs(&symrefs, &refs);
1656 error = got_object_id_str(&id_str, pack_hash);
1660 printf("\nFetched %s.pack\n", id_str);
1663 /* Set up references provided with the pack file. */
1664 TAILQ_FOREACH(pe, &refs, entry) {
1665 const char *refname = pe->path;
1666 struct got_object_id *id = pe->data;
1667 char *remote_refname;
1669 if (is_wanted_ref(&wanted_refs, refname) &&
1670 !mirror_references) {
1671 error = create_wanted_ref(refname, id,
1672 GOT_FETCH_DEFAULT_REMOTE_NAME,
1673 verbosity - 1, repo);
1679 error = create_ref(refname, id, verbosity - 1, repo);
1683 if (mirror_references)
1686 if (strncmp("refs/heads/", refname, 11) != 0)
1689 if (asprintf(&remote_refname,
1690 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1691 refname + 11) == -1) {
1692 error = got_error_from_errno("asprintf");
1695 error = create_ref(remote_refname, id, verbosity - 1, repo);
1696 free(remote_refname);
1701 /* Set the HEAD reference if the server provided one. */
1702 TAILQ_FOREACH(pe, &symrefs, entry) {
1703 struct got_reference *target_ref;
1704 const char *refname = pe->path;
1705 const char *target = pe->data;
1706 char *remote_refname = NULL, *remote_target = NULL;
1708 if (strcmp(refname, GOT_REF_HEAD) != 0)
1711 error = got_ref_open(&target_ref, repo, target, 0);
1713 if (error->code == GOT_ERR_NOT_REF) {
1720 error = create_symref(refname, target_ref, verbosity, repo);
1721 got_ref_close(target_ref);
1725 if (mirror_references)
1728 if (strncmp("refs/heads/", target, 11) != 0)
1731 if (asprintf(&remote_refname,
1732 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1734 error = got_error_from_errno("asprintf");
1737 if (asprintf(&remote_target,
1738 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 target + 11) == -1) {
1740 error = got_error_from_errno("asprintf");
1741 free(remote_refname);
1744 error = got_ref_open(&target_ref, repo, remote_target, 0);
1746 free(remote_refname);
1747 free(remote_target);
1748 if (error->code == GOT_ERR_NOT_REF) {
1754 error = create_symref(remote_refname, target_ref,
1755 verbosity - 1, repo);
1756 free(remote_refname);
1757 free(remote_target);
1758 got_ref_close(target_ref);
1764 * We failed to set the HEAD reference. If we asked for
1765 * a set of wanted branches use the first of one of those
1766 * which could be fetched instead.
1768 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1769 const char *target = pe->path;
1770 struct got_reference *target_ref;
1772 error = got_ref_open(&target_ref, repo, target, 0);
1774 if (error->code == GOT_ERR_NOT_REF) {
1781 error = create_symref(GOT_REF_HEAD, target_ref,
1783 got_ref_close(target_ref);
1791 printf("Created %s repository '%s'\n",
1792 mirror_references ? "mirrored" : "cloned", repo_path);
1795 if (kill(fetchpid, SIGTERM) == -1)
1796 error = got_error_from_errno("kill");
1797 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1798 error = got_error_from_errno("waitpid");
1800 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1801 error = got_error_from_errno("close");
1803 got_repo_close(repo);
1804 TAILQ_FOREACH(pe, &refs, entry) {
1805 free((void *)pe->path);
1808 got_pathlist_free(&refs);
1809 TAILQ_FOREACH(pe, &symrefs, entry) {
1810 free((void *)pe->path);
1813 got_pathlist_free(&symrefs);
1814 got_pathlist_free(&wanted_branches);
1815 got_pathlist_free(&wanted_refs);
1822 free(default_destdir);
1827 static const struct got_error *
1828 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1829 int replace_tags, int verbosity, struct got_repository *repo)
1831 const struct got_error *err = NULL;
1832 char *new_id_str = NULL;
1833 struct got_object_id *old_id = NULL;
1835 err = got_object_id_str(&new_id_str, new_id);
1839 if (!replace_tags &&
1840 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1841 err = got_ref_resolve(&old_id, repo, ref);
1844 if (got_object_id_cmp(old_id, new_id) == 0)
1846 if (verbosity >= 0) {
1847 printf("Rejecting update of existing tag %s: %s\n",
1848 got_ref_get_name(ref), new_id_str);
1853 if (got_ref_is_symbolic(ref)) {
1854 if (verbosity >= 0) {
1855 printf("Replacing reference %s: %s\n",
1856 got_ref_get_name(ref),
1857 got_ref_get_symref_target(ref));
1859 err = got_ref_change_symref_to_ref(ref, new_id);
1862 err = got_ref_write(ref, repo);
1866 err = got_ref_resolve(&old_id, repo, ref);
1869 if (got_object_id_cmp(old_id, new_id) == 0)
1872 err = got_ref_change_ref(ref, new_id);
1875 err = got_ref_write(ref, repo);
1881 printf("Updated %s: %s\n", got_ref_get_name(ref),
1889 static const struct got_error *
1890 update_symref(const char *refname, struct got_reference *target_ref,
1891 int verbosity, struct got_repository *repo)
1893 const struct got_error *err = NULL, *unlock_err;
1894 struct got_reference *symref;
1895 int symref_is_locked = 0;
1897 err = got_ref_open(&symref, repo, refname, 1);
1899 if (err->code != GOT_ERR_NOT_REF)
1901 err = got_ref_alloc_symref(&symref, refname, target_ref);
1905 err = got_ref_write(symref, repo);
1910 printf("Created reference %s: %s\n",
1911 got_ref_get_name(symref),
1912 got_ref_get_symref_target(symref));
1914 symref_is_locked = 1;
1916 if (strcmp(got_ref_get_symref_target(symref),
1917 got_ref_get_name(target_ref)) == 0)
1920 err = got_ref_change_symref(symref,
1921 got_ref_get_name(target_ref));
1925 err = got_ref_write(symref, repo);
1930 printf("Updated %s: %s\n", got_ref_get_name(symref),
1931 got_ref_get_symref_target(symref));
1935 if (symref_is_locked) {
1936 unlock_err = got_ref_unlock(symref);
1937 if (unlock_err && err == NULL)
1940 got_ref_close(symref);
1947 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1948 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1949 "[remote-repository-name]\n",
1954 static const struct got_error *
1955 delete_missing_ref(struct got_reference *ref,
1956 int verbosity, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_object_id *id = NULL;
1960 char *id_str = NULL;
1962 if (got_ref_is_symbolic(ref)) {
1963 err = got_ref_delete(ref, repo);
1966 if (verbosity >= 0) {
1967 printf("Deleted reference %s: %s\n",
1968 got_ref_get_name(ref),
1969 got_ref_get_symref_target(ref));
1972 err = got_ref_resolve(&id, repo, ref);
1975 err = got_object_id_str(&id_str, id);
1979 err = got_ref_delete(ref, repo);
1982 if (verbosity >= 0) {
1983 printf("Deleted reference %s: %s\n",
1984 got_ref_get_name(ref), id_str);
1993 static const struct got_error *
1994 delete_missing_refs(struct got_pathlist_head *their_refs,
1995 struct got_pathlist_head *their_symrefs,
1996 const struct got_remote_repo *remote,
1997 int verbosity, struct got_repository *repo)
1999 const struct got_error *err = NULL, *unlock_err;
2000 struct got_reflist_head my_refs;
2001 struct got_reflist_entry *re;
2002 struct got_pathlist_entry *pe;
2003 char *remote_namespace = NULL;
2004 char *local_refname = NULL;
2006 TAILQ_INIT(&my_refs);
2008 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2010 return got_error_from_errno("asprintf");
2012 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2016 TAILQ_FOREACH(re, &my_refs, entry) {
2017 const char *refname = got_ref_get_name(re->ref);
2019 if (!remote->mirror_references) {
2020 if (strncmp(refname, remote_namespace,
2021 strlen(remote_namespace)) == 0) {
2022 if (strcmp(refname + strlen(remote_namespace),
2025 if (asprintf(&local_refname, "refs/heads/%s",
2026 refname + strlen(remote_namespace)) == -1) {
2027 err = got_error_from_errno("asprintf");
2030 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2034 TAILQ_FOREACH(pe, their_refs, entry) {
2035 if (strcmp(local_refname, pe->path) == 0)
2041 TAILQ_FOREACH(pe, their_symrefs, entry) {
2042 if (strcmp(local_refname, pe->path) == 0)
2048 err = delete_missing_ref(re->ref, verbosity, repo);
2052 if (local_refname) {
2053 struct got_reference *ref;
2054 err = got_ref_open(&ref, repo, local_refname, 1);
2056 if (err->code != GOT_ERR_NOT_REF)
2058 free(local_refname);
2059 local_refname = NULL;
2062 err = delete_missing_ref(ref, verbosity, repo);
2065 unlock_err = got_ref_unlock(ref);
2067 if (unlock_err && err == NULL) {
2072 free(local_refname);
2073 local_refname = NULL;
2077 free(remote_namespace);
2078 free(local_refname);
2082 static const struct got_error *
2083 update_wanted_ref(const char *refname, struct got_object_id *id,
2084 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2086 const struct got_error *err, *unlock_err;
2087 char *remote_refname;
2088 struct got_reference *ref;
2090 if (strncmp("refs/", refname, 5) == 0)
2093 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2094 remote_repo_name, refname) == -1)
2095 return got_error_from_errno("asprintf");
2097 err = got_ref_open(&ref, repo, remote_refname, 1);
2099 if (err->code != GOT_ERR_NOT_REF)
2101 err = create_ref(remote_refname, id, verbosity, repo);
2103 err = update_ref(ref, id, 0, verbosity, repo);
2104 unlock_err = got_ref_unlock(ref);
2105 if (unlock_err && err == NULL)
2110 free(remote_refname);
2114 static const struct got_error *
2115 cmd_fetch(int argc, char *argv[])
2117 const struct got_error *error = NULL, *unlock_err;
2118 char *cwd = NULL, *repo_path = NULL;
2119 const char *remote_name;
2120 char *proto = NULL, *host = NULL, *port = NULL;
2121 char *repo_name = NULL, *server_path = NULL;
2122 const struct got_remote_repo *remotes, *remote = NULL;
2124 char *id_str = NULL;
2125 struct got_repository *repo = NULL;
2126 struct got_worktree *worktree = NULL;
2127 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2128 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2129 struct got_pathlist_entry *pe;
2130 struct got_object_id *pack_hash = NULL;
2131 int i, ch, fetchfd = -1, fetchstatus;
2132 pid_t fetchpid = -1;
2133 struct got_fetch_progress_arg fpa;
2134 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2135 int delete_refs = 0, replace_tags = 0;
2138 TAILQ_INIT(&symrefs);
2139 TAILQ_INIT(&wanted_branches);
2140 TAILQ_INIT(&wanted_refs);
2142 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2145 fetch_all_branches = 1;
2148 error = got_pathlist_append(&wanted_branches,
2160 repo_path = realpath(optarg, NULL);
2161 if (repo_path == NULL)
2162 return got_error_from_errno2("realpath",
2164 got_path_strip_trailing_slashes(repo_path);
2172 else if (verbosity < 3)
2179 error = got_pathlist_append(&wanted_refs,
2192 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2193 option_conflict('a', 'b');
2194 if (list_refs_only) {
2195 if (!TAILQ_EMPTY(&wanted_branches))
2196 option_conflict('l', 'b');
2197 if (fetch_all_branches)
2198 option_conflict('l', 'a');
2200 option_conflict('l', 'd');
2204 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2206 remote_name = argv[0];
2210 cwd = getcwd(NULL, 0);
2212 error = got_error_from_errno("getcwd");
2216 if (repo_path == NULL) {
2217 error = got_worktree_open(&worktree, cwd);
2218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2224 strdup(got_worktree_get_repo_path(worktree));
2225 if (repo_path == NULL)
2226 error = got_error_from_errno("strdup");
2230 repo_path = strdup(cwd);
2231 if (repo_path == NULL) {
2232 error = got_error_from_errno("strdup");
2238 error = got_repo_open(&repo, repo_path, NULL);
2243 worktree_conf = got_worktree_get_gotconfig(worktree);
2244 if (worktree_conf) {
2245 got_gotconfig_get_remotes(&nremotes, &remotes,
2247 for (i = 0; i < nremotes; i++) {
2248 if (strcmp(remotes[i].name, remote_name) == 0) {
2249 remote = &remotes[i];
2255 if (remote == NULL) {
2256 repo_conf = got_repo_get_gotconfig(repo);
2258 got_gotconfig_get_remotes(&nremotes, &remotes,
2260 for (i = 0; i < nremotes; i++) {
2261 if (strcmp(remotes[i].name, remote_name) == 0) {
2262 remote = &remotes[i];
2268 if (remote == NULL) {
2269 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2270 for (i = 0; i < nremotes; i++) {
2271 if (strcmp(remotes[i].name, remote_name) == 0) {
2272 remote = &remotes[i];
2277 if (remote == NULL) {
2278 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2282 if (TAILQ_EMPTY(&wanted_branches)) {
2283 if (!fetch_all_branches)
2284 fetch_all_branches = remote->fetch_all_branches;
2285 for (i = 0; i < remote->nbranches; i++) {
2286 got_pathlist_append(&wanted_branches,
2287 remote->branches[i], NULL);
2290 if (TAILQ_EMPTY(&wanted_refs)) {
2291 for (i = 0; i < remote->nrefs; i++) {
2292 got_pathlist_append(&wanted_refs,
2293 remote->refs[i], NULL);
2297 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2298 &repo_name, remote->url);
2302 if (strcmp(proto, "git") == 0) {
2304 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2305 "sendfd dns inet unveil", NULL) == -1)
2308 } else if (strcmp(proto, "git+ssh") == 0 ||
2309 strcmp(proto, "ssh") == 0) {
2311 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2312 "sendfd unveil", NULL) == -1)
2315 } else if (strcmp(proto, "http") == 0 ||
2316 strcmp(proto, "git+http") == 0) {
2317 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2320 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2324 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2325 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2326 error = got_error_from_errno2("unveil",
2327 GOT_FETCH_PATH_SSH);
2331 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2336 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2337 port ? ":" : "", port ? port : "");
2339 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2340 server_path, verbosity);
2344 fpa.last_scaled_size[0] = '\0';
2345 fpa.last_p_indexed = -1;
2346 fpa.last_p_resolved = -1;
2347 fpa.verbosity = verbosity;
2349 fpa.create_configs = 0;
2350 fpa.configs_created = 0;
2351 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2352 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2353 remote->mirror_references, fetch_all_branches, &wanted_branches,
2354 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2355 fetch_progress, &fpa);
2359 if (list_refs_only) {
2360 error = list_remote_refs(&symrefs, &refs);
2364 if (pack_hash == NULL) {
2366 printf("Already up-to-date\n");
2367 } else if (verbosity >= 0) {
2368 error = got_object_id_str(&id_str, pack_hash);
2371 printf("\nFetched %s.pack\n", id_str);
2376 /* Update references provided with the pack file. */
2377 TAILQ_FOREACH(pe, &refs, entry) {
2378 const char *refname = pe->path;
2379 struct got_object_id *id = pe->data;
2380 struct got_reference *ref;
2381 char *remote_refname;
2383 if (is_wanted_ref(&wanted_refs, refname) &&
2384 !remote->mirror_references) {
2385 error = update_wanted_ref(refname, id,
2386 remote->name, verbosity, repo);
2392 if (remote->mirror_references ||
2393 strncmp("refs/tags/", refname, 10) == 0) {
2394 error = got_ref_open(&ref, repo, refname, 1);
2396 if (error->code != GOT_ERR_NOT_REF)
2398 error = create_ref(refname, id, verbosity,
2403 error = update_ref(ref, id, replace_tags,
2405 unlock_err = got_ref_unlock(ref);
2406 if (unlock_err && error == NULL)
2412 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2413 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2414 remote_name, refname + 11) == -1) {
2415 error = got_error_from_errno("asprintf");
2419 error = got_ref_open(&ref, repo, remote_refname, 1);
2421 if (error->code != GOT_ERR_NOT_REF)
2423 error = create_ref(remote_refname, id,
2428 error = update_ref(ref, id, replace_tags,
2430 unlock_err = got_ref_unlock(ref);
2431 if (unlock_err && error == NULL)
2438 /* Also create a local branch if none exists yet. */
2439 error = got_ref_open(&ref, repo, refname, 1);
2441 if (error->code != GOT_ERR_NOT_REF)
2443 error = create_ref(refname, id, verbosity,
2448 unlock_err = got_ref_unlock(ref);
2449 if (unlock_err && error == NULL)
2456 error = delete_missing_refs(&refs, &symrefs, remote,
2462 if (!remote->mirror_references) {
2463 /* Update remote HEAD reference if the server provided one. */
2464 TAILQ_FOREACH(pe, &symrefs, entry) {
2465 struct got_reference *target_ref;
2466 const char *refname = pe->path;
2467 const char *target = pe->data;
2468 char *remote_refname = NULL, *remote_target = NULL;
2470 if (strcmp(refname, GOT_REF_HEAD) != 0)
2473 if (strncmp("refs/heads/", target, 11) != 0)
2476 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2477 remote->name, refname) == -1) {
2478 error = got_error_from_errno("asprintf");
2481 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2482 remote->name, target + 11) == -1) {
2483 error = got_error_from_errno("asprintf");
2484 free(remote_refname);
2488 error = got_ref_open(&target_ref, repo, remote_target,
2491 free(remote_refname);
2492 free(remote_target);
2493 if (error->code == GOT_ERR_NOT_REF) {
2499 error = update_symref(remote_refname, target_ref,
2501 free(remote_refname);
2502 free(remote_target);
2503 got_ref_close(target_ref);
2510 if (kill(fetchpid, SIGTERM) == -1)
2511 error = got_error_from_errno("kill");
2512 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2513 error = got_error_from_errno("waitpid");
2515 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2516 error = got_error_from_errno("close");
2518 got_repo_close(repo);
2520 got_worktree_close(worktree);
2521 TAILQ_FOREACH(pe, &refs, entry) {
2522 free((void *)pe->path);
2525 got_pathlist_free(&refs);
2526 TAILQ_FOREACH(pe, &symrefs, entry) {
2527 free((void *)pe->path);
2530 got_pathlist_free(&symrefs);
2531 got_pathlist_free(&wanted_branches);
2532 got_pathlist_free(&wanted_refs);
2547 usage_checkout(void)
2549 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2550 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2555 show_worktree_base_ref_warning(void)
2557 fprintf(stderr, "%s: warning: could not create a reference "
2558 "to the work tree's base commit; the commit could be "
2559 "garbage-collected by Git; making the repository "
2560 "writable and running 'got update' will prevent this\n",
2564 struct got_checkout_progress_arg {
2565 const char *worktree_path;
2566 int had_base_commit_ref_error;
2569 static const struct got_error *
2570 checkout_progress(void *arg, unsigned char status, const char *path)
2572 struct got_checkout_progress_arg *a = arg;
2574 /* Base commit bump happens silently. */
2575 if (status == GOT_STATUS_BUMP_BASE)
2578 if (status == GOT_STATUS_BASE_REF_ERR) {
2579 a->had_base_commit_ref_error = 1;
2583 while (path[0] == '/')
2586 printf("%c %s/%s\n", status, a->worktree_path, path);
2590 static const struct got_error *
2591 check_cancelled(void *arg)
2593 if (sigint_received || sigpipe_received)
2594 return got_error(GOT_ERR_CANCELLED);
2598 static const struct got_error *
2599 check_linear_ancestry(struct got_object_id *commit_id,
2600 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2601 struct got_repository *repo)
2603 const struct got_error *err = NULL;
2604 struct got_object_id *yca_id;
2606 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2607 commit_id, base_commit_id, repo, check_cancelled, NULL);
2612 return got_error(GOT_ERR_ANCESTRY);
2615 * Require a straight line of history between the target commit
2616 * and the work tree's base commit.
2618 * Non-linear situations such as this require a rebase:
2620 * (commit) D F (base_commit)
2628 * 'got update' only handles linear cases:
2629 * Update forwards in time: A (base/yca) - B - C - D (commit)
2630 * Update backwards in time: D (base) - C - B - A (commit/yca)
2632 if (allow_forwards_in_time_only) {
2633 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2634 return got_error(GOT_ERR_ANCESTRY);
2635 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2636 got_object_id_cmp(base_commit_id, yca_id) != 0)
2637 return got_error(GOT_ERR_ANCESTRY);
2643 static const struct got_error *
2644 check_same_branch(struct got_object_id *commit_id,
2645 struct got_reference *head_ref, struct got_object_id *yca_id,
2646 struct got_repository *repo)
2648 const struct got_error *err = NULL;
2649 struct got_commit_graph *graph = NULL;
2650 struct got_object_id *head_commit_id = NULL;
2651 int is_same_branch = 0;
2653 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2657 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2661 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2666 err = got_commit_graph_open(&graph, "/", 1);
2670 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2671 check_cancelled, NULL);
2676 struct got_object_id *id;
2677 err = got_commit_graph_iter_next(&id, graph, repo,
2678 check_cancelled, NULL);
2680 if (err->code == GOT_ERR_ITER_COMPLETED)
2686 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2688 if (got_object_id_cmp(id, commit_id) == 0) {
2696 got_commit_graph_close(graph);
2697 free(head_commit_id);
2698 if (!err && !is_same_branch)
2699 err = got_error(GOT_ERR_ANCESTRY);
2703 static const struct got_error *
2704 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2706 static char msg[512];
2707 const char *branch_name;
2709 if (got_ref_is_symbolic(ref))
2710 branch_name = got_ref_get_symref_target(ref);
2712 branch_name = got_ref_get_name(ref);
2714 if (strncmp("refs/heads/", branch_name, 11) == 0)
2717 snprintf(msg, sizeof(msg),
2718 "target commit is not contained in branch '%s'; "
2719 "the branch to use must be specified with -b; "
2720 "if necessary a new branch can be created for "
2721 "this commit with 'got branch -c %s BRANCH_NAME'",
2722 branch_name, commit_id_str);
2724 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2727 static const struct got_error *
2728 cmd_checkout(int argc, char *argv[])
2730 const struct got_error *error = NULL;
2731 struct got_repository *repo = NULL;
2732 struct got_reference *head_ref = NULL;
2733 struct got_worktree *worktree = NULL;
2734 char *repo_path = NULL;
2735 char *worktree_path = NULL;
2736 const char *path_prefix = "";
2737 const char *branch_name = GOT_REF_HEAD;
2738 char *commit_id_str = NULL;
2740 int ch, same_path_prefix, allow_nonempty = 0;
2741 struct got_pathlist_head paths;
2742 struct got_checkout_progress_arg cpa;
2746 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2749 branch_name = optarg;
2752 commit_id_str = strdup(optarg);
2753 if (commit_id_str == NULL)
2754 return got_error_from_errno("strdup");
2760 path_prefix = optarg;
2772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2773 "unveil", NULL) == -1)
2777 char *base, *dotgit;
2779 repo_path = realpath(argv[0], NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath", argv[0]);
2782 cwd = getcwd(NULL, 0);
2784 error = got_error_from_errno("getcwd");
2791 error = got_path_basename(&base, path);
2794 dotgit = strstr(base, ".git");
2797 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2798 error = got_error_from_errno("asprintf");
2803 } else if (argc == 2) {
2804 repo_path = realpath(argv[0], NULL);
2805 if (repo_path == NULL) {
2806 error = got_error_from_errno2("realpath", argv[0]);
2809 worktree_path = realpath(argv[1], NULL);
2810 if (worktree_path == NULL) {
2811 if (errno != ENOENT) {
2812 error = got_error_from_errno2("realpath",
2816 worktree_path = strdup(argv[1]);
2817 if (worktree_path == NULL) {
2818 error = got_error_from_errno("strdup");
2825 got_path_strip_trailing_slashes(repo_path);
2826 got_path_strip_trailing_slashes(worktree_path);
2828 error = got_repo_open(&repo, repo_path, NULL);
2832 /* Pre-create work tree path for unveil(2) */
2833 error = got_path_mkdir(worktree_path);
2835 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2836 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2838 if (!allow_nonempty &&
2839 !got_path_dir_is_empty(worktree_path)) {
2840 error = got_error_path(worktree_path,
2841 GOT_ERR_DIR_NOT_EMPTY);
2846 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2850 error = got_ref_open(&head_ref, repo, branch_name, 0);
2854 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2855 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2858 error = got_worktree_open(&worktree, worktree_path);
2862 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2866 if (!same_path_prefix) {
2867 error = got_error(GOT_ERR_PATH_PREFIX);
2871 if (commit_id_str) {
2872 struct got_object_id *commit_id;
2873 struct got_reflist_head refs;
2875 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2879 error = got_repo_match_object_id(&commit_id, NULL,
2880 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2881 got_ref_list_free(&refs);
2884 error = check_linear_ancestry(commit_id,
2885 got_worktree_get_base_commit_id(worktree), 0, repo);
2886 if (error != NULL) {
2888 if (error->code == GOT_ERR_ANCESTRY) {
2889 error = checkout_ancestry_error(
2890 head_ref, commit_id_str);
2894 error = check_same_branch(commit_id, head_ref, NULL, repo);
2896 if (error->code == GOT_ERR_ANCESTRY) {
2897 error = checkout_ancestry_error(
2898 head_ref, commit_id_str);
2902 error = got_worktree_set_base_commit_id(worktree, repo,
2909 error = got_pathlist_append(&paths, "", NULL);
2912 cpa.worktree_path = worktree_path;
2913 cpa.had_base_commit_ref_error = 0;
2914 error = got_worktree_checkout_files(worktree, &paths, repo,
2915 checkout_progress, &cpa, check_cancelled, NULL);
2919 printf("Now shut up and hack\n");
2920 if (cpa.had_base_commit_ref_error)
2921 show_worktree_base_ref_warning();
2923 got_pathlist_free(&paths);
2924 free(commit_id_str);
2926 free(worktree_path);
2931 struct got_update_progress_arg {
2939 print_update_progress_stats(struct got_update_progress_arg *upa)
2941 if (!upa->did_something)
2944 if (upa->conflicts > 0)
2945 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2946 if (upa->obstructed > 0)
2947 printf("File paths obstructed by a non-regular file: %d\n",
2949 if (upa->not_updated > 0)
2950 printf("Files not updated because of existing merge "
2951 "conflicts: %d\n", upa->not_updated);
2957 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2962 static const struct got_error *
2963 update_progress(void *arg, unsigned char status, const char *path)
2965 struct got_update_progress_arg *upa = arg;
2967 if (status == GOT_STATUS_EXISTS ||
2968 status == GOT_STATUS_BASE_REF_ERR)
2971 upa->did_something = 1;
2973 /* Base commit bump happens silently. */
2974 if (status == GOT_STATUS_BUMP_BASE)
2977 if (status == GOT_STATUS_CONFLICT)
2979 if (status == GOT_STATUS_OBSTRUCTED)
2981 if (status == GOT_STATUS_CANNOT_UPDATE)
2984 while (path[0] == '/')
2986 printf("%c %s\n", status, path);
2990 static const struct got_error *
2991 switch_head_ref(struct got_reference *head_ref,
2992 struct got_object_id *commit_id, struct got_worktree *worktree,
2993 struct got_repository *repo)
2995 const struct got_error *err = NULL;
2997 int ref_has_moved = 0;
2999 /* Trivial case: switching between two different references. */
3000 if (strcmp(got_ref_get_name(head_ref),
3001 got_worktree_get_head_ref_name(worktree)) != 0) {
3002 printf("Switching work tree from %s to %s\n",
3003 got_worktree_get_head_ref_name(worktree),
3004 got_ref_get_name(head_ref));
3005 return got_worktree_set_head_ref(worktree, head_ref);
3008 err = check_linear_ancestry(commit_id,
3009 got_worktree_get_base_commit_id(worktree), 0, repo);
3011 if (err->code != GOT_ERR_ANCESTRY)
3018 /* Switching to a rebased branch with the same reference name. */
3019 err = got_object_id_str(&base_id_str,
3020 got_worktree_get_base_commit_id(worktree));
3023 printf("Reference %s now points at a different branch\n",
3024 got_worktree_get_head_ref_name(worktree));
3025 printf("Switching work tree from %s to %s\n", base_id_str,
3026 got_worktree_get_head_ref_name(worktree));
3030 static const struct got_error *
3031 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3033 const struct got_error *err;
3036 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3040 return got_error(GOT_ERR_REBASING);
3042 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3046 return got_error(GOT_ERR_HISTEDIT_BUSY);
3051 static const struct got_error *
3052 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3053 char *argv[], struct got_worktree *worktree)
3055 const struct got_error *err = NULL;
3062 return got_error_from_errno("strdup");
3063 return got_pathlist_append(paths, path, NULL);
3066 for (i = 0; i < argc; i++) {
3067 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3070 err = got_pathlist_append(paths, path, NULL);
3080 static const struct got_error *
3081 wrap_not_worktree_error(const struct got_error *orig_err,
3082 const char *cmdname, const char *path)
3084 const struct got_error *err;
3085 struct got_repository *repo;
3086 static char msg[512];
3088 err = got_repo_open(&repo, path, NULL);
3092 snprintf(msg, sizeof(msg),
3093 "'got %s' needs a work tree in addition to a git repository\n"
3094 "Work trees can be checked out from this Git repository with "
3096 "The got(1) manual page contains more information.", cmdname);
3097 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3098 got_repo_close(repo);
3102 static const struct got_error *
3103 cmd_update(int argc, char *argv[])
3105 const struct got_error *error = NULL;
3106 struct got_repository *repo = NULL;
3107 struct got_worktree *worktree = NULL;
3108 char *worktree_path = NULL;
3109 struct got_object_id *commit_id = NULL;
3110 char *commit_id_str = NULL;
3111 const char *branch_name = NULL;
3112 struct got_reference *head_ref = NULL;
3113 struct got_pathlist_head paths;
3114 struct got_pathlist_entry *pe;
3116 struct got_update_progress_arg upa;
3120 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3123 branch_name = optarg;
3126 commit_id_str = strdup(optarg);
3127 if (commit_id_str == NULL)
3128 return got_error_from_errno("strdup");
3140 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3141 "unveil", NULL) == -1)
3144 worktree_path = getcwd(NULL, 0);
3145 if (worktree_path == NULL) {
3146 error = got_error_from_errno("getcwd");
3149 error = got_worktree_open(&worktree, worktree_path);
3151 if (error->code == GOT_ERR_NOT_WORKTREE)
3152 error = wrap_not_worktree_error(error, "update",
3157 error = check_rebase_or_histedit_in_progress(worktree);
3161 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3166 error = apply_unveil(got_repo_get_path(repo), 0,
3167 got_worktree_get_root_path(worktree));
3171 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3175 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3176 got_worktree_get_head_ref_name(worktree), 0);
3179 if (commit_id_str == NULL) {
3180 error = got_ref_resolve(&commit_id, repo, head_ref);
3183 error = got_object_id_str(&commit_id_str, commit_id);
3187 struct got_reflist_head refs;
3189 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3193 error = got_repo_match_object_id(&commit_id, NULL,
3194 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3195 got_ref_list_free(&refs);
3196 free(commit_id_str);
3197 commit_id_str = NULL;
3200 error = got_object_id_str(&commit_id_str, commit_id);
3206 struct got_object_id *head_commit_id;
3207 TAILQ_FOREACH(pe, &paths, entry) {
3208 if (pe->path_len == 0)
3210 error = got_error_msg(GOT_ERR_BAD_PATH,
3211 "switching between branches requires that "
3212 "the entire work tree gets updated");
3215 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3218 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3220 free(head_commit_id);
3223 error = check_same_branch(commit_id, head_ref, NULL, repo);
3226 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3230 error = check_linear_ancestry(commit_id,
3231 got_worktree_get_base_commit_id(worktree), 0, repo);
3232 if (error != NULL) {
3233 if (error->code == GOT_ERR_ANCESTRY)
3234 error = got_error(GOT_ERR_BRANCH_MOVED);
3237 error = check_same_branch(commit_id, head_ref, NULL, repo);
3242 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3244 error = got_worktree_set_base_commit_id(worktree, repo,
3250 memset(&upa, 0, sizeof(upa));
3251 error = got_worktree_checkout_files(worktree, &paths, repo,
3252 update_progress, &upa, check_cancelled, NULL);
3256 if (upa.did_something)
3257 printf("Updated to commit %s\n", commit_id_str);
3259 printf("Already up-to-date\n");
3260 print_update_progress_stats(&upa);
3262 free(worktree_path);
3263 TAILQ_FOREACH(pe, &paths, entry)
3264 free((char *)pe->path);
3265 got_pathlist_free(&paths);
3267 free(commit_id_str);
3271 static const struct got_error *
3272 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3273 const char *path, int diff_context, int ignore_whitespace,
3274 int force_text_diff, struct got_repository *repo)
3276 const struct got_error *err = NULL;
3277 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3280 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3285 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3289 while (path[0] == '/')
3291 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3292 diff_context, ignore_whitespace, force_text_diff, stdout);
3295 got_object_blob_close(blob1);
3296 got_object_blob_close(blob2);
3300 static const struct got_error *
3301 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3302 const char *path, int diff_context, int ignore_whitespace,
3303 int force_text_diff, struct got_repository *repo)
3305 const struct got_error *err = NULL;
3306 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3307 struct got_diff_blob_output_unidiff_arg arg;
3310 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3315 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3319 arg.diff_context = diff_context;
3320 arg.ignore_whitespace = ignore_whitespace;
3321 arg.force_text_diff = force_text_diff;
3322 arg.outfile = stdout;
3323 arg.line_offsets = NULL;
3325 while (path[0] == '/')
3327 err = got_diff_tree(tree1, tree2, path, path, repo,
3328 got_diff_blob_output_unidiff, &arg, 1);
3331 got_object_tree_close(tree1);
3333 got_object_tree_close(tree2);
3337 static const struct got_error *
3338 get_changed_paths(struct got_pathlist_head *paths,
3339 struct got_commit_object *commit, struct got_repository *repo)
3341 const struct got_error *err = NULL;
3342 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3343 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3344 struct got_object_qid *qid;
3346 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3348 struct got_commit_object *pcommit;
3349 err = got_object_open_as_commit(&pcommit, repo,
3354 tree_id1 = got_object_commit_get_tree_id(pcommit);
3355 got_object_commit_close(pcommit);
3360 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3365 tree_id2 = got_object_commit_get_tree_id(commit);
3366 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3370 err = got_diff_tree(tree1, tree2, "", "", repo,
3371 got_diff_tree_collect_changed_paths, paths, 0);
3374 got_object_tree_close(tree1);
3376 got_object_tree_close(tree2);
3380 static const struct got_error *
3381 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3382 const char *path, int diff_context, struct got_repository *repo)
3384 const struct got_error *err = NULL;
3385 struct got_commit_object *pcommit = NULL;
3386 char *id_str1 = NULL, *id_str2 = NULL;
3387 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3388 struct got_object_qid *qid;
3390 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3392 err = got_object_open_as_commit(&pcommit, repo,
3398 if (path && path[0] != '\0') {
3400 err = got_object_id_by_path(&obj_id2, repo, id, path);
3403 err = got_object_id_str(&id_str2, obj_id2);
3409 err = got_object_id_by_path(&obj_id1, repo,
3412 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3417 err = got_object_id_str(&id_str1, obj_id1);
3424 err = got_object_get_type(&obj_type, repo, obj_id2);
3429 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3431 case GOT_OBJ_TYPE_BLOB:
3432 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3435 case GOT_OBJ_TYPE_TREE:
3436 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3440 err = got_error(GOT_ERR_OBJ_TYPE);
3446 obj_id2 = got_object_commit_get_tree_id(commit);
3447 err = got_object_id_str(&id_str2, obj_id2);
3451 obj_id1 = got_object_commit_get_tree_id(pcommit);
3452 err = got_object_id_str(&id_str1, obj_id1);
3456 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3458 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3465 got_object_commit_close(pcommit);
3470 get_datestr(time_t *time, char *datebuf)
3472 struct tm mytm, *tm;
3475 tm = gmtime_r(time, &mytm);
3478 s = asctime_r(tm, datebuf);
3481 p = strchr(s, '\n');
3487 static const struct got_error *
3488 match_logmsg(int *have_match, struct got_object_id *id,
3489 struct got_commit_object *commit, regex_t *regex)
3491 const struct got_error *err = NULL;
3492 regmatch_t regmatch;
3493 char *id_str = NULL, *logmsg = NULL;
3497 err = got_object_id_str(&id_str, id);
3501 err = got_object_commit_get_logmsg(&logmsg, commit);
3505 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3514 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3517 regmatch_t regmatch;
3518 struct got_pathlist_entry *pe;
3522 TAILQ_FOREACH(pe, changed_paths, entry) {
3523 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3530 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3532 static const struct got_error*
3533 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3534 struct got_object_id *id, struct got_repository *repo)
3536 static const struct got_error *err = NULL;
3537 struct got_reflist_entry *re;
3543 TAILQ_FOREACH(re, refs, entry) {
3544 struct got_tag_object *tag = NULL;
3545 struct got_object_id *ref_id;
3548 name = got_ref_get_name(re->ref);
3549 if (strcmp(name, GOT_REF_HEAD) == 0)
3551 if (strncmp(name, "refs/", 5) == 0)
3553 if (strncmp(name, "got/", 4) == 0)
3555 if (strncmp(name, "heads/", 6) == 0)
3557 if (strncmp(name, "remotes/", 8) == 0) {
3559 s = strstr(name, "/" GOT_REF_HEAD);
3560 if (s != NULL && s[strlen(s)] == '\0')
3563 err = got_ref_resolve(&ref_id, repo, re->ref);
3566 if (strncmp(name, "tags/", 5) == 0) {
3567 err = got_object_open_as_tag(&tag, repo, ref_id);
3569 if (err->code != GOT_ERR_OBJ_TYPE) {
3573 /* Ref points at something other than a tag. */
3578 cmp = got_object_id_cmp(tag ?
3579 got_object_tag_get_object_id(tag) : ref_id, id);
3582 got_object_tag_close(tag);
3586 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3587 s ? ", " : "", name) == -1) {
3588 err = got_error_from_errno("asprintf");
3599 static const struct got_error *
3600 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3601 struct got_repository *repo, const char *path,
3602 struct got_pathlist_head *changed_paths, int show_patch,
3603 int diff_context, struct got_reflist_object_id_map *refs_idmap)
3605 const struct got_error *err = NULL;
3606 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3608 time_t committer_time;
3609 const char *author, *committer;
3610 char *refs_str = NULL;
3611 struct got_reflist_head *refs;
3613 err = got_object_id_str(&id_str, id);
3617 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3619 err = build_refs_str(&refs_str, refs, id, repo);
3624 printf(GOT_COMMIT_SEP_STR);
3625 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3626 refs_str ? refs_str : "", refs_str ? ")" : "");
3631 printf("from: %s\n", got_object_commit_get_author(commit));
3632 committer_time = got_object_commit_get_committer_time(commit);
3633 datestr = get_datestr(&committer_time, datebuf);
3635 printf("date: %s UTC\n", datestr);
3636 author = got_object_commit_get_author(commit);
3637 committer = got_object_commit_get_committer(commit);
3638 if (strcmp(author, committer) != 0)
3639 printf("via: %s\n", committer);
3640 if (got_object_commit_get_nparents(commit) > 1) {
3641 const struct got_object_id_queue *parent_ids;
3642 struct got_object_qid *qid;
3644 parent_ids = got_object_commit_get_parent_ids(commit);
3645 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3646 err = got_object_id_str(&id_str, qid->id);
3649 printf("parent %d: %s\n", n++, id_str);
3655 err = got_object_commit_get_logmsg(&logmsg0, commit);
3661 line = strsep(&logmsg, "\n");
3663 printf(" %s\n", line);
3667 if (changed_paths) {
3668 struct got_pathlist_entry *pe;
3669 TAILQ_FOREACH(pe, changed_paths, entry) {
3670 struct got_diff_changed_path *cp = pe->data;
3671 printf(" %c %s\n", cp->status, pe->path);
3676 err = print_patch(commit, id, path, diff_context, repo);
3681 if (fflush(stdout) != 0 && err == NULL)
3682 err = got_error_from_errno("fflush");
3689 static const struct got_error *
3690 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3691 struct got_repository *repo, const char *path, int show_changed_paths,
3692 int show_patch, const char *search_pattern, int diff_context, int limit,
3693 int log_branches, int reverse_display_order,
3694 struct got_reflist_object_id_map *refs_idmap)
3696 const struct got_error *err;
3697 struct got_commit_graph *graph;
3700 struct got_object_id_queue reversed_commits;
3701 struct got_object_qid *qid;
3702 struct got_commit_object *commit;
3703 struct got_pathlist_head changed_paths;
3704 struct got_pathlist_entry *pe;
3706 SIMPLEQ_INIT(&reversed_commits);
3707 TAILQ_INIT(&changed_paths);
3709 if (search_pattern && regcomp(®ex, search_pattern,
3710 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3711 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3713 err = got_commit_graph_open(&graph, path, !log_branches);
3716 err = got_commit_graph_iter_start(graph, root_id, repo,
3717 check_cancelled, NULL);
3721 struct got_object_id *id;
3723 if (sigint_received || sigpipe_received)
3726 err = got_commit_graph_iter_next(&id, graph, repo,
3727 check_cancelled, NULL);
3729 if (err->code == GOT_ERR_ITER_COMPLETED)
3736 err = got_object_open_as_commit(&commit, repo, id);
3740 if (show_changed_paths && !reverse_display_order) {
3741 err = get_changed_paths(&changed_paths, commit, repo);
3746 if (search_pattern) {
3747 err = match_logmsg(&have_match, id, commit, ®ex);
3749 got_object_commit_close(commit);
3752 if (have_match == 0 && show_changed_paths)
3753 match_changed_paths(&have_match,
3754 &changed_paths, ®ex);
3755 if (have_match == 0) {
3756 got_object_commit_close(commit);
3757 TAILQ_FOREACH(pe, &changed_paths, entry) {
3758 free((char *)pe->path);
3761 got_pathlist_free(&changed_paths);
3766 if (reverse_display_order) {
3767 err = got_object_qid_alloc(&qid, id);
3770 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3771 got_object_commit_close(commit);
3773 err = print_commit(commit, id, repo, path,
3774 show_changed_paths ? &changed_paths : NULL,
3775 show_patch, diff_context, refs_idmap);
3776 got_object_commit_close(commit);
3780 if ((limit && --limit == 0) ||
3781 (end_id && got_object_id_cmp(id, end_id) == 0))
3784 TAILQ_FOREACH(pe, &changed_paths, entry) {
3785 free((char *)pe->path);
3788 got_pathlist_free(&changed_paths);
3790 if (reverse_display_order) {
3791 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3792 err = got_object_open_as_commit(&commit, repo, qid->id);
3795 if (show_changed_paths) {
3796 err = get_changed_paths(&changed_paths,
3801 err = print_commit(commit, qid->id, repo, path,
3802 show_changed_paths ? &changed_paths : NULL,
3803 show_patch, diff_context, refs_idmap);
3804 got_object_commit_close(commit);
3807 TAILQ_FOREACH(pe, &changed_paths, entry) {
3808 free((char *)pe->path);
3811 got_pathlist_free(&changed_paths);
3815 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3816 qid = SIMPLEQ_FIRST(&reversed_commits);
3817 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3818 got_object_qid_free(qid);
3820 TAILQ_FOREACH(pe, &changed_paths, entry) {
3821 free((char *)pe->path);
3824 got_pathlist_free(&changed_paths);
3827 got_commit_graph_close(graph);
3834 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3835 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3836 "[-R] [path]\n", getprogname());
3841 get_default_log_limit(void)
3843 const char *got_default_log_limit;
3847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3848 if (got_default_log_limit == NULL)
3850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3856 static const struct got_error *
3857 cmd_log(int argc, char *argv[])
3859 const struct got_error *error;
3860 struct got_repository *repo = NULL;
3861 struct got_worktree *worktree = NULL;
3862 struct got_object_id *start_id = NULL, *end_id = NULL;
3863 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3864 const char *start_commit = NULL, *end_commit = NULL;
3865 const char *search_pattern = NULL;
3866 int diff_context = -1, ch;
3867 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3868 int reverse_display_order = 0;
3870 struct got_reflist_head refs;
3871 struct got_reflist_object_id_map *refs_idmap = NULL;
3876 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3882 limit = get_default_log_limit();
3884 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3890 show_changed_paths = 1;
3893 start_commit = optarg;
3896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3899 err(1, "-C option %s", errstr);
3902 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3904 err(1, "-l option %s", errstr);
3910 repo_path = realpath(optarg, NULL);
3911 if (repo_path == NULL)
3912 return got_error_from_errno2("realpath",
3914 got_path_strip_trailing_slashes(repo_path);
3917 reverse_display_order = 1;
3920 search_pattern = optarg;
3923 end_commit = optarg;
3934 if (diff_context == -1)
3936 else if (!show_patch)
3937 errx(1, "-C requires -p");
3939 cwd = getcwd(NULL, 0);
3941 error = got_error_from_errno("getcwd");
3945 if (repo_path == NULL) {
3946 error = got_worktree_open(&worktree, cwd);
3947 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3954 error = got_worktree_resolve_path(&path, worktree,
3959 path = strdup(argv[0]);
3961 error = got_error_from_errno("strdup");
3965 } else if (argc != 0)
3968 if (repo_path == NULL) {
3969 repo_path = worktree ?
3970 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3972 if (repo_path == NULL) {
3973 error = got_error_from_errno("strdup");
3977 error = got_repo_open(&repo, repo_path, NULL);
3981 error = apply_unveil(got_repo_get_path(repo), 1,
3982 worktree ? got_worktree_get_root_path(worktree) : NULL);
3986 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3990 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3994 if (start_commit == NULL) {
3995 struct got_reference *head_ref;
3996 struct got_commit_object *commit = NULL;
3997 error = got_ref_open(&head_ref, repo,
3998 worktree ? got_worktree_get_head_ref_name(worktree)
4002 error = got_ref_resolve(&start_id, repo, head_ref);
4003 got_ref_close(head_ref);
4006 error = got_object_open_as_commit(&commit, repo,
4010 got_object_commit_close(commit);
4012 error = got_repo_match_object_id(&start_id, NULL,
4013 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4017 if (end_commit != NULL) {
4018 error = got_repo_match_object_id(&end_id, NULL,
4019 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4026 * If a path was specified on the command line it was resolved
4027 * to a path in the work tree above. Prepend the work tree's
4028 * path prefix to obtain the corresponding in-repository path.
4032 prefix = got_worktree_get_path_prefix(worktree);
4033 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4034 (path[0] != '\0') ? "/" : "", path) == -1) {
4035 error = got_error_from_errno("asprintf");
4040 error = got_repo_map_path(&in_repo_path, repo,
4046 path = in_repo_path;
4049 error = print_commits(start_id, end_id, repo, path ? path : "",
4050 show_changed_paths, show_patch, search_pattern, diff_context,
4051 limit, log_branches, reverse_display_order, refs_idmap);
4057 got_worktree_close(worktree);
4059 const struct got_error *repo_error;
4060 repo_error = got_repo_close(repo);
4065 got_reflist_object_id_map_free(refs_idmap);
4066 got_ref_list_free(&refs);
4073 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4074 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4078 struct print_diff_arg {
4079 struct got_repository *repo;
4080 struct got_worktree *worktree;
4085 int ignore_whitespace;
4086 int force_text_diff;
4090 * Create a file which contains the target path of a symlink so we can feed
4091 * it as content to the diff engine.
4093 static const struct got_error *
4094 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4095 const char *abspath)
4097 const struct got_error *err = NULL;
4098 char target_path[PATH_MAX];
4099 ssize_t target_len, outlen;
4104 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4105 if (target_len == -1)
4106 return got_error_from_errno2("readlinkat", abspath);
4108 target_len = readlink(abspath, target_path, PATH_MAX);
4109 if (target_len == -1)
4110 return got_error_from_errno2("readlink", abspath);
4113 *fd = got_opentempfd();
4115 return got_error_from_errno("got_opentempfd");
4117 outlen = write(*fd, target_path, target_len);
4119 err = got_error_from_errno("got_opentempfd");
4123 if (lseek(*fd, 0, SEEK_SET) == -1) {
4124 err = got_error_from_errno2("lseek", abspath);
4135 static const struct got_error *
4136 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4137 const char *path, struct got_object_id *blob_id,
4138 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4139 int dirfd, const char *de_name)
4141 struct print_diff_arg *a = arg;
4142 const struct got_error *err = NULL;
4143 struct got_blob_object *blob1 = NULL;
4146 char *abspath = NULL, *label1 = NULL;
4149 if (a->diff_staged) {
4150 if (staged_status != GOT_STATUS_MODIFY &&
4151 staged_status != GOT_STATUS_ADD &&
4152 staged_status != GOT_STATUS_DELETE)
4155 if (staged_status == GOT_STATUS_DELETE)
4157 if (status == GOT_STATUS_NONEXISTENT)
4158 return got_error_set_errno(ENOENT, path);
4159 if (status != GOT_STATUS_MODIFY &&
4160 status != GOT_STATUS_ADD &&
4161 status != GOT_STATUS_DELETE &&
4162 status != GOT_STATUS_CONFLICT)
4166 if (!a->header_shown) {
4167 printf("diff %s %s%s\n", a->id_str,
4168 got_worktree_get_root_path(a->worktree),
4169 a->diff_staged ? " (staged changes)" : "");
4170 a->header_shown = 1;
4173 if (a->diff_staged) {
4174 const char *label1 = NULL, *label2 = NULL;
4175 switch (staged_status) {
4176 case GOT_STATUS_MODIFY:
4180 case GOT_STATUS_ADD:
4183 case GOT_STATUS_DELETE:
4187 return got_error(GOT_ERR_FILE_STATUS);
4189 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4190 staged_blob_id, label1, label2, a->diff_context,
4191 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4194 if (staged_status == GOT_STATUS_ADD ||
4195 staged_status == GOT_STATUS_MODIFY) {
4197 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4201 err = got_object_id_str(&id_str, staged_blob_id);
4204 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4205 err = got_error_from_errno("asprintf");
4210 } else if (status != GOT_STATUS_ADD) {
4211 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4216 if (status != GOT_STATUS_DELETE) {
4217 if (asprintf(&abspath, "%s/%s",
4218 got_worktree_get_root_path(a->worktree), path) == -1) {
4219 err = got_error_from_errno("asprintf");
4224 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4226 if (errno != ELOOP) {
4227 err = got_error_from_errno2("openat",
4231 err = get_symlink_target_file(&fd, dirfd,
4237 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4239 if (errno != ELOOP) {
4240 err = got_error_from_errno2("open",
4244 err = get_symlink_target_file(&fd, dirfd,
4250 if (fstat(fd, &sb) == -1) {
4251 err = got_error_from_errno2("fstat", abspath);
4254 f2 = fdopen(fd, "r");
4256 err = got_error_from_errno2("fdopen", abspath);
4263 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4264 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4267 got_object_blob_close(blob1);
4268 if (f2 && fclose(f2) == EOF && err == NULL)
4269 err = got_error_from_errno("fclose");
4270 if (fd != -1 && close(fd) == -1 && err == NULL)
4271 err = got_error_from_errno("close");
4276 static const struct got_error *
4277 cmd_diff(int argc, char *argv[])
4279 const struct got_error *error;
4280 struct got_repository *repo = NULL;
4281 struct got_worktree *worktree = NULL;
4282 char *cwd = NULL, *repo_path = NULL;
4283 struct got_object_id *id1 = NULL, *id2 = NULL;
4284 const char *id_str1 = NULL, *id_str2 = NULL;
4285 char *label1 = NULL, *label2 = NULL;
4287 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4288 int force_text_diff = 0;
4291 struct got_reflist_head refs;
4296 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4301 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4304 force_text_diff = 1;
4307 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4310 err(1, "-C option %s", errstr);
4313 repo_path = realpath(optarg, NULL);
4314 if (repo_path == NULL)
4315 return got_error_from_errno2("realpath",
4317 got_path_strip_trailing_slashes(repo_path);
4323 ignore_whitespace = 1;
4334 cwd = getcwd(NULL, 0);
4336 error = got_error_from_errno("getcwd");
4342 "-r option can't be used when diffing a work tree");
4343 error = got_worktree_open(&worktree, cwd);
4345 if (error->code == GOT_ERR_NOT_WORKTREE)
4346 error = wrap_not_worktree_error(error, "diff",
4350 repo_path = strdup(got_worktree_get_repo_path(worktree));
4351 if (repo_path == NULL) {
4352 error = got_error_from_errno("strdup");
4356 error = got_worktree_resolve_path(&path, worktree,
4363 error = got_error_from_errno("strdup");
4367 } else if (argc == 2) {
4369 errx(1, "-s option can't be used when diffing "
4370 "objects in repository");
4373 if (repo_path == NULL) {
4374 error = got_worktree_open(&worktree, cwd);
4375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4379 got_worktree_get_repo_path(worktree));
4380 if (repo_path == NULL) {
4381 error = got_error_from_errno("strdup");
4385 repo_path = strdup(cwd);
4386 if (repo_path == NULL) {
4387 error = got_error_from_errno("strdup");
4395 error = got_repo_open(&repo, repo_path, NULL);
4400 error = apply_unveil(got_repo_get_path(repo), 1,
4401 worktree ? got_worktree_get_root_path(worktree) : NULL);
4406 struct print_diff_arg arg;
4407 struct got_pathlist_head paths;
4412 error = got_object_id_str(&id_str,
4413 got_worktree_get_base_commit_id(worktree));
4417 arg.worktree = worktree;
4418 arg.diff_context = diff_context;
4419 arg.id_str = id_str;
4420 arg.header_shown = 0;
4421 arg.diff_staged = diff_staged;
4422 arg.ignore_whitespace = ignore_whitespace;
4423 arg.force_text_diff = force_text_diff;
4425 error = got_pathlist_append(&paths, path, NULL);
4429 error = got_worktree_status(worktree, &paths, repo, print_diff,
4430 &arg, check_cancelled, NULL);
4432 got_pathlist_free(&paths);
4436 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4440 error = got_repo_match_object_id(&id1, &label1, id_str1,
4441 GOT_OBJ_TYPE_ANY, &refs, repo);
4445 error = got_repo_match_object_id(&id2, &label2, id_str2,
4446 GOT_OBJ_TYPE_ANY, &refs, repo);
4450 error = got_object_get_type(&type1, repo, id1);
4454 error = got_object_get_type(&type2, repo, id2);
4458 if (type1 != type2) {
4459 error = got_error(GOT_ERR_OBJ_TYPE);
4464 case GOT_OBJ_TYPE_BLOB:
4465 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4466 NULL, NULL, diff_context, ignore_whitespace,
4467 force_text_diff, repo, stdout);
4469 case GOT_OBJ_TYPE_TREE:
4470 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4471 "", "", diff_context, ignore_whitespace, force_text_diff,
4474 case GOT_OBJ_TYPE_COMMIT:
4475 printf("diff %s %s\n", label1, label2);
4476 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4477 diff_context, ignore_whitespace, force_text_diff, repo,
4481 error = got_error(GOT_ERR_OBJ_TYPE);
4490 got_worktree_close(worktree);
4492 const struct got_error *repo_error;
4493 repo_error = got_repo_close(repo);
4497 got_ref_list_free(&refs);
4505 "usage: %s blame [-c commit] [-r repository-path] path\n",
4514 char datebuf[11]; /* YYYY-MM-DD + NUL */
4517 struct blame_cb_args {
4518 struct blame_line *lines;
4522 off_t *line_offsets;
4524 struct got_repository *repo;
4527 static const struct got_error *
4528 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4530 const struct got_error *err = NULL;
4531 struct blame_cb_args *a = arg;
4532 struct blame_line *bline;
4534 size_t linesize = 0;
4535 struct got_commit_object *commit = NULL;
4538 time_t committer_time;
4540 if (nlines != a->nlines ||
4541 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4542 return got_error(GOT_ERR_RANGE);
4544 if (sigint_received)
4545 return got_error(GOT_ERR_ITER_COMPLETED);
4548 return NULL; /* no change in this commit */
4550 /* Annotate this line. */
4551 bline = &a->lines[lineno - 1];
4552 if (bline->annotated)
4554 err = got_object_id_str(&bline->id_str, id);
4558 err = got_object_open_as_commit(&commit, a->repo, id);
4562 bline->committer = strdup(got_object_commit_get_committer(commit));
4563 if (bline->committer == NULL) {
4564 err = got_error_from_errno("strdup");
4568 committer_time = got_object_commit_get_committer_time(commit);
4569 if (localtime_r(&committer_time, &tm) == NULL)
4570 return got_error_from_errno("localtime_r");
4571 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4572 &tm) >= sizeof(bline->datebuf)) {
4573 err = got_error(GOT_ERR_NO_SPACE);
4576 bline->annotated = 1;
4578 /* Print lines annotated so far. */
4579 bline = &a->lines[a->lineno_cur - 1];
4580 if (!bline->annotated)
4583 offset = a->line_offsets[a->lineno_cur - 1];
4584 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4585 err = got_error_from_errno("fseeko");
4589 while (bline->annotated) {
4590 char *smallerthan, *at, *nl, *committer;
4593 if (getline(&line, &linesize, a->f) == -1) {
4595 err = got_error_from_errno("getline");
4599 committer = bline->committer;
4600 smallerthan = strchr(committer, '<');
4601 if (smallerthan && smallerthan[1] != '\0')
4602 committer = smallerthan + 1;
4603 at = strchr(committer, '@');
4606 len = strlen(committer);
4608 committer[8] = '\0';
4610 nl = strchr(line, '\n');
4613 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4614 bline->id_str, bline->datebuf, committer, line);
4617 bline = &a->lines[a->lineno_cur - 1];
4621 got_object_commit_close(commit);
4626 static const struct got_error *
4627 cmd_blame(int argc, char *argv[])
4629 const struct got_error *error;
4630 struct got_repository *repo = NULL;
4631 struct got_worktree *worktree = NULL;
4632 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4633 char *link_target = NULL;
4634 struct got_object_id *obj_id = NULL;
4635 struct got_object_id *commit_id = NULL;
4636 struct got_blob_object *blob = NULL;
4637 char *commit_id_str = NULL;
4638 struct blame_cb_args bca;
4639 int ch, obj_type, i;
4642 memset(&bca, 0, sizeof(bca));
4645 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4650 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4653 commit_id_str = optarg;
4656 repo_path = realpath(optarg, NULL);
4657 if (repo_path == NULL)
4658 return got_error_from_errno2("realpath",
4660 got_path_strip_trailing_slashes(repo_path);
4676 cwd = getcwd(NULL, 0);
4678 error = got_error_from_errno("getcwd");
4681 if (repo_path == NULL) {
4682 error = got_worktree_open(&worktree, cwd);
4683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4689 strdup(got_worktree_get_repo_path(worktree));
4690 if (repo_path == NULL) {
4691 error = got_error_from_errno("strdup");
4696 repo_path = strdup(cwd);
4697 if (repo_path == NULL) {
4698 error = got_error_from_errno("strdup");
4704 error = got_repo_open(&repo, repo_path, NULL);
4709 const char *prefix = got_worktree_get_path_prefix(worktree);
4712 error = got_worktree_resolve_path(&p, worktree, path);
4715 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4716 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4718 error = got_error_from_errno("asprintf");
4723 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4725 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4728 error = got_repo_map_path(&in_repo_path, repo, path);
4733 if (commit_id_str == NULL) {
4734 struct got_reference *head_ref;
4735 error = got_ref_open(&head_ref, repo, worktree ?
4736 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4739 error = got_ref_resolve(&commit_id, repo, head_ref);
4740 got_ref_close(head_ref);
4744 struct got_reflist_head refs;
4746 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4750 error = got_repo_match_object_id(&commit_id, NULL,
4751 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4752 got_ref_list_free(&refs);
4757 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4762 error = got_object_id_by_path(&obj_id, repo, commit_id,
4763 link_target ? link_target : in_repo_path);
4767 error = got_object_get_type(&obj_type, repo, obj_id);
4771 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4772 error = got_error_path(link_target ? link_target : in_repo_path,
4777 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4780 bca.f = got_opentemp();
4781 if (bca.f == NULL) {
4782 error = got_error_from_errno("got_opentemp");
4785 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4786 &bca.line_offsets, bca.f, blob);
4787 if (error || bca.nlines == 0)
4790 /* Don't include \n at EOF in the blame line count. */
4791 if (bca.line_offsets[bca.nlines - 1] == filesize)
4794 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4795 if (bca.lines == NULL) {
4796 error = got_error_from_errno("calloc");
4800 bca.nlines_prec = 0;
4808 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4809 repo, blame_cb, &bca, check_cancelled, NULL);
4818 got_object_blob_close(blob);
4820 got_worktree_close(worktree);
4822 const struct got_error *repo_error;
4823 repo_error = got_repo_close(repo);
4828 for (i = 0; i < bca.nlines; i++) {
4829 struct blame_line *bline = &bca.lines[i];
4830 free(bline->id_str);
4831 free(bline->committer);
4835 free(bca.line_offsets);
4836 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4837 error = got_error_from_errno("fclose");
4845 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4850 static const struct got_error *
4851 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4852 const char *root_path, struct got_repository *repo)
4854 const struct got_error *err = NULL;
4855 int is_root_path = (strcmp(path, root_path) == 0);
4856 const char *modestr = "";
4857 mode_t mode = got_tree_entry_get_mode(te);
4858 char *link_target = NULL;
4860 path += strlen(root_path);
4861 while (path[0] == '/')
4864 if (got_object_tree_entry_is_submodule(te))
4866 else if (S_ISLNK(mode)) {
4869 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4872 for (i = 0; i < strlen(link_target); i++) {
4873 if (!isprint((unsigned char)link_target[i]))
4874 link_target[i] = '?';
4879 else if (S_ISDIR(mode))
4881 else if (mode & S_IXUSR)
4884 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4885 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4886 link_target ? " -> ": "", link_target ? link_target : "");
4892 static const struct got_error *
4893 print_tree(const char *path, struct got_object_id *commit_id,
4894 int show_ids, int recurse, const char *root_path,
4895 struct got_repository *repo)
4897 const struct got_error *err = NULL;
4898 struct got_object_id *tree_id = NULL;
4899 struct got_tree_object *tree = NULL;
4902 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4906 err = got_object_open_as_tree(&tree, repo, tree_id);
4909 nentries = got_object_tree_get_nentries(tree);
4910 for (i = 0; i < nentries; i++) {
4911 struct got_tree_entry *te;
4914 if (sigint_received || sigpipe_received)
4917 te = got_object_tree_get_entry(tree, i);
4920 err = got_object_id_str(&id_str,
4921 got_tree_entry_get_id(te));
4924 if (asprintf(&id, "%s ", id_str) == -1) {
4925 err = got_error_from_errno("asprintf");
4931 err = print_entry(te, id, path, root_path, repo);
4936 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4938 if (asprintf(&child_path, "%s%s%s", path,
4939 path[0] == '/' && path[1] == '\0' ? "" : "/",
4940 got_tree_entry_get_name(te)) == -1) {
4941 err = got_error_from_errno("asprintf");
4944 err = print_tree(child_path, commit_id, show_ids, 1,
4953 got_object_tree_close(tree);
4958 static const struct got_error *
4959 cmd_tree(int argc, char *argv[])
4961 const struct got_error *error;
4962 struct got_repository *repo = NULL;
4963 struct got_worktree *worktree = NULL;
4964 const char *path, *refname = NULL;
4965 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4966 struct got_object_id *commit_id = NULL;
4967 char *commit_id_str = NULL;
4968 int show_ids = 0, recurse = 0;
4972 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4977 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4980 commit_id_str = optarg;
4983 repo_path = realpath(optarg, NULL);
4984 if (repo_path == NULL)
4985 return got_error_from_errno2("realpath",
4987 got_path_strip_trailing_slashes(repo_path);
5011 cwd = getcwd(NULL, 0);
5013 error = got_error_from_errno("getcwd");
5016 if (repo_path == NULL) {
5017 error = got_worktree_open(&worktree, cwd);
5018 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5024 strdup(got_worktree_get_repo_path(worktree));
5025 if (repo_path == NULL)
5026 error = got_error_from_errno("strdup");
5030 repo_path = strdup(cwd);
5031 if (repo_path == NULL) {
5032 error = got_error_from_errno("strdup");
5038 error = got_repo_open(&repo, repo_path, NULL);
5043 const char *prefix = got_worktree_get_path_prefix(worktree);
5048 error = got_worktree_resolve_path(&p, worktree, path);
5051 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5052 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5054 error = got_error_from_errno("asprintf");
5059 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5063 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5068 error = got_repo_map_path(&in_repo_path, repo, path);
5073 if (commit_id_str == NULL) {
5074 struct got_reference *head_ref;
5076 refname = got_worktree_get_head_ref_name(worktree);
5078 refname = GOT_REF_HEAD;
5079 error = got_ref_open(&head_ref, repo, refname, 0);
5082 error = got_ref_resolve(&commit_id, repo, head_ref);
5083 got_ref_close(head_ref);
5087 struct got_reflist_head refs;
5089 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5093 error = got_repo_match_object_id(&commit_id, NULL,
5094 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5095 got_ref_list_free(&refs);
5100 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5101 in_repo_path, repo);
5108 got_worktree_close(worktree);
5110 const struct got_error *repo_error;
5111 repo_error = got_repo_close(repo);
5121 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5126 static const struct got_error *
5127 print_status(void *arg, unsigned char status, unsigned char staged_status,
5128 const char *path, struct got_object_id *blob_id,
5129 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5130 int dirfd, const char *de_name)
5132 if (status == staged_status && (status == GOT_STATUS_DELETE))
5133 status = GOT_STATUS_NO_CHANGE;
5135 char *status_codes = arg;
5136 size_t ncodes = strlen(status_codes);
5138 for (i = 0; i < ncodes ; i++) {
5139 if (status == status_codes[i] ||
5140 staged_status == status_codes[i])
5146 printf("%c%c %s\n", status, staged_status, path);
5150 static const struct got_error *
5151 cmd_status(int argc, char *argv[])
5153 const struct got_error *error = NULL;
5154 struct got_repository *repo = NULL;
5155 struct got_worktree *worktree = NULL;
5156 char *cwd = NULL, *status_codes = NULL;;
5157 struct got_pathlist_head paths;
5158 struct got_pathlist_entry *pe;
5163 while ((ch = getopt(argc, argv, "s:")) != -1) {
5166 for (i = 0; i < strlen(optarg); i++) {
5167 switch (optarg[i]) {
5168 case GOT_STATUS_MODIFY:
5169 case GOT_STATUS_ADD:
5170 case GOT_STATUS_DELETE:
5171 case GOT_STATUS_CONFLICT:
5172 case GOT_STATUS_MISSING:
5173 case GOT_STATUS_OBSTRUCTED:
5174 case GOT_STATUS_UNVERSIONED:
5175 case GOT_STATUS_MODE_CHANGE:
5176 case GOT_STATUS_NONEXISTENT:
5179 errx(1, "invalid status code '%c'",
5183 status_codes = optarg;
5195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5199 cwd = getcwd(NULL, 0);
5201 error = got_error_from_errno("getcwd");
5205 error = got_worktree_open(&worktree, cwd);
5207 if (error->code == GOT_ERR_NOT_WORKTREE)
5208 error = wrap_not_worktree_error(error, "status", cwd);
5212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5217 error = apply_unveil(got_repo_get_path(repo), 1,
5218 got_worktree_get_root_path(worktree));
5222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5226 error = got_worktree_status(worktree, &paths, repo, print_status,
5227 status_codes, check_cancelled, NULL);
5229 TAILQ_FOREACH(pe, &paths, entry)
5230 free((char *)pe->path);
5231 got_pathlist_free(&paths);
5240 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5246 static const struct got_error *
5247 list_refs(struct got_repository *repo, const char *refname)
5249 static const struct got_error *err = NULL;
5250 struct got_reflist_head refs;
5251 struct got_reflist_entry *re;
5254 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5258 TAILQ_FOREACH(re, &refs, entry) {
5260 refstr = got_ref_to_str(re->ref);
5262 return got_error_from_errno("got_ref_to_str");
5263 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5267 got_ref_list_free(&refs);
5271 static const struct got_error *
5272 delete_ref(struct got_repository *repo, const char *refname)
5274 const struct got_error *err = NULL;
5275 struct got_reference *ref;
5277 err = got_ref_open(&ref, repo, refname, 0);
5281 err = got_ref_delete(ref, repo);
5286 static const struct got_error *
5287 add_ref(struct got_repository *repo, const char *refname, const char *target)
5289 const struct got_error *err = NULL;
5290 struct got_object_id *id;
5291 struct got_reference *ref = NULL;
5294 * Don't let the user create a reference name with a leading '-'.
5295 * While technically a valid reference name, this case is usually
5296 * an unintended typo.
5298 if (refname[0] == '-')
5299 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5301 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5304 struct got_reference *target_ref;
5306 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5308 err = got_ref_open(&target_ref, repo, target, 0);
5311 err = got_ref_resolve(&id, repo, target_ref);
5312 got_ref_close(target_ref);
5317 err = got_ref_alloc(&ref, refname, id);
5321 err = got_ref_write(ref, repo);
5329 static const struct got_error *
5330 add_symref(struct got_repository *repo, const char *refname, const char *target)
5332 const struct got_error *err = NULL;
5333 struct got_reference *ref = NULL;
5334 struct got_reference *target_ref = NULL;
5337 * Don't let the user create a reference name with a leading '-'.
5338 * While technically a valid reference name, this case is usually
5339 * an unintended typo.
5341 if (refname[0] == '-')
5342 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5344 err = got_ref_open(&target_ref, repo, target, 0);
5348 err = got_ref_alloc_symref(&ref, refname, target_ref);
5352 err = got_ref_write(ref, repo);
5355 got_ref_close(target_ref);
5361 static const struct got_error *
5362 cmd_ref(int argc, char *argv[])
5364 const struct got_error *error = NULL;
5365 struct got_repository *repo = NULL;
5366 struct got_worktree *worktree = NULL;
5367 char *cwd = NULL, *repo_path = NULL;
5368 int ch, do_list = 0, do_delete = 0;
5369 const char *obj_arg = NULL, *symref_target= NULL;
5370 char *refname = NULL;
5372 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5381 repo_path = realpath(optarg, NULL);
5382 if (repo_path == NULL)
5383 return got_error_from_errno2("realpath",
5385 got_path_strip_trailing_slashes(repo_path);
5391 symref_target = optarg;
5399 if (obj_arg && do_list)
5400 option_conflict('c', 'l');
5401 if (obj_arg && do_delete)
5402 option_conflict('c', 'd');
5403 if (obj_arg && symref_target)
5404 option_conflict('c', 's');
5405 if (symref_target && do_delete)
5406 option_conflict('s', 'd');
5407 if (symref_target && do_list)
5408 option_conflict('s', 'l');
5409 if (do_delete && do_list)
5410 option_conflict('d', 'l');
5416 if (argc != 0 && argc != 1)
5419 refname = strdup(argv[0]);
5420 if (refname == NULL) {
5421 error = got_error_from_errno("strdup");
5428 refname = strdup(argv[0]);
5429 if (refname == NULL) {
5430 error = got_error_from_errno("strdup");
5436 got_path_strip_trailing_slashes(refname);
5440 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5444 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5445 "sendfd unveil", NULL) == -1)
5449 cwd = getcwd(NULL, 0);
5451 error = got_error_from_errno("getcwd");
5455 if (repo_path == NULL) {
5456 error = got_worktree_open(&worktree, cwd);
5457 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5463 strdup(got_worktree_get_repo_path(worktree));
5464 if (repo_path == NULL)
5465 error = got_error_from_errno("strdup");
5469 repo_path = strdup(cwd);
5470 if (repo_path == NULL) {
5471 error = got_error_from_errno("strdup");
5477 error = got_repo_open(&repo, repo_path, NULL);
5481 error = apply_unveil(got_repo_get_path(repo), do_list,
5482 worktree ? got_worktree_get_root_path(worktree) : NULL);
5487 error = list_refs(repo, refname);
5489 error = delete_ref(repo, refname);
5490 else if (symref_target)
5491 error = add_symref(repo, refname, symref_target);
5493 if (obj_arg == NULL)
5495 error = add_ref(repo, refname, obj_arg);
5500 got_repo_close(repo);
5502 got_worktree_close(worktree);
5512 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5513 "[name]\n", getprogname());
5517 static const struct got_error *
5518 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5519 struct got_reference *ref)
5521 const struct got_error *err = NULL;
5522 const char *refname, *marker = " ";
5525 refname = got_ref_get_name(ref);
5526 if (worktree && strcmp(refname,
5527 got_worktree_get_head_ref_name(worktree)) == 0) {
5528 struct got_object_id *id = NULL;
5530 err = got_ref_resolve(&id, repo, ref);
5533 if (got_object_id_cmp(id,
5534 got_worktree_get_base_commit_id(worktree)) == 0)
5541 if (strncmp(refname, "refs/heads/", 11) == 0)
5543 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5546 refstr = got_ref_to_str(ref);
5548 return got_error_from_errno("got_ref_to_str");
5550 printf("%s%s: %s\n", marker, refname, refstr);
5555 static const struct got_error *
5556 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5558 const char *refname;
5560 if (worktree == NULL)
5561 return got_error(GOT_ERR_NOT_WORKTREE);
5563 refname = got_worktree_get_head_ref_name(worktree);
5565 if (strncmp(refname, "refs/heads/", 11) == 0)
5567 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5570 printf("%s\n", refname);
5575 static const struct got_error *
5576 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5578 static const struct got_error *err = NULL;
5579 struct got_reflist_head refs;
5580 struct got_reflist_entry *re;
5581 struct got_reference *temp_ref = NULL;
5582 int rebase_in_progress, histedit_in_progress;
5587 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5592 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5597 if (rebase_in_progress || histedit_in_progress) {
5598 err = got_ref_open(&temp_ref, repo,
5599 got_worktree_get_head_ref_name(worktree), 0);
5602 list_branch(repo, worktree, temp_ref);
5603 got_ref_close(temp_ref);
5607 err = got_ref_list(&refs, repo, "refs/heads",
5608 got_ref_cmp_by_name, NULL);
5612 TAILQ_FOREACH(re, &refs, entry)
5613 list_branch(repo, worktree, re->ref);
5615 got_ref_list_free(&refs);
5619 static const struct got_error *
5620 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5621 const char *branch_name)
5623 const struct got_error *err = NULL;
5624 struct got_reference *ref = NULL;
5627 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5628 return got_error_from_errno("asprintf");
5630 err = got_ref_open(&ref, repo, refname, 0);
5635 strcmp(got_worktree_get_head_ref_name(worktree),
5636 got_ref_get_name(ref)) == 0) {
5637 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5638 "will not delete this work tree's current branch");
5642 err = got_ref_delete(ref, repo);
5650 static const struct got_error *
5651 add_branch(struct got_repository *repo, const char *branch_name,
5652 struct got_object_id *base_commit_id)
5654 const struct got_error *err = NULL;
5655 struct got_reference *ref = NULL;
5656 char *base_refname = NULL, *refname = NULL;
5659 * Don't let the user create a branch name with a leading '-'.
5660 * While technically a valid reference name, this case is usually
5661 * an unintended typo.
5663 if (branch_name[0] == '-')
5664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5667 err = got_error_from_errno("asprintf");
5671 err = got_ref_open(&ref, repo, refname, 0);
5673 err = got_error(GOT_ERR_BRANCH_EXISTS);
5675 } else if (err->code != GOT_ERR_NOT_REF)
5678 err = got_ref_alloc(&ref, refname, base_commit_id);
5682 err = got_ref_write(ref, repo);
5691 static const struct got_error *
5692 cmd_branch(int argc, char *argv[])
5694 const struct got_error *error = NULL;
5695 struct got_repository *repo = NULL;
5696 struct got_worktree *worktree = NULL;
5697 char *cwd = NULL, *repo_path = NULL;
5698 int ch, do_list = 0, do_show = 0, do_update = 1;
5699 const char *delref = NULL, *commit_id_arg = NULL;
5700 struct got_reference *ref = NULL;
5701 struct got_pathlist_head paths;
5702 struct got_pathlist_entry *pe;
5703 struct got_object_id *commit_id = NULL;
5704 char *commit_id_str = NULL;
5708 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5711 commit_id_arg = optarg;
5717 repo_path = realpath(optarg, NULL);
5718 if (repo_path == NULL)
5719 return got_error_from_errno2("realpath",
5721 got_path_strip_trailing_slashes(repo_path);
5735 if (do_list && delref)
5736 option_conflict('l', 'd');
5741 if (!do_list && !delref && argc == 0)
5744 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5745 errx(1, "-c option can only be used when creating a branch");
5747 if (do_list || delref) {
5750 } else if (!do_show && argc != 1)
5754 if (do_list || do_show) {
5755 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5759 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5760 "sendfd unveil", NULL) == -1)
5764 cwd = getcwd(NULL, 0);
5766 error = got_error_from_errno("getcwd");
5770 if (repo_path == NULL) {
5771 error = got_worktree_open(&worktree, cwd);
5772 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5778 strdup(got_worktree_get_repo_path(worktree));
5779 if (repo_path == NULL)
5780 error = got_error_from_errno("strdup");
5784 repo_path = strdup(cwd);
5785 if (repo_path == NULL) {
5786 error = got_error_from_errno("strdup");
5792 error = got_repo_open(&repo, repo_path, NULL);
5796 error = apply_unveil(got_repo_get_path(repo), do_list,
5797 worktree ? got_worktree_get_root_path(worktree) : NULL);
5802 error = show_current_branch(repo, worktree);
5804 error = list_branches(repo, worktree);
5806 error = delete_branch(repo, worktree, delref);
5808 struct got_reflist_head refs;
5810 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5814 if (commit_id_arg == NULL)
5815 commit_id_arg = worktree ?
5816 got_worktree_get_head_ref_name(worktree) :
5818 error = got_repo_match_object_id(&commit_id, NULL,
5819 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5820 got_ref_list_free(&refs);
5823 error = add_branch(repo, argv[0], commit_id);
5826 if (worktree && do_update) {
5827 struct got_update_progress_arg upa;
5828 char *branch_refname = NULL;
5830 error = got_object_id_str(&commit_id_str, commit_id);
5833 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5837 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5839 error = got_error_from_errno("asprintf");
5842 error = got_ref_open(&ref, repo, branch_refname, 0);
5843 free(branch_refname);
5846 error = switch_head_ref(ref, commit_id, worktree,
5850 error = got_worktree_set_base_commit_id(worktree, repo,
5854 memset(&upa, 0, sizeof(upa));
5855 error = got_worktree_checkout_files(worktree, &paths,
5856 repo, update_progress, &upa, check_cancelled,
5860 if (upa.did_something)
5861 printf("Updated to commit %s\n", commit_id_str);
5862 print_update_progress_stats(&upa);
5869 got_repo_close(repo);
5871 got_worktree_close(worktree);
5875 free(commit_id_str);
5876 TAILQ_FOREACH(pe, &paths, entry)
5877 free((char *)pe->path);
5878 got_pathlist_free(&paths);
5887 "usage: %s tag [-c commit] [-r repository] [-l] "
5888 "[-m message] name\n", getprogname());
5893 static const struct got_error *
5894 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5896 const struct got_error *err = NULL;
5897 struct got_reflist_entry *re, *se, *new;
5898 struct got_object_id *re_id, *se_id;
5899 struct got_tag_object *re_tag, *se_tag;
5900 time_t re_time, se_time;
5902 SIMPLEQ_FOREACH(re, tags, entry) {
5903 se = SIMPLEQ_FIRST(sorted);
5905 err = got_reflist_entry_dup(&new, re);
5908 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5911 err = got_ref_resolve(&re_id, repo, re->ref);
5914 err = got_object_open_as_tag(&re_tag, repo, re_id);
5918 re_time = got_object_tag_get_tagger_time(re_tag);
5919 got_object_tag_close(re_tag);
5923 err = got_ref_resolve(&se_id, repo, re->ref);
5926 err = got_object_open_as_tag(&se_tag, repo, se_id);
5930 se_time = got_object_tag_get_tagger_time(se_tag);
5931 got_object_tag_close(se_tag);
5933 if (se_time > re_time) {
5934 err = got_reflist_entry_dup(&new, re);
5937 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5940 se = SIMPLEQ_NEXT(se, entry);
5949 static const struct got_error *
5950 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5952 static const struct got_error *err = NULL;
5953 struct got_reflist_head refs;
5954 struct got_reflist_entry *re;
5958 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5962 TAILQ_FOREACH(re, &refs, entry) {
5963 const char *refname;
5964 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5968 struct got_object_id *id;
5969 struct got_tag_object *tag;
5970 struct got_commit_object *commit = NULL;
5972 refname = got_ref_get_name(re->ref);
5973 if (strncmp(refname, "refs/tags/", 10) != 0)
5976 refstr = got_ref_to_str(re->ref);
5977 if (refstr == NULL) {
5978 err = got_error_from_errno("got_ref_to_str");
5981 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5984 err = got_ref_resolve(&id, repo, re->ref);
5987 err = got_object_open_as_tag(&tag, repo, id);
5989 if (err->code != GOT_ERR_OBJ_TYPE) {
5993 /* "lightweight" tag */
5994 err = got_object_open_as_commit(&commit, repo, id);
5999 tagger = got_object_commit_get_committer(commit);
6001 got_object_commit_get_committer_time(commit);
6002 err = got_object_id_str(&id_str, id);
6008 tagger = got_object_tag_get_tagger(tag);
6009 tagger_time = got_object_tag_get_tagger_time(tag);
6010 err = got_object_id_str(&id_str,
6011 got_object_tag_get_object_id(tag));
6015 printf("from: %s\n", tagger);
6016 datestr = get_datestr(&tagger_time, datebuf);
6018 printf("date: %s UTC\n", datestr);
6020 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6022 switch (got_object_tag_get_object_type(tag)) {
6023 case GOT_OBJ_TYPE_BLOB:
6024 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6027 case GOT_OBJ_TYPE_TREE:
6028 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6031 case GOT_OBJ_TYPE_COMMIT:
6032 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6035 case GOT_OBJ_TYPE_TAG:
6036 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6045 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6048 got_object_commit_close(commit);
6050 tagmsg0 = strdup(got_object_tag_get_message(tag));
6051 got_object_tag_close(tag);
6052 if (tagmsg0 == NULL) {
6053 err = got_error_from_errno("strdup");
6060 line = strsep(&tagmsg, "\n");
6062 printf(" %s\n", line);
6067 got_ref_list_free(&refs);
6071 static const struct got_error *
6072 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6073 const char *tag_name, const char *repo_path)
6075 const struct got_error *err = NULL;
6076 char *template = NULL, *initial_content = NULL;
6077 char *editor = NULL;
6078 int initial_content_len;
6081 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6082 err = got_error_from_errno("asprintf");
6086 initial_content_len = asprintf(&initial_content,
6087 "\n# tagging commit %s as %s\n",
6088 commit_id_str, tag_name);
6089 if (initial_content_len == -1) {
6090 err = got_error_from_errno("asprintf");
6094 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6098 if (write(fd, initial_content, initial_content_len) == -1) {
6099 err = got_error_from_errno2("write", *tagmsg_path);
6103 err = get_editor(&editor);
6106 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6107 initial_content_len, 1);
6109 free(initial_content);
6113 if (fd != -1 && close(fd) == -1 && err == NULL)
6114 err = got_error_from_errno2("close", *tagmsg_path);
6116 /* Editor is done; we can now apply unveil(2) */
6118 err = apply_unveil(repo_path, 0, NULL);
6126 static const struct got_error *
6127 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6128 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6130 const struct got_error *err = NULL;
6131 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6132 char *label = NULL, *commit_id_str = NULL;
6133 struct got_reference *ref = NULL;
6134 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6135 char *tagmsg_path = NULL, *tag_id_str = NULL;
6136 int preserve_tagmsg = 0;
6137 struct got_reflist_head refs;
6142 * Don't let the user create a tag name with a leading '-'.
6143 * While technically a valid reference name, this case is usually
6144 * an unintended typo.
6146 if (tag_name[0] == '-')
6147 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6149 err = get_author(&tagger, repo, worktree);
6153 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6157 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6158 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6162 err = got_object_id_str(&commit_id_str, commit_id);
6166 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6167 refname = strdup(tag_name);
6168 if (refname == NULL) {
6169 err = got_error_from_errno("strdup");
6173 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6174 err = got_error_from_errno("asprintf");
6178 err = got_ref_open(&ref, repo, refname, 0);
6180 err = got_error(GOT_ERR_TAG_EXISTS);
6182 } else if (err->code != GOT_ERR_NOT_REF)
6185 if (tagmsg_arg == NULL) {
6186 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6187 tag_name, got_repo_get_path(repo));
6189 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6190 tagmsg_path != NULL)
6191 preserve_tagmsg = 1;
6196 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6197 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6200 preserve_tagmsg = 1;
6204 err = got_ref_alloc(&ref, refname, tag_id);
6207 preserve_tagmsg = 1;
6211 err = got_ref_write(ref, repo);
6214 preserve_tagmsg = 1;
6218 err = got_object_id_str(&tag_id_str, tag_id);
6221 preserve_tagmsg = 1;
6224 printf("Created tag %s\n", tag_id_str);
6226 if (preserve_tagmsg) {
6227 fprintf(stderr, "%s: tag message preserved in %s\n",
6228 getprogname(), tagmsg_path);
6229 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6230 err = got_error_from_errno2("unlink", tagmsg_path);
6235 free(commit_id_str);
6240 got_ref_list_free(&refs);
6244 static const struct got_error *
6245 cmd_tag(int argc, char *argv[])
6247 const struct got_error *error = NULL;
6248 struct got_repository *repo = NULL;
6249 struct got_worktree *worktree = NULL;
6250 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6251 char *gitconfig_path = NULL;
6252 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6253 int ch, do_list = 0;
6255 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6258 commit_id_arg = optarg;
6264 repo_path = realpath(optarg, NULL);
6265 if (repo_path == NULL)
6266 return got_error_from_errno2("realpath",
6268 got_path_strip_trailing_slashes(repo_path);
6283 if (commit_id_arg != NULL)
6285 "-c option can only be used when creating a tag");
6287 option_conflict('l', 'm');
6290 } else if (argc != 1)
6297 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6301 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6302 "sendfd unveil", NULL) == -1)
6306 cwd = getcwd(NULL, 0);
6308 error = got_error_from_errno("getcwd");
6312 if (repo_path == NULL) {
6313 error = got_worktree_open(&worktree, cwd);
6314 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6320 strdup(got_worktree_get_repo_path(worktree));
6321 if (repo_path == NULL)
6322 error = got_error_from_errno("strdup");
6326 repo_path = strdup(cwd);
6327 if (repo_path == NULL) {
6328 error = got_error_from_errno("strdup");
6335 error = got_repo_open(&repo, repo_path, NULL);
6338 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6341 error = list_tags(repo, worktree);
6343 error = get_gitconfig_path(&gitconfig_path);
6346 error = got_repo_open(&repo, repo_path, gitconfig_path);
6351 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6356 if (commit_id_arg == NULL) {
6357 struct got_reference *head_ref;
6358 struct got_object_id *commit_id;
6359 error = got_ref_open(&head_ref, repo,
6360 worktree ? got_worktree_get_head_ref_name(worktree)
6364 error = got_ref_resolve(&commit_id, repo, head_ref);
6365 got_ref_close(head_ref);
6368 error = got_object_id_str(&commit_id_str, commit_id);
6374 error = add_tag(repo, worktree, tag_name,
6375 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6379 got_repo_close(repo);
6381 got_worktree_close(worktree);
6384 free(gitconfig_path);
6385 free(commit_id_str);
6392 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6397 static const struct got_error *
6398 add_progress(void *arg, unsigned char status, const char *path)
6400 while (path[0] == '/')
6402 printf("%c %s\n", status, path);
6406 static const struct got_error *
6407 cmd_add(int argc, char *argv[])
6409 const struct got_error *error = NULL;
6410 struct got_repository *repo = NULL;
6411 struct got_worktree *worktree = NULL;
6413 struct got_pathlist_head paths;
6414 struct got_pathlist_entry *pe;
6415 int ch, can_recurse = 0, no_ignores = 0;
6419 while ((ch = getopt(argc, argv, "IR")) != -1) {
6437 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6444 cwd = getcwd(NULL, 0);
6446 error = got_error_from_errno("getcwd");
6450 error = got_worktree_open(&worktree, cwd);
6452 if (error->code == GOT_ERR_NOT_WORKTREE)
6453 error = wrap_not_worktree_error(error, "add", cwd);
6457 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6462 error = apply_unveil(got_repo_get_path(repo), 1,
6463 got_worktree_get_root_path(worktree));
6467 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6471 if (!can_recurse && no_ignores) {
6472 error = got_error_msg(GOT_ERR_BAD_PATH,
6473 "disregarding ignores requires -R option");
6481 TAILQ_FOREACH(pe, &paths, entry) {
6482 if (asprintf(&ondisk_path, "%s/%s",
6483 got_worktree_get_root_path(worktree),
6485 error = got_error_from_errno("asprintf");
6488 if (lstat(ondisk_path, &sb) == -1) {
6489 if (errno == ENOENT) {
6493 error = got_error_from_errno2("lstat",
6499 if (S_ISDIR(sb.st_mode)) {
6500 error = got_error_msg(GOT_ERR_BAD_PATH,
6501 "adding directories requires -R option");
6507 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6508 NULL, repo, no_ignores);
6511 got_repo_close(repo);
6513 got_worktree_close(worktree);
6514 TAILQ_FOREACH(pe, &paths, entry)
6515 free((char *)pe->path);
6516 got_pathlist_free(&paths);
6524 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6525 "path ...\n", getprogname());
6529 static const struct got_error *
6530 print_remove_status(void *arg, unsigned char status,
6531 unsigned char staged_status, const char *path)
6533 while (path[0] == '/')
6535 if (status == GOT_STATUS_NONEXISTENT)
6537 if (status == staged_status && (status == GOT_STATUS_DELETE))
6538 status = GOT_STATUS_NO_CHANGE;
6539 printf("%c%c %s\n", status, staged_status, path);
6543 static const struct got_error *
6544 cmd_remove(int argc, char *argv[])
6546 const struct got_error *error = NULL;
6547 struct got_worktree *worktree = NULL;
6548 struct got_repository *repo = NULL;
6549 const char *status_codes = NULL;
6551 struct got_pathlist_head paths;
6552 struct got_pathlist_entry *pe;
6553 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6557 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6560 delete_local_mods = 1;
6569 for (i = 0; i < strlen(optarg); i++) {
6570 switch (optarg[i]) {
6571 case GOT_STATUS_MODIFY:
6572 delete_local_mods = 1;
6574 case GOT_STATUS_MISSING:
6577 errx(1, "invalid status code '%c'",
6581 status_codes = optarg;
6593 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6600 cwd = getcwd(NULL, 0);
6602 error = got_error_from_errno("getcwd");
6605 error = got_worktree_open(&worktree, cwd);
6607 if (error->code == GOT_ERR_NOT_WORKTREE)
6608 error = wrap_not_worktree_error(error, "remove", cwd);
6612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6617 error = apply_unveil(got_repo_get_path(repo), 1,
6618 got_worktree_get_root_path(worktree));
6622 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6629 TAILQ_FOREACH(pe, &paths, entry) {
6630 if (asprintf(&ondisk_path, "%s/%s",
6631 got_worktree_get_root_path(worktree),
6633 error = got_error_from_errno("asprintf");
6636 if (lstat(ondisk_path, &sb) == -1) {
6637 if (errno == ENOENT) {
6641 error = got_error_from_errno2("lstat",
6647 if (S_ISDIR(sb.st_mode)) {
6648 error = got_error_msg(GOT_ERR_BAD_PATH,
6649 "removing directories requires -R option");
6655 error = got_worktree_schedule_delete(worktree, &paths,
6656 delete_local_mods, status_codes, print_remove_status, NULL,
6657 repo, keep_on_disk);
6660 got_repo_close(repo);
6662 got_worktree_close(worktree);
6663 TAILQ_FOREACH(pe, &paths, entry)
6664 free((char *)pe->path);
6665 got_pathlist_free(&paths);
6673 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6674 "path ...\n", getprogname());
6678 static const struct got_error *
6679 revert_progress(void *arg, unsigned char status, const char *path)
6681 if (status == GOT_STATUS_UNVERSIONED)
6684 while (path[0] == '/')
6686 printf("%c %s\n", status, path);
6690 struct choose_patch_arg {
6691 FILE *patch_script_file;
6695 static const struct got_error *
6696 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6697 int nchanges, const char *action)
6700 size_t linesize = 0;
6704 case GOT_STATUS_ADD:
6705 printf("A %s\n%s this addition? [y/n] ", path, action);
6707 case GOT_STATUS_DELETE:
6708 printf("D %s\n%s this deletion? [y/n] ", path, action);
6710 case GOT_STATUS_MODIFY:
6711 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6712 return got_error_from_errno("fseek");
6713 printf(GOT_COMMIT_SEP_STR);
6714 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6716 if (ferror(patch_file))
6717 return got_error_from_errno("getline");
6718 printf(GOT_COMMIT_SEP_STR);
6719 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6720 path, n, nchanges, action);
6723 return got_error_path(path, GOT_ERR_FILE_STATUS);
6729 static const struct got_error *
6730 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6731 FILE *patch_file, int n, int nchanges)
6733 const struct got_error *err = NULL;
6735 size_t linesize = 0;
6738 struct choose_patch_arg *a = arg;
6740 *choice = GOT_PATCH_CHOICE_NONE;
6742 if (a->patch_script_file) {
6744 err = show_change(status, path, patch_file, n, nchanges,
6748 linelen = getline(&line, &linesize, a->patch_script_file);
6749 if (linelen == -1) {
6750 if (ferror(a->patch_script_file))
6751 return got_error_from_errno("getline");
6754 nl = strchr(line, '\n');
6757 if (strcmp(line, "y") == 0) {
6758 *choice = GOT_PATCH_CHOICE_YES;
6760 } else if (strcmp(line, "n") == 0) {
6761 *choice = GOT_PATCH_CHOICE_NO;
6763 } else if (strcmp(line, "q") == 0 &&
6764 status == GOT_STATUS_MODIFY) {
6765 *choice = GOT_PATCH_CHOICE_QUIT;
6768 printf("invalid response '%s'\n", line);
6773 while (resp != 'y' && resp != 'n' && resp != 'q') {
6774 err = show_change(status, path, patch_file, n, nchanges,
6781 if (status == GOT_STATUS_MODIFY) {
6782 if (resp != 'y' && resp != 'n' && resp != 'q') {
6783 printf("invalid response '%c'\n", resp);
6786 } else if (resp != 'y' && resp != 'n') {
6787 printf("invalid response '%c'\n", resp);
6793 *choice = GOT_PATCH_CHOICE_YES;
6794 else if (resp == 'n')
6795 *choice = GOT_PATCH_CHOICE_NO;
6796 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6797 *choice = GOT_PATCH_CHOICE_QUIT;
6803 static const struct got_error *
6804 cmd_revert(int argc, char *argv[])
6806 const struct got_error *error = NULL;
6807 struct got_worktree *worktree = NULL;
6808 struct got_repository *repo = NULL;
6809 char *cwd = NULL, *path = NULL;
6810 struct got_pathlist_head paths;
6811 struct got_pathlist_entry *pe;
6812 int ch, can_recurse = 0, pflag = 0;
6813 FILE *patch_script_file = NULL;
6814 const char *patch_script_path = NULL;
6815 struct choose_patch_arg cpa;
6819 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6825 patch_script_path = optarg;
6840 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6841 "unveil", NULL) == -1)
6846 if (patch_script_path && !pflag)
6847 errx(1, "-F option can only be used together with -p option");
6849 cwd = getcwd(NULL, 0);
6851 error = got_error_from_errno("getcwd");
6854 error = got_worktree_open(&worktree, cwd);
6856 if (error->code == GOT_ERR_NOT_WORKTREE)
6857 error = wrap_not_worktree_error(error, "revert", cwd);
6861 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6866 if (patch_script_path) {
6867 patch_script_file = fopen(patch_script_path, "r");
6868 if (patch_script_file == NULL) {
6869 error = got_error_from_errno2("fopen",
6874 error = apply_unveil(got_repo_get_path(repo), 1,
6875 got_worktree_get_root_path(worktree));
6879 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6886 TAILQ_FOREACH(pe, &paths, entry) {
6887 if (asprintf(&ondisk_path, "%s/%s",
6888 got_worktree_get_root_path(worktree),
6890 error = got_error_from_errno("asprintf");
6893 if (lstat(ondisk_path, &sb) == -1) {
6894 if (errno == ENOENT) {
6898 error = got_error_from_errno2("lstat",
6904 if (S_ISDIR(sb.st_mode)) {
6905 error = got_error_msg(GOT_ERR_BAD_PATH,
6906 "reverting directories requires -R option");
6912 cpa.patch_script_file = patch_script_file;
6913 cpa.action = "revert";
6914 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6915 pflag ? choose_patch : NULL, &cpa, repo);
6917 if (patch_script_file && fclose(patch_script_file) == EOF &&
6919 error = got_error_from_errno2("fclose", patch_script_path);
6921 got_repo_close(repo);
6923 got_worktree_close(worktree);
6932 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6937 struct collect_commit_logmsg_arg {
6938 const char *cmdline_log;
6940 const char *worktree_path;
6941 const char *branch_name;
6942 const char *repo_path;
6947 static const struct got_error *
6948 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6951 char *initial_content = NULL;
6952 struct got_pathlist_entry *pe;
6953 const struct got_error *err = NULL;
6954 char *template = NULL;
6955 struct collect_commit_logmsg_arg *a = arg;
6956 int initial_content_len;
6960 /* if a message was specified on the command line, just use it */
6961 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6962 len = strlen(a->cmdline_log) + 1;
6963 *logmsg = malloc(len + 1);
6964 if (*logmsg == NULL)
6965 return got_error_from_errno("malloc");
6966 strlcpy(*logmsg, a->cmdline_log, len);
6970 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6971 return got_error_from_errno("asprintf");
6973 initial_content_len = asprintf(&initial_content,
6974 "\n# changes to be committed on branch %s:\n",
6976 if (initial_content_len == -1)
6977 return got_error_from_errno("asprintf");
6979 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6983 if (write(fd, initial_content, initial_content_len) == -1) {
6984 err = got_error_from_errno2("write", a->logmsg_path);
6988 TAILQ_FOREACH(pe, commitable_paths, entry) {
6989 struct got_commitable *ct = pe->data;
6990 dprintf(fd, "# %c %s\n",
6991 got_commitable_get_status(ct),
6992 got_commitable_get_path(ct));
6995 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6996 initial_content_len, 1);
6998 free(initial_content);
7001 if (fd != -1 && close(fd) == -1 && err == NULL)
7002 err = got_error_from_errno2("close", a->logmsg_path);
7004 /* Editor is done; we can now apply unveil(2) */
7006 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7014 static const struct got_error *
7015 cmd_commit(int argc, char *argv[])
7017 const struct got_error *error = NULL;
7018 struct got_worktree *worktree = NULL;
7019 struct got_repository *repo = NULL;
7020 char *cwd = NULL, *id_str = NULL;
7021 struct got_object_id *id = NULL;
7022 const char *logmsg = NULL;
7023 struct collect_commit_logmsg_arg cl_arg;
7024 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7025 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7026 int allow_bad_symlinks = 0;
7027 struct got_pathlist_head paths;
7030 cl_arg.logmsg_path = NULL;
7032 while ((ch = getopt(argc, argv, "m:S")) != -1) {
7038 allow_bad_symlinks = 1;
7050 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7051 "unveil", NULL) == -1)
7054 cwd = getcwd(NULL, 0);
7056 error = got_error_from_errno("getcwd");
7059 error = got_worktree_open(&worktree, cwd);
7061 if (error->code == GOT_ERR_NOT_WORKTREE)
7062 error = wrap_not_worktree_error(error, "commit", cwd);
7066 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7069 if (rebase_in_progress) {
7070 error = got_error(GOT_ERR_REBASING);
7074 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7079 error = get_gitconfig_path(&gitconfig_path);
7082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7087 error = get_author(&author, repo, worktree);
7092 * unveil(2) traverses exec(2); if an editor is used we have
7093 * to apply unveil after the log message has been written.
7095 if (logmsg == NULL || strlen(logmsg) == 0)
7096 error = get_editor(&editor);
7098 error = apply_unveil(got_repo_get_path(repo), 0,
7099 got_worktree_get_root_path(worktree));
7103 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7107 cl_arg.editor = editor;
7108 cl_arg.cmdline_log = logmsg;
7109 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7110 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7111 if (!histedit_in_progress) {
7112 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7113 error = got_error(GOT_ERR_COMMIT_BRANCH);
7116 cl_arg.branch_name += 11;
7118 cl_arg.repo_path = got_repo_get_path(repo);
7119 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7120 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7121 print_status, NULL, repo);
7123 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7124 cl_arg.logmsg_path != NULL)
7125 preserve_logmsg = 1;
7129 error = got_object_id_str(&id_str, id);
7132 printf("Created commit %s\n", id_str);
7134 if (preserve_logmsg) {
7135 fprintf(stderr, "%s: log message preserved in %s\n",
7136 getprogname(), cl_arg.logmsg_path);
7137 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7139 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7140 free(cl_arg.logmsg_path);
7142 got_repo_close(repo);
7144 got_worktree_close(worktree);
7147 free(gitconfig_path);
7154 usage_cherrypick(void)
7156 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7160 static const struct got_error *
7161 cmd_cherrypick(int argc, char *argv[])
7163 const struct got_error *error = NULL;
7164 struct got_worktree *worktree = NULL;
7165 struct got_repository *repo = NULL;
7166 char *cwd = NULL, *commit_id_str = NULL;
7167 struct got_object_id *commit_id = NULL;
7168 struct got_commit_object *commit = NULL;
7169 struct got_object_qid *pid;
7170 struct got_reference *head_ref = NULL;
7172 struct got_update_progress_arg upa;
7174 while ((ch = getopt(argc, argv, "")) != -1) {
7186 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7187 "unveil", NULL) == -1)
7193 cwd = getcwd(NULL, 0);
7195 error = got_error_from_errno("getcwd");
7198 error = got_worktree_open(&worktree, cwd);
7200 if (error->code == GOT_ERR_NOT_WORKTREE)
7201 error = wrap_not_worktree_error(error, "cherrypick",
7206 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7211 error = apply_unveil(got_repo_get_path(repo), 0,
7212 got_worktree_get_root_path(worktree));
7216 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7217 GOT_OBJ_TYPE_COMMIT, repo);
7218 if (error != NULL) {
7219 struct got_reference *ref;
7220 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7222 error = got_ref_open(&ref, repo, argv[0], 0);
7225 error = got_ref_resolve(&commit_id, repo, ref);
7230 error = got_object_id_str(&commit_id_str, commit_id);
7234 error = got_ref_open(&head_ref, repo,
7235 got_worktree_get_head_ref_name(worktree), 0);
7239 error = check_same_branch(commit_id, head_ref, NULL, repo);
7241 if (error->code != GOT_ERR_ANCESTRY)
7245 error = got_error(GOT_ERR_SAME_BRANCH);
7249 error = got_object_open_as_commit(&commit, repo, commit_id);
7252 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7253 memset(&upa, 0, sizeof(upa));
7254 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7255 commit_id, repo, update_progress, &upa, check_cancelled,
7260 if (upa.did_something)
7261 printf("Merged commit %s\n", commit_id_str);
7262 print_update_progress_stats(&upa);
7265 got_object_commit_close(commit);
7266 free(commit_id_str);
7268 got_ref_close(head_ref);
7270 got_worktree_close(worktree);
7272 got_repo_close(repo);
7279 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7283 static const struct got_error *
7284 cmd_backout(int argc, char *argv[])
7286 const struct got_error *error = NULL;
7287 struct got_worktree *worktree = NULL;
7288 struct got_repository *repo = NULL;
7289 char *cwd = NULL, *commit_id_str = NULL;
7290 struct got_object_id *commit_id = NULL;
7291 struct got_commit_object *commit = NULL;
7292 struct got_object_qid *pid;
7293 struct got_reference *head_ref = NULL;
7295 struct got_update_progress_arg upa;
7297 while ((ch = getopt(argc, argv, "")) != -1) {
7309 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7310 "unveil", NULL) == -1)
7316 cwd = getcwd(NULL, 0);
7318 error = got_error_from_errno("getcwd");
7321 error = got_worktree_open(&worktree, cwd);
7323 if (error->code == GOT_ERR_NOT_WORKTREE)
7324 error = wrap_not_worktree_error(error, "backout", cwd);
7328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7333 error = apply_unveil(got_repo_get_path(repo), 0,
7334 got_worktree_get_root_path(worktree));
7338 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7339 GOT_OBJ_TYPE_COMMIT, repo);
7340 if (error != NULL) {
7341 struct got_reference *ref;
7342 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7344 error = got_ref_open(&ref, repo, argv[0], 0);
7347 error = got_ref_resolve(&commit_id, repo, ref);
7352 error = got_object_id_str(&commit_id_str, commit_id);
7356 error = got_ref_open(&head_ref, repo,
7357 got_worktree_get_head_ref_name(worktree), 0);
7361 error = check_same_branch(commit_id, head_ref, NULL, repo);
7365 error = got_object_open_as_commit(&commit, repo, commit_id);
7368 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7370 error = got_error(GOT_ERR_ROOT_COMMIT);
7374 memset(&upa, 0, sizeof(upa));
7375 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7376 update_progress, &upa, check_cancelled, NULL);
7380 if (upa.did_something)
7381 printf("Backed out commit %s\n", commit_id_str);
7382 print_update_progress_stats(&upa);
7385 got_object_commit_close(commit);
7386 free(commit_id_str);
7388 got_ref_close(head_ref);
7390 got_worktree_close(worktree);
7392 got_repo_close(repo);
7399 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7405 trim_logmsg(char *logmsg, int limit)
7410 len = strlen(logmsg);
7414 nl = strchr(logmsg, '\n');
7419 static const struct got_error *
7420 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7422 const struct got_error *err;
7423 char *logmsg0 = NULL;
7426 err = got_object_commit_get_logmsg(&logmsg0, commit);
7431 while (isspace((unsigned char)s[0]))
7434 *logmsg = strdup(s);
7435 if (*logmsg == NULL) {
7436 err = got_error_from_errno("strdup");
7440 trim_logmsg(*logmsg, limit);
7446 static const struct got_error *
7447 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7449 const struct got_error *err;
7450 struct got_commit_object *commit = NULL;
7451 char *id_str = NULL, *logmsg = NULL;
7453 err = got_object_open_as_commit(&commit, repo, id);
7457 err = got_object_id_str(&id_str, id);
7463 err = get_short_logmsg(&logmsg, 42, commit);
7467 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7470 got_object_commit_close(commit);
7475 static const struct got_error *
7476 show_rebase_progress(struct got_commit_object *commit,
7477 struct got_object_id *old_id, struct got_object_id *new_id)
7479 const struct got_error *err;
7480 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7482 err = got_object_id_str(&old_id_str, old_id);
7487 err = got_object_id_str(&new_id_str, new_id);
7492 old_id_str[12] = '\0';
7494 new_id_str[12] = '\0';
7496 err = get_short_logmsg(&logmsg, 42, commit);
7500 printf("%s -> %s: %s\n", old_id_str,
7501 new_id_str ? new_id_str : "no-op change", logmsg);
7509 static const struct got_error *
7510 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7511 struct got_reference *branch, struct got_reference *new_base_branch,
7512 struct got_reference *tmp_branch, struct got_repository *repo)
7514 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7515 return got_worktree_rebase_complete(worktree, fileindex,
7516 new_base_branch, tmp_branch, branch, repo);
7519 static const struct got_error *
7520 rebase_commit(struct got_pathlist_head *merged_paths,
7521 struct got_worktree *worktree, struct got_fileindex *fileindex,
7522 struct got_reference *tmp_branch,
7523 struct got_object_id *commit_id, struct got_repository *repo)
7525 const struct got_error *error;
7526 struct got_commit_object *commit;
7527 struct got_object_id *new_commit_id;
7529 error = got_object_open_as_commit(&commit, repo, commit_id);
7533 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7534 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7536 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7538 error = show_rebase_progress(commit, commit_id, NULL);
7540 error = show_rebase_progress(commit, commit_id, new_commit_id);
7541 free(new_commit_id);
7544 got_object_commit_close(commit);
7548 struct check_path_prefix_arg {
7549 const char *path_prefix;
7554 static const struct got_error *
7555 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7556 struct got_blob_object *blob2, struct got_object_id *id1,
7557 struct got_object_id *id2, const char *path1, const char *path2,
7558 mode_t mode1, mode_t mode2, struct got_repository *repo)
7560 struct check_path_prefix_arg *a = arg;
7562 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7563 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7564 return got_error(a->errcode);
7569 static const struct got_error *
7570 check_path_prefix(struct got_object_id *parent_id,
7571 struct got_object_id *commit_id, const char *path_prefix,
7572 int errcode, struct got_repository *repo)
7574 const struct got_error *err;
7575 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7576 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7577 struct check_path_prefix_arg cpp_arg;
7579 if (got_path_is_root_dir(path_prefix))
7582 err = got_object_open_as_commit(&commit, repo, commit_id);
7586 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7590 err = got_object_open_as_tree(&tree1, repo,
7591 got_object_commit_get_tree_id(parent_commit));
7595 err = got_object_open_as_tree(&tree2, repo,
7596 got_object_commit_get_tree_id(commit));
7600 cpp_arg.path_prefix = path_prefix;
7601 while (cpp_arg.path_prefix[0] == '/')
7602 cpp_arg.path_prefix++;
7603 cpp_arg.len = strlen(cpp_arg.path_prefix);
7604 cpp_arg.errcode = errcode;
7605 err = got_diff_tree(tree1, tree2, "", "", repo,
7606 check_path_prefix_in_diff, &cpp_arg, 0);
7609 got_object_tree_close(tree1);
7611 got_object_tree_close(tree2);
7613 got_object_commit_close(commit);
7615 got_object_commit_close(parent_commit);
7619 static const struct got_error *
7620 collect_commits(struct got_object_id_queue *commits,
7621 struct got_object_id *initial_commit_id,
7622 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7623 const char *path_prefix, int path_prefix_errcode,
7624 struct got_repository *repo)
7626 const struct got_error *err = NULL;
7627 struct got_commit_graph *graph = NULL;
7628 struct got_object_id *parent_id = NULL;
7629 struct got_object_qid *qid;
7630 struct got_object_id *commit_id = initial_commit_id;
7632 err = got_commit_graph_open(&graph, "/", 1);
7636 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7637 check_cancelled, NULL);
7640 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7641 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7642 check_cancelled, NULL);
7644 if (err->code == GOT_ERR_ITER_COMPLETED) {
7645 err = got_error_msg(GOT_ERR_ANCESTRY,
7646 "ran out of commits to rebase before "
7647 "youngest common ancestor commit has "
7652 err = check_path_prefix(parent_id, commit_id,
7653 path_prefix, path_prefix_errcode, repo);
7657 err = got_object_qid_alloc(&qid, commit_id);
7660 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7661 commit_id = parent_id;
7665 got_commit_graph_close(graph);
7669 static const struct got_error *
7670 cmd_rebase(int argc, char *argv[])
7672 const struct got_error *error = NULL;
7673 struct got_worktree *worktree = NULL;
7674 struct got_repository *repo = NULL;
7675 struct got_fileindex *fileindex = NULL;
7677 struct got_reference *branch = NULL;
7678 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7679 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7680 struct got_object_id *resume_commit_id = NULL;
7681 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7682 struct got_commit_object *commit = NULL;
7683 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7684 int histedit_in_progress = 0;
7685 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7686 struct got_object_id_queue commits;
7687 struct got_pathlist_head merged_paths;
7688 const struct got_object_id_queue *parent_ids;
7689 struct got_object_qid *qid, *pid;
7691 SIMPLEQ_INIT(&commits);
7692 TAILQ_INIT(&merged_paths);
7694 while ((ch = getopt(argc, argv, "ac")) != -1) {
7700 continue_rebase = 1;
7712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7713 "unveil", NULL) == -1)
7716 if (abort_rebase && continue_rebase)
7718 else if (abort_rebase || continue_rebase) {
7721 } else if (argc != 1)
7724 cwd = getcwd(NULL, 0);
7726 error = got_error_from_errno("getcwd");
7729 error = got_worktree_open(&worktree, cwd);
7731 if (error->code == GOT_ERR_NOT_WORKTREE)
7732 error = wrap_not_worktree_error(error, "rebase", cwd);
7736 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7741 error = apply_unveil(got_repo_get_path(repo), 0,
7742 got_worktree_get_root_path(worktree));
7746 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7750 if (histedit_in_progress) {
7751 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7755 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7760 struct got_update_progress_arg upa;
7761 if (!rebase_in_progress) {
7762 error = got_error(GOT_ERR_NOT_REBASING);
7765 error = got_worktree_rebase_continue(&resume_commit_id,
7766 &new_base_branch, &tmp_branch, &branch, &fileindex,
7770 printf("Switching work tree to %s\n",
7771 got_ref_get_symref_target(new_base_branch));
7772 memset(&upa, 0, sizeof(upa));
7773 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7774 new_base_branch, update_progress, &upa);
7777 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7778 print_update_progress_stats(&upa);
7779 goto done; /* nothing else to do */
7782 if (continue_rebase) {
7783 if (!rebase_in_progress) {
7784 error = got_error(GOT_ERR_NOT_REBASING);
7787 error = got_worktree_rebase_continue(&resume_commit_id,
7788 &new_base_branch, &tmp_branch, &branch, &fileindex,
7793 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7794 resume_commit_id, repo);
7798 yca_id = got_object_id_dup(resume_commit_id);
7799 if (yca_id == NULL) {
7800 error = got_error_from_errno("got_object_id_dup");
7804 error = got_ref_open(&branch, repo, argv[0], 0);
7809 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7813 if (!continue_rebase) {
7814 struct got_object_id *base_commit_id;
7816 base_commit_id = got_worktree_get_base_commit_id(worktree);
7817 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7818 base_commit_id, branch_head_commit_id, repo,
7819 check_cancelled, NULL);
7822 if (yca_id == NULL) {
7823 error = got_error_msg(GOT_ERR_ANCESTRY,
7824 "specified branch shares no common ancestry "
7825 "with work tree's branch");
7829 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7831 if (error->code != GOT_ERR_ANCESTRY)
7835 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7836 "specified branch resolves to a commit which "
7837 "is already contained in work tree's branch");
7840 error = got_worktree_rebase_prepare(&new_base_branch,
7841 &tmp_branch, &fileindex, worktree, branch, repo);
7846 commit_id = branch_head_commit_id;
7847 error = got_object_open_as_commit(&commit, repo, commit_id);
7851 parent_ids = got_object_commit_get_parent_ids(commit);
7852 pid = SIMPLEQ_FIRST(parent_ids);
7854 if (!continue_rebase) {
7855 struct got_update_progress_arg upa;
7856 memset(&upa, 0, sizeof(upa));
7857 error = got_worktree_rebase_abort(worktree, fileindex,
7858 repo, new_base_branch, update_progress, &upa);
7861 printf("Rebase of %s aborted\n",
7862 got_ref_get_name(branch));
7863 print_update_progress_stats(&upa);
7866 error = got_error(GOT_ERR_EMPTY_REBASE);
7869 error = collect_commits(&commits, commit_id, pid->id,
7870 yca_id, got_worktree_get_path_prefix(worktree),
7871 GOT_ERR_REBASE_PATH, repo);
7872 got_object_commit_close(commit);
7877 if (SIMPLEQ_EMPTY(&commits)) {
7878 if (continue_rebase) {
7879 error = rebase_complete(worktree, fileindex,
7880 branch, new_base_branch, tmp_branch, repo);
7883 /* Fast-forward the reference of the branch. */
7884 struct got_object_id *new_head_commit_id;
7886 error = got_ref_resolve(&new_head_commit_id, repo,
7890 error = got_object_id_str(&id_str, new_head_commit_id);
7891 printf("Forwarding %s to commit %s\n",
7892 got_ref_get_name(branch), id_str);
7894 error = got_ref_change_ref(branch,
7895 new_head_commit_id);
7902 SIMPLEQ_FOREACH(qid, &commits, entry) {
7903 struct got_update_progress_arg upa;
7905 commit_id = qid->id;
7906 parent_id = pid ? pid->id : yca_id;
7909 memset(&upa, 0, sizeof(upa));
7910 error = got_worktree_rebase_merge_files(&merged_paths,
7911 worktree, fileindex, parent_id, commit_id, repo,
7912 update_progress, &upa, check_cancelled, NULL);
7916 print_update_progress_stats(&upa);
7917 if (upa.conflicts > 0)
7918 rebase_status = GOT_STATUS_CONFLICT;
7920 if (rebase_status == GOT_STATUS_CONFLICT) {
7921 error = show_rebase_merge_conflict(qid->id, repo);
7924 got_worktree_rebase_pathlist_free(&merged_paths);
7928 error = rebase_commit(&merged_paths, worktree, fileindex,
7929 tmp_branch, commit_id, repo);
7930 got_worktree_rebase_pathlist_free(&merged_paths);
7935 if (rebase_status == GOT_STATUS_CONFLICT) {
7936 error = got_worktree_rebase_postpone(worktree, fileindex);
7939 error = got_error_msg(GOT_ERR_CONFLICTS,
7940 "conflicts must be resolved before rebasing can continue");
7942 error = rebase_complete(worktree, fileindex, branch,
7943 new_base_branch, tmp_branch, repo);
7945 got_object_id_queue_free(&commits);
7946 free(branch_head_commit_id);
7947 free(resume_commit_id);
7950 got_object_commit_close(commit);
7952 got_ref_close(branch);
7953 if (new_base_branch)
7954 got_ref_close(new_base_branch);
7956 got_ref_close(tmp_branch);
7958 got_worktree_close(worktree);
7960 got_repo_close(repo);
7965 usage_histedit(void)
7967 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7972 #define GOT_HISTEDIT_PICK 'p'
7973 #define GOT_HISTEDIT_EDIT 'e'
7974 #define GOT_HISTEDIT_FOLD 'f'
7975 #define GOT_HISTEDIT_DROP 'd'
7976 #define GOT_HISTEDIT_MESG 'm'
7978 static struct got_histedit_cmd {
7982 } got_histedit_cmds[] = {
7983 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7984 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7985 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7987 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7988 { GOT_HISTEDIT_MESG, "mesg",
7989 "single-line log message for commit above (open editor if empty)" },
7992 struct got_histedit_list_entry {
7993 TAILQ_ENTRY(got_histedit_list_entry) entry;
7994 struct got_object_id *commit_id;
7995 const struct got_histedit_cmd *cmd;
7998 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8000 static const struct got_error *
8001 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8002 FILE *f, struct got_repository *repo)
8004 const struct got_error *err = NULL;
8005 char *logmsg = NULL, *id_str = NULL;
8006 struct got_commit_object *commit = NULL;
8009 err = got_object_open_as_commit(&commit, repo, commit_id);
8013 err = get_short_logmsg(&logmsg, 34, commit);
8017 err = got_object_id_str(&id_str, commit_id);
8021 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8023 err = got_ferror(f, GOT_ERR_IO);
8026 got_object_commit_close(commit);
8032 static const struct got_error *
8033 histedit_write_commit_list(struct got_object_id_queue *commits,
8034 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8036 const struct got_error *err = NULL;
8037 struct got_object_qid *qid;
8038 const char *histedit_cmd = NULL;
8040 if (SIMPLEQ_EMPTY(commits))
8041 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8043 SIMPLEQ_FOREACH(qid, commits, entry) {
8044 histedit_cmd = got_histedit_cmds[0].name;
8045 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8046 histedit_cmd = "fold";
8047 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8050 if (edit_logmsg_only) {
8051 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8053 err = got_ferror(f, GOT_ERR_IO);
8062 static const struct got_error *
8063 write_cmd_list(FILE *f, const char *branch_name,
8064 struct got_object_id_queue *commits)
8066 const struct got_error *err = NULL;
8070 struct got_object_qid *qid;
8072 qid = SIMPLEQ_FIRST(commits);
8073 err = got_object_id_str(&id_str, qid->id);
8078 "# Editing the history of branch '%s' starting at\n"
8080 "# Commits will be processed in order from top to "
8081 "bottom of this file.\n", branch_name, id_str);
8083 err = got_ferror(f, GOT_ERR_IO);
8087 n = fprintf(f, "# Available histedit commands:\n");
8089 err = got_ferror(f, GOT_ERR_IO);
8093 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8094 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8095 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8098 err = got_ferror(f, GOT_ERR_IO);
8107 static const struct got_error *
8108 histedit_syntax_error(int lineno)
8110 static char msg[42];
8113 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8115 if (ret == -1 || ret >= sizeof(msg))
8116 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8118 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8121 static const struct got_error *
8122 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8123 char *logmsg, struct got_repository *repo)
8125 const struct got_error *err;
8126 struct got_commit_object *folded_commit = NULL;
8127 char *id_str, *folded_logmsg = NULL;
8129 err = got_object_id_str(&id_str, hle->commit_id);
8133 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8137 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8140 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8141 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8142 folded_logmsg) == -1) {
8143 err = got_error_from_errno("asprintf");
8147 got_object_commit_close(folded_commit);
8149 free(folded_logmsg);
8153 static struct got_histedit_list_entry *
8154 get_folded_commits(struct got_histedit_list_entry *hle)
8156 struct got_histedit_list_entry *prev, *folded = NULL;
8158 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8159 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8160 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8161 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8163 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8169 static const struct got_error *
8170 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8171 struct got_repository *repo)
8173 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8174 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8175 const struct got_error *err = NULL;
8176 struct got_commit_object *commit = NULL;
8179 struct got_histedit_list_entry *folded = NULL;
8181 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8185 folded = get_folded_commits(hle);
8187 while (folded != hle) {
8188 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8189 folded = TAILQ_NEXT(folded, entry);
8192 err = append_folded_commit_msg(&new_msg, folded,
8198 folded = TAILQ_NEXT(folded, entry);
8202 err = got_object_id_str(&id_str, hle->commit_id);
8205 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8208 logmsg_len = asprintf(&new_msg,
8209 "%s\n# original log message of commit %s: %s",
8210 logmsg ? logmsg : "", id_str, orig_logmsg);
8211 if (logmsg_len == -1) {
8212 err = got_error_from_errno("asprintf");
8218 err = got_object_id_str(&id_str, hle->commit_id);
8222 err = got_opentemp_named_fd(&logmsg_path, &fd,
8223 GOT_TMPDIR_STR "/got-logmsg");
8227 write(fd, logmsg, logmsg_len);
8230 err = get_editor(&editor);
8234 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8237 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8240 hle->logmsg = strdup(new_msg);
8241 if (hle->logmsg == NULL)
8242 err = got_error_from_errno("strdup");
8245 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8246 err = got_error_from_errno2("unlink", logmsg_path);
8252 got_object_commit_close(commit);
8256 static const struct got_error *
8257 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8258 FILE *f, struct got_repository *repo)
8260 const struct got_error *err = NULL;
8261 char *line = NULL, *p, *end;
8265 const struct got_histedit_cmd *cmd;
8266 struct got_object_id *commit_id = NULL;
8267 struct got_histedit_list_entry *hle = NULL;
8270 len = getline(&line, &size, f);
8272 const struct got_error *getline_err;
8275 getline_err = got_error_from_errno("getline");
8276 err = got_ferror(f, getline_err->code);
8281 while (isspace((unsigned char)p[0]))
8283 if (p[0] == '#' || p[0] == '\0') {
8289 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8290 cmd = &got_histedit_cmds[i];
8291 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8292 isspace((unsigned char)p[strlen(cmd->name)])) {
8293 p += strlen(cmd->name);
8296 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8301 if (i == nitems(got_histedit_cmds)) {
8302 err = histedit_syntax_error(lineno);
8305 while (isspace((unsigned char)p[0]))
8307 if (cmd->code == GOT_HISTEDIT_MESG) {
8308 if (hle == NULL || hle->logmsg != NULL) {
8309 err = got_error(GOT_ERR_HISTEDIT_CMD);
8313 err = histedit_edit_logmsg(hle, repo);
8317 hle->logmsg = strdup(p);
8318 if (hle->logmsg == NULL) {
8319 err = got_error_from_errno("strdup");
8328 while (end[0] && !isspace((unsigned char)end[0]))
8332 err = got_object_resolve_id_str(&commit_id, repo, p);
8334 /* override error code */
8335 err = histedit_syntax_error(lineno);
8339 hle = malloc(sizeof(*hle));
8341 err = got_error_from_errno("malloc");
8345 hle->commit_id = commit_id;
8350 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8358 static const struct got_error *
8359 histedit_check_script(struct got_histedit_list *histedit_cmds,
8360 struct got_object_id_queue *commits, struct got_repository *repo)
8362 const struct got_error *err = NULL;
8363 struct got_object_qid *qid;
8364 struct got_histedit_list_entry *hle;
8365 static char msg[92];
8368 if (TAILQ_EMPTY(histedit_cmds))
8369 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8370 "histedit script contains no commands");
8371 if (SIMPLEQ_EMPTY(commits))
8372 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8374 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8375 struct got_histedit_list_entry *hle2;
8376 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8379 if (got_object_id_cmp(hle->commit_id,
8380 hle2->commit_id) != 0)
8382 err = got_object_id_str(&id_str, hle->commit_id);
8385 snprintf(msg, sizeof(msg), "commit %s is listed "
8386 "more than once in histedit script", id_str);
8388 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8392 SIMPLEQ_FOREACH(qid, commits, entry) {
8393 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8394 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8398 err = got_object_id_str(&id_str, qid->id);
8401 snprintf(msg, sizeof(msg),
8402 "commit %s missing from histedit script", id_str);
8404 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8408 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8409 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8410 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8411 "last commit in histedit script cannot be folded");
8416 static const struct got_error *
8417 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8418 const char *path, struct got_object_id_queue *commits,
8419 struct got_repository *repo)
8421 const struct got_error *err = NULL;
8425 err = get_editor(&editor);
8429 if (spawn_editor(editor, path) == -1) {
8430 err = got_error_from_errno("failed spawning editor");
8434 f = fopen(path, "r");
8436 err = got_error_from_errno("fopen");
8439 err = histedit_parse_list(histedit_cmds, f, repo);
8443 err = histedit_check_script(histedit_cmds, commits, repo);
8445 if (f && fclose(f) == EOF && err == NULL)
8446 err = got_error_from_errno("fclose");
8451 static const struct got_error *
8452 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8453 struct got_object_id_queue *, const char *, const char *,
8454 struct got_repository *);
8456 static const struct got_error *
8457 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8458 struct got_object_id_queue *commits, const char *branch_name,
8459 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8461 const struct got_error *err;
8465 err = got_opentemp_named(&path, &f, "got-histedit");
8469 err = write_cmd_list(f, branch_name, commits);
8473 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8478 if (edit_logmsg_only || fold_only) {
8480 err = histedit_parse_list(histedit_cmds, f, repo);
8482 if (fclose(f) == EOF) {
8483 err = got_error_from_errno("fclose");
8487 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8489 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8490 err->code != GOT_ERR_HISTEDIT_CMD)
8492 err = histedit_edit_list_retry(histedit_cmds, err,
8493 commits, path, branch_name, repo);
8497 if (f && fclose(f) == EOF && err == NULL)
8498 err = got_error_from_errno("fclose");
8499 if (path && unlink(path) != 0 && err == NULL)
8500 err = got_error_from_errno2("unlink", path);
8505 static const struct got_error *
8506 histedit_save_list(struct got_histedit_list *histedit_cmds,
8507 struct got_worktree *worktree, struct got_repository *repo)
8509 const struct got_error *err = NULL;
8512 struct got_histedit_list_entry *hle;
8513 struct got_commit_object *commit = NULL;
8515 err = got_worktree_get_histedit_script_path(&path, worktree);
8519 f = fopen(path, "w");
8521 err = got_error_from_errno2("fopen", path);
8524 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8525 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8531 int n = fprintf(f, "%c %s\n",
8532 GOT_HISTEDIT_MESG, hle->logmsg);
8534 err = got_ferror(f, GOT_ERR_IO);
8540 if (f && fclose(f) == EOF && err == NULL)
8541 err = got_error_from_errno("fclose");
8544 got_object_commit_close(commit);
8549 histedit_free_list(struct got_histedit_list *histedit_cmds)
8551 struct got_histedit_list_entry *hle;
8553 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8554 TAILQ_REMOVE(histedit_cmds, hle, entry);
8559 static const struct got_error *
8560 histedit_load_list(struct got_histedit_list *histedit_cmds,
8561 const char *path, struct got_repository *repo)
8563 const struct got_error *err = NULL;
8566 f = fopen(path, "r");
8568 err = got_error_from_errno2("fopen", path);
8572 err = histedit_parse_list(histedit_cmds, f, repo);
8574 if (f && fclose(f) == EOF && err == NULL)
8575 err = got_error_from_errno("fclose");
8579 static const struct got_error *
8580 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8581 const struct got_error *edit_err, struct got_object_id_queue *commits,
8582 const char *path, const char *branch_name, struct got_repository *repo)
8584 const struct got_error *err = NULL, *prev_err = edit_err;
8587 while (resp != 'c' && resp != 'r' && resp != 'a') {
8588 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8589 "or (a)bort: ", getprogname(), prev_err->msg);
8594 histedit_free_list(histedit_cmds);
8595 err = histedit_run_editor(histedit_cmds, path, commits,
8598 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8599 err->code != GOT_ERR_HISTEDIT_CMD)
8606 } else if (resp == 'r') {
8607 histedit_free_list(histedit_cmds);
8608 err = histedit_edit_script(histedit_cmds,
8609 commits, branch_name, 0, 0, repo);
8611 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8612 err->code != GOT_ERR_HISTEDIT_CMD)
8619 } else if (resp == 'a') {
8620 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8623 printf("invalid response '%c'\n", resp);
8629 static const struct got_error *
8630 histedit_complete(struct got_worktree *worktree,
8631 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8632 struct got_reference *branch, struct got_repository *repo)
8634 printf("Switching work tree to %s\n",
8635 got_ref_get_symref_target(branch));
8636 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8640 static const struct got_error *
8641 show_histedit_progress(struct got_commit_object *commit,
8642 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8644 const struct got_error *err;
8645 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8647 err = got_object_id_str(&old_id_str, hle->commit_id);
8652 err = got_object_id_str(&new_id_str, new_id);
8657 old_id_str[12] = '\0';
8659 new_id_str[12] = '\0';
8662 logmsg = strdup(hle->logmsg);
8663 if (logmsg == NULL) {
8664 err = got_error_from_errno("strdup");
8667 trim_logmsg(logmsg, 42);
8669 err = get_short_logmsg(&logmsg, 42, commit);
8674 switch (hle->cmd->code) {
8675 case GOT_HISTEDIT_PICK:
8676 case GOT_HISTEDIT_EDIT:
8677 printf("%s -> %s: %s\n", old_id_str,
8678 new_id_str ? new_id_str : "no-op change", logmsg);
8680 case GOT_HISTEDIT_DROP:
8681 case GOT_HISTEDIT_FOLD:
8682 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8694 static const struct got_error *
8695 histedit_commit(struct got_pathlist_head *merged_paths,
8696 struct got_worktree *worktree, struct got_fileindex *fileindex,
8697 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8698 struct got_repository *repo)
8700 const struct got_error *err;
8701 struct got_commit_object *commit;
8702 struct got_object_id *new_commit_id;
8704 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8705 && hle->logmsg == NULL) {
8706 err = histedit_edit_logmsg(hle, repo);
8711 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8715 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8716 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8719 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8721 err = show_histedit_progress(commit, hle, NULL);
8723 err = show_histedit_progress(commit, hle, new_commit_id);
8724 free(new_commit_id);
8727 got_object_commit_close(commit);
8731 static const struct got_error *
8732 histedit_skip_commit(struct got_histedit_list_entry *hle,
8733 struct got_worktree *worktree, struct got_repository *repo)
8735 const struct got_error *error;
8736 struct got_commit_object *commit;
8738 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8743 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8747 error = show_histedit_progress(commit, hle, NULL);
8748 got_object_commit_close(commit);
8752 static const struct got_error *
8753 check_local_changes(void *arg, unsigned char status,
8754 unsigned char staged_status, const char *path,
8755 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8756 struct got_object_id *commit_id, int dirfd, const char *de_name)
8758 int *have_local_changes = arg;
8761 case GOT_STATUS_ADD:
8762 case GOT_STATUS_DELETE:
8763 case GOT_STATUS_MODIFY:
8764 case GOT_STATUS_CONFLICT:
8765 *have_local_changes = 1;
8766 return got_error(GOT_ERR_CANCELLED);
8771 switch (staged_status) {
8772 case GOT_STATUS_ADD:
8773 case GOT_STATUS_DELETE:
8774 case GOT_STATUS_MODIFY:
8775 *have_local_changes = 1;
8776 return got_error(GOT_ERR_CANCELLED);
8784 static const struct got_error *
8785 cmd_histedit(int argc, char *argv[])
8787 const struct got_error *error = NULL;
8788 struct got_worktree *worktree = NULL;
8789 struct got_fileindex *fileindex = NULL;
8790 struct got_repository *repo = NULL;
8792 struct got_reference *branch = NULL;
8793 struct got_reference *tmp_branch = NULL;
8794 struct got_object_id *resume_commit_id = NULL;
8795 struct got_object_id *base_commit_id = NULL;
8796 struct got_object_id *head_commit_id = NULL;
8797 struct got_commit_object *commit = NULL;
8798 int ch, rebase_in_progress = 0;
8799 struct got_update_progress_arg upa;
8800 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8801 int edit_logmsg_only = 0, fold_only = 0;
8802 const char *edit_script_path = NULL;
8803 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8804 struct got_object_id_queue commits;
8805 struct got_pathlist_head merged_paths;
8806 const struct got_object_id_queue *parent_ids;
8807 struct got_object_qid *pid;
8808 struct got_histedit_list histedit_cmds;
8809 struct got_histedit_list_entry *hle;
8811 SIMPLEQ_INIT(&commits);
8812 TAILQ_INIT(&histedit_cmds);
8813 TAILQ_INIT(&merged_paths);
8814 memset(&upa, 0, sizeof(upa));
8816 while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8828 edit_script_path = optarg;
8831 edit_logmsg_only = 1;
8843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8844 "unveil", NULL) == -1)
8847 if (abort_edit && continue_edit)
8848 option_conflict('a', 'c');
8849 if (edit_script_path && edit_logmsg_only)
8850 option_conflict('F', 'm');
8851 if (abort_edit && edit_logmsg_only)
8852 option_conflict('a', 'm');
8853 if (continue_edit && edit_logmsg_only)
8854 option_conflict('c', 'm');
8855 if (abort_edit && fold_only)
8856 option_conflict('a', 'f');
8857 if (continue_edit && fold_only)
8858 option_conflict('c', 'f');
8859 if (fold_only && edit_logmsg_only)
8860 option_conflict('f', 'm');
8861 if (edit_script_path && fold_only)
8862 option_conflict('F', 'f');
8867 * This command cannot apply unveil(2) in all cases because the
8868 * user may choose to run an editor to edit the histedit script
8869 * and to edit individual commit log messages.
8870 * unveil(2) traverses exec(2); if an editor is used we have to
8871 * apply unveil after edit script and log messages have been written.
8872 * XXX TODO: Make use of unveil(2) where possible.
8875 cwd = getcwd(NULL, 0);
8877 error = got_error_from_errno("getcwd");
8880 error = got_worktree_open(&worktree, cwd);
8882 if (error->code == GOT_ERR_NOT_WORKTREE)
8883 error = wrap_not_worktree_error(error, "histedit", cwd);
8887 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8892 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8895 if (rebase_in_progress) {
8896 error = got_error(GOT_ERR_REBASING);
8900 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8904 if (edit_in_progress && edit_logmsg_only) {
8905 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8906 "histedit operation is in progress in this "
8907 "work tree and must be continued or aborted "
8908 "before the -m option can be used");
8911 if (edit_in_progress && fold_only) {
8912 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8913 "histedit operation is in progress in this "
8914 "work tree and must be continued or aborted "
8915 "before the -f option can be used");
8919 if (edit_in_progress && abort_edit) {
8920 error = got_worktree_histedit_continue(&resume_commit_id,
8921 &tmp_branch, &branch, &base_commit_id, &fileindex,
8925 printf("Switching work tree to %s\n",
8926 got_ref_get_symref_target(branch));
8927 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8928 branch, base_commit_id, update_progress, &upa);
8931 printf("Histedit of %s aborted\n",
8932 got_ref_get_symref_target(branch));
8933 print_update_progress_stats(&upa);
8934 goto done; /* nothing else to do */
8935 } else if (abort_edit) {
8936 error = got_error(GOT_ERR_NOT_HISTEDIT);
8940 if (continue_edit) {
8943 if (!edit_in_progress) {
8944 error = got_error(GOT_ERR_NOT_HISTEDIT);
8948 error = got_worktree_get_histedit_script_path(&path, worktree);
8952 error = histedit_load_list(&histedit_cmds, path, repo);
8957 error = got_worktree_histedit_continue(&resume_commit_id,
8958 &tmp_branch, &branch, &base_commit_id, &fileindex,
8963 error = got_ref_resolve(&head_commit_id, repo, branch);
8967 error = got_object_open_as_commit(&commit, repo,
8971 parent_ids = got_object_commit_get_parent_ids(commit);
8972 pid = SIMPLEQ_FIRST(parent_ids);
8974 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8977 error = collect_commits(&commits, head_commit_id, pid->id,
8978 base_commit_id, got_worktree_get_path_prefix(worktree),
8979 GOT_ERR_HISTEDIT_PATH, repo);
8980 got_object_commit_close(commit);
8985 if (edit_in_progress) {
8986 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8990 error = got_ref_open(&branch, repo,
8991 got_worktree_get_head_ref_name(worktree), 0);
8995 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8996 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8997 "will not edit commit history of a branch outside "
8998 "the \"refs/heads/\" reference namespace");
9002 error = got_ref_resolve(&head_commit_id, repo, branch);
9003 got_ref_close(branch);
9008 error = got_object_open_as_commit(&commit, repo,
9012 parent_ids = got_object_commit_get_parent_ids(commit);
9013 pid = SIMPLEQ_FIRST(parent_ids);
9015 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9018 error = collect_commits(&commits, head_commit_id, pid->id,
9019 got_worktree_get_base_commit_id(worktree),
9020 got_worktree_get_path_prefix(worktree),
9021 GOT_ERR_HISTEDIT_PATH, repo);
9022 got_object_commit_close(commit);
9027 if (SIMPLEQ_EMPTY(&commits)) {
9028 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9032 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9033 &base_commit_id, &fileindex, worktree, repo);
9037 if (edit_script_path) {
9038 error = histedit_load_list(&histedit_cmds,
9039 edit_script_path, repo);
9041 got_worktree_histedit_abort(worktree, fileindex,
9042 repo, branch, base_commit_id,
9043 update_progress, &upa);
9044 print_update_progress_stats(&upa);
9048 const char *branch_name;
9049 branch_name = got_ref_get_symref_target(branch);
9050 if (strncmp(branch_name, "refs/heads/", 11) == 0)
9052 error = histedit_edit_script(&histedit_cmds, &commits,
9053 branch_name, edit_logmsg_only, fold_only, repo);
9055 got_worktree_histedit_abort(worktree, fileindex,
9056 repo, branch, base_commit_id,
9057 update_progress, &upa);
9058 print_update_progress_stats(&upa);
9064 error = histedit_save_list(&histedit_cmds, worktree,
9067 got_worktree_histedit_abort(worktree, fileindex,
9068 repo, branch, base_commit_id,
9069 update_progress, &upa);
9070 print_update_progress_stats(&upa);
9076 error = histedit_check_script(&histedit_cmds, &commits, repo);
9080 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9081 if (resume_commit_id) {
9082 if (got_object_id_cmp(hle->commit_id,
9083 resume_commit_id) != 0)
9086 resume_commit_id = NULL;
9087 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9088 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9089 error = histedit_skip_commit(hle, worktree,
9094 struct got_pathlist_head paths;
9095 int have_changes = 0;
9098 error = got_pathlist_append(&paths, "", NULL);
9101 error = got_worktree_status(worktree, &paths,
9102 repo, check_local_changes, &have_changes,
9103 check_cancelled, NULL);
9104 got_pathlist_free(&paths);
9106 if (error->code != GOT_ERR_CANCELLED)
9108 if (sigint_received || sigpipe_received)
9112 error = histedit_commit(NULL, worktree,
9113 fileindex, tmp_branch, hle, repo);
9117 error = got_object_open_as_commit(
9118 &commit, repo, hle->commit_id);
9121 error = show_histedit_progress(commit,
9123 got_object_commit_close(commit);
9132 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9133 error = histedit_skip_commit(hle, worktree, repo);
9139 error = got_object_open_as_commit(&commit, repo,
9143 parent_ids = got_object_commit_get_parent_ids(commit);
9144 pid = SIMPLEQ_FIRST(parent_ids);
9146 error = got_worktree_histedit_merge_files(&merged_paths,
9147 worktree, fileindex, pid->id, hle->commit_id, repo,
9148 update_progress, &upa, check_cancelled, NULL);
9151 got_object_commit_close(commit);
9154 print_update_progress_stats(&upa);
9155 if (upa.conflicts > 0)
9156 rebase_status = GOT_STATUS_CONFLICT;
9158 if (rebase_status == GOT_STATUS_CONFLICT) {
9159 error = show_rebase_merge_conflict(hle->commit_id,
9163 got_worktree_rebase_pathlist_free(&merged_paths);
9167 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9169 error = got_object_id_str(&id_str, hle->commit_id);
9172 printf("Stopping histedit for amending commit %s\n",
9175 got_worktree_rebase_pathlist_free(&merged_paths);
9176 error = got_worktree_histedit_postpone(worktree,
9181 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9182 error = histedit_skip_commit(hle, worktree, repo);
9188 error = histedit_commit(&merged_paths, worktree, fileindex,
9189 tmp_branch, hle, repo);
9190 got_worktree_rebase_pathlist_free(&merged_paths);
9195 if (rebase_status == GOT_STATUS_CONFLICT) {
9196 error = got_worktree_histedit_postpone(worktree, fileindex);
9199 error = got_error_msg(GOT_ERR_CONFLICTS,
9200 "conflicts must be resolved before histedit can continue");
9202 error = histedit_complete(worktree, fileindex, tmp_branch,
9205 got_object_id_queue_free(&commits);
9206 histedit_free_list(&histedit_cmds);
9207 free(head_commit_id);
9208 free(base_commit_id);
9209 free(resume_commit_id);
9211 got_object_commit_close(commit);
9213 got_ref_close(branch);
9215 got_ref_close(tmp_branch);
9217 got_worktree_close(worktree);
9219 got_repo_close(repo);
9224 usage_integrate(void)
9226 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9230 static const struct got_error *
9231 cmd_integrate(int argc, char *argv[])
9233 const struct got_error *error = NULL;
9234 struct got_repository *repo = NULL;
9235 struct got_worktree *worktree = NULL;
9236 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9237 const char *branch_arg = NULL;
9238 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9239 struct got_fileindex *fileindex = NULL;
9240 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9242 struct got_update_progress_arg upa;
9244 while ((ch = getopt(argc, argv, "")) != -1) {
9257 branch_arg = argv[0];
9259 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9260 "unveil", NULL) == -1)
9263 cwd = getcwd(NULL, 0);
9265 error = got_error_from_errno("getcwd");
9269 error = got_worktree_open(&worktree, cwd);
9271 if (error->code == GOT_ERR_NOT_WORKTREE)
9272 error = wrap_not_worktree_error(error, "integrate",
9277 error = check_rebase_or_histedit_in_progress(worktree);
9281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9286 error = apply_unveil(got_repo_get_path(repo), 0,
9287 got_worktree_get_root_path(worktree));
9291 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9292 error = got_error_from_errno("asprintf");
9296 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9297 &base_branch_ref, worktree, refname, repo);
9301 refname = strdup(got_ref_get_name(branch_ref));
9302 if (refname == NULL) {
9303 error = got_error_from_errno("strdup");
9304 got_worktree_integrate_abort(worktree, fileindex, repo,
9305 branch_ref, base_branch_ref);
9308 base_refname = strdup(got_ref_get_name(base_branch_ref));
9309 if (base_refname == NULL) {
9310 error = got_error_from_errno("strdup");
9311 got_worktree_integrate_abort(worktree, fileindex, repo,
9312 branch_ref, base_branch_ref);
9316 error = got_ref_resolve(&commit_id, repo, branch_ref);
9320 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9324 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9325 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9326 "specified branch has already been integrated");
9327 got_worktree_integrate_abort(worktree, fileindex, repo,
9328 branch_ref, base_branch_ref);
9332 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9334 if (error->code == GOT_ERR_ANCESTRY)
9335 error = got_error(GOT_ERR_REBASE_REQUIRED);
9336 got_worktree_integrate_abort(worktree, fileindex, repo,
9337 branch_ref, base_branch_ref);
9341 memset(&upa, 0, sizeof(upa));
9342 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9343 branch_ref, base_branch_ref, update_progress, &upa,
9344 check_cancelled, NULL);
9348 printf("Integrated %s into %s\n", refname, base_refname);
9349 print_update_progress_stats(&upa);
9352 got_repo_close(repo);
9354 got_worktree_close(worktree);
9356 free(base_commit_id);
9366 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9367 "[-S] [file-path ...]\n",
9372 static const struct got_error *
9373 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9374 const char *path, struct got_object_id *blob_id,
9375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9376 int dirfd, const char *de_name)
9378 const struct got_error *err = NULL;
9379 char *id_str = NULL;
9381 if (staged_status != GOT_STATUS_ADD &&
9382 staged_status != GOT_STATUS_MODIFY &&
9383 staged_status != GOT_STATUS_DELETE)
9386 if (staged_status == GOT_STATUS_ADD ||
9387 staged_status == GOT_STATUS_MODIFY)
9388 err = got_object_id_str(&id_str, staged_blob_id);
9390 err = got_object_id_str(&id_str, blob_id);
9394 printf("%s %c %s\n", id_str, staged_status, path);
9399 static const struct got_error *
9400 cmd_stage(int argc, char *argv[])
9402 const struct got_error *error = NULL;
9403 struct got_repository *repo = NULL;
9404 struct got_worktree *worktree = NULL;
9406 struct got_pathlist_head paths;
9407 struct got_pathlist_entry *pe;
9408 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9409 FILE *patch_script_file = NULL;
9410 const char *patch_script_path = NULL;
9411 struct choose_patch_arg cpa;
9415 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9424 patch_script_path = optarg;
9427 allow_bad_symlinks = 1;
9439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9440 "unveil", NULL) == -1)
9443 if (list_stage && (pflag || patch_script_path))
9444 errx(1, "-l option cannot be used with other options");
9445 if (patch_script_path && !pflag)
9446 errx(1, "-F option can only be used together with -p option");
9448 cwd = getcwd(NULL, 0);
9450 error = got_error_from_errno("getcwd");
9454 error = got_worktree_open(&worktree, cwd);
9456 if (error->code == GOT_ERR_NOT_WORKTREE)
9457 error = wrap_not_worktree_error(error, "stage", cwd);
9461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9466 if (patch_script_path) {
9467 patch_script_file = fopen(patch_script_path, "r");
9468 if (patch_script_file == NULL) {
9469 error = got_error_from_errno2("fopen",
9474 error = apply_unveil(got_repo_get_path(repo), 0,
9475 got_worktree_get_root_path(worktree));
9479 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9484 error = got_worktree_status(worktree, &paths, repo,
9485 print_stage, NULL, check_cancelled, NULL);
9487 cpa.patch_script_file = patch_script_file;
9488 cpa.action = "stage";
9489 error = got_worktree_stage(worktree, &paths,
9490 pflag ? NULL : print_status, NULL,
9491 pflag ? choose_patch : NULL, &cpa,
9492 allow_bad_symlinks, repo);
9495 if (patch_script_file && fclose(patch_script_file) == EOF &&
9497 error = got_error_from_errno2("fclose", patch_script_path);
9499 got_repo_close(repo);
9501 got_worktree_close(worktree);
9502 TAILQ_FOREACH(pe, &paths, entry)
9503 free((char *)pe->path);
9504 got_pathlist_free(&paths);
9512 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9513 "[file-path ...]\n",
9519 static const struct got_error *
9520 cmd_unstage(int argc, char *argv[])
9522 const struct got_error *error = NULL;
9523 struct got_repository *repo = NULL;
9524 struct got_worktree *worktree = NULL;
9526 struct got_pathlist_head paths;
9527 struct got_pathlist_entry *pe;
9529 struct got_update_progress_arg upa;
9530 FILE *patch_script_file = NULL;
9531 const char *patch_script_path = NULL;
9532 struct choose_patch_arg cpa;
9536 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9542 patch_script_path = optarg;
9554 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9555 "unveil", NULL) == -1)
9558 if (patch_script_path && !pflag)
9559 errx(1, "-F option can only be used together with -p option");
9561 cwd = getcwd(NULL, 0);
9563 error = got_error_from_errno("getcwd");
9567 error = got_worktree_open(&worktree, cwd);
9569 if (error->code == GOT_ERR_NOT_WORKTREE)
9570 error = wrap_not_worktree_error(error, "unstage", cwd);
9574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9579 if (patch_script_path) {
9580 patch_script_file = fopen(patch_script_path, "r");
9581 if (patch_script_file == NULL) {
9582 error = got_error_from_errno2("fopen",
9588 error = apply_unveil(got_repo_get_path(repo), 0,
9589 got_worktree_get_root_path(worktree));
9593 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9597 cpa.patch_script_file = patch_script_file;
9598 cpa.action = "unstage";
9599 memset(&upa, 0, sizeof(upa));
9600 error = got_worktree_unstage(worktree, &paths, update_progress,
9601 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9603 print_update_progress_stats(&upa);
9605 if (patch_script_file && fclose(patch_script_file) == EOF &&
9607 error = got_error_from_errno2("fclose", patch_script_path);
9609 got_repo_close(repo);
9611 got_worktree_close(worktree);
9612 TAILQ_FOREACH(pe, &paths, entry)
9613 free((char *)pe->path);
9614 got_pathlist_free(&paths);
9622 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9623 "arg1 [arg2 ...]\n", getprogname());
9627 static const struct got_error *
9628 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9630 const struct got_error *err;
9631 struct got_blob_object *blob;
9633 err = got_object_open_as_blob(&blob, repo, id, 8192);
9637 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9638 got_object_blob_close(blob);
9642 static const struct got_error *
9643 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9645 const struct got_error *err;
9646 struct got_tree_object *tree;
9649 err = got_object_open_as_tree(&tree, repo, id);
9653 nentries = got_object_tree_get_nentries(tree);
9654 for (i = 0; i < nentries; i++) {
9655 struct got_tree_entry *te;
9657 if (sigint_received || sigpipe_received)
9659 te = got_object_tree_get_entry(tree, i);
9660 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9663 fprintf(outfile, "%s %.7o %s\n", id_str,
9664 got_tree_entry_get_mode(te),
9665 got_tree_entry_get_name(te));
9669 got_object_tree_close(tree);
9673 static const struct got_error *
9674 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9676 const struct got_error *err;
9677 struct got_commit_object *commit;
9678 const struct got_object_id_queue *parent_ids;
9679 struct got_object_qid *pid;
9680 char *id_str = NULL;
9681 const char *logmsg = NULL;
9683 err = got_object_open_as_commit(&commit, repo, id);
9687 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9691 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9692 parent_ids = got_object_commit_get_parent_ids(commit);
9693 fprintf(outfile, "numparents %d\n",
9694 got_object_commit_get_nparents(commit));
9695 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9697 err = got_object_id_str(&pid_str, pid->id);
9700 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9703 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9704 got_object_commit_get_author(commit),
9705 (long long)got_object_commit_get_author_time(commit));
9707 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9708 got_object_commit_get_author(commit),
9709 (long long)got_object_commit_get_committer_time(commit));
9711 logmsg = got_object_commit_get_logmsg_raw(commit);
9712 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9713 fprintf(outfile, "%s", logmsg);
9716 got_object_commit_close(commit);
9720 static const struct got_error *
9721 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9723 const struct got_error *err;
9724 struct got_tag_object *tag;
9725 char *id_str = NULL;
9726 const char *tagmsg = NULL;
9728 err = got_object_open_as_tag(&tag, repo, id);
9732 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9736 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9738 switch (got_object_tag_get_object_type(tag)) {
9739 case GOT_OBJ_TYPE_BLOB:
9740 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9741 GOT_OBJ_LABEL_BLOB);
9743 case GOT_OBJ_TYPE_TREE:
9744 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9745 GOT_OBJ_LABEL_TREE);
9747 case GOT_OBJ_TYPE_COMMIT:
9748 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9749 GOT_OBJ_LABEL_COMMIT);
9751 case GOT_OBJ_TYPE_TAG:
9752 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9759 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9760 got_object_tag_get_name(tag));
9762 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9763 got_object_tag_get_tagger(tag),
9764 (long long)got_object_tag_get_tagger_time(tag));
9766 tagmsg = got_object_tag_get_message(tag);
9767 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9768 fprintf(outfile, "%s", tagmsg);
9771 got_object_tag_close(tag);
9775 static const struct got_error *
9776 cmd_cat(int argc, char *argv[])
9778 const struct got_error *error;
9779 struct got_repository *repo = NULL;
9780 struct got_worktree *worktree = NULL;
9781 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9782 const char *commit_id_str = NULL;
9783 struct got_object_id *id = NULL, *commit_id = NULL;
9784 int ch, obj_type, i, force_path = 0;
9785 struct got_reflist_head refs;
9790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9795 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9798 commit_id_str = optarg;
9801 repo_path = realpath(optarg, NULL);
9802 if (repo_path == NULL)
9803 return got_error_from_errno2("realpath",
9805 got_path_strip_trailing_slashes(repo_path);
9819 cwd = getcwd(NULL, 0);
9821 error = got_error_from_errno("getcwd");
9824 error = got_worktree_open(&worktree, cwd);
9825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9828 if (repo_path == NULL) {
9830 got_worktree_get_repo_path(worktree));
9831 if (repo_path == NULL) {
9832 error = got_error_from_errno("strdup");
9838 if (repo_path == NULL) {
9839 repo_path = getcwd(NULL, 0);
9840 if (repo_path == NULL)
9841 return got_error_from_errno("getcwd");
9844 error = got_repo_open(&repo, repo_path, NULL);
9849 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9853 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9857 if (commit_id_str == NULL)
9858 commit_id_str = GOT_REF_HEAD;
9859 error = got_repo_match_object_id(&commit_id, NULL,
9860 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9864 for (i = 0; i < argc; i++) {
9866 error = got_object_id_by_path(&id, repo, commit_id,
9871 error = got_repo_match_object_id(&id, &label, argv[i],
9872 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9875 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9876 error->code != GOT_ERR_NOT_REF)
9878 error = got_object_id_by_path(&id, repo,
9879 commit_id, argv[i]);
9885 error = got_object_get_type(&obj_type, repo, id);
9890 case GOT_OBJ_TYPE_BLOB:
9891 error = cat_blob(id, repo, stdout);
9893 case GOT_OBJ_TYPE_TREE:
9894 error = cat_tree(id, repo, stdout);
9896 case GOT_OBJ_TYPE_COMMIT:
9897 error = cat_commit(id, repo, stdout);
9899 case GOT_OBJ_TYPE_TAG:
9900 error = cat_tag(id, repo, stdout);
9903 error = got_error(GOT_ERR_OBJ_TYPE);
9918 got_worktree_close(worktree);
9920 const struct got_error *repo_error;
9921 repo_error = got_repo_close(repo);
9925 got_ref_list_free(&refs);
9932 fprintf(stderr, "usage: %s info [path ...]\n",
9937 static const struct got_error *
9938 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9939 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9940 struct got_object_id *commit_id)
9942 const struct got_error *err = NULL;
9943 char *id_str = NULL;
9945 struct tm mytm, *tm;
9946 struct got_pathlist_head *paths = arg;
9947 struct got_pathlist_entry *pe;
9950 * Clear error indication from any of the path arguments which
9951 * would cause this file index entry to be displayed.
9953 TAILQ_FOREACH(pe, paths, entry) {
9954 if (got_path_cmp(path, pe->path, strlen(path),
9955 pe->path_len) == 0 ||
9956 got_path_is_child(path, pe->path, pe->path_len))
9957 pe->data = NULL; /* no error */
9960 printf(GOT_COMMIT_SEP_STR);
9962 printf("symlink: %s\n", path);
9963 else if (S_ISREG(mode)) {
9964 printf("file: %s\n", path);
9965 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9966 } else if (S_ISDIR(mode))
9967 printf("directory: %s\n", path);
9969 printf("something: %s\n", path);
9971 tm = localtime_r(&mtime, &mytm);
9974 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9975 return got_error(GOT_ERR_NO_SPACE);
9976 printf("timestamp: %s\n", datebuf);
9979 err = got_object_id_str(&id_str, blob_id);
9982 printf("based on blob: %s\n", id_str);
9986 if (staged_blob_id) {
9987 err = got_object_id_str(&id_str, staged_blob_id);
9990 printf("based on staged blob: %s\n", id_str);
9995 err = got_object_id_str(&id_str, commit_id);
9998 printf("based on commit: %s\n", id_str);
10005 static const struct got_error *
10006 cmd_info(int argc, char *argv[])
10008 const struct got_error *error = NULL;
10009 struct got_worktree *worktree = NULL;
10010 char *cwd = NULL, *id_str = NULL;
10011 struct got_pathlist_head paths;
10012 struct got_pathlist_entry *pe;
10013 char *uuidstr = NULL;
10014 int ch, show_files = 0;
10016 TAILQ_INIT(&paths);
10018 while ((ch = getopt(argc, argv, "")) != -1) {
10030 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10034 cwd = getcwd(NULL, 0);
10036 error = got_error_from_errno("getcwd");
10040 error = got_worktree_open(&worktree, cwd);
10042 if (error->code == GOT_ERR_NOT_WORKTREE)
10043 error = wrap_not_worktree_error(error, "status", cwd);
10047 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10052 error = get_worktree_paths_from_argv(&paths, argc, argv,
10059 error = got_object_id_str(&id_str,
10060 got_worktree_get_base_commit_id(worktree));
10064 error = got_worktree_get_uuid(&uuidstr, worktree);
10068 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10069 printf("work tree base commit: %s\n", id_str);
10070 printf("work tree path prefix: %s\n",
10071 got_worktree_get_path_prefix(worktree));
10072 printf("work tree branch reference: %s\n",
10073 got_worktree_get_head_ref_name(worktree));
10074 printf("work tree UUID: %s\n", uuidstr);
10075 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10078 struct got_pathlist_entry *pe;
10079 TAILQ_FOREACH(pe, &paths, entry) {
10080 if (pe->path_len == 0)
10083 * Assume this path will fail. This will be corrected
10084 * in print_path_info() in case the path does suceeed.
10086 pe->data = (void *)got_error_path(pe->path,
10089 error = got_worktree_path_info(worktree, &paths,
10090 print_path_info, &paths, check_cancelled, NULL);
10093 TAILQ_FOREACH(pe, &paths, entry) {
10094 if (pe->data != NULL) {
10095 error = pe->data; /* bad path */
10101 TAILQ_FOREACH(pe, &paths, entry)
10102 free((char *)pe->path);
10103 got_pathlist_free(&paths);