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;
933 const char *remote_repo_path;
935 int fetch_all_branches;
936 int mirror_references;
940 /* XXX forward declaration */
941 static const struct got_error *
942 create_config_files(const char *proto, const char *host, const char *port,
943 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
944 int mirror_references, struct got_pathlist_head *symrefs,
945 struct got_pathlist_head *wanted_branches, struct got_repository *repo);
947 static const struct got_error *
948 fetch_progress(void *arg, const char *message, off_t packfile_size,
949 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
951 const struct got_error *err = NULL;
952 struct got_fetch_progress_arg *a = arg;
953 char scaled_size[FMT_SCALED_STRSIZE];
954 int p_indexed, p_resolved;
955 int print_size = 0, print_indexed = 0, print_resolved = 0;
958 * In order to allow a failed clone to be resumed with 'got fetch'
959 * we try to create configuration files as soon as possible.
960 * Once the server has sent information about its default branch
961 * we have all required information.
963 if (a->create_configs && !a->configs_created &&
964 !TAILQ_EMPTY(a->config_info.symrefs)) {
965 err = create_config_files(a->config_info.proto,
966 a->config_info.host, a->config_info.port,
967 a->config_info.remote_repo_path,
968 a->config_info.git_url,
969 a->config_info.fetch_all_branches,
970 a->config_info.mirror_references,
971 a->config_info.symrefs,
972 a->config_info.wanted_branches, a->repo);
975 a->configs_created = 1;
978 if (a->verbosity < 0)
981 if (message && message[0] != '\0') {
982 printf("\rserver: %s", message);
987 if (packfile_size > 0 || nobj_indexed > 0) {
988 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
989 (a->last_scaled_size[0] == '\0' ||
990 strcmp(scaled_size, a->last_scaled_size)) != 0) {
992 if (strlcpy(a->last_scaled_size, scaled_size,
993 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
994 return got_error(GOT_ERR_NO_SPACE);
996 if (nobj_indexed > 0) {
997 p_indexed = (nobj_indexed * 100) / nobj_total;
998 if (p_indexed != a->last_p_indexed) {
999 a->last_p_indexed = p_indexed;
1004 if (nobj_resolved > 0) {
1005 p_resolved = (nobj_resolved * 100) /
1006 (nobj_total - nobj_loose);
1007 if (p_resolved != a->last_p_resolved) {
1008 a->last_p_resolved = p_resolved;
1016 if (print_size || print_indexed || print_resolved)
1019 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1021 printf("; indexing %d%%", p_indexed);
1023 printf("; resolving deltas %d%%", p_resolved);
1024 if (print_size || print_indexed || print_resolved)
1030 static const struct got_error *
1031 create_symref(const char *refname, struct got_reference *target_ref,
1032 int verbosity, struct got_repository *repo)
1034 const struct got_error *err;
1035 struct got_reference *head_symref;
1037 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1041 err = got_ref_write(head_symref, repo);
1042 if (err == NULL && verbosity > 0) {
1043 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1044 got_ref_get_name(target_ref));
1046 got_ref_close(head_symref);
1050 static const struct got_error *
1051 list_remote_refs(struct got_pathlist_head *symrefs,
1052 struct got_pathlist_head *refs)
1054 const struct got_error *err;
1055 struct got_pathlist_entry *pe;
1057 TAILQ_FOREACH(pe, symrefs, entry) {
1058 const char *refname = pe->path;
1059 const char *targetref = pe->data;
1061 printf("%s: %s\n", refname, targetref);
1064 TAILQ_FOREACH(pe, refs, entry) {
1065 const char *refname = pe->path;
1066 struct got_object_id *id = pe->data;
1069 err = got_object_id_str(&id_str, id);
1072 printf("%s: %s\n", refname, id_str);
1079 static const struct got_error *
1080 create_ref(const char *refname, struct got_object_id *id,
1081 int verbosity, struct got_repository *repo)
1083 const struct got_error *err = NULL;
1084 struct got_reference *ref;
1087 err = got_object_id_str(&id_str, id);
1091 err = got_ref_alloc(&ref, refname, id);
1095 err = got_ref_write(ref, repo);
1098 if (err == NULL && verbosity >= 0)
1099 printf("Created reference %s: %s\n", refname, id_str);
1106 match_wanted_ref(const char *refname, const char *wanted_ref)
1108 if (strncmp(refname, "refs/", 5) != 0)
1113 * Prevent fetching of references that won't make any
1114 * sense outside of the remote repository's context.
1116 if (strncmp(refname, "got/", 4) == 0)
1118 if (strncmp(refname, "remotes/", 8) == 0)
1121 if (strncmp(wanted_ref, "refs/", 5) == 0)
1124 /* Allow prefix match. */
1125 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1128 /* Allow exact match. */
1129 return (strcmp(refname, wanted_ref) == 0);
1133 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1135 struct got_pathlist_entry *pe;
1137 TAILQ_FOREACH(pe, wanted_refs, entry) {
1138 if (match_wanted_ref(refname, pe->path))
1145 static const struct got_error *
1146 create_wanted_ref(const char *refname, struct got_object_id *id,
1147 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1149 const struct got_error *err;
1150 char *remote_refname;
1152 if (strncmp("refs/", refname, 5) == 0)
1155 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1156 remote_repo_name, refname) == -1)
1157 return got_error_from_errno("asprintf");
1159 err = create_ref(remote_refname, id, verbosity, repo);
1160 free(remote_refname);
1164 static const struct got_error *
1165 create_gotconfig(const char *proto, const char *host, const char *port,
1166 const char *remote_repo_path, const char *default_branch,
1167 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1168 int mirror_references, struct got_repository *repo)
1170 const struct got_error *err = NULL;
1171 char *gotconfig_path = NULL;
1172 char *gotconfig = NULL;
1173 FILE *gotconfig_file = NULL;
1174 const char *branchname = NULL;
1175 char *branches = NULL;
1178 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1179 struct got_pathlist_entry *pe;
1180 TAILQ_FOREACH(pe, wanted_branches, entry) {
1182 branchname = pe->path;
1183 if (strncmp(branchname, "refs/heads/", 11) == 0)
1185 if (asprintf(&s, "%s\"%s\" ",
1186 branches ? branches : "", branchname) == -1) {
1187 err = got_error_from_errno("asprintf");
1193 } else if (!fetch_all_branches && default_branch) {
1194 branchname = default_branch;
1195 if (strncmp(branchname, "refs/heads/", 11) == 0)
1197 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1198 err = got_error_from_errno("asprintf");
1203 /* Create got.conf(5). */
1204 gotconfig_path = got_repo_get_path_gotconfig(repo);
1205 if (gotconfig_path == NULL) {
1206 err = got_error_from_errno("got_repo_get_path_gotconfig");
1209 gotconfig_file = fopen(gotconfig_path, "a");
1210 if (gotconfig_file == NULL) {
1211 err = got_error_from_errno2("fopen", gotconfig_path);
1214 if (asprintf(&gotconfig,
1219 "\trepository \"%s\"\n"
1224 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1225 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1226 remote_repo_path, branches ? "\tbranch { " : "",
1227 branches ? branches : "", branches ? "}\n" : "",
1228 mirror_references ? "\tmirror-references yes\n" : "",
1229 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1230 err = got_error_from_errno("asprintf");
1233 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1234 if (n != strlen(gotconfig)) {
1235 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1240 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1241 err = got_error_from_errno2("fclose", gotconfig_path);
1242 free(gotconfig_path);
1247 static const struct got_error *
1248 create_gitconfig(const char *git_url, const char *default_branch,
1249 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1250 int mirror_references, struct got_repository *repo)
1252 const struct got_error *err = NULL;
1253 char *gitconfig_path = NULL;
1254 char *gitconfig = NULL;
1255 FILE *gitconfig_file = NULL;
1256 char *branches = NULL;
1257 const char *branchname, *mirror = NULL;
1260 /* Create a config file Git can understand. */
1261 gitconfig_path = got_repo_get_path_gitconfig(repo);
1262 if (gitconfig_path == NULL) {
1263 err = got_error_from_errno("got_repo_get_path_gitconfig");
1266 gitconfig_file = fopen(gitconfig_path, "a");
1267 if (gitconfig_file == NULL) {
1268 err = got_error_from_errno2("fopen", gitconfig_path);
1271 if (mirror_references) {
1272 mirror = "\tmirror = true\n";
1273 branches = strdup("\tfetch = +refs/*:refs/*\n");
1274 if (branches == NULL) {
1275 err = got_error_from_errno("strdup");
1278 } else if (fetch_all_branches) {
1279 if (asprintf(&branches,
1280 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1281 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1282 err = got_error_from_errno("asprintf");
1285 } else if (!TAILQ_EMPTY(wanted_branches)) {
1286 struct got_pathlist_entry *pe;
1287 TAILQ_FOREACH(pe, wanted_branches, entry) {
1289 branchname = pe->path;
1290 if (strncmp(branchname, "refs/heads/", 11) == 0)
1293 "%s\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1294 branches ? branches : "",
1295 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1296 branchname) == -1) {
1297 err = got_error_from_errno("asprintf");
1305 * If the server specified a default branch, use just that one.
1306 * Otherwise fall back to fetching all branches on next fetch.
1308 if (default_branch) {
1309 branchname = default_branch;
1310 if (strncmp(branchname, "refs/heads/", 11) == 0)
1313 branchname = "*"; /* fall back to all branches */
1314 if (asprintf(&branches,
1315 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1316 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1317 branchname) == -1) {
1318 err = got_error_from_errno("asprintf");
1322 if (asprintf(&gitconfig,
1327 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1328 mirror ? mirror : "") == -1) {
1329 err = got_error_from_errno("asprintf");
1332 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1333 if (n != strlen(gitconfig)) {
1334 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1338 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1339 err = got_error_from_errno2("fclose", gitconfig_path);
1340 free(gitconfig_path);
1345 static const struct got_error *
1346 create_config_files(const char *proto, const char *host, const char *port,
1347 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1348 int mirror_references, struct got_pathlist_head *symrefs,
1349 struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1351 const struct got_error *err = NULL;
1352 const char *default_branch = NULL;
1353 struct got_pathlist_entry *pe;
1356 * If we asked for a set of wanted branches then use the first
1359 if (!TAILQ_EMPTY(wanted_branches)) {
1360 pe = TAILQ_FIRST(wanted_branches);
1361 default_branch = pe->path;
1363 /* First HEAD ref listed by server is the default branch. */
1364 TAILQ_FOREACH(pe, symrefs, entry) {
1365 const char *refname = pe->path;
1366 const char *target = pe->data;
1368 if (strcmp(refname, GOT_REF_HEAD) != 0)
1371 default_branch = target;
1376 /* Create got.conf(5). */
1377 err = create_gotconfig(proto, host, port, remote_repo_path,
1378 default_branch, fetch_all_branches, wanted_branches,
1379 mirror_references, repo);
1383 /* Create a config file Git can understand. */
1384 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1385 wanted_branches, mirror_references, repo);
1388 static const struct got_error *
1389 cmd_clone(int argc, char *argv[])
1391 const struct got_error *error = NULL;
1392 const char *uri, *dirname;
1393 char *proto, *host, *port, *repo_name, *server_path;
1394 char *default_destdir = NULL, *id_str = NULL;
1395 const char *repo_path;
1396 struct got_repository *repo = NULL;
1397 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1398 struct got_pathlist_entry *pe;
1399 struct got_object_id *pack_hash = NULL;
1400 int ch, fetchfd = -1, fetchstatus;
1401 pid_t fetchpid = -1;
1402 struct got_fetch_progress_arg fpa;
1403 char *git_url = NULL;
1404 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1405 int list_refs_only = 0;
1408 TAILQ_INIT(&symrefs);
1409 TAILQ_INIT(&wanted_branches);
1410 TAILQ_INIT(&wanted_refs);
1412 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1415 fetch_all_branches = 1;
1418 error = got_pathlist_append(&wanted_branches,
1427 mirror_references = 1;
1432 else if (verbosity < 3)
1439 error = got_pathlist_append(&wanted_refs,
1452 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1453 option_conflict('a', 'b');
1454 if (list_refs_only) {
1455 if (!TAILQ_EMPTY(&wanted_branches))
1456 option_conflict('l', 'b');
1457 if (fetch_all_branches)
1458 option_conflict('l', 'a');
1459 if (mirror_references)
1460 option_conflict('l', 'm');
1461 if (verbosity == -1)
1462 option_conflict('l', 'q');
1463 if (!TAILQ_EMPTY(&wanted_refs))
1464 option_conflict('l', 'R');
1476 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1481 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1482 host, port ? ":" : "", port ? port : "",
1483 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1484 error = got_error_from_errno("asprintf");
1488 if (strcmp(proto, "git") == 0) {
1490 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1491 "sendfd dns inet unveil", NULL) == -1)
1494 } else if (strcmp(proto, "git+ssh") == 0 ||
1495 strcmp(proto, "ssh") == 0) {
1497 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1498 "sendfd unveil", NULL) == -1)
1501 } else if (strcmp(proto, "http") == 0 ||
1502 strcmp(proto, "git+http") == 0) {
1503 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1506 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1509 if (dirname == NULL) {
1510 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1511 error = got_error_from_errno("asprintf");
1514 repo_path = default_destdir;
1516 repo_path = dirname;
1518 if (!list_refs_only) {
1519 error = got_path_mkdir(repo_path);
1521 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1522 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1524 if (!got_path_dir_is_empty(repo_path)) {
1525 error = got_error_path(repo_path,
1526 GOT_ERR_DIR_NOT_EMPTY);
1531 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1532 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1533 error = got_error_from_errno2("unveil",
1534 GOT_FETCH_PATH_SSH);
1538 error = apply_unveil(repo_path, 0, NULL);
1543 printf("Connecting to %s%s%s\n", host,
1544 port ? ":" : "", port ? port : "");
1546 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1547 server_path, verbosity);
1551 if (!list_refs_only) {
1552 error = got_repo_init(repo_path);
1555 error = got_repo_open(&repo, repo_path, NULL);
1560 fpa.last_scaled_size[0] = '\0';
1561 fpa.last_p_indexed = -1;
1562 fpa.last_p_resolved = -1;
1563 fpa.verbosity = verbosity;
1564 fpa.create_configs = 1;
1565 fpa.configs_created = 0;
1567 fpa.config_info.symrefs = &symrefs;
1568 fpa.config_info.wanted_branches = &wanted_branches;
1569 fpa.config_info.proto = proto;
1570 fpa.config_info.host = host;
1571 fpa.config_info.port = port;
1572 fpa.config_info.remote_repo_path = server_path;
1573 fpa.config_info.git_url = git_url;
1574 fpa.config_info.fetch_all_branches = fetch_all_branches;
1575 fpa.config_info.mirror_references = mirror_references;
1576 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1577 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1578 fetch_all_branches, &wanted_branches, &wanted_refs,
1579 list_refs_only, verbosity, fetchfd, repo,
1580 fetch_progress, &fpa);
1584 if (list_refs_only) {
1585 error = list_remote_refs(&symrefs, &refs);
1589 error = got_object_id_str(&id_str, pack_hash);
1593 printf("\nFetched %s.pack\n", id_str);
1596 /* Set up references provided with the pack file. */
1597 TAILQ_FOREACH(pe, &refs, entry) {
1598 const char *refname = pe->path;
1599 struct got_object_id *id = pe->data;
1600 char *remote_refname;
1602 if (is_wanted_ref(&wanted_refs, refname) &&
1603 !mirror_references) {
1604 error = create_wanted_ref(refname, id,
1605 GOT_FETCH_DEFAULT_REMOTE_NAME,
1606 verbosity - 1, repo);
1612 error = create_ref(refname, id, verbosity - 1, repo);
1616 if (mirror_references)
1619 if (strncmp("refs/heads/", refname, 11) != 0)
1622 if (asprintf(&remote_refname,
1623 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1624 refname + 11) == -1) {
1625 error = got_error_from_errno("asprintf");
1628 error = create_ref(remote_refname, id, verbosity - 1, repo);
1629 free(remote_refname);
1634 /* Set the HEAD reference if the server provided one. */
1635 TAILQ_FOREACH(pe, &symrefs, entry) {
1636 struct got_reference *target_ref;
1637 const char *refname = pe->path;
1638 const char *target = pe->data;
1639 char *remote_refname = NULL, *remote_target = NULL;
1641 if (strcmp(refname, GOT_REF_HEAD) != 0)
1644 error = got_ref_open(&target_ref, repo, target, 0);
1646 if (error->code == GOT_ERR_NOT_REF) {
1653 error = create_symref(refname, target_ref, verbosity, repo);
1654 got_ref_close(target_ref);
1658 if (mirror_references)
1661 if (strncmp("refs/heads/", target, 11) != 0)
1664 if (asprintf(&remote_refname,
1665 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1667 error = got_error_from_errno("asprintf");
1670 if (asprintf(&remote_target,
1671 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1672 target + 11) == -1) {
1673 error = got_error_from_errno("asprintf");
1674 free(remote_refname);
1677 error = got_ref_open(&target_ref, repo, remote_target, 0);
1679 free(remote_refname);
1680 free(remote_target);
1681 if (error->code == GOT_ERR_NOT_REF) {
1687 error = create_symref(remote_refname, target_ref,
1688 verbosity - 1, repo);
1689 free(remote_refname);
1690 free(remote_target);
1691 got_ref_close(target_ref);
1697 * We failed to set the HEAD reference. If we asked for
1698 * a set of wanted branches use the first of one of those
1699 * which could be fetched instead.
1701 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1702 const char *target = pe->path;
1703 struct got_reference *target_ref;
1705 error = got_ref_open(&target_ref, repo, target, 0);
1707 if (error->code == GOT_ERR_NOT_REF) {
1714 error = create_symref(GOT_REF_HEAD, target_ref,
1716 got_ref_close(target_ref);
1724 printf("Created %s repository '%s'\n",
1725 mirror_references ? "mirrored" : "cloned", repo_path);
1728 if (kill(fetchpid, SIGTERM) == -1)
1729 error = got_error_from_errno("kill");
1730 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1731 error = got_error_from_errno("waitpid");
1733 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1734 error = got_error_from_errno("close");
1736 got_repo_close(repo);
1737 TAILQ_FOREACH(pe, &refs, entry) {
1738 free((void *)pe->path);
1741 got_pathlist_free(&refs);
1742 TAILQ_FOREACH(pe, &symrefs, entry) {
1743 free((void *)pe->path);
1746 got_pathlist_free(&symrefs);
1747 got_pathlist_free(&wanted_branches);
1748 got_pathlist_free(&wanted_refs);
1755 free(default_destdir);
1760 static const struct got_error *
1761 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1762 int replace_tags, int verbosity, struct got_repository *repo)
1764 const struct got_error *err = NULL;
1765 char *new_id_str = NULL;
1766 struct got_object_id *old_id = NULL;
1768 err = got_object_id_str(&new_id_str, new_id);
1772 if (!replace_tags &&
1773 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1774 err = got_ref_resolve(&old_id, repo, ref);
1777 if (got_object_id_cmp(old_id, new_id) == 0)
1779 if (verbosity >= 0) {
1780 printf("Rejecting update of existing tag %s: %s\n",
1781 got_ref_get_name(ref), new_id_str);
1786 if (got_ref_is_symbolic(ref)) {
1787 if (verbosity >= 0) {
1788 printf("Replacing reference %s: %s\n",
1789 got_ref_get_name(ref),
1790 got_ref_get_symref_target(ref));
1792 err = got_ref_change_symref_to_ref(ref, new_id);
1795 err = got_ref_write(ref, repo);
1799 err = got_ref_resolve(&old_id, repo, ref);
1802 if (got_object_id_cmp(old_id, new_id) == 0)
1805 err = got_ref_change_ref(ref, new_id);
1808 err = got_ref_write(ref, repo);
1814 printf("Updated %s: %s\n", got_ref_get_name(ref),
1822 static const struct got_error *
1823 update_symref(const char *refname, struct got_reference *target_ref,
1824 int verbosity, struct got_repository *repo)
1826 const struct got_error *err = NULL, *unlock_err;
1827 struct got_reference *symref;
1828 int symref_is_locked = 0;
1830 err = got_ref_open(&symref, repo, refname, 1);
1832 if (err->code != GOT_ERR_NOT_REF)
1834 err = got_ref_alloc_symref(&symref, refname, target_ref);
1838 err = got_ref_write(symref, repo);
1843 printf("Created reference %s: %s\n",
1844 got_ref_get_name(symref),
1845 got_ref_get_symref_target(symref));
1847 symref_is_locked = 1;
1849 if (strcmp(got_ref_get_symref_target(symref),
1850 got_ref_get_name(target_ref)) == 0)
1853 err = got_ref_change_symref(symref,
1854 got_ref_get_name(target_ref));
1858 err = got_ref_write(symref, repo);
1863 printf("Updated %s: %s\n", got_ref_get_name(symref),
1864 got_ref_get_symref_target(symref));
1868 if (symref_is_locked) {
1869 unlock_err = got_ref_unlock(symref);
1870 if (unlock_err && err == NULL)
1873 got_ref_close(symref);
1880 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1881 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1882 "[remote-repository-name]\n",
1887 static const struct got_error *
1888 delete_missing_ref(struct got_reference *ref,
1889 int verbosity, struct got_repository *repo)
1891 const struct got_error *err = NULL;
1892 struct got_object_id *id = NULL;
1893 char *id_str = NULL;
1895 if (got_ref_is_symbolic(ref)) {
1896 err = got_ref_delete(ref, repo);
1899 if (verbosity >= 0) {
1900 printf("Deleted reference %s: %s\n",
1901 got_ref_get_name(ref),
1902 got_ref_get_symref_target(ref));
1905 err = got_ref_resolve(&id, repo, ref);
1908 err = got_object_id_str(&id_str, id);
1912 err = got_ref_delete(ref, repo);
1915 if (verbosity >= 0) {
1916 printf("Deleted reference %s: %s\n",
1917 got_ref_get_name(ref), id_str);
1926 static const struct got_error *
1927 delete_missing_refs(struct got_pathlist_head *their_refs,
1928 struct got_pathlist_head *their_symrefs,
1929 const struct got_remote_repo *remote,
1930 int verbosity, struct got_repository *repo)
1932 const struct got_error *err = NULL, *unlock_err;
1933 struct got_reflist_head my_refs;
1934 struct got_reflist_entry *re;
1935 struct got_pathlist_entry *pe;
1936 char *remote_namespace = NULL;
1937 char *local_refname = NULL;
1939 TAILQ_INIT(&my_refs);
1941 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1943 return got_error_from_errno("asprintf");
1945 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1949 TAILQ_FOREACH(re, &my_refs, entry) {
1950 const char *refname = got_ref_get_name(re->ref);
1952 if (!remote->mirror_references) {
1953 if (strncmp(refname, remote_namespace,
1954 strlen(remote_namespace)) == 0) {
1955 if (strcmp(refname + strlen(remote_namespace),
1958 if (asprintf(&local_refname, "refs/heads/%s",
1959 refname + strlen(remote_namespace)) == -1) {
1960 err = got_error_from_errno("asprintf");
1963 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1967 TAILQ_FOREACH(pe, their_refs, entry) {
1968 if (strcmp(local_refname, pe->path) == 0)
1974 TAILQ_FOREACH(pe, their_symrefs, entry) {
1975 if (strcmp(local_refname, pe->path) == 0)
1981 err = delete_missing_ref(re->ref, verbosity, repo);
1985 if (local_refname) {
1986 struct got_reference *ref;
1987 err = got_ref_open(&ref, repo, local_refname, 1);
1989 if (err->code != GOT_ERR_NOT_REF)
1991 free(local_refname);
1992 local_refname = NULL;
1995 err = delete_missing_ref(ref, verbosity, repo);
1998 unlock_err = got_ref_unlock(ref);
2000 if (unlock_err && err == NULL) {
2005 free(local_refname);
2006 local_refname = NULL;
2010 free(remote_namespace);
2011 free(local_refname);
2015 static const struct got_error *
2016 update_wanted_ref(const char *refname, struct got_object_id *id,
2017 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2019 const struct got_error *err, *unlock_err;
2020 char *remote_refname;
2021 struct got_reference *ref;
2023 if (strncmp("refs/", refname, 5) == 0)
2026 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2027 remote_repo_name, refname) == -1)
2028 return got_error_from_errno("asprintf");
2030 err = got_ref_open(&ref, repo, remote_refname, 1);
2032 if (err->code != GOT_ERR_NOT_REF)
2034 err = create_ref(remote_refname, id, verbosity, repo);
2036 err = update_ref(ref, id, 0, verbosity, repo);
2037 unlock_err = got_ref_unlock(ref);
2038 if (unlock_err && err == NULL)
2043 free(remote_refname);
2047 static const struct got_error *
2048 cmd_fetch(int argc, char *argv[])
2050 const struct got_error *error = NULL, *unlock_err;
2051 char *cwd = NULL, *repo_path = NULL;
2052 const char *remote_name;
2053 char *proto = NULL, *host = NULL, *port = NULL;
2054 char *repo_name = NULL, *server_path = NULL;
2055 const struct got_remote_repo *remotes, *remote = NULL;
2057 char *id_str = NULL;
2058 struct got_repository *repo = NULL;
2059 struct got_worktree *worktree = NULL;
2060 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2061 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2062 struct got_pathlist_entry *pe;
2063 struct got_object_id *pack_hash = NULL;
2064 int i, ch, fetchfd = -1, fetchstatus;
2065 pid_t fetchpid = -1;
2066 struct got_fetch_progress_arg fpa;
2067 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2068 int delete_refs = 0, replace_tags = 0;
2071 TAILQ_INIT(&symrefs);
2072 TAILQ_INIT(&wanted_branches);
2073 TAILQ_INIT(&wanted_refs);
2075 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2078 fetch_all_branches = 1;
2081 error = got_pathlist_append(&wanted_branches,
2093 repo_path = realpath(optarg, NULL);
2094 if (repo_path == NULL)
2095 return got_error_from_errno2("realpath",
2097 got_path_strip_trailing_slashes(repo_path);
2105 else if (verbosity < 3)
2112 error = got_pathlist_append(&wanted_refs,
2125 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2126 option_conflict('a', 'b');
2127 if (list_refs_only) {
2128 if (!TAILQ_EMPTY(&wanted_branches))
2129 option_conflict('l', 'b');
2130 if (fetch_all_branches)
2131 option_conflict('l', 'a');
2133 option_conflict('l', 'd');
2137 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2139 remote_name = argv[0];
2143 cwd = getcwd(NULL, 0);
2145 error = got_error_from_errno("getcwd");
2149 if (repo_path == NULL) {
2150 error = got_worktree_open(&worktree, cwd);
2151 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2157 strdup(got_worktree_get_repo_path(worktree));
2158 if (repo_path == NULL)
2159 error = got_error_from_errno("strdup");
2163 repo_path = strdup(cwd);
2164 if (repo_path == NULL) {
2165 error = got_error_from_errno("strdup");
2171 error = got_repo_open(&repo, repo_path, NULL);
2176 worktree_conf = got_worktree_get_gotconfig(worktree);
2177 if (worktree_conf) {
2178 got_gotconfig_get_remotes(&nremotes, &remotes,
2180 for (i = 0; i < nremotes; i++) {
2181 if (strcmp(remotes[i].name, remote_name) == 0) {
2182 remote = &remotes[i];
2188 if (remote == NULL) {
2189 repo_conf = got_repo_get_gotconfig(repo);
2191 got_gotconfig_get_remotes(&nremotes, &remotes,
2193 for (i = 0; i < nremotes; i++) {
2194 if (strcmp(remotes[i].name, remote_name) == 0) {
2195 remote = &remotes[i];
2201 if (remote == NULL) {
2202 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2203 for (i = 0; i < nremotes; i++) {
2204 if (strcmp(remotes[i].name, remote_name) == 0) {
2205 remote = &remotes[i];
2210 if (remote == NULL) {
2211 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2215 if (TAILQ_EMPTY(&wanted_branches)) {
2216 if (!fetch_all_branches)
2217 fetch_all_branches = remote->fetch_all_branches;
2218 for (i = 0; i < remote->nbranches; i++) {
2219 got_pathlist_append(&wanted_branches,
2220 remote->branches[i], NULL);
2224 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2225 &repo_name, remote->url);
2229 if (strcmp(proto, "git") == 0) {
2231 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2232 "sendfd dns inet unveil", NULL) == -1)
2235 } else if (strcmp(proto, "git+ssh") == 0 ||
2236 strcmp(proto, "ssh") == 0) {
2238 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2239 "sendfd unveil", NULL) == -1)
2242 } else if (strcmp(proto, "http") == 0 ||
2243 strcmp(proto, "git+http") == 0) {
2244 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2247 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2251 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2252 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2253 error = got_error_from_errno2("unveil",
2254 GOT_FETCH_PATH_SSH);
2258 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2263 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2264 port ? ":" : "", port ? port : "");
2266 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2267 server_path, verbosity);
2271 fpa.last_scaled_size[0] = '\0';
2272 fpa.last_p_indexed = -1;
2273 fpa.last_p_resolved = -1;
2274 fpa.verbosity = verbosity;
2276 fpa.create_configs = 0;
2277 fpa.configs_created = 0;
2278 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2279 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2280 remote->mirror_references, fetch_all_branches, &wanted_branches,
2281 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2282 fetch_progress, &fpa);
2286 if (list_refs_only) {
2287 error = list_remote_refs(&symrefs, &refs);
2291 if (pack_hash == NULL) {
2293 printf("Already up-to-date\n");
2294 } else if (verbosity >= 0) {
2295 error = got_object_id_str(&id_str, pack_hash);
2298 printf("\nFetched %s.pack\n", id_str);
2303 /* Update references provided with the pack file. */
2304 TAILQ_FOREACH(pe, &refs, entry) {
2305 const char *refname = pe->path;
2306 struct got_object_id *id = pe->data;
2307 struct got_reference *ref;
2308 char *remote_refname;
2310 if (is_wanted_ref(&wanted_refs, refname) &&
2311 !remote->mirror_references) {
2312 error = update_wanted_ref(refname, id,
2313 remote->name, verbosity, repo);
2319 if (remote->mirror_references ||
2320 strncmp("refs/tags/", refname, 10) == 0) {
2321 error = got_ref_open(&ref, repo, refname, 1);
2323 if (error->code != GOT_ERR_NOT_REF)
2325 error = create_ref(refname, id, verbosity,
2330 error = update_ref(ref, id, replace_tags,
2332 unlock_err = got_ref_unlock(ref);
2333 if (unlock_err && error == NULL)
2339 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2340 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2341 remote_name, refname + 11) == -1) {
2342 error = got_error_from_errno("asprintf");
2346 error = got_ref_open(&ref, repo, remote_refname, 1);
2348 if (error->code != GOT_ERR_NOT_REF)
2350 error = create_ref(remote_refname, id,
2355 error = update_ref(ref, id, replace_tags,
2357 unlock_err = got_ref_unlock(ref);
2358 if (unlock_err && error == NULL)
2365 /* Also create a local branch if none exists yet. */
2366 error = got_ref_open(&ref, repo, refname, 1);
2368 if (error->code != GOT_ERR_NOT_REF)
2370 error = create_ref(refname, id, verbosity,
2375 unlock_err = got_ref_unlock(ref);
2376 if (unlock_err && error == NULL)
2383 error = delete_missing_refs(&refs, &symrefs, remote,
2389 if (!remote->mirror_references) {
2390 /* Update remote HEAD reference if the server provided one. */
2391 TAILQ_FOREACH(pe, &symrefs, entry) {
2392 struct got_reference *target_ref;
2393 const char *refname = pe->path;
2394 const char *target = pe->data;
2395 char *remote_refname = NULL, *remote_target = NULL;
2397 if (strcmp(refname, GOT_REF_HEAD) != 0)
2400 if (strncmp("refs/heads/", target, 11) != 0)
2403 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2404 remote->name, refname) == -1) {
2405 error = got_error_from_errno("asprintf");
2408 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2409 remote->name, target + 11) == -1) {
2410 error = got_error_from_errno("asprintf");
2411 free(remote_refname);
2415 error = got_ref_open(&target_ref, repo, remote_target,
2418 free(remote_refname);
2419 free(remote_target);
2420 if (error->code == GOT_ERR_NOT_REF) {
2426 error = update_symref(remote_refname, target_ref,
2428 free(remote_refname);
2429 free(remote_target);
2430 got_ref_close(target_ref);
2437 if (kill(fetchpid, SIGTERM) == -1)
2438 error = got_error_from_errno("kill");
2439 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2440 error = got_error_from_errno("waitpid");
2442 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2443 error = got_error_from_errno("close");
2445 got_repo_close(repo);
2447 got_worktree_close(worktree);
2448 TAILQ_FOREACH(pe, &refs, entry) {
2449 free((void *)pe->path);
2452 got_pathlist_free(&refs);
2453 TAILQ_FOREACH(pe, &symrefs, entry) {
2454 free((void *)pe->path);
2457 got_pathlist_free(&symrefs);
2458 got_pathlist_free(&wanted_branches);
2459 got_pathlist_free(&wanted_refs);
2474 usage_checkout(void)
2476 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2477 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2482 show_worktree_base_ref_warning(void)
2484 fprintf(stderr, "%s: warning: could not create a reference "
2485 "to the work tree's base commit; the commit could be "
2486 "garbage-collected by Git; making the repository "
2487 "writable and running 'got update' will prevent this\n",
2491 struct got_checkout_progress_arg {
2492 const char *worktree_path;
2493 int had_base_commit_ref_error;
2496 static const struct got_error *
2497 checkout_progress(void *arg, unsigned char status, const char *path)
2499 struct got_checkout_progress_arg *a = arg;
2501 /* Base commit bump happens silently. */
2502 if (status == GOT_STATUS_BUMP_BASE)
2505 if (status == GOT_STATUS_BASE_REF_ERR) {
2506 a->had_base_commit_ref_error = 1;
2510 while (path[0] == '/')
2513 printf("%c %s/%s\n", status, a->worktree_path, path);
2517 static const struct got_error *
2518 check_cancelled(void *arg)
2520 if (sigint_received || sigpipe_received)
2521 return got_error(GOT_ERR_CANCELLED);
2525 static const struct got_error *
2526 check_linear_ancestry(struct got_object_id *commit_id,
2527 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2528 struct got_repository *repo)
2530 const struct got_error *err = NULL;
2531 struct got_object_id *yca_id;
2533 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2534 commit_id, base_commit_id, repo, check_cancelled, NULL);
2539 return got_error(GOT_ERR_ANCESTRY);
2542 * Require a straight line of history between the target commit
2543 * and the work tree's base commit.
2545 * Non-linear situations such as this require a rebase:
2547 * (commit) D F (base_commit)
2555 * 'got update' only handles linear cases:
2556 * Update forwards in time: A (base/yca) - B - C - D (commit)
2557 * Update backwards in time: D (base) - C - B - A (commit/yca)
2559 if (allow_forwards_in_time_only) {
2560 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2561 return got_error(GOT_ERR_ANCESTRY);
2562 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2563 got_object_id_cmp(base_commit_id, yca_id) != 0)
2564 return got_error(GOT_ERR_ANCESTRY);
2570 static const struct got_error *
2571 check_same_branch(struct got_object_id *commit_id,
2572 struct got_reference *head_ref, struct got_object_id *yca_id,
2573 struct got_repository *repo)
2575 const struct got_error *err = NULL;
2576 struct got_commit_graph *graph = NULL;
2577 struct got_object_id *head_commit_id = NULL;
2578 int is_same_branch = 0;
2580 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2584 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2588 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2593 err = got_commit_graph_open(&graph, "/", 1);
2597 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2598 check_cancelled, NULL);
2603 struct got_object_id *id;
2604 err = got_commit_graph_iter_next(&id, graph, repo,
2605 check_cancelled, NULL);
2607 if (err->code == GOT_ERR_ITER_COMPLETED)
2613 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2615 if (got_object_id_cmp(id, commit_id) == 0) {
2623 got_commit_graph_close(graph);
2624 free(head_commit_id);
2625 if (!err && !is_same_branch)
2626 err = got_error(GOT_ERR_ANCESTRY);
2630 static const struct got_error *
2631 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2633 static char msg[512];
2634 const char *branch_name;
2636 if (got_ref_is_symbolic(ref))
2637 branch_name = got_ref_get_symref_target(ref);
2639 branch_name = got_ref_get_name(ref);
2641 if (strncmp("refs/heads/", branch_name, 11) == 0)
2644 snprintf(msg, sizeof(msg),
2645 "target commit is not contained in branch '%s'; "
2646 "the branch to use must be specified with -b; "
2647 "if necessary a new branch can be created for "
2648 "this commit with 'got branch -c %s BRANCH_NAME'",
2649 branch_name, commit_id_str);
2651 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2654 static const struct got_error *
2655 cmd_checkout(int argc, char *argv[])
2657 const struct got_error *error = NULL;
2658 struct got_repository *repo = NULL;
2659 struct got_reference *head_ref = NULL;
2660 struct got_worktree *worktree = NULL;
2661 char *repo_path = NULL;
2662 char *worktree_path = NULL;
2663 const char *path_prefix = "";
2664 const char *branch_name = GOT_REF_HEAD;
2665 char *commit_id_str = NULL;
2667 int ch, same_path_prefix, allow_nonempty = 0;
2668 struct got_pathlist_head paths;
2669 struct got_checkout_progress_arg cpa;
2673 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2676 branch_name = optarg;
2679 commit_id_str = strdup(optarg);
2680 if (commit_id_str == NULL)
2681 return got_error_from_errno("strdup");
2687 path_prefix = optarg;
2699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2700 "unveil", NULL) == -1)
2704 char *base, *dotgit;
2706 repo_path = realpath(argv[0], NULL);
2707 if (repo_path == NULL)
2708 return got_error_from_errno2("realpath", argv[0]);
2709 cwd = getcwd(NULL, 0);
2711 error = got_error_from_errno("getcwd");
2718 error = got_path_basename(&base, path);
2721 dotgit = strstr(base, ".git");
2724 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2725 error = got_error_from_errno("asprintf");
2730 } else if (argc == 2) {
2731 repo_path = realpath(argv[0], NULL);
2732 if (repo_path == NULL) {
2733 error = got_error_from_errno2("realpath", argv[0]);
2736 worktree_path = realpath(argv[1], NULL);
2737 if (worktree_path == NULL) {
2738 if (errno != ENOENT) {
2739 error = got_error_from_errno2("realpath",
2743 worktree_path = strdup(argv[1]);
2744 if (worktree_path == NULL) {
2745 error = got_error_from_errno("strdup");
2752 got_path_strip_trailing_slashes(repo_path);
2753 got_path_strip_trailing_slashes(worktree_path);
2755 error = got_repo_open(&repo, repo_path, NULL);
2759 /* Pre-create work tree path for unveil(2) */
2760 error = got_path_mkdir(worktree_path);
2762 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2763 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2765 if (!allow_nonempty &&
2766 !got_path_dir_is_empty(worktree_path)) {
2767 error = got_error_path(worktree_path,
2768 GOT_ERR_DIR_NOT_EMPTY);
2773 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2777 error = got_ref_open(&head_ref, repo, branch_name, 0);
2781 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2782 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2785 error = got_worktree_open(&worktree, worktree_path);
2789 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2793 if (!same_path_prefix) {
2794 error = got_error(GOT_ERR_PATH_PREFIX);
2798 if (commit_id_str) {
2799 struct got_object_id *commit_id;
2800 struct got_reflist_head refs;
2802 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2806 error = got_repo_match_object_id(&commit_id, NULL,
2807 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2808 got_ref_list_free(&refs);
2811 error = check_linear_ancestry(commit_id,
2812 got_worktree_get_base_commit_id(worktree), 0, repo);
2813 if (error != NULL) {
2815 if (error->code == GOT_ERR_ANCESTRY) {
2816 error = checkout_ancestry_error(
2817 head_ref, commit_id_str);
2821 error = check_same_branch(commit_id, head_ref, NULL, repo);
2823 if (error->code == GOT_ERR_ANCESTRY) {
2824 error = checkout_ancestry_error(
2825 head_ref, commit_id_str);
2829 error = got_worktree_set_base_commit_id(worktree, repo,
2836 error = got_pathlist_append(&paths, "", NULL);
2839 cpa.worktree_path = worktree_path;
2840 cpa.had_base_commit_ref_error = 0;
2841 error = got_worktree_checkout_files(worktree, &paths, repo,
2842 checkout_progress, &cpa, check_cancelled, NULL);
2846 printf("Now shut up and hack\n");
2847 if (cpa.had_base_commit_ref_error)
2848 show_worktree_base_ref_warning();
2850 got_pathlist_free(&paths);
2851 free(commit_id_str);
2853 free(worktree_path);
2858 struct got_update_progress_arg {
2866 print_update_progress_stats(struct got_update_progress_arg *upa)
2868 if (!upa->did_something)
2871 if (upa->conflicts > 0)
2872 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2873 if (upa->obstructed > 0)
2874 printf("File paths obstructed by a non-regular file: %d\n",
2876 if (upa->not_updated > 0)
2877 printf("Files not updated because of existing merge "
2878 "conflicts: %d\n", upa->not_updated);
2884 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2889 static const struct got_error *
2890 update_progress(void *arg, unsigned char status, const char *path)
2892 struct got_update_progress_arg *upa = arg;
2894 if (status == GOT_STATUS_EXISTS ||
2895 status == GOT_STATUS_BASE_REF_ERR)
2898 upa->did_something = 1;
2900 /* Base commit bump happens silently. */
2901 if (status == GOT_STATUS_BUMP_BASE)
2904 if (status == GOT_STATUS_CONFLICT)
2906 if (status == GOT_STATUS_OBSTRUCTED)
2908 if (status == GOT_STATUS_CANNOT_UPDATE)
2911 while (path[0] == '/')
2913 printf("%c %s\n", status, path);
2917 static const struct got_error *
2918 switch_head_ref(struct got_reference *head_ref,
2919 struct got_object_id *commit_id, struct got_worktree *worktree,
2920 struct got_repository *repo)
2922 const struct got_error *err = NULL;
2924 int ref_has_moved = 0;
2926 /* Trivial case: switching between two different references. */
2927 if (strcmp(got_ref_get_name(head_ref),
2928 got_worktree_get_head_ref_name(worktree)) != 0) {
2929 printf("Switching work tree from %s to %s\n",
2930 got_worktree_get_head_ref_name(worktree),
2931 got_ref_get_name(head_ref));
2932 return got_worktree_set_head_ref(worktree, head_ref);
2935 err = check_linear_ancestry(commit_id,
2936 got_worktree_get_base_commit_id(worktree), 0, repo);
2938 if (err->code != GOT_ERR_ANCESTRY)
2945 /* Switching to a rebased branch with the same reference name. */
2946 err = got_object_id_str(&base_id_str,
2947 got_worktree_get_base_commit_id(worktree));
2950 printf("Reference %s now points at a different branch\n",
2951 got_worktree_get_head_ref_name(worktree));
2952 printf("Switching work tree from %s to %s\n", base_id_str,
2953 got_worktree_get_head_ref_name(worktree));
2957 static const struct got_error *
2958 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2960 const struct got_error *err;
2963 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2967 return got_error(GOT_ERR_REBASING);
2969 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2973 return got_error(GOT_ERR_HISTEDIT_BUSY);
2978 static const struct got_error *
2979 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2980 char *argv[], struct got_worktree *worktree)
2982 const struct got_error *err = NULL;
2989 return got_error_from_errno("strdup");
2990 return got_pathlist_append(paths, path, NULL);
2993 for (i = 0; i < argc; i++) {
2994 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2997 err = got_pathlist_append(paths, path, NULL);
3007 static const struct got_error *
3008 wrap_not_worktree_error(const struct got_error *orig_err,
3009 const char *cmdname, const char *path)
3011 const struct got_error *err;
3012 struct got_repository *repo;
3013 static char msg[512];
3015 err = got_repo_open(&repo, path, NULL);
3019 snprintf(msg, sizeof(msg),
3020 "'got %s' needs a work tree in addition to a git repository\n"
3021 "Work trees can be checked out from this Git repository with "
3023 "The got(1) manual page contains more information.", cmdname);
3024 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3025 got_repo_close(repo);
3029 static const struct got_error *
3030 cmd_update(int argc, char *argv[])
3032 const struct got_error *error = NULL;
3033 struct got_repository *repo = NULL;
3034 struct got_worktree *worktree = NULL;
3035 char *worktree_path = NULL;
3036 struct got_object_id *commit_id = NULL;
3037 char *commit_id_str = NULL;
3038 const char *branch_name = NULL;
3039 struct got_reference *head_ref = NULL;
3040 struct got_pathlist_head paths;
3041 struct got_pathlist_entry *pe;
3043 struct got_update_progress_arg upa;
3047 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3050 branch_name = optarg;
3053 commit_id_str = strdup(optarg);
3054 if (commit_id_str == NULL)
3055 return got_error_from_errno("strdup");
3067 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3068 "unveil", NULL) == -1)
3071 worktree_path = getcwd(NULL, 0);
3072 if (worktree_path == NULL) {
3073 error = got_error_from_errno("getcwd");
3076 error = got_worktree_open(&worktree, worktree_path);
3078 if (error->code == GOT_ERR_NOT_WORKTREE)
3079 error = wrap_not_worktree_error(error, "update",
3084 error = check_rebase_or_histedit_in_progress(worktree);
3088 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3093 error = apply_unveil(got_repo_get_path(repo), 0,
3094 got_worktree_get_root_path(worktree));
3098 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3102 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3103 got_worktree_get_head_ref_name(worktree), 0);
3106 if (commit_id_str == NULL) {
3107 error = got_ref_resolve(&commit_id, repo, head_ref);
3110 error = got_object_id_str(&commit_id_str, commit_id);
3114 struct got_reflist_head refs;
3116 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3120 error = got_repo_match_object_id(&commit_id, NULL,
3121 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3122 got_ref_list_free(&refs);
3123 free(commit_id_str);
3124 commit_id_str = NULL;
3127 error = got_object_id_str(&commit_id_str, commit_id);
3133 struct got_object_id *head_commit_id;
3134 TAILQ_FOREACH(pe, &paths, entry) {
3135 if (pe->path_len == 0)
3137 error = got_error_msg(GOT_ERR_BAD_PATH,
3138 "switching between branches requires that "
3139 "the entire work tree gets updated");
3142 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3145 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3147 free(head_commit_id);
3150 error = check_same_branch(commit_id, head_ref, NULL, repo);
3153 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3157 error = check_linear_ancestry(commit_id,
3158 got_worktree_get_base_commit_id(worktree), 0, repo);
3159 if (error != NULL) {
3160 if (error->code == GOT_ERR_ANCESTRY)
3161 error = got_error(GOT_ERR_BRANCH_MOVED);
3164 error = check_same_branch(commit_id, head_ref, NULL, repo);
3169 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3171 error = got_worktree_set_base_commit_id(worktree, repo,
3177 memset(&upa, 0, sizeof(upa));
3178 error = got_worktree_checkout_files(worktree, &paths, repo,
3179 update_progress, &upa, check_cancelled, NULL);
3183 if (upa.did_something)
3184 printf("Updated to commit %s\n", commit_id_str);
3186 printf("Already up-to-date\n");
3187 print_update_progress_stats(&upa);
3189 free(worktree_path);
3190 TAILQ_FOREACH(pe, &paths, entry)
3191 free((char *)pe->path);
3192 got_pathlist_free(&paths);
3194 free(commit_id_str);
3198 static const struct got_error *
3199 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3200 const char *path, int diff_context, int ignore_whitespace,
3201 int force_text_diff, struct got_repository *repo)
3203 const struct got_error *err = NULL;
3204 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3207 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3212 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3216 while (path[0] == '/')
3218 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3219 diff_context, ignore_whitespace, force_text_diff, stdout);
3222 got_object_blob_close(blob1);
3223 got_object_blob_close(blob2);
3227 static const struct got_error *
3228 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3229 const char *path, int diff_context, int ignore_whitespace,
3230 int force_text_diff, struct got_repository *repo)
3232 const struct got_error *err = NULL;
3233 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3234 struct got_diff_blob_output_unidiff_arg arg;
3237 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3242 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3246 arg.diff_context = diff_context;
3247 arg.ignore_whitespace = ignore_whitespace;
3248 arg.force_text_diff = force_text_diff;
3249 arg.outfile = stdout;
3250 arg.line_offsets = NULL;
3252 while (path[0] == '/')
3254 err = got_diff_tree(tree1, tree2, path, path, repo,
3255 got_diff_blob_output_unidiff, &arg, 1);
3258 got_object_tree_close(tree1);
3260 got_object_tree_close(tree2);
3264 static const struct got_error *
3265 get_changed_paths(struct got_pathlist_head *paths,
3266 struct got_commit_object *commit, struct got_repository *repo)
3268 const struct got_error *err = NULL;
3269 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3270 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3271 struct got_object_qid *qid;
3273 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3275 struct got_commit_object *pcommit;
3276 err = got_object_open_as_commit(&pcommit, repo,
3281 tree_id1 = got_object_commit_get_tree_id(pcommit);
3282 got_object_commit_close(pcommit);
3287 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3292 tree_id2 = got_object_commit_get_tree_id(commit);
3293 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3297 err = got_diff_tree(tree1, tree2, "", "", repo,
3298 got_diff_tree_collect_changed_paths, paths, 0);
3301 got_object_tree_close(tree1);
3303 got_object_tree_close(tree2);
3307 static const struct got_error *
3308 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3309 const char *path, int diff_context, struct got_repository *repo)
3311 const struct got_error *err = NULL;
3312 struct got_commit_object *pcommit = NULL;
3313 char *id_str1 = NULL, *id_str2 = NULL;
3314 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3315 struct got_object_qid *qid;
3317 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3319 err = got_object_open_as_commit(&pcommit, repo,
3325 if (path && path[0] != '\0') {
3327 err = got_object_id_by_path(&obj_id2, repo, id, path);
3330 err = got_object_id_str(&id_str2, obj_id2);
3336 err = got_object_id_by_path(&obj_id1, repo,
3339 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3344 err = got_object_id_str(&id_str1, obj_id1);
3351 err = got_object_get_type(&obj_type, repo, obj_id2);
3356 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3358 case GOT_OBJ_TYPE_BLOB:
3359 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3362 case GOT_OBJ_TYPE_TREE:
3363 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3367 err = got_error(GOT_ERR_OBJ_TYPE);
3373 obj_id2 = got_object_commit_get_tree_id(commit);
3374 err = got_object_id_str(&id_str2, obj_id2);
3378 obj_id1 = got_object_commit_get_tree_id(pcommit);
3379 err = got_object_id_str(&id_str1, obj_id1);
3383 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3385 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3392 got_object_commit_close(pcommit);
3397 get_datestr(time_t *time, char *datebuf)
3399 struct tm mytm, *tm;
3402 tm = gmtime_r(time, &mytm);
3405 s = asctime_r(tm, datebuf);
3408 p = strchr(s, '\n');
3414 static const struct got_error *
3415 match_logmsg(int *have_match, struct got_object_id *id,
3416 struct got_commit_object *commit, regex_t *regex)
3418 const struct got_error *err = NULL;
3419 regmatch_t regmatch;
3420 char *id_str = NULL, *logmsg = NULL;
3424 err = got_object_id_str(&id_str, id);
3428 err = got_object_commit_get_logmsg(&logmsg, commit);
3432 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3441 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3444 regmatch_t regmatch;
3445 struct got_pathlist_entry *pe;
3449 TAILQ_FOREACH(pe, changed_paths, entry) {
3450 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3457 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3459 static const struct got_error*
3460 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3461 struct got_object_id *id, struct got_repository *repo)
3463 static const struct got_error *err = NULL;
3464 struct got_reflist_entry *re;
3470 TAILQ_FOREACH(re, refs, entry) {
3471 struct got_tag_object *tag = NULL;
3472 struct got_object_id *ref_id;
3475 name = got_ref_get_name(re->ref);
3476 if (strcmp(name, GOT_REF_HEAD) == 0)
3478 if (strncmp(name, "refs/", 5) == 0)
3480 if (strncmp(name, "got/", 4) == 0)
3482 if (strncmp(name, "heads/", 6) == 0)
3484 if (strncmp(name, "remotes/", 8) == 0) {
3486 s = strstr(name, "/" GOT_REF_HEAD);
3487 if (s != NULL && s[strlen(s)] == '\0')
3490 err = got_ref_resolve(&ref_id, repo, re->ref);
3493 if (strncmp(name, "tags/", 5) == 0) {
3494 err = got_object_open_as_tag(&tag, repo, ref_id);
3496 if (err->code != GOT_ERR_OBJ_TYPE) {
3500 /* Ref points at something other than a tag. */
3505 cmp = got_object_id_cmp(tag ?
3506 got_object_tag_get_object_id(tag) : ref_id, id);
3509 got_object_tag_close(tag);
3513 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3514 s ? ", " : "", name) == -1) {
3515 err = got_error_from_errno("asprintf");
3526 static const struct got_error *
3527 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3528 struct got_repository *repo, const char *path,
3529 struct got_pathlist_head *changed_paths, int show_patch,
3530 int diff_context, struct got_reflist_object_id_map *refs_idmap)
3532 const struct got_error *err = NULL;
3533 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3535 time_t committer_time;
3536 const char *author, *committer;
3537 char *refs_str = NULL;
3538 struct got_reflist_head *refs;
3540 err = got_object_id_str(&id_str, id);
3544 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3546 err = build_refs_str(&refs_str, refs, id, repo);
3551 printf(GOT_COMMIT_SEP_STR);
3552 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3553 refs_str ? refs_str : "", refs_str ? ")" : "");
3558 printf("from: %s\n", got_object_commit_get_author(commit));
3559 committer_time = got_object_commit_get_committer_time(commit);
3560 datestr = get_datestr(&committer_time, datebuf);
3562 printf("date: %s UTC\n", datestr);
3563 author = got_object_commit_get_author(commit);
3564 committer = got_object_commit_get_committer(commit);
3565 if (strcmp(author, committer) != 0)
3566 printf("via: %s\n", committer);
3567 if (got_object_commit_get_nparents(commit) > 1) {
3568 const struct got_object_id_queue *parent_ids;
3569 struct got_object_qid *qid;
3571 parent_ids = got_object_commit_get_parent_ids(commit);
3572 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3573 err = got_object_id_str(&id_str, qid->id);
3576 printf("parent %d: %s\n", n++, id_str);
3582 err = got_object_commit_get_logmsg(&logmsg0, commit);
3588 line = strsep(&logmsg, "\n");
3590 printf(" %s\n", line);
3594 if (changed_paths) {
3595 struct got_pathlist_entry *pe;
3596 TAILQ_FOREACH(pe, changed_paths, entry) {
3597 struct got_diff_changed_path *cp = pe->data;
3598 printf(" %c %s\n", cp->status, pe->path);
3603 err = print_patch(commit, id, path, diff_context, repo);
3608 if (fflush(stdout) != 0 && err == NULL)
3609 err = got_error_from_errno("fflush");
3616 static const struct got_error *
3617 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3618 struct got_repository *repo, const char *path, int show_changed_paths,
3619 int show_patch, const char *search_pattern, int diff_context, int limit,
3620 int log_branches, int reverse_display_order,
3621 struct got_reflist_object_id_map *refs_idmap)
3623 const struct got_error *err;
3624 struct got_commit_graph *graph;
3627 struct got_object_id_queue reversed_commits;
3628 struct got_object_qid *qid;
3629 struct got_commit_object *commit;
3630 struct got_pathlist_head changed_paths;
3631 struct got_pathlist_entry *pe;
3633 SIMPLEQ_INIT(&reversed_commits);
3634 TAILQ_INIT(&changed_paths);
3636 if (search_pattern && regcomp(®ex, search_pattern,
3637 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3638 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3640 err = got_commit_graph_open(&graph, path, !log_branches);
3643 err = got_commit_graph_iter_start(graph, root_id, repo,
3644 check_cancelled, NULL);
3648 struct got_object_id *id;
3650 if (sigint_received || sigpipe_received)
3653 err = got_commit_graph_iter_next(&id, graph, repo,
3654 check_cancelled, NULL);
3656 if (err->code == GOT_ERR_ITER_COMPLETED)
3663 err = got_object_open_as_commit(&commit, repo, id);
3667 if (show_changed_paths && !reverse_display_order) {
3668 err = get_changed_paths(&changed_paths, commit, repo);
3673 if (search_pattern) {
3674 err = match_logmsg(&have_match, id, commit, ®ex);
3676 got_object_commit_close(commit);
3679 if (have_match == 0 && show_changed_paths)
3680 match_changed_paths(&have_match,
3681 &changed_paths, ®ex);
3682 if (have_match == 0) {
3683 got_object_commit_close(commit);
3684 TAILQ_FOREACH(pe, &changed_paths, entry) {
3685 free((char *)pe->path);
3688 got_pathlist_free(&changed_paths);
3693 if (reverse_display_order) {
3694 err = got_object_qid_alloc(&qid, id);
3697 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3698 got_object_commit_close(commit);
3700 err = print_commit(commit, id, repo, path,
3701 show_changed_paths ? &changed_paths : NULL,
3702 show_patch, diff_context, refs_idmap);
3703 got_object_commit_close(commit);
3707 if ((limit && --limit == 0) ||
3708 (end_id && got_object_id_cmp(id, end_id) == 0))
3711 TAILQ_FOREACH(pe, &changed_paths, entry) {
3712 free((char *)pe->path);
3715 got_pathlist_free(&changed_paths);
3717 if (reverse_display_order) {
3718 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3719 err = got_object_open_as_commit(&commit, repo, qid->id);
3722 if (show_changed_paths) {
3723 err = get_changed_paths(&changed_paths,
3728 err = print_commit(commit, qid->id, repo, path,
3729 show_changed_paths ? &changed_paths : NULL,
3730 show_patch, diff_context, refs_idmap);
3731 got_object_commit_close(commit);
3734 TAILQ_FOREACH(pe, &changed_paths, entry) {
3735 free((char *)pe->path);
3738 got_pathlist_free(&changed_paths);
3742 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3743 qid = SIMPLEQ_FIRST(&reversed_commits);
3744 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3745 got_object_qid_free(qid);
3747 TAILQ_FOREACH(pe, &changed_paths, entry) {
3748 free((char *)pe->path);
3751 got_pathlist_free(&changed_paths);
3754 got_commit_graph_close(graph);
3761 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3762 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3763 "[-R] [path]\n", getprogname());
3768 get_default_log_limit(void)
3770 const char *got_default_log_limit;
3774 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3775 if (got_default_log_limit == NULL)
3777 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3783 static const struct got_error *
3784 cmd_log(int argc, char *argv[])
3786 const struct got_error *error;
3787 struct got_repository *repo = NULL;
3788 struct got_worktree *worktree = NULL;
3789 struct got_object_id *start_id = NULL, *end_id = NULL;
3790 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3791 const char *start_commit = NULL, *end_commit = NULL;
3792 const char *search_pattern = NULL;
3793 int diff_context = -1, ch;
3794 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3795 int reverse_display_order = 0;
3797 struct got_reflist_head refs;
3798 struct got_reflist_object_id_map *refs_idmap = NULL;
3803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3809 limit = get_default_log_limit();
3811 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3817 show_changed_paths = 1;
3820 start_commit = optarg;
3823 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3826 err(1, "-C option %s", errstr);
3829 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3831 err(1, "-l option %s", errstr);
3837 repo_path = realpath(optarg, NULL);
3838 if (repo_path == NULL)
3839 return got_error_from_errno2("realpath",
3841 got_path_strip_trailing_slashes(repo_path);
3844 reverse_display_order = 1;
3847 search_pattern = optarg;
3850 end_commit = optarg;
3861 if (diff_context == -1)
3863 else if (!show_patch)
3864 errx(1, "-C requires -p");
3866 cwd = getcwd(NULL, 0);
3868 error = got_error_from_errno("getcwd");
3872 if (repo_path == NULL) {
3873 error = got_worktree_open(&worktree, cwd);
3874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3881 error = got_worktree_resolve_path(&path, worktree,
3886 path = strdup(argv[0]);
3888 error = got_error_from_errno("strdup");
3892 } else if (argc != 0)
3895 if (repo_path == NULL) {
3896 repo_path = worktree ?
3897 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3899 if (repo_path == NULL) {
3900 error = got_error_from_errno("strdup");
3904 error = got_repo_open(&repo, repo_path, NULL);
3908 error = apply_unveil(got_repo_get_path(repo), 1,
3909 worktree ? got_worktree_get_root_path(worktree) : NULL);
3913 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3917 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3921 if (start_commit == NULL) {
3922 struct got_reference *head_ref;
3923 struct got_commit_object *commit = NULL;
3924 error = got_ref_open(&head_ref, repo,
3925 worktree ? got_worktree_get_head_ref_name(worktree)
3929 error = got_ref_resolve(&start_id, repo, head_ref);
3930 got_ref_close(head_ref);
3933 error = got_object_open_as_commit(&commit, repo,
3937 got_object_commit_close(commit);
3939 error = got_repo_match_object_id(&start_id, NULL,
3940 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3944 if (end_commit != NULL) {
3945 error = got_repo_match_object_id(&end_id, NULL,
3946 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3953 * If a path was specified on the command line it was resolved
3954 * to a path in the work tree above. Prepend the work tree's
3955 * path prefix to obtain the corresponding in-repository path.
3959 prefix = got_worktree_get_path_prefix(worktree);
3960 if (asprintf(&in_repo_path, "%s%s%s", prefix,
3961 (path[0] != '\0') ? "/" : "", path) == -1) {
3962 error = got_error_from_errno("asprintf");
3967 error = got_repo_map_path(&in_repo_path, repo,
3973 path = in_repo_path;
3976 error = print_commits(start_id, end_id, repo, path ? path : "",
3977 show_changed_paths, show_patch, search_pattern, diff_context,
3978 limit, log_branches, reverse_display_order, refs_idmap);
3984 got_worktree_close(worktree);
3986 const struct got_error *repo_error;
3987 repo_error = got_repo_close(repo);
3992 got_reflist_object_id_map_free(refs_idmap);
3993 got_ref_list_free(&refs);
4000 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4001 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4005 struct print_diff_arg {
4006 struct got_repository *repo;
4007 struct got_worktree *worktree;
4012 int ignore_whitespace;
4013 int force_text_diff;
4017 * Create a file which contains the target path of a symlink so we can feed
4018 * it as content to the diff engine.
4020 static const struct got_error *
4021 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4022 const char *abspath)
4024 const struct got_error *err = NULL;
4025 char target_path[PATH_MAX];
4026 ssize_t target_len, outlen;
4031 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4032 if (target_len == -1)
4033 return got_error_from_errno2("readlinkat", abspath);
4035 target_len = readlink(abspath, target_path, PATH_MAX);
4036 if (target_len == -1)
4037 return got_error_from_errno2("readlink", abspath);
4040 *fd = got_opentempfd();
4042 return got_error_from_errno("got_opentempfd");
4044 outlen = write(*fd, target_path, target_len);
4046 err = got_error_from_errno("got_opentempfd");
4050 if (lseek(*fd, 0, SEEK_SET) == -1) {
4051 err = got_error_from_errno2("lseek", abspath);
4062 static const struct got_error *
4063 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4064 const char *path, struct got_object_id *blob_id,
4065 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4066 int dirfd, const char *de_name)
4068 struct print_diff_arg *a = arg;
4069 const struct got_error *err = NULL;
4070 struct got_blob_object *blob1 = NULL;
4073 char *abspath = NULL, *label1 = NULL;
4076 if (a->diff_staged) {
4077 if (staged_status != GOT_STATUS_MODIFY &&
4078 staged_status != GOT_STATUS_ADD &&
4079 staged_status != GOT_STATUS_DELETE)
4082 if (staged_status == GOT_STATUS_DELETE)
4084 if (status == GOT_STATUS_NONEXISTENT)
4085 return got_error_set_errno(ENOENT, path);
4086 if (status != GOT_STATUS_MODIFY &&
4087 status != GOT_STATUS_ADD &&
4088 status != GOT_STATUS_DELETE &&
4089 status != GOT_STATUS_CONFLICT)
4093 if (!a->header_shown) {
4094 printf("diff %s %s%s\n", a->id_str,
4095 got_worktree_get_root_path(a->worktree),
4096 a->diff_staged ? " (staged changes)" : "");
4097 a->header_shown = 1;
4100 if (a->diff_staged) {
4101 const char *label1 = NULL, *label2 = NULL;
4102 switch (staged_status) {
4103 case GOT_STATUS_MODIFY:
4107 case GOT_STATUS_ADD:
4110 case GOT_STATUS_DELETE:
4114 return got_error(GOT_ERR_FILE_STATUS);
4116 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4117 staged_blob_id, label1, label2, a->diff_context,
4118 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4121 if (staged_status == GOT_STATUS_ADD ||
4122 staged_status == GOT_STATUS_MODIFY) {
4124 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4128 err = got_object_id_str(&id_str, staged_blob_id);
4131 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4132 err = got_error_from_errno("asprintf");
4137 } else if (status != GOT_STATUS_ADD) {
4138 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4143 if (status != GOT_STATUS_DELETE) {
4144 if (asprintf(&abspath, "%s/%s",
4145 got_worktree_get_root_path(a->worktree), path) == -1) {
4146 err = got_error_from_errno("asprintf");
4151 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4153 if (errno != ELOOP) {
4154 err = got_error_from_errno2("openat",
4158 err = get_symlink_target_file(&fd, dirfd,
4164 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4166 if (errno != ELOOP) {
4167 err = got_error_from_errno2("open",
4171 err = get_symlink_target_file(&fd, dirfd,
4177 if (fstat(fd, &sb) == -1) {
4178 err = got_error_from_errno2("fstat", abspath);
4181 f2 = fdopen(fd, "r");
4183 err = got_error_from_errno2("fdopen", abspath);
4190 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4191 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4194 got_object_blob_close(blob1);
4195 if (f2 && fclose(f2) == EOF && err == NULL)
4196 err = got_error_from_errno("fclose");
4197 if (fd != -1 && close(fd) == -1 && err == NULL)
4198 err = got_error_from_errno("close");
4203 static const struct got_error *
4204 cmd_diff(int argc, char *argv[])
4206 const struct got_error *error;
4207 struct got_repository *repo = NULL;
4208 struct got_worktree *worktree = NULL;
4209 char *cwd = NULL, *repo_path = NULL;
4210 struct got_object_id *id1 = NULL, *id2 = NULL;
4211 const char *id_str1 = NULL, *id_str2 = NULL;
4212 char *label1 = NULL, *label2 = NULL;
4214 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4215 int force_text_diff = 0;
4218 struct got_reflist_head refs;
4223 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4228 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4231 force_text_diff = 1;
4234 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4237 err(1, "-C option %s", errstr);
4240 repo_path = realpath(optarg, NULL);
4241 if (repo_path == NULL)
4242 return got_error_from_errno2("realpath",
4244 got_path_strip_trailing_slashes(repo_path);
4250 ignore_whitespace = 1;
4261 cwd = getcwd(NULL, 0);
4263 error = got_error_from_errno("getcwd");
4269 "-r option can't be used when diffing a work tree");
4270 error = got_worktree_open(&worktree, cwd);
4272 if (error->code == GOT_ERR_NOT_WORKTREE)
4273 error = wrap_not_worktree_error(error, "diff",
4277 repo_path = strdup(got_worktree_get_repo_path(worktree));
4278 if (repo_path == NULL) {
4279 error = got_error_from_errno("strdup");
4283 error = got_worktree_resolve_path(&path, worktree,
4290 error = got_error_from_errno("strdup");
4294 } else if (argc == 2) {
4296 errx(1, "-s option can't be used when diffing "
4297 "objects in repository");
4300 if (repo_path == NULL) {
4301 error = got_worktree_open(&worktree, cwd);
4302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4306 got_worktree_get_repo_path(worktree));
4307 if (repo_path == NULL) {
4308 error = got_error_from_errno("strdup");
4312 repo_path = strdup(cwd);
4313 if (repo_path == NULL) {
4314 error = got_error_from_errno("strdup");
4322 error = got_repo_open(&repo, repo_path, NULL);
4327 error = apply_unveil(got_repo_get_path(repo), 1,
4328 worktree ? got_worktree_get_root_path(worktree) : NULL);
4333 struct print_diff_arg arg;
4334 struct got_pathlist_head paths;
4339 error = got_object_id_str(&id_str,
4340 got_worktree_get_base_commit_id(worktree));
4344 arg.worktree = worktree;
4345 arg.diff_context = diff_context;
4346 arg.id_str = id_str;
4347 arg.header_shown = 0;
4348 arg.diff_staged = diff_staged;
4349 arg.ignore_whitespace = ignore_whitespace;
4350 arg.force_text_diff = force_text_diff;
4352 error = got_pathlist_append(&paths, path, NULL);
4356 error = got_worktree_status(worktree, &paths, repo, print_diff,
4357 &arg, check_cancelled, NULL);
4359 got_pathlist_free(&paths);
4363 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4367 error = got_repo_match_object_id(&id1, &label1, id_str1,
4368 GOT_OBJ_TYPE_ANY, &refs, repo);
4372 error = got_repo_match_object_id(&id2, &label2, id_str2,
4373 GOT_OBJ_TYPE_ANY, &refs, repo);
4377 error = got_object_get_type(&type1, repo, id1);
4381 error = got_object_get_type(&type2, repo, id2);
4385 if (type1 != type2) {
4386 error = got_error(GOT_ERR_OBJ_TYPE);
4391 case GOT_OBJ_TYPE_BLOB:
4392 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4393 NULL, NULL, diff_context, ignore_whitespace,
4394 force_text_diff, repo, stdout);
4396 case GOT_OBJ_TYPE_TREE:
4397 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4398 "", "", diff_context, ignore_whitespace, force_text_diff,
4401 case GOT_OBJ_TYPE_COMMIT:
4402 printf("diff %s %s\n", label1, label2);
4403 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4404 diff_context, ignore_whitespace, force_text_diff, repo,
4408 error = got_error(GOT_ERR_OBJ_TYPE);
4417 got_worktree_close(worktree);
4419 const struct got_error *repo_error;
4420 repo_error = got_repo_close(repo);
4424 got_ref_list_free(&refs);
4432 "usage: %s blame [-c commit] [-r repository-path] path\n",
4441 char datebuf[11]; /* YYYY-MM-DD + NUL */
4444 struct blame_cb_args {
4445 struct blame_line *lines;
4449 off_t *line_offsets;
4451 struct got_repository *repo;
4454 static const struct got_error *
4455 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4457 const struct got_error *err = NULL;
4458 struct blame_cb_args *a = arg;
4459 struct blame_line *bline;
4461 size_t linesize = 0;
4462 struct got_commit_object *commit = NULL;
4465 time_t committer_time;
4467 if (nlines != a->nlines ||
4468 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4469 return got_error(GOT_ERR_RANGE);
4471 if (sigint_received)
4472 return got_error(GOT_ERR_ITER_COMPLETED);
4475 return NULL; /* no change in this commit */
4477 /* Annotate this line. */
4478 bline = &a->lines[lineno - 1];
4479 if (bline->annotated)
4481 err = got_object_id_str(&bline->id_str, id);
4485 err = got_object_open_as_commit(&commit, a->repo, id);
4489 bline->committer = strdup(got_object_commit_get_committer(commit));
4490 if (bline->committer == NULL) {
4491 err = got_error_from_errno("strdup");
4495 committer_time = got_object_commit_get_committer_time(commit);
4496 if (localtime_r(&committer_time, &tm) == NULL)
4497 return got_error_from_errno("localtime_r");
4498 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4499 &tm) >= sizeof(bline->datebuf)) {
4500 err = got_error(GOT_ERR_NO_SPACE);
4503 bline->annotated = 1;
4505 /* Print lines annotated so far. */
4506 bline = &a->lines[a->lineno_cur - 1];
4507 if (!bline->annotated)
4510 offset = a->line_offsets[a->lineno_cur - 1];
4511 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4512 err = got_error_from_errno("fseeko");
4516 while (bline->annotated) {
4517 char *smallerthan, *at, *nl, *committer;
4520 if (getline(&line, &linesize, a->f) == -1) {
4522 err = got_error_from_errno("getline");
4526 committer = bline->committer;
4527 smallerthan = strchr(committer, '<');
4528 if (smallerthan && smallerthan[1] != '\0')
4529 committer = smallerthan + 1;
4530 at = strchr(committer, '@');
4533 len = strlen(committer);
4535 committer[8] = '\0';
4537 nl = strchr(line, '\n');
4540 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4541 bline->id_str, bline->datebuf, committer, line);
4544 bline = &a->lines[a->lineno_cur - 1];
4548 got_object_commit_close(commit);
4553 static const struct got_error *
4554 cmd_blame(int argc, char *argv[])
4556 const struct got_error *error;
4557 struct got_repository *repo = NULL;
4558 struct got_worktree *worktree = NULL;
4559 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4560 char *link_target = NULL;
4561 struct got_object_id *obj_id = NULL;
4562 struct got_object_id *commit_id = NULL;
4563 struct got_blob_object *blob = NULL;
4564 char *commit_id_str = NULL;
4565 struct blame_cb_args bca;
4566 int ch, obj_type, i;
4569 memset(&bca, 0, sizeof(bca));
4572 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4577 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4580 commit_id_str = optarg;
4583 repo_path = realpath(optarg, NULL);
4584 if (repo_path == NULL)
4585 return got_error_from_errno2("realpath",
4587 got_path_strip_trailing_slashes(repo_path);
4603 cwd = getcwd(NULL, 0);
4605 error = got_error_from_errno("getcwd");
4608 if (repo_path == NULL) {
4609 error = got_worktree_open(&worktree, cwd);
4610 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4616 strdup(got_worktree_get_repo_path(worktree));
4617 if (repo_path == NULL) {
4618 error = got_error_from_errno("strdup");
4623 repo_path = strdup(cwd);
4624 if (repo_path == NULL) {
4625 error = got_error_from_errno("strdup");
4631 error = got_repo_open(&repo, repo_path, NULL);
4636 const char *prefix = got_worktree_get_path_prefix(worktree);
4639 error = got_worktree_resolve_path(&p, worktree, path);
4642 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4643 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4645 error = got_error_from_errno("asprintf");
4650 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4652 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4655 error = got_repo_map_path(&in_repo_path, repo, path);
4660 if (commit_id_str == NULL) {
4661 struct got_reference *head_ref;
4662 error = got_ref_open(&head_ref, repo, worktree ?
4663 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4666 error = got_ref_resolve(&commit_id, repo, head_ref);
4667 got_ref_close(head_ref);
4671 struct got_reflist_head refs;
4673 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4677 error = got_repo_match_object_id(&commit_id, NULL,
4678 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4679 got_ref_list_free(&refs);
4684 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4689 error = got_object_id_by_path(&obj_id, repo, commit_id,
4690 link_target ? link_target : in_repo_path);
4694 error = got_object_get_type(&obj_type, repo, obj_id);
4698 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4699 error = got_error_path(link_target ? link_target : in_repo_path,
4704 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4707 bca.f = got_opentemp();
4708 if (bca.f == NULL) {
4709 error = got_error_from_errno("got_opentemp");
4712 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4713 &bca.line_offsets, bca.f, blob);
4714 if (error || bca.nlines == 0)
4717 /* Don't include \n at EOF in the blame line count. */
4718 if (bca.line_offsets[bca.nlines - 1] == filesize)
4721 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4722 if (bca.lines == NULL) {
4723 error = got_error_from_errno("calloc");
4727 bca.nlines_prec = 0;
4735 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4736 repo, blame_cb, &bca, check_cancelled, NULL);
4745 got_object_blob_close(blob);
4747 got_worktree_close(worktree);
4749 const struct got_error *repo_error;
4750 repo_error = got_repo_close(repo);
4755 for (i = 0; i < bca.nlines; i++) {
4756 struct blame_line *bline = &bca.lines[i];
4757 free(bline->id_str);
4758 free(bline->committer);
4762 free(bca.line_offsets);
4763 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4764 error = got_error_from_errno("fclose");
4772 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4777 static const struct got_error *
4778 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4779 const char *root_path, struct got_repository *repo)
4781 const struct got_error *err = NULL;
4782 int is_root_path = (strcmp(path, root_path) == 0);
4783 const char *modestr = "";
4784 mode_t mode = got_tree_entry_get_mode(te);
4785 char *link_target = NULL;
4787 path += strlen(root_path);
4788 while (path[0] == '/')
4791 if (got_object_tree_entry_is_submodule(te))
4793 else if (S_ISLNK(mode)) {
4796 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4799 for (i = 0; i < strlen(link_target); i++) {
4800 if (!isprint((unsigned char)link_target[i]))
4801 link_target[i] = '?';
4806 else if (S_ISDIR(mode))
4808 else if (mode & S_IXUSR)
4811 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4812 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4813 link_target ? " -> ": "", link_target ? link_target : "");
4819 static const struct got_error *
4820 print_tree(const char *path, struct got_object_id *commit_id,
4821 int show_ids, int recurse, const char *root_path,
4822 struct got_repository *repo)
4824 const struct got_error *err = NULL;
4825 struct got_object_id *tree_id = NULL;
4826 struct got_tree_object *tree = NULL;
4829 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4833 err = got_object_open_as_tree(&tree, repo, tree_id);
4836 nentries = got_object_tree_get_nentries(tree);
4837 for (i = 0; i < nentries; i++) {
4838 struct got_tree_entry *te;
4841 if (sigint_received || sigpipe_received)
4844 te = got_object_tree_get_entry(tree, i);
4847 err = got_object_id_str(&id_str,
4848 got_tree_entry_get_id(te));
4851 if (asprintf(&id, "%s ", id_str) == -1) {
4852 err = got_error_from_errno("asprintf");
4858 err = print_entry(te, id, path, root_path, repo);
4863 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4865 if (asprintf(&child_path, "%s%s%s", path,
4866 path[0] == '/' && path[1] == '\0' ? "" : "/",
4867 got_tree_entry_get_name(te)) == -1) {
4868 err = got_error_from_errno("asprintf");
4871 err = print_tree(child_path, commit_id, show_ids, 1,
4880 got_object_tree_close(tree);
4885 static const struct got_error *
4886 cmd_tree(int argc, char *argv[])
4888 const struct got_error *error;
4889 struct got_repository *repo = NULL;
4890 struct got_worktree *worktree = NULL;
4891 const char *path, *refname = NULL;
4892 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4893 struct got_object_id *commit_id = NULL;
4894 char *commit_id_str = NULL;
4895 int show_ids = 0, recurse = 0;
4899 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4904 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4907 commit_id_str = optarg;
4910 repo_path = realpath(optarg, NULL);
4911 if (repo_path == NULL)
4912 return got_error_from_errno2("realpath",
4914 got_path_strip_trailing_slashes(repo_path);
4938 cwd = getcwd(NULL, 0);
4940 error = got_error_from_errno("getcwd");
4943 if (repo_path == NULL) {
4944 error = got_worktree_open(&worktree, cwd);
4945 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4951 strdup(got_worktree_get_repo_path(worktree));
4952 if (repo_path == NULL)
4953 error = got_error_from_errno("strdup");
4957 repo_path = strdup(cwd);
4958 if (repo_path == NULL) {
4959 error = got_error_from_errno("strdup");
4965 error = got_repo_open(&repo, repo_path, NULL);
4970 const char *prefix = got_worktree_get_path_prefix(worktree);
4975 error = got_worktree_resolve_path(&p, worktree, path);
4978 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4979 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4981 error = got_error_from_errno("asprintf");
4986 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4990 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4995 error = got_repo_map_path(&in_repo_path, repo, path);
5000 if (commit_id_str == NULL) {
5001 struct got_reference *head_ref;
5003 refname = got_worktree_get_head_ref_name(worktree);
5005 refname = GOT_REF_HEAD;
5006 error = got_ref_open(&head_ref, repo, refname, 0);
5009 error = got_ref_resolve(&commit_id, repo, head_ref);
5010 got_ref_close(head_ref);
5014 struct got_reflist_head refs;
5016 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5020 error = got_repo_match_object_id(&commit_id, NULL,
5021 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5022 got_ref_list_free(&refs);
5027 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5028 in_repo_path, repo);
5035 got_worktree_close(worktree);
5037 const struct got_error *repo_error;
5038 repo_error = got_repo_close(repo);
5048 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5053 static const struct got_error *
5054 print_status(void *arg, unsigned char status, unsigned char staged_status,
5055 const char *path, struct got_object_id *blob_id,
5056 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5057 int dirfd, const char *de_name)
5059 if (status == staged_status && (status == GOT_STATUS_DELETE))
5060 status = GOT_STATUS_NO_CHANGE;
5062 char *status_codes = arg;
5063 size_t ncodes = strlen(status_codes);
5065 for (i = 0; i < ncodes ; i++) {
5066 if (status == status_codes[i] ||
5067 staged_status == status_codes[i])
5073 printf("%c%c %s\n", status, staged_status, path);
5077 static const struct got_error *
5078 cmd_status(int argc, char *argv[])
5080 const struct got_error *error = NULL;
5081 struct got_repository *repo = NULL;
5082 struct got_worktree *worktree = NULL;
5083 char *cwd = NULL, *status_codes = NULL;;
5084 struct got_pathlist_head paths;
5085 struct got_pathlist_entry *pe;
5090 while ((ch = getopt(argc, argv, "s:")) != -1) {
5093 for (i = 0; i < strlen(optarg); i++) {
5094 switch (optarg[i]) {
5095 case GOT_STATUS_MODIFY:
5096 case GOT_STATUS_ADD:
5097 case GOT_STATUS_DELETE:
5098 case GOT_STATUS_CONFLICT:
5099 case GOT_STATUS_MISSING:
5100 case GOT_STATUS_OBSTRUCTED:
5101 case GOT_STATUS_UNVERSIONED:
5102 case GOT_STATUS_MODE_CHANGE:
5103 case GOT_STATUS_NONEXISTENT:
5106 errx(1, "invalid status code '%c'",
5110 status_codes = optarg;
5122 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5126 cwd = getcwd(NULL, 0);
5128 error = got_error_from_errno("getcwd");
5132 error = got_worktree_open(&worktree, cwd);
5134 if (error->code == GOT_ERR_NOT_WORKTREE)
5135 error = wrap_not_worktree_error(error, "status", cwd);
5139 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5144 error = apply_unveil(got_repo_get_path(repo), 1,
5145 got_worktree_get_root_path(worktree));
5149 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5153 error = got_worktree_status(worktree, &paths, repo, print_status,
5154 status_codes, check_cancelled, NULL);
5156 TAILQ_FOREACH(pe, &paths, entry)
5157 free((char *)pe->path);
5158 got_pathlist_free(&paths);
5167 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5173 static const struct got_error *
5174 list_refs(struct got_repository *repo, const char *refname)
5176 static const struct got_error *err = NULL;
5177 struct got_reflist_head refs;
5178 struct got_reflist_entry *re;
5181 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5185 TAILQ_FOREACH(re, &refs, entry) {
5187 refstr = got_ref_to_str(re->ref);
5189 return got_error_from_errno("got_ref_to_str");
5190 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5194 got_ref_list_free(&refs);
5198 static const struct got_error *
5199 delete_ref(struct got_repository *repo, const char *refname)
5201 const struct got_error *err = NULL;
5202 struct got_reference *ref;
5204 err = got_ref_open(&ref, repo, refname, 0);
5208 err = got_ref_delete(ref, repo);
5213 static const struct got_error *
5214 add_ref(struct got_repository *repo, const char *refname, const char *target)
5216 const struct got_error *err = NULL;
5217 struct got_object_id *id;
5218 struct got_reference *ref = NULL;
5221 * Don't let the user create a reference name with a leading '-'.
5222 * While technically a valid reference name, this case is usually
5223 * an unintended typo.
5225 if (refname[0] == '-')
5226 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5228 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5231 struct got_reference *target_ref;
5233 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5235 err = got_ref_open(&target_ref, repo, target, 0);
5238 err = got_ref_resolve(&id, repo, target_ref);
5239 got_ref_close(target_ref);
5244 err = got_ref_alloc(&ref, refname, id);
5248 err = got_ref_write(ref, repo);
5256 static const struct got_error *
5257 add_symref(struct got_repository *repo, const char *refname, const char *target)
5259 const struct got_error *err = NULL;
5260 struct got_reference *ref = NULL;
5261 struct got_reference *target_ref = NULL;
5264 * Don't let the user create a reference name with a leading '-'.
5265 * While technically a valid reference name, this case is usually
5266 * an unintended typo.
5268 if (refname[0] == '-')
5269 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5271 err = got_ref_open(&target_ref, repo, target, 0);
5275 err = got_ref_alloc_symref(&ref, refname, target_ref);
5279 err = got_ref_write(ref, repo);
5282 got_ref_close(target_ref);
5288 static const struct got_error *
5289 cmd_ref(int argc, char *argv[])
5291 const struct got_error *error = NULL;
5292 struct got_repository *repo = NULL;
5293 struct got_worktree *worktree = NULL;
5294 char *cwd = NULL, *repo_path = NULL;
5295 int ch, do_list = 0, do_delete = 0;
5296 const char *obj_arg = NULL, *symref_target= NULL;
5297 char *refname = NULL;
5299 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5308 repo_path = realpath(optarg, NULL);
5309 if (repo_path == NULL)
5310 return got_error_from_errno2("realpath",
5312 got_path_strip_trailing_slashes(repo_path);
5318 symref_target = optarg;
5326 if (obj_arg && do_list)
5327 option_conflict('c', 'l');
5328 if (obj_arg && do_delete)
5329 option_conflict('c', 'd');
5330 if (obj_arg && symref_target)
5331 option_conflict('c', 's');
5332 if (symref_target && do_delete)
5333 option_conflict('s', 'd');
5334 if (symref_target && do_list)
5335 option_conflict('s', 'l');
5336 if (do_delete && do_list)
5337 option_conflict('d', 'l');
5343 if (argc != 0 && argc != 1)
5346 refname = strdup(argv[0]);
5347 if (refname == NULL) {
5348 error = got_error_from_errno("strdup");
5355 refname = strdup(argv[0]);
5356 if (refname == NULL) {
5357 error = got_error_from_errno("strdup");
5363 got_path_strip_trailing_slashes(refname);
5367 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5371 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5372 "sendfd unveil", NULL) == -1)
5376 cwd = getcwd(NULL, 0);
5378 error = got_error_from_errno("getcwd");
5382 if (repo_path == NULL) {
5383 error = got_worktree_open(&worktree, cwd);
5384 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5390 strdup(got_worktree_get_repo_path(worktree));
5391 if (repo_path == NULL)
5392 error = got_error_from_errno("strdup");
5396 repo_path = strdup(cwd);
5397 if (repo_path == NULL) {
5398 error = got_error_from_errno("strdup");
5404 error = got_repo_open(&repo, repo_path, NULL);
5408 error = apply_unveil(got_repo_get_path(repo), do_list,
5409 worktree ? got_worktree_get_root_path(worktree) : NULL);
5414 error = list_refs(repo, refname);
5416 error = delete_ref(repo, refname);
5417 else if (symref_target)
5418 error = add_symref(repo, refname, symref_target);
5420 if (obj_arg == NULL)
5422 error = add_ref(repo, refname, obj_arg);
5427 got_repo_close(repo);
5429 got_worktree_close(worktree);
5439 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5440 "[name]\n", getprogname());
5444 static const struct got_error *
5445 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5446 struct got_reference *ref)
5448 const struct got_error *err = NULL;
5449 const char *refname, *marker = " ";
5452 refname = got_ref_get_name(ref);
5453 if (worktree && strcmp(refname,
5454 got_worktree_get_head_ref_name(worktree)) == 0) {
5455 struct got_object_id *id = NULL;
5457 err = got_ref_resolve(&id, repo, ref);
5460 if (got_object_id_cmp(id,
5461 got_worktree_get_base_commit_id(worktree)) == 0)
5468 if (strncmp(refname, "refs/heads/", 11) == 0)
5470 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5473 refstr = got_ref_to_str(ref);
5475 return got_error_from_errno("got_ref_to_str");
5477 printf("%s%s: %s\n", marker, refname, refstr);
5482 static const struct got_error *
5483 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5485 const char *refname;
5487 if (worktree == NULL)
5488 return got_error(GOT_ERR_NOT_WORKTREE);
5490 refname = got_worktree_get_head_ref_name(worktree);
5492 if (strncmp(refname, "refs/heads/", 11) == 0)
5494 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5497 printf("%s\n", refname);
5502 static const struct got_error *
5503 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5505 static const struct got_error *err = NULL;
5506 struct got_reflist_head refs;
5507 struct got_reflist_entry *re;
5508 struct got_reference *temp_ref = NULL;
5509 int rebase_in_progress, histedit_in_progress;
5514 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5519 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5524 if (rebase_in_progress || histedit_in_progress) {
5525 err = got_ref_open(&temp_ref, repo,
5526 got_worktree_get_head_ref_name(worktree), 0);
5529 list_branch(repo, worktree, temp_ref);
5530 got_ref_close(temp_ref);
5534 err = got_ref_list(&refs, repo, "refs/heads",
5535 got_ref_cmp_by_name, NULL);
5539 TAILQ_FOREACH(re, &refs, entry)
5540 list_branch(repo, worktree, re->ref);
5542 got_ref_list_free(&refs);
5546 static const struct got_error *
5547 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5548 const char *branch_name)
5550 const struct got_error *err = NULL;
5551 struct got_reference *ref = NULL;
5554 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5555 return got_error_from_errno("asprintf");
5557 err = got_ref_open(&ref, repo, refname, 0);
5562 strcmp(got_worktree_get_head_ref_name(worktree),
5563 got_ref_get_name(ref)) == 0) {
5564 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5565 "will not delete this work tree's current branch");
5569 err = got_ref_delete(ref, repo);
5577 static const struct got_error *
5578 add_branch(struct got_repository *repo, const char *branch_name,
5579 struct got_object_id *base_commit_id)
5581 const struct got_error *err = NULL;
5582 struct got_reference *ref = NULL;
5583 char *base_refname = NULL, *refname = NULL;
5586 * Don't let the user create a branch name with a leading '-'.
5587 * While technically a valid reference name, this case is usually
5588 * an unintended typo.
5590 if (branch_name[0] == '-')
5591 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5593 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5594 err = got_error_from_errno("asprintf");
5598 err = got_ref_open(&ref, repo, refname, 0);
5600 err = got_error(GOT_ERR_BRANCH_EXISTS);
5602 } else if (err->code != GOT_ERR_NOT_REF)
5605 err = got_ref_alloc(&ref, refname, base_commit_id);
5609 err = got_ref_write(ref, repo);
5618 static const struct got_error *
5619 cmd_branch(int argc, char *argv[])
5621 const struct got_error *error = NULL;
5622 struct got_repository *repo = NULL;
5623 struct got_worktree *worktree = NULL;
5624 char *cwd = NULL, *repo_path = NULL;
5625 int ch, do_list = 0, do_show = 0, do_update = 1;
5626 const char *delref = NULL, *commit_id_arg = NULL;
5627 struct got_reference *ref = NULL;
5628 struct got_pathlist_head paths;
5629 struct got_pathlist_entry *pe;
5630 struct got_object_id *commit_id = NULL;
5631 char *commit_id_str = NULL;
5635 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5638 commit_id_arg = optarg;
5644 repo_path = realpath(optarg, NULL);
5645 if (repo_path == NULL)
5646 return got_error_from_errno2("realpath",
5648 got_path_strip_trailing_slashes(repo_path);
5662 if (do_list && delref)
5663 option_conflict('l', 'd');
5668 if (!do_list && !delref && argc == 0)
5671 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5672 errx(1, "-c option can only be used when creating a branch");
5674 if (do_list || delref) {
5677 } else if (!do_show && argc != 1)
5681 if (do_list || do_show) {
5682 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5686 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5687 "sendfd unveil", NULL) == -1)
5691 cwd = getcwd(NULL, 0);
5693 error = got_error_from_errno("getcwd");
5697 if (repo_path == NULL) {
5698 error = got_worktree_open(&worktree, cwd);
5699 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5705 strdup(got_worktree_get_repo_path(worktree));
5706 if (repo_path == NULL)
5707 error = got_error_from_errno("strdup");
5711 repo_path = strdup(cwd);
5712 if (repo_path == NULL) {
5713 error = got_error_from_errno("strdup");
5719 error = got_repo_open(&repo, repo_path, NULL);
5723 error = apply_unveil(got_repo_get_path(repo), do_list,
5724 worktree ? got_worktree_get_root_path(worktree) : NULL);
5729 error = show_current_branch(repo, worktree);
5731 error = list_branches(repo, worktree);
5733 error = delete_branch(repo, worktree, delref);
5735 struct got_reflist_head refs;
5737 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5741 if (commit_id_arg == NULL)
5742 commit_id_arg = worktree ?
5743 got_worktree_get_head_ref_name(worktree) :
5745 error = got_repo_match_object_id(&commit_id, NULL,
5746 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5747 got_ref_list_free(&refs);
5750 error = add_branch(repo, argv[0], commit_id);
5753 if (worktree && do_update) {
5754 struct got_update_progress_arg upa;
5755 char *branch_refname = NULL;
5757 error = got_object_id_str(&commit_id_str, commit_id);
5760 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5764 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5766 error = got_error_from_errno("asprintf");
5769 error = got_ref_open(&ref, repo, branch_refname, 0);
5770 free(branch_refname);
5773 error = switch_head_ref(ref, commit_id, worktree,
5777 error = got_worktree_set_base_commit_id(worktree, repo,
5781 memset(&upa, 0, sizeof(upa));
5782 error = got_worktree_checkout_files(worktree, &paths,
5783 repo, update_progress, &upa, check_cancelled,
5787 if (upa.did_something)
5788 printf("Updated to commit %s\n", commit_id_str);
5789 print_update_progress_stats(&upa);
5796 got_repo_close(repo);
5798 got_worktree_close(worktree);
5802 free(commit_id_str);
5803 TAILQ_FOREACH(pe, &paths, entry)
5804 free((char *)pe->path);
5805 got_pathlist_free(&paths);
5814 "usage: %s tag [-c commit] [-r repository] [-l] "
5815 "[-m message] name\n", getprogname());
5820 static const struct got_error *
5821 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5823 const struct got_error *err = NULL;
5824 struct got_reflist_entry *re, *se, *new;
5825 struct got_object_id *re_id, *se_id;
5826 struct got_tag_object *re_tag, *se_tag;
5827 time_t re_time, se_time;
5829 SIMPLEQ_FOREACH(re, tags, entry) {
5830 se = SIMPLEQ_FIRST(sorted);
5832 err = got_reflist_entry_dup(&new, re);
5835 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5838 err = got_ref_resolve(&re_id, repo, re->ref);
5841 err = got_object_open_as_tag(&re_tag, repo, re_id);
5845 re_time = got_object_tag_get_tagger_time(re_tag);
5846 got_object_tag_close(re_tag);
5850 err = got_ref_resolve(&se_id, repo, re->ref);
5853 err = got_object_open_as_tag(&se_tag, repo, se_id);
5857 se_time = got_object_tag_get_tagger_time(se_tag);
5858 got_object_tag_close(se_tag);
5860 if (se_time > re_time) {
5861 err = got_reflist_entry_dup(&new, re);
5864 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5867 se = SIMPLEQ_NEXT(se, entry);
5876 static const struct got_error *
5877 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5879 static const struct got_error *err = NULL;
5880 struct got_reflist_head refs;
5881 struct got_reflist_entry *re;
5885 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5889 TAILQ_FOREACH(re, &refs, entry) {
5890 const char *refname;
5891 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5895 struct got_object_id *id;
5896 struct got_tag_object *tag;
5897 struct got_commit_object *commit = NULL;
5899 refname = got_ref_get_name(re->ref);
5900 if (strncmp(refname, "refs/tags/", 10) != 0)
5903 refstr = got_ref_to_str(re->ref);
5904 if (refstr == NULL) {
5905 err = got_error_from_errno("got_ref_to_str");
5908 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5911 err = got_ref_resolve(&id, repo, re->ref);
5914 err = got_object_open_as_tag(&tag, repo, id);
5916 if (err->code != GOT_ERR_OBJ_TYPE) {
5920 /* "lightweight" tag */
5921 err = got_object_open_as_commit(&commit, repo, id);
5926 tagger = got_object_commit_get_committer(commit);
5928 got_object_commit_get_committer_time(commit);
5929 err = got_object_id_str(&id_str, id);
5935 tagger = got_object_tag_get_tagger(tag);
5936 tagger_time = got_object_tag_get_tagger_time(tag);
5937 err = got_object_id_str(&id_str,
5938 got_object_tag_get_object_id(tag));
5942 printf("from: %s\n", tagger);
5943 datestr = get_datestr(&tagger_time, datebuf);
5945 printf("date: %s UTC\n", datestr);
5947 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5949 switch (got_object_tag_get_object_type(tag)) {
5950 case GOT_OBJ_TYPE_BLOB:
5951 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5954 case GOT_OBJ_TYPE_TREE:
5955 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5958 case GOT_OBJ_TYPE_COMMIT:
5959 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5962 case GOT_OBJ_TYPE_TAG:
5963 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5972 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5975 got_object_commit_close(commit);
5977 tagmsg0 = strdup(got_object_tag_get_message(tag));
5978 got_object_tag_close(tag);
5979 if (tagmsg0 == NULL) {
5980 err = got_error_from_errno("strdup");
5987 line = strsep(&tagmsg, "\n");
5989 printf(" %s\n", line);
5994 got_ref_list_free(&refs);
5998 static const struct got_error *
5999 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6000 const char *tag_name, const char *repo_path)
6002 const struct got_error *err = NULL;
6003 char *template = NULL, *initial_content = NULL;
6004 char *editor = NULL;
6005 int initial_content_len;
6008 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6009 err = got_error_from_errno("asprintf");
6013 initial_content_len = asprintf(&initial_content,
6014 "\n# tagging commit %s as %s\n",
6015 commit_id_str, tag_name);
6016 if (initial_content_len == -1) {
6017 err = got_error_from_errno("asprintf");
6021 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6025 if (write(fd, initial_content, initial_content_len) == -1) {
6026 err = got_error_from_errno2("write", *tagmsg_path);
6030 err = get_editor(&editor);
6033 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6034 initial_content_len, 1);
6036 free(initial_content);
6040 if (fd != -1 && close(fd) == -1 && err == NULL)
6041 err = got_error_from_errno2("close", *tagmsg_path);
6043 /* Editor is done; we can now apply unveil(2) */
6045 err = apply_unveil(repo_path, 0, NULL);
6053 static const struct got_error *
6054 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6055 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6057 const struct got_error *err = NULL;
6058 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6059 char *label = NULL, *commit_id_str = NULL;
6060 struct got_reference *ref = NULL;
6061 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6062 char *tagmsg_path = NULL, *tag_id_str = NULL;
6063 int preserve_tagmsg = 0;
6064 struct got_reflist_head refs;
6069 * Don't let the user create a tag name with a leading '-'.
6070 * While technically a valid reference name, this case is usually
6071 * an unintended typo.
6073 if (tag_name[0] == '-')
6074 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6076 err = get_author(&tagger, repo, worktree);
6080 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6084 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6085 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6089 err = got_object_id_str(&commit_id_str, commit_id);
6093 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6094 refname = strdup(tag_name);
6095 if (refname == NULL) {
6096 err = got_error_from_errno("strdup");
6100 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6101 err = got_error_from_errno("asprintf");
6105 err = got_ref_open(&ref, repo, refname, 0);
6107 err = got_error(GOT_ERR_TAG_EXISTS);
6109 } else if (err->code != GOT_ERR_NOT_REF)
6112 if (tagmsg_arg == NULL) {
6113 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6114 tag_name, got_repo_get_path(repo));
6116 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6117 tagmsg_path != NULL)
6118 preserve_tagmsg = 1;
6123 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6124 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6127 preserve_tagmsg = 1;
6131 err = got_ref_alloc(&ref, refname, tag_id);
6134 preserve_tagmsg = 1;
6138 err = got_ref_write(ref, repo);
6141 preserve_tagmsg = 1;
6145 err = got_object_id_str(&tag_id_str, tag_id);
6148 preserve_tagmsg = 1;
6151 printf("Created tag %s\n", tag_id_str);
6153 if (preserve_tagmsg) {
6154 fprintf(stderr, "%s: tag message preserved in %s\n",
6155 getprogname(), tagmsg_path);
6156 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6157 err = got_error_from_errno2("unlink", tagmsg_path);
6162 free(commit_id_str);
6167 got_ref_list_free(&refs);
6171 static const struct got_error *
6172 cmd_tag(int argc, char *argv[])
6174 const struct got_error *error = NULL;
6175 struct got_repository *repo = NULL;
6176 struct got_worktree *worktree = NULL;
6177 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6178 char *gitconfig_path = NULL;
6179 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6180 int ch, do_list = 0;
6182 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6185 commit_id_arg = optarg;
6191 repo_path = realpath(optarg, NULL);
6192 if (repo_path == NULL)
6193 return got_error_from_errno2("realpath",
6195 got_path_strip_trailing_slashes(repo_path);
6210 if (commit_id_arg != NULL)
6212 "-c option can only be used when creating a tag");
6214 option_conflict('l', 'm');
6217 } else if (argc != 1)
6224 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6228 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6229 "sendfd unveil", NULL) == -1)
6233 cwd = getcwd(NULL, 0);
6235 error = got_error_from_errno("getcwd");
6239 if (repo_path == NULL) {
6240 error = got_worktree_open(&worktree, cwd);
6241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6247 strdup(got_worktree_get_repo_path(worktree));
6248 if (repo_path == NULL)
6249 error = got_error_from_errno("strdup");
6253 repo_path = strdup(cwd);
6254 if (repo_path == NULL) {
6255 error = got_error_from_errno("strdup");
6262 error = got_repo_open(&repo, repo_path, NULL);
6265 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6268 error = list_tags(repo, worktree);
6270 error = get_gitconfig_path(&gitconfig_path);
6273 error = got_repo_open(&repo, repo_path, gitconfig_path);
6278 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6283 if (commit_id_arg == NULL) {
6284 struct got_reference *head_ref;
6285 struct got_object_id *commit_id;
6286 error = got_ref_open(&head_ref, repo,
6287 worktree ? got_worktree_get_head_ref_name(worktree)
6291 error = got_ref_resolve(&commit_id, repo, head_ref);
6292 got_ref_close(head_ref);
6295 error = got_object_id_str(&commit_id_str, commit_id);
6301 error = add_tag(repo, worktree, tag_name,
6302 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6306 got_repo_close(repo);
6308 got_worktree_close(worktree);
6311 free(gitconfig_path);
6312 free(commit_id_str);
6319 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6324 static const struct got_error *
6325 add_progress(void *arg, unsigned char status, const char *path)
6327 while (path[0] == '/')
6329 printf("%c %s\n", status, path);
6333 static const struct got_error *
6334 cmd_add(int argc, char *argv[])
6336 const struct got_error *error = NULL;
6337 struct got_repository *repo = NULL;
6338 struct got_worktree *worktree = NULL;
6340 struct got_pathlist_head paths;
6341 struct got_pathlist_entry *pe;
6342 int ch, can_recurse = 0, no_ignores = 0;
6346 while ((ch = getopt(argc, argv, "IR")) != -1) {
6364 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6371 cwd = getcwd(NULL, 0);
6373 error = got_error_from_errno("getcwd");
6377 error = got_worktree_open(&worktree, cwd);
6379 if (error->code == GOT_ERR_NOT_WORKTREE)
6380 error = wrap_not_worktree_error(error, "add", cwd);
6384 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6389 error = apply_unveil(got_repo_get_path(repo), 1,
6390 got_worktree_get_root_path(worktree));
6394 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6398 if (!can_recurse && no_ignores) {
6399 error = got_error_msg(GOT_ERR_BAD_PATH,
6400 "disregarding ignores requires -R option");
6408 TAILQ_FOREACH(pe, &paths, entry) {
6409 if (asprintf(&ondisk_path, "%s/%s",
6410 got_worktree_get_root_path(worktree),
6412 error = got_error_from_errno("asprintf");
6415 if (lstat(ondisk_path, &sb) == -1) {
6416 if (errno == ENOENT) {
6420 error = got_error_from_errno2("lstat",
6426 if (S_ISDIR(sb.st_mode)) {
6427 error = got_error_msg(GOT_ERR_BAD_PATH,
6428 "adding directories requires -R option");
6434 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6435 NULL, repo, no_ignores);
6438 got_repo_close(repo);
6440 got_worktree_close(worktree);
6441 TAILQ_FOREACH(pe, &paths, entry)
6442 free((char *)pe->path);
6443 got_pathlist_free(&paths);
6451 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6452 "path ...\n", getprogname());
6456 static const struct got_error *
6457 print_remove_status(void *arg, unsigned char status,
6458 unsigned char staged_status, const char *path)
6460 while (path[0] == '/')
6462 if (status == GOT_STATUS_NONEXISTENT)
6464 if (status == staged_status && (status == GOT_STATUS_DELETE))
6465 status = GOT_STATUS_NO_CHANGE;
6466 printf("%c%c %s\n", status, staged_status, path);
6470 static const struct got_error *
6471 cmd_remove(int argc, char *argv[])
6473 const struct got_error *error = NULL;
6474 struct got_worktree *worktree = NULL;
6475 struct got_repository *repo = NULL;
6476 const char *status_codes = NULL;
6478 struct got_pathlist_head paths;
6479 struct got_pathlist_entry *pe;
6480 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6484 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6487 delete_local_mods = 1;
6496 for (i = 0; i < strlen(optarg); i++) {
6497 switch (optarg[i]) {
6498 case GOT_STATUS_MODIFY:
6499 delete_local_mods = 1;
6501 case GOT_STATUS_MISSING:
6504 errx(1, "invalid status code '%c'",
6508 status_codes = optarg;
6520 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6527 cwd = getcwd(NULL, 0);
6529 error = got_error_from_errno("getcwd");
6532 error = got_worktree_open(&worktree, cwd);
6534 if (error->code == GOT_ERR_NOT_WORKTREE)
6535 error = wrap_not_worktree_error(error, "remove", cwd);
6539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6544 error = apply_unveil(got_repo_get_path(repo), 1,
6545 got_worktree_get_root_path(worktree));
6549 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6556 TAILQ_FOREACH(pe, &paths, entry) {
6557 if (asprintf(&ondisk_path, "%s/%s",
6558 got_worktree_get_root_path(worktree),
6560 error = got_error_from_errno("asprintf");
6563 if (lstat(ondisk_path, &sb) == -1) {
6564 if (errno == ENOENT) {
6568 error = got_error_from_errno2("lstat",
6574 if (S_ISDIR(sb.st_mode)) {
6575 error = got_error_msg(GOT_ERR_BAD_PATH,
6576 "removing directories requires -R option");
6582 error = got_worktree_schedule_delete(worktree, &paths,
6583 delete_local_mods, status_codes, print_remove_status, NULL,
6584 repo, keep_on_disk);
6587 got_repo_close(repo);
6589 got_worktree_close(worktree);
6590 TAILQ_FOREACH(pe, &paths, entry)
6591 free((char *)pe->path);
6592 got_pathlist_free(&paths);
6600 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6601 "path ...\n", getprogname());
6605 static const struct got_error *
6606 revert_progress(void *arg, unsigned char status, const char *path)
6608 if (status == GOT_STATUS_UNVERSIONED)
6611 while (path[0] == '/')
6613 printf("%c %s\n", status, path);
6617 struct choose_patch_arg {
6618 FILE *patch_script_file;
6622 static const struct got_error *
6623 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6624 int nchanges, const char *action)
6627 size_t linesize = 0;
6631 case GOT_STATUS_ADD:
6632 printf("A %s\n%s this addition? [y/n] ", path, action);
6634 case GOT_STATUS_DELETE:
6635 printf("D %s\n%s this deletion? [y/n] ", path, action);
6637 case GOT_STATUS_MODIFY:
6638 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6639 return got_error_from_errno("fseek");
6640 printf(GOT_COMMIT_SEP_STR);
6641 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6643 if (ferror(patch_file))
6644 return got_error_from_errno("getline");
6645 printf(GOT_COMMIT_SEP_STR);
6646 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6647 path, n, nchanges, action);
6650 return got_error_path(path, GOT_ERR_FILE_STATUS);
6656 static const struct got_error *
6657 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6658 FILE *patch_file, int n, int nchanges)
6660 const struct got_error *err = NULL;
6662 size_t linesize = 0;
6665 struct choose_patch_arg *a = arg;
6667 *choice = GOT_PATCH_CHOICE_NONE;
6669 if (a->patch_script_file) {
6671 err = show_change(status, path, patch_file, n, nchanges,
6675 linelen = getline(&line, &linesize, a->patch_script_file);
6676 if (linelen == -1) {
6677 if (ferror(a->patch_script_file))
6678 return got_error_from_errno("getline");
6681 nl = strchr(line, '\n');
6684 if (strcmp(line, "y") == 0) {
6685 *choice = GOT_PATCH_CHOICE_YES;
6687 } else if (strcmp(line, "n") == 0) {
6688 *choice = GOT_PATCH_CHOICE_NO;
6690 } else if (strcmp(line, "q") == 0 &&
6691 status == GOT_STATUS_MODIFY) {
6692 *choice = GOT_PATCH_CHOICE_QUIT;
6695 printf("invalid response '%s'\n", line);
6700 while (resp != 'y' && resp != 'n' && resp != 'q') {
6701 err = show_change(status, path, patch_file, n, nchanges,
6708 if (status == GOT_STATUS_MODIFY) {
6709 if (resp != 'y' && resp != 'n' && resp != 'q') {
6710 printf("invalid response '%c'\n", resp);
6713 } else if (resp != 'y' && resp != 'n') {
6714 printf("invalid response '%c'\n", resp);
6720 *choice = GOT_PATCH_CHOICE_YES;
6721 else if (resp == 'n')
6722 *choice = GOT_PATCH_CHOICE_NO;
6723 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6724 *choice = GOT_PATCH_CHOICE_QUIT;
6730 static const struct got_error *
6731 cmd_revert(int argc, char *argv[])
6733 const struct got_error *error = NULL;
6734 struct got_worktree *worktree = NULL;
6735 struct got_repository *repo = NULL;
6736 char *cwd = NULL, *path = NULL;
6737 struct got_pathlist_head paths;
6738 struct got_pathlist_entry *pe;
6739 int ch, can_recurse = 0, pflag = 0;
6740 FILE *patch_script_file = NULL;
6741 const char *patch_script_path = NULL;
6742 struct choose_patch_arg cpa;
6746 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6752 patch_script_path = optarg;
6767 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6768 "unveil", NULL) == -1)
6773 if (patch_script_path && !pflag)
6774 errx(1, "-F option can only be used together with -p option");
6776 cwd = getcwd(NULL, 0);
6778 error = got_error_from_errno("getcwd");
6781 error = got_worktree_open(&worktree, cwd);
6783 if (error->code == GOT_ERR_NOT_WORKTREE)
6784 error = wrap_not_worktree_error(error, "revert", cwd);
6788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6793 if (patch_script_path) {
6794 patch_script_file = fopen(patch_script_path, "r");
6795 if (patch_script_file == NULL) {
6796 error = got_error_from_errno2("fopen",
6801 error = apply_unveil(got_repo_get_path(repo), 1,
6802 got_worktree_get_root_path(worktree));
6806 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6813 TAILQ_FOREACH(pe, &paths, entry) {
6814 if (asprintf(&ondisk_path, "%s/%s",
6815 got_worktree_get_root_path(worktree),
6817 error = got_error_from_errno("asprintf");
6820 if (lstat(ondisk_path, &sb) == -1) {
6821 if (errno == ENOENT) {
6825 error = got_error_from_errno2("lstat",
6831 if (S_ISDIR(sb.st_mode)) {
6832 error = got_error_msg(GOT_ERR_BAD_PATH,
6833 "reverting directories requires -R option");
6839 cpa.patch_script_file = patch_script_file;
6840 cpa.action = "revert";
6841 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6842 pflag ? choose_patch : NULL, &cpa, repo);
6844 if (patch_script_file && fclose(patch_script_file) == EOF &&
6846 error = got_error_from_errno2("fclose", patch_script_path);
6848 got_repo_close(repo);
6850 got_worktree_close(worktree);
6859 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6864 struct collect_commit_logmsg_arg {
6865 const char *cmdline_log;
6867 const char *worktree_path;
6868 const char *branch_name;
6869 const char *repo_path;
6874 static const struct got_error *
6875 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6878 char *initial_content = NULL;
6879 struct got_pathlist_entry *pe;
6880 const struct got_error *err = NULL;
6881 char *template = NULL;
6882 struct collect_commit_logmsg_arg *a = arg;
6883 int initial_content_len;
6887 /* if a message was specified on the command line, just use it */
6888 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6889 len = strlen(a->cmdline_log) + 1;
6890 *logmsg = malloc(len + 1);
6891 if (*logmsg == NULL)
6892 return got_error_from_errno("malloc");
6893 strlcpy(*logmsg, a->cmdline_log, len);
6897 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6898 return got_error_from_errno("asprintf");
6900 initial_content_len = asprintf(&initial_content,
6901 "\n# changes to be committed on branch %s:\n",
6903 if (initial_content_len == -1)
6904 return got_error_from_errno("asprintf");
6906 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6910 if (write(fd, initial_content, initial_content_len) == -1) {
6911 err = got_error_from_errno2("write", a->logmsg_path);
6915 TAILQ_FOREACH(pe, commitable_paths, entry) {
6916 struct got_commitable *ct = pe->data;
6917 dprintf(fd, "# %c %s\n",
6918 got_commitable_get_status(ct),
6919 got_commitable_get_path(ct));
6922 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6923 initial_content_len, 1);
6925 free(initial_content);
6928 if (fd != -1 && close(fd) == -1 && err == NULL)
6929 err = got_error_from_errno2("close", a->logmsg_path);
6931 /* Editor is done; we can now apply unveil(2) */
6933 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6941 static const struct got_error *
6942 cmd_commit(int argc, char *argv[])
6944 const struct got_error *error = NULL;
6945 struct got_worktree *worktree = NULL;
6946 struct got_repository *repo = NULL;
6947 char *cwd = NULL, *id_str = NULL;
6948 struct got_object_id *id = NULL;
6949 const char *logmsg = NULL;
6950 struct collect_commit_logmsg_arg cl_arg;
6951 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6952 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6953 int allow_bad_symlinks = 0;
6954 struct got_pathlist_head paths;
6957 cl_arg.logmsg_path = NULL;
6959 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6965 allow_bad_symlinks = 1;
6977 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6978 "unveil", NULL) == -1)
6981 cwd = getcwd(NULL, 0);
6983 error = got_error_from_errno("getcwd");
6986 error = got_worktree_open(&worktree, cwd);
6988 if (error->code == GOT_ERR_NOT_WORKTREE)
6989 error = wrap_not_worktree_error(error, "commit", cwd);
6993 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6996 if (rebase_in_progress) {
6997 error = got_error(GOT_ERR_REBASING);
7001 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7006 error = get_gitconfig_path(&gitconfig_path);
7009 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7014 error = get_author(&author, repo, worktree);
7019 * unveil(2) traverses exec(2); if an editor is used we have
7020 * to apply unveil after the log message has been written.
7022 if (logmsg == NULL || strlen(logmsg) == 0)
7023 error = get_editor(&editor);
7025 error = apply_unveil(got_repo_get_path(repo), 0,
7026 got_worktree_get_root_path(worktree));
7030 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7034 cl_arg.editor = editor;
7035 cl_arg.cmdline_log = logmsg;
7036 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7037 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7038 if (!histedit_in_progress) {
7039 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7040 error = got_error(GOT_ERR_COMMIT_BRANCH);
7043 cl_arg.branch_name += 11;
7045 cl_arg.repo_path = got_repo_get_path(repo);
7046 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7047 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7048 print_status, NULL, repo);
7050 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7051 cl_arg.logmsg_path != NULL)
7052 preserve_logmsg = 1;
7056 error = got_object_id_str(&id_str, id);
7059 printf("Created commit %s\n", id_str);
7061 if (preserve_logmsg) {
7062 fprintf(stderr, "%s: log message preserved in %s\n",
7063 getprogname(), cl_arg.logmsg_path);
7064 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7066 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7067 free(cl_arg.logmsg_path);
7069 got_repo_close(repo);
7071 got_worktree_close(worktree);
7074 free(gitconfig_path);
7081 usage_cherrypick(void)
7083 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7087 static const struct got_error *
7088 cmd_cherrypick(int argc, char *argv[])
7090 const struct got_error *error = NULL;
7091 struct got_worktree *worktree = NULL;
7092 struct got_repository *repo = NULL;
7093 char *cwd = NULL, *commit_id_str = NULL;
7094 struct got_object_id *commit_id = NULL;
7095 struct got_commit_object *commit = NULL;
7096 struct got_object_qid *pid;
7097 struct got_reference *head_ref = NULL;
7099 struct got_update_progress_arg upa;
7101 while ((ch = getopt(argc, argv, "")) != -1) {
7113 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7114 "unveil", NULL) == -1)
7120 cwd = getcwd(NULL, 0);
7122 error = got_error_from_errno("getcwd");
7125 error = got_worktree_open(&worktree, cwd);
7127 if (error->code == GOT_ERR_NOT_WORKTREE)
7128 error = wrap_not_worktree_error(error, "cherrypick",
7133 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7138 error = apply_unveil(got_repo_get_path(repo), 0,
7139 got_worktree_get_root_path(worktree));
7143 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7144 GOT_OBJ_TYPE_COMMIT, repo);
7145 if (error != NULL) {
7146 struct got_reference *ref;
7147 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7149 error = got_ref_open(&ref, repo, argv[0], 0);
7152 error = got_ref_resolve(&commit_id, repo, ref);
7157 error = got_object_id_str(&commit_id_str, commit_id);
7161 error = got_ref_open(&head_ref, repo,
7162 got_worktree_get_head_ref_name(worktree), 0);
7166 error = check_same_branch(commit_id, head_ref, NULL, repo);
7168 if (error->code != GOT_ERR_ANCESTRY)
7172 error = got_error(GOT_ERR_SAME_BRANCH);
7176 error = got_object_open_as_commit(&commit, repo, commit_id);
7179 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7180 memset(&upa, 0, sizeof(upa));
7181 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7182 commit_id, repo, update_progress, &upa, check_cancelled,
7187 if (upa.did_something)
7188 printf("Merged commit %s\n", commit_id_str);
7189 print_update_progress_stats(&upa);
7192 got_object_commit_close(commit);
7193 free(commit_id_str);
7195 got_ref_close(head_ref);
7197 got_worktree_close(worktree);
7199 got_repo_close(repo);
7206 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7210 static const struct got_error *
7211 cmd_backout(int argc, char *argv[])
7213 const struct got_error *error = NULL;
7214 struct got_worktree *worktree = NULL;
7215 struct got_repository *repo = NULL;
7216 char *cwd = NULL, *commit_id_str = NULL;
7217 struct got_object_id *commit_id = NULL;
7218 struct got_commit_object *commit = NULL;
7219 struct got_object_qid *pid;
7220 struct got_reference *head_ref = NULL;
7222 struct got_update_progress_arg upa;
7224 while ((ch = getopt(argc, argv, "")) != -1) {
7236 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7237 "unveil", NULL) == -1)
7243 cwd = getcwd(NULL, 0);
7245 error = got_error_from_errno("getcwd");
7248 error = got_worktree_open(&worktree, cwd);
7250 if (error->code == GOT_ERR_NOT_WORKTREE)
7251 error = wrap_not_worktree_error(error, "backout", cwd);
7255 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7260 error = apply_unveil(got_repo_get_path(repo), 0,
7261 got_worktree_get_root_path(worktree));
7265 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7266 GOT_OBJ_TYPE_COMMIT, repo);
7267 if (error != NULL) {
7268 struct got_reference *ref;
7269 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7271 error = got_ref_open(&ref, repo, argv[0], 0);
7274 error = got_ref_resolve(&commit_id, repo, ref);
7279 error = got_object_id_str(&commit_id_str, commit_id);
7283 error = got_ref_open(&head_ref, repo,
7284 got_worktree_get_head_ref_name(worktree), 0);
7288 error = check_same_branch(commit_id, head_ref, NULL, repo);
7292 error = got_object_open_as_commit(&commit, repo, commit_id);
7295 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7297 error = got_error(GOT_ERR_ROOT_COMMIT);
7301 memset(&upa, 0, sizeof(upa));
7302 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7303 update_progress, &upa, check_cancelled, NULL);
7307 if (upa.did_something)
7308 printf("Backed out commit %s\n", commit_id_str);
7309 print_update_progress_stats(&upa);
7312 got_object_commit_close(commit);
7313 free(commit_id_str);
7315 got_ref_close(head_ref);
7317 got_worktree_close(worktree);
7319 got_repo_close(repo);
7326 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7332 trim_logmsg(char *logmsg, int limit)
7337 len = strlen(logmsg);
7341 nl = strchr(logmsg, '\n');
7346 static const struct got_error *
7347 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7349 const struct got_error *err;
7350 char *logmsg0 = NULL;
7353 err = got_object_commit_get_logmsg(&logmsg0, commit);
7358 while (isspace((unsigned char)s[0]))
7361 *logmsg = strdup(s);
7362 if (*logmsg == NULL) {
7363 err = got_error_from_errno("strdup");
7367 trim_logmsg(*logmsg, limit);
7373 static const struct got_error *
7374 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7376 const struct got_error *err;
7377 struct got_commit_object *commit = NULL;
7378 char *id_str = NULL, *logmsg = NULL;
7380 err = got_object_open_as_commit(&commit, repo, id);
7384 err = got_object_id_str(&id_str, id);
7390 err = get_short_logmsg(&logmsg, 42, commit);
7394 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7397 got_object_commit_close(commit);
7402 static const struct got_error *
7403 show_rebase_progress(struct got_commit_object *commit,
7404 struct got_object_id *old_id, struct got_object_id *new_id)
7406 const struct got_error *err;
7407 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7409 err = got_object_id_str(&old_id_str, old_id);
7414 err = got_object_id_str(&new_id_str, new_id);
7419 old_id_str[12] = '\0';
7421 new_id_str[12] = '\0';
7423 err = get_short_logmsg(&logmsg, 42, commit);
7427 printf("%s -> %s: %s\n", old_id_str,
7428 new_id_str ? new_id_str : "no-op change", logmsg);
7436 static const struct got_error *
7437 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7438 struct got_reference *branch, struct got_reference *new_base_branch,
7439 struct got_reference *tmp_branch, struct got_repository *repo)
7441 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7442 return got_worktree_rebase_complete(worktree, fileindex,
7443 new_base_branch, tmp_branch, branch, repo);
7446 static const struct got_error *
7447 rebase_commit(struct got_pathlist_head *merged_paths,
7448 struct got_worktree *worktree, struct got_fileindex *fileindex,
7449 struct got_reference *tmp_branch,
7450 struct got_object_id *commit_id, struct got_repository *repo)
7452 const struct got_error *error;
7453 struct got_commit_object *commit;
7454 struct got_object_id *new_commit_id;
7456 error = got_object_open_as_commit(&commit, repo, commit_id);
7460 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7461 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7463 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7465 error = show_rebase_progress(commit, commit_id, NULL);
7467 error = show_rebase_progress(commit, commit_id, new_commit_id);
7468 free(new_commit_id);
7471 got_object_commit_close(commit);
7475 struct check_path_prefix_arg {
7476 const char *path_prefix;
7481 static const struct got_error *
7482 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7483 struct got_blob_object *blob2, struct got_object_id *id1,
7484 struct got_object_id *id2, const char *path1, const char *path2,
7485 mode_t mode1, mode_t mode2, struct got_repository *repo)
7487 struct check_path_prefix_arg *a = arg;
7489 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7490 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7491 return got_error(a->errcode);
7496 static const struct got_error *
7497 check_path_prefix(struct got_object_id *parent_id,
7498 struct got_object_id *commit_id, const char *path_prefix,
7499 int errcode, struct got_repository *repo)
7501 const struct got_error *err;
7502 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7503 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7504 struct check_path_prefix_arg cpp_arg;
7506 if (got_path_is_root_dir(path_prefix))
7509 err = got_object_open_as_commit(&commit, repo, commit_id);
7513 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7517 err = got_object_open_as_tree(&tree1, repo,
7518 got_object_commit_get_tree_id(parent_commit));
7522 err = got_object_open_as_tree(&tree2, repo,
7523 got_object_commit_get_tree_id(commit));
7527 cpp_arg.path_prefix = path_prefix;
7528 while (cpp_arg.path_prefix[0] == '/')
7529 cpp_arg.path_prefix++;
7530 cpp_arg.len = strlen(cpp_arg.path_prefix);
7531 cpp_arg.errcode = errcode;
7532 err = got_diff_tree(tree1, tree2, "", "", repo,
7533 check_path_prefix_in_diff, &cpp_arg, 0);
7536 got_object_tree_close(tree1);
7538 got_object_tree_close(tree2);
7540 got_object_commit_close(commit);
7542 got_object_commit_close(parent_commit);
7546 static const struct got_error *
7547 collect_commits(struct got_object_id_queue *commits,
7548 struct got_object_id *initial_commit_id,
7549 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7550 const char *path_prefix, int path_prefix_errcode,
7551 struct got_repository *repo)
7553 const struct got_error *err = NULL;
7554 struct got_commit_graph *graph = NULL;
7555 struct got_object_id *parent_id = NULL;
7556 struct got_object_qid *qid;
7557 struct got_object_id *commit_id = initial_commit_id;
7559 err = got_commit_graph_open(&graph, "/", 1);
7563 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7564 check_cancelled, NULL);
7567 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7568 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7569 check_cancelled, NULL);
7571 if (err->code == GOT_ERR_ITER_COMPLETED) {
7572 err = got_error_msg(GOT_ERR_ANCESTRY,
7573 "ran out of commits to rebase before "
7574 "youngest common ancestor commit has "
7579 err = check_path_prefix(parent_id, commit_id,
7580 path_prefix, path_prefix_errcode, repo);
7584 err = got_object_qid_alloc(&qid, commit_id);
7587 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7588 commit_id = parent_id;
7592 got_commit_graph_close(graph);
7596 static const struct got_error *
7597 cmd_rebase(int argc, char *argv[])
7599 const struct got_error *error = NULL;
7600 struct got_worktree *worktree = NULL;
7601 struct got_repository *repo = NULL;
7602 struct got_fileindex *fileindex = NULL;
7604 struct got_reference *branch = NULL;
7605 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7606 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7607 struct got_object_id *resume_commit_id = NULL;
7608 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7609 struct got_commit_object *commit = NULL;
7610 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7611 int histedit_in_progress = 0;
7612 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7613 struct got_object_id_queue commits;
7614 struct got_pathlist_head merged_paths;
7615 const struct got_object_id_queue *parent_ids;
7616 struct got_object_qid *qid, *pid;
7618 SIMPLEQ_INIT(&commits);
7619 TAILQ_INIT(&merged_paths);
7621 while ((ch = getopt(argc, argv, "ac")) != -1) {
7627 continue_rebase = 1;
7639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7640 "unveil", NULL) == -1)
7643 if (abort_rebase && continue_rebase)
7645 else if (abort_rebase || continue_rebase) {
7648 } else if (argc != 1)
7651 cwd = getcwd(NULL, 0);
7653 error = got_error_from_errno("getcwd");
7656 error = got_worktree_open(&worktree, cwd);
7658 if (error->code == GOT_ERR_NOT_WORKTREE)
7659 error = wrap_not_worktree_error(error, "rebase", cwd);
7663 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7668 error = apply_unveil(got_repo_get_path(repo), 0,
7669 got_worktree_get_root_path(worktree));
7673 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7677 if (histedit_in_progress) {
7678 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7682 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7687 struct got_update_progress_arg upa;
7688 if (!rebase_in_progress) {
7689 error = got_error(GOT_ERR_NOT_REBASING);
7692 error = got_worktree_rebase_continue(&resume_commit_id,
7693 &new_base_branch, &tmp_branch, &branch, &fileindex,
7697 printf("Switching work tree to %s\n",
7698 got_ref_get_symref_target(new_base_branch));
7699 memset(&upa, 0, sizeof(upa));
7700 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7701 new_base_branch, update_progress, &upa);
7704 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7705 print_update_progress_stats(&upa);
7706 goto done; /* nothing else to do */
7709 if (continue_rebase) {
7710 if (!rebase_in_progress) {
7711 error = got_error(GOT_ERR_NOT_REBASING);
7714 error = got_worktree_rebase_continue(&resume_commit_id,
7715 &new_base_branch, &tmp_branch, &branch, &fileindex,
7720 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7721 resume_commit_id, repo);
7725 yca_id = got_object_id_dup(resume_commit_id);
7726 if (yca_id == NULL) {
7727 error = got_error_from_errno("got_object_id_dup");
7731 error = got_ref_open(&branch, repo, argv[0], 0);
7736 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7740 if (!continue_rebase) {
7741 struct got_object_id *base_commit_id;
7743 base_commit_id = got_worktree_get_base_commit_id(worktree);
7744 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7745 base_commit_id, branch_head_commit_id, repo,
7746 check_cancelled, NULL);
7749 if (yca_id == NULL) {
7750 error = got_error_msg(GOT_ERR_ANCESTRY,
7751 "specified branch shares no common ancestry "
7752 "with work tree's branch");
7756 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7758 if (error->code != GOT_ERR_ANCESTRY)
7762 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7763 "specified branch resolves to a commit which "
7764 "is already contained in work tree's branch");
7767 error = got_worktree_rebase_prepare(&new_base_branch,
7768 &tmp_branch, &fileindex, worktree, branch, repo);
7773 commit_id = branch_head_commit_id;
7774 error = got_object_open_as_commit(&commit, repo, commit_id);
7778 parent_ids = got_object_commit_get_parent_ids(commit);
7779 pid = SIMPLEQ_FIRST(parent_ids);
7781 if (!continue_rebase) {
7782 struct got_update_progress_arg upa;
7783 memset(&upa, 0, sizeof(upa));
7784 error = got_worktree_rebase_abort(worktree, fileindex,
7785 repo, new_base_branch, update_progress, &upa);
7788 printf("Rebase of %s aborted\n",
7789 got_ref_get_name(branch));
7790 print_update_progress_stats(&upa);
7793 error = got_error(GOT_ERR_EMPTY_REBASE);
7796 error = collect_commits(&commits, commit_id, pid->id,
7797 yca_id, got_worktree_get_path_prefix(worktree),
7798 GOT_ERR_REBASE_PATH, repo);
7799 got_object_commit_close(commit);
7804 if (SIMPLEQ_EMPTY(&commits)) {
7805 if (continue_rebase) {
7806 error = rebase_complete(worktree, fileindex,
7807 branch, new_base_branch, tmp_branch, repo);
7810 /* Fast-forward the reference of the branch. */
7811 struct got_object_id *new_head_commit_id;
7813 error = got_ref_resolve(&new_head_commit_id, repo,
7817 error = got_object_id_str(&id_str, new_head_commit_id);
7818 printf("Forwarding %s to commit %s\n",
7819 got_ref_get_name(branch), id_str);
7821 error = got_ref_change_ref(branch,
7822 new_head_commit_id);
7829 SIMPLEQ_FOREACH(qid, &commits, entry) {
7830 struct got_update_progress_arg upa;
7832 commit_id = qid->id;
7833 parent_id = pid ? pid->id : yca_id;
7836 memset(&upa, 0, sizeof(upa));
7837 error = got_worktree_rebase_merge_files(&merged_paths,
7838 worktree, fileindex, parent_id, commit_id, repo,
7839 update_progress, &upa, check_cancelled, NULL);
7843 print_update_progress_stats(&upa);
7844 if (upa.conflicts > 0)
7845 rebase_status = GOT_STATUS_CONFLICT;
7847 if (rebase_status == GOT_STATUS_CONFLICT) {
7848 error = show_rebase_merge_conflict(qid->id, repo);
7851 got_worktree_rebase_pathlist_free(&merged_paths);
7855 error = rebase_commit(&merged_paths, worktree, fileindex,
7856 tmp_branch, commit_id, repo);
7857 got_worktree_rebase_pathlist_free(&merged_paths);
7862 if (rebase_status == GOT_STATUS_CONFLICT) {
7863 error = got_worktree_rebase_postpone(worktree, fileindex);
7866 error = got_error_msg(GOT_ERR_CONFLICTS,
7867 "conflicts must be resolved before rebasing can continue");
7869 error = rebase_complete(worktree, fileindex, branch,
7870 new_base_branch, tmp_branch, repo);
7872 got_object_id_queue_free(&commits);
7873 free(branch_head_commit_id);
7874 free(resume_commit_id);
7877 got_object_commit_close(commit);
7879 got_ref_close(branch);
7880 if (new_base_branch)
7881 got_ref_close(new_base_branch);
7883 got_ref_close(tmp_branch);
7885 got_worktree_close(worktree);
7887 got_repo_close(repo);
7892 usage_histedit(void)
7894 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7899 #define GOT_HISTEDIT_PICK 'p'
7900 #define GOT_HISTEDIT_EDIT 'e'
7901 #define GOT_HISTEDIT_FOLD 'f'
7902 #define GOT_HISTEDIT_DROP 'd'
7903 #define GOT_HISTEDIT_MESG 'm'
7905 static struct got_histedit_cmd {
7909 } got_histedit_cmds[] = {
7910 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7911 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7912 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7914 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7915 { GOT_HISTEDIT_MESG, "mesg",
7916 "single-line log message for commit above (open editor if empty)" },
7919 struct got_histedit_list_entry {
7920 TAILQ_ENTRY(got_histedit_list_entry) entry;
7921 struct got_object_id *commit_id;
7922 const struct got_histedit_cmd *cmd;
7925 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7927 static const struct got_error *
7928 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7929 FILE *f, struct got_repository *repo)
7931 const struct got_error *err = NULL;
7932 char *logmsg = NULL, *id_str = NULL;
7933 struct got_commit_object *commit = NULL;
7936 err = got_object_open_as_commit(&commit, repo, commit_id);
7940 err = get_short_logmsg(&logmsg, 34, commit);
7944 err = got_object_id_str(&id_str, commit_id);
7948 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7950 err = got_ferror(f, GOT_ERR_IO);
7953 got_object_commit_close(commit);
7959 static const struct got_error *
7960 histedit_write_commit_list(struct got_object_id_queue *commits,
7961 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
7963 const struct got_error *err = NULL;
7964 struct got_object_qid *qid;
7965 const char *histedit_cmd = NULL;
7967 if (SIMPLEQ_EMPTY(commits))
7968 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7970 SIMPLEQ_FOREACH(qid, commits, entry) {
7971 histedit_cmd = got_histedit_cmds[0].name;
7972 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
7973 histedit_cmd = "fold";
7974 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
7977 if (edit_logmsg_only) {
7978 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7980 err = got_ferror(f, GOT_ERR_IO);
7989 static const struct got_error *
7990 write_cmd_list(FILE *f, const char *branch_name,
7991 struct got_object_id_queue *commits)
7993 const struct got_error *err = NULL;
7997 struct got_object_qid *qid;
7999 qid = SIMPLEQ_FIRST(commits);
8000 err = got_object_id_str(&id_str, qid->id);
8005 "# Editing the history of branch '%s' starting at\n"
8007 "# Commits will be processed in order from top to "
8008 "bottom of this file.\n", branch_name, id_str);
8010 err = got_ferror(f, GOT_ERR_IO);
8014 n = fprintf(f, "# Available histedit commands:\n");
8016 err = got_ferror(f, GOT_ERR_IO);
8020 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8021 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8022 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8025 err = got_ferror(f, GOT_ERR_IO);
8034 static const struct got_error *
8035 histedit_syntax_error(int lineno)
8037 static char msg[42];
8040 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8042 if (ret == -1 || ret >= sizeof(msg))
8043 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8045 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8048 static const struct got_error *
8049 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8050 char *logmsg, struct got_repository *repo)
8052 const struct got_error *err;
8053 struct got_commit_object *folded_commit = NULL;
8054 char *id_str, *folded_logmsg = NULL;
8056 err = got_object_id_str(&id_str, hle->commit_id);
8060 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8064 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8067 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8068 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8069 folded_logmsg) == -1) {
8070 err = got_error_from_errno("asprintf");
8074 got_object_commit_close(folded_commit);
8076 free(folded_logmsg);
8080 static struct got_histedit_list_entry *
8081 get_folded_commits(struct got_histedit_list_entry *hle)
8083 struct got_histedit_list_entry *prev, *folded = NULL;
8085 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8086 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8087 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8088 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8090 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8096 static const struct got_error *
8097 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8098 struct got_repository *repo)
8100 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8101 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8102 const struct got_error *err = NULL;
8103 struct got_commit_object *commit = NULL;
8106 struct got_histedit_list_entry *folded = NULL;
8108 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8112 folded = get_folded_commits(hle);
8114 while (folded != hle) {
8115 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8116 folded = TAILQ_NEXT(folded, entry);
8119 err = append_folded_commit_msg(&new_msg, folded,
8125 folded = TAILQ_NEXT(folded, entry);
8129 err = got_object_id_str(&id_str, hle->commit_id);
8132 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8135 logmsg_len = asprintf(&new_msg,
8136 "%s\n# original log message of commit %s: %s",
8137 logmsg ? logmsg : "", id_str, orig_logmsg);
8138 if (logmsg_len == -1) {
8139 err = got_error_from_errno("asprintf");
8145 err = got_object_id_str(&id_str, hle->commit_id);
8149 err = got_opentemp_named_fd(&logmsg_path, &fd,
8150 GOT_TMPDIR_STR "/got-logmsg");
8154 write(fd, logmsg, logmsg_len);
8157 err = get_editor(&editor);
8161 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8164 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8167 hle->logmsg = strdup(new_msg);
8168 if (hle->logmsg == NULL)
8169 err = got_error_from_errno("strdup");
8172 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8173 err = got_error_from_errno2("unlink", logmsg_path);
8179 got_object_commit_close(commit);
8183 static const struct got_error *
8184 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8185 FILE *f, struct got_repository *repo)
8187 const struct got_error *err = NULL;
8188 char *line = NULL, *p, *end;
8192 const struct got_histedit_cmd *cmd;
8193 struct got_object_id *commit_id = NULL;
8194 struct got_histedit_list_entry *hle = NULL;
8197 len = getline(&line, &size, f);
8199 const struct got_error *getline_err;
8202 getline_err = got_error_from_errno("getline");
8203 err = got_ferror(f, getline_err->code);
8208 while (isspace((unsigned char)p[0]))
8210 if (p[0] == '#' || p[0] == '\0') {
8216 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8217 cmd = &got_histedit_cmds[i];
8218 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8219 isspace((unsigned char)p[strlen(cmd->name)])) {
8220 p += strlen(cmd->name);
8223 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8228 if (i == nitems(got_histedit_cmds)) {
8229 err = histedit_syntax_error(lineno);
8232 while (isspace((unsigned char)p[0]))
8234 if (cmd->code == GOT_HISTEDIT_MESG) {
8235 if (hle == NULL || hle->logmsg != NULL) {
8236 err = got_error(GOT_ERR_HISTEDIT_CMD);
8240 err = histedit_edit_logmsg(hle, repo);
8244 hle->logmsg = strdup(p);
8245 if (hle->logmsg == NULL) {
8246 err = got_error_from_errno("strdup");
8255 while (end[0] && !isspace((unsigned char)end[0]))
8259 err = got_object_resolve_id_str(&commit_id, repo, p);
8261 /* override error code */
8262 err = histedit_syntax_error(lineno);
8266 hle = malloc(sizeof(*hle));
8268 err = got_error_from_errno("malloc");
8272 hle->commit_id = commit_id;
8277 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8285 static const struct got_error *
8286 histedit_check_script(struct got_histedit_list *histedit_cmds,
8287 struct got_object_id_queue *commits, struct got_repository *repo)
8289 const struct got_error *err = NULL;
8290 struct got_object_qid *qid;
8291 struct got_histedit_list_entry *hle;
8292 static char msg[92];
8295 if (TAILQ_EMPTY(histedit_cmds))
8296 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8297 "histedit script contains no commands");
8298 if (SIMPLEQ_EMPTY(commits))
8299 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8301 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8302 struct got_histedit_list_entry *hle2;
8303 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8306 if (got_object_id_cmp(hle->commit_id,
8307 hle2->commit_id) != 0)
8309 err = got_object_id_str(&id_str, hle->commit_id);
8312 snprintf(msg, sizeof(msg), "commit %s is listed "
8313 "more than once in histedit script", id_str);
8315 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8319 SIMPLEQ_FOREACH(qid, commits, entry) {
8320 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8321 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8325 err = got_object_id_str(&id_str, qid->id);
8328 snprintf(msg, sizeof(msg),
8329 "commit %s missing from histedit script", id_str);
8331 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8335 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8336 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8337 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8338 "last commit in histedit script cannot be folded");
8343 static const struct got_error *
8344 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8345 const char *path, struct got_object_id_queue *commits,
8346 struct got_repository *repo)
8348 const struct got_error *err = NULL;
8352 err = get_editor(&editor);
8356 if (spawn_editor(editor, path) == -1) {
8357 err = got_error_from_errno("failed spawning editor");
8361 f = fopen(path, "r");
8363 err = got_error_from_errno("fopen");
8366 err = histedit_parse_list(histedit_cmds, f, repo);
8370 err = histedit_check_script(histedit_cmds, commits, repo);
8372 if (f && fclose(f) != 0 && err == NULL)
8373 err = got_error_from_errno("fclose");
8378 static const struct got_error *
8379 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8380 struct got_object_id_queue *, const char *, const char *,
8381 struct got_repository *);
8383 static const struct got_error *
8384 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8385 struct got_object_id_queue *commits, const char *branch_name,
8386 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8388 const struct got_error *err;
8392 err = got_opentemp_named(&path, &f, "got-histedit");
8396 err = write_cmd_list(f, branch_name, commits);
8400 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8405 if (edit_logmsg_only || fold_only) {
8407 err = histedit_parse_list(histedit_cmds, f, repo);
8409 if (fclose(f) != 0) {
8410 err = got_error_from_errno("fclose");
8414 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8416 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8417 err->code != GOT_ERR_HISTEDIT_CMD)
8419 err = histedit_edit_list_retry(histedit_cmds, err,
8420 commits, path, branch_name, repo);
8424 if (f && fclose(f) != 0 && err == NULL)
8425 err = got_error_from_errno("fclose");
8426 if (path && unlink(path) != 0 && err == NULL)
8427 err = got_error_from_errno2("unlink", path);
8432 static const struct got_error *
8433 histedit_save_list(struct got_histedit_list *histedit_cmds,
8434 struct got_worktree *worktree, struct got_repository *repo)
8436 const struct got_error *err = NULL;
8439 struct got_histedit_list_entry *hle;
8440 struct got_commit_object *commit = NULL;
8442 err = got_worktree_get_histedit_script_path(&path, worktree);
8446 f = fopen(path, "w");
8448 err = got_error_from_errno2("fopen", path);
8451 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8452 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8458 int n = fprintf(f, "%c %s\n",
8459 GOT_HISTEDIT_MESG, hle->logmsg);
8461 err = got_ferror(f, GOT_ERR_IO);
8467 if (f && fclose(f) != 0 && err == NULL)
8468 err = got_error_from_errno("fclose");
8471 got_object_commit_close(commit);
8476 histedit_free_list(struct got_histedit_list *histedit_cmds)
8478 struct got_histedit_list_entry *hle;
8480 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8481 TAILQ_REMOVE(histedit_cmds, hle, entry);
8486 static const struct got_error *
8487 histedit_load_list(struct got_histedit_list *histedit_cmds,
8488 const char *path, struct got_repository *repo)
8490 const struct got_error *err = NULL;
8493 f = fopen(path, "r");
8495 err = got_error_from_errno2("fopen", path);
8499 err = histedit_parse_list(histedit_cmds, f, repo);
8501 if (f && fclose(f) != 0 && err == NULL)
8502 err = got_error_from_errno("fclose");
8506 static const struct got_error *
8507 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8508 const struct got_error *edit_err, struct got_object_id_queue *commits,
8509 const char *path, const char *branch_name, struct got_repository *repo)
8511 const struct got_error *err = NULL, *prev_err = edit_err;
8514 while (resp != 'c' && resp != 'r' && resp != 'a') {
8515 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8516 "or (a)bort: ", getprogname(), prev_err->msg);
8521 histedit_free_list(histedit_cmds);
8522 err = histedit_run_editor(histedit_cmds, path, commits,
8525 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8526 err->code != GOT_ERR_HISTEDIT_CMD)
8533 } else if (resp == 'r') {
8534 histedit_free_list(histedit_cmds);
8535 err = histedit_edit_script(histedit_cmds,
8536 commits, branch_name, 0, 0, repo);
8538 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8539 err->code != GOT_ERR_HISTEDIT_CMD)
8546 } else if (resp == 'a') {
8547 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8550 printf("invalid response '%c'\n", resp);
8556 static const struct got_error *
8557 histedit_complete(struct got_worktree *worktree,
8558 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8559 struct got_reference *branch, struct got_repository *repo)
8561 printf("Switching work tree to %s\n",
8562 got_ref_get_symref_target(branch));
8563 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8567 static const struct got_error *
8568 show_histedit_progress(struct got_commit_object *commit,
8569 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8571 const struct got_error *err;
8572 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8574 err = got_object_id_str(&old_id_str, hle->commit_id);
8579 err = got_object_id_str(&new_id_str, new_id);
8584 old_id_str[12] = '\0';
8586 new_id_str[12] = '\0';
8589 logmsg = strdup(hle->logmsg);
8590 if (logmsg == NULL) {
8591 err = got_error_from_errno("strdup");
8594 trim_logmsg(logmsg, 42);
8596 err = get_short_logmsg(&logmsg, 42, commit);
8601 switch (hle->cmd->code) {
8602 case GOT_HISTEDIT_PICK:
8603 case GOT_HISTEDIT_EDIT:
8604 printf("%s -> %s: %s\n", old_id_str,
8605 new_id_str ? new_id_str : "no-op change", logmsg);
8607 case GOT_HISTEDIT_DROP:
8608 case GOT_HISTEDIT_FOLD:
8609 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8621 static const struct got_error *
8622 histedit_commit(struct got_pathlist_head *merged_paths,
8623 struct got_worktree *worktree, struct got_fileindex *fileindex,
8624 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8625 struct got_repository *repo)
8627 const struct got_error *err;
8628 struct got_commit_object *commit;
8629 struct got_object_id *new_commit_id;
8631 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8632 && hle->logmsg == NULL) {
8633 err = histedit_edit_logmsg(hle, repo);
8638 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8642 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8643 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8646 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8648 err = show_histedit_progress(commit, hle, NULL);
8650 err = show_histedit_progress(commit, hle, new_commit_id);
8651 free(new_commit_id);
8654 got_object_commit_close(commit);
8658 static const struct got_error *
8659 histedit_skip_commit(struct got_histedit_list_entry *hle,
8660 struct got_worktree *worktree, struct got_repository *repo)
8662 const struct got_error *error;
8663 struct got_commit_object *commit;
8665 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8670 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8674 error = show_histedit_progress(commit, hle, NULL);
8675 got_object_commit_close(commit);
8679 static const struct got_error *
8680 check_local_changes(void *arg, unsigned char status,
8681 unsigned char staged_status, const char *path,
8682 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8683 struct got_object_id *commit_id, int dirfd, const char *de_name)
8685 int *have_local_changes = arg;
8688 case GOT_STATUS_ADD:
8689 case GOT_STATUS_DELETE:
8690 case GOT_STATUS_MODIFY:
8691 case GOT_STATUS_CONFLICT:
8692 *have_local_changes = 1;
8693 return got_error(GOT_ERR_CANCELLED);
8698 switch (staged_status) {
8699 case GOT_STATUS_ADD:
8700 case GOT_STATUS_DELETE:
8701 case GOT_STATUS_MODIFY:
8702 *have_local_changes = 1;
8703 return got_error(GOT_ERR_CANCELLED);
8711 static const struct got_error *
8712 cmd_histedit(int argc, char *argv[])
8714 const struct got_error *error = NULL;
8715 struct got_worktree *worktree = NULL;
8716 struct got_fileindex *fileindex = NULL;
8717 struct got_repository *repo = NULL;
8719 struct got_reference *branch = NULL;
8720 struct got_reference *tmp_branch = NULL;
8721 struct got_object_id *resume_commit_id = NULL;
8722 struct got_object_id *base_commit_id = NULL;
8723 struct got_object_id *head_commit_id = NULL;
8724 struct got_commit_object *commit = NULL;
8725 int ch, rebase_in_progress = 0;
8726 struct got_update_progress_arg upa;
8727 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8728 int edit_logmsg_only = 0, fold_only = 0;
8729 const char *edit_script_path = NULL;
8730 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8731 struct got_object_id_queue commits;
8732 struct got_pathlist_head merged_paths;
8733 const struct got_object_id_queue *parent_ids;
8734 struct got_object_qid *pid;
8735 struct got_histedit_list histedit_cmds;
8736 struct got_histedit_list_entry *hle;
8738 SIMPLEQ_INIT(&commits);
8739 TAILQ_INIT(&histedit_cmds);
8740 TAILQ_INIT(&merged_paths);
8741 memset(&upa, 0, sizeof(upa));
8743 while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8755 edit_script_path = optarg;
8758 edit_logmsg_only = 1;
8770 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8771 "unveil", NULL) == -1)
8774 if (abort_edit && continue_edit)
8775 option_conflict('a', 'c');
8776 if (edit_script_path && edit_logmsg_only)
8777 option_conflict('F', 'm');
8778 if (abort_edit && edit_logmsg_only)
8779 option_conflict('a', 'm');
8780 if (continue_edit && edit_logmsg_only)
8781 option_conflict('c', 'm');
8782 if (abort_edit && fold_only)
8783 option_conflict('a', 'f');
8784 if (continue_edit && fold_only)
8785 option_conflict('c', 'f');
8786 if (fold_only && edit_logmsg_only)
8787 option_conflict('f', 'm');
8788 if (edit_script_path && fold_only)
8789 option_conflict('F', 'f');
8794 * This command cannot apply unveil(2) in all cases because the
8795 * user may choose to run an editor to edit the histedit script
8796 * and to edit individual commit log messages.
8797 * unveil(2) traverses exec(2); if an editor is used we have to
8798 * apply unveil after edit script and log messages have been written.
8799 * XXX TODO: Make use of unveil(2) where possible.
8802 cwd = getcwd(NULL, 0);
8804 error = got_error_from_errno("getcwd");
8807 error = got_worktree_open(&worktree, cwd);
8809 if (error->code == GOT_ERR_NOT_WORKTREE)
8810 error = wrap_not_worktree_error(error, "histedit", cwd);
8814 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8819 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8822 if (rebase_in_progress) {
8823 error = got_error(GOT_ERR_REBASING);
8827 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8831 if (edit_in_progress && edit_logmsg_only) {
8832 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8833 "histedit operation is in progress in this "
8834 "work tree and must be continued or aborted "
8835 "before the -m option can be used");
8838 if (edit_in_progress && fold_only) {
8839 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8840 "histedit operation is in progress in this "
8841 "work tree and must be continued or aborted "
8842 "before the -f option can be used");
8846 if (edit_in_progress && abort_edit) {
8847 error = got_worktree_histedit_continue(&resume_commit_id,
8848 &tmp_branch, &branch, &base_commit_id, &fileindex,
8852 printf("Switching work tree to %s\n",
8853 got_ref_get_symref_target(branch));
8854 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8855 branch, base_commit_id, update_progress, &upa);
8858 printf("Histedit of %s aborted\n",
8859 got_ref_get_symref_target(branch));
8860 print_update_progress_stats(&upa);
8861 goto done; /* nothing else to do */
8862 } else if (abort_edit) {
8863 error = got_error(GOT_ERR_NOT_HISTEDIT);
8867 if (continue_edit) {
8870 if (!edit_in_progress) {
8871 error = got_error(GOT_ERR_NOT_HISTEDIT);
8875 error = got_worktree_get_histedit_script_path(&path, worktree);
8879 error = histedit_load_list(&histedit_cmds, path, repo);
8884 error = got_worktree_histedit_continue(&resume_commit_id,
8885 &tmp_branch, &branch, &base_commit_id, &fileindex,
8890 error = got_ref_resolve(&head_commit_id, repo, branch);
8894 error = got_object_open_as_commit(&commit, repo,
8898 parent_ids = got_object_commit_get_parent_ids(commit);
8899 pid = SIMPLEQ_FIRST(parent_ids);
8901 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8904 error = collect_commits(&commits, head_commit_id, pid->id,
8905 base_commit_id, got_worktree_get_path_prefix(worktree),
8906 GOT_ERR_HISTEDIT_PATH, repo);
8907 got_object_commit_close(commit);
8912 if (edit_in_progress) {
8913 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8917 error = got_ref_open(&branch, repo,
8918 got_worktree_get_head_ref_name(worktree), 0);
8922 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8923 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8924 "will not edit commit history of a branch outside "
8925 "the \"refs/heads/\" reference namespace");
8929 error = got_ref_resolve(&head_commit_id, repo, branch);
8930 got_ref_close(branch);
8935 error = got_object_open_as_commit(&commit, repo,
8939 parent_ids = got_object_commit_get_parent_ids(commit);
8940 pid = SIMPLEQ_FIRST(parent_ids);
8942 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8945 error = collect_commits(&commits, head_commit_id, pid->id,
8946 got_worktree_get_base_commit_id(worktree),
8947 got_worktree_get_path_prefix(worktree),
8948 GOT_ERR_HISTEDIT_PATH, repo);
8949 got_object_commit_close(commit);
8954 if (SIMPLEQ_EMPTY(&commits)) {
8955 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8959 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8960 &base_commit_id, &fileindex, worktree, repo);
8964 if (edit_script_path) {
8965 error = histedit_load_list(&histedit_cmds,
8966 edit_script_path, repo);
8968 got_worktree_histedit_abort(worktree, fileindex,
8969 repo, branch, base_commit_id,
8970 update_progress, &upa);
8971 print_update_progress_stats(&upa);
8975 const char *branch_name;
8976 branch_name = got_ref_get_symref_target(branch);
8977 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8979 error = histedit_edit_script(&histedit_cmds, &commits,
8980 branch_name, edit_logmsg_only, fold_only, repo);
8982 got_worktree_histedit_abort(worktree, fileindex,
8983 repo, branch, base_commit_id,
8984 update_progress, &upa);
8985 print_update_progress_stats(&upa);
8991 error = histedit_save_list(&histedit_cmds, worktree,
8994 got_worktree_histedit_abort(worktree, fileindex,
8995 repo, branch, base_commit_id,
8996 update_progress, &upa);
8997 print_update_progress_stats(&upa);
9003 error = histedit_check_script(&histedit_cmds, &commits, repo);
9007 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9008 if (resume_commit_id) {
9009 if (got_object_id_cmp(hle->commit_id,
9010 resume_commit_id) != 0)
9013 resume_commit_id = NULL;
9014 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9015 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9016 error = histedit_skip_commit(hle, worktree,
9021 struct got_pathlist_head paths;
9022 int have_changes = 0;
9025 error = got_pathlist_append(&paths, "", NULL);
9028 error = got_worktree_status(worktree, &paths,
9029 repo, check_local_changes, &have_changes,
9030 check_cancelled, NULL);
9031 got_pathlist_free(&paths);
9033 if (error->code != GOT_ERR_CANCELLED)
9035 if (sigint_received || sigpipe_received)
9039 error = histedit_commit(NULL, worktree,
9040 fileindex, tmp_branch, hle, repo);
9044 error = got_object_open_as_commit(
9045 &commit, repo, hle->commit_id);
9048 error = show_histedit_progress(commit,
9050 got_object_commit_close(commit);
9059 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9060 error = histedit_skip_commit(hle, worktree, repo);
9066 error = got_object_open_as_commit(&commit, repo,
9070 parent_ids = got_object_commit_get_parent_ids(commit);
9071 pid = SIMPLEQ_FIRST(parent_ids);
9073 error = got_worktree_histedit_merge_files(&merged_paths,
9074 worktree, fileindex, pid->id, hle->commit_id, repo,
9075 update_progress, &upa, check_cancelled, NULL);
9078 got_object_commit_close(commit);
9081 print_update_progress_stats(&upa);
9082 if (upa.conflicts > 0)
9083 rebase_status = GOT_STATUS_CONFLICT;
9085 if (rebase_status == GOT_STATUS_CONFLICT) {
9086 error = show_rebase_merge_conflict(hle->commit_id,
9090 got_worktree_rebase_pathlist_free(&merged_paths);
9094 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9096 error = got_object_id_str(&id_str, hle->commit_id);
9099 printf("Stopping histedit for amending commit %s\n",
9102 got_worktree_rebase_pathlist_free(&merged_paths);
9103 error = got_worktree_histedit_postpone(worktree,
9108 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9109 error = histedit_skip_commit(hle, worktree, repo);
9115 error = histedit_commit(&merged_paths, worktree, fileindex,
9116 tmp_branch, hle, repo);
9117 got_worktree_rebase_pathlist_free(&merged_paths);
9122 if (rebase_status == GOT_STATUS_CONFLICT) {
9123 error = got_worktree_histedit_postpone(worktree, fileindex);
9126 error = got_error_msg(GOT_ERR_CONFLICTS,
9127 "conflicts must be resolved before histedit can continue");
9129 error = histedit_complete(worktree, fileindex, tmp_branch,
9132 got_object_id_queue_free(&commits);
9133 histedit_free_list(&histedit_cmds);
9134 free(head_commit_id);
9135 free(base_commit_id);
9136 free(resume_commit_id);
9138 got_object_commit_close(commit);
9140 got_ref_close(branch);
9142 got_ref_close(tmp_branch);
9144 got_worktree_close(worktree);
9146 got_repo_close(repo);
9151 usage_integrate(void)
9153 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9157 static const struct got_error *
9158 cmd_integrate(int argc, char *argv[])
9160 const struct got_error *error = NULL;
9161 struct got_repository *repo = NULL;
9162 struct got_worktree *worktree = NULL;
9163 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9164 const char *branch_arg = NULL;
9165 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9166 struct got_fileindex *fileindex = NULL;
9167 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9169 struct got_update_progress_arg upa;
9171 while ((ch = getopt(argc, argv, "")) != -1) {
9184 branch_arg = argv[0];
9186 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9187 "unveil", NULL) == -1)
9190 cwd = getcwd(NULL, 0);
9192 error = got_error_from_errno("getcwd");
9196 error = got_worktree_open(&worktree, cwd);
9198 if (error->code == GOT_ERR_NOT_WORKTREE)
9199 error = wrap_not_worktree_error(error, "integrate",
9204 error = check_rebase_or_histedit_in_progress(worktree);
9208 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9213 error = apply_unveil(got_repo_get_path(repo), 0,
9214 got_worktree_get_root_path(worktree));
9218 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9219 error = got_error_from_errno("asprintf");
9223 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9224 &base_branch_ref, worktree, refname, repo);
9228 refname = strdup(got_ref_get_name(branch_ref));
9229 if (refname == NULL) {
9230 error = got_error_from_errno("strdup");
9231 got_worktree_integrate_abort(worktree, fileindex, repo,
9232 branch_ref, base_branch_ref);
9235 base_refname = strdup(got_ref_get_name(base_branch_ref));
9236 if (base_refname == NULL) {
9237 error = got_error_from_errno("strdup");
9238 got_worktree_integrate_abort(worktree, fileindex, repo,
9239 branch_ref, base_branch_ref);
9243 error = got_ref_resolve(&commit_id, repo, branch_ref);
9247 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9251 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9252 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9253 "specified branch has already been integrated");
9254 got_worktree_integrate_abort(worktree, fileindex, repo,
9255 branch_ref, base_branch_ref);
9259 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9261 if (error->code == GOT_ERR_ANCESTRY)
9262 error = got_error(GOT_ERR_REBASE_REQUIRED);
9263 got_worktree_integrate_abort(worktree, fileindex, repo,
9264 branch_ref, base_branch_ref);
9268 memset(&upa, 0, sizeof(upa));
9269 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9270 branch_ref, base_branch_ref, update_progress, &upa,
9271 check_cancelled, NULL);
9275 printf("Integrated %s into %s\n", refname, base_refname);
9276 print_update_progress_stats(&upa);
9279 got_repo_close(repo);
9281 got_worktree_close(worktree);
9283 free(base_commit_id);
9293 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9294 "[-S] [file-path ...]\n",
9299 static const struct got_error *
9300 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9301 const char *path, struct got_object_id *blob_id,
9302 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9303 int dirfd, const char *de_name)
9305 const struct got_error *err = NULL;
9306 char *id_str = NULL;
9308 if (staged_status != GOT_STATUS_ADD &&
9309 staged_status != GOT_STATUS_MODIFY &&
9310 staged_status != GOT_STATUS_DELETE)
9313 if (staged_status == GOT_STATUS_ADD ||
9314 staged_status == GOT_STATUS_MODIFY)
9315 err = got_object_id_str(&id_str, staged_blob_id);
9317 err = got_object_id_str(&id_str, blob_id);
9321 printf("%s %c %s\n", id_str, staged_status, path);
9326 static const struct got_error *
9327 cmd_stage(int argc, char *argv[])
9329 const struct got_error *error = NULL;
9330 struct got_repository *repo = NULL;
9331 struct got_worktree *worktree = NULL;
9333 struct got_pathlist_head paths;
9334 struct got_pathlist_entry *pe;
9335 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9336 FILE *patch_script_file = NULL;
9337 const char *patch_script_path = NULL;
9338 struct choose_patch_arg cpa;
9342 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9351 patch_script_path = optarg;
9354 allow_bad_symlinks = 1;
9366 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9367 "unveil", NULL) == -1)
9370 if (list_stage && (pflag || patch_script_path))
9371 errx(1, "-l option cannot be used with other options");
9372 if (patch_script_path && !pflag)
9373 errx(1, "-F option can only be used together with -p option");
9375 cwd = getcwd(NULL, 0);
9377 error = got_error_from_errno("getcwd");
9381 error = got_worktree_open(&worktree, cwd);
9383 if (error->code == GOT_ERR_NOT_WORKTREE)
9384 error = wrap_not_worktree_error(error, "stage", cwd);
9388 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9393 if (patch_script_path) {
9394 patch_script_file = fopen(patch_script_path, "r");
9395 if (patch_script_file == NULL) {
9396 error = got_error_from_errno2("fopen",
9401 error = apply_unveil(got_repo_get_path(repo), 0,
9402 got_worktree_get_root_path(worktree));
9406 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9411 error = got_worktree_status(worktree, &paths, repo,
9412 print_stage, NULL, check_cancelled, NULL);
9414 cpa.patch_script_file = patch_script_file;
9415 cpa.action = "stage";
9416 error = got_worktree_stage(worktree, &paths,
9417 pflag ? NULL : print_status, NULL,
9418 pflag ? choose_patch : NULL, &cpa,
9419 allow_bad_symlinks, repo);
9422 if (patch_script_file && fclose(patch_script_file) == EOF &&
9424 error = got_error_from_errno2("fclose", patch_script_path);
9426 got_repo_close(repo);
9428 got_worktree_close(worktree);
9429 TAILQ_FOREACH(pe, &paths, entry)
9430 free((char *)pe->path);
9431 got_pathlist_free(&paths);
9439 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9440 "[file-path ...]\n",
9446 static const struct got_error *
9447 cmd_unstage(int argc, char *argv[])
9449 const struct got_error *error = NULL;
9450 struct got_repository *repo = NULL;
9451 struct got_worktree *worktree = NULL;
9453 struct got_pathlist_head paths;
9454 struct got_pathlist_entry *pe;
9456 struct got_update_progress_arg upa;
9457 FILE *patch_script_file = NULL;
9458 const char *patch_script_path = NULL;
9459 struct choose_patch_arg cpa;
9463 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9469 patch_script_path = optarg;
9481 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9482 "unveil", NULL) == -1)
9485 if (patch_script_path && !pflag)
9486 errx(1, "-F option can only be used together with -p option");
9488 cwd = getcwd(NULL, 0);
9490 error = got_error_from_errno("getcwd");
9494 error = got_worktree_open(&worktree, cwd);
9496 if (error->code == GOT_ERR_NOT_WORKTREE)
9497 error = wrap_not_worktree_error(error, "unstage", cwd);
9501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9506 if (patch_script_path) {
9507 patch_script_file = fopen(patch_script_path, "r");
9508 if (patch_script_file == NULL) {
9509 error = got_error_from_errno2("fopen",
9515 error = apply_unveil(got_repo_get_path(repo), 0,
9516 got_worktree_get_root_path(worktree));
9520 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9524 cpa.patch_script_file = patch_script_file;
9525 cpa.action = "unstage";
9526 memset(&upa, 0, sizeof(upa));
9527 error = got_worktree_unstage(worktree, &paths, update_progress,
9528 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9530 print_update_progress_stats(&upa);
9532 if (patch_script_file && fclose(patch_script_file) == EOF &&
9534 error = got_error_from_errno2("fclose", patch_script_path);
9536 got_repo_close(repo);
9538 got_worktree_close(worktree);
9539 TAILQ_FOREACH(pe, &paths, entry)
9540 free((char *)pe->path);
9541 got_pathlist_free(&paths);
9549 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9550 "arg1 [arg2 ...]\n", getprogname());
9554 static const struct got_error *
9555 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9557 const struct got_error *err;
9558 struct got_blob_object *blob;
9560 err = got_object_open_as_blob(&blob, repo, id, 8192);
9564 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9565 got_object_blob_close(blob);
9569 static const struct got_error *
9570 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9572 const struct got_error *err;
9573 struct got_tree_object *tree;
9576 err = got_object_open_as_tree(&tree, repo, id);
9580 nentries = got_object_tree_get_nentries(tree);
9581 for (i = 0; i < nentries; i++) {
9582 struct got_tree_entry *te;
9584 if (sigint_received || sigpipe_received)
9586 te = got_object_tree_get_entry(tree, i);
9587 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9590 fprintf(outfile, "%s %.7o %s\n", id_str,
9591 got_tree_entry_get_mode(te),
9592 got_tree_entry_get_name(te));
9596 got_object_tree_close(tree);
9600 static const struct got_error *
9601 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9603 const struct got_error *err;
9604 struct got_commit_object *commit;
9605 const struct got_object_id_queue *parent_ids;
9606 struct got_object_qid *pid;
9607 char *id_str = NULL;
9608 const char *logmsg = NULL;
9610 err = got_object_open_as_commit(&commit, repo, id);
9614 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9618 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9619 parent_ids = got_object_commit_get_parent_ids(commit);
9620 fprintf(outfile, "numparents %d\n",
9621 got_object_commit_get_nparents(commit));
9622 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9624 err = got_object_id_str(&pid_str, pid->id);
9627 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9630 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9631 got_object_commit_get_author(commit),
9632 (long long)got_object_commit_get_author_time(commit));
9634 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9635 got_object_commit_get_author(commit),
9636 (long long)got_object_commit_get_committer_time(commit));
9638 logmsg = got_object_commit_get_logmsg_raw(commit);
9639 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9640 fprintf(outfile, "%s", logmsg);
9643 got_object_commit_close(commit);
9647 static const struct got_error *
9648 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9650 const struct got_error *err;
9651 struct got_tag_object *tag;
9652 char *id_str = NULL;
9653 const char *tagmsg = NULL;
9655 err = got_object_open_as_tag(&tag, repo, id);
9659 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9663 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9665 switch (got_object_tag_get_object_type(tag)) {
9666 case GOT_OBJ_TYPE_BLOB:
9667 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9668 GOT_OBJ_LABEL_BLOB);
9670 case GOT_OBJ_TYPE_TREE:
9671 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9672 GOT_OBJ_LABEL_TREE);
9674 case GOT_OBJ_TYPE_COMMIT:
9675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9676 GOT_OBJ_LABEL_COMMIT);
9678 case GOT_OBJ_TYPE_TAG:
9679 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9686 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9687 got_object_tag_get_name(tag));
9689 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9690 got_object_tag_get_tagger(tag),
9691 (long long)got_object_tag_get_tagger_time(tag));
9693 tagmsg = got_object_tag_get_message(tag);
9694 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9695 fprintf(outfile, "%s", tagmsg);
9698 got_object_tag_close(tag);
9702 static const struct got_error *
9703 cmd_cat(int argc, char *argv[])
9705 const struct got_error *error;
9706 struct got_repository *repo = NULL;
9707 struct got_worktree *worktree = NULL;
9708 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9709 const char *commit_id_str = NULL;
9710 struct got_object_id *id = NULL, *commit_id = NULL;
9711 int ch, obj_type, i, force_path = 0;
9712 struct got_reflist_head refs;
9717 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9722 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9725 commit_id_str = optarg;
9728 repo_path = realpath(optarg, NULL);
9729 if (repo_path == NULL)
9730 return got_error_from_errno2("realpath",
9732 got_path_strip_trailing_slashes(repo_path);
9746 cwd = getcwd(NULL, 0);
9748 error = got_error_from_errno("getcwd");
9751 error = got_worktree_open(&worktree, cwd);
9752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9755 if (repo_path == NULL) {
9757 got_worktree_get_repo_path(worktree));
9758 if (repo_path == NULL) {
9759 error = got_error_from_errno("strdup");
9765 if (repo_path == NULL) {
9766 repo_path = getcwd(NULL, 0);
9767 if (repo_path == NULL)
9768 return got_error_from_errno("getcwd");
9771 error = got_repo_open(&repo, repo_path, NULL);
9776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9780 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9784 if (commit_id_str == NULL)
9785 commit_id_str = GOT_REF_HEAD;
9786 error = got_repo_match_object_id(&commit_id, NULL,
9787 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9791 for (i = 0; i < argc; i++) {
9793 error = got_object_id_by_path(&id, repo, commit_id,
9798 error = got_repo_match_object_id(&id, &label, argv[i],
9799 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9802 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9803 error->code != GOT_ERR_NOT_REF)
9805 error = got_object_id_by_path(&id, repo,
9806 commit_id, argv[i]);
9812 error = got_object_get_type(&obj_type, repo, id);
9817 case GOT_OBJ_TYPE_BLOB:
9818 error = cat_blob(id, repo, stdout);
9820 case GOT_OBJ_TYPE_TREE:
9821 error = cat_tree(id, repo, stdout);
9823 case GOT_OBJ_TYPE_COMMIT:
9824 error = cat_commit(id, repo, stdout);
9826 case GOT_OBJ_TYPE_TAG:
9827 error = cat_tag(id, repo, stdout);
9830 error = got_error(GOT_ERR_OBJ_TYPE);
9845 got_worktree_close(worktree);
9847 const struct got_error *repo_error;
9848 repo_error = got_repo_close(repo);
9852 got_ref_list_free(&refs);
9859 fprintf(stderr, "usage: %s info [path ...]\n",
9864 static const struct got_error *
9865 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9866 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9867 struct got_object_id *commit_id)
9869 const struct got_error *err = NULL;
9870 char *id_str = NULL;
9872 struct tm mytm, *tm;
9873 struct got_pathlist_head *paths = arg;
9874 struct got_pathlist_entry *pe;
9877 * Clear error indication from any of the path arguments which
9878 * would cause this file index entry to be displayed.
9880 TAILQ_FOREACH(pe, paths, entry) {
9881 if (got_path_cmp(path, pe->path, strlen(path),
9882 pe->path_len) == 0 ||
9883 got_path_is_child(path, pe->path, pe->path_len))
9884 pe->data = NULL; /* no error */
9887 printf(GOT_COMMIT_SEP_STR);
9889 printf("symlink: %s\n", path);
9890 else if (S_ISREG(mode)) {
9891 printf("file: %s\n", path);
9892 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9893 } else if (S_ISDIR(mode))
9894 printf("directory: %s\n", path);
9896 printf("something: %s\n", path);
9898 tm = localtime_r(&mtime, &mytm);
9901 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9902 return got_error(GOT_ERR_NO_SPACE);
9903 printf("timestamp: %s\n", datebuf);
9906 err = got_object_id_str(&id_str, blob_id);
9909 printf("based on blob: %s\n", id_str);
9913 if (staged_blob_id) {
9914 err = got_object_id_str(&id_str, staged_blob_id);
9917 printf("based on staged blob: %s\n", id_str);
9922 err = got_object_id_str(&id_str, commit_id);
9925 printf("based on commit: %s\n", id_str);
9932 static const struct got_error *
9933 cmd_info(int argc, char *argv[])
9935 const struct got_error *error = NULL;
9936 struct got_worktree *worktree = NULL;
9937 char *cwd = NULL, *id_str = NULL;
9938 struct got_pathlist_head paths;
9939 struct got_pathlist_entry *pe;
9940 char *uuidstr = NULL;
9941 int ch, show_files = 0;
9945 while ((ch = getopt(argc, argv, "")) != -1) {
9957 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9961 cwd = getcwd(NULL, 0);
9963 error = got_error_from_errno("getcwd");
9967 error = got_worktree_open(&worktree, cwd);
9969 if (error->code == GOT_ERR_NOT_WORKTREE)
9970 error = wrap_not_worktree_error(error, "status", cwd);
9974 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9979 error = get_worktree_paths_from_argv(&paths, argc, argv,
9986 error = got_object_id_str(&id_str,
9987 got_worktree_get_base_commit_id(worktree));
9991 error = got_worktree_get_uuid(&uuidstr, worktree);
9995 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9996 printf("work tree base commit: %s\n", id_str);
9997 printf("work tree path prefix: %s\n",
9998 got_worktree_get_path_prefix(worktree));
9999 printf("work tree branch reference: %s\n",
10000 got_worktree_get_head_ref_name(worktree));
10001 printf("work tree UUID: %s\n", uuidstr);
10002 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10005 struct got_pathlist_entry *pe;
10006 TAILQ_FOREACH(pe, &paths, entry) {
10007 if (pe->path_len == 0)
10010 * Assume this path will fail. This will be corrected
10011 * in print_path_info() in case the path does suceeed.
10013 pe->data = (void *)got_error_path(pe->path,
10016 error = got_worktree_path_info(worktree, &paths,
10017 print_path_info, &paths, check_cancelled, NULL);
10020 TAILQ_FOREACH(pe, &paths, entry) {
10021 if (pe->data != NULL) {
10022 error = pe->data; /* bad path */
10028 TAILQ_FOREACH(pe, &paths, entry)
10029 free((char *)pe->path);
10030 got_pathlist_free(&paths);