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>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 static volatile sig_atomic_t sigint_received;
65 static volatile sig_atomic_t sigpipe_received;
68 catch_sigint(int signo)
74 catch_sigpipe(int signo)
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 const char *cmd_alias;
87 __dead static void usage(int, int);
88 __dead static void usage_init(void);
89 __dead static void usage_import(void);
90 __dead static void usage_clone(void);
91 __dead static void usage_fetch(void);
92 __dead static void usage_checkout(void);
93 __dead static void usage_update(void);
94 __dead static void usage_log(void);
95 __dead static void usage_diff(void);
96 __dead static void usage_blame(void);
97 __dead static void usage_tree(void);
98 __dead static void usage_status(void);
99 __dead static void usage_ref(void);
100 __dead static void usage_branch(void);
101 __dead static void usage_tag(void);
102 __dead static void usage_add(void);
103 __dead static void usage_remove(void);
104 __dead static void usage_revert(void);
105 __dead static void usage_commit(void);
106 __dead static void usage_send(void);
107 __dead static void usage_cherrypick(void);
108 __dead static void usage_backout(void);
109 __dead static void usage_rebase(void);
110 __dead static void usage_histedit(void);
111 __dead static void usage_integrate(void);
112 __dead static void usage_merge(void);
113 __dead static void usage_stage(void);
114 __dead static void usage_unstage(void);
115 __dead static void usage_cat(void);
116 __dead static void usage_info(void);
118 static const struct got_error* cmd_init(int, char *[]);
119 static const struct got_error* cmd_import(int, char *[]);
120 static const struct got_error* cmd_clone(int, char *[]);
121 static const struct got_error* cmd_fetch(int, char *[]);
122 static const struct got_error* cmd_checkout(int, char *[]);
123 static const struct got_error* cmd_update(int, char *[]);
124 static const struct got_error* cmd_log(int, char *[]);
125 static const struct got_error* cmd_diff(int, char *[]);
126 static const struct got_error* cmd_blame(int, char *[]);
127 static const struct got_error* cmd_tree(int, char *[]);
128 static const struct got_error* cmd_status(int, char *[]);
129 static const struct got_error* cmd_ref(int, char *[]);
130 static const struct got_error* cmd_branch(int, char *[]);
131 static const struct got_error* cmd_tag(int, char *[]);
132 static const struct got_error* cmd_add(int, char *[]);
133 static const struct got_error* cmd_remove(int, char *[]);
134 static const struct got_error* cmd_revert(int, char *[]);
135 static const struct got_error* cmd_commit(int, char *[]);
136 static const struct got_error* cmd_send(int, char *[]);
137 static const struct got_error* cmd_cherrypick(int, char *[]);
138 static const struct got_error* cmd_backout(int, char *[]);
139 static const struct got_error* cmd_rebase(int, char *[]);
140 static const struct got_error* cmd_histedit(int, char *[]);
141 static const struct got_error* cmd_integrate(int, char *[]);
142 static const struct got_error* cmd_merge(int, char *[]);
143 static const struct got_error* cmd_stage(int, char *[]);
144 static const struct got_error* cmd_unstage(int, char *[]);
145 static const struct got_error* cmd_cat(int, char *[]);
146 static const struct got_error* cmd_info(int, char *[]);
148 static struct got_cmd got_commands[] = {
149 { "init", cmd_init, usage_init, "" },
150 { "import", cmd_import, usage_import, "im" },
151 { "clone", cmd_clone, usage_clone, "cl" },
152 { "fetch", cmd_fetch, usage_fetch, "fe" },
153 { "checkout", cmd_checkout, usage_checkout, "co" },
154 { "update", cmd_update, usage_update, "up" },
155 { "log", cmd_log, usage_log, "" },
156 { "diff", cmd_diff, usage_diff, "di" },
157 { "blame", cmd_blame, usage_blame, "bl" },
158 { "tree", cmd_tree, usage_tree, "tr" },
159 { "status", cmd_status, usage_status, "st" },
160 { "ref", cmd_ref, usage_ref, "" },
161 { "branch", cmd_branch, usage_branch, "br" },
162 { "tag", cmd_tag, usage_tag, "" },
163 { "add", cmd_add, usage_add, "" },
164 { "remove", cmd_remove, usage_remove, "rm" },
165 { "revert", cmd_revert, usage_revert, "rv" },
166 { "commit", cmd_commit, usage_commit, "ci" },
167 { "send", cmd_send, usage_send, "se" },
168 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
169 { "backout", cmd_backout, usage_backout, "bo" },
170 { "rebase", cmd_rebase, usage_rebase, "rb" },
171 { "histedit", cmd_histedit, usage_histedit, "he" },
172 { "integrate", cmd_integrate, usage_integrate,"ig" },
173 { "merge", cmd_merge, usage_merge, "mg" },
174 { "stage", cmd_stage, usage_stage, "sg" },
175 { "unstage", cmd_unstage, usage_unstage, "ug" },
176 { "cat", cmd_cat, usage_cat, "" },
177 { "info", cmd_info, usage_info, "" },
181 list_commands(FILE *fp)
185 fprintf(fp, "commands:");
186 for (i = 0; i < nitems(got_commands); i++) {
187 struct got_cmd *cmd = &got_commands[i];
188 fprintf(fp, " %s", cmd->cmd_name);
194 option_conflict(char a, char b)
196 errx(1, "-%c and -%c options are mutually exclusive", a, b);
200 main(int argc, char *argv[])
205 int hflag = 0, Vflag = 0;
206 static struct option longopts[] = {
207 { "version", no_argument, NULL, 'V' },
211 setlocale(LC_CTYPE, "");
213 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
233 got_version_print_str();
238 usage(hflag, hflag ? 0 : 1);
240 signal(SIGINT, catch_sigint);
241 signal(SIGPIPE, catch_sigpipe);
243 for (i = 0; i < nitems(got_commands); i++) {
244 const struct got_error *error;
246 cmd = &got_commands[i];
248 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
249 strcmp(cmd->cmd_alias, argv[0]) != 0)
253 got_commands[i].cmd_usage();
255 error = got_commands[i].cmd_main(argc, argv);
256 if (error && error->code != GOT_ERR_CANCELLED &&
257 error->code != GOT_ERR_PRIVSEP_EXIT &&
258 !(sigpipe_received &&
259 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
261 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
262 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
269 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
270 list_commands(stderr);
275 usage(int hflag, int status)
277 FILE *fp = (status == 0) ? stdout : stderr;
279 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
286 static const struct got_error *
287 get_editor(char **abspath)
289 const struct got_error *err = NULL;
294 editor = getenv("VISUAL");
296 editor = getenv("EDITOR");
299 err = got_path_find_prog(abspath, editor);
304 if (*abspath == NULL) {
305 *abspath = strdup("/bin/ed");
306 if (*abspath == NULL)
307 return got_error_from_errno("strdup");
313 static const struct got_error *
314 apply_unveil(const char *repo_path, int repo_read_only,
315 const char *worktree_path)
317 const struct got_error *err;
320 if (unveil("gmon.out", "rwc") != 0)
321 return got_error_from_errno2("unveil", "gmon.out");
323 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
324 return got_error_from_errno2("unveil", repo_path);
326 if (worktree_path && unveil(worktree_path, "rwc") != 0)
327 return got_error_from_errno2("unveil", worktree_path);
329 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
330 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
332 err = got_privsep_unveil_exec_helpers();
336 if (unveil(NULL, NULL) != 0)
337 return got_error_from_errno("unveil");
345 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
349 static const struct got_error *
350 cmd_init(int argc, char *argv[])
352 const struct got_error *error = NULL;
353 char *repo_path = NULL;
356 while ((ch = getopt(argc, argv, "")) != -1) {
368 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
374 repo_path = strdup(argv[0]);
375 if (repo_path == NULL)
376 return got_error_from_errno("strdup");
378 got_path_strip_trailing_slashes(repo_path);
380 error = got_path_mkdir(repo_path);
382 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
385 error = apply_unveil(repo_path, 0, NULL);
389 error = got_repo_init(repo_path);
398 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
399 "[-r repository-path] [-I pattern] path\n", getprogname());
404 spawn_editor(const char *editor, const char *file)
407 sig_t sighup, sigint, sigquit;
410 sighup = signal(SIGHUP, SIG_IGN);
411 sigint = signal(SIGINT, SIG_IGN);
412 sigquit = signal(SIGQUIT, SIG_IGN);
414 switch (pid = fork()) {
418 execl(editor, editor, file, (char *)NULL);
422 while (waitpid(pid, &st, 0) == -1)
427 (void)signal(SIGHUP, sighup);
428 (void)signal(SIGINT, sigint);
429 (void)signal(SIGQUIT, sigquit);
431 if (!WIFEXITED(st)) {
436 return WEXITSTATUS(st);
439 static const struct got_error *
440 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
441 const char *initial_content, size_t initial_content_len,
442 int require_modification)
444 const struct got_error *err = NULL;
450 size_t len, logmsg_len;
451 char *initial_content_stripped = NULL, *buf = NULL, *s;
455 if (stat(logmsg_path, &st) == -1)
456 return got_error_from_errno2("stat", logmsg_path);
458 if (spawn_editor(editor, logmsg_path) == -1)
459 return got_error_from_errno("failed spawning editor");
461 if (stat(logmsg_path, &st2) == -1)
462 return got_error_from_errno("stat");
464 if (require_modification &&
465 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
466 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
467 "no changes made to commit message, aborting");
470 * Set up a stripped version of the initial content without comments
471 * and blank lines. We need this in order to check if the message
472 * has in fact been edited.
474 initial_content_stripped = malloc(initial_content_len + 1);
475 if (initial_content_stripped == NULL)
476 return got_error_from_errno("malloc");
477 initial_content_stripped[0] = '\0';
479 buf = strdup(initial_content);
481 err = got_error_from_errno("strdup");
486 while ((line = strsep(&s, "\n")) != NULL) {
487 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
488 continue; /* remove comments and leading empty lines */
489 len = strlcat(initial_content_stripped, line,
490 initial_content_len + 1);
491 if (len >= initial_content_len + 1) {
492 err = got_error(GOT_ERR_NO_SPACE);
496 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
497 initial_content_stripped[len - 1] = '\0';
501 logmsg_len = st2.st_size;
502 *logmsg = malloc(logmsg_len + 1);
504 return got_error_from_errno("malloc");
507 fp = fopen(logmsg_path, "r");
509 err = got_error_from_errno("fopen");
514 while ((linelen = getline(&line, &linesize, fp)) != -1) {
515 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
516 continue; /* remove comments and leading empty lines */
517 len = strlcat(*logmsg, line, logmsg_len + 1);
518 if (len >= logmsg_len + 1) {
519 err = got_error(GOT_ERR_NO_SPACE);
525 err = got_ferror(fp, GOT_ERR_IO);
528 while (len > 0 && (*logmsg)[len - 1] == '\n') {
529 (*logmsg)[len - 1] = '\0';
534 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
535 "commit message cannot be empty, aborting");
538 if (require_modification &&
539 strcmp(*logmsg, initial_content_stripped) == 0)
540 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
541 "no changes made to commit message, aborting");
543 free(initial_content_stripped);
545 if (fp && fclose(fp) == EOF && err == NULL)
546 err = got_error_from_errno("fclose");
554 static const struct got_error *
555 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
556 const char *path_dir, const char *branch_name)
558 char *initial_content = NULL;
559 const struct got_error *err = NULL;
560 int initial_content_len;
563 initial_content_len = asprintf(&initial_content,
564 "\n# %s to be imported to branch %s\n", path_dir,
566 if (initial_content_len == -1)
567 return got_error_from_errno("asprintf");
569 err = got_opentemp_named_fd(logmsg_path, &fd,
570 GOT_TMPDIR_STR "/got-importmsg");
574 if (write(fd, initial_content, initial_content_len) == -1) {
575 err = got_error_from_errno2("write", *logmsg_path);
579 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
580 initial_content_len, 1);
582 if (fd != -1 && close(fd) == -1 && err == NULL)
583 err = got_error_from_errno2("close", *logmsg_path);
584 free(initial_content);
592 static const struct got_error *
593 import_progress(void *arg, const char *path)
595 printf("A %s\n", path);
599 static const struct got_error *
600 get_author(char **author, struct got_repository *repo,
601 struct got_worktree *worktree)
603 const struct got_error *err = NULL;
604 const char *got_author = NULL, *name, *email;
605 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
610 worktree_conf = got_worktree_get_gotconfig(worktree);
611 repo_conf = got_repo_get_gotconfig(repo);
614 * Priority of potential author information sources, from most
615 * significant to least significant:
616 * 1) work tree's .got/got.conf file
617 * 2) repository's got.conf file
618 * 3) repository's git config file
619 * 4) environment variables
620 * 5) global git config files (in user's home directory or /etc)
624 got_author = got_gotconfig_get_author(worktree_conf);
625 if (got_author == NULL)
626 got_author = got_gotconfig_get_author(repo_conf);
627 if (got_author == NULL) {
628 name = got_repo_get_gitconfig_author_name(repo);
629 email = got_repo_get_gitconfig_author_email(repo);
631 if (asprintf(author, "%s <%s>", name, email) == -1)
632 return got_error_from_errno("asprintf");
636 got_author = getenv("GOT_AUTHOR");
637 if (got_author == NULL) {
638 name = got_repo_get_global_gitconfig_author_name(repo);
639 email = got_repo_get_global_gitconfig_author_email(
642 if (asprintf(author, "%s <%s>", name, email)
644 return got_error_from_errno("asprintf");
647 /* TODO: Look up user in password database? */
648 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
652 *author = strdup(got_author);
654 return got_error_from_errno("strdup");
657 * Really dumb email address check; we're only doing this to
658 * avoid git's object parser breaking on commits we create.
660 while (*got_author && *got_author != '<')
662 if (*got_author != '<') {
663 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 while (*got_author && *got_author != '@')
668 if (*got_author != '@') {
669 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
672 while (*got_author && *got_author != '>')
674 if (*got_author != '>')
675 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
684 static const struct got_error *
685 get_gitconfig_path(char **gitconfig_path)
687 const char *homedir = getenv("HOME");
689 *gitconfig_path = NULL;
691 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
692 return got_error_from_errno("asprintf");
698 static const struct got_error *
699 cmd_import(int argc, char *argv[])
701 const struct got_error *error = NULL;
702 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
703 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
704 const char *branch_name = "main";
705 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
706 struct got_repository *repo = NULL;
707 struct got_reference *branch_ref = NULL, *head_ref = NULL;
708 struct got_object_id *new_commit_id = NULL;
710 struct got_pathlist_head ignores;
711 struct got_pathlist_entry *pe;
712 int preserve_logmsg = 0;
714 TAILQ_INIT(&ignores);
716 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
719 branch_name = optarg;
722 logmsg = strdup(optarg);
723 if (logmsg == NULL) {
724 error = got_error_from_errno("strdup");
729 repo_path = realpath(optarg, NULL);
730 if (repo_path == NULL) {
731 error = got_error_from_errno2("realpath",
737 if (optarg[0] == '\0')
739 error = got_pathlist_insert(&pe, &ignores, optarg,
754 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
762 if (repo_path == NULL) {
763 repo_path = getcwd(NULL, 0);
764 if (repo_path == NULL)
765 return got_error_from_errno("getcwd");
767 got_path_strip_trailing_slashes(repo_path);
768 error = get_gitconfig_path(&gitconfig_path);
771 error = got_repo_open(&repo, repo_path, gitconfig_path);
775 error = get_author(&author, repo, NULL);
780 * Don't let the user create a branch name with a leading '-'.
781 * While technically a valid reference name, this case is usually
782 * an unintended typo.
784 if (branch_name[0] == '-')
785 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
787 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
788 error = got_error_from_errno("asprintf");
792 error = got_ref_open(&branch_ref, repo, refname, 0);
794 if (error->code != GOT_ERR_NOT_REF)
797 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
798 "import target branch already exists");
802 path_dir = realpath(argv[0], NULL);
803 if (path_dir == NULL) {
804 error = got_error_from_errno2("realpath", argv[0]);
807 got_path_strip_trailing_slashes(path_dir);
810 * unveil(2) traverses exec(2); if an editor is used we have
811 * to apply unveil after the log message has been written.
813 if (logmsg == NULL || strlen(logmsg) == 0) {
814 error = get_editor(&editor);
818 error = collect_import_msg(&logmsg, &logmsg_path, editor,
821 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
828 if (unveil(path_dir, "r") != 0) {
829 error = got_error_from_errno2("unveil", path_dir);
835 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
842 error = got_repo_import(&new_commit_id, path_dir, logmsg,
843 author, &ignores, repo, import_progress, NULL);
850 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
857 error = got_ref_write(branch_ref, repo);
864 error = got_object_id_str(&id_str, new_commit_id);
871 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
873 if (error->code != GOT_ERR_NOT_REF) {
879 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
887 error = got_ref_write(head_ref, repo);
895 printf("Created branch %s with commit %s\n",
896 got_ref_get_name(branch_ref), id_str);
898 if (preserve_logmsg) {
899 fprintf(stderr, "%s: log message preserved in %s\n",
900 getprogname(), logmsg_path);
901 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
902 error = got_error_from_errno2("unlink", logmsg_path);
911 free(gitconfig_path);
913 got_ref_close(branch_ref);
915 got_ref_close(head_ref);
922 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
923 "[-R reference] repository-url [directory]\n", getprogname());
927 struct got_fetch_progress_arg {
928 char last_scaled_size[FMT_SCALED_STRSIZE];
933 struct got_repository *repo;
938 struct got_pathlist_head *symrefs;
939 struct got_pathlist_head *wanted_branches;
940 struct got_pathlist_head *wanted_refs;
944 const char *remote_repo_path;
946 int fetch_all_branches;
947 int mirror_references;
951 /* XXX forward declaration */
952 static const struct got_error *
953 create_config_files(const char *proto, const char *host, const char *port,
954 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
955 int mirror_references, struct got_pathlist_head *symrefs,
956 struct got_pathlist_head *wanted_branches,
957 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
959 static const struct got_error *
960 fetch_progress(void *arg, const char *message, off_t packfile_size,
961 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
963 const struct got_error *err = NULL;
964 struct got_fetch_progress_arg *a = arg;
965 char scaled_size[FMT_SCALED_STRSIZE];
966 int p_indexed, p_resolved;
967 int print_size = 0, print_indexed = 0, print_resolved = 0;
970 * In order to allow a failed clone to be resumed with 'got fetch'
971 * we try to create configuration files as soon as possible.
972 * Once the server has sent information about its default branch
973 * we have all required information.
975 if (a->create_configs && !a->configs_created &&
976 !TAILQ_EMPTY(a->config_info.symrefs)) {
977 err = create_config_files(a->config_info.proto,
978 a->config_info.host, a->config_info.port,
979 a->config_info.remote_repo_path,
980 a->config_info.git_url,
981 a->config_info.fetch_all_branches,
982 a->config_info.mirror_references,
983 a->config_info.symrefs,
984 a->config_info.wanted_branches,
985 a->config_info.wanted_refs, a->repo);
988 a->configs_created = 1;
991 if (a->verbosity < 0)
994 if (message && message[0] != '\0') {
995 printf("\rserver: %s", message);
1000 if (packfile_size > 0 || nobj_indexed > 0) {
1001 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1002 (a->last_scaled_size[0] == '\0' ||
1003 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1005 if (strlcpy(a->last_scaled_size, scaled_size,
1006 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1007 return got_error(GOT_ERR_NO_SPACE);
1009 if (nobj_indexed > 0) {
1010 p_indexed = (nobj_indexed * 100) / nobj_total;
1011 if (p_indexed != a->last_p_indexed) {
1012 a->last_p_indexed = p_indexed;
1017 if (nobj_resolved > 0) {
1018 p_resolved = (nobj_resolved * 100) /
1019 (nobj_total - nobj_loose);
1020 if (p_resolved != a->last_p_resolved) {
1021 a->last_p_resolved = p_resolved;
1029 if (print_size || print_indexed || print_resolved)
1032 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1034 printf("; indexing %d%%", p_indexed);
1036 printf("; resolving deltas %d%%", p_resolved);
1037 if (print_size || print_indexed || print_resolved)
1043 static const struct got_error *
1044 create_symref(const char *refname, struct got_reference *target_ref,
1045 int verbosity, struct got_repository *repo)
1047 const struct got_error *err;
1048 struct got_reference *head_symref;
1050 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1054 err = got_ref_write(head_symref, repo);
1055 if (err == NULL && verbosity > 0) {
1056 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1057 got_ref_get_name(target_ref));
1059 got_ref_close(head_symref);
1063 static const struct got_error *
1064 list_remote_refs(struct got_pathlist_head *symrefs,
1065 struct got_pathlist_head *refs)
1067 const struct got_error *err;
1068 struct got_pathlist_entry *pe;
1070 TAILQ_FOREACH(pe, symrefs, entry) {
1071 const char *refname = pe->path;
1072 const char *targetref = pe->data;
1074 printf("%s: %s\n", refname, targetref);
1077 TAILQ_FOREACH(pe, refs, entry) {
1078 const char *refname = pe->path;
1079 struct got_object_id *id = pe->data;
1082 err = got_object_id_str(&id_str, id);
1085 printf("%s: %s\n", refname, id_str);
1092 static const struct got_error *
1093 create_ref(const char *refname, struct got_object_id *id,
1094 int verbosity, struct got_repository *repo)
1096 const struct got_error *err = NULL;
1097 struct got_reference *ref;
1100 err = got_object_id_str(&id_str, id);
1104 err = got_ref_alloc(&ref, refname, id);
1108 err = got_ref_write(ref, repo);
1111 if (err == NULL && verbosity >= 0)
1112 printf("Created reference %s: %s\n", refname, id_str);
1119 match_wanted_ref(const char *refname, const char *wanted_ref)
1121 if (strncmp(refname, "refs/", 5) != 0)
1126 * Prevent fetching of references that won't make any
1127 * sense outside of the remote repository's context.
1129 if (strncmp(refname, "got/", 4) == 0)
1131 if (strncmp(refname, "remotes/", 8) == 0)
1134 if (strncmp(wanted_ref, "refs/", 5) == 0)
1137 /* Allow prefix match. */
1138 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1141 /* Allow exact match. */
1142 return (strcmp(refname, wanted_ref) == 0);
1146 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1148 struct got_pathlist_entry *pe;
1150 TAILQ_FOREACH(pe, wanted_refs, entry) {
1151 if (match_wanted_ref(refname, pe->path))
1158 static const struct got_error *
1159 create_wanted_ref(const char *refname, struct got_object_id *id,
1160 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1162 const struct got_error *err;
1163 char *remote_refname;
1165 if (strncmp("refs/", refname, 5) == 0)
1168 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1169 remote_repo_name, refname) == -1)
1170 return got_error_from_errno("asprintf");
1172 err = create_ref(remote_refname, id, verbosity, repo);
1173 free(remote_refname);
1177 static const struct got_error *
1178 create_gotconfig(const char *proto, const char *host, const char *port,
1179 const char *remote_repo_path, const char *default_branch,
1180 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1181 struct got_pathlist_head *wanted_refs, int mirror_references,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 char *gotconfig_path = NULL;
1186 char *gotconfig = NULL;
1187 FILE *gotconfig_file = NULL;
1188 const char *branchname = NULL;
1189 char *branches = NULL, *refs = NULL;
1192 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1193 struct got_pathlist_entry *pe;
1194 TAILQ_FOREACH(pe, wanted_branches, entry) {
1196 branchname = pe->path;
1197 if (strncmp(branchname, "refs/heads/", 11) == 0)
1199 if (asprintf(&s, "%s\"%s\" ",
1200 branches ? branches : "", branchname) == -1) {
1201 err = got_error_from_errno("asprintf");
1207 } else if (!fetch_all_branches && default_branch) {
1208 branchname = default_branch;
1209 if (strncmp(branchname, "refs/heads/", 11) == 0)
1211 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1212 err = got_error_from_errno("asprintf");
1216 if (!TAILQ_EMPTY(wanted_refs)) {
1217 struct got_pathlist_entry *pe;
1218 TAILQ_FOREACH(pe, wanted_refs, entry) {
1220 const char *refname = pe->path;
1221 if (strncmp(refname, "refs/", 5) == 0)
1223 if (asprintf(&s, "%s\"%s\" ",
1224 refs ? refs : "", refname) == -1) {
1225 err = got_error_from_errno("asprintf");
1233 /* Create got.conf(5). */
1234 gotconfig_path = got_repo_get_path_gotconfig(repo);
1235 if (gotconfig_path == NULL) {
1236 err = got_error_from_errno("got_repo_get_path_gotconfig");
1239 gotconfig_file = fopen(gotconfig_path, "a");
1240 if (gotconfig_file == NULL) {
1241 err = got_error_from_errno2("fopen", gotconfig_path);
1244 if (asprintf(&gotconfig,
1249 "\trepository \"%s\"\n"
1255 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1256 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1257 remote_repo_path, branches ? "\tbranch { " : "",
1258 branches ? branches : "", branches ? "}\n" : "",
1259 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1260 mirror_references ? "\tmirror-references yes\n" : "",
1261 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1262 err = got_error_from_errno("asprintf");
1265 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1266 if (n != strlen(gotconfig)) {
1267 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1272 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1273 err = got_error_from_errno2("fclose", gotconfig_path);
1274 free(gotconfig_path);
1279 static const struct got_error *
1280 create_gitconfig(const char *git_url, const char *default_branch,
1281 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1282 struct got_pathlist_head *wanted_refs, int mirror_references,
1283 struct got_repository *repo)
1285 const struct got_error *err = NULL;
1286 char *gitconfig_path = NULL;
1287 char *gitconfig = NULL;
1288 FILE *gitconfig_file = NULL;
1289 char *branches = NULL, *refs = NULL;
1290 const char *branchname;
1293 /* Create a config file Git can understand. */
1294 gitconfig_path = got_repo_get_path_gitconfig(repo);
1295 if (gitconfig_path == NULL) {
1296 err = got_error_from_errno("got_repo_get_path_gitconfig");
1299 gitconfig_file = fopen(gitconfig_path, "a");
1300 if (gitconfig_file == NULL) {
1301 err = got_error_from_errno2("fopen", gitconfig_path);
1304 if (fetch_all_branches) {
1305 if (mirror_references) {
1306 if (asprintf(&branches,
1307 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1308 err = got_error_from_errno("asprintf");
1311 } else if (asprintf(&branches,
1312 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1313 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1314 err = got_error_from_errno("asprintf");
1317 } else if (!TAILQ_EMPTY(wanted_branches)) {
1318 struct got_pathlist_entry *pe;
1319 TAILQ_FOREACH(pe, wanted_branches, entry) {
1321 branchname = pe->path;
1322 if (strncmp(branchname, "refs/heads/", 11) == 0)
1324 if (mirror_references) {
1326 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1327 branches ? branches : "",
1328 branchname, branchname) == -1) {
1329 err = got_error_from_errno("asprintf");
1332 } else if (asprintf(&s,
1333 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1334 branches ? branches : "",
1335 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1336 branchname) == -1) {
1337 err = got_error_from_errno("asprintf");
1345 * If the server specified a default branch, use just that one.
1346 * Otherwise fall back to fetching all branches on next fetch.
1348 if (default_branch) {
1349 branchname = default_branch;
1350 if (strncmp(branchname, "refs/heads/", 11) == 0)
1353 branchname = "*"; /* fall back to all branches */
1354 if (mirror_references) {
1355 if (asprintf(&branches,
1356 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1357 branchname, branchname) == -1) {
1358 err = got_error_from_errno("asprintf");
1361 } else if (asprintf(&branches,
1362 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1363 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1364 branchname) == -1) {
1365 err = got_error_from_errno("asprintf");
1369 if (!TAILQ_EMPTY(wanted_refs)) {
1370 struct got_pathlist_entry *pe;
1371 TAILQ_FOREACH(pe, wanted_refs, entry) {
1373 const char *refname = pe->path;
1374 if (strncmp(refname, "refs/", 5) == 0)
1376 if (mirror_references) {
1378 "%s\tfetch = refs/%s:refs/%s\n",
1379 refs ? refs : "", refname, refname) == -1) {
1380 err = got_error_from_errno("asprintf");
1383 } else if (asprintf(&s,
1384 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1386 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1388 err = got_error_from_errno("asprintf");
1396 if (asprintf(&gitconfig,
1401 "\tfetch = refs/tags/*:refs/tags/*\n",
1402 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1403 refs ? refs : "") == -1) {
1404 err = got_error_from_errno("asprintf");
1407 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1408 if (n != strlen(gitconfig)) {
1409 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1413 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1414 err = got_error_from_errno2("fclose", gitconfig_path);
1415 free(gitconfig_path);
1420 static const struct got_error *
1421 create_config_files(const char *proto, const char *host, const char *port,
1422 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1423 int mirror_references, struct got_pathlist_head *symrefs,
1424 struct got_pathlist_head *wanted_branches,
1425 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1427 const struct got_error *err = NULL;
1428 const char *default_branch = NULL;
1429 struct got_pathlist_entry *pe;
1432 * If we asked for a set of wanted branches then use the first
1435 if (!TAILQ_EMPTY(wanted_branches)) {
1436 pe = TAILQ_FIRST(wanted_branches);
1437 default_branch = pe->path;
1439 /* First HEAD ref listed by server is the default branch. */
1440 TAILQ_FOREACH(pe, symrefs, entry) {
1441 const char *refname = pe->path;
1442 const char *target = pe->data;
1444 if (strcmp(refname, GOT_REF_HEAD) != 0)
1447 default_branch = target;
1452 /* Create got.conf(5). */
1453 err = create_gotconfig(proto, host, port, remote_repo_path,
1454 default_branch, fetch_all_branches, wanted_branches,
1455 wanted_refs, mirror_references, repo);
1459 /* Create a config file Git can understand. */
1460 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1461 wanted_branches, wanted_refs, mirror_references, repo);
1464 static const struct got_error *
1465 cmd_clone(int argc, char *argv[])
1467 const struct got_error *error = NULL;
1468 const char *uri, *dirname;
1469 char *proto, *host, *port, *repo_name, *server_path;
1470 char *default_destdir = NULL, *id_str = NULL;
1471 const char *repo_path;
1472 struct got_repository *repo = NULL;
1473 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1474 struct got_pathlist_entry *pe;
1475 struct got_object_id *pack_hash = NULL;
1476 int ch, fetchfd = -1, fetchstatus;
1477 pid_t fetchpid = -1;
1478 struct got_fetch_progress_arg fpa;
1479 char *git_url = NULL;
1480 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1481 int list_refs_only = 0;
1484 TAILQ_INIT(&symrefs);
1485 TAILQ_INIT(&wanted_branches);
1486 TAILQ_INIT(&wanted_refs);
1488 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1491 fetch_all_branches = 1;
1494 error = got_pathlist_append(&wanted_branches,
1503 mirror_references = 1;
1508 else if (verbosity < 3)
1515 error = got_pathlist_append(&wanted_refs,
1528 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1529 option_conflict('a', 'b');
1530 if (list_refs_only) {
1531 if (!TAILQ_EMPTY(&wanted_branches))
1532 option_conflict('l', 'b');
1533 if (fetch_all_branches)
1534 option_conflict('l', 'a');
1535 if (mirror_references)
1536 option_conflict('l', 'm');
1537 if (!TAILQ_EMPTY(&wanted_refs))
1538 option_conflict('l', 'R');
1550 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1555 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1556 host, port ? ":" : "", port ? port : "",
1557 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1558 error = got_error_from_errno("asprintf");
1562 if (strcmp(proto, "git") == 0) {
1564 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 "sendfd dns inet unveil", NULL) == -1)
1568 } else if (strcmp(proto, "git+ssh") == 0 ||
1569 strcmp(proto, "ssh") == 0) {
1571 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1572 "sendfd unveil", NULL) == -1)
1575 } else if (strcmp(proto, "http") == 0 ||
1576 strcmp(proto, "git+http") == 0) {
1577 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1580 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1583 if (dirname == NULL) {
1584 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1585 error = got_error_from_errno("asprintf");
1588 repo_path = default_destdir;
1590 repo_path = dirname;
1592 if (!list_refs_only) {
1593 error = got_path_mkdir(repo_path);
1595 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1596 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1598 if (!got_path_dir_is_empty(repo_path)) {
1599 error = got_error_path(repo_path,
1600 GOT_ERR_DIR_NOT_EMPTY);
1605 error = got_dial_apply_unveil(proto);
1609 error = apply_unveil(repo_path, 0, NULL);
1614 printf("Connecting to %s%s%s\n", host,
1615 port ? ":" : "", port ? port : "");
1617 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 server_path, verbosity);
1622 if (!list_refs_only) {
1623 error = got_repo_init(repo_path);
1626 error = got_repo_open(&repo, repo_path, NULL);
1631 fpa.last_scaled_size[0] = '\0';
1632 fpa.last_p_indexed = -1;
1633 fpa.last_p_resolved = -1;
1634 fpa.verbosity = verbosity;
1635 fpa.create_configs = 1;
1636 fpa.configs_created = 0;
1638 fpa.config_info.symrefs = &symrefs;
1639 fpa.config_info.wanted_branches = &wanted_branches;
1640 fpa.config_info.wanted_refs = &wanted_refs;
1641 fpa.config_info.proto = proto;
1642 fpa.config_info.host = host;
1643 fpa.config_info.port = port;
1644 fpa.config_info.remote_repo_path = server_path;
1645 fpa.config_info.git_url = git_url;
1646 fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 fpa.config_info.mirror_references = mirror_references;
1648 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 fetch_all_branches, &wanted_branches, &wanted_refs,
1651 list_refs_only, verbosity, fetchfd, repo,
1652 fetch_progress, &fpa);
1656 if (list_refs_only) {
1657 error = list_remote_refs(&symrefs, &refs);
1661 if (pack_hash == NULL) {
1662 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1663 "server sent an empty pack file");
1666 error = got_object_id_str(&id_str, pack_hash);
1670 printf("\nFetched %s.pack\n", id_str);
1673 /* Set up references provided with the pack file. */
1674 TAILQ_FOREACH(pe, &refs, entry) {
1675 const char *refname = pe->path;
1676 struct got_object_id *id = pe->data;
1677 char *remote_refname;
1679 if (is_wanted_ref(&wanted_refs, refname) &&
1680 !mirror_references) {
1681 error = create_wanted_ref(refname, id,
1682 GOT_FETCH_DEFAULT_REMOTE_NAME,
1683 verbosity - 1, repo);
1689 error = create_ref(refname, id, verbosity - 1, repo);
1693 if (mirror_references)
1696 if (strncmp("refs/heads/", refname, 11) != 0)
1699 if (asprintf(&remote_refname,
1700 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1701 refname + 11) == -1) {
1702 error = got_error_from_errno("asprintf");
1705 error = create_ref(remote_refname, id, verbosity - 1, repo);
1706 free(remote_refname);
1711 /* Set the HEAD reference if the server provided one. */
1712 TAILQ_FOREACH(pe, &symrefs, entry) {
1713 struct got_reference *target_ref;
1714 const char *refname = pe->path;
1715 const char *target = pe->data;
1716 char *remote_refname = NULL, *remote_target = NULL;
1718 if (strcmp(refname, GOT_REF_HEAD) != 0)
1721 error = got_ref_open(&target_ref, repo, target, 0);
1723 if (error->code == GOT_ERR_NOT_REF) {
1730 error = create_symref(refname, target_ref, verbosity, repo);
1731 got_ref_close(target_ref);
1735 if (mirror_references)
1738 if (strncmp("refs/heads/", target, 11) != 0)
1741 if (asprintf(&remote_refname,
1742 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 error = got_error_from_errno("asprintf");
1747 if (asprintf(&remote_target,
1748 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1749 target + 11) == -1) {
1750 error = got_error_from_errno("asprintf");
1751 free(remote_refname);
1754 error = got_ref_open(&target_ref, repo, remote_target, 0);
1756 free(remote_refname);
1757 free(remote_target);
1758 if (error->code == GOT_ERR_NOT_REF) {
1764 error = create_symref(remote_refname, target_ref,
1765 verbosity - 1, repo);
1766 free(remote_refname);
1767 free(remote_target);
1768 got_ref_close(target_ref);
1774 * We failed to set the HEAD reference. If we asked for
1775 * a set of wanted branches use the first of one of those
1776 * which could be fetched instead.
1778 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1779 const char *target = pe->path;
1780 struct got_reference *target_ref;
1782 error = got_ref_open(&target_ref, repo, target, 0);
1784 if (error->code == GOT_ERR_NOT_REF) {
1791 error = create_symref(GOT_REF_HEAD, target_ref,
1793 got_ref_close(target_ref);
1801 printf("Created %s repository '%s'\n",
1802 mirror_references ? "mirrored" : "cloned", repo_path);
1805 if (kill(fetchpid, SIGTERM) == -1)
1806 error = got_error_from_errno("kill");
1807 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1808 error = got_error_from_errno("waitpid");
1810 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1811 error = got_error_from_errno("close");
1813 const struct got_error *close_err = got_repo_close(repo);
1817 TAILQ_FOREACH(pe, &refs, entry) {
1818 free((void *)pe->path);
1821 got_pathlist_free(&refs);
1822 TAILQ_FOREACH(pe, &symrefs, entry) {
1823 free((void *)pe->path);
1826 got_pathlist_free(&symrefs);
1827 got_pathlist_free(&wanted_branches);
1828 got_pathlist_free(&wanted_refs);
1835 free(default_destdir);
1840 static const struct got_error *
1841 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1842 int replace_tags, int verbosity, struct got_repository *repo)
1844 const struct got_error *err = NULL;
1845 char *new_id_str = NULL;
1846 struct got_object_id *old_id = NULL;
1848 err = got_object_id_str(&new_id_str, new_id);
1852 if (!replace_tags &&
1853 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1854 err = got_ref_resolve(&old_id, repo, ref);
1857 if (got_object_id_cmp(old_id, new_id) == 0)
1859 if (verbosity >= 0) {
1860 printf("Rejecting update of existing tag %s: %s\n",
1861 got_ref_get_name(ref), new_id_str);
1866 if (got_ref_is_symbolic(ref)) {
1867 if (verbosity >= 0) {
1868 printf("Replacing reference %s: %s\n",
1869 got_ref_get_name(ref),
1870 got_ref_get_symref_target(ref));
1872 err = got_ref_change_symref_to_ref(ref, new_id);
1875 err = got_ref_write(ref, repo);
1879 err = got_ref_resolve(&old_id, repo, ref);
1882 if (got_object_id_cmp(old_id, new_id) == 0)
1885 err = got_ref_change_ref(ref, new_id);
1888 err = got_ref_write(ref, repo);
1894 printf("Updated %s: %s\n", got_ref_get_name(ref),
1902 static const struct got_error *
1903 update_symref(const char *refname, struct got_reference *target_ref,
1904 int verbosity, struct got_repository *repo)
1906 const struct got_error *err = NULL, *unlock_err;
1907 struct got_reference *symref;
1908 int symref_is_locked = 0;
1910 err = got_ref_open(&symref, repo, refname, 1);
1912 if (err->code != GOT_ERR_NOT_REF)
1914 err = got_ref_alloc_symref(&symref, refname, target_ref);
1918 err = got_ref_write(symref, repo);
1923 printf("Created reference %s: %s\n",
1924 got_ref_get_name(symref),
1925 got_ref_get_symref_target(symref));
1927 symref_is_locked = 1;
1929 if (strcmp(got_ref_get_symref_target(symref),
1930 got_ref_get_name(target_ref)) == 0)
1933 err = got_ref_change_symref(symref,
1934 got_ref_get_name(target_ref));
1938 err = got_ref_write(symref, repo);
1943 printf("Updated %s: %s\n", got_ref_get_name(symref),
1944 got_ref_get_symref_target(symref));
1948 if (symref_is_locked) {
1949 unlock_err = got_ref_unlock(symref);
1950 if (unlock_err && err == NULL)
1953 got_ref_close(symref);
1960 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1961 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1962 "[remote-repository-name]\n",
1967 static const struct got_error *
1968 delete_missing_ref(struct got_reference *ref,
1969 int verbosity, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct got_object_id *id = NULL;
1973 char *id_str = NULL;
1975 if (got_ref_is_symbolic(ref)) {
1976 err = got_ref_delete(ref, repo);
1979 if (verbosity >= 0) {
1980 printf("Deleted %s: %s\n",
1981 got_ref_get_name(ref),
1982 got_ref_get_symref_target(ref));
1985 err = got_ref_resolve(&id, repo, ref);
1988 err = got_object_id_str(&id_str, id);
1992 err = got_ref_delete(ref, repo);
1995 if (verbosity >= 0) {
1996 printf("Deleted %s: %s\n",
1997 got_ref_get_name(ref), id_str);
2006 static const struct got_error *
2007 delete_missing_refs(struct got_pathlist_head *their_refs,
2008 struct got_pathlist_head *their_symrefs,
2009 const struct got_remote_repo *remote,
2010 int verbosity, struct got_repository *repo)
2012 const struct got_error *err = NULL, *unlock_err;
2013 struct got_reflist_head my_refs;
2014 struct got_reflist_entry *re;
2015 struct got_pathlist_entry *pe;
2016 char *remote_namespace = NULL;
2017 char *local_refname = NULL;
2019 TAILQ_INIT(&my_refs);
2021 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2023 return got_error_from_errno("asprintf");
2025 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2029 TAILQ_FOREACH(re, &my_refs, entry) {
2030 const char *refname = got_ref_get_name(re->ref);
2031 const char *their_refname;
2033 if (remote->mirror_references) {
2034 their_refname = refname;
2036 if (strncmp(refname, remote_namespace,
2037 strlen(remote_namespace)) == 0) {
2038 if (strcmp(refname + strlen(remote_namespace),
2041 if (asprintf(&local_refname, "refs/heads/%s",
2042 refname + strlen(remote_namespace)) == -1) {
2043 err = got_error_from_errno("asprintf");
2046 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2049 their_refname = local_refname;
2052 TAILQ_FOREACH(pe, their_refs, entry) {
2053 if (strcmp(their_refname, pe->path) == 0)
2059 TAILQ_FOREACH(pe, their_symrefs, entry) {
2060 if (strcmp(their_refname, pe->path) == 0)
2066 err = delete_missing_ref(re->ref, verbosity, repo);
2070 if (local_refname) {
2071 struct got_reference *ref;
2072 err = got_ref_open(&ref, repo, local_refname, 1);
2074 if (err->code != GOT_ERR_NOT_REF)
2076 free(local_refname);
2077 local_refname = NULL;
2080 err = delete_missing_ref(ref, verbosity, repo);
2083 unlock_err = got_ref_unlock(ref);
2085 if (unlock_err && err == NULL) {
2090 free(local_refname);
2091 local_refname = NULL;
2095 free(remote_namespace);
2096 free(local_refname);
2100 static const struct got_error *
2101 update_wanted_ref(const char *refname, struct got_object_id *id,
2102 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2104 const struct got_error *err, *unlock_err;
2105 char *remote_refname;
2106 struct got_reference *ref;
2108 if (strncmp("refs/", refname, 5) == 0)
2111 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2112 remote_repo_name, refname) == -1)
2113 return got_error_from_errno("asprintf");
2115 err = got_ref_open(&ref, repo, remote_refname, 1);
2117 if (err->code != GOT_ERR_NOT_REF)
2119 err = create_ref(remote_refname, id, verbosity, repo);
2121 err = update_ref(ref, id, 0, verbosity, repo);
2122 unlock_err = got_ref_unlock(ref);
2123 if (unlock_err && err == NULL)
2128 free(remote_refname);
2132 static const struct got_error *
2133 delete_ref(struct got_repository *repo, struct got_reference *ref)
2135 const struct got_error *err = NULL;
2136 struct got_object_id *id = NULL;
2137 char *id_str = NULL;
2140 if (got_ref_is_symbolic(ref)) {
2141 target = got_ref_get_symref_target(ref);
2143 err = got_ref_resolve(&id, repo, ref);
2146 err = got_object_id_str(&id_str, id);
2152 err = got_ref_delete(ref, repo);
2156 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2163 static const struct got_error *
2164 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2166 const struct got_error *err = NULL;
2167 struct got_reflist_head refs;
2168 struct got_reflist_entry *re;
2173 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2174 err = got_error_from_errno("asprintf");
2177 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2181 TAILQ_FOREACH(re, &refs, entry)
2182 delete_ref(repo, re->ref);
2184 got_ref_list_free(&refs);
2188 static const struct got_error *
2189 cmd_fetch(int argc, char *argv[])
2191 const struct got_error *error = NULL, *unlock_err;
2192 char *cwd = NULL, *repo_path = NULL;
2193 const char *remote_name;
2194 char *proto = NULL, *host = NULL, *port = NULL;
2195 char *repo_name = NULL, *server_path = NULL;
2196 const struct got_remote_repo *remotes, *remote = NULL;
2198 char *id_str = NULL;
2199 struct got_repository *repo = NULL;
2200 struct got_worktree *worktree = NULL;
2201 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2202 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2203 struct got_pathlist_entry *pe;
2204 struct got_object_id *pack_hash = NULL;
2205 int i, ch, fetchfd = -1, fetchstatus;
2206 pid_t fetchpid = -1;
2207 struct got_fetch_progress_arg fpa;
2208 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2209 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2212 TAILQ_INIT(&symrefs);
2213 TAILQ_INIT(&wanted_branches);
2214 TAILQ_INIT(&wanted_refs);
2216 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2219 fetch_all_branches = 1;
2222 error = got_pathlist_append(&wanted_branches,
2234 repo_path = realpath(optarg, NULL);
2235 if (repo_path == NULL)
2236 return got_error_from_errno2("realpath",
2238 got_path_strip_trailing_slashes(repo_path);
2246 else if (verbosity < 3)
2253 error = got_pathlist_append(&wanted_refs,
2269 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2270 option_conflict('a', 'b');
2271 if (list_refs_only) {
2272 if (!TAILQ_EMPTY(&wanted_branches))
2273 option_conflict('l', 'b');
2274 if (fetch_all_branches)
2275 option_conflict('l', 'a');
2277 option_conflict('l', 'd');
2279 option_conflict('l', 'X');
2281 if (delete_remote) {
2282 if (fetch_all_branches)
2283 option_conflict('X', 'a');
2284 if (!TAILQ_EMPTY(&wanted_branches))
2285 option_conflict('X', 'b');
2287 option_conflict('X', 'd');
2289 option_conflict('X', 't');
2290 if (!TAILQ_EMPTY(&wanted_refs))
2291 option_conflict('X', 'R');
2296 errx(1, "-X option requires a remote name");
2297 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2298 } else if (argc == 1)
2299 remote_name = argv[0];
2303 cwd = getcwd(NULL, 0);
2305 error = got_error_from_errno("getcwd");
2309 if (repo_path == NULL) {
2310 error = got_worktree_open(&worktree, cwd);
2311 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2317 strdup(got_worktree_get_repo_path(worktree));
2318 if (repo_path == NULL)
2319 error = got_error_from_errno("strdup");
2323 repo_path = strdup(cwd);
2324 if (repo_path == NULL) {
2325 error = got_error_from_errno("strdup");
2331 error = got_repo_open(&repo, repo_path, NULL);
2335 if (delete_remote) {
2336 error = delete_refs_for_remote(repo, remote_name);
2337 goto done; /* nothing else to do */
2341 worktree_conf = got_worktree_get_gotconfig(worktree);
2342 if (worktree_conf) {
2343 got_gotconfig_get_remotes(&nremotes, &remotes,
2345 for (i = 0; i < nremotes; i++) {
2346 if (strcmp(remotes[i].name, remote_name) == 0) {
2347 remote = &remotes[i];
2353 if (remote == NULL) {
2354 repo_conf = got_repo_get_gotconfig(repo);
2356 got_gotconfig_get_remotes(&nremotes, &remotes,
2358 for (i = 0; i < nremotes; i++) {
2359 if (strcmp(remotes[i].name, remote_name) == 0) {
2360 remote = &remotes[i];
2366 if (remote == NULL) {
2367 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2368 for (i = 0; i < nremotes; i++) {
2369 if (strcmp(remotes[i].name, remote_name) == 0) {
2370 remote = &remotes[i];
2375 if (remote == NULL) {
2376 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2380 if (TAILQ_EMPTY(&wanted_branches)) {
2381 if (!fetch_all_branches)
2382 fetch_all_branches = remote->fetch_all_branches;
2383 for (i = 0; i < remote->nfetch_branches; i++) {
2384 got_pathlist_append(&wanted_branches,
2385 remote->fetch_branches[i], NULL);
2388 if (TAILQ_EMPTY(&wanted_refs)) {
2389 for (i = 0; i < remote->nfetch_refs; i++) {
2390 got_pathlist_append(&wanted_refs,
2391 remote->fetch_refs[i], NULL);
2395 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2396 &repo_name, remote->fetch_url);
2400 if (strcmp(proto, "git") == 0) {
2402 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2403 "sendfd dns inet unveil", NULL) == -1)
2406 } else if (strcmp(proto, "git+ssh") == 0 ||
2407 strcmp(proto, "ssh") == 0) {
2409 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2410 "sendfd unveil", NULL) == -1)
2413 } else if (strcmp(proto, "http") == 0 ||
2414 strcmp(proto, "git+http") == 0) {
2415 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2418 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2422 error = got_dial_apply_unveil(proto);
2426 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2431 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2432 port ? ":" : "", port ? port : "");
2434 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2435 server_path, verbosity);
2439 fpa.last_scaled_size[0] = '\0';
2440 fpa.last_p_indexed = -1;
2441 fpa.last_p_resolved = -1;
2442 fpa.verbosity = verbosity;
2444 fpa.create_configs = 0;
2445 fpa.configs_created = 0;
2446 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2447 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2448 remote->mirror_references, fetch_all_branches, &wanted_branches,
2449 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2450 fetch_progress, &fpa);
2454 if (list_refs_only) {
2455 error = list_remote_refs(&symrefs, &refs);
2459 if (pack_hash == NULL) {
2461 printf("Already up-to-date\n");
2462 } else if (verbosity >= 0) {
2463 error = got_object_id_str(&id_str, pack_hash);
2466 printf("\nFetched %s.pack\n", id_str);
2471 /* Update references provided with the pack file. */
2472 TAILQ_FOREACH(pe, &refs, entry) {
2473 const char *refname = pe->path;
2474 struct got_object_id *id = pe->data;
2475 struct got_reference *ref;
2476 char *remote_refname;
2478 if (is_wanted_ref(&wanted_refs, refname) &&
2479 !remote->mirror_references) {
2480 error = update_wanted_ref(refname, id,
2481 remote->name, verbosity, repo);
2487 if (remote->mirror_references ||
2488 strncmp("refs/tags/", refname, 10) == 0) {
2489 error = got_ref_open(&ref, repo, refname, 1);
2491 if (error->code != GOT_ERR_NOT_REF)
2493 error = create_ref(refname, id, verbosity,
2498 error = update_ref(ref, id, replace_tags,
2500 unlock_err = got_ref_unlock(ref);
2501 if (unlock_err && error == NULL)
2507 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2508 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2509 remote_name, refname + 11) == -1) {
2510 error = got_error_from_errno("asprintf");
2514 error = got_ref_open(&ref, repo, remote_refname, 1);
2516 if (error->code != GOT_ERR_NOT_REF)
2518 error = create_ref(remote_refname, id,
2523 error = update_ref(ref, id, replace_tags,
2525 unlock_err = got_ref_unlock(ref);
2526 if (unlock_err && error == NULL)
2533 /* Also create a local branch if none exists yet. */
2534 error = got_ref_open(&ref, repo, refname, 1);
2536 if (error->code != GOT_ERR_NOT_REF)
2538 error = create_ref(refname, id, verbosity,
2543 unlock_err = got_ref_unlock(ref);
2544 if (unlock_err && error == NULL)
2551 error = delete_missing_refs(&refs, &symrefs, remote,
2557 if (!remote->mirror_references) {
2558 /* Update remote HEAD reference if the server provided one. */
2559 TAILQ_FOREACH(pe, &symrefs, entry) {
2560 struct got_reference *target_ref;
2561 const char *refname = pe->path;
2562 const char *target = pe->data;
2563 char *remote_refname = NULL, *remote_target = NULL;
2565 if (strcmp(refname, GOT_REF_HEAD) != 0)
2568 if (strncmp("refs/heads/", target, 11) != 0)
2571 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2572 remote->name, refname) == -1) {
2573 error = got_error_from_errno("asprintf");
2576 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2577 remote->name, target + 11) == -1) {
2578 error = got_error_from_errno("asprintf");
2579 free(remote_refname);
2583 error = got_ref_open(&target_ref, repo, remote_target,
2586 free(remote_refname);
2587 free(remote_target);
2588 if (error->code == GOT_ERR_NOT_REF) {
2594 error = update_symref(remote_refname, target_ref,
2596 free(remote_refname);
2597 free(remote_target);
2598 got_ref_close(target_ref);
2605 if (kill(fetchpid, SIGTERM) == -1)
2606 error = got_error_from_errno("kill");
2607 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2608 error = got_error_from_errno("waitpid");
2610 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2611 error = got_error_from_errno("close");
2613 const struct got_error *close_err = got_repo_close(repo);
2618 got_worktree_close(worktree);
2619 TAILQ_FOREACH(pe, &refs, entry) {
2620 free((void *)pe->path);
2623 got_pathlist_free(&refs);
2624 TAILQ_FOREACH(pe, &symrefs, entry) {
2625 free((void *)pe->path);
2628 got_pathlist_free(&symrefs);
2629 got_pathlist_free(&wanted_branches);
2630 got_pathlist_free(&wanted_refs);
2645 usage_checkout(void)
2647 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2648 "[-p prefix] [-q] repository-path [worktree-path]\n",
2654 show_worktree_base_ref_warning(void)
2656 fprintf(stderr, "%s: warning: could not create a reference "
2657 "to the work tree's base commit; the commit could be "
2658 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2659 "repository writable and running 'got update' will prevent this\n",
2663 struct got_checkout_progress_arg {
2664 const char *worktree_path;
2665 int had_base_commit_ref_error;
2669 static const struct got_error *
2670 checkout_progress(void *arg, unsigned char status, const char *path)
2672 struct got_checkout_progress_arg *a = arg;
2674 /* Base commit bump happens silently. */
2675 if (status == GOT_STATUS_BUMP_BASE)
2678 if (status == GOT_STATUS_BASE_REF_ERR) {
2679 a->had_base_commit_ref_error = 1;
2683 while (path[0] == '/')
2686 if (a->verbosity >= 0)
2687 printf("%c %s/%s\n", status, a->worktree_path, path);
2692 static const struct got_error *
2693 check_cancelled(void *arg)
2695 if (sigint_received || sigpipe_received)
2696 return got_error(GOT_ERR_CANCELLED);
2700 static const struct got_error *
2701 check_linear_ancestry(struct got_object_id *commit_id,
2702 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2703 struct got_repository *repo)
2705 const struct got_error *err = NULL;
2706 struct got_object_id *yca_id;
2708 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2709 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2714 return got_error(GOT_ERR_ANCESTRY);
2717 * Require a straight line of history between the target commit
2718 * and the work tree's base commit.
2720 * Non-linear situations such as this require a rebase:
2722 * (commit) D F (base_commit)
2730 * 'got update' only handles linear cases:
2731 * Update forwards in time: A (base/yca) - B - C - D (commit)
2732 * Update backwards in time: D (base) - C - B - A (commit/yca)
2734 if (allow_forwards_in_time_only) {
2735 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2736 return got_error(GOT_ERR_ANCESTRY);
2737 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2738 got_object_id_cmp(base_commit_id, yca_id) != 0)
2739 return got_error(GOT_ERR_ANCESTRY);
2745 static const struct got_error *
2746 check_same_branch(struct got_object_id *commit_id,
2747 struct got_reference *head_ref, struct got_object_id *yca_id,
2748 struct got_repository *repo)
2750 const struct got_error *err = NULL;
2751 struct got_commit_graph *graph = NULL;
2752 struct got_object_id *head_commit_id = NULL;
2753 int is_same_branch = 0;
2755 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2759 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2763 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2768 err = got_commit_graph_open(&graph, "/", 1);
2772 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2773 check_cancelled, NULL);
2778 struct got_object_id *id;
2779 err = got_commit_graph_iter_next(&id, graph, repo,
2780 check_cancelled, NULL);
2782 if (err->code == GOT_ERR_ITER_COMPLETED)
2788 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2790 if (got_object_id_cmp(id, commit_id) == 0) {
2798 got_commit_graph_close(graph);
2799 free(head_commit_id);
2800 if (!err && !is_same_branch)
2801 err = got_error(GOT_ERR_ANCESTRY);
2805 static const struct got_error *
2806 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2808 static char msg[512];
2809 const char *branch_name;
2811 if (got_ref_is_symbolic(ref))
2812 branch_name = got_ref_get_symref_target(ref);
2814 branch_name = got_ref_get_name(ref);
2816 if (strncmp("refs/heads/", branch_name, 11) == 0)
2819 snprintf(msg, sizeof(msg),
2820 "target commit is not contained in branch '%s'; "
2821 "the branch to use must be specified with -b; "
2822 "if necessary a new branch can be created for "
2823 "this commit with 'got branch -c %s BRANCH_NAME'",
2824 branch_name, commit_id_str);
2826 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2829 static const struct got_error *
2830 cmd_checkout(int argc, char *argv[])
2832 const struct got_error *error = NULL;
2833 struct got_repository *repo = NULL;
2834 struct got_reference *head_ref = NULL, *ref = NULL;
2835 struct got_worktree *worktree = NULL;
2836 char *repo_path = NULL;
2837 char *worktree_path = NULL;
2838 const char *path_prefix = "";
2839 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2840 char *commit_id_str = NULL;
2841 struct got_object_id *commit_id = NULL;
2843 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2844 struct got_pathlist_head paths;
2845 struct got_checkout_progress_arg cpa;
2849 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2852 branch_name = optarg;
2855 commit_id_str = strdup(optarg);
2856 if (commit_id_str == NULL)
2857 return got_error_from_errno("strdup");
2863 path_prefix = optarg;
2878 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2879 "unveil", NULL) == -1)
2883 char *base, *dotgit;
2885 repo_path = realpath(argv[0], NULL);
2886 if (repo_path == NULL)
2887 return got_error_from_errno2("realpath", argv[0]);
2888 cwd = getcwd(NULL, 0);
2890 error = got_error_from_errno("getcwd");
2897 error = got_path_basename(&base, path);
2900 dotgit = strstr(base, ".git");
2903 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2904 error = got_error_from_errno("asprintf");
2909 } else if (argc == 2) {
2910 repo_path = realpath(argv[0], NULL);
2911 if (repo_path == NULL) {
2912 error = got_error_from_errno2("realpath", argv[0]);
2915 worktree_path = realpath(argv[1], NULL);
2916 if (worktree_path == NULL) {
2917 if (errno != ENOENT) {
2918 error = got_error_from_errno2("realpath",
2922 worktree_path = strdup(argv[1]);
2923 if (worktree_path == NULL) {
2924 error = got_error_from_errno("strdup");
2931 got_path_strip_trailing_slashes(repo_path);
2932 got_path_strip_trailing_slashes(worktree_path);
2934 error = got_repo_open(&repo, repo_path, NULL);
2938 /* Pre-create work tree path for unveil(2) */
2939 error = got_path_mkdir(worktree_path);
2941 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2942 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2944 if (!allow_nonempty &&
2945 !got_path_dir_is_empty(worktree_path)) {
2946 error = got_error_path(worktree_path,
2947 GOT_ERR_DIR_NOT_EMPTY);
2952 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2956 error = got_ref_open(&head_ref, repo, branch_name, 0);
2960 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2961 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2964 error = got_worktree_open(&worktree, worktree_path);
2968 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2972 if (!same_path_prefix) {
2973 error = got_error(GOT_ERR_PATH_PREFIX);
2977 if (commit_id_str) {
2978 struct got_reflist_head refs;
2980 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2984 error = got_repo_match_object_id(&commit_id, NULL,
2985 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2986 got_ref_list_free(&refs);
2989 error = check_linear_ancestry(commit_id,
2990 got_worktree_get_base_commit_id(worktree), 0, repo);
2991 if (error != NULL) {
2993 if (error->code == GOT_ERR_ANCESTRY) {
2994 error = checkout_ancestry_error(
2995 head_ref, commit_id_str);
2999 error = check_same_branch(commit_id, head_ref, NULL, repo);
3001 if (error->code == GOT_ERR_ANCESTRY) {
3002 error = checkout_ancestry_error(
3003 head_ref, commit_id_str);
3007 error = got_worktree_set_base_commit_id(worktree, repo,
3011 /* Expand potentially abbreviated commit ID string. */
3012 free(commit_id_str);
3013 error = got_object_id_str(&commit_id_str, commit_id);
3017 commit_id = got_object_id_dup(
3018 got_worktree_get_base_commit_id(worktree));
3019 if (commit_id == NULL) {
3020 error = got_error_from_errno("got_object_id_dup");
3023 error = got_object_id_str(&commit_id_str, commit_id);
3028 error = got_pathlist_append(&paths, "", NULL);
3031 cpa.worktree_path = worktree_path;
3032 cpa.had_base_commit_ref_error = 0;
3033 cpa.verbosity = verbosity;
3034 error = got_worktree_checkout_files(worktree, &paths, repo,
3035 checkout_progress, &cpa, check_cancelled, NULL);
3039 if (got_ref_is_symbolic(head_ref)) {
3040 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3043 refname = got_ref_get_name(ref);
3045 refname = got_ref_get_name(head_ref);
3046 printf("Checked out %s: %s\n", refname, commit_id_str);
3047 printf("Now shut up and hack\n");
3048 if (cpa.had_base_commit_ref_error)
3049 show_worktree_base_ref_warning();
3052 got_ref_close(head_ref);
3055 got_pathlist_free(&paths);
3056 free(commit_id_str);
3059 free(worktree_path);
3064 struct got_update_progress_arg {
3076 print_update_progress_stats(struct got_update_progress_arg *upa)
3078 if (!upa->did_something)
3081 if (upa->conflicts > 0)
3082 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3083 if (upa->obstructed > 0)
3084 printf("File paths obstructed by a non-regular file: %d\n",
3086 if (upa->not_updated > 0)
3087 printf("Files not updated because of existing merge "
3088 "conflicts: %d\n", upa->not_updated);
3092 * The meaning of some status codes differs between merge-style operations and
3093 * update operations. For example, the ! status code means "file was missing"
3094 * if changes were merged into the work tree, and "missing file was restored"
3095 * if the work tree was updated. This function should be used by any operation
3096 * which merges changes into the work tree without updating the work tree.
3099 print_merge_progress_stats(struct got_update_progress_arg *upa)
3101 if (!upa->did_something)
3104 if (upa->conflicts > 0)
3105 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3106 if (upa->obstructed > 0)
3107 printf("File paths obstructed by a non-regular file: %d\n",
3109 if (upa->missing > 0)
3110 printf("Files which had incoming changes but could not be "
3111 "found in the work tree: %d\n", upa->missing);
3112 if (upa->not_deleted > 0)
3113 printf("Files not deleted due to differences in deleted "
3114 "content: %d\n", upa->not_deleted);
3115 if (upa->unversioned > 0)
3116 printf("Files not merged because an unversioned file was "
3117 "found in the work tree: %d\n", upa->unversioned);
3123 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3129 static const struct got_error *
3130 update_progress(void *arg, unsigned char status, const char *path)
3132 struct got_update_progress_arg *upa = arg;
3134 if (status == GOT_STATUS_EXISTS ||
3135 status == GOT_STATUS_BASE_REF_ERR)
3138 upa->did_something = 1;
3140 /* Base commit bump happens silently. */
3141 if (status == GOT_STATUS_BUMP_BASE)
3144 if (status == GOT_STATUS_CONFLICT)
3146 if (status == GOT_STATUS_OBSTRUCTED)
3148 if (status == GOT_STATUS_CANNOT_UPDATE)
3150 if (status == GOT_STATUS_MISSING)
3152 if (status == GOT_STATUS_CANNOT_DELETE)
3154 if (status == GOT_STATUS_UNVERSIONED)
3157 while (path[0] == '/')
3159 if (upa->verbosity >= 0)
3160 printf("%c %s\n", status, path);
3165 static const struct got_error *
3166 switch_head_ref(struct got_reference *head_ref,
3167 struct got_object_id *commit_id, struct got_worktree *worktree,
3168 struct got_repository *repo)
3170 const struct got_error *err = NULL;
3172 int ref_has_moved = 0;
3174 /* Trivial case: switching between two different references. */
3175 if (strcmp(got_ref_get_name(head_ref),
3176 got_worktree_get_head_ref_name(worktree)) != 0) {
3177 printf("Switching work tree from %s to %s\n",
3178 got_worktree_get_head_ref_name(worktree),
3179 got_ref_get_name(head_ref));
3180 return got_worktree_set_head_ref(worktree, head_ref);
3183 err = check_linear_ancestry(commit_id,
3184 got_worktree_get_base_commit_id(worktree), 0, repo);
3186 if (err->code != GOT_ERR_ANCESTRY)
3193 /* Switching to a rebased branch with the same reference name. */
3194 err = got_object_id_str(&base_id_str,
3195 got_worktree_get_base_commit_id(worktree));
3198 printf("Reference %s now points at a different branch\n",
3199 got_worktree_get_head_ref_name(worktree));
3200 printf("Switching work tree from %s to %s\n", base_id_str,
3201 got_worktree_get_head_ref_name(worktree));
3205 static const struct got_error *
3206 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3208 const struct got_error *err;
3211 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3215 return got_error(GOT_ERR_REBASING);
3217 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3221 return got_error(GOT_ERR_HISTEDIT_BUSY);
3226 static const struct got_error *
3227 check_merge_in_progress(struct got_worktree *worktree,
3228 struct got_repository *repo)
3230 const struct got_error *err;
3233 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3237 return got_error(GOT_ERR_MERGE_BUSY);
3242 static const struct got_error *
3243 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3244 char *argv[], struct got_worktree *worktree)
3246 const struct got_error *err = NULL;
3248 struct got_pathlist_entry *new;
3254 return got_error_from_errno("strdup");
3255 return got_pathlist_append(paths, path, NULL);
3258 for (i = 0; i < argc; i++) {
3259 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3262 err = got_pathlist_insert(&new, paths, path, NULL);
3263 if (err || new == NULL /* duplicate */) {
3273 static const struct got_error *
3274 wrap_not_worktree_error(const struct got_error *orig_err,
3275 const char *cmdname, const char *path)
3277 const struct got_error *err;
3278 struct got_repository *repo;
3279 static char msg[512];
3281 err = got_repo_open(&repo, path, NULL);
3285 snprintf(msg, sizeof(msg),
3286 "'got %s' needs a work tree in addition to a git repository\n"
3287 "Work trees can be checked out from this Git repository with "
3289 "The got(1) manual page contains more information.", cmdname);
3290 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3291 got_repo_close(repo);
3295 static const struct got_error *
3296 cmd_update(int argc, char *argv[])
3298 const struct got_error *error = NULL;
3299 struct got_repository *repo = NULL;
3300 struct got_worktree *worktree = NULL;
3301 char *worktree_path = NULL;
3302 struct got_object_id *commit_id = NULL;
3303 char *commit_id_str = NULL;
3304 const char *branch_name = NULL;
3305 struct got_reference *head_ref = NULL;
3306 struct got_pathlist_head paths;
3307 struct got_pathlist_entry *pe;
3308 int ch, verbosity = 0;
3309 struct got_update_progress_arg upa;
3313 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3316 branch_name = optarg;
3319 commit_id_str = strdup(optarg);
3320 if (commit_id_str == NULL)
3321 return got_error_from_errno("strdup");
3336 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3337 "unveil", NULL) == -1)
3340 worktree_path = getcwd(NULL, 0);
3341 if (worktree_path == NULL) {
3342 error = got_error_from_errno("getcwd");
3345 error = got_worktree_open(&worktree, worktree_path);
3347 if (error->code == GOT_ERR_NOT_WORKTREE)
3348 error = wrap_not_worktree_error(error, "update",
3353 error = check_rebase_or_histedit_in_progress(worktree);
3357 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3362 error = apply_unveil(got_repo_get_path(repo), 0,
3363 got_worktree_get_root_path(worktree));
3367 error = check_merge_in_progress(worktree, repo);
3371 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3375 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3376 got_worktree_get_head_ref_name(worktree), 0);
3379 if (commit_id_str == NULL) {
3380 error = got_ref_resolve(&commit_id, repo, head_ref);
3383 error = got_object_id_str(&commit_id_str, commit_id);
3387 struct got_reflist_head refs;
3389 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3393 error = got_repo_match_object_id(&commit_id, NULL,
3394 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3395 got_ref_list_free(&refs);
3396 free(commit_id_str);
3397 commit_id_str = NULL;
3400 error = got_object_id_str(&commit_id_str, commit_id);
3406 struct got_object_id *head_commit_id;
3407 TAILQ_FOREACH(pe, &paths, entry) {
3408 if (pe->path_len == 0)
3410 error = got_error_msg(GOT_ERR_BAD_PATH,
3411 "switching between branches requires that "
3412 "the entire work tree gets updated");
3415 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3418 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3420 free(head_commit_id);
3423 error = check_same_branch(commit_id, head_ref, NULL, repo);
3426 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3430 error = check_linear_ancestry(commit_id,
3431 got_worktree_get_base_commit_id(worktree), 0, repo);
3432 if (error != NULL) {
3433 if (error->code == GOT_ERR_ANCESTRY)
3434 error = got_error(GOT_ERR_BRANCH_MOVED);
3437 error = check_same_branch(commit_id, head_ref, NULL, repo);
3442 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3444 error = got_worktree_set_base_commit_id(worktree, repo,
3450 memset(&upa, 0, sizeof(upa));
3451 upa.verbosity = verbosity;
3452 error = got_worktree_checkout_files(worktree, &paths, repo,
3453 update_progress, &upa, check_cancelled, NULL);
3457 if (upa.did_something) {
3458 printf("Updated to %s: %s\n",
3459 got_worktree_get_head_ref_name(worktree), commit_id_str);
3461 printf("Already up-to-date\n");
3462 print_update_progress_stats(&upa);
3464 free(worktree_path);
3465 TAILQ_FOREACH(pe, &paths, entry)
3466 free((char *)pe->path);
3467 got_pathlist_free(&paths);
3469 free(commit_id_str);
3473 static const struct got_error *
3474 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3475 const char *path, int diff_context, int ignore_whitespace,
3476 int force_text_diff, struct got_repository *repo)
3478 const struct got_error *err = NULL;
3479 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3482 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3487 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3491 while (path[0] == '/')
3493 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3494 diff_context, ignore_whitespace, force_text_diff, stdout);
3497 got_object_blob_close(blob1);
3498 got_object_blob_close(blob2);
3502 static const struct got_error *
3503 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3504 const char *path, int diff_context, int ignore_whitespace,
3505 int force_text_diff, struct got_repository *repo)
3507 const struct got_error *err = NULL;
3508 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3509 struct got_diff_blob_output_unidiff_arg arg;
3512 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3517 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3521 arg.diff_context = diff_context;
3522 arg.ignore_whitespace = ignore_whitespace;
3523 arg.force_text_diff = force_text_diff;
3524 arg.outfile = stdout;
3525 arg.line_offsets = NULL;
3527 while (path[0] == '/')
3529 err = got_diff_tree(tree1, tree2, path, path, repo,
3530 got_diff_blob_output_unidiff, &arg, 1);
3533 got_object_tree_close(tree1);
3535 got_object_tree_close(tree2);
3539 static const struct got_error *
3540 get_changed_paths(struct got_pathlist_head *paths,
3541 struct got_commit_object *commit, struct got_repository *repo)
3543 const struct got_error *err = NULL;
3544 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3545 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3546 struct got_object_qid *qid;
3548 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3550 struct got_commit_object *pcommit;
3551 err = got_object_open_as_commit(&pcommit, repo,
3556 tree_id1 = got_object_id_dup(
3557 got_object_commit_get_tree_id(pcommit));
3558 if (tree_id1 == NULL) {
3559 got_object_commit_close(pcommit);
3560 return got_error_from_errno("got_object_id_dup");
3562 got_object_commit_close(pcommit);
3567 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3572 tree_id2 = got_object_commit_get_tree_id(commit);
3573 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3577 err = got_diff_tree(tree1, tree2, "", "", repo,
3578 got_diff_tree_collect_changed_paths, paths, 0);
3581 got_object_tree_close(tree1);
3583 got_object_tree_close(tree2);
3588 static const struct got_error *
3589 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3590 const char *path, int diff_context, struct got_repository *repo)
3592 const struct got_error *err = NULL;
3593 struct got_commit_object *pcommit = NULL;
3594 char *id_str1 = NULL, *id_str2 = NULL;
3595 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3596 struct got_object_qid *qid;
3598 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3600 err = got_object_open_as_commit(&pcommit, repo,
3606 if (path && path[0] != '\0') {
3608 err = got_object_id_by_path(&obj_id2, repo, id, path);
3611 err = got_object_id_str(&id_str2, obj_id2);
3617 err = got_object_id_by_path(&obj_id1, repo,
3620 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3625 err = got_object_id_str(&id_str1, obj_id1);
3632 err = got_object_get_type(&obj_type, repo, obj_id2);
3637 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3639 case GOT_OBJ_TYPE_BLOB:
3640 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3643 case GOT_OBJ_TYPE_TREE:
3644 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3648 err = got_error(GOT_ERR_OBJ_TYPE);
3654 obj_id2 = got_object_commit_get_tree_id(commit);
3655 err = got_object_id_str(&id_str2, obj_id2);
3659 obj_id1 = got_object_commit_get_tree_id(pcommit);
3660 err = got_object_id_str(&id_str1, obj_id1);
3664 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3666 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3673 got_object_commit_close(pcommit);
3678 get_datestr(time_t *time, char *datebuf)
3680 struct tm mytm, *tm;
3683 tm = gmtime_r(time, &mytm);
3686 s = asctime_r(tm, datebuf);
3689 p = strchr(s, '\n');
3695 static const struct got_error *
3696 match_logmsg(int *have_match, struct got_object_id *id,
3697 struct got_commit_object *commit, regex_t *regex)
3699 const struct got_error *err = NULL;
3700 regmatch_t regmatch;
3701 char *id_str = NULL, *logmsg = NULL;
3705 err = got_object_id_str(&id_str, id);
3709 err = got_object_commit_get_logmsg(&logmsg, commit);
3713 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3722 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3725 regmatch_t regmatch;
3726 struct got_pathlist_entry *pe;
3730 TAILQ_FOREACH(pe, changed_paths, entry) {
3731 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3738 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3740 static const struct got_error*
3741 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3742 struct got_object_id *id, struct got_repository *repo)
3744 static const struct got_error *err = NULL;
3745 struct got_reflist_entry *re;
3751 TAILQ_FOREACH(re, refs, entry) {
3752 struct got_tag_object *tag = NULL;
3753 struct got_object_id *ref_id;
3756 name = got_ref_get_name(re->ref);
3757 if (strcmp(name, GOT_REF_HEAD) == 0)
3759 if (strncmp(name, "refs/", 5) == 0)
3761 if (strncmp(name, "got/", 4) == 0)
3763 if (strncmp(name, "heads/", 6) == 0)
3765 if (strncmp(name, "remotes/", 8) == 0) {
3767 s = strstr(name, "/" GOT_REF_HEAD);
3768 if (s != NULL && s[strlen(s)] == '\0')
3771 err = got_ref_resolve(&ref_id, repo, re->ref);
3774 if (strncmp(name, "tags/", 5) == 0) {
3775 err = got_object_open_as_tag(&tag, repo, ref_id);
3777 if (err->code != GOT_ERR_OBJ_TYPE) {
3781 /* Ref points at something other than a tag. */
3786 cmp = got_object_id_cmp(tag ?
3787 got_object_tag_get_object_id(tag) : ref_id, id);
3790 got_object_tag_close(tag);
3794 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3795 s ? ", " : "", name) == -1) {
3796 err = got_error_from_errno("asprintf");
3807 static const struct got_error *
3808 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3809 struct got_repository *repo, const char *path,
3810 struct got_pathlist_head *changed_paths, int show_patch,
3811 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3812 const char *custom_refs_str)
3814 const struct got_error *err = NULL;
3815 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3817 time_t committer_time;
3818 const char *author, *committer;
3819 char *refs_str = NULL;
3821 err = got_object_id_str(&id_str, id);
3825 if (custom_refs_str == NULL) {
3826 struct got_reflist_head *refs;
3827 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3829 err = build_refs_str(&refs_str, refs, id, repo);
3835 printf(GOT_COMMIT_SEP_STR);
3836 if (custom_refs_str)
3837 printf("commit %s (%s)\n", id_str, custom_refs_str);
3839 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3840 refs_str ? refs_str : "", refs_str ? ")" : "");
3845 printf("from: %s\n", got_object_commit_get_author(commit));
3846 committer_time = got_object_commit_get_committer_time(commit);
3847 datestr = get_datestr(&committer_time, datebuf);
3849 printf("date: %s UTC\n", datestr);
3850 author = got_object_commit_get_author(commit);
3851 committer = got_object_commit_get_committer(commit);
3852 if (strcmp(author, committer) != 0)
3853 printf("via: %s\n", committer);
3854 if (got_object_commit_get_nparents(commit) > 1) {
3855 const struct got_object_id_queue *parent_ids;
3856 struct got_object_qid *qid;
3858 parent_ids = got_object_commit_get_parent_ids(commit);
3859 STAILQ_FOREACH(qid, parent_ids, entry) {
3860 err = got_object_id_str(&id_str, qid->id);
3863 printf("parent %d: %s\n", n++, id_str);
3869 err = got_object_commit_get_logmsg(&logmsg0, commit);
3875 line = strsep(&logmsg, "\n");
3877 printf(" %s\n", line);
3881 if (changed_paths) {
3882 struct got_pathlist_entry *pe;
3883 TAILQ_FOREACH(pe, changed_paths, entry) {
3884 struct got_diff_changed_path *cp = pe->data;
3885 printf(" %c %s\n", cp->status, pe->path);
3890 err = print_patch(commit, id, path, diff_context, repo);
3895 if (fflush(stdout) != 0 && err == NULL)
3896 err = got_error_from_errno("fflush");
3903 static const struct got_error *
3904 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3905 struct got_repository *repo, const char *path, int show_changed_paths,
3906 int show_patch, const char *search_pattern, int diff_context, int limit,
3907 int log_branches, int reverse_display_order,
3908 struct got_reflist_object_id_map *refs_idmap)
3910 const struct got_error *err;
3911 struct got_commit_graph *graph;
3914 struct got_object_id_queue reversed_commits;
3915 struct got_object_qid *qid;
3916 struct got_commit_object *commit;
3917 struct got_pathlist_head changed_paths;
3918 struct got_pathlist_entry *pe;
3920 STAILQ_INIT(&reversed_commits);
3921 TAILQ_INIT(&changed_paths);
3923 if (search_pattern && regcomp(®ex, search_pattern,
3924 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3925 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3927 err = got_commit_graph_open(&graph, path, !log_branches);
3930 err = got_commit_graph_iter_start(graph, root_id, repo,
3931 check_cancelled, NULL);
3935 struct got_object_id *id;
3937 if (sigint_received || sigpipe_received)
3940 err = got_commit_graph_iter_next(&id, graph, repo,
3941 check_cancelled, NULL);
3943 if (err->code == GOT_ERR_ITER_COMPLETED)
3950 err = got_object_open_as_commit(&commit, repo, id);
3954 if (show_changed_paths && !reverse_display_order) {
3955 err = get_changed_paths(&changed_paths, commit, repo);
3960 if (search_pattern) {
3961 err = match_logmsg(&have_match, id, commit, ®ex);
3963 got_object_commit_close(commit);
3966 if (have_match == 0 && show_changed_paths)
3967 match_changed_paths(&have_match,
3968 &changed_paths, ®ex);
3969 if (have_match == 0) {
3970 got_object_commit_close(commit);
3971 TAILQ_FOREACH(pe, &changed_paths, entry) {
3972 free((char *)pe->path);
3975 got_pathlist_free(&changed_paths);
3980 if (reverse_display_order) {
3981 err = got_object_qid_alloc(&qid, id);
3984 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3985 got_object_commit_close(commit);
3987 err = print_commit(commit, id, repo, path,
3988 show_changed_paths ? &changed_paths : NULL,
3989 show_patch, diff_context, refs_idmap, NULL);
3990 got_object_commit_close(commit);
3994 if ((limit && --limit == 0) ||
3995 (end_id && got_object_id_cmp(id, end_id) == 0))
3998 TAILQ_FOREACH(pe, &changed_paths, entry) {
3999 free((char *)pe->path);
4002 got_pathlist_free(&changed_paths);
4004 if (reverse_display_order) {
4005 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4006 err = got_object_open_as_commit(&commit, repo, qid->id);
4009 if (show_changed_paths) {
4010 err = get_changed_paths(&changed_paths,
4015 err = print_commit(commit, qid->id, repo, path,
4016 show_changed_paths ? &changed_paths : NULL,
4017 show_patch, diff_context, refs_idmap, NULL);
4018 got_object_commit_close(commit);
4021 TAILQ_FOREACH(pe, &changed_paths, entry) {
4022 free((char *)pe->path);
4025 got_pathlist_free(&changed_paths);
4029 while (!STAILQ_EMPTY(&reversed_commits)) {
4030 qid = STAILQ_FIRST(&reversed_commits);
4031 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4032 got_object_qid_free(qid);
4034 TAILQ_FOREACH(pe, &changed_paths, entry) {
4035 free((char *)pe->path);
4038 got_pathlist_free(&changed_paths);
4041 got_commit_graph_close(graph);
4048 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4049 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4050 "[-R] [path]\n", getprogname());
4055 get_default_log_limit(void)
4057 const char *got_default_log_limit;
4061 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4062 if (got_default_log_limit == NULL)
4064 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4070 static const struct got_error *
4071 cmd_log(int argc, char *argv[])
4073 const struct got_error *error;
4074 struct got_repository *repo = NULL;
4075 struct got_worktree *worktree = NULL;
4076 struct got_object_id *start_id = NULL, *end_id = NULL;
4077 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4078 const char *start_commit = NULL, *end_commit = NULL;
4079 const char *search_pattern = NULL;
4080 int diff_context = -1, ch;
4081 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4082 int reverse_display_order = 0;
4084 struct got_reflist_head refs;
4085 struct got_reflist_object_id_map *refs_idmap = NULL;
4090 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4096 limit = get_default_log_limit();
4098 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4104 show_changed_paths = 1;
4107 start_commit = optarg;
4110 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4113 err(1, "-C option %s", errstr);
4116 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4118 err(1, "-l option %s", errstr);
4124 repo_path = realpath(optarg, NULL);
4125 if (repo_path == NULL)
4126 return got_error_from_errno2("realpath",
4128 got_path_strip_trailing_slashes(repo_path);
4131 reverse_display_order = 1;
4134 search_pattern = optarg;
4137 end_commit = optarg;
4148 if (diff_context == -1)
4150 else if (!show_patch)
4151 errx(1, "-C requires -p");
4153 cwd = getcwd(NULL, 0);
4155 error = got_error_from_errno("getcwd");
4159 if (repo_path == NULL) {
4160 error = got_worktree_open(&worktree, cwd);
4161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4168 error = got_worktree_resolve_path(&path, worktree,
4173 path = strdup(argv[0]);
4175 error = got_error_from_errno("strdup");
4179 } else if (argc != 0)
4182 if (repo_path == NULL) {
4183 repo_path = worktree ?
4184 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4186 if (repo_path == NULL) {
4187 error = got_error_from_errno("strdup");
4191 error = got_repo_open(&repo, repo_path, NULL);
4195 error = apply_unveil(got_repo_get_path(repo), 1,
4196 worktree ? got_worktree_get_root_path(worktree) : NULL);
4200 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4204 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4208 if (start_commit == NULL) {
4209 struct got_reference *head_ref;
4210 struct got_commit_object *commit = NULL;
4211 error = got_ref_open(&head_ref, repo,
4212 worktree ? got_worktree_get_head_ref_name(worktree)
4216 error = got_ref_resolve(&start_id, repo, head_ref);
4217 got_ref_close(head_ref);
4220 error = got_object_open_as_commit(&commit, repo,
4224 got_object_commit_close(commit);
4226 error = got_repo_match_object_id(&start_id, NULL,
4227 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4231 if (end_commit != NULL) {
4232 error = got_repo_match_object_id(&end_id, NULL,
4233 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4240 * If a path was specified on the command line it was resolved
4241 * to a path in the work tree above. Prepend the work tree's
4242 * path prefix to obtain the corresponding in-repository path.
4246 prefix = got_worktree_get_path_prefix(worktree);
4247 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4248 (path[0] != '\0') ? "/" : "", path) == -1) {
4249 error = got_error_from_errno("asprintf");
4254 error = got_repo_map_path(&in_repo_path, repo,
4260 path = in_repo_path;
4263 error = print_commits(start_id, end_id, repo, path ? path : "",
4264 show_changed_paths, show_patch, search_pattern, diff_context,
4265 limit, log_branches, reverse_display_order, refs_idmap);
4271 got_worktree_close(worktree);
4273 const struct got_error *close_err = got_repo_close(repo);
4278 got_reflist_object_id_map_free(refs_idmap);
4279 got_ref_list_free(&refs);
4286 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4287 "[-r repository-path] [-s] [-w] [-P] "
4288 "[object1 object2 | path ...]\n", getprogname());
4292 struct print_diff_arg {
4293 struct got_repository *repo;
4294 struct got_worktree *worktree;
4299 int ignore_whitespace;
4300 int force_text_diff;
4304 * Create a file which contains the target path of a symlink so we can feed
4305 * it as content to the diff engine.
4307 static const struct got_error *
4308 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4309 const char *abspath)
4311 const struct got_error *err = NULL;
4312 char target_path[PATH_MAX];
4313 ssize_t target_len, outlen;
4318 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4319 if (target_len == -1)
4320 return got_error_from_errno2("readlinkat", abspath);
4322 target_len = readlink(abspath, target_path, PATH_MAX);
4323 if (target_len == -1)
4324 return got_error_from_errno2("readlink", abspath);
4327 *fd = got_opentempfd();
4329 return got_error_from_errno("got_opentempfd");
4331 outlen = write(*fd, target_path, target_len);
4333 err = got_error_from_errno("got_opentempfd");
4337 if (lseek(*fd, 0, SEEK_SET) == -1) {
4338 err = got_error_from_errno2("lseek", abspath);
4349 static const struct got_error *
4350 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4351 const char *path, struct got_object_id *blob_id,
4352 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4353 int dirfd, const char *de_name)
4355 struct print_diff_arg *a = arg;
4356 const struct got_error *err = NULL;
4357 struct got_blob_object *blob1 = NULL;
4360 char *abspath = NULL, *label1 = NULL;
4363 if (a->diff_staged) {
4364 if (staged_status != GOT_STATUS_MODIFY &&
4365 staged_status != GOT_STATUS_ADD &&
4366 staged_status != GOT_STATUS_DELETE)
4369 if (staged_status == GOT_STATUS_DELETE)
4371 if (status == GOT_STATUS_NONEXISTENT)
4372 return got_error_set_errno(ENOENT, path);
4373 if (status != GOT_STATUS_MODIFY &&
4374 status != GOT_STATUS_ADD &&
4375 status != GOT_STATUS_DELETE &&
4376 status != GOT_STATUS_CONFLICT)
4380 if (!a->header_shown) {
4381 printf("diff %s %s%s\n", a->id_str,
4382 got_worktree_get_root_path(a->worktree),
4383 a->diff_staged ? " (staged changes)" : "");
4384 a->header_shown = 1;
4387 if (a->diff_staged) {
4388 const char *label1 = NULL, *label2 = NULL;
4389 switch (staged_status) {
4390 case GOT_STATUS_MODIFY:
4394 case GOT_STATUS_ADD:
4397 case GOT_STATUS_DELETE:
4401 return got_error(GOT_ERR_FILE_STATUS);
4403 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4404 staged_blob_id, label1, label2, a->diff_context,
4405 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4408 if (staged_status == GOT_STATUS_ADD ||
4409 staged_status == GOT_STATUS_MODIFY) {
4411 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4415 err = got_object_id_str(&id_str, staged_blob_id);
4418 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4419 err = got_error_from_errno("asprintf");
4424 } else if (status != GOT_STATUS_ADD) {
4425 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4430 if (status != GOT_STATUS_DELETE) {
4431 if (asprintf(&abspath, "%s/%s",
4432 got_worktree_get_root_path(a->worktree), path) == -1) {
4433 err = got_error_from_errno("asprintf");
4438 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4440 if (!got_err_open_nofollow_on_symlink()) {
4441 err = got_error_from_errno2("openat",
4445 err = get_symlink_target_file(&fd, dirfd,
4451 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4453 if (!got_err_open_nofollow_on_symlink()) {
4454 err = got_error_from_errno2("open",
4458 err = get_symlink_target_file(&fd, dirfd,
4464 if (fstat(fd, &sb) == -1) {
4465 err = got_error_from_errno2("fstat", abspath);
4468 f2 = fdopen(fd, "r");
4470 err = got_error_from_errno2("fdopen", abspath);
4477 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4478 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4481 got_object_blob_close(blob1);
4482 if (f2 && fclose(f2) == EOF && err == NULL)
4483 err = got_error_from_errno("fclose");
4484 if (fd != -1 && close(fd) == -1 && err == NULL)
4485 err = got_error_from_errno("close");
4490 static const struct got_error *
4491 cmd_diff(int argc, char *argv[])
4493 const struct got_error *error;
4494 struct got_repository *repo = NULL;
4495 struct got_worktree *worktree = NULL;
4496 char *cwd = NULL, *repo_path = NULL;
4497 const char *commit_args[2] = { NULL, NULL };
4498 int ncommit_args = 0;
4499 struct got_object_id *ids[2] = { NULL, NULL };
4500 char *labels[2] = { NULL, NULL };
4501 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4502 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4503 int force_text_diff = 0, force_path = 0, rflag = 0;
4505 struct got_reflist_head refs;
4506 struct got_pathlist_head paths;
4507 struct got_pathlist_entry *pe;
4513 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4518 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4521 force_text_diff = 1;
4524 if (ncommit_args >= 2)
4525 errx(1, "too many -c options used");
4526 commit_args[ncommit_args++] = optarg;
4529 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4532 err(1, "-C option %s", errstr);
4535 repo_path = realpath(optarg, NULL);
4536 if (repo_path == NULL)
4537 return got_error_from_errno2("realpath",
4539 got_path_strip_trailing_slashes(repo_path);
4546 ignore_whitespace = 1;
4560 cwd = getcwd(NULL, 0);
4562 error = got_error_from_errno("getcwd");
4566 if (repo_path == NULL) {
4567 error = got_worktree_open(&worktree, cwd);
4568 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4574 strdup(got_worktree_get_repo_path(worktree));
4575 if (repo_path == NULL) {
4576 error = got_error_from_errno("strdup");
4580 repo_path = strdup(cwd);
4581 if (repo_path == NULL) {
4582 error = got_error_from_errno("strdup");
4588 error = got_repo_open(&repo, repo_path, NULL);
4593 if (rflag || worktree == NULL || ncommit_args > 0) {
4595 error = got_error_msg(GOT_ERR_NOT_IMPL,
4596 "-P option can only be used when diffing "
4601 error = got_error_msg(GOT_ERR_NOT_IMPL,
4602 "-s option can only be used when diffing "
4608 error = apply_unveil(got_repo_get_path(repo), 1,
4609 worktree ? got_worktree_get_root_path(worktree) : NULL);
4613 if ((!force_path && argc == 2) || ncommit_args > 0) {
4614 int obj_type = (ncommit_args > 0 ?
4615 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4616 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4620 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4622 if (ncommit_args > 0)
4623 arg = commit_args[i];
4626 error = got_repo_match_object_id(&ids[i], &labels[i],
4627 arg, obj_type, &refs, repo);
4629 if (error->code != GOT_ERR_NOT_REF &&
4630 error->code != GOT_ERR_NO_OBJ)
4632 if (ncommit_args > 0)
4640 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4641 struct print_diff_arg arg;
4644 if (worktree == NULL) {
4645 if (argc == 2 && ids[0] == NULL) {
4646 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4648 } else if (argc == 2 && ids[1] == NULL) {
4649 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4651 } else if (argc > 0) {
4652 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4653 "%s", "specified paths cannot be resolved");
4656 error = got_error(GOT_ERR_NOT_WORKTREE);
4661 error = get_worktree_paths_from_argv(&paths, argc, argv,
4666 error = got_object_id_str(&id_str,
4667 got_worktree_get_base_commit_id(worktree));
4671 arg.worktree = worktree;
4672 arg.diff_context = diff_context;
4673 arg.id_str = id_str;
4674 arg.header_shown = 0;
4675 arg.diff_staged = diff_staged;
4676 arg.ignore_whitespace = ignore_whitespace;
4677 arg.force_text_diff = force_text_diff;
4679 error = got_worktree_status(worktree, &paths, repo, 0,
4680 print_diff, &arg, check_cancelled, NULL);
4685 if (ncommit_args == 1) {
4686 struct got_commit_object *commit;
4687 error = got_object_open_as_commit(&commit, repo, ids[0]);
4691 labels[1] = labels[0];
4693 if (got_object_commit_get_nparents(commit) > 0) {
4694 const struct got_object_id_queue *pids;
4695 struct got_object_qid *pid;
4696 pids = got_object_commit_get_parent_ids(commit);
4697 pid = STAILQ_FIRST(pids);
4698 ids[0] = got_object_id_dup(pid->id);
4699 if (ids[0] == NULL) {
4700 error = got_error_from_errno(
4701 "got_object_id_dup");
4702 got_object_commit_close(commit);
4705 error = got_object_id_str(&labels[0], ids[0]);
4707 got_object_commit_close(commit);
4712 labels[0] = strdup("/dev/null");
4713 if (labels[0] == NULL) {
4714 error = got_error_from_errno("strdup");
4715 got_object_commit_close(commit);
4720 got_object_commit_close(commit);
4723 if (ncommit_args == 0 && argc > 2) {
4724 error = got_error_msg(GOT_ERR_BAD_PATH,
4725 "path arguments cannot be used when diffing two objects");
4730 error = got_object_get_type(&type1, repo, ids[0]);
4735 error = got_object_get_type(&type2, repo, ids[1]);
4738 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4739 error = got_error(GOT_ERR_OBJ_TYPE);
4742 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4743 error = got_error_msg(GOT_ERR_OBJ_TYPE,
4744 "path arguments cannot be used when diffing blobs");
4748 for (i = 0; ncommit_args > 0 && i < argc; i++) {
4750 struct got_pathlist_entry *new;
4754 error = got_worktree_resolve_path(&p, worktree,
4758 prefix = got_worktree_get_path_prefix(worktree);
4759 while (prefix[0] == '/')
4761 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4762 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4764 error = got_error_from_errno("asprintf");
4770 char *mapped_path, *s;
4771 error = got_repo_map_path(&mapped_path, repo, argv[i]);
4777 in_repo_path = strdup(s);
4778 if (in_repo_path == NULL) {
4779 error = got_error_from_errno("asprintf");
4786 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4787 if (error || new == NULL /* duplicate */)
4793 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4794 case GOT_OBJ_TYPE_BLOB:
4795 error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4796 NULL, NULL, diff_context, ignore_whitespace,
4797 force_text_diff, repo, stdout);
4799 case GOT_OBJ_TYPE_TREE:
4800 error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4801 &paths, "", "", diff_context, ignore_whitespace,
4802 force_text_diff, repo, stdout);
4804 case GOT_OBJ_TYPE_COMMIT:
4805 printf("diff %s %s\n", labels[0], labels[1]);
4806 error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4807 &paths, diff_context, ignore_whitespace, force_text_diff,
4811 error = got_error(GOT_ERR_OBJ_TYPE);
4819 got_worktree_close(worktree);
4821 const struct got_error *close_err = got_repo_close(repo);
4825 TAILQ_FOREACH(pe, &paths, entry)
4826 free((char *)pe->path);
4827 got_pathlist_free(&paths);
4828 got_ref_list_free(&refs);
4836 "usage: %s blame [-c commit] [-r repository-path] path\n",
4845 char datebuf[11]; /* YYYY-MM-DD + NUL */
4848 struct blame_cb_args {
4849 struct blame_line *lines;
4853 off_t *line_offsets;
4855 struct got_repository *repo;
4858 static const struct got_error *
4859 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4861 const struct got_error *err = NULL;
4862 struct blame_cb_args *a = arg;
4863 struct blame_line *bline;
4865 size_t linesize = 0;
4866 struct got_commit_object *commit = NULL;
4869 time_t committer_time;
4871 if (nlines != a->nlines ||
4872 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4873 return got_error(GOT_ERR_RANGE);
4875 if (sigint_received)
4876 return got_error(GOT_ERR_ITER_COMPLETED);
4879 return NULL; /* no change in this commit */
4881 /* Annotate this line. */
4882 bline = &a->lines[lineno - 1];
4883 if (bline->annotated)
4885 err = got_object_id_str(&bline->id_str, id);
4889 err = got_object_open_as_commit(&commit, a->repo, id);
4893 bline->committer = strdup(got_object_commit_get_committer(commit));
4894 if (bline->committer == NULL) {
4895 err = got_error_from_errno("strdup");
4899 committer_time = got_object_commit_get_committer_time(commit);
4900 if (gmtime_r(&committer_time, &tm) == NULL)
4901 return got_error_from_errno("gmtime_r");
4902 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4904 err = got_error(GOT_ERR_NO_SPACE);
4907 bline->annotated = 1;
4909 /* Print lines annotated so far. */
4910 bline = &a->lines[a->lineno_cur - 1];
4911 if (!bline->annotated)
4914 offset = a->line_offsets[a->lineno_cur - 1];
4915 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4916 err = got_error_from_errno("fseeko");
4920 while (bline->annotated) {
4921 char *smallerthan, *at, *nl, *committer;
4924 if (getline(&line, &linesize, a->f) == -1) {
4926 err = got_error_from_errno("getline");
4930 committer = bline->committer;
4931 smallerthan = strchr(committer, '<');
4932 if (smallerthan && smallerthan[1] != '\0')
4933 committer = smallerthan + 1;
4934 at = strchr(committer, '@');
4937 len = strlen(committer);
4939 committer[8] = '\0';
4941 nl = strchr(line, '\n');
4944 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4945 bline->id_str, bline->datebuf, committer, line);
4948 bline = &a->lines[a->lineno_cur - 1];
4952 got_object_commit_close(commit);
4957 static const struct got_error *
4958 cmd_blame(int argc, char *argv[])
4960 const struct got_error *error;
4961 struct got_repository *repo = NULL;
4962 struct got_worktree *worktree = NULL;
4963 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4964 char *link_target = NULL;
4965 struct got_object_id *obj_id = NULL;
4966 struct got_object_id *commit_id = NULL;
4967 struct got_blob_object *blob = NULL;
4968 char *commit_id_str = NULL;
4969 struct blame_cb_args bca;
4970 int ch, obj_type, i;
4973 memset(&bca, 0, sizeof(bca));
4976 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4981 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4984 commit_id_str = optarg;
4987 repo_path = realpath(optarg, NULL);
4988 if (repo_path == NULL)
4989 return got_error_from_errno2("realpath",
4991 got_path_strip_trailing_slashes(repo_path);
5007 cwd = getcwd(NULL, 0);
5009 error = got_error_from_errno("getcwd");
5012 if (repo_path == NULL) {
5013 error = got_worktree_open(&worktree, cwd);
5014 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5020 strdup(got_worktree_get_repo_path(worktree));
5021 if (repo_path == NULL) {
5022 error = got_error_from_errno("strdup");
5027 repo_path = strdup(cwd);
5028 if (repo_path == NULL) {
5029 error = got_error_from_errno("strdup");
5035 error = got_repo_open(&repo, repo_path, NULL);
5040 const char *prefix = got_worktree_get_path_prefix(worktree);
5043 error = got_worktree_resolve_path(&p, worktree, path);
5046 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5047 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5049 error = got_error_from_errno("asprintf");
5054 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5056 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5059 error = got_repo_map_path(&in_repo_path, repo, path);
5064 if (commit_id_str == NULL) {
5065 struct got_reference *head_ref;
5066 error = got_ref_open(&head_ref, repo, worktree ?
5067 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5070 error = got_ref_resolve(&commit_id, repo, head_ref);
5071 got_ref_close(head_ref);
5075 struct got_reflist_head refs;
5077 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5081 error = got_repo_match_object_id(&commit_id, NULL,
5082 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5083 got_ref_list_free(&refs);
5088 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5093 error = got_object_id_by_path(&obj_id, repo, commit_id,
5094 link_target ? link_target : in_repo_path);
5098 error = got_object_get_type(&obj_type, repo, obj_id);
5102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5103 error = got_error_path(link_target ? link_target : in_repo_path,
5108 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5111 bca.f = got_opentemp();
5112 if (bca.f == NULL) {
5113 error = got_error_from_errno("got_opentemp");
5116 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5117 &bca.line_offsets, bca.f, blob);
5118 if (error || bca.nlines == 0)
5121 /* Don't include \n at EOF in the blame line count. */
5122 if (bca.line_offsets[bca.nlines - 1] == filesize)
5125 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5126 if (bca.lines == NULL) {
5127 error = got_error_from_errno("calloc");
5131 bca.nlines_prec = 0;
5139 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5140 repo, blame_cb, &bca, check_cancelled, NULL);
5149 got_object_blob_close(blob);
5151 got_worktree_close(worktree);
5153 const struct got_error *close_err = got_repo_close(repo);
5158 for (i = 0; i < bca.nlines; i++) {
5159 struct blame_line *bline = &bca.lines[i];
5160 free(bline->id_str);
5161 free(bline->committer);
5165 free(bca.line_offsets);
5166 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5167 error = got_error_from_errno("fclose");
5175 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5180 static const struct got_error *
5181 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5182 const char *root_path, struct got_repository *repo)
5184 const struct got_error *err = NULL;
5185 int is_root_path = (strcmp(path, root_path) == 0);
5186 const char *modestr = "";
5187 mode_t mode = got_tree_entry_get_mode(te);
5188 char *link_target = NULL;
5190 path += strlen(root_path);
5191 while (path[0] == '/')
5194 if (got_object_tree_entry_is_submodule(te))
5196 else if (S_ISLNK(mode)) {
5199 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5202 for (i = 0; i < strlen(link_target); i++) {
5203 if (!isprint((unsigned char)link_target[i]))
5204 link_target[i] = '?';
5209 else if (S_ISDIR(mode))
5211 else if (mode & S_IXUSR)
5214 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5215 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5216 link_target ? " -> ": "", link_target ? link_target : "");
5222 static const struct got_error *
5223 print_tree(const char *path, struct got_object_id *commit_id,
5224 int show_ids, int recurse, const char *root_path,
5225 struct got_repository *repo)
5227 const struct got_error *err = NULL;
5228 struct got_object_id *tree_id = NULL;
5229 struct got_tree_object *tree = NULL;
5232 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5236 err = got_object_open_as_tree(&tree, repo, tree_id);
5239 nentries = got_object_tree_get_nentries(tree);
5240 for (i = 0; i < nentries; i++) {
5241 struct got_tree_entry *te;
5244 if (sigint_received || sigpipe_received)
5247 te = got_object_tree_get_entry(tree, i);
5250 err = got_object_id_str(&id_str,
5251 got_tree_entry_get_id(te));
5254 if (asprintf(&id, "%s ", id_str) == -1) {
5255 err = got_error_from_errno("asprintf");
5261 err = print_entry(te, id, path, root_path, repo);
5266 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5268 if (asprintf(&child_path, "%s%s%s", path,
5269 path[0] == '/' && path[1] == '\0' ? "" : "/",
5270 got_tree_entry_get_name(te)) == -1) {
5271 err = got_error_from_errno("asprintf");
5274 err = print_tree(child_path, commit_id, show_ids, 1,
5283 got_object_tree_close(tree);
5288 static const struct got_error *
5289 cmd_tree(int argc, char *argv[])
5291 const struct got_error *error;
5292 struct got_repository *repo = NULL;
5293 struct got_worktree *worktree = NULL;
5294 const char *path, *refname = NULL;
5295 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5296 struct got_object_id *commit_id = NULL;
5297 char *commit_id_str = NULL;
5298 int show_ids = 0, recurse = 0;
5302 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5307 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5310 commit_id_str = optarg;
5313 repo_path = realpath(optarg, NULL);
5314 if (repo_path == NULL)
5315 return got_error_from_errno2("realpath",
5317 got_path_strip_trailing_slashes(repo_path);
5341 cwd = getcwd(NULL, 0);
5343 error = got_error_from_errno("getcwd");
5346 if (repo_path == NULL) {
5347 error = got_worktree_open(&worktree, cwd);
5348 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5354 strdup(got_worktree_get_repo_path(worktree));
5355 if (repo_path == NULL)
5356 error = got_error_from_errno("strdup");
5360 repo_path = strdup(cwd);
5361 if (repo_path == NULL) {
5362 error = got_error_from_errno("strdup");
5368 error = got_repo_open(&repo, repo_path, NULL);
5373 const char *prefix = got_worktree_get_path_prefix(worktree);
5378 error = got_worktree_resolve_path(&p, worktree, path);
5381 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5382 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5384 error = got_error_from_errno("asprintf");
5389 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5393 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5398 error = got_repo_map_path(&in_repo_path, repo, path);
5403 if (commit_id_str == NULL) {
5404 struct got_reference *head_ref;
5406 refname = got_worktree_get_head_ref_name(worktree);
5408 refname = GOT_REF_HEAD;
5409 error = got_ref_open(&head_ref, repo, refname, 0);
5412 error = got_ref_resolve(&commit_id, repo, head_ref);
5413 got_ref_close(head_ref);
5417 struct got_reflist_head refs;
5419 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5423 error = got_repo_match_object_id(&commit_id, NULL,
5424 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5425 got_ref_list_free(&refs);
5430 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5431 in_repo_path, repo);
5438 got_worktree_close(worktree);
5440 const struct got_error *close_err = got_repo_close(repo);
5450 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5451 "[-S status-codes] [path ...]\n", getprogname());
5455 struct got_status_arg {
5460 static const struct got_error *
5461 print_status(void *arg, unsigned char status, unsigned char staged_status,
5462 const char *path, struct got_object_id *blob_id,
5463 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5464 int dirfd, const char *de_name)
5466 struct got_status_arg *st = arg;
5468 if (status == staged_status && (status == GOT_STATUS_DELETE))
5469 status = GOT_STATUS_NO_CHANGE;
5470 if (st != NULL && st->status_codes) {
5471 size_t ncodes = strlen(st->status_codes);
5474 for (i = 0; i < ncodes ; i++) {
5476 if (status == st->status_codes[i] ||
5477 staged_status == st->status_codes[i]) {
5482 if (status == st->status_codes[i] ||
5483 staged_status == st->status_codes[i])
5488 if (st->suppress && j == 0)
5495 printf("%c%c %s\n", status, staged_status, path);
5499 static const struct got_error *
5500 cmd_status(int argc, char *argv[])
5502 const struct got_error *error = NULL;
5503 struct got_repository *repo = NULL;
5504 struct got_worktree *worktree = NULL;
5505 struct got_status_arg st;
5507 struct got_pathlist_head paths;
5508 struct got_pathlist_entry *pe;
5509 int ch, i, no_ignores = 0;
5513 memset(&st, 0, sizeof(st));
5514 st.status_codes = NULL;
5517 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5523 if (st.status_codes != NULL && st.suppress == 0)
5524 option_conflict('S', 's');
5528 for (i = 0; i < strlen(optarg); i++) {
5529 switch (optarg[i]) {
5530 case GOT_STATUS_MODIFY:
5531 case GOT_STATUS_ADD:
5532 case GOT_STATUS_DELETE:
5533 case GOT_STATUS_CONFLICT:
5534 case GOT_STATUS_MISSING:
5535 case GOT_STATUS_OBSTRUCTED:
5536 case GOT_STATUS_UNVERSIONED:
5537 case GOT_STATUS_MODE_CHANGE:
5538 case GOT_STATUS_NONEXISTENT:
5541 errx(1, "invalid status code '%c'",
5545 if (ch == 's' && st.suppress)
5546 option_conflict('s', 'S');
5547 st.status_codes = optarg;
5559 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5563 cwd = getcwd(NULL, 0);
5565 error = got_error_from_errno("getcwd");
5569 error = got_worktree_open(&worktree, cwd);
5571 if (error->code == GOT_ERR_NOT_WORKTREE)
5572 error = wrap_not_worktree_error(error, "status", cwd);
5576 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5581 error = apply_unveil(got_repo_get_path(repo), 1,
5582 got_worktree_get_root_path(worktree));
5586 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5590 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5591 print_status, &st, check_cancelled, NULL);
5593 TAILQ_FOREACH(pe, &paths, entry)
5594 free((char *)pe->path);
5595 got_pathlist_free(&paths);
5604 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5605 "[-s reference] [-d] [name]\n",
5610 static const struct got_error *
5611 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5613 static const struct got_error *err = NULL;
5614 struct got_reflist_head refs;
5615 struct got_reflist_entry *re;
5618 err = got_ref_list(&refs, repo, refname, sort_by_time ?
5619 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5624 TAILQ_FOREACH(re, &refs, entry) {
5626 refstr = got_ref_to_str(re->ref);
5628 return got_error_from_errno("got_ref_to_str");
5629 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5633 got_ref_list_free(&refs);
5637 static const struct got_error *
5638 delete_ref_by_name(struct got_repository *repo, const char *refname)
5640 const struct got_error *err;
5641 struct got_reference *ref;
5643 err = got_ref_open(&ref, repo, refname, 0);
5647 err = delete_ref(repo, ref);
5652 static const struct got_error *
5653 add_ref(struct got_repository *repo, const char *refname, const char *target)
5655 const struct got_error *err = NULL;
5656 struct got_object_id *id;
5657 struct got_reference *ref = NULL;
5660 * Don't let the user create a reference name with a leading '-'.
5661 * While technically a valid reference name, this case is usually
5662 * an unintended typo.
5664 if (refname[0] == '-')
5665 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5667 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5670 struct got_reference *target_ref;
5672 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5674 err = got_ref_open(&target_ref, repo, target, 0);
5677 err = got_ref_resolve(&id, repo, target_ref);
5678 got_ref_close(target_ref);
5683 err = got_ref_alloc(&ref, refname, id);
5687 err = got_ref_write(ref, repo);
5695 static const struct got_error *
5696 add_symref(struct got_repository *repo, const char *refname, const char *target)
5698 const struct got_error *err = NULL;
5699 struct got_reference *ref = NULL;
5700 struct got_reference *target_ref = NULL;
5703 * Don't let the user create a reference name with a leading '-'.
5704 * While technically a valid reference name, this case is usually
5705 * an unintended typo.
5707 if (refname[0] == '-')
5708 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5710 err = got_ref_open(&target_ref, repo, target, 0);
5714 err = got_ref_alloc_symref(&ref, refname, target_ref);
5718 err = got_ref_write(ref, repo);
5721 got_ref_close(target_ref);
5727 static const struct got_error *
5728 cmd_ref(int argc, char *argv[])
5730 const struct got_error *error = NULL;
5731 struct got_repository *repo = NULL;
5732 struct got_worktree *worktree = NULL;
5733 char *cwd = NULL, *repo_path = NULL;
5734 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5735 const char *obj_arg = NULL, *symref_target= NULL;
5736 char *refname = NULL;
5738 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5747 repo_path = realpath(optarg, NULL);
5748 if (repo_path == NULL)
5749 return got_error_from_errno2("realpath",
5751 got_path_strip_trailing_slashes(repo_path);
5757 symref_target = optarg;
5768 if (obj_arg && do_list)
5769 option_conflict('c', 'l');
5770 if (obj_arg && do_delete)
5771 option_conflict('c', 'd');
5772 if (obj_arg && symref_target)
5773 option_conflict('c', 's');
5774 if (symref_target && do_delete)
5775 option_conflict('s', 'd');
5776 if (symref_target && do_list)
5777 option_conflict('s', 'l');
5778 if (do_delete && do_list)
5779 option_conflict('d', 'l');
5780 if (sort_by_time && !do_list)
5781 errx(1, "-t option requires -l option");
5787 if (argc != 0 && argc != 1)
5790 refname = strdup(argv[0]);
5791 if (refname == NULL) {
5792 error = got_error_from_errno("strdup");
5799 refname = strdup(argv[0]);
5800 if (refname == NULL) {
5801 error = got_error_from_errno("strdup");
5807 got_path_strip_trailing_slashes(refname);
5811 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5815 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5816 "sendfd unveil", NULL) == -1)
5820 cwd = getcwd(NULL, 0);
5822 error = got_error_from_errno("getcwd");
5826 if (repo_path == NULL) {
5827 error = got_worktree_open(&worktree, cwd);
5828 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5834 strdup(got_worktree_get_repo_path(worktree));
5835 if (repo_path == NULL)
5836 error = got_error_from_errno("strdup");
5840 repo_path = strdup(cwd);
5841 if (repo_path == NULL) {
5842 error = got_error_from_errno("strdup");
5848 error = got_repo_open(&repo, repo_path, NULL);
5852 error = apply_unveil(got_repo_get_path(repo), do_list,
5853 worktree ? got_worktree_get_root_path(worktree) : NULL);
5858 error = list_refs(repo, refname, sort_by_time);
5860 error = delete_ref_by_name(repo, refname);
5861 else if (symref_target)
5862 error = add_symref(repo, refname, symref_target);
5864 if (obj_arg == NULL)
5866 error = add_ref(repo, refname, obj_arg);
5871 const struct got_error *close_err = got_repo_close(repo);
5876 got_worktree_close(worktree);
5886 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5887 "[-n] [name]\n", getprogname());
5891 static const struct got_error *
5892 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5893 struct got_reference *ref)
5895 const struct got_error *err = NULL;
5896 const char *refname, *marker = " ";
5899 refname = got_ref_get_name(ref);
5900 if (worktree && strcmp(refname,
5901 got_worktree_get_head_ref_name(worktree)) == 0) {
5902 struct got_object_id *id = NULL;
5904 err = got_ref_resolve(&id, repo, ref);
5907 if (got_object_id_cmp(id,
5908 got_worktree_get_base_commit_id(worktree)) == 0)
5915 if (strncmp(refname, "refs/heads/", 11) == 0)
5917 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5919 if (strncmp(refname, "refs/remotes/", 13) == 0)
5922 refstr = got_ref_to_str(ref);
5924 return got_error_from_errno("got_ref_to_str");
5926 printf("%s%s: %s\n", marker, refname, refstr);
5931 static const struct got_error *
5932 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5934 const char *refname;
5936 if (worktree == NULL)
5937 return got_error(GOT_ERR_NOT_WORKTREE);
5939 refname = got_worktree_get_head_ref_name(worktree);
5941 if (strncmp(refname, "refs/heads/", 11) == 0)
5943 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5946 printf("%s\n", refname);
5951 static const struct got_error *
5952 list_branches(struct got_repository *repo, struct got_worktree *worktree,
5955 static const struct got_error *err = NULL;
5956 struct got_reflist_head refs;
5957 struct got_reflist_entry *re;
5958 struct got_reference *temp_ref = NULL;
5959 int rebase_in_progress, histedit_in_progress;
5964 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5969 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5974 if (rebase_in_progress || histedit_in_progress) {
5975 err = got_ref_open(&temp_ref, repo,
5976 got_worktree_get_head_ref_name(worktree), 0);
5979 list_branch(repo, worktree, temp_ref);
5980 got_ref_close(temp_ref);
5984 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
5985 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5990 TAILQ_FOREACH(re, &refs, entry)
5991 list_branch(repo, worktree, re->ref);
5993 got_ref_list_free(&refs);
5995 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
5996 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6001 TAILQ_FOREACH(re, &refs, entry)
6002 list_branch(repo, worktree, re->ref);
6004 got_ref_list_free(&refs);
6009 static const struct got_error *
6010 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6011 const char *branch_name)
6013 const struct got_error *err = NULL;
6014 struct got_reference *ref = NULL;
6015 char *refname, *remote_refname = NULL;
6017 if (strncmp(branch_name, "refs/", 5) == 0)
6019 if (strncmp(branch_name, "heads/", 6) == 0)
6021 else if (strncmp(branch_name, "remotes/", 8) == 0)
6024 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6025 return got_error_from_errno("asprintf");
6027 if (asprintf(&remote_refname, "refs/remotes/%s",
6028 branch_name) == -1) {
6029 err = got_error_from_errno("asprintf");
6033 err = got_ref_open(&ref, repo, refname, 0);
6035 const struct got_error *err2;
6036 if (err->code != GOT_ERR_NOT_REF)
6039 * Keep 'err' intact such that if neither branch exists
6040 * we report "refs/heads" rather than "refs/remotes" in
6041 * our error message.
6043 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6050 strcmp(got_worktree_get_head_ref_name(worktree),
6051 got_ref_get_name(ref)) == 0) {
6052 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6053 "will not delete this work tree's current branch");
6057 err = delete_ref(repo, ref);
6062 free(remote_refname);
6066 static const struct got_error *
6067 add_branch(struct got_repository *repo, const char *branch_name,
6068 struct got_object_id *base_commit_id)
6070 const struct got_error *err = NULL;
6071 struct got_reference *ref = NULL;
6072 char *base_refname = NULL, *refname = NULL;
6075 * Don't let the user create a branch name with a leading '-'.
6076 * While technically a valid reference name, this case is usually
6077 * an unintended typo.
6079 if (branch_name[0] == '-')
6080 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6082 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6085 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6086 err = got_error_from_errno("asprintf");
6090 err = got_ref_open(&ref, repo, refname, 0);
6092 err = got_error(GOT_ERR_BRANCH_EXISTS);
6094 } else if (err->code != GOT_ERR_NOT_REF)
6097 err = got_ref_alloc(&ref, refname, base_commit_id);
6101 err = got_ref_write(ref, repo);
6110 static const struct got_error *
6111 cmd_branch(int argc, char *argv[])
6113 const struct got_error *error = NULL;
6114 struct got_repository *repo = NULL;
6115 struct got_worktree *worktree = NULL;
6116 char *cwd = NULL, *repo_path = NULL;
6117 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6118 const char *delref = NULL, *commit_id_arg = NULL;
6119 struct got_reference *ref = NULL;
6120 struct got_pathlist_head paths;
6121 struct got_pathlist_entry *pe;
6122 struct got_object_id *commit_id = NULL;
6123 char *commit_id_str = NULL;
6127 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6130 commit_id_arg = optarg;
6136 repo_path = realpath(optarg, NULL);
6137 if (repo_path == NULL)
6138 return got_error_from_errno2("realpath",
6140 got_path_strip_trailing_slashes(repo_path);
6157 if (do_list && delref)
6158 option_conflict('l', 'd');
6159 if (sort_by_time && !do_list)
6160 errx(1, "-t option requires -l option");
6165 if (!do_list && !delref && argc == 0)
6168 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6169 errx(1, "-c option can only be used when creating a branch");
6171 if (do_list || delref) {
6174 } else if (!do_show && argc != 1)
6178 if (do_list || do_show) {
6179 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6183 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6184 "sendfd unveil", NULL) == -1)
6188 cwd = getcwd(NULL, 0);
6190 error = got_error_from_errno("getcwd");
6194 if (repo_path == NULL) {
6195 error = got_worktree_open(&worktree, cwd);
6196 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6202 strdup(got_worktree_get_repo_path(worktree));
6203 if (repo_path == NULL)
6204 error = got_error_from_errno("strdup");
6208 repo_path = strdup(cwd);
6209 if (repo_path == NULL) {
6210 error = got_error_from_errno("strdup");
6216 error = got_repo_open(&repo, repo_path, NULL);
6220 error = apply_unveil(got_repo_get_path(repo), do_list,
6221 worktree ? got_worktree_get_root_path(worktree) : NULL);
6226 error = show_current_branch(repo, worktree);
6228 error = list_branches(repo, worktree, sort_by_time);
6230 error = delete_branch(repo, worktree, delref);
6232 struct got_reflist_head refs;
6234 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6238 if (commit_id_arg == NULL)
6239 commit_id_arg = worktree ?
6240 got_worktree_get_head_ref_name(worktree) :
6242 error = got_repo_match_object_id(&commit_id, NULL,
6243 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6244 got_ref_list_free(&refs);
6247 error = add_branch(repo, argv[0], commit_id);
6250 if (worktree && do_update) {
6251 struct got_update_progress_arg upa;
6252 char *branch_refname = NULL;
6254 error = got_object_id_str(&commit_id_str, commit_id);
6257 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6261 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6263 error = got_error_from_errno("asprintf");
6266 error = got_ref_open(&ref, repo, branch_refname, 0);
6267 free(branch_refname);
6270 error = switch_head_ref(ref, commit_id, worktree,
6274 error = got_worktree_set_base_commit_id(worktree, repo,
6278 memset(&upa, 0, sizeof(upa));
6279 error = got_worktree_checkout_files(worktree, &paths,
6280 repo, update_progress, &upa, check_cancelled,
6284 if (upa.did_something) {
6285 printf("Updated to %s: %s\n",
6286 got_worktree_get_head_ref_name(worktree),
6289 print_update_progress_stats(&upa);
6296 const struct got_error *close_err = got_repo_close(repo);
6301 got_worktree_close(worktree);
6305 free(commit_id_str);
6306 TAILQ_FOREACH(pe, &paths, entry)
6307 free((char *)pe->path);
6308 got_pathlist_free(&paths);
6317 "usage: %s tag [-c commit] [-r repository] [-l] "
6318 "[-m message] name\n", getprogname());
6323 static const struct got_error *
6324 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6326 const struct got_error *err = NULL;
6327 struct got_reflist_entry *re, *se, *new;
6328 struct got_object_id *re_id, *se_id;
6329 struct got_tag_object *re_tag, *se_tag;
6330 time_t re_time, se_time;
6332 STAILQ_FOREACH(re, tags, entry) {
6333 se = STAILQ_FIRST(sorted);
6335 err = got_reflist_entry_dup(&new, re);
6338 STAILQ_INSERT_HEAD(sorted, new, entry);
6341 err = got_ref_resolve(&re_id, repo, re->ref);
6344 err = got_object_open_as_tag(&re_tag, repo, re_id);
6348 re_time = got_object_tag_get_tagger_time(re_tag);
6349 got_object_tag_close(re_tag);
6353 err = got_ref_resolve(&se_id, repo, re->ref);
6356 err = got_object_open_as_tag(&se_tag, repo, se_id);
6360 se_time = got_object_tag_get_tagger_time(se_tag);
6361 got_object_tag_close(se_tag);
6363 if (se_time > re_time) {
6364 err = got_reflist_entry_dup(&new, re);
6367 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6370 se = STAILQ_NEXT(se, entry);
6379 static const struct got_error *
6380 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6382 static const struct got_error *err = NULL;
6383 struct got_reflist_head refs;
6384 struct got_reflist_entry *re;
6388 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6392 TAILQ_FOREACH(re, &refs, entry) {
6393 const char *refname;
6394 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6398 struct got_object_id *id;
6399 struct got_tag_object *tag;
6400 struct got_commit_object *commit = NULL;
6402 refname = got_ref_get_name(re->ref);
6403 if (strncmp(refname, "refs/tags/", 10) != 0)
6406 refstr = got_ref_to_str(re->ref);
6407 if (refstr == NULL) {
6408 err = got_error_from_errno("got_ref_to_str");
6411 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6414 err = got_ref_resolve(&id, repo, re->ref);
6417 err = got_object_open_as_tag(&tag, repo, id);
6419 if (err->code != GOT_ERR_OBJ_TYPE) {
6423 /* "lightweight" tag */
6424 err = got_object_open_as_commit(&commit, repo, id);
6429 tagger = got_object_commit_get_committer(commit);
6431 got_object_commit_get_committer_time(commit);
6432 err = got_object_id_str(&id_str, id);
6438 tagger = got_object_tag_get_tagger(tag);
6439 tagger_time = got_object_tag_get_tagger_time(tag);
6440 err = got_object_id_str(&id_str,
6441 got_object_tag_get_object_id(tag));
6445 printf("from: %s\n", tagger);
6446 datestr = get_datestr(&tagger_time, datebuf);
6448 printf("date: %s UTC\n", datestr);
6450 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6452 switch (got_object_tag_get_object_type(tag)) {
6453 case GOT_OBJ_TYPE_BLOB:
6454 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6457 case GOT_OBJ_TYPE_TREE:
6458 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6461 case GOT_OBJ_TYPE_COMMIT:
6462 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6465 case GOT_OBJ_TYPE_TAG:
6466 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6475 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6478 got_object_commit_close(commit);
6480 tagmsg0 = strdup(got_object_tag_get_message(tag));
6481 got_object_tag_close(tag);
6482 if (tagmsg0 == NULL) {
6483 err = got_error_from_errno("strdup");
6490 line = strsep(&tagmsg, "\n");
6492 printf(" %s\n", line);
6497 got_ref_list_free(&refs);
6501 static const struct got_error *
6502 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6503 const char *tag_name, const char *repo_path)
6505 const struct got_error *err = NULL;
6506 char *template = NULL, *initial_content = NULL;
6507 char *editor = NULL;
6508 int initial_content_len;
6511 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6512 err = got_error_from_errno("asprintf");
6516 initial_content_len = asprintf(&initial_content,
6517 "\n# tagging commit %s as %s\n",
6518 commit_id_str, tag_name);
6519 if (initial_content_len == -1) {
6520 err = got_error_from_errno("asprintf");
6524 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6528 if (write(fd, initial_content, initial_content_len) == -1) {
6529 err = got_error_from_errno2("write", *tagmsg_path);
6533 err = get_editor(&editor);
6536 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6537 initial_content_len, 1);
6539 free(initial_content);
6543 if (fd != -1 && close(fd) == -1 && err == NULL)
6544 err = got_error_from_errno2("close", *tagmsg_path);
6546 /* Editor is done; we can now apply unveil(2) */
6548 err = apply_unveil(repo_path, 0, NULL);
6556 static const struct got_error *
6557 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6558 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6560 const struct got_error *err = NULL;
6561 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6562 char *label = NULL, *commit_id_str = NULL;
6563 struct got_reference *ref = NULL;
6564 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6565 char *tagmsg_path = NULL, *tag_id_str = NULL;
6566 int preserve_tagmsg = 0;
6567 struct got_reflist_head refs;
6572 * Don't let the user create a tag name with a leading '-'.
6573 * While technically a valid reference name, this case is usually
6574 * an unintended typo.
6576 if (tag_name[0] == '-')
6577 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6579 err = get_author(&tagger, repo, worktree);
6583 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6587 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6588 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6592 err = got_object_id_str(&commit_id_str, commit_id);
6596 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6597 refname = strdup(tag_name);
6598 if (refname == NULL) {
6599 err = got_error_from_errno("strdup");
6603 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6604 err = got_error_from_errno("asprintf");
6608 err = got_ref_open(&ref, repo, refname, 0);
6610 err = got_error(GOT_ERR_TAG_EXISTS);
6612 } else if (err->code != GOT_ERR_NOT_REF)
6615 if (tagmsg_arg == NULL) {
6616 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6617 tag_name, got_repo_get_path(repo));
6619 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6620 tagmsg_path != NULL)
6621 preserve_tagmsg = 1;
6626 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6627 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6630 preserve_tagmsg = 1;
6634 err = got_ref_alloc(&ref, refname, tag_id);
6637 preserve_tagmsg = 1;
6641 err = got_ref_write(ref, repo);
6644 preserve_tagmsg = 1;
6648 err = got_object_id_str(&tag_id_str, tag_id);
6651 preserve_tagmsg = 1;
6654 printf("Created tag %s\n", tag_id_str);
6656 if (preserve_tagmsg) {
6657 fprintf(stderr, "%s: tag message preserved in %s\n",
6658 getprogname(), tagmsg_path);
6659 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6660 err = got_error_from_errno2("unlink", tagmsg_path);
6665 free(commit_id_str);
6670 got_ref_list_free(&refs);
6674 static const struct got_error *
6675 cmd_tag(int argc, char *argv[])
6677 const struct got_error *error = NULL;
6678 struct got_repository *repo = NULL;
6679 struct got_worktree *worktree = NULL;
6680 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6681 char *gitconfig_path = NULL;
6682 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6683 int ch, do_list = 0;
6685 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6688 commit_id_arg = optarg;
6694 repo_path = realpath(optarg, NULL);
6695 if (repo_path == NULL)
6696 return got_error_from_errno2("realpath",
6698 got_path_strip_trailing_slashes(repo_path);
6713 if (commit_id_arg != NULL)
6715 "-c option can only be used when creating a tag");
6717 option_conflict('l', 'm');
6720 } else if (argc != 1)
6727 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6731 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6732 "sendfd unveil", NULL) == -1)
6736 cwd = getcwd(NULL, 0);
6738 error = got_error_from_errno("getcwd");
6742 if (repo_path == NULL) {
6743 error = got_worktree_open(&worktree, cwd);
6744 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6750 strdup(got_worktree_get_repo_path(worktree));
6751 if (repo_path == NULL)
6752 error = got_error_from_errno("strdup");
6756 repo_path = strdup(cwd);
6757 if (repo_path == NULL) {
6758 error = got_error_from_errno("strdup");
6765 error = got_repo_open(&repo, repo_path, NULL);
6768 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6771 error = list_tags(repo, worktree);
6773 error = get_gitconfig_path(&gitconfig_path);
6776 error = got_repo_open(&repo, repo_path, gitconfig_path);
6781 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6786 if (commit_id_arg == NULL) {
6787 struct got_reference *head_ref;
6788 struct got_object_id *commit_id;
6789 error = got_ref_open(&head_ref, repo,
6790 worktree ? got_worktree_get_head_ref_name(worktree)
6794 error = got_ref_resolve(&commit_id, repo, head_ref);
6795 got_ref_close(head_ref);
6798 error = got_object_id_str(&commit_id_str, commit_id);
6804 error = add_tag(repo, worktree, tag_name,
6805 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6809 const struct got_error *close_err = got_repo_close(repo);
6814 got_worktree_close(worktree);
6817 free(gitconfig_path);
6818 free(commit_id_str);
6825 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6830 static const struct got_error *
6831 add_progress(void *arg, unsigned char status, const char *path)
6833 while (path[0] == '/')
6835 printf("%c %s\n", status, path);
6839 static const struct got_error *
6840 cmd_add(int argc, char *argv[])
6842 const struct got_error *error = NULL;
6843 struct got_repository *repo = NULL;
6844 struct got_worktree *worktree = NULL;
6846 struct got_pathlist_head paths;
6847 struct got_pathlist_entry *pe;
6848 int ch, can_recurse = 0, no_ignores = 0;
6852 while ((ch = getopt(argc, argv, "IR")) != -1) {
6870 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6877 cwd = getcwd(NULL, 0);
6879 error = got_error_from_errno("getcwd");
6883 error = got_worktree_open(&worktree, cwd);
6885 if (error->code == GOT_ERR_NOT_WORKTREE)
6886 error = wrap_not_worktree_error(error, "add", cwd);
6890 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6895 error = apply_unveil(got_repo_get_path(repo), 1,
6896 got_worktree_get_root_path(worktree));
6900 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6907 TAILQ_FOREACH(pe, &paths, entry) {
6908 if (asprintf(&ondisk_path, "%s/%s",
6909 got_worktree_get_root_path(worktree),
6911 error = got_error_from_errno("asprintf");
6914 if (lstat(ondisk_path, &sb) == -1) {
6915 if (errno == ENOENT) {
6919 error = got_error_from_errno2("lstat",
6925 if (S_ISDIR(sb.st_mode)) {
6926 error = got_error_msg(GOT_ERR_BAD_PATH,
6927 "adding directories requires -R option");
6933 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6934 NULL, repo, no_ignores);
6937 const struct got_error *close_err = got_repo_close(repo);
6942 got_worktree_close(worktree);
6943 TAILQ_FOREACH(pe, &paths, entry)
6944 free((char *)pe->path);
6945 got_pathlist_free(&paths);
6953 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6954 "path ...\n", getprogname());
6958 static const struct got_error *
6959 print_remove_status(void *arg, unsigned char status,
6960 unsigned char staged_status, const char *path)
6962 while (path[0] == '/')
6964 if (status == GOT_STATUS_NONEXISTENT)
6966 if (status == staged_status && (status == GOT_STATUS_DELETE))
6967 status = GOT_STATUS_NO_CHANGE;
6968 printf("%c%c %s\n", status, staged_status, path);
6972 static const struct got_error *
6973 cmd_remove(int argc, char *argv[])
6975 const struct got_error *error = NULL;
6976 struct got_worktree *worktree = NULL;
6977 struct got_repository *repo = NULL;
6978 const char *status_codes = NULL;
6980 struct got_pathlist_head paths;
6981 struct got_pathlist_entry *pe;
6982 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6986 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6989 delete_local_mods = 1;
6998 for (i = 0; i < strlen(optarg); i++) {
6999 switch (optarg[i]) {
7000 case GOT_STATUS_MODIFY:
7001 delete_local_mods = 1;
7003 case GOT_STATUS_MISSING:
7006 errx(1, "invalid status code '%c'",
7010 status_codes = optarg;
7022 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7029 cwd = getcwd(NULL, 0);
7031 error = got_error_from_errno("getcwd");
7034 error = got_worktree_open(&worktree, cwd);
7036 if (error->code == GOT_ERR_NOT_WORKTREE)
7037 error = wrap_not_worktree_error(error, "remove", cwd);
7041 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7046 error = apply_unveil(got_repo_get_path(repo), 1,
7047 got_worktree_get_root_path(worktree));
7051 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7058 TAILQ_FOREACH(pe, &paths, entry) {
7059 if (asprintf(&ondisk_path, "%s/%s",
7060 got_worktree_get_root_path(worktree),
7062 error = got_error_from_errno("asprintf");
7065 if (lstat(ondisk_path, &sb) == -1) {
7066 if (errno == ENOENT) {
7070 error = got_error_from_errno2("lstat",
7076 if (S_ISDIR(sb.st_mode)) {
7077 error = got_error_msg(GOT_ERR_BAD_PATH,
7078 "removing directories requires -R option");
7084 error = got_worktree_schedule_delete(worktree, &paths,
7085 delete_local_mods, status_codes, print_remove_status, NULL,
7086 repo, keep_on_disk);
7089 const struct got_error *close_err = got_repo_close(repo);
7094 got_worktree_close(worktree);
7095 TAILQ_FOREACH(pe, &paths, entry)
7096 free((char *)pe->path);
7097 got_pathlist_free(&paths);
7105 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7106 "path ...\n", getprogname());
7110 static const struct got_error *
7111 revert_progress(void *arg, unsigned char status, const char *path)
7113 if (status == GOT_STATUS_UNVERSIONED)
7116 while (path[0] == '/')
7118 printf("%c %s\n", status, path);
7122 struct choose_patch_arg {
7123 FILE *patch_script_file;
7127 static const struct got_error *
7128 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7129 int nchanges, const char *action)
7132 size_t linesize = 0;
7136 case GOT_STATUS_ADD:
7137 printf("A %s\n%s this addition? [y/n] ", path, action);
7139 case GOT_STATUS_DELETE:
7140 printf("D %s\n%s this deletion? [y/n] ", path, action);
7142 case GOT_STATUS_MODIFY:
7143 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7144 return got_error_from_errno("fseek");
7145 printf(GOT_COMMIT_SEP_STR);
7146 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7148 if (ferror(patch_file))
7149 return got_error_from_errno("getline");
7150 printf(GOT_COMMIT_SEP_STR);
7151 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7152 path, n, nchanges, action);
7155 return got_error_path(path, GOT_ERR_FILE_STATUS);
7161 static const struct got_error *
7162 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7163 FILE *patch_file, int n, int nchanges)
7165 const struct got_error *err = NULL;
7167 size_t linesize = 0;
7170 struct choose_patch_arg *a = arg;
7172 *choice = GOT_PATCH_CHOICE_NONE;
7174 if (a->patch_script_file) {
7176 err = show_change(status, path, patch_file, n, nchanges,
7180 linelen = getline(&line, &linesize, a->patch_script_file);
7181 if (linelen == -1) {
7182 if (ferror(a->patch_script_file))
7183 return got_error_from_errno("getline");
7186 nl = strchr(line, '\n');
7189 if (strcmp(line, "y") == 0) {
7190 *choice = GOT_PATCH_CHOICE_YES;
7192 } else if (strcmp(line, "n") == 0) {
7193 *choice = GOT_PATCH_CHOICE_NO;
7195 } else if (strcmp(line, "q") == 0 &&
7196 status == GOT_STATUS_MODIFY) {
7197 *choice = GOT_PATCH_CHOICE_QUIT;
7200 printf("invalid response '%s'\n", line);
7205 while (resp != 'y' && resp != 'n' && resp != 'q') {
7206 err = show_change(status, path, patch_file, n, nchanges,
7213 if (status == GOT_STATUS_MODIFY) {
7214 if (resp != 'y' && resp != 'n' && resp != 'q') {
7215 printf("invalid response '%c'\n", resp);
7218 } else if (resp != 'y' && resp != 'n') {
7219 printf("invalid response '%c'\n", resp);
7225 *choice = GOT_PATCH_CHOICE_YES;
7226 else if (resp == 'n')
7227 *choice = GOT_PATCH_CHOICE_NO;
7228 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7229 *choice = GOT_PATCH_CHOICE_QUIT;
7235 static const struct got_error *
7236 cmd_revert(int argc, char *argv[])
7238 const struct got_error *error = NULL;
7239 struct got_worktree *worktree = NULL;
7240 struct got_repository *repo = NULL;
7241 char *cwd = NULL, *path = NULL;
7242 struct got_pathlist_head paths;
7243 struct got_pathlist_entry *pe;
7244 int ch, can_recurse = 0, pflag = 0;
7245 FILE *patch_script_file = NULL;
7246 const char *patch_script_path = NULL;
7247 struct choose_patch_arg cpa;
7251 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7257 patch_script_path = optarg;
7272 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7273 "unveil", NULL) == -1)
7278 if (patch_script_path && !pflag)
7279 errx(1, "-F option can only be used together with -p option");
7281 cwd = getcwd(NULL, 0);
7283 error = got_error_from_errno("getcwd");
7286 error = got_worktree_open(&worktree, cwd);
7288 if (error->code == GOT_ERR_NOT_WORKTREE)
7289 error = wrap_not_worktree_error(error, "revert", cwd);
7293 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7298 if (patch_script_path) {
7299 patch_script_file = fopen(patch_script_path, "r");
7300 if (patch_script_file == NULL) {
7301 error = got_error_from_errno2("fopen",
7306 error = apply_unveil(got_repo_get_path(repo), 1,
7307 got_worktree_get_root_path(worktree));
7311 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7318 TAILQ_FOREACH(pe, &paths, entry) {
7319 if (asprintf(&ondisk_path, "%s/%s",
7320 got_worktree_get_root_path(worktree),
7322 error = got_error_from_errno("asprintf");
7325 if (lstat(ondisk_path, &sb) == -1) {
7326 if (errno == ENOENT) {
7330 error = got_error_from_errno2("lstat",
7336 if (S_ISDIR(sb.st_mode)) {
7337 error = got_error_msg(GOT_ERR_BAD_PATH,
7338 "reverting directories requires -R option");
7344 cpa.patch_script_file = patch_script_file;
7345 cpa.action = "revert";
7346 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7347 pflag ? choose_patch : NULL, &cpa, repo);
7349 if (patch_script_file && fclose(patch_script_file) == EOF &&
7351 error = got_error_from_errno2("fclose", patch_script_path);
7353 const struct got_error *close_err = got_repo_close(repo);
7358 got_worktree_close(worktree);
7367 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7368 "[path ...]\n", getprogname());
7372 struct collect_commit_logmsg_arg {
7373 const char *cmdline_log;
7374 const char *prepared_log;
7375 int non_interactive;
7377 const char *worktree_path;
7378 const char *branch_name;
7379 const char *repo_path;
7384 static const struct got_error *
7385 read_prepared_logmsg(char **logmsg, const char *path)
7387 const struct got_error *err = NULL;
7393 memset(&sb, 0, sizeof(sb));
7395 f = fopen(path, "r");
7397 return got_error_from_errno2("fopen", path);
7399 if (fstat(fileno(f), &sb) == -1) {
7400 err = got_error_from_errno2("fstat", path);
7403 if (sb.st_size == 0) {
7404 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7408 *logmsg = malloc(sb.st_size + 1);
7409 if (*logmsg == NULL) {
7410 err = got_error_from_errno("malloc");
7414 r = fread(*logmsg, 1, sb.st_size, f);
7415 if (r != sb.st_size) {
7417 err = got_error_from_errno2("fread", path);
7419 err = got_error(GOT_ERR_IO);
7422 (*logmsg)[sb.st_size] = '\0';
7424 if (fclose(f) == EOF && err == NULL)
7425 err = got_error_from_errno2("fclose", path);
7434 static const struct got_error *
7435 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7438 char *initial_content = NULL;
7439 struct got_pathlist_entry *pe;
7440 const struct got_error *err = NULL;
7441 char *template = NULL;
7442 struct collect_commit_logmsg_arg *a = arg;
7443 int initial_content_len;
7447 /* if a message was specified on the command line, just use it */
7448 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7449 len = strlen(a->cmdline_log) + 1;
7450 *logmsg = malloc(len + 1);
7451 if (*logmsg == NULL)
7452 return got_error_from_errno("malloc");
7453 strlcpy(*logmsg, a->cmdline_log, len);
7455 } else if (a->prepared_log != NULL && a->non_interactive)
7456 return read_prepared_logmsg(logmsg, a->prepared_log);
7458 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7459 return got_error_from_errno("asprintf");
7461 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7465 if (a->prepared_log) {
7467 err = read_prepared_logmsg(&msg, a->prepared_log);
7470 if (write(fd, msg, strlen(msg)) == -1) {
7471 err = got_error_from_errno2("write", a->logmsg_path);
7478 initial_content_len = asprintf(&initial_content,
7479 "\n# changes to be committed on branch %s:\n",
7481 if (initial_content_len == -1) {
7482 err = got_error_from_errno("asprintf");
7486 if (write(fd, initial_content, initial_content_len) == -1) {
7487 err = got_error_from_errno2("write", a->logmsg_path);
7491 TAILQ_FOREACH(pe, commitable_paths, entry) {
7492 struct got_commitable *ct = pe->data;
7493 dprintf(fd, "# %c %s\n",
7494 got_commitable_get_status(ct),
7495 got_commitable_get_path(ct));
7498 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7499 initial_content_len, a->prepared_log ? 0 : 1);
7501 free(initial_content);
7504 if (fd != -1 && close(fd) == -1 && err == NULL)
7505 err = got_error_from_errno2("close", a->logmsg_path);
7507 /* Editor is done; we can now apply unveil(2) */
7509 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7517 static const struct got_error *
7518 cmd_commit(int argc, char *argv[])
7520 const struct got_error *error = NULL;
7521 struct got_worktree *worktree = NULL;
7522 struct got_repository *repo = NULL;
7523 char *cwd = NULL, *id_str = NULL;
7524 struct got_object_id *id = NULL;
7525 const char *logmsg = NULL;
7526 char *prepared_logmsg = NULL;
7527 struct collect_commit_logmsg_arg cl_arg;
7528 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7529 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7530 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7531 struct got_pathlist_head paths;
7534 cl_arg.logmsg_path = NULL;
7536 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7540 option_conflict('F', 'm');
7541 prepared_logmsg = realpath(optarg, NULL);
7542 if (prepared_logmsg == NULL)
7543 return got_error_from_errno2("realpath",
7547 if (prepared_logmsg)
7548 option_conflict('m', 'F');
7552 non_interactive = 1;
7555 allow_bad_symlinks = 1;
7567 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7568 "unveil", NULL) == -1)
7571 cwd = getcwd(NULL, 0);
7573 error = got_error_from_errno("getcwd");
7576 error = got_worktree_open(&worktree, cwd);
7578 if (error->code == GOT_ERR_NOT_WORKTREE)
7579 error = wrap_not_worktree_error(error, "commit", cwd);
7583 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7586 if (rebase_in_progress) {
7587 error = got_error(GOT_ERR_REBASING);
7591 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7596 error = get_gitconfig_path(&gitconfig_path);
7599 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7604 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7607 if (merge_in_progress) {
7608 error = got_error(GOT_ERR_MERGE_BUSY);
7612 error = get_author(&author, repo, worktree);
7617 * unveil(2) traverses exec(2); if an editor is used we have
7618 * to apply unveil after the log message has been written.
7620 if (logmsg == NULL || strlen(logmsg) == 0)
7621 error = get_editor(&editor);
7623 error = apply_unveil(got_repo_get_path(repo), 0,
7624 got_worktree_get_root_path(worktree));
7628 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7632 cl_arg.editor = editor;
7633 cl_arg.cmdline_log = logmsg;
7634 cl_arg.prepared_log = prepared_logmsg;
7635 cl_arg.non_interactive = non_interactive;
7636 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7637 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7638 if (!histedit_in_progress) {
7639 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7640 error = got_error(GOT_ERR_COMMIT_BRANCH);
7643 cl_arg.branch_name += 11;
7645 cl_arg.repo_path = got_repo_get_path(repo);
7646 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7647 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7648 print_status, NULL, repo);
7650 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7651 cl_arg.logmsg_path != NULL)
7652 preserve_logmsg = 1;
7656 error = got_object_id_str(&id_str, id);
7659 printf("Created commit %s\n", id_str);
7661 if (preserve_logmsg) {
7662 fprintf(stderr, "%s: log message preserved in %s\n",
7663 getprogname(), cl_arg.logmsg_path);
7664 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7666 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7667 free(cl_arg.logmsg_path);
7669 const struct got_error *close_err = got_repo_close(repo);
7674 got_worktree_close(worktree);
7677 free(gitconfig_path);
7680 free(prepared_logmsg);
7687 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7688 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7689 "[remote-repository]\n", getprogname());
7693 struct got_send_progress_arg {
7694 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7697 int last_nobj_total;
7701 int printed_something;
7703 struct got_pathlist_head *delete_branches;
7706 static const struct got_error *
7707 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7708 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7711 struct got_send_progress_arg *a = arg;
7712 char scaled_packsize[FMT_SCALED_STRSIZE];
7713 char scaled_sent[FMT_SCALED_STRSIZE];
7714 int p_deltify = 0, p_written = 0, p_sent = 0;
7715 int print_searching = 0, print_total = 0;
7716 int print_deltify = 0, print_written = 0, print_sent = 0;
7718 if (a->verbosity < 0)
7722 const char *status = success ? "accepted" : "rejected";
7725 struct got_pathlist_entry *pe;
7726 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7727 const char *branchname = pe->path;
7728 if (got_path_cmp(branchname, refname,
7729 strlen(branchname), strlen(refname)) == 0) {
7731 a->sent_something = 1;
7737 if (a->printed_something)
7739 printf("Server has %s %s", status, refname);
7740 a->printed_something = 1;
7744 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7745 return got_error_from_errno("fmt_scaled");
7746 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7747 return got_error_from_errno("fmt_scaled");
7749 if (a->last_ncommits != ncommits) {
7750 print_searching = 1;
7751 a->last_ncommits = ncommits;
7754 if (a->last_nobj_total != nobj_total) {
7755 print_searching = 1;
7757 a->last_nobj_total = nobj_total;
7760 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7761 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7762 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7763 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7764 return got_error(GOT_ERR_NO_SPACE);
7767 if (nobj_deltify > 0 || nobj_written > 0) {
7768 if (nobj_deltify > 0) {
7769 p_deltify = (nobj_deltify * 100) / nobj_total;
7770 if (p_deltify != a->last_p_deltify) {
7771 a->last_p_deltify = p_deltify;
7772 print_searching = 1;
7777 if (nobj_written > 0) {
7778 p_written = (nobj_written * 100) / nobj_total;
7779 if (p_written != a->last_p_written) {
7780 a->last_p_written = p_written;
7781 print_searching = 1;
7789 if (bytes_sent > 0) {
7790 p_sent = (bytes_sent * 100) / packfile_size;
7791 if (p_sent != a->last_p_sent) {
7792 a->last_p_sent = p_sent;
7793 print_searching = 1;
7799 a->sent_something = 1;
7802 if (print_searching || print_total || print_deltify || print_written ||
7805 if (print_searching)
7806 printf("packing %d reference%s", ncommits,
7807 ncommits == 1 ? "" : "s");
7809 printf("; %d object%s", nobj_total,
7810 nobj_total == 1 ? "" : "s");
7812 printf("; deltify: %d%%", p_deltify);
7814 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7815 scaled_packsize, p_sent);
7816 else if (print_written)
7817 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7818 scaled_packsize, p_written);
7819 if (print_searching || print_total || print_deltify ||
7820 print_written || print_sent) {
7821 a->printed_something = 1;
7827 static const struct got_error *
7828 cmd_send(int argc, char *argv[])
7830 const struct got_error *error = NULL;
7831 char *cwd = NULL, *repo_path = NULL;
7832 const char *remote_name;
7833 char *proto = NULL, *host = NULL, *port = NULL;
7834 char *repo_name = NULL, *server_path = NULL;
7835 const struct got_remote_repo *remotes, *remote = NULL;
7836 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7837 struct got_repository *repo = NULL;
7838 struct got_worktree *worktree = NULL;
7839 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7840 struct got_pathlist_head branches;
7841 struct got_pathlist_head tags;
7842 struct got_reflist_head all_branches;
7843 struct got_reflist_head all_tags;
7844 struct got_pathlist_head delete_args;
7845 struct got_pathlist_head delete_branches;
7846 struct got_reflist_entry *re;
7847 struct got_pathlist_entry *pe;
7848 int i, ch, sendfd = -1, sendstatus;
7850 struct got_send_progress_arg spa;
7851 int verbosity = 0, overwrite_refs = 0;
7852 int send_all_branches = 0, send_all_tags = 0;
7853 struct got_reference *ref = NULL;
7855 TAILQ_INIT(&branches);
7857 TAILQ_INIT(&all_branches);
7858 TAILQ_INIT(&all_tags);
7859 TAILQ_INIT(&delete_args);
7860 TAILQ_INIT(&delete_branches);
7862 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7865 send_all_branches = 1;
7868 error = got_pathlist_append(&branches, optarg, NULL);
7874 error = got_pathlist_append(&delete_args, optarg, NULL);
7882 repo_path = realpath(optarg, NULL);
7883 if (repo_path == NULL)
7884 return got_error_from_errno2("realpath",
7886 got_path_strip_trailing_slashes(repo_path);
7889 error = got_pathlist_append(&tags, optarg, NULL);
7900 else if (verbosity < 3)
7914 if (send_all_branches && !TAILQ_EMPTY(&branches))
7915 option_conflict('a', 'b');
7916 if (send_all_tags && !TAILQ_EMPTY(&tags))
7917 option_conflict('T', 't');
7921 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7923 remote_name = argv[0];
7927 cwd = getcwd(NULL, 0);
7929 error = got_error_from_errno("getcwd");
7933 if (repo_path == NULL) {
7934 error = got_worktree_open(&worktree, cwd);
7935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7941 strdup(got_worktree_get_repo_path(worktree));
7942 if (repo_path == NULL)
7943 error = got_error_from_errno("strdup");
7947 repo_path = strdup(cwd);
7948 if (repo_path == NULL) {
7949 error = got_error_from_errno("strdup");
7955 error = got_repo_open(&repo, repo_path, NULL);
7960 worktree_conf = got_worktree_get_gotconfig(worktree);
7961 if (worktree_conf) {
7962 got_gotconfig_get_remotes(&nremotes, &remotes,
7964 for (i = 0; i < nremotes; i++) {
7965 if (strcmp(remotes[i].name, remote_name) == 0) {
7966 remote = &remotes[i];
7972 if (remote == NULL) {
7973 repo_conf = got_repo_get_gotconfig(repo);
7975 got_gotconfig_get_remotes(&nremotes, &remotes,
7977 for (i = 0; i < nremotes; i++) {
7978 if (strcmp(remotes[i].name, remote_name) == 0) {
7979 remote = &remotes[i];
7985 if (remote == NULL) {
7986 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7987 for (i = 0; i < nremotes; i++) {
7988 if (strcmp(remotes[i].name, remote_name) == 0) {
7989 remote = &remotes[i];
7994 if (remote == NULL) {
7995 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7999 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8000 &repo_name, remote->send_url);
8004 if (strcmp(proto, "git") == 0) {
8006 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8007 "sendfd dns inet unveil", NULL) == -1)
8010 } else if (strcmp(proto, "git+ssh") == 0 ||
8011 strcmp(proto, "ssh") == 0) {
8013 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8014 "sendfd unveil", NULL) == -1)
8017 } else if (strcmp(proto, "http") == 0 ||
8018 strcmp(proto, "git+http") == 0) {
8019 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8022 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8026 error = got_dial_apply_unveil(proto);
8030 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8034 if (send_all_branches) {
8035 error = got_ref_list(&all_branches, repo, "refs/heads",
8036 got_ref_cmp_by_name, NULL);
8039 TAILQ_FOREACH(re, &all_branches, entry) {
8040 const char *branchname = got_ref_get_name(re->ref);
8041 error = got_pathlist_append(&branches,
8047 } else if (nbranches == 0) {
8048 for (i = 0; i < remote->nsend_branches; i++) {
8049 got_pathlist_append(&branches,
8050 remote->send_branches[i], NULL);
8054 if (send_all_tags) {
8055 error = got_ref_list(&all_tags, repo, "refs/tags",
8056 got_ref_cmp_by_name, NULL);
8059 TAILQ_FOREACH(re, &all_tags, entry) {
8060 const char *tagname = got_ref_get_name(re->ref);
8061 error = got_pathlist_append(&tags,
8070 * To prevent accidents only branches in refs/heads/ can be deleted
8071 * with 'got send -d'.
8072 * Deleting anything else requires local repository access or Git.
8074 TAILQ_FOREACH(pe, &delete_args, entry) {
8075 const char *branchname = pe->path;
8077 struct got_pathlist_entry *new;
8078 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8079 s = strdup(branchname);
8081 error = got_error_from_errno("strdup");
8085 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8086 error = got_error_from_errno("asprintf");
8090 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8091 if (error || new == NULL /* duplicate */)
8098 if (nbranches == 0 && ndelete_branches == 0) {
8099 struct got_reference *head_ref;
8101 error = got_ref_open(&head_ref, repo,
8102 got_worktree_get_head_ref_name(worktree), 0);
8104 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8107 if (got_ref_is_symbolic(head_ref)) {
8108 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8109 got_ref_close(head_ref);
8114 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8122 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8123 port ? ":" : "", port ? port : "");
8125 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8126 server_path, verbosity);
8130 memset(&spa, 0, sizeof(spa));
8131 spa.last_scaled_packsize[0] = '\0';
8132 spa.last_p_deltify = -1;
8133 spa.last_p_written = -1;
8134 spa.verbosity = verbosity;
8135 spa.delete_branches = &delete_branches;
8136 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8137 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8138 check_cancelled, NULL);
8139 if (spa.printed_something)
8143 if (!spa.sent_something && verbosity >= 0)
8144 printf("Already up-to-date\n");
8147 if (kill(sendpid, SIGTERM) == -1)
8148 error = got_error_from_errno("kill");
8149 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8150 error = got_error_from_errno("waitpid");
8152 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8153 error = got_error_from_errno("close");
8155 const struct got_error *close_err = got_repo_close(repo);
8160 got_worktree_close(worktree);
8163 got_pathlist_free(&branches);
8164 got_pathlist_free(&tags);
8165 got_ref_list_free(&all_branches);
8166 got_ref_list_free(&all_tags);
8167 got_pathlist_free(&delete_args);
8168 TAILQ_FOREACH(pe, &delete_branches, entry)
8169 free((char *)pe->path);
8170 got_pathlist_free(&delete_branches);
8182 usage_cherrypick(void)
8184 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8188 static const struct got_error *
8189 cmd_cherrypick(int argc, char *argv[])
8191 const struct got_error *error = NULL;
8192 struct got_worktree *worktree = NULL;
8193 struct got_repository *repo = NULL;
8194 char *cwd = NULL, *commit_id_str = NULL;
8195 struct got_object_id *commit_id = NULL;
8196 struct got_commit_object *commit = NULL;
8197 struct got_object_qid *pid;
8199 struct got_update_progress_arg upa;
8201 while ((ch = getopt(argc, argv, "")) != -1) {
8213 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8214 "unveil", NULL) == -1)
8220 cwd = getcwd(NULL, 0);
8222 error = got_error_from_errno("getcwd");
8225 error = got_worktree_open(&worktree, cwd);
8227 if (error->code == GOT_ERR_NOT_WORKTREE)
8228 error = wrap_not_worktree_error(error, "cherrypick",
8233 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8238 error = apply_unveil(got_repo_get_path(repo), 0,
8239 got_worktree_get_root_path(worktree));
8243 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8244 GOT_OBJ_TYPE_COMMIT, repo);
8245 if (error != NULL) {
8246 struct got_reference *ref;
8247 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8249 error = got_ref_open(&ref, repo, argv[0], 0);
8252 error = got_ref_resolve(&commit_id, repo, ref);
8257 error = got_object_id_str(&commit_id_str, commit_id);
8261 error = got_object_open_as_commit(&commit, repo, commit_id);
8264 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8265 memset(&upa, 0, sizeof(upa));
8266 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8267 commit_id, repo, update_progress, &upa, check_cancelled,
8272 if (upa.did_something)
8273 printf("Merged commit %s\n", commit_id_str);
8274 print_merge_progress_stats(&upa);
8277 got_object_commit_close(commit);
8278 free(commit_id_str);
8280 got_worktree_close(worktree);
8282 const struct got_error *close_err = got_repo_close(repo);
8292 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8296 static const struct got_error *
8297 cmd_backout(int argc, char *argv[])
8299 const struct got_error *error = NULL;
8300 struct got_worktree *worktree = NULL;
8301 struct got_repository *repo = NULL;
8302 char *cwd = NULL, *commit_id_str = NULL;
8303 struct got_object_id *commit_id = NULL;
8304 struct got_commit_object *commit = NULL;
8305 struct got_object_qid *pid;
8307 struct got_update_progress_arg upa;
8309 while ((ch = getopt(argc, argv, "")) != -1) {
8321 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8322 "unveil", NULL) == -1)
8328 cwd = getcwd(NULL, 0);
8330 error = got_error_from_errno("getcwd");
8333 error = got_worktree_open(&worktree, cwd);
8335 if (error->code == GOT_ERR_NOT_WORKTREE)
8336 error = wrap_not_worktree_error(error, "backout", cwd);
8340 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8345 error = apply_unveil(got_repo_get_path(repo), 0,
8346 got_worktree_get_root_path(worktree));
8350 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8351 GOT_OBJ_TYPE_COMMIT, repo);
8352 if (error != NULL) {
8353 struct got_reference *ref;
8354 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8356 error = got_ref_open(&ref, repo, argv[0], 0);
8359 error = got_ref_resolve(&commit_id, repo, ref);
8364 error = got_object_id_str(&commit_id_str, commit_id);
8368 error = got_object_open_as_commit(&commit, repo, commit_id);
8371 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8373 error = got_error(GOT_ERR_ROOT_COMMIT);
8377 memset(&upa, 0, sizeof(upa));
8378 error = got_worktree_merge_files(worktree, commit_id, pid->id,
8379 repo, update_progress, &upa, check_cancelled, NULL);
8383 if (upa.did_something)
8384 printf("Backed out commit %s\n", commit_id_str);
8385 print_merge_progress_stats(&upa);
8388 got_object_commit_close(commit);
8389 free(commit_id_str);
8391 got_worktree_close(worktree);
8393 const struct got_error *close_err = got_repo_close(repo);
8403 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8409 trim_logmsg(char *logmsg, int limit)
8414 len = strlen(logmsg);
8418 nl = strchr(logmsg, '\n');
8423 static const struct got_error *
8424 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8426 const struct got_error *err;
8427 char *logmsg0 = NULL;
8430 err = got_object_commit_get_logmsg(&logmsg0, commit);
8435 while (isspace((unsigned char)s[0]))
8438 *logmsg = strdup(s);
8439 if (*logmsg == NULL) {
8440 err = got_error_from_errno("strdup");
8444 trim_logmsg(*logmsg, limit);
8450 static const struct got_error *
8451 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8453 const struct got_error *err;
8454 struct got_commit_object *commit = NULL;
8455 char *id_str = NULL, *logmsg = NULL;
8457 err = got_object_open_as_commit(&commit, repo, id);
8461 err = got_object_id_str(&id_str, id);
8467 err = get_short_logmsg(&logmsg, 42, commit);
8471 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8474 got_object_commit_close(commit);
8479 static const struct got_error *
8480 show_rebase_progress(struct got_commit_object *commit,
8481 struct got_object_id *old_id, struct got_object_id *new_id)
8483 const struct got_error *err;
8484 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8486 err = got_object_id_str(&old_id_str, old_id);
8491 err = got_object_id_str(&new_id_str, new_id);
8496 old_id_str[12] = '\0';
8498 new_id_str[12] = '\0';
8500 err = get_short_logmsg(&logmsg, 42, commit);
8504 printf("%s -> %s: %s\n", old_id_str,
8505 new_id_str ? new_id_str : "no-op change", logmsg);
8513 static const struct got_error *
8514 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8515 struct got_reference *branch, struct got_reference *new_base_branch,
8516 struct got_reference *tmp_branch, struct got_repository *repo,
8519 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8520 return got_worktree_rebase_complete(worktree, fileindex,
8521 new_base_branch, tmp_branch, branch, repo, create_backup);
8524 static const struct got_error *
8525 rebase_commit(struct got_pathlist_head *merged_paths,
8526 struct got_worktree *worktree, struct got_fileindex *fileindex,
8527 struct got_reference *tmp_branch,
8528 struct got_object_id *commit_id, struct got_repository *repo)
8530 const struct got_error *error;
8531 struct got_commit_object *commit;
8532 struct got_object_id *new_commit_id;
8534 error = got_object_open_as_commit(&commit, repo, commit_id);
8538 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8539 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8541 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8543 error = show_rebase_progress(commit, commit_id, NULL);
8545 error = show_rebase_progress(commit, commit_id, new_commit_id);
8546 free(new_commit_id);
8549 got_object_commit_close(commit);
8553 struct check_path_prefix_arg {
8554 const char *path_prefix;
8559 static const struct got_error *
8560 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8561 struct got_blob_object *blob2, struct got_object_id *id1,
8562 struct got_object_id *id2, const char *path1, const char *path2,
8563 mode_t mode1, mode_t mode2, struct got_repository *repo)
8565 struct check_path_prefix_arg *a = arg;
8567 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8568 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8569 return got_error(a->errcode);
8574 static const struct got_error *
8575 check_path_prefix(struct got_object_id *parent_id,
8576 struct got_object_id *commit_id, const char *path_prefix,
8577 int errcode, struct got_repository *repo)
8579 const struct got_error *err;
8580 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8581 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8582 struct check_path_prefix_arg cpp_arg;
8584 if (got_path_is_root_dir(path_prefix))
8587 err = got_object_open_as_commit(&commit, repo, commit_id);
8591 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8595 err = got_object_open_as_tree(&tree1, repo,
8596 got_object_commit_get_tree_id(parent_commit));
8600 err = got_object_open_as_tree(&tree2, repo,
8601 got_object_commit_get_tree_id(commit));
8605 cpp_arg.path_prefix = path_prefix;
8606 while (cpp_arg.path_prefix[0] == '/')
8607 cpp_arg.path_prefix++;
8608 cpp_arg.len = strlen(cpp_arg.path_prefix);
8609 cpp_arg.errcode = errcode;
8610 err = got_diff_tree(tree1, tree2, "", "", repo,
8611 check_path_prefix_in_diff, &cpp_arg, 0);
8614 got_object_tree_close(tree1);
8616 got_object_tree_close(tree2);
8618 got_object_commit_close(commit);
8620 got_object_commit_close(parent_commit);
8624 static const struct got_error *
8625 collect_commits(struct got_object_id_queue *commits,
8626 struct got_object_id *initial_commit_id,
8627 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8628 const char *path_prefix, int path_prefix_errcode,
8629 struct got_repository *repo)
8631 const struct got_error *err = NULL;
8632 struct got_commit_graph *graph = NULL;
8633 struct got_object_id *parent_id = NULL;
8634 struct got_object_qid *qid;
8635 struct got_object_id *commit_id = initial_commit_id;
8637 err = got_commit_graph_open(&graph, "/", 1);
8641 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8642 check_cancelled, NULL);
8645 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8646 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8647 check_cancelled, NULL);
8649 if (err->code == GOT_ERR_ITER_COMPLETED) {
8650 err = got_error_msg(GOT_ERR_ANCESTRY,
8651 "ran out of commits to rebase before "
8652 "youngest common ancestor commit has "
8657 err = check_path_prefix(parent_id, commit_id,
8658 path_prefix, path_prefix_errcode, repo);
8662 err = got_object_qid_alloc(&qid, commit_id);
8665 STAILQ_INSERT_HEAD(commits, qid, entry);
8666 commit_id = parent_id;
8670 got_commit_graph_close(graph);
8674 static const struct got_error *
8675 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8677 const struct got_error *err = NULL;
8678 time_t committer_time;
8680 char datebuf[11]; /* YYYY-MM-DD + NUL */
8681 char *author0 = NULL, *author, *smallerthan;
8682 char *logmsg0 = NULL, *logmsg, *newline;
8684 committer_time = got_object_commit_get_committer_time(commit);
8685 if (gmtime_r(&committer_time, &tm) == NULL)
8686 return got_error_from_errno("gmtime_r");
8687 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8688 return got_error(GOT_ERR_NO_SPACE);
8690 author0 = strdup(got_object_commit_get_author(commit));
8691 if (author0 == NULL)
8692 return got_error_from_errno("strdup");
8694 smallerthan = strchr(author, '<');
8695 if (smallerthan && smallerthan[1] != '\0')
8696 author = smallerthan + 1;
8697 author[strcspn(author, "@>")] = '\0';
8699 err = got_object_commit_get_logmsg(&logmsg0, commit);
8703 while (*logmsg == '\n')
8705 newline = strchr(logmsg, '\n');
8709 if (asprintf(brief_str, "%s %s %s",
8710 datebuf, author, logmsg) == -1)
8711 err = got_error_from_errno("asprintf");
8718 static const struct got_error *
8719 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8720 struct got_repository *repo)
8722 const struct got_error *err;
8725 err = got_object_id_str(&id_str, id);
8729 err = got_ref_delete(ref, repo);
8733 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8739 static const struct got_error *
8740 print_backup_ref(const char *branch_name, const char *new_id_str,
8741 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8742 struct got_reflist_object_id_map *refs_idmap,
8743 struct got_repository *repo)
8745 const struct got_error *err = NULL;
8746 struct got_reflist_head *refs;
8747 char *refs_str = NULL;
8748 struct got_object_id *new_commit_id = NULL;
8749 struct got_commit_object *new_commit = NULL;
8750 char *new_commit_brief_str = NULL;
8751 struct got_object_id *yca_id = NULL;
8752 struct got_commit_object *yca_commit = NULL;
8753 char *yca_id_str = NULL, *yca_brief_str = NULL;
8754 char *custom_refs_str;
8756 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8757 return got_error_from_errno("asprintf");
8759 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8760 0, 0, refs_idmap, custom_refs_str);
8764 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8768 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8770 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8775 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8779 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8783 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8784 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8788 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8789 refs_str ? " (" : "", refs_str ? refs_str : "",
8790 refs_str ? ")" : "", new_commit_brief_str);
8791 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8792 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8796 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8800 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8804 err = got_object_id_str(&yca_id_str, yca_id);
8808 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8810 err = build_refs_str(&refs_str, refs, yca_id, repo);
8814 printf("history forked at %s%s%s%s\n %s\n",
8816 refs_str ? " (" : "", refs_str ? refs_str : "",
8817 refs_str ? ")" : "", yca_brief_str);
8820 free(custom_refs_str);
8821 free(new_commit_id);
8825 free(yca_brief_str);
8827 got_object_commit_close(new_commit);
8829 got_object_commit_close(yca_commit);
8834 static const struct got_error *
8835 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8836 int delete, struct got_repository *repo)
8838 const struct got_error *err;
8839 struct got_reflist_head refs, backup_refs;
8840 struct got_reflist_entry *re;
8841 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8842 struct got_object_id *old_commit_id = NULL;
8843 char *branch_name = NULL;
8844 struct got_commit_object *old_commit = NULL;
8845 struct got_reflist_object_id_map *refs_idmap = NULL;
8846 int wanted_branch_found = 0;
8849 TAILQ_INIT(&backup_refs);
8851 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8855 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8859 if (wanted_branch_name) {
8860 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8861 wanted_branch_name += 11;
8864 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8865 got_ref_cmp_by_commit_timestamp_descending, repo);
8869 TAILQ_FOREACH(re, &backup_refs, entry) {
8870 const char *refname = got_ref_get_name(re->ref);
8873 err = check_cancelled(NULL);
8877 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8881 err = got_object_open_as_commit(&old_commit, repo,
8886 if (strncmp(backup_ref_prefix, refname,
8887 backup_ref_prefix_len) == 0)
8888 refname += backup_ref_prefix_len;
8890 while (refname[0] == '/')
8893 branch_name = strdup(refname);
8894 if (branch_name == NULL) {
8895 err = got_error_from_errno("strdup");
8898 slash = strrchr(branch_name, '/');
8901 refname += strlen(branch_name) + 1;
8904 if (wanted_branch_name == NULL ||
8905 strcmp(wanted_branch_name, branch_name) == 0) {
8906 wanted_branch_found = 1;
8908 err = delete_backup_ref(re->ref,
8909 old_commit_id, repo);
8911 err = print_backup_ref(branch_name, refname,
8912 old_commit_id, old_commit, refs_idmap,
8919 free(old_commit_id);
8920 old_commit_id = NULL;
8923 got_object_commit_close(old_commit);
8927 if (wanted_branch_name && !wanted_branch_found) {
8928 err = got_error_fmt(GOT_ERR_NOT_REF,
8929 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8933 got_reflist_object_id_map_free(refs_idmap);
8934 got_ref_list_free(&refs);
8935 got_ref_list_free(&backup_refs);
8936 free(old_commit_id);
8939 got_object_commit_close(old_commit);
8943 static const struct got_error *
8944 abort_progress(void *arg, unsigned char status, const char *path)
8947 * Unversioned files should not clutter progress output when
8948 * an operation is aborted.
8950 if (status == GOT_STATUS_UNVERSIONED)
8953 return update_progress(arg, status, path);
8956 static const struct got_error *
8957 cmd_rebase(int argc, char *argv[])
8959 const struct got_error *error = NULL;
8960 struct got_worktree *worktree = NULL;
8961 struct got_repository *repo = NULL;
8962 struct got_fileindex *fileindex = NULL;
8964 struct got_reference *branch = NULL;
8965 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8966 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8967 struct got_object_id *resume_commit_id = NULL;
8968 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8969 struct got_commit_object *commit = NULL;
8970 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8971 int histedit_in_progress = 0, merge_in_progress = 0;
8972 int create_backup = 1, list_backups = 0, delete_backups = 0;
8973 struct got_object_id_queue commits;
8974 struct got_pathlist_head merged_paths;
8975 const struct got_object_id_queue *parent_ids;
8976 struct got_object_qid *qid, *pid;
8977 struct got_update_progress_arg upa;
8979 STAILQ_INIT(&commits);
8980 TAILQ_INIT(&merged_paths);
8981 memset(&upa, 0, sizeof(upa));
8983 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8989 continue_rebase = 1;
9007 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9008 "unveil", NULL) == -1)
9013 option_conflict('l', 'a');
9014 if (continue_rebase)
9015 option_conflict('l', 'c');
9017 option_conflict('l', 'X');
9018 if (argc != 0 && argc != 1)
9020 } else if (delete_backups) {
9022 option_conflict('X', 'a');
9023 if (continue_rebase)
9024 option_conflict('X', 'c');
9026 option_conflict('l', 'X');
9027 if (argc != 0 && argc != 1)
9030 if (abort_rebase && continue_rebase)
9032 else if (abort_rebase || continue_rebase) {
9035 } else if (argc != 1)
9039 cwd = getcwd(NULL, 0);
9041 error = got_error_from_errno("getcwd");
9044 error = got_worktree_open(&worktree, cwd);
9046 if (list_backups || delete_backups) {
9047 if (error->code != GOT_ERR_NOT_WORKTREE)
9050 if (error->code == GOT_ERR_NOT_WORKTREE)
9051 error = wrap_not_worktree_error(error,
9057 error = got_repo_open(&repo,
9058 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9062 error = apply_unveil(got_repo_get_path(repo), 0,
9063 worktree ? got_worktree_get_root_path(worktree) : NULL);
9067 if (list_backups || delete_backups) {
9068 error = process_backup_refs(
9069 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9070 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9071 goto done; /* nothing else to do */
9074 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9078 if (histedit_in_progress) {
9079 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9083 error = got_worktree_merge_in_progress(&merge_in_progress,
9087 if (merge_in_progress) {
9088 error = got_error(GOT_ERR_MERGE_BUSY);
9092 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9097 if (!rebase_in_progress) {
9098 error = got_error(GOT_ERR_NOT_REBASING);
9101 error = got_worktree_rebase_continue(&resume_commit_id,
9102 &new_base_branch, &tmp_branch, &branch, &fileindex,
9106 printf("Switching work tree to %s\n",
9107 got_ref_get_symref_target(new_base_branch));
9108 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9109 new_base_branch, abort_progress, &upa);
9112 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9113 print_merge_progress_stats(&upa);
9114 goto done; /* nothing else to do */
9117 if (continue_rebase) {
9118 if (!rebase_in_progress) {
9119 error = got_error(GOT_ERR_NOT_REBASING);
9122 error = got_worktree_rebase_continue(&resume_commit_id,
9123 &new_base_branch, &tmp_branch, &branch, &fileindex,
9128 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9129 resume_commit_id, repo);
9133 yca_id = got_object_id_dup(resume_commit_id);
9134 if (yca_id == NULL) {
9135 error = got_error_from_errno("got_object_id_dup");
9139 error = got_ref_open(&branch, repo, argv[0], 0);
9144 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9148 if (!continue_rebase) {
9149 struct got_object_id *base_commit_id;
9151 base_commit_id = got_worktree_get_base_commit_id(worktree);
9152 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9153 base_commit_id, branch_head_commit_id, 1, repo,
9154 check_cancelled, NULL);
9157 if (yca_id == NULL) {
9158 error = got_error_msg(GOT_ERR_ANCESTRY,
9159 "specified branch shares no common ancestry "
9160 "with work tree's branch");
9164 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9166 if (error->code != GOT_ERR_ANCESTRY)
9170 struct got_pathlist_head paths;
9171 printf("%s is already based on %s\n",
9172 got_ref_get_name(branch),
9173 got_worktree_get_head_ref_name(worktree));
9174 error = switch_head_ref(branch, branch_head_commit_id,
9178 error = got_worktree_set_base_commit_id(worktree, repo,
9179 branch_head_commit_id);
9183 error = got_pathlist_append(&paths, "", NULL);
9186 error = got_worktree_checkout_files(worktree,
9187 &paths, repo, update_progress, &upa,
9188 check_cancelled, NULL);
9189 got_pathlist_free(&paths);
9192 if (upa.did_something) {
9194 error = got_object_id_str(&id_str,
9195 branch_head_commit_id);
9198 printf("Updated to %s: %s\n",
9199 got_worktree_get_head_ref_name(worktree),
9203 printf("Already up-to-date\n");
9204 print_update_progress_stats(&upa);
9207 error = got_worktree_rebase_prepare(&new_base_branch,
9208 &tmp_branch, &fileindex, worktree, branch, repo);
9213 commit_id = branch_head_commit_id;
9214 error = got_object_open_as_commit(&commit, repo, commit_id);
9218 parent_ids = got_object_commit_get_parent_ids(commit);
9219 pid = STAILQ_FIRST(parent_ids);
9221 if (!continue_rebase) {
9222 error = got_worktree_rebase_abort(worktree, fileindex,
9223 repo, new_base_branch, abort_progress, &upa);
9226 printf("Rebase of %s aborted\n",
9227 got_ref_get_name(branch));
9228 print_merge_progress_stats(&upa);
9231 error = got_error(GOT_ERR_EMPTY_REBASE);
9234 error = collect_commits(&commits, commit_id, pid->id,
9235 yca_id, got_worktree_get_path_prefix(worktree),
9236 GOT_ERR_REBASE_PATH, repo);
9237 got_object_commit_close(commit);
9242 if (STAILQ_EMPTY(&commits)) {
9243 if (continue_rebase) {
9244 error = rebase_complete(worktree, fileindex,
9245 branch, new_base_branch, tmp_branch, repo,
9249 /* Fast-forward the reference of the branch. */
9250 struct got_object_id *new_head_commit_id;
9252 error = got_ref_resolve(&new_head_commit_id, repo,
9256 error = got_object_id_str(&id_str, new_head_commit_id);
9257 printf("Forwarding %s to commit %s\n",
9258 got_ref_get_name(branch), id_str);
9260 error = got_ref_change_ref(branch,
9261 new_head_commit_id);
9264 /* No backup needed since objects did not change. */
9270 STAILQ_FOREACH(qid, &commits, entry) {
9272 commit_id = qid->id;
9273 parent_id = pid ? pid->id : yca_id;
9276 memset(&upa, 0, sizeof(upa));
9277 error = got_worktree_rebase_merge_files(&merged_paths,
9278 worktree, fileindex, parent_id, commit_id, repo,
9279 update_progress, &upa, check_cancelled, NULL);
9283 print_merge_progress_stats(&upa);
9284 if (upa.conflicts > 0 || upa.missing > 0 ||
9285 upa.not_deleted > 0 || upa.unversioned > 0) {
9286 if (upa.conflicts > 0) {
9287 error = show_rebase_merge_conflict(qid->id,
9292 got_worktree_rebase_pathlist_free(&merged_paths);
9296 error = rebase_commit(&merged_paths, worktree, fileindex,
9297 tmp_branch, commit_id, repo);
9298 got_worktree_rebase_pathlist_free(&merged_paths);
9303 if (upa.conflicts > 0 || upa.missing > 0 ||
9304 upa.not_deleted > 0 || upa.unversioned > 0) {
9305 error = got_worktree_rebase_postpone(worktree, fileindex);
9308 if (upa.conflicts > 0 && upa.missing == 0 &&
9309 upa.not_deleted == 0 && upa.unversioned == 0) {
9310 error = got_error_msg(GOT_ERR_CONFLICTS,
9311 "conflicts must be resolved before rebasing "
9313 } else if (upa.conflicts > 0) {
9314 error = got_error_msg(GOT_ERR_CONFLICTS,
9315 "conflicts must be resolved before rebasing "
9316 "can continue; changes destined for some "
9317 "files were not yet merged and should be "
9318 "merged manually if required before the "
9319 "rebase operation is continued");
9321 error = got_error_msg(GOT_ERR_CONFLICTS,
9322 "changes destined for some files were not "
9323 "yet merged and should be merged manually "
9324 "if required before the rebase operation "
9328 error = rebase_complete(worktree, fileindex, branch,
9329 new_base_branch, tmp_branch, repo, create_backup);
9331 got_object_id_queue_free(&commits);
9332 free(branch_head_commit_id);
9333 free(resume_commit_id);
9336 got_object_commit_close(commit);
9338 got_ref_close(branch);
9339 if (new_base_branch)
9340 got_ref_close(new_base_branch);
9342 got_ref_close(tmp_branch);
9344 got_worktree_close(worktree);
9346 const struct got_error *close_err = got_repo_close(repo);
9354 usage_histedit(void)
9356 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9357 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9362 #define GOT_HISTEDIT_PICK 'p'
9363 #define GOT_HISTEDIT_EDIT 'e'
9364 #define GOT_HISTEDIT_FOLD 'f'
9365 #define GOT_HISTEDIT_DROP 'd'
9366 #define GOT_HISTEDIT_MESG 'm'
9368 static struct got_histedit_cmd {
9372 } got_histedit_cmds[] = {
9373 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9374 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9375 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9377 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9378 { GOT_HISTEDIT_MESG, "mesg",
9379 "single-line log message for commit above (open editor if empty)" },
9382 struct got_histedit_list_entry {
9383 TAILQ_ENTRY(got_histedit_list_entry) entry;
9384 struct got_object_id *commit_id;
9385 const struct got_histedit_cmd *cmd;
9388 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9390 static const struct got_error *
9391 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9392 FILE *f, struct got_repository *repo)
9394 const struct got_error *err = NULL;
9395 char *logmsg = NULL, *id_str = NULL;
9396 struct got_commit_object *commit = NULL;
9399 err = got_object_open_as_commit(&commit, repo, commit_id);
9403 err = get_short_logmsg(&logmsg, 34, commit);
9407 err = got_object_id_str(&id_str, commit_id);
9411 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9413 err = got_ferror(f, GOT_ERR_IO);
9416 got_object_commit_close(commit);
9422 static const struct got_error *
9423 histedit_write_commit_list(struct got_object_id_queue *commits,
9424 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9425 struct got_repository *repo)
9427 const struct got_error *err = NULL;
9428 struct got_object_qid *qid;
9429 const char *histedit_cmd = NULL;
9431 if (STAILQ_EMPTY(commits))
9432 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9434 STAILQ_FOREACH(qid, commits, entry) {
9435 histedit_cmd = got_histedit_cmds[0].name;
9437 histedit_cmd = "edit";
9438 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9439 histedit_cmd = "fold";
9440 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9443 if (edit_logmsg_only) {
9444 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9446 err = got_ferror(f, GOT_ERR_IO);
9455 static const struct got_error *
9456 write_cmd_list(FILE *f, const char *branch_name,
9457 struct got_object_id_queue *commits)
9459 const struct got_error *err = NULL;
9463 struct got_object_qid *qid;
9465 qid = STAILQ_FIRST(commits);
9466 err = got_object_id_str(&id_str, qid->id);
9471 "# Editing the history of branch '%s' starting at\n"
9473 "# Commits will be processed in order from top to "
9474 "bottom of this file.\n", branch_name, id_str);
9476 err = got_ferror(f, GOT_ERR_IO);
9480 n = fprintf(f, "# Available histedit commands:\n");
9482 err = got_ferror(f, GOT_ERR_IO);
9486 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9487 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9488 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9491 err = got_ferror(f, GOT_ERR_IO);
9500 static const struct got_error *
9501 histedit_syntax_error(int lineno)
9503 static char msg[42];
9506 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9508 if (ret == -1 || ret >= sizeof(msg))
9509 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9511 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9514 static const struct got_error *
9515 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9516 char *logmsg, struct got_repository *repo)
9518 const struct got_error *err;
9519 struct got_commit_object *folded_commit = NULL;
9520 char *id_str, *folded_logmsg = NULL;
9522 err = got_object_id_str(&id_str, hle->commit_id);
9526 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9530 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9533 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9534 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9535 folded_logmsg) == -1) {
9536 err = got_error_from_errno("asprintf");
9540 got_object_commit_close(folded_commit);
9542 free(folded_logmsg);
9546 static struct got_histedit_list_entry *
9547 get_folded_commits(struct got_histedit_list_entry *hle)
9549 struct got_histedit_list_entry *prev, *folded = NULL;
9551 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9552 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9553 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9554 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9556 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9562 static const struct got_error *
9563 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9564 struct got_repository *repo)
9566 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9567 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9568 const struct got_error *err = NULL;
9569 struct got_commit_object *commit = NULL;
9572 struct got_histedit_list_entry *folded = NULL;
9574 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9578 folded = get_folded_commits(hle);
9580 while (folded != hle) {
9581 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9582 folded = TAILQ_NEXT(folded, entry);
9585 err = append_folded_commit_msg(&new_msg, folded,
9591 folded = TAILQ_NEXT(folded, entry);
9595 err = got_object_id_str(&id_str, hle->commit_id);
9598 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9601 logmsg_len = asprintf(&new_msg,
9602 "%s\n# original log message of commit %s: %s",
9603 logmsg ? logmsg : "", id_str, orig_logmsg);
9604 if (logmsg_len == -1) {
9605 err = got_error_from_errno("asprintf");
9611 err = got_object_id_str(&id_str, hle->commit_id);
9615 err = got_opentemp_named_fd(&logmsg_path, &fd,
9616 GOT_TMPDIR_STR "/got-logmsg");
9620 write(fd, logmsg, logmsg_len);
9623 err = get_editor(&editor);
9627 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9630 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9633 hle->logmsg = strdup(new_msg);
9634 if (hle->logmsg == NULL)
9635 err = got_error_from_errno("strdup");
9638 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9639 err = got_error_from_errno2("unlink", logmsg_path);
9645 got_object_commit_close(commit);
9649 static const struct got_error *
9650 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9651 FILE *f, struct got_repository *repo)
9653 const struct got_error *err = NULL;
9654 char *line = NULL, *p, *end;
9658 const struct got_histedit_cmd *cmd;
9659 struct got_object_id *commit_id = NULL;
9660 struct got_histedit_list_entry *hle = NULL;
9663 len = getline(&line, &size, f);
9665 const struct got_error *getline_err;
9668 getline_err = got_error_from_errno("getline");
9669 err = got_ferror(f, getline_err->code);
9674 while (isspace((unsigned char)p[0]))
9676 if (p[0] == '#' || p[0] == '\0') {
9682 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9683 cmd = &got_histedit_cmds[i];
9684 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9685 isspace((unsigned char)p[strlen(cmd->name)])) {
9686 p += strlen(cmd->name);
9689 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9694 if (i == nitems(got_histedit_cmds)) {
9695 err = histedit_syntax_error(lineno);
9698 while (isspace((unsigned char)p[0]))
9700 if (cmd->code == GOT_HISTEDIT_MESG) {
9701 if (hle == NULL || hle->logmsg != NULL) {
9702 err = got_error(GOT_ERR_HISTEDIT_CMD);
9706 err = histedit_edit_logmsg(hle, repo);
9710 hle->logmsg = strdup(p);
9711 if (hle->logmsg == NULL) {
9712 err = got_error_from_errno("strdup");
9721 while (end[0] && !isspace((unsigned char)end[0]))
9725 err = got_object_resolve_id_str(&commit_id, repo, p);
9727 /* override error code */
9728 err = histedit_syntax_error(lineno);
9732 hle = malloc(sizeof(*hle));
9734 err = got_error_from_errno("malloc");
9738 hle->commit_id = commit_id;
9743 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9751 static const struct got_error *
9752 histedit_check_script(struct got_histedit_list *histedit_cmds,
9753 struct got_object_id_queue *commits, struct got_repository *repo)
9755 const struct got_error *err = NULL;
9756 struct got_object_qid *qid;
9757 struct got_histedit_list_entry *hle;
9758 static char msg[92];
9761 if (TAILQ_EMPTY(histedit_cmds))
9762 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9763 "histedit script contains no commands");
9764 if (STAILQ_EMPTY(commits))
9765 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9767 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9768 struct got_histedit_list_entry *hle2;
9769 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9772 if (got_object_id_cmp(hle->commit_id,
9773 hle2->commit_id) != 0)
9775 err = got_object_id_str(&id_str, hle->commit_id);
9778 snprintf(msg, sizeof(msg), "commit %s is listed "
9779 "more than once in histedit script", id_str);
9781 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9785 STAILQ_FOREACH(qid, commits, entry) {
9786 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9787 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9791 err = got_object_id_str(&id_str, qid->id);
9794 snprintf(msg, sizeof(msg),
9795 "commit %s missing from histedit script", id_str);
9797 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9801 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9802 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9803 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9804 "last commit in histedit script cannot be folded");
9809 static const struct got_error *
9810 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9811 const char *path, struct got_object_id_queue *commits,
9812 struct got_repository *repo)
9814 const struct got_error *err = NULL;
9818 err = get_editor(&editor);
9822 if (spawn_editor(editor, path) == -1) {
9823 err = got_error_from_errno("failed spawning editor");
9827 f = fopen(path, "r");
9829 err = got_error_from_errno("fopen");
9832 err = histedit_parse_list(histedit_cmds, f, repo);
9836 err = histedit_check_script(histedit_cmds, commits, repo);
9838 if (f && fclose(f) == EOF && err == NULL)
9839 err = got_error_from_errno("fclose");
9844 static const struct got_error *
9845 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9846 struct got_object_id_queue *, const char *, const char *,
9847 struct got_repository *);
9849 static const struct got_error *
9850 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9851 struct got_object_id_queue *commits, const char *branch_name,
9852 int edit_logmsg_only, int fold_only, int edit_only,
9853 struct got_repository *repo)
9855 const struct got_error *err;
9859 err = got_opentemp_named(&path, &f, "got-histedit");
9863 err = write_cmd_list(f, branch_name, commits);
9867 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9868 fold_only, edit_only, repo);
9872 if (edit_logmsg_only || fold_only || edit_only) {
9874 err = histedit_parse_list(histedit_cmds, f, repo);
9876 if (fclose(f) == EOF) {
9877 err = got_error_from_errno("fclose");
9881 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9883 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9884 err->code != GOT_ERR_HISTEDIT_CMD)
9886 err = histedit_edit_list_retry(histedit_cmds, err,
9887 commits, path, branch_name, repo);
9891 if (f && fclose(f) == EOF && err == NULL)
9892 err = got_error_from_errno("fclose");
9893 if (path && unlink(path) != 0 && err == NULL)
9894 err = got_error_from_errno2("unlink", path);
9899 static const struct got_error *
9900 histedit_save_list(struct got_histedit_list *histedit_cmds,
9901 struct got_worktree *worktree, struct got_repository *repo)
9903 const struct got_error *err = NULL;
9906 struct got_histedit_list_entry *hle;
9907 struct got_commit_object *commit = NULL;
9909 err = got_worktree_get_histedit_script_path(&path, worktree);
9913 f = fopen(path, "w");
9915 err = got_error_from_errno2("fopen", path);
9918 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9919 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9925 int n = fprintf(f, "%c %s\n",
9926 GOT_HISTEDIT_MESG, hle->logmsg);
9928 err = got_ferror(f, GOT_ERR_IO);
9934 if (f && fclose(f) == EOF && err == NULL)
9935 err = got_error_from_errno("fclose");
9938 got_object_commit_close(commit);
9943 histedit_free_list(struct got_histedit_list *histedit_cmds)
9945 struct got_histedit_list_entry *hle;
9947 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9948 TAILQ_REMOVE(histedit_cmds, hle, entry);
9953 static const struct got_error *
9954 histedit_load_list(struct got_histedit_list *histedit_cmds,
9955 const char *path, struct got_repository *repo)
9957 const struct got_error *err = NULL;
9960 f = fopen(path, "r");
9962 err = got_error_from_errno2("fopen", path);
9966 err = histedit_parse_list(histedit_cmds, f, repo);
9968 if (f && fclose(f) == EOF && err == NULL)
9969 err = got_error_from_errno("fclose");
9973 static const struct got_error *
9974 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9975 const struct got_error *edit_err, struct got_object_id_queue *commits,
9976 const char *path, const char *branch_name, struct got_repository *repo)
9978 const struct got_error *err = NULL, *prev_err = edit_err;
9981 while (resp != 'c' && resp != 'r' && resp != 'a') {
9982 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9983 "or (a)bort: ", getprogname(), prev_err->msg);
9988 histedit_free_list(histedit_cmds);
9989 err = histedit_run_editor(histedit_cmds, path, commits,
9992 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9993 err->code != GOT_ERR_HISTEDIT_CMD)
10000 } else if (resp == 'r') {
10001 histedit_free_list(histedit_cmds);
10002 err = histedit_edit_script(histedit_cmds,
10003 commits, branch_name, 0, 0, 0, repo);
10005 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10006 err->code != GOT_ERR_HISTEDIT_CMD)
10013 } else if (resp == 'a') {
10014 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10017 printf("invalid response '%c'\n", resp);
10023 static const struct got_error *
10024 histedit_complete(struct got_worktree *worktree,
10025 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10026 struct got_reference *branch, struct got_repository *repo)
10028 printf("Switching work tree to %s\n",
10029 got_ref_get_symref_target(branch));
10030 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10034 static const struct got_error *
10035 show_histedit_progress(struct got_commit_object *commit,
10036 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10038 const struct got_error *err;
10039 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10041 err = got_object_id_str(&old_id_str, hle->commit_id);
10046 err = got_object_id_str(&new_id_str, new_id);
10051 old_id_str[12] = '\0';
10053 new_id_str[12] = '\0';
10056 logmsg = strdup(hle->logmsg);
10057 if (logmsg == NULL) {
10058 err = got_error_from_errno("strdup");
10061 trim_logmsg(logmsg, 42);
10063 err = get_short_logmsg(&logmsg, 42, commit);
10068 switch (hle->cmd->code) {
10069 case GOT_HISTEDIT_PICK:
10070 case GOT_HISTEDIT_EDIT:
10071 printf("%s -> %s: %s\n", old_id_str,
10072 new_id_str ? new_id_str : "no-op change", logmsg);
10074 case GOT_HISTEDIT_DROP:
10075 case GOT_HISTEDIT_FOLD:
10076 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10088 static const struct got_error *
10089 histedit_commit(struct got_pathlist_head *merged_paths,
10090 struct got_worktree *worktree, struct got_fileindex *fileindex,
10091 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10092 struct got_repository *repo)
10094 const struct got_error *err;
10095 struct got_commit_object *commit;
10096 struct got_object_id *new_commit_id;
10098 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10099 && hle->logmsg == NULL) {
10100 err = histedit_edit_logmsg(hle, repo);
10105 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10109 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10110 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10111 hle->logmsg, repo);
10113 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10115 err = show_histedit_progress(commit, hle, NULL);
10117 err = show_histedit_progress(commit, hle, new_commit_id);
10118 free(new_commit_id);
10121 got_object_commit_close(commit);
10125 static const struct got_error *
10126 histedit_skip_commit(struct got_histedit_list_entry *hle,
10127 struct got_worktree *worktree, struct got_repository *repo)
10129 const struct got_error *error;
10130 struct got_commit_object *commit;
10132 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10137 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10141 error = show_histedit_progress(commit, hle, NULL);
10142 got_object_commit_close(commit);
10146 static const struct got_error *
10147 check_local_changes(void *arg, unsigned char status,
10148 unsigned char staged_status, const char *path,
10149 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10150 struct got_object_id *commit_id, int dirfd, const char *de_name)
10152 int *have_local_changes = arg;
10155 case GOT_STATUS_ADD:
10156 case GOT_STATUS_DELETE:
10157 case GOT_STATUS_MODIFY:
10158 case GOT_STATUS_CONFLICT:
10159 *have_local_changes = 1;
10160 return got_error(GOT_ERR_CANCELLED);
10165 switch (staged_status) {
10166 case GOT_STATUS_ADD:
10167 case GOT_STATUS_DELETE:
10168 case GOT_STATUS_MODIFY:
10169 *have_local_changes = 1;
10170 return got_error(GOT_ERR_CANCELLED);
10178 static const struct got_error *
10179 cmd_histedit(int argc, char *argv[])
10181 const struct got_error *error = NULL;
10182 struct got_worktree *worktree = NULL;
10183 struct got_fileindex *fileindex = NULL;
10184 struct got_repository *repo = NULL;
10186 struct got_reference *branch = NULL;
10187 struct got_reference *tmp_branch = NULL;
10188 struct got_object_id *resume_commit_id = NULL;
10189 struct got_object_id *base_commit_id = NULL;
10190 struct got_object_id *head_commit_id = NULL;
10191 struct got_commit_object *commit = NULL;
10192 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10193 struct got_update_progress_arg upa;
10194 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10195 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10196 int list_backups = 0, delete_backups = 0;
10197 const char *edit_script_path = NULL;
10198 struct got_object_id_queue commits;
10199 struct got_pathlist_head merged_paths;
10200 const struct got_object_id_queue *parent_ids;
10201 struct got_object_qid *pid;
10202 struct got_histedit_list histedit_cmds;
10203 struct got_histedit_list_entry *hle;
10205 STAILQ_INIT(&commits);
10206 TAILQ_INIT(&histedit_cmds);
10207 TAILQ_INIT(&merged_paths);
10208 memset(&upa, 0, sizeof(upa));
10210 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10225 edit_script_path = optarg;
10228 edit_logmsg_only = 1;
10234 delete_backups = 1;
10246 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10247 "unveil", NULL) == -1)
10250 if (abort_edit && continue_edit)
10251 option_conflict('a', 'c');
10252 if (edit_script_path && edit_logmsg_only)
10253 option_conflict('F', 'm');
10254 if (abort_edit && edit_logmsg_only)
10255 option_conflict('a', 'm');
10256 if (continue_edit && edit_logmsg_only)
10257 option_conflict('c', 'm');
10258 if (abort_edit && fold_only)
10259 option_conflict('a', 'f');
10260 if (continue_edit && fold_only)
10261 option_conflict('c', 'f');
10262 if (fold_only && edit_logmsg_only)
10263 option_conflict('f', 'm');
10264 if (edit_script_path && fold_only)
10265 option_conflict('F', 'f');
10266 if (abort_edit && edit_only)
10267 option_conflict('a', 'e');
10268 if (continue_edit && edit_only)
10269 option_conflict('c', 'e');
10270 if (edit_only && edit_logmsg_only)
10271 option_conflict('e', 'm');
10272 if (edit_script_path && edit_only)
10273 option_conflict('F', 'e');
10274 if (list_backups) {
10276 option_conflict('l', 'a');
10278 option_conflict('l', 'c');
10279 if (edit_script_path)
10280 option_conflict('l', 'F');
10281 if (edit_logmsg_only)
10282 option_conflict('l', 'm');
10284 option_conflict('l', 'f');
10286 option_conflict('l', 'e');
10287 if (delete_backups)
10288 option_conflict('l', 'X');
10289 if (argc != 0 && argc != 1)
10291 } else if (delete_backups) {
10293 option_conflict('X', 'a');
10295 option_conflict('X', 'c');
10296 if (edit_script_path)
10297 option_conflict('X', 'F');
10298 if (edit_logmsg_only)
10299 option_conflict('X', 'm');
10301 option_conflict('X', 'f');
10303 option_conflict('X', 'e');
10305 option_conflict('X', 'l');
10306 if (argc != 0 && argc != 1)
10308 } else if (argc != 0)
10312 * This command cannot apply unveil(2) in all cases because the
10313 * user may choose to run an editor to edit the histedit script
10314 * and to edit individual commit log messages.
10315 * unveil(2) traverses exec(2); if an editor is used we have to
10316 * apply unveil after edit script and log messages have been written.
10317 * XXX TODO: Make use of unveil(2) where possible.
10320 cwd = getcwd(NULL, 0);
10322 error = got_error_from_errno("getcwd");
10325 error = got_worktree_open(&worktree, cwd);
10327 if (list_backups || delete_backups) {
10328 if (error->code != GOT_ERR_NOT_WORKTREE)
10331 if (error->code == GOT_ERR_NOT_WORKTREE)
10332 error = wrap_not_worktree_error(error,
10338 if (list_backups || delete_backups) {
10339 error = got_repo_open(&repo,
10340 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10344 error = apply_unveil(got_repo_get_path(repo), 0,
10345 worktree ? got_worktree_get_root_path(worktree) : NULL);
10348 error = process_backup_refs(
10349 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10350 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10351 goto done; /* nothing else to do */
10354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10359 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10362 if (rebase_in_progress) {
10363 error = got_error(GOT_ERR_REBASING);
10367 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10371 if (merge_in_progress) {
10372 error = got_error(GOT_ERR_MERGE_BUSY);
10376 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10380 if (edit_in_progress && edit_logmsg_only) {
10381 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10382 "histedit operation is in progress in this "
10383 "work tree and must be continued or aborted "
10384 "before the -m option can be used");
10387 if (edit_in_progress && fold_only) {
10388 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10389 "histedit operation is in progress in this "
10390 "work tree and must be continued or aborted "
10391 "before the -f option can be used");
10394 if (edit_in_progress && edit_only) {
10395 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10396 "histedit operation is in progress in this "
10397 "work tree and must be continued or aborted "
10398 "before the -e option can be used");
10402 if (edit_in_progress && abort_edit) {
10403 error = got_worktree_histedit_continue(&resume_commit_id,
10404 &tmp_branch, &branch, &base_commit_id, &fileindex,
10408 printf("Switching work tree to %s\n",
10409 got_ref_get_symref_target(branch));
10410 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10411 branch, base_commit_id, abort_progress, &upa);
10414 printf("Histedit of %s aborted\n",
10415 got_ref_get_symref_target(branch));
10416 print_merge_progress_stats(&upa);
10417 goto done; /* nothing else to do */
10418 } else if (abort_edit) {
10419 error = got_error(GOT_ERR_NOT_HISTEDIT);
10423 if (continue_edit) {
10426 if (!edit_in_progress) {
10427 error = got_error(GOT_ERR_NOT_HISTEDIT);
10431 error = got_worktree_get_histedit_script_path(&path, worktree);
10435 error = histedit_load_list(&histedit_cmds, path, repo);
10440 error = got_worktree_histedit_continue(&resume_commit_id,
10441 &tmp_branch, &branch, &base_commit_id, &fileindex,
10446 error = got_ref_resolve(&head_commit_id, repo, branch);
10450 error = got_object_open_as_commit(&commit, repo,
10454 parent_ids = got_object_commit_get_parent_ids(commit);
10455 pid = STAILQ_FIRST(parent_ids);
10457 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10460 error = collect_commits(&commits, head_commit_id, pid->id,
10461 base_commit_id, got_worktree_get_path_prefix(worktree),
10462 GOT_ERR_HISTEDIT_PATH, repo);
10463 got_object_commit_close(commit);
10468 if (edit_in_progress) {
10469 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10473 error = got_ref_open(&branch, repo,
10474 got_worktree_get_head_ref_name(worktree), 0);
10478 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10479 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10480 "will not edit commit history of a branch outside "
10481 "the \"refs/heads/\" reference namespace");
10485 error = got_ref_resolve(&head_commit_id, repo, branch);
10486 got_ref_close(branch);
10491 error = got_object_open_as_commit(&commit, repo,
10495 parent_ids = got_object_commit_get_parent_ids(commit);
10496 pid = STAILQ_FIRST(parent_ids);
10498 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10501 error = collect_commits(&commits, head_commit_id, pid->id,
10502 got_worktree_get_base_commit_id(worktree),
10503 got_worktree_get_path_prefix(worktree),
10504 GOT_ERR_HISTEDIT_PATH, repo);
10505 got_object_commit_close(commit);
10510 if (STAILQ_EMPTY(&commits)) {
10511 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10515 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10516 &base_commit_id, &fileindex, worktree, repo);
10520 if (edit_script_path) {
10521 error = histedit_load_list(&histedit_cmds,
10522 edit_script_path, repo);
10524 got_worktree_histedit_abort(worktree, fileindex,
10525 repo, branch, base_commit_id,
10526 abort_progress, &upa);
10527 print_merge_progress_stats(&upa);
10531 const char *branch_name;
10532 branch_name = got_ref_get_symref_target(branch);
10533 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10535 error = histedit_edit_script(&histedit_cmds, &commits,
10536 branch_name, edit_logmsg_only, fold_only,
10539 got_worktree_histedit_abort(worktree, fileindex,
10540 repo, branch, base_commit_id,
10541 abort_progress, &upa);
10542 print_merge_progress_stats(&upa);
10548 error = histedit_save_list(&histedit_cmds, worktree,
10551 got_worktree_histedit_abort(worktree, fileindex,
10552 repo, branch, base_commit_id,
10553 abort_progress, &upa);
10554 print_merge_progress_stats(&upa);
10560 error = histedit_check_script(&histedit_cmds, &commits, repo);
10564 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10565 if (resume_commit_id) {
10566 if (got_object_id_cmp(hle->commit_id,
10567 resume_commit_id) != 0)
10570 resume_commit_id = NULL;
10571 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10572 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10573 error = histedit_skip_commit(hle, worktree,
10578 struct got_pathlist_head paths;
10579 int have_changes = 0;
10581 TAILQ_INIT(&paths);
10582 error = got_pathlist_append(&paths, "", NULL);
10585 error = got_worktree_status(worktree, &paths,
10586 repo, 0, check_local_changes, &have_changes,
10587 check_cancelled, NULL);
10588 got_pathlist_free(&paths);
10590 if (error->code != GOT_ERR_CANCELLED)
10592 if (sigint_received || sigpipe_received)
10595 if (have_changes) {
10596 error = histedit_commit(NULL, worktree,
10597 fileindex, tmp_branch, hle, repo);
10601 error = got_object_open_as_commit(
10602 &commit, repo, hle->commit_id);
10605 error = show_histedit_progress(commit,
10607 got_object_commit_close(commit);
10616 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10617 error = histedit_skip_commit(hle, worktree, repo);
10623 error = got_object_open_as_commit(&commit, repo,
10627 parent_ids = got_object_commit_get_parent_ids(commit);
10628 pid = STAILQ_FIRST(parent_ids);
10630 error = got_worktree_histedit_merge_files(&merged_paths,
10631 worktree, fileindex, pid->id, hle->commit_id, repo,
10632 update_progress, &upa, check_cancelled, NULL);
10635 got_object_commit_close(commit);
10638 print_merge_progress_stats(&upa);
10639 if (upa.conflicts > 0 || upa.missing > 0 ||
10640 upa.not_deleted > 0 || upa.unversioned > 0) {
10641 if (upa.conflicts > 0) {
10642 error = show_rebase_merge_conflict(
10643 hle->commit_id, repo);
10647 got_worktree_rebase_pathlist_free(&merged_paths);
10651 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10653 error = got_object_id_str(&id_str, hle->commit_id);
10656 printf("Stopping histedit for amending commit %s\n",
10659 got_worktree_rebase_pathlist_free(&merged_paths);
10660 error = got_worktree_histedit_postpone(worktree,
10665 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10666 error = histedit_skip_commit(hle, worktree, repo);
10672 error = histedit_commit(&merged_paths, worktree, fileindex,
10673 tmp_branch, hle, repo);
10674 got_worktree_rebase_pathlist_free(&merged_paths);
10679 if (upa.conflicts > 0 || upa.missing > 0 ||
10680 upa.not_deleted > 0 || upa.unversioned > 0) {
10681 error = got_worktree_histedit_postpone(worktree, fileindex);
10684 if (upa.conflicts > 0 && upa.missing == 0 &&
10685 upa.not_deleted == 0 && upa.unversioned == 0) {
10686 error = got_error_msg(GOT_ERR_CONFLICTS,
10687 "conflicts must be resolved before histedit "
10689 } else if (upa.conflicts > 0) {
10690 error = got_error_msg(GOT_ERR_CONFLICTS,
10691 "conflicts must be resolved before histedit "
10692 "can continue; changes destined for some "
10693 "files were not yet merged and should be "
10694 "merged manually if required before the "
10695 "histedit operation is continued");
10697 error = got_error_msg(GOT_ERR_CONFLICTS,
10698 "changes destined for some files were not "
10699 "yet merged and should be merged manually "
10700 "if required before the histedit operation "
10704 error = histedit_complete(worktree, fileindex, tmp_branch,
10707 got_object_id_queue_free(&commits);
10708 histedit_free_list(&histedit_cmds);
10709 free(head_commit_id);
10710 free(base_commit_id);
10711 free(resume_commit_id);
10713 got_object_commit_close(commit);
10715 got_ref_close(branch);
10717 got_ref_close(tmp_branch);
10719 got_worktree_close(worktree);
10721 const struct got_error *close_err = got_repo_close(repo);
10729 usage_integrate(void)
10731 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10735 static const struct got_error *
10736 cmd_integrate(int argc, char *argv[])
10738 const struct got_error *error = NULL;
10739 struct got_repository *repo = NULL;
10740 struct got_worktree *worktree = NULL;
10741 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10742 const char *branch_arg = NULL;
10743 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10744 struct got_fileindex *fileindex = NULL;
10745 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10747 struct got_update_progress_arg upa;
10749 while ((ch = getopt(argc, argv, "")) != -1) {
10762 branch_arg = argv[0];
10764 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10765 "unveil", NULL) == -1)
10768 cwd = getcwd(NULL, 0);
10770 error = got_error_from_errno("getcwd");
10774 error = got_worktree_open(&worktree, cwd);
10776 if (error->code == GOT_ERR_NOT_WORKTREE)
10777 error = wrap_not_worktree_error(error, "integrate",
10782 error = check_rebase_or_histedit_in_progress(worktree);
10786 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10791 error = apply_unveil(got_repo_get_path(repo), 0,
10792 got_worktree_get_root_path(worktree));
10796 error = check_merge_in_progress(worktree, repo);
10800 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10801 error = got_error_from_errno("asprintf");
10805 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10806 &base_branch_ref, worktree, refname, repo);
10810 refname = strdup(got_ref_get_name(branch_ref));
10811 if (refname == NULL) {
10812 error = got_error_from_errno("strdup");
10813 got_worktree_integrate_abort(worktree, fileindex, repo,
10814 branch_ref, base_branch_ref);
10817 base_refname = strdup(got_ref_get_name(base_branch_ref));
10818 if (base_refname == NULL) {
10819 error = got_error_from_errno("strdup");
10820 got_worktree_integrate_abort(worktree, fileindex, repo,
10821 branch_ref, base_branch_ref);
10825 error = got_ref_resolve(&commit_id, repo, branch_ref);
10829 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10833 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10834 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10835 "specified branch has already been integrated");
10836 got_worktree_integrate_abort(worktree, fileindex, repo,
10837 branch_ref, base_branch_ref);
10841 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10843 if (error->code == GOT_ERR_ANCESTRY)
10844 error = got_error(GOT_ERR_REBASE_REQUIRED);
10845 got_worktree_integrate_abort(worktree, fileindex, repo,
10846 branch_ref, base_branch_ref);
10850 memset(&upa, 0, sizeof(upa));
10851 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10852 branch_ref, base_branch_ref, update_progress, &upa,
10853 check_cancelled, NULL);
10857 printf("Integrated %s into %s\n", refname, base_refname);
10858 print_update_progress_stats(&upa);
10861 const struct got_error *close_err = got_repo_close(repo);
10866 got_worktree_close(worktree);
10868 free(base_commit_id);
10871 free(base_refname);
10878 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10883 static const struct got_error *
10884 cmd_merge(int argc, char *argv[])
10886 const struct got_error *error = NULL;
10887 struct got_worktree *worktree = NULL;
10888 struct got_repository *repo = NULL;
10889 struct got_fileindex *fileindex = NULL;
10890 char *cwd = NULL, *id_str = NULL, *author = NULL;
10891 struct got_reference *branch = NULL, *wt_branch = NULL;
10892 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
10893 struct got_object_id *wt_branch_tip = NULL;
10894 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
10895 int interrupt_merge = 0;
10896 struct got_update_progress_arg upa;
10897 struct got_object_id *merge_commit_id = NULL;
10898 char *branch_name = NULL;
10900 memset(&upa, 0, sizeof(upa));
10902 while ((ch = getopt(argc, argv, "acn")) != -1) {
10908 continue_merge = 1;
10911 interrupt_merge = 1;
10923 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10924 "unveil", NULL) == -1)
10928 if (abort_merge && continue_merge)
10929 option_conflict('a', 'c');
10930 if (abort_merge || continue_merge) {
10933 } else if (argc != 1)
10936 cwd = getcwd(NULL, 0);
10938 error = got_error_from_errno("getcwd");
10942 error = got_worktree_open(&worktree, cwd);
10944 if (error->code == GOT_ERR_NOT_WORKTREE)
10945 error = wrap_not_worktree_error(error,
10950 error = got_repo_open(&repo,
10951 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
10955 error = apply_unveil(got_repo_get_path(repo), 0,
10956 worktree ? got_worktree_get_root_path(worktree) : NULL);
10960 error = check_rebase_or_histedit_in_progress(worktree);
10964 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10970 if (!merge_in_progress) {
10971 error = got_error(GOT_ERR_NOT_MERGING);
10974 error = got_worktree_merge_continue(&branch_name,
10975 &branch_tip, &fileindex, worktree, repo);
10978 error = got_worktree_merge_abort(worktree, fileindex, repo,
10979 abort_progress, &upa);
10982 printf("Merge of %s aborted\n", branch_name);
10983 goto done; /* nothing else to do */
10986 error = get_author(&author, repo, worktree);
10990 if (continue_merge) {
10991 if (!merge_in_progress) {
10992 error = got_error(GOT_ERR_NOT_MERGING);
10995 error = got_worktree_merge_continue(&branch_name,
10996 &branch_tip, &fileindex, worktree, repo);
11000 error = got_ref_open(&branch, repo, argv[0], 0);
11003 branch_name = strdup(got_ref_get_name(branch));
11004 if (branch_name == NULL) {
11005 error = got_error_from_errno("strdup");
11008 error = got_ref_resolve(&branch_tip, repo, branch);
11013 error = got_ref_open(&wt_branch, repo,
11014 got_worktree_get_head_ref_name(worktree), 0);
11017 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11020 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11021 wt_branch_tip, branch_tip, 0, repo,
11022 check_cancelled, NULL);
11023 if (error && error->code != GOT_ERR_ANCESTRY)
11026 if (!continue_merge) {
11027 error = check_path_prefix(wt_branch_tip, branch_tip,
11028 got_worktree_get_path_prefix(worktree),
11029 GOT_ERR_MERGE_PATH, repo);
11033 error = check_same_branch(wt_branch_tip, branch,
11036 if (error->code != GOT_ERR_ANCESTRY)
11040 static char msg[512];
11041 snprintf(msg, sizeof(msg),
11042 "cannot create a merge commit because "
11043 "%s is based on %s; %s can be integrated "
11044 "with 'got integrate' instead", branch_name,
11045 got_worktree_get_head_ref_name(worktree),
11047 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11051 error = got_worktree_merge_prepare(&fileindex, worktree,
11056 error = got_worktree_merge_branch(worktree, fileindex,
11057 yca_id, branch_tip, repo, update_progress, &upa,
11058 check_cancelled, NULL);
11061 print_merge_progress_stats(&upa);
11062 if (!upa.did_something) {
11063 error = got_worktree_merge_abort(worktree, fileindex,
11064 repo, abort_progress, &upa);
11067 printf("Already up-to-date\n");
11072 if (interrupt_merge) {
11073 error = got_worktree_merge_postpone(worktree, fileindex);
11076 printf("Merge of %s interrupted on request\n", branch_name);
11077 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11078 upa.not_deleted > 0 || upa.unversioned > 0) {
11079 error = got_worktree_merge_postpone(worktree, fileindex);
11082 if (upa.conflicts > 0 && upa.missing == 0 &&
11083 upa.not_deleted == 0 && upa.unversioned == 0) {
11084 error = got_error_msg(GOT_ERR_CONFLICTS,
11085 "conflicts must be resolved before merging "
11087 } else if (upa.conflicts > 0) {
11088 error = got_error_msg(GOT_ERR_CONFLICTS,
11089 "conflicts must be resolved before merging "
11090 "can continue; changes destined for some "
11091 "files were not yet merged and "
11092 "should be merged manually if required before the "
11093 "merge operation is continued");
11095 error = got_error_msg(GOT_ERR_CONFLICTS,
11096 "changes destined for some "
11097 "files were not yet merged and should be "
11098 "merged manually if required before the "
11099 "merge operation is continued");
11103 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11104 fileindex, author, NULL, 1, branch_tip, branch_name,
11105 repo, continue_merge ? print_status : NULL, NULL);
11108 error = got_worktree_merge_complete(worktree, fileindex, repo);
11111 error = got_object_id_str(&id_str, merge_commit_id);
11114 printf("Merged %s into %s: %s\n", branch_name,
11115 got_worktree_get_head_ref_name(worktree),
11121 free(merge_commit_id);
11127 got_ref_close(branch);
11129 got_ref_close(wt_branch);
11131 got_worktree_close(worktree);
11133 const struct got_error *close_err = got_repo_close(repo);
11143 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11144 "[-S] [file-path ...]\n",
11149 static const struct got_error *
11150 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11151 const char *path, struct got_object_id *blob_id,
11152 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11153 int dirfd, const char *de_name)
11155 const struct got_error *err = NULL;
11156 char *id_str = NULL;
11158 if (staged_status != GOT_STATUS_ADD &&
11159 staged_status != GOT_STATUS_MODIFY &&
11160 staged_status != GOT_STATUS_DELETE)
11163 if (staged_status == GOT_STATUS_ADD ||
11164 staged_status == GOT_STATUS_MODIFY)
11165 err = got_object_id_str(&id_str, staged_blob_id);
11167 err = got_object_id_str(&id_str, blob_id);
11171 printf("%s %c %s\n", id_str, staged_status, path);
11176 static const struct got_error *
11177 cmd_stage(int argc, char *argv[])
11179 const struct got_error *error = NULL;
11180 struct got_repository *repo = NULL;
11181 struct got_worktree *worktree = NULL;
11183 struct got_pathlist_head paths;
11184 struct got_pathlist_entry *pe;
11185 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11186 FILE *patch_script_file = NULL;
11187 const char *patch_script_path = NULL;
11188 struct choose_patch_arg cpa;
11190 TAILQ_INIT(&paths);
11192 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11201 patch_script_path = optarg;
11204 allow_bad_symlinks = 1;
11216 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11217 "unveil", NULL) == -1)
11220 if (list_stage && (pflag || patch_script_path))
11221 errx(1, "-l option cannot be used with other options");
11222 if (patch_script_path && !pflag)
11223 errx(1, "-F option can only be used together with -p option");
11225 cwd = getcwd(NULL, 0);
11227 error = got_error_from_errno("getcwd");
11231 error = got_worktree_open(&worktree, cwd);
11233 if (error->code == GOT_ERR_NOT_WORKTREE)
11234 error = wrap_not_worktree_error(error, "stage", cwd);
11238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11243 if (patch_script_path) {
11244 patch_script_file = fopen(patch_script_path, "r");
11245 if (patch_script_file == NULL) {
11246 error = got_error_from_errno2("fopen",
11247 patch_script_path);
11251 error = apply_unveil(got_repo_get_path(repo), 0,
11252 got_worktree_get_root_path(worktree));
11256 error = check_merge_in_progress(worktree, repo);
11260 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11265 error = got_worktree_status(worktree, &paths, repo, 0,
11266 print_stage, NULL, check_cancelled, NULL);
11268 cpa.patch_script_file = patch_script_file;
11269 cpa.action = "stage";
11270 error = got_worktree_stage(worktree, &paths,
11271 pflag ? NULL : print_status, NULL,
11272 pflag ? choose_patch : NULL, &cpa,
11273 allow_bad_symlinks, repo);
11276 if (patch_script_file && fclose(patch_script_file) == EOF &&
11278 error = got_error_from_errno2("fclose", patch_script_path);
11280 const struct got_error *close_err = got_repo_close(repo);
11285 got_worktree_close(worktree);
11286 TAILQ_FOREACH(pe, &paths, entry)
11287 free((char *)pe->path);
11288 got_pathlist_free(&paths);
11294 usage_unstage(void)
11296 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11297 "[file-path ...]\n",
11303 static const struct got_error *
11304 cmd_unstage(int argc, char *argv[])
11306 const struct got_error *error = NULL;
11307 struct got_repository *repo = NULL;
11308 struct got_worktree *worktree = NULL;
11310 struct got_pathlist_head paths;
11311 struct got_pathlist_entry *pe;
11313 struct got_update_progress_arg upa;
11314 FILE *patch_script_file = NULL;
11315 const char *patch_script_path = NULL;
11316 struct choose_patch_arg cpa;
11318 TAILQ_INIT(&paths);
11320 while ((ch = getopt(argc, argv, "pF:")) != -1) {
11326 patch_script_path = optarg;
11338 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11339 "unveil", NULL) == -1)
11342 if (patch_script_path && !pflag)
11343 errx(1, "-F option can only be used together with -p option");
11345 cwd = getcwd(NULL, 0);
11347 error = got_error_from_errno("getcwd");
11351 error = got_worktree_open(&worktree, cwd);
11353 if (error->code == GOT_ERR_NOT_WORKTREE)
11354 error = wrap_not_worktree_error(error, "unstage", cwd);
11358 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11363 if (patch_script_path) {
11364 patch_script_file = fopen(patch_script_path, "r");
11365 if (patch_script_file == NULL) {
11366 error = got_error_from_errno2("fopen",
11367 patch_script_path);
11372 error = apply_unveil(got_repo_get_path(repo), 0,
11373 got_worktree_get_root_path(worktree));
11377 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11381 cpa.patch_script_file = patch_script_file;
11382 cpa.action = "unstage";
11383 memset(&upa, 0, sizeof(upa));
11384 error = got_worktree_unstage(worktree, &paths, update_progress,
11385 &upa, pflag ? choose_patch : NULL, &cpa, repo);
11387 print_merge_progress_stats(&upa);
11389 if (patch_script_file && fclose(patch_script_file) == EOF &&
11391 error = got_error_from_errno2("fclose", patch_script_path);
11393 const struct got_error *close_err = got_repo_close(repo);
11398 got_worktree_close(worktree);
11399 TAILQ_FOREACH(pe, &paths, entry)
11400 free((char *)pe->path);
11401 got_pathlist_free(&paths);
11409 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11410 "arg1 [arg2 ...]\n", getprogname());
11414 static const struct got_error *
11415 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11417 const struct got_error *err;
11418 struct got_blob_object *blob;
11420 err = got_object_open_as_blob(&blob, repo, id, 8192);
11424 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11425 got_object_blob_close(blob);
11429 static const struct got_error *
11430 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11432 const struct got_error *err;
11433 struct got_tree_object *tree;
11436 err = got_object_open_as_tree(&tree, repo, id);
11440 nentries = got_object_tree_get_nentries(tree);
11441 for (i = 0; i < nentries; i++) {
11442 struct got_tree_entry *te;
11444 if (sigint_received || sigpipe_received)
11446 te = got_object_tree_get_entry(tree, i);
11447 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11450 fprintf(outfile, "%s %.7o %s\n", id_str,
11451 got_tree_entry_get_mode(te),
11452 got_tree_entry_get_name(te));
11456 got_object_tree_close(tree);
11460 static const struct got_error *
11461 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11463 const struct got_error *err;
11464 struct got_commit_object *commit;
11465 const struct got_object_id_queue *parent_ids;
11466 struct got_object_qid *pid;
11467 char *id_str = NULL;
11468 const char *logmsg = NULL;
11470 err = got_object_open_as_commit(&commit, repo, id);
11474 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11478 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11479 parent_ids = got_object_commit_get_parent_ids(commit);
11480 fprintf(outfile, "numparents %d\n",
11481 got_object_commit_get_nparents(commit));
11482 STAILQ_FOREACH(pid, parent_ids, entry) {
11484 err = got_object_id_str(&pid_str, pid->id);
11487 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11490 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
11491 got_object_commit_get_author(commit),
11492 (long long)got_object_commit_get_author_time(commit));
11494 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
11495 got_object_commit_get_author(commit),
11496 (long long)got_object_commit_get_committer_time(commit));
11498 logmsg = got_object_commit_get_logmsg_raw(commit);
11499 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11500 fprintf(outfile, "%s", logmsg);
11503 got_object_commit_close(commit);
11507 static const struct got_error *
11508 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11510 const struct got_error *err;
11511 struct got_tag_object *tag;
11512 char *id_str = NULL;
11513 const char *tagmsg = NULL;
11515 err = got_object_open_as_tag(&tag, repo, id);
11519 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11523 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11525 switch (got_object_tag_get_object_type(tag)) {
11526 case GOT_OBJ_TYPE_BLOB:
11527 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11528 GOT_OBJ_LABEL_BLOB);
11530 case GOT_OBJ_TYPE_TREE:
11531 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11532 GOT_OBJ_LABEL_TREE);
11534 case GOT_OBJ_TYPE_COMMIT:
11535 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11536 GOT_OBJ_LABEL_COMMIT);
11538 case GOT_OBJ_TYPE_TAG:
11539 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11540 GOT_OBJ_LABEL_TAG);
11546 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11547 got_object_tag_get_name(tag));
11549 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
11550 got_object_tag_get_tagger(tag),
11551 (long long)got_object_tag_get_tagger_time(tag));
11553 tagmsg = got_object_tag_get_message(tag);
11554 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11555 fprintf(outfile, "%s", tagmsg);
11558 got_object_tag_close(tag);
11562 static const struct got_error *
11563 cmd_cat(int argc, char *argv[])
11565 const struct got_error *error;
11566 struct got_repository *repo = NULL;
11567 struct got_worktree *worktree = NULL;
11568 char *cwd = NULL, *repo_path = NULL, *label = NULL;
11569 const char *commit_id_str = NULL;
11570 struct got_object_id *id = NULL, *commit_id = NULL;
11571 int ch, obj_type, i, force_path = 0;
11572 struct got_reflist_head refs;
11577 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11582 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11585 commit_id_str = optarg;
11588 repo_path = realpath(optarg, NULL);
11589 if (repo_path == NULL)
11590 return got_error_from_errno2("realpath",
11592 got_path_strip_trailing_slashes(repo_path);
11606 cwd = getcwd(NULL, 0);
11608 error = got_error_from_errno("getcwd");
11611 error = got_worktree_open(&worktree, cwd);
11612 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11615 if (repo_path == NULL) {
11616 repo_path = strdup(
11617 got_worktree_get_repo_path(worktree));
11618 if (repo_path == NULL) {
11619 error = got_error_from_errno("strdup");
11625 if (repo_path == NULL) {
11626 repo_path = getcwd(NULL, 0);
11627 if (repo_path == NULL)
11628 return got_error_from_errno("getcwd");
11631 error = got_repo_open(&repo, repo_path, NULL);
11636 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11640 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11644 if (commit_id_str == NULL)
11645 commit_id_str = GOT_REF_HEAD;
11646 error = got_repo_match_object_id(&commit_id, NULL,
11647 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11651 for (i = 0; i < argc; i++) {
11653 error = got_object_id_by_path(&id, repo, commit_id,
11658 error = got_repo_match_object_id(&id, &label, argv[i],
11659 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11662 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11663 error->code != GOT_ERR_NOT_REF)
11665 error = got_object_id_by_path(&id, repo,
11666 commit_id, argv[i]);
11672 error = got_object_get_type(&obj_type, repo, id);
11676 switch (obj_type) {
11677 case GOT_OBJ_TYPE_BLOB:
11678 error = cat_blob(id, repo, stdout);
11680 case GOT_OBJ_TYPE_TREE:
11681 error = cat_tree(id, repo, stdout);
11683 case GOT_OBJ_TYPE_COMMIT:
11684 error = cat_commit(id, repo, stdout);
11686 case GOT_OBJ_TYPE_TAG:
11687 error = cat_tag(id, repo, stdout);
11690 error = got_error(GOT_ERR_OBJ_TYPE);
11705 got_worktree_close(worktree);
11707 const struct got_error *close_err = got_repo_close(repo);
11711 got_ref_list_free(&refs);
11718 fprintf(stderr, "usage: %s info [path ...]\n",
11723 static const struct got_error *
11724 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11725 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11726 struct got_object_id *commit_id)
11728 const struct got_error *err = NULL;
11729 char *id_str = NULL;
11731 struct tm mytm, *tm;
11732 struct got_pathlist_head *paths = arg;
11733 struct got_pathlist_entry *pe;
11736 * Clear error indication from any of the path arguments which
11737 * would cause this file index entry to be displayed.
11739 TAILQ_FOREACH(pe, paths, entry) {
11740 if (got_path_cmp(path, pe->path, strlen(path),
11741 pe->path_len) == 0 ||
11742 got_path_is_child(path, pe->path, pe->path_len))
11743 pe->data = NULL; /* no error */
11746 printf(GOT_COMMIT_SEP_STR);
11748 printf("symlink: %s\n", path);
11749 else if (S_ISREG(mode)) {
11750 printf("file: %s\n", path);
11751 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11752 } else if (S_ISDIR(mode))
11753 printf("directory: %s\n", path);
11755 printf("something: %s\n", path);
11757 tm = localtime_r(&mtime, &mytm);
11760 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11761 return got_error(GOT_ERR_NO_SPACE);
11762 printf("timestamp: %s\n", datebuf);
11765 err = got_object_id_str(&id_str, blob_id);
11768 printf("based on blob: %s\n", id_str);
11772 if (staged_blob_id) {
11773 err = got_object_id_str(&id_str, staged_blob_id);
11776 printf("based on staged blob: %s\n", id_str);
11781 err = got_object_id_str(&id_str, commit_id);
11784 printf("based on commit: %s\n", id_str);
11791 static const struct got_error *
11792 cmd_info(int argc, char *argv[])
11794 const struct got_error *error = NULL;
11795 struct got_worktree *worktree = NULL;
11796 char *cwd = NULL, *id_str = NULL;
11797 struct got_pathlist_head paths;
11798 struct got_pathlist_entry *pe;
11799 char *uuidstr = NULL;
11800 int ch, show_files = 0;
11802 TAILQ_INIT(&paths);
11804 while ((ch = getopt(argc, argv, "")) != -1) {
11816 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11820 cwd = getcwd(NULL, 0);
11822 error = got_error_from_errno("getcwd");
11826 error = got_worktree_open(&worktree, cwd);
11828 if (error->code == GOT_ERR_NOT_WORKTREE)
11829 error = wrap_not_worktree_error(error, "info", cwd);
11833 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11838 error = get_worktree_paths_from_argv(&paths, argc, argv,
11845 error = got_object_id_str(&id_str,
11846 got_worktree_get_base_commit_id(worktree));
11850 error = got_worktree_get_uuid(&uuidstr, worktree);
11854 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11855 printf("work tree base commit: %s\n", id_str);
11856 printf("work tree path prefix: %s\n",
11857 got_worktree_get_path_prefix(worktree));
11858 printf("work tree branch reference: %s\n",
11859 got_worktree_get_head_ref_name(worktree));
11860 printf("work tree UUID: %s\n", uuidstr);
11861 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11864 struct got_pathlist_entry *pe;
11865 TAILQ_FOREACH(pe, &paths, entry) {
11866 if (pe->path_len == 0)
11869 * Assume this path will fail. This will be corrected
11870 * in print_path_info() in case the path does suceeed.
11872 pe->data = (void *)got_error_path(pe->path,
11875 error = got_worktree_path_info(worktree, &paths,
11876 print_path_info, &paths, check_cancelled, NULL);
11879 TAILQ_FOREACH(pe, &paths, entry) {
11880 if (pe->data != NULL) {
11881 error = pe->data; /* bad path */
11887 TAILQ_FOREACH(pe, &paths, entry)
11888 free((char *)pe->path);
11889 got_pathlist_free(&paths);