2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
402 size_t len, logmsg_len;
403 char *initial_content_stripped = NULL, *buf = NULL, *s;
407 if (stat(logmsg_path, &st) == -1)
408 return got_error_from_errno2("stat", logmsg_path);
410 if (spawn_editor(editor, logmsg_path) == -1)
411 return got_error_from_errno("failed spawning editor");
413 if (stat(logmsg_path, &st2) == -1)
414 return got_error_from_errno("stat");
416 if (require_modification &&
417 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
422 * Set up a stripped version of the initial content without comments
423 * and blank lines. We need this in order to check if the message
424 * has in fact been edited.
426 initial_content_stripped = malloc(initial_content_len + 1);
427 if (initial_content_stripped == NULL)
428 return got_error_from_errno("malloc");
429 initial_content_stripped[0] = '\0';
431 buf = strdup(initial_content);
433 err = got_error_from_errno("strdup");
438 while ((line = strsep(&s, "\n")) != NULL) {
439 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
440 continue; /* remove comments and leading empty lines */
441 len = strlcat(initial_content_stripped, line,
442 initial_content_len + 1);
443 if (len >= initial_content_len + 1) {
444 err = got_error(GOT_ERR_NO_SPACE);
448 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
449 initial_content_stripped[len - 1] = '\0';
453 logmsg_len = st2.st_size;
454 *logmsg = malloc(logmsg_len + 1);
456 return got_error_from_errno("malloc");
459 fp = fopen(logmsg_path, "re");
461 err = got_error_from_errno("fopen");
466 while ((linelen = getline(&line, &linesize, fp)) != -1) {
467 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
468 continue; /* remove comments and leading empty lines */
469 len = strlcat(*logmsg, line, logmsg_len + 1);
470 if (len >= logmsg_len + 1) {
471 err = got_error(GOT_ERR_NO_SPACE);
477 err = got_ferror(fp, GOT_ERR_IO);
480 while (len > 0 && (*logmsg)[len - 1] == '\n') {
481 (*logmsg)[len - 1] = '\0';
486 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
487 "commit message cannot be empty, aborting");
490 if (require_modification &&
491 strcmp(*logmsg, initial_content_stripped) == 0)
492 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
493 "no changes made to commit message, aborting");
495 free(initial_content_stripped);
497 if (fp && fclose(fp) == EOF && err == NULL)
498 err = got_error_from_errno("fclose");
506 static const struct got_error *
507 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
508 const char *path_dir, const char *branch_name)
510 char *initial_content = NULL;
511 const struct got_error *err = NULL;
512 int initial_content_len;
515 initial_content_len = asprintf(&initial_content,
516 "\n# %s to be imported to branch %s\n", path_dir,
518 if (initial_content_len == -1)
519 return got_error_from_errno("asprintf");
521 err = got_opentemp_named_fd(logmsg_path, &fd,
522 GOT_TMPDIR_STR "/got-importmsg");
526 if (write(fd, initial_content, initial_content_len) == -1) {
527 err = got_error_from_errno2("write", *logmsg_path);
531 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
532 initial_content_len, 1);
534 if (fd != -1 && close(fd) == -1 && err == NULL)
535 err = got_error_from_errno2("close", *logmsg_path);
536 free(initial_content);
544 static const struct got_error *
545 import_progress(void *arg, const char *path)
547 printf("A %s\n", path);
552 valid_author(const char *author)
555 * Really dumb email address check; we're only doing this to
556 * avoid git's object parser breaking on commits we create.
558 while (*author && *author != '<')
562 while (*author && *author != '@')
566 while (*author && *author != '>')
568 return *author == '>';
571 static const struct got_error *
572 get_author(char **author, struct got_repository *repo,
573 struct got_worktree *worktree)
575 const struct got_error *err = NULL;
576 const char *got_author = NULL, *name, *email;
577 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
582 worktree_conf = got_worktree_get_gotconfig(worktree);
583 repo_conf = got_repo_get_gotconfig(repo);
586 * Priority of potential author information sources, from most
587 * significant to least significant:
588 * 1) work tree's .got/got.conf file
589 * 2) repository's got.conf file
590 * 3) repository's git config file
591 * 4) environment variables
592 * 5) global git config files (in user's home directory or /etc)
596 got_author = got_gotconfig_get_author(worktree_conf);
597 if (got_author == NULL)
598 got_author = got_gotconfig_get_author(repo_conf);
599 if (got_author == NULL) {
600 name = got_repo_get_gitconfig_author_name(repo);
601 email = got_repo_get_gitconfig_author_email(repo);
603 if (asprintf(author, "%s <%s>", name, email) == -1)
604 return got_error_from_errno("asprintf");
608 got_author = getenv("GOT_AUTHOR");
609 if (got_author == NULL) {
610 name = got_repo_get_global_gitconfig_author_name(repo);
611 email = got_repo_get_global_gitconfig_author_email(
614 if (asprintf(author, "%s <%s>", name, email)
616 return got_error_from_errno("asprintf");
619 /* TODO: Look up user in password database? */
620 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
624 *author = strdup(got_author);
626 return got_error_from_errno("strdup");
628 if (!valid_author(*author)) {
629 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
636 static const struct got_error *
637 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
638 struct got_worktree *worktree)
640 const char *got_allowed_signers = NULL;
641 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
643 *allowed_signers = NULL;
646 worktree_conf = got_worktree_get_gotconfig(worktree);
647 repo_conf = got_repo_get_gotconfig(repo);
650 * Priority of potential author information sources, from most
651 * significant to least significant:
652 * 1) work tree's .got/got.conf file
653 * 2) repository's got.conf file
657 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
659 if (got_allowed_signers == NULL)
660 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
663 if (got_allowed_signers) {
664 *allowed_signers = strdup(got_allowed_signers);
665 if (*allowed_signers == NULL)
666 return got_error_from_errno("strdup");
671 static const struct got_error *
672 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
673 struct got_worktree *worktree)
675 const char *got_revoked_signers = NULL;
676 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
678 *revoked_signers = NULL;
681 worktree_conf = got_worktree_get_gotconfig(worktree);
682 repo_conf = got_repo_get_gotconfig(repo);
685 * Priority of potential author information sources, from most
686 * significant to least significant:
687 * 1) work tree's .got/got.conf file
688 * 2) repository's got.conf file
692 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
694 if (got_revoked_signers == NULL)
695 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
698 if (got_revoked_signers) {
699 *revoked_signers = strdup(got_revoked_signers);
700 if (*revoked_signers == NULL)
701 return got_error_from_errno("strdup");
706 static const struct got_error *
707 get_signer_id(char **signer_id, struct got_repository *repo,
708 struct got_worktree *worktree)
710 const char *got_signer_id = NULL;
711 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
716 worktree_conf = got_worktree_get_gotconfig(worktree);
717 repo_conf = got_repo_get_gotconfig(repo);
720 * Priority of potential author information sources, from most
721 * significant to least significant:
722 * 1) work tree's .got/got.conf file
723 * 2) repository's got.conf file
727 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
728 if (got_signer_id == NULL)
729 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
732 *signer_id = strdup(got_signer_id);
733 if (*signer_id == NULL)
734 return got_error_from_errno("strdup");
739 static const struct got_error *
740 get_gitconfig_path(char **gitconfig_path)
742 const char *homedir = getenv("HOME");
744 *gitconfig_path = NULL;
746 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
747 return got_error_from_errno("asprintf");
753 static const struct got_error *
754 cmd_import(int argc, char *argv[])
756 const struct got_error *error = NULL;
757 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
758 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
759 const char *branch_name = "main";
760 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
761 struct got_repository *repo = NULL;
762 struct got_reference *branch_ref = NULL, *head_ref = NULL;
763 struct got_object_id *new_commit_id = NULL;
765 struct got_pathlist_head ignores;
766 struct got_pathlist_entry *pe;
767 int preserve_logmsg = 0;
768 int *pack_fds = NULL;
770 TAILQ_INIT(&ignores);
772 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
775 branch_name = optarg;
778 logmsg = strdup(optarg);
779 if (logmsg == NULL) {
780 error = got_error_from_errno("strdup");
785 repo_path = realpath(optarg, NULL);
786 if (repo_path == NULL) {
787 error = got_error_from_errno2("realpath",
793 if (optarg[0] == '\0')
795 error = got_pathlist_insert(&pe, &ignores, optarg,
810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
818 if (repo_path == NULL) {
819 repo_path = getcwd(NULL, 0);
820 if (repo_path == NULL)
821 return got_error_from_errno("getcwd");
823 got_path_strip_trailing_slashes(repo_path);
824 error = get_gitconfig_path(&gitconfig_path);
827 error = got_repo_pack_fds_open(&pack_fds);
830 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
834 error = get_author(&author, repo, NULL);
839 * Don't let the user create a branch name with a leading '-'.
840 * While technically a valid reference name, this case is usually
841 * an unintended typo.
843 if (branch_name[0] == '-')
844 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
846 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
847 error = got_error_from_errno("asprintf");
851 error = got_ref_open(&branch_ref, repo, refname, 0);
853 if (error->code != GOT_ERR_NOT_REF)
856 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
857 "import target branch already exists");
861 path_dir = realpath(argv[0], NULL);
862 if (path_dir == NULL) {
863 error = got_error_from_errno2("realpath", argv[0]);
866 got_path_strip_trailing_slashes(path_dir);
869 * unveil(2) traverses exec(2); if an editor is used we have
870 * to apply unveil after the log message has been written.
872 if (logmsg == NULL || strlen(logmsg) == 0) {
873 error = get_editor(&editor);
877 error = collect_import_msg(&logmsg, &logmsg_path, editor,
880 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
887 if (unveil(path_dir, "r") != 0) {
888 error = got_error_from_errno2("unveil", path_dir);
894 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
901 error = got_repo_import(&new_commit_id, path_dir, logmsg,
902 author, &ignores, repo, import_progress, NULL);
909 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
916 error = got_ref_write(branch_ref, repo);
923 error = got_object_id_str(&id_str, new_commit_id);
930 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
932 if (error->code != GOT_ERR_NOT_REF) {
938 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
946 error = got_ref_write(head_ref, repo);
954 printf("Created branch %s with commit %s\n",
955 got_ref_get_name(branch_ref), id_str);
958 const struct got_error *pack_err =
959 got_repo_pack_fds_close(pack_fds);
963 if (preserve_logmsg) {
964 fprintf(stderr, "%s: log message preserved in %s\n",
965 getprogname(), logmsg_path);
966 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
967 error = got_error_from_errno2("unlink", logmsg_path);
976 free(gitconfig_path);
978 got_ref_close(branch_ref);
980 got_ref_close(head_ref);
987 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
988 "[-R reference] repository-url [directory]\n", getprogname());
992 struct got_fetch_progress_arg {
993 char last_scaled_size[FMT_SCALED_STRSIZE];
998 struct got_repository *repo;
1001 int configs_created;
1003 struct got_pathlist_head *symrefs;
1004 struct got_pathlist_head *wanted_branches;
1005 struct got_pathlist_head *wanted_refs;
1009 const char *remote_repo_path;
1010 const char *git_url;
1011 int fetch_all_branches;
1012 int mirror_references;
1016 /* XXX forward declaration */
1017 static const struct got_error *
1018 create_config_files(const char *proto, const char *host, const char *port,
1019 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1020 int mirror_references, struct got_pathlist_head *symrefs,
1021 struct got_pathlist_head *wanted_branches,
1022 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1024 static const struct got_error *
1025 fetch_progress(void *arg, const char *message, off_t packfile_size,
1026 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1028 const struct got_error *err = NULL;
1029 struct got_fetch_progress_arg *a = arg;
1030 char scaled_size[FMT_SCALED_STRSIZE];
1031 int p_indexed, p_resolved;
1032 int print_size = 0, print_indexed = 0, print_resolved = 0;
1035 * In order to allow a failed clone to be resumed with 'got fetch'
1036 * we try to create configuration files as soon as possible.
1037 * Once the server has sent information about its default branch
1038 * we have all required information.
1040 if (a->create_configs && !a->configs_created &&
1041 !TAILQ_EMPTY(a->config_info.symrefs)) {
1042 err = create_config_files(a->config_info.proto,
1043 a->config_info.host, a->config_info.port,
1044 a->config_info.remote_repo_path,
1045 a->config_info.git_url,
1046 a->config_info.fetch_all_branches,
1047 a->config_info.mirror_references,
1048 a->config_info.symrefs,
1049 a->config_info.wanted_branches,
1050 a->config_info.wanted_refs, a->repo);
1053 a->configs_created = 1;
1056 if (a->verbosity < 0)
1059 if (message && message[0] != '\0') {
1060 printf("\rserver: %s", message);
1065 if (packfile_size > 0 || nobj_indexed > 0) {
1066 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1067 (a->last_scaled_size[0] == '\0' ||
1068 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1070 if (strlcpy(a->last_scaled_size, scaled_size,
1071 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1072 return got_error(GOT_ERR_NO_SPACE);
1074 if (nobj_indexed > 0) {
1075 p_indexed = (nobj_indexed * 100) / nobj_total;
1076 if (p_indexed != a->last_p_indexed) {
1077 a->last_p_indexed = p_indexed;
1082 if (nobj_resolved > 0) {
1083 p_resolved = (nobj_resolved * 100) /
1084 (nobj_total - nobj_loose);
1085 if (p_resolved != a->last_p_resolved) {
1086 a->last_p_resolved = p_resolved;
1094 if (print_size || print_indexed || print_resolved)
1097 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1099 printf("; indexing %d%%", p_indexed);
1101 printf("; resolving deltas %d%%", p_resolved);
1102 if (print_size || print_indexed || print_resolved)
1108 static const struct got_error *
1109 create_symref(const char *refname, struct got_reference *target_ref,
1110 int verbosity, struct got_repository *repo)
1112 const struct got_error *err;
1113 struct got_reference *head_symref;
1115 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1119 err = got_ref_write(head_symref, repo);
1120 if (err == NULL && verbosity > 0) {
1121 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1122 got_ref_get_name(target_ref));
1124 got_ref_close(head_symref);
1128 static const struct got_error *
1129 list_remote_refs(struct got_pathlist_head *symrefs,
1130 struct got_pathlist_head *refs)
1132 const struct got_error *err;
1133 struct got_pathlist_entry *pe;
1135 TAILQ_FOREACH(pe, symrefs, entry) {
1136 const char *refname = pe->path;
1137 const char *targetref = pe->data;
1139 printf("%s: %s\n", refname, targetref);
1142 TAILQ_FOREACH(pe, refs, entry) {
1143 const char *refname = pe->path;
1144 struct got_object_id *id = pe->data;
1147 err = got_object_id_str(&id_str, id);
1150 printf("%s: %s\n", refname, id_str);
1157 static const struct got_error *
1158 create_ref(const char *refname, struct got_object_id *id,
1159 int verbosity, struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_reference *ref;
1165 err = got_object_id_str(&id_str, id);
1169 err = got_ref_alloc(&ref, refname, id);
1173 err = got_ref_write(ref, repo);
1176 if (err == NULL && verbosity >= 0)
1177 printf("Created reference %s: %s\n", refname, id_str);
1184 match_wanted_ref(const char *refname, const char *wanted_ref)
1186 if (strncmp(refname, "refs/", 5) != 0)
1191 * Prevent fetching of references that won't make any
1192 * sense outside of the remote repository's context.
1194 if (strncmp(refname, "got/", 4) == 0)
1196 if (strncmp(refname, "remotes/", 8) == 0)
1199 if (strncmp(wanted_ref, "refs/", 5) == 0)
1202 /* Allow prefix match. */
1203 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1206 /* Allow exact match. */
1207 return (strcmp(refname, wanted_ref) == 0);
1211 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1213 struct got_pathlist_entry *pe;
1215 TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 if (match_wanted_ref(refname, pe->path))
1223 static const struct got_error *
1224 create_wanted_ref(const char *refname, struct got_object_id *id,
1225 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1227 const struct got_error *err;
1228 char *remote_refname;
1230 if (strncmp("refs/", refname, 5) == 0)
1233 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1234 remote_repo_name, refname) == -1)
1235 return got_error_from_errno("asprintf");
1237 err = create_ref(remote_refname, id, verbosity, repo);
1238 free(remote_refname);
1242 static const struct got_error *
1243 create_gotconfig(const char *proto, const char *host, const char *port,
1244 const char *remote_repo_path, const char *default_branch,
1245 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1246 struct got_pathlist_head *wanted_refs, int mirror_references,
1247 struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 char *gotconfig_path = NULL;
1251 char *gotconfig = NULL;
1252 FILE *gotconfig_file = NULL;
1253 const char *branchname = NULL;
1254 char *branches = NULL, *refs = NULL;
1257 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1258 struct got_pathlist_entry *pe;
1259 TAILQ_FOREACH(pe, wanted_branches, entry) {
1261 branchname = pe->path;
1262 if (strncmp(branchname, "refs/heads/", 11) == 0)
1264 if (asprintf(&s, "%s\"%s\" ",
1265 branches ? branches : "", branchname) == -1) {
1266 err = got_error_from_errno("asprintf");
1272 } else if (!fetch_all_branches && default_branch) {
1273 branchname = default_branch;
1274 if (strncmp(branchname, "refs/heads/", 11) == 0)
1276 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1277 err = got_error_from_errno("asprintf");
1281 if (!TAILQ_EMPTY(wanted_refs)) {
1282 struct got_pathlist_entry *pe;
1283 TAILQ_FOREACH(pe, wanted_refs, entry) {
1285 const char *refname = pe->path;
1286 if (strncmp(refname, "refs/", 5) == 0)
1288 if (asprintf(&s, "%s\"%s\" ",
1289 refs ? refs : "", refname) == -1) {
1290 err = got_error_from_errno("asprintf");
1298 /* Create got.conf(5). */
1299 gotconfig_path = got_repo_get_path_gotconfig(repo);
1300 if (gotconfig_path == NULL) {
1301 err = got_error_from_errno("got_repo_get_path_gotconfig");
1304 gotconfig_file = fopen(gotconfig_path, "ae");
1305 if (gotconfig_file == NULL) {
1306 err = got_error_from_errno2("fopen", gotconfig_path);
1309 if (asprintf(&gotconfig,
1314 "\trepository \"%s\"\n"
1320 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1321 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1322 remote_repo_path, branches ? "\tbranch { " : "",
1323 branches ? branches : "", branches ? "}\n" : "",
1324 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1325 mirror_references ? "\tmirror_references yes\n" : "",
1326 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1327 err = got_error_from_errno("asprintf");
1330 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1331 if (n != strlen(gotconfig)) {
1332 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1337 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1338 err = got_error_from_errno2("fclose", gotconfig_path);
1339 free(gotconfig_path);
1344 static const struct got_error *
1345 create_gitconfig(const char *git_url, const char *default_branch,
1346 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1347 struct got_pathlist_head *wanted_refs, int mirror_references,
1348 struct got_repository *repo)
1350 const struct got_error *err = NULL;
1351 char *gitconfig_path = NULL;
1352 char *gitconfig = NULL;
1353 FILE *gitconfig_file = NULL;
1354 char *branches = NULL, *refs = NULL;
1355 const char *branchname;
1358 /* Create a config file Git can understand. */
1359 gitconfig_path = got_repo_get_path_gitconfig(repo);
1360 if (gitconfig_path == NULL) {
1361 err = got_error_from_errno("got_repo_get_path_gitconfig");
1364 gitconfig_file = fopen(gitconfig_path, "ae");
1365 if (gitconfig_file == NULL) {
1366 err = got_error_from_errno2("fopen", gitconfig_path);
1369 if (fetch_all_branches) {
1370 if (mirror_references) {
1371 if (asprintf(&branches,
1372 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1373 err = got_error_from_errno("asprintf");
1376 } else if (asprintf(&branches,
1377 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1378 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1379 err = got_error_from_errno("asprintf");
1382 } else if (!TAILQ_EMPTY(wanted_branches)) {
1383 struct got_pathlist_entry *pe;
1384 TAILQ_FOREACH(pe, wanted_branches, entry) {
1386 branchname = pe->path;
1387 if (strncmp(branchname, "refs/heads/", 11) == 0)
1389 if (mirror_references) {
1391 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1392 branches ? branches : "",
1393 branchname, branchname) == -1) {
1394 err = got_error_from_errno("asprintf");
1397 } else if (asprintf(&s,
1398 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1399 branches ? branches : "",
1400 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1401 branchname) == -1) {
1402 err = got_error_from_errno("asprintf");
1410 * If the server specified a default branch, use just that one.
1411 * Otherwise fall back to fetching all branches on next fetch.
1413 if (default_branch) {
1414 branchname = default_branch;
1415 if (strncmp(branchname, "refs/heads/", 11) == 0)
1418 branchname = "*"; /* fall back to all branches */
1419 if (mirror_references) {
1420 if (asprintf(&branches,
1421 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1422 branchname, branchname) == -1) {
1423 err = got_error_from_errno("asprintf");
1426 } else if (asprintf(&branches,
1427 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1428 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1429 branchname) == -1) {
1430 err = got_error_from_errno("asprintf");
1434 if (!TAILQ_EMPTY(wanted_refs)) {
1435 struct got_pathlist_entry *pe;
1436 TAILQ_FOREACH(pe, wanted_refs, entry) {
1438 const char *refname = pe->path;
1439 if (strncmp(refname, "refs/", 5) == 0)
1441 if (mirror_references) {
1443 "%s\tfetch = refs/%s:refs/%s\n",
1444 refs ? refs : "", refname, refname) == -1) {
1445 err = got_error_from_errno("asprintf");
1448 } else if (asprintf(&s,
1449 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1451 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1453 err = got_error_from_errno("asprintf");
1461 if (asprintf(&gitconfig,
1466 "\tfetch = refs/tags/*:refs/tags/*\n",
1467 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1468 refs ? refs : "") == -1) {
1469 err = got_error_from_errno("asprintf");
1472 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1473 if (n != strlen(gitconfig)) {
1474 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1478 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1479 err = got_error_from_errno2("fclose", gitconfig_path);
1480 free(gitconfig_path);
1485 static const struct got_error *
1486 create_config_files(const char *proto, const char *host, const char *port,
1487 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1488 int mirror_references, struct got_pathlist_head *symrefs,
1489 struct got_pathlist_head *wanted_branches,
1490 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 const char *default_branch = NULL;
1494 struct got_pathlist_entry *pe;
1497 * If we asked for a set of wanted branches then use the first
1500 if (!TAILQ_EMPTY(wanted_branches)) {
1501 pe = TAILQ_FIRST(wanted_branches);
1502 default_branch = pe->path;
1504 /* First HEAD ref listed by server is the default branch. */
1505 TAILQ_FOREACH(pe, symrefs, entry) {
1506 const char *refname = pe->path;
1507 const char *target = pe->data;
1509 if (strcmp(refname, GOT_REF_HEAD) != 0)
1512 default_branch = target;
1517 /* Create got.conf(5). */
1518 err = create_gotconfig(proto, host, port, remote_repo_path,
1519 default_branch, fetch_all_branches, wanted_branches,
1520 wanted_refs, mirror_references, repo);
1524 /* Create a config file Git can understand. */
1525 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1526 wanted_branches, wanted_refs, mirror_references, repo);
1529 static const struct got_error *
1530 cmd_clone(int argc, char *argv[])
1532 const struct got_error *error = NULL;
1533 const char *uri, *dirname;
1534 char *proto, *host, *port, *repo_name, *server_path;
1535 char *default_destdir = NULL, *id_str = NULL;
1536 const char *repo_path;
1537 struct got_repository *repo = NULL;
1538 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1539 struct got_pathlist_entry *pe;
1540 struct got_object_id *pack_hash = NULL;
1541 int ch, fetchfd = -1, fetchstatus;
1542 pid_t fetchpid = -1;
1543 struct got_fetch_progress_arg fpa;
1544 char *git_url = NULL;
1545 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1546 int list_refs_only = 0;
1547 int *pack_fds = NULL;
1550 TAILQ_INIT(&symrefs);
1551 TAILQ_INIT(&wanted_branches);
1552 TAILQ_INIT(&wanted_refs);
1554 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1557 fetch_all_branches = 1;
1560 error = got_pathlist_append(&wanted_branches,
1569 mirror_references = 1;
1574 else if (verbosity < 3)
1581 error = got_pathlist_append(&wanted_refs,
1594 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1595 option_conflict('a', 'b');
1596 if (list_refs_only) {
1597 if (!TAILQ_EMPTY(&wanted_branches))
1598 option_conflict('l', 'b');
1599 if (fetch_all_branches)
1600 option_conflict('l', 'a');
1601 if (mirror_references)
1602 option_conflict('l', 'm');
1603 if (!TAILQ_EMPTY(&wanted_refs))
1604 option_conflict('l', 'R');
1616 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1621 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1622 host, port ? ":" : "", port ? port : "",
1623 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1624 error = got_error_from_errno("asprintf");
1628 if (strcmp(proto, "git") == 0) {
1630 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1631 "sendfd dns inet unveil", NULL) == -1)
1634 } else if (strcmp(proto, "git+ssh") == 0 ||
1635 strcmp(proto, "ssh") == 0) {
1637 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1638 "sendfd unveil", NULL) == -1)
1641 } else if (strcmp(proto, "http") == 0 ||
1642 strcmp(proto, "git+http") == 0) {
1643 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1646 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1649 if (dirname == NULL) {
1650 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1651 error = got_error_from_errno("asprintf");
1654 repo_path = default_destdir;
1656 repo_path = dirname;
1658 if (!list_refs_only) {
1659 error = got_path_mkdir(repo_path);
1661 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1662 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1664 if (!got_path_dir_is_empty(repo_path)) {
1665 error = got_error_path(repo_path,
1666 GOT_ERR_DIR_NOT_EMPTY);
1671 error = got_dial_apply_unveil(proto);
1675 error = apply_unveil(repo_path, 0, NULL);
1680 printf("Connecting to %s%s%s\n", host,
1681 port ? ":" : "", port ? port : "");
1683 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1684 server_path, verbosity);
1688 if (!list_refs_only) {
1689 error = got_repo_init(repo_path);
1692 error = got_repo_pack_fds_open(&pack_fds);
1695 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1700 fpa.last_scaled_size[0] = '\0';
1701 fpa.last_p_indexed = -1;
1702 fpa.last_p_resolved = -1;
1703 fpa.verbosity = verbosity;
1704 fpa.create_configs = 1;
1705 fpa.configs_created = 0;
1707 fpa.config_info.symrefs = &symrefs;
1708 fpa.config_info.wanted_branches = &wanted_branches;
1709 fpa.config_info.wanted_refs = &wanted_refs;
1710 fpa.config_info.proto = proto;
1711 fpa.config_info.host = host;
1712 fpa.config_info.port = port;
1713 fpa.config_info.remote_repo_path = server_path;
1714 fpa.config_info.git_url = git_url;
1715 fpa.config_info.fetch_all_branches = fetch_all_branches;
1716 fpa.config_info.mirror_references = mirror_references;
1717 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1718 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1719 fetch_all_branches, &wanted_branches, &wanted_refs,
1720 list_refs_only, verbosity, fetchfd, repo,
1721 fetch_progress, &fpa);
1725 if (list_refs_only) {
1726 error = list_remote_refs(&symrefs, &refs);
1730 if (pack_hash == NULL) {
1731 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1732 "server sent an empty pack file");
1735 error = got_object_id_str(&id_str, pack_hash);
1739 printf("\nFetched %s.pack\n", id_str);
1742 /* Set up references provided with the pack file. */
1743 TAILQ_FOREACH(pe, &refs, entry) {
1744 const char *refname = pe->path;
1745 struct got_object_id *id = pe->data;
1746 char *remote_refname;
1748 if (is_wanted_ref(&wanted_refs, refname) &&
1749 !mirror_references) {
1750 error = create_wanted_ref(refname, id,
1751 GOT_FETCH_DEFAULT_REMOTE_NAME,
1752 verbosity - 1, repo);
1758 error = create_ref(refname, id, verbosity - 1, repo);
1762 if (mirror_references)
1765 if (strncmp("refs/heads/", refname, 11) != 0)
1768 if (asprintf(&remote_refname,
1769 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1770 refname + 11) == -1) {
1771 error = got_error_from_errno("asprintf");
1774 error = create_ref(remote_refname, id, verbosity - 1, repo);
1775 free(remote_refname);
1780 /* Set the HEAD reference if the server provided one. */
1781 TAILQ_FOREACH(pe, &symrefs, entry) {
1782 struct got_reference *target_ref;
1783 const char *refname = pe->path;
1784 const char *target = pe->data;
1785 char *remote_refname = NULL, *remote_target = NULL;
1787 if (strcmp(refname, GOT_REF_HEAD) != 0)
1790 error = got_ref_open(&target_ref, repo, target, 0);
1792 if (error->code == GOT_ERR_NOT_REF) {
1799 error = create_symref(refname, target_ref, verbosity, repo);
1800 got_ref_close(target_ref);
1804 if (mirror_references)
1807 if (strncmp("refs/heads/", target, 11) != 0)
1810 if (asprintf(&remote_refname,
1811 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1813 error = got_error_from_errno("asprintf");
1816 if (asprintf(&remote_target,
1817 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1818 target + 11) == -1) {
1819 error = got_error_from_errno("asprintf");
1820 free(remote_refname);
1823 error = got_ref_open(&target_ref, repo, remote_target, 0);
1825 free(remote_refname);
1826 free(remote_target);
1827 if (error->code == GOT_ERR_NOT_REF) {
1833 error = create_symref(remote_refname, target_ref,
1834 verbosity - 1, repo);
1835 free(remote_refname);
1836 free(remote_target);
1837 got_ref_close(target_ref);
1843 * We failed to set the HEAD reference. If we asked for
1844 * a set of wanted branches use the first of one of those
1845 * which could be fetched instead.
1847 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1848 const char *target = pe->path;
1849 struct got_reference *target_ref;
1851 error = got_ref_open(&target_ref, repo, target, 0);
1853 if (error->code == GOT_ERR_NOT_REF) {
1860 error = create_symref(GOT_REF_HEAD, target_ref,
1862 got_ref_close(target_ref);
1870 printf("Created %s repository '%s'\n",
1871 mirror_references ? "mirrored" : "cloned", repo_path);
1874 const struct got_error *pack_err =
1875 got_repo_pack_fds_close(pack_fds);
1880 if (kill(fetchpid, SIGTERM) == -1)
1881 error = got_error_from_errno("kill");
1882 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1883 error = got_error_from_errno("waitpid");
1885 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1886 error = got_error_from_errno("close");
1888 const struct got_error *close_err = got_repo_close(repo);
1892 TAILQ_FOREACH(pe, &refs, entry) {
1893 free((void *)pe->path);
1896 got_pathlist_free(&refs);
1897 TAILQ_FOREACH(pe, &symrefs, entry) {
1898 free((void *)pe->path);
1901 got_pathlist_free(&symrefs);
1902 got_pathlist_free(&wanted_branches);
1903 got_pathlist_free(&wanted_refs);
1910 free(default_destdir);
1915 static const struct got_error *
1916 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1917 int replace_tags, int verbosity, struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 char *new_id_str = NULL;
1921 struct got_object_id *old_id = NULL;
1923 err = got_object_id_str(&new_id_str, new_id);
1927 if (!replace_tags &&
1928 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1929 err = got_ref_resolve(&old_id, repo, ref);
1932 if (got_object_id_cmp(old_id, new_id) == 0)
1934 if (verbosity >= 0) {
1935 printf("Rejecting update of existing tag %s: %s\n",
1936 got_ref_get_name(ref), new_id_str);
1941 if (got_ref_is_symbolic(ref)) {
1942 if (verbosity >= 0) {
1943 printf("Replacing reference %s: %s\n",
1944 got_ref_get_name(ref),
1945 got_ref_get_symref_target(ref));
1947 err = got_ref_change_symref_to_ref(ref, new_id);
1950 err = got_ref_write(ref, repo);
1954 err = got_ref_resolve(&old_id, repo, ref);
1957 if (got_object_id_cmp(old_id, new_id) == 0)
1960 err = got_ref_change_ref(ref, new_id);
1963 err = got_ref_write(ref, repo);
1969 printf("Updated %s: %s\n", got_ref_get_name(ref),
1977 static const struct got_error *
1978 update_symref(const char *refname, struct got_reference *target_ref,
1979 int verbosity, struct got_repository *repo)
1981 const struct got_error *err = NULL, *unlock_err;
1982 struct got_reference *symref;
1983 int symref_is_locked = 0;
1985 err = got_ref_open(&symref, repo, refname, 1);
1987 if (err->code != GOT_ERR_NOT_REF)
1989 err = got_ref_alloc_symref(&symref, refname, target_ref);
1993 err = got_ref_write(symref, repo);
1998 printf("Created reference %s: %s\n",
1999 got_ref_get_name(symref),
2000 got_ref_get_symref_target(symref));
2002 symref_is_locked = 1;
2004 if (strcmp(got_ref_get_symref_target(symref),
2005 got_ref_get_name(target_ref)) == 0)
2008 err = got_ref_change_symref(symref,
2009 got_ref_get_name(target_ref));
2013 err = got_ref_write(symref, repo);
2018 printf("Updated %s: %s\n", got_ref_get_name(symref),
2019 got_ref_get_symref_target(symref));
2023 if (symref_is_locked) {
2024 unlock_err = got_ref_unlock(symref);
2025 if (unlock_err && err == NULL)
2028 got_ref_close(symref);
2035 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2036 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2037 "[remote-repository-name]\n",
2042 static const struct got_error *
2043 delete_missing_ref(struct got_reference *ref,
2044 int verbosity, struct got_repository *repo)
2046 const struct got_error *err = NULL;
2047 struct got_object_id *id = NULL;
2048 char *id_str = NULL;
2050 if (got_ref_is_symbolic(ref)) {
2051 err = got_ref_delete(ref, repo);
2054 if (verbosity >= 0) {
2055 printf("Deleted %s: %s\n",
2056 got_ref_get_name(ref),
2057 got_ref_get_symref_target(ref));
2060 err = got_ref_resolve(&id, repo, ref);
2063 err = got_object_id_str(&id_str, id);
2067 err = got_ref_delete(ref, repo);
2070 if (verbosity >= 0) {
2071 printf("Deleted %s: %s\n",
2072 got_ref_get_name(ref), id_str);
2081 static const struct got_error *
2082 delete_missing_refs(struct got_pathlist_head *their_refs,
2083 struct got_pathlist_head *their_symrefs,
2084 const struct got_remote_repo *remote,
2085 int verbosity, struct got_repository *repo)
2087 const struct got_error *err = NULL, *unlock_err;
2088 struct got_reflist_head my_refs;
2089 struct got_reflist_entry *re;
2090 struct got_pathlist_entry *pe;
2091 char *remote_namespace = NULL;
2092 char *local_refname = NULL;
2094 TAILQ_INIT(&my_refs);
2096 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2098 return got_error_from_errno("asprintf");
2100 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2104 TAILQ_FOREACH(re, &my_refs, entry) {
2105 const char *refname = got_ref_get_name(re->ref);
2106 const char *their_refname;
2108 if (remote->mirror_references) {
2109 their_refname = refname;
2111 if (strncmp(refname, remote_namespace,
2112 strlen(remote_namespace)) == 0) {
2113 if (strcmp(refname + strlen(remote_namespace),
2116 if (asprintf(&local_refname, "refs/heads/%s",
2117 refname + strlen(remote_namespace)) == -1) {
2118 err = got_error_from_errno("asprintf");
2121 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2124 their_refname = local_refname;
2127 TAILQ_FOREACH(pe, their_refs, entry) {
2128 if (strcmp(their_refname, pe->path) == 0)
2134 TAILQ_FOREACH(pe, their_symrefs, entry) {
2135 if (strcmp(their_refname, pe->path) == 0)
2141 err = delete_missing_ref(re->ref, verbosity, repo);
2145 if (local_refname) {
2146 struct got_reference *ref;
2147 err = got_ref_open(&ref, repo, local_refname, 1);
2149 if (err->code != GOT_ERR_NOT_REF)
2151 free(local_refname);
2152 local_refname = NULL;
2155 err = delete_missing_ref(ref, verbosity, repo);
2158 unlock_err = got_ref_unlock(ref);
2160 if (unlock_err && err == NULL) {
2165 free(local_refname);
2166 local_refname = NULL;
2170 free(remote_namespace);
2171 free(local_refname);
2175 static const struct got_error *
2176 update_wanted_ref(const char *refname, struct got_object_id *id,
2177 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2179 const struct got_error *err, *unlock_err;
2180 char *remote_refname;
2181 struct got_reference *ref;
2183 if (strncmp("refs/", refname, 5) == 0)
2186 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2187 remote_repo_name, refname) == -1)
2188 return got_error_from_errno("asprintf");
2190 err = got_ref_open(&ref, repo, remote_refname, 1);
2192 if (err->code != GOT_ERR_NOT_REF)
2194 err = create_ref(remote_refname, id, verbosity, repo);
2196 err = update_ref(ref, id, 0, verbosity, repo);
2197 unlock_err = got_ref_unlock(ref);
2198 if (unlock_err && err == NULL)
2203 free(remote_refname);
2207 static const struct got_error *
2208 delete_ref(struct got_repository *repo, struct got_reference *ref)
2210 const struct got_error *err = NULL;
2211 struct got_object_id *id = NULL;
2212 char *id_str = NULL;
2215 if (got_ref_is_symbolic(ref)) {
2216 target = got_ref_get_symref_target(ref);
2218 err = got_ref_resolve(&id, repo, ref);
2221 err = got_object_id_str(&id_str, id);
2227 err = got_ref_delete(ref, repo);
2231 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2238 static const struct got_error *
2239 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2241 const struct got_error *err = NULL;
2242 struct got_reflist_head refs;
2243 struct got_reflist_entry *re;
2248 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2249 err = got_error_from_errno("asprintf");
2252 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2256 TAILQ_FOREACH(re, &refs, entry)
2257 delete_ref(repo, re->ref);
2259 got_ref_list_free(&refs);
2263 static const struct got_error *
2264 cmd_fetch(int argc, char *argv[])
2266 const struct got_error *error = NULL, *unlock_err;
2267 char *cwd = NULL, *repo_path = NULL;
2268 const char *remote_name;
2269 char *proto = NULL, *host = NULL, *port = NULL;
2270 char *repo_name = NULL, *server_path = NULL;
2271 const struct got_remote_repo *remotes, *remote = NULL;
2273 char *id_str = NULL;
2274 struct got_repository *repo = NULL;
2275 struct got_worktree *worktree = NULL;
2276 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2277 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2278 struct got_pathlist_entry *pe;
2279 struct got_object_id *pack_hash = NULL;
2280 int i, ch, fetchfd = -1, fetchstatus;
2281 pid_t fetchpid = -1;
2282 struct got_fetch_progress_arg fpa;
2283 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2284 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2285 int *pack_fds = NULL;
2288 TAILQ_INIT(&symrefs);
2289 TAILQ_INIT(&wanted_branches);
2290 TAILQ_INIT(&wanted_refs);
2292 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2295 fetch_all_branches = 1;
2298 error = got_pathlist_append(&wanted_branches,
2310 repo_path = realpath(optarg, NULL);
2311 if (repo_path == NULL)
2312 return got_error_from_errno2("realpath",
2314 got_path_strip_trailing_slashes(repo_path);
2322 else if (verbosity < 3)
2329 error = got_pathlist_append(&wanted_refs,
2345 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2346 option_conflict('a', 'b');
2347 if (list_refs_only) {
2348 if (!TAILQ_EMPTY(&wanted_branches))
2349 option_conflict('l', 'b');
2350 if (fetch_all_branches)
2351 option_conflict('l', 'a');
2353 option_conflict('l', 'd');
2355 option_conflict('l', 'X');
2357 if (delete_remote) {
2358 if (fetch_all_branches)
2359 option_conflict('X', 'a');
2360 if (!TAILQ_EMPTY(&wanted_branches))
2361 option_conflict('X', 'b');
2363 option_conflict('X', 'd');
2365 option_conflict('X', 't');
2366 if (!TAILQ_EMPTY(&wanted_refs))
2367 option_conflict('X', 'R');
2372 errx(1, "-X option requires a remote name");
2373 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2374 } else if (argc == 1)
2375 remote_name = argv[0];
2379 cwd = getcwd(NULL, 0);
2381 error = got_error_from_errno("getcwd");
2385 error = got_repo_pack_fds_open(&pack_fds);
2389 if (repo_path == NULL) {
2390 error = got_worktree_open(&worktree, cwd);
2391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2397 strdup(got_worktree_get_repo_path(worktree));
2398 if (repo_path == NULL)
2399 error = got_error_from_errno("strdup");
2403 repo_path = strdup(cwd);
2404 if (repo_path == NULL) {
2405 error = got_error_from_errno("strdup");
2411 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2415 if (delete_remote) {
2416 error = delete_refs_for_remote(repo, remote_name);
2417 goto done; /* nothing else to do */
2421 worktree_conf = got_worktree_get_gotconfig(worktree);
2422 if (worktree_conf) {
2423 got_gotconfig_get_remotes(&nremotes, &remotes,
2425 for (i = 0; i < nremotes; i++) {
2426 if (strcmp(remotes[i].name, remote_name) == 0) {
2427 remote = &remotes[i];
2433 if (remote == NULL) {
2434 repo_conf = got_repo_get_gotconfig(repo);
2436 got_gotconfig_get_remotes(&nremotes, &remotes,
2438 for (i = 0; i < nremotes; i++) {
2439 if (strcmp(remotes[i].name, remote_name) == 0) {
2440 remote = &remotes[i];
2446 if (remote == NULL) {
2447 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2448 for (i = 0; i < nremotes; i++) {
2449 if (strcmp(remotes[i].name, remote_name) == 0) {
2450 remote = &remotes[i];
2455 if (remote == NULL) {
2456 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2460 if (TAILQ_EMPTY(&wanted_branches)) {
2461 if (!fetch_all_branches)
2462 fetch_all_branches = remote->fetch_all_branches;
2463 for (i = 0; i < remote->nfetch_branches; i++) {
2464 got_pathlist_append(&wanted_branches,
2465 remote->fetch_branches[i], NULL);
2468 if (TAILQ_EMPTY(&wanted_refs)) {
2469 for (i = 0; i < remote->nfetch_refs; i++) {
2470 got_pathlist_append(&wanted_refs,
2471 remote->fetch_refs[i], NULL);
2475 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2476 &repo_name, remote->fetch_url);
2480 if (strcmp(proto, "git") == 0) {
2482 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2483 "sendfd dns inet unveil", NULL) == -1)
2486 } else if (strcmp(proto, "git+ssh") == 0 ||
2487 strcmp(proto, "ssh") == 0) {
2489 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2490 "sendfd unveil", NULL) == -1)
2493 } else if (strcmp(proto, "http") == 0 ||
2494 strcmp(proto, "git+http") == 0) {
2495 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2498 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2502 error = got_dial_apply_unveil(proto);
2506 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2511 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2512 port ? ":" : "", port ? port : "");
2514 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2515 server_path, verbosity);
2519 fpa.last_scaled_size[0] = '\0';
2520 fpa.last_p_indexed = -1;
2521 fpa.last_p_resolved = -1;
2522 fpa.verbosity = verbosity;
2524 fpa.create_configs = 0;
2525 fpa.configs_created = 0;
2526 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2527 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2528 remote->mirror_references, fetch_all_branches, &wanted_branches,
2529 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2530 fetch_progress, &fpa);
2534 if (list_refs_only) {
2535 error = list_remote_refs(&symrefs, &refs);
2539 if (pack_hash == NULL) {
2541 printf("Already up-to-date\n");
2542 } else if (verbosity >= 0) {
2543 error = got_object_id_str(&id_str, pack_hash);
2546 printf("\nFetched %s.pack\n", id_str);
2551 /* Update references provided with the pack file. */
2552 TAILQ_FOREACH(pe, &refs, entry) {
2553 const char *refname = pe->path;
2554 struct got_object_id *id = pe->data;
2555 struct got_reference *ref;
2556 char *remote_refname;
2558 if (is_wanted_ref(&wanted_refs, refname) &&
2559 !remote->mirror_references) {
2560 error = update_wanted_ref(refname, id,
2561 remote->name, verbosity, repo);
2567 if (remote->mirror_references ||
2568 strncmp("refs/tags/", refname, 10) == 0) {
2569 error = got_ref_open(&ref, repo, refname, 1);
2571 if (error->code != GOT_ERR_NOT_REF)
2573 error = create_ref(refname, id, verbosity,
2578 error = update_ref(ref, id, replace_tags,
2580 unlock_err = got_ref_unlock(ref);
2581 if (unlock_err && error == NULL)
2587 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2588 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2589 remote_name, refname + 11) == -1) {
2590 error = got_error_from_errno("asprintf");
2594 error = got_ref_open(&ref, repo, remote_refname, 1);
2596 if (error->code != GOT_ERR_NOT_REF)
2598 error = create_ref(remote_refname, id,
2603 error = update_ref(ref, id, replace_tags,
2605 unlock_err = got_ref_unlock(ref);
2606 if (unlock_err && error == NULL)
2613 /* Also create a local branch if none exists yet. */
2614 error = got_ref_open(&ref, repo, refname, 1);
2616 if (error->code != GOT_ERR_NOT_REF)
2618 error = create_ref(refname, id, verbosity,
2623 unlock_err = got_ref_unlock(ref);
2624 if (unlock_err && error == NULL)
2631 error = delete_missing_refs(&refs, &symrefs, remote,
2637 if (!remote->mirror_references) {
2638 /* Update remote HEAD reference if the server provided one. */
2639 TAILQ_FOREACH(pe, &symrefs, entry) {
2640 struct got_reference *target_ref;
2641 const char *refname = pe->path;
2642 const char *target = pe->data;
2643 char *remote_refname = NULL, *remote_target = NULL;
2645 if (strcmp(refname, GOT_REF_HEAD) != 0)
2648 if (strncmp("refs/heads/", target, 11) != 0)
2651 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2652 remote->name, refname) == -1) {
2653 error = got_error_from_errno("asprintf");
2656 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2657 remote->name, target + 11) == -1) {
2658 error = got_error_from_errno("asprintf");
2659 free(remote_refname);
2663 error = got_ref_open(&target_ref, repo, remote_target,
2666 free(remote_refname);
2667 free(remote_target);
2668 if (error->code == GOT_ERR_NOT_REF) {
2674 error = update_symref(remote_refname, target_ref,
2676 free(remote_refname);
2677 free(remote_target);
2678 got_ref_close(target_ref);
2685 if (kill(fetchpid, SIGTERM) == -1)
2686 error = got_error_from_errno("kill");
2687 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2688 error = got_error_from_errno("waitpid");
2690 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2691 error = got_error_from_errno("close");
2693 const struct got_error *close_err = got_repo_close(repo);
2698 got_worktree_close(worktree);
2700 const struct got_error *pack_err =
2701 got_repo_pack_fds_close(pack_fds);
2705 TAILQ_FOREACH(pe, &refs, entry) {
2706 free((void *)pe->path);
2709 got_pathlist_free(&refs);
2710 TAILQ_FOREACH(pe, &symrefs, entry) {
2711 free((void *)pe->path);
2714 got_pathlist_free(&symrefs);
2715 got_pathlist_free(&wanted_branches);
2716 got_pathlist_free(&wanted_refs);
2731 usage_checkout(void)
2733 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2734 "[-p prefix] [-q] repository-path [worktree-path]\n",
2740 show_worktree_base_ref_warning(void)
2742 fprintf(stderr, "%s: warning: could not create a reference "
2743 "to the work tree's base commit; the commit could be "
2744 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2745 "repository writable and running 'got update' will prevent this\n",
2749 struct got_checkout_progress_arg {
2750 const char *worktree_path;
2751 int had_base_commit_ref_error;
2755 static const struct got_error *
2756 checkout_progress(void *arg, unsigned char status, const char *path)
2758 struct got_checkout_progress_arg *a = arg;
2760 /* Base commit bump happens silently. */
2761 if (status == GOT_STATUS_BUMP_BASE)
2764 if (status == GOT_STATUS_BASE_REF_ERR) {
2765 a->had_base_commit_ref_error = 1;
2769 while (path[0] == '/')
2772 if (a->verbosity >= 0)
2773 printf("%c %s/%s\n", status, a->worktree_path, path);
2778 static const struct got_error *
2779 check_cancelled(void *arg)
2781 if (sigint_received || sigpipe_received)
2782 return got_error(GOT_ERR_CANCELLED);
2786 static const struct got_error *
2787 check_linear_ancestry(struct got_object_id *commit_id,
2788 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2789 struct got_repository *repo)
2791 const struct got_error *err = NULL;
2792 struct got_object_id *yca_id;
2794 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2795 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2800 return got_error(GOT_ERR_ANCESTRY);
2803 * Require a straight line of history between the target commit
2804 * and the work tree's base commit.
2806 * Non-linear situations such as this require a rebase:
2808 * (commit) D F (base_commit)
2816 * 'got update' only handles linear cases:
2817 * Update forwards in time: A (base/yca) - B - C - D (commit)
2818 * Update backwards in time: D (base) - C - B - A (commit/yca)
2820 if (allow_forwards_in_time_only) {
2821 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2822 return got_error(GOT_ERR_ANCESTRY);
2823 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2824 got_object_id_cmp(base_commit_id, yca_id) != 0)
2825 return got_error(GOT_ERR_ANCESTRY);
2831 static const struct got_error *
2832 check_same_branch(struct got_object_id *commit_id,
2833 struct got_reference *head_ref, struct got_object_id *yca_id,
2834 struct got_repository *repo)
2836 const struct got_error *err = NULL;
2837 struct got_commit_graph *graph = NULL;
2838 struct got_object_id *head_commit_id = NULL;
2839 int is_same_branch = 0;
2841 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2845 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2849 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2854 err = got_commit_graph_open(&graph, "/", 1);
2858 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2859 check_cancelled, NULL);
2864 struct got_object_id *id;
2865 err = got_commit_graph_iter_next(&id, graph, repo,
2866 check_cancelled, NULL);
2868 if (err->code == GOT_ERR_ITER_COMPLETED)
2874 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2876 if (got_object_id_cmp(id, commit_id) == 0) {
2884 got_commit_graph_close(graph);
2885 free(head_commit_id);
2886 if (!err && !is_same_branch)
2887 err = got_error(GOT_ERR_ANCESTRY);
2891 static const struct got_error *
2892 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2894 static char msg[512];
2895 const char *branch_name;
2897 if (got_ref_is_symbolic(ref))
2898 branch_name = got_ref_get_symref_target(ref);
2900 branch_name = got_ref_get_name(ref);
2902 if (strncmp("refs/heads/", branch_name, 11) == 0)
2905 snprintf(msg, sizeof(msg),
2906 "target commit is not contained in branch '%s'; "
2907 "the branch to use must be specified with -b; "
2908 "if necessary a new branch can be created for "
2909 "this commit with 'got branch -c %s BRANCH_NAME'",
2910 branch_name, commit_id_str);
2912 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2915 static const struct got_error *
2916 cmd_checkout(int argc, char *argv[])
2918 const struct got_error *error = NULL;
2919 struct got_repository *repo = NULL;
2920 struct got_reference *head_ref = NULL, *ref = NULL;
2921 struct got_worktree *worktree = NULL;
2922 char *repo_path = NULL;
2923 char *worktree_path = NULL;
2924 const char *path_prefix = "";
2925 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2926 char *commit_id_str = NULL;
2927 struct got_object_id *commit_id = NULL;
2929 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2930 struct got_pathlist_head paths;
2931 struct got_checkout_progress_arg cpa;
2932 int *pack_fds = NULL;
2936 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2939 branch_name = optarg;
2942 commit_id_str = strdup(optarg);
2943 if (commit_id_str == NULL)
2944 return got_error_from_errno("strdup");
2950 path_prefix = optarg;
2965 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2966 "unveil", NULL) == -1)
2970 char *base, *dotgit;
2972 repo_path = realpath(argv[0], NULL);
2973 if (repo_path == NULL)
2974 return got_error_from_errno2("realpath", argv[0]);
2975 cwd = getcwd(NULL, 0);
2977 error = got_error_from_errno("getcwd");
2984 error = got_path_basename(&base, path);
2987 dotgit = strstr(base, ".git");
2990 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2991 error = got_error_from_errno("asprintf");
2996 } else if (argc == 2) {
2997 repo_path = realpath(argv[0], NULL);
2998 if (repo_path == NULL) {
2999 error = got_error_from_errno2("realpath", argv[0]);
3002 worktree_path = realpath(argv[1], NULL);
3003 if (worktree_path == NULL) {
3004 if (errno != ENOENT) {
3005 error = got_error_from_errno2("realpath",
3009 worktree_path = strdup(argv[1]);
3010 if (worktree_path == NULL) {
3011 error = got_error_from_errno("strdup");
3018 got_path_strip_trailing_slashes(repo_path);
3019 got_path_strip_trailing_slashes(worktree_path);
3021 error = got_repo_pack_fds_open(&pack_fds);
3025 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3029 /* Pre-create work tree path for unveil(2) */
3030 error = got_path_mkdir(worktree_path);
3032 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3033 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3035 if (!allow_nonempty &&
3036 !got_path_dir_is_empty(worktree_path)) {
3037 error = got_error_path(worktree_path,
3038 GOT_ERR_DIR_NOT_EMPTY);
3043 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3047 error = got_ref_open(&head_ref, repo, branch_name, 0);
3051 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3052 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3055 error = got_worktree_open(&worktree, worktree_path);
3059 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3063 if (!same_path_prefix) {
3064 error = got_error(GOT_ERR_PATH_PREFIX);
3068 if (commit_id_str) {
3069 struct got_reflist_head refs;
3071 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3075 error = got_repo_match_object_id(&commit_id, NULL,
3076 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3077 got_ref_list_free(&refs);
3080 error = check_linear_ancestry(commit_id,
3081 got_worktree_get_base_commit_id(worktree), 0, repo);
3082 if (error != NULL) {
3083 if (error->code == GOT_ERR_ANCESTRY) {
3084 error = checkout_ancestry_error(
3085 head_ref, commit_id_str);
3089 error = check_same_branch(commit_id, head_ref, NULL, repo);
3091 if (error->code == GOT_ERR_ANCESTRY) {
3092 error = checkout_ancestry_error(
3093 head_ref, commit_id_str);
3097 error = got_worktree_set_base_commit_id(worktree, repo,
3101 /* Expand potentially abbreviated commit ID string. */
3102 free(commit_id_str);
3103 error = got_object_id_str(&commit_id_str, commit_id);
3107 commit_id = got_object_id_dup(
3108 got_worktree_get_base_commit_id(worktree));
3109 if (commit_id == NULL) {
3110 error = got_error_from_errno("got_object_id_dup");
3113 error = got_object_id_str(&commit_id_str, commit_id);
3118 error = got_pathlist_append(&paths, "", NULL);
3121 cpa.worktree_path = worktree_path;
3122 cpa.had_base_commit_ref_error = 0;
3123 cpa.verbosity = verbosity;
3124 error = got_worktree_checkout_files(worktree, &paths, repo,
3125 checkout_progress, &cpa, check_cancelled, NULL);
3129 if (got_ref_is_symbolic(head_ref)) {
3130 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3133 refname = got_ref_get_name(ref);
3135 refname = got_ref_get_name(head_ref);
3136 printf("Checked out %s: %s\n", refname, commit_id_str);
3137 printf("Now shut up and hack\n");
3138 if (cpa.had_base_commit_ref_error)
3139 show_worktree_base_ref_warning();
3142 const struct got_error *pack_err =
3143 got_repo_pack_fds_close(pack_fds);
3148 got_ref_close(head_ref);
3151 got_pathlist_free(&paths);
3152 free(commit_id_str);
3155 free(worktree_path);
3160 struct got_update_progress_arg {
3172 print_update_progress_stats(struct got_update_progress_arg *upa)
3174 if (!upa->did_something)
3177 if (upa->conflicts > 0)
3178 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3179 if (upa->obstructed > 0)
3180 printf("File paths obstructed by a non-regular file: %d\n",
3182 if (upa->not_updated > 0)
3183 printf("Files not updated because of existing merge "
3184 "conflicts: %d\n", upa->not_updated);
3188 * The meaning of some status codes differs between merge-style operations and
3189 * update operations. For example, the ! status code means "file was missing"
3190 * if changes were merged into the work tree, and "missing file was restored"
3191 * if the work tree was updated. This function should be used by any operation
3192 * which merges changes into the work tree without updating the work tree.
3195 print_merge_progress_stats(struct got_update_progress_arg *upa)
3197 if (!upa->did_something)
3200 if (upa->conflicts > 0)
3201 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3202 if (upa->obstructed > 0)
3203 printf("File paths obstructed by a non-regular file: %d\n",
3205 if (upa->missing > 0)
3206 printf("Files which had incoming changes but could not be "
3207 "found in the work tree: %d\n", upa->missing);
3208 if (upa->not_deleted > 0)
3209 printf("Files not deleted due to differences in deleted "
3210 "content: %d\n", upa->not_deleted);
3211 if (upa->unversioned > 0)
3212 printf("Files not merged because an unversioned file was "
3213 "found in the work tree: %d\n", upa->unversioned);
3219 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3225 static const struct got_error *
3226 update_progress(void *arg, unsigned char status, const char *path)
3228 struct got_update_progress_arg *upa = arg;
3230 if (status == GOT_STATUS_EXISTS ||
3231 status == GOT_STATUS_BASE_REF_ERR)
3234 upa->did_something = 1;
3236 /* Base commit bump happens silently. */
3237 if (status == GOT_STATUS_BUMP_BASE)
3240 if (status == GOT_STATUS_CONFLICT)
3242 if (status == GOT_STATUS_OBSTRUCTED)
3244 if (status == GOT_STATUS_CANNOT_UPDATE)
3246 if (status == GOT_STATUS_MISSING)
3248 if (status == GOT_STATUS_CANNOT_DELETE)
3250 if (status == GOT_STATUS_UNVERSIONED)
3253 while (path[0] == '/')
3255 if (upa->verbosity >= 0)
3256 printf("%c %s\n", status, path);
3261 static const struct got_error *
3262 switch_head_ref(struct got_reference *head_ref,
3263 struct got_object_id *commit_id, struct got_worktree *worktree,
3264 struct got_repository *repo)
3266 const struct got_error *err = NULL;
3268 int ref_has_moved = 0;
3270 /* Trivial case: switching between two different references. */
3271 if (strcmp(got_ref_get_name(head_ref),
3272 got_worktree_get_head_ref_name(worktree)) != 0) {
3273 printf("Switching work tree from %s to %s\n",
3274 got_worktree_get_head_ref_name(worktree),
3275 got_ref_get_name(head_ref));
3276 return got_worktree_set_head_ref(worktree, head_ref);
3279 err = check_linear_ancestry(commit_id,
3280 got_worktree_get_base_commit_id(worktree), 0, repo);
3282 if (err->code != GOT_ERR_ANCESTRY)
3289 /* Switching to a rebased branch with the same reference name. */
3290 err = got_object_id_str(&base_id_str,
3291 got_worktree_get_base_commit_id(worktree));
3294 printf("Reference %s now points at a different branch\n",
3295 got_worktree_get_head_ref_name(worktree));
3296 printf("Switching work tree from %s to %s\n", base_id_str,
3297 got_worktree_get_head_ref_name(worktree));
3301 static const struct got_error *
3302 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3304 const struct got_error *err;
3307 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3311 return got_error(GOT_ERR_REBASING);
3313 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3317 return got_error(GOT_ERR_HISTEDIT_BUSY);
3322 static const struct got_error *
3323 check_merge_in_progress(struct got_worktree *worktree,
3324 struct got_repository *repo)
3326 const struct got_error *err;
3329 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3333 return got_error(GOT_ERR_MERGE_BUSY);
3338 static const struct got_error *
3339 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3340 char *argv[], struct got_worktree *worktree)
3342 const struct got_error *err = NULL;
3344 struct got_pathlist_entry *new;
3350 return got_error_from_errno("strdup");
3351 return got_pathlist_append(paths, path, NULL);
3354 for (i = 0; i < argc; i++) {
3355 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3358 err = got_pathlist_insert(&new, paths, path, NULL);
3359 if (err || new == NULL /* duplicate */) {
3369 static const struct got_error *
3370 wrap_not_worktree_error(const struct got_error *orig_err,
3371 const char *cmdname, const char *path)
3373 const struct got_error *err;
3374 struct got_repository *repo;
3375 static char msg[512];
3376 int *pack_fds = NULL;
3378 err = got_repo_pack_fds_open(&pack_fds);
3382 err = got_repo_open(&repo, path, NULL, pack_fds);
3386 snprintf(msg, sizeof(msg),
3387 "'got %s' needs a work tree in addition to a git repository\n"
3388 "Work trees can be checked out from this Git repository with "
3390 "The got(1) manual page contains more information.", cmdname);
3391 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3392 got_repo_close(repo);
3394 const struct got_error *pack_err =
3395 got_repo_pack_fds_close(pack_fds);
3402 static const struct got_error *
3403 cmd_update(int argc, char *argv[])
3405 const struct got_error *error = NULL;
3406 struct got_repository *repo = NULL;
3407 struct got_worktree *worktree = NULL;
3408 char *worktree_path = NULL;
3409 struct got_object_id *commit_id = NULL;
3410 char *commit_id_str = NULL;
3411 const char *branch_name = NULL;
3412 struct got_reference *head_ref = NULL;
3413 struct got_pathlist_head paths;
3414 struct got_pathlist_entry *pe;
3415 int ch, verbosity = 0;
3416 struct got_update_progress_arg upa;
3417 int *pack_fds = NULL;
3421 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3424 branch_name = optarg;
3427 commit_id_str = strdup(optarg);
3428 if (commit_id_str == NULL)
3429 return got_error_from_errno("strdup");
3444 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3445 "unveil", NULL) == -1)
3448 worktree_path = getcwd(NULL, 0);
3449 if (worktree_path == NULL) {
3450 error = got_error_from_errno("getcwd");
3454 error = got_repo_pack_fds_open(&pack_fds);
3458 error = got_worktree_open(&worktree, worktree_path);
3460 if (error->code == GOT_ERR_NOT_WORKTREE)
3461 error = wrap_not_worktree_error(error, "update",
3466 error = check_rebase_or_histedit_in_progress(worktree);
3470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3475 error = apply_unveil(got_repo_get_path(repo), 0,
3476 got_worktree_get_root_path(worktree));
3480 error = check_merge_in_progress(worktree, repo);
3484 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3488 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3489 got_worktree_get_head_ref_name(worktree), 0);
3492 if (commit_id_str == NULL) {
3493 error = got_ref_resolve(&commit_id, repo, head_ref);
3496 error = got_object_id_str(&commit_id_str, commit_id);
3500 struct got_reflist_head refs;
3502 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3506 error = got_repo_match_object_id(&commit_id, NULL,
3507 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3508 got_ref_list_free(&refs);
3509 free(commit_id_str);
3510 commit_id_str = NULL;
3513 error = got_object_id_str(&commit_id_str, commit_id);
3519 struct got_object_id *head_commit_id;
3520 TAILQ_FOREACH(pe, &paths, entry) {
3521 if (pe->path_len == 0)
3523 error = got_error_msg(GOT_ERR_BAD_PATH,
3524 "switching between branches requires that "
3525 "the entire work tree gets updated");
3528 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3531 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3533 free(head_commit_id);
3536 error = check_same_branch(commit_id, head_ref, NULL, repo);
3539 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3543 error = check_linear_ancestry(commit_id,
3544 got_worktree_get_base_commit_id(worktree), 0, repo);
3545 if (error != NULL) {
3546 if (error->code == GOT_ERR_ANCESTRY)
3547 error = got_error(GOT_ERR_BRANCH_MOVED);
3550 error = check_same_branch(commit_id, head_ref, NULL, repo);
3555 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3557 error = got_worktree_set_base_commit_id(worktree, repo,
3563 memset(&upa, 0, sizeof(upa));
3564 upa.verbosity = verbosity;
3565 error = got_worktree_checkout_files(worktree, &paths, repo,
3566 update_progress, &upa, check_cancelled, NULL);
3570 if (upa.did_something) {
3571 printf("Updated to %s: %s\n",
3572 got_worktree_get_head_ref_name(worktree), commit_id_str);
3574 printf("Already up-to-date\n");
3576 print_update_progress_stats(&upa);
3579 const struct got_error *pack_err =
3580 got_repo_pack_fds_close(pack_fds);
3584 free(worktree_path);
3585 TAILQ_FOREACH(pe, &paths, entry)
3586 free((char *)pe->path);
3587 got_pathlist_free(&paths);
3589 free(commit_id_str);
3593 static const struct got_error *
3594 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3595 const char *path, int diff_context, int ignore_whitespace,
3596 int force_text_diff, struct got_repository *repo, FILE *outfile)
3598 const struct got_error *err = NULL;
3599 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3600 FILE *f1 = NULL, *f2 = NULL;
3601 int fd1 = -1, fd2 = -1;
3603 fd1 = got_opentempfd();
3605 return got_error_from_errno("got_opentempfd");
3606 fd2 = got_opentempfd();
3608 err = got_error_from_errno("got_opentempfd");
3613 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3619 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3623 f1 = got_opentemp();
3625 err = got_error_from_errno("got_opentemp");
3628 f2 = got_opentemp();
3630 err = got_error_from_errno("got_opentemp");
3634 while (path[0] == '/')
3636 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3637 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3638 force_text_diff, outfile);
3640 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3641 err = got_error_from_errno("close");
3643 got_object_blob_close(blob1);
3644 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3645 err = got_error_from_errno("close");
3646 got_object_blob_close(blob2);
3647 if (f1 && fclose(f1) == EOF && err == NULL)
3648 err = got_error_from_errno("fclose");
3649 if (f2 && fclose(f2) == EOF && err == NULL)
3650 err = got_error_from_errno("fclose");
3654 static const struct got_error *
3655 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3656 const char *path, int diff_context, int ignore_whitespace,
3657 int force_text_diff, struct got_repository *repo, FILE *outfile)
3659 const struct got_error *err = NULL;
3660 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3661 struct got_diff_blob_output_unidiff_arg arg;
3662 FILE *f1 = NULL, *f2 = NULL;
3663 int fd1 = -1, fd2 = -1;
3666 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3669 fd1 = got_opentempfd();
3671 err = got_error_from_errno("got_opentempfd");
3676 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3680 f1 = got_opentemp();
3682 err = got_error_from_errno("got_opentemp");
3686 f2 = got_opentemp();
3688 err = got_error_from_errno("got_opentemp");
3691 fd2 = got_opentempfd();
3693 err = got_error_from_errno("got_opentempfd");
3696 arg.diff_context = diff_context;
3697 arg.ignore_whitespace = ignore_whitespace;
3698 arg.force_text_diff = force_text_diff;
3699 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3700 arg.outfile = outfile;
3701 arg.line_offsets = NULL;
3703 while (path[0] == '/')
3705 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3706 got_diff_blob_output_unidiff, &arg, 1);
3709 got_object_tree_close(tree1);
3711 got_object_tree_close(tree2);
3712 if (f1 && fclose(f1) == EOF && err == NULL)
3713 err = got_error_from_errno("fclose");
3714 if (f2 && fclose(f2) == EOF && err == NULL)
3715 err = got_error_from_errno("fclose");
3716 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3717 err = got_error_from_errno("close");
3718 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3719 err = got_error_from_errno("close");
3723 static const struct got_error *
3724 get_changed_paths(struct got_pathlist_head *paths,
3725 struct got_commit_object *commit, struct got_repository *repo)
3727 const struct got_error *err = NULL;
3728 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3729 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3730 struct got_object_qid *qid;
3732 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3734 struct got_commit_object *pcommit;
3735 err = got_object_open_as_commit(&pcommit, repo,
3740 tree_id1 = got_object_id_dup(
3741 got_object_commit_get_tree_id(pcommit));
3742 if (tree_id1 == NULL) {
3743 got_object_commit_close(pcommit);
3744 return got_error_from_errno("got_object_id_dup");
3746 got_object_commit_close(pcommit);
3751 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3756 tree_id2 = got_object_commit_get_tree_id(commit);
3757 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3761 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3762 got_diff_tree_collect_changed_paths, paths, 0);
3765 got_object_tree_close(tree1);
3767 got_object_tree_close(tree2);
3772 static const struct got_error *
3773 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3774 const char *path, int diff_context, struct got_repository *repo,
3777 const struct got_error *err = NULL;
3778 struct got_commit_object *pcommit = NULL;
3779 char *id_str1 = NULL, *id_str2 = NULL;
3780 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3781 struct got_object_qid *qid;
3783 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3785 err = got_object_open_as_commit(&pcommit, repo,
3789 err = got_object_id_str(&id_str1, &qid->id);
3794 err = got_object_id_str(&id_str2, id);
3798 if (path && path[0] != '\0') {
3800 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3804 err = got_object_id_by_path(&obj_id1, repo,
3807 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3813 err = got_object_get_type(&obj_type, repo, obj_id2);
3819 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3820 fprintf(outfile, "commit - %s\n",
3821 id_str1 ? id_str1 : "/dev/null");
3822 fprintf(outfile, "commit + %s\n", id_str2);
3824 case GOT_OBJ_TYPE_BLOB:
3825 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3826 0, 0, repo, outfile);
3828 case GOT_OBJ_TYPE_TREE:
3829 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3830 0, 0, repo, outfile);
3833 err = got_error(GOT_ERR_OBJ_TYPE);
3839 obj_id2 = got_object_commit_get_tree_id(commit);
3841 obj_id1 = got_object_commit_get_tree_id(pcommit);
3843 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3844 fprintf(outfile, "commit - %s\n",
3845 id_str1 ? id_str1 : "/dev/null");
3846 fprintf(outfile, "commit + %s\n", id_str2);
3847 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3854 got_object_commit_close(pcommit);
3859 get_datestr(time_t *time, char *datebuf)
3861 struct tm mytm, *tm;
3864 tm = gmtime_r(time, &mytm);
3867 s = asctime_r(tm, datebuf);
3870 p = strchr(s, '\n');
3876 static const struct got_error *
3877 match_commit(int *have_match, struct got_object_id *id,
3878 struct got_commit_object *commit, regex_t *regex)
3880 const struct got_error *err = NULL;
3881 regmatch_t regmatch;
3882 char *id_str = NULL, *logmsg = NULL;
3886 err = got_object_id_str(&id_str, id);
3890 err = got_object_commit_get_logmsg(&logmsg, commit);
3894 if (regexec(regex, got_object_commit_get_author(commit), 1,
3895 ®match, 0) == 0 ||
3896 regexec(regex, got_object_commit_get_committer(commit), 1,
3897 ®match, 0) == 0 ||
3898 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3899 regexec(regex, logmsg, 1, ®match, 0) == 0)
3908 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3911 regmatch_t regmatch;
3912 struct got_pathlist_entry *pe;
3916 TAILQ_FOREACH(pe, changed_paths, entry) {
3917 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3924 static const struct got_error *
3925 match_patch(int *have_match, struct got_commit_object *commit,
3926 struct got_object_id *id, const char *path, int diff_context,
3927 struct got_repository *repo, regex_t *regex, FILE *f)
3929 const struct got_error *err = NULL;
3931 size_t linesize = 0;
3933 regmatch_t regmatch;
3937 err = got_opentemp_truncate(f);
3941 err = print_patch(commit, id, path, diff_context, repo, f);
3945 if (fseeko(f, 0L, SEEK_SET) == -1) {
3946 err = got_error_from_errno("fseeko");
3950 while ((linelen = getline(&line, &linesize, f)) != -1) {
3951 if (regexec(regex, line, 1, ®match, 0) == 0) {
3961 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3963 static const struct got_error*
3964 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3965 struct got_object_id *id, struct got_repository *repo,
3968 static const struct got_error *err = NULL;
3969 struct got_reflist_entry *re;
3975 TAILQ_FOREACH(re, refs, entry) {
3976 struct got_tag_object *tag = NULL;
3977 struct got_object_id *ref_id;
3980 name = got_ref_get_name(re->ref);
3981 if (strcmp(name, GOT_REF_HEAD) == 0)
3983 if (strncmp(name, "refs/", 5) == 0)
3985 if (strncmp(name, "got/", 4) == 0)
3987 if (strncmp(name, "heads/", 6) == 0)
3989 if (strncmp(name, "remotes/", 8) == 0) {
3993 s = strstr(name, "/" GOT_REF_HEAD);
3994 if (s != NULL && s[strlen(s)] == '\0')
3997 err = got_ref_resolve(&ref_id, repo, re->ref);
4000 if (strncmp(name, "tags/", 5) == 0) {
4001 err = got_object_open_as_tag(&tag, repo, ref_id);
4003 if (err->code != GOT_ERR_OBJ_TYPE) {
4007 /* Ref points at something other than a tag. */
4012 cmp = got_object_id_cmp(tag ?
4013 got_object_tag_get_object_id(tag) : ref_id, id);
4016 got_object_tag_close(tag);
4020 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4021 s ? ", " : "", name) == -1) {
4022 err = got_error_from_errno("asprintf");
4033 static const struct got_error *
4034 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4035 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4037 const struct got_error *err = NULL;
4038 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4039 char *comma, *s, *nl;
4040 struct got_reflist_head *refs;
4041 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4043 time_t committer_time;
4045 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4047 err = build_refs_str(&ref_str, refs, id, repo, 1);
4051 /* Display the first matching ref only. */
4052 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4056 if (ref_str == NULL) {
4057 err = got_object_id_str(&id_str, id);
4062 committer_time = got_object_commit_get_committer_time(commit);
4063 if (gmtime_r(&committer_time, &tm) == NULL) {
4064 err = got_error_from_errno("gmtime_r");
4067 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4068 err = got_error(GOT_ERR_NO_SPACE);
4072 err = got_object_commit_get_logmsg(&logmsg0, commit);
4077 while (isspace((unsigned char)s[0]))
4080 nl = strchr(s, '\n');
4086 printf("%s%-7s %s\n", datebuf, ref_str, s);
4088 printf("%s%.7s %s\n", datebuf, id_str, s);
4090 if (fflush(stdout) != 0 && err == NULL)
4091 err = got_error_from_errno("fflush");
4099 static const struct got_error *
4100 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4101 struct got_repository *repo, const char *path,
4102 struct got_pathlist_head *changed_paths, int show_patch,
4103 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4104 const char *custom_refs_str)
4106 const struct got_error *err = NULL;
4107 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4109 time_t committer_time;
4110 const char *author, *committer;
4111 char *refs_str = NULL;
4113 err = got_object_id_str(&id_str, id);
4117 if (custom_refs_str == NULL) {
4118 struct got_reflist_head *refs;
4119 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4121 err = build_refs_str(&refs_str, refs, id, repo, 0);
4127 printf(GOT_COMMIT_SEP_STR);
4128 if (custom_refs_str)
4129 printf("commit %s (%s)\n", id_str, custom_refs_str);
4131 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4132 refs_str ? refs_str : "", refs_str ? ")" : "");
4137 printf("from: %s\n", got_object_commit_get_author(commit));
4138 committer_time = got_object_commit_get_committer_time(commit);
4139 datestr = get_datestr(&committer_time, datebuf);
4141 printf("date: %s UTC\n", datestr);
4142 author = got_object_commit_get_author(commit);
4143 committer = got_object_commit_get_committer(commit);
4144 if (strcmp(author, committer) != 0)
4145 printf("via: %s\n", committer);
4146 if (got_object_commit_get_nparents(commit) > 1) {
4147 const struct got_object_id_queue *parent_ids;
4148 struct got_object_qid *qid;
4150 parent_ids = got_object_commit_get_parent_ids(commit);
4151 STAILQ_FOREACH(qid, parent_ids, entry) {
4152 err = got_object_id_str(&id_str, &qid->id);
4155 printf("parent %d: %s\n", n++, id_str);
4161 err = got_object_commit_get_logmsg(&logmsg0, commit);
4167 line = strsep(&logmsg, "\n");
4169 printf(" %s\n", line);
4173 if (changed_paths) {
4174 struct got_pathlist_entry *pe;
4175 TAILQ_FOREACH(pe, changed_paths, entry) {
4176 struct got_diff_changed_path *cp = pe->data;
4177 printf(" %c %s\n", cp->status, pe->path);
4182 err = print_patch(commit, id, path, diff_context, repo, stdout);
4187 if (fflush(stdout) != 0 && err == NULL)
4188 err = got_error_from_errno("fflush");
4195 static const struct got_error *
4196 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4197 struct got_repository *repo, const char *path, int show_changed_paths,
4198 int show_patch, const char *search_pattern, int diff_context, int limit,
4199 int log_branches, int reverse_display_order,
4200 struct got_reflist_object_id_map *refs_idmap, int one_line,
4203 const struct got_error *err;
4204 struct got_commit_graph *graph;
4207 struct got_object_id_queue reversed_commits;
4208 struct got_object_qid *qid;
4209 struct got_commit_object *commit;
4210 struct got_pathlist_head changed_paths;
4211 struct got_pathlist_entry *pe;
4213 STAILQ_INIT(&reversed_commits);
4214 TAILQ_INIT(&changed_paths);
4216 if (search_pattern && regcomp(®ex, search_pattern,
4217 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4218 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4220 err = got_commit_graph_open(&graph, path, !log_branches);
4223 err = got_commit_graph_iter_start(graph, root_id, repo,
4224 check_cancelled, NULL);
4228 struct got_object_id *id;
4230 if (sigint_received || sigpipe_received)
4233 err = got_commit_graph_iter_next(&id, graph, repo,
4234 check_cancelled, NULL);
4236 if (err->code == GOT_ERR_ITER_COMPLETED)
4243 err = got_object_open_as_commit(&commit, repo, id);
4247 if (show_changed_paths && !reverse_display_order) {
4248 err = get_changed_paths(&changed_paths, commit, repo);
4253 if (search_pattern) {
4254 err = match_commit(&have_match, id, commit, ®ex);
4256 got_object_commit_close(commit);
4259 if (have_match == 0 && show_changed_paths)
4260 match_changed_paths(&have_match,
4261 &changed_paths, ®ex);
4262 if (have_match == 0 && show_patch) {
4263 err = match_patch(&have_match, commit, id,
4264 path, diff_context, repo, ®ex,
4269 if (have_match == 0) {
4270 got_object_commit_close(commit);
4271 TAILQ_FOREACH(pe, &changed_paths, entry) {
4272 free((char *)pe->path);
4275 got_pathlist_free(&changed_paths);
4280 if (reverse_display_order) {
4281 err = got_object_qid_alloc(&qid, id);
4284 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4285 got_object_commit_close(commit);
4288 err = print_commit_oneline(commit, id,
4291 err = print_commit(commit, id, repo, path,
4292 show_changed_paths ? &changed_paths : NULL,
4293 show_patch, diff_context, refs_idmap, NULL);
4294 got_object_commit_close(commit);
4298 if ((limit && --limit == 0) ||
4299 (end_id && got_object_id_cmp(id, end_id) == 0))
4302 TAILQ_FOREACH(pe, &changed_paths, entry) {
4303 free((char *)pe->path);
4306 got_pathlist_free(&changed_paths);
4308 if (reverse_display_order) {
4309 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4310 err = got_object_open_as_commit(&commit, repo,
4314 if (show_changed_paths) {
4315 err = get_changed_paths(&changed_paths,
4321 err = print_commit_oneline(commit, &qid->id,
4324 err = print_commit(commit, &qid->id, repo, path,
4325 show_changed_paths ? &changed_paths : NULL,
4326 show_patch, diff_context, refs_idmap, NULL);
4327 got_object_commit_close(commit);
4330 TAILQ_FOREACH(pe, &changed_paths, entry) {
4331 free((char *)pe->path);
4334 got_pathlist_free(&changed_paths);
4338 while (!STAILQ_EMPTY(&reversed_commits)) {
4339 qid = STAILQ_FIRST(&reversed_commits);
4340 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4341 got_object_qid_free(qid);
4343 TAILQ_FOREACH(pe, &changed_paths, entry) {
4344 free((char *)pe->path);
4347 got_pathlist_free(&changed_paths);
4350 got_commit_graph_close(graph);
4357 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4358 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4359 "[-r repository-path] [-R] [path]\n", getprogname());
4364 get_default_log_limit(void)
4366 const char *got_default_log_limit;
4370 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4371 if (got_default_log_limit == NULL)
4373 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4379 static const struct got_error *
4380 cmd_log(int argc, char *argv[])
4382 const struct got_error *error;
4383 struct got_repository *repo = NULL;
4384 struct got_worktree *worktree = NULL;
4385 struct got_object_id *start_id = NULL, *end_id = NULL;
4386 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4387 const char *start_commit = NULL, *end_commit = NULL;
4388 const char *search_pattern = NULL;
4389 int diff_context = -1, ch;
4390 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4391 int reverse_display_order = 0, one_line = 0;
4393 struct got_reflist_head refs;
4394 struct got_reflist_object_id_map *refs_idmap = NULL;
4395 FILE *tmpfile = NULL;
4396 int *pack_fds = NULL;
4401 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4407 limit = get_default_log_limit();
4409 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4415 show_changed_paths = 1;
4418 start_commit = optarg;
4421 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4424 errx(1, "number of context lines is %s: %s",
4428 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4430 errx(1, "number of commits is %s: %s",
4437 repo_path = realpath(optarg, NULL);
4438 if (repo_path == NULL)
4439 return got_error_from_errno2("realpath",
4441 got_path_strip_trailing_slashes(repo_path);
4444 reverse_display_order = 1;
4450 search_pattern = optarg;
4453 end_commit = optarg;
4464 if (diff_context == -1)
4466 else if (!show_patch)
4467 errx(1, "-C requires -p");
4469 if (one_line && (show_patch || show_changed_paths))
4470 errx(1, "cannot use -s with -p or -P");
4472 cwd = getcwd(NULL, 0);
4474 error = got_error_from_errno("getcwd");
4478 error = got_repo_pack_fds_open(&pack_fds);
4482 if (repo_path == NULL) {
4483 error = got_worktree_open(&worktree, cwd);
4484 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4491 error = got_worktree_resolve_path(&path, worktree,
4496 path = strdup(argv[0]);
4498 error = got_error_from_errno("strdup");
4502 } else if (argc != 0)
4505 if (repo_path == NULL) {
4506 repo_path = worktree ?
4507 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4509 if (repo_path == NULL) {
4510 error = got_error_from_errno("strdup");
4514 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4518 error = apply_unveil(got_repo_get_path(repo), 1,
4519 worktree ? got_worktree_get_root_path(worktree) : NULL);
4523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4527 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4531 if (start_commit == NULL) {
4532 struct got_reference *head_ref;
4533 struct got_commit_object *commit = NULL;
4534 error = got_ref_open(&head_ref, repo,
4535 worktree ? got_worktree_get_head_ref_name(worktree)
4539 error = got_ref_resolve(&start_id, repo, head_ref);
4540 got_ref_close(head_ref);
4543 error = got_object_open_as_commit(&commit, repo,
4547 got_object_commit_close(commit);
4549 error = got_repo_match_object_id(&start_id, NULL,
4550 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4554 if (end_commit != NULL) {
4555 error = got_repo_match_object_id(&end_id, NULL,
4556 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4563 * If a path was specified on the command line it was resolved
4564 * to a path in the work tree above. Prepend the work tree's
4565 * path prefix to obtain the corresponding in-repository path.
4569 prefix = got_worktree_get_path_prefix(worktree);
4570 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4571 (path[0] != '\0') ? "/" : "", path) == -1) {
4572 error = got_error_from_errno("asprintf");
4577 error = got_repo_map_path(&in_repo_path, repo,
4583 path = in_repo_path;
4587 /* Release work tree lock. */
4588 got_worktree_close(worktree);
4592 if (search_pattern && show_patch) {
4593 tmpfile = got_opentemp();
4594 if (tmpfile == NULL) {
4595 error = got_error_from_errno("got_opentemp");
4600 error = print_commits(start_id, end_id, repo, path ? path : "",
4601 show_changed_paths, show_patch, search_pattern, diff_context,
4602 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4609 got_worktree_close(worktree);
4611 const struct got_error *close_err = got_repo_close(repo);
4616 const struct got_error *pack_err =
4617 got_repo_pack_fds_close(pack_fds);
4622 got_reflist_object_id_map_free(refs_idmap);
4623 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4624 error = got_error_from_errno("fclose");
4625 got_ref_list_free(&refs);
4632 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4633 "[-r repository-path] [-s] [-w] [-P] "
4634 "[object1 object2 | path ...]\n", getprogname());
4638 struct print_diff_arg {
4639 struct got_repository *repo;
4640 struct got_worktree *worktree;
4645 enum got_diff_algorithm diff_algo;
4646 int ignore_whitespace;
4647 int force_text_diff;
4653 * Create a file which contains the target path of a symlink so we can feed
4654 * it as content to the diff engine.
4656 static const struct got_error *
4657 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4658 const char *abspath)
4660 const struct got_error *err = NULL;
4661 char target_path[PATH_MAX];
4662 ssize_t target_len, outlen;
4667 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4668 if (target_len == -1)
4669 return got_error_from_errno2("readlinkat", abspath);
4671 target_len = readlink(abspath, target_path, PATH_MAX);
4672 if (target_len == -1)
4673 return got_error_from_errno2("readlink", abspath);
4676 *fd = got_opentempfd();
4678 return got_error_from_errno("got_opentempfd");
4680 outlen = write(*fd, target_path, target_len);
4682 err = got_error_from_errno("got_opentempfd");
4686 if (lseek(*fd, 0, SEEK_SET) == -1) {
4687 err = got_error_from_errno2("lseek", abspath);
4698 static const struct got_error *
4699 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4700 const char *path, struct got_object_id *blob_id,
4701 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4702 int dirfd, const char *de_name)
4704 struct print_diff_arg *a = arg;
4705 const struct got_error *err = NULL;
4706 struct got_blob_object *blob1 = NULL;
4707 int fd = -1, fd1 = -1, fd2 = -1;
4709 char *abspath = NULL, *label1 = NULL;
4714 if (a->diff_staged) {
4715 if (staged_status != GOT_STATUS_MODIFY &&
4716 staged_status != GOT_STATUS_ADD &&
4717 staged_status != GOT_STATUS_DELETE)
4720 if (staged_status == GOT_STATUS_DELETE)
4722 if (status == GOT_STATUS_NONEXISTENT)
4723 return got_error_set_errno(ENOENT, path);
4724 if (status != GOT_STATUS_MODIFY &&
4725 status != GOT_STATUS_ADD &&
4726 status != GOT_STATUS_DELETE &&
4727 status != GOT_STATUS_CONFLICT)
4731 err = got_opentemp_truncate(a->f1);
4733 return got_error_from_errno("got_opentemp_truncate");
4734 err = got_opentemp_truncate(a->f2);
4736 return got_error_from_errno("got_opentemp_truncate");
4738 if (!a->header_shown) {
4739 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4740 got_worktree_get_root_path(a->worktree));
4741 printf("commit - %s\n", a->id_str);
4742 printf("path + %s%s\n",
4743 got_worktree_get_root_path(a->worktree),
4744 a->diff_staged ? " (staged changes)" : "");
4745 a->header_shown = 1;
4748 if (a->diff_staged) {
4749 const char *label1 = NULL, *label2 = NULL;
4750 switch (staged_status) {
4751 case GOT_STATUS_MODIFY:
4755 case GOT_STATUS_ADD:
4758 case GOT_STATUS_DELETE:
4762 return got_error(GOT_ERR_FILE_STATUS);
4764 fd1 = got_opentempfd();
4766 err = got_error_from_errno("got_opentempfd");
4769 fd2 = got_opentempfd();
4771 err = got_error_from_errno("got_opentempfd");
4774 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4775 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4776 a->diff_algo, a->diff_context, a->ignore_whitespace,
4777 a->force_text_diff, a->repo, stdout);
4781 fd1 = got_opentempfd();
4783 err = got_error_from_errno("got_opentempfd");
4787 if (staged_status == GOT_STATUS_ADD ||
4788 staged_status == GOT_STATUS_MODIFY) {
4790 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4794 err = got_object_id_str(&id_str, staged_blob_id);
4797 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4798 err = got_error_from_errno("asprintf");
4803 } else if (status != GOT_STATUS_ADD) {
4804 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4810 if (status != GOT_STATUS_DELETE) {
4811 if (asprintf(&abspath, "%s/%s",
4812 got_worktree_get_root_path(a->worktree), path) == -1) {
4813 err = got_error_from_errno("asprintf");
4818 fd = openat(dirfd, de_name,
4819 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4821 if (!got_err_open_nofollow_on_symlink()) {
4822 err = got_error_from_errno2("openat",
4826 err = get_symlink_target_file(&fd, dirfd,
4832 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4834 if (!got_err_open_nofollow_on_symlink()) {
4835 err = got_error_from_errno2("open",
4839 err = get_symlink_target_file(&fd, dirfd,
4845 if (fstat(fd, &sb) == -1) {
4846 err = got_error_from_errno2("fstat", abspath);
4849 f2 = fdopen(fd, "r");
4851 err = got_error_from_errno2("fdopen", abspath);
4861 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4867 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4868 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4869 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4871 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4872 err = got_error_from_errno("close");
4873 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4874 err = got_error_from_errno("close");
4876 got_object_blob_close(blob1);
4877 if (fd != -1 && close(fd) == -1 && err == NULL)
4878 err = got_error_from_errno("close");
4879 if (f2 && fclose(f2) == EOF && err == NULL)
4880 err = got_error_from_errno("fclose");
4885 static const struct got_error *
4886 cmd_diff(int argc, char *argv[])
4888 const struct got_error *error;
4889 struct got_repository *repo = NULL;
4890 struct got_worktree *worktree = NULL;
4891 char *cwd = NULL, *repo_path = NULL;
4892 const char *commit_args[2] = { NULL, NULL };
4893 int ncommit_args = 0;
4894 struct got_object_id *ids[2] = { NULL, NULL };
4895 char *labels[2] = { NULL, NULL };
4896 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4897 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4898 int force_text_diff = 0, force_path = 0, rflag = 0;
4900 struct got_reflist_head refs;
4901 struct got_pathlist_head paths;
4902 struct got_pathlist_entry *pe;
4903 FILE *f1 = NULL, *f2 = NULL;
4904 int fd1 = -1, fd2 = -1;
4905 int *pack_fds = NULL;
4911 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4916 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4919 force_text_diff = 1;
4922 if (ncommit_args >= 2)
4923 errx(1, "too many -c options used");
4924 commit_args[ncommit_args++] = optarg;
4927 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4930 errx(1, "number of context lines is %s: %s",
4934 repo_path = realpath(optarg, NULL);
4935 if (repo_path == NULL)
4936 return got_error_from_errno2("realpath",
4938 got_path_strip_trailing_slashes(repo_path);
4945 ignore_whitespace = 1;
4959 cwd = getcwd(NULL, 0);
4961 error = got_error_from_errno("getcwd");
4965 error = got_repo_pack_fds_open(&pack_fds);
4969 if (repo_path == NULL) {
4970 error = got_worktree_open(&worktree, cwd);
4971 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4977 strdup(got_worktree_get_repo_path(worktree));
4978 if (repo_path == NULL) {
4979 error = got_error_from_errno("strdup");
4983 repo_path = strdup(cwd);
4984 if (repo_path == NULL) {
4985 error = got_error_from_errno("strdup");
4991 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4996 if (rflag || worktree == NULL || ncommit_args > 0) {
4998 error = got_error_msg(GOT_ERR_NOT_IMPL,
4999 "-P option can only be used when diffing "
5004 error = got_error_msg(GOT_ERR_NOT_IMPL,
5005 "-s option can only be used when diffing "
5011 error = apply_unveil(got_repo_get_path(repo), 1,
5012 worktree ? got_worktree_get_root_path(worktree) : NULL);
5016 if ((!force_path && argc == 2) || ncommit_args > 0) {
5017 int obj_type = (ncommit_args > 0 ?
5018 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5019 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5023 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5025 if (ncommit_args > 0)
5026 arg = commit_args[i];
5029 error = got_repo_match_object_id(&ids[i], &labels[i],
5030 arg, obj_type, &refs, repo);
5032 if (error->code != GOT_ERR_NOT_REF &&
5033 error->code != GOT_ERR_NO_OBJ)
5035 if (ncommit_args > 0)
5043 f1 = got_opentemp();
5045 error = got_error_from_errno("got_opentemp");
5049 f2 = got_opentemp();
5051 error = got_error_from_errno("got_opentemp");
5055 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5056 struct print_diff_arg arg;
5059 if (worktree == NULL) {
5060 if (argc == 2 && ids[0] == NULL) {
5061 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5063 } else if (argc == 2 && ids[1] == NULL) {
5064 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5066 } else if (argc > 0) {
5067 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5068 "%s", "specified paths cannot be resolved");
5071 error = got_error(GOT_ERR_NOT_WORKTREE);
5076 error = get_worktree_paths_from_argv(&paths, argc, argv,
5081 error = got_object_id_str(&id_str,
5082 got_worktree_get_base_commit_id(worktree));
5086 arg.worktree = worktree;
5087 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5088 arg.diff_context = diff_context;
5089 arg.id_str = id_str;
5090 arg.header_shown = 0;
5091 arg.diff_staged = diff_staged;
5092 arg.ignore_whitespace = ignore_whitespace;
5093 arg.force_text_diff = force_text_diff;
5097 error = got_worktree_status(worktree, &paths, repo, 0,
5098 print_diff, &arg, check_cancelled, NULL);
5103 if (ncommit_args == 1) {
5104 struct got_commit_object *commit;
5105 error = got_object_open_as_commit(&commit, repo, ids[0]);
5109 labels[1] = labels[0];
5111 if (got_object_commit_get_nparents(commit) > 0) {
5112 const struct got_object_id_queue *pids;
5113 struct got_object_qid *pid;
5114 pids = got_object_commit_get_parent_ids(commit);
5115 pid = STAILQ_FIRST(pids);
5116 ids[0] = got_object_id_dup(&pid->id);
5117 if (ids[0] == NULL) {
5118 error = got_error_from_errno(
5119 "got_object_id_dup");
5120 got_object_commit_close(commit);
5123 error = got_object_id_str(&labels[0], ids[0]);
5125 got_object_commit_close(commit);
5130 labels[0] = strdup("/dev/null");
5131 if (labels[0] == NULL) {
5132 error = got_error_from_errno("strdup");
5133 got_object_commit_close(commit);
5138 got_object_commit_close(commit);
5141 if (ncommit_args == 0 && argc > 2) {
5142 error = got_error_msg(GOT_ERR_BAD_PATH,
5143 "path arguments cannot be used when diffing two objects");
5148 error = got_object_get_type(&type1, repo, ids[0]);
5153 error = got_object_get_type(&type2, repo, ids[1]);
5156 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5157 error = got_error(GOT_ERR_OBJ_TYPE);
5160 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5161 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5162 "path arguments cannot be used when diffing blobs");
5166 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5168 struct got_pathlist_entry *new;
5172 error = got_worktree_resolve_path(&p, worktree,
5176 prefix = got_worktree_get_path_prefix(worktree);
5177 while (prefix[0] == '/')
5179 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5180 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5182 error = got_error_from_errno("asprintf");
5188 char *mapped_path, *s;
5189 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5195 in_repo_path = strdup(s);
5196 if (in_repo_path == NULL) {
5197 error = got_error_from_errno("asprintf");
5204 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5205 if (error || new == NULL /* duplicate */)
5212 /* Release work tree lock. */
5213 got_worktree_close(worktree);
5217 fd1 = got_opentempfd();
5219 error = got_error_from_errno("got_opentempfd");
5223 fd2 = got_opentempfd();
5225 error = got_error_from_errno("got_opentempfd");
5229 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5230 case GOT_OBJ_TYPE_BLOB:
5231 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5232 fd1, fd2, ids[0], ids[1], NULL, NULL,
5233 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5234 ignore_whitespace, force_text_diff, repo, stdout);
5236 case GOT_OBJ_TYPE_TREE:
5237 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5238 ids[0], ids[1], &paths, "", "",
5239 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5240 ignore_whitespace, force_text_diff, repo, stdout);
5242 case GOT_OBJ_TYPE_COMMIT:
5243 printf("diff %s %s\n", labels[0], labels[1]);
5244 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5245 fd1, fd2, ids[0], ids[1], &paths,
5246 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5247 ignore_whitespace, force_text_diff, repo, stdout);
5250 error = got_error(GOT_ERR_OBJ_TYPE);
5258 got_worktree_close(worktree);
5260 const struct got_error *close_err = got_repo_close(repo);
5265 const struct got_error *pack_err =
5266 got_repo_pack_fds_close(pack_fds);
5270 TAILQ_FOREACH(pe, &paths, entry)
5271 free((char *)pe->path);
5272 got_pathlist_free(&paths);
5273 got_ref_list_free(&refs);
5274 if (f1 && fclose(f1) == EOF && error == NULL)
5275 error = got_error_from_errno("fclose");
5276 if (f2 && fclose(f2) == EOF && error == NULL)
5277 error = got_error_from_errno("fclose");
5278 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5279 error = got_error_from_errno("close");
5280 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5281 error = got_error_from_errno("close");
5289 "usage: %s blame [-c commit] [-r repository-path] path\n",
5298 char datebuf[11]; /* YYYY-MM-DD + NUL */
5301 struct blame_cb_args {
5302 struct blame_line *lines;
5306 off_t *line_offsets;
5308 struct got_repository *repo;
5311 static const struct got_error *
5312 blame_cb(void *arg, int nlines, int lineno,
5313 struct got_commit_object *commit, struct got_object_id *id)
5315 const struct got_error *err = NULL;
5316 struct blame_cb_args *a = arg;
5317 struct blame_line *bline;
5319 size_t linesize = 0;
5322 time_t committer_time;
5324 if (nlines != a->nlines ||
5325 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5326 return got_error(GOT_ERR_RANGE);
5328 if (sigint_received)
5329 return got_error(GOT_ERR_ITER_COMPLETED);
5332 return NULL; /* no change in this commit */
5334 /* Annotate this line. */
5335 bline = &a->lines[lineno - 1];
5336 if (bline->annotated)
5338 err = got_object_id_str(&bline->id_str, id);
5342 bline->committer = strdup(got_object_commit_get_committer(commit));
5343 if (bline->committer == NULL) {
5344 err = got_error_from_errno("strdup");
5348 committer_time = got_object_commit_get_committer_time(commit);
5349 if (gmtime_r(&committer_time, &tm) == NULL)
5350 return got_error_from_errno("gmtime_r");
5351 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5353 err = got_error(GOT_ERR_NO_SPACE);
5356 bline->annotated = 1;
5358 /* Print lines annotated so far. */
5359 bline = &a->lines[a->lineno_cur - 1];
5360 if (!bline->annotated)
5363 offset = a->line_offsets[a->lineno_cur - 1];
5364 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5365 err = got_error_from_errno("fseeko");
5369 while (bline->annotated) {
5370 char *smallerthan, *at, *nl, *committer;
5373 if (getline(&line, &linesize, a->f) == -1) {
5375 err = got_error_from_errno("getline");
5379 committer = bline->committer;
5380 smallerthan = strchr(committer, '<');
5381 if (smallerthan && smallerthan[1] != '\0')
5382 committer = smallerthan + 1;
5383 at = strchr(committer, '@');
5386 len = strlen(committer);
5388 committer[8] = '\0';
5390 nl = strchr(line, '\n');
5393 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5394 bline->id_str, bline->datebuf, committer, line);
5397 bline = &a->lines[a->lineno_cur - 1];
5404 static const struct got_error *
5405 cmd_blame(int argc, char *argv[])
5407 const struct got_error *error;
5408 struct got_repository *repo = NULL;
5409 struct got_worktree *worktree = NULL;
5410 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5411 char *link_target = NULL;
5412 struct got_object_id *obj_id = NULL;
5413 struct got_object_id *commit_id = NULL;
5414 struct got_commit_object *commit = NULL;
5415 struct got_blob_object *blob = NULL;
5416 char *commit_id_str = NULL;
5417 struct blame_cb_args bca;
5418 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5420 int *pack_fds = NULL;
5421 FILE *f1 = NULL, *f2 = NULL;
5423 fd1 = got_opentempfd();
5425 return got_error_from_errno("got_opentempfd");
5427 memset(&bca, 0, sizeof(bca));
5430 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5435 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5438 commit_id_str = optarg;
5441 repo_path = realpath(optarg, NULL);
5442 if (repo_path == NULL)
5443 return got_error_from_errno2("realpath",
5445 got_path_strip_trailing_slashes(repo_path);
5461 cwd = getcwd(NULL, 0);
5463 error = got_error_from_errno("getcwd");
5467 error = got_repo_pack_fds_open(&pack_fds);
5471 if (repo_path == NULL) {
5472 error = got_worktree_open(&worktree, cwd);
5473 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5479 strdup(got_worktree_get_repo_path(worktree));
5480 if (repo_path == NULL) {
5481 error = got_error_from_errno("strdup");
5486 repo_path = strdup(cwd);
5487 if (repo_path == NULL) {
5488 error = got_error_from_errno("strdup");
5494 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5499 const char *prefix = got_worktree_get_path_prefix(worktree);
5502 error = got_worktree_resolve_path(&p, worktree, path);
5505 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5506 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5508 error = got_error_from_errno("asprintf");
5513 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5515 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5518 error = got_repo_map_path(&in_repo_path, repo, path);
5523 if (commit_id_str == NULL) {
5524 struct got_reference *head_ref;
5525 error = got_ref_open(&head_ref, repo, worktree ?
5526 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5529 error = got_ref_resolve(&commit_id, repo, head_ref);
5530 got_ref_close(head_ref);
5534 struct got_reflist_head refs;
5536 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5540 error = got_repo_match_object_id(&commit_id, NULL,
5541 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5542 got_ref_list_free(&refs);
5548 /* Release work tree lock. */
5549 got_worktree_close(worktree);
5553 error = got_object_open_as_commit(&commit, repo, commit_id);
5557 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5562 error = got_object_id_by_path(&obj_id, repo, commit,
5563 link_target ? link_target : in_repo_path);
5567 error = got_object_get_type(&obj_type, repo, obj_id);
5571 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5572 error = got_error_path(link_target ? link_target : in_repo_path,
5577 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5580 bca.f = got_opentemp();
5581 if (bca.f == NULL) {
5582 error = got_error_from_errno("got_opentemp");
5585 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5586 &bca.line_offsets, bca.f, blob);
5587 if (error || bca.nlines == 0)
5590 /* Don't include \n at EOF in the blame line count. */
5591 if (bca.line_offsets[bca.nlines - 1] == filesize)
5594 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5595 if (bca.lines == NULL) {
5596 error = got_error_from_errno("calloc");
5600 bca.nlines_prec = 0;
5608 fd2 = got_opentempfd();
5610 error = got_error_from_errno("got_opentempfd");
5613 fd3 = got_opentempfd();
5615 error = got_error_from_errno("got_opentempfd");
5618 f1 = got_opentemp();
5620 error = got_error_from_errno("got_opentemp");
5623 f2 = got_opentemp();
5625 error = got_error_from_errno("got_opentemp");
5628 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5629 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5630 check_cancelled, NULL, fd2, fd3, f1, f2);
5639 got_object_commit_close(commit);
5641 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5642 error = got_error_from_errno("close");
5643 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5644 error = got_error_from_errno("close");
5645 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5646 error = got_error_from_errno("close");
5647 if (f1 && fclose(f1) == EOF && error == NULL)
5648 error = got_error_from_errno("fclose");
5649 if (f2 && fclose(f2) == EOF && error == NULL)
5650 error = got_error_from_errno("fclose");
5653 got_object_blob_close(blob);
5655 got_worktree_close(worktree);
5657 const struct got_error *close_err = got_repo_close(repo);
5662 const struct got_error *pack_err =
5663 got_repo_pack_fds_close(pack_fds);
5668 for (i = 0; i < bca.nlines; i++) {
5669 struct blame_line *bline = &bca.lines[i];
5670 free(bline->id_str);
5671 free(bline->committer);
5675 free(bca.line_offsets);
5676 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5677 error = got_error_from_errno("fclose");
5685 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5690 static const struct got_error *
5691 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5692 const char *root_path, struct got_repository *repo)
5694 const struct got_error *err = NULL;
5695 int is_root_path = (strcmp(path, root_path) == 0);
5696 const char *modestr = "";
5697 mode_t mode = got_tree_entry_get_mode(te);
5698 char *link_target = NULL;
5700 path += strlen(root_path);
5701 while (path[0] == '/')
5704 if (got_object_tree_entry_is_submodule(te))
5706 else if (S_ISLNK(mode)) {
5709 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5712 for (i = 0; i < strlen(link_target); i++) {
5713 if (!isprint((unsigned char)link_target[i]))
5714 link_target[i] = '?';
5719 else if (S_ISDIR(mode))
5721 else if (mode & S_IXUSR)
5724 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5725 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5726 link_target ? " -> ": "", link_target ? link_target : "");
5732 static const struct got_error *
5733 print_tree(const char *path, struct got_commit_object *commit,
5734 int show_ids, int recurse, const char *root_path,
5735 struct got_repository *repo)
5737 const struct got_error *err = NULL;
5738 struct got_object_id *tree_id = NULL;
5739 struct got_tree_object *tree = NULL;
5742 err = got_object_id_by_path(&tree_id, repo, commit, path);
5746 err = got_object_open_as_tree(&tree, repo, tree_id);
5749 nentries = got_object_tree_get_nentries(tree);
5750 for (i = 0; i < nentries; i++) {
5751 struct got_tree_entry *te;
5754 if (sigint_received || sigpipe_received)
5757 te = got_object_tree_get_entry(tree, i);
5760 err = got_object_id_str(&id_str,
5761 got_tree_entry_get_id(te));
5764 if (asprintf(&id, "%s ", id_str) == -1) {
5765 err = got_error_from_errno("asprintf");
5771 err = print_entry(te, id, path, root_path, repo);
5776 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5778 if (asprintf(&child_path, "%s%s%s", path,
5779 path[0] == '/' && path[1] == '\0' ? "" : "/",
5780 got_tree_entry_get_name(te)) == -1) {
5781 err = got_error_from_errno("asprintf");
5784 err = print_tree(child_path, commit, show_ids, 1,
5793 got_object_tree_close(tree);
5798 static const struct got_error *
5799 cmd_tree(int argc, char *argv[])
5801 const struct got_error *error;
5802 struct got_repository *repo = NULL;
5803 struct got_worktree *worktree = NULL;
5804 const char *path, *refname = NULL;
5805 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5806 struct got_object_id *commit_id = NULL;
5807 struct got_commit_object *commit = NULL;
5808 char *commit_id_str = NULL;
5809 int show_ids = 0, recurse = 0;
5811 int *pack_fds = NULL;
5814 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5819 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5822 commit_id_str = optarg;
5825 repo_path = realpath(optarg, NULL);
5826 if (repo_path == NULL)
5827 return got_error_from_errno2("realpath",
5829 got_path_strip_trailing_slashes(repo_path);
5853 cwd = getcwd(NULL, 0);
5855 error = got_error_from_errno("getcwd");
5859 error = got_repo_pack_fds_open(&pack_fds);
5863 if (repo_path == NULL) {
5864 error = got_worktree_open(&worktree, cwd);
5865 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5871 strdup(got_worktree_get_repo_path(worktree));
5872 if (repo_path == NULL)
5873 error = got_error_from_errno("strdup");
5877 repo_path = strdup(cwd);
5878 if (repo_path == NULL) {
5879 error = got_error_from_errno("strdup");
5885 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5890 const char *prefix = got_worktree_get_path_prefix(worktree);
5895 error = got_worktree_resolve_path(&p, worktree, path);
5898 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5899 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5901 error = got_error_from_errno("asprintf");
5906 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5910 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5915 error = got_repo_map_path(&in_repo_path, repo, path);
5920 if (commit_id_str == NULL) {
5921 struct got_reference *head_ref;
5923 refname = got_worktree_get_head_ref_name(worktree);
5925 refname = GOT_REF_HEAD;
5926 error = got_ref_open(&head_ref, repo, refname, 0);
5929 error = got_ref_resolve(&commit_id, repo, head_ref);
5930 got_ref_close(head_ref);
5934 struct got_reflist_head refs;
5936 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5940 error = got_repo_match_object_id(&commit_id, NULL,
5941 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5942 got_ref_list_free(&refs);
5948 /* Release work tree lock. */
5949 got_worktree_close(worktree);
5953 error = got_object_open_as_commit(&commit, repo, commit_id);
5957 error = print_tree(in_repo_path, commit, show_ids, recurse,
5958 in_repo_path, repo);
5965 got_object_commit_close(commit);
5967 got_worktree_close(worktree);
5969 const struct got_error *close_err = got_repo_close(repo);
5974 const struct got_error *pack_err =
5975 got_repo_pack_fds_close(pack_fds);
5985 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5986 "[-S status-codes] [path ...]\n", getprogname());
5990 struct got_status_arg {
5995 static const struct got_error *
5996 print_status(void *arg, unsigned char status, unsigned char staged_status,
5997 const char *path, struct got_object_id *blob_id,
5998 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5999 int dirfd, const char *de_name)
6001 struct got_status_arg *st = arg;
6003 if (status == staged_status && (status == GOT_STATUS_DELETE))
6004 status = GOT_STATUS_NO_CHANGE;
6005 if (st != NULL && st->status_codes) {
6006 size_t ncodes = strlen(st->status_codes);
6009 for (i = 0; i < ncodes ; i++) {
6011 if (status == st->status_codes[i] ||
6012 staged_status == st->status_codes[i]) {
6017 if (status == st->status_codes[i] ||
6018 staged_status == st->status_codes[i])
6023 if (st->suppress && j == 0)
6030 printf("%c%c %s\n", status, staged_status, path);
6034 static const struct got_error *
6035 cmd_status(int argc, char *argv[])
6037 const struct got_error *error = NULL;
6038 struct got_repository *repo = NULL;
6039 struct got_worktree *worktree = NULL;
6040 struct got_status_arg st;
6042 struct got_pathlist_head paths;
6043 struct got_pathlist_entry *pe;
6044 int ch, i, no_ignores = 0;
6045 int *pack_fds = NULL;
6049 memset(&st, 0, sizeof(st));
6050 st.status_codes = NULL;
6053 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6059 if (st.status_codes != NULL && st.suppress == 0)
6060 option_conflict('S', 's');
6064 for (i = 0; i < strlen(optarg); i++) {
6065 switch (optarg[i]) {
6066 case GOT_STATUS_MODIFY:
6067 case GOT_STATUS_ADD:
6068 case GOT_STATUS_DELETE:
6069 case GOT_STATUS_CONFLICT:
6070 case GOT_STATUS_MISSING:
6071 case GOT_STATUS_OBSTRUCTED:
6072 case GOT_STATUS_UNVERSIONED:
6073 case GOT_STATUS_MODE_CHANGE:
6074 case GOT_STATUS_NONEXISTENT:
6077 errx(1, "invalid status code '%c'",
6081 if (ch == 's' && st.suppress)
6082 option_conflict('s', 'S');
6083 st.status_codes = optarg;
6095 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6099 cwd = getcwd(NULL, 0);
6101 error = got_error_from_errno("getcwd");
6105 error = got_repo_pack_fds_open(&pack_fds);
6109 error = got_worktree_open(&worktree, cwd);
6111 if (error->code == GOT_ERR_NOT_WORKTREE)
6112 error = wrap_not_worktree_error(error, "status", cwd);
6116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6121 error = apply_unveil(got_repo_get_path(repo), 1,
6122 got_worktree_get_root_path(worktree));
6126 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6130 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6131 print_status, &st, check_cancelled, NULL);
6134 const struct got_error *pack_err =
6135 got_repo_pack_fds_close(pack_fds);
6140 TAILQ_FOREACH(pe, &paths, entry)
6141 free((char *)pe->path);
6142 got_pathlist_free(&paths);
6151 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6152 "[-s reference] [-d] [name]\n",
6157 static const struct got_error *
6158 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6160 static const struct got_error *err = NULL;
6161 struct got_reflist_head refs;
6162 struct got_reflist_entry *re;
6165 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6166 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6171 TAILQ_FOREACH(re, &refs, entry) {
6173 refstr = got_ref_to_str(re->ref);
6174 if (refstr == NULL) {
6175 err = got_error_from_errno("got_ref_to_str");
6178 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6182 got_ref_list_free(&refs);
6186 static const struct got_error *
6187 delete_ref_by_name(struct got_repository *repo, const char *refname)
6189 const struct got_error *err;
6190 struct got_reference *ref;
6192 err = got_ref_open(&ref, repo, refname, 0);
6196 err = delete_ref(repo, ref);
6201 static const struct got_error *
6202 add_ref(struct got_repository *repo, const char *refname, const char *target)
6204 const struct got_error *err = NULL;
6205 struct got_object_id *id = NULL;
6206 struct got_reference *ref = NULL;
6207 struct got_reflist_head refs;
6210 * Don't let the user create a reference name with a leading '-'.
6211 * While technically a valid reference name, this case is usually
6212 * an unintended typo.
6214 if (refname[0] == '-')
6215 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6218 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6221 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6223 got_ref_list_free(&refs);
6227 err = got_ref_alloc(&ref, refname, id);
6231 err = got_ref_write(ref, repo);
6239 static const struct got_error *
6240 add_symref(struct got_repository *repo, const char *refname, const char *target)
6242 const struct got_error *err = NULL;
6243 struct got_reference *ref = NULL;
6244 struct got_reference *target_ref = NULL;
6247 * Don't let the user create a reference name with a leading '-'.
6248 * While technically a valid reference name, this case is usually
6249 * an unintended typo.
6251 if (refname[0] == '-')
6252 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6254 err = got_ref_open(&target_ref, repo, target, 0);
6258 err = got_ref_alloc_symref(&ref, refname, target_ref);
6262 err = got_ref_write(ref, repo);
6265 got_ref_close(target_ref);
6271 static const struct got_error *
6272 cmd_ref(int argc, char *argv[])
6274 const struct got_error *error = NULL;
6275 struct got_repository *repo = NULL;
6276 struct got_worktree *worktree = NULL;
6277 char *cwd = NULL, *repo_path = NULL;
6278 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6279 const char *obj_arg = NULL, *symref_target= NULL;
6280 char *refname = NULL;
6281 int *pack_fds = NULL;
6283 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6292 repo_path = realpath(optarg, NULL);
6293 if (repo_path == NULL)
6294 return got_error_from_errno2("realpath",
6296 got_path_strip_trailing_slashes(repo_path);
6302 symref_target = optarg;
6313 if (obj_arg && do_list)
6314 option_conflict('c', 'l');
6315 if (obj_arg && do_delete)
6316 option_conflict('c', 'd');
6317 if (obj_arg && symref_target)
6318 option_conflict('c', 's');
6319 if (symref_target && do_delete)
6320 option_conflict('s', 'd');
6321 if (symref_target && do_list)
6322 option_conflict('s', 'l');
6323 if (do_delete && do_list)
6324 option_conflict('d', 'l');
6325 if (sort_by_time && !do_list)
6326 errx(1, "-t option requires -l option");
6332 if (argc != 0 && argc != 1)
6335 refname = strdup(argv[0]);
6336 if (refname == NULL) {
6337 error = got_error_from_errno("strdup");
6344 refname = strdup(argv[0]);
6345 if (refname == NULL) {
6346 error = got_error_from_errno("strdup");
6352 got_path_strip_trailing_slashes(refname);
6355 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6356 "sendfd unveil", NULL) == -1)
6359 cwd = getcwd(NULL, 0);
6361 error = got_error_from_errno("getcwd");
6365 error = got_repo_pack_fds_open(&pack_fds);
6369 if (repo_path == NULL) {
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6377 strdup(got_worktree_get_repo_path(worktree));
6378 if (repo_path == NULL)
6379 error = got_error_from_errno("strdup");
6383 repo_path = strdup(cwd);
6384 if (repo_path == NULL) {
6385 error = got_error_from_errno("strdup");
6391 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6397 /* Remove "cpath" promise. */
6398 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6404 error = apply_unveil(got_repo_get_path(repo), do_list,
6405 worktree ? got_worktree_get_root_path(worktree) : NULL);
6410 error = list_refs(repo, refname, sort_by_time);
6412 error = delete_ref_by_name(repo, refname);
6413 else if (symref_target)
6414 error = add_symref(repo, refname, symref_target);
6416 if (obj_arg == NULL)
6418 error = add_ref(repo, refname, obj_arg);
6423 const struct got_error *close_err = got_repo_close(repo);
6428 got_worktree_close(worktree);
6430 const struct got_error *pack_err =
6431 got_repo_pack_fds_close(pack_fds);
6444 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6445 "[-n] [name]\n", getprogname());
6449 static const struct got_error *
6450 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6451 struct got_reference *ref)
6453 const struct got_error *err = NULL;
6454 const char *refname, *marker = " ";
6457 refname = got_ref_get_name(ref);
6458 if (worktree && strcmp(refname,
6459 got_worktree_get_head_ref_name(worktree)) == 0) {
6460 struct got_object_id *id = NULL;
6462 err = got_ref_resolve(&id, repo, ref);
6465 if (got_object_id_cmp(id,
6466 got_worktree_get_base_commit_id(worktree)) == 0)
6473 if (strncmp(refname, "refs/heads/", 11) == 0)
6475 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6477 if (strncmp(refname, "refs/remotes/", 13) == 0)
6480 refstr = got_ref_to_str(ref);
6482 return got_error_from_errno("got_ref_to_str");
6484 printf("%s%s: %s\n", marker, refname, refstr);
6489 static const struct got_error *
6490 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6492 const char *refname;
6494 if (worktree == NULL)
6495 return got_error(GOT_ERR_NOT_WORKTREE);
6497 refname = got_worktree_get_head_ref_name(worktree);
6499 if (strncmp(refname, "refs/heads/", 11) == 0)
6501 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6504 printf("%s\n", refname);
6509 static const struct got_error *
6510 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6513 static const struct got_error *err = NULL;
6514 struct got_reflist_head refs;
6515 struct got_reflist_entry *re;
6516 struct got_reference *temp_ref = NULL;
6517 int rebase_in_progress, histedit_in_progress;
6522 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6527 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6532 if (rebase_in_progress || histedit_in_progress) {
6533 err = got_ref_open(&temp_ref, repo,
6534 got_worktree_get_head_ref_name(worktree), 0);
6537 list_branch(repo, worktree, temp_ref);
6538 got_ref_close(temp_ref);
6542 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6543 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6548 TAILQ_FOREACH(re, &refs, entry)
6549 list_branch(repo, worktree, re->ref);
6551 got_ref_list_free(&refs);
6553 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6554 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6559 TAILQ_FOREACH(re, &refs, entry)
6560 list_branch(repo, worktree, re->ref);
6562 got_ref_list_free(&refs);
6567 static const struct got_error *
6568 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6569 const char *branch_name)
6571 const struct got_error *err = NULL;
6572 struct got_reference *ref = NULL;
6573 char *refname, *remote_refname = NULL;
6575 if (strncmp(branch_name, "refs/", 5) == 0)
6577 if (strncmp(branch_name, "heads/", 6) == 0)
6579 else if (strncmp(branch_name, "remotes/", 8) == 0)
6582 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6583 return got_error_from_errno("asprintf");
6585 if (asprintf(&remote_refname, "refs/remotes/%s",
6586 branch_name) == -1) {
6587 err = got_error_from_errno("asprintf");
6591 err = got_ref_open(&ref, repo, refname, 0);
6593 const struct got_error *err2;
6594 if (err->code != GOT_ERR_NOT_REF)
6597 * Keep 'err' intact such that if neither branch exists
6598 * we report "refs/heads" rather than "refs/remotes" in
6599 * our error message.
6601 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6608 strcmp(got_worktree_get_head_ref_name(worktree),
6609 got_ref_get_name(ref)) == 0) {
6610 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6611 "will not delete this work tree's current branch");
6615 err = delete_ref(repo, ref);
6620 free(remote_refname);
6624 static const struct got_error *
6625 add_branch(struct got_repository *repo, const char *branch_name,
6626 struct got_object_id *base_commit_id)
6628 const struct got_error *err = NULL;
6629 struct got_reference *ref = NULL;
6630 char *base_refname = NULL, *refname = NULL;
6633 * Don't let the user create a branch name with a leading '-'.
6634 * While technically a valid reference name, this case is usually
6635 * an unintended typo.
6637 if (branch_name[0] == '-')
6638 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6640 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6643 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6644 err = got_error_from_errno("asprintf");
6648 err = got_ref_open(&ref, repo, refname, 0);
6650 err = got_error(GOT_ERR_BRANCH_EXISTS);
6652 } else if (err->code != GOT_ERR_NOT_REF)
6655 err = got_ref_alloc(&ref, refname, base_commit_id);
6659 err = got_ref_write(ref, repo);
6668 static const struct got_error *
6669 cmd_branch(int argc, char *argv[])
6671 const struct got_error *error = NULL;
6672 struct got_repository *repo = NULL;
6673 struct got_worktree *worktree = NULL;
6674 char *cwd = NULL, *repo_path = NULL;
6675 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6676 const char *delref = NULL, *commit_id_arg = NULL;
6677 struct got_reference *ref = NULL;
6678 struct got_pathlist_head paths;
6679 struct got_pathlist_entry *pe;
6680 struct got_object_id *commit_id = NULL;
6681 char *commit_id_str = NULL;
6682 int *pack_fds = NULL;
6686 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6689 commit_id_arg = optarg;
6695 repo_path = realpath(optarg, NULL);
6696 if (repo_path == NULL)
6697 return got_error_from_errno2("realpath",
6699 got_path_strip_trailing_slashes(repo_path);
6716 if (do_list && delref)
6717 option_conflict('l', 'd');
6718 if (sort_by_time && !do_list)
6719 errx(1, "-t option requires -l option");
6724 if (!do_list && !delref && argc == 0)
6727 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6728 errx(1, "-c option can only be used when creating a branch");
6730 if (do_list || delref) {
6733 } else if (!do_show && argc != 1)
6737 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6738 "sendfd unveil", NULL) == -1)
6741 cwd = getcwd(NULL, 0);
6743 error = got_error_from_errno("getcwd");
6747 error = got_repo_pack_fds_open(&pack_fds);
6751 if (repo_path == NULL) {
6752 error = got_worktree_open(&worktree, cwd);
6753 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6759 strdup(got_worktree_get_repo_path(worktree));
6760 if (repo_path == NULL)
6761 error = got_error_from_errno("strdup");
6765 repo_path = strdup(cwd);
6766 if (repo_path == NULL) {
6767 error = got_error_from_errno("strdup");
6773 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6778 if (do_list || do_show) {
6779 /* Remove "cpath" promise. */
6780 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6786 error = apply_unveil(got_repo_get_path(repo), do_list,
6787 worktree ? got_worktree_get_root_path(worktree) : NULL);
6792 error = show_current_branch(repo, worktree);
6794 error = list_branches(repo, worktree, sort_by_time);
6796 error = delete_branch(repo, worktree, delref);
6798 struct got_reflist_head refs;
6800 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6804 if (commit_id_arg == NULL)
6805 commit_id_arg = worktree ?
6806 got_worktree_get_head_ref_name(worktree) :
6808 error = got_repo_match_object_id(&commit_id, NULL,
6809 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6810 got_ref_list_free(&refs);
6813 error = add_branch(repo, argv[0], commit_id);
6816 if (worktree && do_update) {
6817 struct got_update_progress_arg upa;
6818 char *branch_refname = NULL;
6820 error = got_object_id_str(&commit_id_str, commit_id);
6823 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6827 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6829 error = got_error_from_errno("asprintf");
6832 error = got_ref_open(&ref, repo, branch_refname, 0);
6833 free(branch_refname);
6836 error = switch_head_ref(ref, commit_id, worktree,
6840 error = got_worktree_set_base_commit_id(worktree, repo,
6844 memset(&upa, 0, sizeof(upa));
6845 error = got_worktree_checkout_files(worktree, &paths,
6846 repo, update_progress, &upa, check_cancelled,
6850 if (upa.did_something) {
6851 printf("Updated to %s: %s\n",
6852 got_worktree_get_head_ref_name(worktree),
6855 print_update_progress_stats(&upa);
6862 const struct got_error *close_err = got_repo_close(repo);
6867 got_worktree_close(worktree);
6869 const struct got_error *pack_err =
6870 got_repo_pack_fds_close(pack_fds);
6877 free(commit_id_str);
6878 TAILQ_FOREACH(pe, &paths, entry)
6879 free((char *)pe->path);
6880 got_pathlist_free(&paths);
6889 "usage: %s tag [-c commit] [-r repository] [-l] "
6890 "[-m message] [-s signer-id] [-v] [-V] name\n",
6896 static const struct got_error *
6897 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6899 const struct got_error *err = NULL;
6900 struct got_reflist_entry *re, *se, *new;
6901 struct got_object_id *re_id, *se_id;
6902 struct got_tag_object *re_tag, *se_tag;
6903 time_t re_time, se_time;
6905 STAILQ_FOREACH(re, tags, entry) {
6906 se = STAILQ_FIRST(sorted);
6908 err = got_reflist_entry_dup(&new, re);
6911 STAILQ_INSERT_HEAD(sorted, new, entry);
6914 err = got_ref_resolve(&re_id, repo, re->ref);
6917 err = got_object_open_as_tag(&re_tag, repo, re_id);
6921 re_time = got_object_tag_get_tagger_time(re_tag);
6922 got_object_tag_close(re_tag);
6926 err = got_ref_resolve(&se_id, repo, re->ref);
6929 err = got_object_open_as_tag(&se_tag, repo, se_id);
6933 se_time = got_object_tag_get_tagger_time(se_tag);
6934 got_object_tag_close(se_tag);
6936 if (se_time > re_time) {
6937 err = got_reflist_entry_dup(&new, re);
6940 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6943 se = STAILQ_NEXT(se, entry);
6952 static const struct got_error *
6953 get_tag_refname(char **refname, const char *tag_name)
6955 const struct got_error *err;
6957 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6958 *refname = strdup(tag_name);
6959 if (*refname == NULL)
6960 return got_error_from_errno("strdup");
6961 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6962 err = got_error_from_errno("asprintf");
6970 static const struct got_error *
6971 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6972 const char *allowed_signers, const char *revoked_signers, int verbosity)
6974 static const struct got_error *err = NULL;
6975 struct got_reflist_head refs;
6976 struct got_reflist_entry *re;
6977 char *wanted_refname = NULL;
6982 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6987 struct got_reference *ref;
6988 err = get_tag_refname(&wanted_refname, tag_name);
6991 /* Wanted tag reference should exist. */
6992 err = got_ref_open(&ref, repo, wanted_refname, 0);
6998 TAILQ_FOREACH(re, &refs, entry) {
6999 const char *refname;
7000 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7002 const char *tagger, *ssh_sig = NULL;
7003 char *sig_msg = NULL;
7005 struct got_object_id *id;
7006 struct got_tag_object *tag;
7007 struct got_commit_object *commit = NULL;
7009 refname = got_ref_get_name(re->ref);
7010 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7011 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7014 refstr = got_ref_to_str(re->ref);
7015 if (refstr == NULL) {
7016 err = got_error_from_errno("got_ref_to_str");
7020 err = got_ref_resolve(&id, repo, re->ref);
7023 err = got_object_open_as_tag(&tag, repo, id);
7025 if (err->code != GOT_ERR_OBJ_TYPE) {
7029 /* "lightweight" tag */
7030 err = got_object_open_as_commit(&commit, repo, id);
7035 tagger = got_object_commit_get_committer(commit);
7037 got_object_commit_get_committer_time(commit);
7038 err = got_object_id_str(&id_str, id);
7044 tagger = got_object_tag_get_tagger(tag);
7045 tagger_time = got_object_tag_get_tagger_time(tag);
7046 err = got_object_id_str(&id_str,
7047 got_object_tag_get_object_id(tag));
7053 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7054 got_object_tag_get_message(tag));
7055 if (ssh_sig && allowed_signers == NULL) {
7056 err = got_error_msg(
7057 GOT_ERR_VERIFY_TAG_SIGNATURE,
7058 "SSH signature verification requires "
7059 "setting allowed_signers in "
7065 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7067 printf("from: %s\n", tagger);
7068 datestr = get_datestr(&tagger_time, datebuf);
7070 printf("date: %s UTC\n", datestr);
7072 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7074 switch (got_object_tag_get_object_type(tag)) {
7075 case GOT_OBJ_TYPE_BLOB:
7076 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7079 case GOT_OBJ_TYPE_TREE:
7080 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7083 case GOT_OBJ_TYPE_COMMIT:
7084 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7087 case GOT_OBJ_TYPE_TAG:
7088 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7098 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7099 allowed_signers, revoked_signers, verbosity);
7100 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7104 printf("signature: %s", sig_msg);
7110 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7113 got_object_commit_close(commit);
7115 tagmsg0 = strdup(got_object_tag_get_message(tag));
7116 got_object_tag_close(tag);
7117 if (tagmsg0 == NULL) {
7118 err = got_error_from_errno("strdup");
7125 line = strsep(&tagmsg, "\n");
7127 printf(" %s\n", line);
7132 got_ref_list_free(&refs);
7133 free(wanted_refname);
7135 if (err == NULL && bad_sigs)
7136 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7140 static const struct got_error *
7141 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7142 const char *tag_name, const char *repo_path)
7144 const struct got_error *err = NULL;
7145 char *template = NULL, *initial_content = NULL;
7146 char *editor = NULL;
7147 int initial_content_len;
7150 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7151 err = got_error_from_errno("asprintf");
7155 initial_content_len = asprintf(&initial_content,
7156 "\n# tagging commit %s as %s\n",
7157 commit_id_str, tag_name);
7158 if (initial_content_len == -1) {
7159 err = got_error_from_errno("asprintf");
7163 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7167 if (write(fd, initial_content, initial_content_len) == -1) {
7168 err = got_error_from_errno2("write", *tagmsg_path);
7172 err = get_editor(&editor);
7175 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7176 initial_content_len, 1);
7178 free(initial_content);
7182 if (fd != -1 && close(fd) == -1 && err == NULL)
7183 err = got_error_from_errno2("close", *tagmsg_path);
7192 static const struct got_error *
7193 add_tag(struct got_repository *repo, const char *tagger,
7194 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7195 const char *signer_id, int verbosity)
7197 const struct got_error *err = NULL;
7198 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7199 char *label = NULL, *commit_id_str = NULL;
7200 struct got_reference *ref = NULL;
7201 char *refname = NULL, *tagmsg = NULL;
7202 char *tagmsg_path = NULL, *tag_id_str = NULL;
7203 int preserve_tagmsg = 0;
7204 struct got_reflist_head refs;
7209 * Don't let the user create a tag name with a leading '-'.
7210 * While technically a valid reference name, this case is usually
7211 * an unintended typo.
7213 if (tag_name[0] == '-')
7214 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7216 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7220 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7221 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7225 err = got_object_id_str(&commit_id_str, commit_id);
7229 err = get_tag_refname(&refname, tag_name);
7232 if (strncmp("refs/tags/", tag_name, 10) == 0)
7235 err = got_ref_open(&ref, repo, refname, 0);
7237 err = got_error(GOT_ERR_TAG_EXISTS);
7239 } else if (err->code != GOT_ERR_NOT_REF)
7242 if (tagmsg_arg == NULL) {
7243 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7244 tag_name, got_repo_get_path(repo));
7246 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7247 tagmsg_path != NULL)
7248 preserve_tagmsg = 1;
7251 /* Editor is done; we can now apply unveil(2) */
7252 err = got_sigs_apply_unveil();
7255 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7260 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7261 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7265 preserve_tagmsg = 1;
7269 err = got_ref_alloc(&ref, refname, tag_id);
7272 preserve_tagmsg = 1;
7276 err = got_ref_write(ref, repo);
7279 preserve_tagmsg = 1;
7283 err = got_object_id_str(&tag_id_str, tag_id);
7286 preserve_tagmsg = 1;
7289 printf("Created tag %s\n", tag_id_str);
7291 if (preserve_tagmsg) {
7292 fprintf(stderr, "%s: tag message preserved in %s\n",
7293 getprogname(), tagmsg_path);
7294 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7295 err = got_error_from_errno2("unlink", tagmsg_path);
7300 free(commit_id_str);
7304 got_ref_list_free(&refs);
7308 static const struct got_error *
7309 cmd_tag(int argc, char *argv[])
7311 const struct got_error *error = NULL;
7312 struct got_repository *repo = NULL;
7313 struct got_worktree *worktree = NULL;
7314 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7315 char *gitconfig_path = NULL, *tagger = NULL;
7316 char *allowed_signers = NULL, *revoked_signers = NULL;
7317 char *signer_id = NULL;
7318 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7319 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7320 int *pack_fds = NULL;
7322 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7325 commit_id_arg = optarg;
7331 repo_path = realpath(optarg, NULL);
7332 if (repo_path == NULL) {
7333 error = got_error_from_errno2("realpath",
7337 got_path_strip_trailing_slashes(repo_path);
7343 signer_id = strdup(optarg);
7344 if (signer_id == NULL) {
7345 error = got_error_from_errno("strdup");
7355 else if (verbosity < 3)
7367 if (do_list || verify_tags) {
7368 if (commit_id_arg != NULL)
7370 "-c option can only be used when creating a tag");
7373 option_conflict('l', 'm');
7375 option_conflict('V', 'm');
7379 option_conflict('l', 's');
7381 option_conflict('V', 's');
7385 } else if (argc != 1)
7392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7393 "sendfd unveil", NULL) == -1)
7396 cwd = getcwd(NULL, 0);
7398 error = got_error_from_errno("getcwd");
7402 error = got_repo_pack_fds_open(&pack_fds);
7406 if (repo_path == NULL) {
7407 error = got_worktree_open(&worktree, cwd);
7408 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7414 strdup(got_worktree_get_repo_path(worktree));
7415 if (repo_path == NULL)
7416 error = got_error_from_errno("strdup");
7420 repo_path = strdup(cwd);
7421 if (repo_path == NULL) {
7422 error = got_error_from_errno("strdup");
7428 if (do_list || verify_tags) {
7429 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7432 error = get_allowed_signers(&allowed_signers, repo, worktree);
7435 error = get_revoked_signers(&revoked_signers, repo, worktree);
7439 /* Release work tree lock. */
7440 got_worktree_close(worktree);
7445 * Remove "cpath" promise unless needed for signature tmpfile
7449 got_sigs_apply_unveil();
7452 if (pledge("stdio rpath wpath flock proc exec sendfd "
7453 "unveil", NULL) == -1)
7457 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7460 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7461 revoked_signers, verbosity);
7463 error = get_gitconfig_path(&gitconfig_path);
7466 error = got_repo_open(&repo, repo_path, gitconfig_path,
7471 error = get_author(&tagger, repo, worktree);
7474 if (signer_id == NULL) {
7475 error = get_signer_id(&signer_id, repo, worktree);
7480 /* Release work tree lock. */
7481 got_worktree_close(worktree);
7487 error = got_sigs_apply_unveil();
7491 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7496 if (commit_id_arg == NULL) {
7497 struct got_reference *head_ref;
7498 struct got_object_id *commit_id;
7499 error = got_ref_open(&head_ref, repo,
7500 worktree ? got_worktree_get_head_ref_name(worktree)
7504 error = got_ref_resolve(&commit_id, repo, head_ref);
7505 got_ref_close(head_ref);
7508 error = got_object_id_str(&commit_id_str, commit_id);
7514 error = add_tag(repo, tagger, tag_name,
7515 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7516 signer_id, verbosity);
7520 const struct got_error *close_err = got_repo_close(repo);
7525 got_worktree_close(worktree);
7527 const struct got_error *pack_err =
7528 got_repo_pack_fds_close(pack_fds);
7534 free(gitconfig_path);
7535 free(commit_id_str);
7537 free(allowed_signers);
7538 free(revoked_signers);
7546 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7551 static const struct got_error *
7552 add_progress(void *arg, unsigned char status, const char *path)
7554 while (path[0] == '/')
7556 printf("%c %s\n", status, path);
7560 static const struct got_error *
7561 cmd_add(int argc, char *argv[])
7563 const struct got_error *error = NULL;
7564 struct got_repository *repo = NULL;
7565 struct got_worktree *worktree = NULL;
7567 struct got_pathlist_head paths;
7568 struct got_pathlist_entry *pe;
7569 int ch, can_recurse = 0, no_ignores = 0;
7570 int *pack_fds = NULL;
7574 while ((ch = getopt(argc, argv, "IR")) != -1) {
7592 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7599 cwd = getcwd(NULL, 0);
7601 error = got_error_from_errno("getcwd");
7605 error = got_repo_pack_fds_open(&pack_fds);
7609 error = got_worktree_open(&worktree, cwd);
7611 if (error->code == GOT_ERR_NOT_WORKTREE)
7612 error = wrap_not_worktree_error(error, "add", cwd);
7616 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7621 error = apply_unveil(got_repo_get_path(repo), 1,
7622 got_worktree_get_root_path(worktree));
7626 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7633 TAILQ_FOREACH(pe, &paths, entry) {
7634 if (asprintf(&ondisk_path, "%s/%s",
7635 got_worktree_get_root_path(worktree),
7637 error = got_error_from_errno("asprintf");
7640 if (lstat(ondisk_path, &sb) == -1) {
7641 if (errno == ENOENT) {
7645 error = got_error_from_errno2("lstat",
7651 if (S_ISDIR(sb.st_mode)) {
7652 error = got_error_msg(GOT_ERR_BAD_PATH,
7653 "adding directories requires -R option");
7659 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7660 NULL, repo, no_ignores);
7663 const struct got_error *close_err = got_repo_close(repo);
7668 got_worktree_close(worktree);
7670 const struct got_error *pack_err =
7671 got_repo_pack_fds_close(pack_fds);
7675 TAILQ_FOREACH(pe, &paths, entry)
7676 free((char *)pe->path);
7677 got_pathlist_free(&paths);
7685 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7686 "path ...\n", getprogname());
7690 static const struct got_error *
7691 print_remove_status(void *arg, unsigned char status,
7692 unsigned char staged_status, const char *path)
7694 while (path[0] == '/')
7696 if (status == GOT_STATUS_NONEXISTENT)
7698 if (status == staged_status && (status == GOT_STATUS_DELETE))
7699 status = GOT_STATUS_NO_CHANGE;
7700 printf("%c%c %s\n", status, staged_status, path);
7704 static const struct got_error *
7705 cmd_remove(int argc, char *argv[])
7707 const struct got_error *error = NULL;
7708 struct got_worktree *worktree = NULL;
7709 struct got_repository *repo = NULL;
7710 const char *status_codes = NULL;
7712 struct got_pathlist_head paths;
7713 struct got_pathlist_entry *pe;
7714 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7715 int ignore_missing_paths = 0;
7716 int *pack_fds = NULL;
7720 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7723 delete_local_mods = 1;
7724 ignore_missing_paths = 1;
7733 for (i = 0; i < strlen(optarg); i++) {
7734 switch (optarg[i]) {
7735 case GOT_STATUS_MODIFY:
7736 delete_local_mods = 1;
7738 case GOT_STATUS_MISSING:
7739 ignore_missing_paths = 1;
7742 errx(1, "invalid status code '%c'",
7746 status_codes = optarg;
7758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7765 cwd = getcwd(NULL, 0);
7767 error = got_error_from_errno("getcwd");
7771 error = got_repo_pack_fds_open(&pack_fds);
7775 error = got_worktree_open(&worktree, cwd);
7777 if (error->code == GOT_ERR_NOT_WORKTREE)
7778 error = wrap_not_worktree_error(error, "remove", cwd);
7782 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7787 error = apply_unveil(got_repo_get_path(repo), 1,
7788 got_worktree_get_root_path(worktree));
7792 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7799 TAILQ_FOREACH(pe, &paths, entry) {
7800 if (asprintf(&ondisk_path, "%s/%s",
7801 got_worktree_get_root_path(worktree),
7803 error = got_error_from_errno("asprintf");
7806 if (lstat(ondisk_path, &sb) == -1) {
7807 if (errno == ENOENT) {
7811 error = got_error_from_errno2("lstat",
7817 if (S_ISDIR(sb.st_mode)) {
7818 error = got_error_msg(GOT_ERR_BAD_PATH,
7819 "removing directories requires -R option");
7825 error = got_worktree_schedule_delete(worktree, &paths,
7826 delete_local_mods, status_codes, print_remove_status, NULL,
7827 repo, keep_on_disk, ignore_missing_paths);
7830 const struct got_error *close_err = got_repo_close(repo);
7835 got_worktree_close(worktree);
7837 const struct got_error *pack_err =
7838 got_repo_pack_fds_close(pack_fds);
7842 TAILQ_FOREACH(pe, &paths, entry)
7843 free((char *)pe->path);
7844 got_pathlist_free(&paths);
7852 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7853 "[-R] [patchfile]\n", getprogname());
7857 static const struct got_error *
7858 patch_from_stdin(int *patchfd)
7860 const struct got_error *err = NULL;
7862 char *path, buf[BUFSIZ];
7863 sig_t sighup, sigint, sigquit;
7865 err = got_opentemp_named_fd(&path, patchfd,
7866 GOT_TMPDIR_STR "/got-patch");
7872 sighup = signal(SIGHUP, SIG_DFL);
7873 sigint = signal(SIGINT, SIG_DFL);
7874 sigquit = signal(SIGQUIT, SIG_DFL);
7877 r = read(0, buf, sizeof(buf));
7879 err = got_error_from_errno("read");
7884 if (write(*patchfd, buf, r) == -1) {
7885 err = got_error_from_errno("write");
7890 signal(SIGHUP, sighup);
7891 signal(SIGINT, sigint);
7892 signal(SIGQUIT, sigquit);
7894 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7895 err = got_error_from_errno("lseek");
7905 static const struct got_error *
7906 patch_progress(void *arg, const char *old, const char *new,
7907 unsigned char status, const struct got_error *error, int old_from,
7908 int old_lines, int new_from, int new_lines, int offset,
7909 int ws_mangled, const struct got_error *hunk_err)
7911 const char *path = new == NULL ? old : new;
7913 while (*path == '/')
7917 printf("%c %s\n", status, path);
7920 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7922 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7923 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7924 old_lines, new_from, new_lines);
7925 if (hunk_err != NULL)
7926 printf("%s\n", hunk_err->msg);
7927 else if (offset != 0)
7928 printf("applied with offset %d\n", offset);
7930 printf("hunk contains mangled whitespace\n");
7936 static const struct got_error *
7937 cmd_patch(int argc, char *argv[])
7939 const struct got_error *error = NULL, *close_error = NULL;
7940 struct got_worktree *worktree = NULL;
7941 struct got_repository *repo = NULL;
7944 int ch, nop = 0, strip = -1, reverse = 0;
7946 int *pack_fds = NULL;
7948 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7954 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7956 errx(1, "pathname strip count is %s: %s",
7972 error = patch_from_stdin(&patchfd);
7975 } else if (argc == 1) {
7976 patchfd = open(argv[0], O_RDONLY);
7977 if (patchfd == -1) {
7978 error = got_error_from_errno2("open", argv[0]);
7984 if ((cwd = getcwd(NULL, 0)) == NULL) {
7985 error = got_error_from_errno("getcwd");
7989 error = got_repo_pack_fds_open(&pack_fds);
7993 error = got_worktree_open(&worktree, cwd);
7997 const char *repo_path = got_worktree_get_repo_path(worktree);
7998 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8002 error = apply_unveil(got_repo_get_path(repo), 0,
8003 got_worktree_get_root_path(worktree));
8008 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8013 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8014 &patch_progress, NULL, check_cancelled, NULL);
8018 close_error = got_repo_close(repo);
8020 error = close_error;
8022 if (worktree != NULL) {
8023 close_error = got_worktree_close(worktree);
8025 error = close_error;
8028 const struct got_error *pack_err =
8029 got_repo_pack_fds_close(pack_fds);
8040 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8041 "path ...\n", getprogname());
8045 static const struct got_error *
8046 revert_progress(void *arg, unsigned char status, const char *path)
8048 if (status == GOT_STATUS_UNVERSIONED)
8051 while (path[0] == '/')
8053 printf("%c %s\n", status, path);
8057 struct choose_patch_arg {
8058 FILE *patch_script_file;
8062 static const struct got_error *
8063 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8064 int nchanges, const char *action)
8066 const struct got_error *err;
8068 size_t linesize = 0;
8072 case GOT_STATUS_ADD:
8073 printf("A %s\n%s this addition? [y/n] ", path, action);
8075 case GOT_STATUS_DELETE:
8076 printf("D %s\n%s this deletion? [y/n] ", path, action);
8078 case GOT_STATUS_MODIFY:
8079 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8080 return got_error_from_errno("fseek");
8081 printf(GOT_COMMIT_SEP_STR);
8082 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8084 if (linelen == -1 && ferror(patch_file)) {
8085 err = got_error_from_errno("getline");
8090 printf(GOT_COMMIT_SEP_STR);
8091 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8092 path, n, nchanges, action);
8095 return got_error_path(path, GOT_ERR_FILE_STATUS);
8101 static const struct got_error *
8102 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8103 FILE *patch_file, int n, int nchanges)
8105 const struct got_error *err = NULL;
8107 size_t linesize = 0;
8110 struct choose_patch_arg *a = arg;
8112 *choice = GOT_PATCH_CHOICE_NONE;
8114 if (a->patch_script_file) {
8116 err = show_change(status, path, patch_file, n, nchanges,
8120 linelen = getline(&line, &linesize, a->patch_script_file);
8121 if (linelen == -1) {
8122 if (ferror(a->patch_script_file))
8123 return got_error_from_errno("getline");
8126 nl = strchr(line, '\n');
8129 if (strcmp(line, "y") == 0) {
8130 *choice = GOT_PATCH_CHOICE_YES;
8132 } else if (strcmp(line, "n") == 0) {
8133 *choice = GOT_PATCH_CHOICE_NO;
8135 } else if (strcmp(line, "q") == 0 &&
8136 status == GOT_STATUS_MODIFY) {
8137 *choice = GOT_PATCH_CHOICE_QUIT;
8140 printf("invalid response '%s'\n", line);
8145 while (resp != 'y' && resp != 'n' && resp != 'q') {
8146 err = show_change(status, path, patch_file, n, nchanges,
8153 if (status == GOT_STATUS_MODIFY) {
8154 if (resp != 'y' && resp != 'n' && resp != 'q') {
8155 printf("invalid response '%c'\n", resp);
8158 } else if (resp != 'y' && resp != 'n') {
8159 printf("invalid response '%c'\n", resp);
8165 *choice = GOT_PATCH_CHOICE_YES;
8166 else if (resp == 'n')
8167 *choice = GOT_PATCH_CHOICE_NO;
8168 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8169 *choice = GOT_PATCH_CHOICE_QUIT;
8174 static const struct got_error *
8175 cmd_revert(int argc, char *argv[])
8177 const struct got_error *error = NULL;
8178 struct got_worktree *worktree = NULL;
8179 struct got_repository *repo = NULL;
8180 char *cwd = NULL, *path = NULL;
8181 struct got_pathlist_head paths;
8182 struct got_pathlist_entry *pe;
8183 int ch, can_recurse = 0, pflag = 0;
8184 FILE *patch_script_file = NULL;
8185 const char *patch_script_path = NULL;
8186 struct choose_patch_arg cpa;
8187 int *pack_fds = NULL;
8191 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8197 patch_script_path = optarg;
8212 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8213 "unveil", NULL) == -1)
8218 if (patch_script_path && !pflag)
8219 errx(1, "-F option can only be used together with -p option");
8221 cwd = getcwd(NULL, 0);
8223 error = got_error_from_errno("getcwd");
8227 error = got_repo_pack_fds_open(&pack_fds);
8231 error = got_worktree_open(&worktree, cwd);
8233 if (error->code == GOT_ERR_NOT_WORKTREE)
8234 error = wrap_not_worktree_error(error, "revert", cwd);
8238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8243 if (patch_script_path) {
8244 patch_script_file = fopen(patch_script_path, "re");
8245 if (patch_script_file == NULL) {
8246 error = got_error_from_errno2("fopen",
8251 error = apply_unveil(got_repo_get_path(repo), 1,
8252 got_worktree_get_root_path(worktree));
8256 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8263 TAILQ_FOREACH(pe, &paths, entry) {
8264 if (asprintf(&ondisk_path, "%s/%s",
8265 got_worktree_get_root_path(worktree),
8267 error = got_error_from_errno("asprintf");
8270 if (lstat(ondisk_path, &sb) == -1) {
8271 if (errno == ENOENT) {
8275 error = got_error_from_errno2("lstat",
8281 if (S_ISDIR(sb.st_mode)) {
8282 error = got_error_msg(GOT_ERR_BAD_PATH,
8283 "reverting directories requires -R option");
8289 cpa.patch_script_file = patch_script_file;
8290 cpa.action = "revert";
8291 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8292 pflag ? choose_patch : NULL, &cpa, repo);
8294 if (patch_script_file && fclose(patch_script_file) == EOF &&
8296 error = got_error_from_errno2("fclose", patch_script_path);
8298 const struct got_error *close_err = got_repo_close(repo);
8303 got_worktree_close(worktree);
8305 const struct got_error *pack_err =
8306 got_repo_pack_fds_close(pack_fds);
8318 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8319 "[path ...]\n", getprogname());
8323 struct collect_commit_logmsg_arg {
8324 const char *cmdline_log;
8325 const char *prepared_log;
8326 int non_interactive;
8328 const char *worktree_path;
8329 const char *branch_name;
8330 const char *repo_path;
8335 static const struct got_error *
8336 read_prepared_logmsg(char **logmsg, const char *path)
8338 const struct got_error *err = NULL;
8344 memset(&sb, 0, sizeof(sb));
8346 f = fopen(path, "re");
8348 return got_error_from_errno2("fopen", path);
8350 if (fstat(fileno(f), &sb) == -1) {
8351 err = got_error_from_errno2("fstat", path);
8354 if (sb.st_size == 0) {
8355 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8359 *logmsg = malloc(sb.st_size + 1);
8360 if (*logmsg == NULL) {
8361 err = got_error_from_errno("malloc");
8365 r = fread(*logmsg, 1, sb.st_size, f);
8366 if (r != sb.st_size) {
8368 err = got_error_from_errno2("fread", path);
8370 err = got_error(GOT_ERR_IO);
8373 (*logmsg)[sb.st_size] = '\0';
8375 if (fclose(f) == EOF && err == NULL)
8376 err = got_error_from_errno2("fclose", path);
8385 static const struct got_error *
8386 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8389 char *initial_content = NULL;
8390 struct got_pathlist_entry *pe;
8391 const struct got_error *err = NULL;
8392 char *template = NULL;
8393 struct collect_commit_logmsg_arg *a = arg;
8394 int initial_content_len;
8398 /* if a message was specified on the command line, just use it */
8399 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8400 len = strlen(a->cmdline_log) + 1;
8401 *logmsg = malloc(len + 1);
8402 if (*logmsg == NULL)
8403 return got_error_from_errno("malloc");
8404 strlcpy(*logmsg, a->cmdline_log, len);
8406 } else if (a->prepared_log != NULL && a->non_interactive)
8407 return read_prepared_logmsg(logmsg, a->prepared_log);
8409 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8410 return got_error_from_errno("asprintf");
8412 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8416 if (a->prepared_log) {
8418 err = read_prepared_logmsg(&msg, a->prepared_log);
8421 if (write(fd, msg, strlen(msg)) == -1) {
8422 err = got_error_from_errno2("write", a->logmsg_path);
8429 initial_content_len = asprintf(&initial_content,
8430 "\n# changes to be committed on branch %s:\n",
8432 if (initial_content_len == -1) {
8433 err = got_error_from_errno("asprintf");
8437 if (write(fd, initial_content, initial_content_len) == -1) {
8438 err = got_error_from_errno2("write", a->logmsg_path);
8442 TAILQ_FOREACH(pe, commitable_paths, entry) {
8443 struct got_commitable *ct = pe->data;
8444 dprintf(fd, "# %c %s\n",
8445 got_commitable_get_status(ct),
8446 got_commitable_get_path(ct));
8449 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8450 initial_content_len, a->prepared_log ? 0 : 1);
8452 free(initial_content);
8455 if (fd != -1 && close(fd) == -1 && err == NULL)
8456 err = got_error_from_errno2("close", a->logmsg_path);
8458 /* Editor is done; we can now apply unveil(2) */
8460 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8468 static const struct got_error *
8469 cmd_commit(int argc, char *argv[])
8471 const struct got_error *error = NULL;
8472 struct got_worktree *worktree = NULL;
8473 struct got_repository *repo = NULL;
8474 char *cwd = NULL, *id_str = NULL;
8475 struct got_object_id *id = NULL;
8476 const char *logmsg = NULL;
8477 char *prepared_logmsg = NULL;
8478 struct collect_commit_logmsg_arg cl_arg;
8479 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8480 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8481 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8482 struct got_pathlist_head paths;
8483 int *pack_fds = NULL;
8486 cl_arg.logmsg_path = NULL;
8488 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8492 option_conflict('F', 'm');
8493 prepared_logmsg = realpath(optarg, NULL);
8494 if (prepared_logmsg == NULL)
8495 return got_error_from_errno2("realpath",
8499 if (prepared_logmsg)
8500 option_conflict('m', 'F');
8504 non_interactive = 1;
8507 allow_bad_symlinks = 1;
8519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8520 "unveil", NULL) == -1)
8523 cwd = getcwd(NULL, 0);
8525 error = got_error_from_errno("getcwd");
8529 error = got_repo_pack_fds_open(&pack_fds);
8533 error = got_worktree_open(&worktree, cwd);
8535 if (error->code == GOT_ERR_NOT_WORKTREE)
8536 error = wrap_not_worktree_error(error, "commit", cwd);
8540 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8543 if (rebase_in_progress) {
8544 error = got_error(GOT_ERR_REBASING);
8548 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8553 error = get_gitconfig_path(&gitconfig_path);
8556 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8557 gitconfig_path, pack_fds);
8561 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8564 if (merge_in_progress) {
8565 error = got_error(GOT_ERR_MERGE_BUSY);
8569 error = get_author(&author, repo, worktree);
8574 * unveil(2) traverses exec(2); if an editor is used we have
8575 * to apply unveil after the log message has been written.
8577 if (logmsg == NULL || strlen(logmsg) == 0)
8578 error = get_editor(&editor);
8580 error = apply_unveil(got_repo_get_path(repo), 0,
8581 got_worktree_get_root_path(worktree));
8585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8589 cl_arg.editor = editor;
8590 cl_arg.cmdline_log = logmsg;
8591 cl_arg.prepared_log = prepared_logmsg;
8592 cl_arg.non_interactive = non_interactive;
8593 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8594 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8595 if (!histedit_in_progress) {
8596 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8597 error = got_error(GOT_ERR_COMMIT_BRANCH);
8600 cl_arg.branch_name += 11;
8602 cl_arg.repo_path = got_repo_get_path(repo);
8603 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8604 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8605 print_status, NULL, repo);
8607 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8608 cl_arg.logmsg_path != NULL)
8609 preserve_logmsg = 1;
8613 error = got_object_id_str(&id_str, id);
8616 printf("Created commit %s\n", id_str);
8618 if (preserve_logmsg) {
8619 fprintf(stderr, "%s: log message preserved in %s\n",
8620 getprogname(), cl_arg.logmsg_path);
8621 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8623 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8624 free(cl_arg.logmsg_path);
8626 const struct got_error *close_err = got_repo_close(repo);
8631 got_worktree_close(worktree);
8633 const struct got_error *pack_err =
8634 got_repo_pack_fds_close(pack_fds);
8640 free(gitconfig_path);
8643 free(prepared_logmsg);
8650 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8651 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8652 "[remote-repository]\n", getprogname());
8657 print_load_info(int print_colored, int print_found, int print_trees,
8658 int ncolored, int nfound, int ntrees)
8660 if (print_colored) {
8661 printf("%d commit%s colored", ncolored,
8662 ncolored == 1 ? "" : "s");
8665 printf("%s%d object%s found",
8666 ncolored > 0 ? "; " : "",
8667 nfound, nfound == 1 ? "" : "s");
8670 printf("; %d tree%s scanned", ntrees,
8671 ntrees == 1 ? "" : "s");
8675 struct got_send_progress_arg {
8676 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8683 int last_nobj_total;
8687 int printed_something;
8689 struct got_pathlist_head *delete_branches;
8692 static const struct got_error *
8693 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8694 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8695 int nobj_written, off_t bytes_sent, const char *refname, int success)
8697 struct got_send_progress_arg *a = arg;
8698 char scaled_packsize[FMT_SCALED_STRSIZE];
8699 char scaled_sent[FMT_SCALED_STRSIZE];
8700 int p_deltify = 0, p_written = 0, p_sent = 0;
8701 int print_colored = 0, print_found = 0, print_trees = 0;
8702 int print_searching = 0, print_total = 0;
8703 int print_deltify = 0, print_written = 0, print_sent = 0;
8705 if (a->verbosity < 0)
8709 const char *status = success ? "accepted" : "rejected";
8712 struct got_pathlist_entry *pe;
8713 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8714 const char *branchname = pe->path;
8715 if (got_path_cmp(branchname, refname,
8716 strlen(branchname), strlen(refname)) == 0) {
8718 a->sent_something = 1;
8724 if (a->printed_something)
8726 printf("Server has %s %s", status, refname);
8727 a->printed_something = 1;
8731 if (a->last_ncolored != ncolored) {
8733 a->last_ncolored = ncolored;
8736 if (a->last_nfound != nfound) {
8739 a->last_nfound = nfound;
8742 if (a->last_ntrees != ntrees) {
8746 a->last_ntrees = ntrees;
8749 if ((print_colored || print_found || print_trees) &&
8752 print_load_info(print_colored, print_found, print_trees,
8753 ncolored, nfound, ntrees);
8754 a->printed_something = 1;
8757 } else if (!a->loading_done) {
8759 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8761 a->loading_done = 1;
8764 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8765 return got_error_from_errno("fmt_scaled");
8766 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8767 return got_error_from_errno("fmt_scaled");
8769 if (a->last_ncommits != ncommits) {
8770 print_searching = 1;
8771 a->last_ncommits = ncommits;
8774 if (a->last_nobj_total != nobj_total) {
8775 print_searching = 1;
8777 a->last_nobj_total = nobj_total;
8780 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8781 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8782 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8783 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8784 return got_error(GOT_ERR_NO_SPACE);
8787 if (nobj_deltify > 0 || nobj_written > 0) {
8788 if (nobj_deltify > 0) {
8789 p_deltify = (nobj_deltify * 100) / nobj_total;
8790 if (p_deltify != a->last_p_deltify) {
8791 a->last_p_deltify = p_deltify;
8792 print_searching = 1;
8797 if (nobj_written > 0) {
8798 p_written = (nobj_written * 100) / nobj_total;
8799 if (p_written != a->last_p_written) {
8800 a->last_p_written = p_written;
8801 print_searching = 1;
8809 if (bytes_sent > 0) {
8810 p_sent = (bytes_sent * 100) / packfile_size;
8811 if (p_sent != a->last_p_sent) {
8812 a->last_p_sent = p_sent;
8813 print_searching = 1;
8819 a->sent_something = 1;
8822 if (print_searching || print_total || print_deltify || print_written ||
8825 if (print_searching)
8826 printf("packing %d reference%s", ncommits,
8827 ncommits == 1 ? "" : "s");
8829 printf("; %d object%s", nobj_total,
8830 nobj_total == 1 ? "" : "s");
8832 printf("; deltify: %d%%", p_deltify);
8834 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8835 scaled_packsize, p_sent);
8836 else if (print_written)
8837 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8838 scaled_packsize, p_written);
8839 if (print_searching || print_total || print_deltify ||
8840 print_written || print_sent) {
8841 a->printed_something = 1;
8847 static const struct got_error *
8848 cmd_send(int argc, char *argv[])
8850 const struct got_error *error = NULL;
8851 char *cwd = NULL, *repo_path = NULL;
8852 const char *remote_name;
8853 char *proto = NULL, *host = NULL, *port = NULL;
8854 char *repo_name = NULL, *server_path = NULL;
8855 const struct got_remote_repo *remotes, *remote = NULL;
8856 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8857 struct got_repository *repo = NULL;
8858 struct got_worktree *worktree = NULL;
8859 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8860 struct got_pathlist_head branches;
8861 struct got_pathlist_head tags;
8862 struct got_reflist_head all_branches;
8863 struct got_reflist_head all_tags;
8864 struct got_pathlist_head delete_args;
8865 struct got_pathlist_head delete_branches;
8866 struct got_reflist_entry *re;
8867 struct got_pathlist_entry *pe;
8868 int i, ch, sendfd = -1, sendstatus;
8870 struct got_send_progress_arg spa;
8871 int verbosity = 0, overwrite_refs = 0;
8872 int send_all_branches = 0, send_all_tags = 0;
8873 struct got_reference *ref = NULL;
8874 int *pack_fds = NULL;
8876 TAILQ_INIT(&branches);
8878 TAILQ_INIT(&all_branches);
8879 TAILQ_INIT(&all_tags);
8880 TAILQ_INIT(&delete_args);
8881 TAILQ_INIT(&delete_branches);
8883 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8886 send_all_branches = 1;
8889 error = got_pathlist_append(&branches, optarg, NULL);
8895 error = got_pathlist_append(&delete_args, optarg, NULL);
8903 repo_path = realpath(optarg, NULL);
8904 if (repo_path == NULL)
8905 return got_error_from_errno2("realpath",
8907 got_path_strip_trailing_slashes(repo_path);
8910 error = got_pathlist_append(&tags, optarg, NULL);
8921 else if (verbosity < 3)
8935 if (send_all_branches && !TAILQ_EMPTY(&branches))
8936 option_conflict('a', 'b');
8937 if (send_all_tags && !TAILQ_EMPTY(&tags))
8938 option_conflict('T', 't');
8942 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8944 remote_name = argv[0];
8948 cwd = getcwd(NULL, 0);
8950 error = got_error_from_errno("getcwd");
8954 error = got_repo_pack_fds_open(&pack_fds);
8958 if (repo_path == NULL) {
8959 error = got_worktree_open(&worktree, cwd);
8960 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8966 strdup(got_worktree_get_repo_path(worktree));
8967 if (repo_path == NULL)
8968 error = got_error_from_errno("strdup");
8972 repo_path = strdup(cwd);
8973 if (repo_path == NULL) {
8974 error = got_error_from_errno("strdup");
8980 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8985 worktree_conf = got_worktree_get_gotconfig(worktree);
8986 if (worktree_conf) {
8987 got_gotconfig_get_remotes(&nremotes, &remotes,
8989 for (i = 0; i < nremotes; i++) {
8990 if (strcmp(remotes[i].name, remote_name) == 0) {
8991 remote = &remotes[i];
8997 if (remote == NULL) {
8998 repo_conf = got_repo_get_gotconfig(repo);
9000 got_gotconfig_get_remotes(&nremotes, &remotes,
9002 for (i = 0; i < nremotes; i++) {
9003 if (strcmp(remotes[i].name, remote_name) == 0) {
9004 remote = &remotes[i];
9010 if (remote == NULL) {
9011 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9012 for (i = 0; i < nremotes; i++) {
9013 if (strcmp(remotes[i].name, remote_name) == 0) {
9014 remote = &remotes[i];
9019 if (remote == NULL) {
9020 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9024 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9025 &repo_name, remote->send_url);
9029 if (strcmp(proto, "git") == 0) {
9031 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9032 "sendfd dns inet unveil", NULL) == -1)
9035 } else if (strcmp(proto, "git+ssh") == 0 ||
9036 strcmp(proto, "ssh") == 0) {
9038 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9039 "sendfd unveil", NULL) == -1)
9042 } else if (strcmp(proto, "http") == 0 ||
9043 strcmp(proto, "git+http") == 0) {
9044 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9047 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9051 error = got_dial_apply_unveil(proto);
9055 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9059 if (send_all_branches) {
9060 error = got_ref_list(&all_branches, repo, "refs/heads",
9061 got_ref_cmp_by_name, NULL);
9064 TAILQ_FOREACH(re, &all_branches, entry) {
9065 const char *branchname = got_ref_get_name(re->ref);
9066 error = got_pathlist_append(&branches,
9072 } else if (nbranches == 0) {
9073 for (i = 0; i < remote->nsend_branches; i++) {
9074 got_pathlist_append(&branches,
9075 remote->send_branches[i], NULL);
9079 if (send_all_tags) {
9080 error = got_ref_list(&all_tags, repo, "refs/tags",
9081 got_ref_cmp_by_name, NULL);
9084 TAILQ_FOREACH(re, &all_tags, entry) {
9085 const char *tagname = got_ref_get_name(re->ref);
9086 error = got_pathlist_append(&tags,
9095 * To prevent accidents only branches in refs/heads/ can be deleted
9096 * with 'got send -d'.
9097 * Deleting anything else requires local repository access or Git.
9099 TAILQ_FOREACH(pe, &delete_args, entry) {
9100 const char *branchname = pe->path;
9102 struct got_pathlist_entry *new;
9103 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9104 s = strdup(branchname);
9106 error = got_error_from_errno("strdup");
9110 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9111 error = got_error_from_errno("asprintf");
9115 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9116 if (error || new == NULL /* duplicate */)
9123 if (nbranches == 0 && ndelete_branches == 0) {
9124 struct got_reference *head_ref;
9126 error = got_ref_open(&head_ref, repo,
9127 got_worktree_get_head_ref_name(worktree), 0);
9129 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9132 if (got_ref_is_symbolic(head_ref)) {
9133 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9134 got_ref_close(head_ref);
9139 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9147 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9148 port ? ":" : "", port ? port : "");
9150 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9151 server_path, verbosity);
9155 memset(&spa, 0, sizeof(spa));
9156 spa.last_scaled_packsize[0] = '\0';
9157 spa.last_p_deltify = -1;
9158 spa.last_p_written = -1;
9159 spa.verbosity = verbosity;
9160 spa.delete_branches = &delete_branches;
9161 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9162 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9163 check_cancelled, NULL);
9164 if (spa.printed_something)
9168 if (!spa.sent_something && verbosity >= 0)
9169 printf("Already up-to-date\n");
9172 if (kill(sendpid, SIGTERM) == -1)
9173 error = got_error_from_errno("kill");
9174 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9175 error = got_error_from_errno("waitpid");
9177 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9178 error = got_error_from_errno("close");
9180 const struct got_error *close_err = got_repo_close(repo);
9185 got_worktree_close(worktree);
9187 const struct got_error *pack_err =
9188 got_repo_pack_fds_close(pack_fds);
9194 got_pathlist_free(&branches);
9195 got_pathlist_free(&tags);
9196 got_ref_list_free(&all_branches);
9197 got_ref_list_free(&all_tags);
9198 got_pathlist_free(&delete_args);
9199 TAILQ_FOREACH(pe, &delete_branches, entry)
9200 free((char *)pe->path);
9201 got_pathlist_free(&delete_branches);
9213 usage_cherrypick(void)
9215 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9219 static const struct got_error *
9220 cmd_cherrypick(int argc, char *argv[])
9222 const struct got_error *error = NULL;
9223 struct got_worktree *worktree = NULL;
9224 struct got_repository *repo = NULL;
9225 char *cwd = NULL, *commit_id_str = NULL;
9226 struct got_object_id *commit_id = NULL;
9227 struct got_commit_object *commit = NULL;
9228 struct got_object_qid *pid;
9230 struct got_update_progress_arg upa;
9231 int *pack_fds = NULL;
9233 while ((ch = getopt(argc, argv, "")) != -1) {
9245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9246 "unveil", NULL) == -1)
9252 cwd = getcwd(NULL, 0);
9254 error = got_error_from_errno("getcwd");
9258 error = got_repo_pack_fds_open(&pack_fds);
9262 error = got_worktree_open(&worktree, cwd);
9264 if (error->code == GOT_ERR_NOT_WORKTREE)
9265 error = wrap_not_worktree_error(error, "cherrypick",
9270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9275 error = apply_unveil(got_repo_get_path(repo), 0,
9276 got_worktree_get_root_path(worktree));
9280 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9281 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9284 error = got_object_id_str(&commit_id_str, commit_id);
9288 error = got_object_open_as_commit(&commit, repo, commit_id);
9291 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9292 memset(&upa, 0, sizeof(upa));
9293 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9294 commit_id, repo, update_progress, &upa, check_cancelled,
9299 if (upa.did_something)
9300 printf("Merged commit %s\n", commit_id_str);
9301 print_merge_progress_stats(&upa);
9304 got_object_commit_close(commit);
9305 free(commit_id_str);
9307 got_worktree_close(worktree);
9309 const struct got_error *close_err = got_repo_close(repo);
9314 const struct got_error *pack_err =
9315 got_repo_pack_fds_close(pack_fds);
9326 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9330 static const struct got_error *
9331 cmd_backout(int argc, char *argv[])
9333 const struct got_error *error = NULL;
9334 struct got_worktree *worktree = NULL;
9335 struct got_repository *repo = NULL;
9336 char *cwd = NULL, *commit_id_str = NULL;
9337 struct got_object_id *commit_id = NULL;
9338 struct got_commit_object *commit = NULL;
9339 struct got_object_qid *pid;
9341 struct got_update_progress_arg upa;
9342 int *pack_fds = NULL;
9344 while ((ch = getopt(argc, argv, "")) != -1) {
9356 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9357 "unveil", NULL) == -1)
9363 cwd = getcwd(NULL, 0);
9365 error = got_error_from_errno("getcwd");
9369 error = got_repo_pack_fds_open(&pack_fds);
9373 error = got_worktree_open(&worktree, cwd);
9375 if (error->code == GOT_ERR_NOT_WORKTREE)
9376 error = wrap_not_worktree_error(error, "backout", cwd);
9380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9385 error = apply_unveil(got_repo_get_path(repo), 0,
9386 got_worktree_get_root_path(worktree));
9390 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9391 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9394 error = got_object_id_str(&commit_id_str, commit_id);
9398 error = got_object_open_as_commit(&commit, repo, commit_id);
9401 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9403 error = got_error(GOT_ERR_ROOT_COMMIT);
9407 memset(&upa, 0, sizeof(upa));
9408 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9409 repo, update_progress, &upa, check_cancelled, NULL);
9413 if (upa.did_something)
9414 printf("Backed out commit %s\n", commit_id_str);
9415 print_merge_progress_stats(&upa);
9418 got_object_commit_close(commit);
9419 free(commit_id_str);
9421 got_worktree_close(worktree);
9423 const struct got_error *close_err = got_repo_close(repo);
9428 const struct got_error *pack_err =
9429 got_repo_pack_fds_close(pack_fds);
9439 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9445 trim_logmsg(char *logmsg, int limit)
9450 len = strlen(logmsg);
9454 nl = strchr(logmsg, '\n');
9459 static const struct got_error *
9460 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9462 const struct got_error *err;
9463 char *logmsg0 = NULL;
9466 err = got_object_commit_get_logmsg(&logmsg0, commit);
9471 while (isspace((unsigned char)s[0]))
9474 *logmsg = strdup(s);
9475 if (*logmsg == NULL) {
9476 err = got_error_from_errno("strdup");
9480 trim_logmsg(*logmsg, limit);
9486 static const struct got_error *
9487 show_rebase_merge_conflict(struct got_object_id *id,
9488 struct got_repository *repo)
9490 const struct got_error *err;
9491 struct got_commit_object *commit = NULL;
9492 char *id_str = NULL, *logmsg = NULL;
9494 err = got_object_open_as_commit(&commit, repo, id);
9498 err = got_object_id_str(&id_str, id);
9504 err = get_short_logmsg(&logmsg, 42, commit);
9508 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9511 got_object_commit_close(commit);
9516 static const struct got_error *
9517 show_rebase_progress(struct got_commit_object *commit,
9518 struct got_object_id *old_id, struct got_object_id *new_id)
9520 const struct got_error *err;
9521 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9523 err = got_object_id_str(&old_id_str, old_id);
9528 err = got_object_id_str(&new_id_str, new_id);
9533 old_id_str[12] = '\0';
9535 new_id_str[12] = '\0';
9537 err = get_short_logmsg(&logmsg, 42, commit);
9541 printf("%s -> %s: %s\n", old_id_str,
9542 new_id_str ? new_id_str : "no-op change", logmsg);
9550 static const struct got_error *
9551 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9552 struct got_reference *branch, struct got_reference *new_base_branch,
9553 struct got_reference *tmp_branch, struct got_repository *repo,
9556 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9557 return got_worktree_rebase_complete(worktree, fileindex,
9558 new_base_branch, tmp_branch, branch, repo, create_backup);
9561 static const struct got_error *
9562 rebase_commit(struct got_pathlist_head *merged_paths,
9563 struct got_worktree *worktree, struct got_fileindex *fileindex,
9564 struct got_reference *tmp_branch,
9565 struct got_object_id *commit_id, struct got_repository *repo)
9567 const struct got_error *error;
9568 struct got_commit_object *commit;
9569 struct got_object_id *new_commit_id;
9571 error = got_object_open_as_commit(&commit, repo, commit_id);
9575 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9576 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9578 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9580 error = show_rebase_progress(commit, commit_id, NULL);
9582 error = show_rebase_progress(commit, commit_id, new_commit_id);
9583 free(new_commit_id);
9586 got_object_commit_close(commit);
9590 struct check_path_prefix_arg {
9591 const char *path_prefix;
9596 static const struct got_error *
9597 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9598 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9599 struct got_object_id *id1, struct got_object_id *id2,
9600 const char *path1, const char *path2,
9601 mode_t mode1, mode_t mode2, struct got_repository *repo)
9603 struct check_path_prefix_arg *a = arg;
9605 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9606 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9607 return got_error(a->errcode);
9612 static const struct got_error *
9613 check_path_prefix(struct got_object_id *parent_id,
9614 struct got_object_id *commit_id, const char *path_prefix,
9615 int errcode, struct got_repository *repo)
9617 const struct got_error *err;
9618 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9619 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9620 struct check_path_prefix_arg cpp_arg;
9622 if (got_path_is_root_dir(path_prefix))
9625 err = got_object_open_as_commit(&commit, repo, commit_id);
9629 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9633 err = got_object_open_as_tree(&tree1, repo,
9634 got_object_commit_get_tree_id(parent_commit));
9638 err = got_object_open_as_tree(&tree2, repo,
9639 got_object_commit_get_tree_id(commit));
9643 cpp_arg.path_prefix = path_prefix;
9644 while (cpp_arg.path_prefix[0] == '/')
9645 cpp_arg.path_prefix++;
9646 cpp_arg.len = strlen(cpp_arg.path_prefix);
9647 cpp_arg.errcode = errcode;
9648 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9649 check_path_prefix_in_diff, &cpp_arg, 0);
9652 got_object_tree_close(tree1);
9654 got_object_tree_close(tree2);
9656 got_object_commit_close(commit);
9658 got_object_commit_close(parent_commit);
9662 static const struct got_error *
9663 collect_commits(struct got_object_id_queue *commits,
9664 struct got_object_id *initial_commit_id,
9665 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9666 const char *path_prefix, int path_prefix_errcode,
9667 struct got_repository *repo)
9669 const struct got_error *err = NULL;
9670 struct got_commit_graph *graph = NULL;
9671 struct got_object_id *parent_id = NULL;
9672 struct got_object_qid *qid;
9673 struct got_object_id *commit_id = initial_commit_id;
9675 err = got_commit_graph_open(&graph, "/", 1);
9679 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9680 check_cancelled, NULL);
9683 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9684 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9685 check_cancelled, NULL);
9687 if (err->code == GOT_ERR_ITER_COMPLETED) {
9688 err = got_error_msg(GOT_ERR_ANCESTRY,
9689 "ran out of commits to rebase before "
9690 "youngest common ancestor commit has "
9695 err = check_path_prefix(parent_id, commit_id,
9696 path_prefix, path_prefix_errcode, repo);
9700 err = got_object_qid_alloc(&qid, commit_id);
9703 STAILQ_INSERT_HEAD(commits, qid, entry);
9704 commit_id = parent_id;
9708 got_commit_graph_close(graph);
9712 static const struct got_error *
9713 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9715 const struct got_error *err = NULL;
9716 time_t committer_time;
9718 char datebuf[11]; /* YYYY-MM-DD + NUL */
9719 char *author0 = NULL, *author, *smallerthan;
9720 char *logmsg0 = NULL, *logmsg, *newline;
9722 committer_time = got_object_commit_get_committer_time(commit);
9723 if (gmtime_r(&committer_time, &tm) == NULL)
9724 return got_error_from_errno("gmtime_r");
9725 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9726 return got_error(GOT_ERR_NO_SPACE);
9728 author0 = strdup(got_object_commit_get_author(commit));
9729 if (author0 == NULL)
9730 return got_error_from_errno("strdup");
9732 smallerthan = strchr(author, '<');
9733 if (smallerthan && smallerthan[1] != '\0')
9734 author = smallerthan + 1;
9735 author[strcspn(author, "@>")] = '\0';
9737 err = got_object_commit_get_logmsg(&logmsg0, commit);
9741 while (*logmsg == '\n')
9743 newline = strchr(logmsg, '\n');
9747 if (asprintf(brief_str, "%s %s %s",
9748 datebuf, author, logmsg) == -1)
9749 err = got_error_from_errno("asprintf");
9756 static const struct got_error *
9757 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9758 struct got_repository *repo)
9760 const struct got_error *err;
9763 err = got_object_id_str(&id_str, id);
9767 err = got_ref_delete(ref, repo);
9771 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9777 static const struct got_error *
9778 print_backup_ref(const char *branch_name, const char *new_id_str,
9779 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9780 struct got_reflist_object_id_map *refs_idmap,
9781 struct got_repository *repo)
9783 const struct got_error *err = NULL;
9784 struct got_reflist_head *refs;
9785 char *refs_str = NULL;
9786 struct got_object_id *new_commit_id = NULL;
9787 struct got_commit_object *new_commit = NULL;
9788 char *new_commit_brief_str = NULL;
9789 struct got_object_id *yca_id = NULL;
9790 struct got_commit_object *yca_commit = NULL;
9791 char *yca_id_str = NULL, *yca_brief_str = NULL;
9792 char *custom_refs_str;
9794 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9795 return got_error_from_errno("asprintf");
9797 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9798 0, 0, refs_idmap, custom_refs_str);
9802 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9806 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9808 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9813 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9817 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9821 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9822 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9826 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9827 refs_str ? " (" : "", refs_str ? refs_str : "",
9828 refs_str ? ")" : "", new_commit_brief_str);
9829 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9830 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9834 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9838 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9842 err = got_object_id_str(&yca_id_str, yca_id);
9846 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9848 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9852 printf("history forked at %s%s%s%s\n %s\n",
9854 refs_str ? " (" : "", refs_str ? refs_str : "",
9855 refs_str ? ")" : "", yca_brief_str);
9858 free(custom_refs_str);
9859 free(new_commit_id);
9863 free(yca_brief_str);
9865 got_object_commit_close(new_commit);
9867 got_object_commit_close(yca_commit);
9872 static const struct got_error *
9873 process_backup_refs(const char *backup_ref_prefix,
9874 const char *wanted_branch_name,
9875 int delete, struct got_repository *repo)
9877 const struct got_error *err;
9878 struct got_reflist_head refs, backup_refs;
9879 struct got_reflist_entry *re;
9880 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9881 struct got_object_id *old_commit_id = NULL;
9882 char *branch_name = NULL;
9883 struct got_commit_object *old_commit = NULL;
9884 struct got_reflist_object_id_map *refs_idmap = NULL;
9885 int wanted_branch_found = 0;
9888 TAILQ_INIT(&backup_refs);
9890 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9894 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9898 if (wanted_branch_name) {
9899 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9900 wanted_branch_name += 11;
9903 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9904 got_ref_cmp_by_commit_timestamp_descending, repo);
9908 TAILQ_FOREACH(re, &backup_refs, entry) {
9909 const char *refname = got_ref_get_name(re->ref);
9912 err = check_cancelled(NULL);
9916 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9920 err = got_object_open_as_commit(&old_commit, repo,
9925 if (strncmp(backup_ref_prefix, refname,
9926 backup_ref_prefix_len) == 0)
9927 refname += backup_ref_prefix_len;
9929 while (refname[0] == '/')
9932 branch_name = strdup(refname);
9933 if (branch_name == NULL) {
9934 err = got_error_from_errno("strdup");
9937 slash = strrchr(branch_name, '/');
9940 refname += strlen(branch_name) + 1;
9943 if (wanted_branch_name == NULL ||
9944 strcmp(wanted_branch_name, branch_name) == 0) {
9945 wanted_branch_found = 1;
9947 err = delete_backup_ref(re->ref,
9948 old_commit_id, repo);
9950 err = print_backup_ref(branch_name, refname,
9951 old_commit_id, old_commit, refs_idmap,
9958 free(old_commit_id);
9959 old_commit_id = NULL;
9962 got_object_commit_close(old_commit);
9966 if (wanted_branch_name && !wanted_branch_found) {
9967 err = got_error_fmt(GOT_ERR_NOT_REF,
9968 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9972 got_reflist_object_id_map_free(refs_idmap);
9973 got_ref_list_free(&refs);
9974 got_ref_list_free(&backup_refs);
9975 free(old_commit_id);
9978 got_object_commit_close(old_commit);
9982 static const struct got_error *
9983 abort_progress(void *arg, unsigned char status, const char *path)
9986 * Unversioned files should not clutter progress output when
9987 * an operation is aborted.
9989 if (status == GOT_STATUS_UNVERSIONED)
9992 return update_progress(arg, status, path);
9995 static const struct got_error *
9996 cmd_rebase(int argc, char *argv[])
9998 const struct got_error *error = NULL;
9999 struct got_worktree *worktree = NULL;
10000 struct got_repository *repo = NULL;
10001 struct got_fileindex *fileindex = NULL;
10003 struct got_reference *branch = NULL;
10004 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10005 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10006 struct got_object_id *resume_commit_id = NULL;
10007 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10008 struct got_commit_object *commit = NULL;
10009 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10010 int histedit_in_progress = 0, merge_in_progress = 0;
10011 int create_backup = 1, list_backups = 0, delete_backups = 0;
10012 struct got_object_id_queue commits;
10013 struct got_pathlist_head merged_paths;
10014 const struct got_object_id_queue *parent_ids;
10015 struct got_object_qid *qid, *pid;
10016 struct got_update_progress_arg upa;
10017 int *pack_fds = NULL;
10019 STAILQ_INIT(&commits);
10020 TAILQ_INIT(&merged_paths);
10021 memset(&upa, 0, sizeof(upa));
10023 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10029 continue_rebase = 1;
10035 delete_backups = 1;
10047 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10048 "unveil", NULL) == -1)
10051 if (list_backups) {
10053 option_conflict('l', 'a');
10054 if (continue_rebase)
10055 option_conflict('l', 'c');
10056 if (delete_backups)
10057 option_conflict('l', 'X');
10058 if (argc != 0 && argc != 1)
10060 } else if (delete_backups) {
10062 option_conflict('X', 'a');
10063 if (continue_rebase)
10064 option_conflict('X', 'c');
10066 option_conflict('l', 'X');
10067 if (argc != 0 && argc != 1)
10070 if (abort_rebase && continue_rebase)
10072 else if (abort_rebase || continue_rebase) {
10075 } else if (argc != 1)
10079 cwd = getcwd(NULL, 0);
10081 error = got_error_from_errno("getcwd");
10085 error = got_repo_pack_fds_open(&pack_fds);
10089 error = got_worktree_open(&worktree, cwd);
10091 if (list_backups || delete_backups) {
10092 if (error->code != GOT_ERR_NOT_WORKTREE)
10095 if (error->code == GOT_ERR_NOT_WORKTREE)
10096 error = wrap_not_worktree_error(error,
10102 error = got_repo_open(&repo,
10103 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10108 error = apply_unveil(got_repo_get_path(repo), 0,
10109 worktree ? got_worktree_get_root_path(worktree) : NULL);
10113 if (list_backups || delete_backups) {
10114 error = process_backup_refs(
10115 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10116 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10117 goto done; /* nothing else to do */
10120 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10124 if (histedit_in_progress) {
10125 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10129 error = got_worktree_merge_in_progress(&merge_in_progress,
10133 if (merge_in_progress) {
10134 error = got_error(GOT_ERR_MERGE_BUSY);
10138 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10142 if (abort_rebase) {
10143 if (!rebase_in_progress) {
10144 error = got_error(GOT_ERR_NOT_REBASING);
10147 error = got_worktree_rebase_continue(&resume_commit_id,
10148 &new_base_branch, &tmp_branch, &branch, &fileindex,
10152 printf("Switching work tree to %s\n",
10153 got_ref_get_symref_target(new_base_branch));
10154 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10155 new_base_branch, abort_progress, &upa);
10158 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10159 print_merge_progress_stats(&upa);
10160 goto done; /* nothing else to do */
10163 if (continue_rebase) {
10164 if (!rebase_in_progress) {
10165 error = got_error(GOT_ERR_NOT_REBASING);
10168 error = got_worktree_rebase_continue(&resume_commit_id,
10169 &new_base_branch, &tmp_branch, &branch, &fileindex,
10174 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10175 resume_commit_id, repo);
10179 yca_id = got_object_id_dup(resume_commit_id);
10180 if (yca_id == NULL) {
10181 error = got_error_from_errno("got_object_id_dup");
10185 error = got_ref_open(&branch, repo, argv[0], 0);
10190 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10194 if (!continue_rebase) {
10195 struct got_object_id *base_commit_id;
10197 base_commit_id = got_worktree_get_base_commit_id(worktree);
10198 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10199 base_commit_id, branch_head_commit_id, 1, repo,
10200 check_cancelled, NULL);
10203 if (yca_id == NULL) {
10204 error = got_error_msg(GOT_ERR_ANCESTRY,
10205 "specified branch shares no common ancestry "
10206 "with work tree's branch");
10210 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10212 if (error->code != GOT_ERR_ANCESTRY)
10216 struct got_pathlist_head paths;
10217 printf("%s is already based on %s\n",
10218 got_ref_get_name(branch),
10219 got_worktree_get_head_ref_name(worktree));
10220 error = switch_head_ref(branch, branch_head_commit_id,
10224 error = got_worktree_set_base_commit_id(worktree, repo,
10225 branch_head_commit_id);
10228 TAILQ_INIT(&paths);
10229 error = got_pathlist_append(&paths, "", NULL);
10232 error = got_worktree_checkout_files(worktree,
10233 &paths, repo, update_progress, &upa,
10234 check_cancelled, NULL);
10235 got_pathlist_free(&paths);
10238 if (upa.did_something) {
10240 error = got_object_id_str(&id_str,
10241 branch_head_commit_id);
10244 printf("Updated to %s: %s\n",
10245 got_worktree_get_head_ref_name(worktree),
10249 printf("Already up-to-date\n");
10250 print_update_progress_stats(&upa);
10255 commit_id = branch_head_commit_id;
10256 error = got_object_open_as_commit(&commit, repo, commit_id);
10260 parent_ids = got_object_commit_get_parent_ids(commit);
10261 pid = STAILQ_FIRST(parent_ids);
10263 error = got_error(GOT_ERR_EMPTY_REBASE);
10266 error = collect_commits(&commits, commit_id, &pid->id,
10267 yca_id, got_worktree_get_path_prefix(worktree),
10268 GOT_ERR_REBASE_PATH, repo);
10269 got_object_commit_close(commit);
10274 if (!continue_rebase) {
10275 error = got_worktree_rebase_prepare(&new_base_branch,
10276 &tmp_branch, &fileindex, worktree, branch, repo);
10281 if (STAILQ_EMPTY(&commits)) {
10282 if (continue_rebase) {
10283 error = rebase_complete(worktree, fileindex,
10284 branch, new_base_branch, tmp_branch, repo,
10288 /* Fast-forward the reference of the branch. */
10289 struct got_object_id *new_head_commit_id;
10291 error = got_ref_resolve(&new_head_commit_id, repo,
10295 error = got_object_id_str(&id_str, new_head_commit_id);
10296 printf("Forwarding %s to commit %s\n",
10297 got_ref_get_name(branch), id_str);
10299 error = got_ref_change_ref(branch,
10300 new_head_commit_id);
10303 /* No backup needed since objects did not change. */
10309 STAILQ_FOREACH(qid, &commits, entry) {
10311 commit_id = &qid->id;
10312 parent_id = pid ? &pid->id : yca_id;
10315 memset(&upa, 0, sizeof(upa));
10316 error = got_worktree_rebase_merge_files(&merged_paths,
10317 worktree, fileindex, parent_id, commit_id, repo,
10318 update_progress, &upa, check_cancelled, NULL);
10322 print_merge_progress_stats(&upa);
10323 if (upa.conflicts > 0 || upa.missing > 0 ||
10324 upa.not_deleted > 0 || upa.unversioned > 0) {
10325 if (upa.conflicts > 0) {
10326 error = show_rebase_merge_conflict(&qid->id,
10331 got_worktree_rebase_pathlist_free(&merged_paths);
10335 error = rebase_commit(&merged_paths, worktree, fileindex,
10336 tmp_branch, commit_id, repo);
10337 got_worktree_rebase_pathlist_free(&merged_paths);
10342 if (upa.conflicts > 0 || upa.missing > 0 ||
10343 upa.not_deleted > 0 || upa.unversioned > 0) {
10344 error = got_worktree_rebase_postpone(worktree, fileindex);
10347 if (upa.conflicts > 0 && upa.missing == 0 &&
10348 upa.not_deleted == 0 && upa.unversioned == 0) {
10349 error = got_error_msg(GOT_ERR_CONFLICTS,
10350 "conflicts must be resolved before rebasing "
10352 } else if (upa.conflicts > 0) {
10353 error = got_error_msg(GOT_ERR_CONFLICTS,
10354 "conflicts must be resolved before rebasing "
10355 "can continue; changes destined for some "
10356 "files were not yet merged and should be "
10357 "merged manually if required before the "
10358 "rebase operation is continued");
10360 error = got_error_msg(GOT_ERR_CONFLICTS,
10361 "changes destined for some files were not "
10362 "yet merged and should be merged manually "
10363 "if required before the rebase operation "
10367 error = rebase_complete(worktree, fileindex, branch,
10368 new_base_branch, tmp_branch, repo, create_backup);
10370 got_object_id_queue_free(&commits);
10371 free(branch_head_commit_id);
10372 free(resume_commit_id);
10375 got_object_commit_close(commit);
10377 got_ref_close(branch);
10378 if (new_base_branch)
10379 got_ref_close(new_base_branch);
10381 got_ref_close(tmp_branch);
10383 got_worktree_close(worktree);
10385 const struct got_error *close_err = got_repo_close(repo);
10390 const struct got_error *pack_err =
10391 got_repo_pack_fds_close(pack_fds);
10399 usage_histedit(void)
10401 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10402 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10407 #define GOT_HISTEDIT_PICK 'p'
10408 #define GOT_HISTEDIT_EDIT 'e'
10409 #define GOT_HISTEDIT_FOLD 'f'
10410 #define GOT_HISTEDIT_DROP 'd'
10411 #define GOT_HISTEDIT_MESG 'm'
10413 static const struct got_histedit_cmd {
10414 unsigned char code;
10417 } got_histedit_cmds[] = {
10418 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10419 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10420 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10422 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10423 { GOT_HISTEDIT_MESG, "mesg",
10424 "single-line log message for commit above (open editor if empty)" },
10427 struct got_histedit_list_entry {
10428 TAILQ_ENTRY(got_histedit_list_entry) entry;
10429 struct got_object_id *commit_id;
10430 const struct got_histedit_cmd *cmd;
10433 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10435 static const struct got_error *
10436 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10437 FILE *f, struct got_repository *repo)
10439 const struct got_error *err = NULL;
10440 char *logmsg = NULL, *id_str = NULL;
10441 struct got_commit_object *commit = NULL;
10444 err = got_object_open_as_commit(&commit, repo, commit_id);
10448 err = get_short_logmsg(&logmsg, 34, commit);
10452 err = got_object_id_str(&id_str, commit_id);
10456 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10458 err = got_ferror(f, GOT_ERR_IO);
10461 got_object_commit_close(commit);
10467 static const struct got_error *
10468 histedit_write_commit_list(struct got_object_id_queue *commits,
10469 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10470 struct got_repository *repo)
10472 const struct got_error *err = NULL;
10473 struct got_object_qid *qid;
10474 const char *histedit_cmd = NULL;
10476 if (STAILQ_EMPTY(commits))
10477 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10479 STAILQ_FOREACH(qid, commits, entry) {
10480 histedit_cmd = got_histedit_cmds[0].name;
10482 histedit_cmd = "edit";
10483 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10484 histedit_cmd = "fold";
10485 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10488 if (edit_logmsg_only) {
10489 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10491 err = got_ferror(f, GOT_ERR_IO);
10500 static const struct got_error *
10501 write_cmd_list(FILE *f, const char *branch_name,
10502 struct got_object_id_queue *commits)
10504 const struct got_error *err = NULL;
10508 struct got_object_qid *qid;
10510 qid = STAILQ_FIRST(commits);
10511 err = got_object_id_str(&id_str, &qid->id);
10516 "# Editing the history of branch '%s' starting at\n"
10518 "# Commits will be processed in order from top to "
10519 "bottom of this file.\n", branch_name, id_str);
10521 err = got_ferror(f, GOT_ERR_IO);
10525 n = fprintf(f, "# Available histedit commands:\n");
10527 err = got_ferror(f, GOT_ERR_IO);
10531 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10532 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10533 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10536 err = got_ferror(f, GOT_ERR_IO);
10545 static const struct got_error *
10546 histedit_syntax_error(int lineno)
10548 static char msg[42];
10551 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10553 if (ret == -1 || ret >= sizeof(msg))
10554 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10556 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10559 static const struct got_error *
10560 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10561 char *logmsg, struct got_repository *repo)
10563 const struct got_error *err;
10564 struct got_commit_object *folded_commit = NULL;
10565 char *id_str, *folded_logmsg = NULL;
10567 err = got_object_id_str(&id_str, hle->commit_id);
10571 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10575 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10578 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10579 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10580 folded_logmsg) == -1) {
10581 err = got_error_from_errno("asprintf");
10585 got_object_commit_close(folded_commit);
10587 free(folded_logmsg);
10591 static struct got_histedit_list_entry *
10592 get_folded_commits(struct got_histedit_list_entry *hle)
10594 struct got_histedit_list_entry *prev, *folded = NULL;
10596 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10597 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10598 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10599 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10601 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10607 static const struct got_error *
10608 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10609 struct got_repository *repo)
10611 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10612 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10613 const struct got_error *err = NULL;
10614 struct got_commit_object *commit = NULL;
10617 struct got_histedit_list_entry *folded = NULL;
10619 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10623 folded = get_folded_commits(hle);
10625 while (folded != hle) {
10626 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10627 folded = TAILQ_NEXT(folded, entry);
10630 err = append_folded_commit_msg(&new_msg, folded,
10636 folded = TAILQ_NEXT(folded, entry);
10640 err = got_object_id_str(&id_str, hle->commit_id);
10643 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10646 logmsg_len = asprintf(&new_msg,
10647 "%s\n# original log message of commit %s: %s",
10648 logmsg ? logmsg : "", id_str, orig_logmsg);
10649 if (logmsg_len == -1) {
10650 err = got_error_from_errno("asprintf");
10656 err = got_object_id_str(&id_str, hle->commit_id);
10660 err = got_opentemp_named_fd(&logmsg_path, &fd,
10661 GOT_TMPDIR_STR "/got-logmsg");
10665 write(fd, logmsg, logmsg_len);
10668 err = get_editor(&editor);
10672 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10675 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10678 hle->logmsg = strdup(new_msg);
10679 if (hle->logmsg == NULL)
10680 err = got_error_from_errno("strdup");
10683 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10684 err = got_error_from_errno2("unlink", logmsg_path);
10690 got_object_commit_close(commit);
10694 static const struct got_error *
10695 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10696 FILE *f, struct got_repository *repo)
10698 const struct got_error *err = NULL;
10699 char *line = NULL, *p, *end;
10702 int lineno = 0, lastcmd = -1;
10703 const struct got_histedit_cmd *cmd;
10704 struct got_object_id *commit_id = NULL;
10705 struct got_histedit_list_entry *hle = NULL;
10708 len = getline(&line, &size, f);
10710 const struct got_error *getline_err;
10713 getline_err = got_error_from_errno("getline");
10714 err = got_ferror(f, getline_err->code);
10719 while (isspace((unsigned char)p[0]))
10721 if (p[0] == '#' || p[0] == '\0') {
10727 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10728 cmd = &got_histedit_cmds[i];
10729 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10730 isspace((unsigned char)p[strlen(cmd->name)])) {
10731 p += strlen(cmd->name);
10734 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10739 if (i == nitems(got_histedit_cmds)) {
10740 err = histedit_syntax_error(lineno);
10743 while (isspace((unsigned char)p[0]))
10745 if (cmd->code == GOT_HISTEDIT_MESG) {
10746 if (lastcmd != GOT_HISTEDIT_PICK &&
10747 lastcmd != GOT_HISTEDIT_EDIT) {
10748 err = got_error(GOT_ERR_HISTEDIT_CMD);
10751 if (p[0] == '\0') {
10752 err = histedit_edit_logmsg(hle, repo);
10756 hle->logmsg = strdup(p);
10757 if (hle->logmsg == NULL) {
10758 err = got_error_from_errno("strdup");
10764 lastcmd = cmd->code;
10768 while (end[0] && !isspace((unsigned char)end[0]))
10772 err = got_object_resolve_id_str(&commit_id, repo, p);
10774 /* override error code */
10775 err = histedit_syntax_error(lineno);
10779 hle = malloc(sizeof(*hle));
10781 err = got_error_from_errno("malloc");
10785 hle->commit_id = commit_id;
10786 hle->logmsg = NULL;
10790 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10791 lastcmd = cmd->code;
10799 static const struct got_error *
10800 histedit_check_script(struct got_histedit_list *histedit_cmds,
10801 struct got_object_id_queue *commits, struct got_repository *repo)
10803 const struct got_error *err = NULL;
10804 struct got_object_qid *qid;
10805 struct got_histedit_list_entry *hle;
10806 static char msg[92];
10809 if (TAILQ_EMPTY(histedit_cmds))
10810 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10811 "histedit script contains no commands");
10812 if (STAILQ_EMPTY(commits))
10813 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10815 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10816 struct got_histedit_list_entry *hle2;
10817 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10820 if (got_object_id_cmp(hle->commit_id,
10821 hle2->commit_id) != 0)
10823 err = got_object_id_str(&id_str, hle->commit_id);
10826 snprintf(msg, sizeof(msg), "commit %s is listed "
10827 "more than once in histedit script", id_str);
10829 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10833 STAILQ_FOREACH(qid, commits, entry) {
10834 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10835 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10839 err = got_object_id_str(&id_str, &qid->id);
10842 snprintf(msg, sizeof(msg),
10843 "commit %s missing from histedit script", id_str);
10845 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10849 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10850 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10851 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10852 "last commit in histedit script cannot be folded");
10857 static const struct got_error *
10858 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10859 const char *path, struct got_object_id_queue *commits,
10860 struct got_repository *repo)
10862 const struct got_error *err = NULL;
10866 err = get_editor(&editor);
10870 if (spawn_editor(editor, path) == -1) {
10871 err = got_error_from_errno("failed spawning editor");
10875 f = fopen(path, "re");
10877 err = got_error_from_errno("fopen");
10880 err = histedit_parse_list(histedit_cmds, f, repo);
10884 err = histedit_check_script(histedit_cmds, commits, repo);
10886 if (f && fclose(f) == EOF && err == NULL)
10887 err = got_error_from_errno("fclose");
10892 static const struct got_error *
10893 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10894 struct got_object_id_queue *, const char *, const char *,
10895 struct got_repository *);
10897 static const struct got_error *
10898 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10899 struct got_object_id_queue *commits, const char *branch_name,
10900 int edit_logmsg_only, int fold_only, int edit_only,
10901 struct got_repository *repo)
10903 const struct got_error *err;
10907 err = got_opentemp_named(&path, &f, "got-histedit");
10911 err = write_cmd_list(f, branch_name, commits);
10915 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10916 fold_only, edit_only, repo);
10920 if (edit_logmsg_only || fold_only || edit_only) {
10922 err = histedit_parse_list(histedit_cmds, f, repo);
10924 if (fclose(f) == EOF) {
10925 err = got_error_from_errno("fclose");
10929 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10931 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10932 err->code != GOT_ERR_HISTEDIT_CMD)
10934 err = histedit_edit_list_retry(histedit_cmds, err,
10935 commits, path, branch_name, repo);
10939 if (f && fclose(f) == EOF && err == NULL)
10940 err = got_error_from_errno("fclose");
10941 if (path && unlink(path) != 0 && err == NULL)
10942 err = got_error_from_errno2("unlink", path);
10947 static const struct got_error *
10948 histedit_save_list(struct got_histedit_list *histedit_cmds,
10949 struct got_worktree *worktree, struct got_repository *repo)
10951 const struct got_error *err = NULL;
10954 struct got_histedit_list_entry *hle;
10955 struct got_commit_object *commit = NULL;
10957 err = got_worktree_get_histedit_script_path(&path, worktree);
10961 f = fopen(path, "we");
10963 err = got_error_from_errno2("fopen", path);
10966 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10967 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10973 int n = fprintf(f, "%c %s\n",
10974 GOT_HISTEDIT_MESG, hle->logmsg);
10976 err = got_ferror(f, GOT_ERR_IO);
10982 if (f && fclose(f) == EOF && err == NULL)
10983 err = got_error_from_errno("fclose");
10986 got_object_commit_close(commit);
10991 histedit_free_list(struct got_histedit_list *histedit_cmds)
10993 struct got_histedit_list_entry *hle;
10995 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10996 TAILQ_REMOVE(histedit_cmds, hle, entry);
11001 static const struct got_error *
11002 histedit_load_list(struct got_histedit_list *histedit_cmds,
11003 const char *path, struct got_repository *repo)
11005 const struct got_error *err = NULL;
11008 f = fopen(path, "re");
11010 err = got_error_from_errno2("fopen", path);
11014 err = histedit_parse_list(histedit_cmds, f, repo);
11016 if (f && fclose(f) == EOF && err == NULL)
11017 err = got_error_from_errno("fclose");
11021 static const struct got_error *
11022 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11023 const struct got_error *edit_err, struct got_object_id_queue *commits,
11024 const char *path, const char *branch_name, struct got_repository *repo)
11026 const struct got_error *err = NULL, *prev_err = edit_err;
11029 while (resp != 'c' && resp != 'r' && resp != 'a') {
11030 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11031 "or (a)bort: ", getprogname(), prev_err->msg);
11036 histedit_free_list(histedit_cmds);
11037 err = histedit_run_editor(histedit_cmds, path, commits,
11040 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11041 err->code != GOT_ERR_HISTEDIT_CMD)
11048 } else if (resp == 'r') {
11049 histedit_free_list(histedit_cmds);
11050 err = histedit_edit_script(histedit_cmds,
11051 commits, branch_name, 0, 0, 0, repo);
11053 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11054 err->code != GOT_ERR_HISTEDIT_CMD)
11061 } else if (resp == 'a') {
11062 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11065 printf("invalid response '%c'\n", resp);
11071 static const struct got_error *
11072 histedit_complete(struct got_worktree *worktree,
11073 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11074 struct got_reference *branch, struct got_repository *repo)
11076 printf("Switching work tree to %s\n",
11077 got_ref_get_symref_target(branch));
11078 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11082 static const struct got_error *
11083 show_histedit_progress(struct got_commit_object *commit,
11084 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11086 const struct got_error *err;
11087 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11089 err = got_object_id_str(&old_id_str, hle->commit_id);
11094 err = got_object_id_str(&new_id_str, new_id);
11099 old_id_str[12] = '\0';
11101 new_id_str[12] = '\0';
11104 logmsg = strdup(hle->logmsg);
11105 if (logmsg == NULL) {
11106 err = got_error_from_errno("strdup");
11109 trim_logmsg(logmsg, 42);
11111 err = get_short_logmsg(&logmsg, 42, commit);
11116 switch (hle->cmd->code) {
11117 case GOT_HISTEDIT_PICK:
11118 case GOT_HISTEDIT_EDIT:
11119 printf("%s -> %s: %s\n", old_id_str,
11120 new_id_str ? new_id_str : "no-op change", logmsg);
11122 case GOT_HISTEDIT_DROP:
11123 case GOT_HISTEDIT_FOLD:
11124 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11136 static const struct got_error *
11137 histedit_commit(struct got_pathlist_head *merged_paths,
11138 struct got_worktree *worktree, struct got_fileindex *fileindex,
11139 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11140 struct got_repository *repo)
11142 const struct got_error *err;
11143 struct got_commit_object *commit;
11144 struct got_object_id *new_commit_id;
11146 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11147 && hle->logmsg == NULL) {
11148 err = histedit_edit_logmsg(hle, repo);
11153 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11157 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11158 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11159 hle->logmsg, repo);
11161 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11163 err = show_histedit_progress(commit, hle, NULL);
11165 err = show_histedit_progress(commit, hle, new_commit_id);
11166 free(new_commit_id);
11169 got_object_commit_close(commit);
11173 static const struct got_error *
11174 histedit_skip_commit(struct got_histedit_list_entry *hle,
11175 struct got_worktree *worktree, struct got_repository *repo)
11177 const struct got_error *error;
11178 struct got_commit_object *commit;
11180 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11185 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11189 error = show_histedit_progress(commit, hle, NULL);
11190 got_object_commit_close(commit);
11194 static const struct got_error *
11195 check_local_changes(void *arg, unsigned char status,
11196 unsigned char staged_status, const char *path,
11197 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11198 struct got_object_id *commit_id, int dirfd, const char *de_name)
11200 int *have_local_changes = arg;
11203 case GOT_STATUS_ADD:
11204 case GOT_STATUS_DELETE:
11205 case GOT_STATUS_MODIFY:
11206 case GOT_STATUS_CONFLICT:
11207 *have_local_changes = 1;
11208 return got_error(GOT_ERR_CANCELLED);
11213 switch (staged_status) {
11214 case GOT_STATUS_ADD:
11215 case GOT_STATUS_DELETE:
11216 case GOT_STATUS_MODIFY:
11217 *have_local_changes = 1;
11218 return got_error(GOT_ERR_CANCELLED);
11226 static const struct got_error *
11227 cmd_histedit(int argc, char *argv[])
11229 const struct got_error *error = NULL;
11230 struct got_worktree *worktree = NULL;
11231 struct got_fileindex *fileindex = NULL;
11232 struct got_repository *repo = NULL;
11234 struct got_reference *branch = NULL;
11235 struct got_reference *tmp_branch = NULL;
11236 struct got_object_id *resume_commit_id = NULL;
11237 struct got_object_id *base_commit_id = NULL;
11238 struct got_object_id *head_commit_id = NULL;
11239 struct got_commit_object *commit = NULL;
11240 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11241 struct got_update_progress_arg upa;
11242 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11243 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11244 int list_backups = 0, delete_backups = 0;
11245 const char *edit_script_path = NULL;
11246 struct got_object_id_queue commits;
11247 struct got_pathlist_head merged_paths;
11248 const struct got_object_id_queue *parent_ids;
11249 struct got_object_qid *pid;
11250 struct got_histedit_list histedit_cmds;
11251 struct got_histedit_list_entry *hle;
11252 int *pack_fds = NULL;
11254 STAILQ_INIT(&commits);
11255 TAILQ_INIT(&histedit_cmds);
11256 TAILQ_INIT(&merged_paths);
11257 memset(&upa, 0, sizeof(upa));
11259 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11274 edit_script_path = optarg;
11277 edit_logmsg_only = 1;
11283 delete_backups = 1;
11295 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11296 "unveil", NULL) == -1)
11299 if (abort_edit && continue_edit)
11300 option_conflict('a', 'c');
11301 if (edit_script_path && edit_logmsg_only)
11302 option_conflict('F', 'm');
11303 if (abort_edit && edit_logmsg_only)
11304 option_conflict('a', 'm');
11305 if (continue_edit && edit_logmsg_only)
11306 option_conflict('c', 'm');
11307 if (abort_edit && fold_only)
11308 option_conflict('a', 'f');
11309 if (continue_edit && fold_only)
11310 option_conflict('c', 'f');
11311 if (fold_only && edit_logmsg_only)
11312 option_conflict('f', 'm');
11313 if (edit_script_path && fold_only)
11314 option_conflict('F', 'f');
11315 if (abort_edit && edit_only)
11316 option_conflict('a', 'e');
11317 if (continue_edit && edit_only)
11318 option_conflict('c', 'e');
11319 if (edit_only && edit_logmsg_only)
11320 option_conflict('e', 'm');
11321 if (edit_script_path && edit_only)
11322 option_conflict('F', 'e');
11323 if (list_backups) {
11325 option_conflict('l', 'a');
11327 option_conflict('l', 'c');
11328 if (edit_script_path)
11329 option_conflict('l', 'F');
11330 if (edit_logmsg_only)
11331 option_conflict('l', 'm');
11333 option_conflict('l', 'f');
11335 option_conflict('l', 'e');
11336 if (delete_backups)
11337 option_conflict('l', 'X');
11338 if (argc != 0 && argc != 1)
11340 } else if (delete_backups) {
11342 option_conflict('X', 'a');
11344 option_conflict('X', 'c');
11345 if (edit_script_path)
11346 option_conflict('X', 'F');
11347 if (edit_logmsg_only)
11348 option_conflict('X', 'm');
11350 option_conflict('X', 'f');
11352 option_conflict('X', 'e');
11354 option_conflict('X', 'l');
11355 if (argc != 0 && argc != 1)
11357 } else if (argc != 0)
11361 * This command cannot apply unveil(2) in all cases because the
11362 * user may choose to run an editor to edit the histedit script
11363 * and to edit individual commit log messages.
11364 * unveil(2) traverses exec(2); if an editor is used we have to
11365 * apply unveil after edit script and log messages have been written.
11366 * XXX TODO: Make use of unveil(2) where possible.
11369 cwd = getcwd(NULL, 0);
11371 error = got_error_from_errno("getcwd");
11375 error = got_repo_pack_fds_open(&pack_fds);
11379 error = got_worktree_open(&worktree, cwd);
11381 if (list_backups || delete_backups) {
11382 if (error->code != GOT_ERR_NOT_WORKTREE)
11385 if (error->code == GOT_ERR_NOT_WORKTREE)
11386 error = wrap_not_worktree_error(error,
11392 if (list_backups || delete_backups) {
11393 error = got_repo_open(&repo,
11394 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11398 error = apply_unveil(got_repo_get_path(repo), 0,
11399 worktree ? got_worktree_get_root_path(worktree) : NULL);
11402 error = process_backup_refs(
11403 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11404 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11405 goto done; /* nothing else to do */
11408 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11413 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11416 if (rebase_in_progress) {
11417 error = got_error(GOT_ERR_REBASING);
11421 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11425 if (merge_in_progress) {
11426 error = got_error(GOT_ERR_MERGE_BUSY);
11430 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11434 if (edit_in_progress && edit_logmsg_only) {
11435 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11436 "histedit operation is in progress in this "
11437 "work tree and must be continued or aborted "
11438 "before the -m option can be used");
11441 if (edit_in_progress && fold_only) {
11442 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11443 "histedit operation is in progress in this "
11444 "work tree and must be continued or aborted "
11445 "before the -f option can be used");
11448 if (edit_in_progress && edit_only) {
11449 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11450 "histedit operation is in progress in this "
11451 "work tree and must be continued or aborted "
11452 "before the -e option can be used");
11456 if (edit_in_progress && abort_edit) {
11457 error = got_worktree_histedit_continue(&resume_commit_id,
11458 &tmp_branch, &branch, &base_commit_id, &fileindex,
11462 printf("Switching work tree to %s\n",
11463 got_ref_get_symref_target(branch));
11464 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11465 branch, base_commit_id, abort_progress, &upa);
11468 printf("Histedit of %s aborted\n",
11469 got_ref_get_symref_target(branch));
11470 print_merge_progress_stats(&upa);
11471 goto done; /* nothing else to do */
11472 } else if (abort_edit) {
11473 error = got_error(GOT_ERR_NOT_HISTEDIT);
11477 if (continue_edit) {
11480 if (!edit_in_progress) {
11481 error = got_error(GOT_ERR_NOT_HISTEDIT);
11485 error = got_worktree_get_histedit_script_path(&path, worktree);
11489 error = histedit_load_list(&histedit_cmds, path, repo);
11494 error = got_worktree_histedit_continue(&resume_commit_id,
11495 &tmp_branch, &branch, &base_commit_id, &fileindex,
11500 error = got_ref_resolve(&head_commit_id, repo, branch);
11504 error = got_object_open_as_commit(&commit, repo,
11508 parent_ids = got_object_commit_get_parent_ids(commit);
11509 pid = STAILQ_FIRST(parent_ids);
11511 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11514 error = collect_commits(&commits, head_commit_id, &pid->id,
11515 base_commit_id, got_worktree_get_path_prefix(worktree),
11516 GOT_ERR_HISTEDIT_PATH, repo);
11517 got_object_commit_close(commit);
11522 if (edit_in_progress) {
11523 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11527 error = got_ref_open(&branch, repo,
11528 got_worktree_get_head_ref_name(worktree), 0);
11532 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11533 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11534 "will not edit commit history of a branch outside "
11535 "the \"refs/heads/\" reference namespace");
11539 error = got_ref_resolve(&head_commit_id, repo, branch);
11540 got_ref_close(branch);
11545 error = got_object_open_as_commit(&commit, repo,
11549 parent_ids = got_object_commit_get_parent_ids(commit);
11550 pid = STAILQ_FIRST(parent_ids);
11552 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11555 error = collect_commits(&commits, head_commit_id, &pid->id,
11556 got_worktree_get_base_commit_id(worktree),
11557 got_worktree_get_path_prefix(worktree),
11558 GOT_ERR_HISTEDIT_PATH, repo);
11559 got_object_commit_close(commit);
11564 if (STAILQ_EMPTY(&commits)) {
11565 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11569 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11570 &base_commit_id, &fileindex, worktree, repo);
11574 if (edit_script_path) {
11575 error = histedit_load_list(&histedit_cmds,
11576 edit_script_path, repo);
11578 got_worktree_histedit_abort(worktree, fileindex,
11579 repo, branch, base_commit_id,
11580 abort_progress, &upa);
11581 print_merge_progress_stats(&upa);
11585 const char *branch_name;
11586 branch_name = got_ref_get_symref_target(branch);
11587 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11589 error = histedit_edit_script(&histedit_cmds, &commits,
11590 branch_name, edit_logmsg_only, fold_only,
11593 got_worktree_histedit_abort(worktree, fileindex,
11594 repo, branch, base_commit_id,
11595 abort_progress, &upa);
11596 print_merge_progress_stats(&upa);
11602 error = histedit_save_list(&histedit_cmds, worktree,
11605 got_worktree_histedit_abort(worktree, fileindex,
11606 repo, branch, base_commit_id,
11607 abort_progress, &upa);
11608 print_merge_progress_stats(&upa);
11614 error = histedit_check_script(&histedit_cmds, &commits, repo);
11618 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11619 if (resume_commit_id) {
11620 if (got_object_id_cmp(hle->commit_id,
11621 resume_commit_id) != 0)
11624 resume_commit_id = NULL;
11625 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11626 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11627 error = histedit_skip_commit(hle, worktree,
11632 struct got_pathlist_head paths;
11633 int have_changes = 0;
11635 TAILQ_INIT(&paths);
11636 error = got_pathlist_append(&paths, "", NULL);
11639 error = got_worktree_status(worktree, &paths,
11640 repo, 0, check_local_changes, &have_changes,
11641 check_cancelled, NULL);
11642 got_pathlist_free(&paths);
11644 if (error->code != GOT_ERR_CANCELLED)
11646 if (sigint_received || sigpipe_received)
11649 if (have_changes) {
11650 error = histedit_commit(NULL, worktree,
11651 fileindex, tmp_branch, hle, repo);
11655 error = got_object_open_as_commit(
11656 &commit, repo, hle->commit_id);
11659 error = show_histedit_progress(commit,
11661 got_object_commit_close(commit);
11670 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11671 error = histedit_skip_commit(hle, worktree, repo);
11677 error = got_object_open_as_commit(&commit, repo,
11681 parent_ids = got_object_commit_get_parent_ids(commit);
11682 pid = STAILQ_FIRST(parent_ids);
11684 error = got_worktree_histedit_merge_files(&merged_paths,
11685 worktree, fileindex, &pid->id, hle->commit_id, repo,
11686 update_progress, &upa, check_cancelled, NULL);
11689 got_object_commit_close(commit);
11692 print_merge_progress_stats(&upa);
11693 if (upa.conflicts > 0 || upa.missing > 0 ||
11694 upa.not_deleted > 0 || upa.unversioned > 0) {
11695 if (upa.conflicts > 0) {
11696 error = show_rebase_merge_conflict(
11697 hle->commit_id, repo);
11701 got_worktree_rebase_pathlist_free(&merged_paths);
11705 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11707 error = got_object_id_str(&id_str, hle->commit_id);
11710 printf("Stopping histedit for amending commit %s\n",
11713 got_worktree_rebase_pathlist_free(&merged_paths);
11714 error = got_worktree_histedit_postpone(worktree,
11719 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11720 error = histedit_skip_commit(hle, worktree, repo);
11726 error = histedit_commit(&merged_paths, worktree, fileindex,
11727 tmp_branch, hle, repo);
11728 got_worktree_rebase_pathlist_free(&merged_paths);
11733 if (upa.conflicts > 0 || upa.missing > 0 ||
11734 upa.not_deleted > 0 || upa.unversioned > 0) {
11735 error = got_worktree_histedit_postpone(worktree, fileindex);
11738 if (upa.conflicts > 0 && upa.missing == 0 &&
11739 upa.not_deleted == 0 && upa.unversioned == 0) {
11740 error = got_error_msg(GOT_ERR_CONFLICTS,
11741 "conflicts must be resolved before histedit "
11743 } else if (upa.conflicts > 0) {
11744 error = got_error_msg(GOT_ERR_CONFLICTS,
11745 "conflicts must be resolved before histedit "
11746 "can continue; changes destined for some "
11747 "files were not yet merged and should be "
11748 "merged manually if required before the "
11749 "histedit operation is continued");
11751 error = got_error_msg(GOT_ERR_CONFLICTS,
11752 "changes destined for some files were not "
11753 "yet merged and should be merged manually "
11754 "if required before the histedit operation "
11758 error = histedit_complete(worktree, fileindex, tmp_branch,
11761 got_object_id_queue_free(&commits);
11762 histedit_free_list(&histedit_cmds);
11763 free(head_commit_id);
11764 free(base_commit_id);
11765 free(resume_commit_id);
11767 got_object_commit_close(commit);
11769 got_ref_close(branch);
11771 got_ref_close(tmp_branch);
11773 got_worktree_close(worktree);
11775 const struct got_error *close_err = got_repo_close(repo);
11780 const struct got_error *pack_err =
11781 got_repo_pack_fds_close(pack_fds);
11789 usage_integrate(void)
11791 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11795 static const struct got_error *
11796 cmd_integrate(int argc, char *argv[])
11798 const struct got_error *error = NULL;
11799 struct got_repository *repo = NULL;
11800 struct got_worktree *worktree = NULL;
11801 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11802 const char *branch_arg = NULL;
11803 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11804 struct got_fileindex *fileindex = NULL;
11805 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11807 struct got_update_progress_arg upa;
11808 int *pack_fds = NULL;
11810 while ((ch = getopt(argc, argv, "")) != -1) {
11823 branch_arg = argv[0];
11825 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11826 "unveil", NULL) == -1)
11829 cwd = getcwd(NULL, 0);
11831 error = got_error_from_errno("getcwd");
11835 error = got_repo_pack_fds_open(&pack_fds);
11839 error = got_worktree_open(&worktree, cwd);
11841 if (error->code == GOT_ERR_NOT_WORKTREE)
11842 error = wrap_not_worktree_error(error, "integrate",
11847 error = check_rebase_or_histedit_in_progress(worktree);
11851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11856 error = apply_unveil(got_repo_get_path(repo), 0,
11857 got_worktree_get_root_path(worktree));
11861 error = check_merge_in_progress(worktree, repo);
11865 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11866 error = got_error_from_errno("asprintf");
11870 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11871 &base_branch_ref, worktree, refname, repo);
11875 refname = strdup(got_ref_get_name(branch_ref));
11876 if (refname == NULL) {
11877 error = got_error_from_errno("strdup");
11878 got_worktree_integrate_abort(worktree, fileindex, repo,
11879 branch_ref, base_branch_ref);
11882 base_refname = strdup(got_ref_get_name(base_branch_ref));
11883 if (base_refname == NULL) {
11884 error = got_error_from_errno("strdup");
11885 got_worktree_integrate_abort(worktree, fileindex, repo,
11886 branch_ref, base_branch_ref);
11890 error = got_ref_resolve(&commit_id, repo, branch_ref);
11894 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11898 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11899 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11900 "specified branch has already been integrated");
11901 got_worktree_integrate_abort(worktree, fileindex, repo,
11902 branch_ref, base_branch_ref);
11906 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11908 if (error->code == GOT_ERR_ANCESTRY)
11909 error = got_error(GOT_ERR_REBASE_REQUIRED);
11910 got_worktree_integrate_abort(worktree, fileindex, repo,
11911 branch_ref, base_branch_ref);
11915 memset(&upa, 0, sizeof(upa));
11916 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11917 branch_ref, base_branch_ref, update_progress, &upa,
11918 check_cancelled, NULL);
11922 printf("Integrated %s into %s\n", refname, base_refname);
11923 print_update_progress_stats(&upa);
11926 const struct got_error *close_err = got_repo_close(repo);
11931 got_worktree_close(worktree);
11933 const struct got_error *pack_err =
11934 got_repo_pack_fds_close(pack_fds);
11939 free(base_commit_id);
11942 free(base_refname);
11949 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11954 static const struct got_error *
11955 cmd_merge(int argc, char *argv[])
11957 const struct got_error *error = NULL;
11958 struct got_worktree *worktree = NULL;
11959 struct got_repository *repo = NULL;
11960 struct got_fileindex *fileindex = NULL;
11961 char *cwd = NULL, *id_str = NULL, *author = NULL;
11962 struct got_reference *branch = NULL, *wt_branch = NULL;
11963 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11964 struct got_object_id *wt_branch_tip = NULL;
11965 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11966 int interrupt_merge = 0;
11967 struct got_update_progress_arg upa;
11968 struct got_object_id *merge_commit_id = NULL;
11969 char *branch_name = NULL;
11970 int *pack_fds = NULL;
11972 memset(&upa, 0, sizeof(upa));
11974 while ((ch = getopt(argc, argv, "acn")) != -1) {
11980 continue_merge = 1;
11983 interrupt_merge = 1;
11995 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11996 "unveil", NULL) == -1)
12000 if (abort_merge && continue_merge)
12001 option_conflict('a', 'c');
12002 if (abort_merge || continue_merge) {
12005 } else if (argc != 1)
12008 cwd = getcwd(NULL, 0);
12010 error = got_error_from_errno("getcwd");
12014 error = got_repo_pack_fds_open(&pack_fds);
12018 error = got_worktree_open(&worktree, cwd);
12020 if (error->code == GOT_ERR_NOT_WORKTREE)
12021 error = wrap_not_worktree_error(error,
12026 error = got_repo_open(&repo,
12027 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12032 error = apply_unveil(got_repo_get_path(repo), 0,
12033 worktree ? got_worktree_get_root_path(worktree) : NULL);
12037 error = check_rebase_or_histedit_in_progress(worktree);
12041 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12047 if (!merge_in_progress) {
12048 error = got_error(GOT_ERR_NOT_MERGING);
12051 error = got_worktree_merge_continue(&branch_name,
12052 &branch_tip, &fileindex, worktree, repo);
12055 error = got_worktree_merge_abort(worktree, fileindex, repo,
12056 abort_progress, &upa);
12059 printf("Merge of %s aborted\n", branch_name);
12060 goto done; /* nothing else to do */
12063 error = get_author(&author, repo, worktree);
12067 if (continue_merge) {
12068 if (!merge_in_progress) {
12069 error = got_error(GOT_ERR_NOT_MERGING);
12072 error = got_worktree_merge_continue(&branch_name,
12073 &branch_tip, &fileindex, worktree, repo);
12077 error = got_ref_open(&branch, repo, argv[0], 0);
12080 branch_name = strdup(got_ref_get_name(branch));
12081 if (branch_name == NULL) {
12082 error = got_error_from_errno("strdup");
12085 error = got_ref_resolve(&branch_tip, repo, branch);
12090 error = got_ref_open(&wt_branch, repo,
12091 got_worktree_get_head_ref_name(worktree), 0);
12094 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12097 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12098 wt_branch_tip, branch_tip, 0, repo,
12099 check_cancelled, NULL);
12100 if (error && error->code != GOT_ERR_ANCESTRY)
12103 if (!continue_merge) {
12104 error = check_path_prefix(wt_branch_tip, branch_tip,
12105 got_worktree_get_path_prefix(worktree),
12106 GOT_ERR_MERGE_PATH, repo);
12110 error = check_same_branch(wt_branch_tip, branch,
12113 if (error->code != GOT_ERR_ANCESTRY)
12117 static char msg[512];
12118 snprintf(msg, sizeof(msg),
12119 "cannot create a merge commit because "
12120 "%s is based on %s; %s can be integrated "
12121 "with 'got integrate' instead", branch_name,
12122 got_worktree_get_head_ref_name(worktree),
12124 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12128 error = got_worktree_merge_prepare(&fileindex, worktree,
12133 error = got_worktree_merge_branch(worktree, fileindex,
12134 yca_id, branch_tip, repo, update_progress, &upa,
12135 check_cancelled, NULL);
12138 print_merge_progress_stats(&upa);
12139 if (!upa.did_something) {
12140 error = got_worktree_merge_abort(worktree, fileindex,
12141 repo, abort_progress, &upa);
12144 printf("Already up-to-date\n");
12149 if (interrupt_merge) {
12150 error = got_worktree_merge_postpone(worktree, fileindex);
12153 printf("Merge of %s interrupted on request\n", branch_name);
12154 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12155 upa.not_deleted > 0 || upa.unversioned > 0) {
12156 error = got_worktree_merge_postpone(worktree, fileindex);
12159 if (upa.conflicts > 0 && upa.missing == 0 &&
12160 upa.not_deleted == 0 && upa.unversioned == 0) {
12161 error = got_error_msg(GOT_ERR_CONFLICTS,
12162 "conflicts must be resolved before merging "
12164 } else if (upa.conflicts > 0) {
12165 error = got_error_msg(GOT_ERR_CONFLICTS,
12166 "conflicts must be resolved before merging "
12167 "can continue; changes destined for some "
12168 "files were not yet merged and "
12169 "should be merged manually if required before the "
12170 "merge operation is continued");
12172 error = got_error_msg(GOT_ERR_CONFLICTS,
12173 "changes destined for some "
12174 "files were not yet merged and should be "
12175 "merged manually if required before the "
12176 "merge operation is continued");
12180 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12181 fileindex, author, NULL, 1, branch_tip, branch_name,
12182 repo, continue_merge ? print_status : NULL, NULL);
12185 error = got_worktree_merge_complete(worktree, fileindex, repo);
12188 error = got_object_id_str(&id_str, merge_commit_id);
12191 printf("Merged %s into %s: %s\n", branch_name,
12192 got_worktree_get_head_ref_name(worktree),
12198 free(merge_commit_id);
12204 got_ref_close(branch);
12206 got_ref_close(wt_branch);
12208 got_worktree_close(worktree);
12210 const struct got_error *close_err = got_repo_close(repo);
12215 const struct got_error *pack_err =
12216 got_repo_pack_fds_close(pack_fds);
12226 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12227 "[-S] [file-path ...]\n",
12232 static const struct got_error *
12233 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12234 const char *path, struct got_object_id *blob_id,
12235 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12236 int dirfd, const char *de_name)
12238 const struct got_error *err = NULL;
12239 char *id_str = NULL;
12241 if (staged_status != GOT_STATUS_ADD &&
12242 staged_status != GOT_STATUS_MODIFY &&
12243 staged_status != GOT_STATUS_DELETE)
12246 if (staged_status == GOT_STATUS_ADD ||
12247 staged_status == GOT_STATUS_MODIFY)
12248 err = got_object_id_str(&id_str, staged_blob_id);
12250 err = got_object_id_str(&id_str, blob_id);
12254 printf("%s %c %s\n", id_str, staged_status, path);
12259 static const struct got_error *
12260 cmd_stage(int argc, char *argv[])
12262 const struct got_error *error = NULL;
12263 struct got_repository *repo = NULL;
12264 struct got_worktree *worktree = NULL;
12266 struct got_pathlist_head paths;
12267 struct got_pathlist_entry *pe;
12268 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12269 FILE *patch_script_file = NULL;
12270 const char *patch_script_path = NULL;
12271 struct choose_patch_arg cpa;
12272 int *pack_fds = NULL;
12274 TAILQ_INIT(&paths);
12276 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12285 patch_script_path = optarg;
12288 allow_bad_symlinks = 1;
12300 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12301 "unveil", NULL) == -1)
12304 if (list_stage && (pflag || patch_script_path))
12305 errx(1, "-l option cannot be used with other options");
12306 if (patch_script_path && !pflag)
12307 errx(1, "-F option can only be used together with -p option");
12309 cwd = getcwd(NULL, 0);
12311 error = got_error_from_errno("getcwd");
12315 error = got_repo_pack_fds_open(&pack_fds);
12319 error = got_worktree_open(&worktree, cwd);
12321 if (error->code == GOT_ERR_NOT_WORKTREE)
12322 error = wrap_not_worktree_error(error, "stage", cwd);
12326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12331 if (patch_script_path) {
12332 patch_script_file = fopen(patch_script_path, "re");
12333 if (patch_script_file == NULL) {
12334 error = got_error_from_errno2("fopen",
12335 patch_script_path);
12339 error = apply_unveil(got_repo_get_path(repo), 0,
12340 got_worktree_get_root_path(worktree));
12344 error = check_merge_in_progress(worktree, repo);
12348 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12353 error = got_worktree_status(worktree, &paths, repo, 0,
12354 print_stage, NULL, check_cancelled, NULL);
12356 cpa.patch_script_file = patch_script_file;
12357 cpa.action = "stage";
12358 error = got_worktree_stage(worktree, &paths,
12359 pflag ? NULL : print_status, NULL,
12360 pflag ? choose_patch : NULL, &cpa,
12361 allow_bad_symlinks, repo);
12364 if (patch_script_file && fclose(patch_script_file) == EOF &&
12366 error = got_error_from_errno2("fclose", patch_script_path);
12368 const struct got_error *close_err = got_repo_close(repo);
12373 got_worktree_close(worktree);
12375 const struct got_error *pack_err =
12376 got_repo_pack_fds_close(pack_fds);
12380 TAILQ_FOREACH(pe, &paths, entry)
12381 free((char *)pe->path);
12382 got_pathlist_free(&paths);
12388 usage_unstage(void)
12390 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12391 "[file-path ...]\n",
12397 static const struct got_error *
12398 cmd_unstage(int argc, char *argv[])
12400 const struct got_error *error = NULL;
12401 struct got_repository *repo = NULL;
12402 struct got_worktree *worktree = NULL;
12404 struct got_pathlist_head paths;
12405 struct got_pathlist_entry *pe;
12407 struct got_update_progress_arg upa;
12408 FILE *patch_script_file = NULL;
12409 const char *patch_script_path = NULL;
12410 struct choose_patch_arg cpa;
12411 int *pack_fds = NULL;
12413 TAILQ_INIT(&paths);
12415 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12421 patch_script_path = optarg;
12433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12434 "unveil", NULL) == -1)
12437 if (patch_script_path && !pflag)
12438 errx(1, "-F option can only be used together with -p option");
12440 cwd = getcwd(NULL, 0);
12442 error = got_error_from_errno("getcwd");
12446 error = got_repo_pack_fds_open(&pack_fds);
12450 error = got_worktree_open(&worktree, cwd);
12452 if (error->code == GOT_ERR_NOT_WORKTREE)
12453 error = wrap_not_worktree_error(error, "unstage", cwd);
12457 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12462 if (patch_script_path) {
12463 patch_script_file = fopen(patch_script_path, "re");
12464 if (patch_script_file == NULL) {
12465 error = got_error_from_errno2("fopen",
12466 patch_script_path);
12471 error = apply_unveil(got_repo_get_path(repo), 0,
12472 got_worktree_get_root_path(worktree));
12476 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12480 cpa.patch_script_file = patch_script_file;
12481 cpa.action = "unstage";
12482 memset(&upa, 0, sizeof(upa));
12483 error = got_worktree_unstage(worktree, &paths, update_progress,
12484 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12486 print_merge_progress_stats(&upa);
12488 if (patch_script_file && fclose(patch_script_file) == EOF &&
12490 error = got_error_from_errno2("fclose", patch_script_path);
12492 const struct got_error *close_err = got_repo_close(repo);
12497 got_worktree_close(worktree);
12499 const struct got_error *pack_err =
12500 got_repo_pack_fds_close(pack_fds);
12504 TAILQ_FOREACH(pe, &paths, entry)
12505 free((char *)pe->path);
12506 got_pathlist_free(&paths);
12514 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12515 "arg1 [arg2 ...]\n", getprogname());
12519 static const struct got_error *
12520 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12522 const struct got_error *err;
12523 struct got_blob_object *blob;
12526 fd = got_opentempfd();
12528 return got_error_from_errno("got_opentempfd");
12530 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12534 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12536 if (fd != -1 && close(fd) == -1 && err == NULL)
12537 err = got_error_from_errno("close");
12539 got_object_blob_close(blob);
12543 static const struct got_error *
12544 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12546 const struct got_error *err;
12547 struct got_tree_object *tree;
12550 err = got_object_open_as_tree(&tree, repo, id);
12554 nentries = got_object_tree_get_nentries(tree);
12555 for (i = 0; i < nentries; i++) {
12556 struct got_tree_entry *te;
12558 if (sigint_received || sigpipe_received)
12560 te = got_object_tree_get_entry(tree, i);
12561 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12564 fprintf(outfile, "%s %.7o %s\n", id_str,
12565 got_tree_entry_get_mode(te),
12566 got_tree_entry_get_name(te));
12570 got_object_tree_close(tree);
12574 static const struct got_error *
12575 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12577 const struct got_error *err;
12578 struct got_commit_object *commit;
12579 const struct got_object_id_queue *parent_ids;
12580 struct got_object_qid *pid;
12581 char *id_str = NULL;
12582 const char *logmsg = NULL;
12585 err = got_object_open_as_commit(&commit, repo, id);
12589 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12593 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12594 parent_ids = got_object_commit_get_parent_ids(commit);
12595 fprintf(outfile, "numparents %d\n",
12596 got_object_commit_get_nparents(commit));
12597 STAILQ_FOREACH(pid, parent_ids, entry) {
12599 err = got_object_id_str(&pid_str, &pid->id);
12602 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12605 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12606 got_object_commit_get_author_gmtoff(commit));
12607 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12608 got_object_commit_get_author(commit),
12609 (long long)got_object_commit_get_author_time(commit),
12612 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12613 got_object_commit_get_committer_gmtoff(commit));
12614 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12615 got_object_commit_get_author(commit),
12616 (long long)got_object_commit_get_committer_time(commit),
12619 logmsg = got_object_commit_get_logmsg_raw(commit);
12620 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12621 fprintf(outfile, "%s", logmsg);
12624 got_object_commit_close(commit);
12628 static const struct got_error *
12629 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12631 const struct got_error *err;
12632 struct got_tag_object *tag;
12633 char *id_str = NULL;
12634 const char *tagmsg = NULL;
12637 err = got_object_open_as_tag(&tag, repo, id);
12641 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12645 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12647 switch (got_object_tag_get_object_type(tag)) {
12648 case GOT_OBJ_TYPE_BLOB:
12649 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12650 GOT_OBJ_LABEL_BLOB);
12652 case GOT_OBJ_TYPE_TREE:
12653 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12654 GOT_OBJ_LABEL_TREE);
12656 case GOT_OBJ_TYPE_COMMIT:
12657 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12658 GOT_OBJ_LABEL_COMMIT);
12660 case GOT_OBJ_TYPE_TAG:
12661 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12662 GOT_OBJ_LABEL_TAG);
12668 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12669 got_object_tag_get_name(tag));
12671 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12672 got_object_tag_get_tagger_gmtoff(tag));
12673 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12674 got_object_tag_get_tagger(tag),
12675 (long long)got_object_tag_get_tagger_time(tag),
12678 tagmsg = got_object_tag_get_message(tag);
12679 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12680 fprintf(outfile, "%s", tagmsg);
12683 got_object_tag_close(tag);
12687 static const struct got_error *
12688 cmd_cat(int argc, char *argv[])
12690 const struct got_error *error;
12691 struct got_repository *repo = NULL;
12692 struct got_worktree *worktree = NULL;
12693 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12694 const char *commit_id_str = NULL;
12695 struct got_object_id *id = NULL, *commit_id = NULL;
12696 struct got_commit_object *commit = NULL;
12697 int ch, obj_type, i, force_path = 0;
12698 struct got_reflist_head refs;
12699 int *pack_fds = NULL;
12704 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12709 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12712 commit_id_str = optarg;
12715 repo_path = realpath(optarg, NULL);
12716 if (repo_path == NULL)
12717 return got_error_from_errno2("realpath",
12719 got_path_strip_trailing_slashes(repo_path);
12733 cwd = getcwd(NULL, 0);
12735 error = got_error_from_errno("getcwd");
12739 error = got_repo_pack_fds_open(&pack_fds);
12743 if (repo_path == NULL) {
12744 error = got_worktree_open(&worktree, cwd);
12745 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12748 repo_path = strdup(
12749 got_worktree_get_repo_path(worktree));
12750 if (repo_path == NULL) {
12751 error = got_error_from_errno("strdup");
12755 /* Release work tree lock. */
12756 got_worktree_close(worktree);
12761 if (repo_path == NULL) {
12762 repo_path = strdup(cwd);
12763 if (repo_path == NULL)
12764 return got_error_from_errno("strdup");
12767 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12772 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12776 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12780 if (commit_id_str == NULL)
12781 commit_id_str = GOT_REF_HEAD;
12782 error = got_repo_match_object_id(&commit_id, NULL,
12783 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12787 error = got_object_open_as_commit(&commit, repo, commit_id);
12791 for (i = 0; i < argc; i++) {
12793 error = got_object_id_by_path(&id, repo, commit,
12798 error = got_repo_match_object_id(&id, &label, argv[i],
12799 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12802 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12803 error->code != GOT_ERR_NOT_REF)
12805 error = got_object_id_by_path(&id, repo,
12812 error = got_object_get_type(&obj_type, repo, id);
12816 switch (obj_type) {
12817 case GOT_OBJ_TYPE_BLOB:
12818 error = cat_blob(id, repo, stdout);
12820 case GOT_OBJ_TYPE_TREE:
12821 error = cat_tree(id, repo, stdout);
12823 case GOT_OBJ_TYPE_COMMIT:
12824 error = cat_commit(id, repo, stdout);
12826 case GOT_OBJ_TYPE_TAG:
12827 error = cat_tag(id, repo, stdout);
12830 error = got_error(GOT_ERR_OBJ_TYPE);
12845 got_object_commit_close(commit);
12847 got_worktree_close(worktree);
12849 const struct got_error *close_err = got_repo_close(repo);
12854 const struct got_error *pack_err =
12855 got_repo_pack_fds_close(pack_fds);
12860 got_ref_list_free(&refs);
12867 fprintf(stderr, "usage: %s info [path ...]\n",
12872 static const struct got_error *
12873 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12874 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12875 struct got_object_id *commit_id)
12877 const struct got_error *err = NULL;
12878 char *id_str = NULL;
12880 struct tm mytm, *tm;
12881 struct got_pathlist_head *paths = arg;
12882 struct got_pathlist_entry *pe;
12885 * Clear error indication from any of the path arguments which
12886 * would cause this file index entry to be displayed.
12888 TAILQ_FOREACH(pe, paths, entry) {
12889 if (got_path_cmp(path, pe->path, strlen(path),
12890 pe->path_len) == 0 ||
12891 got_path_is_child(path, pe->path, pe->path_len))
12892 pe->data = NULL; /* no error */
12895 printf(GOT_COMMIT_SEP_STR);
12897 printf("symlink: %s\n", path);
12898 else if (S_ISREG(mode)) {
12899 printf("file: %s\n", path);
12900 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12901 } else if (S_ISDIR(mode))
12902 printf("directory: %s\n", path);
12904 printf("something: %s\n", path);
12906 tm = localtime_r(&mtime, &mytm);
12909 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12910 return got_error(GOT_ERR_NO_SPACE);
12911 printf("timestamp: %s\n", datebuf);
12914 err = got_object_id_str(&id_str, blob_id);
12917 printf("based on blob: %s\n", id_str);
12921 if (staged_blob_id) {
12922 err = got_object_id_str(&id_str, staged_blob_id);
12925 printf("based on staged blob: %s\n", id_str);
12930 err = got_object_id_str(&id_str, commit_id);
12933 printf("based on commit: %s\n", id_str);
12940 static const struct got_error *
12941 cmd_info(int argc, char *argv[])
12943 const struct got_error *error = NULL;
12944 struct got_worktree *worktree = NULL;
12945 char *cwd = NULL, *id_str = NULL;
12946 struct got_pathlist_head paths;
12947 struct got_pathlist_entry *pe;
12948 char *uuidstr = NULL;
12949 int ch, show_files = 0;
12950 int *pack_fds = NULL;
12952 TAILQ_INIT(&paths);
12954 while ((ch = getopt(argc, argv, "")) != -1) {
12966 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12970 cwd = getcwd(NULL, 0);
12972 error = got_error_from_errno("getcwd");
12976 error = got_repo_pack_fds_open(&pack_fds);
12980 error = got_worktree_open(&worktree, cwd);
12982 if (error->code == GOT_ERR_NOT_WORKTREE)
12983 error = wrap_not_worktree_error(error, "info", cwd);
12988 /* Remove "cpath" promise. */
12989 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12993 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12998 error = get_worktree_paths_from_argv(&paths, argc, argv,
13005 error = got_object_id_str(&id_str,
13006 got_worktree_get_base_commit_id(worktree));
13010 error = got_worktree_get_uuid(&uuidstr, worktree);
13014 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13015 printf("work tree base commit: %s\n", id_str);
13016 printf("work tree path prefix: %s\n",
13017 got_worktree_get_path_prefix(worktree));
13018 printf("work tree branch reference: %s\n",
13019 got_worktree_get_head_ref_name(worktree));
13020 printf("work tree UUID: %s\n", uuidstr);
13021 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13024 struct got_pathlist_entry *pe;
13025 TAILQ_FOREACH(pe, &paths, entry) {
13026 if (pe->path_len == 0)
13029 * Assume this path will fail. This will be corrected
13030 * in print_path_info() in case the path does suceeed.
13032 pe->data = (void *)got_error_path(pe->path,
13035 error = got_worktree_path_info(worktree, &paths,
13036 print_path_info, &paths, check_cancelled, NULL);
13039 TAILQ_FOREACH(pe, &paths, entry) {
13040 if (pe->data != NULL) {
13041 error = pe->data; /* bad path */
13048 const struct got_error *pack_err =
13049 got_repo_pack_fds_close(pack_fds);
13053 TAILQ_FOREACH(pe, &paths, entry)
13054 free((char *)pe->path);
13055 got_pathlist_free(&paths);