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);
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, "" },
178 fprintf(stderr, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(stderr, " %s", cmd->cmd_name);
187 main(int argc, char *argv[])
192 int hflag = 0, Vflag = 0;
193 static struct option longopts[] = {
194 { "version", no_argument, NULL, 'V' },
198 setlocale(LC_CTYPE, "");
200 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
220 got_version_print_str();
227 signal(SIGINT, catch_sigint);
228 signal(SIGPIPE, catch_sigpipe);
230 for (i = 0; i < nitems(got_commands); i++) {
231 const struct got_error *error;
233 cmd = &got_commands[i];
235 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
236 strcmp(cmd->cmd_alias, argv[0]) != 0)
240 got_commands[i].cmd_usage();
242 error = got_commands[i].cmd_main(argc, argv);
243 if (error && error->code != GOT_ERR_CANCELLED &&
244 error->code != GOT_ERR_PRIVSEP_EXIT &&
245 !(sigpipe_received &&
246 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
248 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
249 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
256 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
264 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
271 static const struct got_error *
272 get_editor(char **abspath)
274 const struct got_error *err = NULL;
279 editor = getenv("VISUAL");
281 editor = getenv("EDITOR");
284 err = got_path_find_prog(abspath, editor);
289 if (*abspath == NULL) {
290 *abspath = strdup("/bin/ed");
291 if (*abspath == NULL)
292 return got_error_from_errno("strdup");
298 static const struct got_error *
299 apply_unveil(const char *repo_path, int repo_read_only,
300 const char *worktree_path)
302 const struct got_error *err;
305 if (unveil("gmon.out", "rwc") != 0)
306 return got_error_from_errno2("unveil", "gmon.out");
308 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
309 return got_error_from_errno2("unveil", repo_path);
311 if (worktree_path && unveil(worktree_path, "rwc") != 0)
312 return got_error_from_errno2("unveil", worktree_path);
314 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
317 err = got_privsep_unveil_exec_helpers();
321 if (unveil(NULL, NULL) != 0)
322 return got_error_from_errno("unveil");
330 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
334 static const struct got_error *
335 cmd_init(int argc, char *argv[])
337 const struct got_error *error = NULL;
338 char *repo_path = NULL;
341 while ((ch = getopt(argc, argv, "")) != -1) {
353 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
359 repo_path = strdup(argv[0]);
360 if (repo_path == NULL)
361 return got_error_from_errno("strdup");
363 got_path_strip_trailing_slashes(repo_path);
365 error = got_path_mkdir(repo_path);
367 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
370 error = apply_unveil(repo_path, 0, NULL);
374 error = got_repo_init(repo_path);
383 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
384 "[-r repository-path] [-I pattern] path\n", getprogname());
389 spawn_editor(const char *editor, const char *file)
392 sig_t sighup, sigint, sigquit;
395 sighup = signal(SIGHUP, SIG_IGN);
396 sigint = signal(SIGINT, SIG_IGN);
397 sigquit = signal(SIGQUIT, SIG_IGN);
399 switch (pid = fork()) {
403 execl(editor, editor, file, (char *)NULL);
407 while (waitpid(pid, &st, 0) == -1)
412 (void)signal(SIGHUP, sighup);
413 (void)signal(SIGINT, sigint);
414 (void)signal(SIGQUIT, sigquit);
416 if (!WIFEXITED(st)) {
421 return WEXITSTATUS(st);
424 static const struct got_error *
425 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
426 const char *initial_content, size_t initial_content_len)
428 const struct got_error *err = NULL;
434 size_t len, logmsg_len;
435 char *initial_content_stripped = NULL, *buf = NULL, *s;
439 if (stat(logmsg_path, &st) == -1)
440 return got_error_from_errno2("stat", logmsg_path);
442 if (spawn_editor(editor, logmsg_path) == -1)
443 return got_error_from_errno("failed spawning editor");
445 if (stat(logmsg_path, &st2) == -1)
446 return got_error_from_errno("stat");
448 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
449 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
450 "no changes made to commit message, aborting");
453 * Set up a stripped version of the initial content without comments
454 * and blank lines. We need this in order to check if the message
455 * has in fact been edited.
457 initial_content_stripped = malloc(initial_content_len + 1);
458 if (initial_content_stripped == NULL)
459 return got_error_from_errno("malloc");
460 initial_content_stripped[0] = '\0';
462 buf = strdup(initial_content);
464 err = got_error_from_errno("strdup");
469 while ((line = strsep(&s, "\n")) != NULL) {
470 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
471 continue; /* remove comments and leading empty lines */
472 len = strlcat(initial_content_stripped, line,
473 initial_content_len + 1);
474 if (len >= initial_content_len + 1) {
475 err = got_error(GOT_ERR_NO_SPACE);
479 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
480 initial_content_stripped[len - 1] = '\0';
484 logmsg_len = st2.st_size;
485 *logmsg = malloc(logmsg_len + 1);
487 return got_error_from_errno("malloc");
490 fp = fopen(logmsg_path, "r");
492 err = got_error_from_errno("fopen");
497 while ((linelen = getline(&line, &linesize, fp)) != -1) {
498 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
499 continue; /* remove comments and leading empty lines */
500 len = strlcat(*logmsg, line, logmsg_len + 1);
501 if (len >= logmsg_len + 1) {
502 err = got_error(GOT_ERR_NO_SPACE);
508 err = got_ferror(fp, GOT_ERR_IO);
511 while (len > 0 && (*logmsg)[len - 1] == '\n') {
512 (*logmsg)[len - 1] = '\0';
517 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
518 "commit message cannot be empty, aborting");
521 if (strcmp(*logmsg, initial_content_stripped) == 0)
522 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
523 "no changes made to commit message, aborting");
525 free(initial_content_stripped);
527 if (fp && fclose(fp) == EOF && err == NULL)
528 err = got_error_from_errno("fclose");
536 static const struct got_error *
537 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
538 const char *path_dir, const char *branch_name)
540 char *initial_content = NULL;
541 const struct got_error *err = NULL;
542 int initial_content_len;
545 initial_content_len = asprintf(&initial_content,
546 "\n# %s to be imported to branch %s\n", path_dir,
548 if (initial_content_len == -1)
549 return got_error_from_errno("asprintf");
551 err = got_opentemp_named_fd(logmsg_path, &fd,
552 GOT_TMPDIR_STR "/got-importmsg");
556 if (write(fd, initial_content, initial_content_len) == -1) {
557 err = got_error_from_errno2("write", *logmsg_path);
561 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
562 initial_content_len);
564 if (fd != -1 && close(fd) == -1 && err == NULL)
565 err = got_error_from_errno2("close", *logmsg_path);
566 free(initial_content);
574 static const struct got_error *
575 import_progress(void *arg, const char *path)
577 printf("A %s\n", path);
581 static const struct got_error *
582 get_author(char **author, struct got_repository *repo,
583 struct got_worktree *worktree)
585 const struct got_error *err = NULL;
586 const char *got_author = NULL, *name, *email;
587 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
592 worktree_conf = got_worktree_get_gotconfig(worktree);
593 repo_conf = got_repo_get_gotconfig(repo);
596 * Priority of potential author information sources, from most
597 * significant to least significant:
598 * 1) work tree's .got/got.conf file
599 * 2) repository's got.conf file
600 * 3) repository's git config file
601 * 4) environment variables
602 * 5) global git config files (in user's home directory or /etc)
606 got_author = got_gotconfig_get_author(worktree_conf);
607 if (got_author == NULL)
608 got_author = got_gotconfig_get_author(repo_conf);
609 if (got_author == NULL) {
610 name = got_repo_get_gitconfig_author_name(repo);
611 email = got_repo_get_gitconfig_author_email(repo);
613 if (asprintf(author, "%s <%s>", name, email) == -1)
614 return got_error_from_errno("asprintf");
618 got_author = getenv("GOT_AUTHOR");
619 if (got_author == NULL) {
620 name = got_repo_get_global_gitconfig_author_name(repo);
621 email = got_repo_get_global_gitconfig_author_email(
624 if (asprintf(author, "%s <%s>", name, email)
626 return got_error_from_errno("asprintf");
629 /* TODO: Look up user in password database? */
630 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
634 *author = strdup(got_author);
636 return got_error_from_errno("strdup");
639 * Really dumb email address check; we're only doing this to
640 * avoid git's object parser breaking on commits we create.
642 while (*got_author && *got_author != '<')
644 if (*got_author != '<') {
645 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
648 while (*got_author && *got_author != '@')
650 if (*got_author != '@') {
651 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
654 while (*got_author && *got_author != '>')
656 if (*got_author != '>')
657 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 static const struct got_error *
667 get_gitconfig_path(char **gitconfig_path)
669 const char *homedir = getenv("HOME");
671 *gitconfig_path = NULL;
673 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
674 return got_error_from_errno("asprintf");
680 static const struct got_error *
681 cmd_import(int argc, char *argv[])
683 const struct got_error *error = NULL;
684 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
685 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
686 const char *branch_name = "main";
687 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
688 struct got_repository *repo = NULL;
689 struct got_reference *branch_ref = NULL, *head_ref = NULL;
690 struct got_object_id *new_commit_id = NULL;
692 struct got_pathlist_head ignores;
693 struct got_pathlist_entry *pe;
694 int preserve_logmsg = 0;
696 TAILQ_INIT(&ignores);
698 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
701 branch_name = optarg;
704 logmsg = strdup(optarg);
705 if (logmsg == NULL) {
706 error = got_error_from_errno("strdup");
711 repo_path = realpath(optarg, NULL);
712 if (repo_path == NULL) {
713 error = got_error_from_errno2("realpath",
719 if (optarg[0] == '\0')
721 error = got_pathlist_insert(&pe, &ignores, optarg,
736 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
744 if (repo_path == NULL) {
745 repo_path = getcwd(NULL, 0);
746 if (repo_path == NULL)
747 return got_error_from_errno("getcwd");
749 got_path_strip_trailing_slashes(repo_path);
750 error = get_gitconfig_path(&gitconfig_path);
753 error = got_repo_open(&repo, repo_path, gitconfig_path);
757 error = get_author(&author, repo, NULL);
762 * Don't let the user create a branch name with a leading '-'.
763 * While technically a valid reference name, this case is usually
764 * an unintended typo.
766 if (branch_name[0] == '-')
767 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
769 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
770 error = got_error_from_errno("asprintf");
774 error = got_ref_open(&branch_ref, repo, refname, 0);
776 if (error->code != GOT_ERR_NOT_REF)
779 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
780 "import target branch already exists");
784 path_dir = realpath(argv[0], NULL);
785 if (path_dir == NULL) {
786 error = got_error_from_errno2("realpath", argv[0]);
789 got_path_strip_trailing_slashes(path_dir);
792 * unveil(2) traverses exec(2); if an editor is used we have
793 * to apply unveil after the log message has been written.
795 if (logmsg == NULL || strlen(logmsg) == 0) {
796 error = get_editor(&editor);
800 error = collect_import_msg(&logmsg, &logmsg_path, editor,
803 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
810 if (unveil(path_dir, "r") != 0) {
811 error = got_error_from_errno2("unveil", path_dir);
817 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
824 error = got_repo_import(&new_commit_id, path_dir, logmsg,
825 author, &ignores, repo, import_progress, NULL);
832 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
839 error = got_ref_write(branch_ref, repo);
846 error = got_object_id_str(&id_str, new_commit_id);
853 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
855 if (error->code != GOT_ERR_NOT_REF) {
861 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
869 error = got_ref_write(head_ref, repo);
877 printf("Created branch %s with commit %s\n",
878 got_ref_get_name(branch_ref), id_str);
880 if (preserve_logmsg) {
881 fprintf(stderr, "%s: log message preserved in %s\n",
882 getprogname(), logmsg_path);
883 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
884 error = got_error_from_errno2("unlink", logmsg_path);
893 free(gitconfig_path);
895 got_ref_close(branch_ref);
897 got_ref_close(head_ref);
904 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
905 "[-R reference] repository-url [directory]\n", getprogname());
909 struct got_fetch_progress_arg {
910 char last_scaled_size[FMT_SCALED_STRSIZE];
915 struct got_repository *repo;
920 struct got_pathlist_head *symrefs;
921 struct got_pathlist_head *wanted_branches;
925 const char *remote_repo_path;
927 int fetch_all_branches;
928 int mirror_references;
932 /* XXX forward declaration */
933 static const struct got_error *
934 create_config_files(const char *proto, const char *host, const char *port,
935 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
936 int mirror_references, struct got_pathlist_head *symrefs,
937 struct got_pathlist_head *wanted_branches, struct got_repository *repo);
939 static const struct got_error *
940 fetch_progress(void *arg, const char *message, off_t packfile_size,
941 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
943 const struct got_error *err = NULL;
944 struct got_fetch_progress_arg *a = arg;
945 char scaled_size[FMT_SCALED_STRSIZE];
946 int p_indexed, p_resolved;
947 int print_size = 0, print_indexed = 0, print_resolved = 0;
950 * In order to allow a failed clone to be resumed with 'got fetch'
951 * we try to create configuration files as soon as possible.
952 * Once the server has sent information about its default branch
953 * we have all required information.
955 if (a->create_configs && !a->configs_created &&
956 !TAILQ_EMPTY(a->config_info.symrefs)) {
957 err = create_config_files(a->config_info.proto,
958 a->config_info.host, a->config_info.port,
959 a->config_info.remote_repo_path,
960 a->config_info.git_url,
961 a->config_info.fetch_all_branches,
962 a->config_info.mirror_references,
963 a->config_info.symrefs,
964 a->config_info.wanted_branches, a->repo);
967 a->configs_created = 1;
970 if (a->verbosity < 0)
973 if (message && message[0] != '\0') {
974 printf("\rserver: %s", message);
979 if (packfile_size > 0 || nobj_indexed > 0) {
980 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
981 (a->last_scaled_size[0] == '\0' ||
982 strcmp(scaled_size, a->last_scaled_size)) != 0) {
984 if (strlcpy(a->last_scaled_size, scaled_size,
985 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
986 return got_error(GOT_ERR_NO_SPACE);
988 if (nobj_indexed > 0) {
989 p_indexed = (nobj_indexed * 100) / nobj_total;
990 if (p_indexed != a->last_p_indexed) {
991 a->last_p_indexed = p_indexed;
996 if (nobj_resolved > 0) {
997 p_resolved = (nobj_resolved * 100) /
998 (nobj_total - nobj_loose);
999 if (p_resolved != a->last_p_resolved) {
1000 a->last_p_resolved = p_resolved;
1008 if (print_size || print_indexed || print_resolved)
1011 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1013 printf("; indexing %d%%", p_indexed);
1015 printf("; resolving deltas %d%%", p_resolved);
1016 if (print_size || print_indexed || print_resolved)
1022 static const struct got_error *
1023 create_symref(const char *refname, struct got_reference *target_ref,
1024 int verbosity, struct got_repository *repo)
1026 const struct got_error *err;
1027 struct got_reference *head_symref;
1029 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1033 err = got_ref_write(head_symref, repo);
1034 if (err == NULL && verbosity > 0) {
1035 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1036 got_ref_get_name(target_ref));
1038 got_ref_close(head_symref);
1042 static const struct got_error *
1043 list_remote_refs(struct got_pathlist_head *symrefs,
1044 struct got_pathlist_head *refs)
1046 const struct got_error *err;
1047 struct got_pathlist_entry *pe;
1049 TAILQ_FOREACH(pe, symrefs, entry) {
1050 const char *refname = pe->path;
1051 const char *targetref = pe->data;
1053 printf("%s: %s\n", refname, targetref);
1056 TAILQ_FOREACH(pe, refs, entry) {
1057 const char *refname = pe->path;
1058 struct got_object_id *id = pe->data;
1061 err = got_object_id_str(&id_str, id);
1064 printf("%s: %s\n", refname, id_str);
1071 static const struct got_error *
1072 create_ref(const char *refname, struct got_object_id *id,
1073 int verbosity, struct got_repository *repo)
1075 const struct got_error *err = NULL;
1076 struct got_reference *ref;
1079 err = got_object_id_str(&id_str, id);
1083 err = got_ref_alloc(&ref, refname, id);
1087 err = got_ref_write(ref, repo);
1090 if (err == NULL && verbosity >= 0)
1091 printf("Created reference %s: %s\n", refname, id_str);
1098 match_wanted_ref(const char *refname, const char *wanted_ref)
1100 if (strncmp(refname, "refs/", 5) != 0)
1105 * Prevent fetching of references that won't make any
1106 * sense outside of the remote repository's context.
1108 if (strncmp(refname, "got/", 4) == 0)
1110 if (strncmp(refname, "remotes/", 8) == 0)
1113 if (strncmp(wanted_ref, "refs/", 5) == 0)
1116 /* Allow prefix match. */
1117 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1120 /* Allow exact match. */
1121 return (strcmp(refname, wanted_ref) == 0);
1125 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1127 struct got_pathlist_entry *pe;
1129 TAILQ_FOREACH(pe, wanted_refs, entry) {
1130 if (match_wanted_ref(refname, pe->path))
1137 static const struct got_error *
1138 create_wanted_ref(const char *refname, struct got_object_id *id,
1139 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1141 const struct got_error *err;
1142 char *remote_refname;
1144 if (strncmp("refs/", refname, 5) == 0)
1147 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1148 remote_repo_name, refname) == -1)
1149 return got_error_from_errno("asprintf");
1151 err = create_ref(remote_refname, id, verbosity, repo);
1152 free(remote_refname);
1156 static const struct got_error *
1157 create_gotconfig(const char *proto, const char *host, const char *port,
1158 const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1159 struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 char *gotconfig_path = NULL;
1163 char *gotconfig = NULL;
1164 FILE *gotconfig_file = NULL;
1167 /* Create got.conf(5). */
1168 gotconfig_path = got_repo_get_path_gotconfig(repo);
1169 if (gotconfig_path == NULL) {
1170 err = got_error_from_errno("got_repo_get_path_gotconfig");
1173 gotconfig_file = fopen(gotconfig_path, "a");
1174 if (gotconfig_file == NULL) {
1175 err = got_error_from_errno2("fopen", gotconfig_path);
1178 if (asprintf(&gotconfig,
1183 "\trepository \"%s\"\n"
1186 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1187 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1189 mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1190 err = got_error_from_errno("asprintf");
1193 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1194 if (n != strlen(gotconfig)) {
1195 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1200 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1201 err = got_error_from_errno2("fclose", gotconfig_path);
1202 free(gotconfig_path);
1206 static const struct got_error *
1207 create_gitconfig(const char *git_url, const char *default_branch,
1208 int fetch_all_branches, int mirror_references, struct got_repository *repo)
1210 const struct got_error *err = NULL;
1211 char *gitconfig_path = NULL;
1212 char *gitconfig = NULL;
1213 FILE *gitconfig_file = NULL;
1216 /* Create a config file Git can understand. */
1217 gitconfig_path = got_repo_get_path_gitconfig(repo);
1218 if (gitconfig_path == NULL) {
1219 err = got_error_from_errno("got_repo_get_path_gitconfig");
1222 gitconfig_file = fopen(gitconfig_path, "a");
1223 if (gitconfig_file == NULL) {
1224 err = got_error_from_errno2("fopen", gitconfig_path);
1227 if (mirror_references) {
1228 if (asprintf(&gitconfig,
1231 "\tfetch = +refs/*:refs/*\n"
1232 "\tmirror = true\n",
1233 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1234 err = got_error_from_errno("asprintf");
1237 } else if (fetch_all_branches) {
1238 if (asprintf(&gitconfig,
1241 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1242 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1243 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1244 err = got_error_from_errno("asprintf");
1248 const char *branchname;
1251 * If the server specified a default branch, use just that one.
1252 * Otherwise fall back to fetching all branches on next fetch.
1254 if (default_branch) {
1255 branchname = default_branch;
1256 if (strncmp(branchname, "refs/heads/", 11) == 0)
1259 branchname = "*"; /* fall back to all branches */
1260 if (asprintf(&gitconfig,
1263 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1264 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1265 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1266 branchname) == -1) {
1267 err = got_error_from_errno("asprintf");
1271 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1272 if (n != strlen(gitconfig)) {
1273 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1277 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1278 err = got_error_from_errno2("fclose", gitconfig_path);
1279 free(gitconfig_path);
1283 static const struct got_error *
1284 create_config_files(const char *proto, const char *host, const char *port,
1285 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1286 int mirror_references, struct got_pathlist_head *symrefs,
1287 struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1289 const struct got_error *err = NULL;
1290 const char *default_branch = NULL;
1291 struct got_pathlist_entry *pe;
1294 * If we asked for a set of wanted branches then use the first
1297 if (!TAILQ_EMPTY(wanted_branches)) {
1298 pe = TAILQ_FIRST(wanted_branches);
1299 default_branch = pe->path;
1301 /* First HEAD ref listed by server is the default branch. */
1302 TAILQ_FOREACH(pe, symrefs, entry) {
1303 const char *refname = pe->path;
1304 const char *target = pe->data;
1306 if (strcmp(refname, GOT_REF_HEAD) != 0)
1309 default_branch = target;
1314 /* Create got.conf(5). */
1315 err = create_gotconfig(proto, host, port, remote_repo_path,
1316 fetch_all_branches, mirror_references, repo);
1320 /* Create a config file Git can understand. */
1321 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1322 mirror_references, repo);
1325 static const struct got_error *
1326 cmd_clone(int argc, char *argv[])
1328 const struct got_error *error = NULL;
1329 const char *uri, *dirname;
1330 char *proto, *host, *port, *repo_name, *server_path;
1331 char *default_destdir = NULL, *id_str = NULL;
1332 const char *repo_path;
1333 struct got_repository *repo = NULL;
1334 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1335 struct got_pathlist_entry *pe;
1336 struct got_object_id *pack_hash = NULL;
1337 int ch, fetchfd = -1, fetchstatus;
1338 pid_t fetchpid = -1;
1339 struct got_fetch_progress_arg fpa;
1340 char *git_url = NULL;
1341 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1342 int list_refs_only = 0;
1345 TAILQ_INIT(&symrefs);
1346 TAILQ_INIT(&wanted_branches);
1347 TAILQ_INIT(&wanted_refs);
1349 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1352 fetch_all_branches = 1;
1355 error = got_pathlist_append(&wanted_branches,
1364 mirror_references = 1;
1369 else if (verbosity < 3)
1376 error = got_pathlist_append(&wanted_refs,
1389 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1390 errx(1, "-a and -b options are mutually exclusive");
1391 if (list_refs_only) {
1392 if (!TAILQ_EMPTY(&wanted_branches))
1393 errx(1, "-l and -b options are mutually exclusive");
1394 if (fetch_all_branches)
1395 errx(1, "-l and -a options are mutually exclusive");
1396 if (mirror_references)
1397 errx(1, "-l and -m options are mutually exclusive");
1398 if (verbosity == -1)
1399 errx(1, "-l and -q options are mutually exclusive");
1400 if (!TAILQ_EMPTY(&wanted_refs))
1401 errx(1, "-l and -R options are mutually exclusive");
1413 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1418 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1419 host, port ? ":" : "", port ? port : "",
1420 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1421 error = got_error_from_errno("asprintf");
1425 if (strcmp(proto, "git") == 0) {
1427 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1428 "sendfd dns inet unveil", NULL) == -1)
1431 } else if (strcmp(proto, "git+ssh") == 0 ||
1432 strcmp(proto, "ssh") == 0) {
1434 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1435 "sendfd unveil", NULL) == -1)
1438 } else if (strcmp(proto, "http") == 0 ||
1439 strcmp(proto, "git+http") == 0) {
1440 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1443 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1446 if (dirname == NULL) {
1447 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1448 error = got_error_from_errno("asprintf");
1451 repo_path = default_destdir;
1453 repo_path = dirname;
1455 if (!list_refs_only) {
1456 error = got_path_mkdir(repo_path);
1458 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1459 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1461 if (!got_path_dir_is_empty(repo_path)) {
1462 error = got_error_path(repo_path,
1463 GOT_ERR_DIR_NOT_EMPTY);
1468 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1469 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1470 error = got_error_from_errno2("unveil",
1471 GOT_FETCH_PATH_SSH);
1475 error = apply_unveil(repo_path, 0, NULL);
1480 printf("Connecting to %s%s%s\n", host,
1481 port ? ":" : "", port ? port : "");
1483 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1484 server_path, verbosity);
1488 if (!list_refs_only) {
1489 error = got_repo_init(repo_path);
1492 error = got_repo_open(&repo, repo_path, NULL);
1497 fpa.last_scaled_size[0] = '\0';
1498 fpa.last_p_indexed = -1;
1499 fpa.last_p_resolved = -1;
1500 fpa.verbosity = verbosity;
1501 fpa.create_configs = 1;
1502 fpa.configs_created = 0;
1504 fpa.config_info.symrefs = &symrefs;
1505 fpa.config_info.wanted_branches = &wanted_branches;
1506 fpa.config_info.proto = proto;
1507 fpa.config_info.host = host;
1508 fpa.config_info.port = port;
1509 fpa.config_info.remote_repo_path = server_path;
1510 fpa.config_info.git_url = git_url;
1511 fpa.config_info.fetch_all_branches = fetch_all_branches;
1512 fpa.config_info.mirror_references = mirror_references;
1513 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1514 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1515 fetch_all_branches, &wanted_branches, &wanted_refs,
1516 list_refs_only, verbosity, fetchfd, repo,
1517 fetch_progress, &fpa);
1521 if (list_refs_only) {
1522 error = list_remote_refs(&symrefs, &refs);
1526 error = got_object_id_str(&id_str, pack_hash);
1530 printf("\nFetched %s.pack\n", id_str);
1533 /* Set up references provided with the pack file. */
1534 TAILQ_FOREACH(pe, &refs, entry) {
1535 const char *refname = pe->path;
1536 struct got_object_id *id = pe->data;
1537 char *remote_refname;
1539 if (is_wanted_ref(&wanted_refs, refname) &&
1540 !mirror_references) {
1541 error = create_wanted_ref(refname, id,
1542 GOT_FETCH_DEFAULT_REMOTE_NAME,
1543 verbosity - 1, repo);
1549 error = create_ref(refname, id, verbosity - 1, repo);
1553 if (mirror_references)
1556 if (strncmp("refs/heads/", refname, 11) != 0)
1559 if (asprintf(&remote_refname,
1560 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1561 refname + 11) == -1) {
1562 error = got_error_from_errno("asprintf");
1565 error = create_ref(remote_refname, id, verbosity - 1, repo);
1566 free(remote_refname);
1571 /* Set the HEAD reference if the server provided one. */
1572 TAILQ_FOREACH(pe, &symrefs, entry) {
1573 struct got_reference *target_ref;
1574 const char *refname = pe->path;
1575 const char *target = pe->data;
1576 char *remote_refname = NULL, *remote_target = NULL;
1578 if (strcmp(refname, GOT_REF_HEAD) != 0)
1581 error = got_ref_open(&target_ref, repo, target, 0);
1583 if (error->code == GOT_ERR_NOT_REF) {
1590 error = create_symref(refname, target_ref, verbosity, repo);
1591 got_ref_close(target_ref);
1595 if (mirror_references)
1598 if (strncmp("refs/heads/", target, 11) != 0)
1601 if (asprintf(&remote_refname,
1602 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1604 error = got_error_from_errno("asprintf");
1607 if (asprintf(&remote_target,
1608 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1609 target + 11) == -1) {
1610 error = got_error_from_errno("asprintf");
1611 free(remote_refname);
1614 error = got_ref_open(&target_ref, repo, remote_target, 0);
1616 free(remote_refname);
1617 free(remote_target);
1618 if (error->code == GOT_ERR_NOT_REF) {
1624 error = create_symref(remote_refname, target_ref,
1625 verbosity - 1, repo);
1626 free(remote_refname);
1627 free(remote_target);
1628 got_ref_close(target_ref);
1634 * We failed to set the HEAD reference. If we asked for
1635 * a set of wanted branches use the first of one of those
1636 * which could be fetched instead.
1638 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1639 const char *target = pe->path;
1640 struct got_reference *target_ref;
1642 error = got_ref_open(&target_ref, repo, target, 0);
1644 if (error->code == GOT_ERR_NOT_REF) {
1651 error = create_symref(GOT_REF_HEAD, target_ref,
1653 got_ref_close(target_ref);
1661 printf("Created %s repository '%s'\n",
1662 mirror_references ? "mirrored" : "cloned", repo_path);
1665 if (kill(fetchpid, SIGTERM) == -1)
1666 error = got_error_from_errno("kill");
1667 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1668 error = got_error_from_errno("waitpid");
1670 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1671 error = got_error_from_errno("close");
1673 got_repo_close(repo);
1674 TAILQ_FOREACH(pe, &refs, entry) {
1675 free((void *)pe->path);
1678 got_pathlist_free(&refs);
1679 TAILQ_FOREACH(pe, &symrefs, entry) {
1680 free((void *)pe->path);
1683 got_pathlist_free(&symrefs);
1684 got_pathlist_free(&wanted_branches);
1685 got_pathlist_free(&wanted_refs);
1692 free(default_destdir);
1697 static const struct got_error *
1698 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1699 int replace_tags, int verbosity, struct got_repository *repo)
1701 const struct got_error *err = NULL;
1702 char *new_id_str = NULL;
1703 struct got_object_id *old_id = NULL;
1705 err = got_object_id_str(&new_id_str, new_id);
1709 if (!replace_tags &&
1710 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1711 err = got_ref_resolve(&old_id, repo, ref);
1714 if (got_object_id_cmp(old_id, new_id) == 0)
1716 if (verbosity >= 0) {
1717 printf("Rejecting update of existing tag %s: %s\n",
1718 got_ref_get_name(ref), new_id_str);
1723 if (got_ref_is_symbolic(ref)) {
1724 if (verbosity >= 0) {
1725 printf("Replacing reference %s: %s\n",
1726 got_ref_get_name(ref),
1727 got_ref_get_symref_target(ref));
1729 err = got_ref_change_symref_to_ref(ref, new_id);
1732 err = got_ref_write(ref, repo);
1736 err = got_ref_resolve(&old_id, repo, ref);
1739 if (got_object_id_cmp(old_id, new_id) == 0)
1742 err = got_ref_change_ref(ref, new_id);
1745 err = got_ref_write(ref, repo);
1751 printf("Updated %s: %s\n", got_ref_get_name(ref),
1759 static const struct got_error *
1760 update_symref(const char *refname, struct got_reference *target_ref,
1761 int verbosity, struct got_repository *repo)
1763 const struct got_error *err = NULL, *unlock_err;
1764 struct got_reference *symref;
1765 int symref_is_locked = 0;
1767 err = got_ref_open(&symref, repo, refname, 1);
1769 if (err->code != GOT_ERR_NOT_REF)
1771 err = got_ref_alloc_symref(&symref, refname, target_ref);
1775 err = got_ref_write(symref, repo);
1780 printf("Created reference %s: %s\n",
1781 got_ref_get_name(symref),
1782 got_ref_get_symref_target(symref));
1784 symref_is_locked = 1;
1786 if (strcmp(got_ref_get_symref_target(symref),
1787 got_ref_get_name(target_ref)) == 0)
1790 err = got_ref_change_symref(symref,
1791 got_ref_get_name(target_ref));
1795 err = got_ref_write(symref, repo);
1800 printf("Updated %s: %s\n", got_ref_get_name(symref),
1801 got_ref_get_symref_target(symref));
1805 if (symref_is_locked) {
1806 unlock_err = got_ref_unlock(symref);
1807 if (unlock_err && err == NULL)
1810 got_ref_close(symref);
1817 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1818 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1819 "[remote-repository-name]\n",
1824 static const struct got_error *
1825 delete_missing_ref(struct got_reference *ref,
1826 int verbosity, struct got_repository *repo)
1828 const struct got_error *err = NULL;
1829 struct got_object_id *id = NULL;
1830 char *id_str = NULL;
1832 if (got_ref_is_symbolic(ref)) {
1833 err = got_ref_delete(ref, repo);
1836 if (verbosity >= 0) {
1837 printf("Deleted reference %s: %s\n",
1838 got_ref_get_name(ref),
1839 got_ref_get_symref_target(ref));
1842 err = got_ref_resolve(&id, repo, ref);
1845 err = got_object_id_str(&id_str, id);
1849 err = got_ref_delete(ref, repo);
1852 if (verbosity >= 0) {
1853 printf("Deleted reference %s: %s\n",
1854 got_ref_get_name(ref), id_str);
1863 static const struct got_error *
1864 delete_missing_refs(struct got_pathlist_head *their_refs,
1865 struct got_pathlist_head *their_symrefs,
1866 const struct got_remote_repo *remote,
1867 int verbosity, struct got_repository *repo)
1869 const struct got_error *err = NULL, *unlock_err;
1870 struct got_reflist_head my_refs;
1871 struct got_reflist_entry *re;
1872 struct got_pathlist_entry *pe;
1873 char *remote_namespace = NULL;
1874 char *local_refname = NULL;
1876 SIMPLEQ_INIT(&my_refs);
1878 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1880 return got_error_from_errno("asprintf");
1882 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1886 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1887 const char *refname = got_ref_get_name(re->ref);
1889 if (!remote->mirror_references) {
1890 if (strncmp(refname, remote_namespace,
1891 strlen(remote_namespace)) == 0) {
1892 if (strcmp(refname + strlen(remote_namespace),
1895 if (asprintf(&local_refname, "refs/heads/%s",
1896 refname + strlen(remote_namespace)) == -1) {
1897 err = got_error_from_errno("asprintf");
1900 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1904 TAILQ_FOREACH(pe, their_refs, entry) {
1905 if (strcmp(local_refname, pe->path) == 0)
1911 TAILQ_FOREACH(pe, their_symrefs, entry) {
1912 if (strcmp(local_refname, pe->path) == 0)
1918 err = delete_missing_ref(re->ref, verbosity, repo);
1922 if (local_refname) {
1923 struct got_reference *ref;
1924 err = got_ref_open(&ref, repo, local_refname, 1);
1926 if (err->code != GOT_ERR_NOT_REF)
1928 free(local_refname);
1929 local_refname = NULL;
1932 err = delete_missing_ref(ref, verbosity, repo);
1935 unlock_err = got_ref_unlock(ref);
1937 if (unlock_err && err == NULL) {
1942 free(local_refname);
1943 local_refname = NULL;
1947 free(remote_namespace);
1948 free(local_refname);
1952 static const struct got_error *
1953 update_wanted_ref(const char *refname, struct got_object_id *id,
1954 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1956 const struct got_error *err, *unlock_err;
1957 char *remote_refname;
1958 struct got_reference *ref;
1960 if (strncmp("refs/", refname, 5) == 0)
1963 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1964 remote_repo_name, refname) == -1)
1965 return got_error_from_errno("asprintf");
1967 err = got_ref_open(&ref, repo, remote_refname, 1);
1969 if (err->code != GOT_ERR_NOT_REF)
1971 err = create_ref(remote_refname, id, verbosity, repo);
1973 err = update_ref(ref, id, 0, verbosity, repo);
1974 unlock_err = got_ref_unlock(ref);
1975 if (unlock_err && err == NULL)
1980 free(remote_refname);
1984 static const struct got_error *
1985 cmd_fetch(int argc, char *argv[])
1987 const struct got_error *error = NULL, *unlock_err;
1988 char *cwd = NULL, *repo_path = NULL;
1989 const char *remote_name;
1990 char *proto = NULL, *host = NULL, *port = NULL;
1991 char *repo_name = NULL, *server_path = NULL;
1992 const struct got_remote_repo *remotes, *remote = NULL;
1994 char *id_str = NULL;
1995 struct got_repository *repo = NULL;
1996 struct got_worktree *worktree = NULL;
1997 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1998 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1999 struct got_pathlist_entry *pe;
2000 struct got_object_id *pack_hash = NULL;
2001 int i, ch, fetchfd = -1, fetchstatus;
2002 pid_t fetchpid = -1;
2003 struct got_fetch_progress_arg fpa;
2004 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2005 int delete_refs = 0, replace_tags = 0;
2008 TAILQ_INIT(&symrefs);
2009 TAILQ_INIT(&wanted_branches);
2010 TAILQ_INIT(&wanted_refs);
2012 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2015 fetch_all_branches = 1;
2018 error = got_pathlist_append(&wanted_branches,
2030 repo_path = realpath(optarg, NULL);
2031 if (repo_path == NULL)
2032 return got_error_from_errno2("realpath",
2034 got_path_strip_trailing_slashes(repo_path);
2042 else if (verbosity < 3)
2049 error = got_pathlist_append(&wanted_refs,
2062 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2063 errx(1, "-a and -b options are mutually exclusive");
2064 if (list_refs_only) {
2065 if (!TAILQ_EMPTY(&wanted_branches))
2066 errx(1, "-l and -b options are mutually exclusive");
2067 if (fetch_all_branches)
2068 errx(1, "-l and -a options are mutually exclusive");
2070 errx(1, "-l and -d options are mutually exclusive");
2071 if (verbosity == -1)
2072 errx(1, "-l and -q options are mutually exclusive");
2076 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2078 remote_name = argv[0];
2082 cwd = getcwd(NULL, 0);
2084 error = got_error_from_errno("getcwd");
2088 if (repo_path == NULL) {
2089 error = got_worktree_open(&worktree, cwd);
2090 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2096 strdup(got_worktree_get_repo_path(worktree));
2097 if (repo_path == NULL)
2098 error = got_error_from_errno("strdup");
2102 repo_path = strdup(cwd);
2103 if (repo_path == NULL) {
2104 error = got_error_from_errno("strdup");
2110 error = got_repo_open(&repo, repo_path, NULL);
2115 worktree_conf = got_worktree_get_gotconfig(worktree);
2116 if (worktree_conf) {
2117 got_gotconfig_get_remotes(&nremotes, &remotes,
2119 for (i = 0; i < nremotes; i++) {
2120 remote = &remotes[i];
2121 if (strcmp(remote->name, remote_name) == 0)
2126 if (remote == NULL) {
2127 repo_conf = got_repo_get_gotconfig(repo);
2129 got_gotconfig_get_remotes(&nremotes, &remotes,
2131 for (i = 0; i < nremotes; i++) {
2132 remote = &remotes[i];
2133 if (strcmp(remote->name, remote_name) == 0)
2138 if (remote == NULL) {
2139 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2140 for (i = 0; i < nremotes; i++) {
2141 remote = &remotes[i];
2142 if (strcmp(remote->name, remote_name) == 0)
2146 if (remote == NULL) {
2147 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2151 if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2152 for (i = 0; i < remote->nbranches; i++) {
2153 got_pathlist_append(&wanted_branches,
2154 remote->branches[i], NULL);
2159 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2160 &repo_name, remote->url);
2164 if (strcmp(proto, "git") == 0) {
2166 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2167 "sendfd dns inet unveil", NULL) == -1)
2170 } else if (strcmp(proto, "git+ssh") == 0 ||
2171 strcmp(proto, "ssh") == 0) {
2173 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2174 "sendfd unveil", NULL) == -1)
2177 } else if (strcmp(proto, "http") == 0 ||
2178 strcmp(proto, "git+http") == 0) {
2179 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2182 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2186 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2187 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2188 error = got_error_from_errno2("unveil",
2189 GOT_FETCH_PATH_SSH);
2193 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2198 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2199 port ? ":" : "", port ? port : "");
2201 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2202 server_path, verbosity);
2206 fpa.last_scaled_size[0] = '\0';
2207 fpa.last_p_indexed = -1;
2208 fpa.last_p_resolved = -1;
2209 fpa.verbosity = verbosity;
2211 fpa.create_configs = 0;
2212 fpa.configs_created = 0;
2213 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2214 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2215 remote->mirror_references, fetch_all_branches, &wanted_branches,
2216 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2217 fetch_progress, &fpa);
2221 if (list_refs_only) {
2222 error = list_remote_refs(&symrefs, &refs);
2226 if (pack_hash == NULL) {
2228 printf("Already up-to-date\n");
2229 } else if (verbosity >= 0) {
2230 error = got_object_id_str(&id_str, pack_hash);
2233 printf("\nFetched %s.pack\n", id_str);
2238 /* Update references provided with the pack file. */
2239 TAILQ_FOREACH(pe, &refs, entry) {
2240 const char *refname = pe->path;
2241 struct got_object_id *id = pe->data;
2242 struct got_reference *ref;
2243 char *remote_refname;
2245 if (is_wanted_ref(&wanted_refs, refname) &&
2246 !remote->mirror_references) {
2247 error = update_wanted_ref(refname, id,
2248 remote->name, verbosity, repo);
2254 if (remote->mirror_references ||
2255 strncmp("refs/tags/", refname, 10) == 0) {
2256 error = got_ref_open(&ref, repo, refname, 1);
2258 if (error->code != GOT_ERR_NOT_REF)
2260 error = create_ref(refname, id, verbosity,
2265 error = update_ref(ref, id, replace_tags,
2267 unlock_err = got_ref_unlock(ref);
2268 if (unlock_err && error == NULL)
2274 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2275 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2276 remote_name, refname + 11) == -1) {
2277 error = got_error_from_errno("asprintf");
2281 error = got_ref_open(&ref, repo, remote_refname, 1);
2283 if (error->code != GOT_ERR_NOT_REF)
2285 error = create_ref(remote_refname, id,
2290 error = update_ref(ref, id, replace_tags,
2292 unlock_err = got_ref_unlock(ref);
2293 if (unlock_err && error == NULL)
2300 /* Also create a local branch if none exists yet. */
2301 error = got_ref_open(&ref, repo, refname, 1);
2303 if (error->code != GOT_ERR_NOT_REF)
2305 error = create_ref(refname, id, verbosity,
2310 unlock_err = got_ref_unlock(ref);
2311 if (unlock_err && error == NULL)
2318 error = delete_missing_refs(&refs, &symrefs, remote,
2324 if (!remote->mirror_references) {
2325 /* Update remote HEAD reference if the server provided one. */
2326 TAILQ_FOREACH(pe, &symrefs, entry) {
2327 struct got_reference *target_ref;
2328 const char *refname = pe->path;
2329 const char *target = pe->data;
2330 char *remote_refname = NULL, *remote_target = NULL;
2332 if (strcmp(refname, GOT_REF_HEAD) != 0)
2335 if (strncmp("refs/heads/", target, 11) != 0)
2338 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2339 remote->name, refname) == -1) {
2340 error = got_error_from_errno("asprintf");
2343 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2344 remote->name, target + 11) == -1) {
2345 error = got_error_from_errno("asprintf");
2346 free(remote_refname);
2350 error = got_ref_open(&target_ref, repo, remote_target,
2353 free(remote_refname);
2354 free(remote_target);
2355 if (error->code == GOT_ERR_NOT_REF) {
2361 error = update_symref(remote_refname, target_ref,
2363 free(remote_refname);
2364 free(remote_target);
2365 got_ref_close(target_ref);
2372 if (kill(fetchpid, SIGTERM) == -1)
2373 error = got_error_from_errno("kill");
2374 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2375 error = got_error_from_errno("waitpid");
2377 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2378 error = got_error_from_errno("close");
2380 got_repo_close(repo);
2382 got_worktree_close(worktree);
2383 TAILQ_FOREACH(pe, &refs, entry) {
2384 free((void *)pe->path);
2387 got_pathlist_free(&refs);
2388 TAILQ_FOREACH(pe, &symrefs, entry) {
2389 free((void *)pe->path);
2392 got_pathlist_free(&symrefs);
2393 got_pathlist_free(&wanted_branches);
2394 got_pathlist_free(&wanted_refs);
2409 usage_checkout(void)
2411 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2412 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2417 show_worktree_base_ref_warning(void)
2419 fprintf(stderr, "%s: warning: could not create a reference "
2420 "to the work tree's base commit; the commit could be "
2421 "garbage-collected by Git; making the repository "
2422 "writable and running 'got update' will prevent this\n",
2426 struct got_checkout_progress_arg {
2427 const char *worktree_path;
2428 int had_base_commit_ref_error;
2431 static const struct got_error *
2432 checkout_progress(void *arg, unsigned char status, const char *path)
2434 struct got_checkout_progress_arg *a = arg;
2436 /* Base commit bump happens silently. */
2437 if (status == GOT_STATUS_BUMP_BASE)
2440 if (status == GOT_STATUS_BASE_REF_ERR) {
2441 a->had_base_commit_ref_error = 1;
2445 while (path[0] == '/')
2448 printf("%c %s/%s\n", status, a->worktree_path, path);
2452 static const struct got_error *
2453 check_cancelled(void *arg)
2455 if (sigint_received || sigpipe_received)
2456 return got_error(GOT_ERR_CANCELLED);
2460 static const struct got_error *
2461 check_linear_ancestry(struct got_object_id *commit_id,
2462 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2463 struct got_repository *repo)
2465 const struct got_error *err = NULL;
2466 struct got_object_id *yca_id;
2468 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2469 commit_id, base_commit_id, repo, check_cancelled, NULL);
2474 return got_error(GOT_ERR_ANCESTRY);
2477 * Require a straight line of history between the target commit
2478 * and the work tree's base commit.
2480 * Non-linear situations such as this require a rebase:
2482 * (commit) D F (base_commit)
2490 * 'got update' only handles linear cases:
2491 * Update forwards in time: A (base/yca) - B - C - D (commit)
2492 * Update backwards in time: D (base) - C - B - A (commit/yca)
2494 if (allow_forwards_in_time_only) {
2495 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2496 return got_error(GOT_ERR_ANCESTRY);
2497 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2498 got_object_id_cmp(base_commit_id, yca_id) != 0)
2499 return got_error(GOT_ERR_ANCESTRY);
2505 static const struct got_error *
2506 check_same_branch(struct got_object_id *commit_id,
2507 struct got_reference *head_ref, struct got_object_id *yca_id,
2508 struct got_repository *repo)
2510 const struct got_error *err = NULL;
2511 struct got_commit_graph *graph = NULL;
2512 struct got_object_id *head_commit_id = NULL;
2513 int is_same_branch = 0;
2515 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2519 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2523 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2528 err = got_commit_graph_open(&graph, "/", 1);
2532 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2533 check_cancelled, NULL);
2538 struct got_object_id *id;
2539 err = got_commit_graph_iter_next(&id, graph, repo,
2540 check_cancelled, NULL);
2542 if (err->code == GOT_ERR_ITER_COMPLETED)
2548 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2550 if (got_object_id_cmp(id, commit_id) == 0) {
2558 got_commit_graph_close(graph);
2559 free(head_commit_id);
2560 if (!err && !is_same_branch)
2561 err = got_error(GOT_ERR_ANCESTRY);
2565 static const struct got_error *
2566 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2568 static char msg[512];
2569 const char *branch_name;
2571 if (got_ref_is_symbolic(ref))
2572 branch_name = got_ref_get_symref_target(ref);
2574 branch_name = got_ref_get_name(ref);
2576 if (strncmp("refs/heads/", branch_name, 11) == 0)
2579 snprintf(msg, sizeof(msg),
2580 "target commit is not contained in branch '%s'; "
2581 "the branch to use must be specified with -b; "
2582 "if necessary a new branch can be created for "
2583 "this commit with 'got branch -c %s BRANCH_NAME'",
2584 branch_name, commit_id_str);
2586 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2589 static const struct got_error *
2590 cmd_checkout(int argc, char *argv[])
2592 const struct got_error *error = NULL;
2593 struct got_repository *repo = NULL;
2594 struct got_reference *head_ref = NULL;
2595 struct got_worktree *worktree = NULL;
2596 char *repo_path = NULL;
2597 char *worktree_path = NULL;
2598 const char *path_prefix = "";
2599 const char *branch_name = GOT_REF_HEAD;
2600 char *commit_id_str = NULL;
2601 char *cwd = NULL, *path = NULL;
2602 int ch, same_path_prefix, allow_nonempty = 0;
2603 struct got_pathlist_head paths;
2604 struct got_checkout_progress_arg cpa;
2608 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2611 branch_name = optarg;
2614 commit_id_str = strdup(optarg);
2615 if (commit_id_str == NULL)
2616 return got_error_from_errno("strdup");
2622 path_prefix = optarg;
2634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2635 "unveil", NULL) == -1)
2639 char *base, *dotgit;
2640 repo_path = realpath(argv[0], NULL);
2641 if (repo_path == NULL)
2642 return got_error_from_errno2("realpath", argv[0]);
2643 cwd = getcwd(NULL, 0);
2645 error = got_error_from_errno("getcwd");
2649 path = strdup(path_prefix);
2651 path = strdup(repo_path);
2653 error = got_error_from_errno("strdup");
2656 base = basename(path);
2658 error = got_error_from_errno2("basename", path);
2661 dotgit = strstr(base, ".git");
2664 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2665 error = got_error_from_errno("asprintf");
2668 } else if (argc == 2) {
2669 repo_path = realpath(argv[0], NULL);
2670 if (repo_path == NULL) {
2671 error = got_error_from_errno2("realpath", argv[0]);
2674 worktree_path = realpath(argv[1], NULL);
2675 if (worktree_path == NULL) {
2676 if (errno != ENOENT) {
2677 error = got_error_from_errno2("realpath",
2681 worktree_path = strdup(argv[1]);
2682 if (worktree_path == NULL) {
2683 error = got_error_from_errno("strdup");
2690 got_path_strip_trailing_slashes(repo_path);
2691 got_path_strip_trailing_slashes(worktree_path);
2693 error = got_repo_open(&repo, repo_path, NULL);
2697 /* Pre-create work tree path for unveil(2) */
2698 error = got_path_mkdir(worktree_path);
2700 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2701 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2703 if (!allow_nonempty &&
2704 !got_path_dir_is_empty(worktree_path)) {
2705 error = got_error_path(worktree_path,
2706 GOT_ERR_DIR_NOT_EMPTY);
2711 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2715 error = got_ref_open(&head_ref, repo, branch_name, 0);
2719 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2720 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2723 error = got_worktree_open(&worktree, worktree_path);
2727 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2731 if (!same_path_prefix) {
2732 error = got_error(GOT_ERR_PATH_PREFIX);
2736 if (commit_id_str) {
2737 struct got_object_id *commit_id;
2738 error = got_repo_match_object_id(&commit_id, NULL,
2739 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 error = check_linear_ancestry(commit_id,
2743 got_worktree_get_base_commit_id(worktree), 0, repo);
2744 if (error != NULL) {
2746 if (error->code == GOT_ERR_ANCESTRY) {
2747 error = checkout_ancestry_error(
2748 head_ref, commit_id_str);
2752 error = check_same_branch(commit_id, head_ref, NULL, repo);
2754 if (error->code == GOT_ERR_ANCESTRY) {
2755 error = checkout_ancestry_error(
2756 head_ref, commit_id_str);
2760 error = got_worktree_set_base_commit_id(worktree, repo,
2767 error = got_pathlist_append(&paths, "", NULL);
2770 cpa.worktree_path = worktree_path;
2771 cpa.had_base_commit_ref_error = 0;
2772 error = got_worktree_checkout_files(worktree, &paths, repo,
2773 checkout_progress, &cpa, check_cancelled, NULL);
2777 printf("Now shut up and hack\n");
2778 if (cpa.had_base_commit_ref_error)
2779 show_worktree_base_ref_warning();
2781 got_pathlist_free(&paths);
2782 free(commit_id_str);
2784 free(worktree_path);
2790 struct got_update_progress_arg {
2798 print_update_progress_stats(struct got_update_progress_arg *upa)
2800 if (!upa->did_something)
2803 if (upa->conflicts > 0)
2804 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2805 if (upa->obstructed > 0)
2806 printf("File paths obstructed by a non-regular file: %d\n",
2808 if (upa->not_updated > 0)
2809 printf("Files not updated because of existing merge "
2810 "conflicts: %d\n", upa->not_updated);
2816 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2821 static const struct got_error *
2822 update_progress(void *arg, unsigned char status, const char *path)
2824 struct got_update_progress_arg *upa = arg;
2826 if (status == GOT_STATUS_EXISTS ||
2827 status == GOT_STATUS_BASE_REF_ERR)
2830 upa->did_something = 1;
2832 /* Base commit bump happens silently. */
2833 if (status == GOT_STATUS_BUMP_BASE)
2836 if (status == GOT_STATUS_CONFLICT)
2838 if (status == GOT_STATUS_OBSTRUCTED)
2840 if (status == GOT_STATUS_CANNOT_UPDATE)
2843 while (path[0] == '/')
2845 printf("%c %s\n", status, path);
2849 static const struct got_error *
2850 switch_head_ref(struct got_reference *head_ref,
2851 struct got_object_id *commit_id, struct got_worktree *worktree,
2852 struct got_repository *repo)
2854 const struct got_error *err = NULL;
2856 int ref_has_moved = 0;
2858 /* Trivial case: switching between two different references. */
2859 if (strcmp(got_ref_get_name(head_ref),
2860 got_worktree_get_head_ref_name(worktree)) != 0) {
2861 printf("Switching work tree from %s to %s\n",
2862 got_worktree_get_head_ref_name(worktree),
2863 got_ref_get_name(head_ref));
2864 return got_worktree_set_head_ref(worktree, head_ref);
2867 err = check_linear_ancestry(commit_id,
2868 got_worktree_get_base_commit_id(worktree), 0, repo);
2870 if (err->code != GOT_ERR_ANCESTRY)
2877 /* Switching to a rebased branch with the same reference name. */
2878 err = got_object_id_str(&base_id_str,
2879 got_worktree_get_base_commit_id(worktree));
2882 printf("Reference %s now points at a different branch\n",
2883 got_worktree_get_head_ref_name(worktree));
2884 printf("Switching work tree from %s to %s\n", base_id_str,
2885 got_worktree_get_head_ref_name(worktree));
2889 static const struct got_error *
2890 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2892 const struct got_error *err;
2895 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2899 return got_error(GOT_ERR_REBASING);
2901 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2905 return got_error(GOT_ERR_HISTEDIT_BUSY);
2910 static const struct got_error *
2911 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2912 char *argv[], struct got_worktree *worktree)
2914 const struct got_error *err = NULL;
2921 return got_error_from_errno("strdup");
2922 return got_pathlist_append(paths, path, NULL);
2925 for (i = 0; i < argc; i++) {
2926 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2929 err = got_pathlist_append(paths, path, NULL);
2939 static const struct got_error *
2940 wrap_not_worktree_error(const struct got_error *orig_err,
2941 const char *cmdname, const char *path)
2943 const struct got_error *err;
2944 struct got_repository *repo;
2945 static char msg[512];
2947 err = got_repo_open(&repo, path, NULL);
2951 snprintf(msg, sizeof(msg),
2952 "'got %s' needs a work tree in addition to a git repository\n"
2953 "Work trees can be checked out from this Git repository with "
2955 "The got(1) manual page contains more information.", cmdname);
2956 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2957 got_repo_close(repo);
2961 static const struct got_error *
2962 cmd_update(int argc, char *argv[])
2964 const struct got_error *error = NULL;
2965 struct got_repository *repo = NULL;
2966 struct got_worktree *worktree = NULL;
2967 char *worktree_path = NULL;
2968 struct got_object_id *commit_id = NULL;
2969 char *commit_id_str = NULL;
2970 const char *branch_name = NULL;
2971 struct got_reference *head_ref = NULL;
2972 struct got_pathlist_head paths;
2973 struct got_pathlist_entry *pe;
2975 struct got_update_progress_arg upa;
2979 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2982 branch_name = optarg;
2985 commit_id_str = strdup(optarg);
2986 if (commit_id_str == NULL)
2987 return got_error_from_errno("strdup");
2999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3000 "unveil", NULL) == -1)
3003 worktree_path = getcwd(NULL, 0);
3004 if (worktree_path == NULL) {
3005 error = got_error_from_errno("getcwd");
3008 error = got_worktree_open(&worktree, worktree_path);
3010 if (error->code == GOT_ERR_NOT_WORKTREE)
3011 error = wrap_not_worktree_error(error, "update",
3016 error = check_rebase_or_histedit_in_progress(worktree);
3020 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3025 error = apply_unveil(got_repo_get_path(repo), 0,
3026 got_worktree_get_root_path(worktree));
3030 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3034 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3035 got_worktree_get_head_ref_name(worktree), 0);
3038 if (commit_id_str == NULL) {
3039 error = got_ref_resolve(&commit_id, repo, head_ref);
3042 error = got_object_id_str(&commit_id_str, commit_id);
3046 error = got_repo_match_object_id(&commit_id, NULL,
3047 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3048 free(commit_id_str);
3049 commit_id_str = NULL;
3052 error = got_object_id_str(&commit_id_str, commit_id);
3058 struct got_object_id *head_commit_id;
3059 TAILQ_FOREACH(pe, &paths, entry) {
3060 if (pe->path_len == 0)
3062 error = got_error_msg(GOT_ERR_BAD_PATH,
3063 "switching between branches requires that "
3064 "the entire work tree gets updated");
3067 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3070 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3072 free(head_commit_id);
3075 error = check_same_branch(commit_id, head_ref, NULL, repo);
3078 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3082 error = check_linear_ancestry(commit_id,
3083 got_worktree_get_base_commit_id(worktree), 0, repo);
3084 if (error != NULL) {
3085 if (error->code == GOT_ERR_ANCESTRY)
3086 error = got_error(GOT_ERR_BRANCH_MOVED);
3089 error = check_same_branch(commit_id, head_ref, NULL, repo);
3094 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3096 error = got_worktree_set_base_commit_id(worktree, repo,
3102 memset(&upa, 0, sizeof(upa));
3103 error = got_worktree_checkout_files(worktree, &paths, repo,
3104 update_progress, &upa, check_cancelled, NULL);
3108 if (upa.did_something)
3109 printf("Updated to commit %s\n", commit_id_str);
3111 printf("Already up-to-date\n");
3112 print_update_progress_stats(&upa);
3114 free(worktree_path);
3115 TAILQ_FOREACH(pe, &paths, entry)
3116 free((char *)pe->path);
3117 got_pathlist_free(&paths);
3119 free(commit_id_str);
3123 static const struct got_error *
3124 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3125 const char *path, int diff_context, int ignore_whitespace,
3126 struct got_repository *repo)
3128 const struct got_error *err = NULL;
3129 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3132 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3137 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3141 while (path[0] == '/')
3143 err = got_diff_blob(blob1, blob2, path, path, diff_context,
3144 ignore_whitespace, stdout);
3147 got_object_blob_close(blob1);
3148 got_object_blob_close(blob2);
3152 static const struct got_error *
3153 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3154 const char *path, int diff_context, int ignore_whitespace,
3155 struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3159 struct got_diff_blob_output_unidiff_arg arg;
3162 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3167 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3171 arg.diff_context = diff_context;
3172 arg.ignore_whitespace = ignore_whitespace;
3173 arg.outfile = stdout;
3174 while (path[0] == '/')
3176 err = got_diff_tree(tree1, tree2, path, path, repo,
3177 got_diff_blob_output_unidiff, &arg, 1);
3180 got_object_tree_close(tree1);
3182 got_object_tree_close(tree2);
3186 static const struct got_error *
3187 get_changed_paths(struct got_pathlist_head *paths,
3188 struct got_commit_object *commit, struct got_repository *repo)
3190 const struct got_error *err = NULL;
3191 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3192 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3193 struct got_object_qid *qid;
3195 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3197 struct got_commit_object *pcommit;
3198 err = got_object_open_as_commit(&pcommit, repo,
3203 tree_id1 = got_object_commit_get_tree_id(pcommit);
3204 got_object_commit_close(pcommit);
3209 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3214 tree_id2 = got_object_commit_get_tree_id(commit);
3215 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3219 err = got_diff_tree(tree1, tree2, "", "", repo,
3220 got_diff_tree_collect_changed_paths, paths, 0);
3223 got_object_tree_close(tree1);
3225 got_object_tree_close(tree2);
3229 static const struct got_error *
3230 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3231 const char *path, int diff_context, struct got_repository *repo)
3233 const struct got_error *err = NULL;
3234 struct got_commit_object *pcommit = NULL;
3235 char *id_str1 = NULL, *id_str2 = NULL;
3236 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3237 struct got_object_qid *qid;
3239 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3241 err = got_object_open_as_commit(&pcommit, repo,
3247 if (path && path[0] != '\0') {
3249 err = got_object_id_by_path(&obj_id2, repo, id, path);
3252 err = got_object_id_str(&id_str2, obj_id2);
3258 err = got_object_id_by_path(&obj_id1, repo,
3261 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3266 err = got_object_id_str(&id_str1, obj_id1);
3273 err = got_object_get_type(&obj_type, repo, obj_id2);
3278 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3280 case GOT_OBJ_TYPE_BLOB:
3281 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3284 case GOT_OBJ_TYPE_TREE:
3285 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3289 err = got_error(GOT_ERR_OBJ_TYPE);
3295 obj_id2 = got_object_commit_get_tree_id(commit);
3296 err = got_object_id_str(&id_str2, obj_id2);
3299 obj_id1 = got_object_commit_get_tree_id(pcommit);
3300 err = got_object_id_str(&id_str1, obj_id1);
3303 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3304 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3310 got_object_commit_close(pcommit);
3315 get_datestr(time_t *time, char *datebuf)
3317 struct tm mytm, *tm;
3320 tm = gmtime_r(time, &mytm);
3323 s = asctime_r(tm, datebuf);
3326 p = strchr(s, '\n');
3332 static const struct got_error *
3333 match_logmsg(int *have_match, struct got_object_id *id,
3334 struct got_commit_object *commit, regex_t *regex)
3336 const struct got_error *err = NULL;
3337 regmatch_t regmatch;
3338 char *id_str = NULL, *logmsg = NULL;
3342 err = got_object_id_str(&id_str, id);
3346 err = got_object_commit_get_logmsg(&logmsg, commit);
3350 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3359 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3362 regmatch_t regmatch;
3363 struct got_pathlist_entry *pe;
3367 TAILQ_FOREACH(pe, changed_paths, entry) {
3368 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3375 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3377 static const struct got_error *
3378 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3379 struct got_repository *repo, const char *path,
3380 struct got_pathlist_head *changed_paths, int show_patch,
3381 int diff_context, struct got_reflist_head *refs)
3383 const struct got_error *err = NULL;
3384 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3386 time_t committer_time;
3387 const char *author, *committer;
3388 char *refs_str = NULL;
3389 struct got_reflist_entry *re;
3391 SIMPLEQ_FOREACH(re, refs, entry) {
3394 struct got_tag_object *tag = NULL;
3395 struct got_object_id *ref_id;
3398 name = got_ref_get_name(re->ref);
3399 if (strcmp(name, GOT_REF_HEAD) == 0)
3401 if (strncmp(name, "refs/", 5) == 0)
3403 if (strncmp(name, "got/", 4) == 0)
3405 if (strncmp(name, "heads/", 6) == 0)
3407 if (strncmp(name, "remotes/", 8) == 0) {
3409 s = strstr(name, "/" GOT_REF_HEAD);
3410 if (s != NULL && s[strlen(s)] == '\0')
3413 err = got_ref_resolve(&ref_id, repo, re->ref);
3416 if (strncmp(name, "tags/", 5) == 0) {
3417 err = got_object_open_as_tag(&tag, repo, ref_id);
3419 if (err->code != GOT_ERR_OBJ_TYPE) {
3423 /* Ref points at something other than a tag. */
3428 cmp = got_object_id_cmp(tag ?
3429 got_object_tag_get_object_id(tag) : ref_id, id);
3432 got_object_tag_close(tag);
3436 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3438 err = got_error_from_errno("asprintf");
3444 err = got_object_id_str(&id_str, id);
3448 printf(GOT_COMMIT_SEP_STR);
3449 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3450 refs_str ? refs_str : "", refs_str ? ")" : "");
3455 printf("from: %s\n", got_object_commit_get_author(commit));
3456 committer_time = got_object_commit_get_committer_time(commit);
3457 datestr = get_datestr(&committer_time, datebuf);
3459 printf("date: %s UTC\n", datestr);
3460 author = got_object_commit_get_author(commit);
3461 committer = got_object_commit_get_committer(commit);
3462 if (strcmp(author, committer) != 0)
3463 printf("via: %s\n", committer);
3464 if (got_object_commit_get_nparents(commit) > 1) {
3465 const struct got_object_id_queue *parent_ids;
3466 struct got_object_qid *qid;
3468 parent_ids = got_object_commit_get_parent_ids(commit);
3469 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3470 err = got_object_id_str(&id_str, qid->id);
3473 printf("parent %d: %s\n", n++, id_str);
3478 err = got_object_commit_get_logmsg(&logmsg0, commit);
3484 line = strsep(&logmsg, "\n");
3486 printf(" %s\n", line);
3490 if (changed_paths) {
3491 struct got_pathlist_entry *pe;
3492 TAILQ_FOREACH(pe, changed_paths, entry) {
3493 struct got_diff_changed_path *cp = pe->data;
3494 printf(" %c %s\n", cp->status, pe->path);
3499 err = print_patch(commit, id, path, diff_context, repo);
3504 if (fflush(stdout) != 0 && err == NULL)
3505 err = got_error_from_errno("fflush");
3509 static const struct got_error *
3510 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3511 struct got_repository *repo, const char *path, int show_changed_paths,
3512 int show_patch, const char *search_pattern, int diff_context, int limit,
3513 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3515 const struct got_error *err;
3516 struct got_commit_graph *graph;
3519 struct got_object_id_queue reversed_commits;
3520 struct got_object_qid *qid;
3521 struct got_commit_object *commit;
3522 struct got_pathlist_head changed_paths;
3523 struct got_pathlist_entry *pe;
3525 SIMPLEQ_INIT(&reversed_commits);
3526 TAILQ_INIT(&changed_paths);
3528 if (search_pattern && regcomp(®ex, search_pattern,
3529 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3530 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3532 err = got_commit_graph_open(&graph, path, !log_branches);
3535 err = got_commit_graph_iter_start(graph, root_id, repo,
3536 check_cancelled, NULL);
3540 struct got_object_id *id;
3542 if (sigint_received || sigpipe_received)
3545 err = got_commit_graph_iter_next(&id, graph, repo,
3546 check_cancelled, NULL);
3548 if (err->code == GOT_ERR_ITER_COMPLETED)
3555 err = got_object_open_as_commit(&commit, repo, id);
3559 if (show_changed_paths && !reverse_display_order) {
3560 err = get_changed_paths(&changed_paths, commit, repo);
3565 if (search_pattern) {
3566 err = match_logmsg(&have_match, id, commit, ®ex);
3568 got_object_commit_close(commit);
3571 if (have_match == 0 && show_changed_paths)
3572 match_changed_paths(&have_match,
3573 &changed_paths, ®ex);
3574 if (have_match == 0) {
3575 got_object_commit_close(commit);
3576 TAILQ_FOREACH(pe, &changed_paths, entry) {
3577 free((char *)pe->path);
3580 got_pathlist_free(&changed_paths);
3585 if (reverse_display_order) {
3586 err = got_object_qid_alloc(&qid, id);
3589 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3590 got_object_commit_close(commit);
3592 err = print_commit(commit, id, repo, path,
3593 show_changed_paths ? &changed_paths : NULL,
3594 show_patch, diff_context, refs);
3595 got_object_commit_close(commit);
3599 if ((limit && --limit == 0) ||
3600 (end_id && got_object_id_cmp(id, end_id) == 0))
3603 TAILQ_FOREACH(pe, &changed_paths, entry) {
3604 free((char *)pe->path);
3607 got_pathlist_free(&changed_paths);
3609 if (reverse_display_order) {
3610 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3611 err = got_object_open_as_commit(&commit, repo, qid->id);
3614 if (show_changed_paths) {
3615 err = get_changed_paths(&changed_paths,
3620 err = print_commit(commit, qid->id, repo, path,
3621 show_changed_paths ? &changed_paths : NULL,
3622 show_patch, diff_context, refs);
3623 got_object_commit_close(commit);
3626 TAILQ_FOREACH(pe, &changed_paths, entry) {
3627 free((char *)pe->path);
3630 got_pathlist_free(&changed_paths);
3634 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3635 qid = SIMPLEQ_FIRST(&reversed_commits);
3636 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3637 got_object_qid_free(qid);
3639 TAILQ_FOREACH(pe, &changed_paths, entry) {
3640 free((char *)pe->path);
3643 got_pathlist_free(&changed_paths);
3646 got_commit_graph_close(graph);
3653 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3654 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3655 "[-R] [path]\n", getprogname());
3660 get_default_log_limit(void)
3662 const char *got_default_log_limit;
3666 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3667 if (got_default_log_limit == NULL)
3669 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3675 static const struct got_error *
3676 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3677 struct got_repository *repo)
3679 const struct got_error *err = NULL;
3680 struct got_reference *ref;
3684 err = got_ref_open(&ref, repo, commit_arg, 0);
3687 err = got_ref_resolve(id, repo, ref);
3691 err = got_object_get_type(&obj_type, repo, *id);
3694 if (obj_type == GOT_OBJ_TYPE_TAG) {
3695 struct got_tag_object *tag;
3696 err = got_object_open_as_tag(&tag, repo, *id);
3699 if (got_object_tag_get_object_type(tag) !=
3700 GOT_OBJ_TYPE_COMMIT) {
3701 got_object_tag_close(tag);
3702 return got_error(GOT_ERR_OBJ_TYPE);
3705 *id = got_object_id_dup(
3706 got_object_tag_get_object_id(tag));
3708 err = got_error_from_errno(
3709 "got_object_id_dup");
3710 got_object_tag_close(tag);
3713 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3714 return got_error(GOT_ERR_OBJ_TYPE);
3716 err = got_repo_match_object_id_prefix(id, commit_arg,
3717 GOT_OBJ_TYPE_COMMIT, repo);
3723 static const struct got_error *
3724 cmd_log(int argc, char *argv[])
3726 const struct got_error *error;
3727 struct got_repository *repo = NULL;
3728 struct got_worktree *worktree = NULL;
3729 struct got_object_id *start_id = NULL, *end_id = NULL;
3730 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3731 const char *start_commit = NULL, *end_commit = NULL;
3732 const char *search_pattern = NULL;
3733 int diff_context = -1, ch;
3734 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3735 int reverse_display_order = 0;
3737 struct got_reflist_head refs;
3739 SIMPLEQ_INIT(&refs);
3742 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3748 limit = get_default_log_limit();
3750 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3756 show_changed_paths = 1;
3759 start_commit = optarg;
3762 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3765 err(1, "-C option %s", errstr);
3768 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3770 err(1, "-l option %s", errstr);
3776 repo_path = realpath(optarg, NULL);
3777 if (repo_path == NULL)
3778 return got_error_from_errno2("realpath",
3780 got_path_strip_trailing_slashes(repo_path);
3783 reverse_display_order = 1;
3786 search_pattern = optarg;
3789 end_commit = optarg;
3800 if (diff_context == -1)
3802 else if (!show_patch)
3803 errx(1, "-C requires -p");
3805 cwd = getcwd(NULL, 0);
3807 error = got_error_from_errno("getcwd");
3811 if (repo_path == NULL) {
3812 error = got_worktree_open(&worktree, cwd);
3813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3821 error = got_error_from_errno("strdup");
3824 } else if (argc == 1) {
3826 error = got_worktree_resolve_path(&path, worktree,
3831 path = strdup(argv[0]);
3833 error = got_error_from_errno("strdup");
3840 if (repo_path == NULL) {
3841 repo_path = worktree ?
3842 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3844 if (repo_path == NULL) {
3845 error = got_error_from_errno("strdup");
3849 error = got_repo_open(&repo, repo_path, NULL);
3853 error = apply_unveil(got_repo_get_path(repo), 1,
3854 worktree ? got_worktree_get_root_path(worktree) : NULL);
3858 if (start_commit == NULL) {
3859 struct got_reference *head_ref;
3860 struct got_commit_object *commit = NULL;
3861 error = got_ref_open(&head_ref, repo,
3862 worktree ? got_worktree_get_head_ref_name(worktree)
3866 error = got_ref_resolve(&start_id, repo, head_ref);
3867 got_ref_close(head_ref);
3870 error = got_object_open_as_commit(&commit, repo,
3874 got_object_commit_close(commit);
3876 error = resolve_commit_arg(&start_id, start_commit, repo);
3880 if (end_commit != NULL) {
3881 error = resolve_commit_arg(&end_id, end_commit, repo);
3887 const char *prefix = got_worktree_get_path_prefix(worktree);
3889 if (asprintf(&p, "%s%s%s", prefix,
3890 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3891 error = got_error_from_errno("asprintf");
3894 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3897 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3902 path = in_repo_path;
3905 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3909 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3910 show_patch, search_pattern, diff_context, limit, log_branches,
3911 reverse_display_order, &refs);
3917 got_worktree_close(worktree);
3919 const struct got_error *repo_error;
3920 repo_error = got_repo_close(repo);
3924 got_ref_list_free(&refs);
3931 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3932 "[-w] [object1 object2 | path]\n", getprogname());
3936 struct print_diff_arg {
3937 struct got_repository *repo;
3938 struct got_worktree *worktree;
3943 int ignore_whitespace;
3947 * Create a file which contains the target path of a symlink so we can feed
3948 * it as content to the diff engine.
3950 static const struct got_error *
3951 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3952 const char *abspath)
3954 const struct got_error *err = NULL;
3955 char target_path[PATH_MAX];
3956 ssize_t target_len, outlen;
3961 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3962 if (target_len == -1)
3963 return got_error_from_errno2("readlinkat", abspath);
3965 target_len = readlink(abspath, target_path, PATH_MAX);
3966 if (target_len == -1)
3967 return got_error_from_errno2("readlink", abspath);
3970 *fd = got_opentempfd();
3972 return got_error_from_errno("got_opentempfd");
3974 outlen = write(*fd, target_path, target_len);
3976 err = got_error_from_errno("got_opentempfd");
3980 if (lseek(*fd, 0, SEEK_SET) == -1) {
3981 err = got_error_from_errno2("lseek", abspath);
3992 static const struct got_error *
3993 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3994 const char *path, struct got_object_id *blob_id,
3995 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3996 int dirfd, const char *de_name)
3998 struct print_diff_arg *a = arg;
3999 const struct got_error *err = NULL;
4000 struct got_blob_object *blob1 = NULL;
4003 char *abspath = NULL, *label1 = NULL;
4006 if (a->diff_staged) {
4007 if (staged_status != GOT_STATUS_MODIFY &&
4008 staged_status != GOT_STATUS_ADD &&
4009 staged_status != GOT_STATUS_DELETE)
4012 if (staged_status == GOT_STATUS_DELETE)
4014 if (status == GOT_STATUS_NONEXISTENT)
4015 return got_error_set_errno(ENOENT, path);
4016 if (status != GOT_STATUS_MODIFY &&
4017 status != GOT_STATUS_ADD &&
4018 status != GOT_STATUS_DELETE &&
4019 status != GOT_STATUS_CONFLICT)
4023 if (!a->header_shown) {
4024 printf("diff %s %s%s\n", a->id_str,
4025 got_worktree_get_root_path(a->worktree),
4026 a->diff_staged ? " (staged changes)" : "");
4027 a->header_shown = 1;
4030 if (a->diff_staged) {
4031 const char *label1 = NULL, *label2 = NULL;
4032 switch (staged_status) {
4033 case GOT_STATUS_MODIFY:
4037 case GOT_STATUS_ADD:
4040 case GOT_STATUS_DELETE:
4044 return got_error(GOT_ERR_FILE_STATUS);
4046 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
4047 label1, label2, a->diff_context, a->ignore_whitespace,
4051 if (staged_status == GOT_STATUS_ADD ||
4052 staged_status == GOT_STATUS_MODIFY) {
4054 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4058 err = got_object_id_str(&id_str, staged_blob_id);
4061 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4062 err = got_error_from_errno("asprintf");
4067 } else if (status != GOT_STATUS_ADD) {
4068 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4073 if (status != GOT_STATUS_DELETE) {
4074 if (asprintf(&abspath, "%s/%s",
4075 got_worktree_get_root_path(a->worktree), path) == -1) {
4076 err = got_error_from_errno("asprintf");
4081 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4083 if (errno != ELOOP) {
4084 err = got_error_from_errno2("openat",
4088 err = get_symlink_target_file(&fd, dirfd,
4094 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4096 if (errno != ELOOP) {
4097 err = got_error_from_errno2("open",
4101 err = get_symlink_target_file(&fd, dirfd,
4107 if (fstat(fd, &sb) == -1) {
4108 err = got_error_from_errno2("fstat", abspath);
4111 f2 = fdopen(fd, "r");
4113 err = got_error_from_errno2("fdopen", abspath);
4120 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4121 a->diff_context, a->ignore_whitespace, stdout);
4124 got_object_blob_close(blob1);
4125 if (f2 && fclose(f2) == EOF && err == NULL)
4126 err = got_error_from_errno("fclose");
4127 if (fd != -1 && close(fd) == -1 && err == NULL)
4128 err = got_error_from_errno("close");
4133 static const struct got_error *
4134 cmd_diff(int argc, char *argv[])
4136 const struct got_error *error;
4137 struct got_repository *repo = NULL;
4138 struct got_worktree *worktree = NULL;
4139 char *cwd = NULL, *repo_path = NULL;
4140 struct got_object_id *id1 = NULL, *id2 = NULL;
4141 const char *id_str1 = NULL, *id_str2 = NULL;
4142 char *label1 = NULL, *label2 = NULL;
4144 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4149 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4154 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4157 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4160 err(1, "-C option %s", errstr);
4163 repo_path = realpath(optarg, NULL);
4164 if (repo_path == NULL)
4165 return got_error_from_errno2("realpath",
4167 got_path_strip_trailing_slashes(repo_path);
4173 ignore_whitespace = 1;
4184 cwd = getcwd(NULL, 0);
4186 error = got_error_from_errno("getcwd");
4192 "-r option can't be used when diffing a work tree");
4193 error = got_worktree_open(&worktree, cwd);
4195 if (error->code == GOT_ERR_NOT_WORKTREE)
4196 error = wrap_not_worktree_error(error, "diff",
4200 repo_path = strdup(got_worktree_get_repo_path(worktree));
4201 if (repo_path == NULL) {
4202 error = got_error_from_errno("strdup");
4206 error = got_worktree_resolve_path(&path, worktree,
4213 error = got_error_from_errno("strdup");
4217 } else if (argc == 2) {
4219 errx(1, "-s option can't be used when diffing "
4220 "objects in repository");
4223 if (repo_path == NULL) {
4224 error = got_worktree_open(&worktree, cwd);
4225 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4229 got_worktree_get_repo_path(worktree));
4230 if (repo_path == NULL) {
4231 error = got_error_from_errno("strdup");
4235 repo_path = strdup(cwd);
4236 if (repo_path == NULL) {
4237 error = got_error_from_errno("strdup");
4245 error = got_repo_open(&repo, repo_path, NULL);
4250 error = apply_unveil(got_repo_get_path(repo), 1,
4251 worktree ? got_worktree_get_root_path(worktree) : NULL);
4256 struct print_diff_arg arg;
4257 struct got_pathlist_head paths;
4262 error = got_object_id_str(&id_str,
4263 got_worktree_get_base_commit_id(worktree));
4267 arg.worktree = worktree;
4268 arg.diff_context = diff_context;
4269 arg.id_str = id_str;
4270 arg.header_shown = 0;
4271 arg.diff_staged = diff_staged;
4272 arg.ignore_whitespace = ignore_whitespace;
4274 error = got_pathlist_append(&paths, path, NULL);
4278 error = got_worktree_status(worktree, &paths, repo, print_diff,
4279 &arg, check_cancelled, NULL);
4281 got_pathlist_free(&paths);
4285 error = got_repo_match_object_id(&id1, &label1, id_str1,
4286 GOT_OBJ_TYPE_ANY, 1, repo);
4290 error = got_repo_match_object_id(&id2, &label2, id_str2,
4291 GOT_OBJ_TYPE_ANY, 1, repo);
4295 error = got_object_get_type(&type1, repo, id1);
4299 error = got_object_get_type(&type2, repo, id2);
4303 if (type1 != type2) {
4304 error = got_error(GOT_ERR_OBJ_TYPE);
4309 case GOT_OBJ_TYPE_BLOB:
4310 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4311 diff_context, ignore_whitespace, repo, stdout);
4313 case GOT_OBJ_TYPE_TREE:
4314 error = got_diff_objects_as_trees(id1, id2, "", "",
4315 diff_context, ignore_whitespace, repo, stdout);
4317 case GOT_OBJ_TYPE_COMMIT:
4318 printf("diff %s %s\n", label1, label2);
4319 error = got_diff_objects_as_commits(id1, id2, diff_context,
4320 ignore_whitespace, repo, stdout);
4323 error = got_error(GOT_ERR_OBJ_TYPE);
4332 got_worktree_close(worktree);
4334 const struct got_error *repo_error;
4335 repo_error = got_repo_close(repo);
4346 "usage: %s blame [-c commit] [-r repository-path] path\n",
4355 char datebuf[11]; /* YYYY-MM-DD + NUL */
4358 struct blame_cb_args {
4359 struct blame_line *lines;
4363 off_t *line_offsets;
4365 struct got_repository *repo;
4368 static const struct got_error *
4369 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4371 const struct got_error *err = NULL;
4372 struct blame_cb_args *a = arg;
4373 struct blame_line *bline;
4375 size_t linesize = 0;
4376 struct got_commit_object *commit = NULL;
4379 time_t committer_time;
4381 if (nlines != a->nlines ||
4382 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4383 return got_error(GOT_ERR_RANGE);
4385 if (sigint_received)
4386 return got_error(GOT_ERR_ITER_COMPLETED);
4389 return NULL; /* no change in this commit */
4391 /* Annotate this line. */
4392 bline = &a->lines[lineno - 1];
4393 if (bline->annotated)
4395 err = got_object_id_str(&bline->id_str, id);
4399 err = got_object_open_as_commit(&commit, a->repo, id);
4403 bline->committer = strdup(got_object_commit_get_committer(commit));
4404 if (bline->committer == NULL) {
4405 err = got_error_from_errno("strdup");
4409 committer_time = got_object_commit_get_committer_time(commit);
4410 if (localtime_r(&committer_time, &tm) == NULL)
4411 return got_error_from_errno("localtime_r");
4412 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4413 &tm) >= sizeof(bline->datebuf)) {
4414 err = got_error(GOT_ERR_NO_SPACE);
4417 bline->annotated = 1;
4419 /* Print lines annotated so far. */
4420 bline = &a->lines[a->lineno_cur - 1];
4421 if (!bline->annotated)
4424 offset = a->line_offsets[a->lineno_cur - 1];
4425 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4426 err = got_error_from_errno("fseeko");
4430 while (bline->annotated) {
4431 char *smallerthan, *at, *nl, *committer;
4434 if (getline(&line, &linesize, a->f) == -1) {
4436 err = got_error_from_errno("getline");
4440 committer = bline->committer;
4441 smallerthan = strchr(committer, '<');
4442 if (smallerthan && smallerthan[1] != '\0')
4443 committer = smallerthan + 1;
4444 at = strchr(committer, '@');
4447 len = strlen(committer);
4449 committer[8] = '\0';
4451 nl = strchr(line, '\n');
4454 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4455 bline->id_str, bline->datebuf, committer, line);
4458 bline = &a->lines[a->lineno_cur - 1];
4462 got_object_commit_close(commit);
4467 static const struct got_error *
4468 cmd_blame(int argc, char *argv[])
4470 const struct got_error *error;
4471 struct got_repository *repo = NULL;
4472 struct got_worktree *worktree = NULL;
4473 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4474 char *link_target = NULL;
4475 struct got_object_id *obj_id = NULL;
4476 struct got_object_id *commit_id = NULL;
4477 struct got_blob_object *blob = NULL;
4478 char *commit_id_str = NULL;
4479 struct blame_cb_args bca;
4480 int ch, obj_type, i;
4483 memset(&bca, 0, sizeof(bca));
4486 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4491 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4494 commit_id_str = optarg;
4497 repo_path = realpath(optarg, NULL);
4498 if (repo_path == NULL)
4499 return got_error_from_errno2("realpath",
4501 got_path_strip_trailing_slashes(repo_path);
4517 cwd = getcwd(NULL, 0);
4519 error = got_error_from_errno("getcwd");
4522 if (repo_path == NULL) {
4523 error = got_worktree_open(&worktree, cwd);
4524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4530 strdup(got_worktree_get_repo_path(worktree));
4531 if (repo_path == NULL) {
4532 error = got_error_from_errno("strdup");
4537 repo_path = strdup(cwd);
4538 if (repo_path == NULL) {
4539 error = got_error_from_errno("strdup");
4545 error = got_repo_open(&repo, repo_path, NULL);
4549 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4554 const char *prefix = got_worktree_get_path_prefix(worktree);
4555 char *p, *worktree_subdir = cwd +
4556 strlen(got_worktree_get_root_path(worktree));
4557 if (asprintf(&p, "%s%s%s%s%s",
4558 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4559 worktree_subdir, worktree_subdir[0] ? "/" : "",
4561 error = got_error_from_errno("asprintf");
4564 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4567 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4572 if (commit_id_str == NULL) {
4573 struct got_reference *head_ref;
4574 error = got_ref_open(&head_ref, repo, worktree ?
4575 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4578 error = got_ref_resolve(&commit_id, repo, head_ref);
4579 got_ref_close(head_ref);
4583 error = got_repo_match_object_id(&commit_id, NULL,
4584 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4589 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4594 error = got_object_id_by_path(&obj_id, repo, commit_id,
4595 link_target ? link_target : in_repo_path);
4599 error = got_object_get_type(&obj_type, repo, obj_id);
4603 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4604 error = got_error_path(link_target ? link_target : in_repo_path,
4609 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4612 bca.f = got_opentemp();
4613 if (bca.f == NULL) {
4614 error = got_error_from_errno("got_opentemp");
4617 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4618 &bca.line_offsets, bca.f, blob);
4619 if (error || bca.nlines == 0)
4622 /* Don't include \n at EOF in the blame line count. */
4623 if (bca.line_offsets[bca.nlines - 1] == filesize)
4626 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4627 if (bca.lines == NULL) {
4628 error = got_error_from_errno("calloc");
4632 bca.nlines_prec = 0;
4640 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4641 repo, blame_cb, &bca, check_cancelled, NULL);
4650 got_object_blob_close(blob);
4652 got_worktree_close(worktree);
4654 const struct got_error *repo_error;
4655 repo_error = got_repo_close(repo);
4660 for (i = 0; i < bca.nlines; i++) {
4661 struct blame_line *bline = &bca.lines[i];
4662 free(bline->id_str);
4663 free(bline->committer);
4667 free(bca.line_offsets);
4668 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4669 error = got_error_from_errno("fclose");
4677 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4682 static const struct got_error *
4683 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4684 const char *root_path, struct got_repository *repo)
4686 const struct got_error *err = NULL;
4687 int is_root_path = (strcmp(path, root_path) == 0);
4688 const char *modestr = "";
4689 mode_t mode = got_tree_entry_get_mode(te);
4690 char *link_target = NULL;
4692 path += strlen(root_path);
4693 while (path[0] == '/')
4696 if (got_object_tree_entry_is_submodule(te))
4698 else if (S_ISLNK(mode)) {
4701 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4704 for (i = 0; i < strlen(link_target); i++) {
4705 if (!isprint((unsigned char)link_target[i]))
4706 link_target[i] = '?';
4711 else if (S_ISDIR(mode))
4713 else if (mode & S_IXUSR)
4716 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4717 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4718 link_target ? " -> ": "", link_target ? link_target : "");
4724 static const struct got_error *
4725 print_tree(const char *path, struct got_object_id *commit_id,
4726 int show_ids, int recurse, const char *root_path,
4727 struct got_repository *repo)
4729 const struct got_error *err = NULL;
4730 struct got_object_id *tree_id = NULL;
4731 struct got_tree_object *tree = NULL;
4734 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4738 err = got_object_open_as_tree(&tree, repo, tree_id);
4741 nentries = got_object_tree_get_nentries(tree);
4742 for (i = 0; i < nentries; i++) {
4743 struct got_tree_entry *te;
4746 if (sigint_received || sigpipe_received)
4749 te = got_object_tree_get_entry(tree, i);
4752 err = got_object_id_str(&id_str,
4753 got_tree_entry_get_id(te));
4756 if (asprintf(&id, "%s ", id_str) == -1) {
4757 err = got_error_from_errno("asprintf");
4763 err = print_entry(te, id, path, root_path, repo);
4768 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4770 if (asprintf(&child_path, "%s%s%s", path,
4771 path[0] == '/' && path[1] == '\0' ? "" : "/",
4772 got_tree_entry_get_name(te)) == -1) {
4773 err = got_error_from_errno("asprintf");
4776 err = print_tree(child_path, commit_id, show_ids, 1,
4785 got_object_tree_close(tree);
4790 static const struct got_error *
4791 cmd_tree(int argc, char *argv[])
4793 const struct got_error *error;
4794 struct got_repository *repo = NULL;
4795 struct got_worktree *worktree = NULL;
4796 const char *path, *refname = NULL;
4797 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4798 struct got_object_id *commit_id = NULL;
4799 char *commit_id_str = NULL;
4800 int show_ids = 0, recurse = 0;
4804 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4809 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4812 commit_id_str = optarg;
4815 repo_path = realpath(optarg, NULL);
4816 if (repo_path == NULL)
4817 return got_error_from_errno2("realpath",
4819 got_path_strip_trailing_slashes(repo_path);
4843 cwd = getcwd(NULL, 0);
4845 error = got_error_from_errno("getcwd");
4848 if (repo_path == NULL) {
4849 error = got_worktree_open(&worktree, cwd);
4850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4856 strdup(got_worktree_get_repo_path(worktree));
4857 if (repo_path == NULL)
4858 error = got_error_from_errno("strdup");
4862 repo_path = strdup(cwd);
4863 if (repo_path == NULL) {
4864 error = got_error_from_errno("strdup");
4870 error = got_repo_open(&repo, repo_path, NULL);
4874 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4880 char *p, *worktree_subdir = cwd +
4881 strlen(got_worktree_get_root_path(worktree));
4882 if (asprintf(&p, "%s/%s",
4883 got_worktree_get_path_prefix(worktree),
4884 worktree_subdir) == -1) {
4885 error = got_error_from_errno("asprintf");
4888 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4895 if (in_repo_path == NULL) {
4896 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4901 if (commit_id_str == NULL) {
4902 struct got_reference *head_ref;
4904 refname = got_worktree_get_head_ref_name(worktree);
4906 refname = GOT_REF_HEAD;
4907 error = got_ref_open(&head_ref, repo, refname, 0);
4910 error = got_ref_resolve(&commit_id, repo, head_ref);
4911 got_ref_close(head_ref);
4915 error = got_repo_match_object_id(&commit_id, NULL,
4916 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4921 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4922 in_repo_path, repo);
4929 got_worktree_close(worktree);
4931 const struct got_error *repo_error;
4932 repo_error = got_repo_close(repo);
4942 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4947 static const struct got_error *
4948 print_status(void *arg, unsigned char status, unsigned char staged_status,
4949 const char *path, struct got_object_id *blob_id,
4950 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4951 int dirfd, const char *de_name)
4953 if (status == staged_status && (status == GOT_STATUS_DELETE))
4954 status = GOT_STATUS_NO_CHANGE;
4956 char *status_codes = arg;
4957 size_t ncodes = strlen(status_codes);
4959 for (i = 0; i < ncodes ; i++) {
4960 if (status == status_codes[i] ||
4961 staged_status == status_codes[i])
4967 printf("%c%c %s\n", status, staged_status, path);
4971 static const struct got_error *
4972 cmd_status(int argc, char *argv[])
4974 const struct got_error *error = NULL;
4975 struct got_repository *repo = NULL;
4976 struct got_worktree *worktree = NULL;
4977 char *cwd = NULL, *status_codes = NULL;;
4978 struct got_pathlist_head paths;
4979 struct got_pathlist_entry *pe;
4984 while ((ch = getopt(argc, argv, "s:")) != -1) {
4987 for (i = 0; i < strlen(optarg); i++) {
4988 switch (optarg[i]) {
4989 case GOT_STATUS_MODIFY:
4990 case GOT_STATUS_ADD:
4991 case GOT_STATUS_DELETE:
4992 case GOT_STATUS_CONFLICT:
4993 case GOT_STATUS_MISSING:
4994 case GOT_STATUS_OBSTRUCTED:
4995 case GOT_STATUS_UNVERSIONED:
4996 case GOT_STATUS_MODE_CHANGE:
4997 case GOT_STATUS_NONEXISTENT:
5000 errx(1, "invalid status code '%c'",
5004 status_codes = optarg;
5016 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5020 cwd = getcwd(NULL, 0);
5022 error = got_error_from_errno("getcwd");
5026 error = got_worktree_open(&worktree, cwd);
5028 if (error->code == GOT_ERR_NOT_WORKTREE)
5029 error = wrap_not_worktree_error(error, "status", cwd);
5033 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5038 error = apply_unveil(got_repo_get_path(repo), 1,
5039 got_worktree_get_root_path(worktree));
5043 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5047 error = got_worktree_status(worktree, &paths, repo, print_status,
5048 status_codes, check_cancelled, NULL);
5050 TAILQ_FOREACH(pe, &paths, entry)
5051 free((char *)pe->path);
5052 got_pathlist_free(&paths);
5061 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5067 static const struct got_error *
5068 list_refs(struct got_repository *repo, const char *refname)
5070 static const struct got_error *err = NULL;
5071 struct got_reflist_head refs;
5072 struct got_reflist_entry *re;
5074 SIMPLEQ_INIT(&refs);
5075 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5079 SIMPLEQ_FOREACH(re, &refs, entry) {
5081 refstr = got_ref_to_str(re->ref);
5083 return got_error_from_errno("got_ref_to_str");
5084 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5088 got_ref_list_free(&refs);
5092 static const struct got_error *
5093 delete_ref(struct got_repository *repo, const char *refname)
5095 const struct got_error *err = NULL;
5096 struct got_reference *ref;
5098 err = got_ref_open(&ref, repo, refname, 0);
5102 err = got_ref_delete(ref, repo);
5107 static const struct got_error *
5108 add_ref(struct got_repository *repo, const char *refname, const char *target)
5110 const struct got_error *err = NULL;
5111 struct got_object_id *id;
5112 struct got_reference *ref = NULL;
5115 * Don't let the user create a reference name with a leading '-'.
5116 * While technically a valid reference name, this case is usually
5117 * an unintended typo.
5119 if (refname[0] == '-')
5120 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5122 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5125 struct got_reference *target_ref;
5127 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5129 err = got_ref_open(&target_ref, repo, target, 0);
5132 err = got_ref_resolve(&id, repo, target_ref);
5133 got_ref_close(target_ref);
5138 err = got_ref_alloc(&ref, refname, id);
5142 err = got_ref_write(ref, repo);
5150 static const struct got_error *
5151 add_symref(struct got_repository *repo, const char *refname, const char *target)
5153 const struct got_error *err = NULL;
5154 struct got_reference *ref = NULL;
5155 struct got_reference *target_ref = NULL;
5158 * Don't let the user create a reference name with a leading '-'.
5159 * While technically a valid reference name, this case is usually
5160 * an unintended typo.
5162 if (refname[0] == '-')
5163 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5165 err = got_ref_open(&target_ref, repo, target, 0);
5169 err = got_ref_alloc_symref(&ref, refname, target_ref);
5173 err = got_ref_write(ref, repo);
5176 got_ref_close(target_ref);
5182 static const struct got_error *
5183 cmd_ref(int argc, char *argv[])
5185 const struct got_error *error = NULL;
5186 struct got_repository *repo = NULL;
5187 struct got_worktree *worktree = NULL;
5188 char *cwd = NULL, *repo_path = NULL;
5189 int ch, do_list = 0, do_delete = 0;
5190 const char *obj_arg = NULL, *symref_target= NULL;
5191 char *refname = NULL;
5193 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5202 repo_path = realpath(optarg, NULL);
5203 if (repo_path == NULL)
5204 return got_error_from_errno2("realpath",
5206 got_path_strip_trailing_slashes(repo_path);
5212 symref_target = optarg;
5220 if (obj_arg && do_list)
5221 errx(1, "-c and -l options are mutually exclusive");
5222 if (obj_arg && do_delete)
5223 errx(1, "-c and -d options are mutually exclusive");
5224 if (obj_arg && symref_target)
5225 errx(1, "-c and -s options are mutually exclusive");
5226 if (symref_target && do_delete)
5227 errx(1, "-s and -d options are mutually exclusive");
5228 if (symref_target && do_list)
5229 errx(1, "-s and -l options are mutually exclusive");
5230 if (do_delete && do_list)
5231 errx(1, "-d and -l options are mutually exclusive");
5237 if (argc != 0 && argc != 1)
5240 refname = strdup(argv[0]);
5241 if (refname == NULL) {
5242 error = got_error_from_errno("strdup");
5249 refname = strdup(argv[0]);
5250 if (refname == NULL) {
5251 error = got_error_from_errno("strdup");
5257 got_path_strip_trailing_slashes(refname);
5261 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5265 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5266 "sendfd unveil", NULL) == -1)
5270 cwd = getcwd(NULL, 0);
5272 error = got_error_from_errno("getcwd");
5276 if (repo_path == NULL) {
5277 error = got_worktree_open(&worktree, cwd);
5278 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5284 strdup(got_worktree_get_repo_path(worktree));
5285 if (repo_path == NULL)
5286 error = got_error_from_errno("strdup");
5290 repo_path = strdup(cwd);
5291 if (repo_path == NULL) {
5292 error = got_error_from_errno("strdup");
5298 error = got_repo_open(&repo, repo_path, NULL);
5302 error = apply_unveil(got_repo_get_path(repo), do_list,
5303 worktree ? got_worktree_get_root_path(worktree) : NULL);
5308 error = list_refs(repo, refname);
5310 error = delete_ref(repo, refname);
5311 else if (symref_target)
5312 error = add_symref(repo, refname, symref_target);
5314 if (obj_arg == NULL)
5316 error = add_ref(repo, refname, obj_arg);
5321 got_repo_close(repo);
5323 got_worktree_close(worktree);
5333 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5334 "[name]\n", getprogname());
5338 static const struct got_error *
5339 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5340 struct got_reference *ref)
5342 const struct got_error *err = NULL;
5343 const char *refname, *marker = " ";
5346 refname = got_ref_get_name(ref);
5347 if (worktree && strcmp(refname,
5348 got_worktree_get_head_ref_name(worktree)) == 0) {
5349 struct got_object_id *id = NULL;
5351 err = got_ref_resolve(&id, repo, ref);
5354 if (got_object_id_cmp(id,
5355 got_worktree_get_base_commit_id(worktree)) == 0)
5362 if (strncmp(refname, "refs/heads/", 11) == 0)
5364 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5367 refstr = got_ref_to_str(ref);
5369 return got_error_from_errno("got_ref_to_str");
5371 printf("%s%s: %s\n", marker, refname, refstr);
5376 static const struct got_error *
5377 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5379 const char *refname;
5381 if (worktree == NULL)
5382 return got_error(GOT_ERR_NOT_WORKTREE);
5384 refname = got_worktree_get_head_ref_name(worktree);
5386 if (strncmp(refname, "refs/heads/", 11) == 0)
5388 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5391 printf("%s\n", refname);
5396 static const struct got_error *
5397 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5399 static const struct got_error *err = NULL;
5400 struct got_reflist_head refs;
5401 struct got_reflist_entry *re;
5402 struct got_reference *temp_ref = NULL;
5403 int rebase_in_progress, histedit_in_progress;
5405 SIMPLEQ_INIT(&refs);
5408 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5413 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5418 if (rebase_in_progress || histedit_in_progress) {
5419 err = got_ref_open(&temp_ref, repo,
5420 got_worktree_get_head_ref_name(worktree), 0);
5423 list_branch(repo, worktree, temp_ref);
5424 got_ref_close(temp_ref);
5428 err = got_ref_list(&refs, repo, "refs/heads",
5429 got_ref_cmp_by_name, NULL);
5433 SIMPLEQ_FOREACH(re, &refs, entry)
5434 list_branch(repo, worktree, re->ref);
5436 got_ref_list_free(&refs);
5440 static const struct got_error *
5441 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5442 const char *branch_name)
5444 const struct got_error *err = NULL;
5445 struct got_reference *ref = NULL;
5448 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5449 return got_error_from_errno("asprintf");
5451 err = got_ref_open(&ref, repo, refname, 0);
5456 strcmp(got_worktree_get_head_ref_name(worktree),
5457 got_ref_get_name(ref)) == 0) {
5458 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5459 "will not delete this work tree's current branch");
5463 err = got_ref_delete(ref, repo);
5471 static const struct got_error *
5472 add_branch(struct got_repository *repo, const char *branch_name,
5473 struct got_object_id *base_commit_id)
5475 const struct got_error *err = NULL;
5476 struct got_reference *ref = NULL;
5477 char *base_refname = NULL, *refname = NULL;
5480 * Don't let the user create a branch name with a leading '-'.
5481 * While technically a valid reference name, this case is usually
5482 * an unintended typo.
5484 if (branch_name[0] == '-')
5485 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5487 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5488 err = got_error_from_errno("asprintf");
5492 err = got_ref_open(&ref, repo, refname, 0);
5494 err = got_error(GOT_ERR_BRANCH_EXISTS);
5496 } else if (err->code != GOT_ERR_NOT_REF)
5499 err = got_ref_alloc(&ref, refname, base_commit_id);
5503 err = got_ref_write(ref, repo);
5512 static const struct got_error *
5513 cmd_branch(int argc, char *argv[])
5515 const struct got_error *error = NULL;
5516 struct got_repository *repo = NULL;
5517 struct got_worktree *worktree = NULL;
5518 char *cwd = NULL, *repo_path = NULL;
5519 int ch, do_list = 0, do_show = 0, do_update = 1;
5520 const char *delref = NULL, *commit_id_arg = NULL;
5521 struct got_reference *ref = NULL;
5522 struct got_pathlist_head paths;
5523 struct got_pathlist_entry *pe;
5524 struct got_object_id *commit_id = NULL;
5525 char *commit_id_str = NULL;
5529 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5532 commit_id_arg = optarg;
5538 repo_path = realpath(optarg, NULL);
5539 if (repo_path == NULL)
5540 return got_error_from_errno2("realpath",
5542 got_path_strip_trailing_slashes(repo_path);
5556 if (do_list && delref)
5557 errx(1, "-l and -d options are mutually exclusive");
5562 if (!do_list && !delref && argc == 0)
5565 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5566 errx(1, "-c option can only be used when creating a branch");
5568 if (do_list || delref) {
5571 } else if (!do_show && argc != 1)
5575 if (do_list || do_show) {
5576 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5581 "sendfd unveil", NULL) == -1)
5585 cwd = getcwd(NULL, 0);
5587 error = got_error_from_errno("getcwd");
5591 if (repo_path == NULL) {
5592 error = got_worktree_open(&worktree, cwd);
5593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5599 strdup(got_worktree_get_repo_path(worktree));
5600 if (repo_path == NULL)
5601 error = got_error_from_errno("strdup");
5605 repo_path = strdup(cwd);
5606 if (repo_path == NULL) {
5607 error = got_error_from_errno("strdup");
5613 error = got_repo_open(&repo, repo_path, NULL);
5617 error = apply_unveil(got_repo_get_path(repo), do_list,
5618 worktree ? got_worktree_get_root_path(worktree) : NULL);
5623 error = show_current_branch(repo, worktree);
5625 error = list_branches(repo, worktree);
5627 error = delete_branch(repo, worktree, delref);
5629 if (commit_id_arg == NULL)
5630 commit_id_arg = worktree ?
5631 got_worktree_get_head_ref_name(worktree) :
5633 error = got_repo_match_object_id(&commit_id, NULL,
5634 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5637 error = add_branch(repo, argv[0], commit_id);
5640 if (worktree && do_update) {
5641 struct got_update_progress_arg upa;
5642 char *branch_refname = NULL;
5644 error = got_object_id_str(&commit_id_str, commit_id);
5647 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5651 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5653 error = got_error_from_errno("asprintf");
5656 error = got_ref_open(&ref, repo, branch_refname, 0);
5657 free(branch_refname);
5660 error = switch_head_ref(ref, commit_id, worktree,
5664 error = got_worktree_set_base_commit_id(worktree, repo,
5668 memset(&upa, 0, sizeof(upa));
5669 error = got_worktree_checkout_files(worktree, &paths,
5670 repo, update_progress, &upa, check_cancelled,
5674 if (upa.did_something)
5675 printf("Updated to commit %s\n", commit_id_str);
5676 print_update_progress_stats(&upa);
5683 got_repo_close(repo);
5685 got_worktree_close(worktree);
5689 free(commit_id_str);
5690 TAILQ_FOREACH(pe, &paths, entry)
5691 free((char *)pe->path);
5692 got_pathlist_free(&paths);
5701 "usage: %s tag [-c commit] [-r repository] [-l] "
5702 "[-m message] name\n", getprogname());
5707 static const struct got_error *
5708 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5710 const struct got_error *err = NULL;
5711 struct got_reflist_entry *re, *se, *new;
5712 struct got_object_id *re_id, *se_id;
5713 struct got_tag_object *re_tag, *se_tag;
5714 time_t re_time, se_time;
5716 SIMPLEQ_FOREACH(re, tags, entry) {
5717 se = SIMPLEQ_FIRST(sorted);
5719 err = got_reflist_entry_dup(&new, re);
5722 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5725 err = got_ref_resolve(&re_id, repo, re->ref);
5728 err = got_object_open_as_tag(&re_tag, repo, re_id);
5732 re_time = got_object_tag_get_tagger_time(re_tag);
5733 got_object_tag_close(re_tag);
5737 err = got_ref_resolve(&se_id, repo, re->ref);
5740 err = got_object_open_as_tag(&se_tag, repo, se_id);
5744 se_time = got_object_tag_get_tagger_time(se_tag);
5745 got_object_tag_close(se_tag);
5747 if (se_time > re_time) {
5748 err = got_reflist_entry_dup(&new, re);
5751 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5754 se = SIMPLEQ_NEXT(se, entry);
5763 static const struct got_error *
5764 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5766 static const struct got_error *err = NULL;
5767 struct got_reflist_head refs;
5768 struct got_reflist_entry *re;
5770 SIMPLEQ_INIT(&refs);
5772 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5776 SIMPLEQ_FOREACH(re, &refs, entry) {
5777 const char *refname;
5778 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5782 struct got_object_id *id;
5783 struct got_tag_object *tag;
5784 struct got_commit_object *commit = NULL;
5786 refname = got_ref_get_name(re->ref);
5787 if (strncmp(refname, "refs/tags/", 10) != 0)
5790 refstr = got_ref_to_str(re->ref);
5791 if (refstr == NULL) {
5792 err = got_error_from_errno("got_ref_to_str");
5795 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5798 err = got_ref_resolve(&id, repo, re->ref);
5801 err = got_object_open_as_tag(&tag, repo, id);
5803 if (err->code != GOT_ERR_OBJ_TYPE) {
5807 /* "lightweight" tag */
5808 err = got_object_open_as_commit(&commit, repo, id);
5813 tagger = got_object_commit_get_committer(commit);
5815 got_object_commit_get_committer_time(commit);
5816 err = got_object_id_str(&id_str, id);
5822 tagger = got_object_tag_get_tagger(tag);
5823 tagger_time = got_object_tag_get_tagger_time(tag);
5824 err = got_object_id_str(&id_str,
5825 got_object_tag_get_object_id(tag));
5829 printf("from: %s\n", tagger);
5830 datestr = get_datestr(&tagger_time, datebuf);
5832 printf("date: %s UTC\n", datestr);
5834 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5836 switch (got_object_tag_get_object_type(tag)) {
5837 case GOT_OBJ_TYPE_BLOB:
5838 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5841 case GOT_OBJ_TYPE_TREE:
5842 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5845 case GOT_OBJ_TYPE_COMMIT:
5846 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5849 case GOT_OBJ_TYPE_TAG:
5850 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5859 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5862 got_object_commit_close(commit);
5864 tagmsg0 = strdup(got_object_tag_get_message(tag));
5865 got_object_tag_close(tag);
5866 if (tagmsg0 == NULL) {
5867 err = got_error_from_errno("strdup");
5874 line = strsep(&tagmsg, "\n");
5876 printf(" %s\n", line);
5881 got_ref_list_free(&refs);
5885 static const struct got_error *
5886 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5887 const char *tag_name, const char *repo_path)
5889 const struct got_error *err = NULL;
5890 char *template = NULL, *initial_content = NULL;
5891 char *editor = NULL;
5892 int initial_content_len;
5895 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5896 err = got_error_from_errno("asprintf");
5900 initial_content_len = asprintf(&initial_content,
5901 "\n# tagging commit %s as %s\n",
5902 commit_id_str, tag_name);
5903 if (initial_content_len == -1) {
5904 err = got_error_from_errno("asprintf");
5908 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5912 if (write(fd, initial_content, initial_content_len) == -1) {
5913 err = got_error_from_errno2("write", *tagmsg_path);
5917 err = get_editor(&editor);
5920 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
5921 initial_content_len);
5923 free(initial_content);
5927 if (fd != -1 && close(fd) == -1 && err == NULL)
5928 err = got_error_from_errno2("close", *tagmsg_path);
5930 /* Editor is done; we can now apply unveil(2) */
5932 err = apply_unveil(repo_path, 0, NULL);
5940 static const struct got_error *
5941 add_tag(struct got_repository *repo, struct got_worktree *worktree,
5942 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5944 const struct got_error *err = NULL;
5945 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5946 char *label = NULL, *commit_id_str = NULL;
5947 struct got_reference *ref = NULL;
5948 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5949 char *tagmsg_path = NULL, *tag_id_str = NULL;
5950 int preserve_tagmsg = 0;
5953 * Don't let the user create a tag name with a leading '-'.
5954 * While technically a valid reference name, this case is usually
5955 * an unintended typo.
5957 if (tag_name[0] == '-')
5958 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5960 err = get_author(&tagger, repo, worktree);
5964 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5965 GOT_OBJ_TYPE_COMMIT, 1, repo);
5969 err = got_object_id_str(&commit_id_str, commit_id);
5973 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5974 refname = strdup(tag_name);
5975 if (refname == NULL) {
5976 err = got_error_from_errno("strdup");
5980 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5981 err = got_error_from_errno("asprintf");
5985 err = got_ref_open(&ref, repo, refname, 0);
5987 err = got_error(GOT_ERR_TAG_EXISTS);
5989 } else if (err->code != GOT_ERR_NOT_REF)
5992 if (tagmsg_arg == NULL) {
5993 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5994 tag_name, got_repo_get_path(repo));
5996 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5997 tagmsg_path != NULL)
5998 preserve_tagmsg = 1;
6003 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6004 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6007 preserve_tagmsg = 1;
6011 err = got_ref_alloc(&ref, refname, tag_id);
6014 preserve_tagmsg = 1;
6018 err = got_ref_write(ref, repo);
6021 preserve_tagmsg = 1;
6025 err = got_object_id_str(&tag_id_str, tag_id);
6028 preserve_tagmsg = 1;
6031 printf("Created tag %s\n", tag_id_str);
6033 if (preserve_tagmsg) {
6034 fprintf(stderr, "%s: tag message preserved in %s\n",
6035 getprogname(), tagmsg_path);
6036 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6037 err = got_error_from_errno2("unlink", tagmsg_path);
6042 free(commit_id_str);
6050 static const struct got_error *
6051 cmd_tag(int argc, char *argv[])
6053 const struct got_error *error = NULL;
6054 struct got_repository *repo = NULL;
6055 struct got_worktree *worktree = NULL;
6056 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6057 char *gitconfig_path = NULL;
6058 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6059 int ch, do_list = 0;
6061 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6064 commit_id_arg = optarg;
6070 repo_path = realpath(optarg, NULL);
6071 if (repo_path == NULL)
6072 return got_error_from_errno2("realpath",
6074 got_path_strip_trailing_slashes(repo_path);
6089 if (commit_id_arg != NULL)
6091 "-c option can only be used when creating a tag");
6093 errx(1, "-l and -m options are mutually exclusive");
6096 } else if (argc != 1)
6103 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6107 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6108 "sendfd unveil", NULL) == -1)
6112 cwd = getcwd(NULL, 0);
6114 error = got_error_from_errno("getcwd");
6118 if (repo_path == NULL) {
6119 error = got_worktree_open(&worktree, cwd);
6120 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6126 strdup(got_worktree_get_repo_path(worktree));
6127 if (repo_path == NULL)
6128 error = got_error_from_errno("strdup");
6132 repo_path = strdup(cwd);
6133 if (repo_path == NULL) {
6134 error = got_error_from_errno("strdup");
6141 error = got_repo_open(&repo, repo_path, NULL);
6144 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6147 error = list_tags(repo, worktree);
6149 error = get_gitconfig_path(&gitconfig_path);
6152 error = got_repo_open(&repo, repo_path, gitconfig_path);
6157 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6162 if (commit_id_arg == NULL) {
6163 struct got_reference *head_ref;
6164 struct got_object_id *commit_id;
6165 error = got_ref_open(&head_ref, repo,
6166 worktree ? got_worktree_get_head_ref_name(worktree)
6170 error = got_ref_resolve(&commit_id, repo, head_ref);
6171 got_ref_close(head_ref);
6174 error = got_object_id_str(&commit_id_str, commit_id);
6180 error = add_tag(repo, worktree, tag_name,
6181 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6185 got_repo_close(repo);
6187 got_worktree_close(worktree);
6190 free(gitconfig_path);
6191 free(commit_id_str);
6198 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6203 static const struct got_error *
6204 add_progress(void *arg, unsigned char status, const char *path)
6206 while (path[0] == '/')
6208 printf("%c %s\n", status, path);
6212 static const struct got_error *
6213 cmd_add(int argc, char *argv[])
6215 const struct got_error *error = NULL;
6216 struct got_repository *repo = NULL;
6217 struct got_worktree *worktree = NULL;
6219 struct got_pathlist_head paths;
6220 struct got_pathlist_entry *pe;
6221 int ch, can_recurse = 0, no_ignores = 0;
6225 while ((ch = getopt(argc, argv, "IR")) != -1) {
6243 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6250 cwd = getcwd(NULL, 0);
6252 error = got_error_from_errno("getcwd");
6256 error = got_worktree_open(&worktree, cwd);
6258 if (error->code == GOT_ERR_NOT_WORKTREE)
6259 error = wrap_not_worktree_error(error, "add", cwd);
6263 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6268 error = apply_unveil(got_repo_get_path(repo), 1,
6269 got_worktree_get_root_path(worktree));
6273 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6277 if (!can_recurse && no_ignores) {
6278 error = got_error_msg(GOT_ERR_BAD_PATH,
6279 "disregarding ignores requires -R option");
6287 TAILQ_FOREACH(pe, &paths, entry) {
6288 if (asprintf(&ondisk_path, "%s/%s",
6289 got_worktree_get_root_path(worktree),
6291 error = got_error_from_errno("asprintf");
6294 if (lstat(ondisk_path, &sb) == -1) {
6295 if (errno == ENOENT) {
6299 error = got_error_from_errno2("lstat",
6305 if (S_ISDIR(sb.st_mode)) {
6306 error = got_error_msg(GOT_ERR_BAD_PATH,
6307 "adding directories requires -R option");
6313 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6314 NULL, repo, no_ignores);
6317 got_repo_close(repo);
6319 got_worktree_close(worktree);
6320 TAILQ_FOREACH(pe, &paths, entry)
6321 free((char *)pe->path);
6322 got_pathlist_free(&paths);
6330 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6331 "path ...\n", getprogname());
6335 static const struct got_error *
6336 print_remove_status(void *arg, unsigned char status,
6337 unsigned char staged_status, const char *path)
6339 while (path[0] == '/')
6341 if (status == GOT_STATUS_NONEXISTENT)
6343 if (status == staged_status && (status == GOT_STATUS_DELETE))
6344 status = GOT_STATUS_NO_CHANGE;
6345 printf("%c%c %s\n", status, staged_status, path);
6349 static const struct got_error *
6350 cmd_remove(int argc, char *argv[])
6352 const struct got_error *error = NULL;
6353 struct got_worktree *worktree = NULL;
6354 struct got_repository *repo = NULL;
6355 const char *status_codes = NULL;
6357 struct got_pathlist_head paths;
6358 struct got_pathlist_entry *pe;
6359 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6363 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6366 delete_local_mods = 1;
6375 for (i = 0; i < strlen(optarg); i++) {
6376 switch (optarg[i]) {
6377 case GOT_STATUS_MODIFY:
6378 delete_local_mods = 1;
6380 case GOT_STATUS_MISSING:
6383 errx(1, "invalid status code '%c'",
6387 status_codes = optarg;
6399 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6406 cwd = getcwd(NULL, 0);
6408 error = got_error_from_errno("getcwd");
6411 error = got_worktree_open(&worktree, cwd);
6413 if (error->code == GOT_ERR_NOT_WORKTREE)
6414 error = wrap_not_worktree_error(error, "remove", cwd);
6418 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6423 error = apply_unveil(got_repo_get_path(repo), 1,
6424 got_worktree_get_root_path(worktree));
6428 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6435 TAILQ_FOREACH(pe, &paths, entry) {
6436 if (asprintf(&ondisk_path, "%s/%s",
6437 got_worktree_get_root_path(worktree),
6439 error = got_error_from_errno("asprintf");
6442 if (lstat(ondisk_path, &sb) == -1) {
6443 if (errno == ENOENT) {
6447 error = got_error_from_errno2("lstat",
6453 if (S_ISDIR(sb.st_mode)) {
6454 error = got_error_msg(GOT_ERR_BAD_PATH,
6455 "removing directories requires -R option");
6461 error = got_worktree_schedule_delete(worktree, &paths,
6462 delete_local_mods, status_codes, print_remove_status, NULL,
6463 repo, keep_on_disk);
6466 got_repo_close(repo);
6468 got_worktree_close(worktree);
6469 TAILQ_FOREACH(pe, &paths, entry)
6470 free((char *)pe->path);
6471 got_pathlist_free(&paths);
6479 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6480 "path ...\n", getprogname());
6484 static const struct got_error *
6485 revert_progress(void *arg, unsigned char status, const char *path)
6487 if (status == GOT_STATUS_UNVERSIONED)
6490 while (path[0] == '/')
6492 printf("%c %s\n", status, path);
6496 struct choose_patch_arg {
6497 FILE *patch_script_file;
6501 static const struct got_error *
6502 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6503 int nchanges, const char *action)
6506 size_t linesize = 0;
6510 case GOT_STATUS_ADD:
6511 printf("A %s\n%s this addition? [y/n] ", path, action);
6513 case GOT_STATUS_DELETE:
6514 printf("D %s\n%s this deletion? [y/n] ", path, action);
6516 case GOT_STATUS_MODIFY:
6517 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6518 return got_error_from_errno("fseek");
6519 printf(GOT_COMMIT_SEP_STR);
6520 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6522 if (ferror(patch_file))
6523 return got_error_from_errno("getline");
6524 printf(GOT_COMMIT_SEP_STR);
6525 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6526 path, n, nchanges, action);
6529 return got_error_path(path, GOT_ERR_FILE_STATUS);
6535 static const struct got_error *
6536 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6537 FILE *patch_file, int n, int nchanges)
6539 const struct got_error *err = NULL;
6541 size_t linesize = 0;
6544 struct choose_patch_arg *a = arg;
6546 *choice = GOT_PATCH_CHOICE_NONE;
6548 if (a->patch_script_file) {
6550 err = show_change(status, path, patch_file, n, nchanges,
6554 linelen = getline(&line, &linesize, a->patch_script_file);
6555 if (linelen == -1) {
6556 if (ferror(a->patch_script_file))
6557 return got_error_from_errno("getline");
6560 nl = strchr(line, '\n');
6563 if (strcmp(line, "y") == 0) {
6564 *choice = GOT_PATCH_CHOICE_YES;
6566 } else if (strcmp(line, "n") == 0) {
6567 *choice = GOT_PATCH_CHOICE_NO;
6569 } else if (strcmp(line, "q") == 0 &&
6570 status == GOT_STATUS_MODIFY) {
6571 *choice = GOT_PATCH_CHOICE_QUIT;
6574 printf("invalid response '%s'\n", line);
6579 while (resp != 'y' && resp != 'n' && resp != 'q') {
6580 err = show_change(status, path, patch_file, n, nchanges,
6587 if (status == GOT_STATUS_MODIFY) {
6588 if (resp != 'y' && resp != 'n' && resp != 'q') {
6589 printf("invalid response '%c'\n", resp);
6592 } else if (resp != 'y' && resp != 'n') {
6593 printf("invalid response '%c'\n", resp);
6599 *choice = GOT_PATCH_CHOICE_YES;
6600 else if (resp == 'n')
6601 *choice = GOT_PATCH_CHOICE_NO;
6602 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6603 *choice = GOT_PATCH_CHOICE_QUIT;
6609 static const struct got_error *
6610 cmd_revert(int argc, char *argv[])
6612 const struct got_error *error = NULL;
6613 struct got_worktree *worktree = NULL;
6614 struct got_repository *repo = NULL;
6615 char *cwd = NULL, *path = NULL;
6616 struct got_pathlist_head paths;
6617 struct got_pathlist_entry *pe;
6618 int ch, can_recurse = 0, pflag = 0;
6619 FILE *patch_script_file = NULL;
6620 const char *patch_script_path = NULL;
6621 struct choose_patch_arg cpa;
6625 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6631 patch_script_path = optarg;
6646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6647 "unveil", NULL) == -1)
6652 if (patch_script_path && !pflag)
6653 errx(1, "-F option can only be used together with -p option");
6655 cwd = getcwd(NULL, 0);
6657 error = got_error_from_errno("getcwd");
6660 error = got_worktree_open(&worktree, cwd);
6662 if (error->code == GOT_ERR_NOT_WORKTREE)
6663 error = wrap_not_worktree_error(error, "revert", cwd);
6667 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6672 if (patch_script_path) {
6673 patch_script_file = fopen(patch_script_path, "r");
6674 if (patch_script_file == NULL) {
6675 error = got_error_from_errno2("fopen",
6680 error = apply_unveil(got_repo_get_path(repo), 1,
6681 got_worktree_get_root_path(worktree));
6685 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6692 TAILQ_FOREACH(pe, &paths, entry) {
6693 if (asprintf(&ondisk_path, "%s/%s",
6694 got_worktree_get_root_path(worktree),
6696 error = got_error_from_errno("asprintf");
6699 if (lstat(ondisk_path, &sb) == -1) {
6700 if (errno == ENOENT) {
6704 error = got_error_from_errno2("lstat",
6710 if (S_ISDIR(sb.st_mode)) {
6711 error = got_error_msg(GOT_ERR_BAD_PATH,
6712 "reverting directories requires -R option");
6718 cpa.patch_script_file = patch_script_file;
6719 cpa.action = "revert";
6720 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6721 pflag ? choose_patch : NULL, &cpa, repo);
6723 if (patch_script_file && fclose(patch_script_file) == EOF &&
6725 error = got_error_from_errno2("fclose", patch_script_path);
6727 got_repo_close(repo);
6729 got_worktree_close(worktree);
6738 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6743 struct collect_commit_logmsg_arg {
6744 const char *cmdline_log;
6746 const char *worktree_path;
6747 const char *branch_name;
6748 const char *repo_path;
6753 static const struct got_error *
6754 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6757 char *initial_content = NULL;
6758 struct got_pathlist_entry *pe;
6759 const struct got_error *err = NULL;
6760 char *template = NULL;
6761 struct collect_commit_logmsg_arg *a = arg;
6762 int initial_content_len;
6766 /* if a message was specified on the command line, just use it */
6767 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6768 len = strlen(a->cmdline_log) + 1;
6769 *logmsg = malloc(len + 1);
6770 if (*logmsg == NULL)
6771 return got_error_from_errno("malloc");
6772 strlcpy(*logmsg, a->cmdline_log, len);
6776 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6777 return got_error_from_errno("asprintf");
6779 initial_content_len = asprintf(&initial_content,
6780 "\n# changes to be committed on branch %s:\n",
6782 if (initial_content_len == -1)
6783 return got_error_from_errno("asprintf");
6785 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6789 if (write(fd, initial_content, initial_content_len) == -1) {
6790 err = got_error_from_errno2("write", a->logmsg_path);
6794 TAILQ_FOREACH(pe, commitable_paths, entry) {
6795 struct got_commitable *ct = pe->data;
6796 dprintf(fd, "# %c %s\n",
6797 got_commitable_get_status(ct),
6798 got_commitable_get_path(ct));
6801 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6802 initial_content_len);
6804 free(initial_content);
6807 if (fd != -1 && close(fd) == -1 && err == NULL)
6808 err = got_error_from_errno2("close", a->logmsg_path);
6810 /* Editor is done; we can now apply unveil(2) */
6812 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6820 static const struct got_error *
6821 cmd_commit(int argc, char *argv[])
6823 const struct got_error *error = NULL;
6824 struct got_worktree *worktree = NULL;
6825 struct got_repository *repo = NULL;
6826 char *cwd = NULL, *id_str = NULL;
6827 struct got_object_id *id = NULL;
6828 const char *logmsg = NULL;
6829 struct collect_commit_logmsg_arg cl_arg;
6830 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6831 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6832 int allow_bad_symlinks = 0;
6833 struct got_pathlist_head paths;
6836 cl_arg.logmsg_path = NULL;
6838 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6844 allow_bad_symlinks = 1;
6856 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6857 "unveil", NULL) == -1)
6860 cwd = getcwd(NULL, 0);
6862 error = got_error_from_errno("getcwd");
6865 error = got_worktree_open(&worktree, cwd);
6867 if (error->code == GOT_ERR_NOT_WORKTREE)
6868 error = wrap_not_worktree_error(error, "commit", cwd);
6872 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6875 if (rebase_in_progress) {
6876 error = got_error(GOT_ERR_REBASING);
6880 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6885 error = get_gitconfig_path(&gitconfig_path);
6888 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6893 error = get_author(&author, repo, worktree);
6898 * unveil(2) traverses exec(2); if an editor is used we have
6899 * to apply unveil after the log message has been written.
6901 if (logmsg == NULL || strlen(logmsg) == 0)
6902 error = get_editor(&editor);
6904 error = apply_unveil(got_repo_get_path(repo), 0,
6905 got_worktree_get_root_path(worktree));
6909 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6913 cl_arg.editor = editor;
6914 cl_arg.cmdline_log = logmsg;
6915 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6916 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6917 if (!histedit_in_progress) {
6918 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6919 error = got_error(GOT_ERR_COMMIT_BRANCH);
6922 cl_arg.branch_name += 11;
6924 cl_arg.repo_path = got_repo_get_path(repo);
6925 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6926 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6927 print_status, NULL, repo);
6929 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6930 cl_arg.logmsg_path != NULL)
6931 preserve_logmsg = 1;
6935 error = got_object_id_str(&id_str, id);
6938 printf("Created commit %s\n", id_str);
6940 if (preserve_logmsg) {
6941 fprintf(stderr, "%s: log message preserved in %s\n",
6942 getprogname(), cl_arg.logmsg_path);
6943 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6945 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6946 free(cl_arg.logmsg_path);
6948 got_repo_close(repo);
6950 got_worktree_close(worktree);
6953 free(gitconfig_path);
6960 usage_cherrypick(void)
6962 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6966 static const struct got_error *
6967 cmd_cherrypick(int argc, char *argv[])
6969 const struct got_error *error = NULL;
6970 struct got_worktree *worktree = NULL;
6971 struct got_repository *repo = NULL;
6972 char *cwd = NULL, *commit_id_str = NULL;
6973 struct got_object_id *commit_id = NULL;
6974 struct got_commit_object *commit = NULL;
6975 struct got_object_qid *pid;
6976 struct got_reference *head_ref = NULL;
6978 struct got_update_progress_arg upa;
6980 while ((ch = getopt(argc, argv, "")) != -1) {
6992 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6993 "unveil", NULL) == -1)
6999 cwd = getcwd(NULL, 0);
7001 error = got_error_from_errno("getcwd");
7004 error = got_worktree_open(&worktree, cwd);
7006 if (error->code == GOT_ERR_NOT_WORKTREE)
7007 error = wrap_not_worktree_error(error, "cherrypick",
7012 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7017 error = apply_unveil(got_repo_get_path(repo), 0,
7018 got_worktree_get_root_path(worktree));
7022 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7023 GOT_OBJ_TYPE_COMMIT, repo);
7024 if (error != NULL) {
7025 struct got_reference *ref;
7026 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7028 error = got_ref_open(&ref, repo, argv[0], 0);
7031 error = got_ref_resolve(&commit_id, repo, ref);
7036 error = got_object_id_str(&commit_id_str, commit_id);
7040 error = got_ref_open(&head_ref, repo,
7041 got_worktree_get_head_ref_name(worktree), 0);
7045 error = check_same_branch(commit_id, head_ref, NULL, repo);
7047 if (error->code != GOT_ERR_ANCESTRY)
7051 error = got_error(GOT_ERR_SAME_BRANCH);
7055 error = got_object_open_as_commit(&commit, repo, commit_id);
7058 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7059 memset(&upa, 0, sizeof(upa));
7060 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7061 commit_id, repo, update_progress, &upa, check_cancelled,
7066 if (upa.did_something)
7067 printf("Merged commit %s\n", commit_id_str);
7068 print_update_progress_stats(&upa);
7071 got_object_commit_close(commit);
7072 free(commit_id_str);
7074 got_ref_close(head_ref);
7076 got_worktree_close(worktree);
7078 got_repo_close(repo);
7085 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7089 static const struct got_error *
7090 cmd_backout(int argc, char *argv[])
7092 const struct got_error *error = NULL;
7093 struct got_worktree *worktree = NULL;
7094 struct got_repository *repo = NULL;
7095 char *cwd = NULL, *commit_id_str = NULL;
7096 struct got_object_id *commit_id = NULL;
7097 struct got_commit_object *commit = NULL;
7098 struct got_object_qid *pid;
7099 struct got_reference *head_ref = NULL;
7101 struct got_update_progress_arg upa;
7103 while ((ch = getopt(argc, argv, "")) != -1) {
7115 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7116 "unveil", NULL) == -1)
7122 cwd = getcwd(NULL, 0);
7124 error = got_error_from_errno("getcwd");
7127 error = got_worktree_open(&worktree, cwd);
7129 if (error->code == GOT_ERR_NOT_WORKTREE)
7130 error = wrap_not_worktree_error(error, "backout", cwd);
7134 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7139 error = apply_unveil(got_repo_get_path(repo), 0,
7140 got_worktree_get_root_path(worktree));
7144 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7145 GOT_OBJ_TYPE_COMMIT, repo);
7146 if (error != NULL) {
7147 struct got_reference *ref;
7148 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7150 error = got_ref_open(&ref, repo, argv[0], 0);
7153 error = got_ref_resolve(&commit_id, repo, ref);
7158 error = got_object_id_str(&commit_id_str, commit_id);
7162 error = got_ref_open(&head_ref, repo,
7163 got_worktree_get_head_ref_name(worktree), 0);
7167 error = check_same_branch(commit_id, head_ref, NULL, repo);
7171 error = got_object_open_as_commit(&commit, repo, commit_id);
7174 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7176 error = got_error(GOT_ERR_ROOT_COMMIT);
7180 memset(&upa, 0, sizeof(upa));
7181 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7182 update_progress, &upa, check_cancelled, NULL);
7186 if (upa.did_something)
7187 printf("Backed out commit %s\n", commit_id_str);
7188 print_update_progress_stats(&upa);
7191 got_object_commit_close(commit);
7192 free(commit_id_str);
7194 got_ref_close(head_ref);
7196 got_worktree_close(worktree);
7198 got_repo_close(repo);
7205 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7211 trim_logmsg(char *logmsg, int limit)
7216 len = strlen(logmsg);
7220 nl = strchr(logmsg, '\n');
7225 static const struct got_error *
7226 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7228 const struct got_error *err;
7229 char *logmsg0 = NULL;
7232 err = got_object_commit_get_logmsg(&logmsg0, commit);
7237 while (isspace((unsigned char)s[0]))
7240 *logmsg = strdup(s);
7241 if (*logmsg == NULL) {
7242 err = got_error_from_errno("strdup");
7246 trim_logmsg(*logmsg, limit);
7252 static const struct got_error *
7253 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7255 const struct got_error *err;
7256 struct got_commit_object *commit = NULL;
7257 char *id_str = NULL, *logmsg = NULL;
7259 err = got_object_open_as_commit(&commit, repo, id);
7263 err = got_object_id_str(&id_str, id);
7269 err = get_short_logmsg(&logmsg, 42, commit);
7273 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7276 got_object_commit_close(commit);
7281 static const struct got_error *
7282 show_rebase_progress(struct got_commit_object *commit,
7283 struct got_object_id *old_id, struct got_object_id *new_id)
7285 const struct got_error *err;
7286 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7288 err = got_object_id_str(&old_id_str, old_id);
7293 err = got_object_id_str(&new_id_str, new_id);
7298 old_id_str[12] = '\0';
7300 new_id_str[12] = '\0';
7302 err = get_short_logmsg(&logmsg, 42, commit);
7306 printf("%s -> %s: %s\n", old_id_str,
7307 new_id_str ? new_id_str : "no-op change", logmsg);
7315 static const struct got_error *
7316 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7317 struct got_reference *branch, struct got_reference *new_base_branch,
7318 struct got_reference *tmp_branch, struct got_repository *repo)
7320 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7321 return got_worktree_rebase_complete(worktree, fileindex,
7322 new_base_branch, tmp_branch, branch, repo);
7325 static const struct got_error *
7326 rebase_commit(struct got_pathlist_head *merged_paths,
7327 struct got_worktree *worktree, struct got_fileindex *fileindex,
7328 struct got_reference *tmp_branch,
7329 struct got_object_id *commit_id, struct got_repository *repo)
7331 const struct got_error *error;
7332 struct got_commit_object *commit;
7333 struct got_object_id *new_commit_id;
7335 error = got_object_open_as_commit(&commit, repo, commit_id);
7339 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7340 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7342 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7344 error = show_rebase_progress(commit, commit_id, NULL);
7346 error = show_rebase_progress(commit, commit_id, new_commit_id);
7347 free(new_commit_id);
7350 got_object_commit_close(commit);
7354 struct check_path_prefix_arg {
7355 const char *path_prefix;
7360 static const struct got_error *
7361 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7362 struct got_blob_object *blob2, struct got_object_id *id1,
7363 struct got_object_id *id2, const char *path1, const char *path2,
7364 mode_t mode1, mode_t mode2, struct got_repository *repo)
7366 struct check_path_prefix_arg *a = arg;
7368 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7369 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7370 return got_error(a->errcode);
7375 static const struct got_error *
7376 check_path_prefix(struct got_object_id *parent_id,
7377 struct got_object_id *commit_id, const char *path_prefix,
7378 int errcode, struct got_repository *repo)
7380 const struct got_error *err;
7381 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7382 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7383 struct check_path_prefix_arg cpp_arg;
7385 if (got_path_is_root_dir(path_prefix))
7388 err = got_object_open_as_commit(&commit, repo, commit_id);
7392 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7396 err = got_object_open_as_tree(&tree1, repo,
7397 got_object_commit_get_tree_id(parent_commit));
7401 err = got_object_open_as_tree(&tree2, repo,
7402 got_object_commit_get_tree_id(commit));
7406 cpp_arg.path_prefix = path_prefix;
7407 while (cpp_arg.path_prefix[0] == '/')
7408 cpp_arg.path_prefix++;
7409 cpp_arg.len = strlen(cpp_arg.path_prefix);
7410 cpp_arg.errcode = errcode;
7411 err = got_diff_tree(tree1, tree2, "", "", repo,
7412 check_path_prefix_in_diff, &cpp_arg, 0);
7415 got_object_tree_close(tree1);
7417 got_object_tree_close(tree2);
7419 got_object_commit_close(commit);
7421 got_object_commit_close(parent_commit);
7425 static const struct got_error *
7426 collect_commits(struct got_object_id_queue *commits,
7427 struct got_object_id *initial_commit_id,
7428 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7429 const char *path_prefix, int path_prefix_errcode,
7430 struct got_repository *repo)
7432 const struct got_error *err = NULL;
7433 struct got_commit_graph *graph = NULL;
7434 struct got_object_id *parent_id = NULL;
7435 struct got_object_qid *qid;
7436 struct got_object_id *commit_id = initial_commit_id;
7438 err = got_commit_graph_open(&graph, "/", 1);
7442 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7443 check_cancelled, NULL);
7446 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7447 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7448 check_cancelled, NULL);
7450 if (err->code == GOT_ERR_ITER_COMPLETED) {
7451 err = got_error_msg(GOT_ERR_ANCESTRY,
7452 "ran out of commits to rebase before "
7453 "youngest common ancestor commit has "
7458 err = check_path_prefix(parent_id, commit_id,
7459 path_prefix, path_prefix_errcode, repo);
7463 err = got_object_qid_alloc(&qid, commit_id);
7466 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7467 commit_id = parent_id;
7471 got_commit_graph_close(graph);
7475 static const struct got_error *
7476 cmd_rebase(int argc, char *argv[])
7478 const struct got_error *error = NULL;
7479 struct got_worktree *worktree = NULL;
7480 struct got_repository *repo = NULL;
7481 struct got_fileindex *fileindex = NULL;
7483 struct got_reference *branch = NULL;
7484 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7485 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7486 struct got_object_id *resume_commit_id = NULL;
7487 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7488 struct got_commit_object *commit = NULL;
7489 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7490 int histedit_in_progress = 0;
7491 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7492 struct got_object_id_queue commits;
7493 struct got_pathlist_head merged_paths;
7494 const struct got_object_id_queue *parent_ids;
7495 struct got_object_qid *qid, *pid;
7497 SIMPLEQ_INIT(&commits);
7498 TAILQ_INIT(&merged_paths);
7500 while ((ch = getopt(argc, argv, "ac")) != -1) {
7506 continue_rebase = 1;
7518 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7519 "unveil", NULL) == -1)
7522 if (abort_rebase && continue_rebase)
7524 else if (abort_rebase || continue_rebase) {
7527 } else if (argc != 1)
7530 cwd = getcwd(NULL, 0);
7532 error = got_error_from_errno("getcwd");
7535 error = got_worktree_open(&worktree, cwd);
7537 if (error->code == GOT_ERR_NOT_WORKTREE)
7538 error = wrap_not_worktree_error(error, "rebase", cwd);
7542 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7547 error = apply_unveil(got_repo_get_path(repo), 0,
7548 got_worktree_get_root_path(worktree));
7552 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7556 if (histedit_in_progress) {
7557 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7561 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7566 struct got_update_progress_arg upa;
7567 if (!rebase_in_progress) {
7568 error = got_error(GOT_ERR_NOT_REBASING);
7571 error = got_worktree_rebase_continue(&resume_commit_id,
7572 &new_base_branch, &tmp_branch, &branch, &fileindex,
7576 printf("Switching work tree to %s\n",
7577 got_ref_get_symref_target(new_base_branch));
7578 memset(&upa, 0, sizeof(upa));
7579 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7580 new_base_branch, update_progress, &upa);
7583 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7584 print_update_progress_stats(&upa);
7585 goto done; /* nothing else to do */
7588 if (continue_rebase) {
7589 if (!rebase_in_progress) {
7590 error = got_error(GOT_ERR_NOT_REBASING);
7593 error = got_worktree_rebase_continue(&resume_commit_id,
7594 &new_base_branch, &tmp_branch, &branch, &fileindex,
7599 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7600 resume_commit_id, repo);
7604 yca_id = got_object_id_dup(resume_commit_id);
7605 if (yca_id == NULL) {
7606 error = got_error_from_errno("got_object_id_dup");
7610 error = got_ref_open(&branch, repo, argv[0], 0);
7615 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7619 if (!continue_rebase) {
7620 struct got_object_id *base_commit_id;
7622 base_commit_id = got_worktree_get_base_commit_id(worktree);
7623 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7624 base_commit_id, branch_head_commit_id, repo,
7625 check_cancelled, NULL);
7628 if (yca_id == NULL) {
7629 error = got_error_msg(GOT_ERR_ANCESTRY,
7630 "specified branch shares no common ancestry "
7631 "with work tree's branch");
7635 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7637 if (error->code != GOT_ERR_ANCESTRY)
7641 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7642 "specified branch resolves to a commit which "
7643 "is already contained in work tree's branch");
7646 error = got_worktree_rebase_prepare(&new_base_branch,
7647 &tmp_branch, &fileindex, worktree, branch, repo);
7652 commit_id = branch_head_commit_id;
7653 error = got_object_open_as_commit(&commit, repo, commit_id);
7657 parent_ids = got_object_commit_get_parent_ids(commit);
7658 pid = SIMPLEQ_FIRST(parent_ids);
7660 if (!continue_rebase) {
7661 struct got_update_progress_arg upa;
7662 memset(&upa, 0, sizeof(upa));
7663 error = got_worktree_rebase_abort(worktree, fileindex,
7664 repo, new_base_branch, update_progress, &upa);
7667 printf("Rebase of %s aborted\n",
7668 got_ref_get_name(branch));
7669 print_update_progress_stats(&upa);
7672 error = got_error(GOT_ERR_EMPTY_REBASE);
7675 error = collect_commits(&commits, commit_id, pid->id,
7676 yca_id, got_worktree_get_path_prefix(worktree),
7677 GOT_ERR_REBASE_PATH, repo);
7678 got_object_commit_close(commit);
7683 if (SIMPLEQ_EMPTY(&commits)) {
7684 if (continue_rebase) {
7685 error = rebase_complete(worktree, fileindex,
7686 branch, new_base_branch, tmp_branch, repo);
7689 /* Fast-forward the reference of the branch. */
7690 struct got_object_id *new_head_commit_id;
7692 error = got_ref_resolve(&new_head_commit_id, repo,
7696 error = got_object_id_str(&id_str, new_head_commit_id);
7697 printf("Forwarding %s to commit %s\n",
7698 got_ref_get_name(branch), id_str);
7700 error = got_ref_change_ref(branch,
7701 new_head_commit_id);
7708 SIMPLEQ_FOREACH(qid, &commits, entry) {
7709 struct got_update_progress_arg upa;
7711 commit_id = qid->id;
7712 parent_id = pid ? pid->id : yca_id;
7715 memset(&upa, 0, sizeof(upa));
7716 error = got_worktree_rebase_merge_files(&merged_paths,
7717 worktree, fileindex, parent_id, commit_id, repo,
7718 update_progress, &upa, check_cancelled, NULL);
7722 print_update_progress_stats(&upa);
7723 if (upa.conflicts > 0)
7724 rebase_status = GOT_STATUS_CONFLICT;
7726 if (rebase_status == GOT_STATUS_CONFLICT) {
7727 error = show_rebase_merge_conflict(qid->id, repo);
7730 got_worktree_rebase_pathlist_free(&merged_paths);
7734 error = rebase_commit(&merged_paths, worktree, fileindex,
7735 tmp_branch, commit_id, repo);
7736 got_worktree_rebase_pathlist_free(&merged_paths);
7741 if (rebase_status == GOT_STATUS_CONFLICT) {
7742 error = got_worktree_rebase_postpone(worktree, fileindex);
7745 error = got_error_msg(GOT_ERR_CONFLICTS,
7746 "conflicts must be resolved before rebasing can continue");
7748 error = rebase_complete(worktree, fileindex, branch,
7749 new_base_branch, tmp_branch, repo);
7751 got_object_id_queue_free(&commits);
7752 free(branch_head_commit_id);
7753 free(resume_commit_id);
7756 got_object_commit_close(commit);
7758 got_ref_close(branch);
7759 if (new_base_branch)
7760 got_ref_close(new_base_branch);
7762 got_ref_close(tmp_branch);
7764 got_worktree_close(worktree);
7766 got_repo_close(repo);
7771 usage_histedit(void)
7773 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7778 #define GOT_HISTEDIT_PICK 'p'
7779 #define GOT_HISTEDIT_EDIT 'e'
7780 #define GOT_HISTEDIT_FOLD 'f'
7781 #define GOT_HISTEDIT_DROP 'd'
7782 #define GOT_HISTEDIT_MESG 'm'
7784 static struct got_histedit_cmd {
7788 } got_histedit_cmds[] = {
7789 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7790 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7791 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7793 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7794 { GOT_HISTEDIT_MESG, "mesg",
7795 "single-line log message for commit above (open editor if empty)" },
7798 struct got_histedit_list_entry {
7799 TAILQ_ENTRY(got_histedit_list_entry) entry;
7800 struct got_object_id *commit_id;
7801 const struct got_histedit_cmd *cmd;
7804 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7806 static const struct got_error *
7807 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7808 FILE *f, struct got_repository *repo)
7810 const struct got_error *err = NULL;
7811 char *logmsg = NULL, *id_str = NULL;
7812 struct got_commit_object *commit = NULL;
7815 err = got_object_open_as_commit(&commit, repo, commit_id);
7819 err = get_short_logmsg(&logmsg, 34, commit);
7823 err = got_object_id_str(&id_str, commit_id);
7827 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7829 err = got_ferror(f, GOT_ERR_IO);
7832 got_object_commit_close(commit);
7838 static const struct got_error *
7839 histedit_write_commit_list(struct got_object_id_queue *commits,
7840 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7842 const struct got_error *err = NULL;
7843 struct got_object_qid *qid;
7845 if (SIMPLEQ_EMPTY(commits))
7846 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7848 SIMPLEQ_FOREACH(qid, commits, entry) {
7849 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7853 if (edit_logmsg_only) {
7854 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7856 err = got_ferror(f, GOT_ERR_IO);
7865 static const struct got_error *
7866 write_cmd_list(FILE *f, const char *branch_name,
7867 struct got_object_id_queue *commits)
7869 const struct got_error *err = NULL;
7872 struct got_object_qid *qid;
7874 qid = SIMPLEQ_FIRST(commits);
7875 err = got_object_id_str(&id_str, qid->id);
7880 "# Editing the history of branch '%s' starting at\n"
7882 "# Commits will be processed in order from top to "
7883 "bottom of this file.\n", branch_name, id_str);
7885 err = got_ferror(f, GOT_ERR_IO);
7889 n = fprintf(f, "# Available histedit commands:\n");
7891 err = got_ferror(f, GOT_ERR_IO);
7895 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7896 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7897 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7900 err = got_ferror(f, GOT_ERR_IO);
7909 static const struct got_error *
7910 histedit_syntax_error(int lineno)
7912 static char msg[42];
7915 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7917 if (ret == -1 || ret >= sizeof(msg))
7918 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7920 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7923 static const struct got_error *
7924 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7925 char *logmsg, struct got_repository *repo)
7927 const struct got_error *err;
7928 struct got_commit_object *folded_commit = NULL;
7929 char *id_str, *folded_logmsg = NULL;
7931 err = got_object_id_str(&id_str, hle->commit_id);
7935 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7939 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7942 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7943 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7944 folded_logmsg) == -1) {
7945 err = got_error_from_errno("asprintf");
7949 got_object_commit_close(folded_commit);
7951 free(folded_logmsg);
7955 static struct got_histedit_list_entry *
7956 get_folded_commits(struct got_histedit_list_entry *hle)
7958 struct got_histedit_list_entry *prev, *folded = NULL;
7960 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7961 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7962 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7963 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7965 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7971 static const struct got_error *
7972 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7973 struct got_repository *repo)
7975 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7976 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7977 const struct got_error *err = NULL;
7978 struct got_commit_object *commit = NULL;
7981 struct got_histedit_list_entry *folded = NULL;
7983 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7987 folded = get_folded_commits(hle);
7989 while (folded != hle) {
7990 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7991 folded = TAILQ_NEXT(folded, entry);
7994 err = append_folded_commit_msg(&new_msg, folded,
8000 folded = TAILQ_NEXT(folded, entry);
8004 err = got_object_id_str(&id_str, hle->commit_id);
8007 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8010 logmsg_len = asprintf(&new_msg,
8011 "%s\n# original log message of commit %s: %s",
8012 logmsg ? logmsg : "", id_str, orig_logmsg);
8013 if (logmsg_len == -1) {
8014 err = got_error_from_errno("asprintf");
8020 err = got_object_id_str(&id_str, hle->commit_id);
8024 err = got_opentemp_named_fd(&logmsg_path, &fd,
8025 GOT_TMPDIR_STR "/got-logmsg");
8029 write(fd, logmsg, logmsg_len);
8032 err = get_editor(&editor);
8036 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8039 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8041 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
8044 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8045 err = got_error_from_errno2("unlink", logmsg_path);
8051 got_object_commit_close(commit);
8055 static const struct got_error *
8056 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8057 FILE *f, struct got_repository *repo)
8059 const struct got_error *err = NULL;
8060 char *line = NULL, *p, *end;
8064 const struct got_histedit_cmd *cmd;
8065 struct got_object_id *commit_id = NULL;
8066 struct got_histedit_list_entry *hle = NULL;
8069 len = getline(&line, &size, f);
8071 const struct got_error *getline_err;
8074 getline_err = got_error_from_errno("getline");
8075 err = got_ferror(f, getline_err->code);
8080 while (isspace((unsigned char)p[0]))
8082 if (p[0] == '#' || p[0] == '\0') {
8088 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8089 cmd = &got_histedit_cmds[i];
8090 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8091 isspace((unsigned char)p[strlen(cmd->name)])) {
8092 p += strlen(cmd->name);
8095 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8100 if (i == nitems(got_histedit_cmds)) {
8101 err = histedit_syntax_error(lineno);
8104 while (isspace((unsigned char)p[0]))
8106 if (cmd->code == GOT_HISTEDIT_MESG) {
8107 if (hle == NULL || hle->logmsg != NULL) {
8108 err = got_error(GOT_ERR_HISTEDIT_CMD);
8112 err = histedit_edit_logmsg(hle, repo);
8116 hle->logmsg = strdup(p);
8117 if (hle->logmsg == NULL) {
8118 err = got_error_from_errno("strdup");
8127 while (end[0] && !isspace((unsigned char)end[0]))
8131 err = got_object_resolve_id_str(&commit_id, repo, p);
8133 /* override error code */
8134 err = histedit_syntax_error(lineno);
8138 hle = malloc(sizeof(*hle));
8140 err = got_error_from_errno("malloc");
8144 hle->commit_id = commit_id;
8149 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8157 static const struct got_error *
8158 histedit_check_script(struct got_histedit_list *histedit_cmds,
8159 struct got_object_id_queue *commits, struct got_repository *repo)
8161 const struct got_error *err = NULL;
8162 struct got_object_qid *qid;
8163 struct got_histedit_list_entry *hle;
8164 static char msg[92];
8167 if (TAILQ_EMPTY(histedit_cmds))
8168 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8169 "histedit script contains no commands");
8170 if (SIMPLEQ_EMPTY(commits))
8171 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8173 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8174 struct got_histedit_list_entry *hle2;
8175 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8178 if (got_object_id_cmp(hle->commit_id,
8179 hle2->commit_id) != 0)
8181 err = got_object_id_str(&id_str, hle->commit_id);
8184 snprintf(msg, sizeof(msg), "commit %s is listed "
8185 "more than once in histedit script", id_str);
8187 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8191 SIMPLEQ_FOREACH(qid, commits, entry) {
8192 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8193 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8197 err = got_object_id_str(&id_str, qid->id);
8200 snprintf(msg, sizeof(msg),
8201 "commit %s missing from histedit script", id_str);
8203 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8207 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8208 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8209 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8210 "last commit in histedit script cannot be folded");
8215 static const struct got_error *
8216 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8217 const char *path, struct got_object_id_queue *commits,
8218 struct got_repository *repo)
8220 const struct got_error *err = NULL;
8224 err = get_editor(&editor);
8228 if (spawn_editor(editor, path) == -1) {
8229 err = got_error_from_errno("failed spawning editor");
8233 f = fopen(path, "r");
8235 err = got_error_from_errno("fopen");
8238 err = histedit_parse_list(histedit_cmds, f, repo);
8242 err = histedit_check_script(histedit_cmds, commits, repo);
8244 if (f && fclose(f) != 0 && err == NULL)
8245 err = got_error_from_errno("fclose");
8250 static const struct got_error *
8251 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8252 struct got_object_id_queue *, const char *, const char *,
8253 struct got_repository *);
8255 static const struct got_error *
8256 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8257 struct got_object_id_queue *commits, const char *branch_name,
8258 int edit_logmsg_only, struct got_repository *repo)
8260 const struct got_error *err;
8264 err = got_opentemp_named(&path, &f, "got-histedit");
8268 err = write_cmd_list(f, branch_name, commits);
8272 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8276 if (edit_logmsg_only) {
8278 err = histedit_parse_list(histedit_cmds, f, repo);
8280 if (fclose(f) != 0) {
8281 err = got_error_from_errno("fclose");
8285 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8287 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8288 err->code != GOT_ERR_HISTEDIT_CMD)
8290 err = histedit_edit_list_retry(histedit_cmds, err,
8291 commits, path, branch_name, repo);
8295 if (f && fclose(f) != 0 && err == NULL)
8296 err = got_error_from_errno("fclose");
8297 if (path && unlink(path) != 0 && err == NULL)
8298 err = got_error_from_errno2("unlink", path);
8303 static const struct got_error *
8304 histedit_save_list(struct got_histedit_list *histedit_cmds,
8305 struct got_worktree *worktree, struct got_repository *repo)
8307 const struct got_error *err = NULL;
8310 struct got_histedit_list_entry *hle;
8311 struct got_commit_object *commit = NULL;
8313 err = got_worktree_get_histedit_script_path(&path, worktree);
8317 f = fopen(path, "w");
8319 err = got_error_from_errno2("fopen", path);
8322 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8323 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8329 int n = fprintf(f, "%c %s\n",
8330 GOT_HISTEDIT_MESG, hle->logmsg);
8332 err = got_ferror(f, GOT_ERR_IO);
8338 if (f && fclose(f) != 0 && err == NULL)
8339 err = got_error_from_errno("fclose");
8342 got_object_commit_close(commit);
8347 histedit_free_list(struct got_histedit_list *histedit_cmds)
8349 struct got_histedit_list_entry *hle;
8351 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8352 TAILQ_REMOVE(histedit_cmds, hle, entry);
8357 static const struct got_error *
8358 histedit_load_list(struct got_histedit_list *histedit_cmds,
8359 const char *path, struct got_repository *repo)
8361 const struct got_error *err = NULL;
8364 f = fopen(path, "r");
8366 err = got_error_from_errno2("fopen", path);
8370 err = histedit_parse_list(histedit_cmds, f, repo);
8372 if (f && fclose(f) != 0 && err == NULL)
8373 err = got_error_from_errno("fclose");
8377 static const struct got_error *
8378 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8379 const struct got_error *edit_err, struct got_object_id_queue *commits,
8380 const char *path, const char *branch_name, struct got_repository *repo)
8382 const struct got_error *err = NULL, *prev_err = edit_err;
8385 while (resp != 'c' && resp != 'r' && resp != 'a') {
8386 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8387 "or (a)bort: ", getprogname(), prev_err->msg);
8392 histedit_free_list(histedit_cmds);
8393 err = histedit_run_editor(histedit_cmds, path, commits,
8396 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8397 err->code != GOT_ERR_HISTEDIT_CMD)
8404 } else if (resp == 'r') {
8405 histedit_free_list(histedit_cmds);
8406 err = histedit_edit_script(histedit_cmds,
8407 commits, branch_name, 0, repo);
8409 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8410 err->code != GOT_ERR_HISTEDIT_CMD)
8417 } else if (resp == 'a') {
8418 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8421 printf("invalid response '%c'\n", resp);
8427 static const struct got_error *
8428 histedit_complete(struct got_worktree *worktree,
8429 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8430 struct got_reference *branch, struct got_repository *repo)
8432 printf("Switching work tree to %s\n",
8433 got_ref_get_symref_target(branch));
8434 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8438 static const struct got_error *
8439 show_histedit_progress(struct got_commit_object *commit,
8440 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8442 const struct got_error *err;
8443 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8445 err = got_object_id_str(&old_id_str, hle->commit_id);
8450 err = got_object_id_str(&new_id_str, new_id);
8455 old_id_str[12] = '\0';
8457 new_id_str[12] = '\0';
8460 logmsg = strdup(hle->logmsg);
8461 if (logmsg == NULL) {
8462 err = got_error_from_errno("strdup");
8465 trim_logmsg(logmsg, 42);
8467 err = get_short_logmsg(&logmsg, 42, commit);
8472 switch (hle->cmd->code) {
8473 case GOT_HISTEDIT_PICK:
8474 case GOT_HISTEDIT_EDIT:
8475 printf("%s -> %s: %s\n", old_id_str,
8476 new_id_str ? new_id_str : "no-op change", logmsg);
8478 case GOT_HISTEDIT_DROP:
8479 case GOT_HISTEDIT_FOLD:
8480 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8492 static const struct got_error *
8493 histedit_commit(struct got_pathlist_head *merged_paths,
8494 struct got_worktree *worktree, struct got_fileindex *fileindex,
8495 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8496 struct got_repository *repo)
8498 const struct got_error *err;
8499 struct got_commit_object *commit;
8500 struct got_object_id *new_commit_id;
8502 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8503 && hle->logmsg == NULL) {
8504 err = histedit_edit_logmsg(hle, repo);
8509 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8513 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8514 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8517 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8519 err = show_histedit_progress(commit, hle, NULL);
8521 err = show_histedit_progress(commit, hle, new_commit_id);
8522 free(new_commit_id);
8525 got_object_commit_close(commit);
8529 static const struct got_error *
8530 histedit_skip_commit(struct got_histedit_list_entry *hle,
8531 struct got_worktree *worktree, struct got_repository *repo)
8533 const struct got_error *error;
8534 struct got_commit_object *commit;
8536 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8541 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8545 error = show_histedit_progress(commit, hle, NULL);
8546 got_object_commit_close(commit);
8550 static const struct got_error *
8551 check_local_changes(void *arg, unsigned char status,
8552 unsigned char staged_status, const char *path,
8553 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8554 struct got_object_id *commit_id, int dirfd, const char *de_name)
8556 int *have_local_changes = arg;
8559 case GOT_STATUS_ADD:
8560 case GOT_STATUS_DELETE:
8561 case GOT_STATUS_MODIFY:
8562 case GOT_STATUS_CONFLICT:
8563 *have_local_changes = 1;
8564 return got_error(GOT_ERR_CANCELLED);
8569 switch (staged_status) {
8570 case GOT_STATUS_ADD:
8571 case GOT_STATUS_DELETE:
8572 case GOT_STATUS_MODIFY:
8573 *have_local_changes = 1;
8574 return got_error(GOT_ERR_CANCELLED);
8582 static const struct got_error *
8583 cmd_histedit(int argc, char *argv[])
8585 const struct got_error *error = NULL;
8586 struct got_worktree *worktree = NULL;
8587 struct got_fileindex *fileindex = NULL;
8588 struct got_repository *repo = NULL;
8590 struct got_reference *branch = NULL;
8591 struct got_reference *tmp_branch = NULL;
8592 struct got_object_id *resume_commit_id = NULL;
8593 struct got_object_id *base_commit_id = NULL;
8594 struct got_object_id *head_commit_id = NULL;
8595 struct got_commit_object *commit = NULL;
8596 int ch, rebase_in_progress = 0;
8597 struct got_update_progress_arg upa;
8598 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8599 int edit_logmsg_only = 0;
8600 const char *edit_script_path = NULL;
8601 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8602 struct got_object_id_queue commits;
8603 struct got_pathlist_head merged_paths;
8604 const struct got_object_id_queue *parent_ids;
8605 struct got_object_qid *pid;
8606 struct got_histedit_list histedit_cmds;
8607 struct got_histedit_list_entry *hle;
8609 SIMPLEQ_INIT(&commits);
8610 TAILQ_INIT(&histedit_cmds);
8611 TAILQ_INIT(&merged_paths);
8612 memset(&upa, 0, sizeof(upa));
8614 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8623 edit_script_path = optarg;
8626 edit_logmsg_only = 1;
8638 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8639 "unveil", NULL) == -1)
8642 if (abort_edit && continue_edit)
8643 errx(1, "histedit's -a and -c options are mutually exclusive");
8644 if (edit_script_path && edit_logmsg_only)
8645 errx(1, "histedit's -F and -m options are mutually exclusive");
8646 if (abort_edit && edit_logmsg_only)
8647 errx(1, "histedit's -a and -m options are mutually exclusive");
8648 if (continue_edit && edit_logmsg_only)
8649 errx(1, "histedit's -c and -m options are mutually exclusive");
8654 * This command cannot apply unveil(2) in all cases because the
8655 * user may choose to run an editor to edit the histedit script
8656 * and to edit individual commit log messages.
8657 * unveil(2) traverses exec(2); if an editor is used we have to
8658 * apply unveil after edit script and log messages have been written.
8659 * XXX TODO: Make use of unveil(2) where possible.
8662 cwd = getcwd(NULL, 0);
8664 error = got_error_from_errno("getcwd");
8667 error = got_worktree_open(&worktree, cwd);
8669 if (error->code == GOT_ERR_NOT_WORKTREE)
8670 error = wrap_not_worktree_error(error, "histedit", cwd);
8674 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8679 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8682 if (rebase_in_progress) {
8683 error = got_error(GOT_ERR_REBASING);
8687 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8691 if (edit_in_progress && edit_logmsg_only) {
8692 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8693 "histedit operation is in progress in this "
8694 "work tree and must be continued or aborted "
8695 "before the -m option can be used");
8699 if (edit_in_progress && abort_edit) {
8700 error = got_worktree_histedit_continue(&resume_commit_id,
8701 &tmp_branch, &branch, &base_commit_id, &fileindex,
8705 printf("Switching work tree to %s\n",
8706 got_ref_get_symref_target(branch));
8707 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8708 branch, base_commit_id, update_progress, &upa);
8711 printf("Histedit of %s aborted\n",
8712 got_ref_get_symref_target(branch));
8713 print_update_progress_stats(&upa);
8714 goto done; /* nothing else to do */
8715 } else if (abort_edit) {
8716 error = got_error(GOT_ERR_NOT_HISTEDIT);
8720 if (continue_edit) {
8723 if (!edit_in_progress) {
8724 error = got_error(GOT_ERR_NOT_HISTEDIT);
8728 error = got_worktree_get_histedit_script_path(&path, worktree);
8732 error = histedit_load_list(&histedit_cmds, path, repo);
8737 error = got_worktree_histedit_continue(&resume_commit_id,
8738 &tmp_branch, &branch, &base_commit_id, &fileindex,
8743 error = got_ref_resolve(&head_commit_id, repo, branch);
8747 error = got_object_open_as_commit(&commit, repo,
8751 parent_ids = got_object_commit_get_parent_ids(commit);
8752 pid = SIMPLEQ_FIRST(parent_ids);
8754 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8757 error = collect_commits(&commits, head_commit_id, pid->id,
8758 base_commit_id, got_worktree_get_path_prefix(worktree),
8759 GOT_ERR_HISTEDIT_PATH, repo);
8760 got_object_commit_close(commit);
8765 if (edit_in_progress) {
8766 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8770 error = got_ref_open(&branch, repo,
8771 got_worktree_get_head_ref_name(worktree), 0);
8775 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8776 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8777 "will not edit commit history of a branch outside "
8778 "the \"refs/heads/\" reference namespace");
8782 error = got_ref_resolve(&head_commit_id, repo, branch);
8783 got_ref_close(branch);
8788 error = got_object_open_as_commit(&commit, repo,
8792 parent_ids = got_object_commit_get_parent_ids(commit);
8793 pid = SIMPLEQ_FIRST(parent_ids);
8795 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8798 error = collect_commits(&commits, head_commit_id, pid->id,
8799 got_worktree_get_base_commit_id(worktree),
8800 got_worktree_get_path_prefix(worktree),
8801 GOT_ERR_HISTEDIT_PATH, repo);
8802 got_object_commit_close(commit);
8807 if (SIMPLEQ_EMPTY(&commits)) {
8808 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8812 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8813 &base_commit_id, &fileindex, worktree, repo);
8817 if (edit_script_path) {
8818 error = histedit_load_list(&histedit_cmds,
8819 edit_script_path, repo);
8821 got_worktree_histedit_abort(worktree, fileindex,
8822 repo, branch, base_commit_id,
8823 update_progress, &upa);
8824 print_update_progress_stats(&upa);
8828 const char *branch_name;
8829 branch_name = got_ref_get_symref_target(branch);
8830 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8832 error = histedit_edit_script(&histedit_cmds, &commits,
8833 branch_name, edit_logmsg_only, repo);
8835 got_worktree_histedit_abort(worktree, fileindex,
8836 repo, branch, base_commit_id,
8837 update_progress, &upa);
8838 print_update_progress_stats(&upa);
8844 error = histedit_save_list(&histedit_cmds, worktree,
8847 got_worktree_histedit_abort(worktree, fileindex,
8848 repo, branch, base_commit_id,
8849 update_progress, &upa);
8850 print_update_progress_stats(&upa);
8856 error = histedit_check_script(&histedit_cmds, &commits, repo);
8860 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8861 if (resume_commit_id) {
8862 if (got_object_id_cmp(hle->commit_id,
8863 resume_commit_id) != 0)
8866 resume_commit_id = NULL;
8867 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8868 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8869 error = histedit_skip_commit(hle, worktree,
8874 struct got_pathlist_head paths;
8875 int have_changes = 0;
8878 error = got_pathlist_append(&paths, "", NULL);
8881 error = got_worktree_status(worktree, &paths,
8882 repo, check_local_changes, &have_changes,
8883 check_cancelled, NULL);
8884 got_pathlist_free(&paths);
8886 if (error->code != GOT_ERR_CANCELLED)
8888 if (sigint_received || sigpipe_received)
8892 error = histedit_commit(NULL, worktree,
8893 fileindex, tmp_branch, hle, repo);
8897 error = got_object_open_as_commit(
8898 &commit, repo, hle->commit_id);
8901 error = show_histedit_progress(commit,
8903 got_object_commit_close(commit);
8912 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8913 error = histedit_skip_commit(hle, worktree, repo);
8919 error = got_object_open_as_commit(&commit, repo,
8923 parent_ids = got_object_commit_get_parent_ids(commit);
8924 pid = SIMPLEQ_FIRST(parent_ids);
8926 error = got_worktree_histedit_merge_files(&merged_paths,
8927 worktree, fileindex, pid->id, hle->commit_id, repo,
8928 update_progress, &upa, check_cancelled, NULL);
8931 got_object_commit_close(commit);
8934 print_update_progress_stats(&upa);
8935 if (upa.conflicts > 0)
8936 rebase_status = GOT_STATUS_CONFLICT;
8938 if (rebase_status == GOT_STATUS_CONFLICT) {
8939 error = show_rebase_merge_conflict(hle->commit_id,
8943 got_worktree_rebase_pathlist_free(&merged_paths);
8947 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8949 error = got_object_id_str(&id_str, hle->commit_id);
8952 printf("Stopping histedit for amending commit %s\n",
8955 got_worktree_rebase_pathlist_free(&merged_paths);
8956 error = got_worktree_histedit_postpone(worktree,
8961 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8962 error = histedit_skip_commit(hle, worktree, repo);
8968 error = histedit_commit(&merged_paths, worktree, fileindex,
8969 tmp_branch, hle, repo);
8970 got_worktree_rebase_pathlist_free(&merged_paths);
8975 if (rebase_status == GOT_STATUS_CONFLICT) {
8976 error = got_worktree_histedit_postpone(worktree, fileindex);
8979 error = got_error_msg(GOT_ERR_CONFLICTS,
8980 "conflicts must be resolved before histedit can continue");
8982 error = histedit_complete(worktree, fileindex, tmp_branch,
8985 got_object_id_queue_free(&commits);
8986 histedit_free_list(&histedit_cmds);
8987 free(head_commit_id);
8988 free(base_commit_id);
8989 free(resume_commit_id);
8991 got_object_commit_close(commit);
8993 got_ref_close(branch);
8995 got_ref_close(tmp_branch);
8997 got_worktree_close(worktree);
8999 got_repo_close(repo);
9004 usage_integrate(void)
9006 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9010 static const struct got_error *
9011 cmd_integrate(int argc, char *argv[])
9013 const struct got_error *error = NULL;
9014 struct got_repository *repo = NULL;
9015 struct got_worktree *worktree = NULL;
9016 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9017 const char *branch_arg = NULL;
9018 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9019 struct got_fileindex *fileindex = NULL;
9020 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9022 struct got_update_progress_arg upa;
9024 while ((ch = getopt(argc, argv, "")) != -1) {
9037 branch_arg = argv[0];
9039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9040 "unveil", NULL) == -1)
9043 cwd = getcwd(NULL, 0);
9045 error = got_error_from_errno("getcwd");
9049 error = got_worktree_open(&worktree, cwd);
9051 if (error->code == GOT_ERR_NOT_WORKTREE)
9052 error = wrap_not_worktree_error(error, "integrate",
9057 error = check_rebase_or_histedit_in_progress(worktree);
9061 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9066 error = apply_unveil(got_repo_get_path(repo), 0,
9067 got_worktree_get_root_path(worktree));
9071 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9072 error = got_error_from_errno("asprintf");
9076 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9077 &base_branch_ref, worktree, refname, repo);
9081 refname = strdup(got_ref_get_name(branch_ref));
9082 if (refname == NULL) {
9083 error = got_error_from_errno("strdup");
9084 got_worktree_integrate_abort(worktree, fileindex, repo,
9085 branch_ref, base_branch_ref);
9088 base_refname = strdup(got_ref_get_name(base_branch_ref));
9089 if (base_refname == NULL) {
9090 error = got_error_from_errno("strdup");
9091 got_worktree_integrate_abort(worktree, fileindex, repo,
9092 branch_ref, base_branch_ref);
9096 error = got_ref_resolve(&commit_id, repo, branch_ref);
9100 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9104 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9105 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9106 "specified branch has already been integrated");
9107 got_worktree_integrate_abort(worktree, fileindex, repo,
9108 branch_ref, base_branch_ref);
9112 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9114 if (error->code == GOT_ERR_ANCESTRY)
9115 error = got_error(GOT_ERR_REBASE_REQUIRED);
9116 got_worktree_integrate_abort(worktree, fileindex, repo,
9117 branch_ref, base_branch_ref);
9121 memset(&upa, 0, sizeof(upa));
9122 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9123 branch_ref, base_branch_ref, update_progress, &upa,
9124 check_cancelled, NULL);
9128 printf("Integrated %s into %s\n", refname, base_refname);
9129 print_update_progress_stats(&upa);
9132 got_repo_close(repo);
9134 got_worktree_close(worktree);
9136 free(base_commit_id);
9146 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9147 "[-S] [file-path ...]\n",
9152 static const struct got_error *
9153 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9154 const char *path, struct got_object_id *blob_id,
9155 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9156 int dirfd, const char *de_name)
9158 const struct got_error *err = NULL;
9159 char *id_str = NULL;
9161 if (staged_status != GOT_STATUS_ADD &&
9162 staged_status != GOT_STATUS_MODIFY &&
9163 staged_status != GOT_STATUS_DELETE)
9166 if (staged_status == GOT_STATUS_ADD ||
9167 staged_status == GOT_STATUS_MODIFY)
9168 err = got_object_id_str(&id_str, staged_blob_id);
9170 err = got_object_id_str(&id_str, blob_id);
9174 printf("%s %c %s\n", id_str, staged_status, path);
9179 static const struct got_error *
9180 cmd_stage(int argc, char *argv[])
9182 const struct got_error *error = NULL;
9183 struct got_repository *repo = NULL;
9184 struct got_worktree *worktree = NULL;
9186 struct got_pathlist_head paths;
9187 struct got_pathlist_entry *pe;
9188 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9189 FILE *patch_script_file = NULL;
9190 const char *patch_script_path = NULL;
9191 struct choose_patch_arg cpa;
9195 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9204 patch_script_path = optarg;
9207 allow_bad_symlinks = 1;
9219 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9220 "unveil", NULL) == -1)
9223 if (list_stage && (pflag || patch_script_path))
9224 errx(1, "-l option cannot be used with other options");
9225 if (patch_script_path && !pflag)
9226 errx(1, "-F option can only be used together with -p option");
9228 cwd = getcwd(NULL, 0);
9230 error = got_error_from_errno("getcwd");
9234 error = got_worktree_open(&worktree, cwd);
9236 if (error->code == GOT_ERR_NOT_WORKTREE)
9237 error = wrap_not_worktree_error(error, "stage", cwd);
9241 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9246 if (patch_script_path) {
9247 patch_script_file = fopen(patch_script_path, "r");
9248 if (patch_script_file == NULL) {
9249 error = got_error_from_errno2("fopen",
9254 error = apply_unveil(got_repo_get_path(repo), 0,
9255 got_worktree_get_root_path(worktree));
9259 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9264 error = got_worktree_status(worktree, &paths, repo,
9265 print_stage, NULL, check_cancelled, NULL);
9267 cpa.patch_script_file = patch_script_file;
9268 cpa.action = "stage";
9269 error = got_worktree_stage(worktree, &paths,
9270 pflag ? NULL : print_status, NULL,
9271 pflag ? choose_patch : NULL, &cpa,
9272 allow_bad_symlinks, repo);
9275 if (patch_script_file && fclose(patch_script_file) == EOF &&
9277 error = got_error_from_errno2("fclose", patch_script_path);
9279 got_repo_close(repo);
9281 got_worktree_close(worktree);
9282 TAILQ_FOREACH(pe, &paths, entry)
9283 free((char *)pe->path);
9284 got_pathlist_free(&paths);
9292 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9293 "[file-path ...]\n",
9299 static const struct got_error *
9300 cmd_unstage(int argc, char *argv[])
9302 const struct got_error *error = NULL;
9303 struct got_repository *repo = NULL;
9304 struct got_worktree *worktree = NULL;
9306 struct got_pathlist_head paths;
9307 struct got_pathlist_entry *pe;
9309 struct got_update_progress_arg upa;
9310 FILE *patch_script_file = NULL;
9311 const char *patch_script_path = NULL;
9312 struct choose_patch_arg cpa;
9316 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9322 patch_script_path = optarg;
9334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9335 "unveil", NULL) == -1)
9338 if (patch_script_path && !pflag)
9339 errx(1, "-F option can only be used together with -p option");
9341 cwd = getcwd(NULL, 0);
9343 error = got_error_from_errno("getcwd");
9347 error = got_worktree_open(&worktree, cwd);
9349 if (error->code == GOT_ERR_NOT_WORKTREE)
9350 error = wrap_not_worktree_error(error, "unstage", cwd);
9354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9359 if (patch_script_path) {
9360 patch_script_file = fopen(patch_script_path, "r");
9361 if (patch_script_file == NULL) {
9362 error = got_error_from_errno2("fopen",
9368 error = apply_unveil(got_repo_get_path(repo), 0,
9369 got_worktree_get_root_path(worktree));
9373 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9377 cpa.patch_script_file = patch_script_file;
9378 cpa.action = "unstage";
9379 memset(&upa, 0, sizeof(upa));
9380 error = got_worktree_unstage(worktree, &paths, update_progress,
9381 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9383 print_update_progress_stats(&upa);
9385 if (patch_script_file && fclose(patch_script_file) == EOF &&
9387 error = got_error_from_errno2("fclose", patch_script_path);
9389 got_repo_close(repo);
9391 got_worktree_close(worktree);
9392 TAILQ_FOREACH(pe, &paths, entry)
9393 free((char *)pe->path);
9394 got_pathlist_free(&paths);
9402 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9403 "arg1 [arg2 ...]\n", getprogname());
9407 static const struct got_error *
9408 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9410 const struct got_error *err;
9411 struct got_blob_object *blob;
9413 err = got_object_open_as_blob(&blob, repo, id, 8192);
9417 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9418 got_object_blob_close(blob);
9422 static const struct got_error *
9423 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9425 const struct got_error *err;
9426 struct got_tree_object *tree;
9429 err = got_object_open_as_tree(&tree, repo, id);
9433 nentries = got_object_tree_get_nentries(tree);
9434 for (i = 0; i < nentries; i++) {
9435 struct got_tree_entry *te;
9437 if (sigint_received || sigpipe_received)
9439 te = got_object_tree_get_entry(tree, i);
9440 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9443 fprintf(outfile, "%s %.7o %s\n", id_str,
9444 got_tree_entry_get_mode(te),
9445 got_tree_entry_get_name(te));
9449 got_object_tree_close(tree);
9453 static const struct got_error *
9454 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9456 const struct got_error *err;
9457 struct got_commit_object *commit;
9458 const struct got_object_id_queue *parent_ids;
9459 struct got_object_qid *pid;
9460 char *id_str = NULL;
9461 const char *logmsg = NULL;
9463 err = got_object_open_as_commit(&commit, repo, id);
9467 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9471 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9472 parent_ids = got_object_commit_get_parent_ids(commit);
9473 fprintf(outfile, "numparents %d\n",
9474 got_object_commit_get_nparents(commit));
9475 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9477 err = got_object_id_str(&pid_str, pid->id);
9480 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9483 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9484 got_object_commit_get_author(commit),
9485 (long long)got_object_commit_get_author_time(commit));
9487 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9488 got_object_commit_get_author(commit),
9489 (long long)got_object_commit_get_committer_time(commit));
9491 logmsg = got_object_commit_get_logmsg_raw(commit);
9492 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9493 fprintf(outfile, "%s", logmsg);
9496 got_object_commit_close(commit);
9500 static const struct got_error *
9501 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9503 const struct got_error *err;
9504 struct got_tag_object *tag;
9505 char *id_str = NULL;
9506 const char *tagmsg = NULL;
9508 err = got_object_open_as_tag(&tag, repo, id);
9512 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9516 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9518 switch (got_object_tag_get_object_type(tag)) {
9519 case GOT_OBJ_TYPE_BLOB:
9520 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9521 GOT_OBJ_LABEL_BLOB);
9523 case GOT_OBJ_TYPE_TREE:
9524 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9525 GOT_OBJ_LABEL_TREE);
9527 case GOT_OBJ_TYPE_COMMIT:
9528 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9529 GOT_OBJ_LABEL_COMMIT);
9531 case GOT_OBJ_TYPE_TAG:
9532 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9539 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9540 got_object_tag_get_name(tag));
9542 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9543 got_object_tag_get_tagger(tag),
9544 (long long)got_object_tag_get_tagger_time(tag));
9546 tagmsg = got_object_tag_get_message(tag);
9547 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9548 fprintf(outfile, "%s", tagmsg);
9551 got_object_tag_close(tag);
9555 static const struct got_error *
9556 cmd_cat(int argc, char *argv[])
9558 const struct got_error *error;
9559 struct got_repository *repo = NULL;
9560 struct got_worktree *worktree = NULL;
9561 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9562 const char *commit_id_str = NULL;
9563 struct got_object_id *id = NULL, *commit_id = NULL;
9564 int ch, obj_type, i, force_path = 0;
9567 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9572 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9575 commit_id_str = optarg;
9578 repo_path = realpath(optarg, NULL);
9579 if (repo_path == NULL)
9580 return got_error_from_errno2("realpath",
9582 got_path_strip_trailing_slashes(repo_path);
9596 cwd = getcwd(NULL, 0);
9598 error = got_error_from_errno("getcwd");
9601 error = got_worktree_open(&worktree, cwd);
9602 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9605 if (repo_path == NULL) {
9607 got_worktree_get_repo_path(worktree));
9608 if (repo_path == NULL) {
9609 error = got_error_from_errno("strdup");
9615 if (repo_path == NULL) {
9616 repo_path = getcwd(NULL, 0);
9617 if (repo_path == NULL)
9618 return got_error_from_errno("getcwd");
9621 error = got_repo_open(&repo, repo_path, NULL);
9626 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9630 if (commit_id_str == NULL)
9631 commit_id_str = GOT_REF_HEAD;
9632 error = got_repo_match_object_id(&commit_id, NULL,
9633 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9637 for (i = 0; i < argc; i++) {
9639 error = got_object_id_by_path(&id, repo, commit_id,
9644 error = got_repo_match_object_id(&id, &label, argv[i],
9645 GOT_OBJ_TYPE_ANY, 0, repo);
9647 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9648 error->code != GOT_ERR_NOT_REF)
9650 error = got_object_id_by_path(&id, repo,
9651 commit_id, argv[i]);
9657 error = got_object_get_type(&obj_type, repo, id);
9662 case GOT_OBJ_TYPE_BLOB:
9663 error = cat_blob(id, repo, stdout);
9665 case GOT_OBJ_TYPE_TREE:
9666 error = cat_tree(id, repo, stdout);
9668 case GOT_OBJ_TYPE_COMMIT:
9669 error = cat_commit(id, repo, stdout);
9671 case GOT_OBJ_TYPE_TAG:
9672 error = cat_tag(id, repo, stdout);
9675 error = got_error(GOT_ERR_OBJ_TYPE);
9690 got_worktree_close(worktree);
9692 const struct got_error *repo_error;
9693 repo_error = got_repo_close(repo);
9703 fprintf(stderr, "usage: %s info [path ...]\n",
9708 static const struct got_error *
9709 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9710 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9711 struct got_object_id *commit_id)
9713 const struct got_error *err = NULL;
9714 char *id_str = NULL;
9716 struct tm mytm, *tm;
9717 struct got_pathlist_head *paths = arg;
9718 struct got_pathlist_entry *pe;
9721 * Clear error indication from any of the path arguments which
9722 * would cause this file index entry to be displayed.
9724 TAILQ_FOREACH(pe, paths, entry) {
9725 if (got_path_cmp(path, pe->path, strlen(path),
9726 pe->path_len) == 0 ||
9727 got_path_is_child(path, pe->path, pe->path_len))
9728 pe->data = NULL; /* no error */
9731 printf(GOT_COMMIT_SEP_STR);
9733 printf("symlink: %s\n", path);
9734 else if (S_ISREG(mode)) {
9735 printf("file: %s\n", path);
9736 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9737 } else if (S_ISDIR(mode))
9738 printf("directory: %s\n", path);
9740 printf("something: %s\n", path);
9742 tm = localtime_r(&mtime, &mytm);
9745 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9746 return got_error(GOT_ERR_NO_SPACE);
9747 printf("timestamp: %s\n", datebuf);
9750 err = got_object_id_str(&id_str, blob_id);
9753 printf("based on blob: %s\n", id_str);
9757 if (staged_blob_id) {
9758 err = got_object_id_str(&id_str, staged_blob_id);
9761 printf("based on staged blob: %s\n", id_str);
9766 err = got_object_id_str(&id_str, commit_id);
9769 printf("based on commit: %s\n", id_str);
9776 static const struct got_error *
9777 cmd_info(int argc, char *argv[])
9779 const struct got_error *error = NULL;
9780 struct got_worktree *worktree = NULL;
9781 char *cwd = NULL, *id_str = NULL;
9782 struct got_pathlist_head paths;
9783 struct got_pathlist_entry *pe;
9784 char *uuidstr = NULL;
9785 int ch, show_files = 0;
9789 while ((ch = getopt(argc, argv, "")) != -1) {
9801 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9805 cwd = getcwd(NULL, 0);
9807 error = got_error_from_errno("getcwd");
9811 error = got_worktree_open(&worktree, cwd);
9813 if (error->code == GOT_ERR_NOT_WORKTREE)
9814 error = wrap_not_worktree_error(error, "status", cwd);
9818 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9823 error = get_worktree_paths_from_argv(&paths, argc, argv,
9830 error = got_object_id_str(&id_str,
9831 got_worktree_get_base_commit_id(worktree));
9835 error = got_worktree_get_uuid(&uuidstr, worktree);
9839 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9840 printf("work tree base commit: %s\n", id_str);
9841 printf("work tree path prefix: %s\n",
9842 got_worktree_get_path_prefix(worktree));
9843 printf("work tree branch reference: %s\n",
9844 got_worktree_get_head_ref_name(worktree));
9845 printf("work tree UUID: %s\n", uuidstr);
9846 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9849 struct got_pathlist_entry *pe;
9850 TAILQ_FOREACH(pe, &paths, entry) {
9851 if (pe->path_len == 0)
9854 * Assume this path will fail. This will be corrected
9855 * in print_path_info() in case the path does suceeed.
9857 pe->data = (void *)got_error_path(pe->path,
9860 error = got_worktree_path_info(worktree, &paths,
9861 print_path_info, &paths, check_cancelled, NULL);
9864 TAILQ_FOREACH(pe, &paths, entry) {
9865 if (pe->data != NULL) {
9866 error = pe->data; /* bad path */
9872 TAILQ_FOREACH(pe, &paths, entry)
9873 free((char *)pe->path);
9874 got_pathlist_free(&paths);