2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
57 #include "got_opentemp.h"
58 #include "got_gotconfig.h"
60 #include "got_patch.h"
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 static volatile sig_atomic_t sigint_received;
67 static volatile sig_atomic_t sigpipe_received;
70 catch_sigint(int signo)
76 catch_sigpipe(int signo)
84 const struct got_error *(*cmd_main)(int, char *[]);
85 void (*cmd_usage)(void);
86 const char *cmd_alias;
89 __dead static void usage(int, int);
90 __dead static void usage_init(void);
91 __dead static void usage_import(void);
92 __dead static void usage_clone(void);
93 __dead static void usage_fetch(void);
94 __dead static void usage_checkout(void);
95 __dead static void usage_update(void);
96 __dead static void usage_log(void);
97 __dead static void usage_diff(void);
98 __dead static void usage_blame(void);
99 __dead static void usage_tree(void);
100 __dead static void usage_status(void);
101 __dead static void usage_ref(void);
102 __dead static void usage_branch(void);
103 __dead static void usage_tag(void);
104 __dead static void usage_add(void);
105 __dead static void usage_remove(void);
106 __dead static void usage_patch(void);
107 __dead static void usage_revert(void);
108 __dead static void usage_commit(void);
109 __dead static void usage_send(void);
110 __dead static void usage_cherrypick(void);
111 __dead static void usage_backout(void);
112 __dead static void usage_rebase(void);
113 __dead static void usage_histedit(void);
114 __dead static void usage_integrate(void);
115 __dead static void usage_merge(void);
116 __dead static void usage_stage(void);
117 __dead static void usage_unstage(void);
118 __dead static void usage_cat(void);
119 __dead static void usage_info(void);
121 static const struct got_error* cmd_init(int, char *[]);
122 static const struct got_error* cmd_import(int, char *[]);
123 static const struct got_error* cmd_clone(int, char *[]);
124 static const struct got_error* cmd_fetch(int, char *[]);
125 static const struct got_error* cmd_checkout(int, char *[]);
126 static const struct got_error* cmd_update(int, char *[]);
127 static const struct got_error* cmd_log(int, char *[]);
128 static const struct got_error* cmd_diff(int, char *[]);
129 static const struct got_error* cmd_blame(int, char *[]);
130 static const struct got_error* cmd_tree(int, char *[]);
131 static const struct got_error* cmd_status(int, char *[]);
132 static const struct got_error* cmd_ref(int, char *[]);
133 static const struct got_error* cmd_branch(int, char *[]);
134 static const struct got_error* cmd_tag(int, char *[]);
135 static const struct got_error* cmd_add(int, char *[]);
136 static const struct got_error* cmd_remove(int, char *[]);
137 static const struct got_error* cmd_patch(int, char *[]);
138 static const struct got_error* cmd_revert(int, char *[]);
139 static const struct got_error* cmd_commit(int, char *[]);
140 static const struct got_error* cmd_send(int, char *[]);
141 static const struct got_error* cmd_cherrypick(int, char *[]);
142 static const struct got_error* cmd_backout(int, char *[]);
143 static const struct got_error* cmd_rebase(int, char *[]);
144 static const struct got_error* cmd_histedit(int, char *[]);
145 static const struct got_error* cmd_integrate(int, char *[]);
146 static const struct got_error* cmd_merge(int, char *[]);
147 static const struct got_error* cmd_stage(int, char *[]);
148 static const struct got_error* cmd_unstage(int, char *[]);
149 static const struct got_error* cmd_cat(int, char *[]);
150 static const struct got_error* cmd_info(int, char *[]);
152 static const struct got_cmd got_commands[] = {
153 { "init", cmd_init, usage_init, "" },
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 init repository-path\n", getprogname());
354 static const struct got_error *
355 cmd_init(int argc, char *argv[])
357 const struct got_error *error = NULL;
358 char *repo_path = NULL;
361 while ((ch = getopt(argc, argv, "")) != -1) {
373 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
379 repo_path = strdup(argv[0]);
380 if (repo_path == NULL)
381 return got_error_from_errno("strdup");
383 got_path_strip_trailing_slashes(repo_path);
385 error = got_path_mkdir(repo_path);
387 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
390 error = apply_unveil(repo_path, 0, NULL);
394 error = got_repo_init(repo_path);
403 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
404 "[-r repository-path] [-I pattern] path\n", getprogname());
409 spawn_editor(const char *editor, const char *file)
412 sig_t sighup, sigint, sigquit;
415 sighup = signal(SIGHUP, SIG_IGN);
416 sigint = signal(SIGINT, SIG_IGN);
417 sigquit = signal(SIGQUIT, SIG_IGN);
419 switch (pid = fork()) {
423 execl(editor, editor, file, (char *)NULL);
427 while (waitpid(pid, &st, 0) == -1)
432 (void)signal(SIGHUP, sighup);
433 (void)signal(SIGINT, sigint);
434 (void)signal(SIGQUIT, sigquit);
436 if (!WIFEXITED(st)) {
441 return WEXITSTATUS(st);
444 static const struct got_error *
445 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
446 const char *initial_content, size_t initial_content_len,
447 int require_modification)
449 const struct got_error *err = NULL;
455 size_t len, logmsg_len;
456 char *initial_content_stripped = NULL, *buf = NULL, *s;
460 if (stat(logmsg_path, &st) == -1)
461 return got_error_from_errno2("stat", logmsg_path);
463 if (spawn_editor(editor, logmsg_path) == -1)
464 return got_error_from_errno("failed spawning editor");
466 if (stat(logmsg_path, &st2) == -1)
467 return got_error_from_errno("stat");
469 if (require_modification &&
470 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
471 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "no changes made to commit message, aborting");
475 * Set up a stripped version of the initial content without comments
476 * and blank lines. We need this in order to check if the message
477 * has in fact been edited.
479 initial_content_stripped = malloc(initial_content_len + 1);
480 if (initial_content_stripped == NULL)
481 return got_error_from_errno("malloc");
482 initial_content_stripped[0] = '\0';
484 buf = strdup(initial_content);
486 err = got_error_from_errno("strdup");
491 while ((line = strsep(&s, "\n")) != NULL) {
492 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
493 continue; /* remove comments and leading empty lines */
494 len = strlcat(initial_content_stripped, line,
495 initial_content_len + 1);
496 if (len >= initial_content_len + 1) {
497 err = got_error(GOT_ERR_NO_SPACE);
501 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
502 initial_content_stripped[len - 1] = '\0';
506 logmsg_len = st2.st_size;
507 *logmsg = malloc(logmsg_len + 1);
509 return got_error_from_errno("malloc");
512 fp = fopen(logmsg_path, "re");
514 err = got_error_from_errno("fopen");
519 while ((linelen = getline(&line, &linesize, fp)) != -1) {
520 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
521 continue; /* remove comments and leading empty lines */
522 len = strlcat(*logmsg, line, logmsg_len + 1);
523 if (len >= logmsg_len + 1) {
524 err = got_error(GOT_ERR_NO_SPACE);
530 err = got_ferror(fp, GOT_ERR_IO);
533 while (len > 0 && (*logmsg)[len - 1] == '\n') {
534 (*logmsg)[len - 1] = '\0';
539 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
540 "commit message cannot be empty, aborting");
543 if (require_modification &&
544 strcmp(*logmsg, initial_content_stripped) == 0)
545 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
546 "no changes made to commit message, aborting");
548 free(initial_content_stripped);
550 if (fp && fclose(fp) == EOF && err == NULL)
551 err = got_error_from_errno("fclose");
559 static const struct got_error *
560 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
561 const char *path_dir, const char *branch_name)
563 char *initial_content = NULL;
564 const struct got_error *err = NULL;
565 int initial_content_len;
568 initial_content_len = asprintf(&initial_content,
569 "\n# %s to be imported to branch %s\n", path_dir,
571 if (initial_content_len == -1)
572 return got_error_from_errno("asprintf");
574 err = got_opentemp_named_fd(logmsg_path, &fd,
575 GOT_TMPDIR_STR "/got-importmsg");
579 if (write(fd, initial_content, initial_content_len) == -1) {
580 err = got_error_from_errno2("write", *logmsg_path);
584 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
585 initial_content_len, 1);
587 if (fd != -1 && close(fd) == -1 && err == NULL)
588 err = got_error_from_errno2("close", *logmsg_path);
589 free(initial_content);
597 static const struct got_error *
598 import_progress(void *arg, const char *path)
600 printf("A %s\n", path);
605 valid_author(const char *author)
608 * Really dumb email address check; we're only doing this to
609 * avoid git's object parser breaking on commits we create.
611 while (*author && *author != '<')
615 while (*author && *author != '@')
619 while (*author && *author != '>')
621 return *author == '>';
624 static const struct got_error *
625 get_author(char **author, struct got_repository *repo,
626 struct got_worktree *worktree)
628 const struct got_error *err = NULL;
629 const char *got_author = NULL, *name, *email;
630 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
635 worktree_conf = got_worktree_get_gotconfig(worktree);
636 repo_conf = got_repo_get_gotconfig(repo);
639 * Priority of potential author information sources, from most
640 * significant to least significant:
641 * 1) work tree's .got/got.conf file
642 * 2) repository's got.conf file
643 * 3) repository's git config file
644 * 4) environment variables
645 * 5) global git config files (in user's home directory or /etc)
649 got_author = got_gotconfig_get_author(worktree_conf);
650 if (got_author == NULL)
651 got_author = got_gotconfig_get_author(repo_conf);
652 if (got_author == NULL) {
653 name = got_repo_get_gitconfig_author_name(repo);
654 email = got_repo_get_gitconfig_author_email(repo);
656 if (asprintf(author, "%s <%s>", name, email) == -1)
657 return got_error_from_errno("asprintf");
661 got_author = getenv("GOT_AUTHOR");
662 if (got_author == NULL) {
663 name = got_repo_get_global_gitconfig_author_name(repo);
664 email = got_repo_get_global_gitconfig_author_email(
667 if (asprintf(author, "%s <%s>", name, email)
669 return got_error_from_errno("asprintf");
672 /* TODO: Look up user in password database? */
673 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
677 *author = strdup(got_author);
679 return got_error_from_errno("strdup");
681 if (!valid_author(*author)) {
682 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
689 static const struct got_error *
690 get_gitconfig_path(char **gitconfig_path)
692 const char *homedir = getenv("HOME");
694 *gitconfig_path = NULL;
696 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
697 return got_error_from_errno("asprintf");
703 static const struct got_error *
704 cmd_import(int argc, char *argv[])
706 const struct got_error *error = NULL;
707 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
708 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
709 const char *branch_name = "main";
710 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
711 struct got_repository *repo = NULL;
712 struct got_reference *branch_ref = NULL, *head_ref = NULL;
713 struct got_object_id *new_commit_id = NULL;
715 struct got_pathlist_head ignores;
716 struct got_pathlist_entry *pe;
717 int preserve_logmsg = 0;
718 int *pack_fds = NULL;
720 TAILQ_INIT(&ignores);
722 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
725 branch_name = optarg;
728 logmsg = strdup(optarg);
729 if (logmsg == NULL) {
730 error = got_error_from_errno("strdup");
735 repo_path = realpath(optarg, NULL);
736 if (repo_path == NULL) {
737 error = got_error_from_errno2("realpath",
743 if (optarg[0] == '\0')
745 error = got_pathlist_insert(&pe, &ignores, optarg,
760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
768 if (repo_path == NULL) {
769 repo_path = getcwd(NULL, 0);
770 if (repo_path == NULL)
771 return got_error_from_errno("getcwd");
773 got_path_strip_trailing_slashes(repo_path);
774 error = get_gitconfig_path(&gitconfig_path);
777 error = got_repo_pack_fds_open(&pack_fds);
780 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
784 error = get_author(&author, repo, NULL);
789 * Don't let the user create a branch name with a leading '-'.
790 * While technically a valid reference name, this case is usually
791 * an unintended typo.
793 if (branch_name[0] == '-')
794 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
796 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
797 error = got_error_from_errno("asprintf");
801 error = got_ref_open(&branch_ref, repo, refname, 0);
803 if (error->code != GOT_ERR_NOT_REF)
806 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
807 "import target branch already exists");
811 path_dir = realpath(argv[0], NULL);
812 if (path_dir == NULL) {
813 error = got_error_from_errno2("realpath", argv[0]);
816 got_path_strip_trailing_slashes(path_dir);
819 * unveil(2) traverses exec(2); if an editor is used we have
820 * to apply unveil after the log message has been written.
822 if (logmsg == NULL || strlen(logmsg) == 0) {
823 error = get_editor(&editor);
827 error = collect_import_msg(&logmsg, &logmsg_path, editor,
830 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
837 if (unveil(path_dir, "r") != 0) {
838 error = got_error_from_errno2("unveil", path_dir);
844 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
851 error = got_repo_import(&new_commit_id, path_dir, logmsg,
852 author, &ignores, repo, import_progress, NULL);
859 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
866 error = got_ref_write(branch_ref, repo);
873 error = got_object_id_str(&id_str, new_commit_id);
880 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
882 if (error->code != GOT_ERR_NOT_REF) {
888 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
896 error = got_ref_write(head_ref, repo);
904 printf("Created branch %s with commit %s\n",
905 got_ref_get_name(branch_ref), id_str);
908 const struct got_error *pack_err =
909 got_repo_pack_fds_close(pack_fds);
913 if (preserve_logmsg) {
914 fprintf(stderr, "%s: log message preserved in %s\n",
915 getprogname(), logmsg_path);
916 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
917 error = got_error_from_errno2("unlink", logmsg_path);
926 free(gitconfig_path);
928 got_ref_close(branch_ref);
930 got_ref_close(head_ref);
937 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
938 "[-R reference] repository-url [directory]\n", getprogname());
942 struct got_fetch_progress_arg {
943 char last_scaled_size[FMT_SCALED_STRSIZE];
948 struct got_repository *repo;
953 struct got_pathlist_head *symrefs;
954 struct got_pathlist_head *wanted_branches;
955 struct got_pathlist_head *wanted_refs;
959 const char *remote_repo_path;
961 int fetch_all_branches;
962 int mirror_references;
966 /* XXX forward declaration */
967 static const struct got_error *
968 create_config_files(const char *proto, const char *host, const char *port,
969 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
970 int mirror_references, struct got_pathlist_head *symrefs,
971 struct got_pathlist_head *wanted_branches,
972 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
974 static const struct got_error *
975 fetch_progress(void *arg, const char *message, off_t packfile_size,
976 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
978 const struct got_error *err = NULL;
979 struct got_fetch_progress_arg *a = arg;
980 char scaled_size[FMT_SCALED_STRSIZE];
981 int p_indexed, p_resolved;
982 int print_size = 0, print_indexed = 0, print_resolved = 0;
985 * In order to allow a failed clone to be resumed with 'got fetch'
986 * we try to create configuration files as soon as possible.
987 * Once the server has sent information about its default branch
988 * we have all required information.
990 if (a->create_configs && !a->configs_created &&
991 !TAILQ_EMPTY(a->config_info.symrefs)) {
992 err = create_config_files(a->config_info.proto,
993 a->config_info.host, a->config_info.port,
994 a->config_info.remote_repo_path,
995 a->config_info.git_url,
996 a->config_info.fetch_all_branches,
997 a->config_info.mirror_references,
998 a->config_info.symrefs,
999 a->config_info.wanted_branches,
1000 a->config_info.wanted_refs, a->repo);
1003 a->configs_created = 1;
1006 if (a->verbosity < 0)
1009 if (message && message[0] != '\0') {
1010 printf("\rserver: %s", message);
1015 if (packfile_size > 0 || nobj_indexed > 0) {
1016 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1017 (a->last_scaled_size[0] == '\0' ||
1018 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1020 if (strlcpy(a->last_scaled_size, scaled_size,
1021 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1022 return got_error(GOT_ERR_NO_SPACE);
1024 if (nobj_indexed > 0) {
1025 p_indexed = (nobj_indexed * 100) / nobj_total;
1026 if (p_indexed != a->last_p_indexed) {
1027 a->last_p_indexed = p_indexed;
1032 if (nobj_resolved > 0) {
1033 p_resolved = (nobj_resolved * 100) /
1034 (nobj_total - nobj_loose);
1035 if (p_resolved != a->last_p_resolved) {
1036 a->last_p_resolved = p_resolved;
1044 if (print_size || print_indexed || print_resolved)
1047 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1049 printf("; indexing %d%%", p_indexed);
1051 printf("; resolving deltas %d%%", p_resolved);
1052 if (print_size || print_indexed || print_resolved)
1058 static const struct got_error *
1059 create_symref(const char *refname, struct got_reference *target_ref,
1060 int verbosity, struct got_repository *repo)
1062 const struct got_error *err;
1063 struct got_reference *head_symref;
1065 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1069 err = got_ref_write(head_symref, repo);
1070 if (err == NULL && verbosity > 0) {
1071 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1072 got_ref_get_name(target_ref));
1074 got_ref_close(head_symref);
1078 static const struct got_error *
1079 list_remote_refs(struct got_pathlist_head *symrefs,
1080 struct got_pathlist_head *refs)
1082 const struct got_error *err;
1083 struct got_pathlist_entry *pe;
1085 TAILQ_FOREACH(pe, symrefs, entry) {
1086 const char *refname = pe->path;
1087 const char *targetref = pe->data;
1089 printf("%s: %s\n", refname, targetref);
1092 TAILQ_FOREACH(pe, refs, entry) {
1093 const char *refname = pe->path;
1094 struct got_object_id *id = pe->data;
1097 err = got_object_id_str(&id_str, id);
1100 printf("%s: %s\n", refname, id_str);
1107 static const struct got_error *
1108 create_ref(const char *refname, struct got_object_id *id,
1109 int verbosity, struct got_repository *repo)
1111 const struct got_error *err = NULL;
1112 struct got_reference *ref;
1115 err = got_object_id_str(&id_str, id);
1119 err = got_ref_alloc(&ref, refname, id);
1123 err = got_ref_write(ref, repo);
1126 if (err == NULL && verbosity >= 0)
1127 printf("Created reference %s: %s\n", refname, id_str);
1134 match_wanted_ref(const char *refname, const char *wanted_ref)
1136 if (strncmp(refname, "refs/", 5) != 0)
1141 * Prevent fetching of references that won't make any
1142 * sense outside of the remote repository's context.
1144 if (strncmp(refname, "got/", 4) == 0)
1146 if (strncmp(refname, "remotes/", 8) == 0)
1149 if (strncmp(wanted_ref, "refs/", 5) == 0)
1152 /* Allow prefix match. */
1153 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1156 /* Allow exact match. */
1157 return (strcmp(refname, wanted_ref) == 0);
1161 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1163 struct got_pathlist_entry *pe;
1165 TAILQ_FOREACH(pe, wanted_refs, entry) {
1166 if (match_wanted_ref(refname, pe->path))
1173 static const struct got_error *
1174 create_wanted_ref(const char *refname, struct got_object_id *id,
1175 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1177 const struct got_error *err;
1178 char *remote_refname;
1180 if (strncmp("refs/", refname, 5) == 0)
1183 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1184 remote_repo_name, refname) == -1)
1185 return got_error_from_errno("asprintf");
1187 err = create_ref(remote_refname, id, verbosity, repo);
1188 free(remote_refname);
1192 static const struct got_error *
1193 create_gotconfig(const char *proto, const char *host, const char *port,
1194 const char *remote_repo_path, const char *default_branch,
1195 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1196 struct got_pathlist_head *wanted_refs, int mirror_references,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 char *gotconfig_path = NULL;
1201 char *gotconfig = NULL;
1202 FILE *gotconfig_file = NULL;
1203 const char *branchname = NULL;
1204 char *branches = NULL, *refs = NULL;
1207 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1208 struct got_pathlist_entry *pe;
1209 TAILQ_FOREACH(pe, wanted_branches, entry) {
1211 branchname = pe->path;
1212 if (strncmp(branchname, "refs/heads/", 11) == 0)
1214 if (asprintf(&s, "%s\"%s\" ",
1215 branches ? branches : "", branchname) == -1) {
1216 err = got_error_from_errno("asprintf");
1222 } else if (!fetch_all_branches && default_branch) {
1223 branchname = default_branch;
1224 if (strncmp(branchname, "refs/heads/", 11) == 0)
1226 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1227 err = got_error_from_errno("asprintf");
1231 if (!TAILQ_EMPTY(wanted_refs)) {
1232 struct got_pathlist_entry *pe;
1233 TAILQ_FOREACH(pe, wanted_refs, entry) {
1235 const char *refname = pe->path;
1236 if (strncmp(refname, "refs/", 5) == 0)
1238 if (asprintf(&s, "%s\"%s\" ",
1239 refs ? refs : "", refname) == -1) {
1240 err = got_error_from_errno("asprintf");
1248 /* Create got.conf(5). */
1249 gotconfig_path = got_repo_get_path_gotconfig(repo);
1250 if (gotconfig_path == NULL) {
1251 err = got_error_from_errno("got_repo_get_path_gotconfig");
1254 gotconfig_file = fopen(gotconfig_path, "ae");
1255 if (gotconfig_file == NULL) {
1256 err = got_error_from_errno2("fopen", gotconfig_path);
1259 if (asprintf(&gotconfig,
1264 "\trepository \"%s\"\n"
1270 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1271 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1272 remote_repo_path, branches ? "\tbranch { " : "",
1273 branches ? branches : "", branches ? "}\n" : "",
1274 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1275 mirror_references ? "\tmirror-references yes\n" : "",
1276 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1277 err = got_error_from_errno("asprintf");
1280 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1281 if (n != strlen(gotconfig)) {
1282 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1287 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1288 err = got_error_from_errno2("fclose", gotconfig_path);
1289 free(gotconfig_path);
1294 static const struct got_error *
1295 create_gitconfig(const char *git_url, const char *default_branch,
1296 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1297 struct got_pathlist_head *wanted_refs, int mirror_references,
1298 struct got_repository *repo)
1300 const struct got_error *err = NULL;
1301 char *gitconfig_path = NULL;
1302 char *gitconfig = NULL;
1303 FILE *gitconfig_file = NULL;
1304 char *branches = NULL, *refs = NULL;
1305 const char *branchname;
1308 /* Create a config file Git can understand. */
1309 gitconfig_path = got_repo_get_path_gitconfig(repo);
1310 if (gitconfig_path == NULL) {
1311 err = got_error_from_errno("got_repo_get_path_gitconfig");
1314 gitconfig_file = fopen(gitconfig_path, "ae");
1315 if (gitconfig_file == NULL) {
1316 err = got_error_from_errno2("fopen", gitconfig_path);
1319 if (fetch_all_branches) {
1320 if (mirror_references) {
1321 if (asprintf(&branches,
1322 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1323 err = got_error_from_errno("asprintf");
1326 } else if (asprintf(&branches,
1327 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1328 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1329 err = got_error_from_errno("asprintf");
1332 } else if (!TAILQ_EMPTY(wanted_branches)) {
1333 struct got_pathlist_entry *pe;
1334 TAILQ_FOREACH(pe, wanted_branches, entry) {
1336 branchname = pe->path;
1337 if (strncmp(branchname, "refs/heads/", 11) == 0)
1339 if (mirror_references) {
1341 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1342 branches ? branches : "",
1343 branchname, branchname) == -1) {
1344 err = got_error_from_errno("asprintf");
1347 } else if (asprintf(&s,
1348 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1349 branches ? branches : "",
1350 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1351 branchname) == -1) {
1352 err = got_error_from_errno("asprintf");
1360 * If the server specified a default branch, use just that one.
1361 * Otherwise fall back to fetching all branches on next fetch.
1363 if (default_branch) {
1364 branchname = default_branch;
1365 if (strncmp(branchname, "refs/heads/", 11) == 0)
1368 branchname = "*"; /* fall back to all branches */
1369 if (mirror_references) {
1370 if (asprintf(&branches,
1371 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1372 branchname, branchname) == -1) {
1373 err = got_error_from_errno("asprintf");
1376 } else if (asprintf(&branches,
1377 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1378 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1379 branchname) == -1) {
1380 err = got_error_from_errno("asprintf");
1384 if (!TAILQ_EMPTY(wanted_refs)) {
1385 struct got_pathlist_entry *pe;
1386 TAILQ_FOREACH(pe, wanted_refs, entry) {
1388 const char *refname = pe->path;
1389 if (strncmp(refname, "refs/", 5) == 0)
1391 if (mirror_references) {
1393 "%s\tfetch = refs/%s:refs/%s\n",
1394 refs ? refs : "", refname, refname) == -1) {
1395 err = got_error_from_errno("asprintf");
1398 } else if (asprintf(&s,
1399 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1401 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1403 err = got_error_from_errno("asprintf");
1411 if (asprintf(&gitconfig,
1416 "\tfetch = refs/tags/*:refs/tags/*\n",
1417 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1418 refs ? refs : "") == -1) {
1419 err = got_error_from_errno("asprintf");
1422 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1423 if (n != strlen(gitconfig)) {
1424 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1428 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1429 err = got_error_from_errno2("fclose", gitconfig_path);
1430 free(gitconfig_path);
1435 static const struct got_error *
1436 create_config_files(const char *proto, const char *host, const char *port,
1437 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1438 int mirror_references, struct got_pathlist_head *symrefs,
1439 struct got_pathlist_head *wanted_branches,
1440 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 const char *default_branch = NULL;
1444 struct got_pathlist_entry *pe;
1447 * If we asked for a set of wanted branches then use the first
1450 if (!TAILQ_EMPTY(wanted_branches)) {
1451 pe = TAILQ_FIRST(wanted_branches);
1452 default_branch = pe->path;
1454 /* First HEAD ref listed by server is the default branch. */
1455 TAILQ_FOREACH(pe, symrefs, entry) {
1456 const char *refname = pe->path;
1457 const char *target = pe->data;
1459 if (strcmp(refname, GOT_REF_HEAD) != 0)
1462 default_branch = target;
1467 /* Create got.conf(5). */
1468 err = create_gotconfig(proto, host, port, remote_repo_path,
1469 default_branch, fetch_all_branches, wanted_branches,
1470 wanted_refs, mirror_references, repo);
1474 /* Create a config file Git can understand. */
1475 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1476 wanted_branches, wanted_refs, mirror_references, repo);
1479 static const struct got_error *
1480 cmd_clone(int argc, char *argv[])
1482 const struct got_error *error = NULL;
1483 const char *uri, *dirname;
1484 char *proto, *host, *port, *repo_name, *server_path;
1485 char *default_destdir = NULL, *id_str = NULL;
1486 const char *repo_path;
1487 struct got_repository *repo = NULL;
1488 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1489 struct got_pathlist_entry *pe;
1490 struct got_object_id *pack_hash = NULL;
1491 int ch, fetchfd = -1, fetchstatus;
1492 pid_t fetchpid = -1;
1493 struct got_fetch_progress_arg fpa;
1494 char *git_url = NULL;
1495 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1496 int list_refs_only = 0;
1497 int *pack_fds = NULL;
1500 TAILQ_INIT(&symrefs);
1501 TAILQ_INIT(&wanted_branches);
1502 TAILQ_INIT(&wanted_refs);
1504 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1507 fetch_all_branches = 1;
1510 error = got_pathlist_append(&wanted_branches,
1519 mirror_references = 1;
1524 else if (verbosity < 3)
1531 error = got_pathlist_append(&wanted_refs,
1544 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1545 option_conflict('a', 'b');
1546 if (list_refs_only) {
1547 if (!TAILQ_EMPTY(&wanted_branches))
1548 option_conflict('l', 'b');
1549 if (fetch_all_branches)
1550 option_conflict('l', 'a');
1551 if (mirror_references)
1552 option_conflict('l', 'm');
1553 if (!TAILQ_EMPTY(&wanted_refs))
1554 option_conflict('l', 'R');
1566 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1571 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1572 host, port ? ":" : "", port ? port : "",
1573 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1574 error = got_error_from_errno("asprintf");
1578 if (strcmp(proto, "git") == 0) {
1580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1581 "sendfd dns inet unveil", NULL) == -1)
1584 } else if (strcmp(proto, "git+ssh") == 0 ||
1585 strcmp(proto, "ssh") == 0) {
1587 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1588 "sendfd unveil", NULL) == -1)
1591 } else if (strcmp(proto, "http") == 0 ||
1592 strcmp(proto, "git+http") == 0) {
1593 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1596 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1599 if (dirname == NULL) {
1600 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1601 error = got_error_from_errno("asprintf");
1604 repo_path = default_destdir;
1606 repo_path = dirname;
1608 if (!list_refs_only) {
1609 error = got_path_mkdir(repo_path);
1611 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1612 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1614 if (!got_path_dir_is_empty(repo_path)) {
1615 error = got_error_path(repo_path,
1616 GOT_ERR_DIR_NOT_EMPTY);
1621 error = got_dial_apply_unveil(proto);
1625 error = apply_unveil(repo_path, 0, NULL);
1630 printf("Connecting to %s%s%s\n", host,
1631 port ? ":" : "", port ? port : "");
1633 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1634 server_path, verbosity);
1638 if (!list_refs_only) {
1639 error = got_repo_init(repo_path);
1642 error = got_repo_pack_fds_open(&pack_fds);
1645 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1650 fpa.last_scaled_size[0] = '\0';
1651 fpa.last_p_indexed = -1;
1652 fpa.last_p_resolved = -1;
1653 fpa.verbosity = verbosity;
1654 fpa.create_configs = 1;
1655 fpa.configs_created = 0;
1657 fpa.config_info.symrefs = &symrefs;
1658 fpa.config_info.wanted_branches = &wanted_branches;
1659 fpa.config_info.wanted_refs = &wanted_refs;
1660 fpa.config_info.proto = proto;
1661 fpa.config_info.host = host;
1662 fpa.config_info.port = port;
1663 fpa.config_info.remote_repo_path = server_path;
1664 fpa.config_info.git_url = git_url;
1665 fpa.config_info.fetch_all_branches = fetch_all_branches;
1666 fpa.config_info.mirror_references = mirror_references;
1667 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1668 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1669 fetch_all_branches, &wanted_branches, &wanted_refs,
1670 list_refs_only, verbosity, fetchfd, repo,
1671 fetch_progress, &fpa);
1675 if (list_refs_only) {
1676 error = list_remote_refs(&symrefs, &refs);
1680 if (pack_hash == NULL) {
1681 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1682 "server sent an empty pack file");
1685 error = got_object_id_str(&id_str, pack_hash);
1689 printf("\nFetched %s.pack\n", id_str);
1692 /* Set up references provided with the pack file. */
1693 TAILQ_FOREACH(pe, &refs, entry) {
1694 const char *refname = pe->path;
1695 struct got_object_id *id = pe->data;
1696 char *remote_refname;
1698 if (is_wanted_ref(&wanted_refs, refname) &&
1699 !mirror_references) {
1700 error = create_wanted_ref(refname, id,
1701 GOT_FETCH_DEFAULT_REMOTE_NAME,
1702 verbosity - 1, repo);
1708 error = create_ref(refname, id, verbosity - 1, repo);
1712 if (mirror_references)
1715 if (strncmp("refs/heads/", refname, 11) != 0)
1718 if (asprintf(&remote_refname,
1719 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1720 refname + 11) == -1) {
1721 error = got_error_from_errno("asprintf");
1724 error = create_ref(remote_refname, id, verbosity - 1, repo);
1725 free(remote_refname);
1730 /* Set the HEAD reference if the server provided one. */
1731 TAILQ_FOREACH(pe, &symrefs, entry) {
1732 struct got_reference *target_ref;
1733 const char *refname = pe->path;
1734 const char *target = pe->data;
1735 char *remote_refname = NULL, *remote_target = NULL;
1737 if (strcmp(refname, GOT_REF_HEAD) != 0)
1740 error = got_ref_open(&target_ref, repo, target, 0);
1742 if (error->code == GOT_ERR_NOT_REF) {
1749 error = create_symref(refname, target_ref, verbosity, repo);
1750 got_ref_close(target_ref);
1754 if (mirror_references)
1757 if (strncmp("refs/heads/", target, 11) != 0)
1760 if (asprintf(&remote_refname,
1761 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1763 error = got_error_from_errno("asprintf");
1766 if (asprintf(&remote_target,
1767 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1768 target + 11) == -1) {
1769 error = got_error_from_errno("asprintf");
1770 free(remote_refname);
1773 error = got_ref_open(&target_ref, repo, remote_target, 0);
1775 free(remote_refname);
1776 free(remote_target);
1777 if (error->code == GOT_ERR_NOT_REF) {
1783 error = create_symref(remote_refname, target_ref,
1784 verbosity - 1, repo);
1785 free(remote_refname);
1786 free(remote_target);
1787 got_ref_close(target_ref);
1793 * We failed to set the HEAD reference. If we asked for
1794 * a set of wanted branches use the first of one of those
1795 * which could be fetched instead.
1797 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1798 const char *target = pe->path;
1799 struct got_reference *target_ref;
1801 error = got_ref_open(&target_ref, repo, target, 0);
1803 if (error->code == GOT_ERR_NOT_REF) {
1810 error = create_symref(GOT_REF_HEAD, target_ref,
1812 got_ref_close(target_ref);
1820 printf("Created %s repository '%s'\n",
1821 mirror_references ? "mirrored" : "cloned", repo_path);
1824 const struct got_error *pack_err =
1825 got_repo_pack_fds_close(pack_fds);
1830 if (kill(fetchpid, SIGTERM) == -1)
1831 error = got_error_from_errno("kill");
1832 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1833 error = got_error_from_errno("waitpid");
1835 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1836 error = got_error_from_errno("close");
1838 const struct got_error *close_err = got_repo_close(repo);
1842 TAILQ_FOREACH(pe, &refs, entry) {
1843 free((void *)pe->path);
1846 got_pathlist_free(&refs);
1847 TAILQ_FOREACH(pe, &symrefs, entry) {
1848 free((void *)pe->path);
1851 got_pathlist_free(&symrefs);
1852 got_pathlist_free(&wanted_branches);
1853 got_pathlist_free(&wanted_refs);
1860 free(default_destdir);
1865 static const struct got_error *
1866 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1867 int replace_tags, int verbosity, struct got_repository *repo)
1869 const struct got_error *err = NULL;
1870 char *new_id_str = NULL;
1871 struct got_object_id *old_id = NULL;
1873 err = got_object_id_str(&new_id_str, new_id);
1877 if (!replace_tags &&
1878 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1879 err = got_ref_resolve(&old_id, repo, ref);
1882 if (got_object_id_cmp(old_id, new_id) == 0)
1884 if (verbosity >= 0) {
1885 printf("Rejecting update of existing tag %s: %s\n",
1886 got_ref_get_name(ref), new_id_str);
1891 if (got_ref_is_symbolic(ref)) {
1892 if (verbosity >= 0) {
1893 printf("Replacing reference %s: %s\n",
1894 got_ref_get_name(ref),
1895 got_ref_get_symref_target(ref));
1897 err = got_ref_change_symref_to_ref(ref, new_id);
1900 err = got_ref_write(ref, repo);
1904 err = got_ref_resolve(&old_id, repo, ref);
1907 if (got_object_id_cmp(old_id, new_id) == 0)
1910 err = got_ref_change_ref(ref, new_id);
1913 err = got_ref_write(ref, repo);
1919 printf("Updated %s: %s\n", got_ref_get_name(ref),
1927 static const struct got_error *
1928 update_symref(const char *refname, struct got_reference *target_ref,
1929 int verbosity, struct got_repository *repo)
1931 const struct got_error *err = NULL, *unlock_err;
1932 struct got_reference *symref;
1933 int symref_is_locked = 0;
1935 err = got_ref_open(&symref, repo, refname, 1);
1937 if (err->code != GOT_ERR_NOT_REF)
1939 err = got_ref_alloc_symref(&symref, refname, target_ref);
1943 err = got_ref_write(symref, repo);
1948 printf("Created reference %s: %s\n",
1949 got_ref_get_name(symref),
1950 got_ref_get_symref_target(symref));
1952 symref_is_locked = 1;
1954 if (strcmp(got_ref_get_symref_target(symref),
1955 got_ref_get_name(target_ref)) == 0)
1958 err = got_ref_change_symref(symref,
1959 got_ref_get_name(target_ref));
1963 err = got_ref_write(symref, repo);
1968 printf("Updated %s: %s\n", got_ref_get_name(symref),
1969 got_ref_get_symref_target(symref));
1973 if (symref_is_locked) {
1974 unlock_err = got_ref_unlock(symref);
1975 if (unlock_err && err == NULL)
1978 got_ref_close(symref);
1985 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1986 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1987 "[remote-repository-name]\n",
1992 static const struct got_error *
1993 delete_missing_ref(struct got_reference *ref,
1994 int verbosity, struct got_repository *repo)
1996 const struct got_error *err = NULL;
1997 struct got_object_id *id = NULL;
1998 char *id_str = NULL;
2000 if (got_ref_is_symbolic(ref)) {
2001 err = got_ref_delete(ref, repo);
2004 if (verbosity >= 0) {
2005 printf("Deleted %s: %s\n",
2006 got_ref_get_name(ref),
2007 got_ref_get_symref_target(ref));
2010 err = got_ref_resolve(&id, repo, ref);
2013 err = got_object_id_str(&id_str, id);
2017 err = got_ref_delete(ref, repo);
2020 if (verbosity >= 0) {
2021 printf("Deleted %s: %s\n",
2022 got_ref_get_name(ref), id_str);
2031 static const struct got_error *
2032 delete_missing_refs(struct got_pathlist_head *their_refs,
2033 struct got_pathlist_head *their_symrefs,
2034 const struct got_remote_repo *remote,
2035 int verbosity, struct got_repository *repo)
2037 const struct got_error *err = NULL, *unlock_err;
2038 struct got_reflist_head my_refs;
2039 struct got_reflist_entry *re;
2040 struct got_pathlist_entry *pe;
2041 char *remote_namespace = NULL;
2042 char *local_refname = NULL;
2044 TAILQ_INIT(&my_refs);
2046 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2048 return got_error_from_errno("asprintf");
2050 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2054 TAILQ_FOREACH(re, &my_refs, entry) {
2055 const char *refname = got_ref_get_name(re->ref);
2056 const char *their_refname;
2058 if (remote->mirror_references) {
2059 their_refname = refname;
2061 if (strncmp(refname, remote_namespace,
2062 strlen(remote_namespace)) == 0) {
2063 if (strcmp(refname + strlen(remote_namespace),
2066 if (asprintf(&local_refname, "refs/heads/%s",
2067 refname + strlen(remote_namespace)) == -1) {
2068 err = got_error_from_errno("asprintf");
2071 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2074 their_refname = local_refname;
2077 TAILQ_FOREACH(pe, their_refs, entry) {
2078 if (strcmp(their_refname, pe->path) == 0)
2084 TAILQ_FOREACH(pe, their_symrefs, entry) {
2085 if (strcmp(their_refname, pe->path) == 0)
2091 err = delete_missing_ref(re->ref, verbosity, repo);
2095 if (local_refname) {
2096 struct got_reference *ref;
2097 err = got_ref_open(&ref, repo, local_refname, 1);
2099 if (err->code != GOT_ERR_NOT_REF)
2101 free(local_refname);
2102 local_refname = NULL;
2105 err = delete_missing_ref(ref, verbosity, repo);
2108 unlock_err = got_ref_unlock(ref);
2110 if (unlock_err && err == NULL) {
2115 free(local_refname);
2116 local_refname = NULL;
2120 free(remote_namespace);
2121 free(local_refname);
2125 static const struct got_error *
2126 update_wanted_ref(const char *refname, struct got_object_id *id,
2127 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2129 const struct got_error *err, *unlock_err;
2130 char *remote_refname;
2131 struct got_reference *ref;
2133 if (strncmp("refs/", refname, 5) == 0)
2136 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2137 remote_repo_name, refname) == -1)
2138 return got_error_from_errno("asprintf");
2140 err = got_ref_open(&ref, repo, remote_refname, 1);
2142 if (err->code != GOT_ERR_NOT_REF)
2144 err = create_ref(remote_refname, id, verbosity, repo);
2146 err = update_ref(ref, id, 0, verbosity, repo);
2147 unlock_err = got_ref_unlock(ref);
2148 if (unlock_err && err == NULL)
2153 free(remote_refname);
2157 static const struct got_error *
2158 delete_ref(struct got_repository *repo, struct got_reference *ref)
2160 const struct got_error *err = NULL;
2161 struct got_object_id *id = NULL;
2162 char *id_str = NULL;
2165 if (got_ref_is_symbolic(ref)) {
2166 target = got_ref_get_symref_target(ref);
2168 err = got_ref_resolve(&id, repo, ref);
2171 err = got_object_id_str(&id_str, id);
2177 err = got_ref_delete(ref, repo);
2181 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2188 static const struct got_error *
2189 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2191 const struct got_error *err = NULL;
2192 struct got_reflist_head refs;
2193 struct got_reflist_entry *re;
2198 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2199 err = got_error_from_errno("asprintf");
2202 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2206 TAILQ_FOREACH(re, &refs, entry)
2207 delete_ref(repo, re->ref);
2209 got_ref_list_free(&refs);
2213 static const struct got_error *
2214 cmd_fetch(int argc, char *argv[])
2216 const struct got_error *error = NULL, *unlock_err;
2217 char *cwd = NULL, *repo_path = NULL;
2218 const char *remote_name;
2219 char *proto = NULL, *host = NULL, *port = NULL;
2220 char *repo_name = NULL, *server_path = NULL;
2221 const struct got_remote_repo *remotes, *remote = NULL;
2223 char *id_str = NULL;
2224 struct got_repository *repo = NULL;
2225 struct got_worktree *worktree = NULL;
2226 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2227 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2228 struct got_pathlist_entry *pe;
2229 struct got_object_id *pack_hash = NULL;
2230 int i, ch, fetchfd = -1, fetchstatus;
2231 pid_t fetchpid = -1;
2232 struct got_fetch_progress_arg fpa;
2233 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2234 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2235 int *pack_fds = NULL;
2238 TAILQ_INIT(&symrefs);
2239 TAILQ_INIT(&wanted_branches);
2240 TAILQ_INIT(&wanted_refs);
2242 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2245 fetch_all_branches = 1;
2248 error = got_pathlist_append(&wanted_branches,
2260 repo_path = realpath(optarg, NULL);
2261 if (repo_path == NULL)
2262 return got_error_from_errno2("realpath",
2264 got_path_strip_trailing_slashes(repo_path);
2272 else if (verbosity < 3)
2279 error = got_pathlist_append(&wanted_refs,
2295 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2296 option_conflict('a', 'b');
2297 if (list_refs_only) {
2298 if (!TAILQ_EMPTY(&wanted_branches))
2299 option_conflict('l', 'b');
2300 if (fetch_all_branches)
2301 option_conflict('l', 'a');
2303 option_conflict('l', 'd');
2305 option_conflict('l', 'X');
2307 if (delete_remote) {
2308 if (fetch_all_branches)
2309 option_conflict('X', 'a');
2310 if (!TAILQ_EMPTY(&wanted_branches))
2311 option_conflict('X', 'b');
2313 option_conflict('X', 'd');
2315 option_conflict('X', 't');
2316 if (!TAILQ_EMPTY(&wanted_refs))
2317 option_conflict('X', 'R');
2322 errx(1, "-X option requires a remote name");
2323 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2324 } else if (argc == 1)
2325 remote_name = argv[0];
2329 cwd = getcwd(NULL, 0);
2331 error = got_error_from_errno("getcwd");
2335 error = got_repo_pack_fds_open(&pack_fds);
2339 if (repo_path == NULL) {
2340 error = got_worktree_open(&worktree, cwd);
2341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2347 strdup(got_worktree_get_repo_path(worktree));
2348 if (repo_path == NULL)
2349 error = got_error_from_errno("strdup");
2353 repo_path = strdup(cwd);
2354 if (repo_path == NULL) {
2355 error = got_error_from_errno("strdup");
2361 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2365 if (delete_remote) {
2366 error = delete_refs_for_remote(repo, remote_name);
2367 goto done; /* nothing else to do */
2371 worktree_conf = got_worktree_get_gotconfig(worktree);
2372 if (worktree_conf) {
2373 got_gotconfig_get_remotes(&nremotes, &remotes,
2375 for (i = 0; i < nremotes; i++) {
2376 if (strcmp(remotes[i].name, remote_name) == 0) {
2377 remote = &remotes[i];
2383 if (remote == NULL) {
2384 repo_conf = got_repo_get_gotconfig(repo);
2386 got_gotconfig_get_remotes(&nremotes, &remotes,
2388 for (i = 0; i < nremotes; i++) {
2389 if (strcmp(remotes[i].name, remote_name) == 0) {
2390 remote = &remotes[i];
2396 if (remote == NULL) {
2397 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2398 for (i = 0; i < nremotes; i++) {
2399 if (strcmp(remotes[i].name, remote_name) == 0) {
2400 remote = &remotes[i];
2405 if (remote == NULL) {
2406 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2410 if (TAILQ_EMPTY(&wanted_branches)) {
2411 if (!fetch_all_branches)
2412 fetch_all_branches = remote->fetch_all_branches;
2413 for (i = 0; i < remote->nfetch_branches; i++) {
2414 got_pathlist_append(&wanted_branches,
2415 remote->fetch_branches[i], NULL);
2418 if (TAILQ_EMPTY(&wanted_refs)) {
2419 for (i = 0; i < remote->nfetch_refs; i++) {
2420 got_pathlist_append(&wanted_refs,
2421 remote->fetch_refs[i], NULL);
2425 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2426 &repo_name, remote->fetch_url);
2430 if (strcmp(proto, "git") == 0) {
2432 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2433 "sendfd dns inet unveil", NULL) == -1)
2436 } else if (strcmp(proto, "git+ssh") == 0 ||
2437 strcmp(proto, "ssh") == 0) {
2439 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2440 "sendfd unveil", NULL) == -1)
2443 } else if (strcmp(proto, "http") == 0 ||
2444 strcmp(proto, "git+http") == 0) {
2445 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2448 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2452 error = got_dial_apply_unveil(proto);
2456 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2461 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2462 port ? ":" : "", port ? port : "");
2464 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2465 server_path, verbosity);
2469 fpa.last_scaled_size[0] = '\0';
2470 fpa.last_p_indexed = -1;
2471 fpa.last_p_resolved = -1;
2472 fpa.verbosity = verbosity;
2474 fpa.create_configs = 0;
2475 fpa.configs_created = 0;
2476 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2477 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2478 remote->mirror_references, fetch_all_branches, &wanted_branches,
2479 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2480 fetch_progress, &fpa);
2484 if (list_refs_only) {
2485 error = list_remote_refs(&symrefs, &refs);
2489 if (pack_hash == NULL) {
2491 printf("Already up-to-date\n");
2492 } else if (verbosity >= 0) {
2493 error = got_object_id_str(&id_str, pack_hash);
2496 printf("\nFetched %s.pack\n", id_str);
2501 /* Update references provided with the pack file. */
2502 TAILQ_FOREACH(pe, &refs, entry) {
2503 const char *refname = pe->path;
2504 struct got_object_id *id = pe->data;
2505 struct got_reference *ref;
2506 char *remote_refname;
2508 if (is_wanted_ref(&wanted_refs, refname) &&
2509 !remote->mirror_references) {
2510 error = update_wanted_ref(refname, id,
2511 remote->name, verbosity, repo);
2517 if (remote->mirror_references ||
2518 strncmp("refs/tags/", refname, 10) == 0) {
2519 error = got_ref_open(&ref, repo, refname, 1);
2521 if (error->code != GOT_ERR_NOT_REF)
2523 error = create_ref(refname, id, verbosity,
2528 error = update_ref(ref, id, replace_tags,
2530 unlock_err = got_ref_unlock(ref);
2531 if (unlock_err && error == NULL)
2537 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2538 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2539 remote_name, refname + 11) == -1) {
2540 error = got_error_from_errno("asprintf");
2544 error = got_ref_open(&ref, repo, remote_refname, 1);
2546 if (error->code != GOT_ERR_NOT_REF)
2548 error = create_ref(remote_refname, id,
2553 error = update_ref(ref, id, replace_tags,
2555 unlock_err = got_ref_unlock(ref);
2556 if (unlock_err && error == NULL)
2563 /* Also create a local branch if none exists yet. */
2564 error = got_ref_open(&ref, repo, refname, 1);
2566 if (error->code != GOT_ERR_NOT_REF)
2568 error = create_ref(refname, id, verbosity,
2573 unlock_err = got_ref_unlock(ref);
2574 if (unlock_err && error == NULL)
2581 error = delete_missing_refs(&refs, &symrefs, remote,
2587 if (!remote->mirror_references) {
2588 /* Update remote HEAD reference if the server provided one. */
2589 TAILQ_FOREACH(pe, &symrefs, entry) {
2590 struct got_reference *target_ref;
2591 const char *refname = pe->path;
2592 const char *target = pe->data;
2593 char *remote_refname = NULL, *remote_target = NULL;
2595 if (strcmp(refname, GOT_REF_HEAD) != 0)
2598 if (strncmp("refs/heads/", target, 11) != 0)
2601 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2602 remote->name, refname) == -1) {
2603 error = got_error_from_errno("asprintf");
2606 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2607 remote->name, target + 11) == -1) {
2608 error = got_error_from_errno("asprintf");
2609 free(remote_refname);
2613 error = got_ref_open(&target_ref, repo, remote_target,
2616 free(remote_refname);
2617 free(remote_target);
2618 if (error->code == GOT_ERR_NOT_REF) {
2624 error = update_symref(remote_refname, target_ref,
2626 free(remote_refname);
2627 free(remote_target);
2628 got_ref_close(target_ref);
2635 if (kill(fetchpid, SIGTERM) == -1)
2636 error = got_error_from_errno("kill");
2637 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2638 error = got_error_from_errno("waitpid");
2640 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2641 error = got_error_from_errno("close");
2643 const struct got_error *close_err = got_repo_close(repo);
2648 got_worktree_close(worktree);
2650 const struct got_error *pack_err =
2651 got_repo_pack_fds_close(pack_fds);
2655 TAILQ_FOREACH(pe, &refs, entry) {
2656 free((void *)pe->path);
2659 got_pathlist_free(&refs);
2660 TAILQ_FOREACH(pe, &symrefs, entry) {
2661 free((void *)pe->path);
2664 got_pathlist_free(&symrefs);
2665 got_pathlist_free(&wanted_branches);
2666 got_pathlist_free(&wanted_refs);
2681 usage_checkout(void)
2683 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2684 "[-p prefix] [-q] repository-path [worktree-path]\n",
2690 show_worktree_base_ref_warning(void)
2692 fprintf(stderr, "%s: warning: could not create a reference "
2693 "to the work tree's base commit; the commit could be "
2694 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2695 "repository writable and running 'got update' will prevent this\n",
2699 struct got_checkout_progress_arg {
2700 const char *worktree_path;
2701 int had_base_commit_ref_error;
2705 static const struct got_error *
2706 checkout_progress(void *arg, unsigned char status, const char *path)
2708 struct got_checkout_progress_arg *a = arg;
2710 /* Base commit bump happens silently. */
2711 if (status == GOT_STATUS_BUMP_BASE)
2714 if (status == GOT_STATUS_BASE_REF_ERR) {
2715 a->had_base_commit_ref_error = 1;
2719 while (path[0] == '/')
2722 if (a->verbosity >= 0)
2723 printf("%c %s/%s\n", status, a->worktree_path, path);
2728 static const struct got_error *
2729 check_cancelled(void *arg)
2731 if (sigint_received || sigpipe_received)
2732 return got_error(GOT_ERR_CANCELLED);
2736 static const struct got_error *
2737 check_linear_ancestry(struct got_object_id *commit_id,
2738 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2739 struct got_repository *repo)
2741 const struct got_error *err = NULL;
2742 struct got_object_id *yca_id;
2744 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2745 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2750 return got_error(GOT_ERR_ANCESTRY);
2753 * Require a straight line of history between the target commit
2754 * and the work tree's base commit.
2756 * Non-linear situations such as this require a rebase:
2758 * (commit) D F (base_commit)
2766 * 'got update' only handles linear cases:
2767 * Update forwards in time: A (base/yca) - B - C - D (commit)
2768 * Update backwards in time: D (base) - C - B - A (commit/yca)
2770 if (allow_forwards_in_time_only) {
2771 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2772 return got_error(GOT_ERR_ANCESTRY);
2773 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2774 got_object_id_cmp(base_commit_id, yca_id) != 0)
2775 return got_error(GOT_ERR_ANCESTRY);
2781 static const struct got_error *
2782 check_same_branch(struct got_object_id *commit_id,
2783 struct got_reference *head_ref, struct got_object_id *yca_id,
2784 struct got_repository *repo)
2786 const struct got_error *err = NULL;
2787 struct got_commit_graph *graph = NULL;
2788 struct got_object_id *head_commit_id = NULL;
2789 int is_same_branch = 0;
2791 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2795 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2799 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2804 err = got_commit_graph_open(&graph, "/", 1);
2808 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2809 check_cancelled, NULL);
2814 struct got_object_id *id;
2815 err = got_commit_graph_iter_next(&id, graph, repo,
2816 check_cancelled, NULL);
2818 if (err->code == GOT_ERR_ITER_COMPLETED)
2824 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2826 if (got_object_id_cmp(id, commit_id) == 0) {
2834 got_commit_graph_close(graph);
2835 free(head_commit_id);
2836 if (!err && !is_same_branch)
2837 err = got_error(GOT_ERR_ANCESTRY);
2841 static const struct got_error *
2842 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2844 static char msg[512];
2845 const char *branch_name;
2847 if (got_ref_is_symbolic(ref))
2848 branch_name = got_ref_get_symref_target(ref);
2850 branch_name = got_ref_get_name(ref);
2852 if (strncmp("refs/heads/", branch_name, 11) == 0)
2855 snprintf(msg, sizeof(msg),
2856 "target commit is not contained in branch '%s'; "
2857 "the branch to use must be specified with -b; "
2858 "if necessary a new branch can be created for "
2859 "this commit with 'got branch -c %s BRANCH_NAME'",
2860 branch_name, commit_id_str);
2862 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2865 static const struct got_error *
2866 cmd_checkout(int argc, char *argv[])
2868 const struct got_error *error = NULL;
2869 struct got_repository *repo = NULL;
2870 struct got_reference *head_ref = NULL, *ref = NULL;
2871 struct got_worktree *worktree = NULL;
2872 char *repo_path = NULL;
2873 char *worktree_path = NULL;
2874 const char *path_prefix = "";
2875 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2876 char *commit_id_str = NULL;
2877 struct got_object_id *commit_id = NULL;
2879 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2880 struct got_pathlist_head paths;
2881 struct got_checkout_progress_arg cpa;
2882 int *pack_fds = NULL;
2886 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2889 branch_name = optarg;
2892 commit_id_str = strdup(optarg);
2893 if (commit_id_str == NULL)
2894 return got_error_from_errno("strdup");
2900 path_prefix = optarg;
2915 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2916 "unveil", NULL) == -1)
2920 char *base, *dotgit;
2922 repo_path = realpath(argv[0], NULL);
2923 if (repo_path == NULL)
2924 return got_error_from_errno2("realpath", argv[0]);
2925 cwd = getcwd(NULL, 0);
2927 error = got_error_from_errno("getcwd");
2934 error = got_path_basename(&base, path);
2937 dotgit = strstr(base, ".git");
2940 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2941 error = got_error_from_errno("asprintf");
2946 } else if (argc == 2) {
2947 repo_path = realpath(argv[0], NULL);
2948 if (repo_path == NULL) {
2949 error = got_error_from_errno2("realpath", argv[0]);
2952 worktree_path = realpath(argv[1], NULL);
2953 if (worktree_path == NULL) {
2954 if (errno != ENOENT) {
2955 error = got_error_from_errno2("realpath",
2959 worktree_path = strdup(argv[1]);
2960 if (worktree_path == NULL) {
2961 error = got_error_from_errno("strdup");
2968 got_path_strip_trailing_slashes(repo_path);
2969 got_path_strip_trailing_slashes(worktree_path);
2971 error = got_repo_pack_fds_open(&pack_fds);
2975 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2979 /* Pre-create work tree path for unveil(2) */
2980 error = got_path_mkdir(worktree_path);
2982 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2983 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2985 if (!allow_nonempty &&
2986 !got_path_dir_is_empty(worktree_path)) {
2987 error = got_error_path(worktree_path,
2988 GOT_ERR_DIR_NOT_EMPTY);
2993 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2997 error = got_ref_open(&head_ref, repo, branch_name, 0);
3001 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3002 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3005 error = got_worktree_open(&worktree, worktree_path);
3009 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3013 if (!same_path_prefix) {
3014 error = got_error(GOT_ERR_PATH_PREFIX);
3018 if (commit_id_str) {
3019 struct got_reflist_head refs;
3021 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3025 error = got_repo_match_object_id(&commit_id, NULL,
3026 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3027 got_ref_list_free(&refs);
3030 error = check_linear_ancestry(commit_id,
3031 got_worktree_get_base_commit_id(worktree), 0, repo);
3032 if (error != NULL) {
3033 if (error->code == GOT_ERR_ANCESTRY) {
3034 error = checkout_ancestry_error(
3035 head_ref, commit_id_str);
3039 error = check_same_branch(commit_id, head_ref, NULL, repo);
3041 if (error->code == GOT_ERR_ANCESTRY) {
3042 error = checkout_ancestry_error(
3043 head_ref, commit_id_str);
3047 error = got_worktree_set_base_commit_id(worktree, repo,
3051 /* Expand potentially abbreviated commit ID string. */
3052 free(commit_id_str);
3053 error = got_object_id_str(&commit_id_str, commit_id);
3057 commit_id = got_object_id_dup(
3058 got_worktree_get_base_commit_id(worktree));
3059 if (commit_id == NULL) {
3060 error = got_error_from_errno("got_object_id_dup");
3063 error = got_object_id_str(&commit_id_str, commit_id);
3068 error = got_pathlist_append(&paths, "", NULL);
3071 cpa.worktree_path = worktree_path;
3072 cpa.had_base_commit_ref_error = 0;
3073 cpa.verbosity = verbosity;
3074 error = got_worktree_checkout_files(worktree, &paths, repo,
3075 checkout_progress, &cpa, check_cancelled, NULL);
3079 if (got_ref_is_symbolic(head_ref)) {
3080 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3083 refname = got_ref_get_name(ref);
3085 refname = got_ref_get_name(head_ref);
3086 printf("Checked out %s: %s\n", refname, commit_id_str);
3087 printf("Now shut up and hack\n");
3088 if (cpa.had_base_commit_ref_error)
3089 show_worktree_base_ref_warning();
3092 const struct got_error *pack_err =
3093 got_repo_pack_fds_close(pack_fds);
3098 got_ref_close(head_ref);
3101 got_pathlist_free(&paths);
3102 free(commit_id_str);
3105 free(worktree_path);
3110 struct got_update_progress_arg {
3122 print_update_progress_stats(struct got_update_progress_arg *upa)
3124 if (!upa->did_something)
3127 if (upa->conflicts > 0)
3128 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3129 if (upa->obstructed > 0)
3130 printf("File paths obstructed by a non-regular file: %d\n",
3132 if (upa->not_updated > 0)
3133 printf("Files not updated because of existing merge "
3134 "conflicts: %d\n", upa->not_updated);
3138 * The meaning of some status codes differs between merge-style operations and
3139 * update operations. For example, the ! status code means "file was missing"
3140 * if changes were merged into the work tree, and "missing file was restored"
3141 * if the work tree was updated. This function should be used by any operation
3142 * which merges changes into the work tree without updating the work tree.
3145 print_merge_progress_stats(struct got_update_progress_arg *upa)
3147 if (!upa->did_something)
3150 if (upa->conflicts > 0)
3151 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3152 if (upa->obstructed > 0)
3153 printf("File paths obstructed by a non-regular file: %d\n",
3155 if (upa->missing > 0)
3156 printf("Files which had incoming changes but could not be "
3157 "found in the work tree: %d\n", upa->missing);
3158 if (upa->not_deleted > 0)
3159 printf("Files not deleted due to differences in deleted "
3160 "content: %d\n", upa->not_deleted);
3161 if (upa->unversioned > 0)
3162 printf("Files not merged because an unversioned file was "
3163 "found in the work tree: %d\n", upa->unversioned);
3169 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3175 static const struct got_error *
3176 update_progress(void *arg, unsigned char status, const char *path)
3178 struct got_update_progress_arg *upa = arg;
3180 if (status == GOT_STATUS_EXISTS ||
3181 status == GOT_STATUS_BASE_REF_ERR)
3184 upa->did_something = 1;
3186 /* Base commit bump happens silently. */
3187 if (status == GOT_STATUS_BUMP_BASE)
3190 if (status == GOT_STATUS_CONFLICT)
3192 if (status == GOT_STATUS_OBSTRUCTED)
3194 if (status == GOT_STATUS_CANNOT_UPDATE)
3196 if (status == GOT_STATUS_MISSING)
3198 if (status == GOT_STATUS_CANNOT_DELETE)
3200 if (status == GOT_STATUS_UNVERSIONED)
3203 while (path[0] == '/')
3205 if (upa->verbosity >= 0)
3206 printf("%c %s\n", status, path);
3211 static const struct got_error *
3212 switch_head_ref(struct got_reference *head_ref,
3213 struct got_object_id *commit_id, struct got_worktree *worktree,
3214 struct got_repository *repo)
3216 const struct got_error *err = NULL;
3218 int ref_has_moved = 0;
3220 /* Trivial case: switching between two different references. */
3221 if (strcmp(got_ref_get_name(head_ref),
3222 got_worktree_get_head_ref_name(worktree)) != 0) {
3223 printf("Switching work tree from %s to %s\n",
3224 got_worktree_get_head_ref_name(worktree),
3225 got_ref_get_name(head_ref));
3226 return got_worktree_set_head_ref(worktree, head_ref);
3229 err = check_linear_ancestry(commit_id,
3230 got_worktree_get_base_commit_id(worktree), 0, repo);
3232 if (err->code != GOT_ERR_ANCESTRY)
3239 /* Switching to a rebased branch with the same reference name. */
3240 err = got_object_id_str(&base_id_str,
3241 got_worktree_get_base_commit_id(worktree));
3244 printf("Reference %s now points at a different branch\n",
3245 got_worktree_get_head_ref_name(worktree));
3246 printf("Switching work tree from %s to %s\n", base_id_str,
3247 got_worktree_get_head_ref_name(worktree));
3251 static const struct got_error *
3252 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3254 const struct got_error *err;
3257 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3261 return got_error(GOT_ERR_REBASING);
3263 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3267 return got_error(GOT_ERR_HISTEDIT_BUSY);
3272 static const struct got_error *
3273 check_merge_in_progress(struct got_worktree *worktree,
3274 struct got_repository *repo)
3276 const struct got_error *err;
3279 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3283 return got_error(GOT_ERR_MERGE_BUSY);
3288 static const struct got_error *
3289 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3290 char *argv[], struct got_worktree *worktree)
3292 const struct got_error *err = NULL;
3294 struct got_pathlist_entry *new;
3300 return got_error_from_errno("strdup");
3301 return got_pathlist_append(paths, path, NULL);
3304 for (i = 0; i < argc; i++) {
3305 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3308 err = got_pathlist_insert(&new, paths, path, NULL);
3309 if (err || new == NULL /* duplicate */) {
3319 static const struct got_error *
3320 wrap_not_worktree_error(const struct got_error *orig_err,
3321 const char *cmdname, const char *path)
3323 const struct got_error *err;
3324 struct got_repository *repo;
3325 static char msg[512];
3326 int *pack_fds = NULL;
3328 err = got_repo_pack_fds_open(&pack_fds);
3332 err = got_repo_open(&repo, path, NULL, pack_fds);
3336 snprintf(msg, sizeof(msg),
3337 "'got %s' needs a work tree in addition to a git repository\n"
3338 "Work trees can be checked out from this Git repository with "
3340 "The got(1) manual page contains more information.", cmdname);
3341 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3342 got_repo_close(repo);
3344 const struct got_error *pack_err =
3345 got_repo_pack_fds_close(pack_fds);
3352 static const struct got_error *
3353 cmd_update(int argc, char *argv[])
3355 const struct got_error *error = NULL;
3356 struct got_repository *repo = NULL;
3357 struct got_worktree *worktree = NULL;
3358 char *worktree_path = NULL;
3359 struct got_object_id *commit_id = NULL;
3360 char *commit_id_str = NULL;
3361 const char *branch_name = NULL;
3362 struct got_reference *head_ref = NULL;
3363 struct got_pathlist_head paths;
3364 struct got_pathlist_entry *pe;
3365 int ch, verbosity = 0;
3366 struct got_update_progress_arg upa;
3367 int *pack_fds = NULL;
3371 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3374 branch_name = optarg;
3377 commit_id_str = strdup(optarg);
3378 if (commit_id_str == NULL)
3379 return got_error_from_errno("strdup");
3394 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3395 "unveil", NULL) == -1)
3398 worktree_path = getcwd(NULL, 0);
3399 if (worktree_path == NULL) {
3400 error = got_error_from_errno("getcwd");
3404 error = got_repo_pack_fds_open(&pack_fds);
3408 error = got_worktree_open(&worktree, worktree_path);
3410 if (error->code == GOT_ERR_NOT_WORKTREE)
3411 error = wrap_not_worktree_error(error, "update",
3416 error = check_rebase_or_histedit_in_progress(worktree);
3420 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3425 error = apply_unveil(got_repo_get_path(repo), 0,
3426 got_worktree_get_root_path(worktree));
3430 error = check_merge_in_progress(worktree, repo);
3434 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3438 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3439 got_worktree_get_head_ref_name(worktree), 0);
3442 if (commit_id_str == NULL) {
3443 error = got_ref_resolve(&commit_id, repo, head_ref);
3446 error = got_object_id_str(&commit_id_str, commit_id);
3450 struct got_reflist_head refs;
3452 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3456 error = got_repo_match_object_id(&commit_id, NULL,
3457 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3458 got_ref_list_free(&refs);
3459 free(commit_id_str);
3460 commit_id_str = NULL;
3463 error = got_object_id_str(&commit_id_str, commit_id);
3469 struct got_object_id *head_commit_id;
3470 TAILQ_FOREACH(pe, &paths, entry) {
3471 if (pe->path_len == 0)
3473 error = got_error_msg(GOT_ERR_BAD_PATH,
3474 "switching between branches requires that "
3475 "the entire work tree gets updated");
3478 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3481 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3483 free(head_commit_id);
3486 error = check_same_branch(commit_id, head_ref, NULL, repo);
3489 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3493 error = check_linear_ancestry(commit_id,
3494 got_worktree_get_base_commit_id(worktree), 0, repo);
3495 if (error != NULL) {
3496 if (error->code == GOT_ERR_ANCESTRY)
3497 error = got_error(GOT_ERR_BRANCH_MOVED);
3500 error = check_same_branch(commit_id, head_ref, NULL, repo);
3505 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3507 error = got_worktree_set_base_commit_id(worktree, repo,
3513 memset(&upa, 0, sizeof(upa));
3514 upa.verbosity = verbosity;
3515 error = got_worktree_checkout_files(worktree, &paths, repo,
3516 update_progress, &upa, check_cancelled, NULL);
3520 if (upa.did_something) {
3521 printf("Updated to %s: %s\n",
3522 got_worktree_get_head_ref_name(worktree), commit_id_str);
3524 printf("Already up-to-date\n");
3526 print_update_progress_stats(&upa);
3529 const struct got_error *pack_err =
3530 got_repo_pack_fds_close(pack_fds);
3534 free(worktree_path);
3535 TAILQ_FOREACH(pe, &paths, entry)
3536 free((char *)pe->path);
3537 got_pathlist_free(&paths);
3539 free(commit_id_str);
3543 static const struct got_error *
3544 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3545 const char *path, int diff_context, int ignore_whitespace,
3546 int force_text_diff, struct got_repository *repo, FILE *outfile)
3548 const struct got_error *err = NULL;
3549 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3550 FILE *f1 = NULL, *f2 = NULL;
3551 int fd1 = -1, fd2 = -1;
3553 fd1 = got_opentempfd();
3555 return got_error_from_errno("got_opentempfd");
3556 fd2 = got_opentempfd();
3558 err = got_error_from_errno("got_opentempfd");
3563 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3569 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3573 f1 = got_opentemp();
3575 err = got_error_from_errno("got_opentemp");
3578 f2 = got_opentemp();
3580 err = got_error_from_errno("got_opentemp");
3584 while (path[0] == '/')
3586 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3587 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3588 force_text_diff, outfile);
3590 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3591 err = got_error_from_errno("close");
3593 got_object_blob_close(blob1);
3594 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3595 err = got_error_from_errno("close");
3596 got_object_blob_close(blob2);
3597 if (f1 && fclose(f1) == EOF && err == NULL)
3598 err = got_error_from_errno("fclose");
3599 if (f2 && fclose(f2) == EOF && err == NULL)
3600 err = got_error_from_errno("fclose");
3604 static const struct got_error *
3605 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3606 const char *path, int diff_context, int ignore_whitespace,
3607 int force_text_diff, struct got_repository *repo, FILE *outfile)
3609 const struct got_error *err = NULL;
3610 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3611 struct got_diff_blob_output_unidiff_arg arg;
3612 FILE *f1 = NULL, *f2 = NULL;
3613 int fd1 = -1, fd2 = -1;
3616 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3619 fd1 = got_opentempfd();
3621 err = got_error_from_errno("got_opentempfd");
3626 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3630 f1 = got_opentemp();
3632 err = got_error_from_errno("got_opentemp");
3636 f2 = got_opentemp();
3638 err = got_error_from_errno("got_opentemp");
3641 fd2 = got_opentempfd();
3643 err = got_error_from_errno("got_opentempfd");
3646 arg.diff_context = diff_context;
3647 arg.ignore_whitespace = ignore_whitespace;
3648 arg.force_text_diff = force_text_diff;
3649 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3650 arg.outfile = outfile;
3651 arg.line_offsets = NULL;
3653 while (path[0] == '/')
3655 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3656 got_diff_blob_output_unidiff, &arg, 1);
3659 got_object_tree_close(tree1);
3661 got_object_tree_close(tree2);
3662 if (f1 && fclose(f1) == EOF && err == NULL)
3663 err = got_error_from_errno("fclose");
3664 if (f2 && fclose(f2) == EOF && err == NULL)
3665 err = got_error_from_errno("fclose");
3666 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3667 err = got_error_from_errno("close");
3668 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3669 err = got_error_from_errno("close");
3673 static const struct got_error *
3674 get_changed_paths(struct got_pathlist_head *paths,
3675 struct got_commit_object *commit, struct got_repository *repo)
3677 const struct got_error *err = NULL;
3678 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3679 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3680 struct got_object_qid *qid;
3682 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3684 struct got_commit_object *pcommit;
3685 err = got_object_open_as_commit(&pcommit, repo,
3690 tree_id1 = got_object_id_dup(
3691 got_object_commit_get_tree_id(pcommit));
3692 if (tree_id1 == NULL) {
3693 got_object_commit_close(pcommit);
3694 return got_error_from_errno("got_object_id_dup");
3696 got_object_commit_close(pcommit);
3701 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3706 tree_id2 = got_object_commit_get_tree_id(commit);
3707 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3711 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3712 got_diff_tree_collect_changed_paths, paths, 0);
3715 got_object_tree_close(tree1);
3717 got_object_tree_close(tree2);
3722 static const struct got_error *
3723 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3724 const char *path, int diff_context, struct got_repository *repo,
3727 const struct got_error *err = NULL;
3728 struct got_commit_object *pcommit = NULL;
3729 char *id_str1 = NULL, *id_str2 = NULL;
3730 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3731 struct got_object_qid *qid;
3733 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3735 err = got_object_open_as_commit(&pcommit, repo,
3739 err = got_object_id_str(&id_str1, &qid->id);
3744 err = got_object_id_str(&id_str2, id);
3748 if (path && path[0] != '\0') {
3750 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3754 err = got_object_id_by_path(&obj_id1, repo,
3757 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3763 err = got_object_get_type(&obj_type, repo, obj_id2);
3769 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3770 fprintf(outfile, "commit - %s\n",
3771 id_str1 ? id_str1 : "/dev/null");
3772 fprintf(outfile, "commit + %s\n", id_str2);
3774 case GOT_OBJ_TYPE_BLOB:
3775 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3776 0, 0, repo, outfile);
3778 case GOT_OBJ_TYPE_TREE:
3779 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3780 0, 0, repo, outfile);
3783 err = got_error(GOT_ERR_OBJ_TYPE);
3789 obj_id2 = got_object_commit_get_tree_id(commit);
3791 obj_id1 = got_object_commit_get_tree_id(pcommit);
3793 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3794 fprintf(outfile, "commit - %s\n",
3795 id_str1 ? id_str1 : "/dev/null");
3796 fprintf(outfile, "commit + %s\n", id_str2);
3797 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3804 got_object_commit_close(pcommit);
3809 get_datestr(time_t *time, char *datebuf)
3811 struct tm mytm, *tm;
3814 tm = gmtime_r(time, &mytm);
3817 s = asctime_r(tm, datebuf);
3820 p = strchr(s, '\n');
3826 static const struct got_error *
3827 match_commit(int *have_match, struct got_object_id *id,
3828 struct got_commit_object *commit, regex_t *regex)
3830 const struct got_error *err = NULL;
3831 regmatch_t regmatch;
3832 char *id_str = NULL, *logmsg = NULL;
3836 err = got_object_id_str(&id_str, id);
3840 err = got_object_commit_get_logmsg(&logmsg, commit);
3844 if (regexec(regex, got_object_commit_get_author(commit), 1,
3845 ®match, 0) == 0 ||
3846 regexec(regex, got_object_commit_get_committer(commit), 1,
3847 ®match, 0) == 0 ||
3848 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3849 regexec(regex, logmsg, 1, ®match, 0) == 0)
3858 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3861 regmatch_t regmatch;
3862 struct got_pathlist_entry *pe;
3866 TAILQ_FOREACH(pe, changed_paths, entry) {
3867 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3874 static const struct got_error *
3875 match_patch(int *have_match, struct got_commit_object *commit,
3876 struct got_object_id *id, const char *path, int diff_context,
3877 struct got_repository *repo, regex_t *regex, FILE *f)
3879 const struct got_error *err = NULL;
3881 size_t linesize = 0;
3883 regmatch_t regmatch;
3887 err = got_opentemp_truncate(f);
3891 err = print_patch(commit, id, path, diff_context, repo, f);
3895 if (fseeko(f, 0L, SEEK_SET) == -1) {
3896 err = got_error_from_errno("fseeko");
3900 while ((linelen = getline(&line, &linesize, f)) != -1) {
3901 if (regexec(regex, line, 1, ®match, 0) == 0) {
3911 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3913 static const struct got_error*
3914 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3915 struct got_object_id *id, struct got_repository *repo,
3918 static const struct got_error *err = NULL;
3919 struct got_reflist_entry *re;
3925 TAILQ_FOREACH(re, refs, entry) {
3926 struct got_tag_object *tag = NULL;
3927 struct got_object_id *ref_id;
3930 name = got_ref_get_name(re->ref);
3931 if (strcmp(name, GOT_REF_HEAD) == 0)
3933 if (strncmp(name, "refs/", 5) == 0)
3935 if (strncmp(name, "got/", 4) == 0)
3937 if (strncmp(name, "heads/", 6) == 0)
3939 if (strncmp(name, "remotes/", 8) == 0) {
3943 s = strstr(name, "/" GOT_REF_HEAD);
3944 if (s != NULL && s[strlen(s)] == '\0')
3947 err = got_ref_resolve(&ref_id, repo, re->ref);
3950 if (strncmp(name, "tags/", 5) == 0) {
3951 err = got_object_open_as_tag(&tag, repo, ref_id);
3953 if (err->code != GOT_ERR_OBJ_TYPE) {
3957 /* Ref points at something other than a tag. */
3962 cmp = got_object_id_cmp(tag ?
3963 got_object_tag_get_object_id(tag) : ref_id, id);
3966 got_object_tag_close(tag);
3970 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3971 s ? ", " : "", name) == -1) {
3972 err = got_error_from_errno("asprintf");
3983 static const struct got_error *
3984 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3985 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3987 const struct got_error *err = NULL;
3988 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3989 char *comma, *s, *nl;
3990 struct got_reflist_head *refs;
3991 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3993 time_t committer_time;
3995 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3997 err = build_refs_str(&ref_str, refs, id, repo, 1);
4001 /* Display the first matching ref only. */
4002 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4006 if (ref_str == NULL) {
4007 err = got_object_id_str(&id_str, id);
4012 committer_time = got_object_commit_get_committer_time(commit);
4013 if (gmtime_r(&committer_time, &tm) == NULL) {
4014 err = got_error_from_errno("gmtime_r");
4017 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4018 err = got_error(GOT_ERR_NO_SPACE);
4022 err = got_object_commit_get_logmsg(&logmsg0, commit);
4027 while (isspace((unsigned char)s[0]))
4030 nl = strchr(s, '\n');
4036 printf("%s%-7s %s\n", datebuf, ref_str, s);
4038 printf("%s%.7s %s\n", datebuf, id_str, s);
4040 if (fflush(stdout) != 0 && err == NULL)
4041 err = got_error_from_errno("fflush");
4049 static const struct got_error *
4050 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4051 struct got_repository *repo, const char *path,
4052 struct got_pathlist_head *changed_paths, int show_patch,
4053 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4054 const char *custom_refs_str)
4056 const struct got_error *err = NULL;
4057 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4059 time_t committer_time;
4060 const char *author, *committer;
4061 char *refs_str = NULL;
4063 err = got_object_id_str(&id_str, id);
4067 if (custom_refs_str == NULL) {
4068 struct got_reflist_head *refs;
4069 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4071 err = build_refs_str(&refs_str, refs, id, repo, 0);
4077 printf(GOT_COMMIT_SEP_STR);
4078 if (custom_refs_str)
4079 printf("commit %s (%s)\n", id_str, custom_refs_str);
4081 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4082 refs_str ? refs_str : "", refs_str ? ")" : "");
4087 printf("from: %s\n", got_object_commit_get_author(commit));
4088 committer_time = got_object_commit_get_committer_time(commit);
4089 datestr = get_datestr(&committer_time, datebuf);
4091 printf("date: %s UTC\n", datestr);
4092 author = got_object_commit_get_author(commit);
4093 committer = got_object_commit_get_committer(commit);
4094 if (strcmp(author, committer) != 0)
4095 printf("via: %s\n", committer);
4096 if (got_object_commit_get_nparents(commit) > 1) {
4097 const struct got_object_id_queue *parent_ids;
4098 struct got_object_qid *qid;
4100 parent_ids = got_object_commit_get_parent_ids(commit);
4101 STAILQ_FOREACH(qid, parent_ids, entry) {
4102 err = got_object_id_str(&id_str, &qid->id);
4105 printf("parent %d: %s\n", n++, id_str);
4111 err = got_object_commit_get_logmsg(&logmsg0, commit);
4117 line = strsep(&logmsg, "\n");
4119 printf(" %s\n", line);
4123 if (changed_paths) {
4124 struct got_pathlist_entry *pe;
4125 TAILQ_FOREACH(pe, changed_paths, entry) {
4126 struct got_diff_changed_path *cp = pe->data;
4127 printf(" %c %s\n", cp->status, pe->path);
4132 err = print_patch(commit, id, path, diff_context, repo, stdout);
4137 if (fflush(stdout) != 0 && err == NULL)
4138 err = got_error_from_errno("fflush");
4145 static const struct got_error *
4146 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4147 struct got_repository *repo, const char *path, int show_changed_paths,
4148 int show_patch, const char *search_pattern, int diff_context, int limit,
4149 int log_branches, int reverse_display_order,
4150 struct got_reflist_object_id_map *refs_idmap, int one_line,
4153 const struct got_error *err;
4154 struct got_commit_graph *graph;
4157 struct got_object_id_queue reversed_commits;
4158 struct got_object_qid *qid;
4159 struct got_commit_object *commit;
4160 struct got_pathlist_head changed_paths;
4161 struct got_pathlist_entry *pe;
4163 STAILQ_INIT(&reversed_commits);
4164 TAILQ_INIT(&changed_paths);
4166 if (search_pattern && regcomp(®ex, search_pattern,
4167 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4168 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4170 err = got_commit_graph_open(&graph, path, !log_branches);
4173 err = got_commit_graph_iter_start(graph, root_id, repo,
4174 check_cancelled, NULL);
4178 struct got_object_id *id;
4180 if (sigint_received || sigpipe_received)
4183 err = got_commit_graph_iter_next(&id, graph, repo,
4184 check_cancelled, NULL);
4186 if (err->code == GOT_ERR_ITER_COMPLETED)
4193 err = got_object_open_as_commit(&commit, repo, id);
4197 if (show_changed_paths && !reverse_display_order) {
4198 err = get_changed_paths(&changed_paths, commit, repo);
4203 if (search_pattern) {
4204 err = match_commit(&have_match, id, commit, ®ex);
4206 got_object_commit_close(commit);
4209 if (have_match == 0 && show_changed_paths)
4210 match_changed_paths(&have_match,
4211 &changed_paths, ®ex);
4212 if (have_match == 0 && show_patch) {
4213 err = match_patch(&have_match, commit, id,
4214 path, diff_context, repo, ®ex,
4219 if (have_match == 0) {
4220 got_object_commit_close(commit);
4221 TAILQ_FOREACH(pe, &changed_paths, entry) {
4222 free((char *)pe->path);
4225 got_pathlist_free(&changed_paths);
4230 if (reverse_display_order) {
4231 err = got_object_qid_alloc(&qid, id);
4234 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4235 got_object_commit_close(commit);
4238 err = print_commit_oneline(commit, id,
4241 err = print_commit(commit, id, repo, path,
4242 show_changed_paths ? &changed_paths : NULL,
4243 show_patch, diff_context, refs_idmap, NULL);
4244 got_object_commit_close(commit);
4248 if ((limit && --limit == 0) ||
4249 (end_id && got_object_id_cmp(id, end_id) == 0))
4252 TAILQ_FOREACH(pe, &changed_paths, entry) {
4253 free((char *)pe->path);
4256 got_pathlist_free(&changed_paths);
4258 if (reverse_display_order) {
4259 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4260 err = got_object_open_as_commit(&commit, repo,
4264 if (show_changed_paths) {
4265 err = get_changed_paths(&changed_paths,
4271 err = print_commit_oneline(commit, &qid->id,
4274 err = print_commit(commit, &qid->id, repo, path,
4275 show_changed_paths ? &changed_paths : NULL,
4276 show_patch, diff_context, refs_idmap, NULL);
4277 got_object_commit_close(commit);
4280 TAILQ_FOREACH(pe, &changed_paths, entry) {
4281 free((char *)pe->path);
4284 got_pathlist_free(&changed_paths);
4288 while (!STAILQ_EMPTY(&reversed_commits)) {
4289 qid = STAILQ_FIRST(&reversed_commits);
4290 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4291 got_object_qid_free(qid);
4293 TAILQ_FOREACH(pe, &changed_paths, entry) {
4294 free((char *)pe->path);
4297 got_pathlist_free(&changed_paths);
4300 got_commit_graph_close(graph);
4307 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4308 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4309 "[-r repository-path] [-R] [path]\n", getprogname());
4314 get_default_log_limit(void)
4316 const char *got_default_log_limit;
4320 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4321 if (got_default_log_limit == NULL)
4323 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4329 static const struct got_error *
4330 cmd_log(int argc, char *argv[])
4332 const struct got_error *error;
4333 struct got_repository *repo = NULL;
4334 struct got_worktree *worktree = NULL;
4335 struct got_object_id *start_id = NULL, *end_id = NULL;
4336 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4337 const char *start_commit = NULL, *end_commit = NULL;
4338 const char *search_pattern = NULL;
4339 int diff_context = -1, ch;
4340 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4341 int reverse_display_order = 0, one_line = 0;
4343 struct got_reflist_head refs;
4344 struct got_reflist_object_id_map *refs_idmap = NULL;
4345 FILE *tmpfile = NULL;
4346 int *pack_fds = NULL;
4351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4357 limit = get_default_log_limit();
4359 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4365 show_changed_paths = 1;
4368 start_commit = optarg;
4371 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4374 errx(1, "number of context lines is %s: %s",
4378 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4380 errx(1, "number of commits is %s: %s",
4387 repo_path = realpath(optarg, NULL);
4388 if (repo_path == NULL)
4389 return got_error_from_errno2("realpath",
4391 got_path_strip_trailing_slashes(repo_path);
4394 reverse_display_order = 1;
4400 search_pattern = optarg;
4403 end_commit = optarg;
4414 if (diff_context == -1)
4416 else if (!show_patch)
4417 errx(1, "-C requires -p");
4419 if (one_line && (show_patch || show_changed_paths))
4420 errx(1, "cannot use -s with -p or -P");
4422 cwd = getcwd(NULL, 0);
4424 error = got_error_from_errno("getcwd");
4428 error = got_repo_pack_fds_open(&pack_fds);
4432 if (repo_path == NULL) {
4433 error = got_worktree_open(&worktree, cwd);
4434 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4441 error = got_worktree_resolve_path(&path, worktree,
4446 path = strdup(argv[0]);
4448 error = got_error_from_errno("strdup");
4452 } else if (argc != 0)
4455 if (repo_path == NULL) {
4456 repo_path = worktree ?
4457 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4459 if (repo_path == NULL) {
4460 error = got_error_from_errno("strdup");
4464 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4468 error = apply_unveil(got_repo_get_path(repo), 1,
4469 worktree ? got_worktree_get_root_path(worktree) : NULL);
4473 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4477 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4481 if (start_commit == NULL) {
4482 struct got_reference *head_ref;
4483 struct got_commit_object *commit = NULL;
4484 error = got_ref_open(&head_ref, repo,
4485 worktree ? got_worktree_get_head_ref_name(worktree)
4489 error = got_ref_resolve(&start_id, repo, head_ref);
4490 got_ref_close(head_ref);
4493 error = got_object_open_as_commit(&commit, repo,
4497 got_object_commit_close(commit);
4499 error = got_repo_match_object_id(&start_id, NULL,
4500 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4504 if (end_commit != NULL) {
4505 error = got_repo_match_object_id(&end_id, NULL,
4506 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4513 * If a path was specified on the command line it was resolved
4514 * to a path in the work tree above. Prepend the work tree's
4515 * path prefix to obtain the corresponding in-repository path.
4519 prefix = got_worktree_get_path_prefix(worktree);
4520 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4521 (path[0] != '\0') ? "/" : "", path) == -1) {
4522 error = got_error_from_errno("asprintf");
4527 error = got_repo_map_path(&in_repo_path, repo,
4533 path = in_repo_path;
4537 /* Release work tree lock. */
4538 got_worktree_close(worktree);
4542 if (search_pattern && show_patch) {
4543 tmpfile = got_opentemp();
4544 if (tmpfile == NULL) {
4545 error = got_error_from_errno("got_opentemp");
4550 error = print_commits(start_id, end_id, repo, path ? path : "",
4551 show_changed_paths, show_patch, search_pattern, diff_context,
4552 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4559 got_worktree_close(worktree);
4561 const struct got_error *close_err = got_repo_close(repo);
4566 const struct got_error *pack_err =
4567 got_repo_pack_fds_close(pack_fds);
4572 got_reflist_object_id_map_free(refs_idmap);
4573 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4574 error = got_error_from_errno("fclose");
4575 got_ref_list_free(&refs);
4582 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4583 "[-r repository-path] [-s] [-w] [-P] "
4584 "[object1 object2 | path ...]\n", getprogname());
4588 struct print_diff_arg {
4589 struct got_repository *repo;
4590 struct got_worktree *worktree;
4595 enum got_diff_algorithm diff_algo;
4596 int ignore_whitespace;
4597 int force_text_diff;
4603 * Create a file which contains the target path of a symlink so we can feed
4604 * it as content to the diff engine.
4606 static const struct got_error *
4607 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4608 const char *abspath)
4610 const struct got_error *err = NULL;
4611 char target_path[PATH_MAX];
4612 ssize_t target_len, outlen;
4617 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4618 if (target_len == -1)
4619 return got_error_from_errno2("readlinkat", abspath);
4621 target_len = readlink(abspath, target_path, PATH_MAX);
4622 if (target_len == -1)
4623 return got_error_from_errno2("readlink", abspath);
4626 *fd = got_opentempfd();
4628 return got_error_from_errno("got_opentempfd");
4630 outlen = write(*fd, target_path, target_len);
4632 err = got_error_from_errno("got_opentempfd");
4636 if (lseek(*fd, 0, SEEK_SET) == -1) {
4637 err = got_error_from_errno2("lseek", abspath);
4648 static const struct got_error *
4649 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4650 const char *path, struct got_object_id *blob_id,
4651 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4652 int dirfd, const char *de_name)
4654 struct print_diff_arg *a = arg;
4655 const struct got_error *err = NULL;
4656 struct got_blob_object *blob1 = NULL;
4657 int fd = -1, fd1 = -1, fd2 = -1;
4659 char *abspath = NULL, *label1 = NULL;
4664 if (a->diff_staged) {
4665 if (staged_status != GOT_STATUS_MODIFY &&
4666 staged_status != GOT_STATUS_ADD &&
4667 staged_status != GOT_STATUS_DELETE)
4670 if (staged_status == GOT_STATUS_DELETE)
4672 if (status == GOT_STATUS_NONEXISTENT)
4673 return got_error_set_errno(ENOENT, path);
4674 if (status != GOT_STATUS_MODIFY &&
4675 status != GOT_STATUS_ADD &&
4676 status != GOT_STATUS_DELETE &&
4677 status != GOT_STATUS_CONFLICT)
4681 err = got_opentemp_truncate(a->f1);
4683 return got_error_from_errno("got_opentemp_truncate");
4684 err = got_opentemp_truncate(a->f2);
4686 return got_error_from_errno("got_opentemp_truncate");
4688 if (!a->header_shown) {
4689 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4690 got_worktree_get_root_path(a->worktree));
4691 printf("commit - %s\n", a->id_str);
4692 printf("path + %s%s\n",
4693 got_worktree_get_root_path(a->worktree),
4694 a->diff_staged ? " (staged changes)" : "");
4695 a->header_shown = 1;
4698 if (a->diff_staged) {
4699 const char *label1 = NULL, *label2 = NULL;
4700 switch (staged_status) {
4701 case GOT_STATUS_MODIFY:
4705 case GOT_STATUS_ADD:
4708 case GOT_STATUS_DELETE:
4712 return got_error(GOT_ERR_FILE_STATUS);
4714 fd1 = got_opentempfd();
4716 err = got_error_from_errno("got_opentempfd");
4719 fd2 = got_opentempfd();
4721 err = got_error_from_errno("got_opentempfd");
4724 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4725 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4726 a->diff_algo, a->diff_context, a->ignore_whitespace,
4727 a->force_text_diff, a->repo, stdout);
4731 fd1 = got_opentempfd();
4733 err = got_error_from_errno("got_opentempfd");
4737 if (staged_status == GOT_STATUS_ADD ||
4738 staged_status == GOT_STATUS_MODIFY) {
4740 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4744 err = got_object_id_str(&id_str, staged_blob_id);
4747 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4748 err = got_error_from_errno("asprintf");
4753 } else if (status != GOT_STATUS_ADD) {
4754 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4760 if (status != GOT_STATUS_DELETE) {
4761 if (asprintf(&abspath, "%s/%s",
4762 got_worktree_get_root_path(a->worktree), path) == -1) {
4763 err = got_error_from_errno("asprintf");
4768 fd = openat(dirfd, de_name,
4769 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4771 if (!got_err_open_nofollow_on_symlink()) {
4772 err = got_error_from_errno2("openat",
4776 err = get_symlink_target_file(&fd, dirfd,
4782 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4784 if (!got_err_open_nofollow_on_symlink()) {
4785 err = got_error_from_errno2("open",
4789 err = get_symlink_target_file(&fd, dirfd,
4795 if (fstat(fd, &sb) == -1) {
4796 err = got_error_from_errno2("fstat", abspath);
4799 f2 = fdopen(fd, "r");
4801 err = got_error_from_errno2("fdopen", abspath);
4811 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4817 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4818 f2_exists, sb.st_size, path, GOT_DIFF_ALGORITHM_PATIENCE,
4819 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4821 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4822 err = got_error_from_errno("close");
4823 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4824 err = got_error_from_errno("close");
4826 got_object_blob_close(blob1);
4827 if (fd != -1 && close(fd) == -1 && err == NULL)
4828 err = got_error_from_errno("close");
4829 if (f2 && fclose(f2) == EOF && err == NULL)
4830 err = got_error_from_errno("fclose");
4835 static const struct got_error *
4836 cmd_diff(int argc, char *argv[])
4838 const struct got_error *error;
4839 struct got_repository *repo = NULL;
4840 struct got_worktree *worktree = NULL;
4841 char *cwd = NULL, *repo_path = NULL;
4842 const char *commit_args[2] = { NULL, NULL };
4843 int ncommit_args = 0;
4844 struct got_object_id *ids[2] = { NULL, NULL };
4845 char *labels[2] = { NULL, NULL };
4846 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4847 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4848 int force_text_diff = 0, force_path = 0, rflag = 0;
4850 struct got_reflist_head refs;
4851 struct got_pathlist_head paths;
4852 struct got_pathlist_entry *pe;
4853 FILE *f1 = NULL, *f2 = NULL;
4854 int fd1 = -1, fd2 = -1;
4855 int *pack_fds = NULL;
4861 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4866 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4869 force_text_diff = 1;
4872 if (ncommit_args >= 2)
4873 errx(1, "too many -c options used");
4874 commit_args[ncommit_args++] = optarg;
4877 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4880 errx(1, "number of context lines is %s: %s",
4884 repo_path = realpath(optarg, NULL);
4885 if (repo_path == NULL)
4886 return got_error_from_errno2("realpath",
4888 got_path_strip_trailing_slashes(repo_path);
4895 ignore_whitespace = 1;
4909 cwd = getcwd(NULL, 0);
4911 error = got_error_from_errno("getcwd");
4915 error = got_repo_pack_fds_open(&pack_fds);
4919 if (repo_path == NULL) {
4920 error = got_worktree_open(&worktree, cwd);
4921 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4927 strdup(got_worktree_get_repo_path(worktree));
4928 if (repo_path == NULL) {
4929 error = got_error_from_errno("strdup");
4933 repo_path = strdup(cwd);
4934 if (repo_path == NULL) {
4935 error = got_error_from_errno("strdup");
4941 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4946 if (rflag || worktree == NULL || ncommit_args > 0) {
4948 error = got_error_msg(GOT_ERR_NOT_IMPL,
4949 "-P option can only be used when diffing "
4954 error = got_error_msg(GOT_ERR_NOT_IMPL,
4955 "-s option can only be used when diffing "
4961 error = apply_unveil(got_repo_get_path(repo), 1,
4962 worktree ? got_worktree_get_root_path(worktree) : NULL);
4966 if ((!force_path && argc == 2) || ncommit_args > 0) {
4967 int obj_type = (ncommit_args > 0 ?
4968 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4969 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4973 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4975 if (ncommit_args > 0)
4976 arg = commit_args[i];
4979 error = got_repo_match_object_id(&ids[i], &labels[i],
4980 arg, obj_type, &refs, repo);
4982 if (error->code != GOT_ERR_NOT_REF &&
4983 error->code != GOT_ERR_NO_OBJ)
4985 if (ncommit_args > 0)
4993 f1 = got_opentemp();
4995 error = got_error_from_errno("got_opentemp");
4999 f2 = got_opentemp();
5001 error = got_error_from_errno("got_opentemp");
5005 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5006 struct print_diff_arg arg;
5009 if (worktree == NULL) {
5010 if (argc == 2 && ids[0] == NULL) {
5011 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5013 } else if (argc == 2 && ids[1] == NULL) {
5014 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5016 } else if (argc > 0) {
5017 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5018 "%s", "specified paths cannot be resolved");
5021 error = got_error(GOT_ERR_NOT_WORKTREE);
5026 error = get_worktree_paths_from_argv(&paths, argc, argv,
5031 error = got_object_id_str(&id_str,
5032 got_worktree_get_base_commit_id(worktree));
5036 arg.worktree = worktree;
5037 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5038 arg.diff_context = diff_context;
5039 arg.id_str = id_str;
5040 arg.header_shown = 0;
5041 arg.diff_staged = diff_staged;
5042 arg.ignore_whitespace = ignore_whitespace;
5043 arg.force_text_diff = force_text_diff;
5047 error = got_worktree_status(worktree, &paths, repo, 0,
5048 print_diff, &arg, check_cancelled, NULL);
5053 if (ncommit_args == 1) {
5054 struct got_commit_object *commit;
5055 error = got_object_open_as_commit(&commit, repo, ids[0]);
5059 labels[1] = labels[0];
5061 if (got_object_commit_get_nparents(commit) > 0) {
5062 const struct got_object_id_queue *pids;
5063 struct got_object_qid *pid;
5064 pids = got_object_commit_get_parent_ids(commit);
5065 pid = STAILQ_FIRST(pids);
5066 ids[0] = got_object_id_dup(&pid->id);
5067 if (ids[0] == NULL) {
5068 error = got_error_from_errno(
5069 "got_object_id_dup");
5070 got_object_commit_close(commit);
5073 error = got_object_id_str(&labels[0], ids[0]);
5075 got_object_commit_close(commit);
5080 labels[0] = strdup("/dev/null");
5081 if (labels[0] == NULL) {
5082 error = got_error_from_errno("strdup");
5083 got_object_commit_close(commit);
5088 got_object_commit_close(commit);
5091 if (ncommit_args == 0 && argc > 2) {
5092 error = got_error_msg(GOT_ERR_BAD_PATH,
5093 "path arguments cannot be used when diffing two objects");
5098 error = got_object_get_type(&type1, repo, ids[0]);
5103 error = got_object_get_type(&type2, repo, ids[1]);
5106 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5107 error = got_error(GOT_ERR_OBJ_TYPE);
5110 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5111 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5112 "path arguments cannot be used when diffing blobs");
5116 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5118 struct got_pathlist_entry *new;
5122 error = got_worktree_resolve_path(&p, worktree,
5126 prefix = got_worktree_get_path_prefix(worktree);
5127 while (prefix[0] == '/')
5129 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5130 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5132 error = got_error_from_errno("asprintf");
5138 char *mapped_path, *s;
5139 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5145 in_repo_path = strdup(s);
5146 if (in_repo_path == NULL) {
5147 error = got_error_from_errno("asprintf");
5154 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5155 if (error || new == NULL /* duplicate */)
5162 /* Release work tree lock. */
5163 got_worktree_close(worktree);
5167 fd1 = got_opentempfd();
5169 error = got_error_from_errno("got_opentempfd");
5173 fd2 = got_opentempfd();
5175 error = got_error_from_errno("got_opentempfd");
5179 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5180 case GOT_OBJ_TYPE_BLOB:
5181 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5182 fd1, fd2, ids[0], ids[1], NULL, NULL,
5183 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5184 ignore_whitespace, force_text_diff, repo, stdout);
5186 case GOT_OBJ_TYPE_TREE:
5187 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5188 ids[0], ids[1], &paths, "", "",
5189 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5190 ignore_whitespace, force_text_diff, repo, stdout);
5192 case GOT_OBJ_TYPE_COMMIT:
5193 printf("diff %s %s\n", labels[0], labels[1]);
5194 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5195 fd1, fd2, ids[0], ids[1], &paths,
5196 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5197 ignore_whitespace, force_text_diff, repo, stdout);
5200 error = got_error(GOT_ERR_OBJ_TYPE);
5208 got_worktree_close(worktree);
5210 const struct got_error *close_err = got_repo_close(repo);
5215 const struct got_error *pack_err =
5216 got_repo_pack_fds_close(pack_fds);
5220 TAILQ_FOREACH(pe, &paths, entry)
5221 free((char *)pe->path);
5222 got_pathlist_free(&paths);
5223 got_ref_list_free(&refs);
5224 if (f1 && fclose(f1) == EOF && error == NULL)
5225 error = got_error_from_errno("fclose");
5226 if (f2 && fclose(f2) == EOF && error == NULL)
5227 error = got_error_from_errno("fclose");
5228 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5229 error = got_error_from_errno("close");
5230 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5231 error = got_error_from_errno("close");
5239 "usage: %s blame [-c commit] [-r repository-path] path\n",
5248 char datebuf[11]; /* YYYY-MM-DD + NUL */
5251 struct blame_cb_args {
5252 struct blame_line *lines;
5256 off_t *line_offsets;
5258 struct got_repository *repo;
5261 static const struct got_error *
5262 blame_cb(void *arg, int nlines, int lineno,
5263 struct got_commit_object *commit, struct got_object_id *id)
5265 const struct got_error *err = NULL;
5266 struct blame_cb_args *a = arg;
5267 struct blame_line *bline;
5269 size_t linesize = 0;
5272 time_t committer_time;
5274 if (nlines != a->nlines ||
5275 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5276 return got_error(GOT_ERR_RANGE);
5278 if (sigint_received)
5279 return got_error(GOT_ERR_ITER_COMPLETED);
5282 return NULL; /* no change in this commit */
5284 /* Annotate this line. */
5285 bline = &a->lines[lineno - 1];
5286 if (bline->annotated)
5288 err = got_object_id_str(&bline->id_str, id);
5292 bline->committer = strdup(got_object_commit_get_committer(commit));
5293 if (bline->committer == NULL) {
5294 err = got_error_from_errno("strdup");
5298 committer_time = got_object_commit_get_committer_time(commit);
5299 if (gmtime_r(&committer_time, &tm) == NULL)
5300 return got_error_from_errno("gmtime_r");
5301 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5303 err = got_error(GOT_ERR_NO_SPACE);
5306 bline->annotated = 1;
5308 /* Print lines annotated so far. */
5309 bline = &a->lines[a->lineno_cur - 1];
5310 if (!bline->annotated)
5313 offset = a->line_offsets[a->lineno_cur - 1];
5314 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5315 err = got_error_from_errno("fseeko");
5319 while (bline->annotated) {
5320 char *smallerthan, *at, *nl, *committer;
5323 if (getline(&line, &linesize, a->f) == -1) {
5325 err = got_error_from_errno("getline");
5329 committer = bline->committer;
5330 smallerthan = strchr(committer, '<');
5331 if (smallerthan && smallerthan[1] != '\0')
5332 committer = smallerthan + 1;
5333 at = strchr(committer, '@');
5336 len = strlen(committer);
5338 committer[8] = '\0';
5340 nl = strchr(line, '\n');
5343 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5344 bline->id_str, bline->datebuf, committer, line);
5347 bline = &a->lines[a->lineno_cur - 1];
5354 static const struct got_error *
5355 cmd_blame(int argc, char *argv[])
5357 const struct got_error *error;
5358 struct got_repository *repo = NULL;
5359 struct got_worktree *worktree = NULL;
5360 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5361 char *link_target = NULL;
5362 struct got_object_id *obj_id = NULL;
5363 struct got_object_id *commit_id = NULL;
5364 struct got_commit_object *commit = NULL;
5365 struct got_blob_object *blob = NULL;
5366 char *commit_id_str = NULL;
5367 struct blame_cb_args bca;
5368 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5370 int *pack_fds = NULL;
5371 FILE *f1 = NULL, *f2 = NULL;
5373 fd1 = got_opentempfd();
5375 return got_error_from_errno("got_opentempfd");
5377 memset(&bca, 0, sizeof(bca));
5380 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5385 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5388 commit_id_str = optarg;
5391 repo_path = realpath(optarg, NULL);
5392 if (repo_path == NULL)
5393 return got_error_from_errno2("realpath",
5395 got_path_strip_trailing_slashes(repo_path);
5411 cwd = getcwd(NULL, 0);
5413 error = got_error_from_errno("getcwd");
5417 error = got_repo_pack_fds_open(&pack_fds);
5421 if (repo_path == NULL) {
5422 error = got_worktree_open(&worktree, cwd);
5423 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5429 strdup(got_worktree_get_repo_path(worktree));
5430 if (repo_path == NULL) {
5431 error = got_error_from_errno("strdup");
5436 repo_path = strdup(cwd);
5437 if (repo_path == NULL) {
5438 error = got_error_from_errno("strdup");
5444 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5449 const char *prefix = got_worktree_get_path_prefix(worktree);
5452 error = got_worktree_resolve_path(&p, worktree, path);
5455 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5456 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5458 error = got_error_from_errno("asprintf");
5463 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5465 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5468 error = got_repo_map_path(&in_repo_path, repo, path);
5473 if (commit_id_str == NULL) {
5474 struct got_reference *head_ref;
5475 error = got_ref_open(&head_ref, repo, worktree ?
5476 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5479 error = got_ref_resolve(&commit_id, repo, head_ref);
5480 got_ref_close(head_ref);
5484 struct got_reflist_head refs;
5486 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5490 error = got_repo_match_object_id(&commit_id, NULL,
5491 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5492 got_ref_list_free(&refs);
5498 /* Release work tree lock. */
5499 got_worktree_close(worktree);
5503 error = got_object_open_as_commit(&commit, repo, commit_id);
5507 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5512 error = got_object_id_by_path(&obj_id, repo, commit,
5513 link_target ? link_target : in_repo_path);
5517 error = got_object_get_type(&obj_type, repo, obj_id);
5521 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5522 error = got_error_path(link_target ? link_target : in_repo_path,
5527 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5530 bca.f = got_opentemp();
5531 if (bca.f == NULL) {
5532 error = got_error_from_errno("got_opentemp");
5535 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5536 &bca.line_offsets, bca.f, blob);
5537 if (error || bca.nlines == 0)
5540 /* Don't include \n at EOF in the blame line count. */
5541 if (bca.line_offsets[bca.nlines - 1] == filesize)
5544 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5545 if (bca.lines == NULL) {
5546 error = got_error_from_errno("calloc");
5550 bca.nlines_prec = 0;
5558 fd2 = got_opentempfd();
5560 error = got_error_from_errno("got_opentempfd");
5563 fd3 = got_opentempfd();
5565 error = got_error_from_errno("got_opentempfd");
5568 f1 = got_opentemp();
5570 error = got_error_from_errno("got_opentemp");
5573 f2 = got_opentemp();
5575 error = got_error_from_errno("got_opentemp");
5578 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5579 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5580 check_cancelled, NULL, fd2, fd3, f1, f2);
5589 got_object_commit_close(commit);
5591 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5592 error = got_error_from_errno("close");
5593 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5594 error = got_error_from_errno("close");
5595 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5596 error = got_error_from_errno("close");
5597 if (f1 && fclose(f1) == EOF && error == NULL)
5598 error = got_error_from_errno("fclose");
5599 if (f2 && fclose(f2) == EOF && error == NULL)
5600 error = got_error_from_errno("fclose");
5603 got_object_blob_close(blob);
5605 got_worktree_close(worktree);
5607 const struct got_error *close_err = got_repo_close(repo);
5612 const struct got_error *pack_err =
5613 got_repo_pack_fds_close(pack_fds);
5618 for (i = 0; i < bca.nlines; i++) {
5619 struct blame_line *bline = &bca.lines[i];
5620 free(bline->id_str);
5621 free(bline->committer);
5625 free(bca.line_offsets);
5626 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5627 error = got_error_from_errno("fclose");
5635 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5640 static const struct got_error *
5641 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5642 const char *root_path, struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 int is_root_path = (strcmp(path, root_path) == 0);
5646 const char *modestr = "";
5647 mode_t mode = got_tree_entry_get_mode(te);
5648 char *link_target = NULL;
5650 path += strlen(root_path);
5651 while (path[0] == '/')
5654 if (got_object_tree_entry_is_submodule(te))
5656 else if (S_ISLNK(mode)) {
5659 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5662 for (i = 0; i < strlen(link_target); i++) {
5663 if (!isprint((unsigned char)link_target[i]))
5664 link_target[i] = '?';
5669 else if (S_ISDIR(mode))
5671 else if (mode & S_IXUSR)
5674 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5675 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5676 link_target ? " -> ": "", link_target ? link_target : "");
5682 static const struct got_error *
5683 print_tree(const char *path, struct got_commit_object *commit,
5684 int show_ids, int recurse, const char *root_path,
5685 struct got_repository *repo)
5687 const struct got_error *err = NULL;
5688 struct got_object_id *tree_id = NULL;
5689 struct got_tree_object *tree = NULL;
5692 err = got_object_id_by_path(&tree_id, repo, commit, path);
5696 err = got_object_open_as_tree(&tree, repo, tree_id);
5699 nentries = got_object_tree_get_nentries(tree);
5700 for (i = 0; i < nentries; i++) {
5701 struct got_tree_entry *te;
5704 if (sigint_received || sigpipe_received)
5707 te = got_object_tree_get_entry(tree, i);
5710 err = got_object_id_str(&id_str,
5711 got_tree_entry_get_id(te));
5714 if (asprintf(&id, "%s ", id_str) == -1) {
5715 err = got_error_from_errno("asprintf");
5721 err = print_entry(te, id, path, root_path, repo);
5726 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5728 if (asprintf(&child_path, "%s%s%s", path,
5729 path[0] == '/' && path[1] == '\0' ? "" : "/",
5730 got_tree_entry_get_name(te)) == -1) {
5731 err = got_error_from_errno("asprintf");
5734 err = print_tree(child_path, commit, show_ids, 1,
5743 got_object_tree_close(tree);
5748 static const struct got_error *
5749 cmd_tree(int argc, char *argv[])
5751 const struct got_error *error;
5752 struct got_repository *repo = NULL;
5753 struct got_worktree *worktree = NULL;
5754 const char *path, *refname = NULL;
5755 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5756 struct got_object_id *commit_id = NULL;
5757 struct got_commit_object *commit = NULL;
5758 char *commit_id_str = NULL;
5759 int show_ids = 0, recurse = 0;
5761 int *pack_fds = NULL;
5764 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5769 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5772 commit_id_str = optarg;
5775 repo_path = realpath(optarg, NULL);
5776 if (repo_path == NULL)
5777 return got_error_from_errno2("realpath",
5779 got_path_strip_trailing_slashes(repo_path);
5803 cwd = getcwd(NULL, 0);
5805 error = got_error_from_errno("getcwd");
5809 error = got_repo_pack_fds_open(&pack_fds);
5813 if (repo_path == NULL) {
5814 error = got_worktree_open(&worktree, cwd);
5815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5821 strdup(got_worktree_get_repo_path(worktree));
5822 if (repo_path == NULL)
5823 error = got_error_from_errno("strdup");
5827 repo_path = strdup(cwd);
5828 if (repo_path == NULL) {
5829 error = got_error_from_errno("strdup");
5835 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5840 const char *prefix = got_worktree_get_path_prefix(worktree);
5845 error = got_worktree_resolve_path(&p, worktree, path);
5848 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5849 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5851 error = got_error_from_errno("asprintf");
5856 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5860 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5865 error = got_repo_map_path(&in_repo_path, repo, path);
5870 if (commit_id_str == NULL) {
5871 struct got_reference *head_ref;
5873 refname = got_worktree_get_head_ref_name(worktree);
5875 refname = GOT_REF_HEAD;
5876 error = got_ref_open(&head_ref, repo, refname, 0);
5879 error = got_ref_resolve(&commit_id, repo, head_ref);
5880 got_ref_close(head_ref);
5884 struct got_reflist_head refs;
5886 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5890 error = got_repo_match_object_id(&commit_id, NULL,
5891 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5892 got_ref_list_free(&refs);
5898 /* Release work tree lock. */
5899 got_worktree_close(worktree);
5903 error = got_object_open_as_commit(&commit, repo, commit_id);
5907 error = print_tree(in_repo_path, commit, show_ids, recurse,
5908 in_repo_path, repo);
5915 got_object_commit_close(commit);
5917 got_worktree_close(worktree);
5919 const struct got_error *close_err = got_repo_close(repo);
5924 const struct got_error *pack_err =
5925 got_repo_pack_fds_close(pack_fds);
5935 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5936 "[-S status-codes] [path ...]\n", getprogname());
5940 struct got_status_arg {
5945 static const struct got_error *
5946 print_status(void *arg, unsigned char status, unsigned char staged_status,
5947 const char *path, struct got_object_id *blob_id,
5948 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5949 int dirfd, const char *de_name)
5951 struct got_status_arg *st = arg;
5953 if (status == staged_status && (status == GOT_STATUS_DELETE))
5954 status = GOT_STATUS_NO_CHANGE;
5955 if (st != NULL && st->status_codes) {
5956 size_t ncodes = strlen(st->status_codes);
5959 for (i = 0; i < ncodes ; i++) {
5961 if (status == st->status_codes[i] ||
5962 staged_status == st->status_codes[i]) {
5967 if (status == st->status_codes[i] ||
5968 staged_status == st->status_codes[i])
5973 if (st->suppress && j == 0)
5980 printf("%c%c %s\n", status, staged_status, path);
5984 static const struct got_error *
5985 cmd_status(int argc, char *argv[])
5987 const struct got_error *error = NULL;
5988 struct got_repository *repo = NULL;
5989 struct got_worktree *worktree = NULL;
5990 struct got_status_arg st;
5992 struct got_pathlist_head paths;
5993 struct got_pathlist_entry *pe;
5994 int ch, i, no_ignores = 0;
5995 int *pack_fds = NULL;
5999 memset(&st, 0, sizeof(st));
6000 st.status_codes = NULL;
6003 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
6009 if (st.status_codes != NULL && st.suppress == 0)
6010 option_conflict('S', 's');
6014 for (i = 0; i < strlen(optarg); i++) {
6015 switch (optarg[i]) {
6016 case GOT_STATUS_MODIFY:
6017 case GOT_STATUS_ADD:
6018 case GOT_STATUS_DELETE:
6019 case GOT_STATUS_CONFLICT:
6020 case GOT_STATUS_MISSING:
6021 case GOT_STATUS_OBSTRUCTED:
6022 case GOT_STATUS_UNVERSIONED:
6023 case GOT_STATUS_MODE_CHANGE:
6024 case GOT_STATUS_NONEXISTENT:
6027 errx(1, "invalid status code '%c'",
6031 if (ch == 's' && st.suppress)
6032 option_conflict('s', 'S');
6033 st.status_codes = optarg;
6045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6049 cwd = getcwd(NULL, 0);
6051 error = got_error_from_errno("getcwd");
6055 error = got_repo_pack_fds_open(&pack_fds);
6059 error = got_worktree_open(&worktree, cwd);
6061 if (error->code == GOT_ERR_NOT_WORKTREE)
6062 error = wrap_not_worktree_error(error, "status", cwd);
6066 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6071 error = apply_unveil(got_repo_get_path(repo), 1,
6072 got_worktree_get_root_path(worktree));
6076 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6080 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6081 print_status, &st, check_cancelled, NULL);
6084 const struct got_error *pack_err =
6085 got_repo_pack_fds_close(pack_fds);
6090 TAILQ_FOREACH(pe, &paths, entry)
6091 free((char *)pe->path);
6092 got_pathlist_free(&paths);
6101 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6102 "[-s reference] [-d] [name]\n",
6107 static const struct got_error *
6108 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6110 static const struct got_error *err = NULL;
6111 struct got_reflist_head refs;
6112 struct got_reflist_entry *re;
6115 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6116 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6121 TAILQ_FOREACH(re, &refs, entry) {
6123 refstr = got_ref_to_str(re->ref);
6124 if (refstr == NULL) {
6125 err = got_error_from_errno("got_ref_to_str");
6128 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6132 got_ref_list_free(&refs);
6136 static const struct got_error *
6137 delete_ref_by_name(struct got_repository *repo, const char *refname)
6139 const struct got_error *err;
6140 struct got_reference *ref;
6142 err = got_ref_open(&ref, repo, refname, 0);
6146 err = delete_ref(repo, ref);
6151 static const struct got_error *
6152 add_ref(struct got_repository *repo, const char *refname, const char *target)
6154 const struct got_error *err = NULL;
6155 struct got_object_id *id = NULL;
6156 struct got_reference *ref = NULL;
6157 struct got_reflist_head refs;
6160 * Don't let the user create a reference name with a leading '-'.
6161 * While technically a valid reference name, this case is usually
6162 * an unintended typo.
6164 if (refname[0] == '-')
6165 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6168 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6171 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6173 got_ref_list_free(&refs);
6177 err = got_ref_alloc(&ref, refname, id);
6181 err = got_ref_write(ref, repo);
6189 static const struct got_error *
6190 add_symref(struct got_repository *repo, const char *refname, const char *target)
6192 const struct got_error *err = NULL;
6193 struct got_reference *ref = NULL;
6194 struct got_reference *target_ref = NULL;
6197 * Don't let the user create a reference name with a leading '-'.
6198 * While technically a valid reference name, this case is usually
6199 * an unintended typo.
6201 if (refname[0] == '-')
6202 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6204 err = got_ref_open(&target_ref, repo, target, 0);
6208 err = got_ref_alloc_symref(&ref, refname, target_ref);
6212 err = got_ref_write(ref, repo);
6215 got_ref_close(target_ref);
6221 static const struct got_error *
6222 cmd_ref(int argc, char *argv[])
6224 const struct got_error *error = NULL;
6225 struct got_repository *repo = NULL;
6226 struct got_worktree *worktree = NULL;
6227 char *cwd = NULL, *repo_path = NULL;
6228 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6229 const char *obj_arg = NULL, *symref_target= NULL;
6230 char *refname = NULL;
6231 int *pack_fds = NULL;
6233 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6242 repo_path = realpath(optarg, NULL);
6243 if (repo_path == NULL)
6244 return got_error_from_errno2("realpath",
6246 got_path_strip_trailing_slashes(repo_path);
6252 symref_target = optarg;
6263 if (obj_arg && do_list)
6264 option_conflict('c', 'l');
6265 if (obj_arg && do_delete)
6266 option_conflict('c', 'd');
6267 if (obj_arg && symref_target)
6268 option_conflict('c', 's');
6269 if (symref_target && do_delete)
6270 option_conflict('s', 'd');
6271 if (symref_target && do_list)
6272 option_conflict('s', 'l');
6273 if (do_delete && do_list)
6274 option_conflict('d', 'l');
6275 if (sort_by_time && !do_list)
6276 errx(1, "-t option requires -l option");
6282 if (argc != 0 && argc != 1)
6285 refname = strdup(argv[0]);
6286 if (refname == NULL) {
6287 error = got_error_from_errno("strdup");
6294 refname = strdup(argv[0]);
6295 if (refname == NULL) {
6296 error = got_error_from_errno("strdup");
6302 got_path_strip_trailing_slashes(refname);
6305 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6306 "sendfd unveil", NULL) == -1)
6309 cwd = getcwd(NULL, 0);
6311 error = got_error_from_errno("getcwd");
6315 error = got_repo_pack_fds_open(&pack_fds);
6319 if (repo_path == NULL) {
6320 error = got_worktree_open(&worktree, cwd);
6321 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6327 strdup(got_worktree_get_repo_path(worktree));
6328 if (repo_path == NULL)
6329 error = got_error_from_errno("strdup");
6333 repo_path = strdup(cwd);
6334 if (repo_path == NULL) {
6335 error = got_error_from_errno("strdup");
6341 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6347 /* Remove "cpath" promise. */
6348 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6354 error = apply_unveil(got_repo_get_path(repo), do_list,
6355 worktree ? got_worktree_get_root_path(worktree) : NULL);
6360 error = list_refs(repo, refname, sort_by_time);
6362 error = delete_ref_by_name(repo, refname);
6363 else if (symref_target)
6364 error = add_symref(repo, refname, symref_target);
6366 if (obj_arg == NULL)
6368 error = add_ref(repo, refname, obj_arg);
6373 const struct got_error *close_err = got_repo_close(repo);
6378 got_worktree_close(worktree);
6380 const struct got_error *pack_err =
6381 got_repo_pack_fds_close(pack_fds);
6394 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6395 "[-n] [name]\n", getprogname());
6399 static const struct got_error *
6400 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6401 struct got_reference *ref)
6403 const struct got_error *err = NULL;
6404 const char *refname, *marker = " ";
6407 refname = got_ref_get_name(ref);
6408 if (worktree && strcmp(refname,
6409 got_worktree_get_head_ref_name(worktree)) == 0) {
6410 struct got_object_id *id = NULL;
6412 err = got_ref_resolve(&id, repo, ref);
6415 if (got_object_id_cmp(id,
6416 got_worktree_get_base_commit_id(worktree)) == 0)
6423 if (strncmp(refname, "refs/heads/", 11) == 0)
6425 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6427 if (strncmp(refname, "refs/remotes/", 13) == 0)
6430 refstr = got_ref_to_str(ref);
6432 return got_error_from_errno("got_ref_to_str");
6434 printf("%s%s: %s\n", marker, refname, refstr);
6439 static const struct got_error *
6440 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6442 const char *refname;
6444 if (worktree == NULL)
6445 return got_error(GOT_ERR_NOT_WORKTREE);
6447 refname = got_worktree_get_head_ref_name(worktree);
6449 if (strncmp(refname, "refs/heads/", 11) == 0)
6451 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6454 printf("%s\n", refname);
6459 static const struct got_error *
6460 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6463 static const struct got_error *err = NULL;
6464 struct got_reflist_head refs;
6465 struct got_reflist_entry *re;
6466 struct got_reference *temp_ref = NULL;
6467 int rebase_in_progress, histedit_in_progress;
6472 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6477 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6482 if (rebase_in_progress || histedit_in_progress) {
6483 err = got_ref_open(&temp_ref, repo,
6484 got_worktree_get_head_ref_name(worktree), 0);
6487 list_branch(repo, worktree, temp_ref);
6488 got_ref_close(temp_ref);
6492 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6493 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6498 TAILQ_FOREACH(re, &refs, entry)
6499 list_branch(repo, worktree, re->ref);
6501 got_ref_list_free(&refs);
6503 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6504 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6509 TAILQ_FOREACH(re, &refs, entry)
6510 list_branch(repo, worktree, re->ref);
6512 got_ref_list_free(&refs);
6517 static const struct got_error *
6518 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6519 const char *branch_name)
6521 const struct got_error *err = NULL;
6522 struct got_reference *ref = NULL;
6523 char *refname, *remote_refname = NULL;
6525 if (strncmp(branch_name, "refs/", 5) == 0)
6527 if (strncmp(branch_name, "heads/", 6) == 0)
6529 else if (strncmp(branch_name, "remotes/", 8) == 0)
6532 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6533 return got_error_from_errno("asprintf");
6535 if (asprintf(&remote_refname, "refs/remotes/%s",
6536 branch_name) == -1) {
6537 err = got_error_from_errno("asprintf");
6541 err = got_ref_open(&ref, repo, refname, 0);
6543 const struct got_error *err2;
6544 if (err->code != GOT_ERR_NOT_REF)
6547 * Keep 'err' intact such that if neither branch exists
6548 * we report "refs/heads" rather than "refs/remotes" in
6549 * our error message.
6551 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6558 strcmp(got_worktree_get_head_ref_name(worktree),
6559 got_ref_get_name(ref)) == 0) {
6560 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6561 "will not delete this work tree's current branch");
6565 err = delete_ref(repo, ref);
6570 free(remote_refname);
6574 static const struct got_error *
6575 add_branch(struct got_repository *repo, const char *branch_name,
6576 struct got_object_id *base_commit_id)
6578 const struct got_error *err = NULL;
6579 struct got_reference *ref = NULL;
6580 char *base_refname = NULL, *refname = NULL;
6583 * Don't let the user create a branch name with a leading '-'.
6584 * While technically a valid reference name, this case is usually
6585 * an unintended typo.
6587 if (branch_name[0] == '-')
6588 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6590 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6593 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6594 err = got_error_from_errno("asprintf");
6598 err = got_ref_open(&ref, repo, refname, 0);
6600 err = got_error(GOT_ERR_BRANCH_EXISTS);
6602 } else if (err->code != GOT_ERR_NOT_REF)
6605 err = got_ref_alloc(&ref, refname, base_commit_id);
6609 err = got_ref_write(ref, repo);
6618 static const struct got_error *
6619 cmd_branch(int argc, char *argv[])
6621 const struct got_error *error = NULL;
6622 struct got_repository *repo = NULL;
6623 struct got_worktree *worktree = NULL;
6624 char *cwd = NULL, *repo_path = NULL;
6625 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6626 const char *delref = NULL, *commit_id_arg = NULL;
6627 struct got_reference *ref = NULL;
6628 struct got_pathlist_head paths;
6629 struct got_pathlist_entry *pe;
6630 struct got_object_id *commit_id = NULL;
6631 char *commit_id_str = NULL;
6632 int *pack_fds = NULL;
6636 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6639 commit_id_arg = optarg;
6645 repo_path = realpath(optarg, NULL);
6646 if (repo_path == NULL)
6647 return got_error_from_errno2("realpath",
6649 got_path_strip_trailing_slashes(repo_path);
6666 if (do_list && delref)
6667 option_conflict('l', 'd');
6668 if (sort_by_time && !do_list)
6669 errx(1, "-t option requires -l option");
6674 if (!do_list && !delref && argc == 0)
6677 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6678 errx(1, "-c option can only be used when creating a branch");
6680 if (do_list || delref) {
6683 } else if (!do_show && argc != 1)
6687 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6688 "sendfd unveil", NULL) == -1)
6691 cwd = getcwd(NULL, 0);
6693 error = got_error_from_errno("getcwd");
6697 error = got_repo_pack_fds_open(&pack_fds);
6701 if (repo_path == NULL) {
6702 error = got_worktree_open(&worktree, cwd);
6703 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6709 strdup(got_worktree_get_repo_path(worktree));
6710 if (repo_path == NULL)
6711 error = got_error_from_errno("strdup");
6715 repo_path = strdup(cwd);
6716 if (repo_path == NULL) {
6717 error = got_error_from_errno("strdup");
6723 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6728 if (do_list || do_show) {
6729 /* Remove "cpath" promise. */
6730 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6736 error = apply_unveil(got_repo_get_path(repo), do_list,
6737 worktree ? got_worktree_get_root_path(worktree) : NULL);
6742 error = show_current_branch(repo, worktree);
6744 error = list_branches(repo, worktree, sort_by_time);
6746 error = delete_branch(repo, worktree, delref);
6748 struct got_reflist_head refs;
6750 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6754 if (commit_id_arg == NULL)
6755 commit_id_arg = worktree ?
6756 got_worktree_get_head_ref_name(worktree) :
6758 error = got_repo_match_object_id(&commit_id, NULL,
6759 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6760 got_ref_list_free(&refs);
6763 error = add_branch(repo, argv[0], commit_id);
6766 if (worktree && do_update) {
6767 struct got_update_progress_arg upa;
6768 char *branch_refname = NULL;
6770 error = got_object_id_str(&commit_id_str, commit_id);
6773 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6777 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6779 error = got_error_from_errno("asprintf");
6782 error = got_ref_open(&ref, repo, branch_refname, 0);
6783 free(branch_refname);
6786 error = switch_head_ref(ref, commit_id, worktree,
6790 error = got_worktree_set_base_commit_id(worktree, repo,
6794 memset(&upa, 0, sizeof(upa));
6795 error = got_worktree_checkout_files(worktree, &paths,
6796 repo, update_progress, &upa, check_cancelled,
6800 if (upa.did_something) {
6801 printf("Updated to %s: %s\n",
6802 got_worktree_get_head_ref_name(worktree),
6805 print_update_progress_stats(&upa);
6812 const struct got_error *close_err = got_repo_close(repo);
6817 got_worktree_close(worktree);
6819 const struct got_error *pack_err =
6820 got_repo_pack_fds_close(pack_fds);
6827 free(commit_id_str);
6828 TAILQ_FOREACH(pe, &paths, entry)
6829 free((char *)pe->path);
6830 got_pathlist_free(&paths);
6839 "usage: %s tag [-c commit] [-r repository] [-l] "
6840 "[-m message] name\n", getprogname());
6845 static const struct got_error *
6846 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6848 const struct got_error *err = NULL;
6849 struct got_reflist_entry *re, *se, *new;
6850 struct got_object_id *re_id, *se_id;
6851 struct got_tag_object *re_tag, *se_tag;
6852 time_t re_time, se_time;
6854 STAILQ_FOREACH(re, tags, entry) {
6855 se = STAILQ_FIRST(sorted);
6857 err = got_reflist_entry_dup(&new, re);
6860 STAILQ_INSERT_HEAD(sorted, new, entry);
6863 err = got_ref_resolve(&re_id, repo, re->ref);
6866 err = got_object_open_as_tag(&re_tag, repo, re_id);
6870 re_time = got_object_tag_get_tagger_time(re_tag);
6871 got_object_tag_close(re_tag);
6875 err = got_ref_resolve(&se_id, repo, re->ref);
6878 err = got_object_open_as_tag(&se_tag, repo, se_id);
6882 se_time = got_object_tag_get_tagger_time(se_tag);
6883 got_object_tag_close(se_tag);
6885 if (se_time > re_time) {
6886 err = got_reflist_entry_dup(&new, re);
6889 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6892 se = STAILQ_NEXT(se, entry);
6901 static const struct got_error *
6902 get_tag_refname(char **refname, const char *tag_name)
6904 const struct got_error *err;
6906 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6907 *refname = strdup(tag_name);
6908 if (*refname == NULL)
6909 return got_error_from_errno("strdup");
6910 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6911 err = got_error_from_errno("asprintf");
6919 static const struct got_error *
6920 list_tags(struct got_repository *repo, const char *tag_name)
6922 static const struct got_error *err = NULL;
6923 struct got_reflist_head refs;
6924 struct got_reflist_entry *re;
6925 char *wanted_refname = NULL;
6929 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6934 struct got_reference *ref;
6935 err = get_tag_refname(&wanted_refname, tag_name);
6938 /* Wanted tag reference should exist. */
6939 err = got_ref_open(&ref, repo, wanted_refname, 0);
6945 TAILQ_FOREACH(re, &refs, entry) {
6946 const char *refname;
6947 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6951 struct got_object_id *id;
6952 struct got_tag_object *tag;
6953 struct got_commit_object *commit = NULL;
6955 refname = got_ref_get_name(re->ref);
6956 if (strncmp(refname, "refs/tags/", 10) != 0 ||
6957 (wanted_refname && strcmp(refname, wanted_refname) != 0))
6960 refstr = got_ref_to_str(re->ref);
6961 if (refstr == NULL) {
6962 err = got_error_from_errno("got_ref_to_str");
6965 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6968 err = got_ref_resolve(&id, repo, re->ref);
6971 err = got_object_open_as_tag(&tag, repo, id);
6973 if (err->code != GOT_ERR_OBJ_TYPE) {
6977 /* "lightweight" tag */
6978 err = got_object_open_as_commit(&commit, repo, id);
6983 tagger = got_object_commit_get_committer(commit);
6985 got_object_commit_get_committer_time(commit);
6986 err = got_object_id_str(&id_str, id);
6992 tagger = got_object_tag_get_tagger(tag);
6993 tagger_time = got_object_tag_get_tagger_time(tag);
6994 err = got_object_id_str(&id_str,
6995 got_object_tag_get_object_id(tag));
6999 printf("from: %s\n", tagger);
7000 datestr = get_datestr(&tagger_time, datebuf);
7002 printf("date: %s UTC\n", datestr);
7004 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7006 switch (got_object_tag_get_object_type(tag)) {
7007 case GOT_OBJ_TYPE_BLOB:
7008 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7011 case GOT_OBJ_TYPE_TREE:
7012 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7015 case GOT_OBJ_TYPE_COMMIT:
7016 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7019 case GOT_OBJ_TYPE_TAG:
7020 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7029 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7032 got_object_commit_close(commit);
7034 tagmsg0 = strdup(got_object_tag_get_message(tag));
7035 got_object_tag_close(tag);
7036 if (tagmsg0 == NULL) {
7037 err = got_error_from_errno("strdup");
7044 line = strsep(&tagmsg, "\n");
7046 printf(" %s\n", line);
7051 got_ref_list_free(&refs);
7052 free(wanted_refname);
7056 static const struct got_error *
7057 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7058 const char *tag_name, const char *repo_path)
7060 const struct got_error *err = NULL;
7061 char *template = NULL, *initial_content = NULL;
7062 char *editor = NULL;
7063 int initial_content_len;
7066 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7067 err = got_error_from_errno("asprintf");
7071 initial_content_len = asprintf(&initial_content,
7072 "\n# tagging commit %s as %s\n",
7073 commit_id_str, tag_name);
7074 if (initial_content_len == -1) {
7075 err = got_error_from_errno("asprintf");
7079 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7083 if (write(fd, initial_content, initial_content_len) == -1) {
7084 err = got_error_from_errno2("write", *tagmsg_path);
7088 err = get_editor(&editor);
7091 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7092 initial_content_len, 1);
7094 free(initial_content);
7098 if (fd != -1 && close(fd) == -1 && err == NULL)
7099 err = got_error_from_errno2("close", *tagmsg_path);
7101 /* Editor is done; we can now apply unveil(2) */
7103 err = apply_unveil(repo_path, 0, NULL);
7111 static const struct got_error *
7112 add_tag(struct got_repository *repo, const char *tagger,
7113 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
7115 const struct got_error *err = NULL;
7116 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7117 char *label = NULL, *commit_id_str = NULL;
7118 struct got_reference *ref = NULL;
7119 char *refname = NULL, *tagmsg = NULL;
7120 char *tagmsg_path = NULL, *tag_id_str = NULL;
7121 int preserve_tagmsg = 0;
7122 struct got_reflist_head refs;
7127 * Don't let the user create a tag name with a leading '-'.
7128 * While technically a valid reference name, this case is usually
7129 * an unintended typo.
7131 if (tag_name[0] == '-')
7132 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7134 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7138 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7139 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7143 err = got_object_id_str(&commit_id_str, commit_id);
7147 err = get_tag_refname(&refname, tag_name);
7150 if (strncmp("refs/tags/", tag_name, 10) == 0)
7153 err = got_ref_open(&ref, repo, refname, 0);
7155 err = got_error(GOT_ERR_TAG_EXISTS);
7157 } else if (err->code != GOT_ERR_NOT_REF)
7160 if (tagmsg_arg == NULL) {
7161 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7162 tag_name, got_repo_get_path(repo));
7164 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7165 tagmsg_path != NULL)
7166 preserve_tagmsg = 1;
7171 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7172 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7175 preserve_tagmsg = 1;
7179 err = got_ref_alloc(&ref, refname, tag_id);
7182 preserve_tagmsg = 1;
7186 err = got_ref_write(ref, repo);
7189 preserve_tagmsg = 1;
7193 err = got_object_id_str(&tag_id_str, tag_id);
7196 preserve_tagmsg = 1;
7199 printf("Created tag %s\n", tag_id_str);
7201 if (preserve_tagmsg) {
7202 fprintf(stderr, "%s: tag message preserved in %s\n",
7203 getprogname(), tagmsg_path);
7204 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7205 err = got_error_from_errno2("unlink", tagmsg_path);
7210 free(commit_id_str);
7214 got_ref_list_free(&refs);
7218 static const struct got_error *
7219 cmd_tag(int argc, char *argv[])
7221 const struct got_error *error = NULL;
7222 struct got_repository *repo = NULL;
7223 struct got_worktree *worktree = NULL;
7224 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7225 char *gitconfig_path = NULL, *tagger = NULL;
7226 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7227 int ch, do_list = 0;
7228 int *pack_fds = NULL;
7230 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7233 commit_id_arg = optarg;
7239 repo_path = realpath(optarg, NULL);
7240 if (repo_path == NULL)
7241 return got_error_from_errno2("realpath",
7243 got_path_strip_trailing_slashes(repo_path);
7258 if (commit_id_arg != NULL)
7260 "-c option can only be used when creating a tag");
7262 option_conflict('l', 'm');
7265 } else if (argc != 1)
7272 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7273 "sendfd unveil", NULL) == -1)
7276 cwd = getcwd(NULL, 0);
7278 error = got_error_from_errno("getcwd");
7282 error = got_repo_pack_fds_open(&pack_fds);
7286 if (repo_path == NULL) {
7287 error = got_worktree_open(&worktree, cwd);
7288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7294 strdup(got_worktree_get_repo_path(worktree));
7295 if (repo_path == NULL)
7296 error = got_error_from_errno("strdup");
7300 repo_path = strdup(cwd);
7301 if (repo_path == NULL) {
7302 error = got_error_from_errno("strdup");
7310 /* Release work tree lock. */
7311 got_worktree_close(worktree);
7314 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7319 /* Remove "cpath" promise. */
7320 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7324 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7327 error = list_tags(repo, tag_name);
7329 error = get_gitconfig_path(&gitconfig_path);
7332 error = got_repo_open(&repo, repo_path, gitconfig_path,
7337 error = get_author(&tagger, repo, worktree);
7341 /* Release work tree lock. */
7342 got_worktree_close(worktree);
7347 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7352 if (commit_id_arg == NULL) {
7353 struct got_reference *head_ref;
7354 struct got_object_id *commit_id;
7355 error = got_ref_open(&head_ref, repo,
7356 worktree ? got_worktree_get_head_ref_name(worktree)
7360 error = got_ref_resolve(&commit_id, repo, head_ref);
7361 got_ref_close(head_ref);
7364 error = got_object_id_str(&commit_id_str, commit_id);
7370 error = add_tag(repo, tagger, tag_name,
7371 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7375 const struct got_error *close_err = got_repo_close(repo);
7380 got_worktree_close(worktree);
7382 const struct got_error *pack_err =
7383 got_repo_pack_fds_close(pack_fds);
7389 free(gitconfig_path);
7390 free(commit_id_str);
7398 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7403 static const struct got_error *
7404 add_progress(void *arg, unsigned char status, const char *path)
7406 while (path[0] == '/')
7408 printf("%c %s\n", status, path);
7412 static const struct got_error *
7413 cmd_add(int argc, char *argv[])
7415 const struct got_error *error = NULL;
7416 struct got_repository *repo = NULL;
7417 struct got_worktree *worktree = NULL;
7419 struct got_pathlist_head paths;
7420 struct got_pathlist_entry *pe;
7421 int ch, can_recurse = 0, no_ignores = 0;
7422 int *pack_fds = NULL;
7426 while ((ch = getopt(argc, argv, "IR")) != -1) {
7444 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7451 cwd = getcwd(NULL, 0);
7453 error = got_error_from_errno("getcwd");
7457 error = got_repo_pack_fds_open(&pack_fds);
7461 error = got_worktree_open(&worktree, cwd);
7463 if (error->code == GOT_ERR_NOT_WORKTREE)
7464 error = wrap_not_worktree_error(error, "add", cwd);
7468 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7473 error = apply_unveil(got_repo_get_path(repo), 1,
7474 got_worktree_get_root_path(worktree));
7478 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7485 TAILQ_FOREACH(pe, &paths, entry) {
7486 if (asprintf(&ondisk_path, "%s/%s",
7487 got_worktree_get_root_path(worktree),
7489 error = got_error_from_errno("asprintf");
7492 if (lstat(ondisk_path, &sb) == -1) {
7493 if (errno == ENOENT) {
7497 error = got_error_from_errno2("lstat",
7503 if (S_ISDIR(sb.st_mode)) {
7504 error = got_error_msg(GOT_ERR_BAD_PATH,
7505 "adding directories requires -R option");
7511 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7512 NULL, repo, no_ignores);
7515 const struct got_error *close_err = got_repo_close(repo);
7520 got_worktree_close(worktree);
7522 const struct got_error *pack_err =
7523 got_repo_pack_fds_close(pack_fds);
7527 TAILQ_FOREACH(pe, &paths, entry)
7528 free((char *)pe->path);
7529 got_pathlist_free(&paths);
7537 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7538 "path ...\n", getprogname());
7542 static const struct got_error *
7543 print_remove_status(void *arg, unsigned char status,
7544 unsigned char staged_status, const char *path)
7546 while (path[0] == '/')
7548 if (status == GOT_STATUS_NONEXISTENT)
7550 if (status == staged_status && (status == GOT_STATUS_DELETE))
7551 status = GOT_STATUS_NO_CHANGE;
7552 printf("%c%c %s\n", status, staged_status, path);
7556 static const struct got_error *
7557 cmd_remove(int argc, char *argv[])
7559 const struct got_error *error = NULL;
7560 struct got_worktree *worktree = NULL;
7561 struct got_repository *repo = NULL;
7562 const char *status_codes = NULL;
7564 struct got_pathlist_head paths;
7565 struct got_pathlist_entry *pe;
7566 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7567 int ignore_missing_paths = 0;
7568 int *pack_fds = NULL;
7572 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7575 delete_local_mods = 1;
7576 ignore_missing_paths = 1;
7585 for (i = 0; i < strlen(optarg); i++) {
7586 switch (optarg[i]) {
7587 case GOT_STATUS_MODIFY:
7588 delete_local_mods = 1;
7590 case GOT_STATUS_MISSING:
7591 ignore_missing_paths = 1;
7594 errx(1, "invalid status code '%c'",
7598 status_codes = optarg;
7610 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7617 cwd = getcwd(NULL, 0);
7619 error = got_error_from_errno("getcwd");
7623 error = got_repo_pack_fds_open(&pack_fds);
7627 error = got_worktree_open(&worktree, cwd);
7629 if (error->code == GOT_ERR_NOT_WORKTREE)
7630 error = wrap_not_worktree_error(error, "remove", cwd);
7634 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7639 error = apply_unveil(got_repo_get_path(repo), 1,
7640 got_worktree_get_root_path(worktree));
7644 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7651 TAILQ_FOREACH(pe, &paths, entry) {
7652 if (asprintf(&ondisk_path, "%s/%s",
7653 got_worktree_get_root_path(worktree),
7655 error = got_error_from_errno("asprintf");
7658 if (lstat(ondisk_path, &sb) == -1) {
7659 if (errno == ENOENT) {
7663 error = got_error_from_errno2("lstat",
7669 if (S_ISDIR(sb.st_mode)) {
7670 error = got_error_msg(GOT_ERR_BAD_PATH,
7671 "removing directories requires -R option");
7677 error = got_worktree_schedule_delete(worktree, &paths,
7678 delete_local_mods, status_codes, print_remove_status, NULL,
7679 repo, keep_on_disk, ignore_missing_paths);
7682 const struct got_error *close_err = got_repo_close(repo);
7687 got_worktree_close(worktree);
7689 const struct got_error *pack_err =
7690 got_repo_pack_fds_close(pack_fds);
7694 TAILQ_FOREACH(pe, &paths, entry)
7695 free((char *)pe->path);
7696 got_pathlist_free(&paths);
7704 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7705 "[-R] [patchfile]\n", getprogname());
7709 static const struct got_error *
7710 patch_from_stdin(int *patchfd)
7712 const struct got_error *err = NULL;
7714 char *path, buf[BUFSIZ];
7715 sig_t sighup, sigint, sigquit;
7717 err = got_opentemp_named_fd(&path, patchfd,
7718 GOT_TMPDIR_STR "/got-patch");
7724 sighup = signal(SIGHUP, SIG_DFL);
7725 sigint = signal(SIGINT, SIG_DFL);
7726 sigquit = signal(SIGQUIT, SIG_DFL);
7729 r = read(0, buf, sizeof(buf));
7731 err = got_error_from_errno("read");
7736 if (write(*patchfd, buf, r) == -1) {
7737 err = got_error_from_errno("write");
7742 signal(SIGHUP, sighup);
7743 signal(SIGINT, sigint);
7744 signal(SIGQUIT, sigquit);
7746 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7747 err = got_error_from_errno("lseek");
7757 static const struct got_error *
7758 patch_progress(void *arg, const char *old, const char *new,
7759 unsigned char status, const struct got_error *error, long old_from,
7760 long old_lines, long new_from, long new_lines, long offset,
7761 const struct got_error *hunk_err)
7763 const char *path = new == NULL ? old : new;
7765 while (*path == '/')
7769 printf("%c %s\n", status, path);
7772 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7774 if (offset != 0 || hunk_err != NULL) {
7775 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7776 old_lines, new_from, new_lines);
7777 if (hunk_err != NULL)
7778 printf("%s\n", hunk_err->msg);
7780 printf("applied with offset %ld\n", offset);
7786 static const struct got_error *
7787 cmd_patch(int argc, char *argv[])
7789 const struct got_error *error = NULL, *close_error = NULL;
7790 struct got_worktree *worktree = NULL;
7791 struct got_repository *repo = NULL;
7794 int ch, nop = 0, strip = -1, reverse = 0;
7796 int *pack_fds = NULL;
7798 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7804 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7806 errx(1, "pathname strip count is %s: %s",
7822 error = patch_from_stdin(&patchfd);
7825 } else if (argc == 1) {
7826 patchfd = open(argv[0], O_RDONLY);
7827 if (patchfd == -1) {
7828 error = got_error_from_errno2("open", argv[0]);
7834 if ((cwd = getcwd(NULL, 0)) == NULL) {
7835 error = got_error_from_errno("getcwd");
7839 error = got_repo_pack_fds_open(&pack_fds);
7843 error = got_worktree_open(&worktree, cwd);
7847 const char *repo_path = got_worktree_get_repo_path(worktree);
7848 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7852 error = apply_unveil(got_repo_get_path(repo), 0,
7853 got_worktree_get_root_path(worktree));
7858 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7863 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7864 &patch_progress, NULL, check_cancelled, NULL);
7868 close_error = got_repo_close(repo);
7870 error = close_error;
7872 if (worktree != NULL) {
7873 close_error = got_worktree_close(worktree);
7875 error = close_error;
7878 const struct got_error *pack_err =
7879 got_repo_pack_fds_close(pack_fds);
7890 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7891 "path ...\n", getprogname());
7895 static const struct got_error *
7896 revert_progress(void *arg, unsigned char status, const char *path)
7898 if (status == GOT_STATUS_UNVERSIONED)
7901 while (path[0] == '/')
7903 printf("%c %s\n", status, path);
7907 struct choose_patch_arg {
7908 FILE *patch_script_file;
7912 static const struct got_error *
7913 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7914 int nchanges, const char *action)
7916 const struct got_error *err;
7918 size_t linesize = 0;
7922 case GOT_STATUS_ADD:
7923 printf("A %s\n%s this addition? [y/n] ", path, action);
7925 case GOT_STATUS_DELETE:
7926 printf("D %s\n%s this deletion? [y/n] ", path, action);
7928 case GOT_STATUS_MODIFY:
7929 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7930 return got_error_from_errno("fseek");
7931 printf(GOT_COMMIT_SEP_STR);
7932 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7934 if (linelen == -1 && ferror(patch_file)) {
7935 err = got_error_from_errno("getline");
7940 printf(GOT_COMMIT_SEP_STR);
7941 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7942 path, n, nchanges, action);
7945 return got_error_path(path, GOT_ERR_FILE_STATUS);
7951 static const struct got_error *
7952 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7953 FILE *patch_file, int n, int nchanges)
7955 const struct got_error *err = NULL;
7957 size_t linesize = 0;
7960 struct choose_patch_arg *a = arg;
7962 *choice = GOT_PATCH_CHOICE_NONE;
7964 if (a->patch_script_file) {
7966 err = show_change(status, path, patch_file, n, nchanges,
7970 linelen = getline(&line, &linesize, a->patch_script_file);
7971 if (linelen == -1) {
7972 if (ferror(a->patch_script_file))
7973 return got_error_from_errno("getline");
7976 nl = strchr(line, '\n');
7979 if (strcmp(line, "y") == 0) {
7980 *choice = GOT_PATCH_CHOICE_YES;
7982 } else if (strcmp(line, "n") == 0) {
7983 *choice = GOT_PATCH_CHOICE_NO;
7985 } else if (strcmp(line, "q") == 0 &&
7986 status == GOT_STATUS_MODIFY) {
7987 *choice = GOT_PATCH_CHOICE_QUIT;
7990 printf("invalid response '%s'\n", line);
7995 while (resp != 'y' && resp != 'n' && resp != 'q') {
7996 err = show_change(status, path, patch_file, n, nchanges,
8003 if (status == GOT_STATUS_MODIFY) {
8004 if (resp != 'y' && resp != 'n' && resp != 'q') {
8005 printf("invalid response '%c'\n", resp);
8008 } else if (resp != 'y' && resp != 'n') {
8009 printf("invalid response '%c'\n", resp);
8015 *choice = GOT_PATCH_CHOICE_YES;
8016 else if (resp == 'n')
8017 *choice = GOT_PATCH_CHOICE_NO;
8018 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8019 *choice = GOT_PATCH_CHOICE_QUIT;
8024 static const struct got_error *
8025 cmd_revert(int argc, char *argv[])
8027 const struct got_error *error = NULL;
8028 struct got_worktree *worktree = NULL;
8029 struct got_repository *repo = NULL;
8030 char *cwd = NULL, *path = NULL;
8031 struct got_pathlist_head paths;
8032 struct got_pathlist_entry *pe;
8033 int ch, can_recurse = 0, pflag = 0;
8034 FILE *patch_script_file = NULL;
8035 const char *patch_script_path = NULL;
8036 struct choose_patch_arg cpa;
8037 int *pack_fds = NULL;
8041 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8047 patch_script_path = optarg;
8062 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8063 "unveil", NULL) == -1)
8068 if (patch_script_path && !pflag)
8069 errx(1, "-F option can only be used together with -p option");
8071 cwd = getcwd(NULL, 0);
8073 error = got_error_from_errno("getcwd");
8077 error = got_repo_pack_fds_open(&pack_fds);
8081 error = got_worktree_open(&worktree, cwd);
8083 if (error->code == GOT_ERR_NOT_WORKTREE)
8084 error = wrap_not_worktree_error(error, "revert", cwd);
8088 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8093 if (patch_script_path) {
8094 patch_script_file = fopen(patch_script_path, "re");
8095 if (patch_script_file == NULL) {
8096 error = got_error_from_errno2("fopen",
8101 error = apply_unveil(got_repo_get_path(repo), 1,
8102 got_worktree_get_root_path(worktree));
8106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8113 TAILQ_FOREACH(pe, &paths, entry) {
8114 if (asprintf(&ondisk_path, "%s/%s",
8115 got_worktree_get_root_path(worktree),
8117 error = got_error_from_errno("asprintf");
8120 if (lstat(ondisk_path, &sb) == -1) {
8121 if (errno == ENOENT) {
8125 error = got_error_from_errno2("lstat",
8131 if (S_ISDIR(sb.st_mode)) {
8132 error = got_error_msg(GOT_ERR_BAD_PATH,
8133 "reverting directories requires -R option");
8139 cpa.patch_script_file = patch_script_file;
8140 cpa.action = "revert";
8141 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8142 pflag ? choose_patch : NULL, &cpa, repo);
8144 if (patch_script_file && fclose(patch_script_file) == EOF &&
8146 error = got_error_from_errno2("fclose", patch_script_path);
8148 const struct got_error *close_err = got_repo_close(repo);
8153 got_worktree_close(worktree);
8155 const struct got_error *pack_err =
8156 got_repo_pack_fds_close(pack_fds);
8168 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8169 "[path ...]\n", getprogname());
8173 struct collect_commit_logmsg_arg {
8174 const char *cmdline_log;
8175 const char *prepared_log;
8176 int non_interactive;
8178 const char *worktree_path;
8179 const char *branch_name;
8180 const char *repo_path;
8185 static const struct got_error *
8186 read_prepared_logmsg(char **logmsg, const char *path)
8188 const struct got_error *err = NULL;
8194 memset(&sb, 0, sizeof(sb));
8196 f = fopen(path, "re");
8198 return got_error_from_errno2("fopen", path);
8200 if (fstat(fileno(f), &sb) == -1) {
8201 err = got_error_from_errno2("fstat", path);
8204 if (sb.st_size == 0) {
8205 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8209 *logmsg = malloc(sb.st_size + 1);
8210 if (*logmsg == NULL) {
8211 err = got_error_from_errno("malloc");
8215 r = fread(*logmsg, 1, sb.st_size, f);
8216 if (r != sb.st_size) {
8218 err = got_error_from_errno2("fread", path);
8220 err = got_error(GOT_ERR_IO);
8223 (*logmsg)[sb.st_size] = '\0';
8225 if (fclose(f) == EOF && err == NULL)
8226 err = got_error_from_errno2("fclose", path);
8235 static const struct got_error *
8236 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8239 char *initial_content = NULL;
8240 struct got_pathlist_entry *pe;
8241 const struct got_error *err = NULL;
8242 char *template = NULL;
8243 struct collect_commit_logmsg_arg *a = arg;
8244 int initial_content_len;
8248 /* if a message was specified on the command line, just use it */
8249 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8250 len = strlen(a->cmdline_log) + 1;
8251 *logmsg = malloc(len + 1);
8252 if (*logmsg == NULL)
8253 return got_error_from_errno("malloc");
8254 strlcpy(*logmsg, a->cmdline_log, len);
8256 } else if (a->prepared_log != NULL && a->non_interactive)
8257 return read_prepared_logmsg(logmsg, a->prepared_log);
8259 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8260 return got_error_from_errno("asprintf");
8262 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8266 if (a->prepared_log) {
8268 err = read_prepared_logmsg(&msg, a->prepared_log);
8271 if (write(fd, msg, strlen(msg)) == -1) {
8272 err = got_error_from_errno2("write", a->logmsg_path);
8279 initial_content_len = asprintf(&initial_content,
8280 "\n# changes to be committed on branch %s:\n",
8282 if (initial_content_len == -1) {
8283 err = got_error_from_errno("asprintf");
8287 if (write(fd, initial_content, initial_content_len) == -1) {
8288 err = got_error_from_errno2("write", a->logmsg_path);
8292 TAILQ_FOREACH(pe, commitable_paths, entry) {
8293 struct got_commitable *ct = pe->data;
8294 dprintf(fd, "# %c %s\n",
8295 got_commitable_get_status(ct),
8296 got_commitable_get_path(ct));
8299 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8300 initial_content_len, a->prepared_log ? 0 : 1);
8302 free(initial_content);
8305 if (fd != -1 && close(fd) == -1 && err == NULL)
8306 err = got_error_from_errno2("close", a->logmsg_path);
8308 /* Editor is done; we can now apply unveil(2) */
8310 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8318 static const struct got_error *
8319 cmd_commit(int argc, char *argv[])
8321 const struct got_error *error = NULL;
8322 struct got_worktree *worktree = NULL;
8323 struct got_repository *repo = NULL;
8324 char *cwd = NULL, *id_str = NULL;
8325 struct got_object_id *id = NULL;
8326 const char *logmsg = NULL;
8327 char *prepared_logmsg = NULL;
8328 struct collect_commit_logmsg_arg cl_arg;
8329 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8330 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8331 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8332 struct got_pathlist_head paths;
8333 int *pack_fds = NULL;
8336 cl_arg.logmsg_path = NULL;
8338 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8342 option_conflict('F', 'm');
8343 prepared_logmsg = realpath(optarg, NULL);
8344 if (prepared_logmsg == NULL)
8345 return got_error_from_errno2("realpath",
8349 if (prepared_logmsg)
8350 option_conflict('m', 'F');
8354 non_interactive = 1;
8357 allow_bad_symlinks = 1;
8369 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8370 "unveil", NULL) == -1)
8373 cwd = getcwd(NULL, 0);
8375 error = got_error_from_errno("getcwd");
8379 error = got_repo_pack_fds_open(&pack_fds);
8383 error = got_worktree_open(&worktree, cwd);
8385 if (error->code == GOT_ERR_NOT_WORKTREE)
8386 error = wrap_not_worktree_error(error, "commit", cwd);
8390 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8393 if (rebase_in_progress) {
8394 error = got_error(GOT_ERR_REBASING);
8398 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8403 error = get_gitconfig_path(&gitconfig_path);
8406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8407 gitconfig_path, pack_fds);
8411 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8414 if (merge_in_progress) {
8415 error = got_error(GOT_ERR_MERGE_BUSY);
8419 error = get_author(&author, repo, worktree);
8424 * unveil(2) traverses exec(2); if an editor is used we have
8425 * to apply unveil after the log message has been written.
8427 if (logmsg == NULL || strlen(logmsg) == 0)
8428 error = get_editor(&editor);
8430 error = apply_unveil(got_repo_get_path(repo), 0,
8431 got_worktree_get_root_path(worktree));
8435 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8439 cl_arg.editor = editor;
8440 cl_arg.cmdline_log = logmsg;
8441 cl_arg.prepared_log = prepared_logmsg;
8442 cl_arg.non_interactive = non_interactive;
8443 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8444 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8445 if (!histedit_in_progress) {
8446 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8447 error = got_error(GOT_ERR_COMMIT_BRANCH);
8450 cl_arg.branch_name += 11;
8452 cl_arg.repo_path = got_repo_get_path(repo);
8453 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8454 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8455 print_status, NULL, repo);
8457 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8458 cl_arg.logmsg_path != NULL)
8459 preserve_logmsg = 1;
8463 error = got_object_id_str(&id_str, id);
8466 printf("Created commit %s\n", id_str);
8468 if (preserve_logmsg) {
8469 fprintf(stderr, "%s: log message preserved in %s\n",
8470 getprogname(), cl_arg.logmsg_path);
8471 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8473 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8474 free(cl_arg.logmsg_path);
8476 const struct got_error *close_err = got_repo_close(repo);
8481 got_worktree_close(worktree);
8483 const struct got_error *pack_err =
8484 got_repo_pack_fds_close(pack_fds);
8490 free(gitconfig_path);
8493 free(prepared_logmsg);
8500 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8501 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8502 "[remote-repository]\n", getprogname());
8507 print_load_info(int print_colored, int print_found, int print_trees,
8508 int ncolored, int nfound, int ntrees)
8510 if (print_colored) {
8511 printf("%d commit%s colored", ncolored,
8512 ncolored == 1 ? "" : "s");
8515 printf("%s%d object%s found",
8516 ncolored > 0 ? "; " : "",
8517 nfound, nfound == 1 ? "" : "s");
8520 printf("; %d tree%s scanned", ntrees,
8521 ntrees == 1 ? "" : "s");
8525 struct got_send_progress_arg {
8526 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8533 int last_nobj_total;
8537 int printed_something;
8539 struct got_pathlist_head *delete_branches;
8542 static const struct got_error *
8543 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8544 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8545 int nobj_written, off_t bytes_sent, const char *refname, int success)
8547 struct got_send_progress_arg *a = arg;
8548 char scaled_packsize[FMT_SCALED_STRSIZE];
8549 char scaled_sent[FMT_SCALED_STRSIZE];
8550 int p_deltify = 0, p_written = 0, p_sent = 0;
8551 int print_colored = 0, print_found = 0, print_trees = 0;
8552 int print_searching = 0, print_total = 0;
8553 int print_deltify = 0, print_written = 0, print_sent = 0;
8555 if (a->verbosity < 0)
8559 const char *status = success ? "accepted" : "rejected";
8562 struct got_pathlist_entry *pe;
8563 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8564 const char *branchname = pe->path;
8565 if (got_path_cmp(branchname, refname,
8566 strlen(branchname), strlen(refname)) == 0) {
8568 a->sent_something = 1;
8574 if (a->printed_something)
8576 printf("Server has %s %s", status, refname);
8577 a->printed_something = 1;
8581 if (a->last_ncolored != ncolored) {
8583 a->last_ncolored = ncolored;
8586 if (a->last_nfound != nfound) {
8589 a->last_nfound = nfound;
8592 if (a->last_ntrees != ntrees) {
8596 a->last_ntrees = ntrees;
8599 if ((print_colored || print_found || print_trees) &&
8602 print_load_info(print_colored, print_found, print_trees,
8603 ncolored, nfound, ntrees);
8604 a->printed_something = 1;
8607 } else if (!a->loading_done) {
8609 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8611 a->loading_done = 1;
8614 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8615 return got_error_from_errno("fmt_scaled");
8616 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8617 return got_error_from_errno("fmt_scaled");
8619 if (a->last_ncommits != ncommits) {
8620 print_searching = 1;
8621 a->last_ncommits = ncommits;
8624 if (a->last_nobj_total != nobj_total) {
8625 print_searching = 1;
8627 a->last_nobj_total = nobj_total;
8630 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8631 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8632 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8633 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8634 return got_error(GOT_ERR_NO_SPACE);
8637 if (nobj_deltify > 0 || nobj_written > 0) {
8638 if (nobj_deltify > 0) {
8639 p_deltify = (nobj_deltify * 100) / nobj_total;
8640 if (p_deltify != a->last_p_deltify) {
8641 a->last_p_deltify = p_deltify;
8642 print_searching = 1;
8647 if (nobj_written > 0) {
8648 p_written = (nobj_written * 100) / nobj_total;
8649 if (p_written != a->last_p_written) {
8650 a->last_p_written = p_written;
8651 print_searching = 1;
8659 if (bytes_sent > 0) {
8660 p_sent = (bytes_sent * 100) / packfile_size;
8661 if (p_sent != a->last_p_sent) {
8662 a->last_p_sent = p_sent;
8663 print_searching = 1;
8669 a->sent_something = 1;
8672 if (print_searching || print_total || print_deltify || print_written ||
8675 if (print_searching)
8676 printf("packing %d reference%s", ncommits,
8677 ncommits == 1 ? "" : "s");
8679 printf("; %d object%s", nobj_total,
8680 nobj_total == 1 ? "" : "s");
8682 printf("; deltify: %d%%", p_deltify);
8684 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8685 scaled_packsize, p_sent);
8686 else if (print_written)
8687 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8688 scaled_packsize, p_written);
8689 if (print_searching || print_total || print_deltify ||
8690 print_written || print_sent) {
8691 a->printed_something = 1;
8697 static const struct got_error *
8698 cmd_send(int argc, char *argv[])
8700 const struct got_error *error = NULL;
8701 char *cwd = NULL, *repo_path = NULL;
8702 const char *remote_name;
8703 char *proto = NULL, *host = NULL, *port = NULL;
8704 char *repo_name = NULL, *server_path = NULL;
8705 const struct got_remote_repo *remotes, *remote = NULL;
8706 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8707 struct got_repository *repo = NULL;
8708 struct got_worktree *worktree = NULL;
8709 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8710 struct got_pathlist_head branches;
8711 struct got_pathlist_head tags;
8712 struct got_reflist_head all_branches;
8713 struct got_reflist_head all_tags;
8714 struct got_pathlist_head delete_args;
8715 struct got_pathlist_head delete_branches;
8716 struct got_reflist_entry *re;
8717 struct got_pathlist_entry *pe;
8718 int i, ch, sendfd = -1, sendstatus;
8720 struct got_send_progress_arg spa;
8721 int verbosity = 0, overwrite_refs = 0;
8722 int send_all_branches = 0, send_all_tags = 0;
8723 struct got_reference *ref = NULL;
8724 int *pack_fds = NULL;
8726 TAILQ_INIT(&branches);
8728 TAILQ_INIT(&all_branches);
8729 TAILQ_INIT(&all_tags);
8730 TAILQ_INIT(&delete_args);
8731 TAILQ_INIT(&delete_branches);
8733 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8736 send_all_branches = 1;
8739 error = got_pathlist_append(&branches, optarg, NULL);
8745 error = got_pathlist_append(&delete_args, optarg, NULL);
8753 repo_path = realpath(optarg, NULL);
8754 if (repo_path == NULL)
8755 return got_error_from_errno2("realpath",
8757 got_path_strip_trailing_slashes(repo_path);
8760 error = got_pathlist_append(&tags, optarg, NULL);
8771 else if (verbosity < 3)
8785 if (send_all_branches && !TAILQ_EMPTY(&branches))
8786 option_conflict('a', 'b');
8787 if (send_all_tags && !TAILQ_EMPTY(&tags))
8788 option_conflict('T', 't');
8792 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8794 remote_name = argv[0];
8798 cwd = getcwd(NULL, 0);
8800 error = got_error_from_errno("getcwd");
8804 error = got_repo_pack_fds_open(&pack_fds);
8808 if (repo_path == NULL) {
8809 error = got_worktree_open(&worktree, cwd);
8810 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8816 strdup(got_worktree_get_repo_path(worktree));
8817 if (repo_path == NULL)
8818 error = got_error_from_errno("strdup");
8822 repo_path = strdup(cwd);
8823 if (repo_path == NULL) {
8824 error = got_error_from_errno("strdup");
8830 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8835 worktree_conf = got_worktree_get_gotconfig(worktree);
8836 if (worktree_conf) {
8837 got_gotconfig_get_remotes(&nremotes, &remotes,
8839 for (i = 0; i < nremotes; i++) {
8840 if (strcmp(remotes[i].name, remote_name) == 0) {
8841 remote = &remotes[i];
8847 if (remote == NULL) {
8848 repo_conf = got_repo_get_gotconfig(repo);
8850 got_gotconfig_get_remotes(&nremotes, &remotes,
8852 for (i = 0; i < nremotes; i++) {
8853 if (strcmp(remotes[i].name, remote_name) == 0) {
8854 remote = &remotes[i];
8860 if (remote == NULL) {
8861 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8862 for (i = 0; i < nremotes; i++) {
8863 if (strcmp(remotes[i].name, remote_name) == 0) {
8864 remote = &remotes[i];
8869 if (remote == NULL) {
8870 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8874 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8875 &repo_name, remote->send_url);
8879 if (strcmp(proto, "git") == 0) {
8881 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8882 "sendfd dns inet unveil", NULL) == -1)
8885 } else if (strcmp(proto, "git+ssh") == 0 ||
8886 strcmp(proto, "ssh") == 0) {
8888 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8889 "sendfd unveil", NULL) == -1)
8892 } else if (strcmp(proto, "http") == 0 ||
8893 strcmp(proto, "git+http") == 0) {
8894 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8897 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8901 error = got_dial_apply_unveil(proto);
8905 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8909 if (send_all_branches) {
8910 error = got_ref_list(&all_branches, repo, "refs/heads",
8911 got_ref_cmp_by_name, NULL);
8914 TAILQ_FOREACH(re, &all_branches, entry) {
8915 const char *branchname = got_ref_get_name(re->ref);
8916 error = got_pathlist_append(&branches,
8922 } else if (nbranches == 0) {
8923 for (i = 0; i < remote->nsend_branches; i++) {
8924 got_pathlist_append(&branches,
8925 remote->send_branches[i], NULL);
8929 if (send_all_tags) {
8930 error = got_ref_list(&all_tags, repo, "refs/tags",
8931 got_ref_cmp_by_name, NULL);
8934 TAILQ_FOREACH(re, &all_tags, entry) {
8935 const char *tagname = got_ref_get_name(re->ref);
8936 error = got_pathlist_append(&tags,
8945 * To prevent accidents only branches in refs/heads/ can be deleted
8946 * with 'got send -d'.
8947 * Deleting anything else requires local repository access or Git.
8949 TAILQ_FOREACH(pe, &delete_args, entry) {
8950 const char *branchname = pe->path;
8952 struct got_pathlist_entry *new;
8953 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8954 s = strdup(branchname);
8956 error = got_error_from_errno("strdup");
8960 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8961 error = got_error_from_errno("asprintf");
8965 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8966 if (error || new == NULL /* duplicate */)
8973 if (nbranches == 0 && ndelete_branches == 0) {
8974 struct got_reference *head_ref;
8976 error = got_ref_open(&head_ref, repo,
8977 got_worktree_get_head_ref_name(worktree), 0);
8979 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8982 if (got_ref_is_symbolic(head_ref)) {
8983 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8984 got_ref_close(head_ref);
8989 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8997 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8998 port ? ":" : "", port ? port : "");
9000 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9001 server_path, verbosity);
9005 memset(&spa, 0, sizeof(spa));
9006 spa.last_scaled_packsize[0] = '\0';
9007 spa.last_p_deltify = -1;
9008 spa.last_p_written = -1;
9009 spa.verbosity = verbosity;
9010 spa.delete_branches = &delete_branches;
9011 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9012 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9013 check_cancelled, NULL);
9014 if (spa.printed_something)
9018 if (!spa.sent_something && verbosity >= 0)
9019 printf("Already up-to-date\n");
9022 if (kill(sendpid, SIGTERM) == -1)
9023 error = got_error_from_errno("kill");
9024 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9025 error = got_error_from_errno("waitpid");
9027 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9028 error = got_error_from_errno("close");
9030 const struct got_error *close_err = got_repo_close(repo);
9035 got_worktree_close(worktree);
9037 const struct got_error *pack_err =
9038 got_repo_pack_fds_close(pack_fds);
9044 got_pathlist_free(&branches);
9045 got_pathlist_free(&tags);
9046 got_ref_list_free(&all_branches);
9047 got_ref_list_free(&all_tags);
9048 got_pathlist_free(&delete_args);
9049 TAILQ_FOREACH(pe, &delete_branches, entry)
9050 free((char *)pe->path);
9051 got_pathlist_free(&delete_branches);
9063 usage_cherrypick(void)
9065 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9069 static const struct got_error *
9070 cmd_cherrypick(int argc, char *argv[])
9072 const struct got_error *error = NULL;
9073 struct got_worktree *worktree = NULL;
9074 struct got_repository *repo = NULL;
9075 char *cwd = NULL, *commit_id_str = NULL;
9076 struct got_object_id *commit_id = NULL;
9077 struct got_commit_object *commit = NULL;
9078 struct got_object_qid *pid;
9080 struct got_update_progress_arg upa;
9081 int *pack_fds = NULL;
9083 while ((ch = getopt(argc, argv, "")) != -1) {
9095 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9096 "unveil", NULL) == -1)
9102 cwd = getcwd(NULL, 0);
9104 error = got_error_from_errno("getcwd");
9108 error = got_repo_pack_fds_open(&pack_fds);
9112 error = got_worktree_open(&worktree, cwd);
9114 if (error->code == GOT_ERR_NOT_WORKTREE)
9115 error = wrap_not_worktree_error(error, "cherrypick",
9120 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9125 error = apply_unveil(got_repo_get_path(repo), 0,
9126 got_worktree_get_root_path(worktree));
9130 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9131 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9134 error = got_object_id_str(&commit_id_str, commit_id);
9138 error = got_object_open_as_commit(&commit, repo, commit_id);
9141 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9142 memset(&upa, 0, sizeof(upa));
9143 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9144 commit_id, repo, update_progress, &upa, check_cancelled,
9149 if (upa.did_something)
9150 printf("Merged commit %s\n", commit_id_str);
9151 print_merge_progress_stats(&upa);
9154 got_object_commit_close(commit);
9155 free(commit_id_str);
9157 got_worktree_close(worktree);
9159 const struct got_error *close_err = got_repo_close(repo);
9164 const struct got_error *pack_err =
9165 got_repo_pack_fds_close(pack_fds);
9176 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9180 static const struct got_error *
9181 cmd_backout(int argc, char *argv[])
9183 const struct got_error *error = NULL;
9184 struct got_worktree *worktree = NULL;
9185 struct got_repository *repo = NULL;
9186 char *cwd = NULL, *commit_id_str = NULL;
9187 struct got_object_id *commit_id = NULL;
9188 struct got_commit_object *commit = NULL;
9189 struct got_object_qid *pid;
9191 struct got_update_progress_arg upa;
9192 int *pack_fds = NULL;
9194 while ((ch = getopt(argc, argv, "")) != -1) {
9206 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9207 "unveil", NULL) == -1)
9213 cwd = getcwd(NULL, 0);
9215 error = got_error_from_errno("getcwd");
9219 error = got_repo_pack_fds_open(&pack_fds);
9223 error = got_worktree_open(&worktree, cwd);
9225 if (error->code == GOT_ERR_NOT_WORKTREE)
9226 error = wrap_not_worktree_error(error, "backout", cwd);
9230 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9235 error = apply_unveil(got_repo_get_path(repo), 0,
9236 got_worktree_get_root_path(worktree));
9240 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9241 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9244 error = got_object_id_str(&commit_id_str, commit_id);
9248 error = got_object_open_as_commit(&commit, repo, commit_id);
9251 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9253 error = got_error(GOT_ERR_ROOT_COMMIT);
9257 memset(&upa, 0, sizeof(upa));
9258 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9259 repo, update_progress, &upa, check_cancelled, NULL);
9263 if (upa.did_something)
9264 printf("Backed out commit %s\n", commit_id_str);
9265 print_merge_progress_stats(&upa);
9268 got_object_commit_close(commit);
9269 free(commit_id_str);
9271 got_worktree_close(worktree);
9273 const struct got_error *close_err = got_repo_close(repo);
9278 const struct got_error *pack_err =
9279 got_repo_pack_fds_close(pack_fds);
9289 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9295 trim_logmsg(char *logmsg, int limit)
9300 len = strlen(logmsg);
9304 nl = strchr(logmsg, '\n');
9309 static const struct got_error *
9310 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9312 const struct got_error *err;
9313 char *logmsg0 = NULL;
9316 err = got_object_commit_get_logmsg(&logmsg0, commit);
9321 while (isspace((unsigned char)s[0]))
9324 *logmsg = strdup(s);
9325 if (*logmsg == NULL) {
9326 err = got_error_from_errno("strdup");
9330 trim_logmsg(*logmsg, limit);
9336 static const struct got_error *
9337 show_rebase_merge_conflict(struct got_object_id *id,
9338 struct got_repository *repo)
9340 const struct got_error *err;
9341 struct got_commit_object *commit = NULL;
9342 char *id_str = NULL, *logmsg = NULL;
9344 err = got_object_open_as_commit(&commit, repo, id);
9348 err = got_object_id_str(&id_str, id);
9354 err = get_short_logmsg(&logmsg, 42, commit);
9358 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9361 got_object_commit_close(commit);
9366 static const struct got_error *
9367 show_rebase_progress(struct got_commit_object *commit,
9368 struct got_object_id *old_id, struct got_object_id *new_id)
9370 const struct got_error *err;
9371 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9373 err = got_object_id_str(&old_id_str, old_id);
9378 err = got_object_id_str(&new_id_str, new_id);
9383 old_id_str[12] = '\0';
9385 new_id_str[12] = '\0';
9387 err = get_short_logmsg(&logmsg, 42, commit);
9391 printf("%s -> %s: %s\n", old_id_str,
9392 new_id_str ? new_id_str : "no-op change", logmsg);
9400 static const struct got_error *
9401 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9402 struct got_reference *branch, struct got_reference *new_base_branch,
9403 struct got_reference *tmp_branch, struct got_repository *repo,
9406 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9407 return got_worktree_rebase_complete(worktree, fileindex,
9408 new_base_branch, tmp_branch, branch, repo, create_backup);
9411 static const struct got_error *
9412 rebase_commit(struct got_pathlist_head *merged_paths,
9413 struct got_worktree *worktree, struct got_fileindex *fileindex,
9414 struct got_reference *tmp_branch,
9415 struct got_object_id *commit_id, struct got_repository *repo)
9417 const struct got_error *error;
9418 struct got_commit_object *commit;
9419 struct got_object_id *new_commit_id;
9421 error = got_object_open_as_commit(&commit, repo, commit_id);
9425 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9426 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9428 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9430 error = show_rebase_progress(commit, commit_id, NULL);
9432 error = show_rebase_progress(commit, commit_id, new_commit_id);
9433 free(new_commit_id);
9436 got_object_commit_close(commit);
9440 struct check_path_prefix_arg {
9441 const char *path_prefix;
9446 static const struct got_error *
9447 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9448 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9449 struct got_object_id *id1, struct got_object_id *id2,
9450 const char *path1, const char *path2,
9451 mode_t mode1, mode_t mode2, struct got_repository *repo)
9453 struct check_path_prefix_arg *a = arg;
9455 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9456 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9457 return got_error(a->errcode);
9462 static const struct got_error *
9463 check_path_prefix(struct got_object_id *parent_id,
9464 struct got_object_id *commit_id, const char *path_prefix,
9465 int errcode, struct got_repository *repo)
9467 const struct got_error *err;
9468 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9469 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9470 struct check_path_prefix_arg cpp_arg;
9472 if (got_path_is_root_dir(path_prefix))
9475 err = got_object_open_as_commit(&commit, repo, commit_id);
9479 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9483 err = got_object_open_as_tree(&tree1, repo,
9484 got_object_commit_get_tree_id(parent_commit));
9488 err = got_object_open_as_tree(&tree2, repo,
9489 got_object_commit_get_tree_id(commit));
9493 cpp_arg.path_prefix = path_prefix;
9494 while (cpp_arg.path_prefix[0] == '/')
9495 cpp_arg.path_prefix++;
9496 cpp_arg.len = strlen(cpp_arg.path_prefix);
9497 cpp_arg.errcode = errcode;
9498 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9499 check_path_prefix_in_diff, &cpp_arg, 0);
9502 got_object_tree_close(tree1);
9504 got_object_tree_close(tree2);
9506 got_object_commit_close(commit);
9508 got_object_commit_close(parent_commit);
9512 static const struct got_error *
9513 collect_commits(struct got_object_id_queue *commits,
9514 struct got_object_id *initial_commit_id,
9515 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9516 const char *path_prefix, int path_prefix_errcode,
9517 struct got_repository *repo)
9519 const struct got_error *err = NULL;
9520 struct got_commit_graph *graph = NULL;
9521 struct got_object_id *parent_id = NULL;
9522 struct got_object_qid *qid;
9523 struct got_object_id *commit_id = initial_commit_id;
9525 err = got_commit_graph_open(&graph, "/", 1);
9529 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9530 check_cancelled, NULL);
9533 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9534 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9535 check_cancelled, NULL);
9537 if (err->code == GOT_ERR_ITER_COMPLETED) {
9538 err = got_error_msg(GOT_ERR_ANCESTRY,
9539 "ran out of commits to rebase before "
9540 "youngest common ancestor commit has "
9545 err = check_path_prefix(parent_id, commit_id,
9546 path_prefix, path_prefix_errcode, repo);
9550 err = got_object_qid_alloc(&qid, commit_id);
9553 STAILQ_INSERT_HEAD(commits, qid, entry);
9554 commit_id = parent_id;
9558 got_commit_graph_close(graph);
9562 static const struct got_error *
9563 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9565 const struct got_error *err = NULL;
9566 time_t committer_time;
9568 char datebuf[11]; /* YYYY-MM-DD + NUL */
9569 char *author0 = NULL, *author, *smallerthan;
9570 char *logmsg0 = NULL, *logmsg, *newline;
9572 committer_time = got_object_commit_get_committer_time(commit);
9573 if (gmtime_r(&committer_time, &tm) == NULL)
9574 return got_error_from_errno("gmtime_r");
9575 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9576 return got_error(GOT_ERR_NO_SPACE);
9578 author0 = strdup(got_object_commit_get_author(commit));
9579 if (author0 == NULL)
9580 return got_error_from_errno("strdup");
9582 smallerthan = strchr(author, '<');
9583 if (smallerthan && smallerthan[1] != '\0')
9584 author = smallerthan + 1;
9585 author[strcspn(author, "@>")] = '\0';
9587 err = got_object_commit_get_logmsg(&logmsg0, commit);
9591 while (*logmsg == '\n')
9593 newline = strchr(logmsg, '\n');
9597 if (asprintf(brief_str, "%s %s %s",
9598 datebuf, author, logmsg) == -1)
9599 err = got_error_from_errno("asprintf");
9606 static const struct got_error *
9607 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9608 struct got_repository *repo)
9610 const struct got_error *err;
9613 err = got_object_id_str(&id_str, id);
9617 err = got_ref_delete(ref, repo);
9621 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9627 static const struct got_error *
9628 print_backup_ref(const char *branch_name, const char *new_id_str,
9629 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9630 struct got_reflist_object_id_map *refs_idmap,
9631 struct got_repository *repo)
9633 const struct got_error *err = NULL;
9634 struct got_reflist_head *refs;
9635 char *refs_str = NULL;
9636 struct got_object_id *new_commit_id = NULL;
9637 struct got_commit_object *new_commit = NULL;
9638 char *new_commit_brief_str = NULL;
9639 struct got_object_id *yca_id = NULL;
9640 struct got_commit_object *yca_commit = NULL;
9641 char *yca_id_str = NULL, *yca_brief_str = NULL;
9642 char *custom_refs_str;
9644 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9645 return got_error_from_errno("asprintf");
9647 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9648 0, 0, refs_idmap, custom_refs_str);
9652 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9656 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9658 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9663 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9667 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9671 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9672 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9676 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9677 refs_str ? " (" : "", refs_str ? refs_str : "",
9678 refs_str ? ")" : "", new_commit_brief_str);
9679 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9680 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9684 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9688 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9692 err = got_object_id_str(&yca_id_str, yca_id);
9696 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9698 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9702 printf("history forked at %s%s%s%s\n %s\n",
9704 refs_str ? " (" : "", refs_str ? refs_str : "",
9705 refs_str ? ")" : "", yca_brief_str);
9708 free(custom_refs_str);
9709 free(new_commit_id);
9713 free(yca_brief_str);
9715 got_object_commit_close(new_commit);
9717 got_object_commit_close(yca_commit);
9722 static const struct got_error *
9723 process_backup_refs(const char *backup_ref_prefix,
9724 const char *wanted_branch_name,
9725 int delete, struct got_repository *repo)
9727 const struct got_error *err;
9728 struct got_reflist_head refs, backup_refs;
9729 struct got_reflist_entry *re;
9730 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9731 struct got_object_id *old_commit_id = NULL;
9732 char *branch_name = NULL;
9733 struct got_commit_object *old_commit = NULL;
9734 struct got_reflist_object_id_map *refs_idmap = NULL;
9735 int wanted_branch_found = 0;
9738 TAILQ_INIT(&backup_refs);
9740 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9744 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9748 if (wanted_branch_name) {
9749 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9750 wanted_branch_name += 11;
9753 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9754 got_ref_cmp_by_commit_timestamp_descending, repo);
9758 TAILQ_FOREACH(re, &backup_refs, entry) {
9759 const char *refname = got_ref_get_name(re->ref);
9762 err = check_cancelled(NULL);
9766 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9770 err = got_object_open_as_commit(&old_commit, repo,
9775 if (strncmp(backup_ref_prefix, refname,
9776 backup_ref_prefix_len) == 0)
9777 refname += backup_ref_prefix_len;
9779 while (refname[0] == '/')
9782 branch_name = strdup(refname);
9783 if (branch_name == NULL) {
9784 err = got_error_from_errno("strdup");
9787 slash = strrchr(branch_name, '/');
9790 refname += strlen(branch_name) + 1;
9793 if (wanted_branch_name == NULL ||
9794 strcmp(wanted_branch_name, branch_name) == 0) {
9795 wanted_branch_found = 1;
9797 err = delete_backup_ref(re->ref,
9798 old_commit_id, repo);
9800 err = print_backup_ref(branch_name, refname,
9801 old_commit_id, old_commit, refs_idmap,
9808 free(old_commit_id);
9809 old_commit_id = NULL;
9812 got_object_commit_close(old_commit);
9816 if (wanted_branch_name && !wanted_branch_found) {
9817 err = got_error_fmt(GOT_ERR_NOT_REF,
9818 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9822 got_reflist_object_id_map_free(refs_idmap);
9823 got_ref_list_free(&refs);
9824 got_ref_list_free(&backup_refs);
9825 free(old_commit_id);
9828 got_object_commit_close(old_commit);
9832 static const struct got_error *
9833 abort_progress(void *arg, unsigned char status, const char *path)
9836 * Unversioned files should not clutter progress output when
9837 * an operation is aborted.
9839 if (status == GOT_STATUS_UNVERSIONED)
9842 return update_progress(arg, status, path);
9845 static const struct got_error *
9846 cmd_rebase(int argc, char *argv[])
9848 const struct got_error *error = NULL;
9849 struct got_worktree *worktree = NULL;
9850 struct got_repository *repo = NULL;
9851 struct got_fileindex *fileindex = NULL;
9853 struct got_reference *branch = NULL;
9854 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9855 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9856 struct got_object_id *resume_commit_id = NULL;
9857 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9858 struct got_commit_object *commit = NULL;
9859 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9860 int histedit_in_progress = 0, merge_in_progress = 0;
9861 int create_backup = 1, list_backups = 0, delete_backups = 0;
9862 struct got_object_id_queue commits;
9863 struct got_pathlist_head merged_paths;
9864 const struct got_object_id_queue *parent_ids;
9865 struct got_object_qid *qid, *pid;
9866 struct got_update_progress_arg upa;
9867 int *pack_fds = NULL;
9869 STAILQ_INIT(&commits);
9870 TAILQ_INIT(&merged_paths);
9871 memset(&upa, 0, sizeof(upa));
9873 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9879 continue_rebase = 1;
9897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9898 "unveil", NULL) == -1)
9903 option_conflict('l', 'a');
9904 if (continue_rebase)
9905 option_conflict('l', 'c');
9907 option_conflict('l', 'X');
9908 if (argc != 0 && argc != 1)
9910 } else if (delete_backups) {
9912 option_conflict('X', 'a');
9913 if (continue_rebase)
9914 option_conflict('X', 'c');
9916 option_conflict('l', 'X');
9917 if (argc != 0 && argc != 1)
9920 if (abort_rebase && continue_rebase)
9922 else if (abort_rebase || continue_rebase) {
9925 } else if (argc != 1)
9929 cwd = getcwd(NULL, 0);
9931 error = got_error_from_errno("getcwd");
9935 error = got_repo_pack_fds_open(&pack_fds);
9939 error = got_worktree_open(&worktree, cwd);
9941 if (list_backups || delete_backups) {
9942 if (error->code != GOT_ERR_NOT_WORKTREE)
9945 if (error->code == GOT_ERR_NOT_WORKTREE)
9946 error = wrap_not_worktree_error(error,
9952 error = got_repo_open(&repo,
9953 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9958 error = apply_unveil(got_repo_get_path(repo), 0,
9959 worktree ? got_worktree_get_root_path(worktree) : NULL);
9963 if (list_backups || delete_backups) {
9964 error = process_backup_refs(
9965 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9966 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9967 goto done; /* nothing else to do */
9970 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9974 if (histedit_in_progress) {
9975 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9979 error = got_worktree_merge_in_progress(&merge_in_progress,
9983 if (merge_in_progress) {
9984 error = got_error(GOT_ERR_MERGE_BUSY);
9988 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9993 if (!rebase_in_progress) {
9994 error = got_error(GOT_ERR_NOT_REBASING);
9997 error = got_worktree_rebase_continue(&resume_commit_id,
9998 &new_base_branch, &tmp_branch, &branch, &fileindex,
10002 printf("Switching work tree to %s\n",
10003 got_ref_get_symref_target(new_base_branch));
10004 error = got_worktree_rebase_abort(worktree, fileindex, repo,
10005 new_base_branch, abort_progress, &upa);
10008 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
10009 print_merge_progress_stats(&upa);
10010 goto done; /* nothing else to do */
10013 if (continue_rebase) {
10014 if (!rebase_in_progress) {
10015 error = got_error(GOT_ERR_NOT_REBASING);
10018 error = got_worktree_rebase_continue(&resume_commit_id,
10019 &new_base_branch, &tmp_branch, &branch, &fileindex,
10024 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
10025 resume_commit_id, repo);
10029 yca_id = got_object_id_dup(resume_commit_id);
10030 if (yca_id == NULL) {
10031 error = got_error_from_errno("got_object_id_dup");
10035 error = got_ref_open(&branch, repo, argv[0], 0);
10040 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10044 if (!continue_rebase) {
10045 struct got_object_id *base_commit_id;
10047 base_commit_id = got_worktree_get_base_commit_id(worktree);
10048 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10049 base_commit_id, branch_head_commit_id, 1, repo,
10050 check_cancelled, NULL);
10053 if (yca_id == NULL) {
10054 error = got_error_msg(GOT_ERR_ANCESTRY,
10055 "specified branch shares no common ancestry "
10056 "with work tree's branch");
10060 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10062 if (error->code != GOT_ERR_ANCESTRY)
10066 struct got_pathlist_head paths;
10067 printf("%s is already based on %s\n",
10068 got_ref_get_name(branch),
10069 got_worktree_get_head_ref_name(worktree));
10070 error = switch_head_ref(branch, branch_head_commit_id,
10074 error = got_worktree_set_base_commit_id(worktree, repo,
10075 branch_head_commit_id);
10078 TAILQ_INIT(&paths);
10079 error = got_pathlist_append(&paths, "", NULL);
10082 error = got_worktree_checkout_files(worktree,
10083 &paths, repo, update_progress, &upa,
10084 check_cancelled, NULL);
10085 got_pathlist_free(&paths);
10088 if (upa.did_something) {
10090 error = got_object_id_str(&id_str,
10091 branch_head_commit_id);
10094 printf("Updated to %s: %s\n",
10095 got_worktree_get_head_ref_name(worktree),
10099 printf("Already up-to-date\n");
10100 print_update_progress_stats(&upa);
10105 commit_id = branch_head_commit_id;
10106 error = got_object_open_as_commit(&commit, repo, commit_id);
10110 parent_ids = got_object_commit_get_parent_ids(commit);
10111 pid = STAILQ_FIRST(parent_ids);
10113 error = got_error(GOT_ERR_EMPTY_REBASE);
10116 error = collect_commits(&commits, commit_id, &pid->id,
10117 yca_id, got_worktree_get_path_prefix(worktree),
10118 GOT_ERR_REBASE_PATH, repo);
10119 got_object_commit_close(commit);
10124 if (!continue_rebase) {
10125 error = got_worktree_rebase_prepare(&new_base_branch,
10126 &tmp_branch, &fileindex, worktree, branch, repo);
10131 if (STAILQ_EMPTY(&commits)) {
10132 if (continue_rebase) {
10133 error = rebase_complete(worktree, fileindex,
10134 branch, new_base_branch, tmp_branch, repo,
10138 /* Fast-forward the reference of the branch. */
10139 struct got_object_id *new_head_commit_id;
10141 error = got_ref_resolve(&new_head_commit_id, repo,
10145 error = got_object_id_str(&id_str, new_head_commit_id);
10146 printf("Forwarding %s to commit %s\n",
10147 got_ref_get_name(branch), id_str);
10149 error = got_ref_change_ref(branch,
10150 new_head_commit_id);
10153 /* No backup needed since objects did not change. */
10159 STAILQ_FOREACH(qid, &commits, entry) {
10161 commit_id = &qid->id;
10162 parent_id = pid ? &pid->id : yca_id;
10165 memset(&upa, 0, sizeof(upa));
10166 error = got_worktree_rebase_merge_files(&merged_paths,
10167 worktree, fileindex, parent_id, commit_id, repo,
10168 update_progress, &upa, check_cancelled, NULL);
10172 print_merge_progress_stats(&upa);
10173 if (upa.conflicts > 0 || upa.missing > 0 ||
10174 upa.not_deleted > 0 || upa.unversioned > 0) {
10175 if (upa.conflicts > 0) {
10176 error = show_rebase_merge_conflict(&qid->id,
10181 got_worktree_rebase_pathlist_free(&merged_paths);
10185 error = rebase_commit(&merged_paths, worktree, fileindex,
10186 tmp_branch, commit_id, repo);
10187 got_worktree_rebase_pathlist_free(&merged_paths);
10192 if (upa.conflicts > 0 || upa.missing > 0 ||
10193 upa.not_deleted > 0 || upa.unversioned > 0) {
10194 error = got_worktree_rebase_postpone(worktree, fileindex);
10197 if (upa.conflicts > 0 && upa.missing == 0 &&
10198 upa.not_deleted == 0 && upa.unversioned == 0) {
10199 error = got_error_msg(GOT_ERR_CONFLICTS,
10200 "conflicts must be resolved before rebasing "
10202 } else if (upa.conflicts > 0) {
10203 error = got_error_msg(GOT_ERR_CONFLICTS,
10204 "conflicts must be resolved before rebasing "
10205 "can continue; changes destined for some "
10206 "files were not yet merged and should be "
10207 "merged manually if required before the "
10208 "rebase operation is continued");
10210 error = got_error_msg(GOT_ERR_CONFLICTS,
10211 "changes destined for some files were not "
10212 "yet merged and should be merged manually "
10213 "if required before the rebase operation "
10217 error = rebase_complete(worktree, fileindex, branch,
10218 new_base_branch, tmp_branch, repo, create_backup);
10220 got_object_id_queue_free(&commits);
10221 free(branch_head_commit_id);
10222 free(resume_commit_id);
10225 got_object_commit_close(commit);
10227 got_ref_close(branch);
10228 if (new_base_branch)
10229 got_ref_close(new_base_branch);
10231 got_ref_close(tmp_branch);
10233 got_worktree_close(worktree);
10235 const struct got_error *close_err = got_repo_close(repo);
10240 const struct got_error *pack_err =
10241 got_repo_pack_fds_close(pack_fds);
10249 usage_histedit(void)
10251 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10252 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10257 #define GOT_HISTEDIT_PICK 'p'
10258 #define GOT_HISTEDIT_EDIT 'e'
10259 #define GOT_HISTEDIT_FOLD 'f'
10260 #define GOT_HISTEDIT_DROP 'd'
10261 #define GOT_HISTEDIT_MESG 'm'
10263 static const struct got_histedit_cmd {
10264 unsigned char code;
10267 } got_histedit_cmds[] = {
10268 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10269 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10270 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10272 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10273 { GOT_HISTEDIT_MESG, "mesg",
10274 "single-line log message for commit above (open editor if empty)" },
10277 struct got_histedit_list_entry {
10278 TAILQ_ENTRY(got_histedit_list_entry) entry;
10279 struct got_object_id *commit_id;
10280 const struct got_histedit_cmd *cmd;
10283 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10285 static const struct got_error *
10286 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10287 FILE *f, struct got_repository *repo)
10289 const struct got_error *err = NULL;
10290 char *logmsg = NULL, *id_str = NULL;
10291 struct got_commit_object *commit = NULL;
10294 err = got_object_open_as_commit(&commit, repo, commit_id);
10298 err = get_short_logmsg(&logmsg, 34, commit);
10302 err = got_object_id_str(&id_str, commit_id);
10306 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10308 err = got_ferror(f, GOT_ERR_IO);
10311 got_object_commit_close(commit);
10317 static const struct got_error *
10318 histedit_write_commit_list(struct got_object_id_queue *commits,
10319 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10320 struct got_repository *repo)
10322 const struct got_error *err = NULL;
10323 struct got_object_qid *qid;
10324 const char *histedit_cmd = NULL;
10326 if (STAILQ_EMPTY(commits))
10327 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10329 STAILQ_FOREACH(qid, commits, entry) {
10330 histedit_cmd = got_histedit_cmds[0].name;
10332 histedit_cmd = "edit";
10333 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10334 histedit_cmd = "fold";
10335 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10338 if (edit_logmsg_only) {
10339 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10341 err = got_ferror(f, GOT_ERR_IO);
10350 static const struct got_error *
10351 write_cmd_list(FILE *f, const char *branch_name,
10352 struct got_object_id_queue *commits)
10354 const struct got_error *err = NULL;
10358 struct got_object_qid *qid;
10360 qid = STAILQ_FIRST(commits);
10361 err = got_object_id_str(&id_str, &qid->id);
10366 "# Editing the history of branch '%s' starting at\n"
10368 "# Commits will be processed in order from top to "
10369 "bottom of this file.\n", branch_name, id_str);
10371 err = got_ferror(f, GOT_ERR_IO);
10375 n = fprintf(f, "# Available histedit commands:\n");
10377 err = got_ferror(f, GOT_ERR_IO);
10381 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10382 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10383 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10386 err = got_ferror(f, GOT_ERR_IO);
10395 static const struct got_error *
10396 histedit_syntax_error(int lineno)
10398 static char msg[42];
10401 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10403 if (ret == -1 || ret >= sizeof(msg))
10404 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10406 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10409 static const struct got_error *
10410 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10411 char *logmsg, struct got_repository *repo)
10413 const struct got_error *err;
10414 struct got_commit_object *folded_commit = NULL;
10415 char *id_str, *folded_logmsg = NULL;
10417 err = got_object_id_str(&id_str, hle->commit_id);
10421 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10425 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10428 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10429 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10430 folded_logmsg) == -1) {
10431 err = got_error_from_errno("asprintf");
10435 got_object_commit_close(folded_commit);
10437 free(folded_logmsg);
10441 static struct got_histedit_list_entry *
10442 get_folded_commits(struct got_histedit_list_entry *hle)
10444 struct got_histedit_list_entry *prev, *folded = NULL;
10446 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10447 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10448 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10449 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10451 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10457 static const struct got_error *
10458 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10459 struct got_repository *repo)
10461 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10462 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10463 const struct got_error *err = NULL;
10464 struct got_commit_object *commit = NULL;
10467 struct got_histedit_list_entry *folded = NULL;
10469 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10473 folded = get_folded_commits(hle);
10475 while (folded != hle) {
10476 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10477 folded = TAILQ_NEXT(folded, entry);
10480 err = append_folded_commit_msg(&new_msg, folded,
10486 folded = TAILQ_NEXT(folded, entry);
10490 err = got_object_id_str(&id_str, hle->commit_id);
10493 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10496 logmsg_len = asprintf(&new_msg,
10497 "%s\n# original log message of commit %s: %s",
10498 logmsg ? logmsg : "", id_str, orig_logmsg);
10499 if (logmsg_len == -1) {
10500 err = got_error_from_errno("asprintf");
10506 err = got_object_id_str(&id_str, hle->commit_id);
10510 err = got_opentemp_named_fd(&logmsg_path, &fd,
10511 GOT_TMPDIR_STR "/got-logmsg");
10515 write(fd, logmsg, logmsg_len);
10518 err = get_editor(&editor);
10522 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10525 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10528 hle->logmsg = strdup(new_msg);
10529 if (hle->logmsg == NULL)
10530 err = got_error_from_errno("strdup");
10533 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10534 err = got_error_from_errno2("unlink", logmsg_path);
10540 got_object_commit_close(commit);
10544 static const struct got_error *
10545 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10546 FILE *f, struct got_repository *repo)
10548 const struct got_error *err = NULL;
10549 char *line = NULL, *p, *end;
10553 const struct got_histedit_cmd *cmd;
10554 struct got_object_id *commit_id = NULL;
10555 struct got_histedit_list_entry *hle = NULL;
10558 len = getline(&line, &size, f);
10560 const struct got_error *getline_err;
10563 getline_err = got_error_from_errno("getline");
10564 err = got_ferror(f, getline_err->code);
10569 while (isspace((unsigned char)p[0]))
10571 if (p[0] == '#' || p[0] == '\0') {
10577 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10578 cmd = &got_histedit_cmds[i];
10579 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10580 isspace((unsigned char)p[strlen(cmd->name)])) {
10581 p += strlen(cmd->name);
10584 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10589 if (i == nitems(got_histedit_cmds)) {
10590 err = histedit_syntax_error(lineno);
10593 while (isspace((unsigned char)p[0]))
10595 if (cmd->code == GOT_HISTEDIT_MESG) {
10596 if (hle == NULL || hle->logmsg != NULL) {
10597 err = got_error(GOT_ERR_HISTEDIT_CMD);
10600 if (p[0] == '\0') {
10601 err = histedit_edit_logmsg(hle, repo);
10605 hle->logmsg = strdup(p);
10606 if (hle->logmsg == NULL) {
10607 err = got_error_from_errno("strdup");
10616 while (end[0] && !isspace((unsigned char)end[0]))
10620 err = got_object_resolve_id_str(&commit_id, repo, p);
10622 /* override error code */
10623 err = histedit_syntax_error(lineno);
10627 hle = malloc(sizeof(*hle));
10629 err = got_error_from_errno("malloc");
10633 hle->commit_id = commit_id;
10634 hle->logmsg = NULL;
10638 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10646 static const struct got_error *
10647 histedit_check_script(struct got_histedit_list *histedit_cmds,
10648 struct got_object_id_queue *commits, struct got_repository *repo)
10650 const struct got_error *err = NULL;
10651 struct got_object_qid *qid;
10652 struct got_histedit_list_entry *hle;
10653 static char msg[92];
10656 if (TAILQ_EMPTY(histedit_cmds))
10657 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10658 "histedit script contains no commands");
10659 if (STAILQ_EMPTY(commits))
10660 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10662 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10663 struct got_histedit_list_entry *hle2;
10664 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10667 if (got_object_id_cmp(hle->commit_id,
10668 hle2->commit_id) != 0)
10670 err = got_object_id_str(&id_str, hle->commit_id);
10673 snprintf(msg, sizeof(msg), "commit %s is listed "
10674 "more than once in histedit script", id_str);
10676 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10680 STAILQ_FOREACH(qid, commits, entry) {
10681 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10682 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10686 err = got_object_id_str(&id_str, &qid->id);
10689 snprintf(msg, sizeof(msg),
10690 "commit %s missing from histedit script", id_str);
10692 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10696 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10697 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10698 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10699 "last commit in histedit script cannot be folded");
10704 static const struct got_error *
10705 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10706 const char *path, struct got_object_id_queue *commits,
10707 struct got_repository *repo)
10709 const struct got_error *err = NULL;
10713 err = get_editor(&editor);
10717 if (spawn_editor(editor, path) == -1) {
10718 err = got_error_from_errno("failed spawning editor");
10722 f = fopen(path, "re");
10724 err = got_error_from_errno("fopen");
10727 err = histedit_parse_list(histedit_cmds, f, repo);
10731 err = histedit_check_script(histedit_cmds, commits, repo);
10733 if (f && fclose(f) == EOF && err == NULL)
10734 err = got_error_from_errno("fclose");
10739 static const struct got_error *
10740 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10741 struct got_object_id_queue *, const char *, const char *,
10742 struct got_repository *);
10744 static const struct got_error *
10745 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10746 struct got_object_id_queue *commits, const char *branch_name,
10747 int edit_logmsg_only, int fold_only, int edit_only,
10748 struct got_repository *repo)
10750 const struct got_error *err;
10754 err = got_opentemp_named(&path, &f, "got-histedit");
10758 err = write_cmd_list(f, branch_name, commits);
10762 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10763 fold_only, edit_only, repo);
10767 if (edit_logmsg_only || fold_only || edit_only) {
10769 err = histedit_parse_list(histedit_cmds, f, repo);
10771 if (fclose(f) == EOF) {
10772 err = got_error_from_errno("fclose");
10776 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10778 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10779 err->code != GOT_ERR_HISTEDIT_CMD)
10781 err = histedit_edit_list_retry(histedit_cmds, err,
10782 commits, path, branch_name, repo);
10786 if (f && fclose(f) == EOF && err == NULL)
10787 err = got_error_from_errno("fclose");
10788 if (path && unlink(path) != 0 && err == NULL)
10789 err = got_error_from_errno2("unlink", path);
10794 static const struct got_error *
10795 histedit_save_list(struct got_histedit_list *histedit_cmds,
10796 struct got_worktree *worktree, struct got_repository *repo)
10798 const struct got_error *err = NULL;
10801 struct got_histedit_list_entry *hle;
10802 struct got_commit_object *commit = NULL;
10804 err = got_worktree_get_histedit_script_path(&path, worktree);
10808 f = fopen(path, "we");
10810 err = got_error_from_errno2("fopen", path);
10813 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10814 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10820 int n = fprintf(f, "%c %s\n",
10821 GOT_HISTEDIT_MESG, hle->logmsg);
10823 err = got_ferror(f, GOT_ERR_IO);
10829 if (f && fclose(f) == EOF && err == NULL)
10830 err = got_error_from_errno("fclose");
10833 got_object_commit_close(commit);
10838 histedit_free_list(struct got_histedit_list *histedit_cmds)
10840 struct got_histedit_list_entry *hle;
10842 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10843 TAILQ_REMOVE(histedit_cmds, hle, entry);
10848 static const struct got_error *
10849 histedit_load_list(struct got_histedit_list *histedit_cmds,
10850 const char *path, struct got_repository *repo)
10852 const struct got_error *err = NULL;
10855 f = fopen(path, "re");
10857 err = got_error_from_errno2("fopen", path);
10861 err = histedit_parse_list(histedit_cmds, f, repo);
10863 if (f && fclose(f) == EOF && err == NULL)
10864 err = got_error_from_errno("fclose");
10868 static const struct got_error *
10869 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10870 const struct got_error *edit_err, struct got_object_id_queue *commits,
10871 const char *path, const char *branch_name, struct got_repository *repo)
10873 const struct got_error *err = NULL, *prev_err = edit_err;
10876 while (resp != 'c' && resp != 'r' && resp != 'a') {
10877 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10878 "or (a)bort: ", getprogname(), prev_err->msg);
10883 histedit_free_list(histedit_cmds);
10884 err = histedit_run_editor(histedit_cmds, path, commits,
10887 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10888 err->code != GOT_ERR_HISTEDIT_CMD)
10895 } else if (resp == 'r') {
10896 histedit_free_list(histedit_cmds);
10897 err = histedit_edit_script(histedit_cmds,
10898 commits, branch_name, 0, 0, 0, repo);
10900 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10901 err->code != GOT_ERR_HISTEDIT_CMD)
10908 } else if (resp == 'a') {
10909 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10912 printf("invalid response '%c'\n", resp);
10918 static const struct got_error *
10919 histedit_complete(struct got_worktree *worktree,
10920 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10921 struct got_reference *branch, struct got_repository *repo)
10923 printf("Switching work tree to %s\n",
10924 got_ref_get_symref_target(branch));
10925 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10929 static const struct got_error *
10930 show_histedit_progress(struct got_commit_object *commit,
10931 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10933 const struct got_error *err;
10934 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10936 err = got_object_id_str(&old_id_str, hle->commit_id);
10941 err = got_object_id_str(&new_id_str, new_id);
10946 old_id_str[12] = '\0';
10948 new_id_str[12] = '\0';
10951 logmsg = strdup(hle->logmsg);
10952 if (logmsg == NULL) {
10953 err = got_error_from_errno("strdup");
10956 trim_logmsg(logmsg, 42);
10958 err = get_short_logmsg(&logmsg, 42, commit);
10963 switch (hle->cmd->code) {
10964 case GOT_HISTEDIT_PICK:
10965 case GOT_HISTEDIT_EDIT:
10966 printf("%s -> %s: %s\n", old_id_str,
10967 new_id_str ? new_id_str : "no-op change", logmsg);
10969 case GOT_HISTEDIT_DROP:
10970 case GOT_HISTEDIT_FOLD:
10971 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10983 static const struct got_error *
10984 histedit_commit(struct got_pathlist_head *merged_paths,
10985 struct got_worktree *worktree, struct got_fileindex *fileindex,
10986 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10987 struct got_repository *repo)
10989 const struct got_error *err;
10990 struct got_commit_object *commit;
10991 struct got_object_id *new_commit_id;
10993 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10994 && hle->logmsg == NULL) {
10995 err = histedit_edit_logmsg(hle, repo);
11000 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11004 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
11005 worktree, fileindex, tmp_branch, commit, hle->commit_id,
11006 hle->logmsg, repo);
11008 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
11010 err = show_histedit_progress(commit, hle, NULL);
11012 err = show_histedit_progress(commit, hle, new_commit_id);
11013 free(new_commit_id);
11016 got_object_commit_close(commit);
11020 static const struct got_error *
11021 histedit_skip_commit(struct got_histedit_list_entry *hle,
11022 struct got_worktree *worktree, struct got_repository *repo)
11024 const struct got_error *error;
11025 struct got_commit_object *commit;
11027 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11032 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11036 error = show_histedit_progress(commit, hle, NULL);
11037 got_object_commit_close(commit);
11041 static const struct got_error *
11042 check_local_changes(void *arg, unsigned char status,
11043 unsigned char staged_status, const char *path,
11044 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11045 struct got_object_id *commit_id, int dirfd, const char *de_name)
11047 int *have_local_changes = arg;
11050 case GOT_STATUS_ADD:
11051 case GOT_STATUS_DELETE:
11052 case GOT_STATUS_MODIFY:
11053 case GOT_STATUS_CONFLICT:
11054 *have_local_changes = 1;
11055 return got_error(GOT_ERR_CANCELLED);
11060 switch (staged_status) {
11061 case GOT_STATUS_ADD:
11062 case GOT_STATUS_DELETE:
11063 case GOT_STATUS_MODIFY:
11064 *have_local_changes = 1;
11065 return got_error(GOT_ERR_CANCELLED);
11073 static const struct got_error *
11074 cmd_histedit(int argc, char *argv[])
11076 const struct got_error *error = NULL;
11077 struct got_worktree *worktree = NULL;
11078 struct got_fileindex *fileindex = NULL;
11079 struct got_repository *repo = NULL;
11081 struct got_reference *branch = NULL;
11082 struct got_reference *tmp_branch = NULL;
11083 struct got_object_id *resume_commit_id = NULL;
11084 struct got_object_id *base_commit_id = NULL;
11085 struct got_object_id *head_commit_id = NULL;
11086 struct got_commit_object *commit = NULL;
11087 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11088 struct got_update_progress_arg upa;
11089 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11090 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11091 int list_backups = 0, delete_backups = 0;
11092 const char *edit_script_path = NULL;
11093 struct got_object_id_queue commits;
11094 struct got_pathlist_head merged_paths;
11095 const struct got_object_id_queue *parent_ids;
11096 struct got_object_qid *pid;
11097 struct got_histedit_list histedit_cmds;
11098 struct got_histedit_list_entry *hle;
11099 int *pack_fds = NULL;
11101 STAILQ_INIT(&commits);
11102 TAILQ_INIT(&histedit_cmds);
11103 TAILQ_INIT(&merged_paths);
11104 memset(&upa, 0, sizeof(upa));
11106 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11121 edit_script_path = optarg;
11124 edit_logmsg_only = 1;
11130 delete_backups = 1;
11142 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11143 "unveil", NULL) == -1)
11146 if (abort_edit && continue_edit)
11147 option_conflict('a', 'c');
11148 if (edit_script_path && edit_logmsg_only)
11149 option_conflict('F', 'm');
11150 if (abort_edit && edit_logmsg_only)
11151 option_conflict('a', 'm');
11152 if (continue_edit && edit_logmsg_only)
11153 option_conflict('c', 'm');
11154 if (abort_edit && fold_only)
11155 option_conflict('a', 'f');
11156 if (continue_edit && fold_only)
11157 option_conflict('c', 'f');
11158 if (fold_only && edit_logmsg_only)
11159 option_conflict('f', 'm');
11160 if (edit_script_path && fold_only)
11161 option_conflict('F', 'f');
11162 if (abort_edit && edit_only)
11163 option_conflict('a', 'e');
11164 if (continue_edit && edit_only)
11165 option_conflict('c', 'e');
11166 if (edit_only && edit_logmsg_only)
11167 option_conflict('e', 'm');
11168 if (edit_script_path && edit_only)
11169 option_conflict('F', 'e');
11170 if (list_backups) {
11172 option_conflict('l', 'a');
11174 option_conflict('l', 'c');
11175 if (edit_script_path)
11176 option_conflict('l', 'F');
11177 if (edit_logmsg_only)
11178 option_conflict('l', 'm');
11180 option_conflict('l', 'f');
11182 option_conflict('l', 'e');
11183 if (delete_backups)
11184 option_conflict('l', 'X');
11185 if (argc != 0 && argc != 1)
11187 } else if (delete_backups) {
11189 option_conflict('X', 'a');
11191 option_conflict('X', 'c');
11192 if (edit_script_path)
11193 option_conflict('X', 'F');
11194 if (edit_logmsg_only)
11195 option_conflict('X', 'm');
11197 option_conflict('X', 'f');
11199 option_conflict('X', 'e');
11201 option_conflict('X', 'l');
11202 if (argc != 0 && argc != 1)
11204 } else if (argc != 0)
11208 * This command cannot apply unveil(2) in all cases because the
11209 * user may choose to run an editor to edit the histedit script
11210 * and to edit individual commit log messages.
11211 * unveil(2) traverses exec(2); if an editor is used we have to
11212 * apply unveil after edit script and log messages have been written.
11213 * XXX TODO: Make use of unveil(2) where possible.
11216 cwd = getcwd(NULL, 0);
11218 error = got_error_from_errno("getcwd");
11222 error = got_repo_pack_fds_open(&pack_fds);
11226 error = got_worktree_open(&worktree, cwd);
11228 if (list_backups || delete_backups) {
11229 if (error->code != GOT_ERR_NOT_WORKTREE)
11232 if (error->code == GOT_ERR_NOT_WORKTREE)
11233 error = wrap_not_worktree_error(error,
11239 if (list_backups || delete_backups) {
11240 error = got_repo_open(&repo,
11241 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11245 error = apply_unveil(got_repo_get_path(repo), 0,
11246 worktree ? got_worktree_get_root_path(worktree) : NULL);
11249 error = process_backup_refs(
11250 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11251 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11252 goto done; /* nothing else to do */
11255 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11260 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11263 if (rebase_in_progress) {
11264 error = got_error(GOT_ERR_REBASING);
11268 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11272 if (merge_in_progress) {
11273 error = got_error(GOT_ERR_MERGE_BUSY);
11277 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11281 if (edit_in_progress && edit_logmsg_only) {
11282 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11283 "histedit operation is in progress in this "
11284 "work tree and must be continued or aborted "
11285 "before the -m option can be used");
11288 if (edit_in_progress && fold_only) {
11289 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11290 "histedit operation is in progress in this "
11291 "work tree and must be continued or aborted "
11292 "before the -f option can be used");
11295 if (edit_in_progress && edit_only) {
11296 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11297 "histedit operation is in progress in this "
11298 "work tree and must be continued or aborted "
11299 "before the -e option can be used");
11303 if (edit_in_progress && abort_edit) {
11304 error = got_worktree_histedit_continue(&resume_commit_id,
11305 &tmp_branch, &branch, &base_commit_id, &fileindex,
11309 printf("Switching work tree to %s\n",
11310 got_ref_get_symref_target(branch));
11311 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11312 branch, base_commit_id, abort_progress, &upa);
11315 printf("Histedit of %s aborted\n",
11316 got_ref_get_symref_target(branch));
11317 print_merge_progress_stats(&upa);
11318 goto done; /* nothing else to do */
11319 } else if (abort_edit) {
11320 error = got_error(GOT_ERR_NOT_HISTEDIT);
11324 if (continue_edit) {
11327 if (!edit_in_progress) {
11328 error = got_error(GOT_ERR_NOT_HISTEDIT);
11332 error = got_worktree_get_histedit_script_path(&path, worktree);
11336 error = histedit_load_list(&histedit_cmds, path, repo);
11341 error = got_worktree_histedit_continue(&resume_commit_id,
11342 &tmp_branch, &branch, &base_commit_id, &fileindex,
11347 error = got_ref_resolve(&head_commit_id, repo, branch);
11351 error = got_object_open_as_commit(&commit, repo,
11355 parent_ids = got_object_commit_get_parent_ids(commit);
11356 pid = STAILQ_FIRST(parent_ids);
11358 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11361 error = collect_commits(&commits, head_commit_id, &pid->id,
11362 base_commit_id, got_worktree_get_path_prefix(worktree),
11363 GOT_ERR_HISTEDIT_PATH, repo);
11364 got_object_commit_close(commit);
11369 if (edit_in_progress) {
11370 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11374 error = got_ref_open(&branch, repo,
11375 got_worktree_get_head_ref_name(worktree), 0);
11379 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11380 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11381 "will not edit commit history of a branch outside "
11382 "the \"refs/heads/\" reference namespace");
11386 error = got_ref_resolve(&head_commit_id, repo, branch);
11387 got_ref_close(branch);
11392 error = got_object_open_as_commit(&commit, repo,
11396 parent_ids = got_object_commit_get_parent_ids(commit);
11397 pid = STAILQ_FIRST(parent_ids);
11399 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11402 error = collect_commits(&commits, head_commit_id, &pid->id,
11403 got_worktree_get_base_commit_id(worktree),
11404 got_worktree_get_path_prefix(worktree),
11405 GOT_ERR_HISTEDIT_PATH, repo);
11406 got_object_commit_close(commit);
11411 if (STAILQ_EMPTY(&commits)) {
11412 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11416 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11417 &base_commit_id, &fileindex, worktree, repo);
11421 if (edit_script_path) {
11422 error = histedit_load_list(&histedit_cmds,
11423 edit_script_path, repo);
11425 got_worktree_histedit_abort(worktree, fileindex,
11426 repo, branch, base_commit_id,
11427 abort_progress, &upa);
11428 print_merge_progress_stats(&upa);
11432 const char *branch_name;
11433 branch_name = got_ref_get_symref_target(branch);
11434 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11436 error = histedit_edit_script(&histedit_cmds, &commits,
11437 branch_name, edit_logmsg_only, fold_only,
11440 got_worktree_histedit_abort(worktree, fileindex,
11441 repo, branch, base_commit_id,
11442 abort_progress, &upa);
11443 print_merge_progress_stats(&upa);
11449 error = histedit_save_list(&histedit_cmds, worktree,
11452 got_worktree_histedit_abort(worktree, fileindex,
11453 repo, branch, base_commit_id,
11454 abort_progress, &upa);
11455 print_merge_progress_stats(&upa);
11461 error = histedit_check_script(&histedit_cmds, &commits, repo);
11465 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11466 if (resume_commit_id) {
11467 if (got_object_id_cmp(hle->commit_id,
11468 resume_commit_id) != 0)
11471 resume_commit_id = NULL;
11472 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11473 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11474 error = histedit_skip_commit(hle, worktree,
11479 struct got_pathlist_head paths;
11480 int have_changes = 0;
11482 TAILQ_INIT(&paths);
11483 error = got_pathlist_append(&paths, "", NULL);
11486 error = got_worktree_status(worktree, &paths,
11487 repo, 0, check_local_changes, &have_changes,
11488 check_cancelled, NULL);
11489 got_pathlist_free(&paths);
11491 if (error->code != GOT_ERR_CANCELLED)
11493 if (sigint_received || sigpipe_received)
11496 if (have_changes) {
11497 error = histedit_commit(NULL, worktree,
11498 fileindex, tmp_branch, hle, repo);
11502 error = got_object_open_as_commit(
11503 &commit, repo, hle->commit_id);
11506 error = show_histedit_progress(commit,
11508 got_object_commit_close(commit);
11517 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11518 error = histedit_skip_commit(hle, worktree, repo);
11524 error = got_object_open_as_commit(&commit, repo,
11528 parent_ids = got_object_commit_get_parent_ids(commit);
11529 pid = STAILQ_FIRST(parent_ids);
11531 error = got_worktree_histedit_merge_files(&merged_paths,
11532 worktree, fileindex, &pid->id, hle->commit_id, repo,
11533 update_progress, &upa, check_cancelled, NULL);
11536 got_object_commit_close(commit);
11539 print_merge_progress_stats(&upa);
11540 if (upa.conflicts > 0 || upa.missing > 0 ||
11541 upa.not_deleted > 0 || upa.unversioned > 0) {
11542 if (upa.conflicts > 0) {
11543 error = show_rebase_merge_conflict(
11544 hle->commit_id, repo);
11548 got_worktree_rebase_pathlist_free(&merged_paths);
11552 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11554 error = got_object_id_str(&id_str, hle->commit_id);
11557 printf("Stopping histedit for amending commit %s\n",
11560 got_worktree_rebase_pathlist_free(&merged_paths);
11561 error = got_worktree_histedit_postpone(worktree,
11566 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11567 error = histedit_skip_commit(hle, worktree, repo);
11573 error = histedit_commit(&merged_paths, worktree, fileindex,
11574 tmp_branch, hle, repo);
11575 got_worktree_rebase_pathlist_free(&merged_paths);
11580 if (upa.conflicts > 0 || upa.missing > 0 ||
11581 upa.not_deleted > 0 || upa.unversioned > 0) {
11582 error = got_worktree_histedit_postpone(worktree, fileindex);
11585 if (upa.conflicts > 0 && upa.missing == 0 &&
11586 upa.not_deleted == 0 && upa.unversioned == 0) {
11587 error = got_error_msg(GOT_ERR_CONFLICTS,
11588 "conflicts must be resolved before histedit "
11590 } else if (upa.conflicts > 0) {
11591 error = got_error_msg(GOT_ERR_CONFLICTS,
11592 "conflicts must be resolved before histedit "
11593 "can continue; changes destined for some "
11594 "files were not yet merged and should be "
11595 "merged manually if required before the "
11596 "histedit operation is continued");
11598 error = got_error_msg(GOT_ERR_CONFLICTS,
11599 "changes destined for some files were not "
11600 "yet merged and should be merged manually "
11601 "if required before the histedit operation "
11605 error = histedit_complete(worktree, fileindex, tmp_branch,
11608 got_object_id_queue_free(&commits);
11609 histedit_free_list(&histedit_cmds);
11610 free(head_commit_id);
11611 free(base_commit_id);
11612 free(resume_commit_id);
11614 got_object_commit_close(commit);
11616 got_ref_close(branch);
11618 got_ref_close(tmp_branch);
11620 got_worktree_close(worktree);
11622 const struct got_error *close_err = got_repo_close(repo);
11627 const struct got_error *pack_err =
11628 got_repo_pack_fds_close(pack_fds);
11636 usage_integrate(void)
11638 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11642 static const struct got_error *
11643 cmd_integrate(int argc, char *argv[])
11645 const struct got_error *error = NULL;
11646 struct got_repository *repo = NULL;
11647 struct got_worktree *worktree = NULL;
11648 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11649 const char *branch_arg = NULL;
11650 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11651 struct got_fileindex *fileindex = NULL;
11652 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11654 struct got_update_progress_arg upa;
11655 int *pack_fds = NULL;
11657 while ((ch = getopt(argc, argv, "")) != -1) {
11670 branch_arg = argv[0];
11672 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11673 "unveil", NULL) == -1)
11676 cwd = getcwd(NULL, 0);
11678 error = got_error_from_errno("getcwd");
11682 error = got_repo_pack_fds_open(&pack_fds);
11686 error = got_worktree_open(&worktree, cwd);
11688 if (error->code == GOT_ERR_NOT_WORKTREE)
11689 error = wrap_not_worktree_error(error, "integrate",
11694 error = check_rebase_or_histedit_in_progress(worktree);
11698 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11703 error = apply_unveil(got_repo_get_path(repo), 0,
11704 got_worktree_get_root_path(worktree));
11708 error = check_merge_in_progress(worktree, repo);
11712 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11713 error = got_error_from_errno("asprintf");
11717 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11718 &base_branch_ref, worktree, refname, repo);
11722 refname = strdup(got_ref_get_name(branch_ref));
11723 if (refname == NULL) {
11724 error = got_error_from_errno("strdup");
11725 got_worktree_integrate_abort(worktree, fileindex, repo,
11726 branch_ref, base_branch_ref);
11729 base_refname = strdup(got_ref_get_name(base_branch_ref));
11730 if (base_refname == NULL) {
11731 error = got_error_from_errno("strdup");
11732 got_worktree_integrate_abort(worktree, fileindex, repo,
11733 branch_ref, base_branch_ref);
11737 error = got_ref_resolve(&commit_id, repo, branch_ref);
11741 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11745 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11746 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11747 "specified branch has already been integrated");
11748 got_worktree_integrate_abort(worktree, fileindex, repo,
11749 branch_ref, base_branch_ref);
11753 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11755 if (error->code == GOT_ERR_ANCESTRY)
11756 error = got_error(GOT_ERR_REBASE_REQUIRED);
11757 got_worktree_integrate_abort(worktree, fileindex, repo,
11758 branch_ref, base_branch_ref);
11762 memset(&upa, 0, sizeof(upa));
11763 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11764 branch_ref, base_branch_ref, update_progress, &upa,
11765 check_cancelled, NULL);
11769 printf("Integrated %s into %s\n", refname, base_refname);
11770 print_update_progress_stats(&upa);
11773 const struct got_error *close_err = got_repo_close(repo);
11778 got_worktree_close(worktree);
11780 const struct got_error *pack_err =
11781 got_repo_pack_fds_close(pack_fds);
11786 free(base_commit_id);
11789 free(base_refname);
11796 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11801 static const struct got_error *
11802 cmd_merge(int argc, char *argv[])
11804 const struct got_error *error = NULL;
11805 struct got_worktree *worktree = NULL;
11806 struct got_repository *repo = NULL;
11807 struct got_fileindex *fileindex = NULL;
11808 char *cwd = NULL, *id_str = NULL, *author = NULL;
11809 struct got_reference *branch = NULL, *wt_branch = NULL;
11810 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11811 struct got_object_id *wt_branch_tip = NULL;
11812 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11813 int interrupt_merge = 0;
11814 struct got_update_progress_arg upa;
11815 struct got_object_id *merge_commit_id = NULL;
11816 char *branch_name = NULL;
11817 int *pack_fds = NULL;
11819 memset(&upa, 0, sizeof(upa));
11821 while ((ch = getopt(argc, argv, "acn")) != -1) {
11827 continue_merge = 1;
11830 interrupt_merge = 1;
11842 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11843 "unveil", NULL) == -1)
11847 if (abort_merge && continue_merge)
11848 option_conflict('a', 'c');
11849 if (abort_merge || continue_merge) {
11852 } else if (argc != 1)
11855 cwd = getcwd(NULL, 0);
11857 error = got_error_from_errno("getcwd");
11861 error = got_repo_pack_fds_open(&pack_fds);
11865 error = got_worktree_open(&worktree, cwd);
11867 if (error->code == GOT_ERR_NOT_WORKTREE)
11868 error = wrap_not_worktree_error(error,
11873 error = got_repo_open(&repo,
11874 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11879 error = apply_unveil(got_repo_get_path(repo), 0,
11880 worktree ? got_worktree_get_root_path(worktree) : NULL);
11884 error = check_rebase_or_histedit_in_progress(worktree);
11888 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11894 if (!merge_in_progress) {
11895 error = got_error(GOT_ERR_NOT_MERGING);
11898 error = got_worktree_merge_continue(&branch_name,
11899 &branch_tip, &fileindex, worktree, repo);
11902 error = got_worktree_merge_abort(worktree, fileindex, repo,
11903 abort_progress, &upa);
11906 printf("Merge of %s aborted\n", branch_name);
11907 goto done; /* nothing else to do */
11910 error = get_author(&author, repo, worktree);
11914 if (continue_merge) {
11915 if (!merge_in_progress) {
11916 error = got_error(GOT_ERR_NOT_MERGING);
11919 error = got_worktree_merge_continue(&branch_name,
11920 &branch_tip, &fileindex, worktree, repo);
11924 error = got_ref_open(&branch, repo, argv[0], 0);
11927 branch_name = strdup(got_ref_get_name(branch));
11928 if (branch_name == NULL) {
11929 error = got_error_from_errno("strdup");
11932 error = got_ref_resolve(&branch_tip, repo, branch);
11937 error = got_ref_open(&wt_branch, repo,
11938 got_worktree_get_head_ref_name(worktree), 0);
11941 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11944 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11945 wt_branch_tip, branch_tip, 0, repo,
11946 check_cancelled, NULL);
11947 if (error && error->code != GOT_ERR_ANCESTRY)
11950 if (!continue_merge) {
11951 error = check_path_prefix(wt_branch_tip, branch_tip,
11952 got_worktree_get_path_prefix(worktree),
11953 GOT_ERR_MERGE_PATH, repo);
11957 error = check_same_branch(wt_branch_tip, branch,
11960 if (error->code != GOT_ERR_ANCESTRY)
11964 static char msg[512];
11965 snprintf(msg, sizeof(msg),
11966 "cannot create a merge commit because "
11967 "%s is based on %s; %s can be integrated "
11968 "with 'got integrate' instead", branch_name,
11969 got_worktree_get_head_ref_name(worktree),
11971 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11975 error = got_worktree_merge_prepare(&fileindex, worktree,
11980 error = got_worktree_merge_branch(worktree, fileindex,
11981 yca_id, branch_tip, repo, update_progress, &upa,
11982 check_cancelled, NULL);
11985 print_merge_progress_stats(&upa);
11986 if (!upa.did_something) {
11987 error = got_worktree_merge_abort(worktree, fileindex,
11988 repo, abort_progress, &upa);
11991 printf("Already up-to-date\n");
11996 if (interrupt_merge) {
11997 error = got_worktree_merge_postpone(worktree, fileindex);
12000 printf("Merge of %s interrupted on request\n", branch_name);
12001 } else if (upa.conflicts > 0 || upa.missing > 0 ||
12002 upa.not_deleted > 0 || upa.unversioned > 0) {
12003 error = got_worktree_merge_postpone(worktree, fileindex);
12006 if (upa.conflicts > 0 && upa.missing == 0 &&
12007 upa.not_deleted == 0 && upa.unversioned == 0) {
12008 error = got_error_msg(GOT_ERR_CONFLICTS,
12009 "conflicts must be resolved before merging "
12011 } else if (upa.conflicts > 0) {
12012 error = got_error_msg(GOT_ERR_CONFLICTS,
12013 "conflicts must be resolved before merging "
12014 "can continue; changes destined for some "
12015 "files were not yet merged and "
12016 "should be merged manually if required before the "
12017 "merge operation is continued");
12019 error = got_error_msg(GOT_ERR_CONFLICTS,
12020 "changes destined for some "
12021 "files were not yet merged and should be "
12022 "merged manually if required before the "
12023 "merge operation is continued");
12027 error = got_worktree_merge_commit(&merge_commit_id, worktree,
12028 fileindex, author, NULL, 1, branch_tip, branch_name,
12029 repo, continue_merge ? print_status : NULL, NULL);
12032 error = got_worktree_merge_complete(worktree, fileindex, repo);
12035 error = got_object_id_str(&id_str, merge_commit_id);
12038 printf("Merged %s into %s: %s\n", branch_name,
12039 got_worktree_get_head_ref_name(worktree),
12045 free(merge_commit_id);
12051 got_ref_close(branch);
12053 got_ref_close(wt_branch);
12055 got_worktree_close(worktree);
12057 const struct got_error *close_err = got_repo_close(repo);
12062 const struct got_error *pack_err =
12063 got_repo_pack_fds_close(pack_fds);
12073 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12074 "[-S] [file-path ...]\n",
12079 static const struct got_error *
12080 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12081 const char *path, struct got_object_id *blob_id,
12082 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12083 int dirfd, const char *de_name)
12085 const struct got_error *err = NULL;
12086 char *id_str = NULL;
12088 if (staged_status != GOT_STATUS_ADD &&
12089 staged_status != GOT_STATUS_MODIFY &&
12090 staged_status != GOT_STATUS_DELETE)
12093 if (staged_status == GOT_STATUS_ADD ||
12094 staged_status == GOT_STATUS_MODIFY)
12095 err = got_object_id_str(&id_str, staged_blob_id);
12097 err = got_object_id_str(&id_str, blob_id);
12101 printf("%s %c %s\n", id_str, staged_status, path);
12106 static const struct got_error *
12107 cmd_stage(int argc, char *argv[])
12109 const struct got_error *error = NULL;
12110 struct got_repository *repo = NULL;
12111 struct got_worktree *worktree = NULL;
12113 struct got_pathlist_head paths;
12114 struct got_pathlist_entry *pe;
12115 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12116 FILE *patch_script_file = NULL;
12117 const char *patch_script_path = NULL;
12118 struct choose_patch_arg cpa;
12119 int *pack_fds = NULL;
12121 TAILQ_INIT(&paths);
12123 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12132 patch_script_path = optarg;
12135 allow_bad_symlinks = 1;
12147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12148 "unveil", NULL) == -1)
12151 if (list_stage && (pflag || patch_script_path))
12152 errx(1, "-l option cannot be used with other options");
12153 if (patch_script_path && !pflag)
12154 errx(1, "-F option can only be used together with -p option");
12156 cwd = getcwd(NULL, 0);
12158 error = got_error_from_errno("getcwd");
12162 error = got_repo_pack_fds_open(&pack_fds);
12166 error = got_worktree_open(&worktree, cwd);
12168 if (error->code == GOT_ERR_NOT_WORKTREE)
12169 error = wrap_not_worktree_error(error, "stage", cwd);
12173 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12178 if (patch_script_path) {
12179 patch_script_file = fopen(patch_script_path, "re");
12180 if (patch_script_file == NULL) {
12181 error = got_error_from_errno2("fopen",
12182 patch_script_path);
12186 error = apply_unveil(got_repo_get_path(repo), 0,
12187 got_worktree_get_root_path(worktree));
12191 error = check_merge_in_progress(worktree, repo);
12195 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12200 error = got_worktree_status(worktree, &paths, repo, 0,
12201 print_stage, NULL, check_cancelled, NULL);
12203 cpa.patch_script_file = patch_script_file;
12204 cpa.action = "stage";
12205 error = got_worktree_stage(worktree, &paths,
12206 pflag ? NULL : print_status, NULL,
12207 pflag ? choose_patch : NULL, &cpa,
12208 allow_bad_symlinks, repo);
12211 if (patch_script_file && fclose(patch_script_file) == EOF &&
12213 error = got_error_from_errno2("fclose", patch_script_path);
12215 const struct got_error *close_err = got_repo_close(repo);
12220 got_worktree_close(worktree);
12222 const struct got_error *pack_err =
12223 got_repo_pack_fds_close(pack_fds);
12227 TAILQ_FOREACH(pe, &paths, entry)
12228 free((char *)pe->path);
12229 got_pathlist_free(&paths);
12235 usage_unstage(void)
12237 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12238 "[file-path ...]\n",
12244 static const struct got_error *
12245 cmd_unstage(int argc, char *argv[])
12247 const struct got_error *error = NULL;
12248 struct got_repository *repo = NULL;
12249 struct got_worktree *worktree = NULL;
12251 struct got_pathlist_head paths;
12252 struct got_pathlist_entry *pe;
12254 struct got_update_progress_arg upa;
12255 FILE *patch_script_file = NULL;
12256 const char *patch_script_path = NULL;
12257 struct choose_patch_arg cpa;
12258 int *pack_fds = NULL;
12260 TAILQ_INIT(&paths);
12262 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12268 patch_script_path = optarg;
12280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12281 "unveil", NULL) == -1)
12284 if (patch_script_path && !pflag)
12285 errx(1, "-F option can only be used together with -p option");
12287 cwd = getcwd(NULL, 0);
12289 error = got_error_from_errno("getcwd");
12293 error = got_repo_pack_fds_open(&pack_fds);
12297 error = got_worktree_open(&worktree, cwd);
12299 if (error->code == GOT_ERR_NOT_WORKTREE)
12300 error = wrap_not_worktree_error(error, "unstage", cwd);
12304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12309 if (patch_script_path) {
12310 patch_script_file = fopen(patch_script_path, "re");
12311 if (patch_script_file == NULL) {
12312 error = got_error_from_errno2("fopen",
12313 patch_script_path);
12318 error = apply_unveil(got_repo_get_path(repo), 0,
12319 got_worktree_get_root_path(worktree));
12323 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12327 cpa.patch_script_file = patch_script_file;
12328 cpa.action = "unstage";
12329 memset(&upa, 0, sizeof(upa));
12330 error = got_worktree_unstage(worktree, &paths, update_progress,
12331 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12333 print_merge_progress_stats(&upa);
12335 if (patch_script_file && fclose(patch_script_file) == EOF &&
12337 error = got_error_from_errno2("fclose", patch_script_path);
12339 const struct got_error *close_err = got_repo_close(repo);
12344 got_worktree_close(worktree);
12346 const struct got_error *pack_err =
12347 got_repo_pack_fds_close(pack_fds);
12351 TAILQ_FOREACH(pe, &paths, entry)
12352 free((char *)pe->path);
12353 got_pathlist_free(&paths);
12361 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12362 "arg1 [arg2 ...]\n", getprogname());
12366 static const struct got_error *
12367 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12369 const struct got_error *err;
12370 struct got_blob_object *blob;
12373 fd = got_opentempfd();
12375 return got_error_from_errno("got_opentempfd");
12377 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12381 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12383 if (fd != -1 && close(fd) == -1 && err == NULL)
12384 err = got_error_from_errno("close");
12386 got_object_blob_close(blob);
12390 static const struct got_error *
12391 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12393 const struct got_error *err;
12394 struct got_tree_object *tree;
12397 err = got_object_open_as_tree(&tree, repo, id);
12401 nentries = got_object_tree_get_nentries(tree);
12402 for (i = 0; i < nentries; i++) {
12403 struct got_tree_entry *te;
12405 if (sigint_received || sigpipe_received)
12407 te = got_object_tree_get_entry(tree, i);
12408 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12411 fprintf(outfile, "%s %.7o %s\n", id_str,
12412 got_tree_entry_get_mode(te),
12413 got_tree_entry_get_name(te));
12417 got_object_tree_close(tree);
12422 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12432 h = (long long)gmtoff / 3600;
12433 m = ((long long)gmtoff - h*3600) / 60;
12434 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12437 static const struct got_error *
12438 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12440 const struct got_error *err;
12441 struct got_commit_object *commit;
12442 const struct got_object_id_queue *parent_ids;
12443 struct got_object_qid *pid;
12444 char *id_str = NULL;
12445 const char *logmsg = NULL;
12448 err = got_object_open_as_commit(&commit, repo, id);
12452 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12456 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12457 parent_ids = got_object_commit_get_parent_ids(commit);
12458 fprintf(outfile, "numparents %d\n",
12459 got_object_commit_get_nparents(commit));
12460 STAILQ_FOREACH(pid, parent_ids, entry) {
12462 err = got_object_id_str(&pid_str, &pid->id);
12465 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12468 format_gmtoff(gmtoff, sizeof(gmtoff),
12469 got_object_commit_get_author_gmtoff(commit));
12470 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12471 got_object_commit_get_author(commit),
12472 (long long)got_object_commit_get_author_time(commit),
12475 format_gmtoff(gmtoff, sizeof(gmtoff),
12476 got_object_commit_get_committer_gmtoff(commit));
12477 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12478 got_object_commit_get_author(commit),
12479 (long long)got_object_commit_get_committer_time(commit),
12482 logmsg = got_object_commit_get_logmsg_raw(commit);
12483 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12484 fprintf(outfile, "%s", logmsg);
12487 got_object_commit_close(commit);
12491 static const struct got_error *
12492 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12494 const struct got_error *err;
12495 struct got_tag_object *tag;
12496 char *id_str = NULL;
12497 const char *tagmsg = NULL;
12500 err = got_object_open_as_tag(&tag, repo, id);
12504 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12508 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12510 switch (got_object_tag_get_object_type(tag)) {
12511 case GOT_OBJ_TYPE_BLOB:
12512 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12513 GOT_OBJ_LABEL_BLOB);
12515 case GOT_OBJ_TYPE_TREE:
12516 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12517 GOT_OBJ_LABEL_TREE);
12519 case GOT_OBJ_TYPE_COMMIT:
12520 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12521 GOT_OBJ_LABEL_COMMIT);
12523 case GOT_OBJ_TYPE_TAG:
12524 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12525 GOT_OBJ_LABEL_TAG);
12531 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12532 got_object_tag_get_name(tag));
12534 format_gmtoff(gmtoff, sizeof(gmtoff),
12535 got_object_tag_get_tagger_gmtoff(tag));
12536 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12537 got_object_tag_get_tagger(tag),
12538 (long long)got_object_tag_get_tagger_time(tag),
12541 tagmsg = got_object_tag_get_message(tag);
12542 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12543 fprintf(outfile, "%s", tagmsg);
12546 got_object_tag_close(tag);
12550 static const struct got_error *
12551 cmd_cat(int argc, char *argv[])
12553 const struct got_error *error;
12554 struct got_repository *repo = NULL;
12555 struct got_worktree *worktree = NULL;
12556 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12557 const char *commit_id_str = NULL;
12558 struct got_object_id *id = NULL, *commit_id = NULL;
12559 struct got_commit_object *commit = NULL;
12560 int ch, obj_type, i, force_path = 0;
12561 struct got_reflist_head refs;
12562 int *pack_fds = NULL;
12567 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12572 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12575 commit_id_str = optarg;
12578 repo_path = realpath(optarg, NULL);
12579 if (repo_path == NULL)
12580 return got_error_from_errno2("realpath",
12582 got_path_strip_trailing_slashes(repo_path);
12596 cwd = getcwd(NULL, 0);
12598 error = got_error_from_errno("getcwd");
12602 error = got_repo_pack_fds_open(&pack_fds);
12606 if (repo_path == NULL) {
12607 error = got_worktree_open(&worktree, cwd);
12608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12611 repo_path = strdup(
12612 got_worktree_get_repo_path(worktree));
12613 if (repo_path == NULL) {
12614 error = got_error_from_errno("strdup");
12618 /* Release work tree lock. */
12619 got_worktree_close(worktree);
12624 if (repo_path == NULL) {
12625 repo_path = strdup(cwd);
12626 if (repo_path == NULL)
12627 return got_error_from_errno("strdup");
12630 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12635 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12639 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12643 if (commit_id_str == NULL)
12644 commit_id_str = GOT_REF_HEAD;
12645 error = got_repo_match_object_id(&commit_id, NULL,
12646 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12650 error = got_object_open_as_commit(&commit, repo, commit_id);
12654 for (i = 0; i < argc; i++) {
12656 error = got_object_id_by_path(&id, repo, commit,
12661 error = got_repo_match_object_id(&id, &label, argv[i],
12662 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12665 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12666 error->code != GOT_ERR_NOT_REF)
12668 error = got_object_id_by_path(&id, repo,
12675 error = got_object_get_type(&obj_type, repo, id);
12679 switch (obj_type) {
12680 case GOT_OBJ_TYPE_BLOB:
12681 error = cat_blob(id, repo, stdout);
12683 case GOT_OBJ_TYPE_TREE:
12684 error = cat_tree(id, repo, stdout);
12686 case GOT_OBJ_TYPE_COMMIT:
12687 error = cat_commit(id, repo, stdout);
12689 case GOT_OBJ_TYPE_TAG:
12690 error = cat_tag(id, repo, stdout);
12693 error = got_error(GOT_ERR_OBJ_TYPE);
12708 got_object_commit_close(commit);
12710 got_worktree_close(worktree);
12712 const struct got_error *close_err = got_repo_close(repo);
12717 const struct got_error *pack_err =
12718 got_repo_pack_fds_close(pack_fds);
12723 got_ref_list_free(&refs);
12730 fprintf(stderr, "usage: %s info [path ...]\n",
12735 static const struct got_error *
12736 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12737 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12738 struct got_object_id *commit_id)
12740 const struct got_error *err = NULL;
12741 char *id_str = NULL;
12743 struct tm mytm, *tm;
12744 struct got_pathlist_head *paths = arg;
12745 struct got_pathlist_entry *pe;
12748 * Clear error indication from any of the path arguments which
12749 * would cause this file index entry to be displayed.
12751 TAILQ_FOREACH(pe, paths, entry) {
12752 if (got_path_cmp(path, pe->path, strlen(path),
12753 pe->path_len) == 0 ||
12754 got_path_is_child(path, pe->path, pe->path_len))
12755 pe->data = NULL; /* no error */
12758 printf(GOT_COMMIT_SEP_STR);
12760 printf("symlink: %s\n", path);
12761 else if (S_ISREG(mode)) {
12762 printf("file: %s\n", path);
12763 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12764 } else if (S_ISDIR(mode))
12765 printf("directory: %s\n", path);
12767 printf("something: %s\n", path);
12769 tm = localtime_r(&mtime, &mytm);
12772 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12773 return got_error(GOT_ERR_NO_SPACE);
12774 printf("timestamp: %s\n", datebuf);
12777 err = got_object_id_str(&id_str, blob_id);
12780 printf("based on blob: %s\n", id_str);
12784 if (staged_blob_id) {
12785 err = got_object_id_str(&id_str, staged_blob_id);
12788 printf("based on staged blob: %s\n", id_str);
12793 err = got_object_id_str(&id_str, commit_id);
12796 printf("based on commit: %s\n", id_str);
12803 static const struct got_error *
12804 cmd_info(int argc, char *argv[])
12806 const struct got_error *error = NULL;
12807 struct got_worktree *worktree = NULL;
12808 char *cwd = NULL, *id_str = NULL;
12809 struct got_pathlist_head paths;
12810 struct got_pathlist_entry *pe;
12811 char *uuidstr = NULL;
12812 int ch, show_files = 0;
12813 int *pack_fds = NULL;
12815 TAILQ_INIT(&paths);
12817 while ((ch = getopt(argc, argv, "")) != -1) {
12829 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12833 cwd = getcwd(NULL, 0);
12835 error = got_error_from_errno("getcwd");
12839 error = got_repo_pack_fds_open(&pack_fds);
12843 error = got_worktree_open(&worktree, cwd);
12845 if (error->code == GOT_ERR_NOT_WORKTREE)
12846 error = wrap_not_worktree_error(error, "info", cwd);
12851 /* Remove "cpath" promise. */
12852 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12856 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12861 error = get_worktree_paths_from_argv(&paths, argc, argv,
12868 error = got_object_id_str(&id_str,
12869 got_worktree_get_base_commit_id(worktree));
12873 error = got_worktree_get_uuid(&uuidstr, worktree);
12877 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12878 printf("work tree base commit: %s\n", id_str);
12879 printf("work tree path prefix: %s\n",
12880 got_worktree_get_path_prefix(worktree));
12881 printf("work tree branch reference: %s\n",
12882 got_worktree_get_head_ref_name(worktree));
12883 printf("work tree UUID: %s\n", uuidstr);
12884 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12887 struct got_pathlist_entry *pe;
12888 TAILQ_FOREACH(pe, &paths, entry) {
12889 if (pe->path_len == 0)
12892 * Assume this path will fail. This will be corrected
12893 * in print_path_info() in case the path does suceeed.
12895 pe->data = (void *)got_error_path(pe->path,
12898 error = got_worktree_path_info(worktree, &paths,
12899 print_path_info, &paths, check_cancelled, NULL);
12902 TAILQ_FOREACH(pe, &paths, entry) {
12903 if (pe->data != NULL) {
12904 error = pe->data; /* bad path */
12911 const struct got_error *pack_err =
12912 got_repo_pack_fds_close(pack_fds);
12916 TAILQ_FOREACH(pe, &paths, entry)
12917 free((char *)pe->path);
12918 got_pathlist_free(&paths);