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);
914 if (preserve_logmsg) {
915 fprintf(stderr, "%s: log message preserved in %s\n",
916 getprogname(), logmsg_path);
917 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
918 error = got_error_from_errno2("unlink", logmsg_path);
927 free(gitconfig_path);
929 got_ref_close(branch_ref);
931 got_ref_close(head_ref);
938 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
939 "[-R reference] repository-url [directory]\n", getprogname());
943 struct got_fetch_progress_arg {
944 char last_scaled_size[FMT_SCALED_STRSIZE];
949 struct got_repository *repo;
954 struct got_pathlist_head *symrefs;
955 struct got_pathlist_head *wanted_branches;
956 struct got_pathlist_head *wanted_refs;
960 const char *remote_repo_path;
962 int fetch_all_branches;
963 int mirror_references;
967 /* XXX forward declaration */
968 static const struct got_error *
969 create_config_files(const char *proto, const char *host, const char *port,
970 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
971 int mirror_references, struct got_pathlist_head *symrefs,
972 struct got_pathlist_head *wanted_branches,
973 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
975 static const struct got_error *
976 fetch_progress(void *arg, const char *message, off_t packfile_size,
977 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
979 const struct got_error *err = NULL;
980 struct got_fetch_progress_arg *a = arg;
981 char scaled_size[FMT_SCALED_STRSIZE];
982 int p_indexed, p_resolved;
983 int print_size = 0, print_indexed = 0, print_resolved = 0;
986 * In order to allow a failed clone to be resumed with 'got fetch'
987 * we try to create configuration files as soon as possible.
988 * Once the server has sent information about its default branch
989 * we have all required information.
991 if (a->create_configs && !a->configs_created &&
992 !TAILQ_EMPTY(a->config_info.symrefs)) {
993 err = create_config_files(a->config_info.proto,
994 a->config_info.host, a->config_info.port,
995 a->config_info.remote_repo_path,
996 a->config_info.git_url,
997 a->config_info.fetch_all_branches,
998 a->config_info.mirror_references,
999 a->config_info.symrefs,
1000 a->config_info.wanted_branches,
1001 a->config_info.wanted_refs, a->repo);
1004 a->configs_created = 1;
1007 if (a->verbosity < 0)
1010 if (message && message[0] != '\0') {
1011 printf("\rserver: %s", message);
1016 if (packfile_size > 0 || nobj_indexed > 0) {
1017 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1018 (a->last_scaled_size[0] == '\0' ||
1019 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1021 if (strlcpy(a->last_scaled_size, scaled_size,
1022 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1023 return got_error(GOT_ERR_NO_SPACE);
1025 if (nobj_indexed > 0) {
1026 p_indexed = (nobj_indexed * 100) / nobj_total;
1027 if (p_indexed != a->last_p_indexed) {
1028 a->last_p_indexed = p_indexed;
1033 if (nobj_resolved > 0) {
1034 p_resolved = (nobj_resolved * 100) /
1035 (nobj_total - nobj_loose);
1036 if (p_resolved != a->last_p_resolved) {
1037 a->last_p_resolved = p_resolved;
1045 if (print_size || print_indexed || print_resolved)
1048 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1050 printf("; indexing %d%%", p_indexed);
1052 printf("; resolving deltas %d%%", p_resolved);
1053 if (print_size || print_indexed || print_resolved)
1059 static const struct got_error *
1060 create_symref(const char *refname, struct got_reference *target_ref,
1061 int verbosity, struct got_repository *repo)
1063 const struct got_error *err;
1064 struct got_reference *head_symref;
1066 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1070 err = got_ref_write(head_symref, repo);
1071 if (err == NULL && verbosity > 0) {
1072 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1073 got_ref_get_name(target_ref));
1075 got_ref_close(head_symref);
1079 static const struct got_error *
1080 list_remote_refs(struct got_pathlist_head *symrefs,
1081 struct got_pathlist_head *refs)
1083 const struct got_error *err;
1084 struct got_pathlist_entry *pe;
1086 TAILQ_FOREACH(pe, symrefs, entry) {
1087 const char *refname = pe->path;
1088 const char *targetref = pe->data;
1090 printf("%s: %s\n", refname, targetref);
1093 TAILQ_FOREACH(pe, refs, entry) {
1094 const char *refname = pe->path;
1095 struct got_object_id *id = pe->data;
1098 err = got_object_id_str(&id_str, id);
1101 printf("%s: %s\n", refname, id_str);
1108 static const struct got_error *
1109 create_ref(const char *refname, struct got_object_id *id,
1110 int verbosity, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_reference *ref;
1116 err = got_object_id_str(&id_str, id);
1120 err = got_ref_alloc(&ref, refname, id);
1124 err = got_ref_write(ref, repo);
1127 if (err == NULL && verbosity >= 0)
1128 printf("Created reference %s: %s\n", refname, id_str);
1135 match_wanted_ref(const char *refname, const char *wanted_ref)
1137 if (strncmp(refname, "refs/", 5) != 0)
1142 * Prevent fetching of references that won't make any
1143 * sense outside of the remote repository's context.
1145 if (strncmp(refname, "got/", 4) == 0)
1147 if (strncmp(refname, "remotes/", 8) == 0)
1150 if (strncmp(wanted_ref, "refs/", 5) == 0)
1153 /* Allow prefix match. */
1154 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1157 /* Allow exact match. */
1158 return (strcmp(refname, wanted_ref) == 0);
1162 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1164 struct got_pathlist_entry *pe;
1166 TAILQ_FOREACH(pe, wanted_refs, entry) {
1167 if (match_wanted_ref(refname, pe->path))
1174 static const struct got_error *
1175 create_wanted_ref(const char *refname, struct got_object_id *id,
1176 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1178 const struct got_error *err;
1179 char *remote_refname;
1181 if (strncmp("refs/", refname, 5) == 0)
1184 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1185 remote_repo_name, refname) == -1)
1186 return got_error_from_errno("asprintf");
1188 err = create_ref(remote_refname, id, verbosity, repo);
1189 free(remote_refname);
1193 static const struct got_error *
1194 create_gotconfig(const char *proto, const char *host, const char *port,
1195 const char *remote_repo_path, const char *default_branch,
1196 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1197 struct got_pathlist_head *wanted_refs, int mirror_references,
1198 struct got_repository *repo)
1200 const struct got_error *err = NULL;
1201 char *gotconfig_path = NULL;
1202 char *gotconfig = NULL;
1203 FILE *gotconfig_file = NULL;
1204 const char *branchname = NULL;
1205 char *branches = NULL, *refs = NULL;
1208 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1209 struct got_pathlist_entry *pe;
1210 TAILQ_FOREACH(pe, wanted_branches, entry) {
1212 branchname = pe->path;
1213 if (strncmp(branchname, "refs/heads/", 11) == 0)
1215 if (asprintf(&s, "%s\"%s\" ",
1216 branches ? branches : "", branchname) == -1) {
1217 err = got_error_from_errno("asprintf");
1223 } else if (!fetch_all_branches && default_branch) {
1224 branchname = default_branch;
1225 if (strncmp(branchname, "refs/heads/", 11) == 0)
1227 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1228 err = got_error_from_errno("asprintf");
1232 if (!TAILQ_EMPTY(wanted_refs)) {
1233 struct got_pathlist_entry *pe;
1234 TAILQ_FOREACH(pe, wanted_refs, entry) {
1236 const char *refname = pe->path;
1237 if (strncmp(refname, "refs/", 5) == 0)
1239 if (asprintf(&s, "%s\"%s\" ",
1240 refs ? refs : "", refname) == -1) {
1241 err = got_error_from_errno("asprintf");
1249 /* Create got.conf(5). */
1250 gotconfig_path = got_repo_get_path_gotconfig(repo);
1251 if (gotconfig_path == NULL) {
1252 err = got_error_from_errno("got_repo_get_path_gotconfig");
1255 gotconfig_file = fopen(gotconfig_path, "ae");
1256 if (gotconfig_file == NULL) {
1257 err = got_error_from_errno2("fopen", gotconfig_path);
1260 if (asprintf(&gotconfig,
1265 "\trepository \"%s\"\n"
1271 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1272 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1273 remote_repo_path, branches ? "\tbranch { " : "",
1274 branches ? branches : "", branches ? "}\n" : "",
1275 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1276 mirror_references ? "\tmirror-references yes\n" : "",
1277 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1278 err = got_error_from_errno("asprintf");
1281 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1282 if (n != strlen(gotconfig)) {
1283 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1288 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1289 err = got_error_from_errno2("fclose", gotconfig_path);
1290 free(gotconfig_path);
1295 static const struct got_error *
1296 create_gitconfig(const char *git_url, const char *default_branch,
1297 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1298 struct got_pathlist_head *wanted_refs, int mirror_references,
1299 struct got_repository *repo)
1301 const struct got_error *err = NULL;
1302 char *gitconfig_path = NULL;
1303 char *gitconfig = NULL;
1304 FILE *gitconfig_file = NULL;
1305 char *branches = NULL, *refs = NULL;
1306 const char *branchname;
1309 /* Create a config file Git can understand. */
1310 gitconfig_path = got_repo_get_path_gitconfig(repo);
1311 if (gitconfig_path == NULL) {
1312 err = got_error_from_errno("got_repo_get_path_gitconfig");
1315 gitconfig_file = fopen(gitconfig_path, "ae");
1316 if (gitconfig_file == NULL) {
1317 err = got_error_from_errno2("fopen", gitconfig_path);
1320 if (fetch_all_branches) {
1321 if (mirror_references) {
1322 if (asprintf(&branches,
1323 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1324 err = got_error_from_errno("asprintf");
1327 } else if (asprintf(&branches,
1328 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1329 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1330 err = got_error_from_errno("asprintf");
1333 } else if (!TAILQ_EMPTY(wanted_branches)) {
1334 struct got_pathlist_entry *pe;
1335 TAILQ_FOREACH(pe, wanted_branches, entry) {
1337 branchname = pe->path;
1338 if (strncmp(branchname, "refs/heads/", 11) == 0)
1340 if (mirror_references) {
1342 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1343 branches ? branches : "",
1344 branchname, branchname) == -1) {
1345 err = got_error_from_errno("asprintf");
1348 } else if (asprintf(&s,
1349 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1350 branches ? branches : "",
1351 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1352 branchname) == -1) {
1353 err = got_error_from_errno("asprintf");
1361 * If the server specified a default branch, use just that one.
1362 * Otherwise fall back to fetching all branches on next fetch.
1364 if (default_branch) {
1365 branchname = default_branch;
1366 if (strncmp(branchname, "refs/heads/", 11) == 0)
1369 branchname = "*"; /* fall back to all branches */
1370 if (mirror_references) {
1371 if (asprintf(&branches,
1372 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1373 branchname, branchname) == -1) {
1374 err = got_error_from_errno("asprintf");
1377 } else if (asprintf(&branches,
1378 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1379 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1380 branchname) == -1) {
1381 err = got_error_from_errno("asprintf");
1385 if (!TAILQ_EMPTY(wanted_refs)) {
1386 struct got_pathlist_entry *pe;
1387 TAILQ_FOREACH(pe, wanted_refs, entry) {
1389 const char *refname = pe->path;
1390 if (strncmp(refname, "refs/", 5) == 0)
1392 if (mirror_references) {
1394 "%s\tfetch = refs/%s:refs/%s\n",
1395 refs ? refs : "", refname, refname) == -1) {
1396 err = got_error_from_errno("asprintf");
1399 } else if (asprintf(&s,
1400 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1402 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1404 err = got_error_from_errno("asprintf");
1412 if (asprintf(&gitconfig,
1417 "\tfetch = refs/tags/*:refs/tags/*\n",
1418 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1419 refs ? refs : "") == -1) {
1420 err = got_error_from_errno("asprintf");
1423 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1424 if (n != strlen(gitconfig)) {
1425 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1429 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1430 err = got_error_from_errno2("fclose", gitconfig_path);
1431 free(gitconfig_path);
1436 static const struct got_error *
1437 create_config_files(const char *proto, const char *host, const char *port,
1438 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1439 int mirror_references, struct got_pathlist_head *symrefs,
1440 struct got_pathlist_head *wanted_branches,
1441 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 const char *default_branch = NULL;
1445 struct got_pathlist_entry *pe;
1448 * If we asked for a set of wanted branches then use the first
1451 if (!TAILQ_EMPTY(wanted_branches)) {
1452 pe = TAILQ_FIRST(wanted_branches);
1453 default_branch = pe->path;
1455 /* First HEAD ref listed by server is the default branch. */
1456 TAILQ_FOREACH(pe, symrefs, entry) {
1457 const char *refname = pe->path;
1458 const char *target = pe->data;
1460 if (strcmp(refname, GOT_REF_HEAD) != 0)
1463 default_branch = target;
1468 /* Create got.conf(5). */
1469 err = create_gotconfig(proto, host, port, remote_repo_path,
1470 default_branch, fetch_all_branches, wanted_branches,
1471 wanted_refs, mirror_references, repo);
1475 /* Create a config file Git can understand. */
1476 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1477 wanted_branches, wanted_refs, mirror_references, repo);
1480 static const struct got_error *
1481 cmd_clone(int argc, char *argv[])
1483 const struct got_error *error = NULL;
1484 const char *uri, *dirname;
1485 char *proto, *host, *port, *repo_name, *server_path;
1486 char *default_destdir = NULL, *id_str = NULL;
1487 const char *repo_path;
1488 struct got_repository *repo = NULL;
1489 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1490 struct got_pathlist_entry *pe;
1491 struct got_object_id *pack_hash = NULL;
1492 int ch, fetchfd = -1, fetchstatus;
1493 pid_t fetchpid = -1;
1494 struct got_fetch_progress_arg fpa;
1495 char *git_url = NULL;
1496 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1497 int list_refs_only = 0;
1498 int *pack_fds = NULL;
1501 TAILQ_INIT(&symrefs);
1502 TAILQ_INIT(&wanted_branches);
1503 TAILQ_INIT(&wanted_refs);
1505 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1508 fetch_all_branches = 1;
1511 error = got_pathlist_append(&wanted_branches,
1520 mirror_references = 1;
1525 else if (verbosity < 3)
1532 error = got_pathlist_append(&wanted_refs,
1545 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1546 option_conflict('a', 'b');
1547 if (list_refs_only) {
1548 if (!TAILQ_EMPTY(&wanted_branches))
1549 option_conflict('l', 'b');
1550 if (fetch_all_branches)
1551 option_conflict('l', 'a');
1552 if (mirror_references)
1553 option_conflict('l', 'm');
1554 if (!TAILQ_EMPTY(&wanted_refs))
1555 option_conflict('l', 'R');
1567 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1572 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1573 host, port ? ":" : "", port ? port : "",
1574 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1575 error = got_error_from_errno("asprintf");
1579 if (strcmp(proto, "git") == 0) {
1581 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1582 "sendfd dns inet unveil", NULL) == -1)
1585 } else if (strcmp(proto, "git+ssh") == 0 ||
1586 strcmp(proto, "ssh") == 0) {
1588 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1589 "sendfd unveil", NULL) == -1)
1592 } else if (strcmp(proto, "http") == 0 ||
1593 strcmp(proto, "git+http") == 0) {
1594 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1597 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1600 if (dirname == NULL) {
1601 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1602 error = got_error_from_errno("asprintf");
1605 repo_path = default_destdir;
1607 repo_path = dirname;
1609 if (!list_refs_only) {
1610 error = got_path_mkdir(repo_path);
1612 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1613 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1615 if (!got_path_dir_is_empty(repo_path)) {
1616 error = got_error_path(repo_path,
1617 GOT_ERR_DIR_NOT_EMPTY);
1622 error = got_dial_apply_unveil(proto);
1626 error = apply_unveil(repo_path, 0, NULL);
1631 printf("Connecting to %s%s%s\n", host,
1632 port ? ":" : "", port ? port : "");
1634 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1635 server_path, verbosity);
1639 if (!list_refs_only) {
1640 error = got_repo_init(repo_path);
1643 error = got_repo_pack_fds_open(&pack_fds);
1646 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1651 fpa.last_scaled_size[0] = '\0';
1652 fpa.last_p_indexed = -1;
1653 fpa.last_p_resolved = -1;
1654 fpa.verbosity = verbosity;
1655 fpa.create_configs = 1;
1656 fpa.configs_created = 0;
1658 fpa.config_info.symrefs = &symrefs;
1659 fpa.config_info.wanted_branches = &wanted_branches;
1660 fpa.config_info.wanted_refs = &wanted_refs;
1661 fpa.config_info.proto = proto;
1662 fpa.config_info.host = host;
1663 fpa.config_info.port = port;
1664 fpa.config_info.remote_repo_path = server_path;
1665 fpa.config_info.git_url = git_url;
1666 fpa.config_info.fetch_all_branches = fetch_all_branches;
1667 fpa.config_info.mirror_references = mirror_references;
1668 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1669 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1670 fetch_all_branches, &wanted_branches, &wanted_refs,
1671 list_refs_only, verbosity, fetchfd, repo,
1672 fetch_progress, &fpa);
1676 if (list_refs_only) {
1677 error = list_remote_refs(&symrefs, &refs);
1681 if (pack_hash == NULL) {
1682 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1683 "server sent an empty pack file");
1686 error = got_object_id_str(&id_str, pack_hash);
1690 printf("\nFetched %s.pack\n", id_str);
1693 /* Set up references provided with the pack file. */
1694 TAILQ_FOREACH(pe, &refs, entry) {
1695 const char *refname = pe->path;
1696 struct got_object_id *id = pe->data;
1697 char *remote_refname;
1699 if (is_wanted_ref(&wanted_refs, refname) &&
1700 !mirror_references) {
1701 error = create_wanted_ref(refname, id,
1702 GOT_FETCH_DEFAULT_REMOTE_NAME,
1703 verbosity - 1, repo);
1709 error = create_ref(refname, id, verbosity - 1, repo);
1713 if (mirror_references)
1716 if (strncmp("refs/heads/", refname, 11) != 0)
1719 if (asprintf(&remote_refname,
1720 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1721 refname + 11) == -1) {
1722 error = got_error_from_errno("asprintf");
1725 error = create_ref(remote_refname, id, verbosity - 1, repo);
1726 free(remote_refname);
1731 /* Set the HEAD reference if the server provided one. */
1732 TAILQ_FOREACH(pe, &symrefs, entry) {
1733 struct got_reference *target_ref;
1734 const char *refname = pe->path;
1735 const char *target = pe->data;
1736 char *remote_refname = NULL, *remote_target = NULL;
1738 if (strcmp(refname, GOT_REF_HEAD) != 0)
1741 error = got_ref_open(&target_ref, repo, target, 0);
1743 if (error->code == GOT_ERR_NOT_REF) {
1750 error = create_symref(refname, target_ref, verbosity, repo);
1751 got_ref_close(target_ref);
1755 if (mirror_references)
1758 if (strncmp("refs/heads/", target, 11) != 0)
1761 if (asprintf(&remote_refname,
1762 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1764 error = got_error_from_errno("asprintf");
1767 if (asprintf(&remote_target,
1768 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1769 target + 11) == -1) {
1770 error = got_error_from_errno("asprintf");
1771 free(remote_refname);
1774 error = got_ref_open(&target_ref, repo, remote_target, 0);
1776 free(remote_refname);
1777 free(remote_target);
1778 if (error->code == GOT_ERR_NOT_REF) {
1784 error = create_symref(remote_refname, target_ref,
1785 verbosity - 1, repo);
1786 free(remote_refname);
1787 free(remote_target);
1788 got_ref_close(target_ref);
1794 * We failed to set the HEAD reference. If we asked for
1795 * a set of wanted branches use the first of one of those
1796 * which could be fetched instead.
1798 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1799 const char *target = pe->path;
1800 struct got_reference *target_ref;
1802 error = got_ref_open(&target_ref, repo, target, 0);
1804 if (error->code == GOT_ERR_NOT_REF) {
1811 error = create_symref(GOT_REF_HEAD, target_ref,
1813 got_ref_close(target_ref);
1821 printf("Created %s repository '%s'\n",
1822 mirror_references ? "mirrored" : "cloned", repo_path);
1825 const struct got_error *pack_err =
1826 got_repo_pack_fds_close(pack_fds);
1832 if (kill(fetchpid, SIGTERM) == -1)
1833 error = got_error_from_errno("kill");
1834 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1835 error = got_error_from_errno("waitpid");
1837 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1838 error = got_error_from_errno("close");
1840 const struct got_error *close_err = got_repo_close(repo);
1844 TAILQ_FOREACH(pe, &refs, entry) {
1845 free((void *)pe->path);
1848 got_pathlist_free(&refs);
1849 TAILQ_FOREACH(pe, &symrefs, entry) {
1850 free((void *)pe->path);
1853 got_pathlist_free(&symrefs);
1854 got_pathlist_free(&wanted_branches);
1855 got_pathlist_free(&wanted_refs);
1862 free(default_destdir);
1867 static const struct got_error *
1868 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1869 int replace_tags, int verbosity, struct got_repository *repo)
1871 const struct got_error *err = NULL;
1872 char *new_id_str = NULL;
1873 struct got_object_id *old_id = NULL;
1875 err = got_object_id_str(&new_id_str, new_id);
1879 if (!replace_tags &&
1880 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1881 err = got_ref_resolve(&old_id, repo, ref);
1884 if (got_object_id_cmp(old_id, new_id) == 0)
1886 if (verbosity >= 0) {
1887 printf("Rejecting update of existing tag %s: %s\n",
1888 got_ref_get_name(ref), new_id_str);
1893 if (got_ref_is_symbolic(ref)) {
1894 if (verbosity >= 0) {
1895 printf("Replacing reference %s: %s\n",
1896 got_ref_get_name(ref),
1897 got_ref_get_symref_target(ref));
1899 err = got_ref_change_symref_to_ref(ref, new_id);
1902 err = got_ref_write(ref, repo);
1906 err = got_ref_resolve(&old_id, repo, ref);
1909 if (got_object_id_cmp(old_id, new_id) == 0)
1912 err = got_ref_change_ref(ref, new_id);
1915 err = got_ref_write(ref, repo);
1921 printf("Updated %s: %s\n", got_ref_get_name(ref),
1929 static const struct got_error *
1930 update_symref(const char *refname, struct got_reference *target_ref,
1931 int verbosity, struct got_repository *repo)
1933 const struct got_error *err = NULL, *unlock_err;
1934 struct got_reference *symref;
1935 int symref_is_locked = 0;
1937 err = got_ref_open(&symref, repo, refname, 1);
1939 if (err->code != GOT_ERR_NOT_REF)
1941 err = got_ref_alloc_symref(&symref, refname, target_ref);
1945 err = got_ref_write(symref, repo);
1950 printf("Created reference %s: %s\n",
1951 got_ref_get_name(symref),
1952 got_ref_get_symref_target(symref));
1954 symref_is_locked = 1;
1956 if (strcmp(got_ref_get_symref_target(symref),
1957 got_ref_get_name(target_ref)) == 0)
1960 err = got_ref_change_symref(symref,
1961 got_ref_get_name(target_ref));
1965 err = got_ref_write(symref, repo);
1970 printf("Updated %s: %s\n", got_ref_get_name(symref),
1971 got_ref_get_symref_target(symref));
1975 if (symref_is_locked) {
1976 unlock_err = got_ref_unlock(symref);
1977 if (unlock_err && err == NULL)
1980 got_ref_close(symref);
1987 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1988 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1989 "[remote-repository-name]\n",
1994 static const struct got_error *
1995 delete_missing_ref(struct got_reference *ref,
1996 int verbosity, struct got_repository *repo)
1998 const struct got_error *err = NULL;
1999 struct got_object_id *id = NULL;
2000 char *id_str = NULL;
2002 if (got_ref_is_symbolic(ref)) {
2003 err = got_ref_delete(ref, repo);
2006 if (verbosity >= 0) {
2007 printf("Deleted %s: %s\n",
2008 got_ref_get_name(ref),
2009 got_ref_get_symref_target(ref));
2012 err = got_ref_resolve(&id, repo, ref);
2015 err = got_object_id_str(&id_str, id);
2019 err = got_ref_delete(ref, repo);
2022 if (verbosity >= 0) {
2023 printf("Deleted %s: %s\n",
2024 got_ref_get_name(ref), id_str);
2033 static const struct got_error *
2034 delete_missing_refs(struct got_pathlist_head *their_refs,
2035 struct got_pathlist_head *their_symrefs,
2036 const struct got_remote_repo *remote,
2037 int verbosity, struct got_repository *repo)
2039 const struct got_error *err = NULL, *unlock_err;
2040 struct got_reflist_head my_refs;
2041 struct got_reflist_entry *re;
2042 struct got_pathlist_entry *pe;
2043 char *remote_namespace = NULL;
2044 char *local_refname = NULL;
2046 TAILQ_INIT(&my_refs);
2048 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2050 return got_error_from_errno("asprintf");
2052 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2056 TAILQ_FOREACH(re, &my_refs, entry) {
2057 const char *refname = got_ref_get_name(re->ref);
2058 const char *their_refname;
2060 if (remote->mirror_references) {
2061 their_refname = refname;
2063 if (strncmp(refname, remote_namespace,
2064 strlen(remote_namespace)) == 0) {
2065 if (strcmp(refname + strlen(remote_namespace),
2068 if (asprintf(&local_refname, "refs/heads/%s",
2069 refname + strlen(remote_namespace)) == -1) {
2070 err = got_error_from_errno("asprintf");
2073 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2076 their_refname = local_refname;
2079 TAILQ_FOREACH(pe, their_refs, entry) {
2080 if (strcmp(their_refname, pe->path) == 0)
2086 TAILQ_FOREACH(pe, their_symrefs, entry) {
2087 if (strcmp(their_refname, pe->path) == 0)
2093 err = delete_missing_ref(re->ref, verbosity, repo);
2097 if (local_refname) {
2098 struct got_reference *ref;
2099 err = got_ref_open(&ref, repo, local_refname, 1);
2101 if (err->code != GOT_ERR_NOT_REF)
2103 free(local_refname);
2104 local_refname = NULL;
2107 err = delete_missing_ref(ref, verbosity, repo);
2110 unlock_err = got_ref_unlock(ref);
2112 if (unlock_err && err == NULL) {
2117 free(local_refname);
2118 local_refname = NULL;
2122 free(remote_namespace);
2123 free(local_refname);
2127 static const struct got_error *
2128 update_wanted_ref(const char *refname, struct got_object_id *id,
2129 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2131 const struct got_error *err, *unlock_err;
2132 char *remote_refname;
2133 struct got_reference *ref;
2135 if (strncmp("refs/", refname, 5) == 0)
2138 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2139 remote_repo_name, refname) == -1)
2140 return got_error_from_errno("asprintf");
2142 err = got_ref_open(&ref, repo, remote_refname, 1);
2144 if (err->code != GOT_ERR_NOT_REF)
2146 err = create_ref(remote_refname, id, verbosity, repo);
2148 err = update_ref(ref, id, 0, verbosity, repo);
2149 unlock_err = got_ref_unlock(ref);
2150 if (unlock_err && err == NULL)
2155 free(remote_refname);
2159 static const struct got_error *
2160 delete_ref(struct got_repository *repo, struct got_reference *ref)
2162 const struct got_error *err = NULL;
2163 struct got_object_id *id = NULL;
2164 char *id_str = NULL;
2167 if (got_ref_is_symbolic(ref)) {
2168 target = got_ref_get_symref_target(ref);
2170 err = got_ref_resolve(&id, repo, ref);
2173 err = got_object_id_str(&id_str, id);
2179 err = got_ref_delete(ref, repo);
2183 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2190 static const struct got_error *
2191 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2193 const struct got_error *err = NULL;
2194 struct got_reflist_head refs;
2195 struct got_reflist_entry *re;
2200 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2201 err = got_error_from_errno("asprintf");
2204 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2208 TAILQ_FOREACH(re, &refs, entry)
2209 delete_ref(repo, re->ref);
2211 got_ref_list_free(&refs);
2215 static const struct got_error *
2216 cmd_fetch(int argc, char *argv[])
2218 const struct got_error *error = NULL, *unlock_err;
2219 char *cwd = NULL, *repo_path = NULL;
2220 const char *remote_name;
2221 char *proto = NULL, *host = NULL, *port = NULL;
2222 char *repo_name = NULL, *server_path = NULL;
2223 const struct got_remote_repo *remotes, *remote = NULL;
2225 char *id_str = NULL;
2226 struct got_repository *repo = NULL;
2227 struct got_worktree *worktree = NULL;
2228 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2229 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2230 struct got_pathlist_entry *pe;
2231 struct got_object_id *pack_hash = NULL;
2232 int i, ch, fetchfd = -1, fetchstatus;
2233 pid_t fetchpid = -1;
2234 struct got_fetch_progress_arg fpa;
2235 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2236 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2237 int *pack_fds = NULL;
2240 TAILQ_INIT(&symrefs);
2241 TAILQ_INIT(&wanted_branches);
2242 TAILQ_INIT(&wanted_refs);
2244 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2247 fetch_all_branches = 1;
2250 error = got_pathlist_append(&wanted_branches,
2262 repo_path = realpath(optarg, NULL);
2263 if (repo_path == NULL)
2264 return got_error_from_errno2("realpath",
2266 got_path_strip_trailing_slashes(repo_path);
2274 else if (verbosity < 3)
2281 error = got_pathlist_append(&wanted_refs,
2297 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2298 option_conflict('a', 'b');
2299 if (list_refs_only) {
2300 if (!TAILQ_EMPTY(&wanted_branches))
2301 option_conflict('l', 'b');
2302 if (fetch_all_branches)
2303 option_conflict('l', 'a');
2305 option_conflict('l', 'd');
2307 option_conflict('l', 'X');
2309 if (delete_remote) {
2310 if (fetch_all_branches)
2311 option_conflict('X', 'a');
2312 if (!TAILQ_EMPTY(&wanted_branches))
2313 option_conflict('X', 'b');
2315 option_conflict('X', 'd');
2317 option_conflict('X', 't');
2318 if (!TAILQ_EMPTY(&wanted_refs))
2319 option_conflict('X', 'R');
2324 errx(1, "-X option requires a remote name");
2325 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2326 } else if (argc == 1)
2327 remote_name = argv[0];
2331 cwd = getcwd(NULL, 0);
2333 error = got_error_from_errno("getcwd");
2337 error = got_repo_pack_fds_open(&pack_fds);
2341 if (repo_path == NULL) {
2342 error = got_worktree_open(&worktree, cwd);
2343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2349 strdup(got_worktree_get_repo_path(worktree));
2350 if (repo_path == NULL)
2351 error = got_error_from_errno("strdup");
2355 repo_path = strdup(cwd);
2356 if (repo_path == NULL) {
2357 error = got_error_from_errno("strdup");
2363 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2367 if (delete_remote) {
2368 error = delete_refs_for_remote(repo, remote_name);
2369 goto done; /* nothing else to do */
2373 worktree_conf = got_worktree_get_gotconfig(worktree);
2374 if (worktree_conf) {
2375 got_gotconfig_get_remotes(&nremotes, &remotes,
2377 for (i = 0; i < nremotes; i++) {
2378 if (strcmp(remotes[i].name, remote_name) == 0) {
2379 remote = &remotes[i];
2385 if (remote == NULL) {
2386 repo_conf = got_repo_get_gotconfig(repo);
2388 got_gotconfig_get_remotes(&nremotes, &remotes,
2390 for (i = 0; i < nremotes; i++) {
2391 if (strcmp(remotes[i].name, remote_name) == 0) {
2392 remote = &remotes[i];
2398 if (remote == NULL) {
2399 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2400 for (i = 0; i < nremotes; i++) {
2401 if (strcmp(remotes[i].name, remote_name) == 0) {
2402 remote = &remotes[i];
2407 if (remote == NULL) {
2408 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2412 if (TAILQ_EMPTY(&wanted_branches)) {
2413 if (!fetch_all_branches)
2414 fetch_all_branches = remote->fetch_all_branches;
2415 for (i = 0; i < remote->nfetch_branches; i++) {
2416 got_pathlist_append(&wanted_branches,
2417 remote->fetch_branches[i], NULL);
2420 if (TAILQ_EMPTY(&wanted_refs)) {
2421 for (i = 0; i < remote->nfetch_refs; i++) {
2422 got_pathlist_append(&wanted_refs,
2423 remote->fetch_refs[i], NULL);
2427 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2428 &repo_name, remote->fetch_url);
2432 if (strcmp(proto, "git") == 0) {
2434 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2435 "sendfd dns inet unveil", NULL) == -1)
2438 } else if (strcmp(proto, "git+ssh") == 0 ||
2439 strcmp(proto, "ssh") == 0) {
2441 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2442 "sendfd unveil", NULL) == -1)
2445 } else if (strcmp(proto, "http") == 0 ||
2446 strcmp(proto, "git+http") == 0) {
2447 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2450 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2454 error = got_dial_apply_unveil(proto);
2458 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2463 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2464 port ? ":" : "", port ? port : "");
2466 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2467 server_path, verbosity);
2471 fpa.last_scaled_size[0] = '\0';
2472 fpa.last_p_indexed = -1;
2473 fpa.last_p_resolved = -1;
2474 fpa.verbosity = verbosity;
2476 fpa.create_configs = 0;
2477 fpa.configs_created = 0;
2478 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2479 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2480 remote->mirror_references, fetch_all_branches, &wanted_branches,
2481 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2482 fetch_progress, &fpa);
2486 if (list_refs_only) {
2487 error = list_remote_refs(&symrefs, &refs);
2491 if (pack_hash == NULL) {
2493 printf("Already up-to-date\n");
2494 } else if (verbosity >= 0) {
2495 error = got_object_id_str(&id_str, pack_hash);
2498 printf("\nFetched %s.pack\n", id_str);
2503 /* Update references provided with the pack file. */
2504 TAILQ_FOREACH(pe, &refs, entry) {
2505 const char *refname = pe->path;
2506 struct got_object_id *id = pe->data;
2507 struct got_reference *ref;
2508 char *remote_refname;
2510 if (is_wanted_ref(&wanted_refs, refname) &&
2511 !remote->mirror_references) {
2512 error = update_wanted_ref(refname, id,
2513 remote->name, verbosity, repo);
2519 if (remote->mirror_references ||
2520 strncmp("refs/tags/", refname, 10) == 0) {
2521 error = got_ref_open(&ref, repo, refname, 1);
2523 if (error->code != GOT_ERR_NOT_REF)
2525 error = create_ref(refname, id, verbosity,
2530 error = update_ref(ref, id, replace_tags,
2532 unlock_err = got_ref_unlock(ref);
2533 if (unlock_err && error == NULL)
2539 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2540 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2541 remote_name, refname + 11) == -1) {
2542 error = got_error_from_errno("asprintf");
2546 error = got_ref_open(&ref, repo, remote_refname, 1);
2548 if (error->code != GOT_ERR_NOT_REF)
2550 error = create_ref(remote_refname, id,
2555 error = update_ref(ref, id, replace_tags,
2557 unlock_err = got_ref_unlock(ref);
2558 if (unlock_err && error == NULL)
2565 /* Also create a local branch if none exists yet. */
2566 error = got_ref_open(&ref, repo, refname, 1);
2568 if (error->code != GOT_ERR_NOT_REF)
2570 error = create_ref(refname, id, verbosity,
2575 unlock_err = got_ref_unlock(ref);
2576 if (unlock_err && error == NULL)
2583 error = delete_missing_refs(&refs, &symrefs, remote,
2589 if (!remote->mirror_references) {
2590 /* Update remote HEAD reference if the server provided one. */
2591 TAILQ_FOREACH(pe, &symrefs, entry) {
2592 struct got_reference *target_ref;
2593 const char *refname = pe->path;
2594 const char *target = pe->data;
2595 char *remote_refname = NULL, *remote_target = NULL;
2597 if (strcmp(refname, GOT_REF_HEAD) != 0)
2600 if (strncmp("refs/heads/", target, 11) != 0)
2603 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2604 remote->name, refname) == -1) {
2605 error = got_error_from_errno("asprintf");
2608 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2609 remote->name, target + 11) == -1) {
2610 error = got_error_from_errno("asprintf");
2611 free(remote_refname);
2615 error = got_ref_open(&target_ref, repo, remote_target,
2618 free(remote_refname);
2619 free(remote_target);
2620 if (error->code == GOT_ERR_NOT_REF) {
2626 error = update_symref(remote_refname, target_ref,
2628 free(remote_refname);
2629 free(remote_target);
2630 got_ref_close(target_ref);
2637 if (kill(fetchpid, SIGTERM) == -1)
2638 error = got_error_from_errno("kill");
2639 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2640 error = got_error_from_errno("waitpid");
2642 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2643 error = got_error_from_errno("close");
2645 const struct got_error *close_err = got_repo_close(repo);
2650 got_worktree_close(worktree);
2652 const struct got_error *pack_err =
2653 got_repo_pack_fds_close(pack_fds);
2658 TAILQ_FOREACH(pe, &refs, entry) {
2659 free((void *)pe->path);
2662 got_pathlist_free(&refs);
2663 TAILQ_FOREACH(pe, &symrefs, entry) {
2664 free((void *)pe->path);
2667 got_pathlist_free(&symrefs);
2668 got_pathlist_free(&wanted_branches);
2669 got_pathlist_free(&wanted_refs);
2684 usage_checkout(void)
2686 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2687 "[-p prefix] [-q] repository-path [worktree-path]\n",
2693 show_worktree_base_ref_warning(void)
2695 fprintf(stderr, "%s: warning: could not create a reference "
2696 "to the work tree's base commit; the commit could be "
2697 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2698 "repository writable and running 'got update' will prevent this\n",
2702 struct got_checkout_progress_arg {
2703 const char *worktree_path;
2704 int had_base_commit_ref_error;
2708 static const struct got_error *
2709 checkout_progress(void *arg, unsigned char status, const char *path)
2711 struct got_checkout_progress_arg *a = arg;
2713 /* Base commit bump happens silently. */
2714 if (status == GOT_STATUS_BUMP_BASE)
2717 if (status == GOT_STATUS_BASE_REF_ERR) {
2718 a->had_base_commit_ref_error = 1;
2722 while (path[0] == '/')
2725 if (a->verbosity >= 0)
2726 printf("%c %s/%s\n", status, a->worktree_path, path);
2731 static const struct got_error *
2732 check_cancelled(void *arg)
2734 if (sigint_received || sigpipe_received)
2735 return got_error(GOT_ERR_CANCELLED);
2739 static const struct got_error *
2740 check_linear_ancestry(struct got_object_id *commit_id,
2741 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2742 struct got_repository *repo)
2744 const struct got_error *err = NULL;
2745 struct got_object_id *yca_id;
2747 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2748 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2753 return got_error(GOT_ERR_ANCESTRY);
2756 * Require a straight line of history between the target commit
2757 * and the work tree's base commit.
2759 * Non-linear situations such as this require a rebase:
2761 * (commit) D F (base_commit)
2769 * 'got update' only handles linear cases:
2770 * Update forwards in time: A (base/yca) - B - C - D (commit)
2771 * Update backwards in time: D (base) - C - B - A (commit/yca)
2773 if (allow_forwards_in_time_only) {
2774 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2775 return got_error(GOT_ERR_ANCESTRY);
2776 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2777 got_object_id_cmp(base_commit_id, yca_id) != 0)
2778 return got_error(GOT_ERR_ANCESTRY);
2784 static const struct got_error *
2785 check_same_branch(struct got_object_id *commit_id,
2786 struct got_reference *head_ref, struct got_object_id *yca_id,
2787 struct got_repository *repo)
2789 const struct got_error *err = NULL;
2790 struct got_commit_graph *graph = NULL;
2791 struct got_object_id *head_commit_id = NULL;
2792 int is_same_branch = 0;
2794 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2798 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2802 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2807 err = got_commit_graph_open(&graph, "/", 1);
2811 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2812 check_cancelled, NULL);
2817 struct got_object_id *id;
2818 err = got_commit_graph_iter_next(&id, graph, repo,
2819 check_cancelled, NULL);
2821 if (err->code == GOT_ERR_ITER_COMPLETED)
2827 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2829 if (got_object_id_cmp(id, commit_id) == 0) {
2837 got_commit_graph_close(graph);
2838 free(head_commit_id);
2839 if (!err && !is_same_branch)
2840 err = got_error(GOT_ERR_ANCESTRY);
2844 static const struct got_error *
2845 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2847 static char msg[512];
2848 const char *branch_name;
2850 if (got_ref_is_symbolic(ref))
2851 branch_name = got_ref_get_symref_target(ref);
2853 branch_name = got_ref_get_name(ref);
2855 if (strncmp("refs/heads/", branch_name, 11) == 0)
2858 snprintf(msg, sizeof(msg),
2859 "target commit is not contained in branch '%s'; "
2860 "the branch to use must be specified with -b; "
2861 "if necessary a new branch can be created for "
2862 "this commit with 'got branch -c %s BRANCH_NAME'",
2863 branch_name, commit_id_str);
2865 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2868 static const struct got_error *
2869 cmd_checkout(int argc, char *argv[])
2871 const struct got_error *error = NULL;
2872 struct got_repository *repo = NULL;
2873 struct got_reference *head_ref = NULL, *ref = NULL;
2874 struct got_worktree *worktree = NULL;
2875 char *repo_path = NULL;
2876 char *worktree_path = NULL;
2877 const char *path_prefix = "";
2878 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2879 char *commit_id_str = NULL;
2880 struct got_object_id *commit_id = NULL;
2882 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2883 struct got_pathlist_head paths;
2884 struct got_checkout_progress_arg cpa;
2885 int *pack_fds = NULL;
2889 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2892 branch_name = optarg;
2895 commit_id_str = strdup(optarg);
2896 if (commit_id_str == NULL)
2897 return got_error_from_errno("strdup");
2903 path_prefix = optarg;
2918 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2919 "unveil", NULL) == -1)
2923 char *base, *dotgit;
2925 repo_path = realpath(argv[0], NULL);
2926 if (repo_path == NULL)
2927 return got_error_from_errno2("realpath", argv[0]);
2928 cwd = getcwd(NULL, 0);
2930 error = got_error_from_errno("getcwd");
2937 error = got_path_basename(&base, path);
2940 dotgit = strstr(base, ".git");
2943 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2944 error = got_error_from_errno("asprintf");
2949 } else if (argc == 2) {
2950 repo_path = realpath(argv[0], NULL);
2951 if (repo_path == NULL) {
2952 error = got_error_from_errno2("realpath", argv[0]);
2955 worktree_path = realpath(argv[1], NULL);
2956 if (worktree_path == NULL) {
2957 if (errno != ENOENT) {
2958 error = got_error_from_errno2("realpath",
2962 worktree_path = strdup(argv[1]);
2963 if (worktree_path == NULL) {
2964 error = got_error_from_errno("strdup");
2971 got_path_strip_trailing_slashes(repo_path);
2972 got_path_strip_trailing_slashes(worktree_path);
2974 error = got_repo_pack_fds_open(&pack_fds);
2978 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2982 /* Pre-create work tree path for unveil(2) */
2983 error = got_path_mkdir(worktree_path);
2985 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2986 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2988 if (!allow_nonempty &&
2989 !got_path_dir_is_empty(worktree_path)) {
2990 error = got_error_path(worktree_path,
2991 GOT_ERR_DIR_NOT_EMPTY);
2996 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3000 error = got_ref_open(&head_ref, repo, branch_name, 0);
3004 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3005 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3008 error = got_worktree_open(&worktree, worktree_path);
3012 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3016 if (!same_path_prefix) {
3017 error = got_error(GOT_ERR_PATH_PREFIX);
3021 if (commit_id_str) {
3022 struct got_reflist_head refs;
3024 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3028 error = got_repo_match_object_id(&commit_id, NULL,
3029 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3030 got_ref_list_free(&refs);
3033 error = check_linear_ancestry(commit_id,
3034 got_worktree_get_base_commit_id(worktree), 0, repo);
3035 if (error != NULL) {
3036 if (error->code == GOT_ERR_ANCESTRY) {
3037 error = checkout_ancestry_error(
3038 head_ref, commit_id_str);
3042 error = check_same_branch(commit_id, head_ref, NULL, repo);
3044 if (error->code == GOT_ERR_ANCESTRY) {
3045 error = checkout_ancestry_error(
3046 head_ref, commit_id_str);
3050 error = got_worktree_set_base_commit_id(worktree, repo,
3054 /* Expand potentially abbreviated commit ID string. */
3055 free(commit_id_str);
3056 error = got_object_id_str(&commit_id_str, commit_id);
3060 commit_id = got_object_id_dup(
3061 got_worktree_get_base_commit_id(worktree));
3062 if (commit_id == NULL) {
3063 error = got_error_from_errno("got_object_id_dup");
3066 error = got_object_id_str(&commit_id_str, commit_id);
3071 error = got_pathlist_append(&paths, "", NULL);
3074 cpa.worktree_path = worktree_path;
3075 cpa.had_base_commit_ref_error = 0;
3076 cpa.verbosity = verbosity;
3077 error = got_worktree_checkout_files(worktree, &paths, repo,
3078 checkout_progress, &cpa, check_cancelled, NULL);
3082 if (got_ref_is_symbolic(head_ref)) {
3083 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3086 refname = got_ref_get_name(ref);
3088 refname = got_ref_get_name(head_ref);
3089 printf("Checked out %s: %s\n", refname, commit_id_str);
3090 printf("Now shut up and hack\n");
3091 if (cpa.had_base_commit_ref_error)
3092 show_worktree_base_ref_warning();
3095 const struct got_error *pack_err =
3096 got_repo_pack_fds_close(pack_fds);
3102 got_ref_close(head_ref);
3105 got_pathlist_free(&paths);
3106 free(commit_id_str);
3109 free(worktree_path);
3114 struct got_update_progress_arg {
3126 print_update_progress_stats(struct got_update_progress_arg *upa)
3128 if (!upa->did_something)
3131 if (upa->conflicts > 0)
3132 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3133 if (upa->obstructed > 0)
3134 printf("File paths obstructed by a non-regular file: %d\n",
3136 if (upa->not_updated > 0)
3137 printf("Files not updated because of existing merge "
3138 "conflicts: %d\n", upa->not_updated);
3142 * The meaning of some status codes differs between merge-style operations and
3143 * update operations. For example, the ! status code means "file was missing"
3144 * if changes were merged into the work tree, and "missing file was restored"
3145 * if the work tree was updated. This function should be used by any operation
3146 * which merges changes into the work tree without updating the work tree.
3149 print_merge_progress_stats(struct got_update_progress_arg *upa)
3151 if (!upa->did_something)
3154 if (upa->conflicts > 0)
3155 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3156 if (upa->obstructed > 0)
3157 printf("File paths obstructed by a non-regular file: %d\n",
3159 if (upa->missing > 0)
3160 printf("Files which had incoming changes but could not be "
3161 "found in the work tree: %d\n", upa->missing);
3162 if (upa->not_deleted > 0)
3163 printf("Files not deleted due to differences in deleted "
3164 "content: %d\n", upa->not_deleted);
3165 if (upa->unversioned > 0)
3166 printf("Files not merged because an unversioned file was "
3167 "found in the work tree: %d\n", upa->unversioned);
3173 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3179 static const struct got_error *
3180 update_progress(void *arg, unsigned char status, const char *path)
3182 struct got_update_progress_arg *upa = arg;
3184 if (status == GOT_STATUS_EXISTS ||
3185 status == GOT_STATUS_BASE_REF_ERR)
3188 upa->did_something = 1;
3190 /* Base commit bump happens silently. */
3191 if (status == GOT_STATUS_BUMP_BASE)
3194 if (status == GOT_STATUS_CONFLICT)
3196 if (status == GOT_STATUS_OBSTRUCTED)
3198 if (status == GOT_STATUS_CANNOT_UPDATE)
3200 if (status == GOT_STATUS_MISSING)
3202 if (status == GOT_STATUS_CANNOT_DELETE)
3204 if (status == GOT_STATUS_UNVERSIONED)
3207 while (path[0] == '/')
3209 if (upa->verbosity >= 0)
3210 printf("%c %s\n", status, path);
3215 static const struct got_error *
3216 switch_head_ref(struct got_reference *head_ref,
3217 struct got_object_id *commit_id, struct got_worktree *worktree,
3218 struct got_repository *repo)
3220 const struct got_error *err = NULL;
3222 int ref_has_moved = 0;
3224 /* Trivial case: switching between two different references. */
3225 if (strcmp(got_ref_get_name(head_ref),
3226 got_worktree_get_head_ref_name(worktree)) != 0) {
3227 printf("Switching work tree from %s to %s\n",
3228 got_worktree_get_head_ref_name(worktree),
3229 got_ref_get_name(head_ref));
3230 return got_worktree_set_head_ref(worktree, head_ref);
3233 err = check_linear_ancestry(commit_id,
3234 got_worktree_get_base_commit_id(worktree), 0, repo);
3236 if (err->code != GOT_ERR_ANCESTRY)
3243 /* Switching to a rebased branch with the same reference name. */
3244 err = got_object_id_str(&base_id_str,
3245 got_worktree_get_base_commit_id(worktree));
3248 printf("Reference %s now points at a different branch\n",
3249 got_worktree_get_head_ref_name(worktree));
3250 printf("Switching work tree from %s to %s\n", base_id_str,
3251 got_worktree_get_head_ref_name(worktree));
3255 static const struct got_error *
3256 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3258 const struct got_error *err;
3261 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3265 return got_error(GOT_ERR_REBASING);
3267 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3271 return got_error(GOT_ERR_HISTEDIT_BUSY);
3276 static const struct got_error *
3277 check_merge_in_progress(struct got_worktree *worktree,
3278 struct got_repository *repo)
3280 const struct got_error *err;
3283 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3287 return got_error(GOT_ERR_MERGE_BUSY);
3292 static const struct got_error *
3293 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3294 char *argv[], struct got_worktree *worktree)
3296 const struct got_error *err = NULL;
3298 struct got_pathlist_entry *new;
3304 return got_error_from_errno("strdup");
3305 return got_pathlist_append(paths, path, NULL);
3308 for (i = 0; i < argc; i++) {
3309 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3312 err = got_pathlist_insert(&new, paths, path, NULL);
3313 if (err || new == NULL /* duplicate */) {
3323 static const struct got_error *
3324 wrap_not_worktree_error(const struct got_error *orig_err,
3325 const char *cmdname, const char *path)
3327 const struct got_error *err;
3328 struct got_repository *repo;
3329 static char msg[512];
3330 int *pack_fds = NULL;
3332 err = got_repo_pack_fds_open(&pack_fds);
3336 err = got_repo_open(&repo, path, NULL, pack_fds);
3340 snprintf(msg, sizeof(msg),
3341 "'got %s' needs a work tree in addition to a git repository\n"
3342 "Work trees can be checked out from this Git repository with "
3344 "The got(1) manual page contains more information.", cmdname);
3345 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3346 got_repo_close(repo);
3348 const struct got_error *pack_err =
3349 got_repo_pack_fds_close(pack_fds);
3357 static const struct got_error *
3358 cmd_update(int argc, char *argv[])
3360 const struct got_error *error = NULL;
3361 struct got_repository *repo = NULL;
3362 struct got_worktree *worktree = NULL;
3363 char *worktree_path = NULL;
3364 struct got_object_id *commit_id = NULL;
3365 char *commit_id_str = NULL;
3366 const char *branch_name = NULL;
3367 struct got_reference *head_ref = NULL;
3368 struct got_pathlist_head paths;
3369 struct got_pathlist_entry *pe;
3370 int ch, verbosity = 0;
3371 struct got_update_progress_arg upa;
3372 int *pack_fds = NULL;
3376 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3379 branch_name = optarg;
3382 commit_id_str = strdup(optarg);
3383 if (commit_id_str == NULL)
3384 return got_error_from_errno("strdup");
3399 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3400 "unveil", NULL) == -1)
3403 worktree_path = getcwd(NULL, 0);
3404 if (worktree_path == NULL) {
3405 error = got_error_from_errno("getcwd");
3409 error = got_repo_pack_fds_open(&pack_fds);
3413 error = got_worktree_open(&worktree, worktree_path);
3415 if (error->code == GOT_ERR_NOT_WORKTREE)
3416 error = wrap_not_worktree_error(error, "update",
3421 error = check_rebase_or_histedit_in_progress(worktree);
3425 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3430 error = apply_unveil(got_repo_get_path(repo), 0,
3431 got_worktree_get_root_path(worktree));
3435 error = check_merge_in_progress(worktree, repo);
3439 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3443 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3444 got_worktree_get_head_ref_name(worktree), 0);
3447 if (commit_id_str == NULL) {
3448 error = got_ref_resolve(&commit_id, repo, head_ref);
3451 error = got_object_id_str(&commit_id_str, commit_id);
3455 struct got_reflist_head refs;
3457 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3461 error = got_repo_match_object_id(&commit_id, NULL,
3462 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3463 got_ref_list_free(&refs);
3464 free(commit_id_str);
3465 commit_id_str = NULL;
3468 error = got_object_id_str(&commit_id_str, commit_id);
3474 struct got_object_id *head_commit_id;
3475 TAILQ_FOREACH(pe, &paths, entry) {
3476 if (pe->path_len == 0)
3478 error = got_error_msg(GOT_ERR_BAD_PATH,
3479 "switching between branches requires that "
3480 "the entire work tree gets updated");
3483 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3486 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3488 free(head_commit_id);
3491 error = check_same_branch(commit_id, head_ref, NULL, repo);
3494 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3498 error = check_linear_ancestry(commit_id,
3499 got_worktree_get_base_commit_id(worktree), 0, repo);
3500 if (error != NULL) {
3501 if (error->code == GOT_ERR_ANCESTRY)
3502 error = got_error(GOT_ERR_BRANCH_MOVED);
3505 error = check_same_branch(commit_id, head_ref, NULL, repo);
3510 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3512 error = got_worktree_set_base_commit_id(worktree, repo,
3518 memset(&upa, 0, sizeof(upa));
3519 upa.verbosity = verbosity;
3520 error = got_worktree_checkout_files(worktree, &paths, repo,
3521 update_progress, &upa, check_cancelled, NULL);
3525 if (upa.did_something) {
3526 printf("Updated to %s: %s\n",
3527 got_worktree_get_head_ref_name(worktree), commit_id_str);
3529 printf("Already up-to-date\n");
3531 print_update_progress_stats(&upa);
3534 const struct got_error *pack_err =
3535 got_repo_pack_fds_close(pack_fds);
3540 free(worktree_path);
3541 TAILQ_FOREACH(pe, &paths, entry)
3542 free((char *)pe->path);
3543 got_pathlist_free(&paths);
3545 free(commit_id_str);
3549 static const struct got_error *
3550 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3551 const char *path, int diff_context, int ignore_whitespace,
3552 int force_text_diff, struct got_repository *repo, FILE *outfile)
3554 const struct got_error *err = NULL;
3555 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3556 FILE *f1 = NULL, *f2 = NULL;
3559 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3562 f1 = got_opentemp();
3564 err = got_error_from_errno("got_opentemp");
3569 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3573 f2 = got_opentemp();
3575 err = got_error_from_errno("got_opentemp");
3579 while (path[0] == '/')
3581 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3582 diff_context, ignore_whitespace, force_text_diff, outfile);
3585 got_object_blob_close(blob1);
3586 got_object_blob_close(blob2);
3587 if (f1 && fclose(f1) == EOF && err == NULL)
3588 err = got_error_from_errno("fclose");
3589 if (f2 && fclose(f2) == EOF && err == NULL)
3590 err = got_error_from_errno("fclose");
3594 static const struct got_error *
3595 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3596 const char *path, int diff_context, int ignore_whitespace,
3597 int force_text_diff, struct got_repository *repo, FILE *outfile)
3599 const struct got_error *err = NULL;
3600 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3601 struct got_diff_blob_output_unidiff_arg arg;
3602 FILE *f1 = NULL, *f2 = NULL;
3605 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3608 f1 = got_opentemp();
3610 err = got_error_from_errno("got_opentemp");
3615 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3619 f2 = got_opentemp();
3621 err = got_error_from_errno("got_opentemp");
3625 arg.diff_context = diff_context;
3626 arg.ignore_whitespace = ignore_whitespace;
3627 arg.force_text_diff = force_text_diff;
3628 arg.outfile = outfile;
3629 arg.line_offsets = NULL;
3631 while (path[0] == '/')
3633 err = got_diff_tree(tree1, tree2, f1, f2, path, path, repo,
3634 got_diff_blob_output_unidiff, &arg, 1);
3637 got_object_tree_close(tree1);
3639 got_object_tree_close(tree2);
3640 if (f1 && fclose(f1) == EOF && err == NULL)
3641 err = got_error_from_errno("fclose");
3642 if (f2 && fclose(f2) == EOF && err == NULL)
3643 err = got_error_from_errno("fclose");
3647 static const struct got_error *
3648 get_changed_paths(struct got_pathlist_head *paths,
3649 struct got_commit_object *commit, struct got_repository *repo)
3651 const struct got_error *err = NULL;
3652 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3653 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3654 struct got_object_qid *qid;
3656 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3658 struct got_commit_object *pcommit;
3659 err = got_object_open_as_commit(&pcommit, repo,
3664 tree_id1 = got_object_id_dup(
3665 got_object_commit_get_tree_id(pcommit));
3666 if (tree_id1 == NULL) {
3667 got_object_commit_close(pcommit);
3668 return got_error_from_errno("got_object_id_dup");
3670 got_object_commit_close(pcommit);
3675 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3680 tree_id2 = got_object_commit_get_tree_id(commit);
3681 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3685 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3686 got_diff_tree_collect_changed_paths, paths, 0);
3689 got_object_tree_close(tree1);
3691 got_object_tree_close(tree2);
3696 static const struct got_error *
3697 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3698 const char *path, int diff_context, struct got_repository *repo,
3701 const struct got_error *err = NULL;
3702 struct got_commit_object *pcommit = NULL;
3703 char *id_str1 = NULL, *id_str2 = NULL;
3704 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3705 struct got_object_qid *qid;
3707 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3709 err = got_object_open_as_commit(&pcommit, repo,
3715 if (path && path[0] != '\0') {
3717 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3720 err = got_object_id_str(&id_str2, obj_id2);
3726 err = got_object_id_by_path(&obj_id1, repo,
3729 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3734 err = got_object_id_str(&id_str1, obj_id1);
3741 err = got_object_get_type(&obj_type, repo, obj_id2);
3747 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3749 case GOT_OBJ_TYPE_BLOB:
3750 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3751 0, 0, repo, outfile);
3753 case GOT_OBJ_TYPE_TREE:
3754 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3755 0, 0, repo, outfile);
3758 err = got_error(GOT_ERR_OBJ_TYPE);
3764 obj_id2 = got_object_commit_get_tree_id(commit);
3765 err = got_object_id_str(&id_str2, obj_id2);
3769 obj_id1 = got_object_commit_get_tree_id(pcommit);
3770 err = got_object_id_str(&id_str1, obj_id1);
3775 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3776 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3783 got_object_commit_close(pcommit);
3788 get_datestr(time_t *time, char *datebuf)
3790 struct tm mytm, *tm;
3793 tm = gmtime_r(time, &mytm);
3796 s = asctime_r(tm, datebuf);
3799 p = strchr(s, '\n');
3805 static const struct got_error *
3806 match_commit(int *have_match, struct got_object_id *id,
3807 struct got_commit_object *commit, regex_t *regex)
3809 const struct got_error *err = NULL;
3810 regmatch_t regmatch;
3811 char *id_str = NULL, *logmsg = NULL;
3815 err = got_object_id_str(&id_str, id);
3819 err = got_object_commit_get_logmsg(&logmsg, commit);
3823 if (regexec(regex, got_object_commit_get_author(commit), 1,
3824 ®match, 0) == 0 ||
3825 regexec(regex, got_object_commit_get_committer(commit), 1,
3826 ®match, 0) == 0 ||
3827 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3828 regexec(regex, logmsg, 1, ®match, 0) == 0)
3837 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3840 regmatch_t regmatch;
3841 struct got_pathlist_entry *pe;
3845 TAILQ_FOREACH(pe, changed_paths, entry) {
3846 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3853 static const struct got_error *
3854 match_patch(int *have_match, struct got_commit_object *commit,
3855 struct got_object_id *id, const char *path, int diff_context,
3856 struct got_repository *repo, regex_t *regex, FILE *f)
3858 const struct got_error *err = NULL;
3860 size_t linesize = 0;
3862 regmatch_t regmatch;
3866 err = got_opentemp_truncate(f);
3870 err = print_patch(commit, id, path, diff_context, repo, f);
3874 if (fseeko(f, 0L, SEEK_SET) == -1) {
3875 err = got_error_from_errno("fseeko");
3879 while ((linelen = getline(&line, &linesize, f)) != -1) {
3880 if (regexec(regex, line, 1, ®match, 0) == 0) {
3890 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3892 static const struct got_error*
3893 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3894 struct got_object_id *id, struct got_repository *repo,
3897 static const struct got_error *err = NULL;
3898 struct got_reflist_entry *re;
3904 TAILQ_FOREACH(re, refs, entry) {
3905 struct got_tag_object *tag = NULL;
3906 struct got_object_id *ref_id;
3909 name = got_ref_get_name(re->ref);
3910 if (strcmp(name, GOT_REF_HEAD) == 0)
3912 if (strncmp(name, "refs/", 5) == 0)
3914 if (strncmp(name, "got/", 4) == 0)
3916 if (strncmp(name, "heads/", 6) == 0)
3918 if (strncmp(name, "remotes/", 8) == 0) {
3922 s = strstr(name, "/" GOT_REF_HEAD);
3923 if (s != NULL && s[strlen(s)] == '\0')
3926 err = got_ref_resolve(&ref_id, repo, re->ref);
3929 if (strncmp(name, "tags/", 5) == 0) {
3930 err = got_object_open_as_tag(&tag, repo, ref_id);
3932 if (err->code != GOT_ERR_OBJ_TYPE) {
3936 /* Ref points at something other than a tag. */
3941 cmp = got_object_id_cmp(tag ?
3942 got_object_tag_get_object_id(tag) : ref_id, id);
3945 got_object_tag_close(tag);
3949 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3950 s ? ", " : "", name) == -1) {
3951 err = got_error_from_errno("asprintf");
3962 static const struct got_error *
3963 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3964 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3966 const struct got_error *err = NULL;
3967 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3968 char *comma, *s, *nl;
3969 struct got_reflist_head *refs;
3970 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3972 time_t committer_time;
3974 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3976 err = build_refs_str(&ref_str, refs, id, repo, 1);
3980 /* Display the first matching ref only. */
3981 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3985 if (ref_str == NULL) {
3986 err = got_object_id_str(&id_str, id);
3991 committer_time = got_object_commit_get_committer_time(commit);
3992 if (gmtime_r(&committer_time, &tm) == NULL) {
3993 err = got_error_from_errno("gmtime_r");
3996 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
3997 err = got_error(GOT_ERR_NO_SPACE);
4001 err = got_object_commit_get_logmsg(&logmsg0, commit);
4006 while (isspace((unsigned char)s[0]))
4009 nl = strchr(s, '\n');
4015 printf("%s%-7s %s\n", datebuf, ref_str, s);
4017 printf("%s%.7s %s\n", datebuf, id_str, s);
4019 if (fflush(stdout) != 0 && err == NULL)
4020 err = got_error_from_errno("fflush");
4028 static const struct got_error *
4029 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4030 struct got_repository *repo, const char *path,
4031 struct got_pathlist_head *changed_paths, int show_patch,
4032 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4033 const char *custom_refs_str)
4035 const struct got_error *err = NULL;
4036 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4038 time_t committer_time;
4039 const char *author, *committer;
4040 char *refs_str = NULL;
4042 err = got_object_id_str(&id_str, id);
4046 if (custom_refs_str == NULL) {
4047 struct got_reflist_head *refs;
4048 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4050 err = build_refs_str(&refs_str, refs, id, repo, 0);
4056 printf(GOT_COMMIT_SEP_STR);
4057 if (custom_refs_str)
4058 printf("commit %s (%s)\n", id_str, custom_refs_str);
4060 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4061 refs_str ? refs_str : "", refs_str ? ")" : "");
4066 printf("from: %s\n", got_object_commit_get_author(commit));
4067 committer_time = got_object_commit_get_committer_time(commit);
4068 datestr = get_datestr(&committer_time, datebuf);
4070 printf("date: %s UTC\n", datestr);
4071 author = got_object_commit_get_author(commit);
4072 committer = got_object_commit_get_committer(commit);
4073 if (strcmp(author, committer) != 0)
4074 printf("via: %s\n", committer);
4075 if (got_object_commit_get_nparents(commit) > 1) {
4076 const struct got_object_id_queue *parent_ids;
4077 struct got_object_qid *qid;
4079 parent_ids = got_object_commit_get_parent_ids(commit);
4080 STAILQ_FOREACH(qid, parent_ids, entry) {
4081 err = got_object_id_str(&id_str, &qid->id);
4084 printf("parent %d: %s\n", n++, id_str);
4090 err = got_object_commit_get_logmsg(&logmsg0, commit);
4096 line = strsep(&logmsg, "\n");
4098 printf(" %s\n", line);
4102 if (changed_paths) {
4103 struct got_pathlist_entry *pe;
4104 TAILQ_FOREACH(pe, changed_paths, entry) {
4105 struct got_diff_changed_path *cp = pe->data;
4106 printf(" %c %s\n", cp->status, pe->path);
4111 err = print_patch(commit, id, path, diff_context, repo, stdout);
4116 if (fflush(stdout) != 0 && err == NULL)
4117 err = got_error_from_errno("fflush");
4124 static const struct got_error *
4125 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4126 struct got_repository *repo, const char *path, int show_changed_paths,
4127 int show_patch, const char *search_pattern, int diff_context, int limit,
4128 int log_branches, int reverse_display_order,
4129 struct got_reflist_object_id_map *refs_idmap, int one_line,
4132 const struct got_error *err;
4133 struct got_commit_graph *graph;
4136 struct got_object_id_queue reversed_commits;
4137 struct got_object_qid *qid;
4138 struct got_commit_object *commit;
4139 struct got_pathlist_head changed_paths;
4140 struct got_pathlist_entry *pe;
4142 STAILQ_INIT(&reversed_commits);
4143 TAILQ_INIT(&changed_paths);
4145 if (search_pattern && regcomp(®ex, search_pattern,
4146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4147 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4149 err = got_commit_graph_open(&graph, path, !log_branches);
4152 err = got_commit_graph_iter_start(graph, root_id, repo,
4153 check_cancelled, NULL);
4157 struct got_object_id *id;
4159 if (sigint_received || sigpipe_received)
4162 err = got_commit_graph_iter_next(&id, graph, repo,
4163 check_cancelled, NULL);
4165 if (err->code == GOT_ERR_ITER_COMPLETED)
4172 err = got_object_open_as_commit(&commit, repo, id);
4176 if (show_changed_paths && !reverse_display_order) {
4177 err = get_changed_paths(&changed_paths, commit, repo);
4182 if (search_pattern) {
4183 err = match_commit(&have_match, id, commit, ®ex);
4185 got_object_commit_close(commit);
4188 if (have_match == 0 && show_changed_paths)
4189 match_changed_paths(&have_match,
4190 &changed_paths, ®ex);
4191 if (have_match == 0 && show_patch) {
4192 err = match_patch(&have_match, commit, id,
4193 path, diff_context, repo, ®ex,
4198 if (have_match == 0) {
4199 got_object_commit_close(commit);
4200 TAILQ_FOREACH(pe, &changed_paths, entry) {
4201 free((char *)pe->path);
4204 got_pathlist_free(&changed_paths);
4209 if (reverse_display_order) {
4210 err = got_object_qid_alloc(&qid, id);
4213 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4214 got_object_commit_close(commit);
4217 err = print_commit_oneline(commit, id,
4220 err = print_commit(commit, id, repo, path,
4221 show_changed_paths ? &changed_paths : NULL,
4222 show_patch, diff_context, refs_idmap, NULL);
4223 got_object_commit_close(commit);
4227 if ((limit && --limit == 0) ||
4228 (end_id && got_object_id_cmp(id, end_id) == 0))
4231 TAILQ_FOREACH(pe, &changed_paths, entry) {
4232 free((char *)pe->path);
4235 got_pathlist_free(&changed_paths);
4237 if (reverse_display_order) {
4238 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4239 err = got_object_open_as_commit(&commit, repo,
4243 if (show_changed_paths) {
4244 err = get_changed_paths(&changed_paths,
4250 err = print_commit_oneline(commit, &qid->id,
4253 err = print_commit(commit, &qid->id, repo, path,
4254 show_changed_paths ? &changed_paths : NULL,
4255 show_patch, diff_context, refs_idmap, NULL);
4256 got_object_commit_close(commit);
4259 TAILQ_FOREACH(pe, &changed_paths, entry) {
4260 free((char *)pe->path);
4263 got_pathlist_free(&changed_paths);
4267 while (!STAILQ_EMPTY(&reversed_commits)) {
4268 qid = STAILQ_FIRST(&reversed_commits);
4269 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4270 got_object_qid_free(qid);
4272 TAILQ_FOREACH(pe, &changed_paths, entry) {
4273 free((char *)pe->path);
4276 got_pathlist_free(&changed_paths);
4279 got_commit_graph_close(graph);
4286 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4287 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4288 "[-r repository-path] [-R] [path]\n", getprogname());
4293 get_default_log_limit(void)
4295 const char *got_default_log_limit;
4299 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4300 if (got_default_log_limit == NULL)
4302 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4308 static const struct got_error *
4309 cmd_log(int argc, char *argv[])
4311 const struct got_error *error;
4312 struct got_repository *repo = NULL;
4313 struct got_worktree *worktree = NULL;
4314 struct got_object_id *start_id = NULL, *end_id = NULL;
4315 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4316 const char *start_commit = NULL, *end_commit = NULL;
4317 const char *search_pattern = NULL;
4318 int diff_context = -1, ch;
4319 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4320 int reverse_display_order = 0, one_line = 0;
4322 struct got_reflist_head refs;
4323 struct got_reflist_object_id_map *refs_idmap = NULL;
4324 FILE *tmpfile = NULL;
4325 int *pack_fds = NULL;
4330 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4336 limit = get_default_log_limit();
4338 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4344 show_changed_paths = 1;
4347 start_commit = optarg;
4350 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4353 errx(1, "number of context lines is %s: %s",
4357 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4359 errx(1, "number of commits is %s: %s",
4366 repo_path = realpath(optarg, NULL);
4367 if (repo_path == NULL)
4368 return got_error_from_errno2("realpath",
4370 got_path_strip_trailing_slashes(repo_path);
4373 reverse_display_order = 1;
4379 search_pattern = optarg;
4382 end_commit = optarg;
4393 if (diff_context == -1)
4395 else if (!show_patch)
4396 errx(1, "-C requires -p");
4398 if (one_line && (show_patch || show_changed_paths))
4399 errx(1, "cannot use -s with -p or -P");
4401 cwd = getcwd(NULL, 0);
4403 error = got_error_from_errno("getcwd");
4407 error = got_repo_pack_fds_open(&pack_fds);
4411 if (repo_path == NULL) {
4412 error = got_worktree_open(&worktree, cwd);
4413 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4420 error = got_worktree_resolve_path(&path, worktree,
4425 path = strdup(argv[0]);
4427 error = got_error_from_errno("strdup");
4431 } else if (argc != 0)
4434 if (repo_path == NULL) {
4435 repo_path = worktree ?
4436 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4438 if (repo_path == NULL) {
4439 error = got_error_from_errno("strdup");
4443 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4447 error = apply_unveil(got_repo_get_path(repo), 1,
4448 worktree ? got_worktree_get_root_path(worktree) : NULL);
4452 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4456 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4460 if (start_commit == NULL) {
4461 struct got_reference *head_ref;
4462 struct got_commit_object *commit = NULL;
4463 error = got_ref_open(&head_ref, repo,
4464 worktree ? got_worktree_get_head_ref_name(worktree)
4468 error = got_ref_resolve(&start_id, repo, head_ref);
4469 got_ref_close(head_ref);
4472 error = got_object_open_as_commit(&commit, repo,
4476 got_object_commit_close(commit);
4478 error = got_repo_match_object_id(&start_id, NULL,
4479 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4483 if (end_commit != NULL) {
4484 error = got_repo_match_object_id(&end_id, NULL,
4485 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4492 * If a path was specified on the command line it was resolved
4493 * to a path in the work tree above. Prepend the work tree's
4494 * path prefix to obtain the corresponding in-repository path.
4498 prefix = got_worktree_get_path_prefix(worktree);
4499 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4500 (path[0] != '\0') ? "/" : "", path) == -1) {
4501 error = got_error_from_errno("asprintf");
4506 error = got_repo_map_path(&in_repo_path, repo,
4512 path = in_repo_path;
4516 /* Release work tree lock. */
4517 got_worktree_close(worktree);
4521 if (search_pattern && show_patch) {
4522 tmpfile = got_opentemp();
4523 if (tmpfile == NULL) {
4524 error = got_error_from_errno("got_opentemp");
4529 error = print_commits(start_id, end_id, repo, path ? path : "",
4530 show_changed_paths, show_patch, search_pattern, diff_context,
4531 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4538 got_worktree_close(worktree);
4540 const struct got_error *close_err = got_repo_close(repo);
4545 const struct got_error *pack_err =
4546 got_repo_pack_fds_close(pack_fds);
4552 got_reflist_object_id_map_free(refs_idmap);
4553 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4554 error = got_error_from_errno("fclose");
4555 got_ref_list_free(&refs);
4562 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4563 "[-r repository-path] [-s] [-w] [-P] "
4564 "[object1 object2 | path ...]\n", getprogname());
4568 struct print_diff_arg {
4569 struct got_repository *repo;
4570 struct got_worktree *worktree;
4575 int ignore_whitespace;
4576 int force_text_diff;
4580 * Create a file which contains the target path of a symlink so we can feed
4581 * it as content to the diff engine.
4583 static const struct got_error *
4584 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4585 const char *abspath)
4587 const struct got_error *err = NULL;
4588 char target_path[PATH_MAX];
4589 ssize_t target_len, outlen;
4594 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4595 if (target_len == -1)
4596 return got_error_from_errno2("readlinkat", abspath);
4598 target_len = readlink(abspath, target_path, PATH_MAX);
4599 if (target_len == -1)
4600 return got_error_from_errno2("readlink", abspath);
4603 *fd = got_opentempfd();
4605 return got_error_from_errno("got_opentempfd");
4607 outlen = write(*fd, target_path, target_len);
4609 err = got_error_from_errno("got_opentempfd");
4613 if (lseek(*fd, 0, SEEK_SET) == -1) {
4614 err = got_error_from_errno2("lseek", abspath);
4625 static const struct got_error *
4626 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4627 const char *path, struct got_object_id *blob_id,
4628 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4629 int dirfd, const char *de_name)
4631 struct print_diff_arg *a = arg;
4632 const struct got_error *err = NULL;
4633 struct got_blob_object *blob1 = NULL;
4635 FILE *f1 = NULL, *f2 = NULL;
4636 char *abspath = NULL, *label1 = NULL;
4640 if (a->diff_staged) {
4641 if (staged_status != GOT_STATUS_MODIFY &&
4642 staged_status != GOT_STATUS_ADD &&
4643 staged_status != GOT_STATUS_DELETE)
4646 if (staged_status == GOT_STATUS_DELETE)
4648 if (status == GOT_STATUS_NONEXISTENT)
4649 return got_error_set_errno(ENOENT, path);
4650 if (status != GOT_STATUS_MODIFY &&
4651 status != GOT_STATUS_ADD &&
4652 status != GOT_STATUS_DELETE &&
4653 status != GOT_STATUS_CONFLICT)
4657 if (!a->header_shown) {
4658 printf("diff %s %s%s\n", a->id_str,
4659 got_worktree_get_root_path(a->worktree),
4660 a->diff_staged ? " (staged changes)" : "");
4661 a->header_shown = 1;
4664 if (a->diff_staged) {
4665 const char *label1 = NULL, *label2 = NULL;
4666 switch (staged_status) {
4667 case GOT_STATUS_MODIFY:
4671 case GOT_STATUS_ADD:
4674 case GOT_STATUS_DELETE:
4678 return got_error(GOT_ERR_FILE_STATUS);
4680 f1 = got_opentemp();
4682 err = got_error_from_errno("got_opentemp");
4685 f2 = got_opentemp();
4687 err = got_error_from_errno("got_opentemp");
4690 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4691 blob_id, staged_blob_id, label1, label2, a->diff_context,
4692 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4696 if (staged_status == GOT_STATUS_ADD ||
4697 staged_status == GOT_STATUS_MODIFY) {
4699 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4703 err = got_object_id_str(&id_str, staged_blob_id);
4706 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4707 err = got_error_from_errno("asprintf");
4712 } else if (status != GOT_STATUS_ADD) {
4713 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4718 if (status != GOT_STATUS_DELETE) {
4719 if (asprintf(&abspath, "%s/%s",
4720 got_worktree_get_root_path(a->worktree), path) == -1) {
4721 err = got_error_from_errno("asprintf");
4726 fd = openat(dirfd, de_name,
4727 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4729 if (!got_err_open_nofollow_on_symlink()) {
4730 err = got_error_from_errno2("openat",
4734 err = get_symlink_target_file(&fd, dirfd,
4740 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4742 if (!got_err_open_nofollow_on_symlink()) {
4743 err = got_error_from_errno2("open",
4747 err = get_symlink_target_file(&fd, dirfd,
4753 if (fstat(fd, &sb) == -1) {
4754 err = got_error_from_errno2("fstat", abspath);
4757 f2 = fdopen(fd, "r");
4759 err = got_error_from_errno2("fdopen", abspath);
4767 f1 = got_opentemp();
4769 err = got_error_from_errno("got_opentemp");
4772 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
4778 err = got_diff_blob_file(blob1, f1, size1, label1, f2, sb.st_size,
4779 path, a->diff_context, a->ignore_whitespace, a->force_text_diff,
4783 got_object_blob_close(blob1);
4784 if (f1 && fclose(f1) == EOF && err == NULL)
4785 err = got_error_from_errno("fclose");
4786 if (f2 && fclose(f2) == EOF && err == NULL)
4787 err = got_error_from_errno("fclose");
4788 if (fd != -1 && close(fd) == -1 && err == NULL)
4789 err = got_error_from_errno("close");
4794 static const struct got_error *
4795 cmd_diff(int argc, char *argv[])
4797 const struct got_error *error;
4798 struct got_repository *repo = NULL;
4799 struct got_worktree *worktree = NULL;
4800 char *cwd = NULL, *repo_path = NULL;
4801 const char *commit_args[2] = { NULL, NULL };
4802 int ncommit_args = 0;
4803 struct got_object_id *ids[2] = { NULL, NULL };
4804 char *labels[2] = { NULL, NULL };
4805 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4806 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4807 int force_text_diff = 0, force_path = 0, rflag = 0;
4809 struct got_reflist_head refs;
4810 struct got_pathlist_head paths;
4811 struct got_pathlist_entry *pe;
4812 FILE *f1 = NULL, *f2 = NULL;
4813 int *pack_fds = NULL;
4819 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4824 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4827 force_text_diff = 1;
4830 if (ncommit_args >= 2)
4831 errx(1, "too many -c options used");
4832 commit_args[ncommit_args++] = optarg;
4835 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4838 errx(1, "number of context lines is %s: %s",
4842 repo_path = realpath(optarg, NULL);
4843 if (repo_path == NULL)
4844 return got_error_from_errno2("realpath",
4846 got_path_strip_trailing_slashes(repo_path);
4853 ignore_whitespace = 1;
4867 cwd = getcwd(NULL, 0);
4869 error = got_error_from_errno("getcwd");
4873 error = got_repo_pack_fds_open(&pack_fds);
4877 if (repo_path == NULL) {
4878 error = got_worktree_open(&worktree, cwd);
4879 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4885 strdup(got_worktree_get_repo_path(worktree));
4886 if (repo_path == NULL) {
4887 error = got_error_from_errno("strdup");
4891 repo_path = strdup(cwd);
4892 if (repo_path == NULL) {
4893 error = got_error_from_errno("strdup");
4899 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4904 if (rflag || worktree == NULL || ncommit_args > 0) {
4906 error = got_error_msg(GOT_ERR_NOT_IMPL,
4907 "-P option can only be used when diffing "
4912 error = got_error_msg(GOT_ERR_NOT_IMPL,
4913 "-s option can only be used when diffing "
4919 error = apply_unveil(got_repo_get_path(repo), 1,
4920 worktree ? got_worktree_get_root_path(worktree) : NULL);
4924 if ((!force_path && argc == 2) || ncommit_args > 0) {
4925 int obj_type = (ncommit_args > 0 ?
4926 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4927 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4931 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4933 if (ncommit_args > 0)
4934 arg = commit_args[i];
4937 error = got_repo_match_object_id(&ids[i], &labels[i],
4938 arg, obj_type, &refs, repo);
4940 if (error->code != GOT_ERR_NOT_REF &&
4941 error->code != GOT_ERR_NO_OBJ)
4943 if (ncommit_args > 0)
4951 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4952 struct print_diff_arg arg;
4955 if (worktree == NULL) {
4956 if (argc == 2 && ids[0] == NULL) {
4957 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4959 } else if (argc == 2 && ids[1] == NULL) {
4960 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4962 } else if (argc > 0) {
4963 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4964 "%s", "specified paths cannot be resolved");
4967 error = got_error(GOT_ERR_NOT_WORKTREE);
4972 error = get_worktree_paths_from_argv(&paths, argc, argv,
4977 error = got_object_id_str(&id_str,
4978 got_worktree_get_base_commit_id(worktree));
4982 arg.worktree = worktree;
4983 arg.diff_context = diff_context;
4984 arg.id_str = id_str;
4985 arg.header_shown = 0;
4986 arg.diff_staged = diff_staged;
4987 arg.ignore_whitespace = ignore_whitespace;
4988 arg.force_text_diff = force_text_diff;
4990 error = got_worktree_status(worktree, &paths, repo, 0,
4991 print_diff, &arg, check_cancelled, NULL);
4996 if (ncommit_args == 1) {
4997 struct got_commit_object *commit;
4998 error = got_object_open_as_commit(&commit, repo, ids[0]);
5002 labels[1] = labels[0];
5004 if (got_object_commit_get_nparents(commit) > 0) {
5005 const struct got_object_id_queue *pids;
5006 struct got_object_qid *pid;
5007 pids = got_object_commit_get_parent_ids(commit);
5008 pid = STAILQ_FIRST(pids);
5009 ids[0] = got_object_id_dup(&pid->id);
5010 if (ids[0] == NULL) {
5011 error = got_error_from_errno(
5012 "got_object_id_dup");
5013 got_object_commit_close(commit);
5016 error = got_object_id_str(&labels[0], ids[0]);
5018 got_object_commit_close(commit);
5023 labels[0] = strdup("/dev/null");
5024 if (labels[0] == NULL) {
5025 error = got_error_from_errno("strdup");
5026 got_object_commit_close(commit);
5031 got_object_commit_close(commit);
5034 if (ncommit_args == 0 && argc > 2) {
5035 error = got_error_msg(GOT_ERR_BAD_PATH,
5036 "path arguments cannot be used when diffing two objects");
5041 error = got_object_get_type(&type1, repo, ids[0]);
5046 error = got_object_get_type(&type2, repo, ids[1]);
5049 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5050 error = got_error(GOT_ERR_OBJ_TYPE);
5053 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5054 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5055 "path arguments cannot be used when diffing blobs");
5059 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5061 struct got_pathlist_entry *new;
5065 error = got_worktree_resolve_path(&p, worktree,
5069 prefix = got_worktree_get_path_prefix(worktree);
5070 while (prefix[0] == '/')
5072 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5073 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5075 error = got_error_from_errno("asprintf");
5081 char *mapped_path, *s;
5082 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5088 in_repo_path = strdup(s);
5089 if (in_repo_path == NULL) {
5090 error = got_error_from_errno("asprintf");
5097 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5098 if (error || new == NULL /* duplicate */)
5105 /* Release work tree lock. */
5106 got_worktree_close(worktree);
5110 f1 = got_opentemp();
5112 error = got_error_from_errno("got_opentemp");
5116 f2 = got_opentemp();
5118 error = got_error_from_errno("got_opentemp");
5122 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5123 case GOT_OBJ_TYPE_BLOB:
5124 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5125 ids[0], ids[1], NULL, NULL, diff_context,
5126 ignore_whitespace, force_text_diff, repo, stdout);
5128 case GOT_OBJ_TYPE_TREE:
5129 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
5130 ids[0], ids[1], &paths, "", "", diff_context,
5131 ignore_whitespace, force_text_diff, repo, stdout);
5133 case GOT_OBJ_TYPE_COMMIT:
5134 printf("diff %s %s\n", labels[0], labels[1]);
5135 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5136 ids[0], ids[1], &paths, diff_context, ignore_whitespace,
5137 force_text_diff, repo, stdout);
5140 error = got_error(GOT_ERR_OBJ_TYPE);
5148 got_worktree_close(worktree);
5150 const struct got_error *close_err = got_repo_close(repo);
5155 const struct got_error *pack_err =
5156 got_repo_pack_fds_close(pack_fds);
5161 TAILQ_FOREACH(pe, &paths, entry)
5162 free((char *)pe->path);
5163 got_pathlist_free(&paths);
5164 got_ref_list_free(&refs);
5165 if (f1 && fclose(f1) == EOF && error == NULL)
5166 error = got_error_from_errno("fclose");
5167 if (f2 && fclose(f2) == EOF && error == NULL)
5168 error = got_error_from_errno("fclose");
5176 "usage: %s blame [-c commit] [-r repository-path] path\n",
5185 char datebuf[11]; /* YYYY-MM-DD + NUL */
5188 struct blame_cb_args {
5189 struct blame_line *lines;
5193 off_t *line_offsets;
5195 struct got_repository *repo;
5198 static const struct got_error *
5199 blame_cb(void *arg, int nlines, int lineno,
5200 struct got_commit_object *commit, struct got_object_id *id)
5202 const struct got_error *err = NULL;
5203 struct blame_cb_args *a = arg;
5204 struct blame_line *bline;
5206 size_t linesize = 0;
5209 time_t committer_time;
5211 if (nlines != a->nlines ||
5212 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5213 return got_error(GOT_ERR_RANGE);
5215 if (sigint_received)
5216 return got_error(GOT_ERR_ITER_COMPLETED);
5219 return NULL; /* no change in this commit */
5221 /* Annotate this line. */
5222 bline = &a->lines[lineno - 1];
5223 if (bline->annotated)
5225 err = got_object_id_str(&bline->id_str, id);
5229 bline->committer = strdup(got_object_commit_get_committer(commit));
5230 if (bline->committer == NULL) {
5231 err = got_error_from_errno("strdup");
5235 committer_time = got_object_commit_get_committer_time(commit);
5236 if (gmtime_r(&committer_time, &tm) == NULL)
5237 return got_error_from_errno("gmtime_r");
5238 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5240 err = got_error(GOT_ERR_NO_SPACE);
5243 bline->annotated = 1;
5245 /* Print lines annotated so far. */
5246 bline = &a->lines[a->lineno_cur - 1];
5247 if (!bline->annotated)
5250 offset = a->line_offsets[a->lineno_cur - 1];
5251 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5252 err = got_error_from_errno("fseeko");
5256 while (bline->annotated) {
5257 char *smallerthan, *at, *nl, *committer;
5260 if (getline(&line, &linesize, a->f) == -1) {
5262 err = got_error_from_errno("getline");
5266 committer = bline->committer;
5267 smallerthan = strchr(committer, '<');
5268 if (smallerthan && smallerthan[1] != '\0')
5269 committer = smallerthan + 1;
5270 at = strchr(committer, '@');
5273 len = strlen(committer);
5275 committer[8] = '\0';
5277 nl = strchr(line, '\n');
5280 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5281 bline->id_str, bline->datebuf, committer, line);
5284 bline = &a->lines[a->lineno_cur - 1];
5291 static const struct got_error *
5292 cmd_blame(int argc, char *argv[])
5294 const struct got_error *error;
5295 struct got_repository *repo = NULL;
5296 struct got_worktree *worktree = NULL;
5297 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5298 char *link_target = NULL;
5299 struct got_object_id *obj_id = NULL;
5300 struct got_object_id *commit_id = NULL;
5301 struct got_commit_object *commit = NULL;
5302 struct got_blob_object *blob = NULL;
5303 char *commit_id_str = NULL;
5304 struct blame_cb_args bca;
5305 int ch, obj_type, i;
5307 int *pack_fds = NULL;
5309 memset(&bca, 0, sizeof(bca));
5312 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5317 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5320 commit_id_str = optarg;
5323 repo_path = realpath(optarg, NULL);
5324 if (repo_path == NULL)
5325 return got_error_from_errno2("realpath",
5327 got_path_strip_trailing_slashes(repo_path);
5343 cwd = getcwd(NULL, 0);
5345 error = got_error_from_errno("getcwd");
5349 error = got_repo_pack_fds_open(&pack_fds);
5353 if (repo_path == NULL) {
5354 error = got_worktree_open(&worktree, cwd);
5355 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5361 strdup(got_worktree_get_repo_path(worktree));
5362 if (repo_path == NULL) {
5363 error = got_error_from_errno("strdup");
5368 repo_path = strdup(cwd);
5369 if (repo_path == NULL) {
5370 error = got_error_from_errno("strdup");
5376 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5381 const char *prefix = got_worktree_get_path_prefix(worktree);
5384 error = got_worktree_resolve_path(&p, worktree, path);
5387 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5388 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5390 error = got_error_from_errno("asprintf");
5395 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5397 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5400 error = got_repo_map_path(&in_repo_path, repo, path);
5405 if (commit_id_str == NULL) {
5406 struct got_reference *head_ref;
5407 error = got_ref_open(&head_ref, repo, worktree ?
5408 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5411 error = got_ref_resolve(&commit_id, repo, head_ref);
5412 got_ref_close(head_ref);
5416 struct got_reflist_head refs;
5418 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5422 error = got_repo_match_object_id(&commit_id, NULL,
5423 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5424 got_ref_list_free(&refs);
5430 /* Release work tree lock. */
5431 got_worktree_close(worktree);
5435 error = got_object_open_as_commit(&commit, repo, commit_id);
5439 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5444 error = got_object_id_by_path(&obj_id, repo, commit,
5445 link_target ? link_target : in_repo_path);
5449 error = got_object_get_type(&obj_type, repo, obj_id);
5453 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5454 error = got_error_path(link_target ? link_target : in_repo_path,
5459 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5462 bca.f = got_opentemp();
5463 if (bca.f == NULL) {
5464 error = got_error_from_errno("got_opentemp");
5467 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5468 &bca.line_offsets, bca.f, blob);
5469 if (error || bca.nlines == 0)
5472 /* Don't include \n at EOF in the blame line count. */
5473 if (bca.line_offsets[bca.nlines - 1] == filesize)
5476 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5477 if (bca.lines == NULL) {
5478 error = got_error_from_errno("calloc");
5482 bca.nlines_prec = 0;
5490 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5491 repo, blame_cb, &bca, check_cancelled, NULL);
5500 got_object_commit_close(commit);
5502 got_object_blob_close(blob);
5504 got_worktree_close(worktree);
5506 const struct got_error *close_err = got_repo_close(repo);
5511 const struct got_error *pack_err =
5512 got_repo_pack_fds_close(pack_fds);
5518 for (i = 0; i < bca.nlines; i++) {
5519 struct blame_line *bline = &bca.lines[i];
5520 free(bline->id_str);
5521 free(bline->committer);
5525 free(bca.line_offsets);
5526 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5527 error = got_error_from_errno("fclose");
5535 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5540 static const struct got_error *
5541 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5542 const char *root_path, struct got_repository *repo)
5544 const struct got_error *err = NULL;
5545 int is_root_path = (strcmp(path, root_path) == 0);
5546 const char *modestr = "";
5547 mode_t mode = got_tree_entry_get_mode(te);
5548 char *link_target = NULL;
5550 path += strlen(root_path);
5551 while (path[0] == '/')
5554 if (got_object_tree_entry_is_submodule(te))
5556 else if (S_ISLNK(mode)) {
5559 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5562 for (i = 0; i < strlen(link_target); i++) {
5563 if (!isprint((unsigned char)link_target[i]))
5564 link_target[i] = '?';
5569 else if (S_ISDIR(mode))
5571 else if (mode & S_IXUSR)
5574 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5575 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5576 link_target ? " -> ": "", link_target ? link_target : "");
5582 static const struct got_error *
5583 print_tree(const char *path, struct got_commit_object *commit,
5584 int show_ids, int recurse, const char *root_path,
5585 struct got_repository *repo)
5587 const struct got_error *err = NULL;
5588 struct got_object_id *tree_id = NULL;
5589 struct got_tree_object *tree = NULL;
5592 err = got_object_id_by_path(&tree_id, repo, commit, path);
5596 err = got_object_open_as_tree(&tree, repo, tree_id);
5599 nentries = got_object_tree_get_nentries(tree);
5600 for (i = 0; i < nentries; i++) {
5601 struct got_tree_entry *te;
5604 if (sigint_received || sigpipe_received)
5607 te = got_object_tree_get_entry(tree, i);
5610 err = got_object_id_str(&id_str,
5611 got_tree_entry_get_id(te));
5614 if (asprintf(&id, "%s ", id_str) == -1) {
5615 err = got_error_from_errno("asprintf");
5621 err = print_entry(te, id, path, root_path, repo);
5626 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5628 if (asprintf(&child_path, "%s%s%s", path,
5629 path[0] == '/' && path[1] == '\0' ? "" : "/",
5630 got_tree_entry_get_name(te)) == -1) {
5631 err = got_error_from_errno("asprintf");
5634 err = print_tree(child_path, commit, show_ids, 1,
5643 got_object_tree_close(tree);
5648 static const struct got_error *
5649 cmd_tree(int argc, char *argv[])
5651 const struct got_error *error;
5652 struct got_repository *repo = NULL;
5653 struct got_worktree *worktree = NULL;
5654 const char *path, *refname = NULL;
5655 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5656 struct got_object_id *commit_id = NULL;
5657 struct got_commit_object *commit = NULL;
5658 char *commit_id_str = NULL;
5659 int show_ids = 0, recurse = 0;
5661 int *pack_fds = NULL;
5664 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5669 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5672 commit_id_str = optarg;
5675 repo_path = realpath(optarg, NULL);
5676 if (repo_path == NULL)
5677 return got_error_from_errno2("realpath",
5679 got_path_strip_trailing_slashes(repo_path);
5703 cwd = getcwd(NULL, 0);
5705 error = got_error_from_errno("getcwd");
5709 error = got_repo_pack_fds_open(&pack_fds);
5713 if (repo_path == NULL) {
5714 error = got_worktree_open(&worktree, cwd);
5715 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5721 strdup(got_worktree_get_repo_path(worktree));
5722 if (repo_path == NULL)
5723 error = got_error_from_errno("strdup");
5727 repo_path = strdup(cwd);
5728 if (repo_path == NULL) {
5729 error = got_error_from_errno("strdup");
5735 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5740 const char *prefix = got_worktree_get_path_prefix(worktree);
5745 error = got_worktree_resolve_path(&p, worktree, path);
5748 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5749 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5751 error = got_error_from_errno("asprintf");
5756 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5760 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5765 error = got_repo_map_path(&in_repo_path, repo, path);
5770 if (commit_id_str == NULL) {
5771 struct got_reference *head_ref;
5773 refname = got_worktree_get_head_ref_name(worktree);
5775 refname = GOT_REF_HEAD;
5776 error = got_ref_open(&head_ref, repo, refname, 0);
5779 error = got_ref_resolve(&commit_id, repo, head_ref);
5780 got_ref_close(head_ref);
5784 struct got_reflist_head refs;
5786 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5790 error = got_repo_match_object_id(&commit_id, NULL,
5791 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5792 got_ref_list_free(&refs);
5798 /* Release work tree lock. */
5799 got_worktree_close(worktree);
5803 error = got_object_open_as_commit(&commit, repo, commit_id);
5807 error = print_tree(in_repo_path, commit, show_ids, recurse,
5808 in_repo_path, repo);
5815 got_object_commit_close(commit);
5817 got_worktree_close(worktree);
5819 const struct got_error *close_err = got_repo_close(repo);
5824 const struct got_error *pack_err =
5825 got_repo_pack_fds_close(pack_fds);
5836 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5837 "[-S status-codes] [path ...]\n", getprogname());
5841 struct got_status_arg {
5846 static const struct got_error *
5847 print_status(void *arg, unsigned char status, unsigned char staged_status,
5848 const char *path, struct got_object_id *blob_id,
5849 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5850 int dirfd, const char *de_name)
5852 struct got_status_arg *st = arg;
5854 if (status == staged_status && (status == GOT_STATUS_DELETE))
5855 status = GOT_STATUS_NO_CHANGE;
5856 if (st != NULL && st->status_codes) {
5857 size_t ncodes = strlen(st->status_codes);
5860 for (i = 0; i < ncodes ; i++) {
5862 if (status == st->status_codes[i] ||
5863 staged_status == st->status_codes[i]) {
5868 if (status == st->status_codes[i] ||
5869 staged_status == st->status_codes[i])
5874 if (st->suppress && j == 0)
5881 printf("%c%c %s\n", status, staged_status, path);
5885 static const struct got_error *
5886 cmd_status(int argc, char *argv[])
5888 const struct got_error *error = NULL;
5889 struct got_repository *repo = NULL;
5890 struct got_worktree *worktree = NULL;
5891 struct got_status_arg st;
5893 struct got_pathlist_head paths;
5894 struct got_pathlist_entry *pe;
5895 int ch, i, no_ignores = 0;
5896 int *pack_fds = NULL;
5900 memset(&st, 0, sizeof(st));
5901 st.status_codes = NULL;
5904 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5910 if (st.status_codes != NULL && st.suppress == 0)
5911 option_conflict('S', 's');
5915 for (i = 0; i < strlen(optarg); i++) {
5916 switch (optarg[i]) {
5917 case GOT_STATUS_MODIFY:
5918 case GOT_STATUS_ADD:
5919 case GOT_STATUS_DELETE:
5920 case GOT_STATUS_CONFLICT:
5921 case GOT_STATUS_MISSING:
5922 case GOT_STATUS_OBSTRUCTED:
5923 case GOT_STATUS_UNVERSIONED:
5924 case GOT_STATUS_MODE_CHANGE:
5925 case GOT_STATUS_NONEXISTENT:
5928 errx(1, "invalid status code '%c'",
5932 if (ch == 's' && st.suppress)
5933 option_conflict('s', 'S');
5934 st.status_codes = optarg;
5946 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5950 cwd = getcwd(NULL, 0);
5952 error = got_error_from_errno("getcwd");
5956 error = got_repo_pack_fds_open(&pack_fds);
5960 error = got_worktree_open(&worktree, cwd);
5962 if (error->code == GOT_ERR_NOT_WORKTREE)
5963 error = wrap_not_worktree_error(error, "status", cwd);
5967 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5972 error = apply_unveil(got_repo_get_path(repo), 1,
5973 got_worktree_get_root_path(worktree));
5977 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5981 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5982 print_status, &st, check_cancelled, NULL);
5985 const struct got_error *pack_err =
5986 got_repo_pack_fds_close(pack_fds);
5992 TAILQ_FOREACH(pe, &paths, entry)
5993 free((char *)pe->path);
5994 got_pathlist_free(&paths);
6003 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6004 "[-s reference] [-d] [name]\n",
6009 static const struct got_error *
6010 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6012 static const struct got_error *err = NULL;
6013 struct got_reflist_head refs;
6014 struct got_reflist_entry *re;
6017 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6018 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6023 TAILQ_FOREACH(re, &refs, entry) {
6025 refstr = got_ref_to_str(re->ref);
6026 if (refstr == NULL) {
6027 err = got_error_from_errno("got_ref_to_str");
6030 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6034 got_ref_list_free(&refs);
6038 static const struct got_error *
6039 delete_ref_by_name(struct got_repository *repo, const char *refname)
6041 const struct got_error *err;
6042 struct got_reference *ref;
6044 err = got_ref_open(&ref, repo, refname, 0);
6048 err = delete_ref(repo, ref);
6053 static const struct got_error *
6054 add_ref(struct got_repository *repo, const char *refname, const char *target)
6056 const struct got_error *err = NULL;
6057 struct got_object_id *id = NULL;
6058 struct got_reference *ref = NULL;
6059 struct got_reflist_head refs;
6062 * Don't let the user create a reference name with a leading '-'.
6063 * While technically a valid reference name, this case is usually
6064 * an unintended typo.
6066 if (refname[0] == '-')
6067 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6070 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6073 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6075 got_ref_list_free(&refs);
6079 err = got_ref_alloc(&ref, refname, id);
6083 err = got_ref_write(ref, repo);
6091 static const struct got_error *
6092 add_symref(struct got_repository *repo, const char *refname, const char *target)
6094 const struct got_error *err = NULL;
6095 struct got_reference *ref = NULL;
6096 struct got_reference *target_ref = NULL;
6099 * Don't let the user create a reference name with a leading '-'.
6100 * While technically a valid reference name, this case is usually
6101 * an unintended typo.
6103 if (refname[0] == '-')
6104 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6106 err = got_ref_open(&target_ref, repo, target, 0);
6110 err = got_ref_alloc_symref(&ref, refname, target_ref);
6114 err = got_ref_write(ref, repo);
6117 got_ref_close(target_ref);
6123 static const struct got_error *
6124 cmd_ref(int argc, char *argv[])
6126 const struct got_error *error = NULL;
6127 struct got_repository *repo = NULL;
6128 struct got_worktree *worktree = NULL;
6129 char *cwd = NULL, *repo_path = NULL;
6130 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6131 const char *obj_arg = NULL, *symref_target= NULL;
6132 char *refname = NULL;
6133 int *pack_fds = NULL;
6135 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6144 repo_path = realpath(optarg, NULL);
6145 if (repo_path == NULL)
6146 return got_error_from_errno2("realpath",
6148 got_path_strip_trailing_slashes(repo_path);
6154 symref_target = optarg;
6165 if (obj_arg && do_list)
6166 option_conflict('c', 'l');
6167 if (obj_arg && do_delete)
6168 option_conflict('c', 'd');
6169 if (obj_arg && symref_target)
6170 option_conflict('c', 's');
6171 if (symref_target && do_delete)
6172 option_conflict('s', 'd');
6173 if (symref_target && do_list)
6174 option_conflict('s', 'l');
6175 if (do_delete && do_list)
6176 option_conflict('d', 'l');
6177 if (sort_by_time && !do_list)
6178 errx(1, "-t option requires -l option");
6184 if (argc != 0 && argc != 1)
6187 refname = strdup(argv[0]);
6188 if (refname == NULL) {
6189 error = got_error_from_errno("strdup");
6196 refname = strdup(argv[0]);
6197 if (refname == NULL) {
6198 error = got_error_from_errno("strdup");
6204 got_path_strip_trailing_slashes(refname);
6207 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6208 "sendfd unveil", NULL) == -1)
6211 cwd = getcwd(NULL, 0);
6213 error = got_error_from_errno("getcwd");
6217 error = got_repo_pack_fds_open(&pack_fds);
6221 if (repo_path == NULL) {
6222 error = got_worktree_open(&worktree, cwd);
6223 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6229 strdup(got_worktree_get_repo_path(worktree));
6230 if (repo_path == NULL)
6231 error = got_error_from_errno("strdup");
6235 repo_path = strdup(cwd);
6236 if (repo_path == NULL) {
6237 error = got_error_from_errno("strdup");
6243 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6249 /* Remove "cpath" promise. */
6250 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6256 error = apply_unveil(got_repo_get_path(repo), do_list,
6257 worktree ? got_worktree_get_root_path(worktree) : NULL);
6262 error = list_refs(repo, refname, sort_by_time);
6264 error = delete_ref_by_name(repo, refname);
6265 else if (symref_target)
6266 error = add_symref(repo, refname, symref_target);
6268 if (obj_arg == NULL)
6270 error = add_ref(repo, refname, obj_arg);
6275 const struct got_error *close_err = got_repo_close(repo);
6280 got_worktree_close(worktree);
6282 const struct got_error *pack_err =
6283 got_repo_pack_fds_close(pack_fds);
6297 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6298 "[-n] [name]\n", getprogname());
6302 static const struct got_error *
6303 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6304 struct got_reference *ref)
6306 const struct got_error *err = NULL;
6307 const char *refname, *marker = " ";
6310 refname = got_ref_get_name(ref);
6311 if (worktree && strcmp(refname,
6312 got_worktree_get_head_ref_name(worktree)) == 0) {
6313 struct got_object_id *id = NULL;
6315 err = got_ref_resolve(&id, repo, ref);
6318 if (got_object_id_cmp(id,
6319 got_worktree_get_base_commit_id(worktree)) == 0)
6326 if (strncmp(refname, "refs/heads/", 11) == 0)
6328 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6330 if (strncmp(refname, "refs/remotes/", 13) == 0)
6333 refstr = got_ref_to_str(ref);
6335 return got_error_from_errno("got_ref_to_str");
6337 printf("%s%s: %s\n", marker, refname, refstr);
6342 static const struct got_error *
6343 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6345 const char *refname;
6347 if (worktree == NULL)
6348 return got_error(GOT_ERR_NOT_WORKTREE);
6350 refname = got_worktree_get_head_ref_name(worktree);
6352 if (strncmp(refname, "refs/heads/", 11) == 0)
6354 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6357 printf("%s\n", refname);
6362 static const struct got_error *
6363 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6366 static const struct got_error *err = NULL;
6367 struct got_reflist_head refs;
6368 struct got_reflist_entry *re;
6369 struct got_reference *temp_ref = NULL;
6370 int rebase_in_progress, histedit_in_progress;
6375 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6380 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6385 if (rebase_in_progress || histedit_in_progress) {
6386 err = got_ref_open(&temp_ref, repo,
6387 got_worktree_get_head_ref_name(worktree), 0);
6390 list_branch(repo, worktree, temp_ref);
6391 got_ref_close(temp_ref);
6395 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6396 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6401 TAILQ_FOREACH(re, &refs, entry)
6402 list_branch(repo, worktree, re->ref);
6404 got_ref_list_free(&refs);
6406 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6407 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6412 TAILQ_FOREACH(re, &refs, entry)
6413 list_branch(repo, worktree, re->ref);
6415 got_ref_list_free(&refs);
6420 static const struct got_error *
6421 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6422 const char *branch_name)
6424 const struct got_error *err = NULL;
6425 struct got_reference *ref = NULL;
6426 char *refname, *remote_refname = NULL;
6428 if (strncmp(branch_name, "refs/", 5) == 0)
6430 if (strncmp(branch_name, "heads/", 6) == 0)
6432 else if (strncmp(branch_name, "remotes/", 8) == 0)
6435 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6436 return got_error_from_errno("asprintf");
6438 if (asprintf(&remote_refname, "refs/remotes/%s",
6439 branch_name) == -1) {
6440 err = got_error_from_errno("asprintf");
6444 err = got_ref_open(&ref, repo, refname, 0);
6446 const struct got_error *err2;
6447 if (err->code != GOT_ERR_NOT_REF)
6450 * Keep 'err' intact such that if neither branch exists
6451 * we report "refs/heads" rather than "refs/remotes" in
6452 * our error message.
6454 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6461 strcmp(got_worktree_get_head_ref_name(worktree),
6462 got_ref_get_name(ref)) == 0) {
6463 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6464 "will not delete this work tree's current branch");
6468 err = delete_ref(repo, ref);
6473 free(remote_refname);
6477 static const struct got_error *
6478 add_branch(struct got_repository *repo, const char *branch_name,
6479 struct got_object_id *base_commit_id)
6481 const struct got_error *err = NULL;
6482 struct got_reference *ref = NULL;
6483 char *base_refname = NULL, *refname = NULL;
6486 * Don't let the user create a branch name with a leading '-'.
6487 * While technically a valid reference name, this case is usually
6488 * an unintended typo.
6490 if (branch_name[0] == '-')
6491 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6493 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6496 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6497 err = got_error_from_errno("asprintf");
6501 err = got_ref_open(&ref, repo, refname, 0);
6503 err = got_error(GOT_ERR_BRANCH_EXISTS);
6505 } else if (err->code != GOT_ERR_NOT_REF)
6508 err = got_ref_alloc(&ref, refname, base_commit_id);
6512 err = got_ref_write(ref, repo);
6521 static const struct got_error *
6522 cmd_branch(int argc, char *argv[])
6524 const struct got_error *error = NULL;
6525 struct got_repository *repo = NULL;
6526 struct got_worktree *worktree = NULL;
6527 char *cwd = NULL, *repo_path = NULL;
6528 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6529 const char *delref = NULL, *commit_id_arg = NULL;
6530 struct got_reference *ref = NULL;
6531 struct got_pathlist_head paths;
6532 struct got_pathlist_entry *pe;
6533 struct got_object_id *commit_id = NULL;
6534 char *commit_id_str = NULL;
6535 int *pack_fds = NULL;
6539 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6542 commit_id_arg = optarg;
6548 repo_path = realpath(optarg, NULL);
6549 if (repo_path == NULL)
6550 return got_error_from_errno2("realpath",
6552 got_path_strip_trailing_slashes(repo_path);
6569 if (do_list && delref)
6570 option_conflict('l', 'd');
6571 if (sort_by_time && !do_list)
6572 errx(1, "-t option requires -l option");
6577 if (!do_list && !delref && argc == 0)
6580 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6581 errx(1, "-c option can only be used when creating a branch");
6583 if (do_list || delref) {
6586 } else if (!do_show && argc != 1)
6590 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6591 "sendfd unveil", NULL) == -1)
6594 cwd = getcwd(NULL, 0);
6596 error = got_error_from_errno("getcwd");
6600 error = got_repo_pack_fds_open(&pack_fds);
6604 if (repo_path == NULL) {
6605 error = got_worktree_open(&worktree, cwd);
6606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6612 strdup(got_worktree_get_repo_path(worktree));
6613 if (repo_path == NULL)
6614 error = got_error_from_errno("strdup");
6618 repo_path = strdup(cwd);
6619 if (repo_path == NULL) {
6620 error = got_error_from_errno("strdup");
6626 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6631 if (do_list || do_show) {
6632 /* Remove "cpath" promise. */
6633 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6639 error = apply_unveil(got_repo_get_path(repo), do_list,
6640 worktree ? got_worktree_get_root_path(worktree) : NULL);
6645 error = show_current_branch(repo, worktree);
6647 error = list_branches(repo, worktree, sort_by_time);
6649 error = delete_branch(repo, worktree, delref);
6651 struct got_reflist_head refs;
6653 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6657 if (commit_id_arg == NULL)
6658 commit_id_arg = worktree ?
6659 got_worktree_get_head_ref_name(worktree) :
6661 error = got_repo_match_object_id(&commit_id, NULL,
6662 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6663 got_ref_list_free(&refs);
6666 error = add_branch(repo, argv[0], commit_id);
6669 if (worktree && do_update) {
6670 struct got_update_progress_arg upa;
6671 char *branch_refname = NULL;
6673 error = got_object_id_str(&commit_id_str, commit_id);
6676 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6680 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6682 error = got_error_from_errno("asprintf");
6685 error = got_ref_open(&ref, repo, branch_refname, 0);
6686 free(branch_refname);
6689 error = switch_head_ref(ref, commit_id, worktree,
6693 error = got_worktree_set_base_commit_id(worktree, repo,
6697 memset(&upa, 0, sizeof(upa));
6698 error = got_worktree_checkout_files(worktree, &paths,
6699 repo, update_progress, &upa, check_cancelled,
6703 if (upa.did_something) {
6704 printf("Updated to %s: %s\n",
6705 got_worktree_get_head_ref_name(worktree),
6708 print_update_progress_stats(&upa);
6715 const struct got_error *close_err = got_repo_close(repo);
6720 got_worktree_close(worktree);
6722 const struct got_error *pack_err =
6723 got_repo_pack_fds_close(pack_fds);
6731 free(commit_id_str);
6732 TAILQ_FOREACH(pe, &paths, entry)
6733 free((char *)pe->path);
6734 got_pathlist_free(&paths);
6743 "usage: %s tag [-c commit] [-r repository] [-l] "
6744 "[-m message] name\n", getprogname());
6749 static const struct got_error *
6750 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6752 const struct got_error *err = NULL;
6753 struct got_reflist_entry *re, *se, *new;
6754 struct got_object_id *re_id, *se_id;
6755 struct got_tag_object *re_tag, *se_tag;
6756 time_t re_time, se_time;
6758 STAILQ_FOREACH(re, tags, entry) {
6759 se = STAILQ_FIRST(sorted);
6761 err = got_reflist_entry_dup(&new, re);
6764 STAILQ_INSERT_HEAD(sorted, new, entry);
6767 err = got_ref_resolve(&re_id, repo, re->ref);
6770 err = got_object_open_as_tag(&re_tag, repo, re_id);
6774 re_time = got_object_tag_get_tagger_time(re_tag);
6775 got_object_tag_close(re_tag);
6779 err = got_ref_resolve(&se_id, repo, re->ref);
6782 err = got_object_open_as_tag(&se_tag, repo, se_id);
6786 se_time = got_object_tag_get_tagger_time(se_tag);
6787 got_object_tag_close(se_tag);
6789 if (se_time > re_time) {
6790 err = got_reflist_entry_dup(&new, re);
6793 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6796 se = STAILQ_NEXT(se, entry);
6805 static const struct got_error *
6806 list_tags(struct got_repository *repo)
6808 static const struct got_error *err = NULL;
6809 struct got_reflist_head refs;
6810 struct got_reflist_entry *re;
6814 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6818 TAILQ_FOREACH(re, &refs, entry) {
6819 const char *refname;
6820 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6824 struct got_object_id *id;
6825 struct got_tag_object *tag;
6826 struct got_commit_object *commit = NULL;
6828 refname = got_ref_get_name(re->ref);
6829 if (strncmp(refname, "refs/tags/", 10) != 0)
6832 refstr = got_ref_to_str(re->ref);
6833 if (refstr == NULL) {
6834 err = got_error_from_errno("got_ref_to_str");
6837 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6840 err = got_ref_resolve(&id, repo, re->ref);
6843 err = got_object_open_as_tag(&tag, repo, id);
6845 if (err->code != GOT_ERR_OBJ_TYPE) {
6849 /* "lightweight" tag */
6850 err = got_object_open_as_commit(&commit, repo, id);
6855 tagger = got_object_commit_get_committer(commit);
6857 got_object_commit_get_committer_time(commit);
6858 err = got_object_id_str(&id_str, id);
6864 tagger = got_object_tag_get_tagger(tag);
6865 tagger_time = got_object_tag_get_tagger_time(tag);
6866 err = got_object_id_str(&id_str,
6867 got_object_tag_get_object_id(tag));
6871 printf("from: %s\n", tagger);
6872 datestr = get_datestr(&tagger_time, datebuf);
6874 printf("date: %s UTC\n", datestr);
6876 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6878 switch (got_object_tag_get_object_type(tag)) {
6879 case GOT_OBJ_TYPE_BLOB:
6880 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6883 case GOT_OBJ_TYPE_TREE:
6884 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6887 case GOT_OBJ_TYPE_COMMIT:
6888 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6891 case GOT_OBJ_TYPE_TAG:
6892 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6901 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6904 got_object_commit_close(commit);
6906 tagmsg0 = strdup(got_object_tag_get_message(tag));
6907 got_object_tag_close(tag);
6908 if (tagmsg0 == NULL) {
6909 err = got_error_from_errno("strdup");
6916 line = strsep(&tagmsg, "\n");
6918 printf(" %s\n", line);
6923 got_ref_list_free(&refs);
6927 static const struct got_error *
6928 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6929 const char *tag_name, const char *repo_path)
6931 const struct got_error *err = NULL;
6932 char *template = NULL, *initial_content = NULL;
6933 char *editor = NULL;
6934 int initial_content_len;
6937 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6938 err = got_error_from_errno("asprintf");
6942 initial_content_len = asprintf(&initial_content,
6943 "\n# tagging commit %s as %s\n",
6944 commit_id_str, tag_name);
6945 if (initial_content_len == -1) {
6946 err = got_error_from_errno("asprintf");
6950 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6954 if (write(fd, initial_content, initial_content_len) == -1) {
6955 err = got_error_from_errno2("write", *tagmsg_path);
6959 err = get_editor(&editor);
6962 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6963 initial_content_len, 1);
6965 free(initial_content);
6969 if (fd != -1 && close(fd) == -1 && err == NULL)
6970 err = got_error_from_errno2("close", *tagmsg_path);
6972 /* Editor is done; we can now apply unveil(2) */
6974 err = apply_unveil(repo_path, 0, NULL);
6982 static const struct got_error *
6983 add_tag(struct got_repository *repo, const char *tagger,
6984 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6986 const struct got_error *err = NULL;
6987 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6988 char *label = NULL, *commit_id_str = NULL;
6989 struct got_reference *ref = NULL;
6990 char *refname = NULL, *tagmsg = NULL;
6991 char *tagmsg_path = NULL, *tag_id_str = NULL;
6992 int preserve_tagmsg = 0;
6993 struct got_reflist_head refs;
6998 * Don't let the user create a tag name with a leading '-'.
6999 * While technically a valid reference name, this case is usually
7000 * an unintended typo.
7002 if (tag_name[0] == '-')
7003 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7005 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7009 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7010 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7014 err = got_object_id_str(&commit_id_str, commit_id);
7018 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7019 refname = strdup(tag_name);
7020 if (refname == NULL) {
7021 err = got_error_from_errno("strdup");
7025 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
7026 err = got_error_from_errno("asprintf");
7030 err = got_ref_open(&ref, repo, refname, 0);
7032 err = got_error(GOT_ERR_TAG_EXISTS);
7034 } else if (err->code != GOT_ERR_NOT_REF)
7037 if (tagmsg_arg == NULL) {
7038 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7039 tag_name, got_repo_get_path(repo));
7041 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7042 tagmsg_path != NULL)
7043 preserve_tagmsg = 1;
7048 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7049 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7052 preserve_tagmsg = 1;
7056 err = got_ref_alloc(&ref, refname, tag_id);
7059 preserve_tagmsg = 1;
7063 err = got_ref_write(ref, repo);
7066 preserve_tagmsg = 1;
7070 err = got_object_id_str(&tag_id_str, tag_id);
7073 preserve_tagmsg = 1;
7076 printf("Created tag %s\n", tag_id_str);
7078 if (preserve_tagmsg) {
7079 fprintf(stderr, "%s: tag message preserved in %s\n",
7080 getprogname(), tagmsg_path);
7081 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7082 err = got_error_from_errno2("unlink", tagmsg_path);
7087 free(commit_id_str);
7091 got_ref_list_free(&refs);
7095 static const struct got_error *
7096 cmd_tag(int argc, char *argv[])
7098 const struct got_error *error = NULL;
7099 struct got_repository *repo = NULL;
7100 struct got_worktree *worktree = NULL;
7101 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7102 char *gitconfig_path = NULL, *tagger = NULL;
7103 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
7104 int ch, do_list = 0;
7105 int *pack_fds = NULL;
7107 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7110 commit_id_arg = optarg;
7116 repo_path = realpath(optarg, NULL);
7117 if (repo_path == NULL)
7118 return got_error_from_errno2("realpath",
7120 got_path_strip_trailing_slashes(repo_path);
7135 if (commit_id_arg != NULL)
7137 "-c option can only be used when creating a tag");
7139 option_conflict('l', 'm');
7142 } else if (argc != 1)
7148 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7149 "sendfd unveil", NULL) == -1)
7152 cwd = getcwd(NULL, 0);
7154 error = got_error_from_errno("getcwd");
7158 error = got_repo_pack_fds_open(&pack_fds);
7162 if (repo_path == NULL) {
7163 error = got_worktree_open(&worktree, cwd);
7164 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7170 strdup(got_worktree_get_repo_path(worktree));
7171 if (repo_path == NULL)
7172 error = got_error_from_errno("strdup");
7176 repo_path = strdup(cwd);
7177 if (repo_path == NULL) {
7178 error = got_error_from_errno("strdup");
7186 /* Release work tree lock. */
7187 got_worktree_close(worktree);
7190 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7195 /* Remove "cpath" promise. */
7196 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7200 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7203 error = list_tags(repo);
7205 error = get_gitconfig_path(&gitconfig_path);
7208 error = got_repo_open(&repo, repo_path, gitconfig_path,
7213 error = get_author(&tagger, repo, worktree);
7217 /* Release work tree lock. */
7218 got_worktree_close(worktree);
7223 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7228 if (commit_id_arg == NULL) {
7229 struct got_reference *head_ref;
7230 struct got_object_id *commit_id;
7231 error = got_ref_open(&head_ref, repo,
7232 worktree ? got_worktree_get_head_ref_name(worktree)
7236 error = got_ref_resolve(&commit_id, repo, head_ref);
7237 got_ref_close(head_ref);
7240 error = got_object_id_str(&commit_id_str, commit_id);
7246 error = add_tag(repo, tagger, tag_name,
7247 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7251 const struct got_error *close_err = got_repo_close(repo);
7256 got_worktree_close(worktree);
7258 const struct got_error *pack_err =
7259 got_repo_pack_fds_close(pack_fds);
7266 free(gitconfig_path);
7267 free(commit_id_str);
7275 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7280 static const struct got_error *
7281 add_progress(void *arg, unsigned char status, const char *path)
7283 while (path[0] == '/')
7285 printf("%c %s\n", status, path);
7289 static const struct got_error *
7290 cmd_add(int argc, char *argv[])
7292 const struct got_error *error = NULL;
7293 struct got_repository *repo = NULL;
7294 struct got_worktree *worktree = NULL;
7296 struct got_pathlist_head paths;
7297 struct got_pathlist_entry *pe;
7298 int ch, can_recurse = 0, no_ignores = 0;
7299 int *pack_fds = NULL;
7303 while ((ch = getopt(argc, argv, "IR")) != -1) {
7321 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7328 cwd = getcwd(NULL, 0);
7330 error = got_error_from_errno("getcwd");
7334 error = got_repo_pack_fds_open(&pack_fds);
7338 error = got_worktree_open(&worktree, cwd);
7340 if (error->code == GOT_ERR_NOT_WORKTREE)
7341 error = wrap_not_worktree_error(error, "add", cwd);
7345 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7350 error = apply_unveil(got_repo_get_path(repo), 1,
7351 got_worktree_get_root_path(worktree));
7355 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7362 TAILQ_FOREACH(pe, &paths, entry) {
7363 if (asprintf(&ondisk_path, "%s/%s",
7364 got_worktree_get_root_path(worktree),
7366 error = got_error_from_errno("asprintf");
7369 if (lstat(ondisk_path, &sb) == -1) {
7370 if (errno == ENOENT) {
7374 error = got_error_from_errno2("lstat",
7380 if (S_ISDIR(sb.st_mode)) {
7381 error = got_error_msg(GOT_ERR_BAD_PATH,
7382 "adding directories requires -R option");
7388 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7389 NULL, repo, no_ignores);
7392 const struct got_error *close_err = got_repo_close(repo);
7397 got_worktree_close(worktree);
7399 const struct got_error *pack_err =
7400 got_repo_pack_fds_close(pack_fds);
7405 TAILQ_FOREACH(pe, &paths, entry)
7406 free((char *)pe->path);
7407 got_pathlist_free(&paths);
7415 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7416 "path ...\n", getprogname());
7420 static const struct got_error *
7421 print_remove_status(void *arg, unsigned char status,
7422 unsigned char staged_status, const char *path)
7424 while (path[0] == '/')
7426 if (status == GOT_STATUS_NONEXISTENT)
7428 if (status == staged_status && (status == GOT_STATUS_DELETE))
7429 status = GOT_STATUS_NO_CHANGE;
7430 printf("%c%c %s\n", status, staged_status, path);
7434 static const struct got_error *
7435 cmd_remove(int argc, char *argv[])
7437 const struct got_error *error = NULL;
7438 struct got_worktree *worktree = NULL;
7439 struct got_repository *repo = NULL;
7440 const char *status_codes = NULL;
7442 struct got_pathlist_head paths;
7443 struct got_pathlist_entry *pe;
7444 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7445 int ignore_missing_paths = 0;
7446 int *pack_fds = NULL;
7450 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7453 delete_local_mods = 1;
7454 ignore_missing_paths = 1;
7463 for (i = 0; i < strlen(optarg); i++) {
7464 switch (optarg[i]) {
7465 case GOT_STATUS_MODIFY:
7466 delete_local_mods = 1;
7468 case GOT_STATUS_MISSING:
7469 ignore_missing_paths = 1;
7472 errx(1, "invalid status code '%c'",
7476 status_codes = optarg;
7488 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7495 cwd = getcwd(NULL, 0);
7497 error = got_error_from_errno("getcwd");
7501 error = got_repo_pack_fds_open(&pack_fds);
7505 error = got_worktree_open(&worktree, cwd);
7507 if (error->code == GOT_ERR_NOT_WORKTREE)
7508 error = wrap_not_worktree_error(error, "remove", cwd);
7512 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7517 error = apply_unveil(got_repo_get_path(repo), 1,
7518 got_worktree_get_root_path(worktree));
7522 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7529 TAILQ_FOREACH(pe, &paths, entry) {
7530 if (asprintf(&ondisk_path, "%s/%s",
7531 got_worktree_get_root_path(worktree),
7533 error = got_error_from_errno("asprintf");
7536 if (lstat(ondisk_path, &sb) == -1) {
7537 if (errno == ENOENT) {
7541 error = got_error_from_errno2("lstat",
7547 if (S_ISDIR(sb.st_mode)) {
7548 error = got_error_msg(GOT_ERR_BAD_PATH,
7549 "removing directories requires -R option");
7555 error = got_worktree_schedule_delete(worktree, &paths,
7556 delete_local_mods, status_codes, print_remove_status, NULL,
7557 repo, keep_on_disk, ignore_missing_paths);
7560 const struct got_error *close_err = got_repo_close(repo);
7565 got_worktree_close(worktree);
7567 const struct got_error *pack_err =
7568 got_repo_pack_fds_close(pack_fds);
7573 TAILQ_FOREACH(pe, &paths, entry)
7574 free((char *)pe->path);
7575 got_pathlist_free(&paths);
7583 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7584 "[-R] [patchfile]\n", getprogname());
7588 static const struct got_error *
7589 patch_from_stdin(int *patchfd)
7591 const struct got_error *err = NULL;
7593 char *path, buf[BUFSIZ];
7594 sig_t sighup, sigint, sigquit;
7596 err = got_opentemp_named_fd(&path, patchfd,
7597 GOT_TMPDIR_STR "/got-patch");
7603 sighup = signal(SIGHUP, SIG_DFL);
7604 sigint = signal(SIGINT, SIG_DFL);
7605 sigquit = signal(SIGQUIT, SIG_DFL);
7608 r = read(0, buf, sizeof(buf));
7610 err = got_error_from_errno("read");
7615 if (write(*patchfd, buf, r) == -1) {
7616 err = got_error_from_errno("write");
7621 signal(SIGHUP, sighup);
7622 signal(SIGINT, sigint);
7623 signal(SIGQUIT, sigquit);
7625 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7626 err = got_error_from_errno("lseek");
7636 static const struct got_error *
7637 patch_progress(void *arg, const char *old, const char *new,
7638 unsigned char status, const struct got_error *error, long old_from,
7639 long old_lines, long new_from, long new_lines, long offset,
7640 const struct got_error *hunk_err)
7642 const char *path = new == NULL ? old : new;
7644 while (*path == '/')
7648 printf("%c %s\n", status, path);
7651 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7653 if (offset != 0 || hunk_err != NULL) {
7654 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7655 old_lines, new_from, new_lines);
7656 if (hunk_err != NULL)
7657 printf("%s\n", hunk_err->msg);
7659 printf("applied with offset %ld\n", offset);
7665 static const struct got_error *
7666 cmd_patch(int argc, char *argv[])
7668 const struct got_error *error = NULL, *close_error = NULL;
7669 struct got_worktree *worktree = NULL;
7670 struct got_repository *repo = NULL;
7673 int ch, nop = 0, strip = -1, reverse = 0;
7675 int *pack_fds = NULL;
7677 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7683 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7685 errx(1, "pathname strip count is %s: %s",
7701 error = patch_from_stdin(&patchfd);
7704 } else if (argc == 1) {
7705 patchfd = open(argv[0], O_RDONLY);
7706 if (patchfd == -1) {
7707 error = got_error_from_errno2("open", argv[0]);
7713 if ((cwd = getcwd(NULL, 0)) == NULL) {
7714 error = got_error_from_errno("getcwd");
7718 error = got_repo_pack_fds_open(&pack_fds);
7722 error = got_worktree_open(&worktree, cwd);
7726 const char *repo_path = got_worktree_get_repo_path(worktree);
7727 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7731 error = apply_unveil(got_repo_get_path(repo), 0,
7732 got_worktree_get_root_path(worktree));
7737 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7742 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7743 &patch_progress, NULL, check_cancelled, NULL);
7747 close_error = got_repo_close(repo);
7749 error = close_error;
7751 if (worktree != NULL) {
7752 close_error = got_worktree_close(worktree);
7754 error = close_error;
7757 const struct got_error *pack_err =
7758 got_repo_pack_fds_close(pack_fds);
7770 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7771 "path ...\n", getprogname());
7775 static const struct got_error *
7776 revert_progress(void *arg, unsigned char status, const char *path)
7778 if (status == GOT_STATUS_UNVERSIONED)
7781 while (path[0] == '/')
7783 printf("%c %s\n", status, path);
7787 struct choose_patch_arg {
7788 FILE *patch_script_file;
7792 static const struct got_error *
7793 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7794 int nchanges, const char *action)
7796 const struct got_error *err;
7798 size_t linesize = 0;
7802 case GOT_STATUS_ADD:
7803 printf("A %s\n%s this addition? [y/n] ", path, action);
7805 case GOT_STATUS_DELETE:
7806 printf("D %s\n%s this deletion? [y/n] ", path, action);
7808 case GOT_STATUS_MODIFY:
7809 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7810 return got_error_from_errno("fseek");
7811 printf(GOT_COMMIT_SEP_STR);
7812 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7814 if (linelen == -1 && ferror(patch_file)) {
7815 err = got_error_from_errno("getline");
7820 printf(GOT_COMMIT_SEP_STR);
7821 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7822 path, n, nchanges, action);
7825 return got_error_path(path, GOT_ERR_FILE_STATUS);
7831 static const struct got_error *
7832 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7833 FILE *patch_file, int n, int nchanges)
7835 const struct got_error *err = NULL;
7837 size_t linesize = 0;
7840 struct choose_patch_arg *a = arg;
7842 *choice = GOT_PATCH_CHOICE_NONE;
7844 if (a->patch_script_file) {
7846 err = show_change(status, path, patch_file, n, nchanges,
7850 linelen = getline(&line, &linesize, a->patch_script_file);
7851 if (linelen == -1) {
7852 if (ferror(a->patch_script_file))
7853 return got_error_from_errno("getline");
7856 nl = strchr(line, '\n');
7859 if (strcmp(line, "y") == 0) {
7860 *choice = GOT_PATCH_CHOICE_YES;
7862 } else if (strcmp(line, "n") == 0) {
7863 *choice = GOT_PATCH_CHOICE_NO;
7865 } else if (strcmp(line, "q") == 0 &&
7866 status == GOT_STATUS_MODIFY) {
7867 *choice = GOT_PATCH_CHOICE_QUIT;
7870 printf("invalid response '%s'\n", line);
7875 while (resp != 'y' && resp != 'n' && resp != 'q') {
7876 err = show_change(status, path, patch_file, n, nchanges,
7883 if (status == GOT_STATUS_MODIFY) {
7884 if (resp != 'y' && resp != 'n' && resp != 'q') {
7885 printf("invalid response '%c'\n", resp);
7888 } else if (resp != 'y' && resp != 'n') {
7889 printf("invalid response '%c'\n", resp);
7895 *choice = GOT_PATCH_CHOICE_YES;
7896 else if (resp == 'n')
7897 *choice = GOT_PATCH_CHOICE_NO;
7898 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7899 *choice = GOT_PATCH_CHOICE_QUIT;
7904 static const struct got_error *
7905 cmd_revert(int argc, char *argv[])
7907 const struct got_error *error = NULL;
7908 struct got_worktree *worktree = NULL;
7909 struct got_repository *repo = NULL;
7910 char *cwd = NULL, *path = NULL;
7911 struct got_pathlist_head paths;
7912 struct got_pathlist_entry *pe;
7913 int ch, can_recurse = 0, pflag = 0;
7914 FILE *patch_script_file = NULL;
7915 const char *patch_script_path = NULL;
7916 struct choose_patch_arg cpa;
7917 int *pack_fds = NULL;
7921 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7927 patch_script_path = optarg;
7942 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7943 "unveil", NULL) == -1)
7948 if (patch_script_path && !pflag)
7949 errx(1, "-F option can only be used together with -p option");
7951 cwd = getcwd(NULL, 0);
7953 error = got_error_from_errno("getcwd");
7957 error = got_repo_pack_fds_open(&pack_fds);
7961 error = got_worktree_open(&worktree, cwd);
7963 if (error->code == GOT_ERR_NOT_WORKTREE)
7964 error = wrap_not_worktree_error(error, "revert", cwd);
7968 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7973 if (patch_script_path) {
7974 patch_script_file = fopen(patch_script_path, "re");
7975 if (patch_script_file == NULL) {
7976 error = got_error_from_errno2("fopen",
7981 error = apply_unveil(got_repo_get_path(repo), 1,
7982 got_worktree_get_root_path(worktree));
7986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7993 TAILQ_FOREACH(pe, &paths, entry) {
7994 if (asprintf(&ondisk_path, "%s/%s",
7995 got_worktree_get_root_path(worktree),
7997 error = got_error_from_errno("asprintf");
8000 if (lstat(ondisk_path, &sb) == -1) {
8001 if (errno == ENOENT) {
8005 error = got_error_from_errno2("lstat",
8011 if (S_ISDIR(sb.st_mode)) {
8012 error = got_error_msg(GOT_ERR_BAD_PATH,
8013 "reverting directories requires -R option");
8019 cpa.patch_script_file = patch_script_file;
8020 cpa.action = "revert";
8021 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8022 pflag ? choose_patch : NULL, &cpa, repo);
8024 if (patch_script_file && fclose(patch_script_file) == EOF &&
8026 error = got_error_from_errno2("fclose", patch_script_path);
8028 const struct got_error *close_err = got_repo_close(repo);
8033 got_worktree_close(worktree);
8035 const struct got_error *pack_err =
8036 got_repo_pack_fds_close(pack_fds);
8049 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8050 "[path ...]\n", getprogname());
8054 struct collect_commit_logmsg_arg {
8055 const char *cmdline_log;
8056 const char *prepared_log;
8057 int non_interactive;
8059 const char *worktree_path;
8060 const char *branch_name;
8061 const char *repo_path;
8066 static const struct got_error *
8067 read_prepared_logmsg(char **logmsg, const char *path)
8069 const struct got_error *err = NULL;
8075 memset(&sb, 0, sizeof(sb));
8077 f = fopen(path, "re");
8079 return got_error_from_errno2("fopen", path);
8081 if (fstat(fileno(f), &sb) == -1) {
8082 err = got_error_from_errno2("fstat", path);
8085 if (sb.st_size == 0) {
8086 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8090 *logmsg = malloc(sb.st_size + 1);
8091 if (*logmsg == NULL) {
8092 err = got_error_from_errno("malloc");
8096 r = fread(*logmsg, 1, sb.st_size, f);
8097 if (r != sb.st_size) {
8099 err = got_error_from_errno2("fread", path);
8101 err = got_error(GOT_ERR_IO);
8104 (*logmsg)[sb.st_size] = '\0';
8106 if (fclose(f) == EOF && err == NULL)
8107 err = got_error_from_errno2("fclose", path);
8116 static const struct got_error *
8117 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8120 char *initial_content = NULL;
8121 struct got_pathlist_entry *pe;
8122 const struct got_error *err = NULL;
8123 char *template = NULL;
8124 struct collect_commit_logmsg_arg *a = arg;
8125 int initial_content_len;
8129 /* if a message was specified on the command line, just use it */
8130 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8131 len = strlen(a->cmdline_log) + 1;
8132 *logmsg = malloc(len + 1);
8133 if (*logmsg == NULL)
8134 return got_error_from_errno("malloc");
8135 strlcpy(*logmsg, a->cmdline_log, len);
8137 } else if (a->prepared_log != NULL && a->non_interactive)
8138 return read_prepared_logmsg(logmsg, a->prepared_log);
8140 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8141 return got_error_from_errno("asprintf");
8143 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8147 if (a->prepared_log) {
8149 err = read_prepared_logmsg(&msg, a->prepared_log);
8152 if (write(fd, msg, strlen(msg)) == -1) {
8153 err = got_error_from_errno2("write", a->logmsg_path);
8160 initial_content_len = asprintf(&initial_content,
8161 "\n# changes to be committed on branch %s:\n",
8163 if (initial_content_len == -1) {
8164 err = got_error_from_errno("asprintf");
8168 if (write(fd, initial_content, initial_content_len) == -1) {
8169 err = got_error_from_errno2("write", a->logmsg_path);
8173 TAILQ_FOREACH(pe, commitable_paths, entry) {
8174 struct got_commitable *ct = pe->data;
8175 dprintf(fd, "# %c %s\n",
8176 got_commitable_get_status(ct),
8177 got_commitable_get_path(ct));
8180 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8181 initial_content_len, a->prepared_log ? 0 : 1);
8183 free(initial_content);
8186 if (fd != -1 && close(fd) == -1 && err == NULL)
8187 err = got_error_from_errno2("close", a->logmsg_path);
8189 /* Editor is done; we can now apply unveil(2) */
8191 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8199 static const struct got_error *
8200 cmd_commit(int argc, char *argv[])
8202 const struct got_error *error = NULL;
8203 struct got_worktree *worktree = NULL;
8204 struct got_repository *repo = NULL;
8205 char *cwd = NULL, *id_str = NULL;
8206 struct got_object_id *id = NULL;
8207 const char *logmsg = NULL;
8208 char *prepared_logmsg = NULL;
8209 struct collect_commit_logmsg_arg cl_arg;
8210 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8211 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8212 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8213 struct got_pathlist_head paths;
8214 int *pack_fds = NULL;
8217 cl_arg.logmsg_path = NULL;
8219 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8223 option_conflict('F', 'm');
8224 prepared_logmsg = realpath(optarg, NULL);
8225 if (prepared_logmsg == NULL)
8226 return got_error_from_errno2("realpath",
8230 if (prepared_logmsg)
8231 option_conflict('m', 'F');
8235 non_interactive = 1;
8238 allow_bad_symlinks = 1;
8250 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8251 "unveil", NULL) == -1)
8254 cwd = getcwd(NULL, 0);
8256 error = got_error_from_errno("getcwd");
8260 error = got_repo_pack_fds_open(&pack_fds);
8264 error = got_worktree_open(&worktree, cwd);
8266 if (error->code == GOT_ERR_NOT_WORKTREE)
8267 error = wrap_not_worktree_error(error, "commit", cwd);
8271 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8274 if (rebase_in_progress) {
8275 error = got_error(GOT_ERR_REBASING);
8279 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8284 error = get_gitconfig_path(&gitconfig_path);
8287 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8288 gitconfig_path, pack_fds);
8292 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8295 if (merge_in_progress) {
8296 error = got_error(GOT_ERR_MERGE_BUSY);
8300 error = get_author(&author, repo, worktree);
8305 * unveil(2) traverses exec(2); if an editor is used we have
8306 * to apply unveil after the log message has been written.
8308 if (logmsg == NULL || strlen(logmsg) == 0)
8309 error = get_editor(&editor);
8311 error = apply_unveil(got_repo_get_path(repo), 0,
8312 got_worktree_get_root_path(worktree));
8316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8320 cl_arg.editor = editor;
8321 cl_arg.cmdline_log = logmsg;
8322 cl_arg.prepared_log = prepared_logmsg;
8323 cl_arg.non_interactive = non_interactive;
8324 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8325 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8326 if (!histedit_in_progress) {
8327 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8328 error = got_error(GOT_ERR_COMMIT_BRANCH);
8331 cl_arg.branch_name += 11;
8333 cl_arg.repo_path = got_repo_get_path(repo);
8334 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8335 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8336 print_status, NULL, repo);
8338 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8339 cl_arg.logmsg_path != NULL)
8340 preserve_logmsg = 1;
8344 error = got_object_id_str(&id_str, id);
8347 printf("Created commit %s\n", id_str);
8349 if (preserve_logmsg) {
8350 fprintf(stderr, "%s: log message preserved in %s\n",
8351 getprogname(), cl_arg.logmsg_path);
8352 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8354 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8355 free(cl_arg.logmsg_path);
8357 const struct got_error *close_err = got_repo_close(repo);
8362 got_worktree_close(worktree);
8364 const struct got_error *pack_err =
8365 got_repo_pack_fds_close(pack_fds);
8372 free(gitconfig_path);
8375 free(prepared_logmsg);
8382 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8383 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8384 "[remote-repository]\n", getprogname());
8389 print_load_info(int print_colored, int print_found, int print_trees,
8390 int ncolored, int nfound, int ntrees)
8392 if (print_colored) {
8393 printf("%d commit%s colored", ncolored,
8394 ncolored == 1 ? "" : "s");
8397 printf("%s%d object%s found",
8398 ncolored > 0 ? "; " : "",
8399 nfound, nfound == 1 ? "" : "s");
8402 printf("; %d tree%s scanned", ntrees,
8403 ntrees == 1 ? "" : "s");
8407 struct got_send_progress_arg {
8408 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8415 int last_nobj_total;
8419 int printed_something;
8421 struct got_pathlist_head *delete_branches;
8424 static const struct got_error *
8425 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8426 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8427 int nobj_written, off_t bytes_sent, const char *refname, int success)
8429 struct got_send_progress_arg *a = arg;
8430 char scaled_packsize[FMT_SCALED_STRSIZE];
8431 char scaled_sent[FMT_SCALED_STRSIZE];
8432 int p_deltify = 0, p_written = 0, p_sent = 0;
8433 int print_colored = 0, print_found = 0, print_trees = 0;
8434 int print_searching = 0, print_total = 0;
8435 int print_deltify = 0, print_written = 0, print_sent = 0;
8437 if (a->verbosity < 0)
8441 const char *status = success ? "accepted" : "rejected";
8444 struct got_pathlist_entry *pe;
8445 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8446 const char *branchname = pe->path;
8447 if (got_path_cmp(branchname, refname,
8448 strlen(branchname), strlen(refname)) == 0) {
8450 a->sent_something = 1;
8456 if (a->printed_something)
8458 printf("Server has %s %s", status, refname);
8459 a->printed_something = 1;
8463 if (a->last_ncolored != ncolored) {
8465 a->last_ncolored = ncolored;
8468 if (a->last_nfound != nfound) {
8471 a->last_nfound = nfound;
8474 if (a->last_ntrees != ntrees) {
8478 a->last_ntrees = ntrees;
8481 if ((print_colored || print_found || print_trees) &&
8484 print_load_info(print_colored, print_found, print_trees,
8485 ncolored, nfound, ntrees);
8486 a->printed_something = 1;
8489 } else if (!a->loading_done) {
8491 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8493 a->loading_done = 1;
8496 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8497 return got_error_from_errno("fmt_scaled");
8498 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8499 return got_error_from_errno("fmt_scaled");
8501 if (a->last_ncommits != ncommits) {
8502 print_searching = 1;
8503 a->last_ncommits = ncommits;
8506 if (a->last_nobj_total != nobj_total) {
8507 print_searching = 1;
8509 a->last_nobj_total = nobj_total;
8512 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8513 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8514 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8515 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8516 return got_error(GOT_ERR_NO_SPACE);
8519 if (nobj_deltify > 0 || nobj_written > 0) {
8520 if (nobj_deltify > 0) {
8521 p_deltify = (nobj_deltify * 100) / nobj_total;
8522 if (p_deltify != a->last_p_deltify) {
8523 a->last_p_deltify = p_deltify;
8524 print_searching = 1;
8529 if (nobj_written > 0) {
8530 p_written = (nobj_written * 100) / nobj_total;
8531 if (p_written != a->last_p_written) {
8532 a->last_p_written = p_written;
8533 print_searching = 1;
8541 if (bytes_sent > 0) {
8542 p_sent = (bytes_sent * 100) / packfile_size;
8543 if (p_sent != a->last_p_sent) {
8544 a->last_p_sent = p_sent;
8545 print_searching = 1;
8551 a->sent_something = 1;
8554 if (print_searching || print_total || print_deltify || print_written ||
8557 if (print_searching)
8558 printf("packing %d reference%s", ncommits,
8559 ncommits == 1 ? "" : "s");
8561 printf("; %d object%s", nobj_total,
8562 nobj_total == 1 ? "" : "s");
8564 printf("; deltify: %d%%", p_deltify);
8566 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8567 scaled_packsize, p_sent);
8568 else if (print_written)
8569 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8570 scaled_packsize, p_written);
8571 if (print_searching || print_total || print_deltify ||
8572 print_written || print_sent) {
8573 a->printed_something = 1;
8579 static const struct got_error *
8580 cmd_send(int argc, char *argv[])
8582 const struct got_error *error = NULL;
8583 char *cwd = NULL, *repo_path = NULL;
8584 const char *remote_name;
8585 char *proto = NULL, *host = NULL, *port = NULL;
8586 char *repo_name = NULL, *server_path = NULL;
8587 const struct got_remote_repo *remotes, *remote = NULL;
8588 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8589 struct got_repository *repo = NULL;
8590 struct got_worktree *worktree = NULL;
8591 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8592 struct got_pathlist_head branches;
8593 struct got_pathlist_head tags;
8594 struct got_reflist_head all_branches;
8595 struct got_reflist_head all_tags;
8596 struct got_pathlist_head delete_args;
8597 struct got_pathlist_head delete_branches;
8598 struct got_reflist_entry *re;
8599 struct got_pathlist_entry *pe;
8600 int i, ch, sendfd = -1, sendstatus;
8602 struct got_send_progress_arg spa;
8603 int verbosity = 0, overwrite_refs = 0;
8604 int send_all_branches = 0, send_all_tags = 0;
8605 struct got_reference *ref = NULL;
8606 int *pack_fds = NULL;
8608 TAILQ_INIT(&branches);
8610 TAILQ_INIT(&all_branches);
8611 TAILQ_INIT(&all_tags);
8612 TAILQ_INIT(&delete_args);
8613 TAILQ_INIT(&delete_branches);
8615 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8618 send_all_branches = 1;
8621 error = got_pathlist_append(&branches, optarg, NULL);
8627 error = got_pathlist_append(&delete_args, optarg, NULL);
8635 repo_path = realpath(optarg, NULL);
8636 if (repo_path == NULL)
8637 return got_error_from_errno2("realpath",
8639 got_path_strip_trailing_slashes(repo_path);
8642 error = got_pathlist_append(&tags, optarg, NULL);
8653 else if (verbosity < 3)
8667 if (send_all_branches && !TAILQ_EMPTY(&branches))
8668 option_conflict('a', 'b');
8669 if (send_all_tags && !TAILQ_EMPTY(&tags))
8670 option_conflict('T', 't');
8674 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8676 remote_name = argv[0];
8680 cwd = getcwd(NULL, 0);
8682 error = got_error_from_errno("getcwd");
8686 error = got_repo_pack_fds_open(&pack_fds);
8690 if (repo_path == NULL) {
8691 error = got_worktree_open(&worktree, cwd);
8692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8698 strdup(got_worktree_get_repo_path(worktree));
8699 if (repo_path == NULL)
8700 error = got_error_from_errno("strdup");
8704 repo_path = strdup(cwd);
8705 if (repo_path == NULL) {
8706 error = got_error_from_errno("strdup");
8712 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8717 worktree_conf = got_worktree_get_gotconfig(worktree);
8718 if (worktree_conf) {
8719 got_gotconfig_get_remotes(&nremotes, &remotes,
8721 for (i = 0; i < nremotes; i++) {
8722 if (strcmp(remotes[i].name, remote_name) == 0) {
8723 remote = &remotes[i];
8729 if (remote == NULL) {
8730 repo_conf = got_repo_get_gotconfig(repo);
8732 got_gotconfig_get_remotes(&nremotes, &remotes,
8734 for (i = 0; i < nremotes; i++) {
8735 if (strcmp(remotes[i].name, remote_name) == 0) {
8736 remote = &remotes[i];
8742 if (remote == NULL) {
8743 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8744 for (i = 0; i < nremotes; i++) {
8745 if (strcmp(remotes[i].name, remote_name) == 0) {
8746 remote = &remotes[i];
8751 if (remote == NULL) {
8752 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8756 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8757 &repo_name, remote->send_url);
8761 if (strcmp(proto, "git") == 0) {
8763 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8764 "sendfd dns inet unveil", NULL) == -1)
8767 } else if (strcmp(proto, "git+ssh") == 0 ||
8768 strcmp(proto, "ssh") == 0) {
8770 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8771 "sendfd unveil", NULL) == -1)
8774 } else if (strcmp(proto, "http") == 0 ||
8775 strcmp(proto, "git+http") == 0) {
8776 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8779 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8783 error = got_dial_apply_unveil(proto);
8787 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8791 if (send_all_branches) {
8792 error = got_ref_list(&all_branches, repo, "refs/heads",
8793 got_ref_cmp_by_name, NULL);
8796 TAILQ_FOREACH(re, &all_branches, entry) {
8797 const char *branchname = got_ref_get_name(re->ref);
8798 error = got_pathlist_append(&branches,
8804 } else if (nbranches == 0) {
8805 for (i = 0; i < remote->nsend_branches; i++) {
8806 got_pathlist_append(&branches,
8807 remote->send_branches[i], NULL);
8811 if (send_all_tags) {
8812 error = got_ref_list(&all_tags, repo, "refs/tags",
8813 got_ref_cmp_by_name, NULL);
8816 TAILQ_FOREACH(re, &all_tags, entry) {
8817 const char *tagname = got_ref_get_name(re->ref);
8818 error = got_pathlist_append(&tags,
8827 * To prevent accidents only branches in refs/heads/ can be deleted
8828 * with 'got send -d'.
8829 * Deleting anything else requires local repository access or Git.
8831 TAILQ_FOREACH(pe, &delete_args, entry) {
8832 const char *branchname = pe->path;
8834 struct got_pathlist_entry *new;
8835 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8836 s = strdup(branchname);
8838 error = got_error_from_errno("strdup");
8842 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8843 error = got_error_from_errno("asprintf");
8847 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8848 if (error || new == NULL /* duplicate */)
8855 if (nbranches == 0 && ndelete_branches == 0) {
8856 struct got_reference *head_ref;
8858 error = got_ref_open(&head_ref, repo,
8859 got_worktree_get_head_ref_name(worktree), 0);
8861 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8864 if (got_ref_is_symbolic(head_ref)) {
8865 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8866 got_ref_close(head_ref);
8871 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8879 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8880 port ? ":" : "", port ? port : "");
8882 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8883 server_path, verbosity);
8887 memset(&spa, 0, sizeof(spa));
8888 spa.last_scaled_packsize[0] = '\0';
8889 spa.last_p_deltify = -1;
8890 spa.last_p_written = -1;
8891 spa.verbosity = verbosity;
8892 spa.delete_branches = &delete_branches;
8893 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8894 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8895 check_cancelled, NULL);
8896 if (spa.printed_something)
8900 if (!spa.sent_something && verbosity >= 0)
8901 printf("Already up-to-date\n");
8904 if (kill(sendpid, SIGTERM) == -1)
8905 error = got_error_from_errno("kill");
8906 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8907 error = got_error_from_errno("waitpid");
8909 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8910 error = got_error_from_errno("close");
8912 const struct got_error *close_err = got_repo_close(repo);
8917 got_worktree_close(worktree);
8919 const struct got_error *pack_err =
8920 got_repo_pack_fds_close(pack_fds);
8927 got_pathlist_free(&branches);
8928 got_pathlist_free(&tags);
8929 got_ref_list_free(&all_branches);
8930 got_ref_list_free(&all_tags);
8931 got_pathlist_free(&delete_args);
8932 TAILQ_FOREACH(pe, &delete_branches, entry)
8933 free((char *)pe->path);
8934 got_pathlist_free(&delete_branches);
8946 usage_cherrypick(void)
8948 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8952 static const struct got_error *
8953 cmd_cherrypick(int argc, char *argv[])
8955 const struct got_error *error = NULL;
8956 struct got_worktree *worktree = NULL;
8957 struct got_repository *repo = NULL;
8958 char *cwd = NULL, *commit_id_str = NULL;
8959 struct got_object_id *commit_id = NULL;
8960 struct got_commit_object *commit = NULL;
8961 struct got_object_qid *pid;
8963 struct got_update_progress_arg upa;
8964 int *pack_fds = NULL;
8966 while ((ch = getopt(argc, argv, "")) != -1) {
8978 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8979 "unveil", NULL) == -1)
8985 cwd = getcwd(NULL, 0);
8987 error = got_error_from_errno("getcwd");
8991 error = got_repo_pack_fds_open(&pack_fds);
8995 error = got_worktree_open(&worktree, cwd);
8997 if (error->code == GOT_ERR_NOT_WORKTREE)
8998 error = wrap_not_worktree_error(error, "cherrypick",
9003 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9008 error = apply_unveil(got_repo_get_path(repo), 0,
9009 got_worktree_get_root_path(worktree));
9013 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9014 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9017 error = got_object_id_str(&commit_id_str, commit_id);
9021 error = got_object_open_as_commit(&commit, repo, commit_id);
9024 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9025 memset(&upa, 0, sizeof(upa));
9026 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9027 commit_id, repo, update_progress, &upa, check_cancelled,
9032 if (upa.did_something)
9033 printf("Merged commit %s\n", commit_id_str);
9034 print_merge_progress_stats(&upa);
9037 got_object_commit_close(commit);
9038 free(commit_id_str);
9040 got_worktree_close(worktree);
9042 const struct got_error *close_err = got_repo_close(repo);
9047 const struct got_error *pack_err =
9048 got_repo_pack_fds_close(pack_fds);
9060 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9064 static const struct got_error *
9065 cmd_backout(int argc, char *argv[])
9067 const struct got_error *error = NULL;
9068 struct got_worktree *worktree = NULL;
9069 struct got_repository *repo = NULL;
9070 char *cwd = NULL, *commit_id_str = NULL;
9071 struct got_object_id *commit_id = NULL;
9072 struct got_commit_object *commit = NULL;
9073 struct got_object_qid *pid;
9075 struct got_update_progress_arg upa;
9076 int *pack_fds = NULL;
9078 while ((ch = getopt(argc, argv, "")) != -1) {
9090 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9091 "unveil", NULL) == -1)
9097 cwd = getcwd(NULL, 0);
9099 error = got_error_from_errno("getcwd");
9103 error = got_repo_pack_fds_open(&pack_fds);
9107 error = got_worktree_open(&worktree, cwd);
9109 if (error->code == GOT_ERR_NOT_WORKTREE)
9110 error = wrap_not_worktree_error(error, "backout", cwd);
9114 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9119 error = apply_unveil(got_repo_get_path(repo), 0,
9120 got_worktree_get_root_path(worktree));
9124 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9125 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9128 error = got_object_id_str(&commit_id_str, commit_id);
9132 error = got_object_open_as_commit(&commit, repo, commit_id);
9135 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9137 error = got_error(GOT_ERR_ROOT_COMMIT);
9141 memset(&upa, 0, sizeof(upa));
9142 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9143 repo, update_progress, &upa, check_cancelled, NULL);
9147 if (upa.did_something)
9148 printf("Backed out commit %s\n", commit_id_str);
9149 print_merge_progress_stats(&upa);
9152 got_object_commit_close(commit);
9153 free(commit_id_str);
9155 got_worktree_close(worktree);
9157 const struct got_error *close_err = got_repo_close(repo);
9162 const struct got_error *pack_err =
9163 got_repo_pack_fds_close(pack_fds);
9174 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9180 trim_logmsg(char *logmsg, int limit)
9185 len = strlen(logmsg);
9189 nl = strchr(logmsg, '\n');
9194 static const struct got_error *
9195 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9197 const struct got_error *err;
9198 char *logmsg0 = NULL;
9201 err = got_object_commit_get_logmsg(&logmsg0, commit);
9206 while (isspace((unsigned char)s[0]))
9209 *logmsg = strdup(s);
9210 if (*logmsg == NULL) {
9211 err = got_error_from_errno("strdup");
9215 trim_logmsg(*logmsg, limit);
9221 static const struct got_error *
9222 show_rebase_merge_conflict(struct got_object_id *id,
9223 struct got_repository *repo)
9225 const struct got_error *err;
9226 struct got_commit_object *commit = NULL;
9227 char *id_str = NULL, *logmsg = NULL;
9229 err = got_object_open_as_commit(&commit, repo, id);
9233 err = got_object_id_str(&id_str, id);
9239 err = get_short_logmsg(&logmsg, 42, commit);
9243 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9246 got_object_commit_close(commit);
9251 static const struct got_error *
9252 show_rebase_progress(struct got_commit_object *commit,
9253 struct got_object_id *old_id, struct got_object_id *new_id)
9255 const struct got_error *err;
9256 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9258 err = got_object_id_str(&old_id_str, old_id);
9263 err = got_object_id_str(&new_id_str, new_id);
9268 old_id_str[12] = '\0';
9270 new_id_str[12] = '\0';
9272 err = get_short_logmsg(&logmsg, 42, commit);
9276 printf("%s -> %s: %s\n", old_id_str,
9277 new_id_str ? new_id_str : "no-op change", logmsg);
9285 static const struct got_error *
9286 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9287 struct got_reference *branch, struct got_reference *new_base_branch,
9288 struct got_reference *tmp_branch, struct got_repository *repo,
9291 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9292 return got_worktree_rebase_complete(worktree, fileindex,
9293 new_base_branch, tmp_branch, branch, repo, create_backup);
9296 static const struct got_error *
9297 rebase_commit(struct got_pathlist_head *merged_paths,
9298 struct got_worktree *worktree, struct got_fileindex *fileindex,
9299 struct got_reference *tmp_branch,
9300 struct got_object_id *commit_id, struct got_repository *repo)
9302 const struct got_error *error;
9303 struct got_commit_object *commit;
9304 struct got_object_id *new_commit_id;
9306 error = got_object_open_as_commit(&commit, repo, commit_id);
9310 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9311 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9313 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9315 error = show_rebase_progress(commit, commit_id, NULL);
9317 error = show_rebase_progress(commit, commit_id, new_commit_id);
9318 free(new_commit_id);
9321 got_object_commit_close(commit);
9325 struct check_path_prefix_arg {
9326 const char *path_prefix;
9331 static const struct got_error *
9332 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9333 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9334 struct got_object_id *id1, struct got_object_id *id2,
9335 const char *path1, const char *path2,
9336 mode_t mode1, mode_t mode2, struct got_repository *repo)
9338 struct check_path_prefix_arg *a = arg;
9340 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9341 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9342 return got_error(a->errcode);
9347 static const struct got_error *
9348 check_path_prefix(struct got_object_id *parent_id,
9349 struct got_object_id *commit_id, const char *path_prefix,
9350 int errcode, struct got_repository *repo)
9352 const struct got_error *err;
9353 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9354 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9355 struct check_path_prefix_arg cpp_arg;
9357 if (got_path_is_root_dir(path_prefix))
9360 err = got_object_open_as_commit(&commit, repo, commit_id);
9364 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9368 err = got_object_open_as_tree(&tree1, repo,
9369 got_object_commit_get_tree_id(parent_commit));
9373 err = got_object_open_as_tree(&tree2, repo,
9374 got_object_commit_get_tree_id(commit));
9378 cpp_arg.path_prefix = path_prefix;
9379 while (cpp_arg.path_prefix[0] == '/')
9380 cpp_arg.path_prefix++;
9381 cpp_arg.len = strlen(cpp_arg.path_prefix);
9382 cpp_arg.errcode = errcode;
9383 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
9384 check_path_prefix_in_diff, &cpp_arg, 0);
9387 got_object_tree_close(tree1);
9389 got_object_tree_close(tree2);
9391 got_object_commit_close(commit);
9393 got_object_commit_close(parent_commit);
9397 static const struct got_error *
9398 collect_commits(struct got_object_id_queue *commits,
9399 struct got_object_id *initial_commit_id,
9400 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9401 const char *path_prefix, int path_prefix_errcode,
9402 struct got_repository *repo)
9404 const struct got_error *err = NULL;
9405 struct got_commit_graph *graph = NULL;
9406 struct got_object_id *parent_id = NULL;
9407 struct got_object_qid *qid;
9408 struct got_object_id *commit_id = initial_commit_id;
9410 err = got_commit_graph_open(&graph, "/", 1);
9414 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9415 check_cancelled, NULL);
9418 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9419 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9420 check_cancelled, NULL);
9422 if (err->code == GOT_ERR_ITER_COMPLETED) {
9423 err = got_error_msg(GOT_ERR_ANCESTRY,
9424 "ran out of commits to rebase before "
9425 "youngest common ancestor commit has "
9430 err = check_path_prefix(parent_id, commit_id,
9431 path_prefix, path_prefix_errcode, repo);
9435 err = got_object_qid_alloc(&qid, commit_id);
9438 STAILQ_INSERT_HEAD(commits, qid, entry);
9439 commit_id = parent_id;
9443 got_commit_graph_close(graph);
9447 static const struct got_error *
9448 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9450 const struct got_error *err = NULL;
9451 time_t committer_time;
9453 char datebuf[11]; /* YYYY-MM-DD + NUL */
9454 char *author0 = NULL, *author, *smallerthan;
9455 char *logmsg0 = NULL, *logmsg, *newline;
9457 committer_time = got_object_commit_get_committer_time(commit);
9458 if (gmtime_r(&committer_time, &tm) == NULL)
9459 return got_error_from_errno("gmtime_r");
9460 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9461 return got_error(GOT_ERR_NO_SPACE);
9463 author0 = strdup(got_object_commit_get_author(commit));
9464 if (author0 == NULL)
9465 return got_error_from_errno("strdup");
9467 smallerthan = strchr(author, '<');
9468 if (smallerthan && smallerthan[1] != '\0')
9469 author = smallerthan + 1;
9470 author[strcspn(author, "@>")] = '\0';
9472 err = got_object_commit_get_logmsg(&logmsg0, commit);
9476 while (*logmsg == '\n')
9478 newline = strchr(logmsg, '\n');
9482 if (asprintf(brief_str, "%s %s %s",
9483 datebuf, author, logmsg) == -1)
9484 err = got_error_from_errno("asprintf");
9491 static const struct got_error *
9492 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9493 struct got_repository *repo)
9495 const struct got_error *err;
9498 err = got_object_id_str(&id_str, id);
9502 err = got_ref_delete(ref, repo);
9506 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9512 static const struct got_error *
9513 print_backup_ref(const char *branch_name, const char *new_id_str,
9514 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9515 struct got_reflist_object_id_map *refs_idmap,
9516 struct got_repository *repo)
9518 const struct got_error *err = NULL;
9519 struct got_reflist_head *refs;
9520 char *refs_str = NULL;
9521 struct got_object_id *new_commit_id = NULL;
9522 struct got_commit_object *new_commit = NULL;
9523 char *new_commit_brief_str = NULL;
9524 struct got_object_id *yca_id = NULL;
9525 struct got_commit_object *yca_commit = NULL;
9526 char *yca_id_str = NULL, *yca_brief_str = NULL;
9527 char *custom_refs_str;
9529 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9530 return got_error_from_errno("asprintf");
9532 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9533 0, 0, refs_idmap, custom_refs_str);
9537 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9541 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9543 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9548 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9552 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9556 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9557 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9561 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9562 refs_str ? " (" : "", refs_str ? refs_str : "",
9563 refs_str ? ")" : "", new_commit_brief_str);
9564 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9565 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9569 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9573 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9577 err = got_object_id_str(&yca_id_str, yca_id);
9581 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9583 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9587 printf("history forked at %s%s%s%s\n %s\n",
9589 refs_str ? " (" : "", refs_str ? refs_str : "",
9590 refs_str ? ")" : "", yca_brief_str);
9593 free(custom_refs_str);
9594 free(new_commit_id);
9598 free(yca_brief_str);
9600 got_object_commit_close(new_commit);
9602 got_object_commit_close(yca_commit);
9607 static const struct got_error *
9608 process_backup_refs(const char *backup_ref_prefix,
9609 const char *wanted_branch_name,
9610 int delete, struct got_repository *repo)
9612 const struct got_error *err;
9613 struct got_reflist_head refs, backup_refs;
9614 struct got_reflist_entry *re;
9615 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9616 struct got_object_id *old_commit_id = NULL;
9617 char *branch_name = NULL;
9618 struct got_commit_object *old_commit = NULL;
9619 struct got_reflist_object_id_map *refs_idmap = NULL;
9620 int wanted_branch_found = 0;
9623 TAILQ_INIT(&backup_refs);
9625 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9629 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9633 if (wanted_branch_name) {
9634 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9635 wanted_branch_name += 11;
9638 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9639 got_ref_cmp_by_commit_timestamp_descending, repo);
9643 TAILQ_FOREACH(re, &backup_refs, entry) {
9644 const char *refname = got_ref_get_name(re->ref);
9647 err = check_cancelled(NULL);
9651 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9655 err = got_object_open_as_commit(&old_commit, repo,
9660 if (strncmp(backup_ref_prefix, refname,
9661 backup_ref_prefix_len) == 0)
9662 refname += backup_ref_prefix_len;
9664 while (refname[0] == '/')
9667 branch_name = strdup(refname);
9668 if (branch_name == NULL) {
9669 err = got_error_from_errno("strdup");
9672 slash = strrchr(branch_name, '/');
9675 refname += strlen(branch_name) + 1;
9678 if (wanted_branch_name == NULL ||
9679 strcmp(wanted_branch_name, branch_name) == 0) {
9680 wanted_branch_found = 1;
9682 err = delete_backup_ref(re->ref,
9683 old_commit_id, repo);
9685 err = print_backup_ref(branch_name, refname,
9686 old_commit_id, old_commit, refs_idmap,
9693 free(old_commit_id);
9694 old_commit_id = NULL;
9697 got_object_commit_close(old_commit);
9701 if (wanted_branch_name && !wanted_branch_found) {
9702 err = got_error_fmt(GOT_ERR_NOT_REF,
9703 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9707 got_reflist_object_id_map_free(refs_idmap);
9708 got_ref_list_free(&refs);
9709 got_ref_list_free(&backup_refs);
9710 free(old_commit_id);
9713 got_object_commit_close(old_commit);
9717 static const struct got_error *
9718 abort_progress(void *arg, unsigned char status, const char *path)
9721 * Unversioned files should not clutter progress output when
9722 * an operation is aborted.
9724 if (status == GOT_STATUS_UNVERSIONED)
9727 return update_progress(arg, status, path);
9730 static const struct got_error *
9731 cmd_rebase(int argc, char *argv[])
9733 const struct got_error *error = NULL;
9734 struct got_worktree *worktree = NULL;
9735 struct got_repository *repo = NULL;
9736 struct got_fileindex *fileindex = NULL;
9738 struct got_reference *branch = NULL;
9739 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9740 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9741 struct got_object_id *resume_commit_id = NULL;
9742 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9743 struct got_commit_object *commit = NULL;
9744 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9745 int histedit_in_progress = 0, merge_in_progress = 0;
9746 int create_backup = 1, list_backups = 0, delete_backups = 0;
9747 struct got_object_id_queue commits;
9748 struct got_pathlist_head merged_paths;
9749 const struct got_object_id_queue *parent_ids;
9750 struct got_object_qid *qid, *pid;
9751 struct got_update_progress_arg upa;
9752 int *pack_fds = NULL;
9754 STAILQ_INIT(&commits);
9755 TAILQ_INIT(&merged_paths);
9756 memset(&upa, 0, sizeof(upa));
9758 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9764 continue_rebase = 1;
9782 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9783 "unveil", NULL) == -1)
9788 option_conflict('l', 'a');
9789 if (continue_rebase)
9790 option_conflict('l', 'c');
9792 option_conflict('l', 'X');
9793 if (argc != 0 && argc != 1)
9795 } else if (delete_backups) {
9797 option_conflict('X', 'a');
9798 if (continue_rebase)
9799 option_conflict('X', 'c');
9801 option_conflict('l', 'X');
9802 if (argc != 0 && argc != 1)
9805 if (abort_rebase && continue_rebase)
9807 else if (abort_rebase || continue_rebase) {
9810 } else if (argc != 1)
9814 cwd = getcwd(NULL, 0);
9816 error = got_error_from_errno("getcwd");
9820 error = got_repo_pack_fds_open(&pack_fds);
9824 error = got_worktree_open(&worktree, cwd);
9826 if (list_backups || delete_backups) {
9827 if (error->code != GOT_ERR_NOT_WORKTREE)
9830 if (error->code == GOT_ERR_NOT_WORKTREE)
9831 error = wrap_not_worktree_error(error,
9837 error = got_repo_open(&repo,
9838 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9843 error = apply_unveil(got_repo_get_path(repo), 0,
9844 worktree ? got_worktree_get_root_path(worktree) : NULL);
9848 if (list_backups || delete_backups) {
9849 error = process_backup_refs(
9850 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9851 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9852 goto done; /* nothing else to do */
9855 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9859 if (histedit_in_progress) {
9860 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9864 error = got_worktree_merge_in_progress(&merge_in_progress,
9868 if (merge_in_progress) {
9869 error = got_error(GOT_ERR_MERGE_BUSY);
9873 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9878 if (!rebase_in_progress) {
9879 error = got_error(GOT_ERR_NOT_REBASING);
9882 error = got_worktree_rebase_continue(&resume_commit_id,
9883 &new_base_branch, &tmp_branch, &branch, &fileindex,
9887 printf("Switching work tree to %s\n",
9888 got_ref_get_symref_target(new_base_branch));
9889 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9890 new_base_branch, abort_progress, &upa);
9893 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9894 print_merge_progress_stats(&upa);
9895 goto done; /* nothing else to do */
9898 if (continue_rebase) {
9899 if (!rebase_in_progress) {
9900 error = got_error(GOT_ERR_NOT_REBASING);
9903 error = got_worktree_rebase_continue(&resume_commit_id,
9904 &new_base_branch, &tmp_branch, &branch, &fileindex,
9909 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9910 resume_commit_id, repo);
9914 yca_id = got_object_id_dup(resume_commit_id);
9915 if (yca_id == NULL) {
9916 error = got_error_from_errno("got_object_id_dup");
9920 error = got_ref_open(&branch, repo, argv[0], 0);
9925 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9929 if (!continue_rebase) {
9930 struct got_object_id *base_commit_id;
9932 base_commit_id = got_worktree_get_base_commit_id(worktree);
9933 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9934 base_commit_id, branch_head_commit_id, 1, repo,
9935 check_cancelled, NULL);
9938 if (yca_id == NULL) {
9939 error = got_error_msg(GOT_ERR_ANCESTRY,
9940 "specified branch shares no common ancestry "
9941 "with work tree's branch");
9945 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9947 if (error->code != GOT_ERR_ANCESTRY)
9951 struct got_pathlist_head paths;
9952 printf("%s is already based on %s\n",
9953 got_ref_get_name(branch),
9954 got_worktree_get_head_ref_name(worktree));
9955 error = switch_head_ref(branch, branch_head_commit_id,
9959 error = got_worktree_set_base_commit_id(worktree, repo,
9960 branch_head_commit_id);
9964 error = got_pathlist_append(&paths, "", NULL);
9967 error = got_worktree_checkout_files(worktree,
9968 &paths, repo, update_progress, &upa,
9969 check_cancelled, NULL);
9970 got_pathlist_free(&paths);
9973 if (upa.did_something) {
9975 error = got_object_id_str(&id_str,
9976 branch_head_commit_id);
9979 printf("Updated to %s: %s\n",
9980 got_worktree_get_head_ref_name(worktree),
9984 printf("Already up-to-date\n");
9985 print_update_progress_stats(&upa);
9990 commit_id = branch_head_commit_id;
9991 error = got_object_open_as_commit(&commit, repo, commit_id);
9995 parent_ids = got_object_commit_get_parent_ids(commit);
9996 pid = STAILQ_FIRST(parent_ids);
9998 error = got_error(GOT_ERR_EMPTY_REBASE);
10001 error = collect_commits(&commits, commit_id, &pid->id,
10002 yca_id, got_worktree_get_path_prefix(worktree),
10003 GOT_ERR_REBASE_PATH, repo);
10004 got_object_commit_close(commit);
10009 if (!continue_rebase) {
10010 error = got_worktree_rebase_prepare(&new_base_branch,
10011 &tmp_branch, &fileindex, worktree, branch, repo);
10016 if (STAILQ_EMPTY(&commits)) {
10017 if (continue_rebase) {
10018 error = rebase_complete(worktree, fileindex,
10019 branch, new_base_branch, tmp_branch, repo,
10023 /* Fast-forward the reference of the branch. */
10024 struct got_object_id *new_head_commit_id;
10026 error = got_ref_resolve(&new_head_commit_id, repo,
10030 error = got_object_id_str(&id_str, new_head_commit_id);
10031 printf("Forwarding %s to commit %s\n",
10032 got_ref_get_name(branch), id_str);
10034 error = got_ref_change_ref(branch,
10035 new_head_commit_id);
10038 /* No backup needed since objects did not change. */
10044 STAILQ_FOREACH(qid, &commits, entry) {
10046 commit_id = &qid->id;
10047 parent_id = pid ? &pid->id : yca_id;
10050 memset(&upa, 0, sizeof(upa));
10051 error = got_worktree_rebase_merge_files(&merged_paths,
10052 worktree, fileindex, parent_id, commit_id, repo,
10053 update_progress, &upa, check_cancelled, NULL);
10057 print_merge_progress_stats(&upa);
10058 if (upa.conflicts > 0 || upa.missing > 0 ||
10059 upa.not_deleted > 0 || upa.unversioned > 0) {
10060 if (upa.conflicts > 0) {
10061 error = show_rebase_merge_conflict(&qid->id,
10066 got_worktree_rebase_pathlist_free(&merged_paths);
10070 error = rebase_commit(&merged_paths, worktree, fileindex,
10071 tmp_branch, commit_id, repo);
10072 got_worktree_rebase_pathlist_free(&merged_paths);
10077 if (upa.conflicts > 0 || upa.missing > 0 ||
10078 upa.not_deleted > 0 || upa.unversioned > 0) {
10079 error = got_worktree_rebase_postpone(worktree, fileindex);
10082 if (upa.conflicts > 0 && upa.missing == 0 &&
10083 upa.not_deleted == 0 && upa.unversioned == 0) {
10084 error = got_error_msg(GOT_ERR_CONFLICTS,
10085 "conflicts must be resolved before rebasing "
10087 } else if (upa.conflicts > 0) {
10088 error = got_error_msg(GOT_ERR_CONFLICTS,
10089 "conflicts must be resolved before rebasing "
10090 "can continue; changes destined for some "
10091 "files were not yet merged and should be "
10092 "merged manually if required before the "
10093 "rebase operation is continued");
10095 error = got_error_msg(GOT_ERR_CONFLICTS,
10096 "changes destined for some files were not "
10097 "yet merged and should be merged manually "
10098 "if required before the rebase operation "
10102 error = rebase_complete(worktree, fileindex, branch,
10103 new_base_branch, tmp_branch, repo, create_backup);
10105 got_object_id_queue_free(&commits);
10106 free(branch_head_commit_id);
10107 free(resume_commit_id);
10110 got_object_commit_close(commit);
10112 got_ref_close(branch);
10113 if (new_base_branch)
10114 got_ref_close(new_base_branch);
10116 got_ref_close(tmp_branch);
10118 got_worktree_close(worktree);
10120 const struct got_error *close_err = got_repo_close(repo);
10125 const struct got_error *pack_err =
10126 got_repo_pack_fds_close(pack_fds);
10135 usage_histedit(void)
10137 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10138 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10143 #define GOT_HISTEDIT_PICK 'p'
10144 #define GOT_HISTEDIT_EDIT 'e'
10145 #define GOT_HISTEDIT_FOLD 'f'
10146 #define GOT_HISTEDIT_DROP 'd'
10147 #define GOT_HISTEDIT_MESG 'm'
10149 static const struct got_histedit_cmd {
10150 unsigned char code;
10153 } got_histedit_cmds[] = {
10154 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10155 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10156 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10158 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10159 { GOT_HISTEDIT_MESG, "mesg",
10160 "single-line log message for commit above (open editor if empty)" },
10163 struct got_histedit_list_entry {
10164 TAILQ_ENTRY(got_histedit_list_entry) entry;
10165 struct got_object_id *commit_id;
10166 const struct got_histedit_cmd *cmd;
10169 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10171 static const struct got_error *
10172 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10173 FILE *f, struct got_repository *repo)
10175 const struct got_error *err = NULL;
10176 char *logmsg = NULL, *id_str = NULL;
10177 struct got_commit_object *commit = NULL;
10180 err = got_object_open_as_commit(&commit, repo, commit_id);
10184 err = get_short_logmsg(&logmsg, 34, commit);
10188 err = got_object_id_str(&id_str, commit_id);
10192 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10194 err = got_ferror(f, GOT_ERR_IO);
10197 got_object_commit_close(commit);
10203 static const struct got_error *
10204 histedit_write_commit_list(struct got_object_id_queue *commits,
10205 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10206 struct got_repository *repo)
10208 const struct got_error *err = NULL;
10209 struct got_object_qid *qid;
10210 const char *histedit_cmd = NULL;
10212 if (STAILQ_EMPTY(commits))
10213 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10215 STAILQ_FOREACH(qid, commits, entry) {
10216 histedit_cmd = got_histedit_cmds[0].name;
10218 histedit_cmd = "edit";
10219 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10220 histedit_cmd = "fold";
10221 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10224 if (edit_logmsg_only) {
10225 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10227 err = got_ferror(f, GOT_ERR_IO);
10236 static const struct got_error *
10237 write_cmd_list(FILE *f, const char *branch_name,
10238 struct got_object_id_queue *commits)
10240 const struct got_error *err = NULL;
10244 struct got_object_qid *qid;
10246 qid = STAILQ_FIRST(commits);
10247 err = got_object_id_str(&id_str, &qid->id);
10252 "# Editing the history of branch '%s' starting at\n"
10254 "# Commits will be processed in order from top to "
10255 "bottom of this file.\n", branch_name, id_str);
10257 err = got_ferror(f, GOT_ERR_IO);
10261 n = fprintf(f, "# Available histedit commands:\n");
10263 err = got_ferror(f, GOT_ERR_IO);
10267 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10268 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10269 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10272 err = got_ferror(f, GOT_ERR_IO);
10281 static const struct got_error *
10282 histedit_syntax_error(int lineno)
10284 static char msg[42];
10287 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10289 if (ret == -1 || ret >= sizeof(msg))
10290 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10292 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10295 static const struct got_error *
10296 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10297 char *logmsg, struct got_repository *repo)
10299 const struct got_error *err;
10300 struct got_commit_object *folded_commit = NULL;
10301 char *id_str, *folded_logmsg = NULL;
10303 err = got_object_id_str(&id_str, hle->commit_id);
10307 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10311 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10314 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10315 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10316 folded_logmsg) == -1) {
10317 err = got_error_from_errno("asprintf");
10321 got_object_commit_close(folded_commit);
10323 free(folded_logmsg);
10327 static struct got_histedit_list_entry *
10328 get_folded_commits(struct got_histedit_list_entry *hle)
10330 struct got_histedit_list_entry *prev, *folded = NULL;
10332 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10333 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10334 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10335 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10337 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10343 static const struct got_error *
10344 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10345 struct got_repository *repo)
10347 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10348 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10349 const struct got_error *err = NULL;
10350 struct got_commit_object *commit = NULL;
10353 struct got_histedit_list_entry *folded = NULL;
10355 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10359 folded = get_folded_commits(hle);
10361 while (folded != hle) {
10362 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10363 folded = TAILQ_NEXT(folded, entry);
10366 err = append_folded_commit_msg(&new_msg, folded,
10372 folded = TAILQ_NEXT(folded, entry);
10376 err = got_object_id_str(&id_str, hle->commit_id);
10379 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10382 logmsg_len = asprintf(&new_msg,
10383 "%s\n# original log message of commit %s: %s",
10384 logmsg ? logmsg : "", id_str, orig_logmsg);
10385 if (logmsg_len == -1) {
10386 err = got_error_from_errno("asprintf");
10392 err = got_object_id_str(&id_str, hle->commit_id);
10396 err = got_opentemp_named_fd(&logmsg_path, &fd,
10397 GOT_TMPDIR_STR "/got-logmsg");
10401 write(fd, logmsg, logmsg_len);
10404 err = get_editor(&editor);
10408 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10411 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10414 hle->logmsg = strdup(new_msg);
10415 if (hle->logmsg == NULL)
10416 err = got_error_from_errno("strdup");
10419 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10420 err = got_error_from_errno2("unlink", logmsg_path);
10426 got_object_commit_close(commit);
10430 static const struct got_error *
10431 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10432 FILE *f, struct got_repository *repo)
10434 const struct got_error *err = NULL;
10435 char *line = NULL, *p, *end;
10439 const struct got_histedit_cmd *cmd;
10440 struct got_object_id *commit_id = NULL;
10441 struct got_histedit_list_entry *hle = NULL;
10444 len = getline(&line, &size, f);
10446 const struct got_error *getline_err;
10449 getline_err = got_error_from_errno("getline");
10450 err = got_ferror(f, getline_err->code);
10455 while (isspace((unsigned char)p[0]))
10457 if (p[0] == '#' || p[0] == '\0') {
10463 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10464 cmd = &got_histedit_cmds[i];
10465 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10466 isspace((unsigned char)p[strlen(cmd->name)])) {
10467 p += strlen(cmd->name);
10470 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10475 if (i == nitems(got_histedit_cmds)) {
10476 err = histedit_syntax_error(lineno);
10479 while (isspace((unsigned char)p[0]))
10481 if (cmd->code == GOT_HISTEDIT_MESG) {
10482 if (hle == NULL || hle->logmsg != NULL) {
10483 err = got_error(GOT_ERR_HISTEDIT_CMD);
10486 if (p[0] == '\0') {
10487 err = histedit_edit_logmsg(hle, repo);
10491 hle->logmsg = strdup(p);
10492 if (hle->logmsg == NULL) {
10493 err = got_error_from_errno("strdup");
10502 while (end[0] && !isspace((unsigned char)end[0]))
10506 err = got_object_resolve_id_str(&commit_id, repo, p);
10508 /* override error code */
10509 err = histedit_syntax_error(lineno);
10513 hle = malloc(sizeof(*hle));
10515 err = got_error_from_errno("malloc");
10519 hle->commit_id = commit_id;
10520 hle->logmsg = NULL;
10524 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10532 static const struct got_error *
10533 histedit_check_script(struct got_histedit_list *histedit_cmds,
10534 struct got_object_id_queue *commits, struct got_repository *repo)
10536 const struct got_error *err = NULL;
10537 struct got_object_qid *qid;
10538 struct got_histedit_list_entry *hle;
10539 static char msg[92];
10542 if (TAILQ_EMPTY(histedit_cmds))
10543 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10544 "histedit script contains no commands");
10545 if (STAILQ_EMPTY(commits))
10546 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10548 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10549 struct got_histedit_list_entry *hle2;
10550 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10553 if (got_object_id_cmp(hle->commit_id,
10554 hle2->commit_id) != 0)
10556 err = got_object_id_str(&id_str, hle->commit_id);
10559 snprintf(msg, sizeof(msg), "commit %s is listed "
10560 "more than once in histedit script", id_str);
10562 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10566 STAILQ_FOREACH(qid, commits, entry) {
10567 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10568 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10572 err = got_object_id_str(&id_str, &qid->id);
10575 snprintf(msg, sizeof(msg),
10576 "commit %s missing from histedit script", id_str);
10578 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10582 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10583 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10584 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10585 "last commit in histedit script cannot be folded");
10590 static const struct got_error *
10591 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10592 const char *path, struct got_object_id_queue *commits,
10593 struct got_repository *repo)
10595 const struct got_error *err = NULL;
10599 err = get_editor(&editor);
10603 if (spawn_editor(editor, path) == -1) {
10604 err = got_error_from_errno("failed spawning editor");
10608 f = fopen(path, "re");
10610 err = got_error_from_errno("fopen");
10613 err = histedit_parse_list(histedit_cmds, f, repo);
10617 err = histedit_check_script(histedit_cmds, commits, repo);
10619 if (f && fclose(f) == EOF && err == NULL)
10620 err = got_error_from_errno("fclose");
10625 static const struct got_error *
10626 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10627 struct got_object_id_queue *, const char *, const char *,
10628 struct got_repository *);
10630 static const struct got_error *
10631 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10632 struct got_object_id_queue *commits, const char *branch_name,
10633 int edit_logmsg_only, int fold_only, int edit_only,
10634 struct got_repository *repo)
10636 const struct got_error *err;
10640 err = got_opentemp_named(&path, &f, "got-histedit");
10644 err = write_cmd_list(f, branch_name, commits);
10648 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10649 fold_only, edit_only, repo);
10653 if (edit_logmsg_only || fold_only || edit_only) {
10655 err = histedit_parse_list(histedit_cmds, f, repo);
10657 if (fclose(f) == EOF) {
10658 err = got_error_from_errno("fclose");
10662 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10664 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10665 err->code != GOT_ERR_HISTEDIT_CMD)
10667 err = histedit_edit_list_retry(histedit_cmds, err,
10668 commits, path, branch_name, repo);
10672 if (f && fclose(f) == EOF && err == NULL)
10673 err = got_error_from_errno("fclose");
10674 if (path && unlink(path) != 0 && err == NULL)
10675 err = got_error_from_errno2("unlink", path);
10680 static const struct got_error *
10681 histedit_save_list(struct got_histedit_list *histedit_cmds,
10682 struct got_worktree *worktree, struct got_repository *repo)
10684 const struct got_error *err = NULL;
10687 struct got_histedit_list_entry *hle;
10688 struct got_commit_object *commit = NULL;
10690 err = got_worktree_get_histedit_script_path(&path, worktree);
10694 f = fopen(path, "we");
10696 err = got_error_from_errno2("fopen", path);
10699 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10700 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10706 int n = fprintf(f, "%c %s\n",
10707 GOT_HISTEDIT_MESG, hle->logmsg);
10709 err = got_ferror(f, GOT_ERR_IO);
10715 if (f && fclose(f) == EOF && err == NULL)
10716 err = got_error_from_errno("fclose");
10719 got_object_commit_close(commit);
10724 histedit_free_list(struct got_histedit_list *histedit_cmds)
10726 struct got_histedit_list_entry *hle;
10728 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10729 TAILQ_REMOVE(histedit_cmds, hle, entry);
10734 static const struct got_error *
10735 histedit_load_list(struct got_histedit_list *histedit_cmds,
10736 const char *path, struct got_repository *repo)
10738 const struct got_error *err = NULL;
10741 f = fopen(path, "re");
10743 err = got_error_from_errno2("fopen", path);
10747 err = histedit_parse_list(histedit_cmds, f, repo);
10749 if (f && fclose(f) == EOF && err == NULL)
10750 err = got_error_from_errno("fclose");
10754 static const struct got_error *
10755 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10756 const struct got_error *edit_err, struct got_object_id_queue *commits,
10757 const char *path, const char *branch_name, struct got_repository *repo)
10759 const struct got_error *err = NULL, *prev_err = edit_err;
10762 while (resp != 'c' && resp != 'r' && resp != 'a') {
10763 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10764 "or (a)bort: ", getprogname(), prev_err->msg);
10769 histedit_free_list(histedit_cmds);
10770 err = histedit_run_editor(histedit_cmds, path, commits,
10773 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10774 err->code != GOT_ERR_HISTEDIT_CMD)
10781 } else if (resp == 'r') {
10782 histedit_free_list(histedit_cmds);
10783 err = histedit_edit_script(histedit_cmds,
10784 commits, branch_name, 0, 0, 0, repo);
10786 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10787 err->code != GOT_ERR_HISTEDIT_CMD)
10794 } else if (resp == 'a') {
10795 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10798 printf("invalid response '%c'\n", resp);
10804 static const struct got_error *
10805 histedit_complete(struct got_worktree *worktree,
10806 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10807 struct got_reference *branch, struct got_repository *repo)
10809 printf("Switching work tree to %s\n",
10810 got_ref_get_symref_target(branch));
10811 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10815 static const struct got_error *
10816 show_histedit_progress(struct got_commit_object *commit,
10817 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10819 const struct got_error *err;
10820 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10822 err = got_object_id_str(&old_id_str, hle->commit_id);
10827 err = got_object_id_str(&new_id_str, new_id);
10832 old_id_str[12] = '\0';
10834 new_id_str[12] = '\0';
10837 logmsg = strdup(hle->logmsg);
10838 if (logmsg == NULL) {
10839 err = got_error_from_errno("strdup");
10842 trim_logmsg(logmsg, 42);
10844 err = get_short_logmsg(&logmsg, 42, commit);
10849 switch (hle->cmd->code) {
10850 case GOT_HISTEDIT_PICK:
10851 case GOT_HISTEDIT_EDIT:
10852 printf("%s -> %s: %s\n", old_id_str,
10853 new_id_str ? new_id_str : "no-op change", logmsg);
10855 case GOT_HISTEDIT_DROP:
10856 case GOT_HISTEDIT_FOLD:
10857 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10869 static const struct got_error *
10870 histedit_commit(struct got_pathlist_head *merged_paths,
10871 struct got_worktree *worktree, struct got_fileindex *fileindex,
10872 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10873 struct got_repository *repo)
10875 const struct got_error *err;
10876 struct got_commit_object *commit;
10877 struct got_object_id *new_commit_id;
10879 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10880 && hle->logmsg == NULL) {
10881 err = histedit_edit_logmsg(hle, repo);
10886 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10890 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10891 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10892 hle->logmsg, repo);
10894 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10896 err = show_histedit_progress(commit, hle, NULL);
10898 err = show_histedit_progress(commit, hle, new_commit_id);
10899 free(new_commit_id);
10902 got_object_commit_close(commit);
10906 static const struct got_error *
10907 histedit_skip_commit(struct got_histedit_list_entry *hle,
10908 struct got_worktree *worktree, struct got_repository *repo)
10910 const struct got_error *error;
10911 struct got_commit_object *commit;
10913 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10918 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10922 error = show_histedit_progress(commit, hle, NULL);
10923 got_object_commit_close(commit);
10927 static const struct got_error *
10928 check_local_changes(void *arg, unsigned char status,
10929 unsigned char staged_status, const char *path,
10930 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10931 struct got_object_id *commit_id, int dirfd, const char *de_name)
10933 int *have_local_changes = arg;
10936 case GOT_STATUS_ADD:
10937 case GOT_STATUS_DELETE:
10938 case GOT_STATUS_MODIFY:
10939 case GOT_STATUS_CONFLICT:
10940 *have_local_changes = 1;
10941 return got_error(GOT_ERR_CANCELLED);
10946 switch (staged_status) {
10947 case GOT_STATUS_ADD:
10948 case GOT_STATUS_DELETE:
10949 case GOT_STATUS_MODIFY:
10950 *have_local_changes = 1;
10951 return got_error(GOT_ERR_CANCELLED);
10959 static const struct got_error *
10960 cmd_histedit(int argc, char *argv[])
10962 const struct got_error *error = NULL;
10963 struct got_worktree *worktree = NULL;
10964 struct got_fileindex *fileindex = NULL;
10965 struct got_repository *repo = NULL;
10967 struct got_reference *branch = NULL;
10968 struct got_reference *tmp_branch = NULL;
10969 struct got_object_id *resume_commit_id = NULL;
10970 struct got_object_id *base_commit_id = NULL;
10971 struct got_object_id *head_commit_id = NULL;
10972 struct got_commit_object *commit = NULL;
10973 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10974 struct got_update_progress_arg upa;
10975 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10976 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10977 int list_backups = 0, delete_backups = 0;
10978 const char *edit_script_path = NULL;
10979 struct got_object_id_queue commits;
10980 struct got_pathlist_head merged_paths;
10981 const struct got_object_id_queue *parent_ids;
10982 struct got_object_qid *pid;
10983 struct got_histedit_list histedit_cmds;
10984 struct got_histedit_list_entry *hle;
10985 int *pack_fds = NULL;
10987 STAILQ_INIT(&commits);
10988 TAILQ_INIT(&histedit_cmds);
10989 TAILQ_INIT(&merged_paths);
10990 memset(&upa, 0, sizeof(upa));
10992 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11007 edit_script_path = optarg;
11010 edit_logmsg_only = 1;
11016 delete_backups = 1;
11028 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11029 "unveil", NULL) == -1)
11032 if (abort_edit && continue_edit)
11033 option_conflict('a', 'c');
11034 if (edit_script_path && edit_logmsg_only)
11035 option_conflict('F', 'm');
11036 if (abort_edit && edit_logmsg_only)
11037 option_conflict('a', 'm');
11038 if (continue_edit && edit_logmsg_only)
11039 option_conflict('c', 'm');
11040 if (abort_edit && fold_only)
11041 option_conflict('a', 'f');
11042 if (continue_edit && fold_only)
11043 option_conflict('c', 'f');
11044 if (fold_only && edit_logmsg_only)
11045 option_conflict('f', 'm');
11046 if (edit_script_path && fold_only)
11047 option_conflict('F', 'f');
11048 if (abort_edit && edit_only)
11049 option_conflict('a', 'e');
11050 if (continue_edit && edit_only)
11051 option_conflict('c', 'e');
11052 if (edit_only && edit_logmsg_only)
11053 option_conflict('e', 'm');
11054 if (edit_script_path && edit_only)
11055 option_conflict('F', 'e');
11056 if (list_backups) {
11058 option_conflict('l', 'a');
11060 option_conflict('l', 'c');
11061 if (edit_script_path)
11062 option_conflict('l', 'F');
11063 if (edit_logmsg_only)
11064 option_conflict('l', 'm');
11066 option_conflict('l', 'f');
11068 option_conflict('l', 'e');
11069 if (delete_backups)
11070 option_conflict('l', 'X');
11071 if (argc != 0 && argc != 1)
11073 } else if (delete_backups) {
11075 option_conflict('X', 'a');
11077 option_conflict('X', 'c');
11078 if (edit_script_path)
11079 option_conflict('X', 'F');
11080 if (edit_logmsg_only)
11081 option_conflict('X', 'm');
11083 option_conflict('X', 'f');
11085 option_conflict('X', 'e');
11087 option_conflict('X', 'l');
11088 if (argc != 0 && argc != 1)
11090 } else if (argc != 0)
11094 * This command cannot apply unveil(2) in all cases because the
11095 * user may choose to run an editor to edit the histedit script
11096 * and to edit individual commit log messages.
11097 * unveil(2) traverses exec(2); if an editor is used we have to
11098 * apply unveil after edit script and log messages have been written.
11099 * XXX TODO: Make use of unveil(2) where possible.
11102 cwd = getcwd(NULL, 0);
11104 error = got_error_from_errno("getcwd");
11108 error = got_repo_pack_fds_open(&pack_fds);
11112 error = got_worktree_open(&worktree, cwd);
11114 if (list_backups || delete_backups) {
11115 if (error->code != GOT_ERR_NOT_WORKTREE)
11118 if (error->code == GOT_ERR_NOT_WORKTREE)
11119 error = wrap_not_worktree_error(error,
11125 if (list_backups || delete_backups) {
11126 error = got_repo_open(&repo,
11127 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11131 error = apply_unveil(got_repo_get_path(repo), 0,
11132 worktree ? got_worktree_get_root_path(worktree) : NULL);
11135 error = process_backup_refs(
11136 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11137 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11138 goto done; /* nothing else to do */
11141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11146 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11149 if (rebase_in_progress) {
11150 error = got_error(GOT_ERR_REBASING);
11154 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11158 if (merge_in_progress) {
11159 error = got_error(GOT_ERR_MERGE_BUSY);
11163 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11167 if (edit_in_progress && edit_logmsg_only) {
11168 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11169 "histedit operation is in progress in this "
11170 "work tree and must be continued or aborted "
11171 "before the -m option can be used");
11174 if (edit_in_progress && fold_only) {
11175 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11176 "histedit operation is in progress in this "
11177 "work tree and must be continued or aborted "
11178 "before the -f option can be used");
11181 if (edit_in_progress && edit_only) {
11182 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11183 "histedit operation is in progress in this "
11184 "work tree and must be continued or aborted "
11185 "before the -e option can be used");
11189 if (edit_in_progress && abort_edit) {
11190 error = got_worktree_histedit_continue(&resume_commit_id,
11191 &tmp_branch, &branch, &base_commit_id, &fileindex,
11195 printf("Switching work tree to %s\n",
11196 got_ref_get_symref_target(branch));
11197 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11198 branch, base_commit_id, abort_progress, &upa);
11201 printf("Histedit of %s aborted\n",
11202 got_ref_get_symref_target(branch));
11203 print_merge_progress_stats(&upa);
11204 goto done; /* nothing else to do */
11205 } else if (abort_edit) {
11206 error = got_error(GOT_ERR_NOT_HISTEDIT);
11210 if (continue_edit) {
11213 if (!edit_in_progress) {
11214 error = got_error(GOT_ERR_NOT_HISTEDIT);
11218 error = got_worktree_get_histedit_script_path(&path, worktree);
11222 error = histedit_load_list(&histedit_cmds, path, repo);
11227 error = got_worktree_histedit_continue(&resume_commit_id,
11228 &tmp_branch, &branch, &base_commit_id, &fileindex,
11233 error = got_ref_resolve(&head_commit_id, repo, branch);
11237 error = got_object_open_as_commit(&commit, repo,
11241 parent_ids = got_object_commit_get_parent_ids(commit);
11242 pid = STAILQ_FIRST(parent_ids);
11244 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11247 error = collect_commits(&commits, head_commit_id, &pid->id,
11248 base_commit_id, got_worktree_get_path_prefix(worktree),
11249 GOT_ERR_HISTEDIT_PATH, repo);
11250 got_object_commit_close(commit);
11255 if (edit_in_progress) {
11256 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11260 error = got_ref_open(&branch, repo,
11261 got_worktree_get_head_ref_name(worktree), 0);
11265 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11266 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11267 "will not edit commit history of a branch outside "
11268 "the \"refs/heads/\" reference namespace");
11272 error = got_ref_resolve(&head_commit_id, repo, branch);
11273 got_ref_close(branch);
11278 error = got_object_open_as_commit(&commit, repo,
11282 parent_ids = got_object_commit_get_parent_ids(commit);
11283 pid = STAILQ_FIRST(parent_ids);
11285 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11288 error = collect_commits(&commits, head_commit_id, &pid->id,
11289 got_worktree_get_base_commit_id(worktree),
11290 got_worktree_get_path_prefix(worktree),
11291 GOT_ERR_HISTEDIT_PATH, repo);
11292 got_object_commit_close(commit);
11297 if (STAILQ_EMPTY(&commits)) {
11298 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11302 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11303 &base_commit_id, &fileindex, worktree, repo);
11307 if (edit_script_path) {
11308 error = histedit_load_list(&histedit_cmds,
11309 edit_script_path, repo);
11311 got_worktree_histedit_abort(worktree, fileindex,
11312 repo, branch, base_commit_id,
11313 abort_progress, &upa);
11314 print_merge_progress_stats(&upa);
11318 const char *branch_name;
11319 branch_name = got_ref_get_symref_target(branch);
11320 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11322 error = histedit_edit_script(&histedit_cmds, &commits,
11323 branch_name, edit_logmsg_only, fold_only,
11326 got_worktree_histedit_abort(worktree, fileindex,
11327 repo, branch, base_commit_id,
11328 abort_progress, &upa);
11329 print_merge_progress_stats(&upa);
11335 error = histedit_save_list(&histedit_cmds, worktree,
11338 got_worktree_histedit_abort(worktree, fileindex,
11339 repo, branch, base_commit_id,
11340 abort_progress, &upa);
11341 print_merge_progress_stats(&upa);
11347 error = histedit_check_script(&histedit_cmds, &commits, repo);
11351 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11352 if (resume_commit_id) {
11353 if (got_object_id_cmp(hle->commit_id,
11354 resume_commit_id) != 0)
11357 resume_commit_id = NULL;
11358 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11359 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11360 error = histedit_skip_commit(hle, worktree,
11365 struct got_pathlist_head paths;
11366 int have_changes = 0;
11368 TAILQ_INIT(&paths);
11369 error = got_pathlist_append(&paths, "", NULL);
11372 error = got_worktree_status(worktree, &paths,
11373 repo, 0, check_local_changes, &have_changes,
11374 check_cancelled, NULL);
11375 got_pathlist_free(&paths);
11377 if (error->code != GOT_ERR_CANCELLED)
11379 if (sigint_received || sigpipe_received)
11382 if (have_changes) {
11383 error = histedit_commit(NULL, worktree,
11384 fileindex, tmp_branch, hle, repo);
11388 error = got_object_open_as_commit(
11389 &commit, repo, hle->commit_id);
11392 error = show_histedit_progress(commit,
11394 got_object_commit_close(commit);
11403 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11404 error = histedit_skip_commit(hle, worktree, repo);
11410 error = got_object_open_as_commit(&commit, repo,
11414 parent_ids = got_object_commit_get_parent_ids(commit);
11415 pid = STAILQ_FIRST(parent_ids);
11417 error = got_worktree_histedit_merge_files(&merged_paths,
11418 worktree, fileindex, &pid->id, hle->commit_id, repo,
11419 update_progress, &upa, check_cancelled, NULL);
11422 got_object_commit_close(commit);
11425 print_merge_progress_stats(&upa);
11426 if (upa.conflicts > 0 || upa.missing > 0 ||
11427 upa.not_deleted > 0 || upa.unversioned > 0) {
11428 if (upa.conflicts > 0) {
11429 error = show_rebase_merge_conflict(
11430 hle->commit_id, repo);
11434 got_worktree_rebase_pathlist_free(&merged_paths);
11438 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11440 error = got_object_id_str(&id_str, hle->commit_id);
11443 printf("Stopping histedit for amending commit %s\n",
11446 got_worktree_rebase_pathlist_free(&merged_paths);
11447 error = got_worktree_histedit_postpone(worktree,
11452 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11453 error = histedit_skip_commit(hle, worktree, repo);
11459 error = histedit_commit(&merged_paths, worktree, fileindex,
11460 tmp_branch, hle, repo);
11461 got_worktree_rebase_pathlist_free(&merged_paths);
11466 if (upa.conflicts > 0 || upa.missing > 0 ||
11467 upa.not_deleted > 0 || upa.unversioned > 0) {
11468 error = got_worktree_histedit_postpone(worktree, fileindex);
11471 if (upa.conflicts > 0 && upa.missing == 0 &&
11472 upa.not_deleted == 0 && upa.unversioned == 0) {
11473 error = got_error_msg(GOT_ERR_CONFLICTS,
11474 "conflicts must be resolved before histedit "
11476 } else if (upa.conflicts > 0) {
11477 error = got_error_msg(GOT_ERR_CONFLICTS,
11478 "conflicts must be resolved before histedit "
11479 "can continue; changes destined for some "
11480 "files were not yet merged and should be "
11481 "merged manually if required before the "
11482 "histedit operation is continued");
11484 error = got_error_msg(GOT_ERR_CONFLICTS,
11485 "changes destined for some files were not "
11486 "yet merged and should be merged manually "
11487 "if required before the histedit operation "
11491 error = histedit_complete(worktree, fileindex, tmp_branch,
11494 got_object_id_queue_free(&commits);
11495 histedit_free_list(&histedit_cmds);
11496 free(head_commit_id);
11497 free(base_commit_id);
11498 free(resume_commit_id);
11500 got_object_commit_close(commit);
11502 got_ref_close(branch);
11504 got_ref_close(tmp_branch);
11506 got_worktree_close(worktree);
11508 const struct got_error *close_err = got_repo_close(repo);
11513 const struct got_error *pack_err =
11514 got_repo_pack_fds_close(pack_fds);
11523 usage_integrate(void)
11525 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11529 static const struct got_error *
11530 cmd_integrate(int argc, char *argv[])
11532 const struct got_error *error = NULL;
11533 struct got_repository *repo = NULL;
11534 struct got_worktree *worktree = NULL;
11535 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11536 const char *branch_arg = NULL;
11537 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11538 struct got_fileindex *fileindex = NULL;
11539 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11541 struct got_update_progress_arg upa;
11542 int *pack_fds = NULL;
11544 while ((ch = getopt(argc, argv, "")) != -1) {
11557 branch_arg = argv[0];
11559 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11560 "unveil", NULL) == -1)
11563 cwd = getcwd(NULL, 0);
11565 error = got_error_from_errno("getcwd");
11569 error = got_repo_pack_fds_open(&pack_fds);
11573 error = got_worktree_open(&worktree, cwd);
11575 if (error->code == GOT_ERR_NOT_WORKTREE)
11576 error = wrap_not_worktree_error(error, "integrate",
11581 error = check_rebase_or_histedit_in_progress(worktree);
11585 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11590 error = apply_unveil(got_repo_get_path(repo), 0,
11591 got_worktree_get_root_path(worktree));
11595 error = check_merge_in_progress(worktree, repo);
11599 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11600 error = got_error_from_errno("asprintf");
11604 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11605 &base_branch_ref, worktree, refname, repo);
11609 refname = strdup(got_ref_get_name(branch_ref));
11610 if (refname == NULL) {
11611 error = got_error_from_errno("strdup");
11612 got_worktree_integrate_abort(worktree, fileindex, repo,
11613 branch_ref, base_branch_ref);
11616 base_refname = strdup(got_ref_get_name(base_branch_ref));
11617 if (base_refname == NULL) {
11618 error = got_error_from_errno("strdup");
11619 got_worktree_integrate_abort(worktree, fileindex, repo,
11620 branch_ref, base_branch_ref);
11624 error = got_ref_resolve(&commit_id, repo, branch_ref);
11628 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11632 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11633 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11634 "specified branch has already been integrated");
11635 got_worktree_integrate_abort(worktree, fileindex, repo,
11636 branch_ref, base_branch_ref);
11640 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11642 if (error->code == GOT_ERR_ANCESTRY)
11643 error = got_error(GOT_ERR_REBASE_REQUIRED);
11644 got_worktree_integrate_abort(worktree, fileindex, repo,
11645 branch_ref, base_branch_ref);
11649 memset(&upa, 0, sizeof(upa));
11650 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11651 branch_ref, base_branch_ref, update_progress, &upa,
11652 check_cancelled, NULL);
11656 printf("Integrated %s into %s\n", refname, base_refname);
11657 print_update_progress_stats(&upa);
11660 const struct got_error *close_err = got_repo_close(repo);
11665 got_worktree_close(worktree);
11667 const struct got_error *pack_err =
11668 got_repo_pack_fds_close(pack_fds);
11674 free(base_commit_id);
11677 free(base_refname);
11684 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11689 static const struct got_error *
11690 cmd_merge(int argc, char *argv[])
11692 const struct got_error *error = NULL;
11693 struct got_worktree *worktree = NULL;
11694 struct got_repository *repo = NULL;
11695 struct got_fileindex *fileindex = NULL;
11696 char *cwd = NULL, *id_str = NULL, *author = NULL;
11697 struct got_reference *branch = NULL, *wt_branch = NULL;
11698 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11699 struct got_object_id *wt_branch_tip = NULL;
11700 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11701 int interrupt_merge = 0;
11702 struct got_update_progress_arg upa;
11703 struct got_object_id *merge_commit_id = NULL;
11704 char *branch_name = NULL;
11705 int *pack_fds = NULL;
11707 memset(&upa, 0, sizeof(upa));
11709 while ((ch = getopt(argc, argv, "acn")) != -1) {
11715 continue_merge = 1;
11718 interrupt_merge = 1;
11730 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11731 "unveil", NULL) == -1)
11735 if (abort_merge && continue_merge)
11736 option_conflict('a', 'c');
11737 if (abort_merge || continue_merge) {
11740 } else if (argc != 1)
11743 cwd = getcwd(NULL, 0);
11745 error = got_error_from_errno("getcwd");
11749 error = got_repo_pack_fds_open(&pack_fds);
11753 error = got_worktree_open(&worktree, cwd);
11755 if (error->code == GOT_ERR_NOT_WORKTREE)
11756 error = wrap_not_worktree_error(error,
11761 error = got_repo_open(&repo,
11762 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11767 error = apply_unveil(got_repo_get_path(repo), 0,
11768 worktree ? got_worktree_get_root_path(worktree) : NULL);
11772 error = check_rebase_or_histedit_in_progress(worktree);
11776 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11782 if (!merge_in_progress) {
11783 error = got_error(GOT_ERR_NOT_MERGING);
11786 error = got_worktree_merge_continue(&branch_name,
11787 &branch_tip, &fileindex, worktree, repo);
11790 error = got_worktree_merge_abort(worktree, fileindex, repo,
11791 abort_progress, &upa);
11794 printf("Merge of %s aborted\n", branch_name);
11795 goto done; /* nothing else to do */
11798 error = get_author(&author, repo, worktree);
11802 if (continue_merge) {
11803 if (!merge_in_progress) {
11804 error = got_error(GOT_ERR_NOT_MERGING);
11807 error = got_worktree_merge_continue(&branch_name,
11808 &branch_tip, &fileindex, worktree, repo);
11812 error = got_ref_open(&branch, repo, argv[0], 0);
11815 branch_name = strdup(got_ref_get_name(branch));
11816 if (branch_name == NULL) {
11817 error = got_error_from_errno("strdup");
11820 error = got_ref_resolve(&branch_tip, repo, branch);
11825 error = got_ref_open(&wt_branch, repo,
11826 got_worktree_get_head_ref_name(worktree), 0);
11829 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11832 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11833 wt_branch_tip, branch_tip, 0, repo,
11834 check_cancelled, NULL);
11835 if (error && error->code != GOT_ERR_ANCESTRY)
11838 if (!continue_merge) {
11839 error = check_path_prefix(wt_branch_tip, branch_tip,
11840 got_worktree_get_path_prefix(worktree),
11841 GOT_ERR_MERGE_PATH, repo);
11845 error = check_same_branch(wt_branch_tip, branch,
11848 if (error->code != GOT_ERR_ANCESTRY)
11852 static char msg[512];
11853 snprintf(msg, sizeof(msg),
11854 "cannot create a merge commit because "
11855 "%s is based on %s; %s can be integrated "
11856 "with 'got integrate' instead", branch_name,
11857 got_worktree_get_head_ref_name(worktree),
11859 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11863 error = got_worktree_merge_prepare(&fileindex, worktree,
11868 error = got_worktree_merge_branch(worktree, fileindex,
11869 yca_id, branch_tip, repo, update_progress, &upa,
11870 check_cancelled, NULL);
11873 print_merge_progress_stats(&upa);
11874 if (!upa.did_something) {
11875 error = got_worktree_merge_abort(worktree, fileindex,
11876 repo, abort_progress, &upa);
11879 printf("Already up-to-date\n");
11884 if (interrupt_merge) {
11885 error = got_worktree_merge_postpone(worktree, fileindex);
11888 printf("Merge of %s interrupted on request\n", branch_name);
11889 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11890 upa.not_deleted > 0 || upa.unversioned > 0) {
11891 error = got_worktree_merge_postpone(worktree, fileindex);
11894 if (upa.conflicts > 0 && upa.missing == 0 &&
11895 upa.not_deleted == 0 && upa.unversioned == 0) {
11896 error = got_error_msg(GOT_ERR_CONFLICTS,
11897 "conflicts must be resolved before merging "
11899 } else if (upa.conflicts > 0) {
11900 error = got_error_msg(GOT_ERR_CONFLICTS,
11901 "conflicts must be resolved before merging "
11902 "can continue; changes destined for some "
11903 "files were not yet merged and "
11904 "should be merged manually if required before the "
11905 "merge operation is continued");
11907 error = got_error_msg(GOT_ERR_CONFLICTS,
11908 "changes destined for some "
11909 "files were not yet merged and should be "
11910 "merged manually if required before the "
11911 "merge operation is continued");
11915 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11916 fileindex, author, NULL, 1, branch_tip, branch_name,
11917 repo, continue_merge ? print_status : NULL, NULL);
11920 error = got_worktree_merge_complete(worktree, fileindex, repo);
11923 error = got_object_id_str(&id_str, merge_commit_id);
11926 printf("Merged %s into %s: %s\n", branch_name,
11927 got_worktree_get_head_ref_name(worktree),
11933 free(merge_commit_id);
11939 got_ref_close(branch);
11941 got_ref_close(wt_branch);
11943 got_worktree_close(worktree);
11945 const struct got_error *close_err = got_repo_close(repo);
11950 const struct got_error *pack_err =
11951 got_repo_pack_fds_close(pack_fds);
11962 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11963 "[-S] [file-path ...]\n",
11968 static const struct got_error *
11969 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11970 const char *path, struct got_object_id *blob_id,
11971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11972 int dirfd, const char *de_name)
11974 const struct got_error *err = NULL;
11975 char *id_str = NULL;
11977 if (staged_status != GOT_STATUS_ADD &&
11978 staged_status != GOT_STATUS_MODIFY &&
11979 staged_status != GOT_STATUS_DELETE)
11982 if (staged_status == GOT_STATUS_ADD ||
11983 staged_status == GOT_STATUS_MODIFY)
11984 err = got_object_id_str(&id_str, staged_blob_id);
11986 err = got_object_id_str(&id_str, blob_id);
11990 printf("%s %c %s\n", id_str, staged_status, path);
11995 static const struct got_error *
11996 cmd_stage(int argc, char *argv[])
11998 const struct got_error *error = NULL;
11999 struct got_repository *repo = NULL;
12000 struct got_worktree *worktree = NULL;
12002 struct got_pathlist_head paths;
12003 struct got_pathlist_entry *pe;
12004 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12005 FILE *patch_script_file = NULL;
12006 const char *patch_script_path = NULL;
12007 struct choose_patch_arg cpa;
12008 int *pack_fds = NULL;
12010 TAILQ_INIT(&paths);
12012 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12021 patch_script_path = optarg;
12024 allow_bad_symlinks = 1;
12036 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12037 "unveil", NULL) == -1)
12040 if (list_stage && (pflag || patch_script_path))
12041 errx(1, "-l option cannot be used with other options");
12042 if (patch_script_path && !pflag)
12043 errx(1, "-F option can only be used together with -p option");
12045 cwd = getcwd(NULL, 0);
12047 error = got_error_from_errno("getcwd");
12051 error = got_repo_pack_fds_open(&pack_fds);
12055 error = got_worktree_open(&worktree, cwd);
12057 if (error->code == GOT_ERR_NOT_WORKTREE)
12058 error = wrap_not_worktree_error(error, "stage", cwd);
12062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12067 if (patch_script_path) {
12068 patch_script_file = fopen(patch_script_path, "re");
12069 if (patch_script_file == NULL) {
12070 error = got_error_from_errno2("fopen",
12071 patch_script_path);
12075 error = apply_unveil(got_repo_get_path(repo), 0,
12076 got_worktree_get_root_path(worktree));
12080 error = check_merge_in_progress(worktree, repo);
12084 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12089 error = got_worktree_status(worktree, &paths, repo, 0,
12090 print_stage, NULL, check_cancelled, NULL);
12092 cpa.patch_script_file = patch_script_file;
12093 cpa.action = "stage";
12094 error = got_worktree_stage(worktree, &paths,
12095 pflag ? NULL : print_status, NULL,
12096 pflag ? choose_patch : NULL, &cpa,
12097 allow_bad_symlinks, repo);
12100 if (patch_script_file && fclose(patch_script_file) == EOF &&
12102 error = got_error_from_errno2("fclose", patch_script_path);
12104 const struct got_error *close_err = got_repo_close(repo);
12109 got_worktree_close(worktree);
12111 const struct got_error *pack_err =
12112 got_repo_pack_fds_close(pack_fds);
12117 TAILQ_FOREACH(pe, &paths, entry)
12118 free((char *)pe->path);
12119 got_pathlist_free(&paths);
12125 usage_unstage(void)
12127 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12128 "[file-path ...]\n",
12134 static const struct got_error *
12135 cmd_unstage(int argc, char *argv[])
12137 const struct got_error *error = NULL;
12138 struct got_repository *repo = NULL;
12139 struct got_worktree *worktree = NULL;
12141 struct got_pathlist_head paths;
12142 struct got_pathlist_entry *pe;
12144 struct got_update_progress_arg upa;
12145 FILE *patch_script_file = NULL;
12146 const char *patch_script_path = NULL;
12147 struct choose_patch_arg cpa;
12148 int *pack_fds = NULL;
12150 TAILQ_INIT(&paths);
12152 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12158 patch_script_path = optarg;
12170 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12171 "unveil", NULL) == -1)
12174 if (patch_script_path && !pflag)
12175 errx(1, "-F option can only be used together with -p option");
12177 cwd = getcwd(NULL, 0);
12179 error = got_error_from_errno("getcwd");
12183 error = got_repo_pack_fds_open(&pack_fds);
12187 error = got_worktree_open(&worktree, cwd);
12189 if (error->code == GOT_ERR_NOT_WORKTREE)
12190 error = wrap_not_worktree_error(error, "unstage", cwd);
12194 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12199 if (patch_script_path) {
12200 patch_script_file = fopen(patch_script_path, "re");
12201 if (patch_script_file == NULL) {
12202 error = got_error_from_errno2("fopen",
12203 patch_script_path);
12208 error = apply_unveil(got_repo_get_path(repo), 0,
12209 got_worktree_get_root_path(worktree));
12213 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12217 cpa.patch_script_file = patch_script_file;
12218 cpa.action = "unstage";
12219 memset(&upa, 0, sizeof(upa));
12220 error = got_worktree_unstage(worktree, &paths, update_progress,
12221 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12223 print_merge_progress_stats(&upa);
12225 if (patch_script_file && fclose(patch_script_file) == EOF &&
12227 error = got_error_from_errno2("fclose", patch_script_path);
12229 const struct got_error *close_err = got_repo_close(repo);
12234 got_worktree_close(worktree);
12236 const struct got_error *pack_err =
12237 got_repo_pack_fds_close(pack_fds);
12242 TAILQ_FOREACH(pe, &paths, entry)
12243 free((char *)pe->path);
12244 got_pathlist_free(&paths);
12252 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12253 "arg1 [arg2 ...]\n", getprogname());
12257 static const struct got_error *
12258 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12260 const struct got_error *err;
12261 struct got_blob_object *blob;
12263 err = got_object_open_as_blob(&blob, repo, id, 8192);
12267 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12268 got_object_blob_close(blob);
12272 static const struct got_error *
12273 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12275 const struct got_error *err;
12276 struct got_tree_object *tree;
12279 err = got_object_open_as_tree(&tree, repo, id);
12283 nentries = got_object_tree_get_nentries(tree);
12284 for (i = 0; i < nentries; i++) {
12285 struct got_tree_entry *te;
12287 if (sigint_received || sigpipe_received)
12289 te = got_object_tree_get_entry(tree, i);
12290 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12293 fprintf(outfile, "%s %.7o %s\n", id_str,
12294 got_tree_entry_get_mode(te),
12295 got_tree_entry_get_name(te));
12299 got_object_tree_close(tree);
12304 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12314 h = (long long)gmtoff / 3600;
12315 m = ((long long)gmtoff - h*3600) / 60;
12316 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12319 static const struct got_error *
12320 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12322 const struct got_error *err;
12323 struct got_commit_object *commit;
12324 const struct got_object_id_queue *parent_ids;
12325 struct got_object_qid *pid;
12326 char *id_str = NULL;
12327 const char *logmsg = NULL;
12330 err = got_object_open_as_commit(&commit, repo, id);
12334 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12338 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12339 parent_ids = got_object_commit_get_parent_ids(commit);
12340 fprintf(outfile, "numparents %d\n",
12341 got_object_commit_get_nparents(commit));
12342 STAILQ_FOREACH(pid, parent_ids, entry) {
12344 err = got_object_id_str(&pid_str, &pid->id);
12347 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12350 format_gmtoff(gmtoff, sizeof(gmtoff),
12351 got_object_commit_get_author_gmtoff(commit));
12352 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12353 got_object_commit_get_author(commit),
12354 (long long)got_object_commit_get_author_time(commit),
12357 format_gmtoff(gmtoff, sizeof(gmtoff),
12358 got_object_commit_get_committer_gmtoff(commit));
12359 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12360 got_object_commit_get_author(commit),
12361 (long long)got_object_commit_get_committer_time(commit),
12364 logmsg = got_object_commit_get_logmsg_raw(commit);
12365 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12366 fprintf(outfile, "%s", logmsg);
12369 got_object_commit_close(commit);
12373 static const struct got_error *
12374 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12376 const struct got_error *err;
12377 struct got_tag_object *tag;
12378 char *id_str = NULL;
12379 const char *tagmsg = NULL;
12382 err = got_object_open_as_tag(&tag, repo, id);
12386 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12390 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12392 switch (got_object_tag_get_object_type(tag)) {
12393 case GOT_OBJ_TYPE_BLOB:
12394 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12395 GOT_OBJ_LABEL_BLOB);
12397 case GOT_OBJ_TYPE_TREE:
12398 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12399 GOT_OBJ_LABEL_TREE);
12401 case GOT_OBJ_TYPE_COMMIT:
12402 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12403 GOT_OBJ_LABEL_COMMIT);
12405 case GOT_OBJ_TYPE_TAG:
12406 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12407 GOT_OBJ_LABEL_TAG);
12413 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12414 got_object_tag_get_name(tag));
12416 format_gmtoff(gmtoff, sizeof(gmtoff),
12417 got_object_tag_get_tagger_gmtoff(tag));
12418 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12419 got_object_tag_get_tagger(tag),
12420 (long long)got_object_tag_get_tagger_time(tag),
12423 tagmsg = got_object_tag_get_message(tag);
12424 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12425 fprintf(outfile, "%s", tagmsg);
12428 got_object_tag_close(tag);
12432 static const struct got_error *
12433 cmd_cat(int argc, char *argv[])
12435 const struct got_error *error;
12436 struct got_repository *repo = NULL;
12437 struct got_worktree *worktree = NULL;
12438 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12439 const char *commit_id_str = NULL;
12440 struct got_object_id *id = NULL, *commit_id = NULL;
12441 struct got_commit_object *commit = NULL;
12442 int ch, obj_type, i, force_path = 0;
12443 struct got_reflist_head refs;
12444 int *pack_fds = NULL;
12449 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12454 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12457 commit_id_str = optarg;
12460 repo_path = realpath(optarg, NULL);
12461 if (repo_path == NULL)
12462 return got_error_from_errno2("realpath",
12464 got_path_strip_trailing_slashes(repo_path);
12478 cwd = getcwd(NULL, 0);
12480 error = got_error_from_errno("getcwd");
12484 error = got_repo_pack_fds_open(&pack_fds);
12488 if (repo_path == NULL) {
12489 error = got_worktree_open(&worktree, cwd);
12490 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12493 repo_path = strdup(
12494 got_worktree_get_repo_path(worktree));
12495 if (repo_path == NULL) {
12496 error = got_error_from_errno("strdup");
12500 /* Release work tree lock. */
12501 got_worktree_close(worktree);
12506 if (repo_path == NULL) {
12507 repo_path = strdup(cwd);
12508 if (repo_path == NULL)
12509 return got_error_from_errno("strdup");
12512 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12517 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12521 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12525 if (commit_id_str == NULL)
12526 commit_id_str = GOT_REF_HEAD;
12527 error = got_repo_match_object_id(&commit_id, NULL,
12528 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12532 error = got_object_open_as_commit(&commit, repo, commit_id);
12536 for (i = 0; i < argc; i++) {
12538 error = got_object_id_by_path(&id, repo, commit,
12543 error = got_repo_match_object_id(&id, &label, argv[i],
12544 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12547 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12548 error->code != GOT_ERR_NOT_REF)
12550 error = got_object_id_by_path(&id, repo,
12557 error = got_object_get_type(&obj_type, repo, id);
12561 switch (obj_type) {
12562 case GOT_OBJ_TYPE_BLOB:
12563 error = cat_blob(id, repo, stdout);
12565 case GOT_OBJ_TYPE_TREE:
12566 error = cat_tree(id, repo, stdout);
12568 case GOT_OBJ_TYPE_COMMIT:
12569 error = cat_commit(id, repo, stdout);
12571 case GOT_OBJ_TYPE_TAG:
12572 error = cat_tag(id, repo, stdout);
12575 error = got_error(GOT_ERR_OBJ_TYPE);
12590 got_object_commit_close(commit);
12592 got_worktree_close(worktree);
12594 const struct got_error *close_err = got_repo_close(repo);
12599 const struct got_error *pack_err =
12600 got_repo_pack_fds_close(pack_fds);
12606 got_ref_list_free(&refs);
12613 fprintf(stderr, "usage: %s info [path ...]\n",
12618 static const struct got_error *
12619 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12620 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12621 struct got_object_id *commit_id)
12623 const struct got_error *err = NULL;
12624 char *id_str = NULL;
12626 struct tm mytm, *tm;
12627 struct got_pathlist_head *paths = arg;
12628 struct got_pathlist_entry *pe;
12631 * Clear error indication from any of the path arguments which
12632 * would cause this file index entry to be displayed.
12634 TAILQ_FOREACH(pe, paths, entry) {
12635 if (got_path_cmp(path, pe->path, strlen(path),
12636 pe->path_len) == 0 ||
12637 got_path_is_child(path, pe->path, pe->path_len))
12638 pe->data = NULL; /* no error */
12641 printf(GOT_COMMIT_SEP_STR);
12643 printf("symlink: %s\n", path);
12644 else if (S_ISREG(mode)) {
12645 printf("file: %s\n", path);
12646 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12647 } else if (S_ISDIR(mode))
12648 printf("directory: %s\n", path);
12650 printf("something: %s\n", path);
12652 tm = localtime_r(&mtime, &mytm);
12655 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12656 return got_error(GOT_ERR_NO_SPACE);
12657 printf("timestamp: %s\n", datebuf);
12660 err = got_object_id_str(&id_str, blob_id);
12663 printf("based on blob: %s\n", id_str);
12667 if (staged_blob_id) {
12668 err = got_object_id_str(&id_str, staged_blob_id);
12671 printf("based on staged blob: %s\n", id_str);
12676 err = got_object_id_str(&id_str, commit_id);
12679 printf("based on commit: %s\n", id_str);
12686 static const struct got_error *
12687 cmd_info(int argc, char *argv[])
12689 const struct got_error *error = NULL;
12690 struct got_worktree *worktree = NULL;
12691 char *cwd = NULL, *id_str = NULL;
12692 struct got_pathlist_head paths;
12693 struct got_pathlist_entry *pe;
12694 char *uuidstr = NULL;
12695 int ch, show_files = 0;
12696 int *pack_fds = NULL;
12698 TAILQ_INIT(&paths);
12700 while ((ch = getopt(argc, argv, "")) != -1) {
12712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12716 cwd = getcwd(NULL, 0);
12718 error = got_error_from_errno("getcwd");
12722 error = got_repo_pack_fds_open(&pack_fds);
12726 error = got_worktree_open(&worktree, cwd);
12728 if (error->code == GOT_ERR_NOT_WORKTREE)
12729 error = wrap_not_worktree_error(error, "info", cwd);
12734 /* Remove "cpath" promise. */
12735 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12739 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12744 error = get_worktree_paths_from_argv(&paths, argc, argv,
12751 error = got_object_id_str(&id_str,
12752 got_worktree_get_base_commit_id(worktree));
12756 error = got_worktree_get_uuid(&uuidstr, worktree);
12760 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12761 printf("work tree base commit: %s\n", id_str);
12762 printf("work tree path prefix: %s\n",
12763 got_worktree_get_path_prefix(worktree));
12764 printf("work tree branch reference: %s\n",
12765 got_worktree_get_head_ref_name(worktree));
12766 printf("work tree UUID: %s\n", uuidstr);
12767 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12770 struct got_pathlist_entry *pe;
12771 TAILQ_FOREACH(pe, &paths, entry) {
12772 if (pe->path_len == 0)
12775 * Assume this path will fail. This will be corrected
12776 * in print_path_info() in case the path does suceeed.
12778 pe->data = (void *)got_error_path(pe->path,
12781 error = got_worktree_path_info(worktree, &paths,
12782 print_path_info, &paths, check_cancelled, NULL);
12785 TAILQ_FOREACH(pe, &paths, entry) {
12786 if (pe->data != NULL) {
12787 error = pe->data; /* bad path */
12794 const struct got_error *pack_err =
12795 got_repo_pack_fds_close(pack_fds);
12800 TAILQ_FOREACH(pe, &paths, entry)
12801 free((char *)pe->path);
12802 got_pathlist_free(&paths);