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>
42 #include "got_compat.h"
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
402 size_t len, logmsg_len;
403 char *initial_content_stripped = NULL, *buf = NULL, *s;
407 if (stat(logmsg_path, &st) == -1)
408 return got_error_from_errno2("stat", logmsg_path);
410 if (spawn_editor(editor, logmsg_path) == -1)
411 return got_error_from_errno("failed spawning editor");
413 if (stat(logmsg_path, &st2) == -1)
414 return got_error_from_errno("stat");
416 if (require_modification &&
417 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
422 * Set up a stripped version of the initial content without comments
423 * and blank lines. We need this in order to check if the message
424 * has in fact been edited.
426 initial_content_stripped = malloc(initial_content_len + 1);
427 if (initial_content_stripped == NULL)
428 return got_error_from_errno("malloc");
429 initial_content_stripped[0] = '\0';
431 buf = strdup(initial_content);
433 err = got_error_from_errno("strdup");
438 while ((line = strsep(&s, "\n")) != NULL) {
439 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
440 continue; /* remove comments and leading empty lines */
441 len = strlcat(initial_content_stripped, line,
442 initial_content_len + 1);
443 if (len >= initial_content_len + 1) {
444 err = got_error(GOT_ERR_NO_SPACE);
448 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
449 initial_content_stripped[len - 1] = '\0';
453 logmsg_len = st2.st_size;
454 *logmsg = malloc(logmsg_len + 1);
456 return got_error_from_errno("malloc");
459 fp = fopen(logmsg_path, "re");
461 err = got_error_from_errno("fopen");
466 while ((linelen = getline(&line, &linesize, fp)) != -1) {
467 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
468 continue; /* remove comments and leading empty lines */
469 len = strlcat(*logmsg, line, logmsg_len + 1);
470 if (len >= logmsg_len + 1) {
471 err = got_error(GOT_ERR_NO_SPACE);
477 err = got_ferror(fp, GOT_ERR_IO);
480 while (len > 0 && (*logmsg)[len - 1] == '\n') {
481 (*logmsg)[len - 1] = '\0';
486 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
487 "commit message cannot be empty, aborting");
490 if (require_modification &&
491 strcmp(*logmsg, initial_content_stripped) == 0)
492 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
493 "no changes made to commit message, aborting");
495 free(initial_content_stripped);
497 if (fp && fclose(fp) == EOF && err == NULL)
498 err = got_error_from_errno("fclose");
506 static const struct got_error *
507 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
508 const char *path_dir, const char *branch_name)
510 char *initial_content = NULL;
511 const struct got_error *err = NULL;
512 int initial_content_len;
515 initial_content_len = asprintf(&initial_content,
516 "\n# %s to be imported to branch %s\n", path_dir,
518 if (initial_content_len == -1)
519 return got_error_from_errno("asprintf");
521 err = got_opentemp_named_fd(logmsg_path, &fd,
522 GOT_TMPDIR_STR "/got-importmsg");
526 if (write(fd, initial_content, initial_content_len) == -1) {
527 err = got_error_from_errno2("write", *logmsg_path);
531 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
532 initial_content_len, 1);
534 if (fd != -1 && close(fd) == -1 && err == NULL)
535 err = got_error_from_errno2("close", *logmsg_path);
536 free(initial_content);
544 static const struct got_error *
545 import_progress(void *arg, const char *path)
547 printf("A %s\n", path);
551 static const struct got_error *
552 valid_author(const char *author)
554 const char *email = author;
557 * Git' expects the author (or committer) to be in the form
558 * "name <email>", which are mostly free form (see the
559 * "committer" description in git-fast-import(1)). We're only
560 * doing this to avoid git's object parser breaking on commits
564 while (*author && *author != '\n' && *author != '<' && *author != '>')
566 if (*author++ != '<')
567 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 while (*author && *author != '\n' && *author != '<' && *author != '>')
570 if (strcmp(author, ">") != 0)
571 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
575 static const struct got_error *
576 get_author(char **author, struct got_repository *repo,
577 struct got_worktree *worktree)
579 const struct got_error *err = NULL;
580 const char *got_author = NULL, *name, *email;
581 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
586 worktree_conf = got_worktree_get_gotconfig(worktree);
587 repo_conf = got_repo_get_gotconfig(repo);
590 * Priority of potential author information sources, from most
591 * significant to least significant:
592 * 1) work tree's .got/got.conf file
593 * 2) repository's got.conf file
594 * 3) repository's git config file
595 * 4) environment variables
596 * 5) global git config files (in user's home directory or /etc)
600 got_author = got_gotconfig_get_author(worktree_conf);
601 if (got_author == NULL)
602 got_author = got_gotconfig_get_author(repo_conf);
603 if (got_author == NULL) {
604 name = got_repo_get_gitconfig_author_name(repo);
605 email = got_repo_get_gitconfig_author_email(repo);
607 if (asprintf(author, "%s <%s>", name, email) == -1)
608 return got_error_from_errno("asprintf");
612 got_author = getenv("GOT_AUTHOR");
613 if (got_author == NULL) {
614 name = got_repo_get_global_gitconfig_author_name(repo);
615 email = got_repo_get_global_gitconfig_author_email(
618 if (asprintf(author, "%s <%s>", name, email)
620 return got_error_from_errno("asprintf");
623 /* TODO: Look up user in password database? */
624 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
628 *author = strdup(got_author);
630 return got_error_from_errno("strdup");
632 err = valid_author(*author);
640 static const struct got_error *
641 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
642 struct got_worktree *worktree)
644 const char *got_allowed_signers = NULL;
645 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
647 *allowed_signers = NULL;
650 worktree_conf = got_worktree_get_gotconfig(worktree);
651 repo_conf = got_repo_get_gotconfig(repo);
654 * Priority of potential author information sources, from most
655 * significant to least significant:
656 * 1) work tree's .got/got.conf file
657 * 2) repository's got.conf file
661 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
663 if (got_allowed_signers == NULL)
664 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
667 if (got_allowed_signers) {
668 *allowed_signers = strdup(got_allowed_signers);
669 if (*allowed_signers == NULL)
670 return got_error_from_errno("strdup");
675 static const struct got_error *
676 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
677 struct got_worktree *worktree)
679 const char *got_revoked_signers = NULL;
680 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
682 *revoked_signers = NULL;
685 worktree_conf = got_worktree_get_gotconfig(worktree);
686 repo_conf = got_repo_get_gotconfig(repo);
689 * Priority of potential author information sources, from most
690 * significant to least significant:
691 * 1) work tree's .got/got.conf file
692 * 2) repository's got.conf file
696 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
698 if (got_revoked_signers == NULL)
699 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
702 if (got_revoked_signers) {
703 *revoked_signers = strdup(got_revoked_signers);
704 if (*revoked_signers == NULL)
705 return got_error_from_errno("strdup");
710 static const struct got_error *
711 get_signer_id(char **signer_id, struct got_repository *repo,
712 struct got_worktree *worktree)
714 const char *got_signer_id = NULL;
715 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
720 worktree_conf = got_worktree_get_gotconfig(worktree);
721 repo_conf = got_repo_get_gotconfig(repo);
724 * Priority of potential author information sources, from most
725 * significant to least significant:
726 * 1) work tree's .got/got.conf file
727 * 2) repository's got.conf file
731 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
732 if (got_signer_id == NULL)
733 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
736 *signer_id = strdup(got_signer_id);
737 if (*signer_id == NULL)
738 return got_error_from_errno("strdup");
743 static const struct got_error *
744 get_gitconfig_path(char **gitconfig_path)
746 const char *homedir = getenv("HOME");
748 *gitconfig_path = NULL;
750 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
751 return got_error_from_errno("asprintf");
757 static const struct got_error *
758 cmd_import(int argc, char *argv[])
760 const struct got_error *error = NULL;
761 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
762 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
763 const char *branch_name = "main";
764 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
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[0] == '-')
848 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
850 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
851 error = got_error_from_errno("asprintf");
855 error = got_ref_open(&branch_ref, repo, refname, 0);
857 if (error->code != GOT_ERR_NOT_REF)
860 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
861 "import target branch already exists");
865 path_dir = realpath(argv[0], NULL);
866 if (path_dir == NULL) {
867 error = got_error_from_errno2("realpath", argv[0]);
870 got_path_strip_trailing_slashes(path_dir);
873 * unveil(2) traverses exec(2); if an editor is used we have
874 * to apply unveil after the log message has been written.
876 if (logmsg == NULL || strlen(logmsg) == 0) {
877 error = get_editor(&editor);
881 error = collect_import_msg(&logmsg, &logmsg_path, editor,
884 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
891 if (unveil(path_dir, "r") != 0) {
892 error = got_error_from_errno2("unveil", path_dir);
898 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
905 error = got_repo_import(&new_commit_id, path_dir, logmsg,
906 author, &ignores, repo, import_progress, NULL);
913 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
920 error = got_ref_write(branch_ref, repo);
927 error = got_object_id_str(&id_str, new_commit_id);
934 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
936 if (error->code != GOT_ERR_NOT_REF) {
942 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
950 error = got_ref_write(head_ref, repo);
958 printf("Created branch %s with commit %s\n",
959 got_ref_get_name(branch_ref), id_str);
962 const struct got_error *pack_err =
963 got_repo_pack_fds_close(pack_fds);
967 if (preserve_logmsg) {
968 fprintf(stderr, "%s: log message preserved in %s\n",
969 getprogname(), logmsg_path);
970 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
971 error = got_error_from_errno2("unlink", logmsg_path);
980 free(gitconfig_path);
982 got_ref_close(branch_ref);
984 got_ref_close(head_ref);
991 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
992 "[-R reference] repository-url [directory]\n", getprogname());
996 struct got_fetch_progress_arg {
997 char last_scaled_size[FMT_SCALED_STRSIZE];
1002 struct got_repository *repo;
1005 int configs_created;
1007 struct got_pathlist_head *symrefs;
1008 struct got_pathlist_head *wanted_branches;
1009 struct got_pathlist_head *wanted_refs;
1013 const char *remote_repo_path;
1014 const char *git_url;
1015 int fetch_all_branches;
1016 int mirror_references;
1020 /* XXX forward declaration */
1021 static const struct got_error *
1022 create_config_files(const char *proto, const char *host, const char *port,
1023 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1024 int mirror_references, struct got_pathlist_head *symrefs,
1025 struct got_pathlist_head *wanted_branches,
1026 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1028 static const struct got_error *
1029 fetch_progress(void *arg, const char *message, off_t packfile_size,
1030 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1032 const struct got_error *err = NULL;
1033 struct got_fetch_progress_arg *a = arg;
1034 char scaled_size[FMT_SCALED_STRSIZE];
1035 int p_indexed, p_resolved;
1036 int print_size = 0, print_indexed = 0, print_resolved = 0;
1039 * In order to allow a failed clone to be resumed with 'got fetch'
1040 * we try to create configuration files as soon as possible.
1041 * Once the server has sent information about its default branch
1042 * we have all required information.
1044 if (a->create_configs && !a->configs_created &&
1045 !TAILQ_EMPTY(a->config_info.symrefs)) {
1046 err = create_config_files(a->config_info.proto,
1047 a->config_info.host, a->config_info.port,
1048 a->config_info.remote_repo_path,
1049 a->config_info.git_url,
1050 a->config_info.fetch_all_branches,
1051 a->config_info.mirror_references,
1052 a->config_info.symrefs,
1053 a->config_info.wanted_branches,
1054 a->config_info.wanted_refs, a->repo);
1057 a->configs_created = 1;
1060 if (a->verbosity < 0)
1063 if (message && message[0] != '\0') {
1064 printf("\rserver: %s", message);
1069 if (packfile_size > 0 || nobj_indexed > 0) {
1070 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1071 (a->last_scaled_size[0] == '\0' ||
1072 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1074 if (strlcpy(a->last_scaled_size, scaled_size,
1075 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1076 return got_error(GOT_ERR_NO_SPACE);
1078 if (nobj_indexed > 0) {
1079 p_indexed = (nobj_indexed * 100) / nobj_total;
1080 if (p_indexed != a->last_p_indexed) {
1081 a->last_p_indexed = p_indexed;
1086 if (nobj_resolved > 0) {
1087 p_resolved = (nobj_resolved * 100) /
1088 (nobj_total - nobj_loose);
1089 if (p_resolved != a->last_p_resolved) {
1090 a->last_p_resolved = p_resolved;
1098 if (print_size || print_indexed || print_resolved)
1101 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1103 printf("; indexing %d%%", p_indexed);
1105 printf("; resolving deltas %d%%", p_resolved);
1106 if (print_size || print_indexed || print_resolved)
1112 static const struct got_error *
1113 create_symref(const char *refname, struct got_reference *target_ref,
1114 int verbosity, struct got_repository *repo)
1116 const struct got_error *err;
1117 struct got_reference *head_symref;
1119 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1123 err = got_ref_write(head_symref, repo);
1124 if (err == NULL && verbosity > 0) {
1125 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1126 got_ref_get_name(target_ref));
1128 got_ref_close(head_symref);
1132 static const struct got_error *
1133 list_remote_refs(struct got_pathlist_head *symrefs,
1134 struct got_pathlist_head *refs)
1136 const struct got_error *err;
1137 struct got_pathlist_entry *pe;
1139 TAILQ_FOREACH(pe, symrefs, entry) {
1140 const char *refname = pe->path;
1141 const char *targetref = pe->data;
1143 printf("%s: %s\n", refname, targetref);
1146 TAILQ_FOREACH(pe, refs, entry) {
1147 const char *refname = pe->path;
1148 struct got_object_id *id = pe->data;
1151 err = got_object_id_str(&id_str, id);
1154 printf("%s: %s\n", refname, id_str);
1161 static const struct got_error *
1162 create_ref(const char *refname, struct got_object_id *id,
1163 int verbosity, struct got_repository *repo)
1165 const struct got_error *err = NULL;
1166 struct got_reference *ref;
1169 err = got_object_id_str(&id_str, id);
1173 err = got_ref_alloc(&ref, refname, id);
1177 err = got_ref_write(ref, repo);
1180 if (err == NULL && verbosity >= 0)
1181 printf("Created reference %s: %s\n", refname, id_str);
1188 match_wanted_ref(const char *refname, const char *wanted_ref)
1190 if (strncmp(refname, "refs/", 5) != 0)
1195 * Prevent fetching of references that won't make any
1196 * sense outside of the remote repository's context.
1198 if (strncmp(refname, "got/", 4) == 0)
1200 if (strncmp(refname, "remotes/", 8) == 0)
1203 if (strncmp(wanted_ref, "refs/", 5) == 0)
1206 /* Allow prefix match. */
1207 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1210 /* Allow exact match. */
1211 return (strcmp(refname, wanted_ref) == 0);
1215 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1217 struct got_pathlist_entry *pe;
1219 TAILQ_FOREACH(pe, wanted_refs, entry) {
1220 if (match_wanted_ref(refname, pe->path))
1227 static const struct got_error *
1228 create_wanted_ref(const char *refname, struct got_object_id *id,
1229 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1231 const struct got_error *err;
1232 char *remote_refname;
1234 if (strncmp("refs/", refname, 5) == 0)
1237 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1238 remote_repo_name, refname) == -1)
1239 return got_error_from_errno("asprintf");
1241 err = create_ref(remote_refname, id, verbosity, repo);
1242 free(remote_refname);
1246 static const struct got_error *
1247 create_gotconfig(const char *proto, const char *host, const char *port,
1248 const char *remote_repo_path, const char *default_branch,
1249 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1250 struct got_pathlist_head *wanted_refs, int mirror_references,
1251 struct got_repository *repo)
1253 const struct got_error *err = NULL;
1254 char *gotconfig_path = NULL;
1255 char *gotconfig = NULL;
1256 FILE *gotconfig_file = NULL;
1257 const char *branchname = NULL;
1258 char *branches = NULL, *refs = NULL;
1261 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1262 struct got_pathlist_entry *pe;
1263 TAILQ_FOREACH(pe, wanted_branches, entry) {
1265 branchname = pe->path;
1266 if (strncmp(branchname, "refs/heads/", 11) == 0)
1268 if (asprintf(&s, "%s\"%s\" ",
1269 branches ? branches : "", branchname) == -1) {
1270 err = got_error_from_errno("asprintf");
1276 } else if (!fetch_all_branches && default_branch) {
1277 branchname = default_branch;
1278 if (strncmp(branchname, "refs/heads/", 11) == 0)
1280 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1281 err = got_error_from_errno("asprintf");
1285 if (!TAILQ_EMPTY(wanted_refs)) {
1286 struct got_pathlist_entry *pe;
1287 TAILQ_FOREACH(pe, wanted_refs, entry) {
1289 const char *refname = pe->path;
1290 if (strncmp(refname, "refs/", 5) == 0)
1292 if (asprintf(&s, "%s\"%s\" ",
1293 refs ? refs : "", refname) == -1) {
1294 err = got_error_from_errno("asprintf");
1302 /* Create got.conf(5). */
1303 gotconfig_path = got_repo_get_path_gotconfig(repo);
1304 if (gotconfig_path == NULL) {
1305 err = got_error_from_errno("got_repo_get_path_gotconfig");
1308 gotconfig_file = fopen(gotconfig_path, "ae");
1309 if (gotconfig_file == NULL) {
1310 err = got_error_from_errno2("fopen", gotconfig_path);
1313 if (asprintf(&gotconfig,
1318 "\trepository \"%s\"\n"
1324 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1325 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1326 remote_repo_path, branches ? "\tbranch { " : "",
1327 branches ? branches : "", branches ? "}\n" : "",
1328 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1329 mirror_references ? "\tmirror_references yes\n" : "",
1330 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1331 err = got_error_from_errno("asprintf");
1334 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1335 if (n != strlen(gotconfig)) {
1336 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1341 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1342 err = got_error_from_errno2("fclose", gotconfig_path);
1343 free(gotconfig_path);
1348 static const struct got_error *
1349 create_gitconfig(const char *git_url, const char *default_branch,
1350 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1351 struct got_pathlist_head *wanted_refs, int mirror_references,
1352 struct got_repository *repo)
1354 const struct got_error *err = NULL;
1355 char *gitconfig_path = NULL;
1356 char *gitconfig = NULL;
1357 FILE *gitconfig_file = NULL;
1358 char *branches = NULL, *refs = NULL;
1359 const char *branchname;
1362 /* Create a config file Git can understand. */
1363 gitconfig_path = got_repo_get_path_gitconfig(repo);
1364 if (gitconfig_path == NULL) {
1365 err = got_error_from_errno("got_repo_get_path_gitconfig");
1368 gitconfig_file = fopen(gitconfig_path, "ae");
1369 if (gitconfig_file == NULL) {
1370 err = got_error_from_errno2("fopen", gitconfig_path);
1373 if (fetch_all_branches) {
1374 if (mirror_references) {
1375 if (asprintf(&branches,
1376 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1377 err = got_error_from_errno("asprintf");
1380 } else if (asprintf(&branches,
1381 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1382 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1383 err = got_error_from_errno("asprintf");
1386 } else if (!TAILQ_EMPTY(wanted_branches)) {
1387 struct got_pathlist_entry *pe;
1388 TAILQ_FOREACH(pe, wanted_branches, entry) {
1390 branchname = pe->path;
1391 if (strncmp(branchname, "refs/heads/", 11) == 0)
1393 if (mirror_references) {
1395 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1396 branches ? branches : "",
1397 branchname, branchname) == -1) {
1398 err = got_error_from_errno("asprintf");
1401 } else if (asprintf(&s,
1402 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1403 branches ? branches : "",
1404 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1405 branchname) == -1) {
1406 err = got_error_from_errno("asprintf");
1414 * If the server specified a default branch, use just that one.
1415 * Otherwise fall back to fetching all branches on next fetch.
1417 if (default_branch) {
1418 branchname = default_branch;
1419 if (strncmp(branchname, "refs/heads/", 11) == 0)
1422 branchname = "*"; /* fall back to all branches */
1423 if (mirror_references) {
1424 if (asprintf(&branches,
1425 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1426 branchname, branchname) == -1) {
1427 err = got_error_from_errno("asprintf");
1430 } else if (asprintf(&branches,
1431 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1432 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1433 branchname) == -1) {
1434 err = got_error_from_errno("asprintf");
1438 if (!TAILQ_EMPTY(wanted_refs)) {
1439 struct got_pathlist_entry *pe;
1440 TAILQ_FOREACH(pe, wanted_refs, entry) {
1442 const char *refname = pe->path;
1443 if (strncmp(refname, "refs/", 5) == 0)
1445 if (mirror_references) {
1447 "%s\tfetch = refs/%s:refs/%s\n",
1448 refs ? refs : "", refname, refname) == -1) {
1449 err = got_error_from_errno("asprintf");
1452 } else if (asprintf(&s,
1453 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1455 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1457 err = got_error_from_errno("asprintf");
1465 if (asprintf(&gitconfig,
1470 "\tfetch = refs/tags/*:refs/tags/*\n",
1471 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1472 refs ? refs : "") == -1) {
1473 err = got_error_from_errno("asprintf");
1476 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1477 if (n != strlen(gitconfig)) {
1478 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1482 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1483 err = got_error_from_errno2("fclose", gitconfig_path);
1484 free(gitconfig_path);
1489 static const struct got_error *
1490 create_config_files(const char *proto, const char *host, const char *port,
1491 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1492 int mirror_references, struct got_pathlist_head *symrefs,
1493 struct got_pathlist_head *wanted_branches,
1494 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 const char *default_branch = NULL;
1498 struct got_pathlist_entry *pe;
1501 * If we asked for a set of wanted branches then use the first
1504 if (!TAILQ_EMPTY(wanted_branches)) {
1505 pe = TAILQ_FIRST(wanted_branches);
1506 default_branch = pe->path;
1508 /* First HEAD ref listed by server is the default branch. */
1509 TAILQ_FOREACH(pe, symrefs, entry) {
1510 const char *refname = pe->path;
1511 const char *target = pe->data;
1513 if (strcmp(refname, GOT_REF_HEAD) != 0)
1516 default_branch = target;
1521 /* Create got.conf(5). */
1522 err = create_gotconfig(proto, host, port, remote_repo_path,
1523 default_branch, fetch_all_branches, wanted_branches,
1524 wanted_refs, mirror_references, repo);
1528 /* Create a config file Git can understand. */
1529 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1530 wanted_branches, wanted_refs, mirror_references, repo);
1533 static const struct got_error *
1534 cmd_clone(int argc, char *argv[])
1536 const struct got_error *error = NULL;
1537 const char *uri, *dirname;
1538 char *proto, *host, *port, *repo_name, *server_path;
1539 char *default_destdir = NULL, *id_str = NULL;
1540 const char *repo_path;
1541 struct got_repository *repo = NULL;
1542 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1543 struct got_pathlist_entry *pe;
1544 struct got_object_id *pack_hash = NULL;
1545 int ch, fetchfd = -1, fetchstatus;
1546 pid_t fetchpid = -1;
1547 struct got_fetch_progress_arg fpa;
1548 char *git_url = NULL;
1549 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1550 int list_refs_only = 0;
1551 int *pack_fds = NULL;
1554 TAILQ_INIT(&symrefs);
1555 TAILQ_INIT(&wanted_branches);
1556 TAILQ_INIT(&wanted_refs);
1558 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1561 fetch_all_branches = 1;
1564 error = got_pathlist_append(&wanted_branches,
1573 mirror_references = 1;
1578 else if (verbosity < 3)
1585 error = got_pathlist_append(&wanted_refs,
1598 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1599 option_conflict('a', 'b');
1600 if (list_refs_only) {
1601 if (!TAILQ_EMPTY(&wanted_branches))
1602 option_conflict('l', 'b');
1603 if (fetch_all_branches)
1604 option_conflict('l', 'a');
1605 if (mirror_references)
1606 option_conflict('l', 'm');
1607 if (!TAILQ_EMPTY(&wanted_refs))
1608 option_conflict('l', 'R');
1620 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1625 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1626 host, port ? ":" : "", port ? port : "",
1627 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1628 error = got_error_from_errno("asprintf");
1632 if (strcmp(proto, "git") == 0) {
1634 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1635 "sendfd dns inet unveil", NULL) == -1)
1638 } else if (strcmp(proto, "git+ssh") == 0 ||
1639 strcmp(proto, "ssh") == 0) {
1641 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1642 "sendfd unveil", NULL) == -1)
1645 } else if (strcmp(proto, "http") == 0 ||
1646 strcmp(proto, "git+http") == 0) {
1647 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1650 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1653 if (dirname == NULL) {
1654 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1655 error = got_error_from_errno("asprintf");
1658 repo_path = default_destdir;
1660 repo_path = dirname;
1662 if (!list_refs_only) {
1663 error = got_path_mkdir(repo_path);
1665 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1666 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1668 if (!got_path_dir_is_empty(repo_path)) {
1669 error = got_error_path(repo_path,
1670 GOT_ERR_DIR_NOT_EMPTY);
1675 error = got_dial_apply_unveil(proto);
1679 error = apply_unveil(repo_path, 0, NULL);
1684 printf("Connecting to %s%s%s\n", host,
1685 port ? ":" : "", port ? port : "");
1687 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1688 server_path, verbosity);
1692 if (!list_refs_only) {
1693 error = got_repo_init(repo_path);
1696 error = got_repo_pack_fds_open(&pack_fds);
1699 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1704 fpa.last_scaled_size[0] = '\0';
1705 fpa.last_p_indexed = -1;
1706 fpa.last_p_resolved = -1;
1707 fpa.verbosity = verbosity;
1708 fpa.create_configs = 1;
1709 fpa.configs_created = 0;
1711 fpa.config_info.symrefs = &symrefs;
1712 fpa.config_info.wanted_branches = &wanted_branches;
1713 fpa.config_info.wanted_refs = &wanted_refs;
1714 fpa.config_info.proto = proto;
1715 fpa.config_info.host = host;
1716 fpa.config_info.port = port;
1717 fpa.config_info.remote_repo_path = server_path;
1718 fpa.config_info.git_url = git_url;
1719 fpa.config_info.fetch_all_branches = fetch_all_branches;
1720 fpa.config_info.mirror_references = mirror_references;
1721 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1722 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1723 fetch_all_branches, &wanted_branches, &wanted_refs,
1724 list_refs_only, verbosity, fetchfd, repo,
1725 fetch_progress, &fpa);
1729 if (list_refs_only) {
1730 error = list_remote_refs(&symrefs, &refs);
1734 if (pack_hash == NULL) {
1735 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1736 "server sent an empty pack file");
1739 error = got_object_id_str(&id_str, pack_hash);
1743 printf("\nFetched %s.pack\n", id_str);
1746 /* Set up references provided with the pack file. */
1747 TAILQ_FOREACH(pe, &refs, entry) {
1748 const char *refname = pe->path;
1749 struct got_object_id *id = pe->data;
1750 char *remote_refname;
1752 if (is_wanted_ref(&wanted_refs, refname) &&
1753 !mirror_references) {
1754 error = create_wanted_ref(refname, id,
1755 GOT_FETCH_DEFAULT_REMOTE_NAME,
1756 verbosity - 1, repo);
1762 error = create_ref(refname, id, verbosity - 1, repo);
1766 if (mirror_references)
1769 if (strncmp("refs/heads/", refname, 11) != 0)
1772 if (asprintf(&remote_refname,
1773 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1774 refname + 11) == -1) {
1775 error = got_error_from_errno("asprintf");
1778 error = create_ref(remote_refname, id, verbosity - 1, repo);
1779 free(remote_refname);
1784 /* Set the HEAD reference if the server provided one. */
1785 TAILQ_FOREACH(pe, &symrefs, entry) {
1786 struct got_reference *target_ref;
1787 const char *refname = pe->path;
1788 const char *target = pe->data;
1789 char *remote_refname = NULL, *remote_target = NULL;
1791 if (strcmp(refname, GOT_REF_HEAD) != 0)
1794 error = got_ref_open(&target_ref, repo, target, 0);
1796 if (error->code == GOT_ERR_NOT_REF) {
1803 error = create_symref(refname, target_ref, verbosity, repo);
1804 got_ref_close(target_ref);
1808 if (mirror_references)
1811 if (strncmp("refs/heads/", target, 11) != 0)
1814 if (asprintf(&remote_refname,
1815 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1817 error = got_error_from_errno("asprintf");
1820 if (asprintf(&remote_target,
1821 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1822 target + 11) == -1) {
1823 error = got_error_from_errno("asprintf");
1824 free(remote_refname);
1827 error = got_ref_open(&target_ref, repo, remote_target, 0);
1829 free(remote_refname);
1830 free(remote_target);
1831 if (error->code == GOT_ERR_NOT_REF) {
1837 error = create_symref(remote_refname, target_ref,
1838 verbosity - 1, repo);
1839 free(remote_refname);
1840 free(remote_target);
1841 got_ref_close(target_ref);
1847 * We failed to set the HEAD reference. If we asked for
1848 * a set of wanted branches use the first of one of those
1849 * which could be fetched instead.
1851 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1852 const char *target = pe->path;
1853 struct got_reference *target_ref;
1855 error = got_ref_open(&target_ref, repo, target, 0);
1857 if (error->code == GOT_ERR_NOT_REF) {
1864 error = create_symref(GOT_REF_HEAD, target_ref,
1866 got_ref_close(target_ref);
1874 printf("Created %s repository '%s'\n",
1875 mirror_references ? "mirrored" : "cloned", repo_path);
1878 const struct got_error *pack_err =
1879 got_repo_pack_fds_close(pack_fds);
1884 if (kill(fetchpid, SIGTERM) == -1)
1885 error = got_error_from_errno("kill");
1886 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1887 error = got_error_from_errno("waitpid");
1889 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1890 error = got_error_from_errno("close");
1892 const struct got_error *close_err = got_repo_close(repo);
1896 TAILQ_FOREACH(pe, &refs, entry) {
1897 free((void *)pe->path);
1900 got_pathlist_free(&refs);
1901 TAILQ_FOREACH(pe, &symrefs, entry) {
1902 free((void *)pe->path);
1905 got_pathlist_free(&symrefs);
1906 got_pathlist_free(&wanted_branches);
1907 got_pathlist_free(&wanted_refs);
1914 free(default_destdir);
1919 static const struct got_error *
1920 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1921 int replace_tags, int verbosity, struct got_repository *repo)
1923 const struct got_error *err = NULL;
1924 char *new_id_str = NULL;
1925 struct got_object_id *old_id = NULL;
1927 err = got_object_id_str(&new_id_str, new_id);
1931 if (!replace_tags &&
1932 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1933 err = got_ref_resolve(&old_id, repo, ref);
1936 if (got_object_id_cmp(old_id, new_id) == 0)
1938 if (verbosity >= 0) {
1939 printf("Rejecting update of existing tag %s: %s\n",
1940 got_ref_get_name(ref), new_id_str);
1945 if (got_ref_is_symbolic(ref)) {
1946 if (verbosity >= 0) {
1947 printf("Replacing reference %s: %s\n",
1948 got_ref_get_name(ref),
1949 got_ref_get_symref_target(ref));
1951 err = got_ref_change_symref_to_ref(ref, new_id);
1954 err = got_ref_write(ref, repo);
1958 err = got_ref_resolve(&old_id, repo, ref);
1961 if (got_object_id_cmp(old_id, new_id) == 0)
1964 err = got_ref_change_ref(ref, new_id);
1967 err = got_ref_write(ref, repo);
1973 printf("Updated %s: %s\n", got_ref_get_name(ref),
1981 static const struct got_error *
1982 update_symref(const char *refname, struct got_reference *target_ref,
1983 int verbosity, struct got_repository *repo)
1985 const struct got_error *err = NULL, *unlock_err;
1986 struct got_reference *symref;
1987 int symref_is_locked = 0;
1989 err = got_ref_open(&symref, repo, refname, 1);
1991 if (err->code != GOT_ERR_NOT_REF)
1993 err = got_ref_alloc_symref(&symref, refname, target_ref);
1997 err = got_ref_write(symref, repo);
2002 printf("Created reference %s: %s\n",
2003 got_ref_get_name(symref),
2004 got_ref_get_symref_target(symref));
2006 symref_is_locked = 1;
2008 if (strcmp(got_ref_get_symref_target(symref),
2009 got_ref_get_name(target_ref)) == 0)
2012 err = got_ref_change_symref(symref,
2013 got_ref_get_name(target_ref));
2017 err = got_ref_write(symref, repo);
2022 printf("Updated %s: %s\n", got_ref_get_name(symref),
2023 got_ref_get_symref_target(symref));
2027 if (symref_is_locked) {
2028 unlock_err = got_ref_unlock(symref);
2029 if (unlock_err && err == NULL)
2032 got_ref_close(symref);
2039 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
2040 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
2041 "[remote-repository-name]\n",
2046 static const struct got_error *
2047 delete_missing_ref(struct got_reference *ref,
2048 int verbosity, struct got_repository *repo)
2050 const struct got_error *err = NULL;
2051 struct got_object_id *id = NULL;
2052 char *id_str = NULL;
2054 if (got_ref_is_symbolic(ref)) {
2055 err = got_ref_delete(ref, repo);
2058 if (verbosity >= 0) {
2059 printf("Deleted %s: %s\n",
2060 got_ref_get_name(ref),
2061 got_ref_get_symref_target(ref));
2064 err = got_ref_resolve(&id, repo, ref);
2067 err = got_object_id_str(&id_str, id);
2071 err = got_ref_delete(ref, repo);
2074 if (verbosity >= 0) {
2075 printf("Deleted %s: %s\n",
2076 got_ref_get_name(ref), id_str);
2085 static const struct got_error *
2086 delete_missing_refs(struct got_pathlist_head *their_refs,
2087 struct got_pathlist_head *their_symrefs,
2088 const struct got_remote_repo *remote,
2089 int verbosity, struct got_repository *repo)
2091 const struct got_error *err = NULL, *unlock_err;
2092 struct got_reflist_head my_refs;
2093 struct got_reflist_entry *re;
2094 struct got_pathlist_entry *pe;
2095 char *remote_namespace = NULL;
2096 char *local_refname = NULL;
2098 TAILQ_INIT(&my_refs);
2100 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2102 return got_error_from_errno("asprintf");
2104 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2108 TAILQ_FOREACH(re, &my_refs, entry) {
2109 const char *refname = got_ref_get_name(re->ref);
2110 const char *their_refname;
2112 if (remote->mirror_references) {
2113 their_refname = refname;
2115 if (strncmp(refname, remote_namespace,
2116 strlen(remote_namespace)) == 0) {
2117 if (strcmp(refname + strlen(remote_namespace),
2120 if (asprintf(&local_refname, "refs/heads/%s",
2121 refname + strlen(remote_namespace)) == -1) {
2122 err = got_error_from_errno("asprintf");
2125 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2128 their_refname = local_refname;
2131 TAILQ_FOREACH(pe, their_refs, entry) {
2132 if (strcmp(their_refname, pe->path) == 0)
2138 TAILQ_FOREACH(pe, their_symrefs, entry) {
2139 if (strcmp(their_refname, pe->path) == 0)
2145 err = delete_missing_ref(re->ref, verbosity, repo);
2149 if (local_refname) {
2150 struct got_reference *ref;
2151 err = got_ref_open(&ref, repo, local_refname, 1);
2153 if (err->code != GOT_ERR_NOT_REF)
2155 free(local_refname);
2156 local_refname = NULL;
2159 err = delete_missing_ref(ref, verbosity, repo);
2162 unlock_err = got_ref_unlock(ref);
2164 if (unlock_err && err == NULL) {
2169 free(local_refname);
2170 local_refname = NULL;
2174 free(remote_namespace);
2175 free(local_refname);
2179 static const struct got_error *
2180 update_wanted_ref(const char *refname, struct got_object_id *id,
2181 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2183 const struct got_error *err, *unlock_err;
2184 char *remote_refname;
2185 struct got_reference *ref;
2187 if (strncmp("refs/", refname, 5) == 0)
2190 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2191 remote_repo_name, refname) == -1)
2192 return got_error_from_errno("asprintf");
2194 err = got_ref_open(&ref, repo, remote_refname, 1);
2196 if (err->code != GOT_ERR_NOT_REF)
2198 err = create_ref(remote_refname, id, verbosity, repo);
2200 err = update_ref(ref, id, 0, verbosity, repo);
2201 unlock_err = got_ref_unlock(ref);
2202 if (unlock_err && err == NULL)
2207 free(remote_refname);
2211 static const struct got_error *
2212 delete_ref(struct got_repository *repo, struct got_reference *ref)
2214 const struct got_error *err = NULL;
2215 struct got_object_id *id = NULL;
2216 char *id_str = NULL;
2219 if (got_ref_is_symbolic(ref)) {
2220 target = got_ref_get_symref_target(ref);
2222 err = got_ref_resolve(&id, repo, ref);
2225 err = got_object_id_str(&id_str, id);
2231 err = got_ref_delete(ref, repo);
2235 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2242 static const struct got_error *
2243 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2245 const struct got_error *err = NULL;
2246 struct got_reflist_head refs;
2247 struct got_reflist_entry *re;
2252 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2253 err = got_error_from_errno("asprintf");
2256 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2260 TAILQ_FOREACH(re, &refs, entry)
2261 delete_ref(repo, re->ref);
2263 got_ref_list_free(&refs);
2267 static const struct got_error *
2268 cmd_fetch(int argc, char *argv[])
2270 const struct got_error *error = NULL, *unlock_err;
2271 char *cwd = NULL, *repo_path = NULL;
2272 const char *remote_name;
2273 char *proto = NULL, *host = NULL, *port = NULL;
2274 char *repo_name = NULL, *server_path = NULL;
2275 const struct got_remote_repo *remotes, *remote = NULL;
2277 char *id_str = NULL;
2278 struct got_repository *repo = NULL;
2279 struct got_worktree *worktree = NULL;
2280 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2281 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2282 struct got_pathlist_entry *pe;
2283 struct got_object_id *pack_hash = NULL;
2284 int i, ch, fetchfd = -1, fetchstatus;
2285 pid_t fetchpid = -1;
2286 struct got_fetch_progress_arg fpa;
2287 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2288 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2289 int *pack_fds = NULL;
2292 TAILQ_INIT(&symrefs);
2293 TAILQ_INIT(&wanted_branches);
2294 TAILQ_INIT(&wanted_refs);
2296 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2299 fetch_all_branches = 1;
2302 error = got_pathlist_append(&wanted_branches,
2314 repo_path = realpath(optarg, NULL);
2315 if (repo_path == NULL)
2316 return got_error_from_errno2("realpath",
2318 got_path_strip_trailing_slashes(repo_path);
2326 else if (verbosity < 3)
2333 error = got_pathlist_append(&wanted_refs,
2349 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2350 option_conflict('a', 'b');
2351 if (list_refs_only) {
2352 if (!TAILQ_EMPTY(&wanted_branches))
2353 option_conflict('l', 'b');
2354 if (fetch_all_branches)
2355 option_conflict('l', 'a');
2357 option_conflict('l', 'd');
2359 option_conflict('l', 'X');
2361 if (delete_remote) {
2362 if (fetch_all_branches)
2363 option_conflict('X', 'a');
2364 if (!TAILQ_EMPTY(&wanted_branches))
2365 option_conflict('X', 'b');
2367 option_conflict('X', 'd');
2369 option_conflict('X', 't');
2370 if (!TAILQ_EMPTY(&wanted_refs))
2371 option_conflict('X', 'R');
2376 errx(1, "-X option requires a remote name");
2377 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2378 } else if (argc == 1)
2379 remote_name = argv[0];
2383 cwd = getcwd(NULL, 0);
2385 error = got_error_from_errno("getcwd");
2389 error = got_repo_pack_fds_open(&pack_fds);
2393 if (repo_path == NULL) {
2394 error = got_worktree_open(&worktree, cwd);
2395 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2401 strdup(got_worktree_get_repo_path(worktree));
2402 if (repo_path == NULL)
2403 error = got_error_from_errno("strdup");
2407 repo_path = strdup(cwd);
2408 if (repo_path == NULL) {
2409 error = got_error_from_errno("strdup");
2415 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2419 if (delete_remote) {
2420 error = delete_refs_for_remote(repo, remote_name);
2421 goto done; /* nothing else to do */
2425 worktree_conf = got_worktree_get_gotconfig(worktree);
2426 if (worktree_conf) {
2427 got_gotconfig_get_remotes(&nremotes, &remotes,
2429 for (i = 0; i < nremotes; i++) {
2430 if (strcmp(remotes[i].name, remote_name) == 0) {
2431 remote = &remotes[i];
2437 if (remote == NULL) {
2438 repo_conf = got_repo_get_gotconfig(repo);
2440 got_gotconfig_get_remotes(&nremotes, &remotes,
2442 for (i = 0; i < nremotes; i++) {
2443 if (strcmp(remotes[i].name, remote_name) == 0) {
2444 remote = &remotes[i];
2450 if (remote == NULL) {
2451 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2452 for (i = 0; i < nremotes; i++) {
2453 if (strcmp(remotes[i].name, remote_name) == 0) {
2454 remote = &remotes[i];
2459 if (remote == NULL) {
2460 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2464 if (TAILQ_EMPTY(&wanted_branches)) {
2465 if (!fetch_all_branches)
2466 fetch_all_branches = remote->fetch_all_branches;
2467 for (i = 0; i < remote->nfetch_branches; i++) {
2468 got_pathlist_append(&wanted_branches,
2469 remote->fetch_branches[i], NULL);
2472 if (TAILQ_EMPTY(&wanted_refs)) {
2473 for (i = 0; i < remote->nfetch_refs; i++) {
2474 got_pathlist_append(&wanted_refs,
2475 remote->fetch_refs[i], NULL);
2479 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2480 &repo_name, remote->fetch_url);
2484 if (strcmp(proto, "git") == 0) {
2486 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2487 "sendfd dns inet unveil", NULL) == -1)
2490 } else if (strcmp(proto, "git+ssh") == 0 ||
2491 strcmp(proto, "ssh") == 0) {
2493 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2494 "sendfd unveil", NULL) == -1)
2497 } else if (strcmp(proto, "http") == 0 ||
2498 strcmp(proto, "git+http") == 0) {
2499 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2502 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2506 error = got_dial_apply_unveil(proto);
2510 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2515 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2516 port ? ":" : "", port ? port : "");
2518 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2519 server_path, verbosity);
2523 fpa.last_scaled_size[0] = '\0';
2524 fpa.last_p_indexed = -1;
2525 fpa.last_p_resolved = -1;
2526 fpa.verbosity = verbosity;
2528 fpa.create_configs = 0;
2529 fpa.configs_created = 0;
2530 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2531 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2532 remote->mirror_references, fetch_all_branches, &wanted_branches,
2533 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2534 fetch_progress, &fpa);
2538 if (list_refs_only) {
2539 error = list_remote_refs(&symrefs, &refs);
2543 if (pack_hash == NULL) {
2545 printf("Already up-to-date\n");
2546 } else if (verbosity >= 0) {
2547 error = got_object_id_str(&id_str, pack_hash);
2550 printf("\nFetched %s.pack\n", id_str);
2555 /* Update references provided with the pack file. */
2556 TAILQ_FOREACH(pe, &refs, entry) {
2557 const char *refname = pe->path;
2558 struct got_object_id *id = pe->data;
2559 struct got_reference *ref;
2560 char *remote_refname;
2562 if (is_wanted_ref(&wanted_refs, refname) &&
2563 !remote->mirror_references) {
2564 error = update_wanted_ref(refname, id,
2565 remote->name, verbosity, repo);
2571 if (remote->mirror_references ||
2572 strncmp("refs/tags/", refname, 10) == 0) {
2573 error = got_ref_open(&ref, repo, refname, 1);
2575 if (error->code != GOT_ERR_NOT_REF)
2577 error = create_ref(refname, id, verbosity,
2582 error = update_ref(ref, id, replace_tags,
2584 unlock_err = got_ref_unlock(ref);
2585 if (unlock_err && error == NULL)
2591 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2592 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2593 remote_name, refname + 11) == -1) {
2594 error = got_error_from_errno("asprintf");
2598 error = got_ref_open(&ref, repo, remote_refname, 1);
2600 if (error->code != GOT_ERR_NOT_REF)
2602 error = create_ref(remote_refname, id,
2607 error = update_ref(ref, id, replace_tags,
2609 unlock_err = got_ref_unlock(ref);
2610 if (unlock_err && error == NULL)
2617 /* Also create a local branch if none exists yet. */
2618 error = got_ref_open(&ref, repo, refname, 1);
2620 if (error->code != GOT_ERR_NOT_REF)
2622 error = create_ref(refname, id, verbosity,
2627 unlock_err = got_ref_unlock(ref);
2628 if (unlock_err && error == NULL)
2635 error = delete_missing_refs(&refs, &symrefs, remote,
2641 if (!remote->mirror_references) {
2642 /* Update remote HEAD reference if the server provided one. */
2643 TAILQ_FOREACH(pe, &symrefs, entry) {
2644 struct got_reference *target_ref;
2645 const char *refname = pe->path;
2646 const char *target = pe->data;
2647 char *remote_refname = NULL, *remote_target = NULL;
2649 if (strcmp(refname, GOT_REF_HEAD) != 0)
2652 if (strncmp("refs/heads/", target, 11) != 0)
2655 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2656 remote->name, refname) == -1) {
2657 error = got_error_from_errno("asprintf");
2660 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2661 remote->name, target + 11) == -1) {
2662 error = got_error_from_errno("asprintf");
2663 free(remote_refname);
2667 error = got_ref_open(&target_ref, repo, remote_target,
2670 free(remote_refname);
2671 free(remote_target);
2672 if (error->code == GOT_ERR_NOT_REF) {
2678 error = update_symref(remote_refname, target_ref,
2680 free(remote_refname);
2681 free(remote_target);
2682 got_ref_close(target_ref);
2689 if (kill(fetchpid, SIGTERM) == -1)
2690 error = got_error_from_errno("kill");
2691 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2692 error = got_error_from_errno("waitpid");
2694 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2695 error = got_error_from_errno("close");
2697 const struct got_error *close_err = got_repo_close(repo);
2702 got_worktree_close(worktree);
2704 const struct got_error *pack_err =
2705 got_repo_pack_fds_close(pack_fds);
2709 TAILQ_FOREACH(pe, &refs, entry) {
2710 free((void *)pe->path);
2713 got_pathlist_free(&refs);
2714 TAILQ_FOREACH(pe, &symrefs, entry) {
2715 free((void *)pe->path);
2718 got_pathlist_free(&symrefs);
2719 got_pathlist_free(&wanted_branches);
2720 got_pathlist_free(&wanted_refs);
2735 usage_checkout(void)
2737 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2738 "[-p prefix] [-q] repository-path [worktree-path]\n",
2744 show_worktree_base_ref_warning(void)
2746 fprintf(stderr, "%s: warning: could not create a reference "
2747 "to the work tree's base commit; the commit could be "
2748 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2749 "repository writable and running 'got update' will prevent this\n",
2753 struct got_checkout_progress_arg {
2754 const char *worktree_path;
2755 int had_base_commit_ref_error;
2759 static const struct got_error *
2760 checkout_progress(void *arg, unsigned char status, const char *path)
2762 struct got_checkout_progress_arg *a = arg;
2764 /* Base commit bump happens silently. */
2765 if (status == GOT_STATUS_BUMP_BASE)
2768 if (status == GOT_STATUS_BASE_REF_ERR) {
2769 a->had_base_commit_ref_error = 1;
2773 while (path[0] == '/')
2776 if (a->verbosity >= 0)
2777 printf("%c %s/%s\n", status, a->worktree_path, path);
2782 static const struct got_error *
2783 check_cancelled(void *arg)
2785 if (sigint_received || sigpipe_received)
2786 return got_error(GOT_ERR_CANCELLED);
2790 static const struct got_error *
2791 check_linear_ancestry(struct got_object_id *commit_id,
2792 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2793 struct got_repository *repo)
2795 const struct got_error *err = NULL;
2796 struct got_object_id *yca_id;
2798 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2799 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2804 return got_error(GOT_ERR_ANCESTRY);
2807 * Require a straight line of history between the target commit
2808 * and the work tree's base commit.
2810 * Non-linear situations such as this require a rebase:
2812 * (commit) D F (base_commit)
2820 * 'got update' only handles linear cases:
2821 * Update forwards in time: A (base/yca) - B - C - D (commit)
2822 * Update backwards in time: D (base) - C - B - A (commit/yca)
2824 if (allow_forwards_in_time_only) {
2825 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2826 return got_error(GOT_ERR_ANCESTRY);
2827 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2828 got_object_id_cmp(base_commit_id, yca_id) != 0)
2829 return got_error(GOT_ERR_ANCESTRY);
2835 static const struct got_error *
2836 check_same_branch(struct got_object_id *commit_id,
2837 struct got_reference *head_ref, struct got_object_id *yca_id,
2838 struct got_repository *repo)
2840 const struct got_error *err = NULL;
2841 struct got_commit_graph *graph = NULL;
2842 struct got_object_id *head_commit_id = NULL;
2843 int is_same_branch = 0;
2845 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2849 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2853 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2858 err = got_commit_graph_open(&graph, "/", 1);
2862 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2863 check_cancelled, NULL);
2868 struct got_object_id *id;
2869 err = got_commit_graph_iter_next(&id, graph, repo,
2870 check_cancelled, NULL);
2872 if (err->code == GOT_ERR_ITER_COMPLETED)
2878 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2880 if (got_object_id_cmp(id, commit_id) == 0) {
2888 got_commit_graph_close(graph);
2889 free(head_commit_id);
2890 if (!err && !is_same_branch)
2891 err = got_error(GOT_ERR_ANCESTRY);
2895 static const struct got_error *
2896 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2898 static char msg[512];
2899 const char *branch_name;
2901 if (got_ref_is_symbolic(ref))
2902 branch_name = got_ref_get_symref_target(ref);
2904 branch_name = got_ref_get_name(ref);
2906 if (strncmp("refs/heads/", branch_name, 11) == 0)
2909 snprintf(msg, sizeof(msg),
2910 "target commit is not contained in branch '%s'; "
2911 "the branch to use must be specified with -b; "
2912 "if necessary a new branch can be created for "
2913 "this commit with 'got branch -c %s BRANCH_NAME'",
2914 branch_name, commit_id_str);
2916 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2919 static const struct got_error *
2920 cmd_checkout(int argc, char *argv[])
2922 const struct got_error *error = NULL;
2923 struct got_repository *repo = NULL;
2924 struct got_reference *head_ref = NULL, *ref = NULL;
2925 struct got_worktree *worktree = NULL;
2926 char *repo_path = NULL;
2927 char *worktree_path = NULL;
2928 const char *path_prefix = "";
2929 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2930 char *commit_id_str = NULL;
2931 struct got_object_id *commit_id = NULL;
2933 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2934 struct got_pathlist_head paths;
2935 struct got_checkout_progress_arg cpa;
2936 int *pack_fds = NULL;
2940 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2943 branch_name = optarg;
2946 commit_id_str = strdup(optarg);
2947 if (commit_id_str == NULL)
2948 return got_error_from_errno("strdup");
2954 path_prefix = optarg;
2969 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2970 "unveil", NULL) == -1)
2974 char *base, *dotgit;
2976 repo_path = realpath(argv[0], NULL);
2977 if (repo_path == NULL)
2978 return got_error_from_errno2("realpath", argv[0]);
2979 cwd = getcwd(NULL, 0);
2981 error = got_error_from_errno("getcwd");
2988 error = got_path_basename(&base, path);
2991 dotgit = strstr(base, ".git");
2994 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2995 error = got_error_from_errno("asprintf");
3000 } else if (argc == 2) {
3001 repo_path = realpath(argv[0], NULL);
3002 if (repo_path == NULL) {
3003 error = got_error_from_errno2("realpath", argv[0]);
3006 worktree_path = realpath(argv[1], NULL);
3007 if (worktree_path == NULL) {
3008 if (errno != ENOENT) {
3009 error = got_error_from_errno2("realpath",
3013 worktree_path = strdup(argv[1]);
3014 if (worktree_path == NULL) {
3015 error = got_error_from_errno("strdup");
3022 got_path_strip_trailing_slashes(repo_path);
3023 got_path_strip_trailing_slashes(worktree_path);
3025 error = got_repo_pack_fds_open(&pack_fds);
3029 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3033 /* Pre-create work tree path for unveil(2) */
3034 error = got_path_mkdir(worktree_path);
3036 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3037 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3039 if (!allow_nonempty &&
3040 !got_path_dir_is_empty(worktree_path)) {
3041 error = got_error_path(worktree_path,
3042 GOT_ERR_DIR_NOT_EMPTY);
3047 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3051 error = got_ref_open(&head_ref, repo, branch_name, 0);
3055 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3056 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3059 error = got_worktree_open(&worktree, worktree_path);
3063 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3067 if (!same_path_prefix) {
3068 error = got_error(GOT_ERR_PATH_PREFIX);
3072 if (commit_id_str) {
3073 struct got_reflist_head refs;
3075 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3079 error = got_repo_match_object_id(&commit_id, NULL,
3080 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3081 got_ref_list_free(&refs);
3084 error = check_linear_ancestry(commit_id,
3085 got_worktree_get_base_commit_id(worktree), 0, repo);
3086 if (error != NULL) {
3087 if (error->code == GOT_ERR_ANCESTRY) {
3088 error = checkout_ancestry_error(
3089 head_ref, commit_id_str);
3093 error = check_same_branch(commit_id, head_ref, NULL, repo);
3095 if (error->code == GOT_ERR_ANCESTRY) {
3096 error = checkout_ancestry_error(
3097 head_ref, commit_id_str);
3101 error = got_worktree_set_base_commit_id(worktree, repo,
3105 /* Expand potentially abbreviated commit ID string. */
3106 free(commit_id_str);
3107 error = got_object_id_str(&commit_id_str, commit_id);
3111 commit_id = got_object_id_dup(
3112 got_worktree_get_base_commit_id(worktree));
3113 if (commit_id == NULL) {
3114 error = got_error_from_errno("got_object_id_dup");
3117 error = got_object_id_str(&commit_id_str, commit_id);
3122 error = got_pathlist_append(&paths, "", NULL);
3125 cpa.worktree_path = worktree_path;
3126 cpa.had_base_commit_ref_error = 0;
3127 cpa.verbosity = verbosity;
3128 error = got_worktree_checkout_files(worktree, &paths, repo,
3129 checkout_progress, &cpa, check_cancelled, NULL);
3133 if (got_ref_is_symbolic(head_ref)) {
3134 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3137 refname = got_ref_get_name(ref);
3139 refname = got_ref_get_name(head_ref);
3140 printf("Checked out %s: %s\n", refname, commit_id_str);
3141 printf("Now shut up and hack\n");
3142 if (cpa.had_base_commit_ref_error)
3143 show_worktree_base_ref_warning();
3146 const struct got_error *pack_err =
3147 got_repo_pack_fds_close(pack_fds);
3152 got_ref_close(head_ref);
3155 got_pathlist_free(&paths);
3156 free(commit_id_str);
3159 free(worktree_path);
3164 struct got_update_progress_arg {
3176 print_update_progress_stats(struct got_update_progress_arg *upa)
3178 if (!upa->did_something)
3181 if (upa->conflicts > 0)
3182 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3183 if (upa->obstructed > 0)
3184 printf("File paths obstructed by a non-regular file: %d\n",
3186 if (upa->not_updated > 0)
3187 printf("Files not updated because of existing merge "
3188 "conflicts: %d\n", upa->not_updated);
3192 * The meaning of some status codes differs between merge-style operations and
3193 * update operations. For example, the ! status code means "file was missing"
3194 * if changes were merged into the work tree, and "missing file was restored"
3195 * if the work tree was updated. This function should be used by any operation
3196 * which merges changes into the work tree without updating the work tree.
3199 print_merge_progress_stats(struct got_update_progress_arg *upa)
3201 if (!upa->did_something)
3204 if (upa->conflicts > 0)
3205 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3206 if (upa->obstructed > 0)
3207 printf("File paths obstructed by a non-regular file: %d\n",
3209 if (upa->missing > 0)
3210 printf("Files which had incoming changes but could not be "
3211 "found in the work tree: %d\n", upa->missing);
3212 if (upa->not_deleted > 0)
3213 printf("Files not deleted due to differences in deleted "
3214 "content: %d\n", upa->not_deleted);
3215 if (upa->unversioned > 0)
3216 printf("Files not merged because an unversioned file was "
3217 "found in the work tree: %d\n", upa->unversioned);
3223 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3229 static const struct got_error *
3230 update_progress(void *arg, unsigned char status, const char *path)
3232 struct got_update_progress_arg *upa = arg;
3234 if (status == GOT_STATUS_EXISTS ||
3235 status == GOT_STATUS_BASE_REF_ERR)
3238 upa->did_something = 1;
3240 /* Base commit bump happens silently. */
3241 if (status == GOT_STATUS_BUMP_BASE)
3244 if (status == GOT_STATUS_CONFLICT)
3246 if (status == GOT_STATUS_OBSTRUCTED)
3248 if (status == GOT_STATUS_CANNOT_UPDATE)
3250 if (status == GOT_STATUS_MISSING)
3252 if (status == GOT_STATUS_CANNOT_DELETE)
3254 if (status == GOT_STATUS_UNVERSIONED)
3257 while (path[0] == '/')
3259 if (upa->verbosity >= 0)
3260 printf("%c %s\n", status, path);
3265 static const struct got_error *
3266 switch_head_ref(struct got_reference *head_ref,
3267 struct got_object_id *commit_id, struct got_worktree *worktree,
3268 struct got_repository *repo)
3270 const struct got_error *err = NULL;
3272 int ref_has_moved = 0;
3274 /* Trivial case: switching between two different references. */
3275 if (strcmp(got_ref_get_name(head_ref),
3276 got_worktree_get_head_ref_name(worktree)) != 0) {
3277 printf("Switching work tree from %s to %s\n",
3278 got_worktree_get_head_ref_name(worktree),
3279 got_ref_get_name(head_ref));
3280 return got_worktree_set_head_ref(worktree, head_ref);
3283 err = check_linear_ancestry(commit_id,
3284 got_worktree_get_base_commit_id(worktree), 0, repo);
3286 if (err->code != GOT_ERR_ANCESTRY)
3293 /* Switching to a rebased branch with the same reference name. */
3294 err = got_object_id_str(&base_id_str,
3295 got_worktree_get_base_commit_id(worktree));
3298 printf("Reference %s now points at a different branch\n",
3299 got_worktree_get_head_ref_name(worktree));
3300 printf("Switching work tree from %s to %s\n", base_id_str,
3301 got_worktree_get_head_ref_name(worktree));
3305 static const struct got_error *
3306 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3308 const struct got_error *err;
3311 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3315 return got_error(GOT_ERR_REBASING);
3317 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3321 return got_error(GOT_ERR_HISTEDIT_BUSY);
3326 static const struct got_error *
3327 check_merge_in_progress(struct got_worktree *worktree,
3328 struct got_repository *repo)
3330 const struct got_error *err;
3333 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3337 return got_error(GOT_ERR_MERGE_BUSY);
3342 static const struct got_error *
3343 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3344 char *argv[], struct got_worktree *worktree)
3346 const struct got_error *err = NULL;
3348 struct got_pathlist_entry *new;
3354 return got_error_from_errno("strdup");
3355 return got_pathlist_append(paths, path, NULL);
3358 for (i = 0; i < argc; i++) {
3359 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3362 err = got_pathlist_insert(&new, paths, path, NULL);
3363 if (err || new == NULL /* duplicate */) {
3373 static const struct got_error *
3374 wrap_not_worktree_error(const struct got_error *orig_err,
3375 const char *cmdname, const char *path)
3377 const struct got_error *err;
3378 struct got_repository *repo;
3379 static char msg[512];
3380 int *pack_fds = NULL;
3382 err = got_repo_pack_fds_open(&pack_fds);
3386 err = got_repo_open(&repo, path, NULL, pack_fds);
3390 snprintf(msg, sizeof(msg),
3391 "'got %s' needs a work tree in addition to a git repository\n"
3392 "Work trees can be checked out from this Git repository with "
3394 "The got(1) manual page contains more information.", cmdname);
3395 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3396 got_repo_close(repo);
3398 const struct got_error *pack_err =
3399 got_repo_pack_fds_close(pack_fds);
3406 static const struct got_error *
3407 cmd_update(int argc, char *argv[])
3409 const struct got_error *error = NULL;
3410 struct got_repository *repo = NULL;
3411 struct got_worktree *worktree = NULL;
3412 char *worktree_path = NULL;
3413 struct got_object_id *commit_id = NULL;
3414 char *commit_id_str = NULL;
3415 const char *branch_name = NULL;
3416 struct got_reference *head_ref = NULL;
3417 struct got_pathlist_head paths;
3418 struct got_pathlist_entry *pe;
3419 int ch, verbosity = 0;
3420 struct got_update_progress_arg upa;
3421 int *pack_fds = NULL;
3425 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3428 branch_name = optarg;
3431 commit_id_str = strdup(optarg);
3432 if (commit_id_str == NULL)
3433 return got_error_from_errno("strdup");
3448 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3449 "unveil", NULL) == -1)
3452 worktree_path = getcwd(NULL, 0);
3453 if (worktree_path == NULL) {
3454 error = got_error_from_errno("getcwd");
3458 error = got_repo_pack_fds_open(&pack_fds);
3462 error = got_worktree_open(&worktree, worktree_path);
3464 if (error->code == GOT_ERR_NOT_WORKTREE)
3465 error = wrap_not_worktree_error(error, "update",
3470 error = check_rebase_or_histedit_in_progress(worktree);
3474 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3479 error = apply_unveil(got_repo_get_path(repo), 0,
3480 got_worktree_get_root_path(worktree));
3484 error = check_merge_in_progress(worktree, repo);
3488 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3492 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3493 got_worktree_get_head_ref_name(worktree), 0);
3496 if (commit_id_str == NULL) {
3497 error = got_ref_resolve(&commit_id, repo, head_ref);
3500 error = got_object_id_str(&commit_id_str, commit_id);
3504 struct got_reflist_head refs;
3506 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3510 error = got_repo_match_object_id(&commit_id, NULL,
3511 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3512 got_ref_list_free(&refs);
3513 free(commit_id_str);
3514 commit_id_str = NULL;
3517 error = got_object_id_str(&commit_id_str, commit_id);
3523 struct got_object_id *head_commit_id;
3524 TAILQ_FOREACH(pe, &paths, entry) {
3525 if (pe->path_len == 0)
3527 error = got_error_msg(GOT_ERR_BAD_PATH,
3528 "switching between branches requires that "
3529 "the entire work tree gets updated");
3532 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3535 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3537 free(head_commit_id);
3540 error = check_same_branch(commit_id, head_ref, NULL, repo);
3543 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3547 error = check_linear_ancestry(commit_id,
3548 got_worktree_get_base_commit_id(worktree), 0, repo);
3549 if (error != NULL) {
3550 if (error->code == GOT_ERR_ANCESTRY)
3551 error = got_error(GOT_ERR_BRANCH_MOVED);
3554 error = check_same_branch(commit_id, head_ref, NULL, repo);
3559 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3561 error = got_worktree_set_base_commit_id(worktree, repo,
3567 memset(&upa, 0, sizeof(upa));
3568 upa.verbosity = verbosity;
3569 error = got_worktree_checkout_files(worktree, &paths, repo,
3570 update_progress, &upa, check_cancelled, NULL);
3574 if (upa.did_something) {
3575 printf("Updated to %s: %s\n",
3576 got_worktree_get_head_ref_name(worktree), commit_id_str);
3578 printf("Already up-to-date\n");
3580 print_update_progress_stats(&upa);
3583 const struct got_error *pack_err =
3584 got_repo_pack_fds_close(pack_fds);
3588 free(worktree_path);
3589 TAILQ_FOREACH(pe, &paths, entry)
3590 free((char *)pe->path);
3591 got_pathlist_free(&paths);
3593 free(commit_id_str);
3597 static const struct got_error *
3598 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3599 const char *path, int diff_context, int ignore_whitespace,
3600 int force_text_diff, struct got_repository *repo, FILE *outfile)
3602 const struct got_error *err = NULL;
3603 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3604 FILE *f1 = NULL, *f2 = NULL;
3605 int fd1 = -1, fd2 = -1;
3607 fd1 = got_opentempfd();
3609 return got_error_from_errno("got_opentempfd");
3610 fd2 = got_opentempfd();
3612 err = got_error_from_errno("got_opentempfd");
3617 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3623 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3627 f1 = got_opentemp();
3629 err = got_error_from_errno("got_opentemp");
3632 f2 = got_opentemp();
3634 err = got_error_from_errno("got_opentemp");
3638 while (path[0] == '/')
3640 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3641 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3642 force_text_diff, outfile);
3644 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3645 err = got_error_from_errno("close");
3647 got_object_blob_close(blob1);
3648 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3649 err = got_error_from_errno("close");
3650 got_object_blob_close(blob2);
3651 if (f1 && fclose(f1) == EOF && err == NULL)
3652 err = got_error_from_errno("fclose");
3653 if (f2 && fclose(f2) == EOF && err == NULL)
3654 err = got_error_from_errno("fclose");
3658 static const struct got_error *
3659 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3660 const char *path, int diff_context, int ignore_whitespace,
3661 int force_text_diff, struct got_repository *repo, FILE *outfile)
3663 const struct got_error *err = NULL;
3664 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3665 struct got_diff_blob_output_unidiff_arg arg;
3666 FILE *f1 = NULL, *f2 = NULL;
3667 int fd1 = -1, fd2 = -1;
3670 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3673 fd1 = got_opentempfd();
3675 err = got_error_from_errno("got_opentempfd");
3680 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3684 f1 = got_opentemp();
3686 err = got_error_from_errno("got_opentemp");
3690 f2 = got_opentemp();
3692 err = got_error_from_errno("got_opentemp");
3695 fd2 = got_opentempfd();
3697 err = got_error_from_errno("got_opentempfd");
3700 arg.diff_context = diff_context;
3701 arg.ignore_whitespace = ignore_whitespace;
3702 arg.force_text_diff = force_text_diff;
3703 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3704 arg.outfile = outfile;
3705 arg.line_offsets = NULL;
3707 while (path[0] == '/')
3709 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3710 got_diff_blob_output_unidiff, &arg, 1);
3713 got_object_tree_close(tree1);
3715 got_object_tree_close(tree2);
3716 if (f1 && fclose(f1) == EOF && err == NULL)
3717 err = got_error_from_errno("fclose");
3718 if (f2 && fclose(f2) == EOF && err == NULL)
3719 err = got_error_from_errno("fclose");
3720 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3721 err = got_error_from_errno("close");
3722 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3723 err = got_error_from_errno("close");
3727 static const struct got_error *
3728 get_changed_paths(struct got_pathlist_head *paths,
3729 struct got_commit_object *commit, struct got_repository *repo)
3731 const struct got_error *err = NULL;
3732 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3733 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3734 struct got_object_qid *qid;
3736 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3738 struct got_commit_object *pcommit;
3739 err = got_object_open_as_commit(&pcommit, repo,
3744 tree_id1 = got_object_id_dup(
3745 got_object_commit_get_tree_id(pcommit));
3746 if (tree_id1 == NULL) {
3747 got_object_commit_close(pcommit);
3748 return got_error_from_errno("got_object_id_dup");
3750 got_object_commit_close(pcommit);
3755 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3760 tree_id2 = got_object_commit_get_tree_id(commit);
3761 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3765 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3766 got_diff_tree_collect_changed_paths, paths, 0);
3769 got_object_tree_close(tree1);
3771 got_object_tree_close(tree2);
3776 static const struct got_error *
3777 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3778 const char *path, int diff_context, struct got_repository *repo,
3781 const struct got_error *err = NULL;
3782 struct got_commit_object *pcommit = NULL;
3783 char *id_str1 = NULL, *id_str2 = NULL;
3784 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3785 struct got_object_qid *qid;
3787 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3789 err = got_object_open_as_commit(&pcommit, repo,
3793 err = got_object_id_str(&id_str1, &qid->id);
3798 err = got_object_id_str(&id_str2, id);
3802 if (path && path[0] != '\0') {
3804 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3808 err = got_object_id_by_path(&obj_id1, repo,
3811 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3817 err = got_object_get_type(&obj_type, repo, obj_id2);
3823 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3824 fprintf(outfile, "commit - %s\n",
3825 id_str1 ? id_str1 : "/dev/null");
3826 fprintf(outfile, "commit + %s\n", id_str2);
3828 case GOT_OBJ_TYPE_BLOB:
3829 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3830 0, 0, repo, outfile);
3832 case GOT_OBJ_TYPE_TREE:
3833 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3834 0, 0, repo, outfile);
3837 err = got_error(GOT_ERR_OBJ_TYPE);
3843 obj_id2 = got_object_commit_get_tree_id(commit);
3845 obj_id1 = got_object_commit_get_tree_id(pcommit);
3847 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3848 fprintf(outfile, "commit - %s\n",
3849 id_str1 ? id_str1 : "/dev/null");
3850 fprintf(outfile, "commit + %s\n", id_str2);
3851 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3858 got_object_commit_close(pcommit);
3863 get_datestr(time_t *time, char *datebuf)
3865 struct tm mytm, *tm;
3868 tm = gmtime_r(time, &mytm);
3871 s = asctime_r(tm, datebuf);
3874 p = strchr(s, '\n');
3880 static const struct got_error *
3881 match_commit(int *have_match, struct got_object_id *id,
3882 struct got_commit_object *commit, regex_t *regex)
3884 const struct got_error *err = NULL;
3885 regmatch_t regmatch;
3886 char *id_str = NULL, *logmsg = NULL;
3890 err = got_object_id_str(&id_str, id);
3894 err = got_object_commit_get_logmsg(&logmsg, commit);
3898 if (regexec(regex, got_object_commit_get_author(commit), 1,
3899 ®match, 0) == 0 ||
3900 regexec(regex, got_object_commit_get_committer(commit), 1,
3901 ®match, 0) == 0 ||
3902 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3903 regexec(regex, logmsg, 1, ®match, 0) == 0)
3912 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3915 regmatch_t regmatch;
3916 struct got_pathlist_entry *pe;
3920 TAILQ_FOREACH(pe, changed_paths, entry) {
3921 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3928 static const struct got_error *
3929 match_patch(int *have_match, struct got_commit_object *commit,
3930 struct got_object_id *id, const char *path, int diff_context,
3931 struct got_repository *repo, regex_t *regex, FILE *f)
3933 const struct got_error *err = NULL;
3935 size_t linesize = 0;
3937 regmatch_t regmatch;
3941 err = got_opentemp_truncate(f);
3945 err = print_patch(commit, id, path, diff_context, repo, f);
3949 if (fseeko(f, 0L, SEEK_SET) == -1) {
3950 err = got_error_from_errno("fseeko");
3954 while ((linelen = getline(&line, &linesize, f)) != -1) {
3955 if (regexec(regex, line, 1, ®match, 0) == 0) {
3965 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3967 static const struct got_error*
3968 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3969 struct got_object_id *id, struct got_repository *repo,
3972 static const struct got_error *err = NULL;
3973 struct got_reflist_entry *re;
3979 TAILQ_FOREACH(re, refs, entry) {
3980 struct got_tag_object *tag = NULL;
3981 struct got_object_id *ref_id;
3984 name = got_ref_get_name(re->ref);
3985 if (strcmp(name, GOT_REF_HEAD) == 0)
3987 if (strncmp(name, "refs/", 5) == 0)
3989 if (strncmp(name, "got/", 4) == 0)
3991 if (strncmp(name, "heads/", 6) == 0)
3993 if (strncmp(name, "remotes/", 8) == 0) {
3997 s = strstr(name, "/" GOT_REF_HEAD);
3998 if (s != NULL && s[strlen(s)] == '\0')
4001 err = got_ref_resolve(&ref_id, repo, re->ref);
4004 if (strncmp(name, "tags/", 5) == 0) {
4005 err = got_object_open_as_tag(&tag, repo, ref_id);
4007 if (err->code != GOT_ERR_OBJ_TYPE) {
4011 /* Ref points at something other than a tag. */
4016 cmp = got_object_id_cmp(tag ?
4017 got_object_tag_get_object_id(tag) : ref_id, id);
4020 got_object_tag_close(tag);
4024 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4025 s ? ", " : "", name) == -1) {
4026 err = got_error_from_errno("asprintf");
4037 static const struct got_error *
4038 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4039 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4041 const struct got_error *err = NULL;
4042 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4043 char *comma, *s, *nl;
4044 struct got_reflist_head *refs;
4045 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4047 time_t committer_time;
4049 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4051 err = build_refs_str(&ref_str, refs, id, repo, 1);
4055 /* Display the first matching ref only. */
4056 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4060 if (ref_str == NULL) {
4061 err = got_object_id_str(&id_str, id);
4066 committer_time = got_object_commit_get_committer_time(commit);
4067 if (gmtime_r(&committer_time, &tm) == NULL) {
4068 err = got_error_from_errno("gmtime_r");
4071 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4072 err = got_error(GOT_ERR_NO_SPACE);
4076 err = got_object_commit_get_logmsg(&logmsg0, commit);
4081 while (isspace((unsigned char)s[0]))
4084 nl = strchr(s, '\n');
4090 printf("%s%-7s %s\n", datebuf, ref_str, s);
4092 printf("%s%.7s %s\n", datebuf, id_str, s);
4094 if (fflush(stdout) != 0 && err == NULL)
4095 err = got_error_from_errno("fflush");
4103 static const struct got_error *
4104 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4105 struct got_repository *repo, const char *path,
4106 struct got_pathlist_head *changed_paths, int show_patch,
4107 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4108 const char *custom_refs_str)
4110 const struct got_error *err = NULL;
4111 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4113 time_t committer_time;
4114 const char *author, *committer;
4115 char *refs_str = NULL;
4117 err = got_object_id_str(&id_str, id);
4121 if (custom_refs_str == NULL) {
4122 struct got_reflist_head *refs;
4123 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4125 err = build_refs_str(&refs_str, refs, id, repo, 0);
4131 printf(GOT_COMMIT_SEP_STR);
4132 if (custom_refs_str)
4133 printf("commit %s (%s)\n", id_str, custom_refs_str);
4135 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4136 refs_str ? refs_str : "", refs_str ? ")" : "");
4141 printf("from: %s\n", got_object_commit_get_author(commit));
4142 committer_time = got_object_commit_get_committer_time(commit);
4143 datestr = get_datestr(&committer_time, datebuf);
4145 printf("date: %s UTC\n", datestr);
4146 author = got_object_commit_get_author(commit);
4147 committer = got_object_commit_get_committer(commit);
4148 if (strcmp(author, committer) != 0)
4149 printf("via: %s\n", committer);
4150 if (got_object_commit_get_nparents(commit) > 1) {
4151 const struct got_object_id_queue *parent_ids;
4152 struct got_object_qid *qid;
4154 parent_ids = got_object_commit_get_parent_ids(commit);
4155 STAILQ_FOREACH(qid, parent_ids, entry) {
4156 err = got_object_id_str(&id_str, &qid->id);
4159 printf("parent %d: %s\n", n++, id_str);
4165 err = got_object_commit_get_logmsg(&logmsg0, commit);
4171 line = strsep(&logmsg, "\n");
4173 printf(" %s\n", line);
4177 if (changed_paths) {
4178 struct got_pathlist_entry *pe;
4179 TAILQ_FOREACH(pe, changed_paths, entry) {
4180 struct got_diff_changed_path *cp = pe->data;
4181 printf(" %c %s\n", cp->status, pe->path);
4186 err = print_patch(commit, id, path, diff_context, repo, stdout);
4191 if (fflush(stdout) != 0 && err == NULL)
4192 err = got_error_from_errno("fflush");
4199 static const struct got_error *
4200 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4201 struct got_repository *repo, const char *path, int show_changed_paths,
4202 int show_patch, const char *search_pattern, int diff_context, int limit,
4203 int log_branches, int reverse_display_order,
4204 struct got_reflist_object_id_map *refs_idmap, int one_line,
4207 const struct got_error *err;
4208 struct got_commit_graph *graph;
4211 struct got_object_id_queue reversed_commits;
4212 struct got_object_qid *qid;
4213 struct got_commit_object *commit;
4214 struct got_pathlist_head changed_paths;
4215 struct got_pathlist_entry *pe;
4217 STAILQ_INIT(&reversed_commits);
4218 TAILQ_INIT(&changed_paths);
4220 if (search_pattern && regcomp(®ex, search_pattern,
4221 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4222 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4224 err = got_commit_graph_open(&graph, path, !log_branches);
4227 err = got_commit_graph_iter_start(graph, root_id, repo,
4228 check_cancelled, NULL);
4232 struct got_object_id *id;
4234 if (sigint_received || sigpipe_received)
4237 err = got_commit_graph_iter_next(&id, graph, repo,
4238 check_cancelled, NULL);
4240 if (err->code == GOT_ERR_ITER_COMPLETED)
4247 err = got_object_open_as_commit(&commit, repo, id);
4251 if (show_changed_paths && !reverse_display_order) {
4252 err = get_changed_paths(&changed_paths, commit, repo);
4257 if (search_pattern) {
4258 err = match_commit(&have_match, id, commit, ®ex);
4260 got_object_commit_close(commit);
4263 if (have_match == 0 && show_changed_paths)
4264 match_changed_paths(&have_match,
4265 &changed_paths, ®ex);
4266 if (have_match == 0 && show_patch) {
4267 err = match_patch(&have_match, commit, id,
4268 path, diff_context, repo, ®ex,
4273 if (have_match == 0) {
4274 got_object_commit_close(commit);
4275 TAILQ_FOREACH(pe, &changed_paths, entry) {
4276 free((char *)pe->path);
4279 got_pathlist_free(&changed_paths);
4284 if (reverse_display_order) {
4285 err = got_object_qid_alloc(&qid, id);
4288 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4289 got_object_commit_close(commit);
4292 err = print_commit_oneline(commit, id,
4295 err = print_commit(commit, id, repo, path,
4296 show_changed_paths ? &changed_paths : NULL,
4297 show_patch, diff_context, refs_idmap, NULL);
4298 got_object_commit_close(commit);
4302 if ((limit && --limit == 0) ||
4303 (end_id && got_object_id_cmp(id, end_id) == 0))
4306 TAILQ_FOREACH(pe, &changed_paths, entry) {
4307 free((char *)pe->path);
4310 got_pathlist_free(&changed_paths);
4312 if (reverse_display_order) {
4313 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4314 err = got_object_open_as_commit(&commit, repo,
4318 if (show_changed_paths) {
4319 err = get_changed_paths(&changed_paths,
4325 err = print_commit_oneline(commit, &qid->id,
4328 err = print_commit(commit, &qid->id, repo, path,
4329 show_changed_paths ? &changed_paths : NULL,
4330 show_patch, diff_context, refs_idmap, NULL);
4331 got_object_commit_close(commit);
4334 TAILQ_FOREACH(pe, &changed_paths, entry) {
4335 free((char *)pe->path);
4338 got_pathlist_free(&changed_paths);
4342 while (!STAILQ_EMPTY(&reversed_commits)) {
4343 qid = STAILQ_FIRST(&reversed_commits);
4344 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4345 got_object_qid_free(qid);
4347 TAILQ_FOREACH(pe, &changed_paths, entry) {
4348 free((char *)pe->path);
4351 got_pathlist_free(&changed_paths);
4354 got_commit_graph_close(graph);
4361 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4362 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4363 "[-r repository-path] [-R] [path]\n", getprogname());
4368 get_default_log_limit(void)
4370 const char *got_default_log_limit;
4374 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4375 if (got_default_log_limit == NULL)
4377 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4383 static const struct got_error *
4384 cmd_log(int argc, char *argv[])
4386 const struct got_error *error;
4387 struct got_repository *repo = NULL;
4388 struct got_worktree *worktree = NULL;
4389 struct got_object_id *start_id = NULL, *end_id = NULL;
4390 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4391 const char *start_commit = NULL, *end_commit = NULL;
4392 const char *search_pattern = NULL;
4393 int diff_context = -1, ch;
4394 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4395 int reverse_display_order = 0, one_line = 0;
4397 struct got_reflist_head refs;
4398 struct got_reflist_object_id_map *refs_idmap = NULL;
4399 FILE *tmpfile = NULL;
4400 int *pack_fds = NULL;
4405 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4411 limit = get_default_log_limit();
4413 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4419 show_changed_paths = 1;
4422 start_commit = optarg;
4425 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4428 errx(1, "number of context lines is %s: %s",
4432 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4434 errx(1, "number of commits is %s: %s",
4441 repo_path = realpath(optarg, NULL);
4442 if (repo_path == NULL)
4443 return got_error_from_errno2("realpath",
4445 got_path_strip_trailing_slashes(repo_path);
4448 reverse_display_order = 1;
4454 search_pattern = optarg;
4457 end_commit = optarg;
4468 if (diff_context == -1)
4470 else if (!show_patch)
4471 errx(1, "-C requires -p");
4473 if (one_line && (show_patch || show_changed_paths))
4474 errx(1, "cannot use -s with -p or -P");
4476 cwd = getcwd(NULL, 0);
4478 error = got_error_from_errno("getcwd");
4482 error = got_repo_pack_fds_open(&pack_fds);
4486 if (repo_path == NULL) {
4487 error = got_worktree_open(&worktree, cwd);
4488 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4495 error = got_worktree_resolve_path(&path, worktree,
4500 path = strdup(argv[0]);
4502 error = got_error_from_errno("strdup");
4506 } else if (argc != 0)
4509 if (repo_path == NULL) {
4510 repo_path = worktree ?
4511 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4513 if (repo_path == NULL) {
4514 error = got_error_from_errno("strdup");
4518 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4522 error = apply_unveil(got_repo_get_path(repo), 1,
4523 worktree ? got_worktree_get_root_path(worktree) : NULL);
4527 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4531 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4535 if (start_commit == NULL) {
4536 struct got_reference *head_ref;
4537 struct got_commit_object *commit = NULL;
4538 error = got_ref_open(&head_ref, repo,
4539 worktree ? got_worktree_get_head_ref_name(worktree)
4543 error = got_ref_resolve(&start_id, repo, head_ref);
4544 got_ref_close(head_ref);
4547 error = got_object_open_as_commit(&commit, repo,
4551 got_object_commit_close(commit);
4553 error = got_repo_match_object_id(&start_id, NULL,
4554 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4558 if (end_commit != NULL) {
4559 error = got_repo_match_object_id(&end_id, NULL,
4560 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4567 * If a path was specified on the command line it was resolved
4568 * to a path in the work tree above. Prepend the work tree's
4569 * path prefix to obtain the corresponding in-repository path.
4573 prefix = got_worktree_get_path_prefix(worktree);
4574 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4575 (path[0] != '\0') ? "/" : "", path) == -1) {
4576 error = got_error_from_errno("asprintf");
4581 error = got_repo_map_path(&in_repo_path, repo,
4587 path = in_repo_path;
4591 /* Release work tree lock. */
4592 got_worktree_close(worktree);
4596 if (search_pattern && show_patch) {
4597 tmpfile = got_opentemp();
4598 if (tmpfile == NULL) {
4599 error = got_error_from_errno("got_opentemp");
4604 error = print_commits(start_id, end_id, repo, path ? path : "",
4605 show_changed_paths, show_patch, search_pattern, diff_context,
4606 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4613 got_worktree_close(worktree);
4615 const struct got_error *close_err = got_repo_close(repo);
4620 const struct got_error *pack_err =
4621 got_repo_pack_fds_close(pack_fds);
4626 got_reflist_object_id_map_free(refs_idmap);
4627 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4628 error = got_error_from_errno("fclose");
4629 got_ref_list_free(&refs);
4636 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4637 "[-r repository-path] [-s] [-w] [-P] "
4638 "[object1 object2 | path ...]\n", getprogname());
4642 struct print_diff_arg {
4643 struct got_repository *repo;
4644 struct got_worktree *worktree;
4649 enum got_diff_algorithm diff_algo;
4650 int ignore_whitespace;
4651 int force_text_diff;
4657 * Create a file which contains the target path of a symlink so we can feed
4658 * it as content to the diff engine.
4660 static const struct got_error *
4661 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4662 const char *abspath)
4664 const struct got_error *err = NULL;
4665 char target_path[PATH_MAX];
4666 ssize_t target_len, outlen;
4671 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4672 if (target_len == -1)
4673 return got_error_from_errno2("readlinkat", abspath);
4675 target_len = readlink(abspath, target_path, PATH_MAX);
4676 if (target_len == -1)
4677 return got_error_from_errno2("readlink", abspath);
4680 *fd = got_opentempfd();
4682 return got_error_from_errno("got_opentempfd");
4684 outlen = write(*fd, target_path, target_len);
4686 err = got_error_from_errno("got_opentempfd");
4690 if (lseek(*fd, 0, SEEK_SET) == -1) {
4691 err = got_error_from_errno2("lseek", abspath);
4702 static const struct got_error *
4703 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4704 const char *path, struct got_object_id *blob_id,
4705 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4706 int dirfd, const char *de_name)
4708 struct print_diff_arg *a = arg;
4709 const struct got_error *err = NULL;
4710 struct got_blob_object *blob1 = NULL;
4711 int fd = -1, fd1 = -1, fd2 = -1;
4713 char *abspath = NULL, *label1 = NULL;
4718 if (a->diff_staged) {
4719 if (staged_status != GOT_STATUS_MODIFY &&
4720 staged_status != GOT_STATUS_ADD &&
4721 staged_status != GOT_STATUS_DELETE)
4724 if (staged_status == GOT_STATUS_DELETE)
4726 if (status == GOT_STATUS_NONEXISTENT)
4727 return got_error_set_errno(ENOENT, path);
4728 if (status != GOT_STATUS_MODIFY &&
4729 status != GOT_STATUS_ADD &&
4730 status != GOT_STATUS_DELETE &&
4731 status != GOT_STATUS_CONFLICT)
4735 err = got_opentemp_truncate(a->f1);
4737 return got_error_from_errno("got_opentemp_truncate");
4738 err = got_opentemp_truncate(a->f2);
4740 return got_error_from_errno("got_opentemp_truncate");
4742 if (!a->header_shown) {
4743 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4744 got_worktree_get_root_path(a->worktree));
4745 printf("commit - %s\n", a->id_str);
4746 printf("path + %s%s\n",
4747 got_worktree_get_root_path(a->worktree),
4748 a->diff_staged ? " (staged changes)" : "");
4749 a->header_shown = 1;
4752 if (a->diff_staged) {
4753 const char *label1 = NULL, *label2 = NULL;
4754 switch (staged_status) {
4755 case GOT_STATUS_MODIFY:
4759 case GOT_STATUS_ADD:
4762 case GOT_STATUS_DELETE:
4766 return got_error(GOT_ERR_FILE_STATUS);
4768 fd1 = got_opentempfd();
4770 err = got_error_from_errno("got_opentempfd");
4773 fd2 = got_opentempfd();
4775 err = got_error_from_errno("got_opentempfd");
4778 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4779 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4780 a->diff_algo, a->diff_context, a->ignore_whitespace,
4781 a->force_text_diff, a->repo, stdout);
4785 fd1 = got_opentempfd();
4787 err = got_error_from_errno("got_opentempfd");
4791 if (staged_status == GOT_STATUS_ADD ||
4792 staged_status == GOT_STATUS_MODIFY) {
4794 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4798 err = got_object_id_str(&id_str, staged_blob_id);
4801 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4802 err = got_error_from_errno("asprintf");
4807 } else if (status != GOT_STATUS_ADD) {
4808 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4814 if (status != GOT_STATUS_DELETE) {
4815 if (asprintf(&abspath, "%s/%s",
4816 got_worktree_get_root_path(a->worktree), path) == -1) {
4817 err = got_error_from_errno("asprintf");
4822 fd = openat(dirfd, de_name,
4823 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4825 if (!got_err_open_nofollow_on_symlink()) {
4826 err = got_error_from_errno2("openat",
4830 err = get_symlink_target_file(&fd, dirfd,
4836 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4838 if (!got_err_open_nofollow_on_symlink()) {
4839 err = got_error_from_errno2("open",
4843 err = get_symlink_target_file(&fd, dirfd,
4849 if (fstat(fd, &sb) == -1) {
4850 err = got_error_from_errno2("fstat", abspath);
4853 f2 = fdopen(fd, "r");
4855 err = got_error_from_errno2("fdopen", abspath);
4865 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4871 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4872 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4873 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4875 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4876 err = got_error_from_errno("close");
4877 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4878 err = got_error_from_errno("close");
4880 got_object_blob_close(blob1);
4881 if (fd != -1 && close(fd) == -1 && err == NULL)
4882 err = got_error_from_errno("close");
4883 if (f2 && fclose(f2) == EOF && err == NULL)
4884 err = got_error_from_errno("fclose");
4889 static const struct got_error *
4890 cmd_diff(int argc, char *argv[])
4892 const struct got_error *error;
4893 struct got_repository *repo = NULL;
4894 struct got_worktree *worktree = NULL;
4895 char *cwd = NULL, *repo_path = NULL;
4896 const char *commit_args[2] = { NULL, NULL };
4897 int ncommit_args = 0;
4898 struct got_object_id *ids[2] = { NULL, NULL };
4899 char *labels[2] = { NULL, NULL };
4900 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4901 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4902 int force_text_diff = 0, force_path = 0, rflag = 0;
4904 struct got_reflist_head refs;
4905 struct got_pathlist_head paths;
4906 struct got_pathlist_entry *pe;
4907 FILE *f1 = NULL, *f2 = NULL;
4908 int fd1 = -1, fd2 = -1;
4909 int *pack_fds = NULL;
4915 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4920 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4923 force_text_diff = 1;
4926 if (ncommit_args >= 2)
4927 errx(1, "too many -c options used");
4928 commit_args[ncommit_args++] = optarg;
4931 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4934 errx(1, "number of context lines is %s: %s",
4938 repo_path = realpath(optarg, NULL);
4939 if (repo_path == NULL)
4940 return got_error_from_errno2("realpath",
4942 got_path_strip_trailing_slashes(repo_path);
4949 ignore_whitespace = 1;
4963 cwd = getcwd(NULL, 0);
4965 error = got_error_from_errno("getcwd");
4969 error = got_repo_pack_fds_open(&pack_fds);
4973 if (repo_path == NULL) {
4974 error = got_worktree_open(&worktree, cwd);
4975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4981 strdup(got_worktree_get_repo_path(worktree));
4982 if (repo_path == NULL) {
4983 error = got_error_from_errno("strdup");
4987 repo_path = strdup(cwd);
4988 if (repo_path == NULL) {
4989 error = got_error_from_errno("strdup");
4995 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5000 if (rflag || worktree == NULL || ncommit_args > 0) {
5002 error = got_error_msg(GOT_ERR_NOT_IMPL,
5003 "-P option can only be used when diffing "
5008 error = got_error_msg(GOT_ERR_NOT_IMPL,
5009 "-s option can only be used when diffing "
5015 error = apply_unveil(got_repo_get_path(repo), 1,
5016 worktree ? got_worktree_get_root_path(worktree) : NULL);
5020 if ((!force_path && argc == 2) || ncommit_args > 0) {
5021 int obj_type = (ncommit_args > 0 ?
5022 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5023 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5027 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5029 if (ncommit_args > 0)
5030 arg = commit_args[i];
5033 error = got_repo_match_object_id(&ids[i], &labels[i],
5034 arg, obj_type, &refs, repo);
5036 if (error->code != GOT_ERR_NOT_REF &&
5037 error->code != GOT_ERR_NO_OBJ)
5039 if (ncommit_args > 0)
5047 f1 = got_opentemp();
5049 error = got_error_from_errno("got_opentemp");
5053 f2 = got_opentemp();
5055 error = got_error_from_errno("got_opentemp");
5059 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5060 struct print_diff_arg arg;
5063 if (worktree == NULL) {
5064 if (argc == 2 && ids[0] == NULL) {
5065 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5067 } else if (argc == 2 && ids[1] == NULL) {
5068 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5070 } else if (argc > 0) {
5071 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5072 "%s", "specified paths cannot be resolved");
5075 error = got_error(GOT_ERR_NOT_WORKTREE);
5080 error = get_worktree_paths_from_argv(&paths, argc, argv,
5085 error = got_object_id_str(&id_str,
5086 got_worktree_get_base_commit_id(worktree));
5090 arg.worktree = worktree;
5091 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5092 arg.diff_context = diff_context;
5093 arg.id_str = id_str;
5094 arg.header_shown = 0;
5095 arg.diff_staged = diff_staged;
5096 arg.ignore_whitespace = ignore_whitespace;
5097 arg.force_text_diff = force_text_diff;
5101 error = got_worktree_status(worktree, &paths, repo, 0,
5102 print_diff, &arg, check_cancelled, NULL);
5107 if (ncommit_args == 1) {
5108 struct got_commit_object *commit;
5109 error = got_object_open_as_commit(&commit, repo, ids[0]);
5113 labels[1] = labels[0];
5115 if (got_object_commit_get_nparents(commit) > 0) {
5116 const struct got_object_id_queue *pids;
5117 struct got_object_qid *pid;
5118 pids = got_object_commit_get_parent_ids(commit);
5119 pid = STAILQ_FIRST(pids);
5120 ids[0] = got_object_id_dup(&pid->id);
5121 if (ids[0] == NULL) {
5122 error = got_error_from_errno(
5123 "got_object_id_dup");
5124 got_object_commit_close(commit);
5127 error = got_object_id_str(&labels[0], ids[0]);
5129 got_object_commit_close(commit);
5134 labels[0] = strdup("/dev/null");
5135 if (labels[0] == NULL) {
5136 error = got_error_from_errno("strdup");
5137 got_object_commit_close(commit);
5142 got_object_commit_close(commit);
5145 if (ncommit_args == 0 && argc > 2) {
5146 error = got_error_msg(GOT_ERR_BAD_PATH,
5147 "path arguments cannot be used when diffing two objects");
5152 error = got_object_get_type(&type1, repo, ids[0]);
5157 error = got_object_get_type(&type2, repo, ids[1]);
5160 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5161 error = got_error(GOT_ERR_OBJ_TYPE);
5164 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5165 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5166 "path arguments cannot be used when diffing blobs");
5170 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5172 struct got_pathlist_entry *new;
5176 error = got_worktree_resolve_path(&p, worktree,
5180 prefix = got_worktree_get_path_prefix(worktree);
5181 while (prefix[0] == '/')
5183 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5184 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5186 error = got_error_from_errno("asprintf");
5192 char *mapped_path, *s;
5193 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5199 in_repo_path = strdup(s);
5200 if (in_repo_path == NULL) {
5201 error = got_error_from_errno("asprintf");
5208 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5209 if (error || new == NULL /* duplicate */)
5216 /* Release work tree lock. */
5217 got_worktree_close(worktree);
5221 fd1 = got_opentempfd();
5223 error = got_error_from_errno("got_opentempfd");
5227 fd2 = got_opentempfd();
5229 error = got_error_from_errno("got_opentempfd");
5233 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5234 case GOT_OBJ_TYPE_BLOB:
5235 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5236 fd1, fd2, ids[0], ids[1], NULL, NULL,
5237 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5238 ignore_whitespace, force_text_diff, repo, stdout);
5240 case GOT_OBJ_TYPE_TREE:
5241 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5242 ids[0], ids[1], &paths, "", "",
5243 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5244 ignore_whitespace, force_text_diff, repo, stdout);
5246 case GOT_OBJ_TYPE_COMMIT:
5247 printf("diff %s %s\n", labels[0], labels[1]);
5248 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5249 fd1, fd2, ids[0], ids[1], &paths,
5250 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5251 ignore_whitespace, force_text_diff, repo, stdout);
5254 error = got_error(GOT_ERR_OBJ_TYPE);
5262 got_worktree_close(worktree);
5264 const struct got_error *close_err = got_repo_close(repo);
5269 const struct got_error *pack_err =
5270 got_repo_pack_fds_close(pack_fds);
5274 TAILQ_FOREACH(pe, &paths, entry)
5275 free((char *)pe->path);
5276 got_pathlist_free(&paths);
5277 got_ref_list_free(&refs);
5278 if (f1 && fclose(f1) == EOF && error == NULL)
5279 error = got_error_from_errno("fclose");
5280 if (f2 && fclose(f2) == EOF && error == NULL)
5281 error = got_error_from_errno("fclose");
5282 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5283 error = got_error_from_errno("close");
5284 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5285 error = got_error_from_errno("close");
5293 "usage: %s blame [-c commit] [-r repository-path] path\n",
5302 char datebuf[11]; /* YYYY-MM-DD + NUL */
5305 struct blame_cb_args {
5306 struct blame_line *lines;
5310 off_t *line_offsets;
5312 struct got_repository *repo;
5315 static const struct got_error *
5316 blame_cb(void *arg, int nlines, int lineno,
5317 struct got_commit_object *commit, struct got_object_id *id)
5319 const struct got_error *err = NULL;
5320 struct blame_cb_args *a = arg;
5321 struct blame_line *bline;
5323 size_t linesize = 0;
5326 time_t committer_time;
5328 if (nlines != a->nlines ||
5329 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5330 return got_error(GOT_ERR_RANGE);
5332 if (sigint_received)
5333 return got_error(GOT_ERR_ITER_COMPLETED);
5336 return NULL; /* no change in this commit */
5338 /* Annotate this line. */
5339 bline = &a->lines[lineno - 1];
5340 if (bline->annotated)
5342 err = got_object_id_str(&bline->id_str, id);
5346 bline->committer = strdup(got_object_commit_get_committer(commit));
5347 if (bline->committer == NULL) {
5348 err = got_error_from_errno("strdup");
5352 committer_time = got_object_commit_get_committer_time(commit);
5353 if (gmtime_r(&committer_time, &tm) == NULL)
5354 return got_error_from_errno("gmtime_r");
5355 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5357 err = got_error(GOT_ERR_NO_SPACE);
5360 bline->annotated = 1;
5362 /* Print lines annotated so far. */
5363 bline = &a->lines[a->lineno_cur - 1];
5364 if (!bline->annotated)
5367 offset = a->line_offsets[a->lineno_cur - 1];
5368 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5369 err = got_error_from_errno("fseeko");
5373 while (bline->annotated) {
5374 char *smallerthan, *at, *nl, *committer;
5377 if (getline(&line, &linesize, a->f) == -1) {
5379 err = got_error_from_errno("getline");
5383 committer = bline->committer;
5384 smallerthan = strchr(committer, '<');
5385 if (smallerthan && smallerthan[1] != '\0')
5386 committer = smallerthan + 1;
5387 at = strchr(committer, '@');
5390 len = strlen(committer);
5392 committer[8] = '\0';
5394 nl = strchr(line, '\n');
5397 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5398 bline->id_str, bline->datebuf, committer, line);
5401 bline = &a->lines[a->lineno_cur - 1];
5408 static const struct got_error *
5409 cmd_blame(int argc, char *argv[])
5411 const struct got_error *error;
5412 struct got_repository *repo = NULL;
5413 struct got_worktree *worktree = NULL;
5414 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5415 char *link_target = NULL;
5416 struct got_object_id *obj_id = NULL;
5417 struct got_object_id *commit_id = NULL;
5418 struct got_commit_object *commit = NULL;
5419 struct got_blob_object *blob = NULL;
5420 char *commit_id_str = NULL;
5421 struct blame_cb_args bca;
5422 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5424 int *pack_fds = NULL;
5425 FILE *f1 = NULL, *f2 = NULL;
5427 fd1 = got_opentempfd();
5429 return got_error_from_errno("got_opentempfd");
5431 memset(&bca, 0, sizeof(bca));
5434 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5439 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5442 commit_id_str = optarg;
5445 repo_path = realpath(optarg, NULL);
5446 if (repo_path == NULL)
5447 return got_error_from_errno2("realpath",
5449 got_path_strip_trailing_slashes(repo_path);
5465 cwd = getcwd(NULL, 0);
5467 error = got_error_from_errno("getcwd");
5471 error = got_repo_pack_fds_open(&pack_fds);
5475 if (repo_path == NULL) {
5476 error = got_worktree_open(&worktree, cwd);
5477 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5483 strdup(got_worktree_get_repo_path(worktree));
5484 if (repo_path == NULL) {
5485 error = got_error_from_errno("strdup");
5490 repo_path = strdup(cwd);
5491 if (repo_path == NULL) {
5492 error = got_error_from_errno("strdup");
5498 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5503 const char *prefix = got_worktree_get_path_prefix(worktree);
5506 error = got_worktree_resolve_path(&p, worktree, path);
5509 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5510 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5512 error = got_error_from_errno("asprintf");
5517 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5519 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5522 error = got_repo_map_path(&in_repo_path, repo, path);
5527 if (commit_id_str == NULL) {
5528 struct got_reference *head_ref;
5529 error = got_ref_open(&head_ref, repo, worktree ?
5530 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5533 error = got_ref_resolve(&commit_id, repo, head_ref);
5534 got_ref_close(head_ref);
5538 struct got_reflist_head refs;
5540 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5544 error = got_repo_match_object_id(&commit_id, NULL,
5545 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5546 got_ref_list_free(&refs);
5552 /* Release work tree lock. */
5553 got_worktree_close(worktree);
5557 error = got_object_open_as_commit(&commit, repo, commit_id);
5561 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5566 error = got_object_id_by_path(&obj_id, repo, commit,
5567 link_target ? link_target : in_repo_path);
5571 error = got_object_get_type(&obj_type, repo, obj_id);
5575 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5576 error = got_error_path(link_target ? link_target : in_repo_path,
5581 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5584 bca.f = got_opentemp();
5585 if (bca.f == NULL) {
5586 error = got_error_from_errno("got_opentemp");
5589 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5590 &bca.line_offsets, bca.f, blob);
5591 if (error || bca.nlines == 0)
5594 /* Don't include \n at EOF in the blame line count. */
5595 if (bca.line_offsets[bca.nlines - 1] == filesize)
5598 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5599 if (bca.lines == NULL) {
5600 error = got_error_from_errno("calloc");
5604 bca.nlines_prec = 0;
5612 fd2 = got_opentempfd();
5614 error = got_error_from_errno("got_opentempfd");
5617 fd3 = got_opentempfd();
5619 error = got_error_from_errno("got_opentempfd");
5622 f1 = got_opentemp();
5624 error = got_error_from_errno("got_opentemp");
5627 f2 = got_opentemp();
5629 error = got_error_from_errno("got_opentemp");
5632 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5633 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5634 check_cancelled, NULL, fd2, fd3, f1, f2);
5643 got_object_commit_close(commit);
5645 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5646 error = got_error_from_errno("close");
5647 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5648 error = got_error_from_errno("close");
5649 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5650 error = got_error_from_errno("close");
5651 if (f1 && fclose(f1) == EOF && error == NULL)
5652 error = got_error_from_errno("fclose");
5653 if (f2 && fclose(f2) == EOF && error == NULL)
5654 error = got_error_from_errno("fclose");
5657 got_object_blob_close(blob);
5659 got_worktree_close(worktree);
5661 const struct got_error *close_err = got_repo_close(repo);
5666 const struct got_error *pack_err =
5667 got_repo_pack_fds_close(pack_fds);
5672 for (i = 0; i < bca.nlines; i++) {
5673 struct blame_line *bline = &bca.lines[i];
5674 free(bline->id_str);
5675 free(bline->committer);
5679 free(bca.line_offsets);
5680 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5681 error = got_error_from_errno("fclose");
5689 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5694 static const struct got_error *
5695 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5696 const char *root_path, struct got_repository *repo)
5698 const struct got_error *err = NULL;
5699 int is_root_path = (strcmp(path, root_path) == 0);
5700 const char *modestr = "";
5701 mode_t mode = got_tree_entry_get_mode(te);
5702 char *link_target = NULL;
5704 path += strlen(root_path);
5705 while (path[0] == '/')
5708 if (got_object_tree_entry_is_submodule(te))
5710 else if (S_ISLNK(mode)) {
5713 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5716 for (i = 0; i < strlen(link_target); i++) {
5717 if (!isprint((unsigned char)link_target[i]))
5718 link_target[i] = '?';
5723 else if (S_ISDIR(mode))
5725 else if (mode & S_IXUSR)
5728 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5729 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5730 link_target ? " -> ": "", link_target ? link_target : "");
5736 static const struct got_error *
5737 print_tree(const char *path, struct got_commit_object *commit,
5738 int show_ids, int recurse, const char *root_path,
5739 struct got_repository *repo)
5741 const struct got_error *err = NULL;
5742 struct got_object_id *tree_id = NULL;
5743 struct got_tree_object *tree = NULL;
5746 err = got_object_id_by_path(&tree_id, repo, commit, path);
5750 err = got_object_open_as_tree(&tree, repo, tree_id);
5753 nentries = got_object_tree_get_nentries(tree);
5754 for (i = 0; i < nentries; i++) {
5755 struct got_tree_entry *te;
5758 if (sigint_received || sigpipe_received)
5761 te = got_object_tree_get_entry(tree, i);
5764 err = got_object_id_str(&id_str,
5765 got_tree_entry_get_id(te));
5768 if (asprintf(&id, "%s ", id_str) == -1) {
5769 err = got_error_from_errno("asprintf");
5775 err = print_entry(te, id, path, root_path, repo);
5780 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5782 if (asprintf(&child_path, "%s%s%s", path,
5783 path[0] == '/' && path[1] == '\0' ? "" : "/",
5784 got_tree_entry_get_name(te)) == -1) {
5785 err = got_error_from_errno("asprintf");
5788 err = print_tree(child_path, commit, show_ids, 1,
5797 got_object_tree_close(tree);
5802 static const struct got_error *
5803 cmd_tree(int argc, char *argv[])
5805 const struct got_error *error;
5806 struct got_repository *repo = NULL;
5807 struct got_worktree *worktree = NULL;
5808 const char *path, *refname = NULL;
5809 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5810 struct got_object_id *commit_id = NULL;
5811 struct got_commit_object *commit = NULL;
5812 char *commit_id_str = NULL;
5813 int show_ids = 0, recurse = 0;
5815 int *pack_fds = NULL;
5818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5823 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5826 commit_id_str = optarg;
5829 repo_path = realpath(optarg, NULL);
5830 if (repo_path == NULL)
5831 return got_error_from_errno2("realpath",
5833 got_path_strip_trailing_slashes(repo_path);
5857 cwd = getcwd(NULL, 0);
5859 error = got_error_from_errno("getcwd");
5863 error = got_repo_pack_fds_open(&pack_fds);
5867 if (repo_path == NULL) {
5868 error = got_worktree_open(&worktree, cwd);
5869 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5875 strdup(got_worktree_get_repo_path(worktree));
5876 if (repo_path == NULL)
5877 error = got_error_from_errno("strdup");
5881 repo_path = strdup(cwd);
5882 if (repo_path == NULL) {
5883 error = got_error_from_errno("strdup");
5889 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5894 const char *prefix = got_worktree_get_path_prefix(worktree);
5899 error = got_worktree_resolve_path(&p, worktree, path);
5902 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5903 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5905 error = got_error_from_errno("asprintf");
5910 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5914 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5919 error = got_repo_map_path(&in_repo_path, repo, path);
5924 if (commit_id_str == NULL) {
5925 struct got_reference *head_ref;
5927 refname = got_worktree_get_head_ref_name(worktree);
5929 refname = GOT_REF_HEAD;
5930 error = got_ref_open(&head_ref, repo, refname, 0);
5933 error = got_ref_resolve(&commit_id, repo, head_ref);
5934 got_ref_close(head_ref);
5938 struct got_reflist_head refs;
5940 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5944 error = got_repo_match_object_id(&commit_id, NULL,
5945 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5946 got_ref_list_free(&refs);
5952 /* Release work tree lock. */
5953 got_worktree_close(worktree);
5957 error = got_object_open_as_commit(&commit, repo, commit_id);
5961 error = print_tree(in_repo_path, commit, show_ids, recurse,
5962 in_repo_path, repo);
5969 got_object_commit_close(commit);
5971 got_worktree_close(worktree);
5973 const struct got_error *close_err = got_repo_close(repo);
5978 const struct got_error *pack_err =
5979 got_repo_pack_fds_close(pack_fds);
5989 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5990 "[-S status-codes] [path ...]\n", getprogname());
5994 struct got_status_arg {
5999 static const struct got_error *
6000 print_status(void *arg, unsigned char status, unsigned char staged_status,
6001 const char *path, struct got_object_id *blob_id,
6002 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6003 int dirfd, const char *de_name)
6005 struct got_status_arg *st = arg;
6007 if (status == staged_status && (status == GOT_STATUS_DELETE))
6008 status = GOT_STATUS_NO_CHANGE;
6009 if (st != NULL && st->status_codes) {
6010 size_t ncodes = strlen(st->status_codes);
6013 for (i = 0; i < ncodes ; i++) {
6015 if (status == st->status_codes[i] ||
6016 staged_status == st->status_codes[i]) {
6021 if (status == st->status_codes[i] ||
6022 staged_status == st->status_codes[i])
6027 if (st->suppress && j == 0)
6034 printf("%c%c %s\n", status, staged_status, path);
6038 static const struct got_error *
6039 cmd_status(int argc, char *argv[])
6041 const struct got_error *error = NULL;
6042 struct got_repository *repo = NULL;
6043 struct got_worktree *worktree = NULL;
6044 struct got_status_arg st;
6046 struct got_pathlist_head paths;
6047 struct got_pathlist_entry *pe;
6048 int ch, i, no_ignores = 0;
6049 int *pack_fds = NULL;
6053 memset(&st, 0, sizeof(st));
6054 st.status_codes = NULL;
6057 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6063 if (st.status_codes != NULL && st.suppress == 0)
6064 option_conflict('S', 's');
6068 for (i = 0; i < strlen(optarg); i++) {
6069 switch (optarg[i]) {
6070 case GOT_STATUS_MODIFY:
6071 case GOT_STATUS_ADD:
6072 case GOT_STATUS_DELETE:
6073 case GOT_STATUS_CONFLICT:
6074 case GOT_STATUS_MISSING:
6075 case GOT_STATUS_OBSTRUCTED:
6076 case GOT_STATUS_UNVERSIONED:
6077 case GOT_STATUS_MODE_CHANGE:
6078 case GOT_STATUS_NONEXISTENT:
6081 errx(1, "invalid status code '%c'",
6085 if (ch == 's' && st.suppress)
6086 option_conflict('s', 'S');
6087 st.status_codes = optarg;
6099 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6103 cwd = getcwd(NULL, 0);
6105 error = got_error_from_errno("getcwd");
6109 error = got_repo_pack_fds_open(&pack_fds);
6113 error = got_worktree_open(&worktree, cwd);
6115 if (error->code == GOT_ERR_NOT_WORKTREE)
6116 error = wrap_not_worktree_error(error, "status", cwd);
6120 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6125 error = apply_unveil(got_repo_get_path(repo), 1,
6126 got_worktree_get_root_path(worktree));
6130 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6134 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6135 print_status, &st, check_cancelled, NULL);
6138 const struct got_error *pack_err =
6139 got_repo_pack_fds_close(pack_fds);
6144 TAILQ_FOREACH(pe, &paths, entry)
6145 free((char *)pe->path);
6146 got_pathlist_free(&paths);
6155 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6156 "[-s reference] [-d] [name]\n",
6161 static const struct got_error *
6162 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6164 static const struct got_error *err = NULL;
6165 struct got_reflist_head refs;
6166 struct got_reflist_entry *re;
6169 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6170 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6175 TAILQ_FOREACH(re, &refs, entry) {
6177 refstr = got_ref_to_str(re->ref);
6178 if (refstr == NULL) {
6179 err = got_error_from_errno("got_ref_to_str");
6182 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6186 got_ref_list_free(&refs);
6190 static const struct got_error *
6191 delete_ref_by_name(struct got_repository *repo, const char *refname)
6193 const struct got_error *err;
6194 struct got_reference *ref;
6196 err = got_ref_open(&ref, repo, refname, 0);
6200 err = delete_ref(repo, ref);
6205 static const struct got_error *
6206 add_ref(struct got_repository *repo, const char *refname, const char *target)
6208 const struct got_error *err = NULL;
6209 struct got_object_id *id = NULL;
6210 struct got_reference *ref = NULL;
6211 struct got_reflist_head refs;
6214 * Don't let the user create a reference name with a leading '-'.
6215 * While technically a valid reference name, this case is usually
6216 * an unintended typo.
6218 if (refname[0] == '-')
6219 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6222 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6225 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6227 got_ref_list_free(&refs);
6231 err = got_ref_alloc(&ref, refname, id);
6235 err = got_ref_write(ref, repo);
6243 static const struct got_error *
6244 add_symref(struct got_repository *repo, const char *refname, const char *target)
6246 const struct got_error *err = NULL;
6247 struct got_reference *ref = NULL;
6248 struct got_reference *target_ref = NULL;
6251 * Don't let the user create a reference name with a leading '-'.
6252 * While technically a valid reference name, this case is usually
6253 * an unintended typo.
6255 if (refname[0] == '-')
6256 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6258 err = got_ref_open(&target_ref, repo, target, 0);
6262 err = got_ref_alloc_symref(&ref, refname, target_ref);
6266 err = got_ref_write(ref, repo);
6269 got_ref_close(target_ref);
6275 static const struct got_error *
6276 cmd_ref(int argc, char *argv[])
6278 const struct got_error *error = NULL;
6279 struct got_repository *repo = NULL;
6280 struct got_worktree *worktree = NULL;
6281 char *cwd = NULL, *repo_path = NULL;
6282 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6283 const char *obj_arg = NULL, *symref_target= NULL;
6284 char *refname = NULL;
6285 int *pack_fds = NULL;
6287 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6296 repo_path = realpath(optarg, NULL);
6297 if (repo_path == NULL)
6298 return got_error_from_errno2("realpath",
6300 got_path_strip_trailing_slashes(repo_path);
6306 symref_target = optarg;
6317 if (obj_arg && do_list)
6318 option_conflict('c', 'l');
6319 if (obj_arg && do_delete)
6320 option_conflict('c', 'd');
6321 if (obj_arg && symref_target)
6322 option_conflict('c', 's');
6323 if (symref_target && do_delete)
6324 option_conflict('s', 'd');
6325 if (symref_target && do_list)
6326 option_conflict('s', 'l');
6327 if (do_delete && do_list)
6328 option_conflict('d', 'l');
6329 if (sort_by_time && !do_list)
6330 errx(1, "-t option requires -l option");
6336 if (argc != 0 && argc != 1)
6339 refname = strdup(argv[0]);
6340 if (refname == NULL) {
6341 error = got_error_from_errno("strdup");
6348 refname = strdup(argv[0]);
6349 if (refname == NULL) {
6350 error = got_error_from_errno("strdup");
6356 got_path_strip_trailing_slashes(refname);
6359 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6360 "sendfd unveil", NULL) == -1)
6363 cwd = getcwd(NULL, 0);
6365 error = got_error_from_errno("getcwd");
6369 error = got_repo_pack_fds_open(&pack_fds);
6373 if (repo_path == NULL) {
6374 error = got_worktree_open(&worktree, cwd);
6375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6381 strdup(got_worktree_get_repo_path(worktree));
6382 if (repo_path == NULL)
6383 error = got_error_from_errno("strdup");
6387 repo_path = strdup(cwd);
6388 if (repo_path == NULL) {
6389 error = got_error_from_errno("strdup");
6395 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6401 /* Remove "cpath" promise. */
6402 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6408 error = apply_unveil(got_repo_get_path(repo), do_list,
6409 worktree ? got_worktree_get_root_path(worktree) : NULL);
6414 error = list_refs(repo, refname, sort_by_time);
6416 error = delete_ref_by_name(repo, refname);
6417 else if (symref_target)
6418 error = add_symref(repo, refname, symref_target);
6420 if (obj_arg == NULL)
6422 error = add_ref(repo, refname, obj_arg);
6427 const struct got_error *close_err = got_repo_close(repo);
6432 got_worktree_close(worktree);
6434 const struct got_error *pack_err =
6435 got_repo_pack_fds_close(pack_fds);
6448 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6449 "[-n] [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);
6893 "usage: %s tag [-c commit] [-r repository] [-l] "
6894 "[-m message] [-s signer-id] [-V] name\n",
6900 static const struct got_error *
6901 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6903 const struct got_error *err = NULL;
6904 struct got_reflist_entry *re, *se, *new;
6905 struct got_object_id *re_id, *se_id;
6906 struct got_tag_object *re_tag, *se_tag;
6907 time_t re_time, se_time;
6909 STAILQ_FOREACH(re, tags, entry) {
6910 se = STAILQ_FIRST(sorted);
6912 err = got_reflist_entry_dup(&new, re);
6915 STAILQ_INSERT_HEAD(sorted, new, entry);
6918 err = got_ref_resolve(&re_id, repo, re->ref);
6921 err = got_object_open_as_tag(&re_tag, repo, re_id);
6925 re_time = got_object_tag_get_tagger_time(re_tag);
6926 got_object_tag_close(re_tag);
6930 err = got_ref_resolve(&se_id, repo, re->ref);
6933 err = got_object_open_as_tag(&se_tag, repo, se_id);
6937 se_time = got_object_tag_get_tagger_time(se_tag);
6938 got_object_tag_close(se_tag);
6940 if (se_time > re_time) {
6941 err = got_reflist_entry_dup(&new, re);
6944 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6947 se = STAILQ_NEXT(se, entry);
6956 static const struct got_error *
6957 get_tag_refname(char **refname, const char *tag_name)
6959 const struct got_error *err;
6961 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6962 *refname = strdup(tag_name);
6963 if (*refname == NULL)
6964 return got_error_from_errno("strdup");
6965 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6966 err = got_error_from_errno("asprintf");
6974 static const struct got_error *
6975 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
6976 const char *allowed_signers, const char *revoked_signers, int verbosity)
6978 static const struct got_error *err = NULL;
6979 struct got_reflist_head refs;
6980 struct got_reflist_entry *re;
6981 char *wanted_refname = NULL;
6986 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6991 struct got_reference *ref;
6992 err = get_tag_refname(&wanted_refname, tag_name);
6995 /* Wanted tag reference should exist. */
6996 err = got_ref_open(&ref, repo, wanted_refname, 0);
7002 TAILQ_FOREACH(re, &refs, entry) {
7003 const char *refname;
7004 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7006 const char *tagger, *ssh_sig = NULL;
7007 char *sig_msg = NULL;
7009 struct got_object_id *id;
7010 struct got_tag_object *tag;
7011 struct got_commit_object *commit = NULL;
7013 refname = got_ref_get_name(re->ref);
7014 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7015 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7018 refstr = got_ref_to_str(re->ref);
7019 if (refstr == NULL) {
7020 err = got_error_from_errno("got_ref_to_str");
7024 err = got_ref_resolve(&id, repo, re->ref);
7027 err = got_object_open_as_tag(&tag, repo, id);
7029 if (err->code != GOT_ERR_OBJ_TYPE) {
7033 /* "lightweight" tag */
7034 err = got_object_open_as_commit(&commit, repo, id);
7039 tagger = got_object_commit_get_committer(commit);
7041 got_object_commit_get_committer_time(commit);
7042 err = got_object_id_str(&id_str, id);
7048 tagger = got_object_tag_get_tagger(tag);
7049 tagger_time = got_object_tag_get_tagger_time(tag);
7050 err = got_object_id_str(&id_str,
7051 got_object_tag_get_object_id(tag));
7057 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7058 got_object_tag_get_message(tag));
7059 if (ssh_sig && allowed_signers == NULL) {
7060 err = got_error_msg(
7061 GOT_ERR_VERIFY_TAG_SIGNATURE,
7062 "SSH signature verification requires "
7063 "setting allowed_signers in "
7069 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7071 printf("from: %s\n", tagger);
7072 datestr = get_datestr(&tagger_time, datebuf);
7074 printf("date: %s UTC\n", datestr);
7076 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7078 switch (got_object_tag_get_object_type(tag)) {
7079 case GOT_OBJ_TYPE_BLOB:
7080 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7083 case GOT_OBJ_TYPE_TREE:
7084 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7087 case GOT_OBJ_TYPE_COMMIT:
7088 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7091 case GOT_OBJ_TYPE_TAG:
7092 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7102 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7103 allowed_signers, revoked_signers, verbosity);
7104 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7108 printf("signature: %s", sig_msg);
7114 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7117 got_object_commit_close(commit);
7119 tagmsg0 = strdup(got_object_tag_get_message(tag));
7120 got_object_tag_close(tag);
7121 if (tagmsg0 == NULL) {
7122 err = got_error_from_errno("strdup");
7129 line = strsep(&tagmsg, "\n");
7131 printf(" %s\n", line);
7136 got_ref_list_free(&refs);
7137 free(wanted_refname);
7139 if (err == NULL && bad_sigs)
7140 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7144 static const struct got_error *
7145 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7146 const char *tag_name, const char *repo_path)
7148 const struct got_error *err = NULL;
7149 char *template = NULL, *initial_content = NULL;
7150 char *editor = NULL;
7151 int initial_content_len;
7154 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7155 err = got_error_from_errno("asprintf");
7159 initial_content_len = asprintf(&initial_content,
7160 "\n# tagging commit %s as %s\n",
7161 commit_id_str, tag_name);
7162 if (initial_content_len == -1) {
7163 err = got_error_from_errno("asprintf");
7167 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7171 if (write(fd, initial_content, initial_content_len) == -1) {
7172 err = got_error_from_errno2("write", *tagmsg_path);
7176 err = get_editor(&editor);
7179 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7180 initial_content_len, 1);
7182 free(initial_content);
7186 if (fd != -1 && close(fd) == -1 && err == NULL)
7187 err = got_error_from_errno2("close", *tagmsg_path);
7196 static const struct got_error *
7197 add_tag(struct got_repository *repo, const char *tagger,
7198 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7199 const char *signer_id, int verbosity)
7201 const struct got_error *err = NULL;
7202 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7203 char *label = NULL, *commit_id_str = NULL;
7204 struct got_reference *ref = NULL;
7205 char *refname = NULL, *tagmsg = NULL;
7206 char *tagmsg_path = NULL, *tag_id_str = NULL;
7207 int preserve_tagmsg = 0;
7208 struct got_reflist_head refs;
7213 * Don't let the user create a tag name with a leading '-'.
7214 * While technically a valid reference name, this case is usually
7215 * an unintended typo.
7217 if (tag_name[0] == '-')
7218 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7220 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7224 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7225 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7229 err = got_object_id_str(&commit_id_str, commit_id);
7233 err = get_tag_refname(&refname, tag_name);
7236 if (strncmp("refs/tags/", tag_name, 10) == 0)
7239 err = got_ref_open(&ref, repo, refname, 0);
7241 err = got_error(GOT_ERR_TAG_EXISTS);
7243 } else if (err->code != GOT_ERR_NOT_REF)
7246 if (tagmsg_arg == NULL) {
7247 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7248 tag_name, got_repo_get_path(repo));
7250 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7251 tagmsg_path != NULL)
7252 preserve_tagmsg = 1;
7255 /* Editor is done; we can now apply unveil(2) */
7256 err = got_sigs_apply_unveil();
7259 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7264 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7265 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7269 preserve_tagmsg = 1;
7273 err = got_ref_alloc(&ref, refname, tag_id);
7276 preserve_tagmsg = 1;
7280 err = got_ref_write(ref, repo);
7283 preserve_tagmsg = 1;
7287 err = got_object_id_str(&tag_id_str, tag_id);
7290 preserve_tagmsg = 1;
7293 printf("Created tag %s\n", tag_id_str);
7295 if (preserve_tagmsg) {
7296 fprintf(stderr, "%s: tag message preserved in %s\n",
7297 getprogname(), tagmsg_path);
7298 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7299 err = got_error_from_errno2("unlink", tagmsg_path);
7304 free(commit_id_str);
7308 got_ref_list_free(&refs);
7312 static const struct got_error *
7313 cmd_tag(int argc, char *argv[])
7315 const struct got_error *error = NULL;
7316 struct got_repository *repo = NULL;
7317 struct got_worktree *worktree = NULL;
7318 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7319 char *gitconfig_path = NULL, *tagger = NULL;
7320 char *allowed_signers = NULL, *revoked_signers = NULL;
7321 char *signer_id = NULL;
7322 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7323 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7324 int *pack_fds = NULL;
7326 while ((ch = getopt(argc, argv, "c:m:r:ls:Vv")) != -1) {
7329 commit_id_arg = optarg;
7335 repo_path = realpath(optarg, NULL);
7336 if (repo_path == NULL) {
7337 error = got_error_from_errno2("realpath",
7341 got_path_strip_trailing_slashes(repo_path);
7347 signer_id = strdup(optarg);
7348 if (signer_id == NULL) {
7349 error = got_error_from_errno("strdup");
7359 else if (verbosity < 3)
7371 if (do_list || verify_tags) {
7372 if (commit_id_arg != NULL)
7374 "-c option can only be used when creating a tag");
7377 option_conflict('l', 'm');
7379 option_conflict('V', 'm');
7383 option_conflict('l', 's');
7385 option_conflict('V', 's');
7389 } else if (argc != 1)
7396 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7397 "sendfd unveil", NULL) == -1)
7400 cwd = getcwd(NULL, 0);
7402 error = got_error_from_errno("getcwd");
7406 error = got_repo_pack_fds_open(&pack_fds);
7410 if (repo_path == NULL) {
7411 error = got_worktree_open(&worktree, cwd);
7412 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7418 strdup(got_worktree_get_repo_path(worktree));
7419 if (repo_path == NULL)
7420 error = got_error_from_errno("strdup");
7424 repo_path = strdup(cwd);
7425 if (repo_path == NULL) {
7426 error = got_error_from_errno("strdup");
7432 if (do_list || verify_tags) {
7433 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7436 error = get_allowed_signers(&allowed_signers, repo, worktree);
7439 error = get_revoked_signers(&revoked_signers, repo, worktree);
7443 /* Release work tree lock. */
7444 got_worktree_close(worktree);
7449 * Remove "cpath" promise unless needed for signature tmpfile
7453 got_sigs_apply_unveil();
7456 if (pledge("stdio rpath wpath flock proc exec sendfd "
7457 "unveil", NULL) == -1)
7461 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7464 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7465 revoked_signers, verbosity);
7467 error = get_gitconfig_path(&gitconfig_path);
7470 error = got_repo_open(&repo, repo_path, gitconfig_path,
7475 error = get_author(&tagger, repo, worktree);
7478 if (signer_id == NULL) {
7479 error = get_signer_id(&signer_id, repo, worktree);
7484 /* Release work tree lock. */
7485 got_worktree_close(worktree);
7491 error = got_sigs_apply_unveil();
7495 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7500 if (commit_id_arg == NULL) {
7501 struct got_reference *head_ref;
7502 struct got_object_id *commit_id;
7503 error = got_ref_open(&head_ref, repo,
7504 worktree ? got_worktree_get_head_ref_name(worktree)
7508 error = got_ref_resolve(&commit_id, repo, head_ref);
7509 got_ref_close(head_ref);
7512 error = got_object_id_str(&commit_id_str, commit_id);
7518 error = add_tag(repo, tagger, tag_name,
7519 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7520 signer_id, verbosity);
7524 const struct got_error *close_err = got_repo_close(repo);
7529 got_worktree_close(worktree);
7531 const struct got_error *pack_err =
7532 got_repo_pack_fds_close(pack_fds);
7538 free(gitconfig_path);
7539 free(commit_id_str);
7541 free(allowed_signers);
7542 free(revoked_signers);
7550 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7555 static const struct got_error *
7556 add_progress(void *arg, unsigned char status, const char *path)
7558 while (path[0] == '/')
7560 printf("%c %s\n", status, path);
7564 static const struct got_error *
7565 cmd_add(int argc, char *argv[])
7567 const struct got_error *error = NULL;
7568 struct got_repository *repo = NULL;
7569 struct got_worktree *worktree = NULL;
7571 struct got_pathlist_head paths;
7572 struct got_pathlist_entry *pe;
7573 int ch, can_recurse = 0, no_ignores = 0;
7574 int *pack_fds = NULL;
7578 while ((ch = getopt(argc, argv, "IR")) != -1) {
7596 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7603 cwd = getcwd(NULL, 0);
7605 error = got_error_from_errno("getcwd");
7609 error = got_repo_pack_fds_open(&pack_fds);
7613 error = got_worktree_open(&worktree, cwd);
7615 if (error->code == GOT_ERR_NOT_WORKTREE)
7616 error = wrap_not_worktree_error(error, "add", cwd);
7620 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7625 error = apply_unveil(got_repo_get_path(repo), 1,
7626 got_worktree_get_root_path(worktree));
7630 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7637 TAILQ_FOREACH(pe, &paths, entry) {
7638 if (asprintf(&ondisk_path, "%s/%s",
7639 got_worktree_get_root_path(worktree),
7641 error = got_error_from_errno("asprintf");
7644 if (lstat(ondisk_path, &sb) == -1) {
7645 if (errno == ENOENT) {
7649 error = got_error_from_errno2("lstat",
7655 if (S_ISDIR(sb.st_mode)) {
7656 error = got_error_msg(GOT_ERR_BAD_PATH,
7657 "adding directories requires -R option");
7663 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7664 NULL, repo, no_ignores);
7667 const struct got_error *close_err = got_repo_close(repo);
7672 got_worktree_close(worktree);
7674 const struct got_error *pack_err =
7675 got_repo_pack_fds_close(pack_fds);
7679 TAILQ_FOREACH(pe, &paths, entry)
7680 free((char *)pe->path);
7681 got_pathlist_free(&paths);
7689 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7690 "path ...\n", getprogname());
7694 static const struct got_error *
7695 print_remove_status(void *arg, unsigned char status,
7696 unsigned char staged_status, const char *path)
7698 while (path[0] == '/')
7700 if (status == GOT_STATUS_NONEXISTENT)
7702 if (status == staged_status && (status == GOT_STATUS_DELETE))
7703 status = GOT_STATUS_NO_CHANGE;
7704 printf("%c%c %s\n", status, staged_status, path);
7708 static const struct got_error *
7709 cmd_remove(int argc, char *argv[])
7711 const struct got_error *error = NULL;
7712 struct got_worktree *worktree = NULL;
7713 struct got_repository *repo = NULL;
7714 const char *status_codes = NULL;
7716 struct got_pathlist_head paths;
7717 struct got_pathlist_entry *pe;
7718 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7719 int ignore_missing_paths = 0;
7720 int *pack_fds = NULL;
7724 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7727 delete_local_mods = 1;
7728 ignore_missing_paths = 1;
7737 for (i = 0; i < strlen(optarg); i++) {
7738 switch (optarg[i]) {
7739 case GOT_STATUS_MODIFY:
7740 delete_local_mods = 1;
7742 case GOT_STATUS_MISSING:
7743 ignore_missing_paths = 1;
7746 errx(1, "invalid status code '%c'",
7750 status_codes = optarg;
7762 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7769 cwd = getcwd(NULL, 0);
7771 error = got_error_from_errno("getcwd");
7775 error = got_repo_pack_fds_open(&pack_fds);
7779 error = got_worktree_open(&worktree, cwd);
7781 if (error->code == GOT_ERR_NOT_WORKTREE)
7782 error = wrap_not_worktree_error(error, "remove", cwd);
7786 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7791 error = apply_unveil(got_repo_get_path(repo), 1,
7792 got_worktree_get_root_path(worktree));
7796 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7803 TAILQ_FOREACH(pe, &paths, entry) {
7804 if (asprintf(&ondisk_path, "%s/%s",
7805 got_worktree_get_root_path(worktree),
7807 error = got_error_from_errno("asprintf");
7810 if (lstat(ondisk_path, &sb) == -1) {
7811 if (errno == ENOENT) {
7815 error = got_error_from_errno2("lstat",
7821 if (S_ISDIR(sb.st_mode)) {
7822 error = got_error_msg(GOT_ERR_BAD_PATH,
7823 "removing directories requires -R option");
7829 error = got_worktree_schedule_delete(worktree, &paths,
7830 delete_local_mods, status_codes, print_remove_status, NULL,
7831 repo, keep_on_disk, ignore_missing_paths);
7834 const struct got_error *close_err = got_repo_close(repo);
7839 got_worktree_close(worktree);
7841 const struct got_error *pack_err =
7842 got_repo_pack_fds_close(pack_fds);
7846 TAILQ_FOREACH(pe, &paths, entry)
7847 free((char *)pe->path);
7848 got_pathlist_free(&paths);
7856 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7857 "[-R] [patchfile]\n", getprogname());
7861 static const struct got_error *
7862 patch_from_stdin(int *patchfd)
7864 const struct got_error *err = NULL;
7866 char *path, buf[BUFSIZ];
7867 sig_t sighup, sigint, sigquit;
7869 err = got_opentemp_named_fd(&path, patchfd,
7870 GOT_TMPDIR_STR "/got-patch");
7876 sighup = signal(SIGHUP, SIG_DFL);
7877 sigint = signal(SIGINT, SIG_DFL);
7878 sigquit = signal(SIGQUIT, SIG_DFL);
7881 r = read(0, buf, sizeof(buf));
7883 err = got_error_from_errno("read");
7888 if (write(*patchfd, buf, r) == -1) {
7889 err = got_error_from_errno("write");
7894 signal(SIGHUP, sighup);
7895 signal(SIGINT, sigint);
7896 signal(SIGQUIT, sigquit);
7898 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7899 err = got_error_from_errno("lseek");
7909 static const struct got_error *
7910 patch_progress(void *arg, const char *old, const char *new,
7911 unsigned char status, const struct got_error *error, int old_from,
7912 int old_lines, int new_from, int new_lines, int offset,
7913 int ws_mangled, const struct got_error *hunk_err)
7915 const char *path = new == NULL ? old : new;
7917 while (*path == '/')
7921 printf("%c %s\n", status, path);
7924 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7926 if (offset != 0 || hunk_err != NULL || ws_mangled) {
7927 printf("@@ -%d,%d +%d,%d @@ ", old_from,
7928 old_lines, new_from, new_lines);
7929 if (hunk_err != NULL)
7930 printf("%s\n", hunk_err->msg);
7931 else if (offset != 0)
7932 printf("applied with offset %d\n", offset);
7934 printf("hunk contains mangled whitespace\n");
7940 static const struct got_error *
7941 cmd_patch(int argc, char *argv[])
7943 const struct got_error *error = NULL, *close_error = NULL;
7944 struct got_worktree *worktree = NULL;
7945 struct got_repository *repo = NULL;
7948 int ch, nop = 0, strip = -1, reverse = 0;
7950 int *pack_fds = NULL;
7952 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7958 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7960 errx(1, "pathname strip count is %s: %s",
7976 error = patch_from_stdin(&patchfd);
7979 } else if (argc == 1) {
7980 patchfd = open(argv[0], O_RDONLY);
7981 if (patchfd == -1) {
7982 error = got_error_from_errno2("open", argv[0]);
7988 if ((cwd = getcwd(NULL, 0)) == NULL) {
7989 error = got_error_from_errno("getcwd");
7993 error = got_repo_pack_fds_open(&pack_fds);
7997 error = got_worktree_open(&worktree, cwd);
8001 const char *repo_path = got_worktree_get_repo_path(worktree);
8002 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8006 error = apply_unveil(got_repo_get_path(repo), 0,
8007 got_worktree_get_root_path(worktree));
8012 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
8017 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8018 &patch_progress, NULL, check_cancelled, NULL);
8022 close_error = got_repo_close(repo);
8024 error = close_error;
8026 if (worktree != NULL) {
8027 close_error = got_worktree_close(worktree);
8029 error = close_error;
8032 const struct got_error *pack_err =
8033 got_repo_pack_fds_close(pack_fds);
8044 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
8045 "path ...\n", getprogname());
8049 static const struct got_error *
8050 revert_progress(void *arg, unsigned char status, const char *path)
8052 if (status == GOT_STATUS_UNVERSIONED)
8055 while (path[0] == '/')
8057 printf("%c %s\n", status, path);
8061 struct choose_patch_arg {
8062 FILE *patch_script_file;
8066 static const struct got_error *
8067 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8068 int nchanges, const char *action)
8070 const struct got_error *err;
8072 size_t linesize = 0;
8076 case GOT_STATUS_ADD:
8077 printf("A %s\n%s this addition? [y/n] ", path, action);
8079 case GOT_STATUS_DELETE:
8080 printf("D %s\n%s this deletion? [y/n] ", path, action);
8082 case GOT_STATUS_MODIFY:
8083 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8084 return got_error_from_errno("fseek");
8085 printf(GOT_COMMIT_SEP_STR);
8086 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8088 if (linelen == -1 && ferror(patch_file)) {
8089 err = got_error_from_errno("getline");
8094 printf(GOT_COMMIT_SEP_STR);
8095 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8096 path, n, nchanges, action);
8099 return got_error_path(path, GOT_ERR_FILE_STATUS);
8105 static const struct got_error *
8106 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8107 FILE *patch_file, int n, int nchanges)
8109 const struct got_error *err = NULL;
8111 size_t linesize = 0;
8114 struct choose_patch_arg *a = arg;
8116 *choice = GOT_PATCH_CHOICE_NONE;
8118 if (a->patch_script_file) {
8120 err = show_change(status, path, patch_file, n, nchanges,
8124 linelen = getline(&line, &linesize, a->patch_script_file);
8125 if (linelen == -1) {
8126 if (ferror(a->patch_script_file))
8127 return got_error_from_errno("getline");
8130 nl = strchr(line, '\n');
8133 if (strcmp(line, "y") == 0) {
8134 *choice = GOT_PATCH_CHOICE_YES;
8136 } else if (strcmp(line, "n") == 0) {
8137 *choice = GOT_PATCH_CHOICE_NO;
8139 } else if (strcmp(line, "q") == 0 &&
8140 status == GOT_STATUS_MODIFY) {
8141 *choice = GOT_PATCH_CHOICE_QUIT;
8144 printf("invalid response '%s'\n", line);
8149 while (resp != 'y' && resp != 'n' && resp != 'q') {
8150 err = show_change(status, path, patch_file, n, nchanges,
8157 if (status == GOT_STATUS_MODIFY) {
8158 if (resp != 'y' && resp != 'n' && resp != 'q') {
8159 printf("invalid response '%c'\n", resp);
8162 } else if (resp != 'y' && resp != 'n') {
8163 printf("invalid response '%c'\n", resp);
8169 *choice = GOT_PATCH_CHOICE_YES;
8170 else if (resp == 'n')
8171 *choice = GOT_PATCH_CHOICE_NO;
8172 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8173 *choice = GOT_PATCH_CHOICE_QUIT;
8178 static const struct got_error *
8179 cmd_revert(int argc, char *argv[])
8181 const struct got_error *error = NULL;
8182 struct got_worktree *worktree = NULL;
8183 struct got_repository *repo = NULL;
8184 char *cwd = NULL, *path = NULL;
8185 struct got_pathlist_head paths;
8186 struct got_pathlist_entry *pe;
8187 int ch, can_recurse = 0, pflag = 0;
8188 FILE *patch_script_file = NULL;
8189 const char *patch_script_path = NULL;
8190 struct choose_patch_arg cpa;
8191 int *pack_fds = NULL;
8195 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8201 patch_script_path = optarg;
8216 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8217 "unveil", NULL) == -1)
8222 if (patch_script_path && !pflag)
8223 errx(1, "-F option can only be used together with -p option");
8225 cwd = getcwd(NULL, 0);
8227 error = got_error_from_errno("getcwd");
8231 error = got_repo_pack_fds_open(&pack_fds);
8235 error = got_worktree_open(&worktree, cwd);
8237 if (error->code == GOT_ERR_NOT_WORKTREE)
8238 error = wrap_not_worktree_error(error, "revert", cwd);
8242 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8247 if (patch_script_path) {
8248 patch_script_file = fopen(patch_script_path, "re");
8249 if (patch_script_file == NULL) {
8250 error = got_error_from_errno2("fopen",
8255 error = apply_unveil(got_repo_get_path(repo), 1,
8256 got_worktree_get_root_path(worktree));
8260 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8267 TAILQ_FOREACH(pe, &paths, entry) {
8268 if (asprintf(&ondisk_path, "%s/%s",
8269 got_worktree_get_root_path(worktree),
8271 error = got_error_from_errno("asprintf");
8274 if (lstat(ondisk_path, &sb) == -1) {
8275 if (errno == ENOENT) {
8279 error = got_error_from_errno2("lstat",
8285 if (S_ISDIR(sb.st_mode)) {
8286 error = got_error_msg(GOT_ERR_BAD_PATH,
8287 "reverting directories requires -R option");
8293 cpa.patch_script_file = patch_script_file;
8294 cpa.action = "revert";
8295 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8296 pflag ? choose_patch : NULL, &cpa, repo);
8298 if (patch_script_file && fclose(patch_script_file) == EOF &&
8300 error = got_error_from_errno2("fclose", patch_script_path);
8302 const struct got_error *close_err = got_repo_close(repo);
8307 got_worktree_close(worktree);
8309 const struct got_error *pack_err =
8310 got_repo_pack_fds_close(pack_fds);
8322 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8323 "[path ...]\n", getprogname());
8327 struct collect_commit_logmsg_arg {
8328 const char *cmdline_log;
8329 const char *prepared_log;
8330 int non_interactive;
8332 const char *worktree_path;
8333 const char *branch_name;
8334 const char *repo_path;
8339 static const struct got_error *
8340 read_prepared_logmsg(char **logmsg, const char *path)
8342 const struct got_error *err = NULL;
8348 memset(&sb, 0, sizeof(sb));
8350 f = fopen(path, "re");
8352 return got_error_from_errno2("fopen", path);
8354 if (fstat(fileno(f), &sb) == -1) {
8355 err = got_error_from_errno2("fstat", path);
8358 if (sb.st_size == 0) {
8359 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8363 *logmsg = malloc(sb.st_size + 1);
8364 if (*logmsg == NULL) {
8365 err = got_error_from_errno("malloc");
8369 r = fread(*logmsg, 1, sb.st_size, f);
8370 if (r != sb.st_size) {
8372 err = got_error_from_errno2("fread", path);
8374 err = got_error(GOT_ERR_IO);
8377 (*logmsg)[sb.st_size] = '\0';
8379 if (fclose(f) == EOF && err == NULL)
8380 err = got_error_from_errno2("fclose", path);
8389 static const struct got_error *
8390 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8393 char *initial_content = NULL;
8394 struct got_pathlist_entry *pe;
8395 const struct got_error *err = NULL;
8396 char *template = NULL;
8397 struct collect_commit_logmsg_arg *a = arg;
8398 int initial_content_len;
8402 /* if a message was specified on the command line, just use it */
8403 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8404 len = strlen(a->cmdline_log) + 1;
8405 *logmsg = malloc(len + 1);
8406 if (*logmsg == NULL)
8407 return got_error_from_errno("malloc");
8408 strlcpy(*logmsg, a->cmdline_log, len);
8410 } else if (a->prepared_log != NULL && a->non_interactive)
8411 return read_prepared_logmsg(logmsg, a->prepared_log);
8413 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8414 return got_error_from_errno("asprintf");
8416 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8420 if (a->prepared_log) {
8422 err = read_prepared_logmsg(&msg, a->prepared_log);
8425 if (write(fd, msg, strlen(msg)) == -1) {
8426 err = got_error_from_errno2("write", a->logmsg_path);
8433 initial_content_len = asprintf(&initial_content,
8434 "\n# changes to be committed on branch %s:\n",
8436 if (initial_content_len == -1) {
8437 err = got_error_from_errno("asprintf");
8441 if (write(fd, initial_content, initial_content_len) == -1) {
8442 err = got_error_from_errno2("write", a->logmsg_path);
8446 TAILQ_FOREACH(pe, commitable_paths, entry) {
8447 struct got_commitable *ct = pe->data;
8448 dprintf(fd, "# %c %s\n",
8449 got_commitable_get_status(ct),
8450 got_commitable_get_path(ct));
8453 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8454 initial_content_len, a->prepared_log ? 0 : 1);
8456 free(initial_content);
8459 if (fd != -1 && close(fd) == -1 && err == NULL)
8460 err = got_error_from_errno2("close", a->logmsg_path);
8462 /* Editor is done; we can now apply unveil(2) */
8464 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8472 static const struct got_error *
8473 cmd_commit(int argc, char *argv[])
8475 const struct got_error *error = NULL;
8476 struct got_worktree *worktree = NULL;
8477 struct got_repository *repo = NULL;
8478 char *cwd = NULL, *id_str = NULL;
8479 struct got_object_id *id = NULL;
8480 const char *logmsg = NULL;
8481 char *prepared_logmsg = NULL;
8482 struct collect_commit_logmsg_arg cl_arg;
8483 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8484 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8485 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8486 struct got_pathlist_head paths;
8487 int *pack_fds = NULL;
8490 cl_arg.logmsg_path = NULL;
8492 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8496 option_conflict('F', 'm');
8497 prepared_logmsg = realpath(optarg, NULL);
8498 if (prepared_logmsg == NULL)
8499 return got_error_from_errno2("realpath",
8503 if (prepared_logmsg)
8504 option_conflict('m', 'F');
8508 non_interactive = 1;
8511 allow_bad_symlinks = 1;
8523 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8524 "unveil", NULL) == -1)
8527 cwd = getcwd(NULL, 0);
8529 error = got_error_from_errno("getcwd");
8533 error = got_repo_pack_fds_open(&pack_fds);
8537 error = got_worktree_open(&worktree, cwd);
8539 if (error->code == GOT_ERR_NOT_WORKTREE)
8540 error = wrap_not_worktree_error(error, "commit", cwd);
8544 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8547 if (rebase_in_progress) {
8548 error = got_error(GOT_ERR_REBASING);
8552 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8557 error = get_gitconfig_path(&gitconfig_path);
8560 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8561 gitconfig_path, pack_fds);
8565 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8568 if (merge_in_progress) {
8569 error = got_error(GOT_ERR_MERGE_BUSY);
8573 error = get_author(&author, repo, worktree);
8578 * unveil(2) traverses exec(2); if an editor is used we have
8579 * to apply unveil after the log message has been written.
8581 if (logmsg == NULL || strlen(logmsg) == 0)
8582 error = get_editor(&editor);
8584 error = apply_unveil(got_repo_get_path(repo), 0,
8585 got_worktree_get_root_path(worktree));
8589 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8593 cl_arg.editor = editor;
8594 cl_arg.cmdline_log = logmsg;
8595 cl_arg.prepared_log = prepared_logmsg;
8596 cl_arg.non_interactive = non_interactive;
8597 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8598 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8599 if (!histedit_in_progress) {
8600 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8601 error = got_error(GOT_ERR_COMMIT_BRANCH);
8604 cl_arg.branch_name += 11;
8606 cl_arg.repo_path = got_repo_get_path(repo);
8607 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8608 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8609 print_status, NULL, repo);
8611 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8612 cl_arg.logmsg_path != NULL)
8613 preserve_logmsg = 1;
8617 error = got_object_id_str(&id_str, id);
8620 printf("Created commit %s\n", id_str);
8622 if (preserve_logmsg) {
8623 fprintf(stderr, "%s: log message preserved in %s\n",
8624 getprogname(), cl_arg.logmsg_path);
8625 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8627 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8628 free(cl_arg.logmsg_path);
8630 const struct got_error *close_err = got_repo_close(repo);
8635 got_worktree_close(worktree);
8637 const struct got_error *pack_err =
8638 got_repo_pack_fds_close(pack_fds);
8644 free(gitconfig_path);
8647 free(prepared_logmsg);
8654 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8655 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8656 "[remote-repository]\n", getprogname());
8661 print_load_info(int print_colored, int print_found, int print_trees,
8662 int ncolored, int nfound, int ntrees)
8664 if (print_colored) {
8665 printf("%d commit%s colored", ncolored,
8666 ncolored == 1 ? "" : "s");
8669 printf("%s%d object%s found",
8670 ncolored > 0 ? "; " : "",
8671 nfound, nfound == 1 ? "" : "s");
8674 printf("; %d tree%s scanned", ntrees,
8675 ntrees == 1 ? "" : "s");
8679 struct got_send_progress_arg {
8680 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8687 int last_nobj_total;
8691 int printed_something;
8693 struct got_pathlist_head *delete_branches;
8696 static const struct got_error *
8697 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8698 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8699 int nobj_written, off_t bytes_sent, const char *refname, int success)
8701 struct got_send_progress_arg *a = arg;
8702 char scaled_packsize[FMT_SCALED_STRSIZE];
8703 char scaled_sent[FMT_SCALED_STRSIZE];
8704 int p_deltify = 0, p_written = 0, p_sent = 0;
8705 int print_colored = 0, print_found = 0, print_trees = 0;
8706 int print_searching = 0, print_total = 0;
8707 int print_deltify = 0, print_written = 0, print_sent = 0;
8709 if (a->verbosity < 0)
8713 const char *status = success ? "accepted" : "rejected";
8716 struct got_pathlist_entry *pe;
8717 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8718 const char *branchname = pe->path;
8719 if (got_path_cmp(branchname, refname,
8720 strlen(branchname), strlen(refname)) == 0) {
8722 a->sent_something = 1;
8728 if (a->printed_something)
8730 printf("Server has %s %s", status, refname);
8731 a->printed_something = 1;
8735 if (a->last_ncolored != ncolored) {
8737 a->last_ncolored = ncolored;
8740 if (a->last_nfound != nfound) {
8743 a->last_nfound = nfound;
8746 if (a->last_ntrees != ntrees) {
8750 a->last_ntrees = ntrees;
8753 if ((print_colored || print_found || print_trees) &&
8756 print_load_info(print_colored, print_found, print_trees,
8757 ncolored, nfound, ntrees);
8758 a->printed_something = 1;
8761 } else if (!a->loading_done) {
8763 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8765 a->loading_done = 1;
8768 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8769 return got_error_from_errno("fmt_scaled");
8770 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8771 return got_error_from_errno("fmt_scaled");
8773 if (a->last_ncommits != ncommits) {
8774 print_searching = 1;
8775 a->last_ncommits = ncommits;
8778 if (a->last_nobj_total != nobj_total) {
8779 print_searching = 1;
8781 a->last_nobj_total = nobj_total;
8784 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8785 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8786 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8787 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8788 return got_error(GOT_ERR_NO_SPACE);
8791 if (nobj_deltify > 0 || nobj_written > 0) {
8792 if (nobj_deltify > 0) {
8793 p_deltify = (nobj_deltify * 100) / nobj_total;
8794 if (p_deltify != a->last_p_deltify) {
8795 a->last_p_deltify = p_deltify;
8796 print_searching = 1;
8801 if (nobj_written > 0) {
8802 p_written = (nobj_written * 100) / nobj_total;
8803 if (p_written != a->last_p_written) {
8804 a->last_p_written = p_written;
8805 print_searching = 1;
8813 if (bytes_sent > 0) {
8814 p_sent = (bytes_sent * 100) / packfile_size;
8815 if (p_sent != a->last_p_sent) {
8816 a->last_p_sent = p_sent;
8817 print_searching = 1;
8823 a->sent_something = 1;
8826 if (print_searching || print_total || print_deltify || print_written ||
8829 if (print_searching)
8830 printf("packing %d reference%s", ncommits,
8831 ncommits == 1 ? "" : "s");
8833 printf("; %d object%s", nobj_total,
8834 nobj_total == 1 ? "" : "s");
8836 printf("; deltify: %d%%", p_deltify);
8838 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8839 scaled_packsize, p_sent);
8840 else if (print_written)
8841 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8842 scaled_packsize, p_written);
8843 if (print_searching || print_total || print_deltify ||
8844 print_written || print_sent) {
8845 a->printed_something = 1;
8851 static const struct got_error *
8852 cmd_send(int argc, char *argv[])
8854 const struct got_error *error = NULL;
8855 char *cwd = NULL, *repo_path = NULL;
8856 const char *remote_name;
8857 char *proto = NULL, *host = NULL, *port = NULL;
8858 char *repo_name = NULL, *server_path = NULL;
8859 const struct got_remote_repo *remotes, *remote = NULL;
8860 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8861 struct got_repository *repo = NULL;
8862 struct got_worktree *worktree = NULL;
8863 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8864 struct got_pathlist_head branches;
8865 struct got_pathlist_head tags;
8866 struct got_reflist_head all_branches;
8867 struct got_reflist_head all_tags;
8868 struct got_pathlist_head delete_args;
8869 struct got_pathlist_head delete_branches;
8870 struct got_reflist_entry *re;
8871 struct got_pathlist_entry *pe;
8872 int i, ch, sendfd = -1, sendstatus;
8874 struct got_send_progress_arg spa;
8875 int verbosity = 0, overwrite_refs = 0;
8876 int send_all_branches = 0, send_all_tags = 0;
8877 struct got_reference *ref = NULL;
8878 int *pack_fds = NULL;
8880 TAILQ_INIT(&branches);
8882 TAILQ_INIT(&all_branches);
8883 TAILQ_INIT(&all_tags);
8884 TAILQ_INIT(&delete_args);
8885 TAILQ_INIT(&delete_branches);
8887 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8890 send_all_branches = 1;
8893 error = got_pathlist_append(&branches, optarg, NULL);
8899 error = got_pathlist_append(&delete_args, optarg, NULL);
8907 repo_path = realpath(optarg, NULL);
8908 if (repo_path == NULL)
8909 return got_error_from_errno2("realpath",
8911 got_path_strip_trailing_slashes(repo_path);
8914 error = got_pathlist_append(&tags, optarg, NULL);
8925 else if (verbosity < 3)
8939 if (send_all_branches && !TAILQ_EMPTY(&branches))
8940 option_conflict('a', 'b');
8941 if (send_all_tags && !TAILQ_EMPTY(&tags))
8942 option_conflict('T', 't');
8946 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8948 remote_name = argv[0];
8952 cwd = getcwd(NULL, 0);
8954 error = got_error_from_errno("getcwd");
8958 error = got_repo_pack_fds_open(&pack_fds);
8962 if (repo_path == NULL) {
8963 error = got_worktree_open(&worktree, cwd);
8964 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8970 strdup(got_worktree_get_repo_path(worktree));
8971 if (repo_path == NULL)
8972 error = got_error_from_errno("strdup");
8976 repo_path = strdup(cwd);
8977 if (repo_path == NULL) {
8978 error = got_error_from_errno("strdup");
8984 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8989 worktree_conf = got_worktree_get_gotconfig(worktree);
8990 if (worktree_conf) {
8991 got_gotconfig_get_remotes(&nremotes, &remotes,
8993 for (i = 0; i < nremotes; i++) {
8994 if (strcmp(remotes[i].name, remote_name) == 0) {
8995 remote = &remotes[i];
9001 if (remote == NULL) {
9002 repo_conf = got_repo_get_gotconfig(repo);
9004 got_gotconfig_get_remotes(&nremotes, &remotes,
9006 for (i = 0; i < nremotes; i++) {
9007 if (strcmp(remotes[i].name, remote_name) == 0) {
9008 remote = &remotes[i];
9014 if (remote == NULL) {
9015 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9016 for (i = 0; i < nremotes; i++) {
9017 if (strcmp(remotes[i].name, remote_name) == 0) {
9018 remote = &remotes[i];
9023 if (remote == NULL) {
9024 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9028 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9029 &repo_name, remote->send_url);
9033 if (strcmp(proto, "git") == 0) {
9035 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9036 "sendfd dns inet unveil", NULL) == -1)
9039 } else if (strcmp(proto, "git+ssh") == 0 ||
9040 strcmp(proto, "ssh") == 0) {
9042 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9043 "sendfd unveil", NULL) == -1)
9046 } else if (strcmp(proto, "http") == 0 ||
9047 strcmp(proto, "git+http") == 0) {
9048 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9051 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9055 error = got_dial_apply_unveil(proto);
9059 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9063 if (send_all_branches) {
9064 error = got_ref_list(&all_branches, repo, "refs/heads",
9065 got_ref_cmp_by_name, NULL);
9068 TAILQ_FOREACH(re, &all_branches, entry) {
9069 const char *branchname = got_ref_get_name(re->ref);
9070 error = got_pathlist_append(&branches,
9076 } else if (nbranches == 0) {
9077 for (i = 0; i < remote->nsend_branches; i++) {
9078 got_pathlist_append(&branches,
9079 remote->send_branches[i], NULL);
9083 if (send_all_tags) {
9084 error = got_ref_list(&all_tags, repo, "refs/tags",
9085 got_ref_cmp_by_name, NULL);
9088 TAILQ_FOREACH(re, &all_tags, entry) {
9089 const char *tagname = got_ref_get_name(re->ref);
9090 error = got_pathlist_append(&tags,
9099 * To prevent accidents only branches in refs/heads/ can be deleted
9100 * with 'got send -d'.
9101 * Deleting anything else requires local repository access or Git.
9103 TAILQ_FOREACH(pe, &delete_args, entry) {
9104 const char *branchname = pe->path;
9106 struct got_pathlist_entry *new;
9107 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9108 s = strdup(branchname);
9110 error = got_error_from_errno("strdup");
9114 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9115 error = got_error_from_errno("asprintf");
9119 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9120 if (error || new == NULL /* duplicate */)
9127 if (nbranches == 0 && ndelete_branches == 0) {
9128 struct got_reference *head_ref;
9130 error = got_ref_open(&head_ref, repo,
9131 got_worktree_get_head_ref_name(worktree), 0);
9133 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9136 if (got_ref_is_symbolic(head_ref)) {
9137 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9138 got_ref_close(head_ref);
9143 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9151 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
9152 port ? ":" : "", port ? port : "");
9154 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9155 server_path, verbosity);
9159 memset(&spa, 0, sizeof(spa));
9160 spa.last_scaled_packsize[0] = '\0';
9161 spa.last_p_deltify = -1;
9162 spa.last_p_written = -1;
9163 spa.verbosity = verbosity;
9164 spa.delete_branches = &delete_branches;
9165 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9166 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9167 check_cancelled, NULL);
9168 if (spa.printed_something)
9172 if (!spa.sent_something && verbosity >= 0)
9173 printf("Already up-to-date\n");
9176 if (kill(sendpid, SIGTERM) == -1)
9177 error = got_error_from_errno("kill");
9178 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9179 error = got_error_from_errno("waitpid");
9181 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9182 error = got_error_from_errno("close");
9184 const struct got_error *close_err = got_repo_close(repo);
9189 got_worktree_close(worktree);
9191 const struct got_error *pack_err =
9192 got_repo_pack_fds_close(pack_fds);
9198 got_pathlist_free(&branches);
9199 got_pathlist_free(&tags);
9200 got_ref_list_free(&all_branches);
9201 got_ref_list_free(&all_tags);
9202 got_pathlist_free(&delete_args);
9203 TAILQ_FOREACH(pe, &delete_branches, entry)
9204 free((char *)pe->path);
9205 got_pathlist_free(&delete_branches);
9217 usage_cherrypick(void)
9219 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9223 static const struct got_error *
9224 cmd_cherrypick(int argc, char *argv[])
9226 const struct got_error *error = NULL;
9227 struct got_worktree *worktree = NULL;
9228 struct got_repository *repo = NULL;
9229 char *cwd = NULL, *commit_id_str = NULL;
9230 struct got_object_id *commit_id = NULL;
9231 struct got_commit_object *commit = NULL;
9232 struct got_object_qid *pid;
9234 struct got_update_progress_arg upa;
9235 int *pack_fds = NULL;
9237 while ((ch = getopt(argc, argv, "")) != -1) {
9249 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9250 "unveil", NULL) == -1)
9256 cwd = getcwd(NULL, 0);
9258 error = got_error_from_errno("getcwd");
9262 error = got_repo_pack_fds_open(&pack_fds);
9266 error = got_worktree_open(&worktree, cwd);
9268 if (error->code == GOT_ERR_NOT_WORKTREE)
9269 error = wrap_not_worktree_error(error, "cherrypick",
9274 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9279 error = apply_unveil(got_repo_get_path(repo), 0,
9280 got_worktree_get_root_path(worktree));
9284 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9285 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9288 error = got_object_id_str(&commit_id_str, commit_id);
9292 error = got_object_open_as_commit(&commit, repo, commit_id);
9295 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9296 memset(&upa, 0, sizeof(upa));
9297 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9298 commit_id, repo, update_progress, &upa, check_cancelled,
9303 if (upa.did_something)
9304 printf("Merged commit %s\n", commit_id_str);
9305 print_merge_progress_stats(&upa);
9308 got_object_commit_close(commit);
9309 free(commit_id_str);
9311 got_worktree_close(worktree);
9313 const struct got_error *close_err = got_repo_close(repo);
9318 const struct got_error *pack_err =
9319 got_repo_pack_fds_close(pack_fds);
9330 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9334 static const struct got_error *
9335 cmd_backout(int argc, char *argv[])
9337 const struct got_error *error = NULL;
9338 struct got_worktree *worktree = NULL;
9339 struct got_repository *repo = NULL;
9340 char *cwd = NULL, *commit_id_str = NULL;
9341 struct got_object_id *commit_id = NULL;
9342 struct got_commit_object *commit = NULL;
9343 struct got_object_qid *pid;
9345 struct got_update_progress_arg upa;
9346 int *pack_fds = NULL;
9348 while ((ch = getopt(argc, argv, "")) != -1) {
9360 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9361 "unveil", NULL) == -1)
9367 cwd = getcwd(NULL, 0);
9369 error = got_error_from_errno("getcwd");
9373 error = got_repo_pack_fds_open(&pack_fds);
9377 error = got_worktree_open(&worktree, cwd);
9379 if (error->code == GOT_ERR_NOT_WORKTREE)
9380 error = wrap_not_worktree_error(error, "backout", cwd);
9384 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9389 error = apply_unveil(got_repo_get_path(repo), 0,
9390 got_worktree_get_root_path(worktree));
9394 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9395 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9398 error = got_object_id_str(&commit_id_str, commit_id);
9402 error = got_object_open_as_commit(&commit, repo, commit_id);
9405 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9407 error = got_error(GOT_ERR_ROOT_COMMIT);
9411 memset(&upa, 0, sizeof(upa));
9412 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9413 repo, update_progress, &upa, check_cancelled, NULL);
9417 if (upa.did_something)
9418 printf("Backed out commit %s\n", commit_id_str);
9419 print_merge_progress_stats(&upa);
9422 got_object_commit_close(commit);
9423 free(commit_id_str);
9425 got_worktree_close(worktree);
9427 const struct got_error *close_err = got_repo_close(repo);
9432 const struct got_error *pack_err =
9433 got_repo_pack_fds_close(pack_fds);
9443 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9449 trim_logmsg(char *logmsg, int limit)
9454 len = strlen(logmsg);
9458 nl = strchr(logmsg, '\n');
9463 static const struct got_error *
9464 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9466 const struct got_error *err;
9467 char *logmsg0 = NULL;
9470 err = got_object_commit_get_logmsg(&logmsg0, commit);
9475 while (isspace((unsigned char)s[0]))
9478 *logmsg = strdup(s);
9479 if (*logmsg == NULL) {
9480 err = got_error_from_errno("strdup");
9484 trim_logmsg(*logmsg, limit);
9490 static const struct got_error *
9491 show_rebase_merge_conflict(struct got_object_id *id,
9492 struct got_repository *repo)
9494 const struct got_error *err;
9495 struct got_commit_object *commit = NULL;
9496 char *id_str = NULL, *logmsg = NULL;
9498 err = got_object_open_as_commit(&commit, repo, id);
9502 err = got_object_id_str(&id_str, id);
9508 err = get_short_logmsg(&logmsg, 42, commit);
9512 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9515 got_object_commit_close(commit);
9520 static const struct got_error *
9521 show_rebase_progress(struct got_commit_object *commit,
9522 struct got_object_id *old_id, struct got_object_id *new_id)
9524 const struct got_error *err;
9525 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9527 err = got_object_id_str(&old_id_str, old_id);
9532 err = got_object_id_str(&new_id_str, new_id);
9537 old_id_str[12] = '\0';
9539 new_id_str[12] = '\0';
9541 err = get_short_logmsg(&logmsg, 42, commit);
9545 printf("%s -> %s: %s\n", old_id_str,
9546 new_id_str ? new_id_str : "no-op change", logmsg);
9554 static const struct got_error *
9555 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9556 struct got_reference *branch, struct got_reference *new_base_branch,
9557 struct got_reference *tmp_branch, struct got_repository *repo,
9560 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9561 return got_worktree_rebase_complete(worktree, fileindex,
9562 new_base_branch, tmp_branch, branch, repo, create_backup);
9565 static const struct got_error *
9566 rebase_commit(struct got_pathlist_head *merged_paths,
9567 struct got_worktree *worktree, struct got_fileindex *fileindex,
9568 struct got_reference *tmp_branch,
9569 struct got_object_id *commit_id, struct got_repository *repo)
9571 const struct got_error *error;
9572 struct got_commit_object *commit;
9573 struct got_object_id *new_commit_id;
9575 error = got_object_open_as_commit(&commit, repo, commit_id);
9579 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9580 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9582 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9584 error = show_rebase_progress(commit, commit_id, NULL);
9586 error = show_rebase_progress(commit, commit_id, new_commit_id);
9587 free(new_commit_id);
9590 got_object_commit_close(commit);
9594 struct check_path_prefix_arg {
9595 const char *path_prefix;
9600 static const struct got_error *
9601 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9602 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9603 struct got_object_id *id1, struct got_object_id *id2,
9604 const char *path1, const char *path2,
9605 mode_t mode1, mode_t mode2, struct got_repository *repo)
9607 struct check_path_prefix_arg *a = arg;
9609 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9610 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9611 return got_error(a->errcode);
9616 static const struct got_error *
9617 check_path_prefix(struct got_object_id *parent_id,
9618 struct got_object_id *commit_id, const char *path_prefix,
9619 int errcode, struct got_repository *repo)
9621 const struct got_error *err;
9622 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9623 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9624 struct check_path_prefix_arg cpp_arg;
9626 if (got_path_is_root_dir(path_prefix))
9629 err = got_object_open_as_commit(&commit, repo, commit_id);
9633 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9637 err = got_object_open_as_tree(&tree1, repo,
9638 got_object_commit_get_tree_id(parent_commit));
9642 err = got_object_open_as_tree(&tree2, repo,
9643 got_object_commit_get_tree_id(commit));
9647 cpp_arg.path_prefix = path_prefix;
9648 while (cpp_arg.path_prefix[0] == '/')
9649 cpp_arg.path_prefix++;
9650 cpp_arg.len = strlen(cpp_arg.path_prefix);
9651 cpp_arg.errcode = errcode;
9652 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9653 check_path_prefix_in_diff, &cpp_arg, 0);
9656 got_object_tree_close(tree1);
9658 got_object_tree_close(tree2);
9660 got_object_commit_close(commit);
9662 got_object_commit_close(parent_commit);
9666 static const struct got_error *
9667 collect_commits(struct got_object_id_queue *commits,
9668 struct got_object_id *initial_commit_id,
9669 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9670 const char *path_prefix, int path_prefix_errcode,
9671 struct got_repository *repo)
9673 const struct got_error *err = NULL;
9674 struct got_commit_graph *graph = NULL;
9675 struct got_object_id *parent_id = NULL;
9676 struct got_object_qid *qid;
9677 struct got_object_id *commit_id = initial_commit_id;
9679 err = got_commit_graph_open(&graph, "/", 1);
9683 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9684 check_cancelled, NULL);
9687 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9688 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9689 check_cancelled, NULL);
9691 if (err->code == GOT_ERR_ITER_COMPLETED) {
9692 err = got_error_msg(GOT_ERR_ANCESTRY,
9693 "ran out of commits to rebase before "
9694 "youngest common ancestor commit has "
9699 err = check_path_prefix(parent_id, commit_id,
9700 path_prefix, path_prefix_errcode, repo);
9704 err = got_object_qid_alloc(&qid, commit_id);
9707 STAILQ_INSERT_HEAD(commits, qid, entry);
9708 commit_id = parent_id;
9712 got_commit_graph_close(graph);
9716 static const struct got_error *
9717 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9719 const struct got_error *err = NULL;
9720 time_t committer_time;
9722 char datebuf[11]; /* YYYY-MM-DD + NUL */
9723 char *author0 = NULL, *author, *smallerthan;
9724 char *logmsg0 = NULL, *logmsg, *newline;
9726 committer_time = got_object_commit_get_committer_time(commit);
9727 if (gmtime_r(&committer_time, &tm) == NULL)
9728 return got_error_from_errno("gmtime_r");
9729 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9730 return got_error(GOT_ERR_NO_SPACE);
9732 author0 = strdup(got_object_commit_get_author(commit));
9733 if (author0 == NULL)
9734 return got_error_from_errno("strdup");
9736 smallerthan = strchr(author, '<');
9737 if (smallerthan && smallerthan[1] != '\0')
9738 author = smallerthan + 1;
9739 author[strcspn(author, "@>")] = '\0';
9741 err = got_object_commit_get_logmsg(&logmsg0, commit);
9745 while (*logmsg == '\n')
9747 newline = strchr(logmsg, '\n');
9751 if (asprintf(brief_str, "%s %s %s",
9752 datebuf, author, logmsg) == -1)
9753 err = got_error_from_errno("asprintf");
9760 static const struct got_error *
9761 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9762 struct got_repository *repo)
9764 const struct got_error *err;
9767 err = got_object_id_str(&id_str, id);
9771 err = got_ref_delete(ref, repo);
9775 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9781 static const struct got_error *
9782 print_backup_ref(const char *branch_name, const char *new_id_str,
9783 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9784 struct got_reflist_object_id_map *refs_idmap,
9785 struct got_repository *repo)
9787 const struct got_error *err = NULL;
9788 struct got_reflist_head *refs;
9789 char *refs_str = NULL;
9790 struct got_object_id *new_commit_id = NULL;
9791 struct got_commit_object *new_commit = NULL;
9792 char *new_commit_brief_str = NULL;
9793 struct got_object_id *yca_id = NULL;
9794 struct got_commit_object *yca_commit = NULL;
9795 char *yca_id_str = NULL, *yca_brief_str = NULL;
9796 char *custom_refs_str;
9798 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9799 return got_error_from_errno("asprintf");
9801 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9802 0, 0, refs_idmap, custom_refs_str);
9806 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9810 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9812 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9817 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9821 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9825 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9826 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9830 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9831 refs_str ? " (" : "", refs_str ? refs_str : "",
9832 refs_str ? ")" : "", new_commit_brief_str);
9833 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9834 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9838 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9842 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9846 err = got_object_id_str(&yca_id_str, yca_id);
9850 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9852 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9856 printf("history forked at %s%s%s%s\n %s\n",
9858 refs_str ? " (" : "", refs_str ? refs_str : "",
9859 refs_str ? ")" : "", yca_brief_str);
9862 free(custom_refs_str);
9863 free(new_commit_id);
9867 free(yca_brief_str);
9869 got_object_commit_close(new_commit);
9871 got_object_commit_close(yca_commit);
9876 static const struct got_error *
9877 process_backup_refs(const char *backup_ref_prefix,
9878 const char *wanted_branch_name,
9879 int delete, struct got_repository *repo)
9881 const struct got_error *err;
9882 struct got_reflist_head refs, backup_refs;
9883 struct got_reflist_entry *re;
9884 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9885 struct got_object_id *old_commit_id = NULL;
9886 char *branch_name = NULL;
9887 struct got_commit_object *old_commit = NULL;
9888 struct got_reflist_object_id_map *refs_idmap = NULL;
9889 int wanted_branch_found = 0;
9892 TAILQ_INIT(&backup_refs);
9894 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9898 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9902 if (wanted_branch_name) {
9903 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9904 wanted_branch_name += 11;
9907 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9908 got_ref_cmp_by_commit_timestamp_descending, repo);
9912 TAILQ_FOREACH(re, &backup_refs, entry) {
9913 const char *refname = got_ref_get_name(re->ref);
9916 err = check_cancelled(NULL);
9920 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9924 err = got_object_open_as_commit(&old_commit, repo,
9929 if (strncmp(backup_ref_prefix, refname,
9930 backup_ref_prefix_len) == 0)
9931 refname += backup_ref_prefix_len;
9933 while (refname[0] == '/')
9936 branch_name = strdup(refname);
9937 if (branch_name == NULL) {
9938 err = got_error_from_errno("strdup");
9941 slash = strrchr(branch_name, '/');
9944 refname += strlen(branch_name) + 1;
9947 if (wanted_branch_name == NULL ||
9948 strcmp(wanted_branch_name, branch_name) == 0) {
9949 wanted_branch_found = 1;
9951 err = delete_backup_ref(re->ref,
9952 old_commit_id, repo);
9954 err = print_backup_ref(branch_name, refname,
9955 old_commit_id, old_commit, refs_idmap,
9962 free(old_commit_id);
9963 old_commit_id = NULL;
9966 got_object_commit_close(old_commit);
9970 if (wanted_branch_name && !wanted_branch_found) {
9971 err = got_error_fmt(GOT_ERR_NOT_REF,
9972 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9976 got_reflist_object_id_map_free(refs_idmap);
9977 got_ref_list_free(&refs);
9978 got_ref_list_free(&backup_refs);
9979 free(old_commit_id);
9982 got_object_commit_close(old_commit);
9986 static const struct got_error *
9987 abort_progress(void *arg, unsigned char status, const char *path)
9990 * Unversioned files should not clutter progress output when
9991 * an operation is aborted.
9993 if (status == GOT_STATUS_UNVERSIONED)
9996 return update_progress(arg, status, path);
9999 static const struct got_error *
10000 cmd_rebase(int argc, char *argv[])
10002 const struct got_error *error = NULL;
10003 struct got_worktree *worktree = NULL;
10004 struct got_repository *repo = NULL;
10005 struct got_fileindex *fileindex = NULL;
10007 struct got_reference *branch = NULL;
10008 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10009 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10010 struct got_object_id *resume_commit_id = NULL;
10011 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10012 struct got_commit_object *commit = NULL;
10013 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10014 int histedit_in_progress = 0, merge_in_progress = 0;
10015 int create_backup = 1, list_backups = 0, delete_backups = 0;
10016 struct got_object_id_queue commits;
10017 struct got_pathlist_head merged_paths;
10018 const struct got_object_id_queue *parent_ids;
10019 struct got_object_qid *qid, *pid;
10020 struct got_update_progress_arg upa;
10021 int *pack_fds = NULL;
10023 STAILQ_INIT(&commits);
10024 TAILQ_INIT(&merged_paths);
10025 memset(&upa, 0, sizeof(upa));
10027 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10033 continue_rebase = 1;
10039 delete_backups = 1;
10051 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10052 "unveil", NULL) == -1)
10055 if (list_backups) {
10057 option_conflict('l', 'a');
10058 if (continue_rebase)
10059 option_conflict('l', 'c');
10060 if (delete_backups)
10061 option_conflict('l', 'X');
10062 if (argc != 0 && argc != 1)
10064 } else if (delete_backups) {
10066 option_conflict('X', 'a');
10067 if (continue_rebase)
10068 option_conflict('X', 'c');
10070 option_conflict('l', 'X');
10071 if (argc != 0 && argc != 1)
10074 if (abort_rebase && continue_rebase)
10076 else if (abort_rebase || continue_rebase) {
10079 } else if (argc != 1)
10083 cwd = getcwd(NULL, 0);
10085 error = got_error_from_errno("getcwd");
10089 error = got_repo_pack_fds_open(&pack_fds);
10093 error = got_worktree_open(&worktree, cwd);
10095 if (list_backups || delete_backups) {
10096 if (error->code != GOT_ERR_NOT_WORKTREE)
10099 if (error->code == GOT_ERR_NOT_WORKTREE)
10100 error = wrap_not_worktree_error(error,
10106 error = got_repo_open(&repo,
10107 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
10112 error = apply_unveil(got_repo_get_path(repo), 0,
10113 worktree ? got_worktree_get_root_path(worktree) : NULL);
10117 if (list_backups || delete_backups) {
10118 error = process_backup_refs(
10119 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
10120 argc == 1 ? argv[0] : NULL, delete_backups, repo);
10121 goto done; /* nothing else to do */
10124 error = got_worktree_histedit_in_progress(&histedit_in_progress,
10128 if (histedit_in_progress) {
10129 error = got_error(GOT_ERR_HISTEDIT_BUSY);
10133 error = got_worktree_merge_in_progress(&merge_in_progress,
10137 if (merge_in_progress) {
10138 error = got_error(GOT_ERR_MERGE_BUSY);
10142 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10146 if (abort_rebase) {
10147 if (!rebase_in_progress) {
10148 error = got_error(GOT_ERR_NOT_REBASING);
10151 error = got_worktree_rebase_continue(&resume_commit_id,
10152 &new_base_branch, &tmp_branch, &branch, &fileindex,
10156 printf("Switching work tree to %s\n",
10157 got_ref_get_symref_target(new_base_branch));
10158 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10159 new_base_branch, abort_progress, &upa);
10162 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10163 print_merge_progress_stats(&upa);
10164 goto done; /* nothing else to do */
10167 if (continue_rebase) {
10168 if (!rebase_in_progress) {
10169 error = got_error(GOT_ERR_NOT_REBASING);
10172 error = got_worktree_rebase_continue(&resume_commit_id,
10173 &new_base_branch, &tmp_branch, &branch, &fileindex,
10178 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10179 resume_commit_id, repo);
10183 yca_id = got_object_id_dup(resume_commit_id);
10184 if (yca_id == NULL) {
10185 error = got_error_from_errno("got_object_id_dup");
10189 error = got_ref_open(&branch, repo, argv[0], 0);
10194 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10198 if (!continue_rebase) {
10199 struct got_object_id *base_commit_id;
10201 base_commit_id = got_worktree_get_base_commit_id(worktree);
10202 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10203 base_commit_id, branch_head_commit_id, 1, repo,
10204 check_cancelled, NULL);
10207 if (yca_id == NULL) {
10208 error = got_error_msg(GOT_ERR_ANCESTRY,
10209 "specified branch shares no common ancestry "
10210 "with work tree's branch");
10214 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10216 if (error->code != GOT_ERR_ANCESTRY)
10220 struct got_pathlist_head paths;
10221 printf("%s is already based on %s\n",
10222 got_ref_get_name(branch),
10223 got_worktree_get_head_ref_name(worktree));
10224 error = switch_head_ref(branch, branch_head_commit_id,
10228 error = got_worktree_set_base_commit_id(worktree, repo,
10229 branch_head_commit_id);
10232 TAILQ_INIT(&paths);
10233 error = got_pathlist_append(&paths, "", NULL);
10236 error = got_worktree_checkout_files(worktree,
10237 &paths, repo, update_progress, &upa,
10238 check_cancelled, NULL);
10239 got_pathlist_free(&paths);
10242 if (upa.did_something) {
10244 error = got_object_id_str(&id_str,
10245 branch_head_commit_id);
10248 printf("Updated to %s: %s\n",
10249 got_worktree_get_head_ref_name(worktree),
10253 printf("Already up-to-date\n");
10254 print_update_progress_stats(&upa);
10259 commit_id = branch_head_commit_id;
10260 error = got_object_open_as_commit(&commit, repo, commit_id);
10264 parent_ids = got_object_commit_get_parent_ids(commit);
10265 pid = STAILQ_FIRST(parent_ids);
10267 error = got_error(GOT_ERR_EMPTY_REBASE);
10270 error = collect_commits(&commits, commit_id, &pid->id,
10271 yca_id, got_worktree_get_path_prefix(worktree),
10272 GOT_ERR_REBASE_PATH, repo);
10273 got_object_commit_close(commit);
10278 if (!continue_rebase) {
10279 error = got_worktree_rebase_prepare(&new_base_branch,
10280 &tmp_branch, &fileindex, worktree, branch, repo);
10285 if (STAILQ_EMPTY(&commits)) {
10286 if (continue_rebase) {
10287 error = rebase_complete(worktree, fileindex,
10288 branch, new_base_branch, tmp_branch, repo,
10292 /* Fast-forward the reference of the branch. */
10293 struct got_object_id *new_head_commit_id;
10295 error = got_ref_resolve(&new_head_commit_id, repo,
10299 error = got_object_id_str(&id_str, new_head_commit_id);
10300 printf("Forwarding %s to commit %s\n",
10301 got_ref_get_name(branch), id_str);
10303 error = got_ref_change_ref(branch,
10304 new_head_commit_id);
10307 /* No backup needed since objects did not change. */
10313 STAILQ_FOREACH(qid, &commits, entry) {
10315 commit_id = &qid->id;
10316 parent_id = pid ? &pid->id : yca_id;
10319 memset(&upa, 0, sizeof(upa));
10320 error = got_worktree_rebase_merge_files(&merged_paths,
10321 worktree, fileindex, parent_id, commit_id, repo,
10322 update_progress, &upa, check_cancelled, NULL);
10326 print_merge_progress_stats(&upa);
10327 if (upa.conflicts > 0 || upa.missing > 0 ||
10328 upa.not_deleted > 0 || upa.unversioned > 0) {
10329 if (upa.conflicts > 0) {
10330 error = show_rebase_merge_conflict(&qid->id,
10335 got_worktree_rebase_pathlist_free(&merged_paths);
10339 error = rebase_commit(&merged_paths, worktree, fileindex,
10340 tmp_branch, commit_id, repo);
10341 got_worktree_rebase_pathlist_free(&merged_paths);
10346 if (upa.conflicts > 0 || upa.missing > 0 ||
10347 upa.not_deleted > 0 || upa.unversioned > 0) {
10348 error = got_worktree_rebase_postpone(worktree, fileindex);
10351 if (upa.conflicts > 0 && upa.missing == 0 &&
10352 upa.not_deleted == 0 && upa.unversioned == 0) {
10353 error = got_error_msg(GOT_ERR_CONFLICTS,
10354 "conflicts must be resolved before rebasing "
10356 } else if (upa.conflicts > 0) {
10357 error = got_error_msg(GOT_ERR_CONFLICTS,
10358 "conflicts must be resolved before rebasing "
10359 "can continue; changes destined for some "
10360 "files were not yet merged and should be "
10361 "merged manually if required before the "
10362 "rebase operation is continued");
10364 error = got_error_msg(GOT_ERR_CONFLICTS,
10365 "changes destined for some files were not "
10366 "yet merged and should be merged manually "
10367 "if required before the rebase operation "
10371 error = rebase_complete(worktree, fileindex, branch,
10372 new_base_branch, tmp_branch, repo, create_backup);
10374 got_object_id_queue_free(&commits);
10375 free(branch_head_commit_id);
10376 free(resume_commit_id);
10379 got_object_commit_close(commit);
10381 got_ref_close(branch);
10382 if (new_base_branch)
10383 got_ref_close(new_base_branch);
10385 got_ref_close(tmp_branch);
10387 got_worktree_close(worktree);
10389 const struct got_error *close_err = got_repo_close(repo);
10394 const struct got_error *pack_err =
10395 got_repo_pack_fds_close(pack_fds);
10403 usage_histedit(void)
10405 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10406 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10411 #define GOT_HISTEDIT_PICK 'p'
10412 #define GOT_HISTEDIT_EDIT 'e'
10413 #define GOT_HISTEDIT_FOLD 'f'
10414 #define GOT_HISTEDIT_DROP 'd'
10415 #define GOT_HISTEDIT_MESG 'm'
10417 static const struct got_histedit_cmd {
10418 unsigned char code;
10421 } got_histedit_cmds[] = {
10422 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10423 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10424 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10426 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10427 { GOT_HISTEDIT_MESG, "mesg",
10428 "single-line log message for commit above (open editor if empty)" },
10431 struct got_histedit_list_entry {
10432 TAILQ_ENTRY(got_histedit_list_entry) entry;
10433 struct got_object_id *commit_id;
10434 const struct got_histedit_cmd *cmd;
10437 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10439 static const struct got_error *
10440 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10441 FILE *f, struct got_repository *repo)
10443 const struct got_error *err = NULL;
10444 char *logmsg = NULL, *id_str = NULL;
10445 struct got_commit_object *commit = NULL;
10448 err = got_object_open_as_commit(&commit, repo, commit_id);
10452 err = get_short_logmsg(&logmsg, 34, commit);
10456 err = got_object_id_str(&id_str, commit_id);
10460 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10462 err = got_ferror(f, GOT_ERR_IO);
10465 got_object_commit_close(commit);
10471 static const struct got_error *
10472 histedit_write_commit_list(struct got_object_id_queue *commits,
10473 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10474 struct got_repository *repo)
10476 const struct got_error *err = NULL;
10477 struct got_object_qid *qid;
10478 const char *histedit_cmd = NULL;
10480 if (STAILQ_EMPTY(commits))
10481 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10483 STAILQ_FOREACH(qid, commits, entry) {
10484 histedit_cmd = got_histedit_cmds[0].name;
10486 histedit_cmd = "edit";
10487 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10488 histedit_cmd = "fold";
10489 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10492 if (edit_logmsg_only) {
10493 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10495 err = got_ferror(f, GOT_ERR_IO);
10504 static const struct got_error *
10505 write_cmd_list(FILE *f, const char *branch_name,
10506 struct got_object_id_queue *commits)
10508 const struct got_error *err = NULL;
10512 struct got_object_qid *qid;
10514 qid = STAILQ_FIRST(commits);
10515 err = got_object_id_str(&id_str, &qid->id);
10520 "# Editing the history of branch '%s' starting at\n"
10522 "# Commits will be processed in order from top to "
10523 "bottom of this file.\n", branch_name, id_str);
10525 err = got_ferror(f, GOT_ERR_IO);
10529 n = fprintf(f, "# Available histedit commands:\n");
10531 err = got_ferror(f, GOT_ERR_IO);
10535 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10536 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10537 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10540 err = got_ferror(f, GOT_ERR_IO);
10549 static const struct got_error *
10550 histedit_syntax_error(int lineno)
10552 static char msg[42];
10555 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10557 if (ret == -1 || ret >= sizeof(msg))
10558 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10560 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10563 static const struct got_error *
10564 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10565 char *logmsg, struct got_repository *repo)
10567 const struct got_error *err;
10568 struct got_commit_object *folded_commit = NULL;
10569 char *id_str, *folded_logmsg = NULL;
10571 err = got_object_id_str(&id_str, hle->commit_id);
10575 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10579 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10582 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10583 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10584 folded_logmsg) == -1) {
10585 err = got_error_from_errno("asprintf");
10589 got_object_commit_close(folded_commit);
10591 free(folded_logmsg);
10595 static struct got_histedit_list_entry *
10596 get_folded_commits(struct got_histedit_list_entry *hle)
10598 struct got_histedit_list_entry *prev, *folded = NULL;
10600 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10601 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10602 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10603 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10605 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10611 static const struct got_error *
10612 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10613 struct got_repository *repo)
10615 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10616 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10617 const struct got_error *err = NULL;
10618 struct got_commit_object *commit = NULL;
10621 struct got_histedit_list_entry *folded = NULL;
10623 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10627 folded = get_folded_commits(hle);
10629 while (folded != hle) {
10630 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10631 folded = TAILQ_NEXT(folded, entry);
10634 err = append_folded_commit_msg(&new_msg, folded,
10640 folded = TAILQ_NEXT(folded, entry);
10644 err = got_object_id_str(&id_str, hle->commit_id);
10647 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10650 logmsg_len = asprintf(&new_msg,
10651 "%s\n# original log message of commit %s: %s",
10652 logmsg ? logmsg : "", id_str, orig_logmsg);
10653 if (logmsg_len == -1) {
10654 err = got_error_from_errno("asprintf");
10660 err = got_object_id_str(&id_str, hle->commit_id);
10664 err = got_opentemp_named_fd(&logmsg_path, &fd,
10665 GOT_TMPDIR_STR "/got-logmsg");
10669 write(fd, logmsg, logmsg_len);
10672 err = get_editor(&editor);
10676 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10679 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10682 hle->logmsg = strdup(new_msg);
10683 if (hle->logmsg == NULL)
10684 err = got_error_from_errno("strdup");
10687 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10688 err = got_error_from_errno2("unlink", logmsg_path);
10694 got_object_commit_close(commit);
10698 static const struct got_error *
10699 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10700 FILE *f, struct got_repository *repo)
10702 const struct got_error *err = NULL;
10703 char *line = NULL, *p, *end;
10706 int lineno = 0, lastcmd = -1;
10707 const struct got_histedit_cmd *cmd;
10708 struct got_object_id *commit_id = NULL;
10709 struct got_histedit_list_entry *hle = NULL;
10712 len = getline(&line, &size, f);
10714 const struct got_error *getline_err;
10717 getline_err = got_error_from_errno("getline");
10718 err = got_ferror(f, getline_err->code);
10723 while (isspace((unsigned char)p[0]))
10725 if (p[0] == '#' || p[0] == '\0') {
10731 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10732 cmd = &got_histedit_cmds[i];
10733 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10734 isspace((unsigned char)p[strlen(cmd->name)])) {
10735 p += strlen(cmd->name);
10738 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10743 if (i == nitems(got_histedit_cmds)) {
10744 err = histedit_syntax_error(lineno);
10747 while (isspace((unsigned char)p[0]))
10749 if (cmd->code == GOT_HISTEDIT_MESG) {
10750 if (lastcmd != GOT_HISTEDIT_PICK &&
10751 lastcmd != GOT_HISTEDIT_EDIT) {
10752 err = got_error(GOT_ERR_HISTEDIT_CMD);
10755 if (p[0] == '\0') {
10756 err = histedit_edit_logmsg(hle, repo);
10760 hle->logmsg = strdup(p);
10761 if (hle->logmsg == NULL) {
10762 err = got_error_from_errno("strdup");
10768 lastcmd = cmd->code;
10772 while (end[0] && !isspace((unsigned char)end[0]))
10776 err = got_object_resolve_id_str(&commit_id, repo, p);
10778 /* override error code */
10779 err = histedit_syntax_error(lineno);
10783 hle = malloc(sizeof(*hle));
10785 err = got_error_from_errno("malloc");
10789 hle->commit_id = commit_id;
10790 hle->logmsg = NULL;
10794 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10795 lastcmd = cmd->code;
10803 static const struct got_error *
10804 histedit_check_script(struct got_histedit_list *histedit_cmds,
10805 struct got_object_id_queue *commits, struct got_repository *repo)
10807 const struct got_error *err = NULL;
10808 struct got_object_qid *qid;
10809 struct got_histedit_list_entry *hle;
10810 static char msg[92];
10813 if (TAILQ_EMPTY(histedit_cmds))
10814 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10815 "histedit script contains no commands");
10816 if (STAILQ_EMPTY(commits))
10817 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10819 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10820 struct got_histedit_list_entry *hle2;
10821 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10824 if (got_object_id_cmp(hle->commit_id,
10825 hle2->commit_id) != 0)
10827 err = got_object_id_str(&id_str, hle->commit_id);
10830 snprintf(msg, sizeof(msg), "commit %s is listed "
10831 "more than once in histedit script", id_str);
10833 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10837 STAILQ_FOREACH(qid, commits, entry) {
10838 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10839 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10843 err = got_object_id_str(&id_str, &qid->id);
10846 snprintf(msg, sizeof(msg),
10847 "commit %s missing from histedit script", id_str);
10849 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10853 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10854 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10855 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10856 "last commit in histedit script cannot be folded");
10861 static const struct got_error *
10862 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10863 const char *path, struct got_object_id_queue *commits,
10864 struct got_repository *repo)
10866 const struct got_error *err = NULL;
10870 err = get_editor(&editor);
10874 if (spawn_editor(editor, path) == -1) {
10875 err = got_error_from_errno("failed spawning editor");
10879 f = fopen(path, "re");
10881 err = got_error_from_errno("fopen");
10884 err = histedit_parse_list(histedit_cmds, f, repo);
10888 err = histedit_check_script(histedit_cmds, commits, repo);
10890 if (f && fclose(f) == EOF && err == NULL)
10891 err = got_error_from_errno("fclose");
10896 static const struct got_error *
10897 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10898 struct got_object_id_queue *, const char *, const char *,
10899 struct got_repository *);
10901 static const struct got_error *
10902 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10903 struct got_object_id_queue *commits, const char *branch_name,
10904 int edit_logmsg_only, int fold_only, int edit_only,
10905 struct got_repository *repo)
10907 const struct got_error *err;
10911 err = got_opentemp_named(&path, &f, "got-histedit");
10915 err = write_cmd_list(f, branch_name, commits);
10919 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10920 fold_only, edit_only, repo);
10924 if (edit_logmsg_only || fold_only || edit_only) {
10926 err = histedit_parse_list(histedit_cmds, f, repo);
10928 if (fclose(f) == EOF) {
10929 err = got_error_from_errno("fclose");
10933 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10935 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10936 err->code != GOT_ERR_HISTEDIT_CMD)
10938 err = histedit_edit_list_retry(histedit_cmds, err,
10939 commits, path, branch_name, repo);
10943 if (f && fclose(f) == EOF && err == NULL)
10944 err = got_error_from_errno("fclose");
10945 if (path && unlink(path) != 0 && err == NULL)
10946 err = got_error_from_errno2("unlink", path);
10951 static const struct got_error *
10952 histedit_save_list(struct got_histedit_list *histedit_cmds,
10953 struct got_worktree *worktree, struct got_repository *repo)
10955 const struct got_error *err = NULL;
10958 struct got_histedit_list_entry *hle;
10959 struct got_commit_object *commit = NULL;
10961 err = got_worktree_get_histedit_script_path(&path, worktree);
10965 f = fopen(path, "we");
10967 err = got_error_from_errno2("fopen", path);
10970 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10971 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10977 int n = fprintf(f, "%c %s\n",
10978 GOT_HISTEDIT_MESG, hle->logmsg);
10980 err = got_ferror(f, GOT_ERR_IO);
10986 if (f && fclose(f) == EOF && err == NULL)
10987 err = got_error_from_errno("fclose");
10990 got_object_commit_close(commit);
10995 histedit_free_list(struct got_histedit_list *histedit_cmds)
10997 struct got_histedit_list_entry *hle;
10999 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11000 TAILQ_REMOVE(histedit_cmds, hle, entry);
11005 static const struct got_error *
11006 histedit_load_list(struct got_histedit_list *histedit_cmds,
11007 const char *path, struct got_repository *repo)
11009 const struct got_error *err = NULL;
11012 f = fopen(path, "re");
11014 err = got_error_from_errno2("fopen", path);
11018 err = histedit_parse_list(histedit_cmds, f, repo);
11020 if (f && fclose(f) == EOF && err == NULL)
11021 err = got_error_from_errno("fclose");
11025 static const struct got_error *
11026 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11027 const struct got_error *edit_err, struct got_object_id_queue *commits,
11028 const char *path, const char *branch_name, struct got_repository *repo)
11030 const struct got_error *err = NULL, *prev_err = edit_err;
11033 while (resp != 'c' && resp != 'r' && resp != 'a') {
11034 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
11035 "or (a)bort: ", getprogname(), prev_err->msg);
11040 histedit_free_list(histedit_cmds);
11041 err = histedit_run_editor(histedit_cmds, path, commits,
11044 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11045 err->code != GOT_ERR_HISTEDIT_CMD)
11052 } else if (resp == 'r') {
11053 histedit_free_list(histedit_cmds);
11054 err = histedit_edit_script(histedit_cmds,
11055 commits, branch_name, 0, 0, 0, repo);
11057 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11058 err->code != GOT_ERR_HISTEDIT_CMD)
11065 } else if (resp == 'a') {
11066 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
11069 printf("invalid response '%c'\n", resp);
11075 static const struct got_error *
11076 histedit_complete(struct got_worktree *worktree,
11077 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
11078 struct got_reference *branch, struct got_repository *repo)
11080 printf("Switching work tree to %s\n",
11081 got_ref_get_symref_target(branch));
11082 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
11086 static const struct got_error *
11087 show_histedit_progress(struct got_commit_object *commit,
11088 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
11090 const struct got_error *err;
11091 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
11093 err = got_object_id_str(&old_id_str, hle->commit_id);
11098 err = got_object_id_str(&new_id_str, new_id);
11103 old_id_str[12] = '\0';
11105 new_id_str[12] = '\0';
11108 logmsg = strdup(hle->logmsg);
11109 if (logmsg == NULL) {
11110 err = got_error_from_errno("strdup");
11113 trim_logmsg(logmsg, 42);
11115 err = get_short_logmsg(&logmsg, 42, commit);
11120 switch (hle->cmd->code) {
11121 case GOT_HISTEDIT_PICK:
11122 case GOT_HISTEDIT_EDIT:
11123 printf("%s -> %s: %s\n", old_id_str,
11124 new_id_str ? new_id_str : "no-op change", logmsg);
11126 case GOT_HISTEDIT_DROP:
11127 case GOT_HISTEDIT_FOLD:
11128 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
11140 static const struct got_error *
11141 histedit_commit(struct got_pathlist_head *merged_paths,
11142 struct got_worktree *worktree, struct got_fileindex *fileindex,
11143 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
11144 struct got_repository *repo)
11146 const struct got_error *err;
11147 struct got_commit_object *commit;
11148 struct got_object_id *new_commit_id;
11150 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
11151 && hle->logmsg == NULL) {
11152 err = histedit_edit_logmsg(hle, repo);
11157 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11161 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11162 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11163 hle->logmsg, repo);
11165 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11167 err = show_histedit_progress(commit, hle, NULL);
11169 err = show_histedit_progress(commit, hle, new_commit_id);
11170 free(new_commit_id);
11173 got_object_commit_close(commit);
11177 static const struct got_error *
11178 histedit_skip_commit(struct got_histedit_list_entry *hle,
11179 struct got_worktree *worktree, struct got_repository *repo)
11181 const struct got_error *error;
11182 struct got_commit_object *commit;
11184 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11189 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11193 error = show_histedit_progress(commit, hle, NULL);
11194 got_object_commit_close(commit);
11198 static const struct got_error *
11199 check_local_changes(void *arg, unsigned char status,
11200 unsigned char staged_status, const char *path,
11201 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11202 struct got_object_id *commit_id, int dirfd, const char *de_name)
11204 int *have_local_changes = arg;
11207 case GOT_STATUS_ADD:
11208 case GOT_STATUS_DELETE:
11209 case GOT_STATUS_MODIFY:
11210 case GOT_STATUS_CONFLICT:
11211 *have_local_changes = 1;
11212 return got_error(GOT_ERR_CANCELLED);
11217 switch (staged_status) {
11218 case GOT_STATUS_ADD:
11219 case GOT_STATUS_DELETE:
11220 case GOT_STATUS_MODIFY:
11221 *have_local_changes = 1;
11222 return got_error(GOT_ERR_CANCELLED);
11230 static const struct got_error *
11231 cmd_histedit(int argc, char *argv[])
11233 const struct got_error *error = NULL;
11234 struct got_worktree *worktree = NULL;
11235 struct got_fileindex *fileindex = NULL;
11236 struct got_repository *repo = NULL;
11238 struct got_reference *branch = NULL;
11239 struct got_reference *tmp_branch = NULL;
11240 struct got_object_id *resume_commit_id = NULL;
11241 struct got_object_id *base_commit_id = NULL;
11242 struct got_object_id *head_commit_id = NULL;
11243 struct got_commit_object *commit = NULL;
11244 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11245 struct got_update_progress_arg upa;
11246 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11247 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11248 int list_backups = 0, delete_backups = 0;
11249 const char *edit_script_path = NULL;
11250 struct got_object_id_queue commits;
11251 struct got_pathlist_head merged_paths;
11252 const struct got_object_id_queue *parent_ids;
11253 struct got_object_qid *pid;
11254 struct got_histedit_list histedit_cmds;
11255 struct got_histedit_list_entry *hle;
11256 int *pack_fds = NULL;
11258 STAILQ_INIT(&commits);
11259 TAILQ_INIT(&histedit_cmds);
11260 TAILQ_INIT(&merged_paths);
11261 memset(&upa, 0, sizeof(upa));
11263 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11278 edit_script_path = optarg;
11281 edit_logmsg_only = 1;
11287 delete_backups = 1;
11299 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11300 "unveil", NULL) == -1)
11303 if (abort_edit && continue_edit)
11304 option_conflict('a', 'c');
11305 if (edit_script_path && edit_logmsg_only)
11306 option_conflict('F', 'm');
11307 if (abort_edit && edit_logmsg_only)
11308 option_conflict('a', 'm');
11309 if (continue_edit && edit_logmsg_only)
11310 option_conflict('c', 'm');
11311 if (abort_edit && fold_only)
11312 option_conflict('a', 'f');
11313 if (continue_edit && fold_only)
11314 option_conflict('c', 'f');
11315 if (fold_only && edit_logmsg_only)
11316 option_conflict('f', 'm');
11317 if (edit_script_path && fold_only)
11318 option_conflict('F', 'f');
11319 if (abort_edit && edit_only)
11320 option_conflict('a', 'e');
11321 if (continue_edit && edit_only)
11322 option_conflict('c', 'e');
11323 if (edit_only && edit_logmsg_only)
11324 option_conflict('e', 'm');
11325 if (edit_script_path && edit_only)
11326 option_conflict('F', 'e');
11327 if (list_backups) {
11329 option_conflict('l', 'a');
11331 option_conflict('l', 'c');
11332 if (edit_script_path)
11333 option_conflict('l', 'F');
11334 if (edit_logmsg_only)
11335 option_conflict('l', 'm');
11337 option_conflict('l', 'f');
11339 option_conflict('l', 'e');
11340 if (delete_backups)
11341 option_conflict('l', 'X');
11342 if (argc != 0 && argc != 1)
11344 } else if (delete_backups) {
11346 option_conflict('X', 'a');
11348 option_conflict('X', 'c');
11349 if (edit_script_path)
11350 option_conflict('X', 'F');
11351 if (edit_logmsg_only)
11352 option_conflict('X', 'm');
11354 option_conflict('X', 'f');
11356 option_conflict('X', 'e');
11358 option_conflict('X', 'l');
11359 if (argc != 0 && argc != 1)
11361 } else if (argc != 0)
11365 * This command cannot apply unveil(2) in all cases because the
11366 * user may choose to run an editor to edit the histedit script
11367 * and to edit individual commit log messages.
11368 * unveil(2) traverses exec(2); if an editor is used we have to
11369 * apply unveil after edit script and log messages have been written.
11370 * XXX TODO: Make use of unveil(2) where possible.
11373 cwd = getcwd(NULL, 0);
11375 error = got_error_from_errno("getcwd");
11379 error = got_repo_pack_fds_open(&pack_fds);
11383 error = got_worktree_open(&worktree, cwd);
11385 if (list_backups || delete_backups) {
11386 if (error->code != GOT_ERR_NOT_WORKTREE)
11389 if (error->code == GOT_ERR_NOT_WORKTREE)
11390 error = wrap_not_worktree_error(error,
11396 if (list_backups || delete_backups) {
11397 error = got_repo_open(&repo,
11398 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11402 error = apply_unveil(got_repo_get_path(repo), 0,
11403 worktree ? got_worktree_get_root_path(worktree) : NULL);
11406 error = process_backup_refs(
11407 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11408 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11409 goto done; /* nothing else to do */
11412 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11417 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11420 if (rebase_in_progress) {
11421 error = got_error(GOT_ERR_REBASING);
11425 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11429 if (merge_in_progress) {
11430 error = got_error(GOT_ERR_MERGE_BUSY);
11434 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11438 if (edit_in_progress && edit_logmsg_only) {
11439 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11440 "histedit operation is in progress in this "
11441 "work tree and must be continued or aborted "
11442 "before the -m option can be used");
11445 if (edit_in_progress && fold_only) {
11446 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11447 "histedit operation is in progress in this "
11448 "work tree and must be continued or aborted "
11449 "before the -f option can be used");
11452 if (edit_in_progress && edit_only) {
11453 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11454 "histedit operation is in progress in this "
11455 "work tree and must be continued or aborted "
11456 "before the -e option can be used");
11460 if (edit_in_progress && abort_edit) {
11461 error = got_worktree_histedit_continue(&resume_commit_id,
11462 &tmp_branch, &branch, &base_commit_id, &fileindex,
11466 printf("Switching work tree to %s\n",
11467 got_ref_get_symref_target(branch));
11468 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11469 branch, base_commit_id, abort_progress, &upa);
11472 printf("Histedit of %s aborted\n",
11473 got_ref_get_symref_target(branch));
11474 print_merge_progress_stats(&upa);
11475 goto done; /* nothing else to do */
11476 } else if (abort_edit) {
11477 error = got_error(GOT_ERR_NOT_HISTEDIT);
11481 if (continue_edit) {
11484 if (!edit_in_progress) {
11485 error = got_error(GOT_ERR_NOT_HISTEDIT);
11489 error = got_worktree_get_histedit_script_path(&path, worktree);
11493 error = histedit_load_list(&histedit_cmds, path, repo);
11498 error = got_worktree_histedit_continue(&resume_commit_id,
11499 &tmp_branch, &branch, &base_commit_id, &fileindex,
11504 error = got_ref_resolve(&head_commit_id, repo, branch);
11508 error = got_object_open_as_commit(&commit, repo,
11512 parent_ids = got_object_commit_get_parent_ids(commit);
11513 pid = STAILQ_FIRST(parent_ids);
11515 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11518 error = collect_commits(&commits, head_commit_id, &pid->id,
11519 base_commit_id, got_worktree_get_path_prefix(worktree),
11520 GOT_ERR_HISTEDIT_PATH, repo);
11521 got_object_commit_close(commit);
11526 if (edit_in_progress) {
11527 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11531 error = got_ref_open(&branch, repo,
11532 got_worktree_get_head_ref_name(worktree), 0);
11536 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11537 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11538 "will not edit commit history of a branch outside "
11539 "the \"refs/heads/\" reference namespace");
11543 error = got_ref_resolve(&head_commit_id, repo, branch);
11544 got_ref_close(branch);
11549 error = got_object_open_as_commit(&commit, repo,
11553 parent_ids = got_object_commit_get_parent_ids(commit);
11554 pid = STAILQ_FIRST(parent_ids);
11556 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11559 error = collect_commits(&commits, head_commit_id, &pid->id,
11560 got_worktree_get_base_commit_id(worktree),
11561 got_worktree_get_path_prefix(worktree),
11562 GOT_ERR_HISTEDIT_PATH, repo);
11563 got_object_commit_close(commit);
11568 if (STAILQ_EMPTY(&commits)) {
11569 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11573 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11574 &base_commit_id, &fileindex, worktree, repo);
11578 if (edit_script_path) {
11579 error = histedit_load_list(&histedit_cmds,
11580 edit_script_path, repo);
11582 got_worktree_histedit_abort(worktree, fileindex,
11583 repo, branch, base_commit_id,
11584 abort_progress, &upa);
11585 print_merge_progress_stats(&upa);
11589 const char *branch_name;
11590 branch_name = got_ref_get_symref_target(branch);
11591 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11593 error = histedit_edit_script(&histedit_cmds, &commits,
11594 branch_name, edit_logmsg_only, fold_only,
11597 got_worktree_histedit_abort(worktree, fileindex,
11598 repo, branch, base_commit_id,
11599 abort_progress, &upa);
11600 print_merge_progress_stats(&upa);
11606 error = histedit_save_list(&histedit_cmds, worktree,
11609 got_worktree_histedit_abort(worktree, fileindex,
11610 repo, branch, base_commit_id,
11611 abort_progress, &upa);
11612 print_merge_progress_stats(&upa);
11618 error = histedit_check_script(&histedit_cmds, &commits, repo);
11622 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11623 if (resume_commit_id) {
11624 if (got_object_id_cmp(hle->commit_id,
11625 resume_commit_id) != 0)
11628 resume_commit_id = NULL;
11629 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11630 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11631 error = histedit_skip_commit(hle, worktree,
11636 struct got_pathlist_head paths;
11637 int have_changes = 0;
11639 TAILQ_INIT(&paths);
11640 error = got_pathlist_append(&paths, "", NULL);
11643 error = got_worktree_status(worktree, &paths,
11644 repo, 0, check_local_changes, &have_changes,
11645 check_cancelled, NULL);
11646 got_pathlist_free(&paths);
11648 if (error->code != GOT_ERR_CANCELLED)
11650 if (sigint_received || sigpipe_received)
11653 if (have_changes) {
11654 error = histedit_commit(NULL, worktree,
11655 fileindex, tmp_branch, hle, repo);
11659 error = got_object_open_as_commit(
11660 &commit, repo, hle->commit_id);
11663 error = show_histedit_progress(commit,
11665 got_object_commit_close(commit);
11674 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11675 error = histedit_skip_commit(hle, worktree, repo);
11681 error = got_object_open_as_commit(&commit, repo,
11685 parent_ids = got_object_commit_get_parent_ids(commit);
11686 pid = STAILQ_FIRST(parent_ids);
11688 error = got_worktree_histedit_merge_files(&merged_paths,
11689 worktree, fileindex, &pid->id, hle->commit_id, repo,
11690 update_progress, &upa, check_cancelled, NULL);
11693 got_object_commit_close(commit);
11696 print_merge_progress_stats(&upa);
11697 if (upa.conflicts > 0 || upa.missing > 0 ||
11698 upa.not_deleted > 0 || upa.unversioned > 0) {
11699 if (upa.conflicts > 0) {
11700 error = show_rebase_merge_conflict(
11701 hle->commit_id, repo);
11705 got_worktree_rebase_pathlist_free(&merged_paths);
11709 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11711 error = got_object_id_str(&id_str, hle->commit_id);
11714 printf("Stopping histedit for amending commit %s\n",
11717 got_worktree_rebase_pathlist_free(&merged_paths);
11718 error = got_worktree_histedit_postpone(worktree,
11723 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11724 error = histedit_skip_commit(hle, worktree, repo);
11730 error = histedit_commit(&merged_paths, worktree, fileindex,
11731 tmp_branch, hle, repo);
11732 got_worktree_rebase_pathlist_free(&merged_paths);
11737 if (upa.conflicts > 0 || upa.missing > 0 ||
11738 upa.not_deleted > 0 || upa.unversioned > 0) {
11739 error = got_worktree_histedit_postpone(worktree, fileindex);
11742 if (upa.conflicts > 0 && upa.missing == 0 &&
11743 upa.not_deleted == 0 && upa.unversioned == 0) {
11744 error = got_error_msg(GOT_ERR_CONFLICTS,
11745 "conflicts must be resolved before histedit "
11747 } else if (upa.conflicts > 0) {
11748 error = got_error_msg(GOT_ERR_CONFLICTS,
11749 "conflicts must be resolved before histedit "
11750 "can continue; changes destined for some "
11751 "files were not yet merged and should be "
11752 "merged manually if required before the "
11753 "histedit operation is continued");
11755 error = got_error_msg(GOT_ERR_CONFLICTS,
11756 "changes destined for some files were not "
11757 "yet merged and should be merged manually "
11758 "if required before the histedit operation "
11762 error = histedit_complete(worktree, fileindex, tmp_branch,
11765 got_object_id_queue_free(&commits);
11766 histedit_free_list(&histedit_cmds);
11767 free(head_commit_id);
11768 free(base_commit_id);
11769 free(resume_commit_id);
11771 got_object_commit_close(commit);
11773 got_ref_close(branch);
11775 got_ref_close(tmp_branch);
11777 got_worktree_close(worktree);
11779 const struct got_error *close_err = got_repo_close(repo);
11784 const struct got_error *pack_err =
11785 got_repo_pack_fds_close(pack_fds);
11793 usage_integrate(void)
11795 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11799 static const struct got_error *
11800 cmd_integrate(int argc, char *argv[])
11802 const struct got_error *error = NULL;
11803 struct got_repository *repo = NULL;
11804 struct got_worktree *worktree = NULL;
11805 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11806 const char *branch_arg = NULL;
11807 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11808 struct got_fileindex *fileindex = NULL;
11809 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11811 struct got_update_progress_arg upa;
11812 int *pack_fds = NULL;
11814 while ((ch = getopt(argc, argv, "")) != -1) {
11827 branch_arg = argv[0];
11829 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11830 "unveil", NULL) == -1)
11833 cwd = getcwd(NULL, 0);
11835 error = got_error_from_errno("getcwd");
11839 error = got_repo_pack_fds_open(&pack_fds);
11843 error = got_worktree_open(&worktree, cwd);
11845 if (error->code == GOT_ERR_NOT_WORKTREE)
11846 error = wrap_not_worktree_error(error, "integrate",
11851 error = check_rebase_or_histedit_in_progress(worktree);
11855 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11860 error = apply_unveil(got_repo_get_path(repo), 0,
11861 got_worktree_get_root_path(worktree));
11865 error = check_merge_in_progress(worktree, repo);
11869 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11870 error = got_error_from_errno("asprintf");
11874 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11875 &base_branch_ref, worktree, refname, repo);
11879 refname = strdup(got_ref_get_name(branch_ref));
11880 if (refname == NULL) {
11881 error = got_error_from_errno("strdup");
11882 got_worktree_integrate_abort(worktree, fileindex, repo,
11883 branch_ref, base_branch_ref);
11886 base_refname = strdup(got_ref_get_name(base_branch_ref));
11887 if (base_refname == NULL) {
11888 error = got_error_from_errno("strdup");
11889 got_worktree_integrate_abort(worktree, fileindex, repo,
11890 branch_ref, base_branch_ref);
11894 error = got_ref_resolve(&commit_id, repo, branch_ref);
11898 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11902 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11903 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11904 "specified branch has already been integrated");
11905 got_worktree_integrate_abort(worktree, fileindex, repo,
11906 branch_ref, base_branch_ref);
11910 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11912 if (error->code == GOT_ERR_ANCESTRY)
11913 error = got_error(GOT_ERR_REBASE_REQUIRED);
11914 got_worktree_integrate_abort(worktree, fileindex, repo,
11915 branch_ref, base_branch_ref);
11919 memset(&upa, 0, sizeof(upa));
11920 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11921 branch_ref, base_branch_ref, update_progress, &upa,
11922 check_cancelled, NULL);
11926 printf("Integrated %s into %s\n", refname, base_refname);
11927 print_update_progress_stats(&upa);
11930 const struct got_error *close_err = got_repo_close(repo);
11935 got_worktree_close(worktree);
11937 const struct got_error *pack_err =
11938 got_repo_pack_fds_close(pack_fds);
11943 free(base_commit_id);
11946 free(base_refname);
11953 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11958 static const struct got_error *
11959 cmd_merge(int argc, char *argv[])
11961 const struct got_error *error = NULL;
11962 struct got_worktree *worktree = NULL;
11963 struct got_repository *repo = NULL;
11964 struct got_fileindex *fileindex = NULL;
11965 char *cwd = NULL, *id_str = NULL, *author = NULL;
11966 struct got_reference *branch = NULL, *wt_branch = NULL;
11967 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11968 struct got_object_id *wt_branch_tip = NULL;
11969 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11970 int interrupt_merge = 0;
11971 struct got_update_progress_arg upa;
11972 struct got_object_id *merge_commit_id = NULL;
11973 char *branch_name = NULL;
11974 int *pack_fds = NULL;
11976 memset(&upa, 0, sizeof(upa));
11978 while ((ch = getopt(argc, argv, "acn")) != -1) {
11984 continue_merge = 1;
11987 interrupt_merge = 1;
11999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12000 "unveil", NULL) == -1)
12004 if (abort_merge && continue_merge)
12005 option_conflict('a', 'c');
12006 if (abort_merge || continue_merge) {
12009 } else if (argc != 1)
12012 cwd = getcwd(NULL, 0);
12014 error = got_error_from_errno("getcwd");
12018 error = got_repo_pack_fds_open(&pack_fds);
12022 error = got_worktree_open(&worktree, cwd);
12024 if (error->code == GOT_ERR_NOT_WORKTREE)
12025 error = wrap_not_worktree_error(error,
12030 error = got_repo_open(&repo,
12031 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
12036 error = apply_unveil(got_repo_get_path(repo), 0,
12037 worktree ? got_worktree_get_root_path(worktree) : NULL);
12041 error = check_rebase_or_histedit_in_progress(worktree);
12045 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12051 if (!merge_in_progress) {
12052 error = got_error(GOT_ERR_NOT_MERGING);
12055 error = got_worktree_merge_continue(&branch_name,
12056 &branch_tip, &fileindex, worktree, repo);
12059 error = got_worktree_merge_abort(worktree, fileindex, repo,
12060 abort_progress, &upa);
12063 printf("Merge of %s aborted\n", branch_name);
12064 goto done; /* nothing else to do */
12067 error = get_author(&author, repo, worktree);
12071 if (continue_merge) {
12072 if (!merge_in_progress) {
12073 error = got_error(GOT_ERR_NOT_MERGING);
12076 error = got_worktree_merge_continue(&branch_name,
12077 &branch_tip, &fileindex, worktree, repo);
12081 error = got_ref_open(&branch, repo, argv[0], 0);
12084 branch_name = strdup(got_ref_get_name(branch));
12085 if (branch_name == NULL) {
12086 error = got_error_from_errno("strdup");
12089 error = got_ref_resolve(&branch_tip, repo, branch);
12094 error = got_ref_open(&wt_branch, repo,
12095 got_worktree_get_head_ref_name(worktree), 0);
12098 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
12101 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
12102 wt_branch_tip, branch_tip, 0, repo,
12103 check_cancelled, NULL);
12104 if (error && error->code != GOT_ERR_ANCESTRY)
12107 if (!continue_merge) {
12108 error = check_path_prefix(wt_branch_tip, branch_tip,
12109 got_worktree_get_path_prefix(worktree),
12110 GOT_ERR_MERGE_PATH, repo);
12114 error = check_same_branch(wt_branch_tip, branch,
12117 if (error->code != GOT_ERR_ANCESTRY)
12121 static char msg[512];
12122 snprintf(msg, sizeof(msg),
12123 "cannot create a merge commit because "
12124 "%s is based on %s; %s can be integrated "
12125 "with 'got integrate' instead", branch_name,
12126 got_worktree_get_head_ref_name(worktree),
12128 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
12132 error = got_worktree_merge_prepare(&fileindex, worktree,
12137 error = got_worktree_merge_branch(worktree, fileindex,
12138 yca_id, branch_tip, repo, update_progress, &upa,
12139 check_cancelled, NULL);
12142 print_merge_progress_stats(&upa);
12143 if (!upa.did_something) {
12144 error = got_worktree_merge_abort(worktree, fileindex,
12145 repo, abort_progress, &upa);
12148 printf("Already up-to-date\n");
12153 if (interrupt_merge) {
12154 error = got_worktree_merge_postpone(worktree, fileindex);
12157 printf("Merge of %s interrupted on request\n", branch_name);
12158 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12159 upa.not_deleted > 0 || upa.unversioned > 0) {
12160 error = got_worktree_merge_postpone(worktree, fileindex);
12163 if (upa.conflicts > 0 && upa.missing == 0 &&
12164 upa.not_deleted == 0 && upa.unversioned == 0) {
12165 error = got_error_msg(GOT_ERR_CONFLICTS,
12166 "conflicts must be resolved before merging "
12168 } else if (upa.conflicts > 0) {
12169 error = got_error_msg(GOT_ERR_CONFLICTS,
12170 "conflicts must be resolved before merging "
12171 "can continue; changes destined for some "
12172 "files were not yet merged and "
12173 "should be merged manually if required before the "
12174 "merge operation is continued");
12176 error = got_error_msg(GOT_ERR_CONFLICTS,
12177 "changes destined for some "
12178 "files were not yet merged and should be "
12179 "merged manually if required before the "
12180 "merge operation is continued");
12184 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12185 fileindex, author, NULL, 1, branch_tip, branch_name,
12186 repo, continue_merge ? print_status : NULL, NULL);
12189 error = got_worktree_merge_complete(worktree, fileindex, repo);
12192 error = got_object_id_str(&id_str, merge_commit_id);
12195 printf("Merged %s into %s: %s\n", branch_name,
12196 got_worktree_get_head_ref_name(worktree),
12202 free(merge_commit_id);
12208 got_ref_close(branch);
12210 got_ref_close(wt_branch);
12212 got_worktree_close(worktree);
12214 const struct got_error *close_err = got_repo_close(repo);
12219 const struct got_error *pack_err =
12220 got_repo_pack_fds_close(pack_fds);
12230 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12231 "[-S] [file-path ...]\n",
12236 static const struct got_error *
12237 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12238 const char *path, struct got_object_id *blob_id,
12239 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12240 int dirfd, const char *de_name)
12242 const struct got_error *err = NULL;
12243 char *id_str = NULL;
12245 if (staged_status != GOT_STATUS_ADD &&
12246 staged_status != GOT_STATUS_MODIFY &&
12247 staged_status != GOT_STATUS_DELETE)
12250 if (staged_status == GOT_STATUS_ADD ||
12251 staged_status == GOT_STATUS_MODIFY)
12252 err = got_object_id_str(&id_str, staged_blob_id);
12254 err = got_object_id_str(&id_str, blob_id);
12258 printf("%s %c %s\n", id_str, staged_status, path);
12263 static const struct got_error *
12264 cmd_stage(int argc, char *argv[])
12266 const struct got_error *error = NULL;
12267 struct got_repository *repo = NULL;
12268 struct got_worktree *worktree = NULL;
12270 struct got_pathlist_head paths;
12271 struct got_pathlist_entry *pe;
12272 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12273 FILE *patch_script_file = NULL;
12274 const char *patch_script_path = NULL;
12275 struct choose_patch_arg cpa;
12276 int *pack_fds = NULL;
12278 TAILQ_INIT(&paths);
12280 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12289 patch_script_path = optarg;
12292 allow_bad_symlinks = 1;
12304 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12305 "unveil", NULL) == -1)
12308 if (list_stage && (pflag || patch_script_path))
12309 errx(1, "-l option cannot be used with other options");
12310 if (patch_script_path && !pflag)
12311 errx(1, "-F option can only be used together with -p option");
12313 cwd = getcwd(NULL, 0);
12315 error = got_error_from_errno("getcwd");
12319 error = got_repo_pack_fds_open(&pack_fds);
12323 error = got_worktree_open(&worktree, cwd);
12325 if (error->code == GOT_ERR_NOT_WORKTREE)
12326 error = wrap_not_worktree_error(error, "stage", cwd);
12330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12335 if (patch_script_path) {
12336 patch_script_file = fopen(patch_script_path, "re");
12337 if (patch_script_file == NULL) {
12338 error = got_error_from_errno2("fopen",
12339 patch_script_path);
12343 error = apply_unveil(got_repo_get_path(repo), 0,
12344 got_worktree_get_root_path(worktree));
12348 error = check_merge_in_progress(worktree, repo);
12352 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12357 error = got_worktree_status(worktree, &paths, repo, 0,
12358 print_stage, NULL, check_cancelled, NULL);
12360 cpa.patch_script_file = patch_script_file;
12361 cpa.action = "stage";
12362 error = got_worktree_stage(worktree, &paths,
12363 pflag ? NULL : print_status, NULL,
12364 pflag ? choose_patch : NULL, &cpa,
12365 allow_bad_symlinks, repo);
12368 if (patch_script_file && fclose(patch_script_file) == EOF &&
12370 error = got_error_from_errno2("fclose", patch_script_path);
12372 const struct got_error *close_err = got_repo_close(repo);
12377 got_worktree_close(worktree);
12379 const struct got_error *pack_err =
12380 got_repo_pack_fds_close(pack_fds);
12384 TAILQ_FOREACH(pe, &paths, entry)
12385 free((char *)pe->path);
12386 got_pathlist_free(&paths);
12392 usage_unstage(void)
12394 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12395 "[file-path ...]\n",
12401 static const struct got_error *
12402 cmd_unstage(int argc, char *argv[])
12404 const struct got_error *error = NULL;
12405 struct got_repository *repo = NULL;
12406 struct got_worktree *worktree = NULL;
12408 struct got_pathlist_head paths;
12409 struct got_pathlist_entry *pe;
12411 struct got_update_progress_arg upa;
12412 FILE *patch_script_file = NULL;
12413 const char *patch_script_path = NULL;
12414 struct choose_patch_arg cpa;
12415 int *pack_fds = NULL;
12417 TAILQ_INIT(&paths);
12419 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12425 patch_script_path = optarg;
12437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12438 "unveil", NULL) == -1)
12441 if (patch_script_path && !pflag)
12442 errx(1, "-F option can only be used together with -p option");
12444 cwd = getcwd(NULL, 0);
12446 error = got_error_from_errno("getcwd");
12450 error = got_repo_pack_fds_open(&pack_fds);
12454 error = got_worktree_open(&worktree, cwd);
12456 if (error->code == GOT_ERR_NOT_WORKTREE)
12457 error = wrap_not_worktree_error(error, "unstage", cwd);
12461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12466 if (patch_script_path) {
12467 patch_script_file = fopen(patch_script_path, "re");
12468 if (patch_script_file == NULL) {
12469 error = got_error_from_errno2("fopen",
12470 patch_script_path);
12475 error = apply_unveil(got_repo_get_path(repo), 0,
12476 got_worktree_get_root_path(worktree));
12480 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12484 cpa.patch_script_file = patch_script_file;
12485 cpa.action = "unstage";
12486 memset(&upa, 0, sizeof(upa));
12487 error = got_worktree_unstage(worktree, &paths, update_progress,
12488 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12490 print_merge_progress_stats(&upa);
12492 if (patch_script_file && fclose(patch_script_file) == EOF &&
12494 error = got_error_from_errno2("fclose", patch_script_path);
12496 const struct got_error *close_err = got_repo_close(repo);
12501 got_worktree_close(worktree);
12503 const struct got_error *pack_err =
12504 got_repo_pack_fds_close(pack_fds);
12508 TAILQ_FOREACH(pe, &paths, entry)
12509 free((char *)pe->path);
12510 got_pathlist_free(&paths);
12518 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12519 "arg1 [arg2 ...]\n", getprogname());
12523 static const struct got_error *
12524 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12526 const struct got_error *err;
12527 struct got_blob_object *blob;
12530 fd = got_opentempfd();
12532 return got_error_from_errno("got_opentempfd");
12534 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12538 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12540 if (fd != -1 && close(fd) == -1 && err == NULL)
12541 err = got_error_from_errno("close");
12543 got_object_blob_close(blob);
12547 static const struct got_error *
12548 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12550 const struct got_error *err;
12551 struct got_tree_object *tree;
12554 err = got_object_open_as_tree(&tree, repo, id);
12558 nentries = got_object_tree_get_nentries(tree);
12559 for (i = 0; i < nentries; i++) {
12560 struct got_tree_entry *te;
12562 if (sigint_received || sigpipe_received)
12564 te = got_object_tree_get_entry(tree, i);
12565 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12568 fprintf(outfile, "%s %.7o %s\n", id_str,
12569 got_tree_entry_get_mode(te),
12570 got_tree_entry_get_name(te));
12574 got_object_tree_close(tree);
12578 static const struct got_error *
12579 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12581 const struct got_error *err;
12582 struct got_commit_object *commit;
12583 const struct got_object_id_queue *parent_ids;
12584 struct got_object_qid *pid;
12585 char *id_str = NULL;
12586 const char *logmsg = NULL;
12589 err = got_object_open_as_commit(&commit, repo, id);
12593 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12597 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12598 parent_ids = got_object_commit_get_parent_ids(commit);
12599 fprintf(outfile, "numparents %d\n",
12600 got_object_commit_get_nparents(commit));
12601 STAILQ_FOREACH(pid, parent_ids, entry) {
12603 err = got_object_id_str(&pid_str, &pid->id);
12606 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12609 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12610 got_object_commit_get_author_gmtoff(commit));
12611 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12612 got_object_commit_get_author(commit),
12613 (long long)got_object_commit_get_author_time(commit),
12616 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12617 got_object_commit_get_committer_gmtoff(commit));
12618 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12619 got_object_commit_get_author(commit),
12620 (long long)got_object_commit_get_committer_time(commit),
12623 logmsg = got_object_commit_get_logmsg_raw(commit);
12624 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12625 fprintf(outfile, "%s", logmsg);
12628 got_object_commit_close(commit);
12632 static const struct got_error *
12633 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12635 const struct got_error *err;
12636 struct got_tag_object *tag;
12637 char *id_str = NULL;
12638 const char *tagmsg = NULL;
12641 err = got_object_open_as_tag(&tag, repo, id);
12645 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12649 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12651 switch (got_object_tag_get_object_type(tag)) {
12652 case GOT_OBJ_TYPE_BLOB:
12653 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12654 GOT_OBJ_LABEL_BLOB);
12656 case GOT_OBJ_TYPE_TREE:
12657 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12658 GOT_OBJ_LABEL_TREE);
12660 case GOT_OBJ_TYPE_COMMIT:
12661 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12662 GOT_OBJ_LABEL_COMMIT);
12664 case GOT_OBJ_TYPE_TAG:
12665 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12666 GOT_OBJ_LABEL_TAG);
12672 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12673 got_object_tag_get_name(tag));
12675 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
12676 got_object_tag_get_tagger_gmtoff(tag));
12677 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12678 got_object_tag_get_tagger(tag),
12679 (long long)got_object_tag_get_tagger_time(tag),
12682 tagmsg = got_object_tag_get_message(tag);
12683 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12684 fprintf(outfile, "%s", tagmsg);
12687 got_object_tag_close(tag);
12691 static const struct got_error *
12692 cmd_cat(int argc, char *argv[])
12694 const struct got_error *error;
12695 struct got_repository *repo = NULL;
12696 struct got_worktree *worktree = NULL;
12697 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12698 const char *commit_id_str = NULL;
12699 struct got_object_id *id = NULL, *commit_id = NULL;
12700 struct got_commit_object *commit = NULL;
12701 int ch, obj_type, i, force_path = 0;
12702 struct got_reflist_head refs;
12703 int *pack_fds = NULL;
12708 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12713 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12716 commit_id_str = optarg;
12719 repo_path = realpath(optarg, NULL);
12720 if (repo_path == NULL)
12721 return got_error_from_errno2("realpath",
12723 got_path_strip_trailing_slashes(repo_path);
12737 cwd = getcwd(NULL, 0);
12739 error = got_error_from_errno("getcwd");
12743 error = got_repo_pack_fds_open(&pack_fds);
12747 if (repo_path == NULL) {
12748 error = got_worktree_open(&worktree, cwd);
12749 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12752 repo_path = strdup(
12753 got_worktree_get_repo_path(worktree));
12754 if (repo_path == NULL) {
12755 error = got_error_from_errno("strdup");
12759 /* Release work tree lock. */
12760 got_worktree_close(worktree);
12765 if (repo_path == NULL) {
12766 repo_path = strdup(cwd);
12767 if (repo_path == NULL)
12768 return got_error_from_errno("strdup");
12771 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12776 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12780 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12784 if (commit_id_str == NULL)
12785 commit_id_str = GOT_REF_HEAD;
12786 error = got_repo_match_object_id(&commit_id, NULL,
12787 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12791 error = got_object_open_as_commit(&commit, repo, commit_id);
12795 for (i = 0; i < argc; i++) {
12797 error = got_object_id_by_path(&id, repo, commit,
12802 error = got_repo_match_object_id(&id, &label, argv[i],
12803 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12806 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12807 error->code != GOT_ERR_NOT_REF)
12809 error = got_object_id_by_path(&id, repo,
12816 error = got_object_get_type(&obj_type, repo, id);
12820 switch (obj_type) {
12821 case GOT_OBJ_TYPE_BLOB:
12822 error = cat_blob(id, repo, stdout);
12824 case GOT_OBJ_TYPE_TREE:
12825 error = cat_tree(id, repo, stdout);
12827 case GOT_OBJ_TYPE_COMMIT:
12828 error = cat_commit(id, repo, stdout);
12830 case GOT_OBJ_TYPE_TAG:
12831 error = cat_tag(id, repo, stdout);
12834 error = got_error(GOT_ERR_OBJ_TYPE);
12849 got_object_commit_close(commit);
12851 got_worktree_close(worktree);
12853 const struct got_error *close_err = got_repo_close(repo);
12858 const struct got_error *pack_err =
12859 got_repo_pack_fds_close(pack_fds);
12864 got_ref_list_free(&refs);
12871 fprintf(stderr, "usage: %s info [path ...]\n",
12876 static const struct got_error *
12877 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12878 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12879 struct got_object_id *commit_id)
12881 const struct got_error *err = NULL;
12882 char *id_str = NULL;
12884 struct tm mytm, *tm;
12885 struct got_pathlist_head *paths = arg;
12886 struct got_pathlist_entry *pe;
12889 * Clear error indication from any of the path arguments which
12890 * would cause this file index entry to be displayed.
12892 TAILQ_FOREACH(pe, paths, entry) {
12893 if (got_path_cmp(path, pe->path, strlen(path),
12894 pe->path_len) == 0 ||
12895 got_path_is_child(path, pe->path, pe->path_len))
12896 pe->data = NULL; /* no error */
12899 printf(GOT_COMMIT_SEP_STR);
12901 printf("symlink: %s\n", path);
12902 else if (S_ISREG(mode)) {
12903 printf("file: %s\n", path);
12904 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12905 } else if (S_ISDIR(mode))
12906 printf("directory: %s\n", path);
12908 printf("something: %s\n", path);
12910 tm = localtime_r(&mtime, &mytm);
12913 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12914 return got_error(GOT_ERR_NO_SPACE);
12915 printf("timestamp: %s\n", datebuf);
12918 err = got_object_id_str(&id_str, blob_id);
12921 printf("based on blob: %s\n", id_str);
12925 if (staged_blob_id) {
12926 err = got_object_id_str(&id_str, staged_blob_id);
12929 printf("based on staged blob: %s\n", id_str);
12934 err = got_object_id_str(&id_str, commit_id);
12937 printf("based on commit: %s\n", id_str);
12944 static const struct got_error *
12945 cmd_info(int argc, char *argv[])
12947 const struct got_error *error = NULL;
12948 struct got_worktree *worktree = NULL;
12949 char *cwd = NULL, *id_str = NULL;
12950 struct got_pathlist_head paths;
12951 struct got_pathlist_entry *pe;
12952 char *uuidstr = NULL;
12953 int ch, show_files = 0;
12954 int *pack_fds = NULL;
12956 TAILQ_INIT(&paths);
12958 while ((ch = getopt(argc, argv, "")) != -1) {
12970 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12974 cwd = getcwd(NULL, 0);
12976 error = got_error_from_errno("getcwd");
12980 error = got_repo_pack_fds_open(&pack_fds);
12984 error = got_worktree_open(&worktree, cwd);
12986 if (error->code == GOT_ERR_NOT_WORKTREE)
12987 error = wrap_not_worktree_error(error, "info", cwd);
12992 /* Remove "cpath" promise. */
12993 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12997 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
13002 error = get_worktree_paths_from_argv(&paths, argc, argv,
13009 error = got_object_id_str(&id_str,
13010 got_worktree_get_base_commit_id(worktree));
13014 error = got_worktree_get_uuid(&uuidstr, worktree);
13018 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
13019 printf("work tree base commit: %s\n", id_str);
13020 printf("work tree path prefix: %s\n",
13021 got_worktree_get_path_prefix(worktree));
13022 printf("work tree branch reference: %s\n",
13023 got_worktree_get_head_ref_name(worktree));
13024 printf("work tree UUID: %s\n", uuidstr);
13025 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
13028 struct got_pathlist_entry *pe;
13029 TAILQ_FOREACH(pe, &paths, entry) {
13030 if (pe->path_len == 0)
13033 * Assume this path will fail. This will be corrected
13034 * in print_path_info() in case the path does suceeed.
13036 pe->data = (void *)got_error_path(pe->path,
13039 error = got_worktree_path_info(worktree, &paths,
13040 print_path_info, &paths, check_cancelled, NULL);
13043 TAILQ_FOREACH(pe, &paths, entry) {
13044 if (pe->data != NULL) {
13045 error = pe->data; /* bad path */
13052 const struct got_error *pack_err =
13053 got_repo_pack_fds_close(pack_fds);
13057 TAILQ_FOREACH(pe, &paths, entry)
13058 free((char *)pe->path);
13059 got_pathlist_free(&paths);