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 (!TAILQ_EMPTY(&wanted_refs))
1528 option_conflict('l', 'R');
1540 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1545 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1546 host, port ? ":" : "", port ? port : "",
1547 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1548 error = got_error_from_errno("asprintf");
1552 if (strcmp(proto, "git") == 0) {
1554 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1555 "sendfd dns inet unveil", NULL) == -1)
1558 } else if (strcmp(proto, "git+ssh") == 0 ||
1559 strcmp(proto, "ssh") == 0) {
1561 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 "sendfd unveil", NULL) == -1)
1565 } else if (strcmp(proto, "http") == 0 ||
1566 strcmp(proto, "git+http") == 0) {
1567 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1570 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1573 if (dirname == NULL) {
1574 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1575 error = got_error_from_errno("asprintf");
1578 repo_path = default_destdir;
1580 repo_path = dirname;
1582 if (!list_refs_only) {
1583 error = got_path_mkdir(repo_path);
1585 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1586 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1588 if (!got_path_dir_is_empty(repo_path)) {
1589 error = got_error_path(repo_path,
1590 GOT_ERR_DIR_NOT_EMPTY);
1595 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1596 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1597 error = got_error_from_errno2("unveil",
1598 GOT_FETCH_PATH_SSH);
1602 error = apply_unveil(repo_path, 0, NULL);
1607 printf("Connecting to %s%s%s\n", host,
1608 port ? ":" : "", port ? port : "");
1610 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1611 server_path, verbosity);
1615 if (!list_refs_only) {
1616 error = got_repo_init(repo_path);
1619 error = got_repo_open(&repo, repo_path, NULL);
1624 fpa.last_scaled_size[0] = '\0';
1625 fpa.last_p_indexed = -1;
1626 fpa.last_p_resolved = -1;
1627 fpa.verbosity = verbosity;
1628 fpa.create_configs = 1;
1629 fpa.configs_created = 0;
1631 fpa.config_info.symrefs = &symrefs;
1632 fpa.config_info.wanted_branches = &wanted_branches;
1633 fpa.config_info.wanted_refs = &wanted_refs;
1634 fpa.config_info.proto = proto;
1635 fpa.config_info.host = host;
1636 fpa.config_info.port = port;
1637 fpa.config_info.remote_repo_path = server_path;
1638 fpa.config_info.git_url = git_url;
1639 fpa.config_info.fetch_all_branches = fetch_all_branches;
1640 fpa.config_info.mirror_references = mirror_references;
1641 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1642 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1643 fetch_all_branches, &wanted_branches, &wanted_refs,
1644 list_refs_only, verbosity, fetchfd, repo,
1645 fetch_progress, &fpa);
1649 if (list_refs_only) {
1650 error = list_remote_refs(&symrefs, &refs);
1654 error = got_object_id_str(&id_str, pack_hash);
1658 printf("\nFetched %s.pack\n", id_str);
1661 /* Set up references provided with the pack file. */
1662 TAILQ_FOREACH(pe, &refs, entry) {
1663 const char *refname = pe->path;
1664 struct got_object_id *id = pe->data;
1665 char *remote_refname;
1667 if (is_wanted_ref(&wanted_refs, refname) &&
1668 !mirror_references) {
1669 error = create_wanted_ref(refname, id,
1670 GOT_FETCH_DEFAULT_REMOTE_NAME,
1671 verbosity - 1, repo);
1677 error = create_ref(refname, id, verbosity - 1, repo);
1681 if (mirror_references)
1684 if (strncmp("refs/heads/", refname, 11) != 0)
1687 if (asprintf(&remote_refname,
1688 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1689 refname + 11) == -1) {
1690 error = got_error_from_errno("asprintf");
1693 error = create_ref(remote_refname, id, verbosity - 1, repo);
1694 free(remote_refname);
1699 /* Set the HEAD reference if the server provided one. */
1700 TAILQ_FOREACH(pe, &symrefs, entry) {
1701 struct got_reference *target_ref;
1702 const char *refname = pe->path;
1703 const char *target = pe->data;
1704 char *remote_refname = NULL, *remote_target = NULL;
1706 if (strcmp(refname, GOT_REF_HEAD) != 0)
1709 error = got_ref_open(&target_ref, repo, target, 0);
1711 if (error->code == GOT_ERR_NOT_REF) {
1718 error = create_symref(refname, target_ref, verbosity, repo);
1719 got_ref_close(target_ref);
1723 if (mirror_references)
1726 if (strncmp("refs/heads/", target, 11) != 0)
1729 if (asprintf(&remote_refname,
1730 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1732 error = got_error_from_errno("asprintf");
1735 if (asprintf(&remote_target,
1736 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1737 target + 11) == -1) {
1738 error = got_error_from_errno("asprintf");
1739 free(remote_refname);
1742 error = got_ref_open(&target_ref, repo, remote_target, 0);
1744 free(remote_refname);
1745 free(remote_target);
1746 if (error->code == GOT_ERR_NOT_REF) {
1752 error = create_symref(remote_refname, target_ref,
1753 verbosity - 1, repo);
1754 free(remote_refname);
1755 free(remote_target);
1756 got_ref_close(target_ref);
1762 * We failed to set the HEAD reference. If we asked for
1763 * a set of wanted branches use the first of one of those
1764 * which could be fetched instead.
1766 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1767 const char *target = pe->path;
1768 struct got_reference *target_ref;
1770 error = got_ref_open(&target_ref, repo, target, 0);
1772 if (error->code == GOT_ERR_NOT_REF) {
1779 error = create_symref(GOT_REF_HEAD, target_ref,
1781 got_ref_close(target_ref);
1789 printf("Created %s repository '%s'\n",
1790 mirror_references ? "mirrored" : "cloned", repo_path);
1793 if (kill(fetchpid, SIGTERM) == -1)
1794 error = got_error_from_errno("kill");
1795 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1796 error = got_error_from_errno("waitpid");
1798 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1799 error = got_error_from_errno("close");
1801 got_repo_close(repo);
1802 TAILQ_FOREACH(pe, &refs, entry) {
1803 free((void *)pe->path);
1806 got_pathlist_free(&refs);
1807 TAILQ_FOREACH(pe, &symrefs, entry) {
1808 free((void *)pe->path);
1811 got_pathlist_free(&symrefs);
1812 got_pathlist_free(&wanted_branches);
1813 got_pathlist_free(&wanted_refs);
1820 free(default_destdir);
1825 static const struct got_error *
1826 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1827 int replace_tags, int verbosity, struct got_repository *repo)
1829 const struct got_error *err = NULL;
1830 char *new_id_str = NULL;
1831 struct got_object_id *old_id = NULL;
1833 err = got_object_id_str(&new_id_str, new_id);
1837 if (!replace_tags &&
1838 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1839 err = got_ref_resolve(&old_id, repo, ref);
1842 if (got_object_id_cmp(old_id, new_id) == 0)
1844 if (verbosity >= 0) {
1845 printf("Rejecting update of existing tag %s: %s\n",
1846 got_ref_get_name(ref), new_id_str);
1851 if (got_ref_is_symbolic(ref)) {
1852 if (verbosity >= 0) {
1853 printf("Replacing reference %s: %s\n",
1854 got_ref_get_name(ref),
1855 got_ref_get_symref_target(ref));
1857 err = got_ref_change_symref_to_ref(ref, new_id);
1860 err = got_ref_write(ref, repo);
1864 err = got_ref_resolve(&old_id, repo, ref);
1867 if (got_object_id_cmp(old_id, new_id) == 0)
1870 err = got_ref_change_ref(ref, new_id);
1873 err = got_ref_write(ref, repo);
1879 printf("Updated %s: %s\n", got_ref_get_name(ref),
1887 static const struct got_error *
1888 update_symref(const char *refname, struct got_reference *target_ref,
1889 int verbosity, struct got_repository *repo)
1891 const struct got_error *err = NULL, *unlock_err;
1892 struct got_reference *symref;
1893 int symref_is_locked = 0;
1895 err = got_ref_open(&symref, repo, refname, 1);
1897 if (err->code != GOT_ERR_NOT_REF)
1899 err = got_ref_alloc_symref(&symref, refname, target_ref);
1903 err = got_ref_write(symref, repo);
1908 printf("Created reference %s: %s\n",
1909 got_ref_get_name(symref),
1910 got_ref_get_symref_target(symref));
1912 symref_is_locked = 1;
1914 if (strcmp(got_ref_get_symref_target(symref),
1915 got_ref_get_name(target_ref)) == 0)
1918 err = got_ref_change_symref(symref,
1919 got_ref_get_name(target_ref));
1923 err = got_ref_write(symref, repo);
1928 printf("Updated %s: %s\n", got_ref_get_name(symref),
1929 got_ref_get_symref_target(symref));
1933 if (symref_is_locked) {
1934 unlock_err = got_ref_unlock(symref);
1935 if (unlock_err && err == NULL)
1938 got_ref_close(symref);
1945 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1946 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1947 "[remote-repository-name]\n",
1952 static const struct got_error *
1953 delete_missing_ref(struct got_reference *ref,
1954 int verbosity, struct got_repository *repo)
1956 const struct got_error *err = NULL;
1957 struct got_object_id *id = NULL;
1958 char *id_str = NULL;
1960 if (got_ref_is_symbolic(ref)) {
1961 err = got_ref_delete(ref, repo);
1964 if (verbosity >= 0) {
1965 printf("Deleted reference %s: %s\n",
1966 got_ref_get_name(ref),
1967 got_ref_get_symref_target(ref));
1970 err = got_ref_resolve(&id, repo, ref);
1973 err = got_object_id_str(&id_str, id);
1977 err = got_ref_delete(ref, repo);
1980 if (verbosity >= 0) {
1981 printf("Deleted reference %s: %s\n",
1982 got_ref_get_name(ref), id_str);
1991 static const struct got_error *
1992 delete_missing_refs(struct got_pathlist_head *their_refs,
1993 struct got_pathlist_head *their_symrefs,
1994 const struct got_remote_repo *remote,
1995 int verbosity, struct got_repository *repo)
1997 const struct got_error *err = NULL, *unlock_err;
1998 struct got_reflist_head my_refs;
1999 struct got_reflist_entry *re;
2000 struct got_pathlist_entry *pe;
2001 char *remote_namespace = NULL;
2002 char *local_refname = NULL;
2004 TAILQ_INIT(&my_refs);
2006 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2008 return got_error_from_errno("asprintf");
2010 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2014 TAILQ_FOREACH(re, &my_refs, entry) {
2015 const char *refname = got_ref_get_name(re->ref);
2017 if (!remote->mirror_references) {
2018 if (strncmp(refname, remote_namespace,
2019 strlen(remote_namespace)) == 0) {
2020 if (strcmp(refname + strlen(remote_namespace),
2023 if (asprintf(&local_refname, "refs/heads/%s",
2024 refname + strlen(remote_namespace)) == -1) {
2025 err = got_error_from_errno("asprintf");
2028 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2032 TAILQ_FOREACH(pe, their_refs, entry) {
2033 if (strcmp(local_refname, pe->path) == 0)
2039 TAILQ_FOREACH(pe, their_symrefs, entry) {
2040 if (strcmp(local_refname, pe->path) == 0)
2046 err = delete_missing_ref(re->ref, verbosity, repo);
2050 if (local_refname) {
2051 struct got_reference *ref;
2052 err = got_ref_open(&ref, repo, local_refname, 1);
2054 if (err->code != GOT_ERR_NOT_REF)
2056 free(local_refname);
2057 local_refname = NULL;
2060 err = delete_missing_ref(ref, verbosity, repo);
2063 unlock_err = got_ref_unlock(ref);
2065 if (unlock_err && err == NULL) {
2070 free(local_refname);
2071 local_refname = NULL;
2075 free(remote_namespace);
2076 free(local_refname);
2080 static const struct got_error *
2081 update_wanted_ref(const char *refname, struct got_object_id *id,
2082 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2084 const struct got_error *err, *unlock_err;
2085 char *remote_refname;
2086 struct got_reference *ref;
2088 if (strncmp("refs/", refname, 5) == 0)
2091 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2092 remote_repo_name, refname) == -1)
2093 return got_error_from_errno("asprintf");
2095 err = got_ref_open(&ref, repo, remote_refname, 1);
2097 if (err->code != GOT_ERR_NOT_REF)
2099 err = create_ref(remote_refname, id, verbosity, repo);
2101 err = update_ref(ref, id, 0, verbosity, repo);
2102 unlock_err = got_ref_unlock(ref);
2103 if (unlock_err && err == NULL)
2108 free(remote_refname);
2112 static const struct got_error *
2113 cmd_fetch(int argc, char *argv[])
2115 const struct got_error *error = NULL, *unlock_err;
2116 char *cwd = NULL, *repo_path = NULL;
2117 const char *remote_name;
2118 char *proto = NULL, *host = NULL, *port = NULL;
2119 char *repo_name = NULL, *server_path = NULL;
2120 const struct got_remote_repo *remotes, *remote = NULL;
2122 char *id_str = NULL;
2123 struct got_repository *repo = NULL;
2124 struct got_worktree *worktree = NULL;
2125 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2126 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2127 struct got_pathlist_entry *pe;
2128 struct got_object_id *pack_hash = NULL;
2129 int i, ch, fetchfd = -1, fetchstatus;
2130 pid_t fetchpid = -1;
2131 struct got_fetch_progress_arg fpa;
2132 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2133 int delete_refs = 0, replace_tags = 0;
2136 TAILQ_INIT(&symrefs);
2137 TAILQ_INIT(&wanted_branches);
2138 TAILQ_INIT(&wanted_refs);
2140 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2143 fetch_all_branches = 1;
2146 error = got_pathlist_append(&wanted_branches,
2158 repo_path = realpath(optarg, NULL);
2159 if (repo_path == NULL)
2160 return got_error_from_errno2("realpath",
2162 got_path_strip_trailing_slashes(repo_path);
2170 else if (verbosity < 3)
2177 error = got_pathlist_append(&wanted_refs,
2190 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2191 option_conflict('a', 'b');
2192 if (list_refs_only) {
2193 if (!TAILQ_EMPTY(&wanted_branches))
2194 option_conflict('l', 'b');
2195 if (fetch_all_branches)
2196 option_conflict('l', 'a');
2198 option_conflict('l', 'd');
2202 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2204 remote_name = argv[0];
2208 cwd = getcwd(NULL, 0);
2210 error = got_error_from_errno("getcwd");
2214 if (repo_path == NULL) {
2215 error = got_worktree_open(&worktree, cwd);
2216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2222 strdup(got_worktree_get_repo_path(worktree));
2223 if (repo_path == NULL)
2224 error = got_error_from_errno("strdup");
2228 repo_path = strdup(cwd);
2229 if (repo_path == NULL) {
2230 error = got_error_from_errno("strdup");
2236 error = got_repo_open(&repo, repo_path, NULL);
2241 worktree_conf = got_worktree_get_gotconfig(worktree);
2242 if (worktree_conf) {
2243 got_gotconfig_get_remotes(&nremotes, &remotes,
2245 for (i = 0; i < nremotes; i++) {
2246 if (strcmp(remotes[i].name, remote_name) == 0) {
2247 remote = &remotes[i];
2253 if (remote == NULL) {
2254 repo_conf = got_repo_get_gotconfig(repo);
2256 got_gotconfig_get_remotes(&nremotes, &remotes,
2258 for (i = 0; i < nremotes; i++) {
2259 if (strcmp(remotes[i].name, remote_name) == 0) {
2260 remote = &remotes[i];
2266 if (remote == NULL) {
2267 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2268 for (i = 0; i < nremotes; i++) {
2269 if (strcmp(remotes[i].name, remote_name) == 0) {
2270 remote = &remotes[i];
2275 if (remote == NULL) {
2276 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2280 if (TAILQ_EMPTY(&wanted_branches)) {
2281 if (!fetch_all_branches)
2282 fetch_all_branches = remote->fetch_all_branches;
2283 for (i = 0; i < remote->nbranches; i++) {
2284 got_pathlist_append(&wanted_branches,
2285 remote->branches[i], NULL);
2288 if (TAILQ_EMPTY(&wanted_refs)) {
2289 for (i = 0; i < remote->nrefs; i++) {
2290 got_pathlist_append(&wanted_refs,
2291 remote->refs[i], NULL);
2295 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2296 &repo_name, remote->url);
2300 if (strcmp(proto, "git") == 0) {
2302 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2303 "sendfd dns inet unveil", NULL) == -1)
2306 } else if (strcmp(proto, "git+ssh") == 0 ||
2307 strcmp(proto, "ssh") == 0) {
2309 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2310 "sendfd unveil", NULL) == -1)
2313 } else if (strcmp(proto, "http") == 0 ||
2314 strcmp(proto, "git+http") == 0) {
2315 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2318 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2322 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2323 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2324 error = got_error_from_errno2("unveil",
2325 GOT_FETCH_PATH_SSH);
2329 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2334 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2335 port ? ":" : "", port ? port : "");
2337 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2338 server_path, verbosity);
2342 fpa.last_scaled_size[0] = '\0';
2343 fpa.last_p_indexed = -1;
2344 fpa.last_p_resolved = -1;
2345 fpa.verbosity = verbosity;
2347 fpa.create_configs = 0;
2348 fpa.configs_created = 0;
2349 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2350 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2351 remote->mirror_references, fetch_all_branches, &wanted_branches,
2352 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2353 fetch_progress, &fpa);
2357 if (list_refs_only) {
2358 error = list_remote_refs(&symrefs, &refs);
2362 if (pack_hash == NULL) {
2364 printf("Already up-to-date\n");
2365 } else if (verbosity >= 0) {
2366 error = got_object_id_str(&id_str, pack_hash);
2369 printf("\nFetched %s.pack\n", id_str);
2374 /* Update references provided with the pack file. */
2375 TAILQ_FOREACH(pe, &refs, entry) {
2376 const char *refname = pe->path;
2377 struct got_object_id *id = pe->data;
2378 struct got_reference *ref;
2379 char *remote_refname;
2381 if (is_wanted_ref(&wanted_refs, refname) &&
2382 !remote->mirror_references) {
2383 error = update_wanted_ref(refname, id,
2384 remote->name, verbosity, repo);
2390 if (remote->mirror_references ||
2391 strncmp("refs/tags/", refname, 10) == 0) {
2392 error = got_ref_open(&ref, repo, refname, 1);
2394 if (error->code != GOT_ERR_NOT_REF)
2396 error = create_ref(refname, id, verbosity,
2401 error = update_ref(ref, id, replace_tags,
2403 unlock_err = got_ref_unlock(ref);
2404 if (unlock_err && error == NULL)
2410 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2411 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2412 remote_name, refname + 11) == -1) {
2413 error = got_error_from_errno("asprintf");
2417 error = got_ref_open(&ref, repo, remote_refname, 1);
2419 if (error->code != GOT_ERR_NOT_REF)
2421 error = create_ref(remote_refname, id,
2426 error = update_ref(ref, id, replace_tags,
2428 unlock_err = got_ref_unlock(ref);
2429 if (unlock_err && error == NULL)
2436 /* Also create a local branch if none exists yet. */
2437 error = got_ref_open(&ref, repo, refname, 1);
2439 if (error->code != GOT_ERR_NOT_REF)
2441 error = create_ref(refname, id, verbosity,
2446 unlock_err = got_ref_unlock(ref);
2447 if (unlock_err && error == NULL)
2454 error = delete_missing_refs(&refs, &symrefs, remote,
2460 if (!remote->mirror_references) {
2461 /* Update remote HEAD reference if the server provided one. */
2462 TAILQ_FOREACH(pe, &symrefs, entry) {
2463 struct got_reference *target_ref;
2464 const char *refname = pe->path;
2465 const char *target = pe->data;
2466 char *remote_refname = NULL, *remote_target = NULL;
2468 if (strcmp(refname, GOT_REF_HEAD) != 0)
2471 if (strncmp("refs/heads/", target, 11) != 0)
2474 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2475 remote->name, refname) == -1) {
2476 error = got_error_from_errno("asprintf");
2479 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2480 remote->name, target + 11) == -1) {
2481 error = got_error_from_errno("asprintf");
2482 free(remote_refname);
2486 error = got_ref_open(&target_ref, repo, remote_target,
2489 free(remote_refname);
2490 free(remote_target);
2491 if (error->code == GOT_ERR_NOT_REF) {
2497 error = update_symref(remote_refname, target_ref,
2499 free(remote_refname);
2500 free(remote_target);
2501 got_ref_close(target_ref);
2508 if (kill(fetchpid, SIGTERM) == -1)
2509 error = got_error_from_errno("kill");
2510 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2511 error = got_error_from_errno("waitpid");
2513 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2514 error = got_error_from_errno("close");
2516 got_repo_close(repo);
2518 got_worktree_close(worktree);
2519 TAILQ_FOREACH(pe, &refs, entry) {
2520 free((void *)pe->path);
2523 got_pathlist_free(&refs);
2524 TAILQ_FOREACH(pe, &symrefs, entry) {
2525 free((void *)pe->path);
2528 got_pathlist_free(&symrefs);
2529 got_pathlist_free(&wanted_branches);
2530 got_pathlist_free(&wanted_refs);
2545 usage_checkout(void)
2547 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2548 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2553 show_worktree_base_ref_warning(void)
2555 fprintf(stderr, "%s: warning: could not create a reference "
2556 "to the work tree's base commit; the commit could be "
2557 "garbage-collected by Git; making the repository "
2558 "writable and running 'got update' will prevent this\n",
2562 struct got_checkout_progress_arg {
2563 const char *worktree_path;
2564 int had_base_commit_ref_error;
2567 static const struct got_error *
2568 checkout_progress(void *arg, unsigned char status, const char *path)
2570 struct got_checkout_progress_arg *a = arg;
2572 /* Base commit bump happens silently. */
2573 if (status == GOT_STATUS_BUMP_BASE)
2576 if (status == GOT_STATUS_BASE_REF_ERR) {
2577 a->had_base_commit_ref_error = 1;
2581 while (path[0] == '/')
2584 printf("%c %s/%s\n", status, a->worktree_path, path);
2588 static const struct got_error *
2589 check_cancelled(void *arg)
2591 if (sigint_received || sigpipe_received)
2592 return got_error(GOT_ERR_CANCELLED);
2596 static const struct got_error *
2597 check_linear_ancestry(struct got_object_id *commit_id,
2598 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2599 struct got_repository *repo)
2601 const struct got_error *err = NULL;
2602 struct got_object_id *yca_id;
2604 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2605 commit_id, base_commit_id, repo, check_cancelled, NULL);
2610 return got_error(GOT_ERR_ANCESTRY);
2613 * Require a straight line of history between the target commit
2614 * and the work tree's base commit.
2616 * Non-linear situations such as this require a rebase:
2618 * (commit) D F (base_commit)
2626 * 'got update' only handles linear cases:
2627 * Update forwards in time: A (base/yca) - B - C - D (commit)
2628 * Update backwards in time: D (base) - C - B - A (commit/yca)
2630 if (allow_forwards_in_time_only) {
2631 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2632 return got_error(GOT_ERR_ANCESTRY);
2633 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2634 got_object_id_cmp(base_commit_id, yca_id) != 0)
2635 return got_error(GOT_ERR_ANCESTRY);
2641 static const struct got_error *
2642 check_same_branch(struct got_object_id *commit_id,
2643 struct got_reference *head_ref, struct got_object_id *yca_id,
2644 struct got_repository *repo)
2646 const struct got_error *err = NULL;
2647 struct got_commit_graph *graph = NULL;
2648 struct got_object_id *head_commit_id = NULL;
2649 int is_same_branch = 0;
2651 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2655 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2659 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2664 err = got_commit_graph_open(&graph, "/", 1);
2668 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2669 check_cancelled, NULL);
2674 struct got_object_id *id;
2675 err = got_commit_graph_iter_next(&id, graph, repo,
2676 check_cancelled, NULL);
2678 if (err->code == GOT_ERR_ITER_COMPLETED)
2684 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2686 if (got_object_id_cmp(id, commit_id) == 0) {
2694 got_commit_graph_close(graph);
2695 free(head_commit_id);
2696 if (!err && !is_same_branch)
2697 err = got_error(GOT_ERR_ANCESTRY);
2701 static const struct got_error *
2702 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2704 static char msg[512];
2705 const char *branch_name;
2707 if (got_ref_is_symbolic(ref))
2708 branch_name = got_ref_get_symref_target(ref);
2710 branch_name = got_ref_get_name(ref);
2712 if (strncmp("refs/heads/", branch_name, 11) == 0)
2715 snprintf(msg, sizeof(msg),
2716 "target commit is not contained in branch '%s'; "
2717 "the branch to use must be specified with -b; "
2718 "if necessary a new branch can be created for "
2719 "this commit with 'got branch -c %s BRANCH_NAME'",
2720 branch_name, commit_id_str);
2722 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2725 static const struct got_error *
2726 cmd_checkout(int argc, char *argv[])
2728 const struct got_error *error = NULL;
2729 struct got_repository *repo = NULL;
2730 struct got_reference *head_ref = NULL;
2731 struct got_worktree *worktree = NULL;
2732 char *repo_path = NULL;
2733 char *worktree_path = NULL;
2734 const char *path_prefix = "";
2735 const char *branch_name = GOT_REF_HEAD;
2736 char *commit_id_str = NULL;
2738 int ch, same_path_prefix, allow_nonempty = 0;
2739 struct got_pathlist_head paths;
2740 struct got_checkout_progress_arg cpa;
2744 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2747 branch_name = optarg;
2750 commit_id_str = strdup(optarg);
2751 if (commit_id_str == NULL)
2752 return got_error_from_errno("strdup");
2758 path_prefix = optarg;
2770 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2771 "unveil", NULL) == -1)
2775 char *base, *dotgit;
2777 repo_path = realpath(argv[0], NULL);
2778 if (repo_path == NULL)
2779 return got_error_from_errno2("realpath", argv[0]);
2780 cwd = getcwd(NULL, 0);
2782 error = got_error_from_errno("getcwd");
2789 error = got_path_basename(&base, path);
2792 dotgit = strstr(base, ".git");
2795 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2796 error = got_error_from_errno("asprintf");
2801 } else if (argc == 2) {
2802 repo_path = realpath(argv[0], NULL);
2803 if (repo_path == NULL) {
2804 error = got_error_from_errno2("realpath", argv[0]);
2807 worktree_path = realpath(argv[1], NULL);
2808 if (worktree_path == NULL) {
2809 if (errno != ENOENT) {
2810 error = got_error_from_errno2("realpath",
2814 worktree_path = strdup(argv[1]);
2815 if (worktree_path == NULL) {
2816 error = got_error_from_errno("strdup");
2823 got_path_strip_trailing_slashes(repo_path);
2824 got_path_strip_trailing_slashes(worktree_path);
2826 error = got_repo_open(&repo, repo_path, NULL);
2830 /* Pre-create work tree path for unveil(2) */
2831 error = got_path_mkdir(worktree_path);
2833 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2834 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2836 if (!allow_nonempty &&
2837 !got_path_dir_is_empty(worktree_path)) {
2838 error = got_error_path(worktree_path,
2839 GOT_ERR_DIR_NOT_EMPTY);
2844 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2848 error = got_ref_open(&head_ref, repo, branch_name, 0);
2852 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2853 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2856 error = got_worktree_open(&worktree, worktree_path);
2860 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2864 if (!same_path_prefix) {
2865 error = got_error(GOT_ERR_PATH_PREFIX);
2869 if (commit_id_str) {
2870 struct got_object_id *commit_id;
2871 struct got_reflist_head refs;
2873 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2877 error = got_repo_match_object_id(&commit_id, NULL,
2878 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2879 got_ref_list_free(&refs);
2882 error = check_linear_ancestry(commit_id,
2883 got_worktree_get_base_commit_id(worktree), 0, repo);
2884 if (error != NULL) {
2886 if (error->code == GOT_ERR_ANCESTRY) {
2887 error = checkout_ancestry_error(
2888 head_ref, commit_id_str);
2892 error = check_same_branch(commit_id, head_ref, NULL, repo);
2894 if (error->code == GOT_ERR_ANCESTRY) {
2895 error = checkout_ancestry_error(
2896 head_ref, commit_id_str);
2900 error = got_worktree_set_base_commit_id(worktree, repo,
2907 error = got_pathlist_append(&paths, "", NULL);
2910 cpa.worktree_path = worktree_path;
2911 cpa.had_base_commit_ref_error = 0;
2912 error = got_worktree_checkout_files(worktree, &paths, repo,
2913 checkout_progress, &cpa, check_cancelled, NULL);
2917 printf("Now shut up and hack\n");
2918 if (cpa.had_base_commit_ref_error)
2919 show_worktree_base_ref_warning();
2921 got_pathlist_free(&paths);
2922 free(commit_id_str);
2924 free(worktree_path);
2929 struct got_update_progress_arg {
2937 print_update_progress_stats(struct got_update_progress_arg *upa)
2939 if (!upa->did_something)
2942 if (upa->conflicts > 0)
2943 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2944 if (upa->obstructed > 0)
2945 printf("File paths obstructed by a non-regular file: %d\n",
2947 if (upa->not_updated > 0)
2948 printf("Files not updated because of existing merge "
2949 "conflicts: %d\n", upa->not_updated);
2955 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2960 static const struct got_error *
2961 update_progress(void *arg, unsigned char status, const char *path)
2963 struct got_update_progress_arg *upa = arg;
2965 if (status == GOT_STATUS_EXISTS ||
2966 status == GOT_STATUS_BASE_REF_ERR)
2969 upa->did_something = 1;
2971 /* Base commit bump happens silently. */
2972 if (status == GOT_STATUS_BUMP_BASE)
2975 if (status == GOT_STATUS_CONFLICT)
2977 if (status == GOT_STATUS_OBSTRUCTED)
2979 if (status == GOT_STATUS_CANNOT_UPDATE)
2982 while (path[0] == '/')
2984 printf("%c %s\n", status, path);
2988 static const struct got_error *
2989 switch_head_ref(struct got_reference *head_ref,
2990 struct got_object_id *commit_id, struct got_worktree *worktree,
2991 struct got_repository *repo)
2993 const struct got_error *err = NULL;
2995 int ref_has_moved = 0;
2997 /* Trivial case: switching between two different references. */
2998 if (strcmp(got_ref_get_name(head_ref),
2999 got_worktree_get_head_ref_name(worktree)) != 0) {
3000 printf("Switching work tree from %s to %s\n",
3001 got_worktree_get_head_ref_name(worktree),
3002 got_ref_get_name(head_ref));
3003 return got_worktree_set_head_ref(worktree, head_ref);
3006 err = check_linear_ancestry(commit_id,
3007 got_worktree_get_base_commit_id(worktree), 0, repo);
3009 if (err->code != GOT_ERR_ANCESTRY)
3016 /* Switching to a rebased branch with the same reference name. */
3017 err = got_object_id_str(&base_id_str,
3018 got_worktree_get_base_commit_id(worktree));
3021 printf("Reference %s now points at a different branch\n",
3022 got_worktree_get_head_ref_name(worktree));
3023 printf("Switching work tree from %s to %s\n", base_id_str,
3024 got_worktree_get_head_ref_name(worktree));
3028 static const struct got_error *
3029 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3031 const struct got_error *err;
3034 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3038 return got_error(GOT_ERR_REBASING);
3040 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3044 return got_error(GOT_ERR_HISTEDIT_BUSY);
3049 static const struct got_error *
3050 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3051 char *argv[], struct got_worktree *worktree)
3053 const struct got_error *err = NULL;
3060 return got_error_from_errno("strdup");
3061 return got_pathlist_append(paths, path, NULL);
3064 for (i = 0; i < argc; i++) {
3065 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3068 err = got_pathlist_append(paths, path, NULL);
3078 static const struct got_error *
3079 wrap_not_worktree_error(const struct got_error *orig_err,
3080 const char *cmdname, const char *path)
3082 const struct got_error *err;
3083 struct got_repository *repo;
3084 static char msg[512];
3086 err = got_repo_open(&repo, path, NULL);
3090 snprintf(msg, sizeof(msg),
3091 "'got %s' needs a work tree in addition to a git repository\n"
3092 "Work trees can be checked out from this Git repository with "
3094 "The got(1) manual page contains more information.", cmdname);
3095 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3096 got_repo_close(repo);
3100 static const struct got_error *
3101 cmd_update(int argc, char *argv[])
3103 const struct got_error *error = NULL;
3104 struct got_repository *repo = NULL;
3105 struct got_worktree *worktree = NULL;
3106 char *worktree_path = NULL;
3107 struct got_object_id *commit_id = NULL;
3108 char *commit_id_str = NULL;
3109 const char *branch_name = NULL;
3110 struct got_reference *head_ref = NULL;
3111 struct got_pathlist_head paths;
3112 struct got_pathlist_entry *pe;
3114 struct got_update_progress_arg upa;
3118 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3121 branch_name = optarg;
3124 commit_id_str = strdup(optarg);
3125 if (commit_id_str == NULL)
3126 return got_error_from_errno("strdup");
3138 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3139 "unveil", NULL) == -1)
3142 worktree_path = getcwd(NULL, 0);
3143 if (worktree_path == NULL) {
3144 error = got_error_from_errno("getcwd");
3147 error = got_worktree_open(&worktree, worktree_path);
3149 if (error->code == GOT_ERR_NOT_WORKTREE)
3150 error = wrap_not_worktree_error(error, "update",
3155 error = check_rebase_or_histedit_in_progress(worktree);
3159 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3164 error = apply_unveil(got_repo_get_path(repo), 0,
3165 got_worktree_get_root_path(worktree));
3169 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3173 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3174 got_worktree_get_head_ref_name(worktree), 0);
3177 if (commit_id_str == NULL) {
3178 error = got_ref_resolve(&commit_id, repo, head_ref);
3181 error = got_object_id_str(&commit_id_str, commit_id);
3185 struct got_reflist_head refs;
3187 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3191 error = got_repo_match_object_id(&commit_id, NULL,
3192 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3193 got_ref_list_free(&refs);
3194 free(commit_id_str);
3195 commit_id_str = NULL;
3198 error = got_object_id_str(&commit_id_str, commit_id);
3204 struct got_object_id *head_commit_id;
3205 TAILQ_FOREACH(pe, &paths, entry) {
3206 if (pe->path_len == 0)
3208 error = got_error_msg(GOT_ERR_BAD_PATH,
3209 "switching between branches requires that "
3210 "the entire work tree gets updated");
3213 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3216 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3218 free(head_commit_id);
3221 error = check_same_branch(commit_id, head_ref, NULL, repo);
3224 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3228 error = check_linear_ancestry(commit_id,
3229 got_worktree_get_base_commit_id(worktree), 0, repo);
3230 if (error != NULL) {
3231 if (error->code == GOT_ERR_ANCESTRY)
3232 error = got_error(GOT_ERR_BRANCH_MOVED);
3235 error = check_same_branch(commit_id, head_ref, NULL, repo);
3240 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3242 error = got_worktree_set_base_commit_id(worktree, repo,
3248 memset(&upa, 0, sizeof(upa));
3249 error = got_worktree_checkout_files(worktree, &paths, repo,
3250 update_progress, &upa, check_cancelled, NULL);
3254 if (upa.did_something)
3255 printf("Updated to commit %s\n", commit_id_str);
3257 printf("Already up-to-date\n");
3258 print_update_progress_stats(&upa);
3260 free(worktree_path);
3261 TAILQ_FOREACH(pe, &paths, entry)
3262 free((char *)pe->path);
3263 got_pathlist_free(&paths);
3265 free(commit_id_str);
3269 static const struct got_error *
3270 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3271 const char *path, int diff_context, int ignore_whitespace,
3272 int force_text_diff, struct got_repository *repo)
3274 const struct got_error *err = NULL;
3275 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3278 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3283 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3287 while (path[0] == '/')
3289 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3290 diff_context, ignore_whitespace, force_text_diff, stdout);
3293 got_object_blob_close(blob1);
3294 got_object_blob_close(blob2);
3298 static const struct got_error *
3299 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3300 const char *path, int diff_context, int ignore_whitespace,
3301 int force_text_diff, struct got_repository *repo)
3303 const struct got_error *err = NULL;
3304 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 struct got_diff_blob_output_unidiff_arg arg;
3308 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3313 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3317 arg.diff_context = diff_context;
3318 arg.ignore_whitespace = ignore_whitespace;
3319 arg.force_text_diff = force_text_diff;
3320 arg.outfile = stdout;
3321 arg.line_offsets = NULL;
3323 while (path[0] == '/')
3325 err = got_diff_tree(tree1, tree2, path, path, repo,
3326 got_diff_blob_output_unidiff, &arg, 1);
3329 got_object_tree_close(tree1);
3331 got_object_tree_close(tree2);
3335 static const struct got_error *
3336 get_changed_paths(struct got_pathlist_head *paths,
3337 struct got_commit_object *commit, struct got_repository *repo)
3339 const struct got_error *err = NULL;
3340 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3341 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3342 struct got_object_qid *qid;
3344 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3346 struct got_commit_object *pcommit;
3347 err = got_object_open_as_commit(&pcommit, repo,
3352 tree_id1 = got_object_commit_get_tree_id(pcommit);
3353 got_object_commit_close(pcommit);
3358 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3363 tree_id2 = got_object_commit_get_tree_id(commit);
3364 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3368 err = got_diff_tree(tree1, tree2, "", "", repo,
3369 got_diff_tree_collect_changed_paths, paths, 0);
3372 got_object_tree_close(tree1);
3374 got_object_tree_close(tree2);
3378 static const struct got_error *
3379 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3380 const char *path, int diff_context, struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 struct got_commit_object *pcommit = NULL;
3384 char *id_str1 = NULL, *id_str2 = NULL;
3385 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3386 struct got_object_qid *qid;
3388 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3390 err = got_object_open_as_commit(&pcommit, repo,
3396 if (path && path[0] != '\0') {
3398 err = got_object_id_by_path(&obj_id2, repo, id, path);
3401 err = got_object_id_str(&id_str2, obj_id2);
3407 err = got_object_id_by_path(&obj_id1, repo,
3410 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3415 err = got_object_id_str(&id_str1, obj_id1);
3422 err = got_object_get_type(&obj_type, repo, obj_id2);
3427 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3429 case GOT_OBJ_TYPE_BLOB:
3430 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3433 case GOT_OBJ_TYPE_TREE:
3434 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3438 err = got_error(GOT_ERR_OBJ_TYPE);
3444 obj_id2 = got_object_commit_get_tree_id(commit);
3445 err = got_object_id_str(&id_str2, obj_id2);
3449 obj_id1 = got_object_commit_get_tree_id(pcommit);
3450 err = got_object_id_str(&id_str1, obj_id1);
3454 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3456 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3463 got_object_commit_close(pcommit);
3468 get_datestr(time_t *time, char *datebuf)
3470 struct tm mytm, *tm;
3473 tm = gmtime_r(time, &mytm);
3476 s = asctime_r(tm, datebuf);
3479 p = strchr(s, '\n');
3485 static const struct got_error *
3486 match_logmsg(int *have_match, struct got_object_id *id,
3487 struct got_commit_object *commit, regex_t *regex)
3489 const struct got_error *err = NULL;
3490 regmatch_t regmatch;
3491 char *id_str = NULL, *logmsg = NULL;
3495 err = got_object_id_str(&id_str, id);
3499 err = got_object_commit_get_logmsg(&logmsg, commit);
3503 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3512 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3515 regmatch_t regmatch;
3516 struct got_pathlist_entry *pe;
3520 TAILQ_FOREACH(pe, changed_paths, entry) {
3521 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3528 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3530 static const struct got_error*
3531 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3532 struct got_object_id *id, struct got_repository *repo)
3534 static const struct got_error *err = NULL;
3535 struct got_reflist_entry *re;
3541 TAILQ_FOREACH(re, refs, entry) {
3542 struct got_tag_object *tag = NULL;
3543 struct got_object_id *ref_id;
3546 name = got_ref_get_name(re->ref);
3547 if (strcmp(name, GOT_REF_HEAD) == 0)
3549 if (strncmp(name, "refs/", 5) == 0)
3551 if (strncmp(name, "got/", 4) == 0)
3553 if (strncmp(name, "heads/", 6) == 0)
3555 if (strncmp(name, "remotes/", 8) == 0) {
3557 s = strstr(name, "/" GOT_REF_HEAD);
3558 if (s != NULL && s[strlen(s)] == '\0')
3561 err = got_ref_resolve(&ref_id, repo, re->ref);
3564 if (strncmp(name, "tags/", 5) == 0) {
3565 err = got_object_open_as_tag(&tag, repo, ref_id);
3567 if (err->code != GOT_ERR_OBJ_TYPE) {
3571 /* Ref points at something other than a tag. */
3576 cmp = got_object_id_cmp(tag ?
3577 got_object_tag_get_object_id(tag) : ref_id, id);
3580 got_object_tag_close(tag);
3584 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3585 s ? ", " : "", name) == -1) {
3586 err = got_error_from_errno("asprintf");
3597 static const struct got_error *
3598 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3599 struct got_repository *repo, const char *path,
3600 struct got_pathlist_head *changed_paths, int show_patch,
3601 int diff_context, struct got_reflist_object_id_map *refs_idmap)
3603 const struct got_error *err = NULL;
3604 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3606 time_t committer_time;
3607 const char *author, *committer;
3608 char *refs_str = NULL;
3609 struct got_reflist_head *refs;
3611 err = got_object_id_str(&id_str, id);
3615 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3617 err = build_refs_str(&refs_str, refs, id, repo);
3622 printf(GOT_COMMIT_SEP_STR);
3623 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3624 refs_str ? refs_str : "", refs_str ? ")" : "");
3629 printf("from: %s\n", got_object_commit_get_author(commit));
3630 committer_time = got_object_commit_get_committer_time(commit);
3631 datestr = get_datestr(&committer_time, datebuf);
3633 printf("date: %s UTC\n", datestr);
3634 author = got_object_commit_get_author(commit);
3635 committer = got_object_commit_get_committer(commit);
3636 if (strcmp(author, committer) != 0)
3637 printf("via: %s\n", committer);
3638 if (got_object_commit_get_nparents(commit) > 1) {
3639 const struct got_object_id_queue *parent_ids;
3640 struct got_object_qid *qid;
3642 parent_ids = got_object_commit_get_parent_ids(commit);
3643 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3644 err = got_object_id_str(&id_str, qid->id);
3647 printf("parent %d: %s\n", n++, id_str);
3653 err = got_object_commit_get_logmsg(&logmsg0, commit);
3659 line = strsep(&logmsg, "\n");
3661 printf(" %s\n", line);
3665 if (changed_paths) {
3666 struct got_pathlist_entry *pe;
3667 TAILQ_FOREACH(pe, changed_paths, entry) {
3668 struct got_diff_changed_path *cp = pe->data;
3669 printf(" %c %s\n", cp->status, pe->path);
3674 err = print_patch(commit, id, path, diff_context, repo);
3679 if (fflush(stdout) != 0 && err == NULL)
3680 err = got_error_from_errno("fflush");
3687 static const struct got_error *
3688 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3689 struct got_repository *repo, const char *path, int show_changed_paths,
3690 int show_patch, const char *search_pattern, int diff_context, int limit,
3691 int log_branches, int reverse_display_order,
3692 struct got_reflist_object_id_map *refs_idmap)
3694 const struct got_error *err;
3695 struct got_commit_graph *graph;
3698 struct got_object_id_queue reversed_commits;
3699 struct got_object_qid *qid;
3700 struct got_commit_object *commit;
3701 struct got_pathlist_head changed_paths;
3702 struct got_pathlist_entry *pe;
3704 SIMPLEQ_INIT(&reversed_commits);
3705 TAILQ_INIT(&changed_paths);
3707 if (search_pattern && regcomp(®ex, search_pattern,
3708 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3709 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3711 err = got_commit_graph_open(&graph, path, !log_branches);
3714 err = got_commit_graph_iter_start(graph, root_id, repo,
3715 check_cancelled, NULL);
3719 struct got_object_id *id;
3721 if (sigint_received || sigpipe_received)
3724 err = got_commit_graph_iter_next(&id, graph, repo,
3725 check_cancelled, NULL);
3727 if (err->code == GOT_ERR_ITER_COMPLETED)
3734 err = got_object_open_as_commit(&commit, repo, id);
3738 if (show_changed_paths && !reverse_display_order) {
3739 err = get_changed_paths(&changed_paths, commit, repo);
3744 if (search_pattern) {
3745 err = match_logmsg(&have_match, id, commit, ®ex);
3747 got_object_commit_close(commit);
3750 if (have_match == 0 && show_changed_paths)
3751 match_changed_paths(&have_match,
3752 &changed_paths, ®ex);
3753 if (have_match == 0) {
3754 got_object_commit_close(commit);
3755 TAILQ_FOREACH(pe, &changed_paths, entry) {
3756 free((char *)pe->path);
3759 got_pathlist_free(&changed_paths);
3764 if (reverse_display_order) {
3765 err = got_object_qid_alloc(&qid, id);
3768 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3769 got_object_commit_close(commit);
3771 err = print_commit(commit, id, repo, path,
3772 show_changed_paths ? &changed_paths : NULL,
3773 show_patch, diff_context, refs_idmap);
3774 got_object_commit_close(commit);
3778 if ((limit && --limit == 0) ||
3779 (end_id && got_object_id_cmp(id, end_id) == 0))
3782 TAILQ_FOREACH(pe, &changed_paths, entry) {
3783 free((char *)pe->path);
3786 got_pathlist_free(&changed_paths);
3788 if (reverse_display_order) {
3789 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3790 err = got_object_open_as_commit(&commit, repo, qid->id);
3793 if (show_changed_paths) {
3794 err = get_changed_paths(&changed_paths,
3799 err = print_commit(commit, qid->id, repo, path,
3800 show_changed_paths ? &changed_paths : NULL,
3801 show_patch, diff_context, refs_idmap);
3802 got_object_commit_close(commit);
3805 TAILQ_FOREACH(pe, &changed_paths, entry) {
3806 free((char *)pe->path);
3809 got_pathlist_free(&changed_paths);
3813 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3814 qid = SIMPLEQ_FIRST(&reversed_commits);
3815 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3816 got_object_qid_free(qid);
3818 TAILQ_FOREACH(pe, &changed_paths, entry) {
3819 free((char *)pe->path);
3822 got_pathlist_free(&changed_paths);
3825 got_commit_graph_close(graph);
3832 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3833 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3834 "[-R] [path]\n", getprogname());
3839 get_default_log_limit(void)
3841 const char *got_default_log_limit;
3845 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3846 if (got_default_log_limit == NULL)
3848 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3854 static const struct got_error *
3855 cmd_log(int argc, char *argv[])
3857 const struct got_error *error;
3858 struct got_repository *repo = NULL;
3859 struct got_worktree *worktree = NULL;
3860 struct got_object_id *start_id = NULL, *end_id = NULL;
3861 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3862 const char *start_commit = NULL, *end_commit = NULL;
3863 const char *search_pattern = NULL;
3864 int diff_context = -1, ch;
3865 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3866 int reverse_display_order = 0;
3868 struct got_reflist_head refs;
3869 struct got_reflist_object_id_map *refs_idmap = NULL;
3874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3880 limit = get_default_log_limit();
3882 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3888 show_changed_paths = 1;
3891 start_commit = optarg;
3894 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3897 err(1, "-C option %s", errstr);
3900 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3902 err(1, "-l option %s", errstr);
3908 repo_path = realpath(optarg, NULL);
3909 if (repo_path == NULL)
3910 return got_error_from_errno2("realpath",
3912 got_path_strip_trailing_slashes(repo_path);
3915 reverse_display_order = 1;
3918 search_pattern = optarg;
3921 end_commit = optarg;
3932 if (diff_context == -1)
3934 else if (!show_patch)
3935 errx(1, "-C requires -p");
3937 cwd = getcwd(NULL, 0);
3939 error = got_error_from_errno("getcwd");
3943 if (repo_path == NULL) {
3944 error = got_worktree_open(&worktree, cwd);
3945 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3952 error = got_worktree_resolve_path(&path, worktree,
3957 path = strdup(argv[0]);
3959 error = got_error_from_errno("strdup");
3963 } else if (argc != 0)
3966 if (repo_path == NULL) {
3967 repo_path = worktree ?
3968 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3970 if (repo_path == NULL) {
3971 error = got_error_from_errno("strdup");
3975 error = got_repo_open(&repo, repo_path, NULL);
3979 error = apply_unveil(got_repo_get_path(repo), 1,
3980 worktree ? got_worktree_get_root_path(worktree) : NULL);
3984 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3988 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3992 if (start_commit == NULL) {
3993 struct got_reference *head_ref;
3994 struct got_commit_object *commit = NULL;
3995 error = got_ref_open(&head_ref, repo,
3996 worktree ? got_worktree_get_head_ref_name(worktree)
4000 error = got_ref_resolve(&start_id, repo, head_ref);
4001 got_ref_close(head_ref);
4004 error = got_object_open_as_commit(&commit, repo,
4008 got_object_commit_close(commit);
4010 error = got_repo_match_object_id(&start_id, NULL,
4011 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4015 if (end_commit != NULL) {
4016 error = got_repo_match_object_id(&end_id, NULL,
4017 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4024 * If a path was specified on the command line it was resolved
4025 * to a path in the work tree above. Prepend the work tree's
4026 * path prefix to obtain the corresponding in-repository path.
4030 prefix = got_worktree_get_path_prefix(worktree);
4031 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4032 (path[0] != '\0') ? "/" : "", path) == -1) {
4033 error = got_error_from_errno("asprintf");
4038 error = got_repo_map_path(&in_repo_path, repo,
4044 path = in_repo_path;
4047 error = print_commits(start_id, end_id, repo, path ? path : "",
4048 show_changed_paths, show_patch, search_pattern, diff_context,
4049 limit, log_branches, reverse_display_order, refs_idmap);
4055 got_worktree_close(worktree);
4057 const struct got_error *repo_error;
4058 repo_error = got_repo_close(repo);
4063 got_reflist_object_id_map_free(refs_idmap);
4064 got_ref_list_free(&refs);
4071 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4072 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4076 struct print_diff_arg {
4077 struct got_repository *repo;
4078 struct got_worktree *worktree;
4083 int ignore_whitespace;
4084 int force_text_diff;
4088 * Create a file which contains the target path of a symlink so we can feed
4089 * it as content to the diff engine.
4091 static const struct got_error *
4092 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4093 const char *abspath)
4095 const struct got_error *err = NULL;
4096 char target_path[PATH_MAX];
4097 ssize_t target_len, outlen;
4102 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4103 if (target_len == -1)
4104 return got_error_from_errno2("readlinkat", abspath);
4106 target_len = readlink(abspath, target_path, PATH_MAX);
4107 if (target_len == -1)
4108 return got_error_from_errno2("readlink", abspath);
4111 *fd = got_opentempfd();
4113 return got_error_from_errno("got_opentempfd");
4115 outlen = write(*fd, target_path, target_len);
4117 err = got_error_from_errno("got_opentempfd");
4121 if (lseek(*fd, 0, SEEK_SET) == -1) {
4122 err = got_error_from_errno2("lseek", abspath);
4133 static const struct got_error *
4134 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4135 const char *path, struct got_object_id *blob_id,
4136 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4137 int dirfd, const char *de_name)
4139 struct print_diff_arg *a = arg;
4140 const struct got_error *err = NULL;
4141 struct got_blob_object *blob1 = NULL;
4144 char *abspath = NULL, *label1 = NULL;
4147 if (a->diff_staged) {
4148 if (staged_status != GOT_STATUS_MODIFY &&
4149 staged_status != GOT_STATUS_ADD &&
4150 staged_status != GOT_STATUS_DELETE)
4153 if (staged_status == GOT_STATUS_DELETE)
4155 if (status == GOT_STATUS_NONEXISTENT)
4156 return got_error_set_errno(ENOENT, path);
4157 if (status != GOT_STATUS_MODIFY &&
4158 status != GOT_STATUS_ADD &&
4159 status != GOT_STATUS_DELETE &&
4160 status != GOT_STATUS_CONFLICT)
4164 if (!a->header_shown) {
4165 printf("diff %s %s%s\n", a->id_str,
4166 got_worktree_get_root_path(a->worktree),
4167 a->diff_staged ? " (staged changes)" : "");
4168 a->header_shown = 1;
4171 if (a->diff_staged) {
4172 const char *label1 = NULL, *label2 = NULL;
4173 switch (staged_status) {
4174 case GOT_STATUS_MODIFY:
4178 case GOT_STATUS_ADD:
4181 case GOT_STATUS_DELETE:
4185 return got_error(GOT_ERR_FILE_STATUS);
4187 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4188 staged_blob_id, label1, label2, a->diff_context,
4189 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4192 if (staged_status == GOT_STATUS_ADD ||
4193 staged_status == GOT_STATUS_MODIFY) {
4195 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4199 err = got_object_id_str(&id_str, staged_blob_id);
4202 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4203 err = got_error_from_errno("asprintf");
4208 } else if (status != GOT_STATUS_ADD) {
4209 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4214 if (status != GOT_STATUS_DELETE) {
4215 if (asprintf(&abspath, "%s/%s",
4216 got_worktree_get_root_path(a->worktree), path) == -1) {
4217 err = got_error_from_errno("asprintf");
4222 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4224 if (errno != ELOOP) {
4225 err = got_error_from_errno2("openat",
4229 err = get_symlink_target_file(&fd, dirfd,
4235 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4237 if (errno != ELOOP) {
4238 err = got_error_from_errno2("open",
4242 err = get_symlink_target_file(&fd, dirfd,
4248 if (fstat(fd, &sb) == -1) {
4249 err = got_error_from_errno2("fstat", abspath);
4252 f2 = fdopen(fd, "r");
4254 err = got_error_from_errno2("fdopen", abspath);
4261 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4262 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4265 got_object_blob_close(blob1);
4266 if (f2 && fclose(f2) == EOF && err == NULL)
4267 err = got_error_from_errno("fclose");
4268 if (fd != -1 && close(fd) == -1 && err == NULL)
4269 err = got_error_from_errno("close");
4274 static const struct got_error *
4275 cmd_diff(int argc, char *argv[])
4277 const struct got_error *error;
4278 struct got_repository *repo = NULL;
4279 struct got_worktree *worktree = NULL;
4280 char *cwd = NULL, *repo_path = NULL;
4281 struct got_object_id *id1 = NULL, *id2 = NULL;
4282 const char *id_str1 = NULL, *id_str2 = NULL;
4283 char *label1 = NULL, *label2 = NULL;
4285 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4286 int force_text_diff = 0;
4289 struct got_reflist_head refs;
4294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4299 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4302 force_text_diff = 1;
4305 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4308 err(1, "-C option %s", errstr);
4311 repo_path = realpath(optarg, NULL);
4312 if (repo_path == NULL)
4313 return got_error_from_errno2("realpath",
4315 got_path_strip_trailing_slashes(repo_path);
4321 ignore_whitespace = 1;
4332 cwd = getcwd(NULL, 0);
4334 error = got_error_from_errno("getcwd");
4340 "-r option can't be used when diffing a work tree");
4341 error = got_worktree_open(&worktree, cwd);
4343 if (error->code == GOT_ERR_NOT_WORKTREE)
4344 error = wrap_not_worktree_error(error, "diff",
4348 repo_path = strdup(got_worktree_get_repo_path(worktree));
4349 if (repo_path == NULL) {
4350 error = got_error_from_errno("strdup");
4354 error = got_worktree_resolve_path(&path, worktree,
4361 error = got_error_from_errno("strdup");
4365 } else if (argc == 2) {
4367 errx(1, "-s option can't be used when diffing "
4368 "objects in repository");
4371 if (repo_path == NULL) {
4372 error = got_worktree_open(&worktree, cwd);
4373 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4377 got_worktree_get_repo_path(worktree));
4378 if (repo_path == NULL) {
4379 error = got_error_from_errno("strdup");
4383 repo_path = strdup(cwd);
4384 if (repo_path == NULL) {
4385 error = got_error_from_errno("strdup");
4393 error = got_repo_open(&repo, repo_path, NULL);
4398 error = apply_unveil(got_repo_get_path(repo), 1,
4399 worktree ? got_worktree_get_root_path(worktree) : NULL);
4404 struct print_diff_arg arg;
4405 struct got_pathlist_head paths;
4410 error = got_object_id_str(&id_str,
4411 got_worktree_get_base_commit_id(worktree));
4415 arg.worktree = worktree;
4416 arg.diff_context = diff_context;
4417 arg.id_str = id_str;
4418 arg.header_shown = 0;
4419 arg.diff_staged = diff_staged;
4420 arg.ignore_whitespace = ignore_whitespace;
4421 arg.force_text_diff = force_text_diff;
4423 error = got_pathlist_append(&paths, path, NULL);
4427 error = got_worktree_status(worktree, &paths, repo, print_diff,
4428 &arg, check_cancelled, NULL);
4430 got_pathlist_free(&paths);
4434 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4438 error = got_repo_match_object_id(&id1, &label1, id_str1,
4439 GOT_OBJ_TYPE_ANY, &refs, repo);
4443 error = got_repo_match_object_id(&id2, &label2, id_str2,
4444 GOT_OBJ_TYPE_ANY, &refs, repo);
4448 error = got_object_get_type(&type1, repo, id1);
4452 error = got_object_get_type(&type2, repo, id2);
4456 if (type1 != type2) {
4457 error = got_error(GOT_ERR_OBJ_TYPE);
4462 case GOT_OBJ_TYPE_BLOB:
4463 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4464 NULL, NULL, diff_context, ignore_whitespace,
4465 force_text_diff, repo, stdout);
4467 case GOT_OBJ_TYPE_TREE:
4468 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4469 "", "", diff_context, ignore_whitespace, force_text_diff,
4472 case GOT_OBJ_TYPE_COMMIT:
4473 printf("diff %s %s\n", label1, label2);
4474 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4475 diff_context, ignore_whitespace, force_text_diff, repo,
4479 error = got_error(GOT_ERR_OBJ_TYPE);
4488 got_worktree_close(worktree);
4490 const struct got_error *repo_error;
4491 repo_error = got_repo_close(repo);
4495 got_ref_list_free(&refs);
4503 "usage: %s blame [-c commit] [-r repository-path] path\n",
4512 char datebuf[11]; /* YYYY-MM-DD + NUL */
4515 struct blame_cb_args {
4516 struct blame_line *lines;
4520 off_t *line_offsets;
4522 struct got_repository *repo;
4525 static const struct got_error *
4526 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4528 const struct got_error *err = NULL;
4529 struct blame_cb_args *a = arg;
4530 struct blame_line *bline;
4532 size_t linesize = 0;
4533 struct got_commit_object *commit = NULL;
4536 time_t committer_time;
4538 if (nlines != a->nlines ||
4539 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4540 return got_error(GOT_ERR_RANGE);
4542 if (sigint_received)
4543 return got_error(GOT_ERR_ITER_COMPLETED);
4546 return NULL; /* no change in this commit */
4548 /* Annotate this line. */
4549 bline = &a->lines[lineno - 1];
4550 if (bline->annotated)
4552 err = got_object_id_str(&bline->id_str, id);
4556 err = got_object_open_as_commit(&commit, a->repo, id);
4560 bline->committer = strdup(got_object_commit_get_committer(commit));
4561 if (bline->committer == NULL) {
4562 err = got_error_from_errno("strdup");
4566 committer_time = got_object_commit_get_committer_time(commit);
4567 if (localtime_r(&committer_time, &tm) == NULL)
4568 return got_error_from_errno("localtime_r");
4569 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4570 &tm) >= sizeof(bline->datebuf)) {
4571 err = got_error(GOT_ERR_NO_SPACE);
4574 bline->annotated = 1;
4576 /* Print lines annotated so far. */
4577 bline = &a->lines[a->lineno_cur - 1];
4578 if (!bline->annotated)
4581 offset = a->line_offsets[a->lineno_cur - 1];
4582 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4583 err = got_error_from_errno("fseeko");
4587 while (bline->annotated) {
4588 char *smallerthan, *at, *nl, *committer;
4591 if (getline(&line, &linesize, a->f) == -1) {
4593 err = got_error_from_errno("getline");
4597 committer = bline->committer;
4598 smallerthan = strchr(committer, '<');
4599 if (smallerthan && smallerthan[1] != '\0')
4600 committer = smallerthan + 1;
4601 at = strchr(committer, '@');
4604 len = strlen(committer);
4606 committer[8] = '\0';
4608 nl = strchr(line, '\n');
4611 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4612 bline->id_str, bline->datebuf, committer, line);
4615 bline = &a->lines[a->lineno_cur - 1];
4619 got_object_commit_close(commit);
4624 static const struct got_error *
4625 cmd_blame(int argc, char *argv[])
4627 const struct got_error *error;
4628 struct got_repository *repo = NULL;
4629 struct got_worktree *worktree = NULL;
4630 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4631 char *link_target = NULL;
4632 struct got_object_id *obj_id = NULL;
4633 struct got_object_id *commit_id = NULL;
4634 struct got_blob_object *blob = NULL;
4635 char *commit_id_str = NULL;
4636 struct blame_cb_args bca;
4637 int ch, obj_type, i;
4640 memset(&bca, 0, sizeof(bca));
4643 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4648 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4651 commit_id_str = optarg;
4654 repo_path = realpath(optarg, NULL);
4655 if (repo_path == NULL)
4656 return got_error_from_errno2("realpath",
4658 got_path_strip_trailing_slashes(repo_path);
4674 cwd = getcwd(NULL, 0);
4676 error = got_error_from_errno("getcwd");
4679 if (repo_path == NULL) {
4680 error = got_worktree_open(&worktree, cwd);
4681 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4687 strdup(got_worktree_get_repo_path(worktree));
4688 if (repo_path == NULL) {
4689 error = got_error_from_errno("strdup");
4694 repo_path = strdup(cwd);
4695 if (repo_path == NULL) {
4696 error = got_error_from_errno("strdup");
4702 error = got_repo_open(&repo, repo_path, NULL);
4707 const char *prefix = got_worktree_get_path_prefix(worktree);
4710 error = got_worktree_resolve_path(&p, worktree, path);
4713 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4714 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4716 error = got_error_from_errno("asprintf");
4721 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4723 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4726 error = got_repo_map_path(&in_repo_path, repo, path);
4731 if (commit_id_str == NULL) {
4732 struct got_reference *head_ref;
4733 error = got_ref_open(&head_ref, repo, worktree ?
4734 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4737 error = got_ref_resolve(&commit_id, repo, head_ref);
4738 got_ref_close(head_ref);
4742 struct got_reflist_head refs;
4744 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4748 error = got_repo_match_object_id(&commit_id, NULL,
4749 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4750 got_ref_list_free(&refs);
4755 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4760 error = got_object_id_by_path(&obj_id, repo, commit_id,
4761 link_target ? link_target : in_repo_path);
4765 error = got_object_get_type(&obj_type, repo, obj_id);
4769 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4770 error = got_error_path(link_target ? link_target : in_repo_path,
4775 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4778 bca.f = got_opentemp();
4779 if (bca.f == NULL) {
4780 error = got_error_from_errno("got_opentemp");
4783 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4784 &bca.line_offsets, bca.f, blob);
4785 if (error || bca.nlines == 0)
4788 /* Don't include \n at EOF in the blame line count. */
4789 if (bca.line_offsets[bca.nlines - 1] == filesize)
4792 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4793 if (bca.lines == NULL) {
4794 error = got_error_from_errno("calloc");
4798 bca.nlines_prec = 0;
4806 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4807 repo, blame_cb, &bca, check_cancelled, NULL);
4816 got_object_blob_close(blob);
4818 got_worktree_close(worktree);
4820 const struct got_error *repo_error;
4821 repo_error = got_repo_close(repo);
4826 for (i = 0; i < bca.nlines; i++) {
4827 struct blame_line *bline = &bca.lines[i];
4828 free(bline->id_str);
4829 free(bline->committer);
4833 free(bca.line_offsets);
4834 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4835 error = got_error_from_errno("fclose");
4843 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4848 static const struct got_error *
4849 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4850 const char *root_path, struct got_repository *repo)
4852 const struct got_error *err = NULL;
4853 int is_root_path = (strcmp(path, root_path) == 0);
4854 const char *modestr = "";
4855 mode_t mode = got_tree_entry_get_mode(te);
4856 char *link_target = NULL;
4858 path += strlen(root_path);
4859 while (path[0] == '/')
4862 if (got_object_tree_entry_is_submodule(te))
4864 else if (S_ISLNK(mode)) {
4867 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4870 for (i = 0; i < strlen(link_target); i++) {
4871 if (!isprint((unsigned char)link_target[i]))
4872 link_target[i] = '?';
4877 else if (S_ISDIR(mode))
4879 else if (mode & S_IXUSR)
4882 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4883 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4884 link_target ? " -> ": "", link_target ? link_target : "");
4890 static const struct got_error *
4891 print_tree(const char *path, struct got_object_id *commit_id,
4892 int show_ids, int recurse, const char *root_path,
4893 struct got_repository *repo)
4895 const struct got_error *err = NULL;
4896 struct got_object_id *tree_id = NULL;
4897 struct got_tree_object *tree = NULL;
4900 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4904 err = got_object_open_as_tree(&tree, repo, tree_id);
4907 nentries = got_object_tree_get_nentries(tree);
4908 for (i = 0; i < nentries; i++) {
4909 struct got_tree_entry *te;
4912 if (sigint_received || sigpipe_received)
4915 te = got_object_tree_get_entry(tree, i);
4918 err = got_object_id_str(&id_str,
4919 got_tree_entry_get_id(te));
4922 if (asprintf(&id, "%s ", id_str) == -1) {
4923 err = got_error_from_errno("asprintf");
4929 err = print_entry(te, id, path, root_path, repo);
4934 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4936 if (asprintf(&child_path, "%s%s%s", path,
4937 path[0] == '/' && path[1] == '\0' ? "" : "/",
4938 got_tree_entry_get_name(te)) == -1) {
4939 err = got_error_from_errno("asprintf");
4942 err = print_tree(child_path, commit_id, show_ids, 1,
4951 got_object_tree_close(tree);
4956 static const struct got_error *
4957 cmd_tree(int argc, char *argv[])
4959 const struct got_error *error;
4960 struct got_repository *repo = NULL;
4961 struct got_worktree *worktree = NULL;
4962 const char *path, *refname = NULL;
4963 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4964 struct got_object_id *commit_id = NULL;
4965 char *commit_id_str = NULL;
4966 int show_ids = 0, recurse = 0;
4970 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4975 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4978 commit_id_str = optarg;
4981 repo_path = realpath(optarg, NULL);
4982 if (repo_path == NULL)
4983 return got_error_from_errno2("realpath",
4985 got_path_strip_trailing_slashes(repo_path);
5009 cwd = getcwd(NULL, 0);
5011 error = got_error_from_errno("getcwd");
5014 if (repo_path == NULL) {
5015 error = got_worktree_open(&worktree, cwd);
5016 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5022 strdup(got_worktree_get_repo_path(worktree));
5023 if (repo_path == NULL)
5024 error = got_error_from_errno("strdup");
5028 repo_path = strdup(cwd);
5029 if (repo_path == NULL) {
5030 error = got_error_from_errno("strdup");
5036 error = got_repo_open(&repo, repo_path, NULL);
5041 const char *prefix = got_worktree_get_path_prefix(worktree);
5046 error = got_worktree_resolve_path(&p, worktree, path);
5049 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5050 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5052 error = got_error_from_errno("asprintf");
5057 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5061 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5066 error = got_repo_map_path(&in_repo_path, repo, path);
5071 if (commit_id_str == NULL) {
5072 struct got_reference *head_ref;
5074 refname = got_worktree_get_head_ref_name(worktree);
5076 refname = GOT_REF_HEAD;
5077 error = got_ref_open(&head_ref, repo, refname, 0);
5080 error = got_ref_resolve(&commit_id, repo, head_ref);
5081 got_ref_close(head_ref);
5085 struct got_reflist_head refs;
5087 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5091 error = got_repo_match_object_id(&commit_id, NULL,
5092 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5093 got_ref_list_free(&refs);
5098 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5099 in_repo_path, repo);
5106 got_worktree_close(worktree);
5108 const struct got_error *repo_error;
5109 repo_error = got_repo_close(repo);
5119 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5124 static const struct got_error *
5125 print_status(void *arg, unsigned char status, unsigned char staged_status,
5126 const char *path, struct got_object_id *blob_id,
5127 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5128 int dirfd, const char *de_name)
5130 if (status == staged_status && (status == GOT_STATUS_DELETE))
5131 status = GOT_STATUS_NO_CHANGE;
5133 char *status_codes = arg;
5134 size_t ncodes = strlen(status_codes);
5136 for (i = 0; i < ncodes ; i++) {
5137 if (status == status_codes[i] ||
5138 staged_status == status_codes[i])
5144 printf("%c%c %s\n", status, staged_status, path);
5148 static const struct got_error *
5149 cmd_status(int argc, char *argv[])
5151 const struct got_error *error = NULL;
5152 struct got_repository *repo = NULL;
5153 struct got_worktree *worktree = NULL;
5154 char *cwd = NULL, *status_codes = NULL;;
5155 struct got_pathlist_head paths;
5156 struct got_pathlist_entry *pe;
5161 while ((ch = getopt(argc, argv, "s:")) != -1) {
5164 for (i = 0; i < strlen(optarg); i++) {
5165 switch (optarg[i]) {
5166 case GOT_STATUS_MODIFY:
5167 case GOT_STATUS_ADD:
5168 case GOT_STATUS_DELETE:
5169 case GOT_STATUS_CONFLICT:
5170 case GOT_STATUS_MISSING:
5171 case GOT_STATUS_OBSTRUCTED:
5172 case GOT_STATUS_UNVERSIONED:
5173 case GOT_STATUS_MODE_CHANGE:
5174 case GOT_STATUS_NONEXISTENT:
5177 errx(1, "invalid status code '%c'",
5181 status_codes = optarg;
5193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5197 cwd = getcwd(NULL, 0);
5199 error = got_error_from_errno("getcwd");
5203 error = got_worktree_open(&worktree, cwd);
5205 if (error->code == GOT_ERR_NOT_WORKTREE)
5206 error = wrap_not_worktree_error(error, "status", cwd);
5210 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5215 error = apply_unveil(got_repo_get_path(repo), 1,
5216 got_worktree_get_root_path(worktree));
5220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5224 error = got_worktree_status(worktree, &paths, repo, print_status,
5225 status_codes, check_cancelled, NULL);
5227 TAILQ_FOREACH(pe, &paths, entry)
5228 free((char *)pe->path);
5229 got_pathlist_free(&paths);
5238 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5244 static const struct got_error *
5245 list_refs(struct got_repository *repo, const char *refname)
5247 static const struct got_error *err = NULL;
5248 struct got_reflist_head refs;
5249 struct got_reflist_entry *re;
5252 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5256 TAILQ_FOREACH(re, &refs, entry) {
5258 refstr = got_ref_to_str(re->ref);
5260 return got_error_from_errno("got_ref_to_str");
5261 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5265 got_ref_list_free(&refs);
5269 static const struct got_error *
5270 delete_ref(struct got_repository *repo, const char *refname)
5272 const struct got_error *err = NULL;
5273 struct got_reference *ref;
5275 err = got_ref_open(&ref, repo, refname, 0);
5279 err = got_ref_delete(ref, repo);
5284 static const struct got_error *
5285 add_ref(struct got_repository *repo, const char *refname, const char *target)
5287 const struct got_error *err = NULL;
5288 struct got_object_id *id;
5289 struct got_reference *ref = NULL;
5292 * Don't let the user create a reference name with a leading '-'.
5293 * While technically a valid reference name, this case is usually
5294 * an unintended typo.
5296 if (refname[0] == '-')
5297 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5299 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5302 struct got_reference *target_ref;
5304 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5306 err = got_ref_open(&target_ref, repo, target, 0);
5309 err = got_ref_resolve(&id, repo, target_ref);
5310 got_ref_close(target_ref);
5315 err = got_ref_alloc(&ref, refname, id);
5319 err = got_ref_write(ref, repo);
5327 static const struct got_error *
5328 add_symref(struct got_repository *repo, const char *refname, const char *target)
5330 const struct got_error *err = NULL;
5331 struct got_reference *ref = NULL;
5332 struct got_reference *target_ref = NULL;
5335 * Don't let the user create a reference name with a leading '-'.
5336 * While technically a valid reference name, this case is usually
5337 * an unintended typo.
5339 if (refname[0] == '-')
5340 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5342 err = got_ref_open(&target_ref, repo, target, 0);
5346 err = got_ref_alloc_symref(&ref, refname, target_ref);
5350 err = got_ref_write(ref, repo);
5353 got_ref_close(target_ref);
5359 static const struct got_error *
5360 cmd_ref(int argc, char *argv[])
5362 const struct got_error *error = NULL;
5363 struct got_repository *repo = NULL;
5364 struct got_worktree *worktree = NULL;
5365 char *cwd = NULL, *repo_path = NULL;
5366 int ch, do_list = 0, do_delete = 0;
5367 const char *obj_arg = NULL, *symref_target= NULL;
5368 char *refname = NULL;
5370 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5379 repo_path = realpath(optarg, NULL);
5380 if (repo_path == NULL)
5381 return got_error_from_errno2("realpath",
5383 got_path_strip_trailing_slashes(repo_path);
5389 symref_target = optarg;
5397 if (obj_arg && do_list)
5398 option_conflict('c', 'l');
5399 if (obj_arg && do_delete)
5400 option_conflict('c', 'd');
5401 if (obj_arg && symref_target)
5402 option_conflict('c', 's');
5403 if (symref_target && do_delete)
5404 option_conflict('s', 'd');
5405 if (symref_target && do_list)
5406 option_conflict('s', 'l');
5407 if (do_delete && do_list)
5408 option_conflict('d', 'l');
5414 if (argc != 0 && argc != 1)
5417 refname = strdup(argv[0]);
5418 if (refname == NULL) {
5419 error = got_error_from_errno("strdup");
5426 refname = strdup(argv[0]);
5427 if (refname == NULL) {
5428 error = got_error_from_errno("strdup");
5434 got_path_strip_trailing_slashes(refname);
5438 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5442 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5443 "sendfd unveil", NULL) == -1)
5447 cwd = getcwd(NULL, 0);
5449 error = got_error_from_errno("getcwd");
5453 if (repo_path == NULL) {
5454 error = got_worktree_open(&worktree, cwd);
5455 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5461 strdup(got_worktree_get_repo_path(worktree));
5462 if (repo_path == NULL)
5463 error = got_error_from_errno("strdup");
5467 repo_path = strdup(cwd);
5468 if (repo_path == NULL) {
5469 error = got_error_from_errno("strdup");
5475 error = got_repo_open(&repo, repo_path, NULL);
5479 error = apply_unveil(got_repo_get_path(repo), do_list,
5480 worktree ? got_worktree_get_root_path(worktree) : NULL);
5485 error = list_refs(repo, refname);
5487 error = delete_ref(repo, refname);
5488 else if (symref_target)
5489 error = add_symref(repo, refname, symref_target);
5491 if (obj_arg == NULL)
5493 error = add_ref(repo, refname, obj_arg);
5498 got_repo_close(repo);
5500 got_worktree_close(worktree);
5510 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5511 "[name]\n", getprogname());
5515 static const struct got_error *
5516 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5517 struct got_reference *ref)
5519 const struct got_error *err = NULL;
5520 const char *refname, *marker = " ";
5523 refname = got_ref_get_name(ref);
5524 if (worktree && strcmp(refname,
5525 got_worktree_get_head_ref_name(worktree)) == 0) {
5526 struct got_object_id *id = NULL;
5528 err = got_ref_resolve(&id, repo, ref);
5531 if (got_object_id_cmp(id,
5532 got_worktree_get_base_commit_id(worktree)) == 0)
5539 if (strncmp(refname, "refs/heads/", 11) == 0)
5541 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5544 refstr = got_ref_to_str(ref);
5546 return got_error_from_errno("got_ref_to_str");
5548 printf("%s%s: %s\n", marker, refname, refstr);
5553 static const struct got_error *
5554 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5556 const char *refname;
5558 if (worktree == NULL)
5559 return got_error(GOT_ERR_NOT_WORKTREE);
5561 refname = got_worktree_get_head_ref_name(worktree);
5563 if (strncmp(refname, "refs/heads/", 11) == 0)
5565 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5568 printf("%s\n", refname);
5573 static const struct got_error *
5574 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5576 static const struct got_error *err = NULL;
5577 struct got_reflist_head refs;
5578 struct got_reflist_entry *re;
5579 struct got_reference *temp_ref = NULL;
5580 int rebase_in_progress, histedit_in_progress;
5585 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5590 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5595 if (rebase_in_progress || histedit_in_progress) {
5596 err = got_ref_open(&temp_ref, repo,
5597 got_worktree_get_head_ref_name(worktree), 0);
5600 list_branch(repo, worktree, temp_ref);
5601 got_ref_close(temp_ref);
5605 err = got_ref_list(&refs, repo, "refs/heads",
5606 got_ref_cmp_by_name, NULL);
5610 TAILQ_FOREACH(re, &refs, entry)
5611 list_branch(repo, worktree, re->ref);
5613 got_ref_list_free(&refs);
5617 static const struct got_error *
5618 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5619 const char *branch_name)
5621 const struct got_error *err = NULL;
5622 struct got_reference *ref = NULL;
5625 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5626 return got_error_from_errno("asprintf");
5628 err = got_ref_open(&ref, repo, refname, 0);
5633 strcmp(got_worktree_get_head_ref_name(worktree),
5634 got_ref_get_name(ref)) == 0) {
5635 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5636 "will not delete this work tree's current branch");
5640 err = got_ref_delete(ref, repo);
5648 static const struct got_error *
5649 add_branch(struct got_repository *repo, const char *branch_name,
5650 struct got_object_id *base_commit_id)
5652 const struct got_error *err = NULL;
5653 struct got_reference *ref = NULL;
5654 char *base_refname = NULL, *refname = NULL;
5657 * Don't let the user create a branch name with a leading '-'.
5658 * While technically a valid reference name, this case is usually
5659 * an unintended typo.
5661 if (branch_name[0] == '-')
5662 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5664 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5665 err = got_error_from_errno("asprintf");
5669 err = got_ref_open(&ref, repo, refname, 0);
5671 err = got_error(GOT_ERR_BRANCH_EXISTS);
5673 } else if (err->code != GOT_ERR_NOT_REF)
5676 err = got_ref_alloc(&ref, refname, base_commit_id);
5680 err = got_ref_write(ref, repo);
5689 static const struct got_error *
5690 cmd_branch(int argc, char *argv[])
5692 const struct got_error *error = NULL;
5693 struct got_repository *repo = NULL;
5694 struct got_worktree *worktree = NULL;
5695 char *cwd = NULL, *repo_path = NULL;
5696 int ch, do_list = 0, do_show = 0, do_update = 1;
5697 const char *delref = NULL, *commit_id_arg = NULL;
5698 struct got_reference *ref = NULL;
5699 struct got_pathlist_head paths;
5700 struct got_pathlist_entry *pe;
5701 struct got_object_id *commit_id = NULL;
5702 char *commit_id_str = NULL;
5706 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5709 commit_id_arg = optarg;
5715 repo_path = realpath(optarg, NULL);
5716 if (repo_path == NULL)
5717 return got_error_from_errno2("realpath",
5719 got_path_strip_trailing_slashes(repo_path);
5733 if (do_list && delref)
5734 option_conflict('l', 'd');
5739 if (!do_list && !delref && argc == 0)
5742 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5743 errx(1, "-c option can only be used when creating a branch");
5745 if (do_list || delref) {
5748 } else if (!do_show && argc != 1)
5752 if (do_list || do_show) {
5753 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5757 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5758 "sendfd unveil", NULL) == -1)
5762 cwd = getcwd(NULL, 0);
5764 error = got_error_from_errno("getcwd");
5768 if (repo_path == NULL) {
5769 error = got_worktree_open(&worktree, cwd);
5770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5776 strdup(got_worktree_get_repo_path(worktree));
5777 if (repo_path == NULL)
5778 error = got_error_from_errno("strdup");
5782 repo_path = strdup(cwd);
5783 if (repo_path == NULL) {
5784 error = got_error_from_errno("strdup");
5790 error = got_repo_open(&repo, repo_path, NULL);
5794 error = apply_unveil(got_repo_get_path(repo), do_list,
5795 worktree ? got_worktree_get_root_path(worktree) : NULL);
5800 error = show_current_branch(repo, worktree);
5802 error = list_branches(repo, worktree);
5804 error = delete_branch(repo, worktree, delref);
5806 struct got_reflist_head refs;
5808 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5812 if (commit_id_arg == NULL)
5813 commit_id_arg = worktree ?
5814 got_worktree_get_head_ref_name(worktree) :
5816 error = got_repo_match_object_id(&commit_id, NULL,
5817 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5818 got_ref_list_free(&refs);
5821 error = add_branch(repo, argv[0], commit_id);
5824 if (worktree && do_update) {
5825 struct got_update_progress_arg upa;
5826 char *branch_refname = NULL;
5828 error = got_object_id_str(&commit_id_str, commit_id);
5831 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5835 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5837 error = got_error_from_errno("asprintf");
5840 error = got_ref_open(&ref, repo, branch_refname, 0);
5841 free(branch_refname);
5844 error = switch_head_ref(ref, commit_id, worktree,
5848 error = got_worktree_set_base_commit_id(worktree, repo,
5852 memset(&upa, 0, sizeof(upa));
5853 error = got_worktree_checkout_files(worktree, &paths,
5854 repo, update_progress, &upa, check_cancelled,
5858 if (upa.did_something)
5859 printf("Updated to commit %s\n", commit_id_str);
5860 print_update_progress_stats(&upa);
5867 got_repo_close(repo);
5869 got_worktree_close(worktree);
5873 free(commit_id_str);
5874 TAILQ_FOREACH(pe, &paths, entry)
5875 free((char *)pe->path);
5876 got_pathlist_free(&paths);
5885 "usage: %s tag [-c commit] [-r repository] [-l] "
5886 "[-m message] name\n", getprogname());
5891 static const struct got_error *
5892 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5894 const struct got_error *err = NULL;
5895 struct got_reflist_entry *re, *se, *new;
5896 struct got_object_id *re_id, *se_id;
5897 struct got_tag_object *re_tag, *se_tag;
5898 time_t re_time, se_time;
5900 SIMPLEQ_FOREACH(re, tags, entry) {
5901 se = SIMPLEQ_FIRST(sorted);
5903 err = got_reflist_entry_dup(&new, re);
5906 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5909 err = got_ref_resolve(&re_id, repo, re->ref);
5912 err = got_object_open_as_tag(&re_tag, repo, re_id);
5916 re_time = got_object_tag_get_tagger_time(re_tag);
5917 got_object_tag_close(re_tag);
5921 err = got_ref_resolve(&se_id, repo, re->ref);
5924 err = got_object_open_as_tag(&se_tag, repo, se_id);
5928 se_time = got_object_tag_get_tagger_time(se_tag);
5929 got_object_tag_close(se_tag);
5931 if (se_time > re_time) {
5932 err = got_reflist_entry_dup(&new, re);
5935 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5938 se = SIMPLEQ_NEXT(se, entry);
5947 static const struct got_error *
5948 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5950 static const struct got_error *err = NULL;
5951 struct got_reflist_head refs;
5952 struct got_reflist_entry *re;
5956 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5960 TAILQ_FOREACH(re, &refs, entry) {
5961 const char *refname;
5962 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5966 struct got_object_id *id;
5967 struct got_tag_object *tag;
5968 struct got_commit_object *commit = NULL;
5970 refname = got_ref_get_name(re->ref);
5971 if (strncmp(refname, "refs/tags/", 10) != 0)
5974 refstr = got_ref_to_str(re->ref);
5975 if (refstr == NULL) {
5976 err = got_error_from_errno("got_ref_to_str");
5979 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5982 err = got_ref_resolve(&id, repo, re->ref);
5985 err = got_object_open_as_tag(&tag, repo, id);
5987 if (err->code != GOT_ERR_OBJ_TYPE) {
5991 /* "lightweight" tag */
5992 err = got_object_open_as_commit(&commit, repo, id);
5997 tagger = got_object_commit_get_committer(commit);
5999 got_object_commit_get_committer_time(commit);
6000 err = got_object_id_str(&id_str, id);
6006 tagger = got_object_tag_get_tagger(tag);
6007 tagger_time = got_object_tag_get_tagger_time(tag);
6008 err = got_object_id_str(&id_str,
6009 got_object_tag_get_object_id(tag));
6013 printf("from: %s\n", tagger);
6014 datestr = get_datestr(&tagger_time, datebuf);
6016 printf("date: %s UTC\n", datestr);
6018 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6020 switch (got_object_tag_get_object_type(tag)) {
6021 case GOT_OBJ_TYPE_BLOB:
6022 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6025 case GOT_OBJ_TYPE_TREE:
6026 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6029 case GOT_OBJ_TYPE_COMMIT:
6030 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6033 case GOT_OBJ_TYPE_TAG:
6034 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6043 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6046 got_object_commit_close(commit);
6048 tagmsg0 = strdup(got_object_tag_get_message(tag));
6049 got_object_tag_close(tag);
6050 if (tagmsg0 == NULL) {
6051 err = got_error_from_errno("strdup");
6058 line = strsep(&tagmsg, "\n");
6060 printf(" %s\n", line);
6065 got_ref_list_free(&refs);
6069 static const struct got_error *
6070 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6071 const char *tag_name, const char *repo_path)
6073 const struct got_error *err = NULL;
6074 char *template = NULL, *initial_content = NULL;
6075 char *editor = NULL;
6076 int initial_content_len;
6079 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6080 err = got_error_from_errno("asprintf");
6084 initial_content_len = asprintf(&initial_content,
6085 "\n# tagging commit %s as %s\n",
6086 commit_id_str, tag_name);
6087 if (initial_content_len == -1) {
6088 err = got_error_from_errno("asprintf");
6092 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6096 if (write(fd, initial_content, initial_content_len) == -1) {
6097 err = got_error_from_errno2("write", *tagmsg_path);
6101 err = get_editor(&editor);
6104 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6105 initial_content_len, 1);
6107 free(initial_content);
6111 if (fd != -1 && close(fd) == -1 && err == NULL)
6112 err = got_error_from_errno2("close", *tagmsg_path);
6114 /* Editor is done; we can now apply unveil(2) */
6116 err = apply_unveil(repo_path, 0, NULL);
6124 static const struct got_error *
6125 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6126 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6128 const struct got_error *err = NULL;
6129 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6130 char *label = NULL, *commit_id_str = NULL;
6131 struct got_reference *ref = NULL;
6132 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6133 char *tagmsg_path = NULL, *tag_id_str = NULL;
6134 int preserve_tagmsg = 0;
6135 struct got_reflist_head refs;
6140 * Don't let the user create a tag name with a leading '-'.
6141 * While technically a valid reference name, this case is usually
6142 * an unintended typo.
6144 if (tag_name[0] == '-')
6145 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6147 err = get_author(&tagger, repo, worktree);
6151 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6155 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6156 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6160 err = got_object_id_str(&commit_id_str, commit_id);
6164 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6165 refname = strdup(tag_name);
6166 if (refname == NULL) {
6167 err = got_error_from_errno("strdup");
6171 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6172 err = got_error_from_errno("asprintf");
6176 err = got_ref_open(&ref, repo, refname, 0);
6178 err = got_error(GOT_ERR_TAG_EXISTS);
6180 } else if (err->code != GOT_ERR_NOT_REF)
6183 if (tagmsg_arg == NULL) {
6184 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6185 tag_name, got_repo_get_path(repo));
6187 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6188 tagmsg_path != NULL)
6189 preserve_tagmsg = 1;
6194 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6195 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6198 preserve_tagmsg = 1;
6202 err = got_ref_alloc(&ref, refname, tag_id);
6205 preserve_tagmsg = 1;
6209 err = got_ref_write(ref, repo);
6212 preserve_tagmsg = 1;
6216 err = got_object_id_str(&tag_id_str, tag_id);
6219 preserve_tagmsg = 1;
6222 printf("Created tag %s\n", tag_id_str);
6224 if (preserve_tagmsg) {
6225 fprintf(stderr, "%s: tag message preserved in %s\n",
6226 getprogname(), tagmsg_path);
6227 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6228 err = got_error_from_errno2("unlink", tagmsg_path);
6233 free(commit_id_str);
6238 got_ref_list_free(&refs);
6242 static const struct got_error *
6243 cmd_tag(int argc, char *argv[])
6245 const struct got_error *error = NULL;
6246 struct got_repository *repo = NULL;
6247 struct got_worktree *worktree = NULL;
6248 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6249 char *gitconfig_path = NULL;
6250 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6251 int ch, do_list = 0;
6253 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6256 commit_id_arg = optarg;
6262 repo_path = realpath(optarg, NULL);
6263 if (repo_path == NULL)
6264 return got_error_from_errno2("realpath",
6266 got_path_strip_trailing_slashes(repo_path);
6281 if (commit_id_arg != NULL)
6283 "-c option can only be used when creating a tag");
6285 option_conflict('l', 'm');
6288 } else if (argc != 1)
6295 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6299 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6300 "sendfd unveil", NULL) == -1)
6304 cwd = getcwd(NULL, 0);
6306 error = got_error_from_errno("getcwd");
6310 if (repo_path == NULL) {
6311 error = got_worktree_open(&worktree, cwd);
6312 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6318 strdup(got_worktree_get_repo_path(worktree));
6319 if (repo_path == NULL)
6320 error = got_error_from_errno("strdup");
6324 repo_path = strdup(cwd);
6325 if (repo_path == NULL) {
6326 error = got_error_from_errno("strdup");
6333 error = got_repo_open(&repo, repo_path, NULL);
6336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6339 error = list_tags(repo, worktree);
6341 error = get_gitconfig_path(&gitconfig_path);
6344 error = got_repo_open(&repo, repo_path, gitconfig_path);
6349 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6354 if (commit_id_arg == NULL) {
6355 struct got_reference *head_ref;
6356 struct got_object_id *commit_id;
6357 error = got_ref_open(&head_ref, repo,
6358 worktree ? got_worktree_get_head_ref_name(worktree)
6362 error = got_ref_resolve(&commit_id, repo, head_ref);
6363 got_ref_close(head_ref);
6366 error = got_object_id_str(&commit_id_str, commit_id);
6372 error = add_tag(repo, worktree, tag_name,
6373 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6377 got_repo_close(repo);
6379 got_worktree_close(worktree);
6382 free(gitconfig_path);
6383 free(commit_id_str);
6390 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6395 static const struct got_error *
6396 add_progress(void *arg, unsigned char status, const char *path)
6398 while (path[0] == '/')
6400 printf("%c %s\n", status, path);
6404 static const struct got_error *
6405 cmd_add(int argc, char *argv[])
6407 const struct got_error *error = NULL;
6408 struct got_repository *repo = NULL;
6409 struct got_worktree *worktree = NULL;
6411 struct got_pathlist_head paths;
6412 struct got_pathlist_entry *pe;
6413 int ch, can_recurse = 0, no_ignores = 0;
6417 while ((ch = getopt(argc, argv, "IR")) != -1) {
6435 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6442 cwd = getcwd(NULL, 0);
6444 error = got_error_from_errno("getcwd");
6448 error = got_worktree_open(&worktree, cwd);
6450 if (error->code == GOT_ERR_NOT_WORKTREE)
6451 error = wrap_not_worktree_error(error, "add", cwd);
6455 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6460 error = apply_unveil(got_repo_get_path(repo), 1,
6461 got_worktree_get_root_path(worktree));
6465 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6469 if (!can_recurse && no_ignores) {
6470 error = got_error_msg(GOT_ERR_BAD_PATH,
6471 "disregarding ignores requires -R option");
6479 TAILQ_FOREACH(pe, &paths, entry) {
6480 if (asprintf(&ondisk_path, "%s/%s",
6481 got_worktree_get_root_path(worktree),
6483 error = got_error_from_errno("asprintf");
6486 if (lstat(ondisk_path, &sb) == -1) {
6487 if (errno == ENOENT) {
6491 error = got_error_from_errno2("lstat",
6497 if (S_ISDIR(sb.st_mode)) {
6498 error = got_error_msg(GOT_ERR_BAD_PATH,
6499 "adding directories requires -R option");
6505 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6506 NULL, repo, no_ignores);
6509 got_repo_close(repo);
6511 got_worktree_close(worktree);
6512 TAILQ_FOREACH(pe, &paths, entry)
6513 free((char *)pe->path);
6514 got_pathlist_free(&paths);
6522 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6523 "path ...\n", getprogname());
6527 static const struct got_error *
6528 print_remove_status(void *arg, unsigned char status,
6529 unsigned char staged_status, const char *path)
6531 while (path[0] == '/')
6533 if (status == GOT_STATUS_NONEXISTENT)
6535 if (status == staged_status && (status == GOT_STATUS_DELETE))
6536 status = GOT_STATUS_NO_CHANGE;
6537 printf("%c%c %s\n", status, staged_status, path);
6541 static const struct got_error *
6542 cmd_remove(int argc, char *argv[])
6544 const struct got_error *error = NULL;
6545 struct got_worktree *worktree = NULL;
6546 struct got_repository *repo = NULL;
6547 const char *status_codes = NULL;
6549 struct got_pathlist_head paths;
6550 struct got_pathlist_entry *pe;
6551 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6555 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6558 delete_local_mods = 1;
6567 for (i = 0; i < strlen(optarg); i++) {
6568 switch (optarg[i]) {
6569 case GOT_STATUS_MODIFY:
6570 delete_local_mods = 1;
6572 case GOT_STATUS_MISSING:
6575 errx(1, "invalid status code '%c'",
6579 status_codes = optarg;
6591 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6598 cwd = getcwd(NULL, 0);
6600 error = got_error_from_errno("getcwd");
6603 error = got_worktree_open(&worktree, cwd);
6605 if (error->code == GOT_ERR_NOT_WORKTREE)
6606 error = wrap_not_worktree_error(error, "remove", cwd);
6610 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6615 error = apply_unveil(got_repo_get_path(repo), 1,
6616 got_worktree_get_root_path(worktree));
6620 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6627 TAILQ_FOREACH(pe, &paths, entry) {
6628 if (asprintf(&ondisk_path, "%s/%s",
6629 got_worktree_get_root_path(worktree),
6631 error = got_error_from_errno("asprintf");
6634 if (lstat(ondisk_path, &sb) == -1) {
6635 if (errno == ENOENT) {
6639 error = got_error_from_errno2("lstat",
6645 if (S_ISDIR(sb.st_mode)) {
6646 error = got_error_msg(GOT_ERR_BAD_PATH,
6647 "removing directories requires -R option");
6653 error = got_worktree_schedule_delete(worktree, &paths,
6654 delete_local_mods, status_codes, print_remove_status, NULL,
6655 repo, keep_on_disk);
6658 got_repo_close(repo);
6660 got_worktree_close(worktree);
6661 TAILQ_FOREACH(pe, &paths, entry)
6662 free((char *)pe->path);
6663 got_pathlist_free(&paths);
6671 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6672 "path ...\n", getprogname());
6676 static const struct got_error *
6677 revert_progress(void *arg, unsigned char status, const char *path)
6679 if (status == GOT_STATUS_UNVERSIONED)
6682 while (path[0] == '/')
6684 printf("%c %s\n", status, path);
6688 struct choose_patch_arg {
6689 FILE *patch_script_file;
6693 static const struct got_error *
6694 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6695 int nchanges, const char *action)
6698 size_t linesize = 0;
6702 case GOT_STATUS_ADD:
6703 printf("A %s\n%s this addition? [y/n] ", path, action);
6705 case GOT_STATUS_DELETE:
6706 printf("D %s\n%s this deletion? [y/n] ", path, action);
6708 case GOT_STATUS_MODIFY:
6709 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6710 return got_error_from_errno("fseek");
6711 printf(GOT_COMMIT_SEP_STR);
6712 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6714 if (ferror(patch_file))
6715 return got_error_from_errno("getline");
6716 printf(GOT_COMMIT_SEP_STR);
6717 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6718 path, n, nchanges, action);
6721 return got_error_path(path, GOT_ERR_FILE_STATUS);
6727 static const struct got_error *
6728 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6729 FILE *patch_file, int n, int nchanges)
6731 const struct got_error *err = NULL;
6733 size_t linesize = 0;
6736 struct choose_patch_arg *a = arg;
6738 *choice = GOT_PATCH_CHOICE_NONE;
6740 if (a->patch_script_file) {
6742 err = show_change(status, path, patch_file, n, nchanges,
6746 linelen = getline(&line, &linesize, a->patch_script_file);
6747 if (linelen == -1) {
6748 if (ferror(a->patch_script_file))
6749 return got_error_from_errno("getline");
6752 nl = strchr(line, '\n');
6755 if (strcmp(line, "y") == 0) {
6756 *choice = GOT_PATCH_CHOICE_YES;
6758 } else if (strcmp(line, "n") == 0) {
6759 *choice = GOT_PATCH_CHOICE_NO;
6761 } else if (strcmp(line, "q") == 0 &&
6762 status == GOT_STATUS_MODIFY) {
6763 *choice = GOT_PATCH_CHOICE_QUIT;
6766 printf("invalid response '%s'\n", line);
6771 while (resp != 'y' && resp != 'n' && resp != 'q') {
6772 err = show_change(status, path, patch_file, n, nchanges,
6779 if (status == GOT_STATUS_MODIFY) {
6780 if (resp != 'y' && resp != 'n' && resp != 'q') {
6781 printf("invalid response '%c'\n", resp);
6784 } else if (resp != 'y' && resp != 'n') {
6785 printf("invalid response '%c'\n", resp);
6791 *choice = GOT_PATCH_CHOICE_YES;
6792 else if (resp == 'n')
6793 *choice = GOT_PATCH_CHOICE_NO;
6794 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6795 *choice = GOT_PATCH_CHOICE_QUIT;
6801 static const struct got_error *
6802 cmd_revert(int argc, char *argv[])
6804 const struct got_error *error = NULL;
6805 struct got_worktree *worktree = NULL;
6806 struct got_repository *repo = NULL;
6807 char *cwd = NULL, *path = NULL;
6808 struct got_pathlist_head paths;
6809 struct got_pathlist_entry *pe;
6810 int ch, can_recurse = 0, pflag = 0;
6811 FILE *patch_script_file = NULL;
6812 const char *patch_script_path = NULL;
6813 struct choose_patch_arg cpa;
6817 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6823 patch_script_path = optarg;
6838 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6839 "unveil", NULL) == -1)
6844 if (patch_script_path && !pflag)
6845 errx(1, "-F option can only be used together with -p option");
6847 cwd = getcwd(NULL, 0);
6849 error = got_error_from_errno("getcwd");
6852 error = got_worktree_open(&worktree, cwd);
6854 if (error->code == GOT_ERR_NOT_WORKTREE)
6855 error = wrap_not_worktree_error(error, "revert", cwd);
6859 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6864 if (patch_script_path) {
6865 patch_script_file = fopen(patch_script_path, "r");
6866 if (patch_script_file == NULL) {
6867 error = got_error_from_errno2("fopen",
6872 error = apply_unveil(got_repo_get_path(repo), 1,
6873 got_worktree_get_root_path(worktree));
6877 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6884 TAILQ_FOREACH(pe, &paths, entry) {
6885 if (asprintf(&ondisk_path, "%s/%s",
6886 got_worktree_get_root_path(worktree),
6888 error = got_error_from_errno("asprintf");
6891 if (lstat(ondisk_path, &sb) == -1) {
6892 if (errno == ENOENT) {
6896 error = got_error_from_errno2("lstat",
6902 if (S_ISDIR(sb.st_mode)) {
6903 error = got_error_msg(GOT_ERR_BAD_PATH,
6904 "reverting directories requires -R option");
6910 cpa.patch_script_file = patch_script_file;
6911 cpa.action = "revert";
6912 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6913 pflag ? choose_patch : NULL, &cpa, repo);
6915 if (patch_script_file && fclose(patch_script_file) == EOF &&
6917 error = got_error_from_errno2("fclose", patch_script_path);
6919 got_repo_close(repo);
6921 got_worktree_close(worktree);
6930 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6935 struct collect_commit_logmsg_arg {
6936 const char *cmdline_log;
6938 const char *worktree_path;
6939 const char *branch_name;
6940 const char *repo_path;
6945 static const struct got_error *
6946 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6949 char *initial_content = NULL;
6950 struct got_pathlist_entry *pe;
6951 const struct got_error *err = NULL;
6952 char *template = NULL;
6953 struct collect_commit_logmsg_arg *a = arg;
6954 int initial_content_len;
6958 /* if a message was specified on the command line, just use it */
6959 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6960 len = strlen(a->cmdline_log) + 1;
6961 *logmsg = malloc(len + 1);
6962 if (*logmsg == NULL)
6963 return got_error_from_errno("malloc");
6964 strlcpy(*logmsg, a->cmdline_log, len);
6968 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6969 return got_error_from_errno("asprintf");
6971 initial_content_len = asprintf(&initial_content,
6972 "\n# changes to be committed on branch %s:\n",
6974 if (initial_content_len == -1)
6975 return got_error_from_errno("asprintf");
6977 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6981 if (write(fd, initial_content, initial_content_len) == -1) {
6982 err = got_error_from_errno2("write", a->logmsg_path);
6986 TAILQ_FOREACH(pe, commitable_paths, entry) {
6987 struct got_commitable *ct = pe->data;
6988 dprintf(fd, "# %c %s\n",
6989 got_commitable_get_status(ct),
6990 got_commitable_get_path(ct));
6993 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6994 initial_content_len, 1);
6996 free(initial_content);
6999 if (fd != -1 && close(fd) == -1 && err == NULL)
7000 err = got_error_from_errno2("close", a->logmsg_path);
7002 /* Editor is done; we can now apply unveil(2) */
7004 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7012 static const struct got_error *
7013 cmd_commit(int argc, char *argv[])
7015 const struct got_error *error = NULL;
7016 struct got_worktree *worktree = NULL;
7017 struct got_repository *repo = NULL;
7018 char *cwd = NULL, *id_str = NULL;
7019 struct got_object_id *id = NULL;
7020 const char *logmsg = NULL;
7021 struct collect_commit_logmsg_arg cl_arg;
7022 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7023 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7024 int allow_bad_symlinks = 0;
7025 struct got_pathlist_head paths;
7028 cl_arg.logmsg_path = NULL;
7030 while ((ch = getopt(argc, argv, "m:S")) != -1) {
7036 allow_bad_symlinks = 1;
7048 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7049 "unveil", NULL) == -1)
7052 cwd = getcwd(NULL, 0);
7054 error = got_error_from_errno("getcwd");
7057 error = got_worktree_open(&worktree, cwd);
7059 if (error->code == GOT_ERR_NOT_WORKTREE)
7060 error = wrap_not_worktree_error(error, "commit", cwd);
7064 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7067 if (rebase_in_progress) {
7068 error = got_error(GOT_ERR_REBASING);
7072 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7077 error = get_gitconfig_path(&gitconfig_path);
7080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7085 error = get_author(&author, repo, worktree);
7090 * unveil(2) traverses exec(2); if an editor is used we have
7091 * to apply unveil after the log message has been written.
7093 if (logmsg == NULL || strlen(logmsg) == 0)
7094 error = get_editor(&editor);
7096 error = apply_unveil(got_repo_get_path(repo), 0,
7097 got_worktree_get_root_path(worktree));
7101 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7105 cl_arg.editor = editor;
7106 cl_arg.cmdline_log = logmsg;
7107 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7108 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7109 if (!histedit_in_progress) {
7110 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7111 error = got_error(GOT_ERR_COMMIT_BRANCH);
7114 cl_arg.branch_name += 11;
7116 cl_arg.repo_path = got_repo_get_path(repo);
7117 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7118 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7119 print_status, NULL, repo);
7121 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7122 cl_arg.logmsg_path != NULL)
7123 preserve_logmsg = 1;
7127 error = got_object_id_str(&id_str, id);
7130 printf("Created commit %s\n", id_str);
7132 if (preserve_logmsg) {
7133 fprintf(stderr, "%s: log message preserved in %s\n",
7134 getprogname(), cl_arg.logmsg_path);
7135 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7137 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7138 free(cl_arg.logmsg_path);
7140 got_repo_close(repo);
7142 got_worktree_close(worktree);
7145 free(gitconfig_path);
7152 usage_cherrypick(void)
7154 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7158 static const struct got_error *
7159 cmd_cherrypick(int argc, char *argv[])
7161 const struct got_error *error = NULL;
7162 struct got_worktree *worktree = NULL;
7163 struct got_repository *repo = NULL;
7164 char *cwd = NULL, *commit_id_str = NULL;
7165 struct got_object_id *commit_id = NULL;
7166 struct got_commit_object *commit = NULL;
7167 struct got_object_qid *pid;
7168 struct got_reference *head_ref = NULL;
7170 struct got_update_progress_arg upa;
7172 while ((ch = getopt(argc, argv, "")) != -1) {
7184 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7185 "unveil", NULL) == -1)
7191 cwd = getcwd(NULL, 0);
7193 error = got_error_from_errno("getcwd");
7196 error = got_worktree_open(&worktree, cwd);
7198 if (error->code == GOT_ERR_NOT_WORKTREE)
7199 error = wrap_not_worktree_error(error, "cherrypick",
7204 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7209 error = apply_unveil(got_repo_get_path(repo), 0,
7210 got_worktree_get_root_path(worktree));
7214 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7215 GOT_OBJ_TYPE_COMMIT, repo);
7216 if (error != NULL) {
7217 struct got_reference *ref;
7218 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7220 error = got_ref_open(&ref, repo, argv[0], 0);
7223 error = got_ref_resolve(&commit_id, repo, ref);
7228 error = got_object_id_str(&commit_id_str, commit_id);
7232 error = got_ref_open(&head_ref, repo,
7233 got_worktree_get_head_ref_name(worktree), 0);
7237 error = check_same_branch(commit_id, head_ref, NULL, repo);
7239 if (error->code != GOT_ERR_ANCESTRY)
7243 error = got_error(GOT_ERR_SAME_BRANCH);
7247 error = got_object_open_as_commit(&commit, repo, commit_id);
7250 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7251 memset(&upa, 0, sizeof(upa));
7252 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7253 commit_id, repo, update_progress, &upa, check_cancelled,
7258 if (upa.did_something)
7259 printf("Merged commit %s\n", commit_id_str);
7260 print_update_progress_stats(&upa);
7263 got_object_commit_close(commit);
7264 free(commit_id_str);
7266 got_ref_close(head_ref);
7268 got_worktree_close(worktree);
7270 got_repo_close(repo);
7277 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7281 static const struct got_error *
7282 cmd_backout(int argc, char *argv[])
7284 const struct got_error *error = NULL;
7285 struct got_worktree *worktree = NULL;
7286 struct got_repository *repo = NULL;
7287 char *cwd = NULL, *commit_id_str = NULL;
7288 struct got_object_id *commit_id = NULL;
7289 struct got_commit_object *commit = NULL;
7290 struct got_object_qid *pid;
7291 struct got_reference *head_ref = NULL;
7293 struct got_update_progress_arg upa;
7295 while ((ch = getopt(argc, argv, "")) != -1) {
7307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7308 "unveil", NULL) == -1)
7314 cwd = getcwd(NULL, 0);
7316 error = got_error_from_errno("getcwd");
7319 error = got_worktree_open(&worktree, cwd);
7321 if (error->code == GOT_ERR_NOT_WORKTREE)
7322 error = wrap_not_worktree_error(error, "backout", cwd);
7326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7331 error = apply_unveil(got_repo_get_path(repo), 0,
7332 got_worktree_get_root_path(worktree));
7336 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7337 GOT_OBJ_TYPE_COMMIT, repo);
7338 if (error != NULL) {
7339 struct got_reference *ref;
7340 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7342 error = got_ref_open(&ref, repo, argv[0], 0);
7345 error = got_ref_resolve(&commit_id, repo, ref);
7350 error = got_object_id_str(&commit_id_str, commit_id);
7354 error = got_ref_open(&head_ref, repo,
7355 got_worktree_get_head_ref_name(worktree), 0);
7359 error = check_same_branch(commit_id, head_ref, NULL, repo);
7363 error = got_object_open_as_commit(&commit, repo, commit_id);
7366 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7368 error = got_error(GOT_ERR_ROOT_COMMIT);
7372 memset(&upa, 0, sizeof(upa));
7373 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7374 update_progress, &upa, check_cancelled, NULL);
7378 if (upa.did_something)
7379 printf("Backed out commit %s\n", commit_id_str);
7380 print_update_progress_stats(&upa);
7383 got_object_commit_close(commit);
7384 free(commit_id_str);
7386 got_ref_close(head_ref);
7388 got_worktree_close(worktree);
7390 got_repo_close(repo);
7397 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7403 trim_logmsg(char *logmsg, int limit)
7408 len = strlen(logmsg);
7412 nl = strchr(logmsg, '\n');
7417 static const struct got_error *
7418 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7420 const struct got_error *err;
7421 char *logmsg0 = NULL;
7424 err = got_object_commit_get_logmsg(&logmsg0, commit);
7429 while (isspace((unsigned char)s[0]))
7432 *logmsg = strdup(s);
7433 if (*logmsg == NULL) {
7434 err = got_error_from_errno("strdup");
7438 trim_logmsg(*logmsg, limit);
7444 static const struct got_error *
7445 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7447 const struct got_error *err;
7448 struct got_commit_object *commit = NULL;
7449 char *id_str = NULL, *logmsg = NULL;
7451 err = got_object_open_as_commit(&commit, repo, id);
7455 err = got_object_id_str(&id_str, id);
7461 err = get_short_logmsg(&logmsg, 42, commit);
7465 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7468 got_object_commit_close(commit);
7473 static const struct got_error *
7474 show_rebase_progress(struct got_commit_object *commit,
7475 struct got_object_id *old_id, struct got_object_id *new_id)
7477 const struct got_error *err;
7478 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7480 err = got_object_id_str(&old_id_str, old_id);
7485 err = got_object_id_str(&new_id_str, new_id);
7490 old_id_str[12] = '\0';
7492 new_id_str[12] = '\0';
7494 err = get_short_logmsg(&logmsg, 42, commit);
7498 printf("%s -> %s: %s\n", old_id_str,
7499 new_id_str ? new_id_str : "no-op change", logmsg);
7507 static const struct got_error *
7508 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7509 struct got_reference *branch, struct got_reference *new_base_branch,
7510 struct got_reference *tmp_branch, struct got_repository *repo)
7512 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7513 return got_worktree_rebase_complete(worktree, fileindex,
7514 new_base_branch, tmp_branch, branch, repo);
7517 static const struct got_error *
7518 rebase_commit(struct got_pathlist_head *merged_paths,
7519 struct got_worktree *worktree, struct got_fileindex *fileindex,
7520 struct got_reference *tmp_branch,
7521 struct got_object_id *commit_id, struct got_repository *repo)
7523 const struct got_error *error;
7524 struct got_commit_object *commit;
7525 struct got_object_id *new_commit_id;
7527 error = got_object_open_as_commit(&commit, repo, commit_id);
7531 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7532 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7534 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7536 error = show_rebase_progress(commit, commit_id, NULL);
7538 error = show_rebase_progress(commit, commit_id, new_commit_id);
7539 free(new_commit_id);
7542 got_object_commit_close(commit);
7546 struct check_path_prefix_arg {
7547 const char *path_prefix;
7552 static const struct got_error *
7553 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7554 struct got_blob_object *blob2, struct got_object_id *id1,
7555 struct got_object_id *id2, const char *path1, const char *path2,
7556 mode_t mode1, mode_t mode2, struct got_repository *repo)
7558 struct check_path_prefix_arg *a = arg;
7560 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7561 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7562 return got_error(a->errcode);
7567 static const struct got_error *
7568 check_path_prefix(struct got_object_id *parent_id,
7569 struct got_object_id *commit_id, const char *path_prefix,
7570 int errcode, struct got_repository *repo)
7572 const struct got_error *err;
7573 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7574 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7575 struct check_path_prefix_arg cpp_arg;
7577 if (got_path_is_root_dir(path_prefix))
7580 err = got_object_open_as_commit(&commit, repo, commit_id);
7584 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7588 err = got_object_open_as_tree(&tree1, repo,
7589 got_object_commit_get_tree_id(parent_commit));
7593 err = got_object_open_as_tree(&tree2, repo,
7594 got_object_commit_get_tree_id(commit));
7598 cpp_arg.path_prefix = path_prefix;
7599 while (cpp_arg.path_prefix[0] == '/')
7600 cpp_arg.path_prefix++;
7601 cpp_arg.len = strlen(cpp_arg.path_prefix);
7602 cpp_arg.errcode = errcode;
7603 err = got_diff_tree(tree1, tree2, "", "", repo,
7604 check_path_prefix_in_diff, &cpp_arg, 0);
7607 got_object_tree_close(tree1);
7609 got_object_tree_close(tree2);
7611 got_object_commit_close(commit);
7613 got_object_commit_close(parent_commit);
7617 static const struct got_error *
7618 collect_commits(struct got_object_id_queue *commits,
7619 struct got_object_id *initial_commit_id,
7620 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7621 const char *path_prefix, int path_prefix_errcode,
7622 struct got_repository *repo)
7624 const struct got_error *err = NULL;
7625 struct got_commit_graph *graph = NULL;
7626 struct got_object_id *parent_id = NULL;
7627 struct got_object_qid *qid;
7628 struct got_object_id *commit_id = initial_commit_id;
7630 err = got_commit_graph_open(&graph, "/", 1);
7634 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7635 check_cancelled, NULL);
7638 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7639 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7640 check_cancelled, NULL);
7642 if (err->code == GOT_ERR_ITER_COMPLETED) {
7643 err = got_error_msg(GOT_ERR_ANCESTRY,
7644 "ran out of commits to rebase before "
7645 "youngest common ancestor commit has "
7650 err = check_path_prefix(parent_id, commit_id,
7651 path_prefix, path_prefix_errcode, repo);
7655 err = got_object_qid_alloc(&qid, commit_id);
7658 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7659 commit_id = parent_id;
7663 got_commit_graph_close(graph);
7667 static const struct got_error *
7668 cmd_rebase(int argc, char *argv[])
7670 const struct got_error *error = NULL;
7671 struct got_worktree *worktree = NULL;
7672 struct got_repository *repo = NULL;
7673 struct got_fileindex *fileindex = NULL;
7675 struct got_reference *branch = NULL;
7676 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7677 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7678 struct got_object_id *resume_commit_id = NULL;
7679 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7680 struct got_commit_object *commit = NULL;
7681 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7682 int histedit_in_progress = 0;
7683 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7684 struct got_object_id_queue commits;
7685 struct got_pathlist_head merged_paths;
7686 const struct got_object_id_queue *parent_ids;
7687 struct got_object_qid *qid, *pid;
7689 SIMPLEQ_INIT(&commits);
7690 TAILQ_INIT(&merged_paths);
7692 while ((ch = getopt(argc, argv, "ac")) != -1) {
7698 continue_rebase = 1;
7710 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7711 "unveil", NULL) == -1)
7714 if (abort_rebase && continue_rebase)
7716 else if (abort_rebase || continue_rebase) {
7719 } else if (argc != 1)
7722 cwd = getcwd(NULL, 0);
7724 error = got_error_from_errno("getcwd");
7727 error = got_worktree_open(&worktree, cwd);
7729 if (error->code == GOT_ERR_NOT_WORKTREE)
7730 error = wrap_not_worktree_error(error, "rebase", cwd);
7734 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7739 error = apply_unveil(got_repo_get_path(repo), 0,
7740 got_worktree_get_root_path(worktree));
7744 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7748 if (histedit_in_progress) {
7749 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7753 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7758 struct got_update_progress_arg upa;
7759 if (!rebase_in_progress) {
7760 error = got_error(GOT_ERR_NOT_REBASING);
7763 error = got_worktree_rebase_continue(&resume_commit_id,
7764 &new_base_branch, &tmp_branch, &branch, &fileindex,
7768 printf("Switching work tree to %s\n",
7769 got_ref_get_symref_target(new_base_branch));
7770 memset(&upa, 0, sizeof(upa));
7771 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7772 new_base_branch, update_progress, &upa);
7775 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7776 print_update_progress_stats(&upa);
7777 goto done; /* nothing else to do */
7780 if (continue_rebase) {
7781 if (!rebase_in_progress) {
7782 error = got_error(GOT_ERR_NOT_REBASING);
7785 error = got_worktree_rebase_continue(&resume_commit_id,
7786 &new_base_branch, &tmp_branch, &branch, &fileindex,
7791 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7792 resume_commit_id, repo);
7796 yca_id = got_object_id_dup(resume_commit_id);
7797 if (yca_id == NULL) {
7798 error = got_error_from_errno("got_object_id_dup");
7802 error = got_ref_open(&branch, repo, argv[0], 0);
7807 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7811 if (!continue_rebase) {
7812 struct got_object_id *base_commit_id;
7814 base_commit_id = got_worktree_get_base_commit_id(worktree);
7815 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7816 base_commit_id, branch_head_commit_id, repo,
7817 check_cancelled, NULL);
7820 if (yca_id == NULL) {
7821 error = got_error_msg(GOT_ERR_ANCESTRY,
7822 "specified branch shares no common ancestry "
7823 "with work tree's branch");
7827 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7829 if (error->code != GOT_ERR_ANCESTRY)
7833 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7834 "specified branch resolves to a commit which "
7835 "is already contained in work tree's branch");
7838 error = got_worktree_rebase_prepare(&new_base_branch,
7839 &tmp_branch, &fileindex, worktree, branch, repo);
7844 commit_id = branch_head_commit_id;
7845 error = got_object_open_as_commit(&commit, repo, commit_id);
7849 parent_ids = got_object_commit_get_parent_ids(commit);
7850 pid = SIMPLEQ_FIRST(parent_ids);
7852 if (!continue_rebase) {
7853 struct got_update_progress_arg upa;
7854 memset(&upa, 0, sizeof(upa));
7855 error = got_worktree_rebase_abort(worktree, fileindex,
7856 repo, new_base_branch, update_progress, &upa);
7859 printf("Rebase of %s aborted\n",
7860 got_ref_get_name(branch));
7861 print_update_progress_stats(&upa);
7864 error = got_error(GOT_ERR_EMPTY_REBASE);
7867 error = collect_commits(&commits, commit_id, pid->id,
7868 yca_id, got_worktree_get_path_prefix(worktree),
7869 GOT_ERR_REBASE_PATH, repo);
7870 got_object_commit_close(commit);
7875 if (SIMPLEQ_EMPTY(&commits)) {
7876 if (continue_rebase) {
7877 error = rebase_complete(worktree, fileindex,
7878 branch, new_base_branch, tmp_branch, repo);
7881 /* Fast-forward the reference of the branch. */
7882 struct got_object_id *new_head_commit_id;
7884 error = got_ref_resolve(&new_head_commit_id, repo,
7888 error = got_object_id_str(&id_str, new_head_commit_id);
7889 printf("Forwarding %s to commit %s\n",
7890 got_ref_get_name(branch), id_str);
7892 error = got_ref_change_ref(branch,
7893 new_head_commit_id);
7900 SIMPLEQ_FOREACH(qid, &commits, entry) {
7901 struct got_update_progress_arg upa;
7903 commit_id = qid->id;
7904 parent_id = pid ? pid->id : yca_id;
7907 memset(&upa, 0, sizeof(upa));
7908 error = got_worktree_rebase_merge_files(&merged_paths,
7909 worktree, fileindex, parent_id, commit_id, repo,
7910 update_progress, &upa, check_cancelled, NULL);
7914 print_update_progress_stats(&upa);
7915 if (upa.conflicts > 0)
7916 rebase_status = GOT_STATUS_CONFLICT;
7918 if (rebase_status == GOT_STATUS_CONFLICT) {
7919 error = show_rebase_merge_conflict(qid->id, repo);
7922 got_worktree_rebase_pathlist_free(&merged_paths);
7926 error = rebase_commit(&merged_paths, worktree, fileindex,
7927 tmp_branch, commit_id, repo);
7928 got_worktree_rebase_pathlist_free(&merged_paths);
7933 if (rebase_status == GOT_STATUS_CONFLICT) {
7934 error = got_worktree_rebase_postpone(worktree, fileindex);
7937 error = got_error_msg(GOT_ERR_CONFLICTS,
7938 "conflicts must be resolved before rebasing can continue");
7940 error = rebase_complete(worktree, fileindex, branch,
7941 new_base_branch, tmp_branch, repo);
7943 got_object_id_queue_free(&commits);
7944 free(branch_head_commit_id);
7945 free(resume_commit_id);
7948 got_object_commit_close(commit);
7950 got_ref_close(branch);
7951 if (new_base_branch)
7952 got_ref_close(new_base_branch);
7954 got_ref_close(tmp_branch);
7956 got_worktree_close(worktree);
7958 got_repo_close(repo);
7963 usage_histedit(void)
7965 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7970 #define GOT_HISTEDIT_PICK 'p'
7971 #define GOT_HISTEDIT_EDIT 'e'
7972 #define GOT_HISTEDIT_FOLD 'f'
7973 #define GOT_HISTEDIT_DROP 'd'
7974 #define GOT_HISTEDIT_MESG 'm'
7976 static struct got_histedit_cmd {
7980 } got_histedit_cmds[] = {
7981 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7982 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7983 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7985 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7986 { GOT_HISTEDIT_MESG, "mesg",
7987 "single-line log message for commit above (open editor if empty)" },
7990 struct got_histedit_list_entry {
7991 TAILQ_ENTRY(got_histedit_list_entry) entry;
7992 struct got_object_id *commit_id;
7993 const struct got_histedit_cmd *cmd;
7996 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7998 static const struct got_error *
7999 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8000 FILE *f, struct got_repository *repo)
8002 const struct got_error *err = NULL;
8003 char *logmsg = NULL, *id_str = NULL;
8004 struct got_commit_object *commit = NULL;
8007 err = got_object_open_as_commit(&commit, repo, commit_id);
8011 err = get_short_logmsg(&logmsg, 34, commit);
8015 err = got_object_id_str(&id_str, commit_id);
8019 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8021 err = got_ferror(f, GOT_ERR_IO);
8024 got_object_commit_close(commit);
8030 static const struct got_error *
8031 histedit_write_commit_list(struct got_object_id_queue *commits,
8032 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8034 const struct got_error *err = NULL;
8035 struct got_object_qid *qid;
8036 const char *histedit_cmd = NULL;
8038 if (SIMPLEQ_EMPTY(commits))
8039 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8041 SIMPLEQ_FOREACH(qid, commits, entry) {
8042 histedit_cmd = got_histedit_cmds[0].name;
8043 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8044 histedit_cmd = "fold";
8045 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8048 if (edit_logmsg_only) {
8049 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8051 err = got_ferror(f, GOT_ERR_IO);
8060 static const struct got_error *
8061 write_cmd_list(FILE *f, const char *branch_name,
8062 struct got_object_id_queue *commits)
8064 const struct got_error *err = NULL;
8068 struct got_object_qid *qid;
8070 qid = SIMPLEQ_FIRST(commits);
8071 err = got_object_id_str(&id_str, qid->id);
8076 "# Editing the history of branch '%s' starting at\n"
8078 "# Commits will be processed in order from top to "
8079 "bottom of this file.\n", branch_name, id_str);
8081 err = got_ferror(f, GOT_ERR_IO);
8085 n = fprintf(f, "# Available histedit commands:\n");
8087 err = got_ferror(f, GOT_ERR_IO);
8091 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8092 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8093 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8096 err = got_ferror(f, GOT_ERR_IO);
8105 static const struct got_error *
8106 histedit_syntax_error(int lineno)
8108 static char msg[42];
8111 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8113 if (ret == -1 || ret >= sizeof(msg))
8114 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8116 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8119 static const struct got_error *
8120 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8121 char *logmsg, struct got_repository *repo)
8123 const struct got_error *err;
8124 struct got_commit_object *folded_commit = NULL;
8125 char *id_str, *folded_logmsg = NULL;
8127 err = got_object_id_str(&id_str, hle->commit_id);
8131 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8135 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8138 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8139 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8140 folded_logmsg) == -1) {
8141 err = got_error_from_errno("asprintf");
8145 got_object_commit_close(folded_commit);
8147 free(folded_logmsg);
8151 static struct got_histedit_list_entry *
8152 get_folded_commits(struct got_histedit_list_entry *hle)
8154 struct got_histedit_list_entry *prev, *folded = NULL;
8156 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8157 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8158 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8159 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8161 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8167 static const struct got_error *
8168 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8169 struct got_repository *repo)
8171 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8172 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8173 const struct got_error *err = NULL;
8174 struct got_commit_object *commit = NULL;
8177 struct got_histedit_list_entry *folded = NULL;
8179 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8183 folded = get_folded_commits(hle);
8185 while (folded != hle) {
8186 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8187 folded = TAILQ_NEXT(folded, entry);
8190 err = append_folded_commit_msg(&new_msg, folded,
8196 folded = TAILQ_NEXT(folded, entry);
8200 err = got_object_id_str(&id_str, hle->commit_id);
8203 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8206 logmsg_len = asprintf(&new_msg,
8207 "%s\n# original log message of commit %s: %s",
8208 logmsg ? logmsg : "", id_str, orig_logmsg);
8209 if (logmsg_len == -1) {
8210 err = got_error_from_errno("asprintf");
8216 err = got_object_id_str(&id_str, hle->commit_id);
8220 err = got_opentemp_named_fd(&logmsg_path, &fd,
8221 GOT_TMPDIR_STR "/got-logmsg");
8225 write(fd, logmsg, logmsg_len);
8228 err = get_editor(&editor);
8232 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8235 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8238 hle->logmsg = strdup(new_msg);
8239 if (hle->logmsg == NULL)
8240 err = got_error_from_errno("strdup");
8243 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8244 err = got_error_from_errno2("unlink", logmsg_path);
8250 got_object_commit_close(commit);
8254 static const struct got_error *
8255 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8256 FILE *f, struct got_repository *repo)
8258 const struct got_error *err = NULL;
8259 char *line = NULL, *p, *end;
8263 const struct got_histedit_cmd *cmd;
8264 struct got_object_id *commit_id = NULL;
8265 struct got_histedit_list_entry *hle = NULL;
8268 len = getline(&line, &size, f);
8270 const struct got_error *getline_err;
8273 getline_err = got_error_from_errno("getline");
8274 err = got_ferror(f, getline_err->code);
8279 while (isspace((unsigned char)p[0]))
8281 if (p[0] == '#' || p[0] == '\0') {
8287 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8288 cmd = &got_histedit_cmds[i];
8289 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8290 isspace((unsigned char)p[strlen(cmd->name)])) {
8291 p += strlen(cmd->name);
8294 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8299 if (i == nitems(got_histedit_cmds)) {
8300 err = histedit_syntax_error(lineno);
8303 while (isspace((unsigned char)p[0]))
8305 if (cmd->code == GOT_HISTEDIT_MESG) {
8306 if (hle == NULL || hle->logmsg != NULL) {
8307 err = got_error(GOT_ERR_HISTEDIT_CMD);
8311 err = histedit_edit_logmsg(hle, repo);
8315 hle->logmsg = strdup(p);
8316 if (hle->logmsg == NULL) {
8317 err = got_error_from_errno("strdup");
8326 while (end[0] && !isspace((unsigned char)end[0]))
8330 err = got_object_resolve_id_str(&commit_id, repo, p);
8332 /* override error code */
8333 err = histedit_syntax_error(lineno);
8337 hle = malloc(sizeof(*hle));
8339 err = got_error_from_errno("malloc");
8343 hle->commit_id = commit_id;
8348 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8356 static const struct got_error *
8357 histedit_check_script(struct got_histedit_list *histedit_cmds,
8358 struct got_object_id_queue *commits, struct got_repository *repo)
8360 const struct got_error *err = NULL;
8361 struct got_object_qid *qid;
8362 struct got_histedit_list_entry *hle;
8363 static char msg[92];
8366 if (TAILQ_EMPTY(histedit_cmds))
8367 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8368 "histedit script contains no commands");
8369 if (SIMPLEQ_EMPTY(commits))
8370 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8372 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8373 struct got_histedit_list_entry *hle2;
8374 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8377 if (got_object_id_cmp(hle->commit_id,
8378 hle2->commit_id) != 0)
8380 err = got_object_id_str(&id_str, hle->commit_id);
8383 snprintf(msg, sizeof(msg), "commit %s is listed "
8384 "more than once in histedit script", id_str);
8386 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8390 SIMPLEQ_FOREACH(qid, commits, entry) {
8391 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8392 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8396 err = got_object_id_str(&id_str, qid->id);
8399 snprintf(msg, sizeof(msg),
8400 "commit %s missing from histedit script", id_str);
8402 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8406 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8407 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8408 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8409 "last commit in histedit script cannot be folded");
8414 static const struct got_error *
8415 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8416 const char *path, struct got_object_id_queue *commits,
8417 struct got_repository *repo)
8419 const struct got_error *err = NULL;
8423 err = get_editor(&editor);
8427 if (spawn_editor(editor, path) == -1) {
8428 err = got_error_from_errno("failed spawning editor");
8432 f = fopen(path, "r");
8434 err = got_error_from_errno("fopen");
8437 err = histedit_parse_list(histedit_cmds, f, repo);
8441 err = histedit_check_script(histedit_cmds, commits, repo);
8443 if (f && fclose(f) == EOF && err == NULL)
8444 err = got_error_from_errno("fclose");
8449 static const struct got_error *
8450 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8451 struct got_object_id_queue *, const char *, const char *,
8452 struct got_repository *);
8454 static const struct got_error *
8455 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8456 struct got_object_id_queue *commits, const char *branch_name,
8457 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8459 const struct got_error *err;
8463 err = got_opentemp_named(&path, &f, "got-histedit");
8467 err = write_cmd_list(f, branch_name, commits);
8471 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8476 if (edit_logmsg_only || fold_only) {
8478 err = histedit_parse_list(histedit_cmds, f, repo);
8480 if (fclose(f) == EOF) {
8481 err = got_error_from_errno("fclose");
8485 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8487 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8488 err->code != GOT_ERR_HISTEDIT_CMD)
8490 err = histedit_edit_list_retry(histedit_cmds, err,
8491 commits, path, branch_name, repo);
8495 if (f && fclose(f) == EOF && err == NULL)
8496 err = got_error_from_errno("fclose");
8497 if (path && unlink(path) != 0 && err == NULL)
8498 err = got_error_from_errno2("unlink", path);
8503 static const struct got_error *
8504 histedit_save_list(struct got_histedit_list *histedit_cmds,
8505 struct got_worktree *worktree, struct got_repository *repo)
8507 const struct got_error *err = NULL;
8510 struct got_histedit_list_entry *hle;
8511 struct got_commit_object *commit = NULL;
8513 err = got_worktree_get_histedit_script_path(&path, worktree);
8517 f = fopen(path, "w");
8519 err = got_error_from_errno2("fopen", path);
8522 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8523 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8529 int n = fprintf(f, "%c %s\n",
8530 GOT_HISTEDIT_MESG, hle->logmsg);
8532 err = got_ferror(f, GOT_ERR_IO);
8538 if (f && fclose(f) == EOF && err == NULL)
8539 err = got_error_from_errno("fclose");
8542 got_object_commit_close(commit);
8547 histedit_free_list(struct got_histedit_list *histedit_cmds)
8549 struct got_histedit_list_entry *hle;
8551 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8552 TAILQ_REMOVE(histedit_cmds, hle, entry);
8557 static const struct got_error *
8558 histedit_load_list(struct got_histedit_list *histedit_cmds,
8559 const char *path, struct got_repository *repo)
8561 const struct got_error *err = NULL;
8564 f = fopen(path, "r");
8566 err = got_error_from_errno2("fopen", path);
8570 err = histedit_parse_list(histedit_cmds, f, repo);
8572 if (f && fclose(f) == EOF && err == NULL)
8573 err = got_error_from_errno("fclose");
8577 static const struct got_error *
8578 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8579 const struct got_error *edit_err, struct got_object_id_queue *commits,
8580 const char *path, const char *branch_name, struct got_repository *repo)
8582 const struct got_error *err = NULL, *prev_err = edit_err;
8585 while (resp != 'c' && resp != 'r' && resp != 'a') {
8586 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8587 "or (a)bort: ", getprogname(), prev_err->msg);
8592 histedit_free_list(histedit_cmds);
8593 err = histedit_run_editor(histedit_cmds, path, commits,
8596 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8597 err->code != GOT_ERR_HISTEDIT_CMD)
8604 } else if (resp == 'r') {
8605 histedit_free_list(histedit_cmds);
8606 err = histedit_edit_script(histedit_cmds,
8607 commits, branch_name, 0, 0, repo);
8609 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8610 err->code != GOT_ERR_HISTEDIT_CMD)
8617 } else if (resp == 'a') {
8618 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8621 printf("invalid response '%c'\n", resp);
8627 static const struct got_error *
8628 histedit_complete(struct got_worktree *worktree,
8629 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8630 struct got_reference *branch, struct got_repository *repo)
8632 printf("Switching work tree to %s\n",
8633 got_ref_get_symref_target(branch));
8634 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8638 static const struct got_error *
8639 show_histedit_progress(struct got_commit_object *commit,
8640 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8642 const struct got_error *err;
8643 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8645 err = got_object_id_str(&old_id_str, hle->commit_id);
8650 err = got_object_id_str(&new_id_str, new_id);
8655 old_id_str[12] = '\0';
8657 new_id_str[12] = '\0';
8660 logmsg = strdup(hle->logmsg);
8661 if (logmsg == NULL) {
8662 err = got_error_from_errno("strdup");
8665 trim_logmsg(logmsg, 42);
8667 err = get_short_logmsg(&logmsg, 42, commit);
8672 switch (hle->cmd->code) {
8673 case GOT_HISTEDIT_PICK:
8674 case GOT_HISTEDIT_EDIT:
8675 printf("%s -> %s: %s\n", old_id_str,
8676 new_id_str ? new_id_str : "no-op change", logmsg);
8678 case GOT_HISTEDIT_DROP:
8679 case GOT_HISTEDIT_FOLD:
8680 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8692 static const struct got_error *
8693 histedit_commit(struct got_pathlist_head *merged_paths,
8694 struct got_worktree *worktree, struct got_fileindex *fileindex,
8695 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8696 struct got_repository *repo)
8698 const struct got_error *err;
8699 struct got_commit_object *commit;
8700 struct got_object_id *new_commit_id;
8702 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8703 && hle->logmsg == NULL) {
8704 err = histedit_edit_logmsg(hle, repo);
8709 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8713 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8714 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8717 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8719 err = show_histedit_progress(commit, hle, NULL);
8721 err = show_histedit_progress(commit, hle, new_commit_id);
8722 free(new_commit_id);
8725 got_object_commit_close(commit);
8729 static const struct got_error *
8730 histedit_skip_commit(struct got_histedit_list_entry *hle,
8731 struct got_worktree *worktree, struct got_repository *repo)
8733 const struct got_error *error;
8734 struct got_commit_object *commit;
8736 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8741 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8745 error = show_histedit_progress(commit, hle, NULL);
8746 got_object_commit_close(commit);
8750 static const struct got_error *
8751 check_local_changes(void *arg, unsigned char status,
8752 unsigned char staged_status, const char *path,
8753 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8754 struct got_object_id *commit_id, int dirfd, const char *de_name)
8756 int *have_local_changes = arg;
8759 case GOT_STATUS_ADD:
8760 case GOT_STATUS_DELETE:
8761 case GOT_STATUS_MODIFY:
8762 case GOT_STATUS_CONFLICT:
8763 *have_local_changes = 1;
8764 return got_error(GOT_ERR_CANCELLED);
8769 switch (staged_status) {
8770 case GOT_STATUS_ADD:
8771 case GOT_STATUS_DELETE:
8772 case GOT_STATUS_MODIFY:
8773 *have_local_changes = 1;
8774 return got_error(GOT_ERR_CANCELLED);
8782 static const struct got_error *
8783 cmd_histedit(int argc, char *argv[])
8785 const struct got_error *error = NULL;
8786 struct got_worktree *worktree = NULL;
8787 struct got_fileindex *fileindex = NULL;
8788 struct got_repository *repo = NULL;
8790 struct got_reference *branch = NULL;
8791 struct got_reference *tmp_branch = NULL;
8792 struct got_object_id *resume_commit_id = NULL;
8793 struct got_object_id *base_commit_id = NULL;
8794 struct got_object_id *head_commit_id = NULL;
8795 struct got_commit_object *commit = NULL;
8796 int ch, rebase_in_progress = 0;
8797 struct got_update_progress_arg upa;
8798 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8799 int edit_logmsg_only = 0, fold_only = 0;
8800 const char *edit_script_path = NULL;
8801 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8802 struct got_object_id_queue commits;
8803 struct got_pathlist_head merged_paths;
8804 const struct got_object_id_queue *parent_ids;
8805 struct got_object_qid *pid;
8806 struct got_histedit_list histedit_cmds;
8807 struct got_histedit_list_entry *hle;
8809 SIMPLEQ_INIT(&commits);
8810 TAILQ_INIT(&histedit_cmds);
8811 TAILQ_INIT(&merged_paths);
8812 memset(&upa, 0, sizeof(upa));
8814 while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8826 edit_script_path = optarg;
8829 edit_logmsg_only = 1;
8841 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8842 "unveil", NULL) == -1)
8845 if (abort_edit && continue_edit)
8846 option_conflict('a', 'c');
8847 if (edit_script_path && edit_logmsg_only)
8848 option_conflict('F', 'm');
8849 if (abort_edit && edit_logmsg_only)
8850 option_conflict('a', 'm');
8851 if (continue_edit && edit_logmsg_only)
8852 option_conflict('c', 'm');
8853 if (abort_edit && fold_only)
8854 option_conflict('a', 'f');
8855 if (continue_edit && fold_only)
8856 option_conflict('c', 'f');
8857 if (fold_only && edit_logmsg_only)
8858 option_conflict('f', 'm');
8859 if (edit_script_path && fold_only)
8860 option_conflict('F', 'f');
8865 * This command cannot apply unveil(2) in all cases because the
8866 * user may choose to run an editor to edit the histedit script
8867 * and to edit individual commit log messages.
8868 * unveil(2) traverses exec(2); if an editor is used we have to
8869 * apply unveil after edit script and log messages have been written.
8870 * XXX TODO: Make use of unveil(2) where possible.
8873 cwd = getcwd(NULL, 0);
8875 error = got_error_from_errno("getcwd");
8878 error = got_worktree_open(&worktree, cwd);
8880 if (error->code == GOT_ERR_NOT_WORKTREE)
8881 error = wrap_not_worktree_error(error, "histedit", cwd);
8885 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8890 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8893 if (rebase_in_progress) {
8894 error = got_error(GOT_ERR_REBASING);
8898 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8902 if (edit_in_progress && edit_logmsg_only) {
8903 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8904 "histedit operation is in progress in this "
8905 "work tree and must be continued or aborted "
8906 "before the -m option can be used");
8909 if (edit_in_progress && fold_only) {
8910 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8911 "histedit operation is in progress in this "
8912 "work tree and must be continued or aborted "
8913 "before the -f option can be used");
8917 if (edit_in_progress && abort_edit) {
8918 error = got_worktree_histedit_continue(&resume_commit_id,
8919 &tmp_branch, &branch, &base_commit_id, &fileindex,
8923 printf("Switching work tree to %s\n",
8924 got_ref_get_symref_target(branch));
8925 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8926 branch, base_commit_id, update_progress, &upa);
8929 printf("Histedit of %s aborted\n",
8930 got_ref_get_symref_target(branch));
8931 print_update_progress_stats(&upa);
8932 goto done; /* nothing else to do */
8933 } else if (abort_edit) {
8934 error = got_error(GOT_ERR_NOT_HISTEDIT);
8938 if (continue_edit) {
8941 if (!edit_in_progress) {
8942 error = got_error(GOT_ERR_NOT_HISTEDIT);
8946 error = got_worktree_get_histedit_script_path(&path, worktree);
8950 error = histedit_load_list(&histedit_cmds, path, repo);
8955 error = got_worktree_histedit_continue(&resume_commit_id,
8956 &tmp_branch, &branch, &base_commit_id, &fileindex,
8961 error = got_ref_resolve(&head_commit_id, repo, branch);
8965 error = got_object_open_as_commit(&commit, repo,
8969 parent_ids = got_object_commit_get_parent_ids(commit);
8970 pid = SIMPLEQ_FIRST(parent_ids);
8972 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8975 error = collect_commits(&commits, head_commit_id, pid->id,
8976 base_commit_id, got_worktree_get_path_prefix(worktree),
8977 GOT_ERR_HISTEDIT_PATH, repo);
8978 got_object_commit_close(commit);
8983 if (edit_in_progress) {
8984 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8988 error = got_ref_open(&branch, repo,
8989 got_worktree_get_head_ref_name(worktree), 0);
8993 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8994 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8995 "will not edit commit history of a branch outside "
8996 "the \"refs/heads/\" reference namespace");
9000 error = got_ref_resolve(&head_commit_id, repo, branch);
9001 got_ref_close(branch);
9006 error = got_object_open_as_commit(&commit, repo,
9010 parent_ids = got_object_commit_get_parent_ids(commit);
9011 pid = SIMPLEQ_FIRST(parent_ids);
9013 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9016 error = collect_commits(&commits, head_commit_id, pid->id,
9017 got_worktree_get_base_commit_id(worktree),
9018 got_worktree_get_path_prefix(worktree),
9019 GOT_ERR_HISTEDIT_PATH, repo);
9020 got_object_commit_close(commit);
9025 if (SIMPLEQ_EMPTY(&commits)) {
9026 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9030 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9031 &base_commit_id, &fileindex, worktree, repo);
9035 if (edit_script_path) {
9036 error = histedit_load_list(&histedit_cmds,
9037 edit_script_path, repo);
9039 got_worktree_histedit_abort(worktree, fileindex,
9040 repo, branch, base_commit_id,
9041 update_progress, &upa);
9042 print_update_progress_stats(&upa);
9046 const char *branch_name;
9047 branch_name = got_ref_get_symref_target(branch);
9048 if (strncmp(branch_name, "refs/heads/", 11) == 0)
9050 error = histedit_edit_script(&histedit_cmds, &commits,
9051 branch_name, edit_logmsg_only, fold_only, repo);
9053 got_worktree_histedit_abort(worktree, fileindex,
9054 repo, branch, base_commit_id,
9055 update_progress, &upa);
9056 print_update_progress_stats(&upa);
9062 error = histedit_save_list(&histedit_cmds, worktree,
9065 got_worktree_histedit_abort(worktree, fileindex,
9066 repo, branch, base_commit_id,
9067 update_progress, &upa);
9068 print_update_progress_stats(&upa);
9074 error = histedit_check_script(&histedit_cmds, &commits, repo);
9078 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9079 if (resume_commit_id) {
9080 if (got_object_id_cmp(hle->commit_id,
9081 resume_commit_id) != 0)
9084 resume_commit_id = NULL;
9085 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9086 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9087 error = histedit_skip_commit(hle, worktree,
9092 struct got_pathlist_head paths;
9093 int have_changes = 0;
9096 error = got_pathlist_append(&paths, "", NULL);
9099 error = got_worktree_status(worktree, &paths,
9100 repo, check_local_changes, &have_changes,
9101 check_cancelled, NULL);
9102 got_pathlist_free(&paths);
9104 if (error->code != GOT_ERR_CANCELLED)
9106 if (sigint_received || sigpipe_received)
9110 error = histedit_commit(NULL, worktree,
9111 fileindex, tmp_branch, hle, repo);
9115 error = got_object_open_as_commit(
9116 &commit, repo, hle->commit_id);
9119 error = show_histedit_progress(commit,
9121 got_object_commit_close(commit);
9130 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9131 error = histedit_skip_commit(hle, worktree, repo);
9137 error = got_object_open_as_commit(&commit, repo,
9141 parent_ids = got_object_commit_get_parent_ids(commit);
9142 pid = SIMPLEQ_FIRST(parent_ids);
9144 error = got_worktree_histedit_merge_files(&merged_paths,
9145 worktree, fileindex, pid->id, hle->commit_id, repo,
9146 update_progress, &upa, check_cancelled, NULL);
9149 got_object_commit_close(commit);
9152 print_update_progress_stats(&upa);
9153 if (upa.conflicts > 0)
9154 rebase_status = GOT_STATUS_CONFLICT;
9156 if (rebase_status == GOT_STATUS_CONFLICT) {
9157 error = show_rebase_merge_conflict(hle->commit_id,
9161 got_worktree_rebase_pathlist_free(&merged_paths);
9165 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9167 error = got_object_id_str(&id_str, hle->commit_id);
9170 printf("Stopping histedit for amending commit %s\n",
9173 got_worktree_rebase_pathlist_free(&merged_paths);
9174 error = got_worktree_histedit_postpone(worktree,
9179 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9180 error = histedit_skip_commit(hle, worktree, repo);
9186 error = histedit_commit(&merged_paths, worktree, fileindex,
9187 tmp_branch, hle, repo);
9188 got_worktree_rebase_pathlist_free(&merged_paths);
9193 if (rebase_status == GOT_STATUS_CONFLICT) {
9194 error = got_worktree_histedit_postpone(worktree, fileindex);
9197 error = got_error_msg(GOT_ERR_CONFLICTS,
9198 "conflicts must be resolved before histedit can continue");
9200 error = histedit_complete(worktree, fileindex, tmp_branch,
9203 got_object_id_queue_free(&commits);
9204 histedit_free_list(&histedit_cmds);
9205 free(head_commit_id);
9206 free(base_commit_id);
9207 free(resume_commit_id);
9209 got_object_commit_close(commit);
9211 got_ref_close(branch);
9213 got_ref_close(tmp_branch);
9215 got_worktree_close(worktree);
9217 got_repo_close(repo);
9222 usage_integrate(void)
9224 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9228 static const struct got_error *
9229 cmd_integrate(int argc, char *argv[])
9231 const struct got_error *error = NULL;
9232 struct got_repository *repo = NULL;
9233 struct got_worktree *worktree = NULL;
9234 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9235 const char *branch_arg = NULL;
9236 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9237 struct got_fileindex *fileindex = NULL;
9238 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9240 struct got_update_progress_arg upa;
9242 while ((ch = getopt(argc, argv, "")) != -1) {
9255 branch_arg = argv[0];
9257 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9258 "unveil", NULL) == -1)
9261 cwd = getcwd(NULL, 0);
9263 error = got_error_from_errno("getcwd");
9267 error = got_worktree_open(&worktree, cwd);
9269 if (error->code == GOT_ERR_NOT_WORKTREE)
9270 error = wrap_not_worktree_error(error, "integrate",
9275 error = check_rebase_or_histedit_in_progress(worktree);
9279 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9284 error = apply_unveil(got_repo_get_path(repo), 0,
9285 got_worktree_get_root_path(worktree));
9289 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9290 error = got_error_from_errno("asprintf");
9294 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9295 &base_branch_ref, worktree, refname, repo);
9299 refname = strdup(got_ref_get_name(branch_ref));
9300 if (refname == NULL) {
9301 error = got_error_from_errno("strdup");
9302 got_worktree_integrate_abort(worktree, fileindex, repo,
9303 branch_ref, base_branch_ref);
9306 base_refname = strdup(got_ref_get_name(base_branch_ref));
9307 if (base_refname == NULL) {
9308 error = got_error_from_errno("strdup");
9309 got_worktree_integrate_abort(worktree, fileindex, repo,
9310 branch_ref, base_branch_ref);
9314 error = got_ref_resolve(&commit_id, repo, branch_ref);
9318 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9322 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9323 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9324 "specified branch has already been integrated");
9325 got_worktree_integrate_abort(worktree, fileindex, repo,
9326 branch_ref, base_branch_ref);
9330 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9332 if (error->code == GOT_ERR_ANCESTRY)
9333 error = got_error(GOT_ERR_REBASE_REQUIRED);
9334 got_worktree_integrate_abort(worktree, fileindex, repo,
9335 branch_ref, base_branch_ref);
9339 memset(&upa, 0, sizeof(upa));
9340 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9341 branch_ref, base_branch_ref, update_progress, &upa,
9342 check_cancelled, NULL);
9346 printf("Integrated %s into %s\n", refname, base_refname);
9347 print_update_progress_stats(&upa);
9350 got_repo_close(repo);
9352 got_worktree_close(worktree);
9354 free(base_commit_id);
9364 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9365 "[-S] [file-path ...]\n",
9370 static const struct got_error *
9371 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9372 const char *path, struct got_object_id *blob_id,
9373 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9374 int dirfd, const char *de_name)
9376 const struct got_error *err = NULL;
9377 char *id_str = NULL;
9379 if (staged_status != GOT_STATUS_ADD &&
9380 staged_status != GOT_STATUS_MODIFY &&
9381 staged_status != GOT_STATUS_DELETE)
9384 if (staged_status == GOT_STATUS_ADD ||
9385 staged_status == GOT_STATUS_MODIFY)
9386 err = got_object_id_str(&id_str, staged_blob_id);
9388 err = got_object_id_str(&id_str, blob_id);
9392 printf("%s %c %s\n", id_str, staged_status, path);
9397 static const struct got_error *
9398 cmd_stage(int argc, char *argv[])
9400 const struct got_error *error = NULL;
9401 struct got_repository *repo = NULL;
9402 struct got_worktree *worktree = NULL;
9404 struct got_pathlist_head paths;
9405 struct got_pathlist_entry *pe;
9406 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9407 FILE *patch_script_file = NULL;
9408 const char *patch_script_path = NULL;
9409 struct choose_patch_arg cpa;
9413 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9422 patch_script_path = optarg;
9425 allow_bad_symlinks = 1;
9437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9438 "unveil", NULL) == -1)
9441 if (list_stage && (pflag || patch_script_path))
9442 errx(1, "-l option cannot be used with other options");
9443 if (patch_script_path && !pflag)
9444 errx(1, "-F option can only be used together with -p option");
9446 cwd = getcwd(NULL, 0);
9448 error = got_error_from_errno("getcwd");
9452 error = got_worktree_open(&worktree, cwd);
9454 if (error->code == GOT_ERR_NOT_WORKTREE)
9455 error = wrap_not_worktree_error(error, "stage", cwd);
9459 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9464 if (patch_script_path) {
9465 patch_script_file = fopen(patch_script_path, "r");
9466 if (patch_script_file == NULL) {
9467 error = got_error_from_errno2("fopen",
9472 error = apply_unveil(got_repo_get_path(repo), 0,
9473 got_worktree_get_root_path(worktree));
9477 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9482 error = got_worktree_status(worktree, &paths, repo,
9483 print_stage, NULL, check_cancelled, NULL);
9485 cpa.patch_script_file = patch_script_file;
9486 cpa.action = "stage";
9487 error = got_worktree_stage(worktree, &paths,
9488 pflag ? NULL : print_status, NULL,
9489 pflag ? choose_patch : NULL, &cpa,
9490 allow_bad_symlinks, repo);
9493 if (patch_script_file && fclose(patch_script_file) == EOF &&
9495 error = got_error_from_errno2("fclose", patch_script_path);
9497 got_repo_close(repo);
9499 got_worktree_close(worktree);
9500 TAILQ_FOREACH(pe, &paths, entry)
9501 free((char *)pe->path);
9502 got_pathlist_free(&paths);
9510 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9511 "[file-path ...]\n",
9517 static const struct got_error *
9518 cmd_unstage(int argc, char *argv[])
9520 const struct got_error *error = NULL;
9521 struct got_repository *repo = NULL;
9522 struct got_worktree *worktree = NULL;
9524 struct got_pathlist_head paths;
9525 struct got_pathlist_entry *pe;
9527 struct got_update_progress_arg upa;
9528 FILE *patch_script_file = NULL;
9529 const char *patch_script_path = NULL;
9530 struct choose_patch_arg cpa;
9534 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9540 patch_script_path = optarg;
9552 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9553 "unveil", NULL) == -1)
9556 if (patch_script_path && !pflag)
9557 errx(1, "-F option can only be used together with -p option");
9559 cwd = getcwd(NULL, 0);
9561 error = got_error_from_errno("getcwd");
9565 error = got_worktree_open(&worktree, cwd);
9567 if (error->code == GOT_ERR_NOT_WORKTREE)
9568 error = wrap_not_worktree_error(error, "unstage", cwd);
9572 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9577 if (patch_script_path) {
9578 patch_script_file = fopen(patch_script_path, "r");
9579 if (patch_script_file == NULL) {
9580 error = got_error_from_errno2("fopen",
9586 error = apply_unveil(got_repo_get_path(repo), 0,
9587 got_worktree_get_root_path(worktree));
9591 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9595 cpa.patch_script_file = patch_script_file;
9596 cpa.action = "unstage";
9597 memset(&upa, 0, sizeof(upa));
9598 error = got_worktree_unstage(worktree, &paths, update_progress,
9599 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9601 print_update_progress_stats(&upa);
9603 if (patch_script_file && fclose(patch_script_file) == EOF &&
9605 error = got_error_from_errno2("fclose", patch_script_path);
9607 got_repo_close(repo);
9609 got_worktree_close(worktree);
9610 TAILQ_FOREACH(pe, &paths, entry)
9611 free((char *)pe->path);
9612 got_pathlist_free(&paths);
9620 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9621 "arg1 [arg2 ...]\n", getprogname());
9625 static const struct got_error *
9626 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9628 const struct got_error *err;
9629 struct got_blob_object *blob;
9631 err = got_object_open_as_blob(&blob, repo, id, 8192);
9635 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9636 got_object_blob_close(blob);
9640 static const struct got_error *
9641 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9643 const struct got_error *err;
9644 struct got_tree_object *tree;
9647 err = got_object_open_as_tree(&tree, repo, id);
9651 nentries = got_object_tree_get_nentries(tree);
9652 for (i = 0; i < nentries; i++) {
9653 struct got_tree_entry *te;
9655 if (sigint_received || sigpipe_received)
9657 te = got_object_tree_get_entry(tree, i);
9658 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9661 fprintf(outfile, "%s %.7o %s\n", id_str,
9662 got_tree_entry_get_mode(te),
9663 got_tree_entry_get_name(te));
9667 got_object_tree_close(tree);
9671 static const struct got_error *
9672 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9674 const struct got_error *err;
9675 struct got_commit_object *commit;
9676 const struct got_object_id_queue *parent_ids;
9677 struct got_object_qid *pid;
9678 char *id_str = NULL;
9679 const char *logmsg = NULL;
9681 err = got_object_open_as_commit(&commit, repo, id);
9685 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9689 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9690 parent_ids = got_object_commit_get_parent_ids(commit);
9691 fprintf(outfile, "numparents %d\n",
9692 got_object_commit_get_nparents(commit));
9693 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9695 err = got_object_id_str(&pid_str, pid->id);
9698 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9701 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9702 got_object_commit_get_author(commit),
9703 (long long)got_object_commit_get_author_time(commit));
9705 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9706 got_object_commit_get_author(commit),
9707 (long long)got_object_commit_get_committer_time(commit));
9709 logmsg = got_object_commit_get_logmsg_raw(commit);
9710 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9711 fprintf(outfile, "%s", logmsg);
9714 got_object_commit_close(commit);
9718 static const struct got_error *
9719 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9721 const struct got_error *err;
9722 struct got_tag_object *tag;
9723 char *id_str = NULL;
9724 const char *tagmsg = NULL;
9726 err = got_object_open_as_tag(&tag, repo, id);
9730 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9734 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9736 switch (got_object_tag_get_object_type(tag)) {
9737 case GOT_OBJ_TYPE_BLOB:
9738 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9739 GOT_OBJ_LABEL_BLOB);
9741 case GOT_OBJ_TYPE_TREE:
9742 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9743 GOT_OBJ_LABEL_TREE);
9745 case GOT_OBJ_TYPE_COMMIT:
9746 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9747 GOT_OBJ_LABEL_COMMIT);
9749 case GOT_OBJ_TYPE_TAG:
9750 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9757 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9758 got_object_tag_get_name(tag));
9760 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9761 got_object_tag_get_tagger(tag),
9762 (long long)got_object_tag_get_tagger_time(tag));
9764 tagmsg = got_object_tag_get_message(tag);
9765 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9766 fprintf(outfile, "%s", tagmsg);
9769 got_object_tag_close(tag);
9773 static const struct got_error *
9774 cmd_cat(int argc, char *argv[])
9776 const struct got_error *error;
9777 struct got_repository *repo = NULL;
9778 struct got_worktree *worktree = NULL;
9779 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9780 const char *commit_id_str = NULL;
9781 struct got_object_id *id = NULL, *commit_id = NULL;
9782 int ch, obj_type, i, force_path = 0;
9783 struct got_reflist_head refs;
9788 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9793 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9796 commit_id_str = optarg;
9799 repo_path = realpath(optarg, NULL);
9800 if (repo_path == NULL)
9801 return got_error_from_errno2("realpath",
9803 got_path_strip_trailing_slashes(repo_path);
9817 cwd = getcwd(NULL, 0);
9819 error = got_error_from_errno("getcwd");
9822 error = got_worktree_open(&worktree, cwd);
9823 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9826 if (repo_path == NULL) {
9828 got_worktree_get_repo_path(worktree));
9829 if (repo_path == NULL) {
9830 error = got_error_from_errno("strdup");
9836 if (repo_path == NULL) {
9837 repo_path = getcwd(NULL, 0);
9838 if (repo_path == NULL)
9839 return got_error_from_errno("getcwd");
9842 error = got_repo_open(&repo, repo_path, NULL);
9847 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9851 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9855 if (commit_id_str == NULL)
9856 commit_id_str = GOT_REF_HEAD;
9857 error = got_repo_match_object_id(&commit_id, NULL,
9858 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9862 for (i = 0; i < argc; i++) {
9864 error = got_object_id_by_path(&id, repo, commit_id,
9869 error = got_repo_match_object_id(&id, &label, argv[i],
9870 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9873 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9874 error->code != GOT_ERR_NOT_REF)
9876 error = got_object_id_by_path(&id, repo,
9877 commit_id, argv[i]);
9883 error = got_object_get_type(&obj_type, repo, id);
9888 case GOT_OBJ_TYPE_BLOB:
9889 error = cat_blob(id, repo, stdout);
9891 case GOT_OBJ_TYPE_TREE:
9892 error = cat_tree(id, repo, stdout);
9894 case GOT_OBJ_TYPE_COMMIT:
9895 error = cat_commit(id, repo, stdout);
9897 case GOT_OBJ_TYPE_TAG:
9898 error = cat_tag(id, repo, stdout);
9901 error = got_error(GOT_ERR_OBJ_TYPE);
9916 got_worktree_close(worktree);
9918 const struct got_error *repo_error;
9919 repo_error = got_repo_close(repo);
9923 got_ref_list_free(&refs);
9930 fprintf(stderr, "usage: %s info [path ...]\n",
9935 static const struct got_error *
9936 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9937 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9938 struct got_object_id *commit_id)
9940 const struct got_error *err = NULL;
9941 char *id_str = NULL;
9943 struct tm mytm, *tm;
9944 struct got_pathlist_head *paths = arg;
9945 struct got_pathlist_entry *pe;
9948 * Clear error indication from any of the path arguments which
9949 * would cause this file index entry to be displayed.
9951 TAILQ_FOREACH(pe, paths, entry) {
9952 if (got_path_cmp(path, pe->path, strlen(path),
9953 pe->path_len) == 0 ||
9954 got_path_is_child(path, pe->path, pe->path_len))
9955 pe->data = NULL; /* no error */
9958 printf(GOT_COMMIT_SEP_STR);
9960 printf("symlink: %s\n", path);
9961 else if (S_ISREG(mode)) {
9962 printf("file: %s\n", path);
9963 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9964 } else if (S_ISDIR(mode))
9965 printf("directory: %s\n", path);
9967 printf("something: %s\n", path);
9969 tm = localtime_r(&mtime, &mytm);
9972 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9973 return got_error(GOT_ERR_NO_SPACE);
9974 printf("timestamp: %s\n", datebuf);
9977 err = got_object_id_str(&id_str, blob_id);
9980 printf("based on blob: %s\n", id_str);
9984 if (staged_blob_id) {
9985 err = got_object_id_str(&id_str, staged_blob_id);
9988 printf("based on staged blob: %s\n", id_str);
9993 err = got_object_id_str(&id_str, commit_id);
9996 printf("based on commit: %s\n", id_str);
10003 static const struct got_error *
10004 cmd_info(int argc, char *argv[])
10006 const struct got_error *error = NULL;
10007 struct got_worktree *worktree = NULL;
10008 char *cwd = NULL, *id_str = NULL;
10009 struct got_pathlist_head paths;
10010 struct got_pathlist_entry *pe;
10011 char *uuidstr = NULL;
10012 int ch, show_files = 0;
10014 TAILQ_INIT(&paths);
10016 while ((ch = getopt(argc, argv, "")) != -1) {
10028 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10032 cwd = getcwd(NULL, 0);
10034 error = got_error_from_errno("getcwd");
10038 error = got_worktree_open(&worktree, cwd);
10040 if (error->code == GOT_ERR_NOT_WORKTREE)
10041 error = wrap_not_worktree_error(error, "status", cwd);
10045 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10050 error = get_worktree_paths_from_argv(&paths, argc, argv,
10057 error = got_object_id_str(&id_str,
10058 got_worktree_get_base_commit_id(worktree));
10062 error = got_worktree_get_uuid(&uuidstr, worktree);
10066 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10067 printf("work tree base commit: %s\n", id_str);
10068 printf("work tree path prefix: %s\n",
10069 got_worktree_get_path_prefix(worktree));
10070 printf("work tree branch reference: %s\n",
10071 got_worktree_get_head_ref_name(worktree));
10072 printf("work tree UUID: %s\n", uuidstr);
10073 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10076 struct got_pathlist_entry *pe;
10077 TAILQ_FOREACH(pe, &paths, entry) {
10078 if (pe->path_len == 0)
10081 * Assume this path will fail. This will be corrected
10082 * in print_path_info() in case the path does suceeed.
10084 pe->data = (void *)got_error_path(pe->path,
10087 error = got_worktree_path_info(worktree, &paths,
10088 print_path_info, &paths, check_cancelled, NULL);
10091 TAILQ_FOREACH(pe, &paths, entry) {
10092 if (pe->data != NULL) {
10093 error = pe->data; /* bad path */
10099 TAILQ_FOREACH(pe, &paths, entry)
10100 free((char *)pe->path);
10101 got_pathlist_free(&paths);