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/types.h>
40 #include "got_compat.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 error = got_object_id_str(&id_str, pack_hash);
1665 printf("\nFetched %s.pack\n", id_str);
1668 /* Set up references provided with the pack file. */
1669 TAILQ_FOREACH(pe, &refs, entry) {
1670 const char *refname = pe->path;
1671 struct got_object_id *id = pe->data;
1672 char *remote_refname;
1674 if (is_wanted_ref(&wanted_refs, refname) &&
1675 !mirror_references) {
1676 error = create_wanted_ref(refname, id,
1677 GOT_FETCH_DEFAULT_REMOTE_NAME,
1678 verbosity - 1, repo);
1684 error = create_ref(refname, id, verbosity - 1, repo);
1688 if (mirror_references)
1691 if (strncmp("refs/heads/", refname, 11) != 0)
1694 if (asprintf(&remote_refname,
1695 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1696 refname + 11) == -1) {
1697 error = got_error_from_errno("asprintf");
1700 error = create_ref(remote_refname, id, verbosity - 1, repo);
1701 free(remote_refname);
1706 /* Set the HEAD reference if the server provided one. */
1707 TAILQ_FOREACH(pe, &symrefs, entry) {
1708 struct got_reference *target_ref;
1709 const char *refname = pe->path;
1710 const char *target = pe->data;
1711 char *remote_refname = NULL, *remote_target = NULL;
1713 if (strcmp(refname, GOT_REF_HEAD) != 0)
1716 error = got_ref_open(&target_ref, repo, target, 0);
1718 if (error->code == GOT_ERR_NOT_REF) {
1725 error = create_symref(refname, target_ref, verbosity, repo);
1726 got_ref_close(target_ref);
1730 if (mirror_references)
1733 if (strncmp("refs/heads/", target, 11) != 0)
1736 if (asprintf(&remote_refname,
1737 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 error = got_error_from_errno("asprintf");
1742 if (asprintf(&remote_target,
1743 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 target + 11) == -1) {
1745 error = got_error_from_errno("asprintf");
1746 free(remote_refname);
1749 error = got_ref_open(&target_ref, repo, remote_target, 0);
1751 free(remote_refname);
1752 free(remote_target);
1753 if (error->code == GOT_ERR_NOT_REF) {
1759 error = create_symref(remote_refname, target_ref,
1760 verbosity - 1, repo);
1761 free(remote_refname);
1762 free(remote_target);
1763 got_ref_close(target_ref);
1769 * We failed to set the HEAD reference. If we asked for
1770 * a set of wanted branches use the first of one of those
1771 * which could be fetched instead.
1773 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1774 const char *target = pe->path;
1775 struct got_reference *target_ref;
1777 error = got_ref_open(&target_ref, repo, target, 0);
1779 if (error->code == GOT_ERR_NOT_REF) {
1786 error = create_symref(GOT_REF_HEAD, target_ref,
1788 got_ref_close(target_ref);
1796 printf("Created %s repository '%s'\n",
1797 mirror_references ? "mirrored" : "cloned", repo_path);
1800 if (kill(fetchpid, SIGTERM) == -1)
1801 error = got_error_from_errno("kill");
1802 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1803 error = got_error_from_errno("waitpid");
1805 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1806 error = got_error_from_errno("close");
1808 const struct got_error *close_err = got_repo_close(repo);
1812 TAILQ_FOREACH(pe, &refs, entry) {
1813 free((void *)pe->path);
1816 got_pathlist_free(&refs);
1817 TAILQ_FOREACH(pe, &symrefs, entry) {
1818 free((void *)pe->path);
1821 got_pathlist_free(&symrefs);
1822 got_pathlist_free(&wanted_branches);
1823 got_pathlist_free(&wanted_refs);
1830 free(default_destdir);
1835 static const struct got_error *
1836 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1837 int replace_tags, int verbosity, struct got_repository *repo)
1839 const struct got_error *err = NULL;
1840 char *new_id_str = NULL;
1841 struct got_object_id *old_id = NULL;
1843 err = got_object_id_str(&new_id_str, new_id);
1847 if (!replace_tags &&
1848 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1849 err = got_ref_resolve(&old_id, repo, ref);
1852 if (got_object_id_cmp(old_id, new_id) == 0)
1854 if (verbosity >= 0) {
1855 printf("Rejecting update of existing tag %s: %s\n",
1856 got_ref_get_name(ref), new_id_str);
1861 if (got_ref_is_symbolic(ref)) {
1862 if (verbosity >= 0) {
1863 printf("Replacing reference %s: %s\n",
1864 got_ref_get_name(ref),
1865 got_ref_get_symref_target(ref));
1867 err = got_ref_change_symref_to_ref(ref, new_id);
1870 err = got_ref_write(ref, repo);
1874 err = got_ref_resolve(&old_id, repo, ref);
1877 if (got_object_id_cmp(old_id, new_id) == 0)
1880 err = got_ref_change_ref(ref, new_id);
1883 err = got_ref_write(ref, repo);
1889 printf("Updated %s: %s\n", got_ref_get_name(ref),
1897 static const struct got_error *
1898 update_symref(const char *refname, struct got_reference *target_ref,
1899 int verbosity, struct got_repository *repo)
1901 const struct got_error *err = NULL, *unlock_err;
1902 struct got_reference *symref;
1903 int symref_is_locked = 0;
1905 err = got_ref_open(&symref, repo, refname, 1);
1907 if (err->code != GOT_ERR_NOT_REF)
1909 err = got_ref_alloc_symref(&symref, refname, target_ref);
1913 err = got_ref_write(symref, repo);
1918 printf("Created reference %s: %s\n",
1919 got_ref_get_name(symref),
1920 got_ref_get_symref_target(symref));
1922 symref_is_locked = 1;
1924 if (strcmp(got_ref_get_symref_target(symref),
1925 got_ref_get_name(target_ref)) == 0)
1928 err = got_ref_change_symref(symref,
1929 got_ref_get_name(target_ref));
1933 err = got_ref_write(symref, repo);
1938 printf("Updated %s: %s\n", got_ref_get_name(symref),
1939 got_ref_get_symref_target(symref));
1943 if (symref_is_locked) {
1944 unlock_err = got_ref_unlock(symref);
1945 if (unlock_err && err == NULL)
1948 got_ref_close(symref);
1955 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 "[remote-repository-name]\n",
1962 static const struct got_error *
1963 delete_missing_ref(struct got_reference *ref,
1964 int verbosity, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_object_id *id = NULL;
1968 char *id_str = NULL;
1970 if (got_ref_is_symbolic(ref)) {
1971 err = got_ref_delete(ref, repo);
1974 if (verbosity >= 0) {
1975 printf("Deleted %s: %s\n",
1976 got_ref_get_name(ref),
1977 got_ref_get_symref_target(ref));
1980 err = got_ref_resolve(&id, repo, ref);
1983 err = got_object_id_str(&id_str, id);
1987 err = got_ref_delete(ref, repo);
1990 if (verbosity >= 0) {
1991 printf("Deleted %s: %s\n",
1992 got_ref_get_name(ref), id_str);
2001 static const struct got_error *
2002 delete_missing_refs(struct got_pathlist_head *their_refs,
2003 struct got_pathlist_head *their_symrefs,
2004 const struct got_remote_repo *remote,
2005 int verbosity, struct got_repository *repo)
2007 const struct got_error *err = NULL, *unlock_err;
2008 struct got_reflist_head my_refs;
2009 struct got_reflist_entry *re;
2010 struct got_pathlist_entry *pe;
2011 char *remote_namespace = NULL;
2012 char *local_refname = NULL;
2014 TAILQ_INIT(&my_refs);
2016 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2018 return got_error_from_errno("asprintf");
2020 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2024 TAILQ_FOREACH(re, &my_refs, entry) {
2025 const char *refname = got_ref_get_name(re->ref);
2026 const char *their_refname;
2028 if (remote->mirror_references) {
2029 their_refname = refname;
2031 if (strncmp(refname, remote_namespace,
2032 strlen(remote_namespace)) == 0) {
2033 if (strcmp(refname + strlen(remote_namespace),
2036 if (asprintf(&local_refname, "refs/heads/%s",
2037 refname + strlen(remote_namespace)) == -1) {
2038 err = got_error_from_errno("asprintf");
2041 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2044 their_refname = local_refname;
2047 TAILQ_FOREACH(pe, their_refs, entry) {
2048 if (strcmp(their_refname, pe->path) == 0)
2054 TAILQ_FOREACH(pe, their_symrefs, entry) {
2055 if (strcmp(their_refname, pe->path) == 0)
2061 err = delete_missing_ref(re->ref, verbosity, repo);
2065 if (local_refname) {
2066 struct got_reference *ref;
2067 err = got_ref_open(&ref, repo, local_refname, 1);
2069 if (err->code != GOT_ERR_NOT_REF)
2071 free(local_refname);
2072 local_refname = NULL;
2075 err = delete_missing_ref(ref, verbosity, repo);
2078 unlock_err = got_ref_unlock(ref);
2080 if (unlock_err && err == NULL) {
2085 free(local_refname);
2086 local_refname = NULL;
2090 free(remote_namespace);
2091 free(local_refname);
2095 static const struct got_error *
2096 update_wanted_ref(const char *refname, struct got_object_id *id,
2097 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2099 const struct got_error *err, *unlock_err;
2100 char *remote_refname;
2101 struct got_reference *ref;
2103 if (strncmp("refs/", refname, 5) == 0)
2106 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2107 remote_repo_name, refname) == -1)
2108 return got_error_from_errno("asprintf");
2110 err = got_ref_open(&ref, repo, remote_refname, 1);
2112 if (err->code != GOT_ERR_NOT_REF)
2114 err = create_ref(remote_refname, id, verbosity, repo);
2116 err = update_ref(ref, id, 0, verbosity, repo);
2117 unlock_err = got_ref_unlock(ref);
2118 if (unlock_err && err == NULL)
2123 free(remote_refname);
2127 static const struct got_error *
2128 delete_ref(struct got_repository *repo, struct got_reference *ref)
2130 const struct got_error *err = NULL;
2131 struct got_object_id *id = NULL;
2132 char *id_str = NULL;
2135 if (got_ref_is_symbolic(ref)) {
2136 target = got_ref_get_symref_target(ref);
2138 err = got_ref_resolve(&id, repo, ref);
2141 err = got_object_id_str(&id_str, id);
2147 err = got_ref_delete(ref, repo);
2151 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2158 static const struct got_error *
2159 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2161 const struct got_error *err = NULL;
2162 struct got_reflist_head refs;
2163 struct got_reflist_entry *re;
2168 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2169 err = got_error_from_errno("asprintf");
2172 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2176 TAILQ_FOREACH(re, &refs, entry)
2177 delete_ref(repo, re->ref);
2179 got_ref_list_free(&refs);
2183 static const struct got_error *
2184 cmd_fetch(int argc, char *argv[])
2186 const struct got_error *error = NULL, *unlock_err;
2187 char *cwd = NULL, *repo_path = NULL;
2188 const char *remote_name;
2189 char *proto = NULL, *host = NULL, *port = NULL;
2190 char *repo_name = NULL, *server_path = NULL;
2191 const struct got_remote_repo *remotes, *remote = NULL;
2193 char *id_str = NULL;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2196 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2197 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2198 struct got_pathlist_entry *pe;
2199 struct got_object_id *pack_hash = NULL;
2200 int i, ch, fetchfd = -1, fetchstatus;
2201 pid_t fetchpid = -1;
2202 struct got_fetch_progress_arg fpa;
2203 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2204 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2207 TAILQ_INIT(&symrefs);
2208 TAILQ_INIT(&wanted_branches);
2209 TAILQ_INIT(&wanted_refs);
2211 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2214 fetch_all_branches = 1;
2217 error = got_pathlist_append(&wanted_branches,
2229 repo_path = realpath(optarg, NULL);
2230 if (repo_path == NULL)
2231 return got_error_from_errno2("realpath",
2233 got_path_strip_trailing_slashes(repo_path);
2241 else if (verbosity < 3)
2248 error = got_pathlist_append(&wanted_refs,
2264 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2265 option_conflict('a', 'b');
2266 if (list_refs_only) {
2267 if (!TAILQ_EMPTY(&wanted_branches))
2268 option_conflict('l', 'b');
2269 if (fetch_all_branches)
2270 option_conflict('l', 'a');
2272 option_conflict('l', 'd');
2274 option_conflict('l', 'X');
2276 if (delete_remote) {
2277 if (fetch_all_branches)
2278 option_conflict('X', 'a');
2279 if (!TAILQ_EMPTY(&wanted_branches))
2280 option_conflict('X', 'b');
2282 option_conflict('X', 'd');
2284 option_conflict('X', 't');
2285 if (!TAILQ_EMPTY(&wanted_refs))
2286 option_conflict('X', 'R');
2291 errx(1, "-X option requires a remote name");
2292 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2293 } else if (argc == 1)
2294 remote_name = argv[0];
2298 cwd = getcwd(NULL, 0);
2300 error = got_error_from_errno("getcwd");
2304 if (repo_path == NULL) {
2305 error = got_worktree_open(&worktree, cwd);
2306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2312 strdup(got_worktree_get_repo_path(worktree));
2313 if (repo_path == NULL)
2314 error = got_error_from_errno("strdup");
2318 repo_path = strdup(cwd);
2319 if (repo_path == NULL) {
2320 error = got_error_from_errno("strdup");
2326 error = got_repo_open(&repo, repo_path, NULL);
2330 if (delete_remote) {
2331 error = delete_refs_for_remote(repo, remote_name);
2332 goto done; /* nothing else to do */
2336 worktree_conf = got_worktree_get_gotconfig(worktree);
2337 if (worktree_conf) {
2338 got_gotconfig_get_remotes(&nremotes, &remotes,
2340 for (i = 0; i < nremotes; i++) {
2341 if (strcmp(remotes[i].name, remote_name) == 0) {
2342 remote = &remotes[i];
2348 if (remote == NULL) {
2349 repo_conf = got_repo_get_gotconfig(repo);
2351 got_gotconfig_get_remotes(&nremotes, &remotes,
2353 for (i = 0; i < nremotes; i++) {
2354 if (strcmp(remotes[i].name, remote_name) == 0) {
2355 remote = &remotes[i];
2361 if (remote == NULL) {
2362 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2363 for (i = 0; i < nremotes; i++) {
2364 if (strcmp(remotes[i].name, remote_name) == 0) {
2365 remote = &remotes[i];
2370 if (remote == NULL) {
2371 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2375 if (TAILQ_EMPTY(&wanted_branches)) {
2376 if (!fetch_all_branches)
2377 fetch_all_branches = remote->fetch_all_branches;
2378 for (i = 0; i < remote->nfetch_branches; i++) {
2379 got_pathlist_append(&wanted_branches,
2380 remote->fetch_branches[i], NULL);
2383 if (TAILQ_EMPTY(&wanted_refs)) {
2384 for (i = 0; i < remote->nfetch_refs; i++) {
2385 got_pathlist_append(&wanted_refs,
2386 remote->fetch_refs[i], NULL);
2390 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2391 &repo_name, remote->fetch_url);
2395 if (strcmp(proto, "git") == 0) {
2397 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2398 "sendfd dns inet unveil", NULL) == -1)
2401 } else if (strcmp(proto, "git+ssh") == 0 ||
2402 strcmp(proto, "ssh") == 0) {
2404 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2405 "sendfd unveil", NULL) == -1)
2408 } else if (strcmp(proto, "http") == 0 ||
2409 strcmp(proto, "git+http") == 0) {
2410 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2413 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2417 error = got_dial_apply_unveil(proto);
2421 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2426 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2427 port ? ":" : "", port ? port : "");
2429 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2430 server_path, verbosity);
2434 fpa.last_scaled_size[0] = '\0';
2435 fpa.last_p_indexed = -1;
2436 fpa.last_p_resolved = -1;
2437 fpa.verbosity = verbosity;
2439 fpa.create_configs = 0;
2440 fpa.configs_created = 0;
2441 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2442 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2443 remote->mirror_references, fetch_all_branches, &wanted_branches,
2444 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2445 fetch_progress, &fpa);
2449 if (list_refs_only) {
2450 error = list_remote_refs(&symrefs, &refs);
2454 if (pack_hash == NULL) {
2456 printf("Already up-to-date\n");
2457 } else if (verbosity >= 0) {
2458 error = got_object_id_str(&id_str, pack_hash);
2461 printf("\nFetched %s.pack\n", id_str);
2466 /* Update references provided with the pack file. */
2467 TAILQ_FOREACH(pe, &refs, entry) {
2468 const char *refname = pe->path;
2469 struct got_object_id *id = pe->data;
2470 struct got_reference *ref;
2471 char *remote_refname;
2473 if (is_wanted_ref(&wanted_refs, refname) &&
2474 !remote->mirror_references) {
2475 error = update_wanted_ref(refname, id,
2476 remote->name, verbosity, repo);
2482 if (remote->mirror_references ||
2483 strncmp("refs/tags/", refname, 10) == 0) {
2484 error = got_ref_open(&ref, repo, refname, 1);
2486 if (error->code != GOT_ERR_NOT_REF)
2488 error = create_ref(refname, id, verbosity,
2493 error = update_ref(ref, id, replace_tags,
2495 unlock_err = got_ref_unlock(ref);
2496 if (unlock_err && error == NULL)
2502 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2503 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2504 remote_name, refname + 11) == -1) {
2505 error = got_error_from_errno("asprintf");
2509 error = got_ref_open(&ref, repo, remote_refname, 1);
2511 if (error->code != GOT_ERR_NOT_REF)
2513 error = create_ref(remote_refname, id,
2518 error = update_ref(ref, id, replace_tags,
2520 unlock_err = got_ref_unlock(ref);
2521 if (unlock_err && error == NULL)
2528 /* Also create a local branch if none exists yet. */
2529 error = got_ref_open(&ref, repo, refname, 1);
2531 if (error->code != GOT_ERR_NOT_REF)
2533 error = create_ref(refname, id, verbosity,
2538 unlock_err = got_ref_unlock(ref);
2539 if (unlock_err && error == NULL)
2546 error = delete_missing_refs(&refs, &symrefs, remote,
2552 if (!remote->mirror_references) {
2553 /* Update remote HEAD reference if the server provided one. */
2554 TAILQ_FOREACH(pe, &symrefs, entry) {
2555 struct got_reference *target_ref;
2556 const char *refname = pe->path;
2557 const char *target = pe->data;
2558 char *remote_refname = NULL, *remote_target = NULL;
2560 if (strcmp(refname, GOT_REF_HEAD) != 0)
2563 if (strncmp("refs/heads/", target, 11) != 0)
2566 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2567 remote->name, refname) == -1) {
2568 error = got_error_from_errno("asprintf");
2571 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2572 remote->name, target + 11) == -1) {
2573 error = got_error_from_errno("asprintf");
2574 free(remote_refname);
2578 error = got_ref_open(&target_ref, repo, remote_target,
2581 free(remote_refname);
2582 free(remote_target);
2583 if (error->code == GOT_ERR_NOT_REF) {
2589 error = update_symref(remote_refname, target_ref,
2591 free(remote_refname);
2592 free(remote_target);
2593 got_ref_close(target_ref);
2600 if (kill(fetchpid, SIGTERM) == -1)
2601 error = got_error_from_errno("kill");
2602 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2603 error = got_error_from_errno("waitpid");
2605 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2606 error = got_error_from_errno("close");
2608 const struct got_error *close_err = got_repo_close(repo);
2613 got_worktree_close(worktree);
2614 TAILQ_FOREACH(pe, &refs, entry) {
2615 free((void *)pe->path);
2618 got_pathlist_free(&refs);
2619 TAILQ_FOREACH(pe, &symrefs, entry) {
2620 free((void *)pe->path);
2623 got_pathlist_free(&symrefs);
2624 got_pathlist_free(&wanted_branches);
2625 got_pathlist_free(&wanted_refs);
2640 usage_checkout(void)
2642 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2643 "[-p prefix] [-q] repository-path [worktree-path]\n",
2649 show_worktree_base_ref_warning(void)
2651 fprintf(stderr, "%s: warning: could not create a reference "
2652 "to the work tree's base commit; the commit could be "
2653 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2654 "repository writable and running 'got update' will prevent this\n",
2658 struct got_checkout_progress_arg {
2659 const char *worktree_path;
2660 int had_base_commit_ref_error;
2664 static const struct got_error *
2665 checkout_progress(void *arg, unsigned char status, const char *path)
2667 struct got_checkout_progress_arg *a = arg;
2669 /* Base commit bump happens silently. */
2670 if (status == GOT_STATUS_BUMP_BASE)
2673 if (status == GOT_STATUS_BASE_REF_ERR) {
2674 a->had_base_commit_ref_error = 1;
2678 while (path[0] == '/')
2681 if (a->verbosity >= 0)
2682 printf("%c %s/%s\n", status, a->worktree_path, path);
2687 static const struct got_error *
2688 check_cancelled(void *arg)
2690 if (sigint_received || sigpipe_received)
2691 return got_error(GOT_ERR_CANCELLED);
2695 static const struct got_error *
2696 check_linear_ancestry(struct got_object_id *commit_id,
2697 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2698 struct got_repository *repo)
2700 const struct got_error *err = NULL;
2701 struct got_object_id *yca_id;
2703 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2704 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2709 return got_error(GOT_ERR_ANCESTRY);
2712 * Require a straight line of history between the target commit
2713 * and the work tree's base commit.
2715 * Non-linear situations such as this require a rebase:
2717 * (commit) D F (base_commit)
2725 * 'got update' only handles linear cases:
2726 * Update forwards in time: A (base/yca) - B - C - D (commit)
2727 * Update backwards in time: D (base) - C - B - A (commit/yca)
2729 if (allow_forwards_in_time_only) {
2730 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2731 return got_error(GOT_ERR_ANCESTRY);
2732 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2733 got_object_id_cmp(base_commit_id, yca_id) != 0)
2734 return got_error(GOT_ERR_ANCESTRY);
2740 static const struct got_error *
2741 check_same_branch(struct got_object_id *commit_id,
2742 struct got_reference *head_ref, struct got_object_id *yca_id,
2743 struct got_repository *repo)
2745 const struct got_error *err = NULL;
2746 struct got_commit_graph *graph = NULL;
2747 struct got_object_id *head_commit_id = NULL;
2748 int is_same_branch = 0;
2750 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2754 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2758 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2763 err = got_commit_graph_open(&graph, "/", 1);
2767 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2768 check_cancelled, NULL);
2773 struct got_object_id *id;
2774 err = got_commit_graph_iter_next(&id, graph, repo,
2775 check_cancelled, NULL);
2777 if (err->code == GOT_ERR_ITER_COMPLETED)
2783 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2785 if (got_object_id_cmp(id, commit_id) == 0) {
2793 got_commit_graph_close(graph);
2794 free(head_commit_id);
2795 if (!err && !is_same_branch)
2796 err = got_error(GOT_ERR_ANCESTRY);
2800 static const struct got_error *
2801 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2803 static char msg[512];
2804 const char *branch_name;
2806 if (got_ref_is_symbolic(ref))
2807 branch_name = got_ref_get_symref_target(ref);
2809 branch_name = got_ref_get_name(ref);
2811 if (strncmp("refs/heads/", branch_name, 11) == 0)
2814 snprintf(msg, sizeof(msg),
2815 "target commit is not contained in branch '%s'; "
2816 "the branch to use must be specified with -b; "
2817 "if necessary a new branch can be created for "
2818 "this commit with 'got branch -c %s BRANCH_NAME'",
2819 branch_name, commit_id_str);
2821 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2824 static const struct got_error *
2825 cmd_checkout(int argc, char *argv[])
2827 const struct got_error *error = NULL;
2828 struct got_repository *repo = NULL;
2829 struct got_reference *head_ref = NULL, *ref = NULL;
2830 struct got_worktree *worktree = NULL;
2831 char *repo_path = NULL;
2832 char *worktree_path = NULL;
2833 const char *path_prefix = "";
2834 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2835 char *commit_id_str = NULL;
2836 struct got_object_id *commit_id = NULL;
2838 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2839 struct got_pathlist_head paths;
2840 struct got_checkout_progress_arg cpa;
2844 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2847 branch_name = optarg;
2850 commit_id_str = strdup(optarg);
2851 if (commit_id_str == NULL)
2852 return got_error_from_errno("strdup");
2858 path_prefix = optarg;
2873 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2874 "unveil", NULL) == -1)
2878 char *base, *dotgit;
2880 repo_path = realpath(argv[0], NULL);
2881 if (repo_path == NULL)
2882 return got_error_from_errno2("realpath", argv[0]);
2883 cwd = getcwd(NULL, 0);
2885 error = got_error_from_errno("getcwd");
2892 error = got_path_basename(&base, path);
2895 dotgit = strstr(base, ".git");
2898 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2899 error = got_error_from_errno("asprintf");
2904 } else if (argc == 2) {
2905 repo_path = realpath(argv[0], NULL);
2906 if (repo_path == NULL) {
2907 error = got_error_from_errno2("realpath", argv[0]);
2910 worktree_path = realpath(argv[1], NULL);
2911 if (worktree_path == NULL) {
2912 if (errno != ENOENT) {
2913 error = got_error_from_errno2("realpath",
2917 worktree_path = strdup(argv[1]);
2918 if (worktree_path == NULL) {
2919 error = got_error_from_errno("strdup");
2926 got_path_strip_trailing_slashes(repo_path);
2927 got_path_strip_trailing_slashes(worktree_path);
2929 error = got_repo_open(&repo, repo_path, NULL);
2933 /* Pre-create work tree path for unveil(2) */
2934 error = got_path_mkdir(worktree_path);
2936 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2937 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2939 if (!allow_nonempty &&
2940 !got_path_dir_is_empty(worktree_path)) {
2941 error = got_error_path(worktree_path,
2942 GOT_ERR_DIR_NOT_EMPTY);
2947 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2951 error = got_ref_open(&head_ref, repo, branch_name, 0);
2955 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2956 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2959 error = got_worktree_open(&worktree, worktree_path);
2963 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2967 if (!same_path_prefix) {
2968 error = got_error(GOT_ERR_PATH_PREFIX);
2972 if (commit_id_str) {
2973 struct got_reflist_head refs;
2975 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2979 error = got_repo_match_object_id(&commit_id, NULL,
2980 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2981 got_ref_list_free(&refs);
2984 error = check_linear_ancestry(commit_id,
2985 got_worktree_get_base_commit_id(worktree), 0, repo);
2986 if (error != NULL) {
2988 if (error->code == GOT_ERR_ANCESTRY) {
2989 error = checkout_ancestry_error(
2990 head_ref, commit_id_str);
2994 error = check_same_branch(commit_id, head_ref, NULL, repo);
2996 if (error->code == GOT_ERR_ANCESTRY) {
2997 error = checkout_ancestry_error(
2998 head_ref, commit_id_str);
3002 error = got_worktree_set_base_commit_id(worktree, repo,
3006 /* Expand potentially abbreviated commit ID string. */
3007 free(commit_id_str);
3008 error = got_object_id_str(&commit_id_str, commit_id);
3012 commit_id = got_object_id_dup(
3013 got_worktree_get_base_commit_id(worktree));
3014 if (commit_id == NULL) {
3015 error = got_error_from_errno("got_object_id_dup");
3018 error = got_object_id_str(&commit_id_str, commit_id);
3023 error = got_pathlist_append(&paths, "", NULL);
3026 cpa.worktree_path = worktree_path;
3027 cpa.had_base_commit_ref_error = 0;
3028 cpa.verbosity = verbosity;
3029 error = got_worktree_checkout_files(worktree, &paths, repo,
3030 checkout_progress, &cpa, check_cancelled, NULL);
3034 if (got_ref_is_symbolic(head_ref)) {
3035 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3038 refname = got_ref_get_name(ref);
3040 refname = got_ref_get_name(head_ref);
3041 printf("Checked out %s: %s\n", refname, commit_id_str);
3042 printf("Now shut up and hack\n");
3043 if (cpa.had_base_commit_ref_error)
3044 show_worktree_base_ref_warning();
3047 got_ref_close(head_ref);
3050 got_pathlist_free(&paths);
3051 free(commit_id_str);
3054 free(worktree_path);
3059 struct got_update_progress_arg {
3071 print_update_progress_stats(struct got_update_progress_arg *upa)
3073 if (!upa->did_something)
3076 if (upa->conflicts > 0)
3077 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3078 if (upa->obstructed > 0)
3079 printf("File paths obstructed by a non-regular file: %d\n",
3081 if (upa->not_updated > 0)
3082 printf("Files not updated because of existing merge "
3083 "conflicts: %d\n", upa->not_updated);
3087 * The meaning of some status codes differs between merge-style operations and
3088 * update operations. For example, the ! status code means "file was missing"
3089 * if changes were merged into the work tree, and "missing file was restored"
3090 * if the work tree was updated. This function should be used by any operation
3091 * which merges changes into the work tree without updating the work tree.
3094 print_merge_progress_stats(struct got_update_progress_arg *upa)
3096 if (!upa->did_something)
3099 if (upa->conflicts > 0)
3100 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3101 if (upa->obstructed > 0)
3102 printf("File paths obstructed by a non-regular file: %d\n",
3104 if (upa->missing > 0)
3105 printf("Files which had incoming changes but could not be "
3106 "found in the work tree: %d\n", upa->missing);
3107 if (upa->not_deleted > 0)
3108 printf("Files not deleted due to differences in deleted "
3109 "content: %d\n", upa->not_deleted);
3110 if (upa->unversioned > 0)
3111 printf("Files not merged because an unversioned file was "
3112 "found in the work tree: %d\n", upa->unversioned);
3118 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3124 static const struct got_error *
3125 update_progress(void *arg, unsigned char status, const char *path)
3127 struct got_update_progress_arg *upa = arg;
3129 if (status == GOT_STATUS_EXISTS ||
3130 status == GOT_STATUS_BASE_REF_ERR)
3133 upa->did_something = 1;
3135 /* Base commit bump happens silently. */
3136 if (status == GOT_STATUS_BUMP_BASE)
3139 if (status == GOT_STATUS_CONFLICT)
3141 if (status == GOT_STATUS_OBSTRUCTED)
3143 if (status == GOT_STATUS_CANNOT_UPDATE)
3145 if (status == GOT_STATUS_MISSING)
3147 if (status == GOT_STATUS_CANNOT_DELETE)
3149 if (status == GOT_STATUS_UNVERSIONED)
3152 while (path[0] == '/')
3154 if (upa->verbosity >= 0)
3155 printf("%c %s\n", status, path);
3160 static const struct got_error *
3161 switch_head_ref(struct got_reference *head_ref,
3162 struct got_object_id *commit_id, struct got_worktree *worktree,
3163 struct got_repository *repo)
3165 const struct got_error *err = NULL;
3167 int ref_has_moved = 0;
3169 /* Trivial case: switching between two different references. */
3170 if (strcmp(got_ref_get_name(head_ref),
3171 got_worktree_get_head_ref_name(worktree)) != 0) {
3172 printf("Switching work tree from %s to %s\n",
3173 got_worktree_get_head_ref_name(worktree),
3174 got_ref_get_name(head_ref));
3175 return got_worktree_set_head_ref(worktree, head_ref);
3178 err = check_linear_ancestry(commit_id,
3179 got_worktree_get_base_commit_id(worktree), 0, repo);
3181 if (err->code != GOT_ERR_ANCESTRY)
3188 /* Switching to a rebased branch with the same reference name. */
3189 err = got_object_id_str(&base_id_str,
3190 got_worktree_get_base_commit_id(worktree));
3193 printf("Reference %s now points at a different branch\n",
3194 got_worktree_get_head_ref_name(worktree));
3195 printf("Switching work tree from %s to %s\n", base_id_str,
3196 got_worktree_get_head_ref_name(worktree));
3200 static const struct got_error *
3201 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3203 const struct got_error *err;
3206 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3210 return got_error(GOT_ERR_REBASING);
3212 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3216 return got_error(GOT_ERR_HISTEDIT_BUSY);
3221 static const struct got_error *
3222 check_merge_in_progress(struct got_worktree *worktree,
3223 struct got_repository *repo)
3225 const struct got_error *err;
3228 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3232 return got_error(GOT_ERR_MERGE_BUSY);
3237 static const struct got_error *
3238 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3239 char *argv[], struct got_worktree *worktree)
3241 const struct got_error *err = NULL;
3248 return got_error_from_errno("strdup");
3249 return got_pathlist_append(paths, path, NULL);
3252 for (i = 0; i < argc; i++) {
3253 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3256 err = got_pathlist_append(paths, path, NULL);
3266 static const struct got_error *
3267 wrap_not_worktree_error(const struct got_error *orig_err,
3268 const char *cmdname, const char *path)
3270 const struct got_error *err;
3271 struct got_repository *repo;
3272 static char msg[512];
3274 err = got_repo_open(&repo, path, NULL);
3278 snprintf(msg, sizeof(msg),
3279 "'got %s' needs a work tree in addition to a git repository\n"
3280 "Work trees can be checked out from this Git repository with "
3282 "The got(1) manual page contains more information.", cmdname);
3283 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3284 got_repo_close(repo);
3288 static const struct got_error *
3289 cmd_update(int argc, char *argv[])
3291 const struct got_error *error = NULL;
3292 struct got_repository *repo = NULL;
3293 struct got_worktree *worktree = NULL;
3294 char *worktree_path = NULL;
3295 struct got_object_id *commit_id = NULL;
3296 char *commit_id_str = NULL;
3297 const char *branch_name = NULL;
3298 struct got_reference *head_ref = NULL;
3299 struct got_pathlist_head paths;
3300 struct got_pathlist_entry *pe;
3301 int ch, verbosity = 0;
3302 struct got_update_progress_arg upa;
3306 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3309 branch_name = optarg;
3312 commit_id_str = strdup(optarg);
3313 if (commit_id_str == NULL)
3314 return got_error_from_errno("strdup");
3329 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3330 "unveil", NULL) == -1)
3333 worktree_path = getcwd(NULL, 0);
3334 if (worktree_path == NULL) {
3335 error = got_error_from_errno("getcwd");
3338 error = got_worktree_open(&worktree, worktree_path);
3340 if (error->code == GOT_ERR_NOT_WORKTREE)
3341 error = wrap_not_worktree_error(error, "update",
3346 error = check_rebase_or_histedit_in_progress(worktree);
3350 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3355 error = apply_unveil(got_repo_get_path(repo), 0,
3356 got_worktree_get_root_path(worktree));
3360 error = check_merge_in_progress(worktree, repo);
3364 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3368 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3369 got_worktree_get_head_ref_name(worktree), 0);
3372 if (commit_id_str == NULL) {
3373 error = got_ref_resolve(&commit_id, repo, head_ref);
3376 error = got_object_id_str(&commit_id_str, commit_id);
3380 struct got_reflist_head refs;
3382 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3386 error = got_repo_match_object_id(&commit_id, NULL,
3387 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3388 got_ref_list_free(&refs);
3389 free(commit_id_str);
3390 commit_id_str = NULL;
3393 error = got_object_id_str(&commit_id_str, commit_id);
3399 struct got_object_id *head_commit_id;
3400 TAILQ_FOREACH(pe, &paths, entry) {
3401 if (pe->path_len == 0)
3403 error = got_error_msg(GOT_ERR_BAD_PATH,
3404 "switching between branches requires that "
3405 "the entire work tree gets updated");
3408 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3411 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3413 free(head_commit_id);
3416 error = check_same_branch(commit_id, head_ref, NULL, repo);
3419 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3423 error = check_linear_ancestry(commit_id,
3424 got_worktree_get_base_commit_id(worktree), 0, repo);
3425 if (error != NULL) {
3426 if (error->code == GOT_ERR_ANCESTRY)
3427 error = got_error(GOT_ERR_BRANCH_MOVED);
3430 error = check_same_branch(commit_id, head_ref, NULL, repo);
3435 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3437 error = got_worktree_set_base_commit_id(worktree, repo,
3443 memset(&upa, 0, sizeof(upa));
3444 upa.verbosity = verbosity;
3445 error = got_worktree_checkout_files(worktree, &paths, repo,
3446 update_progress, &upa, check_cancelled, NULL);
3450 if (upa.did_something) {
3451 printf("Updated to %s: %s\n",
3452 got_worktree_get_head_ref_name(worktree), commit_id_str);
3454 printf("Already up-to-date\n");
3455 print_update_progress_stats(&upa);
3457 free(worktree_path);
3458 TAILQ_FOREACH(pe, &paths, entry)
3459 free((char *)pe->path);
3460 got_pathlist_free(&paths);
3462 free(commit_id_str);
3466 static const struct got_error *
3467 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3468 const char *path, int diff_context, int ignore_whitespace,
3469 int force_text_diff, struct got_repository *repo)
3471 const struct got_error *err = NULL;
3472 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3475 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3480 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3484 while (path[0] == '/')
3486 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3487 diff_context, ignore_whitespace, force_text_diff, stdout);
3490 got_object_blob_close(blob1);
3491 got_object_blob_close(blob2);
3495 static const struct got_error *
3496 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3497 const char *path, int diff_context, int ignore_whitespace,
3498 int force_text_diff, struct got_repository *repo)
3500 const struct got_error *err = NULL;
3501 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3502 struct got_diff_blob_output_unidiff_arg arg;
3505 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3510 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3514 arg.diff_context = diff_context;
3515 arg.ignore_whitespace = ignore_whitespace;
3516 arg.force_text_diff = force_text_diff;
3517 arg.outfile = stdout;
3518 arg.line_offsets = NULL;
3520 while (path[0] == '/')
3522 err = got_diff_tree(tree1, tree2, path, path, repo,
3523 got_diff_blob_output_unidiff, &arg, 1);
3526 got_object_tree_close(tree1);
3528 got_object_tree_close(tree2);
3532 static const struct got_error *
3533 get_changed_paths(struct got_pathlist_head *paths,
3534 struct got_commit_object *commit, struct got_repository *repo)
3536 const struct got_error *err = NULL;
3537 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3538 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3539 struct got_object_qid *qid;
3541 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3543 struct got_commit_object *pcommit;
3544 err = got_object_open_as_commit(&pcommit, repo,
3549 tree_id1 = got_object_id_dup(
3550 got_object_commit_get_tree_id(pcommit));
3551 if (tree_id1 == NULL) {
3552 got_object_commit_close(pcommit);
3553 return got_error_from_errno("got_object_id_dup");
3555 got_object_commit_close(pcommit);
3560 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3565 tree_id2 = got_object_commit_get_tree_id(commit);
3566 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3570 err = got_diff_tree(tree1, tree2, "", "", repo,
3571 got_diff_tree_collect_changed_paths, paths, 0);
3574 got_object_tree_close(tree1);
3576 got_object_tree_close(tree2);
3581 static const struct got_error *
3582 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3583 const char *path, int diff_context, struct got_repository *repo)
3585 const struct got_error *err = NULL;
3586 struct got_commit_object *pcommit = NULL;
3587 char *id_str1 = NULL, *id_str2 = NULL;
3588 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3589 struct got_object_qid *qid;
3591 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3593 err = got_object_open_as_commit(&pcommit, repo,
3599 if (path && path[0] != '\0') {
3601 err = got_object_id_by_path(&obj_id2, repo, id, path);
3604 err = got_object_id_str(&id_str2, obj_id2);
3610 err = got_object_id_by_path(&obj_id1, repo,
3613 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3618 err = got_object_id_str(&id_str1, obj_id1);
3625 err = got_object_get_type(&obj_type, repo, obj_id2);
3630 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3632 case GOT_OBJ_TYPE_BLOB:
3633 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3636 case GOT_OBJ_TYPE_TREE:
3637 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3641 err = got_error(GOT_ERR_OBJ_TYPE);
3647 obj_id2 = got_object_commit_get_tree_id(commit);
3648 err = got_object_id_str(&id_str2, obj_id2);
3652 obj_id1 = got_object_commit_get_tree_id(pcommit);
3653 err = got_object_id_str(&id_str1, obj_id1);
3657 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3659 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3666 got_object_commit_close(pcommit);
3671 get_datestr(time_t *time, char *datebuf)
3673 struct tm mytm, *tm;
3676 tm = gmtime_r(time, &mytm);
3679 s = asctime_r(tm, datebuf);
3682 p = strchr(s, '\n');
3688 static const struct got_error *
3689 match_logmsg(int *have_match, struct got_object_id *id,
3690 struct got_commit_object *commit, regex_t *regex)
3692 const struct got_error *err = NULL;
3693 regmatch_t regmatch;
3694 char *id_str = NULL, *logmsg = NULL;
3698 err = got_object_id_str(&id_str, id);
3702 err = got_object_commit_get_logmsg(&logmsg, commit);
3706 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3715 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3718 regmatch_t regmatch;
3719 struct got_pathlist_entry *pe;
3723 TAILQ_FOREACH(pe, changed_paths, entry) {
3724 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3731 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3733 static const struct got_error*
3734 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3735 struct got_object_id *id, struct got_repository *repo)
3737 static const struct got_error *err = NULL;
3738 struct got_reflist_entry *re;
3744 TAILQ_FOREACH(re, refs, entry) {
3745 struct got_tag_object *tag = NULL;
3746 struct got_object_id *ref_id;
3749 name = got_ref_get_name(re->ref);
3750 if (strcmp(name, GOT_REF_HEAD) == 0)
3752 if (strncmp(name, "refs/", 5) == 0)
3754 if (strncmp(name, "got/", 4) == 0)
3756 if (strncmp(name, "heads/", 6) == 0)
3758 if (strncmp(name, "remotes/", 8) == 0) {
3760 s = strstr(name, "/" GOT_REF_HEAD);
3761 if (s != NULL && s[strlen(s)] == '\0')
3764 err = got_ref_resolve(&ref_id, repo, re->ref);
3767 if (strncmp(name, "tags/", 5) == 0) {
3768 err = got_object_open_as_tag(&tag, repo, ref_id);
3770 if (err->code != GOT_ERR_OBJ_TYPE) {
3774 /* Ref points at something other than a tag. */
3779 cmp = got_object_id_cmp(tag ?
3780 got_object_tag_get_object_id(tag) : ref_id, id);
3783 got_object_tag_close(tag);
3787 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3788 s ? ", " : "", name) == -1) {
3789 err = got_error_from_errno("asprintf");
3800 static const struct got_error *
3801 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3802 struct got_repository *repo, const char *path,
3803 struct got_pathlist_head *changed_paths, int show_patch,
3804 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3805 const char *custom_refs_str)
3807 const struct got_error *err = NULL;
3808 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3810 time_t committer_time;
3811 const char *author, *committer;
3812 char *refs_str = NULL;
3814 err = got_object_id_str(&id_str, id);
3818 if (custom_refs_str == NULL) {
3819 struct got_reflist_head *refs;
3820 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3822 err = build_refs_str(&refs_str, refs, id, repo);
3828 printf(GOT_COMMIT_SEP_STR);
3829 if (custom_refs_str)
3830 printf("commit %s (%s)\n", id_str, custom_refs_str);
3832 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3833 refs_str ? refs_str : "", refs_str ? ")" : "");
3838 printf("from: %s\n", got_object_commit_get_author(commit));
3839 committer_time = got_object_commit_get_committer_time(commit);
3840 datestr = get_datestr(&committer_time, datebuf);
3842 printf("date: %s UTC\n", datestr);
3843 author = got_object_commit_get_author(commit);
3844 committer = got_object_commit_get_committer(commit);
3845 if (strcmp(author, committer) != 0)
3846 printf("via: %s\n", committer);
3847 if (got_object_commit_get_nparents(commit) > 1) {
3848 const struct got_object_id_queue *parent_ids;
3849 struct got_object_qid *qid;
3851 parent_ids = got_object_commit_get_parent_ids(commit);
3852 STAILQ_FOREACH(qid, parent_ids, entry) {
3853 err = got_object_id_str(&id_str, qid->id);
3856 printf("parent %d: %s\n", n++, id_str);
3862 err = got_object_commit_get_logmsg(&logmsg0, commit);
3868 line = strsep(&logmsg, "\n");
3870 printf(" %s\n", line);
3874 if (changed_paths) {
3875 struct got_pathlist_entry *pe;
3876 TAILQ_FOREACH(pe, changed_paths, entry) {
3877 struct got_diff_changed_path *cp = pe->data;
3878 printf(" %c %s\n", cp->status, pe->path);
3883 err = print_patch(commit, id, path, diff_context, repo);
3888 if (fflush(stdout) != 0 && err == NULL)
3889 err = got_error_from_errno("fflush");
3896 static const struct got_error *
3897 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3898 struct got_repository *repo, const char *path, int show_changed_paths,
3899 int show_patch, const char *search_pattern, int diff_context, int limit,
3900 int log_branches, int reverse_display_order,
3901 struct got_reflist_object_id_map *refs_idmap)
3903 const struct got_error *err;
3904 struct got_commit_graph *graph;
3907 struct got_object_id_queue reversed_commits;
3908 struct got_object_qid *qid;
3909 struct got_commit_object *commit;
3910 struct got_pathlist_head changed_paths;
3911 struct got_pathlist_entry *pe;
3913 STAILQ_INIT(&reversed_commits);
3914 TAILQ_INIT(&changed_paths);
3916 if (search_pattern && regcomp(®ex, search_pattern,
3917 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3918 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3920 err = got_commit_graph_open(&graph, path, !log_branches);
3923 err = got_commit_graph_iter_start(graph, root_id, repo,
3924 check_cancelled, NULL);
3928 struct got_object_id *id;
3930 if (sigint_received || sigpipe_received)
3933 err = got_commit_graph_iter_next(&id, graph, repo,
3934 check_cancelled, NULL);
3936 if (err->code == GOT_ERR_ITER_COMPLETED)
3943 err = got_object_open_as_commit(&commit, repo, id);
3947 if (show_changed_paths && !reverse_display_order) {
3948 err = get_changed_paths(&changed_paths, commit, repo);
3953 if (search_pattern) {
3954 err = match_logmsg(&have_match, id, commit, ®ex);
3956 got_object_commit_close(commit);
3959 if (have_match == 0 && show_changed_paths)
3960 match_changed_paths(&have_match,
3961 &changed_paths, ®ex);
3962 if (have_match == 0) {
3963 got_object_commit_close(commit);
3964 TAILQ_FOREACH(pe, &changed_paths, entry) {
3965 free((char *)pe->path);
3968 got_pathlist_free(&changed_paths);
3973 if (reverse_display_order) {
3974 err = got_object_qid_alloc(&qid, id);
3977 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3978 got_object_commit_close(commit);
3980 err = print_commit(commit, id, repo, path,
3981 show_changed_paths ? &changed_paths : NULL,
3982 show_patch, diff_context, refs_idmap, NULL);
3983 got_object_commit_close(commit);
3987 if ((limit && --limit == 0) ||
3988 (end_id && got_object_id_cmp(id, end_id) == 0))
3991 TAILQ_FOREACH(pe, &changed_paths, entry) {
3992 free((char *)pe->path);
3995 got_pathlist_free(&changed_paths);
3997 if (reverse_display_order) {
3998 STAILQ_FOREACH(qid, &reversed_commits, entry) {
3999 err = got_object_open_as_commit(&commit, repo, qid->id);
4002 if (show_changed_paths) {
4003 err = get_changed_paths(&changed_paths,
4008 err = print_commit(commit, qid->id, repo, path,
4009 show_changed_paths ? &changed_paths : NULL,
4010 show_patch, diff_context, refs_idmap, NULL);
4011 got_object_commit_close(commit);
4014 TAILQ_FOREACH(pe, &changed_paths, entry) {
4015 free((char *)pe->path);
4018 got_pathlist_free(&changed_paths);
4022 while (!STAILQ_EMPTY(&reversed_commits)) {
4023 qid = STAILQ_FIRST(&reversed_commits);
4024 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4025 got_object_qid_free(qid);
4027 TAILQ_FOREACH(pe, &changed_paths, entry) {
4028 free((char *)pe->path);
4031 got_pathlist_free(&changed_paths);
4034 got_commit_graph_close(graph);
4041 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4042 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4043 "[-R] [path]\n", getprogname());
4048 get_default_log_limit(void)
4050 const char *got_default_log_limit;
4054 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4055 if (got_default_log_limit == NULL)
4057 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4063 static const struct got_error *
4064 cmd_log(int argc, char *argv[])
4066 const struct got_error *error;
4067 struct got_repository *repo = NULL;
4068 struct got_worktree *worktree = NULL;
4069 struct got_object_id *start_id = NULL, *end_id = NULL;
4070 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4071 const char *start_commit = NULL, *end_commit = NULL;
4072 const char *search_pattern = NULL;
4073 int diff_context = -1, ch;
4074 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4075 int reverse_display_order = 0;
4077 struct got_reflist_head refs;
4078 struct got_reflist_object_id_map *refs_idmap = NULL;
4083 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4089 limit = get_default_log_limit();
4091 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4097 show_changed_paths = 1;
4100 start_commit = optarg;
4103 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4106 err(1, "-C option %s", errstr);
4109 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4111 err(1, "-l option %s", errstr);
4117 repo_path = realpath(optarg, NULL);
4118 if (repo_path == NULL)
4119 return got_error_from_errno2("realpath",
4121 got_path_strip_trailing_slashes(repo_path);
4124 reverse_display_order = 1;
4127 search_pattern = optarg;
4130 end_commit = optarg;
4141 if (diff_context == -1)
4143 else if (!show_patch)
4144 errx(1, "-C requires -p");
4146 cwd = getcwd(NULL, 0);
4148 error = got_error_from_errno("getcwd");
4152 if (repo_path == NULL) {
4153 error = got_worktree_open(&worktree, cwd);
4154 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4161 error = got_worktree_resolve_path(&path, worktree,
4166 path = strdup(argv[0]);
4168 error = got_error_from_errno("strdup");
4172 } else if (argc != 0)
4175 if (repo_path == NULL) {
4176 repo_path = worktree ?
4177 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4179 if (repo_path == NULL) {
4180 error = got_error_from_errno("strdup");
4184 error = got_repo_open(&repo, repo_path, NULL);
4188 error = apply_unveil(got_repo_get_path(repo), 1,
4189 worktree ? got_worktree_get_root_path(worktree) : NULL);
4193 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4197 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4201 if (start_commit == NULL) {
4202 struct got_reference *head_ref;
4203 struct got_commit_object *commit = NULL;
4204 error = got_ref_open(&head_ref, repo,
4205 worktree ? got_worktree_get_head_ref_name(worktree)
4209 error = got_ref_resolve(&start_id, repo, head_ref);
4210 got_ref_close(head_ref);
4213 error = got_object_open_as_commit(&commit, repo,
4217 got_object_commit_close(commit);
4219 error = got_repo_match_object_id(&start_id, NULL,
4220 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4224 if (end_commit != NULL) {
4225 error = got_repo_match_object_id(&end_id, NULL,
4226 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4233 * If a path was specified on the command line it was resolved
4234 * to a path in the work tree above. Prepend the work tree's
4235 * path prefix to obtain the corresponding in-repository path.
4239 prefix = got_worktree_get_path_prefix(worktree);
4240 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4241 (path[0] != '\0') ? "/" : "", path) == -1) {
4242 error = got_error_from_errno("asprintf");
4247 error = got_repo_map_path(&in_repo_path, repo,
4253 path = in_repo_path;
4256 error = print_commits(start_id, end_id, repo, path ? path : "",
4257 show_changed_paths, show_patch, search_pattern, diff_context,
4258 limit, log_branches, reverse_display_order, refs_idmap);
4264 got_worktree_close(worktree);
4266 const struct got_error *close_err = got_repo_close(repo);
4271 got_reflist_object_id_map_free(refs_idmap);
4272 got_ref_list_free(&refs);
4279 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4280 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4284 struct print_diff_arg {
4285 struct got_repository *repo;
4286 struct got_worktree *worktree;
4291 int ignore_whitespace;
4292 int force_text_diff;
4296 * Create a file which contains the target path of a symlink so we can feed
4297 * it as content to the diff engine.
4299 static const struct got_error *
4300 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4301 const char *abspath)
4303 const struct got_error *err = NULL;
4304 char target_path[PATH_MAX];
4305 ssize_t target_len, outlen;
4310 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4311 if (target_len == -1)
4312 return got_error_from_errno2("readlinkat", abspath);
4314 target_len = readlink(abspath, target_path, PATH_MAX);
4315 if (target_len == -1)
4316 return got_error_from_errno2("readlink", abspath);
4319 *fd = got_opentempfd();
4321 return got_error_from_errno("got_opentempfd");
4323 outlen = write(*fd, target_path, target_len);
4325 err = got_error_from_errno("got_opentempfd");
4329 if (lseek(*fd, 0, SEEK_SET) == -1) {
4330 err = got_error_from_errno2("lseek", abspath);
4341 static const struct got_error *
4342 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4343 const char *path, struct got_object_id *blob_id,
4344 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4345 int dirfd, const char *de_name)
4347 struct print_diff_arg *a = arg;
4348 const struct got_error *err = NULL;
4349 struct got_blob_object *blob1 = NULL;
4352 char *abspath = NULL, *label1 = NULL;
4355 if (a->diff_staged) {
4356 if (staged_status != GOT_STATUS_MODIFY &&
4357 staged_status != GOT_STATUS_ADD &&
4358 staged_status != GOT_STATUS_DELETE)
4361 if (staged_status == GOT_STATUS_DELETE)
4363 if (status == GOT_STATUS_NONEXISTENT)
4364 return got_error_set_errno(ENOENT, path);
4365 if (status != GOT_STATUS_MODIFY &&
4366 status != GOT_STATUS_ADD &&
4367 status != GOT_STATUS_DELETE &&
4368 status != GOT_STATUS_CONFLICT)
4372 if (!a->header_shown) {
4373 printf("diff %s %s%s\n", a->id_str,
4374 got_worktree_get_root_path(a->worktree),
4375 a->diff_staged ? " (staged changes)" : "");
4376 a->header_shown = 1;
4379 if (a->diff_staged) {
4380 const char *label1 = NULL, *label2 = NULL;
4381 switch (staged_status) {
4382 case GOT_STATUS_MODIFY:
4386 case GOT_STATUS_ADD:
4389 case GOT_STATUS_DELETE:
4393 return got_error(GOT_ERR_FILE_STATUS);
4395 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4396 staged_blob_id, label1, label2, a->diff_context,
4397 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4400 if (staged_status == GOT_STATUS_ADD ||
4401 staged_status == GOT_STATUS_MODIFY) {
4403 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4407 err = got_object_id_str(&id_str, staged_blob_id);
4410 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4411 err = got_error_from_errno("asprintf");
4416 } else if (status != GOT_STATUS_ADD) {
4417 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4422 if (status != GOT_STATUS_DELETE) {
4423 if (asprintf(&abspath, "%s/%s",
4424 got_worktree_get_root_path(a->worktree), path) == -1) {
4425 err = got_error_from_errno("asprintf");
4430 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4432 if (!got_err_open_nofollow_on_symlink()) {
4433 err = got_error_from_errno2("openat",
4437 err = get_symlink_target_file(&fd, dirfd,
4443 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4445 if (!got_err_open_nofollow_on_symlink()) {
4446 err = got_error_from_errno2("open",
4450 err = get_symlink_target_file(&fd, dirfd,
4456 if (fstat(fd, &sb) == -1) {
4457 err = got_error_from_errno2("fstat", abspath);
4460 f2 = fdopen(fd, "r");
4462 err = got_error_from_errno2("fdopen", abspath);
4469 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4470 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4473 got_object_blob_close(blob1);
4474 if (f2 && fclose(f2) == EOF && err == NULL)
4475 err = got_error_from_errno("fclose");
4476 if (fd != -1 && close(fd) == -1 && err == NULL)
4477 err = got_error_from_errno("close");
4482 static const struct got_error *
4483 cmd_diff(int argc, char *argv[])
4485 const struct got_error *error;
4486 struct got_repository *repo = NULL;
4487 struct got_worktree *worktree = NULL;
4488 char *cwd = NULL, *repo_path = NULL;
4489 struct got_object_id *id1 = NULL, *id2 = NULL;
4490 const char *id_str1 = NULL, *id_str2 = NULL;
4491 char *label1 = NULL, *label2 = NULL;
4493 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4494 int force_text_diff = 0;
4497 struct got_reflist_head refs;
4502 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4507 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4510 force_text_diff = 1;
4513 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4516 err(1, "-C option %s", errstr);
4519 repo_path = realpath(optarg, NULL);
4520 if (repo_path == NULL)
4521 return got_error_from_errno2("realpath",
4523 got_path_strip_trailing_slashes(repo_path);
4529 ignore_whitespace = 1;
4540 cwd = getcwd(NULL, 0);
4542 error = got_error_from_errno("getcwd");
4548 "-r option can't be used when diffing a work tree");
4549 error = got_worktree_open(&worktree, cwd);
4551 if (error->code == GOT_ERR_NOT_WORKTREE)
4552 error = wrap_not_worktree_error(error, "diff",
4556 repo_path = strdup(got_worktree_get_repo_path(worktree));
4557 if (repo_path == NULL) {
4558 error = got_error_from_errno("strdup");
4562 error = got_worktree_resolve_path(&path, worktree,
4569 error = got_error_from_errno("strdup");
4573 } else if (argc == 2) {
4575 errx(1, "-s option can't be used when diffing "
4576 "objects in repository");
4579 if (repo_path == NULL) {
4580 error = got_worktree_open(&worktree, cwd);
4581 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4583 repo_path = strdup(worktree ?
4584 got_worktree_get_repo_path(worktree) : cwd);
4585 if (repo_path == NULL) {
4586 error = got_error_from_errno("strdup");
4593 error = got_repo_open(&repo, repo_path, NULL);
4598 error = apply_unveil(got_repo_get_path(repo), 1,
4599 worktree ? got_worktree_get_root_path(worktree) : NULL);
4604 struct print_diff_arg arg;
4605 struct got_pathlist_head paths;
4610 error = got_object_id_str(&id_str,
4611 got_worktree_get_base_commit_id(worktree));
4615 arg.worktree = worktree;
4616 arg.diff_context = diff_context;
4617 arg.id_str = id_str;
4618 arg.header_shown = 0;
4619 arg.diff_staged = diff_staged;
4620 arg.ignore_whitespace = ignore_whitespace;
4621 arg.force_text_diff = force_text_diff;
4623 error = got_pathlist_append(&paths, path, NULL);
4627 error = got_worktree_status(worktree, &paths, repo, 0,
4628 print_diff, &arg, check_cancelled, NULL);
4630 got_pathlist_free(&paths);
4634 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4638 error = got_repo_match_object_id(&id1, &label1, id_str1,
4639 GOT_OBJ_TYPE_ANY, &refs, repo);
4643 error = got_repo_match_object_id(&id2, &label2, id_str2,
4644 GOT_OBJ_TYPE_ANY, &refs, repo);
4648 error = got_object_get_type(&type1, repo, id1);
4652 error = got_object_get_type(&type2, repo, id2);
4656 if (type1 != type2) {
4657 error = got_error(GOT_ERR_OBJ_TYPE);
4662 case GOT_OBJ_TYPE_BLOB:
4663 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4664 NULL, NULL, diff_context, ignore_whitespace,
4665 force_text_diff, repo, stdout);
4667 case GOT_OBJ_TYPE_TREE:
4668 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4669 "", "", diff_context, ignore_whitespace, force_text_diff,
4672 case GOT_OBJ_TYPE_COMMIT:
4673 printf("diff %s %s\n", label1, label2);
4674 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4675 diff_context, ignore_whitespace, force_text_diff, repo,
4679 error = got_error(GOT_ERR_OBJ_TYPE);
4688 got_worktree_close(worktree);
4690 const struct got_error *close_err = got_repo_close(repo);
4694 got_ref_list_free(&refs);
4702 "usage: %s blame [-c commit] [-r repository-path] path\n",
4711 char datebuf[11]; /* YYYY-MM-DD + NUL */
4714 struct blame_cb_args {
4715 struct blame_line *lines;
4719 off_t *line_offsets;
4721 struct got_repository *repo;
4724 static const struct got_error *
4725 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4727 const struct got_error *err = NULL;
4728 struct blame_cb_args *a = arg;
4729 struct blame_line *bline;
4731 size_t linesize = 0;
4732 struct got_commit_object *commit = NULL;
4735 time_t committer_time;
4737 if (nlines != a->nlines ||
4738 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4739 return got_error(GOT_ERR_RANGE);
4741 if (sigint_received)
4742 return got_error(GOT_ERR_ITER_COMPLETED);
4745 return NULL; /* no change in this commit */
4747 /* Annotate this line. */
4748 bline = &a->lines[lineno - 1];
4749 if (bline->annotated)
4751 err = got_object_id_str(&bline->id_str, id);
4755 err = got_object_open_as_commit(&commit, a->repo, id);
4759 bline->committer = strdup(got_object_commit_get_committer(commit));
4760 if (bline->committer == NULL) {
4761 err = got_error_from_errno("strdup");
4765 committer_time = got_object_commit_get_committer_time(commit);
4766 if (gmtime_r(&committer_time, &tm) == NULL)
4767 return got_error_from_errno("gmtime_r");
4768 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4770 err = got_error(GOT_ERR_NO_SPACE);
4773 bline->annotated = 1;
4775 /* Print lines annotated so far. */
4776 bline = &a->lines[a->lineno_cur - 1];
4777 if (!bline->annotated)
4780 offset = a->line_offsets[a->lineno_cur - 1];
4781 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4782 err = got_error_from_errno("fseeko");
4786 while (bline->annotated) {
4787 char *smallerthan, *at, *nl, *committer;
4790 if (getline(&line, &linesize, a->f) == -1) {
4792 err = got_error_from_errno("getline");
4796 committer = bline->committer;
4797 smallerthan = strchr(committer, '<');
4798 if (smallerthan && smallerthan[1] != '\0')
4799 committer = smallerthan + 1;
4800 at = strchr(committer, '@');
4803 len = strlen(committer);
4805 committer[8] = '\0';
4807 nl = strchr(line, '\n');
4810 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4811 bline->id_str, bline->datebuf, committer, line);
4814 bline = &a->lines[a->lineno_cur - 1];
4818 got_object_commit_close(commit);
4823 static const struct got_error *
4824 cmd_blame(int argc, char *argv[])
4826 const struct got_error *error;
4827 struct got_repository *repo = NULL;
4828 struct got_worktree *worktree = NULL;
4829 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4830 char *link_target = NULL;
4831 struct got_object_id *obj_id = NULL;
4832 struct got_object_id *commit_id = NULL;
4833 struct got_blob_object *blob = NULL;
4834 char *commit_id_str = NULL;
4835 struct blame_cb_args bca;
4836 int ch, obj_type, i;
4839 memset(&bca, 0, sizeof(bca));
4842 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4847 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4850 commit_id_str = optarg;
4853 repo_path = realpath(optarg, NULL);
4854 if (repo_path == NULL)
4855 return got_error_from_errno2("realpath",
4857 got_path_strip_trailing_slashes(repo_path);
4873 cwd = getcwd(NULL, 0);
4875 error = got_error_from_errno("getcwd");
4878 if (repo_path == NULL) {
4879 error = got_worktree_open(&worktree, cwd);
4880 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4886 strdup(got_worktree_get_repo_path(worktree));
4887 if (repo_path == NULL) {
4888 error = got_error_from_errno("strdup");
4893 repo_path = strdup(cwd);
4894 if (repo_path == NULL) {
4895 error = got_error_from_errno("strdup");
4901 error = got_repo_open(&repo, repo_path, NULL);
4906 const char *prefix = got_worktree_get_path_prefix(worktree);
4909 error = got_worktree_resolve_path(&p, worktree, path);
4912 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4913 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4915 error = got_error_from_errno("asprintf");
4920 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4922 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4925 error = got_repo_map_path(&in_repo_path, repo, path);
4930 if (commit_id_str == NULL) {
4931 struct got_reference *head_ref;
4932 error = got_ref_open(&head_ref, repo, worktree ?
4933 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4936 error = got_ref_resolve(&commit_id, repo, head_ref);
4937 got_ref_close(head_ref);
4941 struct got_reflist_head refs;
4943 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4947 error = got_repo_match_object_id(&commit_id, NULL,
4948 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4949 got_ref_list_free(&refs);
4954 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4959 error = got_object_id_by_path(&obj_id, repo, commit_id,
4960 link_target ? link_target : in_repo_path);
4964 error = got_object_get_type(&obj_type, repo, obj_id);
4968 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4969 error = got_error_path(link_target ? link_target : in_repo_path,
4974 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4977 bca.f = got_opentemp();
4978 if (bca.f == NULL) {
4979 error = got_error_from_errno("got_opentemp");
4982 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4983 &bca.line_offsets, bca.f, blob);
4984 if (error || bca.nlines == 0)
4987 /* Don't include \n at EOF in the blame line count. */
4988 if (bca.line_offsets[bca.nlines - 1] == filesize)
4991 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4992 if (bca.lines == NULL) {
4993 error = got_error_from_errno("calloc");
4997 bca.nlines_prec = 0;
5005 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5006 repo, blame_cb, &bca, check_cancelled, NULL);
5015 got_object_blob_close(blob);
5017 got_worktree_close(worktree);
5019 const struct got_error *close_err = got_repo_close(repo);
5024 for (i = 0; i < bca.nlines; i++) {
5025 struct blame_line *bline = &bca.lines[i];
5026 free(bline->id_str);
5027 free(bline->committer);
5031 free(bca.line_offsets);
5032 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5033 error = got_error_from_errno("fclose");
5041 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5046 static const struct got_error *
5047 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5048 const char *root_path, struct got_repository *repo)
5050 const struct got_error *err = NULL;
5051 int is_root_path = (strcmp(path, root_path) == 0);
5052 const char *modestr = "";
5053 mode_t mode = got_tree_entry_get_mode(te);
5054 char *link_target = NULL;
5056 path += strlen(root_path);
5057 while (path[0] == '/')
5060 if (got_object_tree_entry_is_submodule(te))
5062 else if (S_ISLNK(mode)) {
5065 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5068 for (i = 0; i < strlen(link_target); i++) {
5069 if (!isprint((unsigned char)link_target[i]))
5070 link_target[i] = '?';
5075 else if (S_ISDIR(mode))
5077 else if (mode & S_IXUSR)
5080 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5081 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5082 link_target ? " -> ": "", link_target ? link_target : "");
5088 static const struct got_error *
5089 print_tree(const char *path, struct got_object_id *commit_id,
5090 int show_ids, int recurse, const char *root_path,
5091 struct got_repository *repo)
5093 const struct got_error *err = NULL;
5094 struct got_object_id *tree_id = NULL;
5095 struct got_tree_object *tree = NULL;
5098 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5102 err = got_object_open_as_tree(&tree, repo, tree_id);
5105 nentries = got_object_tree_get_nentries(tree);
5106 for (i = 0; i < nentries; i++) {
5107 struct got_tree_entry *te;
5110 if (sigint_received || sigpipe_received)
5113 te = got_object_tree_get_entry(tree, i);
5116 err = got_object_id_str(&id_str,
5117 got_tree_entry_get_id(te));
5120 if (asprintf(&id, "%s ", id_str) == -1) {
5121 err = got_error_from_errno("asprintf");
5127 err = print_entry(te, id, path, root_path, repo);
5132 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5134 if (asprintf(&child_path, "%s%s%s", path,
5135 path[0] == '/' && path[1] == '\0' ? "" : "/",
5136 got_tree_entry_get_name(te)) == -1) {
5137 err = got_error_from_errno("asprintf");
5140 err = print_tree(child_path, commit_id, show_ids, 1,
5149 got_object_tree_close(tree);
5154 static const struct got_error *
5155 cmd_tree(int argc, char *argv[])
5157 const struct got_error *error;
5158 struct got_repository *repo = NULL;
5159 struct got_worktree *worktree = NULL;
5160 const char *path, *refname = NULL;
5161 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5162 struct got_object_id *commit_id = NULL;
5163 char *commit_id_str = NULL;
5164 int show_ids = 0, recurse = 0;
5168 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5173 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5176 commit_id_str = optarg;
5179 repo_path = realpath(optarg, NULL);
5180 if (repo_path == NULL)
5181 return got_error_from_errno2("realpath",
5183 got_path_strip_trailing_slashes(repo_path);
5207 cwd = getcwd(NULL, 0);
5209 error = got_error_from_errno("getcwd");
5212 if (repo_path == NULL) {
5213 error = got_worktree_open(&worktree, cwd);
5214 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5220 strdup(got_worktree_get_repo_path(worktree));
5221 if (repo_path == NULL)
5222 error = got_error_from_errno("strdup");
5226 repo_path = strdup(cwd);
5227 if (repo_path == NULL) {
5228 error = got_error_from_errno("strdup");
5234 error = got_repo_open(&repo, repo_path, NULL);
5239 const char *prefix = got_worktree_get_path_prefix(worktree);
5244 error = got_worktree_resolve_path(&p, worktree, path);
5247 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5248 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5250 error = got_error_from_errno("asprintf");
5255 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5259 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5264 error = got_repo_map_path(&in_repo_path, repo, path);
5269 if (commit_id_str == NULL) {
5270 struct got_reference *head_ref;
5272 refname = got_worktree_get_head_ref_name(worktree);
5274 refname = GOT_REF_HEAD;
5275 error = got_ref_open(&head_ref, repo, refname, 0);
5278 error = got_ref_resolve(&commit_id, repo, head_ref);
5279 got_ref_close(head_ref);
5283 struct got_reflist_head refs;
5285 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5289 error = got_repo_match_object_id(&commit_id, NULL,
5290 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5291 got_ref_list_free(&refs);
5296 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5297 in_repo_path, repo);
5304 got_worktree_close(worktree);
5306 const struct got_error *close_err = got_repo_close(repo);
5316 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5317 "[-S status-codes] [path ...]\n", getprogname());
5321 struct got_status_arg {
5326 static const struct got_error *
5327 print_status(void *arg, unsigned char status, unsigned char staged_status,
5328 const char *path, struct got_object_id *blob_id,
5329 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5330 int dirfd, const char *de_name)
5332 struct got_status_arg *st = arg;
5334 if (status == staged_status && (status == GOT_STATUS_DELETE))
5335 status = GOT_STATUS_NO_CHANGE;
5336 if (st != NULL && st->status_codes) {
5337 size_t ncodes = strlen(st->status_codes);
5340 for (i = 0; i < ncodes ; i++) {
5342 if (status == st->status_codes[i] ||
5343 staged_status == st->status_codes[i]) {
5348 if (status == st->status_codes[i] ||
5349 staged_status == st->status_codes[i])
5354 if (st->suppress && j == 0)
5361 printf("%c%c %s\n", status, staged_status, path);
5365 static const struct got_error *
5366 cmd_status(int argc, char *argv[])
5368 const struct got_error *error = NULL;
5369 struct got_repository *repo = NULL;
5370 struct got_worktree *worktree = NULL;
5371 struct got_status_arg st;
5373 struct got_pathlist_head paths;
5374 struct got_pathlist_entry *pe;
5375 int ch, i, no_ignores = 0;
5379 memset(&st, 0, sizeof(st));
5380 st.status_codes = NULL;
5383 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5389 if (st.status_codes != NULL && st.suppress == 0)
5390 option_conflict('S', 's');
5394 for (i = 0; i < strlen(optarg); i++) {
5395 switch (optarg[i]) {
5396 case GOT_STATUS_MODIFY:
5397 case GOT_STATUS_ADD:
5398 case GOT_STATUS_DELETE:
5399 case GOT_STATUS_CONFLICT:
5400 case GOT_STATUS_MISSING:
5401 case GOT_STATUS_OBSTRUCTED:
5402 case GOT_STATUS_UNVERSIONED:
5403 case GOT_STATUS_MODE_CHANGE:
5404 case GOT_STATUS_NONEXISTENT:
5407 errx(1, "invalid status code '%c'",
5411 if (ch == 's' && st.suppress)
5412 option_conflict('s', 'S');
5413 st.status_codes = optarg;
5425 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5429 cwd = getcwd(NULL, 0);
5431 error = got_error_from_errno("getcwd");
5435 error = got_worktree_open(&worktree, cwd);
5437 if (error->code == GOT_ERR_NOT_WORKTREE)
5438 error = wrap_not_worktree_error(error, "status", cwd);
5442 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5447 error = apply_unveil(got_repo_get_path(repo), 1,
5448 got_worktree_get_root_path(worktree));
5452 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5456 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5457 print_status, &st, check_cancelled, NULL);
5459 TAILQ_FOREACH(pe, &paths, entry)
5460 free((char *)pe->path);
5461 got_pathlist_free(&paths);
5470 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5476 static const struct got_error *
5477 list_refs(struct got_repository *repo, const char *refname)
5479 static const struct got_error *err = NULL;
5480 struct got_reflist_head refs;
5481 struct got_reflist_entry *re;
5484 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5488 TAILQ_FOREACH(re, &refs, entry) {
5490 refstr = got_ref_to_str(re->ref);
5492 return got_error_from_errno("got_ref_to_str");
5493 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5497 got_ref_list_free(&refs);
5501 static const struct got_error *
5502 delete_ref_by_name(struct got_repository *repo, const char *refname)
5504 const struct got_error *err;
5505 struct got_reference *ref;
5507 err = got_ref_open(&ref, repo, refname, 0);
5511 err = delete_ref(repo, ref);
5516 static const struct got_error *
5517 add_ref(struct got_repository *repo, const char *refname, const char *target)
5519 const struct got_error *err = NULL;
5520 struct got_object_id *id;
5521 struct got_reference *ref = NULL;
5524 * Don't let the user create a reference name with a leading '-'.
5525 * While technically a valid reference name, this case is usually
5526 * an unintended typo.
5528 if (refname[0] == '-')
5529 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5531 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5534 struct got_reference *target_ref;
5536 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5538 err = got_ref_open(&target_ref, repo, target, 0);
5541 err = got_ref_resolve(&id, repo, target_ref);
5542 got_ref_close(target_ref);
5547 err = got_ref_alloc(&ref, refname, id);
5551 err = got_ref_write(ref, repo);
5559 static const struct got_error *
5560 add_symref(struct got_repository *repo, const char *refname, const char *target)
5562 const struct got_error *err = NULL;
5563 struct got_reference *ref = NULL;
5564 struct got_reference *target_ref = NULL;
5567 * Don't let the user create a reference name with a leading '-'.
5568 * While technically a valid reference name, this case is usually
5569 * an unintended typo.
5571 if (refname[0] == '-')
5572 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5574 err = got_ref_open(&target_ref, repo, target, 0);
5578 err = got_ref_alloc_symref(&ref, refname, target_ref);
5582 err = got_ref_write(ref, repo);
5585 got_ref_close(target_ref);
5591 static const struct got_error *
5592 cmd_ref(int argc, char *argv[])
5594 const struct got_error *error = NULL;
5595 struct got_repository *repo = NULL;
5596 struct got_worktree *worktree = NULL;
5597 char *cwd = NULL, *repo_path = NULL;
5598 int ch, do_list = 0, do_delete = 0;
5599 const char *obj_arg = NULL, *symref_target= NULL;
5600 char *refname = NULL;
5602 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5611 repo_path = realpath(optarg, NULL);
5612 if (repo_path == NULL)
5613 return got_error_from_errno2("realpath",
5615 got_path_strip_trailing_slashes(repo_path);
5621 symref_target = optarg;
5629 if (obj_arg && do_list)
5630 option_conflict('c', 'l');
5631 if (obj_arg && do_delete)
5632 option_conflict('c', 'd');
5633 if (obj_arg && symref_target)
5634 option_conflict('c', 's');
5635 if (symref_target && do_delete)
5636 option_conflict('s', 'd');
5637 if (symref_target && do_list)
5638 option_conflict('s', 'l');
5639 if (do_delete && do_list)
5640 option_conflict('d', 'l');
5646 if (argc != 0 && argc != 1)
5649 refname = strdup(argv[0]);
5650 if (refname == NULL) {
5651 error = got_error_from_errno("strdup");
5658 refname = strdup(argv[0]);
5659 if (refname == NULL) {
5660 error = got_error_from_errno("strdup");
5666 got_path_strip_trailing_slashes(refname);
5670 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5674 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5675 "sendfd unveil", NULL) == -1)
5679 cwd = getcwd(NULL, 0);
5681 error = got_error_from_errno("getcwd");
5685 if (repo_path == NULL) {
5686 error = got_worktree_open(&worktree, cwd);
5687 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5693 strdup(got_worktree_get_repo_path(worktree));
5694 if (repo_path == NULL)
5695 error = got_error_from_errno("strdup");
5699 repo_path = strdup(cwd);
5700 if (repo_path == NULL) {
5701 error = got_error_from_errno("strdup");
5707 error = got_repo_open(&repo, repo_path, NULL);
5711 error = apply_unveil(got_repo_get_path(repo), do_list,
5712 worktree ? got_worktree_get_root_path(worktree) : NULL);
5717 error = list_refs(repo, refname);
5719 error = delete_ref_by_name(repo, refname);
5720 else if (symref_target)
5721 error = add_symref(repo, refname, symref_target);
5723 if (obj_arg == NULL)
5725 error = add_ref(repo, refname, obj_arg);
5730 const struct got_error *close_err = got_repo_close(repo);
5735 got_worktree_close(worktree);
5745 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5746 "[name]\n", getprogname());
5750 static const struct got_error *
5751 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5752 struct got_reference *ref)
5754 const struct got_error *err = NULL;
5755 const char *refname, *marker = " ";
5758 refname = got_ref_get_name(ref);
5759 if (worktree && strcmp(refname,
5760 got_worktree_get_head_ref_name(worktree)) == 0) {
5761 struct got_object_id *id = NULL;
5763 err = got_ref_resolve(&id, repo, ref);
5766 if (got_object_id_cmp(id,
5767 got_worktree_get_base_commit_id(worktree)) == 0)
5774 if (strncmp(refname, "refs/heads/", 11) == 0)
5776 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5778 if (strncmp(refname, "refs/remotes/", 13) == 0)
5781 refstr = got_ref_to_str(ref);
5783 return got_error_from_errno("got_ref_to_str");
5785 printf("%s%s: %s\n", marker, refname, refstr);
5790 static const struct got_error *
5791 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5793 const char *refname;
5795 if (worktree == NULL)
5796 return got_error(GOT_ERR_NOT_WORKTREE);
5798 refname = got_worktree_get_head_ref_name(worktree);
5800 if (strncmp(refname, "refs/heads/", 11) == 0)
5802 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5805 printf("%s\n", refname);
5810 static const struct got_error *
5811 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5813 static const struct got_error *err = NULL;
5814 struct got_reflist_head refs;
5815 struct got_reflist_entry *re;
5816 struct got_reference *temp_ref = NULL;
5817 int rebase_in_progress, histedit_in_progress;
5822 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5827 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5832 if (rebase_in_progress || histedit_in_progress) {
5833 err = got_ref_open(&temp_ref, repo,
5834 got_worktree_get_head_ref_name(worktree), 0);
5837 list_branch(repo, worktree, temp_ref);
5838 got_ref_close(temp_ref);
5842 err = got_ref_list(&refs, repo, "refs/heads",
5843 got_ref_cmp_by_name, NULL);
5847 TAILQ_FOREACH(re, &refs, entry)
5848 list_branch(repo, worktree, re->ref);
5850 got_ref_list_free(&refs);
5852 err = got_ref_list(&refs, repo, "refs/remotes",
5853 got_ref_cmp_by_name, NULL);
5857 TAILQ_FOREACH(re, &refs, entry)
5858 list_branch(repo, worktree, re->ref);
5860 got_ref_list_free(&refs);
5865 static const struct got_error *
5866 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5867 const char *branch_name)
5869 const struct got_error *err = NULL;
5870 struct got_reference *ref = NULL;
5871 char *refname, *remote_refname = NULL;
5873 if (strncmp(branch_name, "refs/", 5) == 0)
5875 if (strncmp(branch_name, "heads/", 6) == 0)
5877 else if (strncmp(branch_name, "remotes/", 8) == 0)
5880 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5881 return got_error_from_errno("asprintf");
5883 if (asprintf(&remote_refname, "refs/remotes/%s",
5884 branch_name) == -1) {
5885 err = got_error_from_errno("asprintf");
5889 err = got_ref_open(&ref, repo, refname, 0);
5891 const struct got_error *err2;
5892 if (err->code != GOT_ERR_NOT_REF)
5895 * Keep 'err' intact such that if neither branch exists
5896 * we report "refs/heads" rather than "refs/remotes" in
5897 * our error message.
5899 err2 = got_ref_open(&ref, repo, remote_refname, 0);
5906 strcmp(got_worktree_get_head_ref_name(worktree),
5907 got_ref_get_name(ref)) == 0) {
5908 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5909 "will not delete this work tree's current branch");
5913 err = delete_ref(repo, ref);
5918 free(remote_refname);
5922 static const struct got_error *
5923 add_branch(struct got_repository *repo, const char *branch_name,
5924 struct got_object_id *base_commit_id)
5926 const struct got_error *err = NULL;
5927 struct got_reference *ref = NULL;
5928 char *base_refname = NULL, *refname = NULL;
5931 * Don't let the user create a branch name with a leading '-'.
5932 * While technically a valid reference name, this case is usually
5933 * an unintended typo.
5935 if (branch_name[0] == '-')
5936 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5938 if (strncmp(branch_name, "refs/heads/", 11) == 0)
5941 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5942 err = got_error_from_errno("asprintf");
5946 err = got_ref_open(&ref, repo, refname, 0);
5948 err = got_error(GOT_ERR_BRANCH_EXISTS);
5950 } else if (err->code != GOT_ERR_NOT_REF)
5953 err = got_ref_alloc(&ref, refname, base_commit_id);
5957 err = got_ref_write(ref, repo);
5966 static const struct got_error *
5967 cmd_branch(int argc, char *argv[])
5969 const struct got_error *error = NULL;
5970 struct got_repository *repo = NULL;
5971 struct got_worktree *worktree = NULL;
5972 char *cwd = NULL, *repo_path = NULL;
5973 int ch, do_list = 0, do_show = 0, do_update = 1;
5974 const char *delref = NULL, *commit_id_arg = NULL;
5975 struct got_reference *ref = NULL;
5976 struct got_pathlist_head paths;
5977 struct got_pathlist_entry *pe;
5978 struct got_object_id *commit_id = NULL;
5979 char *commit_id_str = NULL;
5983 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5986 commit_id_arg = optarg;
5992 repo_path = realpath(optarg, NULL);
5993 if (repo_path == NULL)
5994 return got_error_from_errno2("realpath",
5996 got_path_strip_trailing_slashes(repo_path);
6010 if (do_list && delref)
6011 option_conflict('l', 'd');
6016 if (!do_list && !delref && argc == 0)
6019 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6020 errx(1, "-c option can only be used when creating a branch");
6022 if (do_list || delref) {
6025 } else if (!do_show && argc != 1)
6029 if (do_list || do_show) {
6030 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6034 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6035 "sendfd unveil", NULL) == -1)
6039 cwd = getcwd(NULL, 0);
6041 error = got_error_from_errno("getcwd");
6045 if (repo_path == NULL) {
6046 error = got_worktree_open(&worktree, cwd);
6047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6053 strdup(got_worktree_get_repo_path(worktree));
6054 if (repo_path == NULL)
6055 error = got_error_from_errno("strdup");
6059 repo_path = strdup(cwd);
6060 if (repo_path == NULL) {
6061 error = got_error_from_errno("strdup");
6067 error = got_repo_open(&repo, repo_path, NULL);
6071 error = apply_unveil(got_repo_get_path(repo), do_list,
6072 worktree ? got_worktree_get_root_path(worktree) : NULL);
6077 error = show_current_branch(repo, worktree);
6079 error = list_branches(repo, worktree);
6081 error = delete_branch(repo, worktree, delref);
6083 struct got_reflist_head refs;
6085 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6089 if (commit_id_arg == NULL)
6090 commit_id_arg = worktree ?
6091 got_worktree_get_head_ref_name(worktree) :
6093 error = got_repo_match_object_id(&commit_id, NULL,
6094 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6095 got_ref_list_free(&refs);
6098 error = add_branch(repo, argv[0], commit_id);
6101 if (worktree && do_update) {
6102 struct got_update_progress_arg upa;
6103 char *branch_refname = NULL;
6105 error = got_object_id_str(&commit_id_str, commit_id);
6108 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6112 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6114 error = got_error_from_errno("asprintf");
6117 error = got_ref_open(&ref, repo, branch_refname, 0);
6118 free(branch_refname);
6121 error = switch_head_ref(ref, commit_id, worktree,
6125 error = got_worktree_set_base_commit_id(worktree, repo,
6129 memset(&upa, 0, sizeof(upa));
6130 error = got_worktree_checkout_files(worktree, &paths,
6131 repo, update_progress, &upa, check_cancelled,
6135 if (upa.did_something) {
6136 printf("Updated to %s: %s\n",
6137 got_worktree_get_head_ref_name(worktree),
6140 print_update_progress_stats(&upa);
6147 const struct got_error *close_err = got_repo_close(repo);
6152 got_worktree_close(worktree);
6156 free(commit_id_str);
6157 TAILQ_FOREACH(pe, &paths, entry)
6158 free((char *)pe->path);
6159 got_pathlist_free(&paths);
6168 "usage: %s tag [-c commit] [-r repository] [-l] "
6169 "[-m message] name\n", getprogname());
6174 static const struct got_error *
6175 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6177 const struct got_error *err = NULL;
6178 struct got_reflist_entry *re, *se, *new;
6179 struct got_object_id *re_id, *se_id;
6180 struct got_tag_object *re_tag, *se_tag;
6181 time_t re_time, se_time;
6183 STAILQ_FOREACH(re, tags, entry) {
6184 se = STAILQ_FIRST(sorted);
6186 err = got_reflist_entry_dup(&new, re);
6189 STAILQ_INSERT_HEAD(sorted, new, entry);
6192 err = got_ref_resolve(&re_id, repo, re->ref);
6195 err = got_object_open_as_tag(&re_tag, repo, re_id);
6199 re_time = got_object_tag_get_tagger_time(re_tag);
6200 got_object_tag_close(re_tag);
6204 err = got_ref_resolve(&se_id, repo, re->ref);
6207 err = got_object_open_as_tag(&se_tag, repo, se_id);
6211 se_time = got_object_tag_get_tagger_time(se_tag);
6212 got_object_tag_close(se_tag);
6214 if (se_time > re_time) {
6215 err = got_reflist_entry_dup(&new, re);
6218 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6221 se = STAILQ_NEXT(se, entry);
6230 static const struct got_error *
6231 list_tags(struct got_repository *repo, struct got_worktree *worktree)
6233 static const struct got_error *err = NULL;
6234 struct got_reflist_head refs;
6235 struct got_reflist_entry *re;
6239 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6243 TAILQ_FOREACH(re, &refs, entry) {
6244 const char *refname;
6245 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6249 struct got_object_id *id;
6250 struct got_tag_object *tag;
6251 struct got_commit_object *commit = NULL;
6253 refname = got_ref_get_name(re->ref);
6254 if (strncmp(refname, "refs/tags/", 10) != 0)
6257 refstr = got_ref_to_str(re->ref);
6258 if (refstr == NULL) {
6259 err = got_error_from_errno("got_ref_to_str");
6262 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6265 err = got_ref_resolve(&id, repo, re->ref);
6268 err = got_object_open_as_tag(&tag, repo, id);
6270 if (err->code != GOT_ERR_OBJ_TYPE) {
6274 /* "lightweight" tag */
6275 err = got_object_open_as_commit(&commit, repo, id);
6280 tagger = got_object_commit_get_committer(commit);
6282 got_object_commit_get_committer_time(commit);
6283 err = got_object_id_str(&id_str, id);
6289 tagger = got_object_tag_get_tagger(tag);
6290 tagger_time = got_object_tag_get_tagger_time(tag);
6291 err = got_object_id_str(&id_str,
6292 got_object_tag_get_object_id(tag));
6296 printf("from: %s\n", tagger);
6297 datestr = get_datestr(&tagger_time, datebuf);
6299 printf("date: %s UTC\n", datestr);
6301 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6303 switch (got_object_tag_get_object_type(tag)) {
6304 case GOT_OBJ_TYPE_BLOB:
6305 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6308 case GOT_OBJ_TYPE_TREE:
6309 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6312 case GOT_OBJ_TYPE_COMMIT:
6313 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6316 case GOT_OBJ_TYPE_TAG:
6317 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6326 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6329 got_object_commit_close(commit);
6331 tagmsg0 = strdup(got_object_tag_get_message(tag));
6332 got_object_tag_close(tag);
6333 if (tagmsg0 == NULL) {
6334 err = got_error_from_errno("strdup");
6341 line = strsep(&tagmsg, "\n");
6343 printf(" %s\n", line);
6348 got_ref_list_free(&refs);
6352 static const struct got_error *
6353 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6354 const char *tag_name, const char *repo_path)
6356 const struct got_error *err = NULL;
6357 char *template = NULL, *initial_content = NULL;
6358 char *editor = NULL;
6359 int initial_content_len;
6362 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6363 err = got_error_from_errno("asprintf");
6367 initial_content_len = asprintf(&initial_content,
6368 "\n# tagging commit %s as %s\n",
6369 commit_id_str, tag_name);
6370 if (initial_content_len == -1) {
6371 err = got_error_from_errno("asprintf");
6375 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6379 if (write(fd, initial_content, initial_content_len) == -1) {
6380 err = got_error_from_errno2("write", *tagmsg_path);
6384 err = get_editor(&editor);
6387 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6388 initial_content_len, 1);
6390 free(initial_content);
6394 if (fd != -1 && close(fd) == -1 && err == NULL)
6395 err = got_error_from_errno2("close", *tagmsg_path);
6397 /* Editor is done; we can now apply unveil(2) */
6399 err = apply_unveil(repo_path, 0, NULL);
6407 static const struct got_error *
6408 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6409 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6411 const struct got_error *err = NULL;
6412 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6413 char *label = NULL, *commit_id_str = NULL;
6414 struct got_reference *ref = NULL;
6415 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6416 char *tagmsg_path = NULL, *tag_id_str = NULL;
6417 int preserve_tagmsg = 0;
6418 struct got_reflist_head refs;
6423 * Don't let the user create a tag name with a leading '-'.
6424 * While technically a valid reference name, this case is usually
6425 * an unintended typo.
6427 if (tag_name[0] == '-')
6428 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6430 err = get_author(&tagger, repo, worktree);
6434 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6438 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6439 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6443 err = got_object_id_str(&commit_id_str, commit_id);
6447 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6448 refname = strdup(tag_name);
6449 if (refname == NULL) {
6450 err = got_error_from_errno("strdup");
6454 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6455 err = got_error_from_errno("asprintf");
6459 err = got_ref_open(&ref, repo, refname, 0);
6461 err = got_error(GOT_ERR_TAG_EXISTS);
6463 } else if (err->code != GOT_ERR_NOT_REF)
6466 if (tagmsg_arg == NULL) {
6467 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6468 tag_name, got_repo_get_path(repo));
6470 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6471 tagmsg_path != NULL)
6472 preserve_tagmsg = 1;
6477 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6478 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6481 preserve_tagmsg = 1;
6485 err = got_ref_alloc(&ref, refname, tag_id);
6488 preserve_tagmsg = 1;
6492 err = got_ref_write(ref, repo);
6495 preserve_tagmsg = 1;
6499 err = got_object_id_str(&tag_id_str, tag_id);
6502 preserve_tagmsg = 1;
6505 printf("Created tag %s\n", tag_id_str);
6507 if (preserve_tagmsg) {
6508 fprintf(stderr, "%s: tag message preserved in %s\n",
6509 getprogname(), tagmsg_path);
6510 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6511 err = got_error_from_errno2("unlink", tagmsg_path);
6516 free(commit_id_str);
6521 got_ref_list_free(&refs);
6525 static const struct got_error *
6526 cmd_tag(int argc, char *argv[])
6528 const struct got_error *error = NULL;
6529 struct got_repository *repo = NULL;
6530 struct got_worktree *worktree = NULL;
6531 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6532 char *gitconfig_path = NULL;
6533 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6534 int ch, do_list = 0;
6536 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6539 commit_id_arg = optarg;
6545 repo_path = realpath(optarg, NULL);
6546 if (repo_path == NULL)
6547 return got_error_from_errno2("realpath",
6549 got_path_strip_trailing_slashes(repo_path);
6564 if (commit_id_arg != NULL)
6566 "-c option can only be used when creating a tag");
6568 option_conflict('l', 'm');
6571 } else if (argc != 1)
6578 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6582 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6583 "sendfd unveil", NULL) == -1)
6587 cwd = getcwd(NULL, 0);
6589 error = got_error_from_errno("getcwd");
6593 if (repo_path == NULL) {
6594 error = got_worktree_open(&worktree, cwd);
6595 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6601 strdup(got_worktree_get_repo_path(worktree));
6602 if (repo_path == NULL)
6603 error = got_error_from_errno("strdup");
6607 repo_path = strdup(cwd);
6608 if (repo_path == NULL) {
6609 error = got_error_from_errno("strdup");
6616 error = got_repo_open(&repo, repo_path, NULL);
6619 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6622 error = list_tags(repo, worktree);
6624 error = get_gitconfig_path(&gitconfig_path);
6627 error = got_repo_open(&repo, repo_path, gitconfig_path);
6632 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6637 if (commit_id_arg == NULL) {
6638 struct got_reference *head_ref;
6639 struct got_object_id *commit_id;
6640 error = got_ref_open(&head_ref, repo,
6641 worktree ? got_worktree_get_head_ref_name(worktree)
6645 error = got_ref_resolve(&commit_id, repo, head_ref);
6646 got_ref_close(head_ref);
6649 error = got_object_id_str(&commit_id_str, commit_id);
6655 error = add_tag(repo, worktree, tag_name,
6656 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6660 const struct got_error *close_err = got_repo_close(repo);
6665 got_worktree_close(worktree);
6668 free(gitconfig_path);
6669 free(commit_id_str);
6676 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6681 static const struct got_error *
6682 add_progress(void *arg, unsigned char status, const char *path)
6684 while (path[0] == '/')
6686 printf("%c %s\n", status, path);
6690 static const struct got_error *
6691 cmd_add(int argc, char *argv[])
6693 const struct got_error *error = NULL;
6694 struct got_repository *repo = NULL;
6695 struct got_worktree *worktree = NULL;
6697 struct got_pathlist_head paths;
6698 struct got_pathlist_entry *pe;
6699 int ch, can_recurse = 0, no_ignores = 0;
6703 while ((ch = getopt(argc, argv, "IR")) != -1) {
6721 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6728 cwd = getcwd(NULL, 0);
6730 error = got_error_from_errno("getcwd");
6734 error = got_worktree_open(&worktree, cwd);
6736 if (error->code == GOT_ERR_NOT_WORKTREE)
6737 error = wrap_not_worktree_error(error, "add", cwd);
6741 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6746 error = apply_unveil(got_repo_get_path(repo), 1,
6747 got_worktree_get_root_path(worktree));
6751 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6758 TAILQ_FOREACH(pe, &paths, entry) {
6759 if (asprintf(&ondisk_path, "%s/%s",
6760 got_worktree_get_root_path(worktree),
6762 error = got_error_from_errno("asprintf");
6765 if (lstat(ondisk_path, &sb) == -1) {
6766 if (errno == ENOENT) {
6770 error = got_error_from_errno2("lstat",
6776 if (S_ISDIR(sb.st_mode)) {
6777 error = got_error_msg(GOT_ERR_BAD_PATH,
6778 "adding directories requires -R option");
6784 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6785 NULL, repo, no_ignores);
6788 const struct got_error *close_err = got_repo_close(repo);
6793 got_worktree_close(worktree);
6794 TAILQ_FOREACH(pe, &paths, entry)
6795 free((char *)pe->path);
6796 got_pathlist_free(&paths);
6804 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6805 "path ...\n", getprogname());
6809 static const struct got_error *
6810 print_remove_status(void *arg, unsigned char status,
6811 unsigned char staged_status, const char *path)
6813 while (path[0] == '/')
6815 if (status == GOT_STATUS_NONEXISTENT)
6817 if (status == staged_status && (status == GOT_STATUS_DELETE))
6818 status = GOT_STATUS_NO_CHANGE;
6819 printf("%c%c %s\n", status, staged_status, path);
6823 static const struct got_error *
6824 cmd_remove(int argc, char *argv[])
6826 const struct got_error *error = NULL;
6827 struct got_worktree *worktree = NULL;
6828 struct got_repository *repo = NULL;
6829 const char *status_codes = NULL;
6831 struct got_pathlist_head paths;
6832 struct got_pathlist_entry *pe;
6833 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6837 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6840 delete_local_mods = 1;
6849 for (i = 0; i < strlen(optarg); i++) {
6850 switch (optarg[i]) {
6851 case GOT_STATUS_MODIFY:
6852 delete_local_mods = 1;
6854 case GOT_STATUS_MISSING:
6857 errx(1, "invalid status code '%c'",
6861 status_codes = optarg;
6873 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6880 cwd = getcwd(NULL, 0);
6882 error = got_error_from_errno("getcwd");
6885 error = got_worktree_open(&worktree, cwd);
6887 if (error->code == GOT_ERR_NOT_WORKTREE)
6888 error = wrap_not_worktree_error(error, "remove", cwd);
6892 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6897 error = apply_unveil(got_repo_get_path(repo), 1,
6898 got_worktree_get_root_path(worktree));
6902 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6909 TAILQ_FOREACH(pe, &paths, entry) {
6910 if (asprintf(&ondisk_path, "%s/%s",
6911 got_worktree_get_root_path(worktree),
6913 error = got_error_from_errno("asprintf");
6916 if (lstat(ondisk_path, &sb) == -1) {
6917 if (errno == ENOENT) {
6921 error = got_error_from_errno2("lstat",
6927 if (S_ISDIR(sb.st_mode)) {
6928 error = got_error_msg(GOT_ERR_BAD_PATH,
6929 "removing directories requires -R option");
6935 error = got_worktree_schedule_delete(worktree, &paths,
6936 delete_local_mods, status_codes, print_remove_status, NULL,
6937 repo, keep_on_disk);
6940 const struct got_error *close_err = got_repo_close(repo);
6945 got_worktree_close(worktree);
6946 TAILQ_FOREACH(pe, &paths, entry)
6947 free((char *)pe->path);
6948 got_pathlist_free(&paths);
6956 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6957 "path ...\n", getprogname());
6961 static const struct got_error *
6962 revert_progress(void *arg, unsigned char status, const char *path)
6964 if (status == GOT_STATUS_UNVERSIONED)
6967 while (path[0] == '/')
6969 printf("%c %s\n", status, path);
6973 struct choose_patch_arg {
6974 FILE *patch_script_file;
6978 static const struct got_error *
6979 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6980 int nchanges, const char *action)
6983 size_t linesize = 0;
6987 case GOT_STATUS_ADD:
6988 printf("A %s\n%s this addition? [y/n] ", path, action);
6990 case GOT_STATUS_DELETE:
6991 printf("D %s\n%s this deletion? [y/n] ", path, action);
6993 case GOT_STATUS_MODIFY:
6994 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6995 return got_error_from_errno("fseek");
6996 printf(GOT_COMMIT_SEP_STR);
6997 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6999 if (ferror(patch_file))
7000 return got_error_from_errno("getline");
7001 printf(GOT_COMMIT_SEP_STR);
7002 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7003 path, n, nchanges, action);
7006 return got_error_path(path, GOT_ERR_FILE_STATUS);
7012 static const struct got_error *
7013 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7014 FILE *patch_file, int n, int nchanges)
7016 const struct got_error *err = NULL;
7018 size_t linesize = 0;
7021 struct choose_patch_arg *a = arg;
7023 *choice = GOT_PATCH_CHOICE_NONE;
7025 if (a->patch_script_file) {
7027 err = show_change(status, path, patch_file, n, nchanges,
7031 linelen = getline(&line, &linesize, a->patch_script_file);
7032 if (linelen == -1) {
7033 if (ferror(a->patch_script_file))
7034 return got_error_from_errno("getline");
7037 nl = strchr(line, '\n');
7040 if (strcmp(line, "y") == 0) {
7041 *choice = GOT_PATCH_CHOICE_YES;
7043 } else if (strcmp(line, "n") == 0) {
7044 *choice = GOT_PATCH_CHOICE_NO;
7046 } else if (strcmp(line, "q") == 0 &&
7047 status == GOT_STATUS_MODIFY) {
7048 *choice = GOT_PATCH_CHOICE_QUIT;
7051 printf("invalid response '%s'\n", line);
7056 while (resp != 'y' && resp != 'n' && resp != 'q') {
7057 err = show_change(status, path, patch_file, n, nchanges,
7064 if (status == GOT_STATUS_MODIFY) {
7065 if (resp != 'y' && resp != 'n' && resp != 'q') {
7066 printf("invalid response '%c'\n", resp);
7069 } else if (resp != 'y' && resp != 'n') {
7070 printf("invalid response '%c'\n", resp);
7076 *choice = GOT_PATCH_CHOICE_YES;
7077 else if (resp == 'n')
7078 *choice = GOT_PATCH_CHOICE_NO;
7079 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7080 *choice = GOT_PATCH_CHOICE_QUIT;
7086 static const struct got_error *
7087 cmd_revert(int argc, char *argv[])
7089 const struct got_error *error = NULL;
7090 struct got_worktree *worktree = NULL;
7091 struct got_repository *repo = NULL;
7092 char *cwd = NULL, *path = NULL;
7093 struct got_pathlist_head paths;
7094 struct got_pathlist_entry *pe;
7095 int ch, can_recurse = 0, pflag = 0;
7096 FILE *patch_script_file = NULL;
7097 const char *patch_script_path = NULL;
7098 struct choose_patch_arg cpa;
7102 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7108 patch_script_path = optarg;
7123 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 "unveil", NULL) == -1)
7129 if (patch_script_path && !pflag)
7130 errx(1, "-F option can only be used together with -p option");
7132 cwd = getcwd(NULL, 0);
7134 error = got_error_from_errno("getcwd");
7137 error = got_worktree_open(&worktree, cwd);
7139 if (error->code == GOT_ERR_NOT_WORKTREE)
7140 error = wrap_not_worktree_error(error, "revert", cwd);
7144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7149 if (patch_script_path) {
7150 patch_script_file = fopen(patch_script_path, "r");
7151 if (patch_script_file == NULL) {
7152 error = got_error_from_errno2("fopen",
7157 error = apply_unveil(got_repo_get_path(repo), 1,
7158 got_worktree_get_root_path(worktree));
7162 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7169 TAILQ_FOREACH(pe, &paths, entry) {
7170 if (asprintf(&ondisk_path, "%s/%s",
7171 got_worktree_get_root_path(worktree),
7173 error = got_error_from_errno("asprintf");
7176 if (lstat(ondisk_path, &sb) == -1) {
7177 if (errno == ENOENT) {
7181 error = got_error_from_errno2("lstat",
7187 if (S_ISDIR(sb.st_mode)) {
7188 error = got_error_msg(GOT_ERR_BAD_PATH,
7189 "reverting directories requires -R option");
7195 cpa.patch_script_file = patch_script_file;
7196 cpa.action = "revert";
7197 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7198 pflag ? choose_patch : NULL, &cpa, repo);
7200 if (patch_script_file && fclose(patch_script_file) == EOF &&
7202 error = got_error_from_errno2("fclose", patch_script_path);
7204 const struct got_error *close_err = got_repo_close(repo);
7209 got_worktree_close(worktree);
7218 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7219 "[path ...]\n", getprogname());
7223 struct collect_commit_logmsg_arg {
7224 const char *cmdline_log;
7225 const char *prepared_log;
7226 int non_interactive;
7228 const char *worktree_path;
7229 const char *branch_name;
7230 const char *repo_path;
7235 static const struct got_error *
7236 read_prepared_logmsg(char **logmsg, const char *path)
7238 const struct got_error *err = NULL;
7244 memset(&sb, 0, sizeof(sb));
7246 f = fopen(path, "r");
7248 return got_error_from_errno2("fopen", path);
7250 if (fstat(fileno(f), &sb) == -1) {
7251 err = got_error_from_errno2("fstat", path);
7254 if (sb.st_size == 0) {
7255 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7259 *logmsg = malloc(sb.st_size + 1);
7260 if (*logmsg == NULL) {
7261 err = got_error_from_errno("malloc");
7265 r = fread(*logmsg, 1, sb.st_size, f);
7266 if (r != sb.st_size) {
7268 err = got_error_from_errno2("fread", path);
7270 err = got_error(GOT_ERR_IO);
7273 (*logmsg)[sb.st_size] = '\0';
7275 if (fclose(f) == EOF && err == NULL)
7276 err = got_error_from_errno2("fclose", path);
7285 static const struct got_error *
7286 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7289 char *initial_content = NULL;
7290 struct got_pathlist_entry *pe;
7291 const struct got_error *err = NULL;
7292 char *template = NULL;
7293 struct collect_commit_logmsg_arg *a = arg;
7294 int initial_content_len;
7298 /* if a message was specified on the command line, just use it */
7299 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7300 len = strlen(a->cmdline_log) + 1;
7301 *logmsg = malloc(len + 1);
7302 if (*logmsg == NULL)
7303 return got_error_from_errno("malloc");
7304 strlcpy(*logmsg, a->cmdline_log, len);
7306 } else if (a->prepared_log != NULL && a->non_interactive)
7307 return read_prepared_logmsg(logmsg, a->prepared_log);
7309 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7310 return got_error_from_errno("asprintf");
7312 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7316 if (a->prepared_log) {
7318 err = read_prepared_logmsg(&msg, a->prepared_log);
7321 if (write(fd, msg, strlen(msg)) == -1) {
7322 err = got_error_from_errno2("write", a->logmsg_path);
7329 initial_content_len = asprintf(&initial_content,
7330 "\n# changes to be committed on branch %s:\n",
7332 if (initial_content_len == -1) {
7333 err = got_error_from_errno("asprintf");
7337 if (write(fd, initial_content, initial_content_len) == -1) {
7338 err = got_error_from_errno2("write", a->logmsg_path);
7342 TAILQ_FOREACH(pe, commitable_paths, entry) {
7343 struct got_commitable *ct = pe->data;
7344 dprintf(fd, "# %c %s\n",
7345 got_commitable_get_status(ct),
7346 got_commitable_get_path(ct));
7349 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7350 initial_content_len, a->prepared_log ? 0 : 1);
7352 free(initial_content);
7355 if (fd != -1 && close(fd) == -1 && err == NULL)
7356 err = got_error_from_errno2("close", a->logmsg_path);
7358 /* Editor is done; we can now apply unveil(2) */
7360 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7368 static const struct got_error *
7369 cmd_commit(int argc, char *argv[])
7371 const struct got_error *error = NULL;
7372 struct got_worktree *worktree = NULL;
7373 struct got_repository *repo = NULL;
7374 char *cwd = NULL, *id_str = NULL;
7375 struct got_object_id *id = NULL;
7376 const char *logmsg = NULL;
7377 char *prepared_logmsg = NULL;
7378 struct collect_commit_logmsg_arg cl_arg;
7379 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7380 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7381 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7382 struct got_pathlist_head paths;
7385 cl_arg.logmsg_path = NULL;
7387 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7391 option_conflict('F', 'm');
7392 prepared_logmsg = realpath(optarg, NULL);
7393 if (prepared_logmsg == NULL)
7394 return got_error_from_errno2("realpath",
7398 if (prepared_logmsg)
7399 option_conflict('m', 'F');
7403 non_interactive = 1;
7406 allow_bad_symlinks = 1;
7418 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7419 "unveil", NULL) == -1)
7422 cwd = getcwd(NULL, 0);
7424 error = got_error_from_errno("getcwd");
7427 error = got_worktree_open(&worktree, cwd);
7429 if (error->code == GOT_ERR_NOT_WORKTREE)
7430 error = wrap_not_worktree_error(error, "commit", cwd);
7434 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7437 if (rebase_in_progress) {
7438 error = got_error(GOT_ERR_REBASING);
7442 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7447 error = get_gitconfig_path(&gitconfig_path);
7450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7455 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7458 if (merge_in_progress) {
7459 error = got_error(GOT_ERR_MERGE_BUSY);
7463 error = get_author(&author, repo, worktree);
7468 * unveil(2) traverses exec(2); if an editor is used we have
7469 * to apply unveil after the log message has been written.
7471 if (logmsg == NULL || strlen(logmsg) == 0)
7472 error = get_editor(&editor);
7474 error = apply_unveil(got_repo_get_path(repo), 0,
7475 got_worktree_get_root_path(worktree));
7479 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7483 cl_arg.editor = editor;
7484 cl_arg.cmdline_log = logmsg;
7485 cl_arg.prepared_log = prepared_logmsg;
7486 cl_arg.non_interactive = non_interactive;
7487 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7488 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7489 if (!histedit_in_progress) {
7490 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7491 error = got_error(GOT_ERR_COMMIT_BRANCH);
7494 cl_arg.branch_name += 11;
7496 cl_arg.repo_path = got_repo_get_path(repo);
7497 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7498 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7499 print_status, NULL, repo);
7501 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7502 cl_arg.logmsg_path != NULL)
7503 preserve_logmsg = 1;
7507 error = got_object_id_str(&id_str, id);
7510 printf("Created commit %s\n", id_str);
7512 if (preserve_logmsg) {
7513 fprintf(stderr, "%s: log message preserved in %s\n",
7514 getprogname(), cl_arg.logmsg_path);
7515 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7517 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7518 free(cl_arg.logmsg_path);
7520 const struct got_error *close_err = got_repo_close(repo);
7525 got_worktree_close(worktree);
7528 free(gitconfig_path);
7531 free(prepared_logmsg);
7538 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7539 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7540 "[remote-repository]\n", getprogname());
7544 struct got_send_progress_arg {
7545 char last_scaled_packsize[FMT_SCALED_STRSIZE];
7548 int last_nobj_total;
7552 int printed_something;
7554 struct got_pathlist_head *delete_branches;
7557 static const struct got_error *
7558 send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7559 int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7562 struct got_send_progress_arg *a = arg;
7563 char scaled_packsize[FMT_SCALED_STRSIZE];
7564 char scaled_sent[FMT_SCALED_STRSIZE];
7565 int p_deltify = 0, p_written = 0, p_sent = 0;
7566 int print_searching = 0, print_total = 0;
7567 int print_deltify = 0, print_written = 0, print_sent = 0;
7569 if (a->verbosity < 0)
7573 const char *status = success ? "accepted" : "rejected";
7576 struct got_pathlist_entry *pe;
7577 TAILQ_FOREACH(pe, a->delete_branches, entry) {
7578 const char *branchname = pe->path;
7579 if (got_path_cmp(branchname, refname,
7580 strlen(branchname), strlen(refname)) == 0) {
7582 a->sent_something = 1;
7588 if (a->printed_something)
7590 printf("Server has %s %s", status, refname);
7591 a->printed_something = 1;
7595 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7596 return got_error_from_errno("fmt_scaled");
7597 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7598 return got_error_from_errno("fmt_scaled");
7600 if (a->last_ncommits != ncommits) {
7601 print_searching = 1;
7602 a->last_ncommits = ncommits;
7605 if (a->last_nobj_total != nobj_total) {
7606 print_searching = 1;
7608 a->last_nobj_total = nobj_total;
7611 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7612 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7613 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7614 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7615 return got_error(GOT_ERR_NO_SPACE);
7618 if (nobj_deltify > 0 || nobj_written > 0) {
7619 if (nobj_deltify > 0) {
7620 p_deltify = (nobj_deltify * 100) / nobj_total;
7621 if (p_deltify != a->last_p_deltify) {
7622 a->last_p_deltify = p_deltify;
7623 print_searching = 1;
7628 if (nobj_written > 0) {
7629 p_written = (nobj_written * 100) / nobj_total;
7630 if (p_written != a->last_p_written) {
7631 a->last_p_written = p_written;
7632 print_searching = 1;
7640 if (bytes_sent > 0) {
7641 p_sent = (bytes_sent * 100) / packfile_size;
7642 if (p_sent != a->last_p_sent) {
7643 a->last_p_sent = p_sent;
7644 print_searching = 1;
7650 a->sent_something = 1;
7653 if (print_searching || print_total || print_deltify || print_written ||
7656 if (print_searching)
7657 printf("packing %d reference%s", ncommits,
7658 ncommits == 1 ? "" : "s");
7660 printf("; %d object%s", nobj_total,
7661 nobj_total == 1 ? "" : "s");
7663 printf("; deltify: %d%%", p_deltify);
7665 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7666 scaled_packsize, p_sent);
7667 else if (print_written)
7668 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7669 scaled_packsize, p_written);
7670 if (print_searching || print_total || print_deltify ||
7671 print_written || print_sent) {
7672 a->printed_something = 1;
7678 static const struct got_error *
7679 cmd_send(int argc, char *argv[])
7681 const struct got_error *error = NULL;
7682 char *cwd = NULL, *repo_path = NULL;
7683 const char *remote_name;
7684 char *proto = NULL, *host = NULL, *port = NULL;
7685 char *repo_name = NULL, *server_path = NULL;
7686 const struct got_remote_repo *remotes, *remote = NULL;
7687 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7688 struct got_repository *repo = NULL;
7689 struct got_worktree *worktree = NULL;
7690 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7691 struct got_pathlist_head branches;
7692 struct got_pathlist_head tags;
7693 struct got_reflist_head all_branches;
7694 struct got_reflist_head all_tags;
7695 struct got_pathlist_head delete_args;
7696 struct got_pathlist_head delete_branches;
7697 struct got_reflist_entry *re;
7698 struct got_pathlist_entry *pe;
7699 int i, ch, sendfd = -1, sendstatus;
7701 struct got_send_progress_arg spa;
7702 int verbosity = 0, overwrite_refs = 0;
7703 int send_all_branches = 0, send_all_tags = 0;
7704 struct got_reference *ref = NULL;
7706 TAILQ_INIT(&branches);
7708 TAILQ_INIT(&all_branches);
7709 TAILQ_INIT(&all_tags);
7710 TAILQ_INIT(&delete_args);
7711 TAILQ_INIT(&delete_branches);
7713 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7716 send_all_branches = 1;
7719 error = got_pathlist_append(&branches, optarg, NULL);
7725 error = got_pathlist_append(&delete_args, optarg, NULL);
7733 repo_path = realpath(optarg, NULL);
7734 if (repo_path == NULL)
7735 return got_error_from_errno2("realpath",
7737 got_path_strip_trailing_slashes(repo_path);
7740 error = got_pathlist_append(&tags, optarg, NULL);
7751 else if (verbosity < 3)
7765 if (send_all_branches && !TAILQ_EMPTY(&branches))
7766 option_conflict('a', 'b');
7767 if (send_all_tags && !TAILQ_EMPTY(&tags))
7768 option_conflict('T', 't');
7772 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7774 remote_name = argv[0];
7778 cwd = getcwd(NULL, 0);
7780 error = got_error_from_errno("getcwd");
7784 if (repo_path == NULL) {
7785 error = got_worktree_open(&worktree, cwd);
7786 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7792 strdup(got_worktree_get_repo_path(worktree));
7793 if (repo_path == NULL)
7794 error = got_error_from_errno("strdup");
7798 repo_path = strdup(cwd);
7799 if (repo_path == NULL) {
7800 error = got_error_from_errno("strdup");
7806 error = got_repo_open(&repo, repo_path, NULL);
7811 worktree_conf = got_worktree_get_gotconfig(worktree);
7812 if (worktree_conf) {
7813 got_gotconfig_get_remotes(&nremotes, &remotes,
7815 for (i = 0; i < nremotes; i++) {
7816 if (strcmp(remotes[i].name, remote_name) == 0) {
7817 remote = &remotes[i];
7823 if (remote == NULL) {
7824 repo_conf = got_repo_get_gotconfig(repo);
7826 got_gotconfig_get_remotes(&nremotes, &remotes,
7828 for (i = 0; i < nremotes; i++) {
7829 if (strcmp(remotes[i].name, remote_name) == 0) {
7830 remote = &remotes[i];
7836 if (remote == NULL) {
7837 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7838 for (i = 0; i < nremotes; i++) {
7839 if (strcmp(remotes[i].name, remote_name) == 0) {
7840 remote = &remotes[i];
7845 if (remote == NULL) {
7846 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7850 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7851 &repo_name, remote->send_url);
7855 if (strcmp(proto, "git") == 0) {
7857 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7858 "sendfd dns inet unveil", NULL) == -1)
7861 } else if (strcmp(proto, "git+ssh") == 0 ||
7862 strcmp(proto, "ssh") == 0) {
7864 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7865 "sendfd unveil", NULL) == -1)
7868 } else if (strcmp(proto, "http") == 0 ||
7869 strcmp(proto, "git+http") == 0) {
7870 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7873 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7877 error = got_dial_apply_unveil(proto);
7881 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7885 if (send_all_branches) {
7886 error = got_ref_list(&all_branches, repo, "refs/heads",
7887 got_ref_cmp_by_name, NULL);
7890 TAILQ_FOREACH(re, &all_branches, entry) {
7891 const char *branchname = got_ref_get_name(re->ref);
7892 error = got_pathlist_append(&branches,
7898 } else if (nbranches == 0) {
7899 for (i = 0; i < remote->nsend_branches; i++) {
7900 got_pathlist_append(&branches,
7901 remote->send_branches[i], NULL);
7905 if (send_all_tags) {
7906 error = got_ref_list(&all_tags, repo, "refs/tags",
7907 got_ref_cmp_by_name, NULL);
7910 TAILQ_FOREACH(re, &all_tags, entry) {
7911 const char *tagname = got_ref_get_name(re->ref);
7912 error = got_pathlist_append(&tags,
7921 * To prevent accidents only branches in refs/heads/ can be deleted
7922 * with 'got send -d'.
7923 * Deleting anything else requires local repository access or Git.
7925 TAILQ_FOREACH(pe, &delete_args, entry) {
7926 const char *branchname = pe->path;
7928 struct got_pathlist_entry *new;
7929 if (strncmp(branchname, "refs/heads/", 11) == 0) {
7930 s = strdup(branchname);
7932 error = got_error_from_errno("strdup");
7936 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7937 error = got_error_from_errno("asprintf");
7941 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7942 if (error || new == NULL /* duplicate */)
7949 if (nbranches == 0 && ndelete_branches == 0) {
7950 struct got_reference *head_ref;
7952 error = got_ref_open(&head_ref, repo,
7953 got_worktree_get_head_ref_name(worktree), 0);
7955 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7958 if (got_ref_is_symbolic(head_ref)) {
7959 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7960 got_ref_close(head_ref);
7965 error = got_pathlist_append(&branches, got_ref_get_name(ref),
7973 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7974 port ? ":" : "", port ? port : "");
7976 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7977 server_path, verbosity);
7981 memset(&spa, 0, sizeof(spa));
7982 spa.last_scaled_packsize[0] = '\0';
7983 spa.last_p_deltify = -1;
7984 spa.last_p_written = -1;
7985 spa.verbosity = verbosity;
7986 spa.delete_branches = &delete_branches;
7987 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7988 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7989 check_cancelled, NULL);
7990 if (spa.printed_something)
7994 if (!spa.sent_something && verbosity >= 0)
7995 printf("Already up-to-date\n");
7998 if (kill(sendpid, SIGTERM) == -1)
7999 error = got_error_from_errno("kill");
8000 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8001 error = got_error_from_errno("waitpid");
8003 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8004 error = got_error_from_errno("close");
8006 const struct got_error *close_err = got_repo_close(repo);
8011 got_worktree_close(worktree);
8014 got_pathlist_free(&branches);
8015 got_pathlist_free(&tags);
8016 got_ref_list_free(&all_branches);
8017 got_ref_list_free(&all_tags);
8018 got_pathlist_free(&delete_args);
8019 TAILQ_FOREACH(pe, &delete_branches, entry)
8020 free((char *)pe->path);
8021 got_pathlist_free(&delete_branches);
8033 usage_cherrypick(void)
8035 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8039 static const struct got_error *
8040 cmd_cherrypick(int argc, char *argv[])
8042 const struct got_error *error = NULL;
8043 struct got_worktree *worktree = NULL;
8044 struct got_repository *repo = NULL;
8045 char *cwd = NULL, *commit_id_str = NULL;
8046 struct got_object_id *commit_id = NULL;
8047 struct got_commit_object *commit = NULL;
8048 struct got_object_qid *pid;
8050 struct got_update_progress_arg upa;
8052 while ((ch = getopt(argc, argv, "")) != -1) {
8064 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8065 "unveil", NULL) == -1)
8071 cwd = getcwd(NULL, 0);
8073 error = got_error_from_errno("getcwd");
8076 error = got_worktree_open(&worktree, cwd);
8078 if (error->code == GOT_ERR_NOT_WORKTREE)
8079 error = wrap_not_worktree_error(error, "cherrypick",
8084 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8089 error = apply_unveil(got_repo_get_path(repo), 0,
8090 got_worktree_get_root_path(worktree));
8094 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8095 GOT_OBJ_TYPE_COMMIT, repo);
8096 if (error != NULL) {
8097 struct got_reference *ref;
8098 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8100 error = got_ref_open(&ref, repo, argv[0], 0);
8103 error = got_ref_resolve(&commit_id, repo, ref);
8108 error = got_object_id_str(&commit_id_str, commit_id);
8112 error = got_object_open_as_commit(&commit, repo, commit_id);
8115 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8116 memset(&upa, 0, sizeof(upa));
8117 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8118 commit_id, repo, update_progress, &upa, check_cancelled,
8123 if (upa.did_something)
8124 printf("Merged commit %s\n", commit_id_str);
8125 print_merge_progress_stats(&upa);
8128 got_object_commit_close(commit);
8129 free(commit_id_str);
8131 got_worktree_close(worktree);
8133 const struct got_error *close_err = got_repo_close(repo);
8143 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8147 static const struct got_error *
8148 cmd_backout(int argc, char *argv[])
8150 const struct got_error *error = NULL;
8151 struct got_worktree *worktree = NULL;
8152 struct got_repository *repo = NULL;
8153 char *cwd = NULL, *commit_id_str = NULL;
8154 struct got_object_id *commit_id = NULL;
8155 struct got_commit_object *commit = NULL;
8156 struct got_object_qid *pid;
8158 struct got_update_progress_arg upa;
8160 while ((ch = getopt(argc, argv, "")) != -1) {
8172 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8173 "unveil", NULL) == -1)
8179 cwd = getcwd(NULL, 0);
8181 error = got_error_from_errno("getcwd");
8184 error = got_worktree_open(&worktree, cwd);
8186 if (error->code == GOT_ERR_NOT_WORKTREE)
8187 error = wrap_not_worktree_error(error, "backout", cwd);
8191 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8196 error = apply_unveil(got_repo_get_path(repo), 0,
8197 got_worktree_get_root_path(worktree));
8201 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8202 GOT_OBJ_TYPE_COMMIT, repo);
8203 if (error != NULL) {
8204 struct got_reference *ref;
8205 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8207 error = got_ref_open(&ref, repo, argv[0], 0);
8210 error = got_ref_resolve(&commit_id, repo, ref);
8215 error = got_object_id_str(&commit_id_str, commit_id);
8219 error = got_object_open_as_commit(&commit, repo, commit_id);
8222 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8224 error = got_error(GOT_ERR_ROOT_COMMIT);
8228 memset(&upa, 0, sizeof(upa));
8229 error = got_worktree_merge_files(worktree, commit_id, pid->id,
8230 repo, update_progress, &upa, check_cancelled, NULL);
8234 if (upa.did_something)
8235 printf("Backed out commit %s\n", commit_id_str);
8236 print_merge_progress_stats(&upa);
8239 got_object_commit_close(commit);
8240 free(commit_id_str);
8242 got_worktree_close(worktree);
8244 const struct got_error *close_err = got_repo_close(repo);
8254 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8260 trim_logmsg(char *logmsg, int limit)
8265 len = strlen(logmsg);
8269 nl = strchr(logmsg, '\n');
8274 static const struct got_error *
8275 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8277 const struct got_error *err;
8278 char *logmsg0 = NULL;
8281 err = got_object_commit_get_logmsg(&logmsg0, commit);
8286 while (isspace((unsigned char)s[0]))
8289 *logmsg = strdup(s);
8290 if (*logmsg == NULL) {
8291 err = got_error_from_errno("strdup");
8295 trim_logmsg(*logmsg, limit);
8301 static const struct got_error *
8302 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8304 const struct got_error *err;
8305 struct got_commit_object *commit = NULL;
8306 char *id_str = NULL, *logmsg = NULL;
8308 err = got_object_open_as_commit(&commit, repo, id);
8312 err = got_object_id_str(&id_str, id);
8318 err = get_short_logmsg(&logmsg, 42, commit);
8322 printf("%s -> merge conflict: %s\n", id_str, logmsg);
8325 got_object_commit_close(commit);
8330 static const struct got_error *
8331 show_rebase_progress(struct got_commit_object *commit,
8332 struct got_object_id *old_id, struct got_object_id *new_id)
8334 const struct got_error *err;
8335 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8337 err = got_object_id_str(&old_id_str, old_id);
8342 err = got_object_id_str(&new_id_str, new_id);
8347 old_id_str[12] = '\0';
8349 new_id_str[12] = '\0';
8351 err = get_short_logmsg(&logmsg, 42, commit);
8355 printf("%s -> %s: %s\n", old_id_str,
8356 new_id_str ? new_id_str : "no-op change", logmsg);
8364 static const struct got_error *
8365 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8366 struct got_reference *branch, struct got_reference *new_base_branch,
8367 struct got_reference *tmp_branch, struct got_repository *repo,
8370 printf("Switching work tree to %s\n", got_ref_get_name(branch));
8371 return got_worktree_rebase_complete(worktree, fileindex,
8372 new_base_branch, tmp_branch, branch, repo, create_backup);
8375 static const struct got_error *
8376 rebase_commit(struct got_pathlist_head *merged_paths,
8377 struct got_worktree *worktree, struct got_fileindex *fileindex,
8378 struct got_reference *tmp_branch,
8379 struct got_object_id *commit_id, struct got_repository *repo)
8381 const struct got_error *error;
8382 struct got_commit_object *commit;
8383 struct got_object_id *new_commit_id;
8385 error = got_object_open_as_commit(&commit, repo, commit_id);
8389 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8390 worktree, fileindex, tmp_branch, commit, commit_id, repo);
8392 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8394 error = show_rebase_progress(commit, commit_id, NULL);
8396 error = show_rebase_progress(commit, commit_id, new_commit_id);
8397 free(new_commit_id);
8400 got_object_commit_close(commit);
8404 struct check_path_prefix_arg {
8405 const char *path_prefix;
8410 static const struct got_error *
8411 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8412 struct got_blob_object *blob2, struct got_object_id *id1,
8413 struct got_object_id *id2, const char *path1, const char *path2,
8414 mode_t mode1, mode_t mode2, struct got_repository *repo)
8416 struct check_path_prefix_arg *a = arg;
8418 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8419 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8420 return got_error(a->errcode);
8425 static const struct got_error *
8426 check_path_prefix(struct got_object_id *parent_id,
8427 struct got_object_id *commit_id, const char *path_prefix,
8428 int errcode, struct got_repository *repo)
8430 const struct got_error *err;
8431 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8432 struct got_commit_object *commit = NULL, *parent_commit = NULL;
8433 struct check_path_prefix_arg cpp_arg;
8435 if (got_path_is_root_dir(path_prefix))
8438 err = got_object_open_as_commit(&commit, repo, commit_id);
8442 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8446 err = got_object_open_as_tree(&tree1, repo,
8447 got_object_commit_get_tree_id(parent_commit));
8451 err = got_object_open_as_tree(&tree2, repo,
8452 got_object_commit_get_tree_id(commit));
8456 cpp_arg.path_prefix = path_prefix;
8457 while (cpp_arg.path_prefix[0] == '/')
8458 cpp_arg.path_prefix++;
8459 cpp_arg.len = strlen(cpp_arg.path_prefix);
8460 cpp_arg.errcode = errcode;
8461 err = got_diff_tree(tree1, tree2, "", "", repo,
8462 check_path_prefix_in_diff, &cpp_arg, 0);
8465 got_object_tree_close(tree1);
8467 got_object_tree_close(tree2);
8469 got_object_commit_close(commit);
8471 got_object_commit_close(parent_commit);
8475 static const struct got_error *
8476 collect_commits(struct got_object_id_queue *commits,
8477 struct got_object_id *initial_commit_id,
8478 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8479 const char *path_prefix, int path_prefix_errcode,
8480 struct got_repository *repo)
8482 const struct got_error *err = NULL;
8483 struct got_commit_graph *graph = NULL;
8484 struct got_object_id *parent_id = NULL;
8485 struct got_object_qid *qid;
8486 struct got_object_id *commit_id = initial_commit_id;
8488 err = got_commit_graph_open(&graph, "/", 1);
8492 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8493 check_cancelled, NULL);
8496 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8497 err = got_commit_graph_iter_next(&parent_id, graph, repo,
8498 check_cancelled, NULL);
8500 if (err->code == GOT_ERR_ITER_COMPLETED) {
8501 err = got_error_msg(GOT_ERR_ANCESTRY,
8502 "ran out of commits to rebase before "
8503 "youngest common ancestor commit has "
8508 err = check_path_prefix(parent_id, commit_id,
8509 path_prefix, path_prefix_errcode, repo);
8513 err = got_object_qid_alloc(&qid, commit_id);
8516 STAILQ_INSERT_HEAD(commits, qid, entry);
8517 commit_id = parent_id;
8521 got_commit_graph_close(graph);
8525 static const struct got_error *
8526 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8528 const struct got_error *err = NULL;
8529 time_t committer_time;
8531 char datebuf[11]; /* YYYY-MM-DD + NUL */
8532 char *author0 = NULL, *author, *smallerthan;
8533 char *logmsg0 = NULL, *logmsg, *newline;
8535 committer_time = got_object_commit_get_committer_time(commit);
8536 if (gmtime_r(&committer_time, &tm) == NULL)
8537 return got_error_from_errno("gmtime_r");
8538 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8539 return got_error(GOT_ERR_NO_SPACE);
8541 author0 = strdup(got_object_commit_get_author(commit));
8542 if (author0 == NULL)
8543 return got_error_from_errno("strdup");
8545 smallerthan = strchr(author, '<');
8546 if (smallerthan && smallerthan[1] != '\0')
8547 author = smallerthan + 1;
8548 author[strcspn(author, "@>")] = '\0';
8550 err = got_object_commit_get_logmsg(&logmsg0, commit);
8554 while (*logmsg == '\n')
8556 newline = strchr(logmsg, '\n');
8560 if (asprintf(brief_str, "%s %s %s",
8561 datebuf, author, logmsg) == -1)
8562 err = got_error_from_errno("asprintf");
8569 static const struct got_error *
8570 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8571 struct got_repository *repo)
8573 const struct got_error *err;
8576 err = got_object_id_str(&id_str, id);
8580 err = got_ref_delete(ref, repo);
8584 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8590 static const struct got_error *
8591 print_backup_ref(const char *branch_name, const char *new_id_str,
8592 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8593 struct got_reflist_object_id_map *refs_idmap,
8594 struct got_repository *repo)
8596 const struct got_error *err = NULL;
8597 struct got_reflist_head *refs;
8598 char *refs_str = NULL;
8599 struct got_object_id *new_commit_id = NULL;
8600 struct got_commit_object *new_commit = NULL;
8601 char *new_commit_brief_str = NULL;
8602 struct got_object_id *yca_id = NULL;
8603 struct got_commit_object *yca_commit = NULL;
8604 char *yca_id_str = NULL, *yca_brief_str = NULL;
8605 char *custom_refs_str;
8607 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8608 return got_error_from_errno("asprintf");
8610 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8611 0, 0, refs_idmap, custom_refs_str);
8615 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8619 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8621 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8626 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8630 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8634 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8635 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8639 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8640 refs_str ? " (" : "", refs_str ? refs_str : "",
8641 refs_str ? ")" : "", new_commit_brief_str);
8642 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8643 got_object_id_cmp(yca_id, old_commit_id) != 0) {
8647 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8651 err = get_commit_brief_str(&yca_brief_str, yca_commit);
8655 err = got_object_id_str(&yca_id_str, yca_id);
8659 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8661 err = build_refs_str(&refs_str, refs, yca_id, repo);
8665 printf("history forked at %s%s%s%s\n %s\n",
8667 refs_str ? " (" : "", refs_str ? refs_str : "",
8668 refs_str ? ")" : "", yca_brief_str);
8671 free(custom_refs_str);
8672 free(new_commit_id);
8676 free(yca_brief_str);
8678 got_object_commit_close(new_commit);
8680 got_object_commit_close(yca_commit);
8685 static const struct got_error *
8686 process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8687 int delete, struct got_repository *repo)
8689 const struct got_error *err;
8690 struct got_reflist_head refs, backup_refs;
8691 struct got_reflist_entry *re;
8692 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8693 struct got_object_id *old_commit_id = NULL;
8694 char *branch_name = NULL;
8695 struct got_commit_object *old_commit = NULL;
8696 struct got_reflist_object_id_map *refs_idmap = NULL;
8697 int wanted_branch_found = 0;
8700 TAILQ_INIT(&backup_refs);
8702 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8706 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8710 if (wanted_branch_name) {
8711 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8712 wanted_branch_name += 11;
8715 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8716 got_ref_cmp_by_commit_timestamp_descending, repo);
8720 TAILQ_FOREACH(re, &backup_refs, entry) {
8721 const char *refname = got_ref_get_name(re->ref);
8724 err = check_cancelled(NULL);
8728 err = got_ref_resolve(&old_commit_id, repo, re->ref);
8732 err = got_object_open_as_commit(&old_commit, repo,
8737 if (strncmp(backup_ref_prefix, refname,
8738 backup_ref_prefix_len) == 0)
8739 refname += backup_ref_prefix_len;
8741 while (refname[0] == '/')
8744 branch_name = strdup(refname);
8745 if (branch_name == NULL) {
8746 err = got_error_from_errno("strdup");
8749 slash = strrchr(branch_name, '/');
8752 refname += strlen(branch_name) + 1;
8755 if (wanted_branch_name == NULL ||
8756 strcmp(wanted_branch_name, branch_name) == 0) {
8757 wanted_branch_found = 1;
8759 err = delete_backup_ref(re->ref,
8760 old_commit_id, repo);
8762 err = print_backup_ref(branch_name, refname,
8763 old_commit_id, old_commit, refs_idmap,
8770 free(old_commit_id);
8771 old_commit_id = NULL;
8774 got_object_commit_close(old_commit);
8778 if (wanted_branch_name && !wanted_branch_found) {
8779 err = got_error_fmt(GOT_ERR_NOT_REF,
8780 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8784 got_reflist_object_id_map_free(refs_idmap);
8785 got_ref_list_free(&refs);
8786 got_ref_list_free(&backup_refs);
8787 free(old_commit_id);
8790 got_object_commit_close(old_commit);
8794 static const struct got_error *
8795 cmd_rebase(int argc, char *argv[])
8797 const struct got_error *error = NULL;
8798 struct got_worktree *worktree = NULL;
8799 struct got_repository *repo = NULL;
8800 struct got_fileindex *fileindex = NULL;
8802 struct got_reference *branch = NULL;
8803 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8804 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8805 struct got_object_id *resume_commit_id = NULL;
8806 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8807 struct got_commit_object *commit = NULL;
8808 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8809 int histedit_in_progress = 0, merge_in_progress = 0;
8810 int create_backup = 1, list_backups = 0, delete_backups = 0;
8811 struct got_object_id_queue commits;
8812 struct got_pathlist_head merged_paths;
8813 const struct got_object_id_queue *parent_ids;
8814 struct got_object_qid *qid, *pid;
8815 struct got_update_progress_arg upa;
8817 STAILQ_INIT(&commits);
8818 TAILQ_INIT(&merged_paths);
8819 memset(&upa, 0, sizeof(upa));
8821 while ((ch = getopt(argc, argv, "aclX")) != -1) {
8827 continue_rebase = 1;
8845 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8846 "unveil", NULL) == -1)
8851 option_conflict('l', 'a');
8852 if (continue_rebase)
8853 option_conflict('l', 'c');
8855 option_conflict('l', 'X');
8856 if (argc != 0 && argc != 1)
8858 } else if (delete_backups) {
8860 option_conflict('X', 'a');
8861 if (continue_rebase)
8862 option_conflict('X', 'c');
8864 option_conflict('l', 'X');
8865 if (argc != 0 && argc != 1)
8868 if (abort_rebase && continue_rebase)
8870 else if (abort_rebase || continue_rebase) {
8873 } else if (argc != 1)
8877 cwd = getcwd(NULL, 0);
8879 error = got_error_from_errno("getcwd");
8882 error = got_worktree_open(&worktree, cwd);
8884 if (list_backups || delete_backups) {
8885 if (error->code != GOT_ERR_NOT_WORKTREE)
8888 if (error->code == GOT_ERR_NOT_WORKTREE)
8889 error = wrap_not_worktree_error(error,
8895 error = got_repo_open(&repo,
8896 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8900 error = apply_unveil(got_repo_get_path(repo), 0,
8901 worktree ? got_worktree_get_root_path(worktree) : NULL);
8905 if (list_backups || delete_backups) {
8906 error = process_backup_refs(
8907 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8908 argc == 1 ? argv[0] : NULL, delete_backups, repo);
8909 goto done; /* nothing else to do */
8912 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8916 if (histedit_in_progress) {
8917 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8921 error = got_worktree_merge_in_progress(&merge_in_progress,
8925 if (merge_in_progress) {
8926 error = got_error(GOT_ERR_MERGE_BUSY);
8930 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8935 if (!rebase_in_progress) {
8936 error = got_error(GOT_ERR_NOT_REBASING);
8939 error = got_worktree_rebase_continue(&resume_commit_id,
8940 &new_base_branch, &tmp_branch, &branch, &fileindex,
8944 printf("Switching work tree to %s\n",
8945 got_ref_get_symref_target(new_base_branch));
8946 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8947 new_base_branch, update_progress, &upa);
8950 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8951 print_merge_progress_stats(&upa);
8952 goto done; /* nothing else to do */
8955 if (continue_rebase) {
8956 if (!rebase_in_progress) {
8957 error = got_error(GOT_ERR_NOT_REBASING);
8960 error = got_worktree_rebase_continue(&resume_commit_id,
8961 &new_base_branch, &tmp_branch, &branch, &fileindex,
8966 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8967 resume_commit_id, repo);
8971 yca_id = got_object_id_dup(resume_commit_id);
8972 if (yca_id == NULL) {
8973 error = got_error_from_errno("got_object_id_dup");
8977 error = got_ref_open(&branch, repo, argv[0], 0);
8982 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8986 if (!continue_rebase) {
8987 struct got_object_id *base_commit_id;
8989 base_commit_id = got_worktree_get_base_commit_id(worktree);
8990 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8991 base_commit_id, branch_head_commit_id, 1, repo,
8992 check_cancelled, NULL);
8995 if (yca_id == NULL) {
8996 error = got_error_msg(GOT_ERR_ANCESTRY,
8997 "specified branch shares no common ancestry "
8998 "with work tree's branch");
9002 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9004 if (error->code != GOT_ERR_ANCESTRY)
9008 static char msg[128];
9009 snprintf(msg, sizeof(msg),
9010 "%s is already based on %s",
9011 got_ref_get_name(branch),
9012 got_worktree_get_head_ref_name(worktree));
9013 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
9016 error = got_worktree_rebase_prepare(&new_base_branch,
9017 &tmp_branch, &fileindex, worktree, branch, repo);
9022 commit_id = branch_head_commit_id;
9023 error = got_object_open_as_commit(&commit, repo, commit_id);
9027 parent_ids = got_object_commit_get_parent_ids(commit);
9028 pid = STAILQ_FIRST(parent_ids);
9030 if (!continue_rebase) {
9031 error = got_worktree_rebase_abort(worktree, fileindex,
9032 repo, new_base_branch, update_progress, &upa);
9035 printf("Rebase of %s aborted\n",
9036 got_ref_get_name(branch));
9037 print_merge_progress_stats(&upa);
9040 error = got_error(GOT_ERR_EMPTY_REBASE);
9043 error = collect_commits(&commits, commit_id, pid->id,
9044 yca_id, got_worktree_get_path_prefix(worktree),
9045 GOT_ERR_REBASE_PATH, repo);
9046 got_object_commit_close(commit);
9051 if (STAILQ_EMPTY(&commits)) {
9052 if (continue_rebase) {
9053 error = rebase_complete(worktree, fileindex,
9054 branch, new_base_branch, tmp_branch, repo,
9058 /* Fast-forward the reference of the branch. */
9059 struct got_object_id *new_head_commit_id;
9061 error = got_ref_resolve(&new_head_commit_id, repo,
9065 error = got_object_id_str(&id_str, new_head_commit_id);
9066 printf("Forwarding %s to commit %s\n",
9067 got_ref_get_name(branch), id_str);
9069 error = got_ref_change_ref(branch,
9070 new_head_commit_id);
9073 /* No backup needed since objects did not change. */
9079 STAILQ_FOREACH(qid, &commits, entry) {
9081 commit_id = qid->id;
9082 parent_id = pid ? pid->id : yca_id;
9085 memset(&upa, 0, sizeof(upa));
9086 error = got_worktree_rebase_merge_files(&merged_paths,
9087 worktree, fileindex, parent_id, commit_id, repo,
9088 update_progress, &upa, check_cancelled, NULL);
9092 print_merge_progress_stats(&upa);
9093 if (upa.conflicts > 0 || upa.missing > 0 ||
9094 upa.not_deleted > 0 || upa.unversioned > 0) {
9095 if (upa.conflicts > 0) {
9096 error = show_rebase_merge_conflict(qid->id,
9101 got_worktree_rebase_pathlist_free(&merged_paths);
9105 error = rebase_commit(&merged_paths, worktree, fileindex,
9106 tmp_branch, commit_id, repo);
9107 got_worktree_rebase_pathlist_free(&merged_paths);
9112 if (upa.conflicts > 0 || upa.missing > 0 ||
9113 upa.not_deleted > 0 || upa.unversioned > 0) {
9114 error = got_worktree_rebase_postpone(worktree, fileindex);
9117 if (upa.conflicts > 0 && upa.missing == 0 &&
9118 upa.not_deleted == 0 && upa.unversioned == 0) {
9119 error = got_error_msg(GOT_ERR_CONFLICTS,
9120 "conflicts must be resolved before rebasing "
9122 } else if (upa.conflicts > 0) {
9123 error = got_error_msg(GOT_ERR_CONFLICTS,
9124 "conflicts must be resolved before rebasing "
9125 "can continue; changes destined for some "
9126 "files were not yet merged and should be "
9127 "merged manually if required before the "
9128 "rebase operation is continued");
9130 error = got_error_msg(GOT_ERR_CONFLICTS,
9131 "changes destined for some files were not "
9132 "yet merged and should be merged manually "
9133 "if required before the rebase operation "
9137 error = rebase_complete(worktree, fileindex, branch,
9138 new_base_branch, tmp_branch, repo, create_backup);
9140 got_object_id_queue_free(&commits);
9141 free(branch_head_commit_id);
9142 free(resume_commit_id);
9145 got_object_commit_close(commit);
9147 got_ref_close(branch);
9148 if (new_base_branch)
9149 got_ref_close(new_base_branch);
9151 got_ref_close(tmp_branch);
9153 got_worktree_close(worktree);
9155 const struct got_error *close_err = got_repo_close(repo);
9163 usage_histedit(void)
9165 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9166 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9171 #define GOT_HISTEDIT_PICK 'p'
9172 #define GOT_HISTEDIT_EDIT 'e'
9173 #define GOT_HISTEDIT_FOLD 'f'
9174 #define GOT_HISTEDIT_DROP 'd'
9175 #define GOT_HISTEDIT_MESG 'm'
9177 static struct got_histedit_cmd {
9181 } got_histedit_cmds[] = {
9182 { GOT_HISTEDIT_PICK, "pick", "use commit" },
9183 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9184 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9186 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9187 { GOT_HISTEDIT_MESG, "mesg",
9188 "single-line log message for commit above (open editor if empty)" },
9191 struct got_histedit_list_entry {
9192 TAILQ_ENTRY(got_histedit_list_entry) entry;
9193 struct got_object_id *commit_id;
9194 const struct got_histedit_cmd *cmd;
9197 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9199 static const struct got_error *
9200 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9201 FILE *f, struct got_repository *repo)
9203 const struct got_error *err = NULL;
9204 char *logmsg = NULL, *id_str = NULL;
9205 struct got_commit_object *commit = NULL;
9208 err = got_object_open_as_commit(&commit, repo, commit_id);
9212 err = get_short_logmsg(&logmsg, 34, commit);
9216 err = got_object_id_str(&id_str, commit_id);
9220 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9222 err = got_ferror(f, GOT_ERR_IO);
9225 got_object_commit_close(commit);
9231 static const struct got_error *
9232 histedit_write_commit_list(struct got_object_id_queue *commits,
9233 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9235 const struct got_error *err = NULL;
9236 struct got_object_qid *qid;
9237 const char *histedit_cmd = NULL;
9239 if (STAILQ_EMPTY(commits))
9240 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9242 STAILQ_FOREACH(qid, commits, entry) {
9243 histedit_cmd = got_histedit_cmds[0].name;
9244 if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9245 histedit_cmd = "fold";
9246 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9249 if (edit_logmsg_only) {
9250 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9252 err = got_ferror(f, GOT_ERR_IO);
9261 static const struct got_error *
9262 write_cmd_list(FILE *f, const char *branch_name,
9263 struct got_object_id_queue *commits)
9265 const struct got_error *err = NULL;
9269 struct got_object_qid *qid;
9271 qid = STAILQ_FIRST(commits);
9272 err = got_object_id_str(&id_str, qid->id);
9277 "# Editing the history of branch '%s' starting at\n"
9279 "# Commits will be processed in order from top to "
9280 "bottom of this file.\n", branch_name, id_str);
9282 err = got_ferror(f, GOT_ERR_IO);
9286 n = fprintf(f, "# Available histedit commands:\n");
9288 err = got_ferror(f, GOT_ERR_IO);
9292 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9293 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9294 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9297 err = got_ferror(f, GOT_ERR_IO);
9306 static const struct got_error *
9307 histedit_syntax_error(int lineno)
9309 static char msg[42];
9312 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9314 if (ret == -1 || ret >= sizeof(msg))
9315 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9317 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9320 static const struct got_error *
9321 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9322 char *logmsg, struct got_repository *repo)
9324 const struct got_error *err;
9325 struct got_commit_object *folded_commit = NULL;
9326 char *id_str, *folded_logmsg = NULL;
9328 err = got_object_id_str(&id_str, hle->commit_id);
9332 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9336 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9339 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9340 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9341 folded_logmsg) == -1) {
9342 err = got_error_from_errno("asprintf");
9346 got_object_commit_close(folded_commit);
9348 free(folded_logmsg);
9352 static struct got_histedit_list_entry *
9353 get_folded_commits(struct got_histedit_list_entry *hle)
9355 struct got_histedit_list_entry *prev, *folded = NULL;
9357 prev = TAILQ_PREV(hle, got_histedit_list, entry);
9358 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9359 prev->cmd->code == GOT_HISTEDIT_DROP)) {
9360 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9362 prev = TAILQ_PREV(prev, got_histedit_list, entry);
9368 static const struct got_error *
9369 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9370 struct got_repository *repo)
9372 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9373 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9374 const struct got_error *err = NULL;
9375 struct got_commit_object *commit = NULL;
9378 struct got_histedit_list_entry *folded = NULL;
9380 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9384 folded = get_folded_commits(hle);
9386 while (folded != hle) {
9387 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9388 folded = TAILQ_NEXT(folded, entry);
9391 err = append_folded_commit_msg(&new_msg, folded,
9397 folded = TAILQ_NEXT(folded, entry);
9401 err = got_object_id_str(&id_str, hle->commit_id);
9404 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9407 logmsg_len = asprintf(&new_msg,
9408 "%s\n# original log message of commit %s: %s",
9409 logmsg ? logmsg : "", id_str, orig_logmsg);
9410 if (logmsg_len == -1) {
9411 err = got_error_from_errno("asprintf");
9417 err = got_object_id_str(&id_str, hle->commit_id);
9421 err = got_opentemp_named_fd(&logmsg_path, &fd,
9422 GOT_TMPDIR_STR "/got-logmsg");
9426 write(fd, logmsg, logmsg_len);
9429 err = get_editor(&editor);
9433 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9436 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9439 hle->logmsg = strdup(new_msg);
9440 if (hle->logmsg == NULL)
9441 err = got_error_from_errno("strdup");
9444 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9445 err = got_error_from_errno2("unlink", logmsg_path);
9451 got_object_commit_close(commit);
9455 static const struct got_error *
9456 histedit_parse_list(struct got_histedit_list *histedit_cmds,
9457 FILE *f, struct got_repository *repo)
9459 const struct got_error *err = NULL;
9460 char *line = NULL, *p, *end;
9464 const struct got_histedit_cmd *cmd;
9465 struct got_object_id *commit_id = NULL;
9466 struct got_histedit_list_entry *hle = NULL;
9469 len = getline(&line, &size, f);
9471 const struct got_error *getline_err;
9474 getline_err = got_error_from_errno("getline");
9475 err = got_ferror(f, getline_err->code);
9480 while (isspace((unsigned char)p[0]))
9482 if (p[0] == '#' || p[0] == '\0') {
9488 for (i = 0; i < nitems(got_histedit_cmds); i++) {
9489 cmd = &got_histedit_cmds[i];
9490 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9491 isspace((unsigned char)p[strlen(cmd->name)])) {
9492 p += strlen(cmd->name);
9495 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9500 if (i == nitems(got_histedit_cmds)) {
9501 err = histedit_syntax_error(lineno);
9504 while (isspace((unsigned char)p[0]))
9506 if (cmd->code == GOT_HISTEDIT_MESG) {
9507 if (hle == NULL || hle->logmsg != NULL) {
9508 err = got_error(GOT_ERR_HISTEDIT_CMD);
9512 err = histedit_edit_logmsg(hle, repo);
9516 hle->logmsg = strdup(p);
9517 if (hle->logmsg == NULL) {
9518 err = got_error_from_errno("strdup");
9527 while (end[0] && !isspace((unsigned char)end[0]))
9531 err = got_object_resolve_id_str(&commit_id, repo, p);
9533 /* override error code */
9534 err = histedit_syntax_error(lineno);
9538 hle = malloc(sizeof(*hle));
9540 err = got_error_from_errno("malloc");
9544 hle->commit_id = commit_id;
9549 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9557 static const struct got_error *
9558 histedit_check_script(struct got_histedit_list *histedit_cmds,
9559 struct got_object_id_queue *commits, struct got_repository *repo)
9561 const struct got_error *err = NULL;
9562 struct got_object_qid *qid;
9563 struct got_histedit_list_entry *hle;
9564 static char msg[92];
9567 if (TAILQ_EMPTY(histedit_cmds))
9568 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9569 "histedit script contains no commands");
9570 if (STAILQ_EMPTY(commits))
9571 return got_error(GOT_ERR_EMPTY_HISTEDIT);
9573 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9574 struct got_histedit_list_entry *hle2;
9575 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9578 if (got_object_id_cmp(hle->commit_id,
9579 hle2->commit_id) != 0)
9581 err = got_object_id_str(&id_str, hle->commit_id);
9584 snprintf(msg, sizeof(msg), "commit %s is listed "
9585 "more than once in histedit script", id_str);
9587 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9591 STAILQ_FOREACH(qid, commits, entry) {
9592 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9593 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9597 err = got_object_id_str(&id_str, qid->id);
9600 snprintf(msg, sizeof(msg),
9601 "commit %s missing from histedit script", id_str);
9603 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9607 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9608 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9609 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9610 "last commit in histedit script cannot be folded");
9615 static const struct got_error *
9616 histedit_run_editor(struct got_histedit_list *histedit_cmds,
9617 const char *path, struct got_object_id_queue *commits,
9618 struct got_repository *repo)
9620 const struct got_error *err = NULL;
9624 err = get_editor(&editor);
9628 if (spawn_editor(editor, path) == -1) {
9629 err = got_error_from_errno("failed spawning editor");
9633 f = fopen(path, "r");
9635 err = got_error_from_errno("fopen");
9638 err = histedit_parse_list(histedit_cmds, f, repo);
9642 err = histedit_check_script(histedit_cmds, commits, repo);
9644 if (f && fclose(f) == EOF && err == NULL)
9645 err = got_error_from_errno("fclose");
9650 static const struct got_error *
9651 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9652 struct got_object_id_queue *, const char *, const char *,
9653 struct got_repository *);
9655 static const struct got_error *
9656 histedit_edit_script(struct got_histedit_list *histedit_cmds,
9657 struct got_object_id_queue *commits, const char *branch_name,
9658 int edit_logmsg_only, int fold_only, struct got_repository *repo)
9660 const struct got_error *err;
9664 err = got_opentemp_named(&path, &f, "got-histedit");
9668 err = write_cmd_list(f, branch_name, commits);
9672 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9677 if (edit_logmsg_only || fold_only) {
9679 err = histedit_parse_list(histedit_cmds, f, repo);
9681 if (fclose(f) == EOF) {
9682 err = got_error_from_errno("fclose");
9686 err = histedit_run_editor(histedit_cmds, path, commits, repo);
9688 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9689 err->code != GOT_ERR_HISTEDIT_CMD)
9691 err = histedit_edit_list_retry(histedit_cmds, err,
9692 commits, path, branch_name, repo);
9696 if (f && fclose(f) == EOF && err == NULL)
9697 err = got_error_from_errno("fclose");
9698 if (path && unlink(path) != 0 && err == NULL)
9699 err = got_error_from_errno2("unlink", path);
9704 static const struct got_error *
9705 histedit_save_list(struct got_histedit_list *histedit_cmds,
9706 struct got_worktree *worktree, struct got_repository *repo)
9708 const struct got_error *err = NULL;
9711 struct got_histedit_list_entry *hle;
9712 struct got_commit_object *commit = NULL;
9714 err = got_worktree_get_histedit_script_path(&path, worktree);
9718 f = fopen(path, "w");
9720 err = got_error_from_errno2("fopen", path);
9723 TAILQ_FOREACH(hle, histedit_cmds, entry) {
9724 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9730 int n = fprintf(f, "%c %s\n",
9731 GOT_HISTEDIT_MESG, hle->logmsg);
9733 err = got_ferror(f, GOT_ERR_IO);
9739 if (f && fclose(f) == EOF && err == NULL)
9740 err = got_error_from_errno("fclose");
9743 got_object_commit_close(commit);
9748 histedit_free_list(struct got_histedit_list *histedit_cmds)
9750 struct got_histedit_list_entry *hle;
9752 while ((hle = TAILQ_FIRST(histedit_cmds))) {
9753 TAILQ_REMOVE(histedit_cmds, hle, entry);
9758 static const struct got_error *
9759 histedit_load_list(struct got_histedit_list *histedit_cmds,
9760 const char *path, struct got_repository *repo)
9762 const struct got_error *err = NULL;
9765 f = fopen(path, "r");
9767 err = got_error_from_errno2("fopen", path);
9771 err = histedit_parse_list(histedit_cmds, f, repo);
9773 if (f && fclose(f) == EOF && err == NULL)
9774 err = got_error_from_errno("fclose");
9778 static const struct got_error *
9779 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9780 const struct got_error *edit_err, struct got_object_id_queue *commits,
9781 const char *path, const char *branch_name, struct got_repository *repo)
9783 const struct got_error *err = NULL, *prev_err = edit_err;
9786 while (resp != 'c' && resp != 'r' && resp != 'a') {
9787 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9788 "or (a)bort: ", getprogname(), prev_err->msg);
9793 histedit_free_list(histedit_cmds);
9794 err = histedit_run_editor(histedit_cmds, path, commits,
9797 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9798 err->code != GOT_ERR_HISTEDIT_CMD)
9805 } else if (resp == 'r') {
9806 histedit_free_list(histedit_cmds);
9807 err = histedit_edit_script(histedit_cmds,
9808 commits, branch_name, 0, 0, repo);
9810 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9811 err->code != GOT_ERR_HISTEDIT_CMD)
9818 } else if (resp == 'a') {
9819 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9822 printf("invalid response '%c'\n", resp);
9828 static const struct got_error *
9829 histedit_complete(struct got_worktree *worktree,
9830 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9831 struct got_reference *branch, struct got_repository *repo)
9833 printf("Switching work tree to %s\n",
9834 got_ref_get_symref_target(branch));
9835 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9839 static const struct got_error *
9840 show_histedit_progress(struct got_commit_object *commit,
9841 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9843 const struct got_error *err;
9844 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9846 err = got_object_id_str(&old_id_str, hle->commit_id);
9851 err = got_object_id_str(&new_id_str, new_id);
9856 old_id_str[12] = '\0';
9858 new_id_str[12] = '\0';
9861 logmsg = strdup(hle->logmsg);
9862 if (logmsg == NULL) {
9863 err = got_error_from_errno("strdup");
9866 trim_logmsg(logmsg, 42);
9868 err = get_short_logmsg(&logmsg, 42, commit);
9873 switch (hle->cmd->code) {
9874 case GOT_HISTEDIT_PICK:
9875 case GOT_HISTEDIT_EDIT:
9876 printf("%s -> %s: %s\n", old_id_str,
9877 new_id_str ? new_id_str : "no-op change", logmsg);
9879 case GOT_HISTEDIT_DROP:
9880 case GOT_HISTEDIT_FOLD:
9881 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9893 static const struct got_error *
9894 histedit_commit(struct got_pathlist_head *merged_paths,
9895 struct got_worktree *worktree, struct got_fileindex *fileindex,
9896 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9897 struct got_repository *repo)
9899 const struct got_error *err;
9900 struct got_commit_object *commit;
9901 struct got_object_id *new_commit_id;
9903 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9904 && hle->logmsg == NULL) {
9905 err = histedit_edit_logmsg(hle, repo);
9910 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9914 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9915 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9918 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9920 err = show_histedit_progress(commit, hle, NULL);
9922 err = show_histedit_progress(commit, hle, new_commit_id);
9923 free(new_commit_id);
9926 got_object_commit_close(commit);
9930 static const struct got_error *
9931 histedit_skip_commit(struct got_histedit_list_entry *hle,
9932 struct got_worktree *worktree, struct got_repository *repo)
9934 const struct got_error *error;
9935 struct got_commit_object *commit;
9937 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9942 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9946 error = show_histedit_progress(commit, hle, NULL);
9947 got_object_commit_close(commit);
9951 static const struct got_error *
9952 check_local_changes(void *arg, unsigned char status,
9953 unsigned char staged_status, const char *path,
9954 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9955 struct got_object_id *commit_id, int dirfd, const char *de_name)
9957 int *have_local_changes = arg;
9960 case GOT_STATUS_ADD:
9961 case GOT_STATUS_DELETE:
9962 case GOT_STATUS_MODIFY:
9963 case GOT_STATUS_CONFLICT:
9964 *have_local_changes = 1;
9965 return got_error(GOT_ERR_CANCELLED);
9970 switch (staged_status) {
9971 case GOT_STATUS_ADD:
9972 case GOT_STATUS_DELETE:
9973 case GOT_STATUS_MODIFY:
9974 *have_local_changes = 1;
9975 return got_error(GOT_ERR_CANCELLED);
9983 static const struct got_error *
9984 cmd_histedit(int argc, char *argv[])
9986 const struct got_error *error = NULL;
9987 struct got_worktree *worktree = NULL;
9988 struct got_fileindex *fileindex = NULL;
9989 struct got_repository *repo = NULL;
9991 struct got_reference *branch = NULL;
9992 struct got_reference *tmp_branch = NULL;
9993 struct got_object_id *resume_commit_id = NULL;
9994 struct got_object_id *base_commit_id = NULL;
9995 struct got_object_id *head_commit_id = NULL;
9996 struct got_commit_object *commit = NULL;
9997 int ch, rebase_in_progress = 0, merge_in_progress = 0;
9998 struct got_update_progress_arg upa;
9999 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10000 int edit_logmsg_only = 0, fold_only = 0;
10001 int list_backups = 0, delete_backups = 0;
10002 const char *edit_script_path = NULL;
10003 struct got_object_id_queue commits;
10004 struct got_pathlist_head merged_paths;
10005 const struct got_object_id_queue *parent_ids;
10006 struct got_object_qid *pid;
10007 struct got_histedit_list histedit_cmds;
10008 struct got_histedit_list_entry *hle;
10010 STAILQ_INIT(&commits);
10011 TAILQ_INIT(&histedit_cmds);
10012 TAILQ_INIT(&merged_paths);
10013 memset(&upa, 0, sizeof(upa));
10015 while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
10027 edit_script_path = optarg;
10030 edit_logmsg_only = 1;
10036 delete_backups = 1;
10048 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10049 "unveil", NULL) == -1)
10052 if (abort_edit && continue_edit)
10053 option_conflict('a', 'c');
10054 if (edit_script_path && edit_logmsg_only)
10055 option_conflict('F', 'm');
10056 if (abort_edit && edit_logmsg_only)
10057 option_conflict('a', 'm');
10058 if (continue_edit && edit_logmsg_only)
10059 option_conflict('c', 'm');
10060 if (abort_edit && fold_only)
10061 option_conflict('a', 'f');
10062 if (continue_edit && fold_only)
10063 option_conflict('c', 'f');
10064 if (fold_only && edit_logmsg_only)
10065 option_conflict('f', 'm');
10066 if (edit_script_path && fold_only)
10067 option_conflict('F', 'f');
10068 if (list_backups) {
10070 option_conflict('l', 'a');
10072 option_conflict('l', 'c');
10073 if (edit_script_path)
10074 option_conflict('l', 'F');
10075 if (edit_logmsg_only)
10076 option_conflict('l', 'm');
10078 option_conflict('l', 'f');
10079 if (delete_backups)
10080 option_conflict('l', 'X');
10081 if (argc != 0 && argc != 1)
10083 } else if (delete_backups) {
10085 option_conflict('X', 'a');
10087 option_conflict('X', 'c');
10088 if (edit_script_path)
10089 option_conflict('X', 'F');
10090 if (edit_logmsg_only)
10091 option_conflict('X', 'm');
10093 option_conflict('X', 'f');
10095 option_conflict('X', 'l');
10096 if (argc != 0 && argc != 1)
10098 } else if (argc != 0)
10102 * This command cannot apply unveil(2) in all cases because the
10103 * user may choose to run an editor to edit the histedit script
10104 * and to edit individual commit log messages.
10105 * unveil(2) traverses exec(2); if an editor is used we have to
10106 * apply unveil after edit script and log messages have been written.
10107 * XXX TODO: Make use of unveil(2) where possible.
10110 cwd = getcwd(NULL, 0);
10112 error = got_error_from_errno("getcwd");
10115 error = got_worktree_open(&worktree, cwd);
10117 if (list_backups || delete_backups) {
10118 if (error->code != GOT_ERR_NOT_WORKTREE)
10121 if (error->code == GOT_ERR_NOT_WORKTREE)
10122 error = wrap_not_worktree_error(error,
10128 if (list_backups || delete_backups) {
10129 error = got_repo_open(&repo,
10130 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10134 error = apply_unveil(got_repo_get_path(repo), 0,
10135 worktree ? got_worktree_get_root_path(worktree) : NULL);
10138 error = process_backup_refs(
10139 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10140 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10141 goto done; /* nothing else to do */
10144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10149 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10152 if (rebase_in_progress) {
10153 error = got_error(GOT_ERR_REBASING);
10157 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10161 if (merge_in_progress) {
10162 error = got_error(GOT_ERR_MERGE_BUSY);
10166 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10170 if (edit_in_progress && edit_logmsg_only) {
10171 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10172 "histedit operation is in progress in this "
10173 "work tree and must be continued or aborted "
10174 "before the -m option can be used");
10177 if (edit_in_progress && fold_only) {
10178 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10179 "histedit operation is in progress in this "
10180 "work tree and must be continued or aborted "
10181 "before the -f option can be used");
10185 if (edit_in_progress && abort_edit) {
10186 error = got_worktree_histedit_continue(&resume_commit_id,
10187 &tmp_branch, &branch, &base_commit_id, &fileindex,
10191 printf("Switching work tree to %s\n",
10192 got_ref_get_symref_target(branch));
10193 error = got_worktree_histedit_abort(worktree, fileindex, repo,
10194 branch, base_commit_id, update_progress, &upa);
10197 printf("Histedit of %s aborted\n",
10198 got_ref_get_symref_target(branch));
10199 print_merge_progress_stats(&upa);
10200 goto done; /* nothing else to do */
10201 } else if (abort_edit) {
10202 error = got_error(GOT_ERR_NOT_HISTEDIT);
10206 if (continue_edit) {
10209 if (!edit_in_progress) {
10210 error = got_error(GOT_ERR_NOT_HISTEDIT);
10214 error = got_worktree_get_histedit_script_path(&path, worktree);
10218 error = histedit_load_list(&histedit_cmds, path, repo);
10223 error = got_worktree_histedit_continue(&resume_commit_id,
10224 &tmp_branch, &branch, &base_commit_id, &fileindex,
10229 error = got_ref_resolve(&head_commit_id, repo, branch);
10233 error = got_object_open_as_commit(&commit, repo,
10237 parent_ids = got_object_commit_get_parent_ids(commit);
10238 pid = STAILQ_FIRST(parent_ids);
10240 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10243 error = collect_commits(&commits, head_commit_id, pid->id,
10244 base_commit_id, got_worktree_get_path_prefix(worktree),
10245 GOT_ERR_HISTEDIT_PATH, repo);
10246 got_object_commit_close(commit);
10251 if (edit_in_progress) {
10252 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10256 error = got_ref_open(&branch, repo,
10257 got_worktree_get_head_ref_name(worktree), 0);
10261 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10262 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10263 "will not edit commit history of a branch outside "
10264 "the \"refs/heads/\" reference namespace");
10268 error = got_ref_resolve(&head_commit_id, repo, branch);
10269 got_ref_close(branch);
10274 error = got_object_open_as_commit(&commit, repo,
10278 parent_ids = got_object_commit_get_parent_ids(commit);
10279 pid = STAILQ_FIRST(parent_ids);
10281 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10284 error = collect_commits(&commits, head_commit_id, pid->id,
10285 got_worktree_get_base_commit_id(worktree),
10286 got_worktree_get_path_prefix(worktree),
10287 GOT_ERR_HISTEDIT_PATH, repo);
10288 got_object_commit_close(commit);
10293 if (STAILQ_EMPTY(&commits)) {
10294 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10298 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10299 &base_commit_id, &fileindex, worktree, repo);
10303 if (edit_script_path) {
10304 error = histedit_load_list(&histedit_cmds,
10305 edit_script_path, repo);
10307 got_worktree_histedit_abort(worktree, fileindex,
10308 repo, branch, base_commit_id,
10309 update_progress, &upa);
10310 print_merge_progress_stats(&upa);
10314 const char *branch_name;
10315 branch_name = got_ref_get_symref_target(branch);
10316 if (strncmp(branch_name, "refs/heads/", 11) == 0)
10318 error = histedit_edit_script(&histedit_cmds, &commits,
10319 branch_name, edit_logmsg_only, fold_only, repo);
10321 got_worktree_histedit_abort(worktree, fileindex,
10322 repo, branch, base_commit_id,
10323 update_progress, &upa);
10324 print_merge_progress_stats(&upa);
10330 error = histedit_save_list(&histedit_cmds, worktree,
10333 got_worktree_histedit_abort(worktree, fileindex,
10334 repo, branch, base_commit_id,
10335 update_progress, &upa);
10336 print_merge_progress_stats(&upa);
10342 error = histedit_check_script(&histedit_cmds, &commits, repo);
10346 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10347 if (resume_commit_id) {
10348 if (got_object_id_cmp(hle->commit_id,
10349 resume_commit_id) != 0)
10352 resume_commit_id = NULL;
10353 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10354 hle->cmd->code == GOT_HISTEDIT_FOLD) {
10355 error = histedit_skip_commit(hle, worktree,
10360 struct got_pathlist_head paths;
10361 int have_changes = 0;
10363 TAILQ_INIT(&paths);
10364 error = got_pathlist_append(&paths, "", NULL);
10367 error = got_worktree_status(worktree, &paths,
10368 repo, 0, check_local_changes, &have_changes,
10369 check_cancelled, NULL);
10370 got_pathlist_free(&paths);
10372 if (error->code != GOT_ERR_CANCELLED)
10374 if (sigint_received || sigpipe_received)
10377 if (have_changes) {
10378 error = histedit_commit(NULL, worktree,
10379 fileindex, tmp_branch, hle, repo);
10383 error = got_object_open_as_commit(
10384 &commit, repo, hle->commit_id);
10387 error = show_histedit_progress(commit,
10389 got_object_commit_close(commit);
10398 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10399 error = histedit_skip_commit(hle, worktree, repo);
10405 error = got_object_open_as_commit(&commit, repo,
10409 parent_ids = got_object_commit_get_parent_ids(commit);
10410 pid = STAILQ_FIRST(parent_ids);
10412 error = got_worktree_histedit_merge_files(&merged_paths,
10413 worktree, fileindex, pid->id, hle->commit_id, repo,
10414 update_progress, &upa, check_cancelled, NULL);
10417 got_object_commit_close(commit);
10420 print_merge_progress_stats(&upa);
10421 if (upa.conflicts > 0 || upa.missing > 0 ||
10422 upa.not_deleted > 0 || upa.unversioned > 0) {
10423 if (upa.conflicts > 0) {
10424 error = show_rebase_merge_conflict(
10425 hle->commit_id, repo);
10429 got_worktree_rebase_pathlist_free(&merged_paths);
10433 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10435 error = got_object_id_str(&id_str, hle->commit_id);
10438 printf("Stopping histedit for amending commit %s\n",
10441 got_worktree_rebase_pathlist_free(&merged_paths);
10442 error = got_worktree_histedit_postpone(worktree,
10447 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10448 error = histedit_skip_commit(hle, worktree, repo);
10454 error = histedit_commit(&merged_paths, worktree, fileindex,
10455 tmp_branch, hle, repo);
10456 got_worktree_rebase_pathlist_free(&merged_paths);
10461 if (upa.conflicts > 0 || upa.missing > 0 ||
10462 upa.not_deleted > 0 || upa.unversioned > 0) {
10463 error = got_worktree_histedit_postpone(worktree, fileindex);
10466 if (upa.conflicts > 0 && upa.missing == 0 &&
10467 upa.not_deleted == 0 && upa.unversioned == 0) {
10468 error = got_error_msg(GOT_ERR_CONFLICTS,
10469 "conflicts must be resolved before histedit "
10471 } else if (upa.conflicts > 0) {
10472 error = got_error_msg(GOT_ERR_CONFLICTS,
10473 "conflicts must be resolved before histedit "
10474 "can continue; changes destined for some "
10475 "files were not yet merged and should be "
10476 "merged manually if required before the "
10477 "histedit operation is continued");
10479 error = got_error_msg(GOT_ERR_CONFLICTS,
10480 "changes destined for some files were not "
10481 "yet merged and should be merged manually "
10482 "if required before the histedit operation "
10486 error = histedit_complete(worktree, fileindex, tmp_branch,
10489 got_object_id_queue_free(&commits);
10490 histedit_free_list(&histedit_cmds);
10491 free(head_commit_id);
10492 free(base_commit_id);
10493 free(resume_commit_id);
10495 got_object_commit_close(commit);
10497 got_ref_close(branch);
10499 got_ref_close(tmp_branch);
10501 got_worktree_close(worktree);
10503 const struct got_error *close_err = got_repo_close(repo);
10511 usage_integrate(void)
10513 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10517 static const struct got_error *
10518 cmd_integrate(int argc, char *argv[])
10520 const struct got_error *error = NULL;
10521 struct got_repository *repo = NULL;
10522 struct got_worktree *worktree = NULL;
10523 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10524 const char *branch_arg = NULL;
10525 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10526 struct got_fileindex *fileindex = NULL;
10527 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10529 struct got_update_progress_arg upa;
10531 while ((ch = getopt(argc, argv, "")) != -1) {
10544 branch_arg = argv[0];
10546 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10547 "unveil", NULL) == -1)
10550 cwd = getcwd(NULL, 0);
10552 error = got_error_from_errno("getcwd");
10556 error = got_worktree_open(&worktree, cwd);
10558 if (error->code == GOT_ERR_NOT_WORKTREE)
10559 error = wrap_not_worktree_error(error, "integrate",
10564 error = check_rebase_or_histedit_in_progress(worktree);
10568 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10573 error = apply_unveil(got_repo_get_path(repo), 0,
10574 got_worktree_get_root_path(worktree));
10578 error = check_merge_in_progress(worktree, repo);
10582 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10583 error = got_error_from_errno("asprintf");
10587 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10588 &base_branch_ref, worktree, refname, repo);
10592 refname = strdup(got_ref_get_name(branch_ref));
10593 if (refname == NULL) {
10594 error = got_error_from_errno("strdup");
10595 got_worktree_integrate_abort(worktree, fileindex, repo,
10596 branch_ref, base_branch_ref);
10599 base_refname = strdup(got_ref_get_name(base_branch_ref));
10600 if (base_refname == NULL) {
10601 error = got_error_from_errno("strdup");
10602 got_worktree_integrate_abort(worktree, fileindex, repo,
10603 branch_ref, base_branch_ref);
10607 error = got_ref_resolve(&commit_id, repo, branch_ref);
10611 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10615 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10616 error = got_error_msg(GOT_ERR_SAME_BRANCH,
10617 "specified branch has already been integrated");
10618 got_worktree_integrate_abort(worktree, fileindex, repo,
10619 branch_ref, base_branch_ref);
10623 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10625 if (error->code == GOT_ERR_ANCESTRY)
10626 error = got_error(GOT_ERR_REBASE_REQUIRED);
10627 got_worktree_integrate_abort(worktree, fileindex, repo,
10628 branch_ref, base_branch_ref);
10632 memset(&upa, 0, sizeof(upa));
10633 error = got_worktree_integrate_continue(worktree, fileindex, repo,
10634 branch_ref, base_branch_ref, update_progress, &upa,
10635 check_cancelled, NULL);
10639 printf("Integrated %s into %s\n", refname, base_refname);
10640 print_update_progress_stats(&upa);
10643 const struct got_error *close_err = got_repo_close(repo);
10648 got_worktree_close(worktree);
10650 free(base_commit_id);
10653 free(base_refname);
10660 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
10665 static const struct got_error *
10666 cmd_merge(int argc, char *argv[])
10668 const struct got_error *error = NULL;
10669 struct got_worktree *worktree = NULL;
10670 struct got_repository *repo = NULL;
10671 struct got_fileindex *fileindex = NULL;
10672 char *cwd = NULL, *id_str = NULL, *author = NULL;
10673 struct got_reference *branch = NULL, *wt_branch = NULL;
10674 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
10675 struct got_object_id *wt_branch_tip = NULL;
10676 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
10677 int interrupt_merge = 0;
10678 struct got_update_progress_arg upa;
10679 struct got_object_id *merge_commit_id = NULL;
10680 char *branch_name = NULL;
10682 memset(&upa, 0, sizeof(upa));
10684 while ((ch = getopt(argc, argv, "acn")) != -1) {
10690 continue_merge = 1;
10693 interrupt_merge = 1;
10705 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10706 "unveil", NULL) == -1)
10710 if (abort_merge && continue_merge)
10711 option_conflict('a', 'c');
10712 if (abort_merge || continue_merge) {
10715 } else if (argc != 1)
10718 cwd = getcwd(NULL, 0);
10720 error = got_error_from_errno("getcwd");
10724 error = got_worktree_open(&worktree, cwd);
10726 if (error->code == GOT_ERR_NOT_WORKTREE)
10727 error = wrap_not_worktree_error(error,
10732 error = got_repo_open(&repo,
10733 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
10737 error = apply_unveil(got_repo_get_path(repo), 0,
10738 worktree ? got_worktree_get_root_path(worktree) : NULL);
10742 error = check_rebase_or_histedit_in_progress(worktree);
10746 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10752 if (!merge_in_progress) {
10753 error = got_error(GOT_ERR_NOT_MERGING);
10756 error = got_worktree_merge_continue(&branch_name,
10757 &branch_tip, &fileindex, worktree, repo);
10760 error = got_worktree_merge_abort(worktree, fileindex, repo,
10761 update_progress, &upa);
10764 printf("Merge of %s aborted\n", branch_name);
10765 goto done; /* nothing else to do */
10768 error = get_author(&author, repo, worktree);
10772 if (continue_merge) {
10773 if (!merge_in_progress) {
10774 error = got_error(GOT_ERR_NOT_MERGING);
10777 error = got_worktree_merge_continue(&branch_name,
10778 &branch_tip, &fileindex, worktree, repo);
10782 error = got_ref_open(&branch, repo, argv[0], 0);
10785 branch_name = strdup(got_ref_get_name(branch));
10786 if (branch_name == NULL) {
10787 error = got_error_from_errno("strdup");
10790 error = got_ref_resolve(&branch_tip, repo, branch);
10795 error = got_ref_open(&wt_branch, repo,
10796 got_worktree_get_head_ref_name(worktree), 0);
10799 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
10802 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10803 wt_branch_tip, branch_tip, 0, repo,
10804 check_cancelled, NULL);
10805 if (error && error->code != GOT_ERR_ANCESTRY)
10808 if (!continue_merge) {
10809 error = check_path_prefix(wt_branch_tip, branch_tip,
10810 got_worktree_get_path_prefix(worktree),
10811 GOT_ERR_MERGE_PATH, repo);
10815 error = check_same_branch(wt_branch_tip, branch,
10818 if (error->code != GOT_ERR_ANCESTRY)
10822 static char msg[512];
10823 snprintf(msg, sizeof(msg),
10824 "cannot create a merge commit because "
10825 "%s is based on %s; %s can be integrated "
10826 "with 'got integrate' instead", branch_name,
10827 got_worktree_get_head_ref_name(worktree),
10829 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
10833 error = got_worktree_merge_prepare(&fileindex, worktree,
10838 error = got_worktree_merge_branch(worktree, fileindex,
10839 yca_id, branch_tip, repo, update_progress, &upa,
10840 check_cancelled, NULL);
10843 print_merge_progress_stats(&upa);
10844 if (!upa.did_something) {
10845 error = got_worktree_merge_abort(worktree, fileindex,
10846 repo, update_progress, &upa);
10849 printf("Already up-to-date\n");
10854 if (interrupt_merge) {
10855 error = got_worktree_merge_postpone(worktree, fileindex);
10858 printf("Merge of %s interrupted on request\n", branch_name);
10859 } else if (upa.conflicts > 0 || upa.missing > 0 ||
10860 upa.not_deleted > 0 || upa.unversioned > 0) {
10861 error = got_worktree_merge_postpone(worktree, fileindex);
10864 if (upa.conflicts > 0 && upa.missing == 0 &&
10865 upa.not_deleted == 0 && upa.unversioned == 0) {
10866 error = got_error_msg(GOT_ERR_CONFLICTS,
10867 "conflicts must be resolved before merging "
10869 } else if (upa.conflicts > 0) {
10870 error = got_error_msg(GOT_ERR_CONFLICTS,
10871 "conflicts must be resolved before merging "
10872 "can continue; changes destined for some "
10873 "files were not yet merged and "
10874 "should be merged manually if required before the "
10875 "merge operation is continued");
10877 error = got_error_msg(GOT_ERR_CONFLICTS,
10878 "changes destined for some "
10879 "files were not yet merged and should be "
10880 "merged manually if required before the "
10881 "merge operation is continued");
10885 error = got_worktree_merge_commit(&merge_commit_id, worktree,
10886 fileindex, author, NULL, 1, branch_tip, branch_name,
10887 repo, continue_merge ? print_status : NULL, NULL);
10890 error = got_worktree_merge_complete(worktree, fileindex, repo);
10893 error = got_object_id_str(&id_str, merge_commit_id);
10896 printf("Merged %s into %s: %s\n", branch_name,
10897 got_worktree_get_head_ref_name(worktree),
10903 free(merge_commit_id);
10909 got_ref_close(branch);
10911 got_ref_close(wt_branch);
10913 got_worktree_close(worktree);
10915 const struct got_error *close_err = got_repo_close(repo);
10925 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10926 "[-S] [file-path ...]\n",
10931 static const struct got_error *
10932 print_stage(void *arg, unsigned char status, unsigned char staged_status,
10933 const char *path, struct got_object_id *blob_id,
10934 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10935 int dirfd, const char *de_name)
10937 const struct got_error *err = NULL;
10938 char *id_str = NULL;
10940 if (staged_status != GOT_STATUS_ADD &&
10941 staged_status != GOT_STATUS_MODIFY &&
10942 staged_status != GOT_STATUS_DELETE)
10945 if (staged_status == GOT_STATUS_ADD ||
10946 staged_status == GOT_STATUS_MODIFY)
10947 err = got_object_id_str(&id_str, staged_blob_id);
10949 err = got_object_id_str(&id_str, blob_id);
10953 printf("%s %c %s\n", id_str, staged_status, path);
10958 static const struct got_error *
10959 cmd_stage(int argc, char *argv[])
10961 const struct got_error *error = NULL;
10962 struct got_repository *repo = NULL;
10963 struct got_worktree *worktree = NULL;
10965 struct got_pathlist_head paths;
10966 struct got_pathlist_entry *pe;
10967 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10968 FILE *patch_script_file = NULL;
10969 const char *patch_script_path = NULL;
10970 struct choose_patch_arg cpa;
10972 TAILQ_INIT(&paths);
10974 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10983 patch_script_path = optarg;
10986 allow_bad_symlinks = 1;
10998 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10999 "unveil", NULL) == -1)
11002 if (list_stage && (pflag || patch_script_path))
11003 errx(1, "-l option cannot be used with other options");
11004 if (patch_script_path && !pflag)
11005 errx(1, "-F option can only be used together with -p option");
11007 cwd = getcwd(NULL, 0);
11009 error = got_error_from_errno("getcwd");
11013 error = got_worktree_open(&worktree, cwd);
11015 if (error->code == GOT_ERR_NOT_WORKTREE)
11016 error = wrap_not_worktree_error(error, "stage", cwd);
11020 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11025 if (patch_script_path) {
11026 patch_script_file = fopen(patch_script_path, "r");
11027 if (patch_script_file == NULL) {
11028 error = got_error_from_errno2("fopen",
11029 patch_script_path);
11033 error = apply_unveil(got_repo_get_path(repo), 0,
11034 got_worktree_get_root_path(worktree));
11038 error = check_merge_in_progress(worktree, repo);
11042 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11047 error = got_worktree_status(worktree, &paths, repo, 0,
11048 print_stage, NULL, check_cancelled, NULL);
11050 cpa.patch_script_file = patch_script_file;
11051 cpa.action = "stage";
11052 error = got_worktree_stage(worktree, &paths,
11053 pflag ? NULL : print_status, NULL,
11054 pflag ? choose_patch : NULL, &cpa,
11055 allow_bad_symlinks, repo);
11058 if (patch_script_file && fclose(patch_script_file) == EOF &&
11060 error = got_error_from_errno2("fclose", patch_script_path);
11062 const struct got_error *close_err = got_repo_close(repo);
11067 got_worktree_close(worktree);
11068 TAILQ_FOREACH(pe, &paths, entry)
11069 free((char *)pe->path);
11070 got_pathlist_free(&paths);
11076 usage_unstage(void)
11078 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11079 "[file-path ...]\n",
11085 static const struct got_error *
11086 cmd_unstage(int argc, char *argv[])
11088 const struct got_error *error = NULL;
11089 struct got_repository *repo = NULL;
11090 struct got_worktree *worktree = NULL;
11092 struct got_pathlist_head paths;
11093 struct got_pathlist_entry *pe;
11095 struct got_update_progress_arg upa;
11096 FILE *patch_script_file = NULL;
11097 const char *patch_script_path = NULL;
11098 struct choose_patch_arg cpa;
11100 TAILQ_INIT(&paths);
11102 while ((ch = getopt(argc, argv, "pF:")) != -1) {
11108 patch_script_path = optarg;
11120 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11121 "unveil", NULL) == -1)
11124 if (patch_script_path && !pflag)
11125 errx(1, "-F option can only be used together with -p option");
11127 cwd = getcwd(NULL, 0);
11129 error = got_error_from_errno("getcwd");
11133 error = got_worktree_open(&worktree, cwd);
11135 if (error->code == GOT_ERR_NOT_WORKTREE)
11136 error = wrap_not_worktree_error(error, "unstage", cwd);
11140 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11145 if (patch_script_path) {
11146 patch_script_file = fopen(patch_script_path, "r");
11147 if (patch_script_file == NULL) {
11148 error = got_error_from_errno2("fopen",
11149 patch_script_path);
11154 error = apply_unveil(got_repo_get_path(repo), 0,
11155 got_worktree_get_root_path(worktree));
11159 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11163 cpa.patch_script_file = patch_script_file;
11164 cpa.action = "unstage";
11165 memset(&upa, 0, sizeof(upa));
11166 error = got_worktree_unstage(worktree, &paths, update_progress,
11167 &upa, pflag ? choose_patch : NULL, &cpa, repo);
11169 print_merge_progress_stats(&upa);
11171 if (patch_script_file && fclose(patch_script_file) == EOF &&
11173 error = got_error_from_errno2("fclose", patch_script_path);
11175 const struct got_error *close_err = got_repo_close(repo);
11180 got_worktree_close(worktree);
11181 TAILQ_FOREACH(pe, &paths, entry)
11182 free((char *)pe->path);
11183 got_pathlist_free(&paths);
11191 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11192 "arg1 [arg2 ...]\n", getprogname());
11196 static const struct got_error *
11197 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11199 const struct got_error *err;
11200 struct got_blob_object *blob;
11202 err = got_object_open_as_blob(&blob, repo, id, 8192);
11206 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11207 got_object_blob_close(blob);
11211 static const struct got_error *
11212 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11214 const struct got_error *err;
11215 struct got_tree_object *tree;
11218 err = got_object_open_as_tree(&tree, repo, id);
11222 nentries = got_object_tree_get_nentries(tree);
11223 for (i = 0; i < nentries; i++) {
11224 struct got_tree_entry *te;
11226 if (sigint_received || sigpipe_received)
11228 te = got_object_tree_get_entry(tree, i);
11229 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11232 fprintf(outfile, "%s %.7o %s\n", id_str,
11233 got_tree_entry_get_mode(te),
11234 got_tree_entry_get_name(te));
11238 got_object_tree_close(tree);
11242 static const struct got_error *
11243 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11245 const struct got_error *err;
11246 struct got_commit_object *commit;
11247 const struct got_object_id_queue *parent_ids;
11248 struct got_object_qid *pid;
11249 char *id_str = NULL;
11250 const char *logmsg = NULL;
11252 err = got_object_open_as_commit(&commit, repo, id);
11256 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11260 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11261 parent_ids = got_object_commit_get_parent_ids(commit);
11262 fprintf(outfile, "numparents %d\n",
11263 got_object_commit_get_nparents(commit));
11264 STAILQ_FOREACH(pid, parent_ids, entry) {
11266 err = got_object_id_str(&pid_str, pid->id);
11269 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11272 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
11273 got_object_commit_get_author(commit),
11274 (long long)got_object_commit_get_author_time(commit));
11276 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
11277 got_object_commit_get_author(commit),
11278 (long long)got_object_commit_get_committer_time(commit));
11280 logmsg = got_object_commit_get_logmsg_raw(commit);
11281 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11282 fprintf(outfile, "%s", logmsg);
11285 got_object_commit_close(commit);
11289 static const struct got_error *
11290 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11292 const struct got_error *err;
11293 struct got_tag_object *tag;
11294 char *id_str = NULL;
11295 const char *tagmsg = NULL;
11297 err = got_object_open_as_tag(&tag, repo, id);
11301 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11305 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11307 switch (got_object_tag_get_object_type(tag)) {
11308 case GOT_OBJ_TYPE_BLOB:
11309 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11310 GOT_OBJ_LABEL_BLOB);
11312 case GOT_OBJ_TYPE_TREE:
11313 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11314 GOT_OBJ_LABEL_TREE);
11316 case GOT_OBJ_TYPE_COMMIT:
11317 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11318 GOT_OBJ_LABEL_COMMIT);
11320 case GOT_OBJ_TYPE_TAG:
11321 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11322 GOT_OBJ_LABEL_TAG);
11328 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11329 got_object_tag_get_name(tag));
11331 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
11332 got_object_tag_get_tagger(tag),
11333 (long long)got_object_tag_get_tagger_time(tag));
11335 tagmsg = got_object_tag_get_message(tag);
11336 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11337 fprintf(outfile, "%s", tagmsg);
11340 got_object_tag_close(tag);
11344 static const struct got_error *
11345 cmd_cat(int argc, char *argv[])
11347 const struct got_error *error;
11348 struct got_repository *repo = NULL;
11349 struct got_worktree *worktree = NULL;
11350 char *cwd = NULL, *repo_path = NULL, *label = NULL;
11351 const char *commit_id_str = NULL;
11352 struct got_object_id *id = NULL, *commit_id = NULL;
11353 int ch, obj_type, i, force_path = 0;
11354 struct got_reflist_head refs;
11359 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11364 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11367 commit_id_str = optarg;
11370 repo_path = realpath(optarg, NULL);
11371 if (repo_path == NULL)
11372 return got_error_from_errno2("realpath",
11374 got_path_strip_trailing_slashes(repo_path);
11388 cwd = getcwd(NULL, 0);
11390 error = got_error_from_errno("getcwd");
11393 error = got_worktree_open(&worktree, cwd);
11394 if (error && error->code != GOT_ERR_NOT_WORKTREE)
11397 if (repo_path == NULL) {
11398 repo_path = strdup(
11399 got_worktree_get_repo_path(worktree));
11400 if (repo_path == NULL) {
11401 error = got_error_from_errno("strdup");
11407 if (repo_path == NULL) {
11408 repo_path = getcwd(NULL, 0);
11409 if (repo_path == NULL)
11410 return got_error_from_errno("getcwd");
11413 error = got_repo_open(&repo, repo_path, NULL);
11418 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11422 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11426 if (commit_id_str == NULL)
11427 commit_id_str = GOT_REF_HEAD;
11428 error = got_repo_match_object_id(&commit_id, NULL,
11429 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11433 for (i = 0; i < argc; i++) {
11435 error = got_object_id_by_path(&id, repo, commit_id,
11440 error = got_repo_match_object_id(&id, &label, argv[i],
11441 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11444 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11445 error->code != GOT_ERR_NOT_REF)
11447 error = got_object_id_by_path(&id, repo,
11448 commit_id, argv[i]);
11454 error = got_object_get_type(&obj_type, repo, id);
11458 switch (obj_type) {
11459 case GOT_OBJ_TYPE_BLOB:
11460 error = cat_blob(id, repo, stdout);
11462 case GOT_OBJ_TYPE_TREE:
11463 error = cat_tree(id, repo, stdout);
11465 case GOT_OBJ_TYPE_COMMIT:
11466 error = cat_commit(id, repo, stdout);
11468 case GOT_OBJ_TYPE_TAG:
11469 error = cat_tag(id, repo, stdout);
11472 error = got_error(GOT_ERR_OBJ_TYPE);
11487 got_worktree_close(worktree);
11489 const struct got_error *close_err = got_repo_close(repo);
11493 got_ref_list_free(&refs);
11500 fprintf(stderr, "usage: %s info [path ...]\n",
11505 static const struct got_error *
11506 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11507 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11508 struct got_object_id *commit_id)
11510 const struct got_error *err = NULL;
11511 char *id_str = NULL;
11513 struct tm mytm, *tm;
11514 struct got_pathlist_head *paths = arg;
11515 struct got_pathlist_entry *pe;
11518 * Clear error indication from any of the path arguments which
11519 * would cause this file index entry to be displayed.
11521 TAILQ_FOREACH(pe, paths, entry) {
11522 if (got_path_cmp(path, pe->path, strlen(path),
11523 pe->path_len) == 0 ||
11524 got_path_is_child(path, pe->path, pe->path_len))
11525 pe->data = NULL; /* no error */
11528 printf(GOT_COMMIT_SEP_STR);
11530 printf("symlink: %s\n", path);
11531 else if (S_ISREG(mode)) {
11532 printf("file: %s\n", path);
11533 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11534 } else if (S_ISDIR(mode))
11535 printf("directory: %s\n", path);
11537 printf("something: %s\n", path);
11539 tm = localtime_r(&mtime, &mytm);
11542 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11543 return got_error(GOT_ERR_NO_SPACE);
11544 printf("timestamp: %s\n", datebuf);
11547 err = got_object_id_str(&id_str, blob_id);
11550 printf("based on blob: %s\n", id_str);
11554 if (staged_blob_id) {
11555 err = got_object_id_str(&id_str, staged_blob_id);
11558 printf("based on staged blob: %s\n", id_str);
11563 err = got_object_id_str(&id_str, commit_id);
11566 printf("based on commit: %s\n", id_str);
11573 static const struct got_error *
11574 cmd_info(int argc, char *argv[])
11576 const struct got_error *error = NULL;
11577 struct got_worktree *worktree = NULL;
11578 char *cwd = NULL, *id_str = NULL;
11579 struct got_pathlist_head paths;
11580 struct got_pathlist_entry *pe;
11581 char *uuidstr = NULL;
11582 int ch, show_files = 0;
11584 TAILQ_INIT(&paths);
11586 while ((ch = getopt(argc, argv, "")) != -1) {
11598 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11602 cwd = getcwd(NULL, 0);
11604 error = got_error_from_errno("getcwd");
11608 error = got_worktree_open(&worktree, cwd);
11610 if (error->code == GOT_ERR_NOT_WORKTREE)
11611 error = wrap_not_worktree_error(error, "info", cwd);
11615 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11620 error = get_worktree_paths_from_argv(&paths, argc, argv,
11627 error = got_object_id_str(&id_str,
11628 got_worktree_get_base_commit_id(worktree));
11632 error = got_worktree_get_uuid(&uuidstr, worktree);
11636 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11637 printf("work tree base commit: %s\n", id_str);
11638 printf("work tree path prefix: %s\n",
11639 got_worktree_get_path_prefix(worktree));
11640 printf("work tree branch reference: %s\n",
11641 got_worktree_get_head_ref_name(worktree));
11642 printf("work tree UUID: %s\n", uuidstr);
11643 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11646 struct got_pathlist_entry *pe;
11647 TAILQ_FOREACH(pe, &paths, entry) {
11648 if (pe->path_len == 0)
11651 * Assume this path will fail. This will be corrected
11652 * in print_path_info() in case the path does suceeed.
11654 pe->data = (void *)got_error_path(pe->path,
11657 error = got_worktree_path_info(worktree, &paths,
11658 print_path_info, &paths, check_cancelled, NULL);
11661 TAILQ_FOREACH(pe, &paths, entry) {
11662 if (pe->data != NULL) {
11663 error = pe->data; /* bad path */
11669 TAILQ_FOREACH(pe, &paths, entry)
11670 free((char *)pe->path);
11671 got_pathlist_free(&paths);