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] [-I pattern] [-m message] "
351 "[-r repository-path] directory\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;
401 size_t len, logmsg_len;
402 char *initial_content_stripped = NULL, *buf = NULL, *s;
406 if (stat(logmsg_path, &st) == -1)
407 return got_error_from_errno2("stat", logmsg_path);
409 if (spawn_editor(editor, logmsg_path) == -1)
410 return got_error_from_errno("failed spawning editor");
412 if (stat(logmsg_path, &st2) == -1)
413 return got_error_from_errno("stat");
415 if (require_modification &&
416 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
417 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
418 "no changes made to commit message, aborting");
421 * Set up a stripped version of the initial content without comments
422 * and blank lines. We need this in order to check if the message
423 * has in fact been edited.
425 initial_content_stripped = malloc(initial_content_len + 1);
426 if (initial_content_stripped == NULL)
427 return got_error_from_errno("malloc");
428 initial_content_stripped[0] = '\0';
430 buf = strdup(initial_content);
432 err = got_error_from_errno("strdup");
437 while ((line = strsep(&s, "\n")) != NULL) {
438 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(initial_content_stripped, line,
441 initial_content_len + 1);
442 if (len >= initial_content_len + 1) {
443 err = got_error(GOT_ERR_NO_SPACE);
447 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
448 initial_content_stripped[len - 1] = '\0';
452 logmsg_len = st2.st_size;
453 *logmsg = malloc(logmsg_len + 1);
455 return got_error_from_errno("malloc");
458 fp = fopen(logmsg_path, "re");
460 err = got_error_from_errno("fopen");
465 while (getline(&line, &linesize, fp) != -1) {
466 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
467 continue; /* remove comments and leading empty lines */
468 len = strlcat(*logmsg, line, logmsg_len + 1);
469 if (len >= logmsg_len + 1) {
470 err = got_error(GOT_ERR_NO_SPACE);
476 err = got_ferror(fp, GOT_ERR_IO);
479 while (len > 0 && (*logmsg)[len - 1] == '\n') {
480 (*logmsg)[len - 1] = '\0';
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
489 if (require_modification &&
490 strcmp(*logmsg, initial_content_stripped) == 0)
491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
492 "no changes made to commit message, aborting");
494 free(initial_content_stripped);
496 if (fp && fclose(fp) == EOF && err == NULL)
497 err = got_error_from_errno("fclose");
505 static const struct got_error *
506 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
507 const char *path_dir, const char *branch_name)
509 char *initial_content = NULL;
510 const struct got_error *err = NULL;
511 int initial_content_len;
514 initial_content_len = asprintf(&initial_content,
515 "\n# %s to be imported to branch %s\n", path_dir,
517 if (initial_content_len == -1)
518 return got_error_from_errno("asprintf");
520 err = got_opentemp_named_fd(logmsg_path, &fd,
521 GOT_TMPDIR_STR "/got-importmsg");
525 if (write(fd, initial_content, initial_content_len) == -1) {
526 err = got_error_from_errno2("write", *logmsg_path);
530 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
531 initial_content_len, 1);
533 if (fd != -1 && close(fd) == -1 && err == NULL)
534 err = got_error_from_errno2("close", *logmsg_path);
535 free(initial_content);
543 static const struct got_error *
544 import_progress(void *arg, const char *path)
546 printf("A %s\n", path);
550 static const struct got_error *
551 valid_author(const char *author)
553 const char *email = author;
556 * Git' expects the author (or committer) to be in the form
557 * "name <email>", which are mostly free form (see the
558 * "committer" description in git-fast-import(1)). We're only
559 * doing this to avoid git's object parser breaking on commits
563 while (*author && *author != '\n' && *author != '<' && *author != '>')
565 if (*author++ != '<')
566 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
567 while (*author && *author != '\n' && *author != '<' && *author != '>')
569 if (strcmp(author, ">") != 0)
570 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
574 static const struct got_error *
575 get_author(char **author, struct got_repository *repo,
576 struct got_worktree *worktree)
578 const struct got_error *err = NULL;
579 const char *got_author = NULL, *name, *email;
580 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
585 worktree_conf = got_worktree_get_gotconfig(worktree);
586 repo_conf = got_repo_get_gotconfig(repo);
589 * Priority of potential author information sources, from most
590 * significant to least significant:
591 * 1) work tree's .got/got.conf file
592 * 2) repository's got.conf file
593 * 3) repository's git config file
594 * 4) environment variables
595 * 5) global git config files (in user's home directory or /etc)
599 got_author = got_gotconfig_get_author(worktree_conf);
600 if (got_author == NULL)
601 got_author = got_gotconfig_get_author(repo_conf);
602 if (got_author == NULL) {
603 name = got_repo_get_gitconfig_author_name(repo);
604 email = got_repo_get_gitconfig_author_email(repo);
606 if (asprintf(author, "%s <%s>", name, email) == -1)
607 return got_error_from_errno("asprintf");
611 got_author = getenv("GOT_AUTHOR");
612 if (got_author == NULL) {
613 name = got_repo_get_global_gitconfig_author_name(repo);
614 email = got_repo_get_global_gitconfig_author_email(
617 if (asprintf(author, "%s <%s>", name, email)
619 return got_error_from_errno("asprintf");
622 /* TODO: Look up user in password database? */
623 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
627 *author = strdup(got_author);
629 return got_error_from_errno("strdup");
631 err = valid_author(*author);
639 static const struct got_error *
640 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
641 struct got_worktree *worktree)
643 const char *got_allowed_signers = NULL;
644 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
646 *allowed_signers = NULL;
649 worktree_conf = got_worktree_get_gotconfig(worktree);
650 repo_conf = got_repo_get_gotconfig(repo);
653 * Priority of potential author information sources, from most
654 * significant to least significant:
655 * 1) work tree's .got/got.conf file
656 * 2) repository's got.conf file
660 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
662 if (got_allowed_signers == NULL)
663 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
666 if (got_allowed_signers) {
667 *allowed_signers = strdup(got_allowed_signers);
668 if (*allowed_signers == NULL)
669 return got_error_from_errno("strdup");
674 static const struct got_error *
675 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
676 struct got_worktree *worktree)
678 const char *got_revoked_signers = NULL;
679 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
681 *revoked_signers = NULL;
684 worktree_conf = got_worktree_get_gotconfig(worktree);
685 repo_conf = got_repo_get_gotconfig(repo);
688 * Priority of potential author information sources, from most
689 * significant to least significant:
690 * 1) work tree's .got/got.conf file
691 * 2) repository's got.conf file
695 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
697 if (got_revoked_signers == NULL)
698 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
701 if (got_revoked_signers) {
702 *revoked_signers = strdup(got_revoked_signers);
703 if (*revoked_signers == NULL)
704 return got_error_from_errno("strdup");
709 static const struct got_error *
710 get_signer_id(char **signer_id, struct got_repository *repo,
711 struct got_worktree *worktree)
713 const char *got_signer_id = NULL;
714 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
719 worktree_conf = got_worktree_get_gotconfig(worktree);
720 repo_conf = got_repo_get_gotconfig(repo);
723 * Priority of potential author information sources, from most
724 * significant to least significant:
725 * 1) work tree's .got/got.conf file
726 * 2) repository's got.conf file
730 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
731 if (got_signer_id == NULL)
732 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
735 *signer_id = strdup(got_signer_id);
736 if (*signer_id == NULL)
737 return got_error_from_errno("strdup");
742 static const struct got_error *
743 get_gitconfig_path(char **gitconfig_path)
745 const char *homedir = getenv("HOME");
747 *gitconfig_path = NULL;
749 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
750 return got_error_from_errno("asprintf");
756 static const struct got_error *
757 cmd_import(int argc, char *argv[])
759 const struct got_error *error = NULL;
760 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
761 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
762 const char *branch_name = NULL;
763 char *id_str = NULL, *logmsg_path = NULL;
764 char refname[PATH_MAX] = "refs/heads/";
765 struct got_repository *repo = NULL;
766 struct got_reference *branch_ref = NULL, *head_ref = NULL;
767 struct got_object_id *new_commit_id = NULL;
769 struct got_pathlist_head ignores;
770 struct got_pathlist_entry *pe;
771 int preserve_logmsg = 0;
772 int *pack_fds = NULL;
774 TAILQ_INIT(&ignores);
776 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
779 branch_name = optarg;
782 logmsg = strdup(optarg);
783 if (logmsg == NULL) {
784 error = got_error_from_errno("strdup");
789 repo_path = realpath(optarg, NULL);
790 if (repo_path == NULL) {
791 error = got_error_from_errno2("realpath",
797 if (optarg[0] == '\0')
799 error = got_pathlist_insert(&pe, &ignores, optarg,
814 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
822 if (repo_path == NULL) {
823 repo_path = getcwd(NULL, 0);
824 if (repo_path == NULL)
825 return got_error_from_errno("getcwd");
827 got_path_strip_trailing_slashes(repo_path);
828 error = get_gitconfig_path(&gitconfig_path);
831 error = got_repo_pack_fds_open(&pack_fds);
834 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
838 error = get_author(&author, repo, NULL);
843 * Don't let the user create a branch name with a leading '-'.
844 * While technically a valid reference name, this case is usually
845 * an unintended typo.
847 if (branch_name && branch_name[0] == '-')
848 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
850 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
851 if (error && error->code != GOT_ERR_NOT_REF)
855 n = strlcat(refname, branch_name, sizeof(refname));
856 else if (head_ref && got_ref_is_symbolic(head_ref))
857 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
860 n = strlcat(refname, "main", sizeof(refname));
861 if (n >= sizeof(refname)) {
862 error = got_error(GOT_ERR_NO_SPACE);
866 error = got_ref_open(&branch_ref, repo, refname, 0);
868 if (error->code != GOT_ERR_NOT_REF)
871 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
872 "import target branch already exists");
876 path_dir = realpath(argv[0], NULL);
877 if (path_dir == NULL) {
878 error = got_error_from_errno2("realpath", argv[0]);
881 got_path_strip_trailing_slashes(path_dir);
884 * unveil(2) traverses exec(2); if an editor is used we have
885 * to apply unveil after the log message has been written.
887 if (logmsg == NULL || strlen(logmsg) == 0) {
888 error = get_editor(&editor);
892 error = collect_import_msg(&logmsg, &logmsg_path, editor,
895 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
902 if (unveil(path_dir, "r") != 0) {
903 error = got_error_from_errno2("unveil", path_dir);
909 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
916 error = got_repo_import(&new_commit_id, path_dir, logmsg,
917 author, &ignores, repo, import_progress, NULL);
924 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
931 error = got_ref_write(branch_ref, repo);
938 error = got_object_id_str(&id_str, new_commit_id);
945 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
947 if (error->code != GOT_ERR_NOT_REF) {
953 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
961 error = got_ref_write(head_ref, repo);
969 printf("Created branch %s with commit %s\n",
970 got_ref_get_name(branch_ref), id_str);
973 const struct got_error *pack_err =
974 got_repo_pack_fds_close(pack_fds);
978 if (preserve_logmsg) {
979 fprintf(stderr, "%s: log message preserved in %s\n",
980 getprogname(), logmsg_path);
981 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
982 error = got_error_from_errno2("unlink", logmsg_path);
990 free(gitconfig_path);
992 got_ref_close(branch_ref);
994 got_ref_close(head_ref);
1001 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1002 "repository-URL [directory]\n", getprogname());
1006 struct got_fetch_progress_arg {
1007 char last_scaled_size[FMT_SCALED_STRSIZE];
1009 int last_p_resolved;
1012 struct got_repository *repo;
1015 int configs_created;
1017 struct got_pathlist_head *symrefs;
1018 struct got_pathlist_head *wanted_branches;
1019 struct got_pathlist_head *wanted_refs;
1023 const char *remote_repo_path;
1024 const char *git_url;
1025 int fetch_all_branches;
1026 int mirror_references;
1030 /* XXX forward declaration */
1031 static const struct got_error *
1032 create_config_files(const char *proto, const char *host, const char *port,
1033 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1034 int mirror_references, struct got_pathlist_head *symrefs,
1035 struct got_pathlist_head *wanted_branches,
1036 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1038 static const struct got_error *
1039 fetch_progress(void *arg, const char *message, off_t packfile_size,
1040 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1042 const struct got_error *err = NULL;
1043 struct got_fetch_progress_arg *a = arg;
1044 char scaled_size[FMT_SCALED_STRSIZE];
1045 int p_indexed, p_resolved;
1046 int print_size = 0, print_indexed = 0, print_resolved = 0;
1049 * In order to allow a failed clone to be resumed with 'got fetch'
1050 * we try to create configuration files as soon as possible.
1051 * Once the server has sent information about its default branch
1052 * we have all required information.
1054 if (a->create_configs && !a->configs_created &&
1055 !TAILQ_EMPTY(a->config_info.symrefs)) {
1056 err = create_config_files(a->config_info.proto,
1057 a->config_info.host, a->config_info.port,
1058 a->config_info.remote_repo_path,
1059 a->config_info.git_url,
1060 a->config_info.fetch_all_branches,
1061 a->config_info.mirror_references,
1062 a->config_info.symrefs,
1063 a->config_info.wanted_branches,
1064 a->config_info.wanted_refs, a->repo);
1067 a->configs_created = 1;
1070 if (a->verbosity < 0)
1073 if (message && message[0] != '\0') {
1074 printf("\rserver: %s", message);
1079 if (packfile_size > 0 || nobj_indexed > 0) {
1080 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1081 (a->last_scaled_size[0] == '\0' ||
1082 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1084 if (strlcpy(a->last_scaled_size, scaled_size,
1085 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1086 return got_error(GOT_ERR_NO_SPACE);
1088 if (nobj_indexed > 0) {
1089 p_indexed = (nobj_indexed * 100) / nobj_total;
1090 if (p_indexed != a->last_p_indexed) {
1091 a->last_p_indexed = p_indexed;
1096 if (nobj_resolved > 0) {
1097 p_resolved = (nobj_resolved * 100) /
1098 (nobj_total - nobj_loose);
1099 if (p_resolved != a->last_p_resolved) {
1100 a->last_p_resolved = p_resolved;
1108 if (print_size || print_indexed || print_resolved)
1111 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1113 printf("; indexing %d%%", p_indexed);
1115 printf("; resolving deltas %d%%", p_resolved);
1116 if (print_size || print_indexed || print_resolved)
1122 static const struct got_error *
1123 create_symref(const char *refname, struct got_reference *target_ref,
1124 int verbosity, struct got_repository *repo)
1126 const struct got_error *err;
1127 struct got_reference *head_symref;
1129 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1133 err = got_ref_write(head_symref, repo);
1134 if (err == NULL && verbosity > 0) {
1135 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1136 got_ref_get_name(target_ref));
1138 got_ref_close(head_symref);
1142 static const struct got_error *
1143 list_remote_refs(struct got_pathlist_head *symrefs,
1144 struct got_pathlist_head *refs)
1146 const struct got_error *err;
1147 struct got_pathlist_entry *pe;
1149 TAILQ_FOREACH(pe, symrefs, entry) {
1150 const char *refname = pe->path;
1151 const char *targetref = pe->data;
1153 printf("%s: %s\n", refname, targetref);
1156 TAILQ_FOREACH(pe, refs, entry) {
1157 const char *refname = pe->path;
1158 struct got_object_id *id = pe->data;
1161 err = got_object_id_str(&id_str, id);
1164 printf("%s: %s\n", refname, id_str);
1171 static const struct got_error *
1172 create_ref(const char *refname, struct got_object_id *id,
1173 int verbosity, struct got_repository *repo)
1175 const struct got_error *err = NULL;
1176 struct got_reference *ref;
1179 err = got_object_id_str(&id_str, id);
1183 err = got_ref_alloc(&ref, refname, id);
1187 err = got_ref_write(ref, repo);
1190 if (err == NULL && verbosity >= 0)
1191 printf("Created reference %s: %s\n", refname, id_str);
1198 match_wanted_ref(const char *refname, const char *wanted_ref)
1200 if (strncmp(refname, "refs/", 5) != 0)
1205 * Prevent fetching of references that won't make any
1206 * sense outside of the remote repository's context.
1208 if (strncmp(refname, "got/", 4) == 0)
1210 if (strncmp(refname, "remotes/", 8) == 0)
1213 if (strncmp(wanted_ref, "refs/", 5) == 0)
1216 /* Allow prefix match. */
1217 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1220 /* Allow exact match. */
1221 return (strcmp(refname, wanted_ref) == 0);
1225 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1227 struct got_pathlist_entry *pe;
1229 TAILQ_FOREACH(pe, wanted_refs, entry) {
1230 if (match_wanted_ref(refname, pe->path))
1237 static const struct got_error *
1238 create_wanted_ref(const char *refname, struct got_object_id *id,
1239 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1241 const struct got_error *err;
1242 char *remote_refname;
1244 if (strncmp("refs/", refname, 5) == 0)
1247 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1248 remote_repo_name, refname) == -1)
1249 return got_error_from_errno("asprintf");
1251 err = create_ref(remote_refname, id, verbosity, repo);
1252 free(remote_refname);
1256 static const struct got_error *
1257 create_gotconfig(const char *proto, const char *host, const char *port,
1258 const char *remote_repo_path, const char *default_branch,
1259 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1260 struct got_pathlist_head *wanted_refs, int mirror_references,
1261 struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 char *gotconfig_path = NULL;
1265 char *gotconfig = NULL;
1266 FILE *gotconfig_file = NULL;
1267 const char *branchname = NULL;
1268 char *branches = NULL, *refs = NULL;
1271 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1272 struct got_pathlist_entry *pe;
1273 TAILQ_FOREACH(pe, wanted_branches, entry) {
1275 branchname = pe->path;
1276 if (strncmp(branchname, "refs/heads/", 11) == 0)
1278 if (asprintf(&s, "%s\"%s\" ",
1279 branches ? branches : "", branchname) == -1) {
1280 err = got_error_from_errno("asprintf");
1286 } else if (!fetch_all_branches && default_branch) {
1287 branchname = default_branch;
1288 if (strncmp(branchname, "refs/heads/", 11) == 0)
1290 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1291 err = got_error_from_errno("asprintf");
1295 if (!TAILQ_EMPTY(wanted_refs)) {
1296 struct got_pathlist_entry *pe;
1297 TAILQ_FOREACH(pe, wanted_refs, entry) {
1299 const char *refname = pe->path;
1300 if (strncmp(refname, "refs/", 5) == 0)
1302 if (asprintf(&s, "%s\"%s\" ",
1303 refs ? refs : "", refname) == -1) {
1304 err = got_error_from_errno("asprintf");
1312 /* Create got.conf(5). */
1313 gotconfig_path = got_repo_get_path_gotconfig(repo);
1314 if (gotconfig_path == NULL) {
1315 err = got_error_from_errno("got_repo_get_path_gotconfig");
1318 gotconfig_file = fopen(gotconfig_path, "ae");
1319 if (gotconfig_file == NULL) {
1320 err = got_error_from_errno2("fopen", gotconfig_path);
1323 if (asprintf(&gotconfig,
1328 "\trepository \"%s\"\n"
1334 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1335 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1336 remote_repo_path, branches ? "\tbranch { " : "",
1337 branches ? branches : "", branches ? "}\n" : "",
1338 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1339 mirror_references ? "\tmirror_references yes\n" : "",
1340 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1341 err = got_error_from_errno("asprintf");
1344 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1345 if (n != strlen(gotconfig)) {
1346 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1351 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1352 err = got_error_from_errno2("fclose", gotconfig_path);
1353 free(gotconfig_path);
1358 static const struct got_error *
1359 create_gitconfig(const char *git_url, const char *default_branch,
1360 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1361 struct got_pathlist_head *wanted_refs, int mirror_references,
1362 struct got_repository *repo)
1364 const struct got_error *err = NULL;
1365 char *gitconfig_path = NULL;
1366 char *gitconfig = NULL;
1367 FILE *gitconfig_file = NULL;
1368 char *branches = NULL, *refs = NULL;
1369 const char *branchname;
1372 /* Create a config file Git can understand. */
1373 gitconfig_path = got_repo_get_path_gitconfig(repo);
1374 if (gitconfig_path == NULL) {
1375 err = got_error_from_errno("got_repo_get_path_gitconfig");
1378 gitconfig_file = fopen(gitconfig_path, "ae");
1379 if (gitconfig_file == NULL) {
1380 err = got_error_from_errno2("fopen", gitconfig_path);
1383 if (fetch_all_branches) {
1384 if (mirror_references) {
1385 if (asprintf(&branches,
1386 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1387 err = got_error_from_errno("asprintf");
1390 } else if (asprintf(&branches,
1391 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1392 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1393 err = got_error_from_errno("asprintf");
1396 } else if (!TAILQ_EMPTY(wanted_branches)) {
1397 struct got_pathlist_entry *pe;
1398 TAILQ_FOREACH(pe, wanted_branches, entry) {
1400 branchname = pe->path;
1401 if (strncmp(branchname, "refs/heads/", 11) == 0)
1403 if (mirror_references) {
1405 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1406 branches ? branches : "",
1407 branchname, branchname) == -1) {
1408 err = got_error_from_errno("asprintf");
1411 } else if (asprintf(&s,
1412 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1413 branches ? branches : "",
1414 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1415 branchname) == -1) {
1416 err = got_error_from_errno("asprintf");
1424 * If the server specified a default branch, use just that one.
1425 * Otherwise fall back to fetching all branches on next fetch.
1427 if (default_branch) {
1428 branchname = default_branch;
1429 if (strncmp(branchname, "refs/heads/", 11) == 0)
1432 branchname = "*"; /* fall back to all branches */
1433 if (mirror_references) {
1434 if (asprintf(&branches,
1435 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1436 branchname, branchname) == -1) {
1437 err = got_error_from_errno("asprintf");
1440 } else if (asprintf(&branches,
1441 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1442 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1443 branchname) == -1) {
1444 err = got_error_from_errno("asprintf");
1448 if (!TAILQ_EMPTY(wanted_refs)) {
1449 struct got_pathlist_entry *pe;
1450 TAILQ_FOREACH(pe, wanted_refs, entry) {
1452 const char *refname = pe->path;
1453 if (strncmp(refname, "refs/", 5) == 0)
1455 if (mirror_references) {
1457 "%s\tfetch = refs/%s:refs/%s\n",
1458 refs ? refs : "", refname, refname) == -1) {
1459 err = got_error_from_errno("asprintf");
1462 } else if (asprintf(&s,
1463 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1465 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1467 err = got_error_from_errno("asprintf");
1475 if (asprintf(&gitconfig,
1480 "\tfetch = refs/tags/*:refs/tags/*\n",
1481 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1482 refs ? refs : "") == -1) {
1483 err = got_error_from_errno("asprintf");
1486 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1487 if (n != strlen(gitconfig)) {
1488 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1492 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1493 err = got_error_from_errno2("fclose", gitconfig_path);
1494 free(gitconfig_path);
1499 static const struct got_error *
1500 create_config_files(const char *proto, const char *host, const char *port,
1501 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1502 int mirror_references, struct got_pathlist_head *symrefs,
1503 struct got_pathlist_head *wanted_branches,
1504 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1506 const struct got_error *err = NULL;
1507 const char *default_branch = NULL;
1508 struct got_pathlist_entry *pe;
1511 * If we asked for a set of wanted branches then use the first
1514 if (!TAILQ_EMPTY(wanted_branches)) {
1515 pe = TAILQ_FIRST(wanted_branches);
1516 default_branch = pe->path;
1518 /* First HEAD ref listed by server is the default branch. */
1519 TAILQ_FOREACH(pe, symrefs, entry) {
1520 const char *refname = pe->path;
1521 const char *target = pe->data;
1523 if (strcmp(refname, GOT_REF_HEAD) != 0)
1526 default_branch = target;
1531 /* Create got.conf(5). */
1532 err = create_gotconfig(proto, host, port, remote_repo_path,
1533 default_branch, fetch_all_branches, wanted_branches,
1534 wanted_refs, mirror_references, repo);
1538 /* Create a config file Git can understand. */
1539 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1540 wanted_branches, wanted_refs, mirror_references, repo);
1543 static const struct got_error *
1544 cmd_clone(int argc, char *argv[])
1546 const struct got_error *error = NULL;
1547 const char *uri, *dirname;
1548 char *proto, *host, *port, *repo_name, *server_path;
1549 char *default_destdir = NULL, *id_str = NULL;
1550 const char *repo_path;
1551 struct got_repository *repo = NULL;
1552 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1553 struct got_pathlist_entry *pe;
1554 struct got_object_id *pack_hash = NULL;
1555 int ch, fetchfd = -1, fetchstatus;
1556 pid_t fetchpid = -1;
1557 struct got_fetch_progress_arg fpa;
1558 char *git_url = NULL;
1559 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1560 int list_refs_only = 0;
1561 int *pack_fds = NULL;
1564 TAILQ_INIT(&symrefs);
1565 TAILQ_INIT(&wanted_branches);
1566 TAILQ_INIT(&wanted_refs);
1568 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1571 fetch_all_branches = 1;
1574 error = got_pathlist_append(&wanted_branches,
1583 mirror_references = 1;
1588 else if (verbosity < 3)
1595 error = got_pathlist_append(&wanted_refs,
1608 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1609 option_conflict('a', 'b');
1610 if (list_refs_only) {
1611 if (!TAILQ_EMPTY(&wanted_branches))
1612 option_conflict('l', 'b');
1613 if (fetch_all_branches)
1614 option_conflict('l', 'a');
1615 if (mirror_references)
1616 option_conflict('l', 'm');
1617 if (!TAILQ_EMPTY(&wanted_refs))
1618 option_conflict('l', 'R');
1630 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1635 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1636 host, port ? ":" : "", port ? port : "",
1637 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1638 error = got_error_from_errno("asprintf");
1642 if (strcmp(proto, "git") == 0) {
1644 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1645 "sendfd dns inet unveil", NULL) == -1)
1648 } else if (strcmp(proto, "git+ssh") == 0 ||
1649 strcmp(proto, "ssh") == 0) {
1651 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1652 "sendfd unveil", NULL) == -1)
1655 } else if (strcmp(proto, "http") == 0 ||
1656 strcmp(proto, "git+http") == 0) {
1657 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1660 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1663 if (dirname == NULL) {
1664 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1665 error = got_error_from_errno("asprintf");
1668 repo_path = default_destdir;
1670 repo_path = dirname;
1672 if (!list_refs_only) {
1673 error = got_path_mkdir(repo_path);
1675 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1676 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1678 if (!got_path_dir_is_empty(repo_path)) {
1679 error = got_error_path(repo_path,
1680 GOT_ERR_DIR_NOT_EMPTY);
1685 error = got_dial_apply_unveil(proto);
1689 error = apply_unveil(repo_path, 0, NULL);
1694 printf("Connecting to %s%s%s\n", host,
1695 port ? ":" : "", port ? port : "");
1697 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1698 server_path, verbosity);
1702 if (!list_refs_only) {
1703 error = got_repo_init(repo_path, NULL);
1706 error = got_repo_pack_fds_open(&pack_fds);
1709 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1714 fpa.last_scaled_size[0] = '\0';
1715 fpa.last_p_indexed = -1;
1716 fpa.last_p_resolved = -1;
1717 fpa.verbosity = verbosity;
1718 fpa.create_configs = 1;
1719 fpa.configs_created = 0;
1721 fpa.config_info.symrefs = &symrefs;
1722 fpa.config_info.wanted_branches = &wanted_branches;
1723 fpa.config_info.wanted_refs = &wanted_refs;
1724 fpa.config_info.proto = proto;
1725 fpa.config_info.host = host;
1726 fpa.config_info.port = port;
1727 fpa.config_info.remote_repo_path = server_path;
1728 fpa.config_info.git_url = git_url;
1729 fpa.config_info.fetch_all_branches = fetch_all_branches;
1730 fpa.config_info.mirror_references = mirror_references;
1731 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1732 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1733 fetch_all_branches, &wanted_branches, &wanted_refs,
1734 list_refs_only, verbosity, fetchfd, repo,
1735 fetch_progress, &fpa);
1739 if (list_refs_only) {
1740 error = list_remote_refs(&symrefs, &refs);
1744 if (pack_hash == NULL) {
1745 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1746 "server sent an empty pack file");
1749 error = got_object_id_str(&id_str, pack_hash);
1753 printf("\nFetched %s.pack\n", id_str);
1756 /* Set up references provided with the pack file. */
1757 TAILQ_FOREACH(pe, &refs, entry) {
1758 const char *refname = pe->path;
1759 struct got_object_id *id = pe->data;
1760 char *remote_refname;
1762 if (is_wanted_ref(&wanted_refs, refname) &&
1763 !mirror_references) {
1764 error = create_wanted_ref(refname, id,
1765 GOT_FETCH_DEFAULT_REMOTE_NAME,
1766 verbosity - 1, repo);
1772 error = create_ref(refname, id, verbosity - 1, repo);
1776 if (mirror_references)
1779 if (strncmp("refs/heads/", refname, 11) != 0)
1782 if (asprintf(&remote_refname,
1783 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1784 refname + 11) == -1) {
1785 error = got_error_from_errno("asprintf");
1788 error = create_ref(remote_refname, id, verbosity - 1, repo);
1789 free(remote_refname);
1794 /* Set the HEAD reference if the server provided one. */
1795 TAILQ_FOREACH(pe, &symrefs, entry) {
1796 struct got_reference *target_ref;
1797 const char *refname = pe->path;
1798 const char *target = pe->data;
1799 char *remote_refname = NULL, *remote_target = NULL;
1801 if (strcmp(refname, GOT_REF_HEAD) != 0)
1804 error = got_ref_open(&target_ref, repo, target, 0);
1806 if (error->code == GOT_ERR_NOT_REF) {
1813 error = create_symref(refname, target_ref, verbosity, repo);
1814 got_ref_close(target_ref);
1818 if (mirror_references)
1821 if (strncmp("refs/heads/", target, 11) != 0)
1824 if (asprintf(&remote_refname,
1825 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1827 error = got_error_from_errno("asprintf");
1830 if (asprintf(&remote_target,
1831 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1832 target + 11) == -1) {
1833 error = got_error_from_errno("asprintf");
1834 free(remote_refname);
1837 error = got_ref_open(&target_ref, repo, remote_target, 0);
1839 free(remote_refname);
1840 free(remote_target);
1841 if (error->code == GOT_ERR_NOT_REF) {
1847 error = create_symref(remote_refname, target_ref,
1848 verbosity - 1, repo);
1849 free(remote_refname);
1850 free(remote_target);
1851 got_ref_close(target_ref);
1857 * We failed to set the HEAD reference. If we asked for
1858 * a set of wanted branches use the first of one of those
1859 * which could be fetched instead.
1861 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1862 const char *target = pe->path;
1863 struct got_reference *target_ref;
1865 error = got_ref_open(&target_ref, repo, target, 0);
1867 if (error->code == GOT_ERR_NOT_REF) {
1874 error = create_symref(GOT_REF_HEAD, target_ref,
1876 got_ref_close(target_ref);
1884 printf("Created %s repository '%s'\n",
1885 mirror_references ? "mirrored" : "cloned", repo_path);
1888 const struct got_error *pack_err =
1889 got_repo_pack_fds_close(pack_fds);
1894 if (kill(fetchpid, SIGTERM) == -1)
1895 error = got_error_from_errno("kill");
1896 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1897 error = got_error_from_errno("waitpid");
1899 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1900 error = got_error_from_errno("close");
1902 const struct got_error *close_err = got_repo_close(repo);
1906 TAILQ_FOREACH(pe, &refs, entry) {
1907 free((void *)pe->path);
1910 got_pathlist_free(&refs);
1911 TAILQ_FOREACH(pe, &symrefs, entry) {
1912 free((void *)pe->path);
1915 got_pathlist_free(&symrefs);
1916 got_pathlist_free(&wanted_branches);
1917 got_pathlist_free(&wanted_refs);
1924 free(default_destdir);
1929 static const struct got_error *
1930 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1931 int replace_tags, int verbosity, struct got_repository *repo)
1933 const struct got_error *err = NULL;
1934 char *new_id_str = NULL;
1935 struct got_object_id *old_id = NULL;
1937 err = got_object_id_str(&new_id_str, new_id);
1941 if (!replace_tags &&
1942 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1943 err = got_ref_resolve(&old_id, repo, ref);
1946 if (got_object_id_cmp(old_id, new_id) == 0)
1948 if (verbosity >= 0) {
1949 printf("Rejecting update of existing tag %s: %s\n",
1950 got_ref_get_name(ref), new_id_str);
1955 if (got_ref_is_symbolic(ref)) {
1956 if (verbosity >= 0) {
1957 printf("Replacing reference %s: %s\n",
1958 got_ref_get_name(ref),
1959 got_ref_get_symref_target(ref));
1961 err = got_ref_change_symref_to_ref(ref, new_id);
1964 err = got_ref_write(ref, repo);
1968 err = got_ref_resolve(&old_id, repo, ref);
1971 if (got_object_id_cmp(old_id, new_id) == 0)
1974 err = got_ref_change_ref(ref, new_id);
1977 err = got_ref_write(ref, repo);
1983 printf("Updated %s: %s\n", got_ref_get_name(ref),
1991 static const struct got_error *
1992 update_symref(const char *refname, struct got_reference *target_ref,
1993 int verbosity, struct got_repository *repo)
1995 const struct got_error *err = NULL, *unlock_err;
1996 struct got_reference *symref;
1997 int symref_is_locked = 0;
1999 err = got_ref_open(&symref, repo, refname, 1);
2001 if (err->code != GOT_ERR_NOT_REF)
2003 err = got_ref_alloc_symref(&symref, refname, target_ref);
2007 err = got_ref_write(symref, repo);
2012 printf("Created reference %s: %s\n",
2013 got_ref_get_name(symref),
2014 got_ref_get_symref_target(symref));
2016 symref_is_locked = 1;
2018 if (strcmp(got_ref_get_symref_target(symref),
2019 got_ref_get_name(target_ref)) == 0)
2022 err = got_ref_change_symref(symref,
2023 got_ref_get_name(target_ref));
2027 err = got_ref_write(symref, repo);
2032 printf("Updated %s: %s\n", got_ref_get_name(symref),
2033 got_ref_get_symref_target(symref));
2037 if (symref_is_locked) {
2038 unlock_err = got_ref_unlock(symref);
2039 if (unlock_err && err == NULL)
2042 got_ref_close(symref);
2049 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2050 "[-R reference] [-r repository-path] [remote-repository]\n",
2055 static const struct got_error *
2056 delete_missing_ref(struct got_reference *ref,
2057 int verbosity, struct got_repository *repo)
2059 const struct got_error *err = NULL;
2060 struct got_object_id *id = NULL;
2061 char *id_str = NULL;
2063 if (got_ref_is_symbolic(ref)) {
2064 err = got_ref_delete(ref, repo);
2067 if (verbosity >= 0) {
2068 printf("Deleted %s: %s\n",
2069 got_ref_get_name(ref),
2070 got_ref_get_symref_target(ref));
2073 err = got_ref_resolve(&id, repo, ref);
2076 err = got_object_id_str(&id_str, id);
2080 err = got_ref_delete(ref, repo);
2083 if (verbosity >= 0) {
2084 printf("Deleted %s: %s\n",
2085 got_ref_get_name(ref), id_str);
2094 static const struct got_error *
2095 delete_missing_refs(struct got_pathlist_head *their_refs,
2096 struct got_pathlist_head *their_symrefs,
2097 const struct got_remote_repo *remote,
2098 int verbosity, struct got_repository *repo)
2100 const struct got_error *err = NULL, *unlock_err;
2101 struct got_reflist_head my_refs;
2102 struct got_reflist_entry *re;
2103 struct got_pathlist_entry *pe;
2104 char *remote_namespace = NULL;
2105 char *local_refname = NULL;
2107 TAILQ_INIT(&my_refs);
2109 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2111 return got_error_from_errno("asprintf");
2113 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2117 TAILQ_FOREACH(re, &my_refs, entry) {
2118 const char *refname = got_ref_get_name(re->ref);
2119 const char *their_refname;
2121 if (remote->mirror_references) {
2122 their_refname = refname;
2124 if (strncmp(refname, remote_namespace,
2125 strlen(remote_namespace)) == 0) {
2126 if (strcmp(refname + strlen(remote_namespace),
2129 if (asprintf(&local_refname, "refs/heads/%s",
2130 refname + strlen(remote_namespace)) == -1) {
2131 err = got_error_from_errno("asprintf");
2134 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2137 their_refname = local_refname;
2140 TAILQ_FOREACH(pe, their_refs, entry) {
2141 if (strcmp(their_refname, pe->path) == 0)
2147 TAILQ_FOREACH(pe, their_symrefs, entry) {
2148 if (strcmp(their_refname, pe->path) == 0)
2154 err = delete_missing_ref(re->ref, verbosity, repo);
2158 if (local_refname) {
2159 struct got_reference *ref;
2160 err = got_ref_open(&ref, repo, local_refname, 1);
2162 if (err->code != GOT_ERR_NOT_REF)
2164 free(local_refname);
2165 local_refname = NULL;
2168 err = delete_missing_ref(ref, verbosity, repo);
2171 unlock_err = got_ref_unlock(ref);
2173 if (unlock_err && err == NULL) {
2178 free(local_refname);
2179 local_refname = NULL;
2183 free(remote_namespace);
2184 free(local_refname);
2188 static const struct got_error *
2189 update_wanted_ref(const char *refname, struct got_object_id *id,
2190 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2192 const struct got_error *err, *unlock_err;
2193 char *remote_refname;
2194 struct got_reference *ref;
2196 if (strncmp("refs/", refname, 5) == 0)
2199 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2200 remote_repo_name, refname) == -1)
2201 return got_error_from_errno("asprintf");
2203 err = got_ref_open(&ref, repo, remote_refname, 1);
2205 if (err->code != GOT_ERR_NOT_REF)
2207 err = create_ref(remote_refname, id, verbosity, repo);
2209 err = update_ref(ref, id, 0, verbosity, repo);
2210 unlock_err = got_ref_unlock(ref);
2211 if (unlock_err && err == NULL)
2216 free(remote_refname);
2220 static const struct got_error *
2221 delete_ref(struct got_repository *repo, struct got_reference *ref)
2223 const struct got_error *err = NULL;
2224 struct got_object_id *id = NULL;
2225 char *id_str = NULL;
2228 if (got_ref_is_symbolic(ref)) {
2229 target = got_ref_get_symref_target(ref);
2231 err = got_ref_resolve(&id, repo, ref);
2234 err = got_object_id_str(&id_str, id);
2240 err = got_ref_delete(ref, repo);
2244 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2251 static const struct got_error *
2252 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2254 const struct got_error *err = NULL;
2255 struct got_reflist_head refs;
2256 struct got_reflist_entry *re;
2261 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2262 err = got_error_from_errno("asprintf");
2265 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2269 TAILQ_FOREACH(re, &refs, entry)
2270 delete_ref(repo, re->ref);
2272 got_ref_list_free(&refs);
2276 static const struct got_error *
2277 cmd_fetch(int argc, char *argv[])
2279 const struct got_error *error = NULL, *unlock_err;
2280 char *cwd = NULL, *repo_path = NULL;
2281 const char *remote_name;
2282 char *proto = NULL, *host = NULL, *port = NULL;
2283 char *repo_name = NULL, *server_path = NULL;
2284 const struct got_remote_repo *remotes, *remote = NULL;
2286 char *id_str = NULL;
2287 struct got_repository *repo = NULL;
2288 struct got_worktree *worktree = NULL;
2289 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2290 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2291 struct got_pathlist_entry *pe;
2292 struct got_object_id *pack_hash = NULL;
2293 int i, ch, fetchfd = -1, fetchstatus;
2294 pid_t fetchpid = -1;
2295 struct got_fetch_progress_arg fpa;
2296 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2297 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2298 int *pack_fds = NULL;
2301 TAILQ_INIT(&symrefs);
2302 TAILQ_INIT(&wanted_branches);
2303 TAILQ_INIT(&wanted_refs);
2305 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2308 fetch_all_branches = 1;
2311 error = got_pathlist_append(&wanted_branches,
2323 repo_path = realpath(optarg, NULL);
2324 if (repo_path == NULL)
2325 return got_error_from_errno2("realpath",
2327 got_path_strip_trailing_slashes(repo_path);
2335 else if (verbosity < 3)
2342 error = got_pathlist_append(&wanted_refs,
2358 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2359 option_conflict('a', 'b');
2360 if (list_refs_only) {
2361 if (!TAILQ_EMPTY(&wanted_branches))
2362 option_conflict('l', 'b');
2363 if (fetch_all_branches)
2364 option_conflict('l', 'a');
2366 option_conflict('l', 'd');
2368 option_conflict('l', 'X');
2370 if (delete_remote) {
2371 if (fetch_all_branches)
2372 option_conflict('X', 'a');
2373 if (!TAILQ_EMPTY(&wanted_branches))
2374 option_conflict('X', 'b');
2376 option_conflict('X', 'd');
2378 option_conflict('X', 't');
2379 if (!TAILQ_EMPTY(&wanted_refs))
2380 option_conflict('X', 'R');
2385 errx(1, "-X option requires a remote name");
2386 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2387 } else if (argc == 1)
2388 remote_name = argv[0];
2392 cwd = getcwd(NULL, 0);
2394 error = got_error_from_errno("getcwd");
2398 error = got_repo_pack_fds_open(&pack_fds);
2402 if (repo_path == NULL) {
2403 error = got_worktree_open(&worktree, cwd);
2404 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2410 strdup(got_worktree_get_repo_path(worktree));
2411 if (repo_path == NULL)
2412 error = got_error_from_errno("strdup");
2416 repo_path = strdup(cwd);
2417 if (repo_path == NULL) {
2418 error = got_error_from_errno("strdup");
2424 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2428 if (delete_remote) {
2429 error = delete_refs_for_remote(repo, remote_name);
2430 goto done; /* nothing else to do */
2434 worktree_conf = got_worktree_get_gotconfig(worktree);
2435 if (worktree_conf) {
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 repo_conf = got_repo_get_gotconfig(repo);
2449 got_gotconfig_get_remotes(&nremotes, &remotes,
2451 for (i = 0; i < nremotes; i++) {
2452 if (strcmp(remotes[i].name, remote_name) == 0) {
2453 remote = &remotes[i];
2459 if (remote == NULL) {
2460 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2461 for (i = 0; i < nremotes; i++) {
2462 if (strcmp(remotes[i].name, remote_name) == 0) {
2463 remote = &remotes[i];
2468 if (remote == NULL) {
2469 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2473 if (TAILQ_EMPTY(&wanted_branches)) {
2474 if (!fetch_all_branches)
2475 fetch_all_branches = remote->fetch_all_branches;
2476 for (i = 0; i < remote->nfetch_branches; i++) {
2477 got_pathlist_append(&wanted_branches,
2478 remote->fetch_branches[i], NULL);
2481 if (TAILQ_EMPTY(&wanted_refs)) {
2482 for (i = 0; i < remote->nfetch_refs; i++) {
2483 got_pathlist_append(&wanted_refs,
2484 remote->fetch_refs[i], NULL);
2488 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2489 &repo_name, remote->fetch_url);
2493 if (strcmp(proto, "git") == 0) {
2495 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2496 "sendfd dns inet unveil", NULL) == -1)
2499 } else if (strcmp(proto, "git+ssh") == 0 ||
2500 strcmp(proto, "ssh") == 0) {
2502 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2503 "sendfd unveil", NULL) == -1)
2506 } else if (strcmp(proto, "http") == 0 ||
2507 strcmp(proto, "git+http") == 0) {
2508 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2511 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2515 error = got_dial_apply_unveil(proto);
2519 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2524 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2525 port ? ":" : "", port ? port : "");
2527 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2528 server_path, verbosity);
2532 fpa.last_scaled_size[0] = '\0';
2533 fpa.last_p_indexed = -1;
2534 fpa.last_p_resolved = -1;
2535 fpa.verbosity = verbosity;
2537 fpa.create_configs = 0;
2538 fpa.configs_created = 0;
2539 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2540 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2541 remote->mirror_references, fetch_all_branches, &wanted_branches,
2542 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2543 fetch_progress, &fpa);
2547 if (list_refs_only) {
2548 error = list_remote_refs(&symrefs, &refs);
2552 if (pack_hash == NULL) {
2554 printf("Already up-to-date\n");
2555 } else if (verbosity >= 0) {
2556 error = got_object_id_str(&id_str, pack_hash);
2559 printf("\nFetched %s.pack\n", id_str);
2564 /* Update references provided with the pack file. */
2565 TAILQ_FOREACH(pe, &refs, entry) {
2566 const char *refname = pe->path;
2567 struct got_object_id *id = pe->data;
2568 struct got_reference *ref;
2569 char *remote_refname;
2571 if (is_wanted_ref(&wanted_refs, refname) &&
2572 !remote->mirror_references) {
2573 error = update_wanted_ref(refname, id,
2574 remote->name, verbosity, repo);
2580 if (remote->mirror_references ||
2581 strncmp("refs/tags/", refname, 10) == 0) {
2582 error = got_ref_open(&ref, repo, refname, 1);
2584 if (error->code != GOT_ERR_NOT_REF)
2586 error = create_ref(refname, id, verbosity,
2591 error = update_ref(ref, id, replace_tags,
2593 unlock_err = got_ref_unlock(ref);
2594 if (unlock_err && error == NULL)
2600 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2601 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2602 remote_name, refname + 11) == -1) {
2603 error = got_error_from_errno("asprintf");
2607 error = got_ref_open(&ref, repo, remote_refname, 1);
2609 if (error->code != GOT_ERR_NOT_REF)
2611 error = create_ref(remote_refname, id,
2616 error = update_ref(ref, id, replace_tags,
2618 unlock_err = got_ref_unlock(ref);
2619 if (unlock_err && error == NULL)
2626 /* Also create a local branch if none exists yet. */
2627 error = got_ref_open(&ref, repo, refname, 1);
2629 if (error->code != GOT_ERR_NOT_REF)
2631 error = create_ref(refname, id, verbosity,
2636 unlock_err = got_ref_unlock(ref);
2637 if (unlock_err && error == NULL)
2644 error = delete_missing_refs(&refs, &symrefs, remote,
2650 if (!remote->mirror_references) {
2651 /* Update remote HEAD reference if the server provided one. */
2652 TAILQ_FOREACH(pe, &symrefs, entry) {
2653 struct got_reference *target_ref;
2654 const char *refname = pe->path;
2655 const char *target = pe->data;
2656 char *remote_refname = NULL, *remote_target = NULL;
2658 if (strcmp(refname, GOT_REF_HEAD) != 0)
2661 if (strncmp("refs/heads/", target, 11) != 0)
2664 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2665 remote->name, refname) == -1) {
2666 error = got_error_from_errno("asprintf");
2669 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2670 remote->name, target + 11) == -1) {
2671 error = got_error_from_errno("asprintf");
2672 free(remote_refname);
2676 error = got_ref_open(&target_ref, repo, remote_target,
2679 free(remote_refname);
2680 free(remote_target);
2681 if (error->code == GOT_ERR_NOT_REF) {
2687 error = update_symref(remote_refname, target_ref,
2689 free(remote_refname);
2690 free(remote_target);
2691 got_ref_close(target_ref);
2698 if (kill(fetchpid, SIGTERM) == -1)
2699 error = got_error_from_errno("kill");
2700 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2701 error = got_error_from_errno("waitpid");
2703 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2704 error = got_error_from_errno("close");
2706 const struct got_error *close_err = got_repo_close(repo);
2711 got_worktree_close(worktree);
2713 const struct got_error *pack_err =
2714 got_repo_pack_fds_close(pack_fds);
2718 TAILQ_FOREACH(pe, &refs, entry) {
2719 free((void *)pe->path);
2722 got_pathlist_free(&refs);
2723 TAILQ_FOREACH(pe, &symrefs, entry) {
2724 free((void *)pe->path);
2727 got_pathlist_free(&symrefs);
2728 got_pathlist_free(&wanted_branches);
2729 got_pathlist_free(&wanted_refs);
2744 usage_checkout(void)
2746 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2747 "[-p path-prefix] repository-path [work-tree-path]\n",
2753 show_worktree_base_ref_warning(void)
2755 fprintf(stderr, "%s: warning: could not create a reference "
2756 "to the work tree's base commit; the commit could be "
2757 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2758 "repository writable and running 'got update' will prevent this\n",
2762 struct got_checkout_progress_arg {
2763 const char *worktree_path;
2764 int had_base_commit_ref_error;
2768 static const struct got_error *
2769 checkout_progress(void *arg, unsigned char status, const char *path)
2771 struct got_checkout_progress_arg *a = arg;
2773 /* Base commit bump happens silently. */
2774 if (status == GOT_STATUS_BUMP_BASE)
2777 if (status == GOT_STATUS_BASE_REF_ERR) {
2778 a->had_base_commit_ref_error = 1;
2782 while (path[0] == '/')
2785 if (a->verbosity >= 0)
2786 printf("%c %s/%s\n", status, a->worktree_path, path);
2791 static const struct got_error *
2792 check_cancelled(void *arg)
2794 if (sigint_received || sigpipe_received)
2795 return got_error(GOT_ERR_CANCELLED);
2799 static const struct got_error *
2800 check_linear_ancestry(struct got_object_id *commit_id,
2801 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2802 struct got_repository *repo)
2804 const struct got_error *err = NULL;
2805 struct got_object_id *yca_id;
2807 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2808 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2813 return got_error(GOT_ERR_ANCESTRY);
2816 * Require a straight line of history between the target commit
2817 * and the work tree's base commit.
2819 * Non-linear situations such as this require a rebase:
2821 * (commit) D F (base_commit)
2829 * 'got update' only handles linear cases:
2830 * Update forwards in time: A (base/yca) - B - C - D (commit)
2831 * Update backwards in time: D (base) - C - B - A (commit/yca)
2833 if (allow_forwards_in_time_only) {
2834 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2835 return got_error(GOT_ERR_ANCESTRY);
2836 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2837 got_object_id_cmp(base_commit_id, yca_id) != 0)
2838 return got_error(GOT_ERR_ANCESTRY);
2844 static const struct got_error *
2845 check_same_branch(struct got_object_id *commit_id,
2846 struct got_reference *head_ref, struct got_object_id *yca_id,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_commit_graph *graph = NULL;
2851 struct got_object_id *head_commit_id = NULL;
2852 int is_same_branch = 0;
2854 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2858 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2862 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2867 err = got_commit_graph_open(&graph, "/", 1);
2871 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2872 check_cancelled, NULL);
2877 struct got_object_id id;
2879 err = got_commit_graph_iter_next(&id, graph, repo,
2880 check_cancelled, NULL);
2882 if (err->code == GOT_ERR_ITER_COMPLETED)
2887 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2889 if (got_object_id_cmp(&id, commit_id) == 0) {
2896 got_commit_graph_close(graph);
2897 free(head_commit_id);
2898 if (!err && !is_same_branch)
2899 err = got_error(GOT_ERR_ANCESTRY);
2903 static const struct got_error *
2904 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2906 static char msg[512];
2907 const char *branch_name;
2909 if (got_ref_is_symbolic(ref))
2910 branch_name = got_ref_get_symref_target(ref);
2912 branch_name = got_ref_get_name(ref);
2914 if (strncmp("refs/heads/", branch_name, 11) == 0)
2917 snprintf(msg, sizeof(msg),
2918 "target commit is not contained in branch '%s'; "
2919 "the branch to use must be specified with -b; "
2920 "if necessary a new branch can be created for "
2921 "this commit with 'got branch -c %s BRANCH_NAME'",
2922 branch_name, commit_id_str);
2924 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2927 static const struct got_error *
2928 cmd_checkout(int argc, char *argv[])
2930 const struct got_error *error = NULL;
2931 struct got_repository *repo = NULL;
2932 struct got_reference *head_ref = NULL, *ref = NULL;
2933 struct got_worktree *worktree = NULL;
2934 char *repo_path = NULL;
2935 char *worktree_path = NULL;
2936 const char *path_prefix = "";
2937 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2938 char *commit_id_str = NULL;
2939 struct got_object_id *commit_id = NULL;
2941 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2942 struct got_pathlist_head paths;
2943 struct got_checkout_progress_arg cpa;
2944 int *pack_fds = NULL;
2948 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2951 branch_name = optarg;
2954 commit_id_str = strdup(optarg);
2955 if (commit_id_str == NULL)
2956 return got_error_from_errno("strdup");
2962 path_prefix = optarg;
2977 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2978 "unveil", NULL) == -1)
2982 char *base, *dotgit;
2984 repo_path = realpath(argv[0], NULL);
2985 if (repo_path == NULL)
2986 return got_error_from_errno2("realpath", argv[0]);
2987 cwd = getcwd(NULL, 0);
2989 error = got_error_from_errno("getcwd");
2996 error = got_path_basename(&base, path);
2999 dotgit = strstr(base, ".git");
3002 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3003 error = got_error_from_errno("asprintf");
3008 } else if (argc == 2) {
3009 repo_path = realpath(argv[0], NULL);
3010 if (repo_path == NULL) {
3011 error = got_error_from_errno2("realpath", argv[0]);
3014 worktree_path = realpath(argv[1], NULL);
3015 if (worktree_path == NULL) {
3016 if (errno != ENOENT) {
3017 error = got_error_from_errno2("realpath",
3021 worktree_path = strdup(argv[1]);
3022 if (worktree_path == NULL) {
3023 error = got_error_from_errno("strdup");
3030 got_path_strip_trailing_slashes(repo_path);
3031 got_path_strip_trailing_slashes(worktree_path);
3033 error = got_repo_pack_fds_open(&pack_fds);
3037 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3041 /* Pre-create work tree path for unveil(2) */
3042 error = got_path_mkdir(worktree_path);
3044 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3045 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3047 if (!allow_nonempty &&
3048 !got_path_dir_is_empty(worktree_path)) {
3049 error = got_error_path(worktree_path,
3050 GOT_ERR_DIR_NOT_EMPTY);
3055 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3059 error = got_ref_open(&head_ref, repo, branch_name, 0);
3063 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3064 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3067 error = got_worktree_open(&worktree, worktree_path);
3071 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3075 if (!same_path_prefix) {
3076 error = got_error(GOT_ERR_PATH_PREFIX);
3080 if (commit_id_str) {
3081 struct got_reflist_head refs;
3083 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3087 error = got_repo_match_object_id(&commit_id, NULL,
3088 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3089 got_ref_list_free(&refs);
3092 error = check_linear_ancestry(commit_id,
3093 got_worktree_get_base_commit_id(worktree), 0, repo);
3094 if (error != NULL) {
3095 if (error->code == GOT_ERR_ANCESTRY) {
3096 error = checkout_ancestry_error(
3097 head_ref, commit_id_str);
3101 error = check_same_branch(commit_id, head_ref, NULL, repo);
3103 if (error->code == GOT_ERR_ANCESTRY) {
3104 error = checkout_ancestry_error(
3105 head_ref, commit_id_str);
3109 error = got_worktree_set_base_commit_id(worktree, repo,
3113 /* Expand potentially abbreviated commit ID string. */
3114 free(commit_id_str);
3115 error = got_object_id_str(&commit_id_str, commit_id);
3119 commit_id = got_object_id_dup(
3120 got_worktree_get_base_commit_id(worktree));
3121 if (commit_id == NULL) {
3122 error = got_error_from_errno("got_object_id_dup");
3125 error = got_object_id_str(&commit_id_str, commit_id);
3130 error = got_pathlist_append(&paths, "", NULL);
3133 cpa.worktree_path = worktree_path;
3134 cpa.had_base_commit_ref_error = 0;
3135 cpa.verbosity = verbosity;
3136 error = got_worktree_checkout_files(worktree, &paths, repo,
3137 checkout_progress, &cpa, check_cancelled, NULL);
3141 if (got_ref_is_symbolic(head_ref)) {
3142 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3145 refname = got_ref_get_name(ref);
3147 refname = got_ref_get_name(head_ref);
3148 printf("Checked out %s: %s\n", refname, commit_id_str);
3149 printf("Now shut up and hack\n");
3150 if (cpa.had_base_commit_ref_error)
3151 show_worktree_base_ref_warning();
3154 const struct got_error *pack_err =
3155 got_repo_pack_fds_close(pack_fds);
3160 got_ref_close(head_ref);
3163 got_pathlist_free(&paths);
3164 free(commit_id_str);
3167 free(worktree_path);
3172 struct got_update_progress_arg {
3184 print_update_progress_stats(struct got_update_progress_arg *upa)
3186 if (!upa->did_something)
3189 if (upa->conflicts > 0)
3190 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3191 if (upa->obstructed > 0)
3192 printf("File paths obstructed by a non-regular file: %d\n",
3194 if (upa->not_updated > 0)
3195 printf("Files not updated because of existing merge "
3196 "conflicts: %d\n", upa->not_updated);
3200 * The meaning of some status codes differs between merge-style operations and
3201 * update operations. For example, the ! status code means "file was missing"
3202 * if changes were merged into the work tree, and "missing file was restored"
3203 * if the work tree was updated. This function should be used by any operation
3204 * which merges changes into the work tree without updating the work tree.
3207 print_merge_progress_stats(struct got_update_progress_arg *upa)
3209 if (!upa->did_something)
3212 if (upa->conflicts > 0)
3213 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3214 if (upa->obstructed > 0)
3215 printf("File paths obstructed by a non-regular file: %d\n",
3217 if (upa->missing > 0)
3218 printf("Files which had incoming changes but could not be "
3219 "found in the work tree: %d\n", upa->missing);
3220 if (upa->not_deleted > 0)
3221 printf("Files not deleted due to differences in deleted "
3222 "content: %d\n", upa->not_deleted);
3223 if (upa->unversioned > 0)
3224 printf("Files not merged because an unversioned file was "
3225 "found in the work tree: %d\n", upa->unversioned);
3231 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3232 "[path ...]\n", getprogname());
3236 static const struct got_error *
3237 update_progress(void *arg, unsigned char status, const char *path)
3239 struct got_update_progress_arg *upa = arg;
3241 if (status == GOT_STATUS_EXISTS ||
3242 status == GOT_STATUS_BASE_REF_ERR)
3245 upa->did_something = 1;
3247 /* Base commit bump happens silently. */
3248 if (status == GOT_STATUS_BUMP_BASE)
3251 if (status == GOT_STATUS_CONFLICT)
3253 if (status == GOT_STATUS_OBSTRUCTED)
3255 if (status == GOT_STATUS_CANNOT_UPDATE)
3257 if (status == GOT_STATUS_MISSING)
3259 if (status == GOT_STATUS_CANNOT_DELETE)
3261 if (status == GOT_STATUS_UNVERSIONED)
3264 while (path[0] == '/')
3266 if (upa->verbosity >= 0)
3267 printf("%c %s\n", status, path);
3272 static const struct got_error *
3273 switch_head_ref(struct got_reference *head_ref,
3274 struct got_object_id *commit_id, struct got_worktree *worktree,
3275 struct got_repository *repo)
3277 const struct got_error *err = NULL;
3279 int ref_has_moved = 0;
3281 /* Trivial case: switching between two different references. */
3282 if (strcmp(got_ref_get_name(head_ref),
3283 got_worktree_get_head_ref_name(worktree)) != 0) {
3284 printf("Switching work tree from %s to %s\n",
3285 got_worktree_get_head_ref_name(worktree),
3286 got_ref_get_name(head_ref));
3287 return got_worktree_set_head_ref(worktree, head_ref);
3290 err = check_linear_ancestry(commit_id,
3291 got_worktree_get_base_commit_id(worktree), 0, repo);
3293 if (err->code != GOT_ERR_ANCESTRY)
3300 /* Switching to a rebased branch with the same reference name. */
3301 err = got_object_id_str(&base_id_str,
3302 got_worktree_get_base_commit_id(worktree));
3305 printf("Reference %s now points at a different branch\n",
3306 got_worktree_get_head_ref_name(worktree));
3307 printf("Switching work tree from %s to %s\n", base_id_str,
3308 got_worktree_get_head_ref_name(worktree));
3312 static const struct got_error *
3313 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3315 const struct got_error *err;
3318 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3322 return got_error(GOT_ERR_REBASING);
3324 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3328 return got_error(GOT_ERR_HISTEDIT_BUSY);
3333 static const struct got_error *
3334 check_merge_in_progress(struct got_worktree *worktree,
3335 struct got_repository *repo)
3337 const struct got_error *err;
3340 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3344 return got_error(GOT_ERR_MERGE_BUSY);
3349 static const struct got_error *
3350 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3351 char *argv[], struct got_worktree *worktree)
3353 const struct got_error *err = NULL;
3355 struct got_pathlist_entry *new;
3361 return got_error_from_errno("strdup");
3362 return got_pathlist_append(paths, path, NULL);
3365 for (i = 0; i < argc; i++) {
3366 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3369 err = got_pathlist_insert(&new, paths, path, NULL);
3370 if (err || new == NULL /* duplicate */) {
3380 static const struct got_error *
3381 wrap_not_worktree_error(const struct got_error *orig_err,
3382 const char *cmdname, const char *path)
3384 const struct got_error *err;
3385 struct got_repository *repo;
3386 static char msg[512];
3387 int *pack_fds = NULL;
3389 err = got_repo_pack_fds_open(&pack_fds);
3393 err = got_repo_open(&repo, path, NULL, pack_fds);
3397 snprintf(msg, sizeof(msg),
3398 "'got %s' needs a work tree in addition to a git repository\n"
3399 "Work trees can be checked out from this Git repository with "
3401 "The got(1) manual page contains more information.", cmdname);
3402 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3403 got_repo_close(repo);
3405 const struct got_error *pack_err =
3406 got_repo_pack_fds_close(pack_fds);
3413 static const struct got_error *
3414 cmd_update(int argc, char *argv[])
3416 const struct got_error *error = NULL;
3417 struct got_repository *repo = NULL;
3418 struct got_worktree *worktree = NULL;
3419 char *worktree_path = NULL;
3420 struct got_object_id *commit_id = NULL;
3421 char *commit_id_str = NULL;
3422 const char *branch_name = NULL;
3423 struct got_reference *head_ref = NULL;
3424 struct got_pathlist_head paths;
3425 struct got_pathlist_entry *pe;
3426 int ch, verbosity = 0;
3427 struct got_update_progress_arg upa;
3428 int *pack_fds = NULL;
3432 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3435 branch_name = optarg;
3438 commit_id_str = strdup(optarg);
3439 if (commit_id_str == NULL)
3440 return got_error_from_errno("strdup");
3455 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3456 "unveil", NULL) == -1)
3459 worktree_path = getcwd(NULL, 0);
3460 if (worktree_path == NULL) {
3461 error = got_error_from_errno("getcwd");
3465 error = got_repo_pack_fds_open(&pack_fds);
3469 error = got_worktree_open(&worktree, worktree_path);
3471 if (error->code == GOT_ERR_NOT_WORKTREE)
3472 error = wrap_not_worktree_error(error, "update",
3477 error = check_rebase_or_histedit_in_progress(worktree);
3481 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3486 error = apply_unveil(got_repo_get_path(repo), 0,
3487 got_worktree_get_root_path(worktree));
3491 error = check_merge_in_progress(worktree, repo);
3495 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3499 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3500 got_worktree_get_head_ref_name(worktree), 0);
3503 if (commit_id_str == NULL) {
3504 error = got_ref_resolve(&commit_id, repo, head_ref);
3507 error = got_object_id_str(&commit_id_str, commit_id);
3511 struct got_reflist_head refs;
3513 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3517 error = got_repo_match_object_id(&commit_id, NULL,
3518 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3519 got_ref_list_free(&refs);
3520 free(commit_id_str);
3521 commit_id_str = NULL;
3524 error = got_object_id_str(&commit_id_str, commit_id);
3530 struct got_object_id *head_commit_id;
3531 TAILQ_FOREACH(pe, &paths, entry) {
3532 if (pe->path_len == 0)
3534 error = got_error_msg(GOT_ERR_BAD_PATH,
3535 "switching between branches requires that "
3536 "the entire work tree gets updated");
3539 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3542 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3544 free(head_commit_id);
3547 error = check_same_branch(commit_id, head_ref, NULL, repo);
3550 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3554 error = check_linear_ancestry(commit_id,
3555 got_worktree_get_base_commit_id(worktree), 0, repo);
3556 if (error != NULL) {
3557 if (error->code == GOT_ERR_ANCESTRY)
3558 error = got_error(GOT_ERR_BRANCH_MOVED);
3561 error = check_same_branch(commit_id, head_ref, NULL, repo);
3566 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3568 error = got_worktree_set_base_commit_id(worktree, repo,
3574 memset(&upa, 0, sizeof(upa));
3575 upa.verbosity = verbosity;
3576 error = got_worktree_checkout_files(worktree, &paths, repo,
3577 update_progress, &upa, check_cancelled, NULL);
3581 if (upa.did_something) {
3582 printf("Updated to %s: %s\n",
3583 got_worktree_get_head_ref_name(worktree), commit_id_str);
3585 printf("Already up-to-date\n");
3587 print_update_progress_stats(&upa);
3590 const struct got_error *pack_err =
3591 got_repo_pack_fds_close(pack_fds);
3595 free(worktree_path);
3596 TAILQ_FOREACH(pe, &paths, entry)
3597 free((char *)pe->path);
3598 got_pathlist_free(&paths);
3600 free(commit_id_str);
3604 static const struct got_error *
3605 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3606 const char *path, int diff_context, int ignore_whitespace,
3607 int force_text_diff, struct got_repository *repo, FILE *outfile)
3609 const struct got_error *err = NULL;
3610 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3611 FILE *f1 = NULL, *f2 = NULL;
3612 int fd1 = -1, fd2 = -1;
3614 fd1 = got_opentempfd();
3616 return got_error_from_errno("got_opentempfd");
3617 fd2 = got_opentempfd();
3619 err = got_error_from_errno("got_opentempfd");
3624 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3630 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3634 f1 = got_opentemp();
3636 err = got_error_from_errno("got_opentemp");
3639 f2 = got_opentemp();
3641 err = got_error_from_errno("got_opentemp");
3645 while (path[0] == '/')
3647 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3648 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3649 force_text_diff, outfile);
3651 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3652 err = got_error_from_errno("close");
3654 got_object_blob_close(blob1);
3655 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3656 err = got_error_from_errno("close");
3657 got_object_blob_close(blob2);
3658 if (f1 && fclose(f1) == EOF && err == NULL)
3659 err = got_error_from_errno("fclose");
3660 if (f2 && fclose(f2) == EOF && err == NULL)
3661 err = got_error_from_errno("fclose");
3665 static const struct got_error *
3666 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3667 const char *path, int diff_context, int ignore_whitespace,
3668 int force_text_diff, struct got_repository *repo, FILE *outfile)
3670 const struct got_error *err = NULL;
3671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 struct got_diff_blob_output_unidiff_arg arg;
3673 FILE *f1 = NULL, *f2 = NULL;
3674 int fd1 = -1, fd2 = -1;
3677 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3680 fd1 = got_opentempfd();
3682 err = got_error_from_errno("got_opentempfd");
3687 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3691 f1 = got_opentemp();
3693 err = got_error_from_errno("got_opentemp");
3697 f2 = got_opentemp();
3699 err = got_error_from_errno("got_opentemp");
3702 fd2 = got_opentempfd();
3704 err = got_error_from_errno("got_opentempfd");
3707 arg.diff_context = diff_context;
3708 arg.ignore_whitespace = ignore_whitespace;
3709 arg.force_text_diff = force_text_diff;
3710 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3711 arg.outfile = outfile;
3714 while (path[0] == '/')
3716 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3717 got_diff_blob_output_unidiff, &arg, 1);
3720 got_object_tree_close(tree1);
3722 got_object_tree_close(tree2);
3723 if (f1 && fclose(f1) == EOF && err == NULL)
3724 err = got_error_from_errno("fclose");
3725 if (f2 && fclose(f2) == EOF && err == NULL)
3726 err = got_error_from_errno("fclose");
3727 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3728 err = got_error_from_errno("close");
3729 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3730 err = got_error_from_errno("close");
3734 static const struct got_error *
3735 get_changed_paths(struct got_pathlist_head *paths,
3736 struct got_commit_object *commit, struct got_repository *repo)
3738 const struct got_error *err = NULL;
3739 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3740 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3741 struct got_object_qid *qid;
3743 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3745 struct got_commit_object *pcommit;
3746 err = got_object_open_as_commit(&pcommit, repo,
3751 tree_id1 = got_object_id_dup(
3752 got_object_commit_get_tree_id(pcommit));
3753 if (tree_id1 == NULL) {
3754 got_object_commit_close(pcommit);
3755 return got_error_from_errno("got_object_id_dup");
3757 got_object_commit_close(pcommit);
3762 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3767 tree_id2 = got_object_commit_get_tree_id(commit);
3768 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3772 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3773 got_diff_tree_collect_changed_paths, paths, 0);
3776 got_object_tree_close(tree1);
3778 got_object_tree_close(tree2);
3783 static const struct got_error *
3784 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3785 const char *path, int diff_context, struct got_repository *repo,
3788 const struct got_error *err = NULL;
3789 struct got_commit_object *pcommit = NULL;
3790 char *id_str1 = NULL, *id_str2 = NULL;
3791 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3792 struct got_object_qid *qid;
3794 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3796 err = got_object_open_as_commit(&pcommit, repo,
3800 err = got_object_id_str(&id_str1, &qid->id);
3805 err = got_object_id_str(&id_str2, id);
3809 if (path && path[0] != '\0') {
3811 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3815 err = got_object_id_by_path(&obj_id1, repo,
3818 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3824 err = got_object_get_type(&obj_type, repo, obj_id2);
3830 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3831 fprintf(outfile, "commit - %s\n",
3832 id_str1 ? id_str1 : "/dev/null");
3833 fprintf(outfile, "commit + %s\n", id_str2);
3835 case GOT_OBJ_TYPE_BLOB:
3836 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3837 0, 0, repo, outfile);
3839 case GOT_OBJ_TYPE_TREE:
3840 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3841 0, 0, repo, outfile);
3844 err = got_error(GOT_ERR_OBJ_TYPE);
3850 obj_id2 = got_object_commit_get_tree_id(commit);
3852 obj_id1 = got_object_commit_get_tree_id(pcommit);
3854 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3855 fprintf(outfile, "commit - %s\n",
3856 id_str1 ? id_str1 : "/dev/null");
3857 fprintf(outfile, "commit + %s\n", id_str2);
3858 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3865 got_object_commit_close(pcommit);
3870 get_datestr(time_t *time, char *datebuf)
3872 struct tm mytm, *tm;
3875 tm = gmtime_r(time, &mytm);
3878 s = asctime_r(tm, datebuf);
3881 p = strchr(s, '\n');
3887 static const struct got_error *
3888 match_commit(int *have_match, struct got_object_id *id,
3889 struct got_commit_object *commit, regex_t *regex)
3891 const struct got_error *err = NULL;
3892 regmatch_t regmatch;
3893 char *id_str = NULL, *logmsg = NULL;
3897 err = got_object_id_str(&id_str, id);
3901 err = got_object_commit_get_logmsg(&logmsg, commit);
3905 if (regexec(regex, got_object_commit_get_author(commit), 1,
3906 ®match, 0) == 0 ||
3907 regexec(regex, got_object_commit_get_committer(commit), 1,
3908 ®match, 0) == 0 ||
3909 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3910 regexec(regex, logmsg, 1, ®match, 0) == 0)
3919 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3922 regmatch_t regmatch;
3923 struct got_pathlist_entry *pe;
3927 TAILQ_FOREACH(pe, changed_paths, entry) {
3928 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3935 static const struct got_error *
3936 match_patch(int *have_match, struct got_commit_object *commit,
3937 struct got_object_id *id, const char *path, int diff_context,
3938 struct got_repository *repo, regex_t *regex, FILE *f)
3940 const struct got_error *err = NULL;
3942 size_t linesize = 0;
3943 regmatch_t regmatch;
3947 err = got_opentemp_truncate(f);
3951 err = print_patch(commit, id, path, diff_context, repo, f);
3955 if (fseeko(f, 0L, SEEK_SET) == -1) {
3956 err = got_error_from_errno("fseeko");
3960 while (getline(&line, &linesize, f) != -1) {
3961 if (regexec(regex, line, 1, ®match, 0) == 0) {
3971 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3973 static const struct got_error*
3974 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3975 struct got_object_id *id, struct got_repository *repo,
3978 static const struct got_error *err = NULL;
3979 struct got_reflist_entry *re;
3985 TAILQ_FOREACH(re, refs, entry) {
3986 struct got_tag_object *tag = NULL;
3987 struct got_object_id *ref_id;
3990 name = got_ref_get_name(re->ref);
3991 if (strcmp(name, GOT_REF_HEAD) == 0)
3993 if (strncmp(name, "refs/", 5) == 0)
3995 if (strncmp(name, "got/", 4) == 0)
3997 if (strncmp(name, "heads/", 6) == 0)
3999 if (strncmp(name, "remotes/", 8) == 0) {
4003 s = strstr(name, "/" GOT_REF_HEAD);
4004 if (s != NULL && s[strlen(s)] == '\0')
4007 err = got_ref_resolve(&ref_id, repo, re->ref);
4010 if (strncmp(name, "tags/", 5) == 0) {
4011 err = got_object_open_as_tag(&tag, repo, ref_id);
4013 if (err->code != GOT_ERR_OBJ_TYPE) {
4017 /* Ref points at something other than a tag. */
4022 cmp = got_object_id_cmp(tag ?
4023 got_object_tag_get_object_id(tag) : ref_id, id);
4026 got_object_tag_close(tag);
4030 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4031 s ? ", " : "", name) == -1) {
4032 err = got_error_from_errno("asprintf");
4043 static const struct got_error *
4044 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4045 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4047 const struct got_error *err = NULL;
4048 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4049 char *comma, *s, *nl;
4050 struct got_reflist_head *refs;
4051 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4053 time_t committer_time;
4055 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4057 err = build_refs_str(&ref_str, refs, id, repo, 1);
4061 /* Display the first matching ref only. */
4062 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4066 if (ref_str == NULL) {
4067 err = got_object_id_str(&id_str, id);
4072 committer_time = got_object_commit_get_committer_time(commit);
4073 if (gmtime_r(&committer_time, &tm) == NULL) {
4074 err = got_error_from_errno("gmtime_r");
4077 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4078 err = got_error(GOT_ERR_NO_SPACE);
4082 err = got_object_commit_get_logmsg(&logmsg0, commit);
4087 while (isspace((unsigned char)s[0]))
4090 nl = strchr(s, '\n');
4096 printf("%s%-7s %s\n", datebuf, ref_str, s);
4098 printf("%s%.7s %s\n", datebuf, id_str, s);
4100 if (fflush(stdout) != 0 && err == NULL)
4101 err = got_error_from_errno("fflush");
4109 static const struct got_error *
4110 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4111 struct got_repository *repo, const char *path,
4112 struct got_pathlist_head *changed_paths, int show_patch,
4113 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4114 const char *custom_refs_str)
4116 const struct got_error *err = NULL;
4117 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4119 time_t committer_time;
4120 const char *author, *committer;
4121 char *refs_str = NULL;
4123 err = got_object_id_str(&id_str, id);
4127 if (custom_refs_str == NULL) {
4128 struct got_reflist_head *refs;
4129 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4131 err = build_refs_str(&refs_str, refs, id, repo, 0);
4137 printf(GOT_COMMIT_SEP_STR);
4138 if (custom_refs_str)
4139 printf("commit %s (%s)\n", id_str, custom_refs_str);
4141 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4142 refs_str ? refs_str : "", refs_str ? ")" : "");
4147 printf("from: %s\n", got_object_commit_get_author(commit));
4148 committer_time = got_object_commit_get_committer_time(commit);
4149 datestr = get_datestr(&committer_time, datebuf);
4151 printf("date: %s UTC\n", datestr);
4152 author = got_object_commit_get_author(commit);
4153 committer = got_object_commit_get_committer(commit);
4154 if (strcmp(author, committer) != 0)
4155 printf("via: %s\n", committer);
4156 if (got_object_commit_get_nparents(commit) > 1) {
4157 const struct got_object_id_queue *parent_ids;
4158 struct got_object_qid *qid;
4160 parent_ids = got_object_commit_get_parent_ids(commit);
4161 STAILQ_FOREACH(qid, parent_ids, entry) {
4162 err = got_object_id_str(&id_str, &qid->id);
4165 printf("parent %d: %s\n", n++, id_str);
4171 err = got_object_commit_get_logmsg(&logmsg0, commit);
4177 line = strsep(&logmsg, "\n");
4179 printf(" %s\n", line);
4183 if (changed_paths) {
4184 struct got_pathlist_entry *pe;
4185 TAILQ_FOREACH(pe, changed_paths, entry) {
4186 struct got_diff_changed_path *cp = pe->data;
4187 printf(" %c %s\n", cp->status, pe->path);
4192 err = print_patch(commit, id, path, diff_context, repo, stdout);
4197 if (fflush(stdout) != 0 && err == NULL)
4198 err = got_error_from_errno("fflush");
4205 static const struct got_error *
4206 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4207 struct got_repository *repo, const char *path, int show_changed_paths,
4208 int show_patch, const char *search_pattern, int diff_context, int limit,
4209 int log_branches, int reverse_display_order,
4210 struct got_reflist_object_id_map *refs_idmap, int one_line,
4213 const struct got_error *err;
4214 struct got_commit_graph *graph;
4217 struct got_object_id_queue reversed_commits;
4218 struct got_object_qid *qid;
4219 struct got_commit_object *commit;
4220 struct got_pathlist_head changed_paths;
4221 struct got_pathlist_entry *pe;
4223 STAILQ_INIT(&reversed_commits);
4224 TAILQ_INIT(&changed_paths);
4226 if (search_pattern && regcomp(®ex, search_pattern,
4227 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4228 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4230 err = got_commit_graph_open(&graph, path, !log_branches);
4233 err = got_commit_graph_iter_start(graph, root_id, repo,
4234 check_cancelled, NULL);
4238 struct got_object_id id;
4240 if (sigint_received || sigpipe_received)
4243 err = got_commit_graph_iter_next(&id, graph, repo,
4244 check_cancelled, NULL);
4246 if (err->code == GOT_ERR_ITER_COMPLETED)
4251 err = got_object_open_as_commit(&commit, repo, &id);
4255 if (show_changed_paths && !reverse_display_order) {
4256 err = get_changed_paths(&changed_paths, commit, repo);
4261 if (search_pattern) {
4262 err = match_commit(&have_match, &id, commit, ®ex);
4264 got_object_commit_close(commit);
4267 if (have_match == 0 && show_changed_paths)
4268 match_changed_paths(&have_match,
4269 &changed_paths, ®ex);
4270 if (have_match == 0 && show_patch) {
4271 err = match_patch(&have_match, commit, &id,
4272 path, diff_context, repo, ®ex,
4277 if (have_match == 0) {
4278 got_object_commit_close(commit);
4279 TAILQ_FOREACH(pe, &changed_paths, entry) {
4280 free((char *)pe->path);
4283 got_pathlist_free(&changed_paths);
4288 if (reverse_display_order) {
4289 err = got_object_qid_alloc(&qid, &id);
4292 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4293 got_object_commit_close(commit);
4296 err = print_commit_oneline(commit, &id,
4299 err = print_commit(commit, &id, repo, path,
4300 show_changed_paths ? &changed_paths : NULL,
4301 show_patch, diff_context, refs_idmap, NULL);
4302 got_object_commit_close(commit);
4306 if ((limit && --limit == 0) ||
4307 (end_id && got_object_id_cmp(&id, end_id) == 0))
4310 TAILQ_FOREACH(pe, &changed_paths, entry) {
4311 free((char *)pe->path);
4314 got_pathlist_free(&changed_paths);
4316 if (reverse_display_order) {
4317 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4318 err = got_object_open_as_commit(&commit, repo,
4322 if (show_changed_paths) {
4323 err = get_changed_paths(&changed_paths,
4329 err = print_commit_oneline(commit, &qid->id,
4332 err = print_commit(commit, &qid->id, repo, path,
4333 show_changed_paths ? &changed_paths : NULL,
4334 show_patch, diff_context, refs_idmap, NULL);
4335 got_object_commit_close(commit);
4338 TAILQ_FOREACH(pe, &changed_paths, entry) {
4339 free((char *)pe->path);
4342 got_pathlist_free(&changed_paths);
4346 while (!STAILQ_EMPTY(&reversed_commits)) {
4347 qid = STAILQ_FIRST(&reversed_commits);
4348 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4349 got_object_qid_free(qid);
4351 TAILQ_FOREACH(pe, &changed_paths, entry) {
4352 free((char *)pe->path);
4355 got_pathlist_free(&changed_paths);
4358 got_commit_graph_close(graph);
4365 fprintf(stderr, "usage: %s log [-bPpRs] [-C number] [-c commit] [-l N] "
4366 "[-r repository-path] [-S search-pattern] [-x commit] [path]\n",
4372 get_default_log_limit(void)
4374 const char *got_default_log_limit;
4378 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4379 if (got_default_log_limit == NULL)
4381 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4387 static const struct got_error *
4388 cmd_log(int argc, char *argv[])
4390 const struct got_error *error;
4391 struct got_repository *repo = NULL;
4392 struct got_worktree *worktree = NULL;
4393 struct got_object_id *start_id = NULL, *end_id = NULL;
4394 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4395 const char *start_commit = NULL, *end_commit = NULL;
4396 const char *search_pattern = NULL;
4397 int diff_context = -1, ch;
4398 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4399 int reverse_display_order = 0, one_line = 0;
4401 struct got_reflist_head refs;
4402 struct got_reflist_object_id_map *refs_idmap = NULL;
4403 FILE *tmpfile = NULL;
4404 int *pack_fds = NULL;
4409 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4415 limit = get_default_log_limit();
4417 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4423 show_changed_paths = 1;
4426 start_commit = optarg;
4429 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4432 errx(1, "number of context lines is %s: %s",
4436 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4438 errx(1, "number of commits is %s: %s",
4445 repo_path = realpath(optarg, NULL);
4446 if (repo_path == NULL)
4447 return got_error_from_errno2("realpath",
4449 got_path_strip_trailing_slashes(repo_path);
4452 reverse_display_order = 1;
4458 search_pattern = optarg;
4461 end_commit = optarg;
4472 if (diff_context == -1)
4474 else if (!show_patch)
4475 errx(1, "-C requires -p");
4477 if (one_line && (show_patch || show_changed_paths))
4478 errx(1, "cannot use -s with -p or -P");
4480 cwd = getcwd(NULL, 0);
4482 error = got_error_from_errno("getcwd");
4486 error = got_repo_pack_fds_open(&pack_fds);
4490 if (repo_path == NULL) {
4491 error = got_worktree_open(&worktree, cwd);
4492 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4499 error = got_worktree_resolve_path(&path, worktree,
4504 path = strdup(argv[0]);
4506 error = got_error_from_errno("strdup");
4510 } else if (argc != 0)
4513 if (repo_path == NULL) {
4514 repo_path = worktree ?
4515 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4517 if (repo_path == NULL) {
4518 error = got_error_from_errno("strdup");
4522 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4526 error = apply_unveil(got_repo_get_path(repo), 1,
4527 worktree ? got_worktree_get_root_path(worktree) : NULL);
4531 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4535 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4539 if (start_commit == NULL) {
4540 struct got_reference *head_ref;
4541 struct got_commit_object *commit = NULL;
4542 error = got_ref_open(&head_ref, repo,
4543 worktree ? got_worktree_get_head_ref_name(worktree)
4547 error = got_ref_resolve(&start_id, repo, head_ref);
4548 got_ref_close(head_ref);
4551 error = got_object_open_as_commit(&commit, repo,
4555 got_object_commit_close(commit);
4557 error = got_repo_match_object_id(&start_id, NULL,
4558 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4562 if (end_commit != NULL) {
4563 error = got_repo_match_object_id(&end_id, NULL,
4564 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4571 * If a path was specified on the command line it was resolved
4572 * to a path in the work tree above. Prepend the work tree's
4573 * path prefix to obtain the corresponding in-repository path.
4577 prefix = got_worktree_get_path_prefix(worktree);
4578 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4579 (path[0] != '\0') ? "/" : "", path) == -1) {
4580 error = got_error_from_errno("asprintf");
4585 error = got_repo_map_path(&in_repo_path, repo,
4591 path = in_repo_path;
4595 /* Release work tree lock. */
4596 got_worktree_close(worktree);
4600 if (search_pattern && show_patch) {
4601 tmpfile = got_opentemp();
4602 if (tmpfile == NULL) {
4603 error = got_error_from_errno("got_opentemp");
4608 error = print_commits(start_id, end_id, repo, path ? path : "",
4609 show_changed_paths, show_patch, search_pattern, diff_context,
4610 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4617 got_worktree_close(worktree);
4619 const struct got_error *close_err = got_repo_close(repo);
4624 const struct got_error *pack_err =
4625 got_repo_pack_fds_close(pack_fds);
4630 got_reflist_object_id_map_free(refs_idmap);
4631 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4632 error = got_error_from_errno("fclose");
4633 got_ref_list_free(&refs);
4640 fprintf(stderr, "usage: %s diff [-aPsw] [-C number] [-c commit] "
4641 "[-r repository-path] [object1 object2 | path ...]\n",
4646 struct print_diff_arg {
4647 struct got_repository *repo;
4648 struct got_worktree *worktree;
4653 enum got_diff_algorithm diff_algo;
4654 int ignore_whitespace;
4655 int force_text_diff;
4661 * Create a file which contains the target path of a symlink so we can feed
4662 * it as content to the diff engine.
4664 static const struct got_error *
4665 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4666 const char *abspath)
4668 const struct got_error *err = NULL;
4669 char target_path[PATH_MAX];
4670 ssize_t target_len, outlen;
4675 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4676 if (target_len == -1)
4677 return got_error_from_errno2("readlinkat", abspath);
4679 target_len = readlink(abspath, target_path, PATH_MAX);
4680 if (target_len == -1)
4681 return got_error_from_errno2("readlink", abspath);
4684 *fd = got_opentempfd();
4686 return got_error_from_errno("got_opentempfd");
4688 outlen = write(*fd, target_path, target_len);
4690 err = got_error_from_errno("got_opentempfd");
4694 if (lseek(*fd, 0, SEEK_SET) == -1) {
4695 err = got_error_from_errno2("lseek", abspath);
4706 static const struct got_error *
4707 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4708 const char *path, struct got_object_id *blob_id,
4709 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4710 int dirfd, const char *de_name)
4712 struct print_diff_arg *a = arg;
4713 const struct got_error *err = NULL;
4714 struct got_blob_object *blob1 = NULL;
4715 int fd = -1, fd1 = -1, fd2 = -1;
4717 char *abspath = NULL, *label1 = NULL;
4722 memset(&sb, 0, sizeof(sb));
4724 if (a->diff_staged) {
4725 if (staged_status != GOT_STATUS_MODIFY &&
4726 staged_status != GOT_STATUS_ADD &&
4727 staged_status != GOT_STATUS_DELETE)
4730 if (staged_status == GOT_STATUS_DELETE)
4732 if (status == GOT_STATUS_NONEXISTENT)
4733 return got_error_set_errno(ENOENT, path);
4734 if (status != GOT_STATUS_MODIFY &&
4735 status != GOT_STATUS_ADD &&
4736 status != GOT_STATUS_DELETE &&
4737 status != GOT_STATUS_CONFLICT)
4741 err = got_opentemp_truncate(a->f1);
4743 return got_error_from_errno("got_opentemp_truncate");
4744 err = got_opentemp_truncate(a->f2);
4746 return got_error_from_errno("got_opentemp_truncate");
4748 if (!a->header_shown) {
4749 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4750 got_worktree_get_root_path(a->worktree));
4751 printf("commit - %s\n", a->id_str);
4752 printf("path + %s%s\n",
4753 got_worktree_get_root_path(a->worktree),
4754 a->diff_staged ? " (staged changes)" : "");
4755 a->header_shown = 1;
4758 if (a->diff_staged) {
4759 const char *label1 = NULL, *label2 = NULL;
4760 switch (staged_status) {
4761 case GOT_STATUS_MODIFY:
4765 case GOT_STATUS_ADD:
4768 case GOT_STATUS_DELETE:
4772 return got_error(GOT_ERR_FILE_STATUS);
4774 fd1 = got_opentempfd();
4776 err = got_error_from_errno("got_opentempfd");
4779 fd2 = got_opentempfd();
4781 err = got_error_from_errno("got_opentempfd");
4784 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4785 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4786 a->diff_algo, a->diff_context, a->ignore_whitespace,
4787 a->force_text_diff, a->repo, stdout);
4791 fd1 = got_opentempfd();
4793 err = got_error_from_errno("got_opentempfd");
4797 if (staged_status == GOT_STATUS_ADD ||
4798 staged_status == GOT_STATUS_MODIFY) {
4800 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4804 err = got_object_id_str(&id_str, staged_blob_id);
4807 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4808 err = got_error_from_errno("asprintf");
4813 } else if (status != GOT_STATUS_ADD) {
4814 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4820 if (status != GOT_STATUS_DELETE) {
4821 if (asprintf(&abspath, "%s/%s",
4822 got_worktree_get_root_path(a->worktree), path) == -1) {
4823 err = got_error_from_errno("asprintf");
4828 fd = openat(dirfd, de_name,
4829 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4831 if (!got_err_open_nofollow_on_symlink()) {
4832 err = got_error_from_errno2("openat",
4836 err = get_symlink_target_file(&fd, dirfd,
4842 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4844 if (!got_err_open_nofollow_on_symlink()) {
4845 err = got_error_from_errno2("open",
4849 err = get_symlink_target_file(&fd, dirfd,
4855 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4856 err = got_error_from_errno2("fstatat", abspath);
4859 f2 = fdopen(fd, "r");
4861 err = got_error_from_errno2("fdopen", abspath);
4869 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4875 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4876 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4877 a->ignore_whitespace, a->force_text_diff, stdout);
4879 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4880 err = got_error_from_errno("close");
4881 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4882 err = got_error_from_errno("close");
4884 got_object_blob_close(blob1);
4885 if (fd != -1 && close(fd) == -1 && err == NULL)
4886 err = got_error_from_errno("close");
4887 if (f2 && fclose(f2) == EOF && err == NULL)
4888 err = got_error_from_errno("fclose");
4893 static const struct got_error *
4894 cmd_diff(int argc, char *argv[])
4896 const struct got_error *error;
4897 struct got_repository *repo = NULL;
4898 struct got_worktree *worktree = NULL;
4899 char *cwd = NULL, *repo_path = NULL;
4900 const char *commit_args[2] = { NULL, NULL };
4901 int ncommit_args = 0;
4902 struct got_object_id *ids[2] = { NULL, NULL };
4903 char *labels[2] = { NULL, NULL };
4904 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4905 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4906 int force_text_diff = 0, force_path = 0, rflag = 0;
4908 struct got_reflist_head refs;
4909 struct got_pathlist_head paths;
4910 struct got_pathlist_entry *pe;
4911 FILE *f1 = NULL, *f2 = NULL;
4912 int fd1 = -1, fd2 = -1;
4913 int *pack_fds = NULL;
4919 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4924 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4927 force_text_diff = 1;
4930 if (ncommit_args >= 2)
4931 errx(1, "too many -c options used");
4932 commit_args[ncommit_args++] = optarg;
4935 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4938 errx(1, "number of context lines is %s: %s",
4942 repo_path = realpath(optarg, NULL);
4943 if (repo_path == NULL)
4944 return got_error_from_errno2("realpath",
4946 got_path_strip_trailing_slashes(repo_path);
4953 ignore_whitespace = 1;
4967 cwd = getcwd(NULL, 0);
4969 error = got_error_from_errno("getcwd");
4973 error = got_repo_pack_fds_open(&pack_fds);
4977 if (repo_path == NULL) {
4978 error = got_worktree_open(&worktree, cwd);
4979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4985 strdup(got_worktree_get_repo_path(worktree));
4986 if (repo_path == NULL) {
4987 error = got_error_from_errno("strdup");
4991 repo_path = strdup(cwd);
4992 if (repo_path == NULL) {
4993 error = got_error_from_errno("strdup");
4999 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5004 if (rflag || worktree == NULL || ncommit_args > 0) {
5006 error = got_error_msg(GOT_ERR_NOT_IMPL,
5007 "-P option can only be used when diffing "
5012 error = got_error_msg(GOT_ERR_NOT_IMPL,
5013 "-s option can only be used when diffing "
5019 error = apply_unveil(got_repo_get_path(repo), 1,
5020 worktree ? got_worktree_get_root_path(worktree) : NULL);
5024 if ((!force_path && argc == 2) || ncommit_args > 0) {
5025 int obj_type = (ncommit_args > 0 ?
5026 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5027 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5031 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5033 if (ncommit_args > 0)
5034 arg = commit_args[i];
5037 error = got_repo_match_object_id(&ids[i], &labels[i],
5038 arg, obj_type, &refs, repo);
5040 if (error->code != GOT_ERR_NOT_REF &&
5041 error->code != GOT_ERR_NO_OBJ)
5043 if (ncommit_args > 0)
5051 f1 = got_opentemp();
5053 error = got_error_from_errno("got_opentemp");
5057 f2 = got_opentemp();
5059 error = got_error_from_errno("got_opentemp");
5063 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5064 struct print_diff_arg arg;
5067 if (worktree == NULL) {
5068 if (argc == 2 && ids[0] == NULL) {
5069 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5071 } else if (argc == 2 && ids[1] == NULL) {
5072 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5074 } else if (argc > 0) {
5075 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5076 "%s", "specified paths cannot be resolved");
5079 error = got_error(GOT_ERR_NOT_WORKTREE);
5084 error = get_worktree_paths_from_argv(&paths, argc, argv,
5089 error = got_object_id_str(&id_str,
5090 got_worktree_get_base_commit_id(worktree));
5094 arg.worktree = worktree;
5095 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5096 arg.diff_context = diff_context;
5097 arg.id_str = id_str;
5098 arg.header_shown = 0;
5099 arg.diff_staged = diff_staged;
5100 arg.ignore_whitespace = ignore_whitespace;
5101 arg.force_text_diff = force_text_diff;
5105 error = got_worktree_status(worktree, &paths, repo, 0,
5106 print_diff, &arg, check_cancelled, NULL);
5111 if (ncommit_args == 1) {
5112 struct got_commit_object *commit;
5113 error = got_object_open_as_commit(&commit, repo, ids[0]);
5117 labels[1] = labels[0];
5119 if (got_object_commit_get_nparents(commit) > 0) {
5120 const struct got_object_id_queue *pids;
5121 struct got_object_qid *pid;
5122 pids = got_object_commit_get_parent_ids(commit);
5123 pid = STAILQ_FIRST(pids);
5124 ids[0] = got_object_id_dup(&pid->id);
5125 if (ids[0] == NULL) {
5126 error = got_error_from_errno(
5127 "got_object_id_dup");
5128 got_object_commit_close(commit);
5131 error = got_object_id_str(&labels[0], ids[0]);
5133 got_object_commit_close(commit);
5138 labels[0] = strdup("/dev/null");
5139 if (labels[0] == NULL) {
5140 error = got_error_from_errno("strdup");
5141 got_object_commit_close(commit);
5146 got_object_commit_close(commit);
5149 if (ncommit_args == 0 && argc > 2) {
5150 error = got_error_msg(GOT_ERR_BAD_PATH,
5151 "path arguments cannot be used when diffing two objects");
5156 error = got_object_get_type(&type1, repo, ids[0]);
5161 error = got_object_get_type(&type2, repo, ids[1]);
5164 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5165 error = got_error(GOT_ERR_OBJ_TYPE);
5168 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5169 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5170 "path arguments cannot be used when diffing blobs");
5174 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5176 struct got_pathlist_entry *new;
5180 error = got_worktree_resolve_path(&p, worktree,
5184 prefix = got_worktree_get_path_prefix(worktree);
5185 while (prefix[0] == '/')
5187 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5188 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5190 error = got_error_from_errno("asprintf");
5196 char *mapped_path, *s;
5197 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5203 in_repo_path = strdup(s);
5204 if (in_repo_path == NULL) {
5205 error = got_error_from_errno("asprintf");
5212 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5213 if (error || new == NULL /* duplicate */)
5220 /* Release work tree lock. */
5221 got_worktree_close(worktree);
5225 fd1 = got_opentempfd();
5227 error = got_error_from_errno("got_opentempfd");
5231 fd2 = got_opentempfd();
5233 error = got_error_from_errno("got_opentempfd");
5237 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5238 case GOT_OBJ_TYPE_BLOB:
5239 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5240 fd1, fd2, ids[0], ids[1], NULL, NULL,
5241 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5242 ignore_whitespace, force_text_diff, repo, stdout);
5244 case GOT_OBJ_TYPE_TREE:
5245 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5246 ids[0], ids[1], &paths, "", "",
5247 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5248 ignore_whitespace, force_text_diff, repo, stdout);
5250 case GOT_OBJ_TYPE_COMMIT:
5251 printf("diff %s %s\n", labels[0], labels[1]);
5252 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5253 fd1, fd2, ids[0], ids[1], &paths,
5254 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5255 ignore_whitespace, force_text_diff, repo, stdout);
5258 error = got_error(GOT_ERR_OBJ_TYPE);
5266 got_worktree_close(worktree);
5268 const struct got_error *close_err = got_repo_close(repo);
5273 const struct got_error *pack_err =
5274 got_repo_pack_fds_close(pack_fds);
5278 TAILQ_FOREACH(pe, &paths, entry)
5279 free((char *)pe->path);
5280 got_pathlist_free(&paths);
5281 got_ref_list_free(&refs);
5282 if (f1 && fclose(f1) == EOF && error == NULL)
5283 error = got_error_from_errno("fclose");
5284 if (f2 && fclose(f2) == EOF && error == NULL)
5285 error = got_error_from_errno("fclose");
5286 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5287 error = got_error_from_errno("close");
5288 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5289 error = got_error_from_errno("close");
5297 "usage: %s blame [-c commit] [-r repository-path] path\n",
5306 char datebuf[11]; /* YYYY-MM-DD + NUL */
5309 struct blame_cb_args {
5310 struct blame_line *lines;
5314 off_t *line_offsets;
5316 struct got_repository *repo;
5319 static const struct got_error *
5320 blame_cb(void *arg, int nlines, int lineno,
5321 struct got_commit_object *commit, struct got_object_id *id)
5323 const struct got_error *err = NULL;
5324 struct blame_cb_args *a = arg;
5325 struct blame_line *bline;
5327 size_t linesize = 0;
5330 time_t committer_time;
5332 if (nlines != a->nlines ||
5333 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5334 return got_error(GOT_ERR_RANGE);
5336 if (sigint_received)
5337 return got_error(GOT_ERR_ITER_COMPLETED);
5340 return NULL; /* no change in this commit */
5342 /* Annotate this line. */
5343 bline = &a->lines[lineno - 1];
5344 if (bline->annotated)
5346 err = got_object_id_str(&bline->id_str, id);
5350 bline->committer = strdup(got_object_commit_get_committer(commit));
5351 if (bline->committer == NULL) {
5352 err = got_error_from_errno("strdup");
5356 committer_time = got_object_commit_get_committer_time(commit);
5357 if (gmtime_r(&committer_time, &tm) == NULL)
5358 return got_error_from_errno("gmtime_r");
5359 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5361 err = got_error(GOT_ERR_NO_SPACE);
5364 bline->annotated = 1;
5366 /* Print lines annotated so far. */
5367 bline = &a->lines[a->lineno_cur - 1];
5368 if (!bline->annotated)
5371 offset = a->line_offsets[a->lineno_cur - 1];
5372 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5373 err = got_error_from_errno("fseeko");
5377 while (a->lineno_cur <= a->nlines && bline->annotated) {
5378 char *smallerthan, *at, *nl, *committer;
5381 if (getline(&line, &linesize, a->f) == -1) {
5383 err = got_error_from_errno("getline");
5387 committer = bline->committer;
5388 smallerthan = strchr(committer, '<');
5389 if (smallerthan && smallerthan[1] != '\0')
5390 committer = smallerthan + 1;
5391 at = strchr(committer, '@');
5394 len = strlen(committer);
5396 committer[8] = '\0';
5398 nl = strchr(line, '\n');
5401 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5402 bline->id_str, bline->datebuf, committer, line);
5405 bline = &a->lines[a->lineno_cur - 1];
5412 static const struct got_error *
5413 cmd_blame(int argc, char *argv[])
5415 const struct got_error *error;
5416 struct got_repository *repo = NULL;
5417 struct got_worktree *worktree = NULL;
5418 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5419 char *link_target = NULL;
5420 struct got_object_id *obj_id = NULL;
5421 struct got_object_id *commit_id = NULL;
5422 struct got_commit_object *commit = NULL;
5423 struct got_blob_object *blob = NULL;
5424 char *commit_id_str = NULL;
5425 struct blame_cb_args bca;
5426 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5428 int *pack_fds = NULL;
5429 FILE *f1 = NULL, *f2 = NULL;
5431 fd1 = got_opentempfd();
5433 return got_error_from_errno("got_opentempfd");
5435 memset(&bca, 0, sizeof(bca));
5438 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5443 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5446 commit_id_str = optarg;
5449 repo_path = realpath(optarg, NULL);
5450 if (repo_path == NULL)
5451 return got_error_from_errno2("realpath",
5453 got_path_strip_trailing_slashes(repo_path);
5469 cwd = getcwd(NULL, 0);
5471 error = got_error_from_errno("getcwd");
5475 error = got_repo_pack_fds_open(&pack_fds);
5479 if (repo_path == NULL) {
5480 error = got_worktree_open(&worktree, cwd);
5481 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5487 strdup(got_worktree_get_repo_path(worktree));
5488 if (repo_path == NULL) {
5489 error = got_error_from_errno("strdup");
5494 repo_path = strdup(cwd);
5495 if (repo_path == NULL) {
5496 error = got_error_from_errno("strdup");
5502 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5507 const char *prefix = got_worktree_get_path_prefix(worktree);
5510 error = got_worktree_resolve_path(&p, worktree, path);
5513 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5514 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5516 error = got_error_from_errno("asprintf");
5521 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5523 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5526 error = got_repo_map_path(&in_repo_path, repo, path);
5531 if (commit_id_str == NULL) {
5532 struct got_reference *head_ref;
5533 error = got_ref_open(&head_ref, repo, worktree ?
5534 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5537 error = got_ref_resolve(&commit_id, repo, head_ref);
5538 got_ref_close(head_ref);
5542 struct got_reflist_head refs;
5544 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5548 error = got_repo_match_object_id(&commit_id, NULL,
5549 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5550 got_ref_list_free(&refs);
5556 /* Release work tree lock. */
5557 got_worktree_close(worktree);
5561 error = got_object_open_as_commit(&commit, repo, commit_id);
5565 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5570 error = got_object_id_by_path(&obj_id, repo, commit,
5571 link_target ? link_target : in_repo_path);
5575 error = got_object_get_type(&obj_type, repo, obj_id);
5579 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5580 error = got_error_path(link_target ? link_target : in_repo_path,
5585 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5588 bca.f = got_opentemp();
5589 if (bca.f == NULL) {
5590 error = got_error_from_errno("got_opentemp");
5593 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5594 &bca.line_offsets, bca.f, blob);
5595 if (error || bca.nlines == 0)
5598 /* Don't include \n at EOF in the blame line count. */
5599 if (bca.line_offsets[bca.nlines - 1] == filesize)
5602 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5603 if (bca.lines == NULL) {
5604 error = got_error_from_errno("calloc");
5608 bca.nlines_prec = 0;
5616 fd2 = got_opentempfd();
5618 error = got_error_from_errno("got_opentempfd");
5621 fd3 = got_opentempfd();
5623 error = got_error_from_errno("got_opentempfd");
5626 f1 = got_opentemp();
5628 error = got_error_from_errno("got_opentemp");
5631 f2 = got_opentemp();
5633 error = got_error_from_errno("got_opentemp");
5636 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5637 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5638 check_cancelled, NULL, fd2, fd3, f1, f2);
5647 got_object_commit_close(commit);
5649 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5650 error = got_error_from_errno("close");
5651 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5652 error = got_error_from_errno("close");
5653 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5654 error = got_error_from_errno("close");
5655 if (f1 && fclose(f1) == EOF && error == NULL)
5656 error = got_error_from_errno("fclose");
5657 if (f2 && fclose(f2) == EOF && error == NULL)
5658 error = got_error_from_errno("fclose");
5661 got_object_blob_close(blob);
5663 got_worktree_close(worktree);
5665 const struct got_error *close_err = got_repo_close(repo);
5670 const struct got_error *pack_err =
5671 got_repo_pack_fds_close(pack_fds);
5676 for (i = 0; i < bca.nlines; i++) {
5677 struct blame_line *bline = &bca.lines[i];
5678 free(bline->id_str);
5679 free(bline->committer);
5683 free(bca.line_offsets);
5684 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5685 error = got_error_from_errno("fclose");
5692 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5693 "[path]\n", getprogname());
5697 static const struct got_error *
5698 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5699 const char *root_path, struct got_repository *repo)
5701 const struct got_error *err = NULL;
5702 int is_root_path = (strcmp(path, root_path) == 0);
5703 const char *modestr = "";
5704 mode_t mode = got_tree_entry_get_mode(te);
5705 char *link_target = NULL;
5707 path += strlen(root_path);
5708 while (path[0] == '/')
5711 if (got_object_tree_entry_is_submodule(te))
5713 else if (S_ISLNK(mode)) {
5716 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5719 for (i = 0; i < strlen(link_target); i++) {
5720 if (!isprint((unsigned char)link_target[i]))
5721 link_target[i] = '?';
5726 else if (S_ISDIR(mode))
5728 else if (mode & S_IXUSR)
5731 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5732 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5733 link_target ? " -> ": "", link_target ? link_target : "");
5739 static const struct got_error *
5740 print_tree(const char *path, struct got_commit_object *commit,
5741 int show_ids, int recurse, const char *root_path,
5742 struct got_repository *repo)
5744 const struct got_error *err = NULL;
5745 struct got_object_id *tree_id = NULL;
5746 struct got_tree_object *tree = NULL;
5749 err = got_object_id_by_path(&tree_id, repo, commit, path);
5753 err = got_object_open_as_tree(&tree, repo, tree_id);
5756 nentries = got_object_tree_get_nentries(tree);
5757 for (i = 0; i < nentries; i++) {
5758 struct got_tree_entry *te;
5761 if (sigint_received || sigpipe_received)
5764 te = got_object_tree_get_entry(tree, i);
5767 err = got_object_id_str(&id_str,
5768 got_tree_entry_get_id(te));
5771 if (asprintf(&id, "%s ", id_str) == -1) {
5772 err = got_error_from_errno("asprintf");
5778 err = print_entry(te, id, path, root_path, repo);
5783 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5785 if (asprintf(&child_path, "%s%s%s", path,
5786 path[0] == '/' && path[1] == '\0' ? "" : "/",
5787 got_tree_entry_get_name(te)) == -1) {
5788 err = got_error_from_errno("asprintf");
5791 err = print_tree(child_path, commit, show_ids, 1,
5800 got_object_tree_close(tree);
5805 static const struct got_error *
5806 cmd_tree(int argc, char *argv[])
5808 const struct got_error *error;
5809 struct got_repository *repo = NULL;
5810 struct got_worktree *worktree = NULL;
5811 const char *path, *refname = NULL;
5812 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5813 struct got_object_id *commit_id = NULL;
5814 struct got_commit_object *commit = NULL;
5815 char *commit_id_str = NULL;
5816 int show_ids = 0, recurse = 0;
5818 int *pack_fds = NULL;
5821 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5826 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5829 commit_id_str = optarg;
5832 repo_path = realpath(optarg, NULL);
5833 if (repo_path == NULL)
5834 return got_error_from_errno2("realpath",
5836 got_path_strip_trailing_slashes(repo_path);
5860 cwd = getcwd(NULL, 0);
5862 error = got_error_from_errno("getcwd");
5866 error = got_repo_pack_fds_open(&pack_fds);
5870 if (repo_path == NULL) {
5871 error = got_worktree_open(&worktree, cwd);
5872 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5878 strdup(got_worktree_get_repo_path(worktree));
5879 if (repo_path == NULL)
5880 error = got_error_from_errno("strdup");
5884 repo_path = strdup(cwd);
5885 if (repo_path == NULL) {
5886 error = got_error_from_errno("strdup");
5892 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5897 const char *prefix = got_worktree_get_path_prefix(worktree);
5902 error = got_worktree_resolve_path(&p, worktree, path);
5905 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5906 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5908 error = got_error_from_errno("asprintf");
5913 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5917 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5922 error = got_repo_map_path(&in_repo_path, repo, path);
5927 if (commit_id_str == NULL) {
5928 struct got_reference *head_ref;
5930 refname = got_worktree_get_head_ref_name(worktree);
5932 refname = GOT_REF_HEAD;
5933 error = got_ref_open(&head_ref, repo, refname, 0);
5936 error = got_ref_resolve(&commit_id, repo, head_ref);
5937 got_ref_close(head_ref);
5941 struct got_reflist_head refs;
5943 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5947 error = got_repo_match_object_id(&commit_id, NULL,
5948 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5949 got_ref_list_free(&refs);
5955 /* Release work tree lock. */
5956 got_worktree_close(worktree);
5960 error = got_object_open_as_commit(&commit, repo, commit_id);
5964 error = print_tree(in_repo_path, commit, show_ids, recurse,
5965 in_repo_path, repo);
5972 got_object_commit_close(commit);
5974 got_worktree_close(worktree);
5976 const struct got_error *close_err = got_repo_close(repo);
5981 const struct got_error *pack_err =
5982 got_repo_pack_fds_close(pack_fds);
5992 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5993 "[-s status-codes] [path ...]\n", getprogname());
5997 struct got_status_arg {
6002 static const struct got_error *
6003 print_status(void *arg, unsigned char status, unsigned char staged_status,
6004 const char *path, struct got_object_id *blob_id,
6005 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6006 int dirfd, const char *de_name)
6008 struct got_status_arg *st = arg;
6010 if (status == staged_status && (status == GOT_STATUS_DELETE))
6011 status = GOT_STATUS_NO_CHANGE;
6012 if (st != NULL && st->status_codes) {
6013 size_t ncodes = strlen(st->status_codes);
6016 for (i = 0; i < ncodes ; i++) {
6018 if (status == st->status_codes[i] ||
6019 staged_status == st->status_codes[i]) {
6024 if (status == st->status_codes[i] ||
6025 staged_status == st->status_codes[i])
6030 if (st->suppress && j == 0)
6037 printf("%c%c %s\n", status, staged_status, path);
6041 static const struct got_error *
6042 cmd_status(int argc, char *argv[])
6044 const struct got_error *error = NULL;
6045 struct got_repository *repo = NULL;
6046 struct got_worktree *worktree = NULL;
6047 struct got_status_arg st;
6049 struct got_pathlist_head paths;
6050 struct got_pathlist_entry *pe;
6051 int ch, i, no_ignores = 0;
6052 int *pack_fds = NULL;
6056 memset(&st, 0, sizeof(st));
6057 st.status_codes = NULL;
6060 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6066 if (st.status_codes != NULL && st.suppress == 0)
6067 option_conflict('S', 's');
6071 for (i = 0; i < strlen(optarg); i++) {
6072 switch (optarg[i]) {
6073 case GOT_STATUS_MODIFY:
6074 case GOT_STATUS_ADD:
6075 case GOT_STATUS_DELETE:
6076 case GOT_STATUS_CONFLICT:
6077 case GOT_STATUS_MISSING:
6078 case GOT_STATUS_OBSTRUCTED:
6079 case GOT_STATUS_UNVERSIONED:
6080 case GOT_STATUS_MODE_CHANGE:
6081 case GOT_STATUS_NONEXISTENT:
6084 errx(1, "invalid status code '%c'",
6088 if (ch == 's' && st.suppress)
6089 option_conflict('s', 'S');
6090 st.status_codes = optarg;
6102 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6106 cwd = getcwd(NULL, 0);
6108 error = got_error_from_errno("getcwd");
6112 error = got_repo_pack_fds_open(&pack_fds);
6116 error = got_worktree_open(&worktree, cwd);
6118 if (error->code == GOT_ERR_NOT_WORKTREE)
6119 error = wrap_not_worktree_error(error, "status", cwd);
6123 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6128 error = apply_unveil(got_repo_get_path(repo), 1,
6129 got_worktree_get_root_path(worktree));
6133 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6137 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6138 print_status, &st, check_cancelled, NULL);
6141 const struct got_error *pack_err =
6142 got_repo_pack_fds_close(pack_fds);
6147 TAILQ_FOREACH(pe, &paths, entry)
6148 free((char *)pe->path);
6149 got_pathlist_free(&paths);
6157 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6158 "[-s reference] [name]\n", getprogname());
6162 static const struct got_error *
6163 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6165 static const struct got_error *err = NULL;
6166 struct got_reflist_head refs;
6167 struct got_reflist_entry *re;
6170 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6171 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6176 TAILQ_FOREACH(re, &refs, entry) {
6178 refstr = got_ref_to_str(re->ref);
6179 if (refstr == NULL) {
6180 err = got_error_from_errno("got_ref_to_str");
6183 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6187 got_ref_list_free(&refs);
6191 static const struct got_error *
6192 delete_ref_by_name(struct got_repository *repo, const char *refname)
6194 const struct got_error *err;
6195 struct got_reference *ref;
6197 err = got_ref_open(&ref, repo, refname, 0);
6201 err = delete_ref(repo, ref);
6206 static const struct got_error *
6207 add_ref(struct got_repository *repo, const char *refname, const char *target)
6209 const struct got_error *err = NULL;
6210 struct got_object_id *id = NULL;
6211 struct got_reference *ref = NULL;
6212 struct got_reflist_head refs;
6215 * Don't let the user create a reference name with a leading '-'.
6216 * While technically a valid reference name, this case is usually
6217 * an unintended typo.
6219 if (refname[0] == '-')
6220 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6223 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6226 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6228 got_ref_list_free(&refs);
6232 err = got_ref_alloc(&ref, refname, id);
6236 err = got_ref_write(ref, repo);
6244 static const struct got_error *
6245 add_symref(struct got_repository *repo, const char *refname, const char *target)
6247 const struct got_error *err = NULL;
6248 struct got_reference *ref = NULL;
6249 struct got_reference *target_ref = NULL;
6252 * Don't let the user create a reference name with a leading '-'.
6253 * While technically a valid reference name, this case is usually
6254 * an unintended typo.
6256 if (refname[0] == '-')
6257 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6259 err = got_ref_open(&target_ref, repo, target, 0);
6263 err = got_ref_alloc_symref(&ref, refname, target_ref);
6267 err = got_ref_write(ref, repo);
6270 got_ref_close(target_ref);
6276 static const struct got_error *
6277 cmd_ref(int argc, char *argv[])
6279 const struct got_error *error = NULL;
6280 struct got_repository *repo = NULL;
6281 struct got_worktree *worktree = NULL;
6282 char *cwd = NULL, *repo_path = NULL;
6283 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6284 const char *obj_arg = NULL, *symref_target= NULL;
6285 char *refname = NULL;
6286 int *pack_fds = NULL;
6288 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6297 repo_path = realpath(optarg, NULL);
6298 if (repo_path == NULL)
6299 return got_error_from_errno2("realpath",
6301 got_path_strip_trailing_slashes(repo_path);
6307 symref_target = optarg;
6318 if (obj_arg && do_list)
6319 option_conflict('c', 'l');
6320 if (obj_arg && do_delete)
6321 option_conflict('c', 'd');
6322 if (obj_arg && symref_target)
6323 option_conflict('c', 's');
6324 if (symref_target && do_delete)
6325 option_conflict('s', 'd');
6326 if (symref_target && do_list)
6327 option_conflict('s', 'l');
6328 if (do_delete && do_list)
6329 option_conflict('d', 'l');
6330 if (sort_by_time && !do_list)
6331 errx(1, "-t option requires -l option");
6337 if (argc != 0 && argc != 1)
6340 refname = strdup(argv[0]);
6341 if (refname == NULL) {
6342 error = got_error_from_errno("strdup");
6349 refname = strdup(argv[0]);
6350 if (refname == NULL) {
6351 error = got_error_from_errno("strdup");
6357 got_path_strip_trailing_slashes(refname);
6360 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6361 "sendfd unveil", NULL) == -1)
6364 cwd = getcwd(NULL, 0);
6366 error = got_error_from_errno("getcwd");
6370 error = got_repo_pack_fds_open(&pack_fds);
6374 if (repo_path == NULL) {
6375 error = got_worktree_open(&worktree, cwd);
6376 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6382 strdup(got_worktree_get_repo_path(worktree));
6383 if (repo_path == NULL)
6384 error = got_error_from_errno("strdup");
6388 repo_path = strdup(cwd);
6389 if (repo_path == NULL) {
6390 error = got_error_from_errno("strdup");
6396 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6402 /* Remove "cpath" promise. */
6403 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6409 error = apply_unveil(got_repo_get_path(repo), do_list,
6410 worktree ? got_worktree_get_root_path(worktree) : NULL);
6415 error = list_refs(repo, refname, sort_by_time);
6417 error = delete_ref_by_name(repo, refname);
6418 else if (symref_target)
6419 error = add_symref(repo, refname, symref_target);
6421 if (obj_arg == NULL)
6423 error = add_ref(repo, refname, obj_arg);
6428 const struct got_error *close_err = got_repo_close(repo);
6433 got_worktree_close(worktree);
6435 const struct got_error *pack_err =
6436 got_repo_pack_fds_close(pack_fds);
6448 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6449 "[-r repository-path] [name]\n", getprogname());
6453 static const struct got_error *
6454 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6455 struct got_reference *ref)
6457 const struct got_error *err = NULL;
6458 const char *refname, *marker = " ";
6461 refname = got_ref_get_name(ref);
6462 if (worktree && strcmp(refname,
6463 got_worktree_get_head_ref_name(worktree)) == 0) {
6464 struct got_object_id *id = NULL;
6466 err = got_ref_resolve(&id, repo, ref);
6469 if (got_object_id_cmp(id,
6470 got_worktree_get_base_commit_id(worktree)) == 0)
6477 if (strncmp(refname, "refs/heads/", 11) == 0)
6479 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6481 if (strncmp(refname, "refs/remotes/", 13) == 0)
6484 refstr = got_ref_to_str(ref);
6486 return got_error_from_errno("got_ref_to_str");
6488 printf("%s%s: %s\n", marker, refname, refstr);
6493 static const struct got_error *
6494 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6496 const char *refname;
6498 if (worktree == NULL)
6499 return got_error(GOT_ERR_NOT_WORKTREE);
6501 refname = got_worktree_get_head_ref_name(worktree);
6503 if (strncmp(refname, "refs/heads/", 11) == 0)
6505 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6508 printf("%s\n", refname);
6513 static const struct got_error *
6514 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6517 static const struct got_error *err = NULL;
6518 struct got_reflist_head refs;
6519 struct got_reflist_entry *re;
6520 struct got_reference *temp_ref = NULL;
6521 int rebase_in_progress, histedit_in_progress;
6526 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6531 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6536 if (rebase_in_progress || histedit_in_progress) {
6537 err = got_ref_open(&temp_ref, repo,
6538 got_worktree_get_head_ref_name(worktree), 0);
6541 list_branch(repo, worktree, temp_ref);
6542 got_ref_close(temp_ref);
6546 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6547 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6552 TAILQ_FOREACH(re, &refs, entry)
6553 list_branch(repo, worktree, re->ref);
6555 got_ref_list_free(&refs);
6557 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6558 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6563 TAILQ_FOREACH(re, &refs, entry)
6564 list_branch(repo, worktree, re->ref);
6566 got_ref_list_free(&refs);
6571 static const struct got_error *
6572 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6573 const char *branch_name)
6575 const struct got_error *err = NULL;
6576 struct got_reference *ref = NULL;
6577 char *refname, *remote_refname = NULL;
6579 if (strncmp(branch_name, "refs/", 5) == 0)
6581 if (strncmp(branch_name, "heads/", 6) == 0)
6583 else if (strncmp(branch_name, "remotes/", 8) == 0)
6586 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6587 return got_error_from_errno("asprintf");
6589 if (asprintf(&remote_refname, "refs/remotes/%s",
6590 branch_name) == -1) {
6591 err = got_error_from_errno("asprintf");
6595 err = got_ref_open(&ref, repo, refname, 0);
6597 const struct got_error *err2;
6598 if (err->code != GOT_ERR_NOT_REF)
6601 * Keep 'err' intact such that if neither branch exists
6602 * we report "refs/heads" rather than "refs/remotes" in
6603 * our error message.
6605 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6612 strcmp(got_worktree_get_head_ref_name(worktree),
6613 got_ref_get_name(ref)) == 0) {
6614 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6615 "will not delete this work tree's current branch");
6619 err = delete_ref(repo, ref);
6624 free(remote_refname);
6628 static const struct got_error *
6629 add_branch(struct got_repository *repo, const char *branch_name,
6630 struct got_object_id *base_commit_id)
6632 const struct got_error *err = NULL;
6633 struct got_reference *ref = NULL;
6634 char *base_refname = NULL, *refname = NULL;
6637 * Don't let the user create a branch name with a leading '-'.
6638 * While technically a valid reference name, this case is usually
6639 * an unintended typo.
6641 if (branch_name[0] == '-')
6642 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6644 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6647 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6648 err = got_error_from_errno("asprintf");
6652 err = got_ref_open(&ref, repo, refname, 0);
6654 err = got_error(GOT_ERR_BRANCH_EXISTS);
6656 } else if (err->code != GOT_ERR_NOT_REF)
6659 err = got_ref_alloc(&ref, refname, base_commit_id);
6663 err = got_ref_write(ref, repo);
6672 static const struct got_error *
6673 cmd_branch(int argc, char *argv[])
6675 const struct got_error *error = NULL;
6676 struct got_repository *repo = NULL;
6677 struct got_worktree *worktree = NULL;
6678 char *cwd = NULL, *repo_path = NULL;
6679 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6680 const char *delref = NULL, *commit_id_arg = NULL;
6681 struct got_reference *ref = NULL;
6682 struct got_pathlist_head paths;
6683 struct got_pathlist_entry *pe;
6684 struct got_object_id *commit_id = NULL;
6685 char *commit_id_str = NULL;
6686 int *pack_fds = NULL;
6690 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6693 commit_id_arg = optarg;
6699 repo_path = realpath(optarg, NULL);
6700 if (repo_path == NULL)
6701 return got_error_from_errno2("realpath",
6703 got_path_strip_trailing_slashes(repo_path);
6720 if (do_list && delref)
6721 option_conflict('l', 'd');
6722 if (sort_by_time && !do_list)
6723 errx(1, "-t option requires -l option");
6728 if (!do_list && !delref && argc == 0)
6731 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6732 errx(1, "-c option can only be used when creating a branch");
6734 if (do_list || delref) {
6737 } else if (!do_show && argc != 1)
6741 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6742 "sendfd unveil", NULL) == -1)
6745 cwd = getcwd(NULL, 0);
6747 error = got_error_from_errno("getcwd");
6751 error = got_repo_pack_fds_open(&pack_fds);
6755 if (repo_path == NULL) {
6756 error = got_worktree_open(&worktree, cwd);
6757 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6763 strdup(got_worktree_get_repo_path(worktree));
6764 if (repo_path == NULL)
6765 error = got_error_from_errno("strdup");
6769 repo_path = strdup(cwd);
6770 if (repo_path == NULL) {
6771 error = got_error_from_errno("strdup");
6777 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6782 if (do_list || do_show) {
6783 /* Remove "cpath" promise. */
6784 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6790 error = apply_unveil(got_repo_get_path(repo), do_list,
6791 worktree ? got_worktree_get_root_path(worktree) : NULL);
6796 error = show_current_branch(repo, worktree);
6798 error = list_branches(repo, worktree, sort_by_time);
6800 error = delete_branch(repo, worktree, delref);
6802 struct got_reflist_head refs;
6804 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6808 if (commit_id_arg == NULL)
6809 commit_id_arg = worktree ?
6810 got_worktree_get_head_ref_name(worktree) :
6812 error = got_repo_match_object_id(&commit_id, NULL,
6813 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6814 got_ref_list_free(&refs);
6817 error = add_branch(repo, argv[0], commit_id);
6820 if (worktree && do_update) {
6821 struct got_update_progress_arg upa;
6822 char *branch_refname = NULL;
6824 error = got_object_id_str(&commit_id_str, commit_id);
6827 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6831 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6833 error = got_error_from_errno("asprintf");
6836 error = got_ref_open(&ref, repo, branch_refname, 0);
6837 free(branch_refname);
6840 error = switch_head_ref(ref, commit_id, worktree,
6844 error = got_worktree_set_base_commit_id(worktree, repo,
6848 memset(&upa, 0, sizeof(upa));
6849 error = got_worktree_checkout_files(worktree, &paths,
6850 repo, update_progress, &upa, check_cancelled,
6854 if (upa.did_something) {
6855 printf("Updated to %s: %s\n",
6856 got_worktree_get_head_ref_name(worktree),
6859 print_update_progress_stats(&upa);
6866 const struct got_error *close_err = got_repo_close(repo);
6871 got_worktree_close(worktree);
6873 const struct got_error *pack_err =
6874 got_repo_pack_fds_close(pack_fds);
6881 free(commit_id_str);
6882 TAILQ_FOREACH(pe, &paths, entry)
6883 free((char *)pe->path);
6884 got_pathlist_free(&paths);
6892 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
6893 "[-r repository-path] [-s signer-id] name\n", getprogname());
6898 static const struct got_error *
6899 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6901 const struct got_error *err = NULL;
6902 struct got_reflist_entry *re, *se, *new;
6903 struct got_object_id *re_id, *se_id;
6904 struct got_tag_object *re_tag, *se_tag;
6905 time_t re_time, se_time;
6907 STAILQ_FOREACH(re, tags, entry) {
6908 se = STAILQ_FIRST(sorted);
6910 err = got_reflist_entry_dup(&new, re);
6913 STAILQ_INSERT_HEAD(sorted, new, entry);
6916 err = got_ref_resolve(&re_id, repo, re->ref);
6919 err = got_object_open_as_tag(&re_tag, repo, re_id);
6923 re_time = got_object_tag_get_tagger_time(re_tag);
6924 got_object_tag_close(re_tag);
6928 err = got_ref_resolve(&se_id, repo, re->ref);
6931 err = got_object_open_as_tag(&se_tag, repo, se_id);
6935 se_time = got_object_tag_get_tagger_time(se_tag);
6936 got_object_tag_close(se_tag);
6938 if (se_time > re_time) {
6939 err = got_reflist_entry_dup(&new, re);
6942 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6945 se = STAILQ_NEXT(se, entry);
6954 static const struct got_error *
6955 get_tag_refname(char **refname, const char *tag_name)
6957 const struct got_error *err;
6959 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6960 *refname = strdup(tag_name);
6961 if (*refname == NULL)
6962 return got_error_from_errno("strdup");
6963 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6964 err = got_error_from_errno("asprintf");
6972 static const struct got_error *
6973 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6974 const char *allowed_signers, const char *revoked_signers, int verbosity)
6976 static const struct got_error *err = NULL;
6977 struct got_reflist_head refs;
6978 struct got_reflist_entry *re;
6979 char *wanted_refname = NULL;
6984 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6989 struct got_reference *ref;
6990 err = get_tag_refname(&wanted_refname, tag_name);
6993 /* Wanted tag reference should exist. */
6994 err = got_ref_open(&ref, repo, wanted_refname, 0);
7000 TAILQ_FOREACH(re, &refs, entry) {
7001 const char *refname;
7002 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7004 const char *tagger, *ssh_sig = NULL;
7005 char *sig_msg = NULL;
7007 struct got_object_id *id;
7008 struct got_tag_object *tag;
7009 struct got_commit_object *commit = NULL;
7011 refname = got_ref_get_name(re->ref);
7012 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7013 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7016 refstr = got_ref_to_str(re->ref);
7017 if (refstr == NULL) {
7018 err = got_error_from_errno("got_ref_to_str");
7022 err = got_ref_resolve(&id, repo, re->ref);
7025 err = got_object_open_as_tag(&tag, repo, id);
7027 if (err->code != GOT_ERR_OBJ_TYPE) {
7031 /* "lightweight" tag */
7032 err = got_object_open_as_commit(&commit, repo, id);
7037 tagger = got_object_commit_get_committer(commit);
7039 got_object_commit_get_committer_time(commit);
7040 err = got_object_id_str(&id_str, id);
7046 tagger = got_object_tag_get_tagger(tag);
7047 tagger_time = got_object_tag_get_tagger_time(tag);
7048 err = got_object_id_str(&id_str,
7049 got_object_tag_get_object_id(tag));
7054 if (tag && verify_tags) {
7055 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7056 got_object_tag_get_message(tag));
7057 if (ssh_sig && allowed_signers == NULL) {
7058 err = got_error_msg(
7059 GOT_ERR_VERIFY_TAG_SIGNATURE,
7060 "SSH signature verification requires "
7061 "setting allowed_signers in "
7067 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7069 printf("from: %s\n", tagger);
7070 datestr = get_datestr(&tagger_time, datebuf);
7072 printf("date: %s UTC\n", datestr);
7074 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7076 switch (got_object_tag_get_object_type(tag)) {
7077 case GOT_OBJ_TYPE_BLOB:
7078 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7081 case GOT_OBJ_TYPE_TREE:
7082 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7085 case GOT_OBJ_TYPE_COMMIT:
7086 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7089 case GOT_OBJ_TYPE_TAG:
7090 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7100 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7101 allowed_signers, revoked_signers, verbosity);
7102 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7106 printf("signature: %s", sig_msg);
7112 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7115 got_object_commit_close(commit);
7117 tagmsg0 = strdup(got_object_tag_get_message(tag));
7118 got_object_tag_close(tag);
7119 if (tagmsg0 == NULL) {
7120 err = got_error_from_errno("strdup");
7127 line = strsep(&tagmsg, "\n");
7129 printf(" %s\n", line);
7134 got_ref_list_free(&refs);
7135 free(wanted_refname);
7137 if (err == NULL && bad_sigs)
7138 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7142 static const struct got_error *
7143 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7144 const char *tag_name, const char *repo_path)
7146 const struct got_error *err = NULL;
7147 char *template = NULL, *initial_content = NULL;
7148 char *editor = NULL;
7149 int initial_content_len;
7152 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7153 err = got_error_from_errno("asprintf");
7157 initial_content_len = asprintf(&initial_content,
7158 "\n# tagging commit %s as %s\n",
7159 commit_id_str, tag_name);
7160 if (initial_content_len == -1) {
7161 err = got_error_from_errno("asprintf");
7165 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7169 if (write(fd, initial_content, initial_content_len) == -1) {
7170 err = got_error_from_errno2("write", *tagmsg_path);
7174 err = get_editor(&editor);
7177 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7178 initial_content_len, 1);
7180 free(initial_content);
7184 if (fd != -1 && close(fd) == -1 && err == NULL)
7185 err = got_error_from_errno2("close", *tagmsg_path);
7194 static const struct got_error *
7195 add_tag(struct got_repository *repo, const char *tagger,
7196 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7197 const char *signer_id, int verbosity)
7199 const struct got_error *err = NULL;
7200 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7201 char *label = NULL, *commit_id_str = NULL;
7202 struct got_reference *ref = NULL;
7203 char *refname = NULL, *tagmsg = NULL;
7204 char *tagmsg_path = NULL, *tag_id_str = NULL;
7205 int preserve_tagmsg = 0;
7206 struct got_reflist_head refs;
7211 * Don't let the user create a tag name with a leading '-'.
7212 * While technically a valid reference name, this case is usually
7213 * an unintended typo.
7215 if (tag_name[0] == '-')
7216 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7218 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7222 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7223 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7227 err = got_object_id_str(&commit_id_str, commit_id);
7231 err = get_tag_refname(&refname, tag_name);
7234 if (strncmp("refs/tags/", tag_name, 10) == 0)
7237 err = got_ref_open(&ref, repo, refname, 0);
7239 err = got_error(GOT_ERR_TAG_EXISTS);
7241 } else if (err->code != GOT_ERR_NOT_REF)
7244 if (tagmsg_arg == NULL) {
7245 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7246 tag_name, got_repo_get_path(repo));
7248 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7249 tagmsg_path != NULL)
7250 preserve_tagmsg = 1;
7253 /* Editor is done; we can now apply unveil(2) */
7254 err = got_sigs_apply_unveil();
7257 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7262 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7263 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7267 preserve_tagmsg = 1;
7271 err = got_ref_alloc(&ref, refname, tag_id);
7274 preserve_tagmsg = 1;
7278 err = got_ref_write(ref, repo);
7281 preserve_tagmsg = 1;
7285 err = got_object_id_str(&tag_id_str, tag_id);
7288 preserve_tagmsg = 1;
7291 printf("Created tag %s\n", tag_id_str);
7293 if (preserve_tagmsg) {
7294 fprintf(stderr, "%s: tag message preserved in %s\n",
7295 getprogname(), tagmsg_path);
7296 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7297 err = got_error_from_errno2("unlink", tagmsg_path);
7302 free(commit_id_str);
7306 got_ref_list_free(&refs);
7310 static const struct got_error *
7311 cmd_tag(int argc, char *argv[])
7313 const struct got_error *error = NULL;
7314 struct got_repository *repo = NULL;
7315 struct got_worktree *worktree = NULL;
7316 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7317 char *gitconfig_path = NULL, *tagger = NULL;
7318 char *allowed_signers = NULL, *revoked_signers = NULL;
7319 char *signer_id = NULL;
7320 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7321 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7322 int *pack_fds = NULL;
7324 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7327 commit_id_arg = optarg;
7333 repo_path = realpath(optarg, NULL);
7334 if (repo_path == NULL) {
7335 error = got_error_from_errno2("realpath",
7339 got_path_strip_trailing_slashes(repo_path);
7345 signer_id = strdup(optarg);
7346 if (signer_id == NULL) {
7347 error = got_error_from_errno("strdup");
7357 else if (verbosity < 3)
7369 if (do_list || verify_tags) {
7370 if (commit_id_arg != NULL)
7372 "-c option can only be used when creating a tag");
7375 option_conflict('l', 'm');
7377 option_conflict('V', 'm');
7381 option_conflict('l', 's');
7383 option_conflict('V', 's');
7387 } else if (argc != 1)
7394 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7395 "sendfd unveil", NULL) == -1)
7398 cwd = getcwd(NULL, 0);
7400 error = got_error_from_errno("getcwd");
7404 error = got_repo_pack_fds_open(&pack_fds);
7408 if (repo_path == NULL) {
7409 error = got_worktree_open(&worktree, cwd);
7410 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7416 strdup(got_worktree_get_repo_path(worktree));
7417 if (repo_path == NULL)
7418 error = got_error_from_errno("strdup");
7422 repo_path = strdup(cwd);
7423 if (repo_path == NULL) {
7424 error = got_error_from_errno("strdup");
7430 if (do_list || verify_tags) {
7431 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7434 error = get_allowed_signers(&allowed_signers, repo, worktree);
7437 error = get_revoked_signers(&revoked_signers, repo, worktree);
7441 /* Release work tree lock. */
7442 got_worktree_close(worktree);
7447 * Remove "cpath" promise unless needed for signature tmpfile
7451 got_sigs_apply_unveil();
7454 if (pledge("stdio rpath wpath flock proc exec sendfd "
7455 "unveil", NULL) == -1)
7459 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7462 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7463 revoked_signers, verbosity);
7465 error = get_gitconfig_path(&gitconfig_path);
7468 error = got_repo_open(&repo, repo_path, gitconfig_path,
7473 error = get_author(&tagger, repo, worktree);
7476 if (signer_id == NULL) {
7477 error = get_signer_id(&signer_id, repo, worktree);
7484 error = got_sigs_apply_unveil();
7488 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7493 if (commit_id_arg == NULL) {
7494 struct got_reference *head_ref;
7495 struct got_object_id *commit_id;
7496 error = got_ref_open(&head_ref, repo,
7497 worktree ? got_worktree_get_head_ref_name(worktree)
7501 error = got_ref_resolve(&commit_id, repo, head_ref);
7502 got_ref_close(head_ref);
7505 error = got_object_id_str(&commit_id_str, commit_id);
7512 /* Release work tree lock. */
7513 got_worktree_close(worktree);
7517 error = add_tag(repo, tagger, tag_name,
7518 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7519 signer_id, verbosity);
7523 const struct got_error *close_err = got_repo_close(repo);
7528 got_worktree_close(worktree);
7530 const struct got_error *pack_err =
7531 got_repo_pack_fds_close(pack_fds);
7537 free(gitconfig_path);
7538 free(commit_id_str);
7540 free(allowed_signers);
7541 free(revoked_signers);
7549 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7553 static const struct got_error *
7554 add_progress(void *arg, unsigned char status, const char *path)
7556 while (path[0] == '/')
7558 printf("%c %s\n", status, path);
7562 static const struct got_error *
7563 cmd_add(int argc, char *argv[])
7565 const struct got_error *error = NULL;
7566 struct got_repository *repo = NULL;
7567 struct got_worktree *worktree = NULL;
7569 struct got_pathlist_head paths;
7570 struct got_pathlist_entry *pe;
7571 int ch, can_recurse = 0, no_ignores = 0;
7572 int *pack_fds = NULL;
7576 while ((ch = getopt(argc, argv, "IR")) != -1) {
7594 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7601 cwd = getcwd(NULL, 0);
7603 error = got_error_from_errno("getcwd");
7607 error = got_repo_pack_fds_open(&pack_fds);
7611 error = got_worktree_open(&worktree, cwd);
7613 if (error->code == GOT_ERR_NOT_WORKTREE)
7614 error = wrap_not_worktree_error(error, "add", cwd);
7618 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7623 error = apply_unveil(got_repo_get_path(repo), 1,
7624 got_worktree_get_root_path(worktree));
7628 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7635 TAILQ_FOREACH(pe, &paths, entry) {
7636 if (asprintf(&ondisk_path, "%s/%s",
7637 got_worktree_get_root_path(worktree),
7639 error = got_error_from_errno("asprintf");
7642 if (lstat(ondisk_path, &sb) == -1) {
7643 if (errno == ENOENT) {
7647 error = got_error_from_errno2("lstat",
7653 if (S_ISDIR(sb.st_mode)) {
7654 error = got_error_msg(GOT_ERR_BAD_PATH,
7655 "adding directories requires -R option");
7661 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7662 NULL, repo, no_ignores);
7665 const struct got_error *close_err = got_repo_close(repo);
7670 got_worktree_close(worktree);
7672 const struct got_error *pack_err =
7673 got_repo_pack_fds_close(pack_fds);
7677 TAILQ_FOREACH(pe, &paths, entry)
7678 free((char *)pe->path);
7679 got_pathlist_free(&paths);
7687 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7692 static const struct got_error *
7693 print_remove_status(void *arg, unsigned char status,
7694 unsigned char staged_status, const char *path)
7696 while (path[0] == '/')
7698 if (status == GOT_STATUS_NONEXISTENT)
7700 if (status == staged_status && (status == GOT_STATUS_DELETE))
7701 status = GOT_STATUS_NO_CHANGE;
7702 printf("%c%c %s\n", status, staged_status, path);
7706 static const struct got_error *
7707 cmd_remove(int argc, char *argv[])
7709 const struct got_error *error = NULL;
7710 struct got_worktree *worktree = NULL;
7711 struct got_repository *repo = NULL;
7712 const char *status_codes = NULL;
7714 struct got_pathlist_head paths;
7715 struct got_pathlist_entry *pe;
7716 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7717 int ignore_missing_paths = 0;
7718 int *pack_fds = NULL;
7722 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7725 delete_local_mods = 1;
7726 ignore_missing_paths = 1;
7735 for (i = 0; i < strlen(optarg); i++) {
7736 switch (optarg[i]) {
7737 case GOT_STATUS_MODIFY:
7738 delete_local_mods = 1;
7740 case GOT_STATUS_MISSING:
7741 ignore_missing_paths = 1;
7744 errx(1, "invalid status code '%c'",
7748 status_codes = optarg;
7760 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7767 cwd = getcwd(NULL, 0);
7769 error = got_error_from_errno("getcwd");
7773 error = got_repo_pack_fds_open(&pack_fds);
7777 error = got_worktree_open(&worktree, cwd);
7779 if (error->code == GOT_ERR_NOT_WORKTREE)
7780 error = wrap_not_worktree_error(error, "remove", cwd);
7784 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7789 error = apply_unveil(got_repo_get_path(repo), 1,
7790 got_worktree_get_root_path(worktree));
7794 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7801 TAILQ_FOREACH(pe, &paths, entry) {
7802 if (asprintf(&ondisk_path, "%s/%s",
7803 got_worktree_get_root_path(worktree),
7805 error = got_error_from_errno("asprintf");
7808 if (lstat(ondisk_path, &sb) == -1) {
7809 if (errno == ENOENT) {
7813 error = got_error_from_errno2("lstat",
7819 if (S_ISDIR(sb.st_mode)) {
7820 error = got_error_msg(GOT_ERR_BAD_PATH,
7821 "removing directories requires -R option");
7827 error = got_worktree_schedule_delete(worktree, &paths,
7828 delete_local_mods, status_codes, print_remove_status, NULL,
7829 repo, keep_on_disk, ignore_missing_paths);
7832 const struct got_error *close_err = got_repo_close(repo);
7837 got_worktree_close(worktree);
7839 const struct got_error *pack_err =
7840 got_repo_pack_fds_close(pack_fds);
7844 TAILQ_FOREACH(pe, &paths, entry)
7845 free((char *)pe->path);
7846 got_pathlist_free(&paths);
7854 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
7855 "[patchfile]\n", getprogname());
7859 static const struct got_error *
7860 patch_from_stdin(int *patchfd)
7862 const struct got_error *err = NULL;
7864 char *path, buf[BUFSIZ];
7865 sig_t sighup, sigint, sigquit;
7867 err = got_opentemp_named_fd(&path, patchfd,
7868 GOT_TMPDIR_STR "/got-patch");
7874 sighup = signal(SIGHUP, SIG_DFL);
7875 sigint = signal(SIGINT, SIG_DFL);
7876 sigquit = signal(SIGQUIT, SIG_DFL);
7879 r = read(0, buf, sizeof(buf));
7881 err = got_error_from_errno("read");
7886 if (write(*patchfd, buf, r) == -1) {
7887 err = got_error_from_errno("write");
7892 signal(SIGHUP, sighup);
7893 signal(SIGINT, sigint);
7894 signal(SIGQUIT, sigquit);
7896 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7897 err = got_error_from_errno("lseek");
7907 static const struct got_error *
7908 patch_progress(void *arg, const char *old, const char *new,
7909 unsigned char status, const struct got_error *error, int old_from,
7910 int old_lines, int new_from, int new_lines, int offset,
7911 int ws_mangled, const struct got_error *hunk_err)
7913 const char *path = new == NULL ? old : new;
7915 while (*path == '/')
7919 printf("%c %s\n", status, path);
7922 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7924 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7925 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7926 old_lines, new_from, new_lines);
7927 if (hunk_err != NULL)
7928 printf("%s\n", hunk_err->msg);
7929 else if (offset != 0)
7930 printf("applied with offset %d\n", offset);
7932 printf("hunk contains mangled whitespace\n");
7938 static const struct got_error *
7939 cmd_patch(int argc, char *argv[])
7941 const struct got_error *error = NULL, *close_error = NULL;
7942 struct got_worktree *worktree = NULL;
7943 struct got_repository *repo = NULL;
7944 struct got_reflist_head refs;
7945 struct got_object_id *commit_id = NULL;
7946 const char *commit_id_str = NULL;
7950 int ch, nop = 0, strip = -1, reverse = 0;
7952 int *pack_fds = NULL;
7957 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
7958 "unveil", NULL) == -1)
7962 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
7965 commit_id_str = optarg;
7971 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7973 errx(1, "pathname strip count is %s: %s",
7989 error = patch_from_stdin(&patchfd);
7992 } else if (argc == 1) {
7993 patchfd = open(argv[0], O_RDONLY);
7994 if (patchfd == -1) {
7995 error = got_error_from_errno2("open", argv[0]);
7998 if (fstat(patchfd, &sb) == -1) {
7999 error = got_error_from_errno2("fstat", argv[0]);
8002 if (!S_ISREG(sb.st_mode)) {
8003 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8009 if ((cwd = getcwd(NULL, 0)) == NULL) {
8010 error = got_error_from_errno("getcwd");
8014 error = got_repo_pack_fds_open(&pack_fds);
8018 error = got_worktree_open(&worktree, cwd);
8022 const char *repo_path = got_worktree_get_repo_path(worktree);
8023 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8027 error = apply_unveil(got_repo_get_path(repo), 0,
8028 got_worktree_get_root_path(worktree));
8032 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8036 if (commit_id_str != NULL) {
8037 error = got_repo_match_object_id(&commit_id, NULL,
8038 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8043 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8044 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8047 got_ref_list_free(&refs);
8050 close_error = got_repo_close(repo);
8052 error = close_error;
8054 if (worktree != NULL) {
8055 close_error = got_worktree_close(worktree);
8057 error = close_error;
8060 const struct got_error *pack_err =
8061 got_repo_pack_fds_close(pack_fds);
8072 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8077 static const struct got_error *
8078 revert_progress(void *arg, unsigned char status, const char *path)
8080 if (status == GOT_STATUS_UNVERSIONED)
8083 while (path[0] == '/')
8085 printf("%c %s\n", status, path);
8089 struct choose_patch_arg {
8090 FILE *patch_script_file;
8094 static const struct got_error *
8095 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8096 int nchanges, const char *action)
8098 const struct got_error *err;
8100 size_t linesize = 0;
8104 case GOT_STATUS_ADD:
8105 printf("A %s\n%s this addition? [y/n] ", path, action);
8107 case GOT_STATUS_DELETE:
8108 printf("D %s\n%s this deletion? [y/n] ", path, action);
8110 case GOT_STATUS_MODIFY:
8111 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8112 return got_error_from_errno("fseek");
8113 printf(GOT_COMMIT_SEP_STR);
8114 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8116 if (linelen == -1 && ferror(patch_file)) {
8117 err = got_error_from_errno("getline");
8122 printf(GOT_COMMIT_SEP_STR);
8123 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8124 path, n, nchanges, action);
8127 return got_error_path(path, GOT_ERR_FILE_STATUS);
8134 static const struct got_error *
8135 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8136 FILE *patch_file, int n, int nchanges)
8138 const struct got_error *err = NULL;
8140 size_t linesize = 0;
8143 struct choose_patch_arg *a = arg;
8145 *choice = GOT_PATCH_CHOICE_NONE;
8147 if (a->patch_script_file) {
8149 err = show_change(status, path, patch_file, n, nchanges,
8153 linelen = getline(&line, &linesize, a->patch_script_file);
8154 if (linelen == -1) {
8155 if (ferror(a->patch_script_file))
8156 return got_error_from_errno("getline");
8159 nl = strchr(line, '\n');
8162 if (strcmp(line, "y") == 0) {
8163 *choice = GOT_PATCH_CHOICE_YES;
8165 } else if (strcmp(line, "n") == 0) {
8166 *choice = GOT_PATCH_CHOICE_NO;
8168 } else if (strcmp(line, "q") == 0 &&
8169 status == GOT_STATUS_MODIFY) {
8170 *choice = GOT_PATCH_CHOICE_QUIT;
8173 printf("invalid response '%s'\n", line);
8178 while (resp != 'y' && resp != 'n' && resp != 'q') {
8179 err = show_change(status, path, patch_file, n, nchanges,
8186 if (status == GOT_STATUS_MODIFY) {
8187 if (resp != 'y' && resp != 'n' && resp != 'q') {
8188 printf("invalid response '%c'\n", resp);
8191 } else if (resp != 'y' && resp != 'n') {
8192 printf("invalid response '%c'\n", resp);
8198 *choice = GOT_PATCH_CHOICE_YES;
8199 else if (resp == 'n')
8200 *choice = GOT_PATCH_CHOICE_NO;
8201 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8202 *choice = GOT_PATCH_CHOICE_QUIT;
8207 static const struct got_error *
8208 cmd_revert(int argc, char *argv[])
8210 const struct got_error *error = NULL;
8211 struct got_worktree *worktree = NULL;
8212 struct got_repository *repo = NULL;
8213 char *cwd = NULL, *path = NULL;
8214 struct got_pathlist_head paths;
8215 struct got_pathlist_entry *pe;
8216 int ch, can_recurse = 0, pflag = 0;
8217 FILE *patch_script_file = NULL;
8218 const char *patch_script_path = NULL;
8219 struct choose_patch_arg cpa;
8220 int *pack_fds = NULL;
8224 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8230 patch_script_path = optarg;
8245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8246 "unveil", NULL) == -1)
8251 if (patch_script_path && !pflag)
8252 errx(1, "-F option can only be used together with -p option");
8254 cwd = getcwd(NULL, 0);
8256 error = got_error_from_errno("getcwd");
8260 error = got_repo_pack_fds_open(&pack_fds);
8264 error = got_worktree_open(&worktree, cwd);
8266 if (error->code == GOT_ERR_NOT_WORKTREE)
8267 error = wrap_not_worktree_error(error, "revert", cwd);
8271 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8276 if (patch_script_path) {
8277 patch_script_file = fopen(patch_script_path, "re");
8278 if (patch_script_file == NULL) {
8279 error = got_error_from_errno2("fopen",
8284 error = apply_unveil(got_repo_get_path(repo), 1,
8285 got_worktree_get_root_path(worktree));
8289 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8296 TAILQ_FOREACH(pe, &paths, entry) {
8297 if (asprintf(&ondisk_path, "%s/%s",
8298 got_worktree_get_root_path(worktree),
8300 error = got_error_from_errno("asprintf");
8303 if (lstat(ondisk_path, &sb) == -1) {
8304 if (errno == ENOENT) {
8308 error = got_error_from_errno2("lstat",
8314 if (S_ISDIR(sb.st_mode)) {
8315 error = got_error_msg(GOT_ERR_BAD_PATH,
8316 "reverting directories requires -R option");
8322 cpa.patch_script_file = patch_script_file;
8323 cpa.action = "revert";
8324 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8325 pflag ? choose_patch : NULL, &cpa, repo);
8327 if (patch_script_file && fclose(patch_script_file) == EOF &&
8329 error = got_error_from_errno2("fclose", patch_script_path);
8331 const struct got_error *close_err = got_repo_close(repo);
8336 got_worktree_close(worktree);
8338 const struct got_error *pack_err =
8339 got_repo_pack_fds_close(pack_fds);
8351 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8352 "[-m message] [path ...]\n", getprogname());
8356 struct collect_commit_logmsg_arg {
8357 const char *cmdline_log;
8358 const char *prepared_log;
8359 int non_interactive;
8361 const char *worktree_path;
8362 const char *branch_name;
8363 const char *repo_path;
8368 static const struct got_error *
8369 read_prepared_logmsg(char **logmsg, const char *path)
8371 const struct got_error *err = NULL;
8377 memset(&sb, 0, sizeof(sb));
8379 f = fopen(path, "re");
8381 return got_error_from_errno2("fopen", path);
8383 if (fstat(fileno(f), &sb) == -1) {
8384 err = got_error_from_errno2("fstat", path);
8387 if (sb.st_size == 0) {
8388 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8392 *logmsg = malloc(sb.st_size + 1);
8393 if (*logmsg == NULL) {
8394 err = got_error_from_errno("malloc");
8398 r = fread(*logmsg, 1, sb.st_size, f);
8399 if (r != sb.st_size) {
8401 err = got_error_from_errno2("fread", path);
8403 err = got_error(GOT_ERR_IO);
8406 (*logmsg)[sb.st_size] = '\0';
8408 if (fclose(f) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", path);
8417 static const struct got_error *
8418 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8421 char *initial_content = NULL;
8422 struct got_pathlist_entry *pe;
8423 const struct got_error *err = NULL;
8424 char *template = NULL;
8425 struct collect_commit_logmsg_arg *a = arg;
8426 int initial_content_len;
8430 /* if a message was specified on the command line, just use it */
8431 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8432 len = strlen(a->cmdline_log) + 1;
8433 *logmsg = malloc(len + 1);
8434 if (*logmsg == NULL)
8435 return got_error_from_errno("malloc");
8436 strlcpy(*logmsg, a->cmdline_log, len);
8438 } else if (a->prepared_log != NULL && a->non_interactive)
8439 return read_prepared_logmsg(logmsg, a->prepared_log);
8441 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8442 return got_error_from_errno("asprintf");
8444 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8448 if (a->prepared_log) {
8450 err = read_prepared_logmsg(&msg, a->prepared_log);
8453 if (write(fd, msg, strlen(msg)) == -1) {
8454 err = got_error_from_errno2("write", a->logmsg_path);
8461 initial_content_len = asprintf(&initial_content,
8462 "\n# changes to be committed on branch %s:\n",
8464 if (initial_content_len == -1) {
8465 err = got_error_from_errno("asprintf");
8469 if (write(fd, initial_content, initial_content_len) == -1) {
8470 err = got_error_from_errno2("write", a->logmsg_path);
8474 TAILQ_FOREACH(pe, commitable_paths, entry) {
8475 struct got_commitable *ct = pe->data;
8476 dprintf(fd, "# %c %s\n",
8477 got_commitable_get_status(ct),
8478 got_commitable_get_path(ct));
8481 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8482 initial_content_len, a->prepared_log ? 0 : 1);
8484 free(initial_content);
8487 if (fd != -1 && close(fd) == -1 && err == NULL)
8488 err = got_error_from_errno2("close", a->logmsg_path);
8490 /* Editor is done; we can now apply unveil(2) */
8492 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8500 static const struct got_error *
8501 cmd_commit(int argc, char *argv[])
8503 const struct got_error *error = NULL;
8504 struct got_worktree *worktree = NULL;
8505 struct got_repository *repo = NULL;
8506 char *cwd = NULL, *id_str = NULL;
8507 struct got_object_id *id = NULL;
8508 const char *logmsg = NULL;
8509 char *prepared_logmsg = NULL;
8510 struct collect_commit_logmsg_arg cl_arg;
8511 const char *author = NULL;
8512 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
8513 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8514 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8515 struct got_pathlist_head paths;
8516 int *pack_fds = NULL;
8519 cl_arg.logmsg_path = NULL;
8521 while ((ch = getopt(argc, argv, "A:F:m:NS")) != -1) {
8525 error = valid_author(author);
8531 option_conflict('F', 'm');
8532 prepared_logmsg = realpath(optarg, NULL);
8533 if (prepared_logmsg == NULL)
8534 return got_error_from_errno2("realpath",
8538 if (prepared_logmsg)
8539 option_conflict('m', 'F');
8543 non_interactive = 1;
8546 allow_bad_symlinks = 1;
8558 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8559 "unveil", NULL) == -1)
8562 cwd = getcwd(NULL, 0);
8564 error = got_error_from_errno("getcwd");
8568 error = got_repo_pack_fds_open(&pack_fds);
8572 error = got_worktree_open(&worktree, cwd);
8574 if (error->code == GOT_ERR_NOT_WORKTREE)
8575 error = wrap_not_worktree_error(error, "commit", cwd);
8579 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8582 if (rebase_in_progress) {
8583 error = got_error(GOT_ERR_REBASING);
8587 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8592 error = get_gitconfig_path(&gitconfig_path);
8595 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8596 gitconfig_path, pack_fds);
8600 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8603 if (merge_in_progress) {
8604 error = got_error(GOT_ERR_MERGE_BUSY);
8608 error = get_author(&committer, repo, worktree);
8612 if (author != NULL && !strcmp(committer, author)) {
8613 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
8621 * unveil(2) traverses exec(2); if an editor is used we have
8622 * to apply unveil after the log message has been written.
8624 if (logmsg == NULL || strlen(logmsg) == 0)
8625 error = get_editor(&editor);
8627 error = apply_unveil(got_repo_get_path(repo), 0,
8628 got_worktree_get_root_path(worktree));
8632 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8636 cl_arg.editor = editor;
8637 cl_arg.cmdline_log = logmsg;
8638 cl_arg.prepared_log = prepared_logmsg;
8639 cl_arg.non_interactive = non_interactive;
8640 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8641 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8642 if (!histedit_in_progress) {
8643 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8644 error = got_error(GOT_ERR_COMMIT_BRANCH);
8647 cl_arg.branch_name += 11;
8649 cl_arg.repo_path = got_repo_get_path(repo);
8650 error = got_worktree_commit(&id, worktree, &paths, author, committer,
8651 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8652 print_status, NULL, repo);
8654 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8655 cl_arg.logmsg_path != NULL)
8656 preserve_logmsg = 1;
8660 error = got_object_id_str(&id_str, id);
8663 printf("Created commit %s\n", id_str);
8665 if (preserve_logmsg) {
8666 fprintf(stderr, "%s: log message preserved in %s\n",
8667 getprogname(), cl_arg.logmsg_path);
8668 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8670 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8671 free(cl_arg.logmsg_path);
8673 const struct got_error *close_err = got_repo_close(repo);
8678 got_worktree_close(worktree);
8680 const struct got_error *pack_err =
8681 got_repo_pack_fds_close(pack_fds);
8687 free(gitconfig_path);
8690 free(prepared_logmsg);
8697 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
8698 "[-r repository-path] [-t tag] [remote-repository]\n",
8704 print_load_info(int print_colored, int print_found, int print_trees,
8705 int ncolored, int nfound, int ntrees)
8707 if (print_colored) {
8708 printf("%d commit%s colored", ncolored,
8709 ncolored == 1 ? "" : "s");
8712 printf("%s%d object%s found",
8713 ncolored > 0 ? "; " : "",
8714 nfound, nfound == 1 ? "" : "s");
8717 printf("; %d tree%s scanned", ntrees,
8718 ntrees == 1 ? "" : "s");
8722 struct got_send_progress_arg {
8723 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8730 int last_nobj_total;
8734 int printed_something;
8736 struct got_pathlist_head *delete_branches;
8739 static const struct got_error *
8740 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8741 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8742 int nobj_written, off_t bytes_sent, const char *refname, int success)
8744 struct got_send_progress_arg *a = arg;
8745 char scaled_packsize[FMT_SCALED_STRSIZE];
8746 char scaled_sent[FMT_SCALED_STRSIZE];
8747 int p_deltify = 0, p_written = 0, p_sent = 0;
8748 int print_colored = 0, print_found = 0, print_trees = 0;
8749 int print_searching = 0, print_total = 0;
8750 int print_deltify = 0, print_written = 0, print_sent = 0;
8752 if (a->verbosity < 0)
8756 const char *status = success ? "accepted" : "rejected";
8759 struct got_pathlist_entry *pe;
8760 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8761 const char *branchname = pe->path;
8762 if (got_path_cmp(branchname, refname,
8763 strlen(branchname), strlen(refname)) == 0) {
8765 a->sent_something = 1;
8771 if (a->printed_something)
8773 printf("Server has %s %s", status, refname);
8774 a->printed_something = 1;
8778 if (a->last_ncolored != ncolored) {
8780 a->last_ncolored = ncolored;
8783 if (a->last_nfound != nfound) {
8786 a->last_nfound = nfound;
8789 if (a->last_ntrees != ntrees) {
8793 a->last_ntrees = ntrees;
8796 if ((print_colored || print_found || print_trees) &&
8799 print_load_info(print_colored, print_found, print_trees,
8800 ncolored, nfound, ntrees);
8801 a->printed_something = 1;
8804 } else if (!a->loading_done) {
8806 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8808 a->loading_done = 1;
8811 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8812 return got_error_from_errno("fmt_scaled");
8813 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8814 return got_error_from_errno("fmt_scaled");
8816 if (a->last_ncommits != ncommits) {
8817 print_searching = 1;
8818 a->last_ncommits = ncommits;
8821 if (a->last_nobj_total != nobj_total) {
8822 print_searching = 1;
8824 a->last_nobj_total = nobj_total;
8827 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8828 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8829 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8830 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8831 return got_error(GOT_ERR_NO_SPACE);
8834 if (nobj_deltify > 0 || nobj_written > 0) {
8835 if (nobj_deltify > 0) {
8836 p_deltify = (nobj_deltify * 100) / nobj_total;
8837 if (p_deltify != a->last_p_deltify) {
8838 a->last_p_deltify = p_deltify;
8839 print_searching = 1;
8844 if (nobj_written > 0) {
8845 p_written = (nobj_written * 100) / nobj_total;
8846 if (p_written != a->last_p_written) {
8847 a->last_p_written = p_written;
8848 print_searching = 1;
8856 if (bytes_sent > 0) {
8857 p_sent = (bytes_sent * 100) / packfile_size;
8858 if (p_sent != a->last_p_sent) {
8859 a->last_p_sent = p_sent;
8860 print_searching = 1;
8866 a->sent_something = 1;
8869 if (print_searching || print_total || print_deltify || print_written ||
8872 if (print_searching)
8873 printf("packing %d reference%s", ncommits,
8874 ncommits == 1 ? "" : "s");
8876 printf("; %d object%s", nobj_total,
8877 nobj_total == 1 ? "" : "s");
8879 printf("; deltify: %d%%", p_deltify);
8881 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8882 scaled_packsize, p_sent);
8883 else if (print_written)
8884 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8885 scaled_packsize, p_written);
8886 if (print_searching || print_total || print_deltify ||
8887 print_written || print_sent) {
8888 a->printed_something = 1;
8894 static const struct got_error *
8895 cmd_send(int argc, char *argv[])
8897 const struct got_error *error = NULL;
8898 char *cwd = NULL, *repo_path = NULL;
8899 const char *remote_name;
8900 char *proto = NULL, *host = NULL, *port = NULL;
8901 char *repo_name = NULL, *server_path = NULL;
8902 const struct got_remote_repo *remotes, *remote = NULL;
8903 int nremotes, nbranches = 0, ndelete_branches = 0;
8904 struct got_repository *repo = NULL;
8905 struct got_worktree *worktree = NULL;
8906 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8907 struct got_pathlist_head branches;
8908 struct got_pathlist_head tags;
8909 struct got_reflist_head all_branches;
8910 struct got_reflist_head all_tags;
8911 struct got_pathlist_head delete_args;
8912 struct got_pathlist_head delete_branches;
8913 struct got_reflist_entry *re;
8914 struct got_pathlist_entry *pe;
8915 int i, ch, sendfd = -1, sendstatus;
8917 struct got_send_progress_arg spa;
8918 int verbosity = 0, overwrite_refs = 0;
8919 int send_all_branches = 0, send_all_tags = 0;
8920 struct got_reference *ref = NULL;
8921 int *pack_fds = NULL;
8923 TAILQ_INIT(&branches);
8925 TAILQ_INIT(&all_branches);
8926 TAILQ_INIT(&all_tags);
8927 TAILQ_INIT(&delete_args);
8928 TAILQ_INIT(&delete_branches);
8930 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8933 send_all_branches = 1;
8936 error = got_pathlist_append(&branches, optarg, NULL);
8942 error = got_pathlist_append(&delete_args, optarg, NULL);
8950 repo_path = realpath(optarg, NULL);
8951 if (repo_path == NULL)
8952 return got_error_from_errno2("realpath",
8954 got_path_strip_trailing_slashes(repo_path);
8957 error = got_pathlist_append(&tags, optarg, NULL);
8967 else if (verbosity < 3)
8981 if (send_all_branches && !TAILQ_EMPTY(&branches))
8982 option_conflict('a', 'b');
8983 if (send_all_tags && !TAILQ_EMPTY(&tags))
8984 option_conflict('T', 't');
8988 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8990 remote_name = argv[0];
8994 cwd = getcwd(NULL, 0);
8996 error = got_error_from_errno("getcwd");
9000 error = got_repo_pack_fds_open(&pack_fds);
9004 if (repo_path == NULL) {
9005 error = got_worktree_open(&worktree, cwd);
9006 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9012 strdup(got_worktree_get_repo_path(worktree));
9013 if (repo_path == NULL)
9014 error = got_error_from_errno("strdup");
9018 repo_path = strdup(cwd);
9019 if (repo_path == NULL) {
9020 error = got_error_from_errno("strdup");
9026 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9031 worktree_conf = got_worktree_get_gotconfig(worktree);
9032 if (worktree_conf) {
9033 got_gotconfig_get_remotes(&nremotes, &remotes,
9035 for (i = 0; i < nremotes; i++) {
9036 if (strcmp(remotes[i].name, remote_name) == 0) {
9037 remote = &remotes[i];
9043 if (remote == NULL) {
9044 repo_conf = got_repo_get_gotconfig(repo);
9046 got_gotconfig_get_remotes(&nremotes, &remotes,
9048 for (i = 0; i < nremotes; i++) {
9049 if (strcmp(remotes[i].name, remote_name) == 0) {
9050 remote = &remotes[i];
9056 if (remote == NULL) {
9057 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9058 for (i = 0; i < nremotes; i++) {
9059 if (strcmp(remotes[i].name, remote_name) == 0) {
9060 remote = &remotes[i];
9065 if (remote == NULL) {
9066 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9070 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9071 &repo_name, remote->send_url);
9075 if (strcmp(proto, "git") == 0) {
9077 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9078 "sendfd dns inet unveil", NULL) == -1)
9081 } else if (strcmp(proto, "git+ssh") == 0 ||
9082 strcmp(proto, "ssh") == 0) {
9084 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9085 "sendfd unveil", NULL) == -1)
9088 } else if (strcmp(proto, "http") == 0 ||
9089 strcmp(proto, "git+http") == 0) {
9090 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9093 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9097 error = got_dial_apply_unveil(proto);
9101 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9105 if (send_all_branches) {
9106 error = got_ref_list(&all_branches, repo, "refs/heads",
9107 got_ref_cmp_by_name, NULL);
9110 TAILQ_FOREACH(re, &all_branches, entry) {
9111 const char *branchname = got_ref_get_name(re->ref);
9112 error = got_pathlist_append(&branches,
9118 } else if (nbranches == 0) {
9119 for (i = 0; i < remote->nsend_branches; i++) {
9120 got_pathlist_append(&branches,
9121 remote->send_branches[i], NULL);
9125 if (send_all_tags) {
9126 error = got_ref_list(&all_tags, repo, "refs/tags",
9127 got_ref_cmp_by_name, NULL);
9130 TAILQ_FOREACH(re, &all_tags, entry) {
9131 const char *tagname = got_ref_get_name(re->ref);
9132 error = got_pathlist_append(&tags,
9140 * To prevent accidents only branches in refs/heads/ can be deleted
9141 * with 'got send -d'.
9142 * Deleting anything else requires local repository access or Git.
9144 TAILQ_FOREACH(pe, &delete_args, entry) {
9145 const char *branchname = pe->path;
9147 struct got_pathlist_entry *new;
9148 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9149 s = strdup(branchname);
9151 error = got_error_from_errno("strdup");
9155 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9156 error = got_error_from_errno("asprintf");
9160 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9161 if (error || new == NULL /* duplicate */)
9168 if (nbranches == 0 && ndelete_branches == 0) {
9169 struct got_reference *head_ref;
9171 error = got_ref_open(&head_ref, repo,
9172 got_worktree_get_head_ref_name(worktree), 0);
9174 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9177 if (got_ref_is_symbolic(head_ref)) {
9178 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9179 got_ref_close(head_ref);
9184 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9192 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9193 port ? ":" : "", port ? port : "");
9195 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9196 server_path, verbosity);
9200 memset(&spa, 0, sizeof(spa));
9201 spa.last_scaled_packsize[0] = '\0';
9202 spa.last_p_deltify = -1;
9203 spa.last_p_written = -1;
9204 spa.verbosity = verbosity;
9205 spa.delete_branches = &delete_branches;
9206 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9207 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9208 check_cancelled, NULL);
9209 if (spa.printed_something)
9213 if (!spa.sent_something && verbosity >= 0)
9214 printf("Already up-to-date\n");
9217 if (kill(sendpid, SIGTERM) == -1)
9218 error = got_error_from_errno("kill");
9219 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9220 error = got_error_from_errno("waitpid");
9222 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9223 error = got_error_from_errno("close");
9225 const struct got_error *close_err = got_repo_close(repo);
9230 got_worktree_close(worktree);
9232 const struct got_error *pack_err =
9233 got_repo_pack_fds_close(pack_fds);
9239 got_pathlist_free(&branches);
9240 got_pathlist_free(&tags);
9241 got_ref_list_free(&all_branches);
9242 got_ref_list_free(&all_tags);
9243 got_pathlist_free(&delete_args);
9244 TAILQ_FOREACH(pe, &delete_branches, entry)
9245 free((char *)pe->path);
9246 got_pathlist_free(&delete_branches);
9258 usage_cherrypick(void)
9260 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9264 static const struct got_error *
9265 cmd_cherrypick(int argc, char *argv[])
9267 const struct got_error *error = NULL;
9268 struct got_worktree *worktree = NULL;
9269 struct got_repository *repo = NULL;
9270 char *cwd = NULL, *commit_id_str = NULL;
9271 struct got_object_id *commit_id = NULL;
9272 struct got_commit_object *commit = NULL;
9273 struct got_object_qid *pid;
9275 struct got_update_progress_arg upa;
9276 int *pack_fds = NULL;
9278 while ((ch = getopt(argc, argv, "")) != -1) {
9290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9291 "unveil", NULL) == -1)
9297 cwd = getcwd(NULL, 0);
9299 error = got_error_from_errno("getcwd");
9303 error = got_repo_pack_fds_open(&pack_fds);
9307 error = got_worktree_open(&worktree, cwd);
9309 if (error->code == GOT_ERR_NOT_WORKTREE)
9310 error = wrap_not_worktree_error(error, "cherrypick",
9315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9320 error = apply_unveil(got_repo_get_path(repo), 0,
9321 got_worktree_get_root_path(worktree));
9325 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9326 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9329 error = got_object_id_str(&commit_id_str, commit_id);
9333 error = got_object_open_as_commit(&commit, repo, commit_id);
9336 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9337 memset(&upa, 0, sizeof(upa));
9338 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9339 commit_id, repo, update_progress, &upa, check_cancelled,
9344 if (upa.did_something)
9345 printf("Merged commit %s\n", commit_id_str);
9346 print_merge_progress_stats(&upa);
9349 got_object_commit_close(commit);
9350 free(commit_id_str);
9352 got_worktree_close(worktree);
9354 const struct got_error *close_err = got_repo_close(repo);
9359 const struct got_error *pack_err =
9360 got_repo_pack_fds_close(pack_fds);
9371 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9375 static const struct got_error *
9376 cmd_backout(int argc, char *argv[])
9378 const struct got_error *error = NULL;
9379 struct got_worktree *worktree = NULL;
9380 struct got_repository *repo = NULL;
9381 char *cwd = NULL, *commit_id_str = NULL;
9382 struct got_object_id *commit_id = NULL;
9383 struct got_commit_object *commit = NULL;
9384 struct got_object_qid *pid;
9386 struct got_update_progress_arg upa;
9387 int *pack_fds = NULL;
9389 while ((ch = getopt(argc, argv, "")) != -1) {
9401 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9402 "unveil", NULL) == -1)
9408 cwd = getcwd(NULL, 0);
9410 error = got_error_from_errno("getcwd");
9414 error = got_repo_pack_fds_open(&pack_fds);
9418 error = got_worktree_open(&worktree, cwd);
9420 if (error->code == GOT_ERR_NOT_WORKTREE)
9421 error = wrap_not_worktree_error(error, "backout", cwd);
9425 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9430 error = apply_unveil(got_repo_get_path(repo), 0,
9431 got_worktree_get_root_path(worktree));
9435 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9436 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9439 error = got_object_id_str(&commit_id_str, commit_id);
9443 error = got_object_open_as_commit(&commit, repo, commit_id);
9446 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9448 error = got_error(GOT_ERR_ROOT_COMMIT);
9452 memset(&upa, 0, sizeof(upa));
9453 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9454 repo, update_progress, &upa, check_cancelled, NULL);
9458 if (upa.did_something)
9459 printf("Backed out commit %s\n", commit_id_str);
9460 print_merge_progress_stats(&upa);
9463 got_object_commit_close(commit);
9464 free(commit_id_str);
9466 got_worktree_close(worktree);
9468 const struct got_error *close_err = got_repo_close(repo);
9473 const struct got_error *pack_err =
9474 got_repo_pack_fds_close(pack_fds);
9484 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
9489 trim_logmsg(char *logmsg, int limit)
9494 len = strlen(logmsg);
9498 nl = strchr(logmsg, '\n');
9503 static const struct got_error *
9504 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9506 const struct got_error *err;
9507 char *logmsg0 = NULL;
9510 err = got_object_commit_get_logmsg(&logmsg0, commit);
9515 while (isspace((unsigned char)s[0]))
9518 *logmsg = strdup(s);
9519 if (*logmsg == NULL) {
9520 err = got_error_from_errno("strdup");
9524 trim_logmsg(*logmsg, limit);
9530 static const struct got_error *
9531 show_rebase_merge_conflict(struct got_object_id *id,
9532 struct got_repository *repo)
9534 const struct got_error *err;
9535 struct got_commit_object *commit = NULL;
9536 char *id_str = NULL, *logmsg = NULL;
9538 err = got_object_open_as_commit(&commit, repo, id);
9542 err = got_object_id_str(&id_str, id);
9548 err = get_short_logmsg(&logmsg, 42, commit);
9552 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9555 got_object_commit_close(commit);
9560 static const struct got_error *
9561 show_rebase_progress(struct got_commit_object *commit,
9562 struct got_object_id *old_id, struct got_object_id *new_id)
9564 const struct got_error *err;
9565 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9567 err = got_object_id_str(&old_id_str, old_id);
9572 err = got_object_id_str(&new_id_str, new_id);
9577 old_id_str[12] = '\0';
9579 new_id_str[12] = '\0';
9581 err = get_short_logmsg(&logmsg, 42, commit);
9585 printf("%s -> %s: %s\n", old_id_str,
9586 new_id_str ? new_id_str : "no-op change", logmsg);
9594 static const struct got_error *
9595 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9596 struct got_reference *branch, struct got_reference *new_base_branch,
9597 struct got_reference *tmp_branch, struct got_repository *repo,
9600 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9601 return got_worktree_rebase_complete(worktree, fileindex,
9602 new_base_branch, tmp_branch, branch, repo, create_backup);
9605 static const struct got_error *
9606 rebase_commit(struct got_pathlist_head *merged_paths,
9607 struct got_worktree *worktree, struct got_fileindex *fileindex,
9608 struct got_reference *tmp_branch, const char *committer,
9609 struct got_object_id *commit_id, struct got_repository *repo)
9611 const struct got_error *error;
9612 struct got_commit_object *commit;
9613 struct got_object_id *new_commit_id;
9615 error = got_object_open_as_commit(&commit, repo, commit_id);
9619 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9620 worktree, fileindex, tmp_branch, committer, commit, commit_id,
9623 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9625 error = show_rebase_progress(commit, commit_id, NULL);
9627 error = show_rebase_progress(commit, commit_id, new_commit_id);
9628 free(new_commit_id);
9631 got_object_commit_close(commit);
9635 struct check_path_prefix_arg {
9636 const char *path_prefix;
9641 static const struct got_error *
9642 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9643 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9644 struct got_object_id *id1, struct got_object_id *id2,
9645 const char *path1, const char *path2,
9646 mode_t mode1, mode_t mode2, struct got_repository *repo)
9648 struct check_path_prefix_arg *a = arg;
9650 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9651 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9652 return got_error(a->errcode);
9657 static const struct got_error *
9658 check_path_prefix(struct got_object_id *parent_id,
9659 struct got_object_id *commit_id, const char *path_prefix,
9660 int errcode, struct got_repository *repo)
9662 const struct got_error *err;
9663 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9664 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9665 struct check_path_prefix_arg cpp_arg;
9667 if (got_path_is_root_dir(path_prefix))
9670 err = got_object_open_as_commit(&commit, repo, commit_id);
9674 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9678 err = got_object_open_as_tree(&tree1, repo,
9679 got_object_commit_get_tree_id(parent_commit));
9683 err = got_object_open_as_tree(&tree2, repo,
9684 got_object_commit_get_tree_id(commit));
9688 cpp_arg.path_prefix = path_prefix;
9689 while (cpp_arg.path_prefix[0] == '/')
9690 cpp_arg.path_prefix++;
9691 cpp_arg.len = strlen(cpp_arg.path_prefix);
9692 cpp_arg.errcode = errcode;
9693 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9694 check_path_prefix_in_diff, &cpp_arg, 0);
9697 got_object_tree_close(tree1);
9699 got_object_tree_close(tree2);
9701 got_object_commit_close(commit);
9703 got_object_commit_close(parent_commit);
9707 static const struct got_error *
9708 collect_commits(struct got_object_id_queue *commits,
9709 struct got_object_id *initial_commit_id,
9710 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9711 const char *path_prefix, int path_prefix_errcode,
9712 struct got_repository *repo)
9714 const struct got_error *err = NULL;
9715 struct got_commit_graph *graph = NULL;
9716 struct got_object_id parent_id, commit_id;
9717 struct got_object_qid *qid;
9719 err = got_commit_graph_open(&graph, "/", 1);
9723 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9724 check_cancelled, NULL);
9728 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
9729 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
9730 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9731 check_cancelled, NULL);
9733 if (err->code == GOT_ERR_ITER_COMPLETED) {
9734 err = got_error_msg(GOT_ERR_ANCESTRY,
9735 "ran out of commits to rebase before "
9736 "youngest common ancestor commit has "
9741 err = check_path_prefix(&parent_id, &commit_id,
9742 path_prefix, path_prefix_errcode, repo);
9746 err = got_object_qid_alloc(&qid, &commit_id);
9749 STAILQ_INSERT_HEAD(commits, qid, entry);
9751 memcpy(&commit_id, &parent_id, sizeof(commit_id));
9755 got_commit_graph_close(graph);
9759 static const struct got_error *
9760 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9762 const struct got_error *err = NULL;
9763 time_t committer_time;
9765 char datebuf[11]; /* YYYY-MM-DD + NUL */
9766 char *author0 = NULL, *author, *smallerthan;
9767 char *logmsg0 = NULL, *logmsg, *newline;
9769 committer_time = got_object_commit_get_committer_time(commit);
9770 if (gmtime_r(&committer_time, &tm) == NULL)
9771 return got_error_from_errno("gmtime_r");
9772 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9773 return got_error(GOT_ERR_NO_SPACE);
9775 author0 = strdup(got_object_commit_get_author(commit));
9776 if (author0 == NULL)
9777 return got_error_from_errno("strdup");
9779 smallerthan = strchr(author, '<');
9780 if (smallerthan && smallerthan[1] != '\0')
9781 author = smallerthan + 1;
9782 author[strcspn(author, "@>")] = '\0';
9784 err = got_object_commit_get_logmsg(&logmsg0, commit);
9788 while (*logmsg == '\n')
9790 newline = strchr(logmsg, '\n');
9794 if (asprintf(brief_str, "%s %s %s",
9795 datebuf, author, logmsg) == -1)
9796 err = got_error_from_errno("asprintf");
9803 static const struct got_error *
9804 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9805 struct got_repository *repo)
9807 const struct got_error *err;
9810 err = got_object_id_str(&id_str, id);
9814 err = got_ref_delete(ref, repo);
9818 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9824 static const struct got_error *
9825 print_backup_ref(const char *branch_name, const char *new_id_str,
9826 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9827 struct got_reflist_object_id_map *refs_idmap,
9828 struct got_repository *repo)
9830 const struct got_error *err = NULL;
9831 struct got_reflist_head *refs;
9832 char *refs_str = NULL;
9833 struct got_object_id *new_commit_id = NULL;
9834 struct got_commit_object *new_commit = NULL;
9835 char *new_commit_brief_str = NULL;
9836 struct got_object_id *yca_id = NULL;
9837 struct got_commit_object *yca_commit = NULL;
9838 char *yca_id_str = NULL, *yca_brief_str = NULL;
9839 char *custom_refs_str;
9841 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9842 return got_error_from_errno("asprintf");
9844 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9845 0, 0, refs_idmap, custom_refs_str);
9849 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9853 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9855 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9860 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9864 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9868 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9869 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9873 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9874 refs_str ? " (" : "", refs_str ? refs_str : "",
9875 refs_str ? ")" : "", new_commit_brief_str);
9876 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9877 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9881 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9885 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9889 err = got_object_id_str(&yca_id_str, yca_id);
9893 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9895 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9899 printf("history forked at %s%s%s%s\n %s\n",
9901 refs_str ? " (" : "", refs_str ? refs_str : "",
9902 refs_str ? ")" : "", yca_brief_str);
9905 free(custom_refs_str);
9906 free(new_commit_id);
9910 free(yca_brief_str);
9912 got_object_commit_close(new_commit);
9914 got_object_commit_close(yca_commit);
9919 static const struct got_error *
9920 process_backup_refs(const char *backup_ref_prefix,
9921 const char *wanted_branch_name,
9922 int delete, struct got_repository *repo)
9924 const struct got_error *err;
9925 struct got_reflist_head refs, backup_refs;
9926 struct got_reflist_entry *re;
9927 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9928 struct got_object_id *old_commit_id = NULL;
9929 char *branch_name = NULL;
9930 struct got_commit_object *old_commit = NULL;
9931 struct got_reflist_object_id_map *refs_idmap = NULL;
9932 int wanted_branch_found = 0;
9935 TAILQ_INIT(&backup_refs);
9937 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9941 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9945 if (wanted_branch_name) {
9946 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9947 wanted_branch_name += 11;
9950 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9951 got_ref_cmp_by_commit_timestamp_descending, repo);
9955 TAILQ_FOREACH(re, &backup_refs, entry) {
9956 const char *refname = got_ref_get_name(re->ref);
9959 err = check_cancelled(NULL);
9963 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9967 err = got_object_open_as_commit(&old_commit, repo,
9972 if (strncmp(backup_ref_prefix, refname,
9973 backup_ref_prefix_len) == 0)
9974 refname += backup_ref_prefix_len;
9976 while (refname[0] == '/')
9979 branch_name = strdup(refname);
9980 if (branch_name == NULL) {
9981 err = got_error_from_errno("strdup");
9984 slash = strrchr(branch_name, '/');
9987 refname += strlen(branch_name) + 1;
9990 if (wanted_branch_name == NULL ||
9991 strcmp(wanted_branch_name, branch_name) == 0) {
9992 wanted_branch_found = 1;
9994 err = delete_backup_ref(re->ref,
9995 old_commit_id, repo);
9997 err = print_backup_ref(branch_name, refname,
9998 old_commit_id, old_commit, refs_idmap,
10005 free(old_commit_id);
10006 old_commit_id = NULL;
10008 branch_name = NULL;
10009 got_object_commit_close(old_commit);
10013 if (wanted_branch_name && !wanted_branch_found) {
10014 err = got_error_fmt(GOT_ERR_NOT_REF,
10015 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10019 got_reflist_object_id_map_free(refs_idmap);
10020 got_ref_list_free(&refs);
10021 got_ref_list_free(&backup_refs);
10022 free(old_commit_id);
10025 got_object_commit_close(old_commit);
10029 static const struct got_error *
10030 abort_progress(void *arg, unsigned char status, const char *path)
10033 * Unversioned files should not clutter progress output when
10034 * an operation is aborted.
10036 if (status == GOT_STATUS_UNVERSIONED)
10039 return update_progress(arg, status, path);
10042 static const struct got_error *
10043 cmd_rebase(int argc, char *argv[])
10045 const struct got_error *error = NULL;
10046 struct got_worktree *worktree = NULL;
10047 struct got_repository *repo = NULL;
10048 struct got_fileindex *fileindex = NULL;
10049 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10050 struct got_reference *branch = NULL;
10051 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10052 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10053 struct got_object_id *resume_commit_id = NULL;
10054 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10055 struct got_commit_object *commit = NULL;
10056 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10057 int histedit_in_progress = 0, merge_in_progress = 0;
10058 int create_backup = 1, list_backups = 0, delete_backups = 0;
10059 struct got_object_id_queue commits;
10060 struct got_pathlist_head merged_paths;
10061 const struct got_object_id_queue *parent_ids;
10062 struct got_object_qid *qid, *pid;
10063 struct got_update_progress_arg upa;
10064 int *pack_fds = NULL;
10066 STAILQ_INIT(&commits);
10067 TAILQ_INIT(&merged_paths);
10068 memset(&upa, 0, sizeof(upa));
10070 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10076 continue_rebase = 1;
10082 delete_backups = 1;
10094 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10095 "unveil", NULL) == -1)
10098 if (list_backups) {
10100 option_conflict('l', 'a');
10101 if (continue_rebase)
10102 option_conflict('l', 'c');
10103 if (delete_backups)
10104 option_conflict('l', 'X');
10105 if (argc != 0 && argc != 1)
10107 } else if (delete_backups) {
10109 option_conflict('X', 'a');
10110 if (continue_rebase)
10111 option_conflict('X', 'c');
10113 option_conflict('l', 'X');
10114 if (argc != 0 && argc != 1)
10117 if (abort_rebase && continue_rebase)
10119 else if (abort_rebase || continue_rebase) {
10122 } else if (argc != 1)
10126 cwd = getcwd(NULL, 0);
10128 error = got_error_from_errno("getcwd");
10132 error = got_repo_pack_fds_open(&pack_fds);
10136 error = got_worktree_open(&worktree, cwd);
10138 if (list_backups || delete_backups) {
10139 if (error->code != GOT_ERR_NOT_WORKTREE)
10142 if (error->code == GOT_ERR_NOT_WORKTREE)
10143 error = wrap_not_worktree_error(error,
10149 error = get_gitconfig_path(&gitconfig_path);
10152 error = got_repo_open(&repo,
10153 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10154 gitconfig_path, pack_fds);
10158 error = get_author(&committer, repo, worktree);
10159 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
10162 error = apply_unveil(got_repo_get_path(repo), 0,
10163 worktree ? got_worktree_get_root_path(worktree) : NULL);
10167 if (list_backups || delete_backups) {
10168 error = process_backup_refs(
10169 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10170 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10171 goto done; /* nothing else to do */
10174 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10178 if (histedit_in_progress) {
10179 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10183 error = got_worktree_merge_in_progress(&merge_in_progress,
10187 if (merge_in_progress) {
10188 error = got_error(GOT_ERR_MERGE_BUSY);
10192 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10196 if (abort_rebase) {
10197 if (!rebase_in_progress) {
10198 error = got_error(GOT_ERR_NOT_REBASING);
10201 error = got_worktree_rebase_continue(&resume_commit_id,
10202 &new_base_branch, &tmp_branch, &branch, &fileindex,
10206 printf("Switching work tree to %s\n",
10207 got_ref_get_symref_target(new_base_branch));
10208 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10209 new_base_branch, abort_progress, &upa);
10212 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10213 print_merge_progress_stats(&upa);
10214 goto done; /* nothing else to do */
10217 if (continue_rebase) {
10218 if (!rebase_in_progress) {
10219 error = got_error(GOT_ERR_NOT_REBASING);
10222 error = got_worktree_rebase_continue(&resume_commit_id,
10223 &new_base_branch, &tmp_branch, &branch, &fileindex,
10228 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10229 committer, resume_commit_id, repo);
10233 yca_id = got_object_id_dup(resume_commit_id);
10234 if (yca_id == NULL) {
10235 error = got_error_from_errno("got_object_id_dup");
10239 error = got_ref_open(&branch, repo, argv[0], 0);
10242 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10243 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10244 "will not rebase a branch which lives outside "
10245 "the \"refs/heads/\" reference namespace");
10250 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10254 if (!continue_rebase) {
10255 struct got_object_id *base_commit_id;
10257 base_commit_id = got_worktree_get_base_commit_id(worktree);
10258 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10259 base_commit_id, branch_head_commit_id, 1, repo,
10260 check_cancelled, NULL);
10263 if (yca_id == NULL) {
10264 error = got_error_msg(GOT_ERR_ANCESTRY,
10265 "specified branch shares no common ancestry "
10266 "with work tree's branch");
10270 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10272 if (error->code != GOT_ERR_ANCESTRY)
10276 struct got_pathlist_head paths;
10277 printf("%s is already based on %s\n",
10278 got_ref_get_name(branch),
10279 got_worktree_get_head_ref_name(worktree));
10280 error = switch_head_ref(branch, branch_head_commit_id,
10284 error = got_worktree_set_base_commit_id(worktree, repo,
10285 branch_head_commit_id);
10288 TAILQ_INIT(&paths);
10289 error = got_pathlist_append(&paths, "", NULL);
10292 error = got_worktree_checkout_files(worktree,
10293 &paths, repo, update_progress, &upa,
10294 check_cancelled, NULL);
10295 got_pathlist_free(&paths);
10298 if (upa.did_something) {
10300 error = got_object_id_str(&id_str,
10301 branch_head_commit_id);
10304 printf("Updated to %s: %s\n",
10305 got_worktree_get_head_ref_name(worktree),
10309 printf("Already up-to-date\n");
10310 print_update_progress_stats(&upa);
10315 commit_id = branch_head_commit_id;
10316 error = got_object_open_as_commit(&commit, repo, commit_id);
10320 parent_ids = got_object_commit_get_parent_ids(commit);
10321 pid = STAILQ_FIRST(parent_ids);
10323 error = got_error(GOT_ERR_EMPTY_REBASE);
10326 error = collect_commits(&commits, commit_id, &pid->id,
10327 yca_id, got_worktree_get_path_prefix(worktree),
10328 GOT_ERR_REBASE_PATH, repo);
10329 got_object_commit_close(commit);
10334 if (!continue_rebase) {
10335 error = got_worktree_rebase_prepare(&new_base_branch,
10336 &tmp_branch, &fileindex, worktree, branch, repo);
10341 if (STAILQ_EMPTY(&commits)) {
10342 if (continue_rebase) {
10343 error = rebase_complete(worktree, fileindex,
10344 branch, new_base_branch, tmp_branch, repo,
10348 /* Fast-forward the reference of the branch. */
10349 struct got_object_id *new_head_commit_id;
10351 error = got_ref_resolve(&new_head_commit_id, repo,
10355 error = got_object_id_str(&id_str, new_head_commit_id);
10358 printf("Forwarding %s to commit %s\n",
10359 got_ref_get_name(branch), id_str);
10361 error = got_ref_change_ref(branch,
10362 new_head_commit_id);
10365 /* No backup needed since objects did not change. */
10371 STAILQ_FOREACH(qid, &commits, entry) {
10373 commit_id = &qid->id;
10374 parent_id = pid ? &pid->id : yca_id;
10377 memset(&upa, 0, sizeof(upa));
10378 error = got_worktree_rebase_merge_files(&merged_paths,
10379 worktree, fileindex, parent_id, commit_id, repo,
10380 update_progress, &upa, check_cancelled, NULL);
10384 print_merge_progress_stats(&upa);
10385 if (upa.conflicts > 0 || upa.missing > 0 ||
10386 upa.not_deleted > 0 || upa.unversioned > 0) {
10387 if (upa.conflicts > 0) {
10388 error = show_rebase_merge_conflict(&qid->id,
10393 got_worktree_rebase_pathlist_free(&merged_paths);
10397 error = rebase_commit(&merged_paths, worktree, fileindex,
10398 tmp_branch, committer, commit_id, repo);
10399 got_worktree_rebase_pathlist_free(&merged_paths);
10404 if (upa.conflicts > 0 || upa.missing > 0 ||
10405 upa.not_deleted > 0 || upa.unversioned > 0) {
10406 error = got_worktree_rebase_postpone(worktree, fileindex);
10409 if (upa.conflicts > 0 && upa.missing == 0 &&
10410 upa.not_deleted == 0 && upa.unversioned == 0) {
10411 error = got_error_msg(GOT_ERR_CONFLICTS,
10412 "conflicts must be resolved before rebasing "
10414 } else if (upa.conflicts > 0) {
10415 error = got_error_msg(GOT_ERR_CONFLICTS,
10416 "conflicts must be resolved before rebasing "
10417 "can continue; changes destined for some "
10418 "files were not yet merged and should be "
10419 "merged manually if required before the "
10420 "rebase operation is continued");
10422 error = got_error_msg(GOT_ERR_CONFLICTS,
10423 "changes destined for some files were not "
10424 "yet merged and should be merged manually "
10425 "if required before the rebase operation "
10429 error = rebase_complete(worktree, fileindex, branch,
10430 new_base_branch, tmp_branch, repo, create_backup);
10434 free(gitconfig_path);
10435 got_object_id_queue_free(&commits);
10436 free(branch_head_commit_id);
10437 free(resume_commit_id);
10440 got_object_commit_close(commit);
10442 got_ref_close(branch);
10443 if (new_base_branch)
10444 got_ref_close(new_base_branch);
10446 got_ref_close(tmp_branch);
10448 got_worktree_close(worktree);
10450 const struct got_error *close_err = got_repo_close(repo);
10455 const struct got_error *pack_err =
10456 got_repo_pack_fds_close(pack_fds);
10464 usage_histedit(void)
10466 fprintf(stderr, "usage: %s histedit [-aceflmX] [-F histedit-script] "
10467 "[branch]\n", getprogname());
10471 #define GOT_HISTEDIT_PICK 'p'
10472 #define GOT_HISTEDIT_EDIT 'e'
10473 #define GOT_HISTEDIT_FOLD 'f'
10474 #define GOT_HISTEDIT_DROP 'd'
10475 #define GOT_HISTEDIT_MESG 'm'
10477 static const struct got_histedit_cmd {
10478 unsigned char code;
10481 } got_histedit_cmds[] = {
10482 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10483 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10484 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10486 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10487 { GOT_HISTEDIT_MESG, "mesg",
10488 "single-line log message for commit above (open editor if empty)" },
10491 struct got_histedit_list_entry {
10492 TAILQ_ENTRY(got_histedit_list_entry) entry;
10493 struct got_object_id *commit_id;
10494 const struct got_histedit_cmd *cmd;
10497 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10499 static const struct got_error *
10500 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10501 FILE *f, struct got_repository *repo)
10503 const struct got_error *err = NULL;
10504 char *logmsg = NULL, *id_str = NULL;
10505 struct got_commit_object *commit = NULL;
10508 err = got_object_open_as_commit(&commit, repo, commit_id);
10512 err = get_short_logmsg(&logmsg, 34, commit);
10516 err = got_object_id_str(&id_str, commit_id);
10520 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10522 err = got_ferror(f, GOT_ERR_IO);
10525 got_object_commit_close(commit);
10531 static const struct got_error *
10532 histedit_write_commit_list(struct got_object_id_queue *commits,
10533 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10534 struct got_repository *repo)
10536 const struct got_error *err = NULL;
10537 struct got_object_qid *qid;
10538 const char *histedit_cmd = NULL;
10540 if (STAILQ_EMPTY(commits))
10541 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10543 STAILQ_FOREACH(qid, commits, entry) {
10544 histedit_cmd = got_histedit_cmds[0].name;
10546 histedit_cmd = "edit";
10547 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10548 histedit_cmd = "fold";
10549 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10552 if (edit_logmsg_only) {
10553 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10555 err = got_ferror(f, GOT_ERR_IO);
10564 static const struct got_error *
10565 write_cmd_list(FILE *f, const char *branch_name,
10566 struct got_object_id_queue *commits)
10568 const struct got_error *err = NULL;
10572 struct got_object_qid *qid;
10574 qid = STAILQ_FIRST(commits);
10575 err = got_object_id_str(&id_str, &qid->id);
10580 "# Editing the history of branch '%s' starting at\n"
10582 "# Commits will be processed in order from top to "
10583 "bottom of this file.\n", branch_name, id_str);
10585 err = got_ferror(f, GOT_ERR_IO);
10589 n = fprintf(f, "# Available histedit commands:\n");
10591 err = got_ferror(f, GOT_ERR_IO);
10595 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10596 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10597 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10600 err = got_ferror(f, GOT_ERR_IO);
10609 static const struct got_error *
10610 histedit_syntax_error(int lineno)
10612 static char msg[42];
10615 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10617 if (ret < 0 || (size_t)ret >= sizeof(msg))
10618 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10620 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10623 static const struct got_error *
10624 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10625 char *logmsg, struct got_repository *repo)
10627 const struct got_error *err;
10628 struct got_commit_object *folded_commit = NULL;
10629 char *id_str, *folded_logmsg = NULL;
10631 err = got_object_id_str(&id_str, hle->commit_id);
10635 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10639 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10642 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10643 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10644 folded_logmsg) == -1) {
10645 err = got_error_from_errno("asprintf");
10649 got_object_commit_close(folded_commit);
10651 free(folded_logmsg);
10655 static struct got_histedit_list_entry *
10656 get_folded_commits(struct got_histedit_list_entry *hle)
10658 struct got_histedit_list_entry *prev, *folded = NULL;
10660 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10661 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10662 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10663 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10665 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10671 static const struct got_error *
10672 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10673 struct got_repository *repo)
10675 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10676 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10677 const struct got_error *err = NULL;
10678 struct got_commit_object *commit = NULL;
10681 struct got_histedit_list_entry *folded = NULL;
10683 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10687 folded = get_folded_commits(hle);
10689 while (folded != hle) {
10690 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10691 folded = TAILQ_NEXT(folded, entry);
10694 err = append_folded_commit_msg(&new_msg, folded,
10700 folded = TAILQ_NEXT(folded, entry);
10704 err = got_object_id_str(&id_str, hle->commit_id);
10707 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10710 logmsg_len = asprintf(&new_msg,
10711 "%s\n# original log message of commit %s: %s",
10712 logmsg ? logmsg : "", id_str, orig_logmsg);
10713 if (logmsg_len == -1) {
10714 err = got_error_from_errno("asprintf");
10720 err = got_object_id_str(&id_str, hle->commit_id);
10724 err = got_opentemp_named_fd(&logmsg_path, &fd,
10725 GOT_TMPDIR_STR "/got-logmsg");
10729 write(fd, logmsg, logmsg_len);
10732 err = get_editor(&editor);
10736 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10739 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10742 hle->logmsg = strdup(new_msg);
10743 if (hle->logmsg == NULL)
10744 err = got_error_from_errno("strdup");
10747 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10748 err = got_error_from_errno2("unlink", logmsg_path);
10754 got_object_commit_close(commit);
10758 static const struct got_error *
10759 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10760 FILE *f, struct got_repository *repo)
10762 const struct got_error *err = NULL;
10763 char *line = NULL, *p, *end;
10766 int lineno = 0, lastcmd = -1;
10767 const struct got_histedit_cmd *cmd;
10768 struct got_object_id *commit_id = NULL;
10769 struct got_histedit_list_entry *hle = NULL;
10772 len = getline(&line, &size, f);
10774 const struct got_error *getline_err;
10777 getline_err = got_error_from_errno("getline");
10778 err = got_ferror(f, getline_err->code);
10783 while (isspace((unsigned char)p[0]))
10785 if (p[0] == '#' || p[0] == '\0') {
10791 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10792 cmd = &got_histedit_cmds[i];
10793 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10794 isspace((unsigned char)p[strlen(cmd->name)])) {
10795 p += strlen(cmd->name);
10798 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10803 if (i == nitems(got_histedit_cmds)) {
10804 err = histedit_syntax_error(lineno);
10807 while (isspace((unsigned char)p[0]))
10809 if (cmd->code == GOT_HISTEDIT_MESG) {
10810 if (lastcmd != GOT_HISTEDIT_PICK &&
10811 lastcmd != GOT_HISTEDIT_EDIT) {
10812 err = got_error(GOT_ERR_HISTEDIT_CMD);
10815 if (p[0] == '\0') {
10816 err = histedit_edit_logmsg(hle, repo);
10820 hle->logmsg = strdup(p);
10821 if (hle->logmsg == NULL) {
10822 err = got_error_from_errno("strdup");
10828 lastcmd = cmd->code;
10832 while (end[0] && !isspace((unsigned char)end[0]))
10836 err = got_object_resolve_id_str(&commit_id, repo, p);
10838 /* override error code */
10839 err = histedit_syntax_error(lineno);
10843 hle = malloc(sizeof(*hle));
10845 err = got_error_from_errno("malloc");
10849 hle->commit_id = commit_id;
10850 hle->logmsg = NULL;
10854 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10855 lastcmd = cmd->code;
10863 static const struct got_error *
10864 histedit_check_script(struct got_histedit_list *histedit_cmds,
10865 struct got_object_id_queue *commits, struct got_repository *repo)
10867 const struct got_error *err = NULL;
10868 struct got_object_qid *qid;
10869 struct got_histedit_list_entry *hle;
10870 static char msg[92];
10873 if (TAILQ_EMPTY(histedit_cmds))
10874 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10875 "histedit script contains no commands");
10876 if (STAILQ_EMPTY(commits))
10877 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10879 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10880 struct got_histedit_list_entry *hle2;
10881 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10884 if (got_object_id_cmp(hle->commit_id,
10885 hle2->commit_id) != 0)
10887 err = got_object_id_str(&id_str, hle->commit_id);
10890 snprintf(msg, sizeof(msg), "commit %s is listed "
10891 "more than once in histedit script", id_str);
10893 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10897 STAILQ_FOREACH(qid, commits, entry) {
10898 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10899 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10903 err = got_object_id_str(&id_str, &qid->id);
10906 snprintf(msg, sizeof(msg),
10907 "commit %s missing from histedit script", id_str);
10909 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10913 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10914 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10915 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10916 "last commit in histedit script cannot be folded");
10921 static const struct got_error *
10922 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10923 const char *path, struct got_object_id_queue *commits,
10924 struct got_repository *repo)
10926 const struct got_error *err = NULL;
10930 err = get_editor(&editor);
10934 if (spawn_editor(editor, path) == -1) {
10935 err = got_error_from_errno("failed spawning editor");
10939 f = fopen(path, "re");
10941 err = got_error_from_errno("fopen");
10944 err = histedit_parse_list(histedit_cmds, f, repo);
10948 err = histedit_check_script(histedit_cmds, commits, repo);
10950 if (f && fclose(f) == EOF && err == NULL)
10951 err = got_error_from_errno("fclose");
10956 static const struct got_error *
10957 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10958 struct got_object_id_queue *, const char *, const char *,
10959 struct got_repository *);
10961 static const struct got_error *
10962 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10963 struct got_object_id_queue *commits, const char *branch_name,
10964 int edit_logmsg_only, int fold_only, int edit_only,
10965 struct got_repository *repo)
10967 const struct got_error *err;
10971 err = got_opentemp_named(&path, &f, "got-histedit");
10975 err = write_cmd_list(f, branch_name, commits);
10979 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10980 fold_only, edit_only, repo);
10984 if (edit_logmsg_only || fold_only || edit_only) {
10986 err = histedit_parse_list(histedit_cmds, f, repo);
10988 if (fclose(f) == EOF) {
10989 err = got_error_from_errno("fclose");
10993 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10995 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10996 err->code != GOT_ERR_HISTEDIT_CMD)
10998 err = histedit_edit_list_retry(histedit_cmds, err,
10999 commits, path, branch_name, repo);
11003 if (f && fclose(f) == EOF && err == NULL)
11004 err = got_error_from_errno("fclose");
11005 if (path && unlink(path) != 0 && err == NULL)
11006 err = got_error_from_errno2("unlink", path);
11011 static const struct got_error *
11012 histedit_save_list(struct got_histedit_list *histedit_cmds,
11013 struct got_worktree *worktree, struct got_repository *repo)
11015 const struct got_error *err = NULL;
11018 struct got_histedit_list_entry *hle;
11019 struct got_commit_object *commit = NULL;
11021 err = got_worktree_get_histedit_script_path(&path, worktree);
11025 f = fopen(path, "we");
11027 err = got_error_from_errno2("fopen", path);
11030 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11031 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11037 int n = fprintf(f, "%c %s\n",
11038 GOT_HISTEDIT_MESG, hle->logmsg);
11040 err = got_ferror(f, GOT_ERR_IO);
11046 if (f && fclose(f) == EOF && err == NULL)
11047 err = got_error_from_errno("fclose");
11050 got_object_commit_close(commit);
11055 histedit_free_list(struct got_histedit_list *histedit_cmds)
11057 struct got_histedit_list_entry *hle;
11059 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11060 TAILQ_REMOVE(histedit_cmds, hle, entry);
11065 static const struct got_error *
11066 histedit_load_list(struct got_histedit_list *histedit_cmds,
11067 const char *path, struct got_repository *repo)
11069 const struct got_error *err = NULL;
11072 f = fopen(path, "re");
11074 err = got_error_from_errno2("fopen", path);
11078 err = histedit_parse_list(histedit_cmds, f, repo);
11080 if (f && fclose(f) == EOF && err == NULL)
11081 err = got_error_from_errno("fclose");
11085 static const struct got_error *
11086 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11087 const struct got_error *edit_err, struct got_object_id_queue *commits,
11088 const char *path, const char *branch_name, struct got_repository *repo)
11090 const struct got_error *err = NULL, *prev_err = edit_err;
11093 while (resp != 'c' && resp != 'r' && resp != 'a') {
11094 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11095 "or (a)bort: ", getprogname(), prev_err->msg);
11100 histedit_free_list(histedit_cmds);
11101 err = histedit_run_editor(histedit_cmds, path, commits,
11104 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11105 err->code != GOT_ERR_HISTEDIT_CMD)
11112 } else if (resp == 'r') {
11113 histedit_free_list(histedit_cmds);
11114 err = histedit_edit_script(histedit_cmds,
11115 commits, branch_name, 0, 0, 0, repo);
11117 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11118 err->code != GOT_ERR_HISTEDIT_CMD)
11125 } else if (resp == 'a') {
11126 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11129 printf("invalid response '%c'\n", resp);
11135 static const struct got_error *
11136 histedit_complete(struct got_worktree *worktree,
11137 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11138 struct got_reference *branch, struct got_repository *repo)
11140 printf("Switching work tree to %s\n",
11141 got_ref_get_symref_target(branch));
11142 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11146 static const struct got_error *
11147 show_histedit_progress(struct got_commit_object *commit,
11148 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11150 const struct got_error *err;
11151 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11153 err = got_object_id_str(&old_id_str, hle->commit_id);
11158 err = got_object_id_str(&new_id_str, new_id);
11163 old_id_str[12] = '\0';
11165 new_id_str[12] = '\0';
11168 logmsg = strdup(hle->logmsg);
11169 if (logmsg == NULL) {
11170 err = got_error_from_errno("strdup");
11173 trim_logmsg(logmsg, 42);
11175 err = get_short_logmsg(&logmsg, 42, commit);
11180 switch (hle->cmd->code) {
11181 case GOT_HISTEDIT_PICK:
11182 case GOT_HISTEDIT_EDIT:
11183 printf("%s -> %s: %s\n", old_id_str,
11184 new_id_str ? new_id_str : "no-op change", logmsg);
11186 case GOT_HISTEDIT_DROP:
11187 case GOT_HISTEDIT_FOLD:
11188 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11200 static const struct got_error *
11201 histedit_commit(struct got_pathlist_head *merged_paths,
11202 struct got_worktree *worktree, struct got_fileindex *fileindex,
11203 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11204 const char *committer, struct got_repository *repo)
11206 const struct got_error *err;
11207 struct got_commit_object *commit;
11208 struct got_object_id *new_commit_id;
11210 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11211 && hle->logmsg == NULL) {
11212 err = histedit_edit_logmsg(hle, repo);
11217 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11221 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11222 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
11223 hle->logmsg, repo);
11225 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11227 err = show_histedit_progress(commit, hle, NULL);
11229 err = show_histedit_progress(commit, hle, new_commit_id);
11230 free(new_commit_id);
11233 got_object_commit_close(commit);
11237 static const struct got_error *
11238 histedit_skip_commit(struct got_histedit_list_entry *hle,
11239 struct got_worktree *worktree, struct got_repository *repo)
11241 const struct got_error *error;
11242 struct got_commit_object *commit;
11244 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11249 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11253 error = show_histedit_progress(commit, hle, NULL);
11254 got_object_commit_close(commit);
11258 static const struct got_error *
11259 check_local_changes(void *arg, unsigned char status,
11260 unsigned char staged_status, const char *path,
11261 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11262 struct got_object_id *commit_id, int dirfd, const char *de_name)
11264 int *have_local_changes = arg;
11267 case GOT_STATUS_ADD:
11268 case GOT_STATUS_DELETE:
11269 case GOT_STATUS_MODIFY:
11270 case GOT_STATUS_CONFLICT:
11271 *have_local_changes = 1;
11272 return got_error(GOT_ERR_CANCELLED);
11277 switch (staged_status) {
11278 case GOT_STATUS_ADD:
11279 case GOT_STATUS_DELETE:
11280 case GOT_STATUS_MODIFY:
11281 *have_local_changes = 1;
11282 return got_error(GOT_ERR_CANCELLED);
11290 static const struct got_error *
11291 cmd_histedit(int argc, char *argv[])
11293 const struct got_error *error = NULL;
11294 struct got_worktree *worktree = NULL;
11295 struct got_fileindex *fileindex = NULL;
11296 struct got_repository *repo = NULL;
11297 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11298 struct got_reference *branch = NULL;
11299 struct got_reference *tmp_branch = NULL;
11300 struct got_object_id *resume_commit_id = NULL;
11301 struct got_object_id *base_commit_id = NULL;
11302 struct got_object_id *head_commit_id = NULL;
11303 struct got_commit_object *commit = NULL;
11304 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11305 struct got_update_progress_arg upa;
11306 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11307 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11308 int list_backups = 0, delete_backups = 0;
11309 const char *edit_script_path = NULL;
11310 struct got_object_id_queue commits;
11311 struct got_pathlist_head merged_paths;
11312 const struct got_object_id_queue *parent_ids;
11313 struct got_object_qid *pid;
11314 struct got_histedit_list histedit_cmds;
11315 struct got_histedit_list_entry *hle;
11316 int *pack_fds = NULL;
11318 STAILQ_INIT(&commits);
11319 TAILQ_INIT(&histedit_cmds);
11320 TAILQ_INIT(&merged_paths);
11321 memset(&upa, 0, sizeof(upa));
11323 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11338 edit_script_path = optarg;
11341 edit_logmsg_only = 1;
11347 delete_backups = 1;
11359 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11360 "unveil", NULL) == -1)
11363 if (abort_edit && continue_edit)
11364 option_conflict('a', 'c');
11365 if (edit_script_path && edit_logmsg_only)
11366 option_conflict('F', 'm');
11367 if (abort_edit && edit_logmsg_only)
11368 option_conflict('a', 'm');
11369 if (continue_edit && edit_logmsg_only)
11370 option_conflict('c', 'm');
11371 if (abort_edit && fold_only)
11372 option_conflict('a', 'f');
11373 if (continue_edit && fold_only)
11374 option_conflict('c', 'f');
11375 if (fold_only && edit_logmsg_only)
11376 option_conflict('f', 'm');
11377 if (edit_script_path && fold_only)
11378 option_conflict('F', 'f');
11379 if (abort_edit && edit_only)
11380 option_conflict('a', 'e');
11381 if (continue_edit && edit_only)
11382 option_conflict('c', 'e');
11383 if (edit_only && edit_logmsg_only)
11384 option_conflict('e', 'm');
11385 if (edit_script_path && edit_only)
11386 option_conflict('F', 'e');
11387 if (list_backups) {
11389 option_conflict('l', 'a');
11391 option_conflict('l', 'c');
11392 if (edit_script_path)
11393 option_conflict('l', 'F');
11394 if (edit_logmsg_only)
11395 option_conflict('l', 'm');
11397 option_conflict('l', 'f');
11399 option_conflict('l', 'e');
11400 if (delete_backups)
11401 option_conflict('l', 'X');
11402 if (argc != 0 && argc != 1)
11404 } else if (delete_backups) {
11406 option_conflict('X', 'a');
11408 option_conflict('X', 'c');
11409 if (edit_script_path)
11410 option_conflict('X', 'F');
11411 if (edit_logmsg_only)
11412 option_conflict('X', 'm');
11414 option_conflict('X', 'f');
11416 option_conflict('X', 'e');
11418 option_conflict('X', 'l');
11419 if (argc != 0 && argc != 1)
11421 } else if (argc != 0)
11425 * This command cannot apply unveil(2) in all cases because the
11426 * user may choose to run an editor to edit the histedit script
11427 * and to edit individual commit log messages.
11428 * unveil(2) traverses exec(2); if an editor is used we have to
11429 * apply unveil after edit script and log messages have been written.
11430 * XXX TODO: Make use of unveil(2) where possible.
11433 cwd = getcwd(NULL, 0);
11435 error = got_error_from_errno("getcwd");
11439 error = got_repo_pack_fds_open(&pack_fds);
11443 error = got_worktree_open(&worktree, cwd);
11445 if (list_backups || delete_backups) {
11446 if (error->code != GOT_ERR_NOT_WORKTREE)
11449 if (error->code == GOT_ERR_NOT_WORKTREE)
11450 error = wrap_not_worktree_error(error,
11456 if (list_backups || delete_backups) {
11457 error = got_repo_open(&repo,
11458 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11462 error = apply_unveil(got_repo_get_path(repo), 0,
11463 worktree ? got_worktree_get_root_path(worktree) : NULL);
11466 error = process_backup_refs(
11467 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11468 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11469 goto done; /* nothing else to do */
11472 error = get_gitconfig_path(&gitconfig_path);
11475 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11476 gitconfig_path, pack_fds);
11480 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11483 if (rebase_in_progress) {
11484 error = got_error(GOT_ERR_REBASING);
11488 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11492 if (merge_in_progress) {
11493 error = got_error(GOT_ERR_MERGE_BUSY);
11497 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11501 if (edit_in_progress && edit_logmsg_only) {
11502 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11503 "histedit operation is in progress in this "
11504 "work tree and must be continued or aborted "
11505 "before the -m option can be used");
11508 if (edit_in_progress && fold_only) {
11509 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11510 "histedit operation is in progress in this "
11511 "work tree and must be continued or aborted "
11512 "before the -f option can be used");
11515 if (edit_in_progress && edit_only) {
11516 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11517 "histedit operation is in progress in this "
11518 "work tree and must be continued or aborted "
11519 "before the -e option can be used");
11523 if (edit_in_progress && abort_edit) {
11524 error = got_worktree_histedit_continue(&resume_commit_id,
11525 &tmp_branch, &branch, &base_commit_id, &fileindex,
11529 printf("Switching work tree to %s\n",
11530 got_ref_get_symref_target(branch));
11531 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11532 branch, base_commit_id, abort_progress, &upa);
11535 printf("Histedit of %s aborted\n",
11536 got_ref_get_symref_target(branch));
11537 print_merge_progress_stats(&upa);
11538 goto done; /* nothing else to do */
11539 } else if (abort_edit) {
11540 error = got_error(GOT_ERR_NOT_HISTEDIT);
11544 error = get_author(&committer, repo, worktree);
11548 if (continue_edit) {
11551 if (!edit_in_progress) {
11552 error = got_error(GOT_ERR_NOT_HISTEDIT);
11556 error = got_worktree_get_histedit_script_path(&path, worktree);
11560 error = histedit_load_list(&histedit_cmds, path, repo);
11565 error = got_worktree_histedit_continue(&resume_commit_id,
11566 &tmp_branch, &branch, &base_commit_id, &fileindex,
11571 error = got_ref_resolve(&head_commit_id, repo, branch);
11575 error = got_object_open_as_commit(&commit, repo,
11579 parent_ids = got_object_commit_get_parent_ids(commit);
11580 pid = STAILQ_FIRST(parent_ids);
11582 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11585 error = collect_commits(&commits, head_commit_id, &pid->id,
11586 base_commit_id, got_worktree_get_path_prefix(worktree),
11587 GOT_ERR_HISTEDIT_PATH, repo);
11588 got_object_commit_close(commit);
11593 if (edit_in_progress) {
11594 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11598 error = got_ref_open(&branch, repo,
11599 got_worktree_get_head_ref_name(worktree), 0);
11603 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11604 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11605 "will not edit commit history of a branch outside "
11606 "the \"refs/heads/\" reference namespace");
11610 error = got_ref_resolve(&head_commit_id, repo, branch);
11611 got_ref_close(branch);
11616 error = got_object_open_as_commit(&commit, repo,
11620 parent_ids = got_object_commit_get_parent_ids(commit);
11621 pid = STAILQ_FIRST(parent_ids);
11623 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11626 error = collect_commits(&commits, head_commit_id, &pid->id,
11627 got_worktree_get_base_commit_id(worktree),
11628 got_worktree_get_path_prefix(worktree),
11629 GOT_ERR_HISTEDIT_PATH, repo);
11630 got_object_commit_close(commit);
11635 if (STAILQ_EMPTY(&commits)) {
11636 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11640 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11641 &base_commit_id, &fileindex, worktree, repo);
11645 if (edit_script_path) {
11646 error = histedit_load_list(&histedit_cmds,
11647 edit_script_path, repo);
11649 got_worktree_histedit_abort(worktree, fileindex,
11650 repo, branch, base_commit_id,
11651 abort_progress, &upa);
11652 print_merge_progress_stats(&upa);
11656 const char *branch_name;
11657 branch_name = got_ref_get_symref_target(branch);
11658 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11660 error = histedit_edit_script(&histedit_cmds, &commits,
11661 branch_name, edit_logmsg_only, fold_only,
11664 got_worktree_histedit_abort(worktree, fileindex,
11665 repo, branch, base_commit_id,
11666 abort_progress, &upa);
11667 print_merge_progress_stats(&upa);
11673 error = histedit_save_list(&histedit_cmds, worktree,
11676 got_worktree_histedit_abort(worktree, fileindex,
11677 repo, branch, base_commit_id,
11678 abort_progress, &upa);
11679 print_merge_progress_stats(&upa);
11685 error = histedit_check_script(&histedit_cmds, &commits, repo);
11689 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11690 if (resume_commit_id) {
11691 if (got_object_id_cmp(hle->commit_id,
11692 resume_commit_id) != 0)
11695 resume_commit_id = NULL;
11696 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11697 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11698 error = histedit_skip_commit(hle, worktree,
11703 struct got_pathlist_head paths;
11704 int have_changes = 0;
11706 TAILQ_INIT(&paths);
11707 error = got_pathlist_append(&paths, "", NULL);
11710 error = got_worktree_status(worktree, &paths,
11711 repo, 0, check_local_changes, &have_changes,
11712 check_cancelled, NULL);
11713 got_pathlist_free(&paths);
11715 if (error->code != GOT_ERR_CANCELLED)
11717 if (sigint_received || sigpipe_received)
11720 if (have_changes) {
11721 error = histedit_commit(NULL, worktree,
11722 fileindex, tmp_branch, hle,
11727 error = got_object_open_as_commit(
11728 &commit, repo, hle->commit_id);
11731 error = show_histedit_progress(commit,
11733 got_object_commit_close(commit);
11742 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11743 error = histedit_skip_commit(hle, worktree, repo);
11749 error = got_object_open_as_commit(&commit, repo,
11753 parent_ids = got_object_commit_get_parent_ids(commit);
11754 pid = STAILQ_FIRST(parent_ids);
11756 error = got_worktree_histedit_merge_files(&merged_paths,
11757 worktree, fileindex, &pid->id, hle->commit_id, repo,
11758 update_progress, &upa, check_cancelled, NULL);
11761 got_object_commit_close(commit);
11764 print_merge_progress_stats(&upa);
11765 if (upa.conflicts > 0 || upa.missing > 0 ||
11766 upa.not_deleted > 0 || upa.unversioned > 0) {
11767 if (upa.conflicts > 0) {
11768 error = show_rebase_merge_conflict(
11769 hle->commit_id, repo);
11773 got_worktree_rebase_pathlist_free(&merged_paths);
11777 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11779 error = got_object_id_str(&id_str, hle->commit_id);
11782 printf("Stopping histedit for amending commit %s\n",
11785 got_worktree_rebase_pathlist_free(&merged_paths);
11786 error = got_worktree_histedit_postpone(worktree,
11791 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11792 error = histedit_skip_commit(hle, worktree, repo);
11798 error = histedit_commit(&merged_paths, worktree, fileindex,
11799 tmp_branch, hle, committer, repo);
11800 got_worktree_rebase_pathlist_free(&merged_paths);
11805 if (upa.conflicts > 0 || upa.missing > 0 ||
11806 upa.not_deleted > 0 || upa.unversioned > 0) {
11807 error = got_worktree_histedit_postpone(worktree, fileindex);
11810 if (upa.conflicts > 0 && upa.missing == 0 &&
11811 upa.not_deleted == 0 && upa.unversioned == 0) {
11812 error = got_error_msg(GOT_ERR_CONFLICTS,
11813 "conflicts must be resolved before histedit "
11815 } else if (upa.conflicts > 0) {
11816 error = got_error_msg(GOT_ERR_CONFLICTS,
11817 "conflicts must be resolved before histedit "
11818 "can continue; changes destined for some "
11819 "files were not yet merged and should be "
11820 "merged manually if required before the "
11821 "histedit operation is continued");
11823 error = got_error_msg(GOT_ERR_CONFLICTS,
11824 "changes destined for some files were not "
11825 "yet merged and should be merged manually "
11826 "if required before the histedit operation "
11830 error = histedit_complete(worktree, fileindex, tmp_branch,
11835 free(gitconfig_path);
11836 got_object_id_queue_free(&commits);
11837 histedit_free_list(&histedit_cmds);
11838 free(head_commit_id);
11839 free(base_commit_id);
11840 free(resume_commit_id);
11842 got_object_commit_close(commit);
11844 got_ref_close(branch);
11846 got_ref_close(tmp_branch);
11848 got_worktree_close(worktree);
11850 const struct got_error *close_err = got_repo_close(repo);
11855 const struct got_error *pack_err =
11856 got_repo_pack_fds_close(pack_fds);
11864 usage_integrate(void)
11866 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11870 static const struct got_error *
11871 cmd_integrate(int argc, char *argv[])
11873 const struct got_error *error = NULL;
11874 struct got_repository *repo = NULL;
11875 struct got_worktree *worktree = NULL;
11876 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11877 const char *branch_arg = NULL;
11878 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11879 struct got_fileindex *fileindex = NULL;
11880 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11882 struct got_update_progress_arg upa;
11883 int *pack_fds = NULL;
11885 while ((ch = getopt(argc, argv, "")) != -1) {
11898 branch_arg = argv[0];
11900 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11901 "unveil", NULL) == -1)
11904 cwd = getcwd(NULL, 0);
11906 error = got_error_from_errno("getcwd");
11910 error = got_repo_pack_fds_open(&pack_fds);
11914 error = got_worktree_open(&worktree, cwd);
11916 if (error->code == GOT_ERR_NOT_WORKTREE)
11917 error = wrap_not_worktree_error(error, "integrate",
11922 error = check_rebase_or_histedit_in_progress(worktree);
11926 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11931 error = apply_unveil(got_repo_get_path(repo), 0,
11932 got_worktree_get_root_path(worktree));
11936 error = check_merge_in_progress(worktree, repo);
11940 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11941 error = got_error_from_errno("asprintf");
11945 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11946 &base_branch_ref, worktree, refname, repo);
11950 refname = strdup(got_ref_get_name(branch_ref));
11951 if (refname == NULL) {
11952 error = got_error_from_errno("strdup");
11953 got_worktree_integrate_abort(worktree, fileindex, repo,
11954 branch_ref, base_branch_ref);
11957 base_refname = strdup(got_ref_get_name(base_branch_ref));
11958 if (base_refname == NULL) {
11959 error = got_error_from_errno("strdup");
11960 got_worktree_integrate_abort(worktree, fileindex, repo,
11961 branch_ref, base_branch_ref);
11965 error = got_ref_resolve(&commit_id, repo, branch_ref);
11969 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11973 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11974 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11975 "specified branch has already been integrated");
11976 got_worktree_integrate_abort(worktree, fileindex, repo,
11977 branch_ref, base_branch_ref);
11981 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11983 if (error->code == GOT_ERR_ANCESTRY)
11984 error = got_error(GOT_ERR_REBASE_REQUIRED);
11985 got_worktree_integrate_abort(worktree, fileindex, repo,
11986 branch_ref, base_branch_ref);
11990 memset(&upa, 0, sizeof(upa));
11991 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11992 branch_ref, base_branch_ref, update_progress, &upa,
11993 check_cancelled, NULL);
11997 printf("Integrated %s into %s\n", refname, base_refname);
11998 print_update_progress_stats(&upa);
12001 const struct got_error *close_err = got_repo_close(repo);
12006 got_worktree_close(worktree);
12008 const struct got_error *pack_err =
12009 got_repo_pack_fds_close(pack_fds);
12014 free(base_commit_id);
12017 free(base_refname);
12024 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12028 static const struct got_error *
12029 cmd_merge(int argc, char *argv[])
12031 const struct got_error *error = NULL;
12032 struct got_worktree *worktree = NULL;
12033 struct got_repository *repo = NULL;
12034 struct got_fileindex *fileindex = NULL;
12035 char *cwd = NULL, *id_str = NULL, *author = NULL;
12036 struct got_reference *branch = NULL, *wt_branch = NULL;
12037 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12038 struct got_object_id *wt_branch_tip = NULL;
12039 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12040 int interrupt_merge = 0;
12041 struct got_update_progress_arg upa;
12042 struct got_object_id *merge_commit_id = NULL;
12043 char *branch_name = NULL;
12044 int *pack_fds = NULL;
12046 memset(&upa, 0, sizeof(upa));
12048 while ((ch = getopt(argc, argv, "acn")) != -1) {
12054 continue_merge = 1;
12057 interrupt_merge = 1;
12069 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12070 "unveil", NULL) == -1)
12074 if (abort_merge && continue_merge)
12075 option_conflict('a', 'c');
12076 if (abort_merge || continue_merge) {
12079 } else if (argc != 1)
12082 cwd = getcwd(NULL, 0);
12084 error = got_error_from_errno("getcwd");
12088 error = got_repo_pack_fds_open(&pack_fds);
12092 error = got_worktree_open(&worktree, cwd);
12094 if (error->code == GOT_ERR_NOT_WORKTREE)
12095 error = wrap_not_worktree_error(error,
12100 error = got_repo_open(&repo,
12101 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12106 error = apply_unveil(got_repo_get_path(repo), 0,
12107 worktree ? got_worktree_get_root_path(worktree) : NULL);
12111 error = check_rebase_or_histedit_in_progress(worktree);
12115 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12121 if (!merge_in_progress) {
12122 error = got_error(GOT_ERR_NOT_MERGING);
12125 error = got_worktree_merge_continue(&branch_name,
12126 &branch_tip, &fileindex, worktree, repo);
12129 error = got_worktree_merge_abort(worktree, fileindex, repo,
12130 abort_progress, &upa);
12133 printf("Merge of %s aborted\n", branch_name);
12134 goto done; /* nothing else to do */
12137 error = get_author(&author, repo, worktree);
12141 if (continue_merge) {
12142 if (!merge_in_progress) {
12143 error = got_error(GOT_ERR_NOT_MERGING);
12146 error = got_worktree_merge_continue(&branch_name,
12147 &branch_tip, &fileindex, worktree, repo);
12151 error = got_ref_open(&branch, repo, argv[0], 0);
12154 branch_name = strdup(got_ref_get_name(branch));
12155 if (branch_name == NULL) {
12156 error = got_error_from_errno("strdup");
12159 error = got_ref_resolve(&branch_tip, repo, branch);
12164 error = got_ref_open(&wt_branch, repo,
12165 got_worktree_get_head_ref_name(worktree), 0);
12168 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12171 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12172 wt_branch_tip, branch_tip, 0, repo,
12173 check_cancelled, NULL);
12174 if (error && error->code != GOT_ERR_ANCESTRY)
12177 if (!continue_merge) {
12178 error = check_path_prefix(wt_branch_tip, branch_tip,
12179 got_worktree_get_path_prefix(worktree),
12180 GOT_ERR_MERGE_PATH, repo);
12184 error = check_same_branch(wt_branch_tip, branch,
12187 if (error->code != GOT_ERR_ANCESTRY)
12191 static char msg[512];
12192 snprintf(msg, sizeof(msg),
12193 "cannot create a merge commit because "
12194 "%s is based on %s; %s can be integrated "
12195 "with 'got integrate' instead", branch_name,
12196 got_worktree_get_head_ref_name(worktree),
12198 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12202 error = got_worktree_merge_prepare(&fileindex, worktree,
12207 error = got_worktree_merge_branch(worktree, fileindex,
12208 yca_id, branch_tip, repo, update_progress, &upa,
12209 check_cancelled, NULL);
12212 print_merge_progress_stats(&upa);
12213 if (!upa.did_something) {
12214 error = got_worktree_merge_abort(worktree, fileindex,
12215 repo, abort_progress, &upa);
12218 printf("Already up-to-date\n");
12223 if (interrupt_merge) {
12224 error = got_worktree_merge_postpone(worktree, fileindex);
12227 printf("Merge of %s interrupted on request\n", branch_name);
12228 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12229 upa.not_deleted > 0 || upa.unversioned > 0) {
12230 error = got_worktree_merge_postpone(worktree, fileindex);
12233 if (upa.conflicts > 0 && upa.missing == 0 &&
12234 upa.not_deleted == 0 && upa.unversioned == 0) {
12235 error = got_error_msg(GOT_ERR_CONFLICTS,
12236 "conflicts must be resolved before merging "
12238 } else if (upa.conflicts > 0) {
12239 error = got_error_msg(GOT_ERR_CONFLICTS,
12240 "conflicts must be resolved before merging "
12241 "can continue; changes destined for some "
12242 "files were not yet merged and "
12243 "should be merged manually if required before the "
12244 "merge operation is continued");
12246 error = got_error_msg(GOT_ERR_CONFLICTS,
12247 "changes destined for some "
12248 "files were not yet merged and should be "
12249 "merged manually if required before the "
12250 "merge operation is continued");
12254 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12255 fileindex, author, NULL, 1, branch_tip, branch_name,
12256 repo, continue_merge ? print_status : NULL, NULL);
12259 error = got_worktree_merge_complete(worktree, fileindex, repo);
12262 error = got_object_id_str(&id_str, merge_commit_id);
12265 printf("Merged %s into %s: %s\n", branch_name,
12266 got_worktree_get_head_ref_name(worktree),
12272 free(merge_commit_id);
12278 got_ref_close(branch);
12280 got_ref_close(wt_branch);
12282 got_worktree_close(worktree);
12284 const struct got_error *close_err = got_repo_close(repo);
12289 const struct got_error *pack_err =
12290 got_repo_pack_fds_close(pack_fds);
12300 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
12301 "[path ...]\n", getprogname());
12305 static const struct got_error *
12306 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12307 const char *path, struct got_object_id *blob_id,
12308 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12309 int dirfd, const char *de_name)
12311 const struct got_error *err = NULL;
12312 char *id_str = NULL;
12314 if (staged_status != GOT_STATUS_ADD &&
12315 staged_status != GOT_STATUS_MODIFY &&
12316 staged_status != GOT_STATUS_DELETE)
12319 if (staged_status == GOT_STATUS_ADD ||
12320 staged_status == GOT_STATUS_MODIFY)
12321 err = got_object_id_str(&id_str, staged_blob_id);
12323 err = got_object_id_str(&id_str, blob_id);
12327 printf("%s %c %s\n", id_str, staged_status, path);
12332 static const struct got_error *
12333 cmd_stage(int argc, char *argv[])
12335 const struct got_error *error = NULL;
12336 struct got_repository *repo = NULL;
12337 struct got_worktree *worktree = NULL;
12339 struct got_pathlist_head paths;
12340 struct got_pathlist_entry *pe;
12341 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12342 FILE *patch_script_file = NULL;
12343 const char *patch_script_path = NULL;
12344 struct choose_patch_arg cpa;
12345 int *pack_fds = NULL;
12347 TAILQ_INIT(&paths);
12349 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12358 patch_script_path = optarg;
12361 allow_bad_symlinks = 1;
12373 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12374 "unveil", NULL) == -1)
12377 if (list_stage && (pflag || patch_script_path))
12378 errx(1, "-l option cannot be used with other options");
12379 if (patch_script_path && !pflag)
12380 errx(1, "-F option can only be used together with -p option");
12382 cwd = getcwd(NULL, 0);
12384 error = got_error_from_errno("getcwd");
12388 error = got_repo_pack_fds_open(&pack_fds);
12392 error = got_worktree_open(&worktree, cwd);
12394 if (error->code == GOT_ERR_NOT_WORKTREE)
12395 error = wrap_not_worktree_error(error, "stage", cwd);
12399 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12404 if (patch_script_path) {
12405 patch_script_file = fopen(patch_script_path, "re");
12406 if (patch_script_file == NULL) {
12407 error = got_error_from_errno2("fopen",
12408 patch_script_path);
12412 error = apply_unveil(got_repo_get_path(repo), 0,
12413 got_worktree_get_root_path(worktree));
12417 error = check_merge_in_progress(worktree, repo);
12421 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12426 error = got_worktree_status(worktree, &paths, repo, 0,
12427 print_stage, NULL, check_cancelled, NULL);
12429 cpa.patch_script_file = patch_script_file;
12430 cpa.action = "stage";
12431 error = got_worktree_stage(worktree, &paths,
12432 pflag ? NULL : print_status, NULL,
12433 pflag ? choose_patch : NULL, &cpa,
12434 allow_bad_symlinks, repo);
12437 if (patch_script_file && fclose(patch_script_file) == EOF &&
12439 error = got_error_from_errno2("fclose", patch_script_path);
12441 const struct got_error *close_err = got_repo_close(repo);
12446 got_worktree_close(worktree);
12448 const struct got_error *pack_err =
12449 got_repo_pack_fds_close(pack_fds);
12453 TAILQ_FOREACH(pe, &paths, entry)
12454 free((char *)pe->path);
12455 got_pathlist_free(&paths);
12461 usage_unstage(void)
12463 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12464 "[path ...]\n", getprogname());
12469 static const struct got_error *
12470 cmd_unstage(int argc, char *argv[])
12472 const struct got_error *error = NULL;
12473 struct got_repository *repo = NULL;
12474 struct got_worktree *worktree = NULL;
12476 struct got_pathlist_head paths;
12477 struct got_pathlist_entry *pe;
12479 struct got_update_progress_arg upa;
12480 FILE *patch_script_file = NULL;
12481 const char *patch_script_path = NULL;
12482 struct choose_patch_arg cpa;
12483 int *pack_fds = NULL;
12485 TAILQ_INIT(&paths);
12487 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12493 patch_script_path = optarg;
12505 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12506 "unveil", NULL) == -1)
12509 if (patch_script_path && !pflag)
12510 errx(1, "-F option can only be used together with -p option");
12512 cwd = getcwd(NULL, 0);
12514 error = got_error_from_errno("getcwd");
12518 error = got_repo_pack_fds_open(&pack_fds);
12522 error = got_worktree_open(&worktree, cwd);
12524 if (error->code == GOT_ERR_NOT_WORKTREE)
12525 error = wrap_not_worktree_error(error, "unstage", cwd);
12529 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12534 if (patch_script_path) {
12535 patch_script_file = fopen(patch_script_path, "re");
12536 if (patch_script_file == NULL) {
12537 error = got_error_from_errno2("fopen",
12538 patch_script_path);
12543 error = apply_unveil(got_repo_get_path(repo), 0,
12544 got_worktree_get_root_path(worktree));
12548 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12552 cpa.patch_script_file = patch_script_file;
12553 cpa.action = "unstage";
12554 memset(&upa, 0, sizeof(upa));
12555 error = got_worktree_unstage(worktree, &paths, update_progress,
12556 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12558 print_merge_progress_stats(&upa);
12560 if (patch_script_file && fclose(patch_script_file) == EOF &&
12562 error = got_error_from_errno2("fclose", patch_script_path);
12564 const struct got_error *close_err = got_repo_close(repo);
12569 got_worktree_close(worktree);
12571 const struct got_error *pack_err =
12572 got_repo_pack_fds_close(pack_fds);
12576 TAILQ_FOREACH(pe, &paths, entry)
12577 free((char *)pe->path);
12578 got_pathlist_free(&paths);
12586 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
12587 "arg ...\n", getprogname());
12591 static const struct got_error *
12592 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12594 const struct got_error *err;
12595 struct got_blob_object *blob;
12598 fd = got_opentempfd();
12600 return got_error_from_errno("got_opentempfd");
12602 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12606 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12608 if (fd != -1 && close(fd) == -1 && err == NULL)
12609 err = got_error_from_errno("close");
12611 got_object_blob_close(blob);
12615 static const struct got_error *
12616 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12618 const struct got_error *err;
12619 struct got_tree_object *tree;
12622 err = got_object_open_as_tree(&tree, repo, id);
12626 nentries = got_object_tree_get_nentries(tree);
12627 for (i = 0; i < nentries; i++) {
12628 struct got_tree_entry *te;
12630 if (sigint_received || sigpipe_received)
12632 te = got_object_tree_get_entry(tree, i);
12633 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12636 fprintf(outfile, "%s %.7o %s\n", id_str,
12637 got_tree_entry_get_mode(te),
12638 got_tree_entry_get_name(te));
12642 got_object_tree_close(tree);
12646 static const struct got_error *
12647 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12649 const struct got_error *err;
12650 struct got_commit_object *commit;
12651 const struct got_object_id_queue *parent_ids;
12652 struct got_object_qid *pid;
12653 char *id_str = NULL;
12654 const char *logmsg = NULL;
12657 err = got_object_open_as_commit(&commit, repo, id);
12661 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12665 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12666 parent_ids = got_object_commit_get_parent_ids(commit);
12667 fprintf(outfile, "numparents %d\n",
12668 got_object_commit_get_nparents(commit));
12669 STAILQ_FOREACH(pid, parent_ids, entry) {
12671 err = got_object_id_str(&pid_str, &pid->id);
12674 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12677 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12678 got_object_commit_get_author_gmtoff(commit));
12679 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12680 got_object_commit_get_author(commit),
12681 (long long)got_object_commit_get_author_time(commit),
12684 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12685 got_object_commit_get_committer_gmtoff(commit));
12686 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12687 got_object_commit_get_committer(commit),
12688 (long long)got_object_commit_get_committer_time(commit),
12691 logmsg = got_object_commit_get_logmsg_raw(commit);
12692 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12693 fprintf(outfile, "%s", logmsg);
12696 got_object_commit_close(commit);
12700 static const struct got_error *
12701 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12703 const struct got_error *err;
12704 struct got_tag_object *tag;
12705 char *id_str = NULL;
12706 const char *tagmsg = NULL;
12709 err = got_object_open_as_tag(&tag, repo, id);
12713 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12717 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12719 switch (got_object_tag_get_object_type(tag)) {
12720 case GOT_OBJ_TYPE_BLOB:
12721 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12722 GOT_OBJ_LABEL_BLOB);
12724 case GOT_OBJ_TYPE_TREE:
12725 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12726 GOT_OBJ_LABEL_TREE);
12728 case GOT_OBJ_TYPE_COMMIT:
12729 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12730 GOT_OBJ_LABEL_COMMIT);
12732 case GOT_OBJ_TYPE_TAG:
12733 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12734 GOT_OBJ_LABEL_TAG);
12740 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12741 got_object_tag_get_name(tag));
12743 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12744 got_object_tag_get_tagger_gmtoff(tag));
12745 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12746 got_object_tag_get_tagger(tag),
12747 (long long)got_object_tag_get_tagger_time(tag),
12750 tagmsg = got_object_tag_get_message(tag);
12751 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12752 fprintf(outfile, "%s", tagmsg);
12755 got_object_tag_close(tag);
12759 static const struct got_error *
12760 cmd_cat(int argc, char *argv[])
12762 const struct got_error *error;
12763 struct got_repository *repo = NULL;
12764 struct got_worktree *worktree = NULL;
12765 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12766 const char *commit_id_str = NULL;
12767 struct got_object_id *id = NULL, *commit_id = NULL;
12768 struct got_commit_object *commit = NULL;
12769 int ch, obj_type, i, force_path = 0;
12770 struct got_reflist_head refs;
12771 int *pack_fds = NULL;
12776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12781 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12784 commit_id_str = optarg;
12787 repo_path = realpath(optarg, NULL);
12788 if (repo_path == NULL)
12789 return got_error_from_errno2("realpath",
12791 got_path_strip_trailing_slashes(repo_path);
12805 cwd = getcwd(NULL, 0);
12807 error = got_error_from_errno("getcwd");
12811 error = got_repo_pack_fds_open(&pack_fds);
12815 if (repo_path == NULL) {
12816 error = got_worktree_open(&worktree, cwd);
12817 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12820 repo_path = strdup(
12821 got_worktree_get_repo_path(worktree));
12822 if (repo_path == NULL) {
12823 error = got_error_from_errno("strdup");
12827 /* Release work tree lock. */
12828 got_worktree_close(worktree);
12833 if (repo_path == NULL) {
12834 repo_path = strdup(cwd);
12835 if (repo_path == NULL)
12836 return got_error_from_errno("strdup");
12839 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12848 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12852 if (commit_id_str == NULL)
12853 commit_id_str = GOT_REF_HEAD;
12854 error = got_repo_match_object_id(&commit_id, NULL,
12855 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12859 error = got_object_open_as_commit(&commit, repo, commit_id);
12863 for (i = 0; i < argc; i++) {
12865 error = got_object_id_by_path(&id, repo, commit,
12870 error = got_repo_match_object_id(&id, &label, argv[i],
12871 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12874 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12875 error->code != GOT_ERR_NOT_REF)
12877 error = got_object_id_by_path(&id, repo,
12884 error = got_object_get_type(&obj_type, repo, id);
12888 switch (obj_type) {
12889 case GOT_OBJ_TYPE_BLOB:
12890 error = cat_blob(id, repo, stdout);
12892 case GOT_OBJ_TYPE_TREE:
12893 error = cat_tree(id, repo, stdout);
12895 case GOT_OBJ_TYPE_COMMIT:
12896 error = cat_commit(id, repo, stdout);
12898 case GOT_OBJ_TYPE_TAG:
12899 error = cat_tag(id, repo, stdout);
12902 error = got_error(GOT_ERR_OBJ_TYPE);
12917 got_object_commit_close(commit);
12919 got_worktree_close(worktree);
12921 const struct got_error *close_err = got_repo_close(repo);
12926 const struct got_error *pack_err =
12927 got_repo_pack_fds_close(pack_fds);
12932 got_ref_list_free(&refs);
12939 fprintf(stderr, "usage: %s info [path ...]\n",
12944 static const struct got_error *
12945 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12946 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12947 struct got_object_id *commit_id)
12949 const struct got_error *err = NULL;
12950 char *id_str = NULL;
12952 struct tm mytm, *tm;
12953 struct got_pathlist_head *paths = arg;
12954 struct got_pathlist_entry *pe;
12957 * Clear error indication from any of the path arguments which
12958 * would cause this file index entry to be displayed.
12960 TAILQ_FOREACH(pe, paths, entry) {
12961 if (got_path_cmp(path, pe->path, strlen(path),
12962 pe->path_len) == 0 ||
12963 got_path_is_child(path, pe->path, pe->path_len))
12964 pe->data = NULL; /* no error */
12967 printf(GOT_COMMIT_SEP_STR);
12969 printf("symlink: %s\n", path);
12970 else if (S_ISREG(mode)) {
12971 printf("file: %s\n", path);
12972 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12973 } else if (S_ISDIR(mode))
12974 printf("directory: %s\n", path);
12976 printf("something: %s\n", path);
12978 tm = localtime_r(&mtime, &mytm);
12981 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12982 return got_error(GOT_ERR_NO_SPACE);
12983 printf("timestamp: %s\n", datebuf);
12986 err = got_object_id_str(&id_str, blob_id);
12989 printf("based on blob: %s\n", id_str);
12993 if (staged_blob_id) {
12994 err = got_object_id_str(&id_str, staged_blob_id);
12997 printf("based on staged blob: %s\n", id_str);
13002 err = got_object_id_str(&id_str, commit_id);
13005 printf("based on commit: %s\n", id_str);
13012 static const struct got_error *
13013 cmd_info(int argc, char *argv[])
13015 const struct got_error *error = NULL;
13016 struct got_worktree *worktree = NULL;
13017 char *cwd = NULL, *id_str = NULL;
13018 struct got_pathlist_head paths;
13019 struct got_pathlist_entry *pe;
13020 char *uuidstr = NULL;
13021 int ch, show_files = 0;
13023 TAILQ_INIT(&paths);
13025 while ((ch = getopt(argc, argv, "")) != -1) {
13037 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13041 cwd = getcwd(NULL, 0);
13043 error = got_error_from_errno("getcwd");
13047 error = got_worktree_open(&worktree, cwd);
13049 if (error->code == GOT_ERR_NOT_WORKTREE)
13050 error = wrap_not_worktree_error(error, "info", cwd);
13055 /* Remove "wpath cpath proc exec sendfd" promises. */
13056 if (pledge("stdio rpath flock unveil", NULL) == -1)
13059 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13064 error = get_worktree_paths_from_argv(&paths, argc, argv,
13071 error = got_object_id_str(&id_str,
13072 got_worktree_get_base_commit_id(worktree));
13076 error = got_worktree_get_uuid(&uuidstr, worktree);
13080 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13081 printf("work tree base commit: %s\n", id_str);
13082 printf("work tree path prefix: %s\n",
13083 got_worktree_get_path_prefix(worktree));
13084 printf("work tree branch reference: %s\n",
13085 got_worktree_get_head_ref_name(worktree));
13086 printf("work tree UUID: %s\n", uuidstr);
13087 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13090 struct got_pathlist_entry *pe;
13091 TAILQ_FOREACH(pe, &paths, entry) {
13092 if (pe->path_len == 0)
13095 * Assume this path will fail. This will be corrected
13096 * in print_path_info() in case the path does suceeed.
13098 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
13100 error = got_worktree_path_info(worktree, &paths,
13101 print_path_info, &paths, check_cancelled, NULL);
13104 TAILQ_FOREACH(pe, &paths, entry) {
13105 if (pe->data != NULL) {
13106 const struct got_error *perr;
13109 error = got_error_fmt(perr->code, "%s",
13117 got_worktree_close(worktree);
13118 TAILQ_FOREACH(pe, &paths, entry)
13119 free((char *)pe->path);
13120 got_pathlist_free(&paths);