2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
57 #include "got_opentemp.h"
58 #include "got_gotconfig.h"
60 #include "got_patch.h"
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 static volatile sig_atomic_t sigint_received;
67 static volatile sig_atomic_t sigpipe_received;
70 catch_sigint(int signo)
76 catch_sigpipe(int signo)
84 const struct got_error *(*cmd_main)(int, char *[]);
85 void (*cmd_usage)(void);
86 const char *cmd_alias;
89 __dead static void usage(int, int);
90 __dead static void usage_init(void);
91 __dead static void usage_import(void);
92 __dead static void usage_clone(void);
93 __dead static void usage_fetch(void);
94 __dead static void usage_checkout(void);
95 __dead static void usage_update(void);
96 __dead static void usage_log(void);
97 __dead static void usage_diff(void);
98 __dead static void usage_blame(void);
99 __dead static void usage_tree(void);
100 __dead static void usage_status(void);
101 __dead static void usage_ref(void);
102 __dead static void usage_branch(void);
103 __dead static void usage_tag(void);
104 __dead static void usage_add(void);
105 __dead static void usage_remove(void);
106 __dead static void usage_patch(void);
107 __dead static void usage_revert(void);
108 __dead static void usage_commit(void);
109 __dead static void usage_send(void);
110 __dead static void usage_cherrypick(void);
111 __dead static void usage_backout(void);
112 __dead static void usage_rebase(void);
113 __dead static void usage_histedit(void);
114 __dead static void usage_integrate(void);
115 __dead static void usage_merge(void);
116 __dead static void usage_stage(void);
117 __dead static void usage_unstage(void);
118 __dead static void usage_cat(void);
119 __dead static void usage_info(void);
121 static const struct got_error* cmd_init(int, char *[]);
122 static const struct got_error* cmd_import(int, char *[]);
123 static const struct got_error* cmd_clone(int, char *[]);
124 static const struct got_error* cmd_fetch(int, char *[]);
125 static const struct got_error* cmd_checkout(int, char *[]);
126 static const struct got_error* cmd_update(int, char *[]);
127 static const struct got_error* cmd_log(int, char *[]);
128 static const struct got_error* cmd_diff(int, char *[]);
129 static const struct got_error* cmd_blame(int, char *[]);
130 static const struct got_error* cmd_tree(int, char *[]);
131 static const struct got_error* cmd_status(int, char *[]);
132 static const struct got_error* cmd_ref(int, char *[]);
133 static const struct got_error* cmd_branch(int, char *[]);
134 static const struct got_error* cmd_tag(int, char *[]);
135 static const struct got_error* cmd_add(int, char *[]);
136 static const struct got_error* cmd_remove(int, char *[]);
137 static const struct got_error* cmd_patch(int, char *[]);
138 static const struct got_error* cmd_revert(int, char *[]);
139 static const struct got_error* cmd_commit(int, char *[]);
140 static const struct got_error* cmd_send(int, char *[]);
141 static const struct got_error* cmd_cherrypick(int, char *[]);
142 static const struct got_error* cmd_backout(int, char *[]);
143 static const struct got_error* cmd_rebase(int, char *[]);
144 static const struct got_error* cmd_histedit(int, char *[]);
145 static const struct got_error* cmd_integrate(int, char *[]);
146 static const struct got_error* cmd_merge(int, char *[]);
147 static const struct got_error* cmd_stage(int, char *[]);
148 static const struct got_error* cmd_unstage(int, char *[]);
149 static const struct got_error* cmd_cat(int, char *[]);
150 static const struct got_error* cmd_info(int, char *[]);
152 static const struct got_cmd got_commands[] = {
153 { "init", cmd_init, usage_init, "" },
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
354 static const struct got_error *
355 cmd_init(int argc, char *argv[])
357 const struct got_error *error = NULL;
358 char *repo_path = NULL;
361 while ((ch = getopt(argc, argv, "")) != -1) {
373 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
379 repo_path = strdup(argv[0]);
380 if (repo_path == NULL)
381 return got_error_from_errno("strdup");
383 got_path_strip_trailing_slashes(repo_path);
385 error = got_path_mkdir(repo_path);
387 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
390 error = apply_unveil(repo_path, 0, NULL);
394 error = got_repo_init(repo_path);
403 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
404 "[-r repository-path] [-I pattern] path\n", getprogname());
409 spawn_editor(const char *editor, const char *file)
412 sig_t sighup, sigint, sigquit;
415 sighup = signal(SIGHUP, SIG_IGN);
416 sigint = signal(SIGINT, SIG_IGN);
417 sigquit = signal(SIGQUIT, SIG_IGN);
419 switch (pid = fork()) {
423 execl(editor, editor, file, (char *)NULL);
427 while (waitpid(pid, &st, 0) == -1)
432 (void)signal(SIGHUP, sighup);
433 (void)signal(SIGINT, sigint);
434 (void)signal(SIGQUIT, sigquit);
436 if (!WIFEXITED(st)) {
441 return WEXITSTATUS(st);
444 static const struct got_error *
445 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
446 const char *initial_content, size_t initial_content_len,
447 int require_modification)
449 const struct got_error *err = NULL;
455 size_t len, logmsg_len;
456 char *initial_content_stripped = NULL, *buf = NULL, *s;
460 if (stat(logmsg_path, &st) == -1)
461 return got_error_from_errno2("stat", logmsg_path);
463 if (spawn_editor(editor, logmsg_path) == -1)
464 return got_error_from_errno("failed spawning editor");
466 if (stat(logmsg_path, &st2) == -1)
467 return got_error_from_errno("stat");
469 if (require_modification &&
470 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
471 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "no changes made to commit message, aborting");
475 * Set up a stripped version of the initial content without comments
476 * and blank lines. We need this in order to check if the message
477 * has in fact been edited.
479 initial_content_stripped = malloc(initial_content_len + 1);
480 if (initial_content_stripped == NULL)
481 return got_error_from_errno("malloc");
482 initial_content_stripped[0] = '\0';
484 buf = strdup(initial_content);
486 err = got_error_from_errno("strdup");
491 while ((line = strsep(&s, "\n")) != NULL) {
492 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
493 continue; /* remove comments and leading empty lines */
494 len = strlcat(initial_content_stripped, line,
495 initial_content_len + 1);
496 if (len >= initial_content_len + 1) {
497 err = got_error(GOT_ERR_NO_SPACE);
501 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
502 initial_content_stripped[len - 1] = '\0';
506 logmsg_len = st2.st_size;
507 *logmsg = malloc(logmsg_len + 1);
509 return got_error_from_errno("malloc");
512 fp = fopen(logmsg_path, "re");
514 err = got_error_from_errno("fopen");
519 while ((linelen = getline(&line, &linesize, fp)) != -1) {
520 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
521 continue; /* remove comments and leading empty lines */
522 len = strlcat(*logmsg, line, logmsg_len + 1);
523 if (len >= logmsg_len + 1) {
524 err = got_error(GOT_ERR_NO_SPACE);
530 err = got_ferror(fp, GOT_ERR_IO);
533 while (len > 0 && (*logmsg)[len - 1] == '\n') {
534 (*logmsg)[len - 1] = '\0';
539 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
540 "commit message cannot be empty, aborting");
543 if (require_modification &&
544 strcmp(*logmsg, initial_content_stripped) == 0)
545 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
546 "no changes made to commit message, aborting");
548 free(initial_content_stripped);
550 if (fp && fclose(fp) == EOF && err == NULL)
551 err = got_error_from_errno("fclose");
559 static const struct got_error *
560 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
561 const char *path_dir, const char *branch_name)
563 char *initial_content = NULL;
564 const struct got_error *err = NULL;
565 int initial_content_len;
568 initial_content_len = asprintf(&initial_content,
569 "\n# %s to be imported to branch %s\n", path_dir,
571 if (initial_content_len == -1)
572 return got_error_from_errno("asprintf");
574 err = got_opentemp_named_fd(logmsg_path, &fd,
575 GOT_TMPDIR_STR "/got-importmsg");
579 if (write(fd, initial_content, initial_content_len) == -1) {
580 err = got_error_from_errno2("write", *logmsg_path);
584 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
585 initial_content_len, 1);
587 if (fd != -1 && close(fd) == -1 && err == NULL)
588 err = got_error_from_errno2("close", *logmsg_path);
589 free(initial_content);
597 static const struct got_error *
598 import_progress(void *arg, const char *path)
600 printf("A %s\n", path);
605 valid_author(const char *author)
608 * Really dumb email address check; we're only doing this to
609 * avoid git's object parser breaking on commits we create.
611 while (*author && *author != '<')
615 while (*author && *author != '@')
619 while (*author && *author != '>')
621 return *author == '>';
624 static const struct got_error *
625 get_author(char **author, struct got_repository *repo,
626 struct got_worktree *worktree)
628 const struct got_error *err = NULL;
629 const char *got_author = NULL, *name, *email;
630 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
635 worktree_conf = got_worktree_get_gotconfig(worktree);
636 repo_conf = got_repo_get_gotconfig(repo);
639 * Priority of potential author information sources, from most
640 * significant to least significant:
641 * 1) work tree's .got/got.conf file
642 * 2) repository's got.conf file
643 * 3) repository's git config file
644 * 4) environment variables
645 * 5) global git config files (in user's home directory or /etc)
649 got_author = got_gotconfig_get_author(worktree_conf);
650 if (got_author == NULL)
651 got_author = got_gotconfig_get_author(repo_conf);
652 if (got_author == NULL) {
653 name = got_repo_get_gitconfig_author_name(repo);
654 email = got_repo_get_gitconfig_author_email(repo);
656 if (asprintf(author, "%s <%s>", name, email) == -1)
657 return got_error_from_errno("asprintf");
661 got_author = getenv("GOT_AUTHOR");
662 if (got_author == NULL) {
663 name = got_repo_get_global_gitconfig_author_name(repo);
664 email = got_repo_get_global_gitconfig_author_email(
667 if (asprintf(author, "%s <%s>", name, email)
669 return got_error_from_errno("asprintf");
672 /* TODO: Look up user in password database? */
673 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
677 *author = strdup(got_author);
679 return got_error_from_errno("strdup");
681 if (!valid_author(*author)) {
682 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
689 static const struct got_error *
690 get_gitconfig_path(char **gitconfig_path)
692 const char *homedir = getenv("HOME");
694 *gitconfig_path = NULL;
696 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
697 return got_error_from_errno("asprintf");
703 static const struct got_error *
704 cmd_import(int argc, char *argv[])
706 const struct got_error *error = NULL;
707 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
708 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
709 const char *branch_name = "main";
710 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
711 struct got_repository *repo = NULL;
712 struct got_reference *branch_ref = NULL, *head_ref = NULL;
713 struct got_object_id *new_commit_id = NULL;
715 struct got_pathlist_head ignores;
716 struct got_pathlist_entry *pe;
717 int preserve_logmsg = 0;
718 int *pack_fds = NULL;
720 TAILQ_INIT(&ignores);
722 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
725 branch_name = optarg;
728 logmsg = strdup(optarg);
729 if (logmsg == NULL) {
730 error = got_error_from_errno("strdup");
735 repo_path = realpath(optarg, NULL);
736 if (repo_path == NULL) {
737 error = got_error_from_errno2("realpath",
743 if (optarg[0] == '\0')
745 error = got_pathlist_insert(&pe, &ignores, optarg,
760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
768 if (repo_path == NULL) {
769 repo_path = getcwd(NULL, 0);
770 if (repo_path == NULL)
771 return got_error_from_errno("getcwd");
773 got_path_strip_trailing_slashes(repo_path);
774 error = get_gitconfig_path(&gitconfig_path);
777 error = got_repo_pack_fds_open(&pack_fds);
780 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
784 error = get_author(&author, repo, NULL);
789 * Don't let the user create a branch name with a leading '-'.
790 * While technically a valid reference name, this case is usually
791 * an unintended typo.
793 if (branch_name[0] == '-')
794 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
796 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
797 error = got_error_from_errno("asprintf");
801 error = got_ref_open(&branch_ref, repo, refname, 0);
803 if (error->code != GOT_ERR_NOT_REF)
806 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
807 "import target branch already exists");
811 path_dir = realpath(argv[0], NULL);
812 if (path_dir == NULL) {
813 error = got_error_from_errno2("realpath", argv[0]);
816 got_path_strip_trailing_slashes(path_dir);
819 * unveil(2) traverses exec(2); if an editor is used we have
820 * to apply unveil after the log message has been written.
822 if (logmsg == NULL || strlen(logmsg) == 0) {
823 error = get_editor(&editor);
827 error = collect_import_msg(&logmsg, &logmsg_path, editor,
830 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
837 if (unveil(path_dir, "r") != 0) {
838 error = got_error_from_errno2("unveil", path_dir);
844 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
851 error = got_repo_import(&new_commit_id, path_dir, logmsg,
852 author, &ignores, repo, import_progress, NULL);
859 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
866 error = got_ref_write(branch_ref, repo);
873 error = got_object_id_str(&id_str, new_commit_id);
880 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
882 if (error->code != GOT_ERR_NOT_REF) {
888 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
896 error = got_ref_write(head_ref, repo);
904 printf("Created branch %s with commit %s\n",
905 got_ref_get_name(branch_ref), id_str);
908 const struct got_error *pack_err =
909 got_repo_pack_fds_close(pack_fds);
913 if (preserve_logmsg) {
914 fprintf(stderr, "%s: log message preserved in %s\n",
915 getprogname(), logmsg_path);
916 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
917 error = got_error_from_errno2("unlink", logmsg_path);
926 free(gitconfig_path);
928 got_ref_close(branch_ref);
930 got_ref_close(head_ref);
937 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
938 "[-R reference] repository-url [directory]\n", getprogname());
942 struct got_fetch_progress_arg {
943 char last_scaled_size[FMT_SCALED_STRSIZE];
948 struct got_repository *repo;
953 struct got_pathlist_head *symrefs;
954 struct got_pathlist_head *wanted_branches;
955 struct got_pathlist_head *wanted_refs;
959 const char *remote_repo_path;
961 int fetch_all_branches;
962 int mirror_references;
966 /* XXX forward declaration */
967 static const struct got_error *
968 create_config_files(const char *proto, const char *host, const char *port,
969 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
970 int mirror_references, struct got_pathlist_head *symrefs,
971 struct got_pathlist_head *wanted_branches,
972 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
974 static const struct got_error *
975 fetch_progress(void *arg, const char *message, off_t packfile_size,
976 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
978 const struct got_error *err = NULL;
979 struct got_fetch_progress_arg *a = arg;
980 char scaled_size[FMT_SCALED_STRSIZE];
981 int p_indexed, p_resolved;
982 int print_size = 0, print_indexed = 0, print_resolved = 0;
985 * In order to allow a failed clone to be resumed with 'got fetch'
986 * we try to create configuration files as soon as possible.
987 * Once the server has sent information about its default branch
988 * we have all required information.
990 if (a->create_configs && !a->configs_created &&
991 !TAILQ_EMPTY(a->config_info.symrefs)) {
992 err = create_config_files(a->config_info.proto,
993 a->config_info.host, a->config_info.port,
994 a->config_info.remote_repo_path,
995 a->config_info.git_url,
996 a->config_info.fetch_all_branches,
997 a->config_info.mirror_references,
998 a->config_info.symrefs,
999 a->config_info.wanted_branches,
1000 a->config_info.wanted_refs, a->repo);
1003 a->configs_created = 1;
1006 if (a->verbosity < 0)
1009 if (message && message[0] != '\0') {
1010 printf("\rserver: %s", message);
1015 if (packfile_size > 0 || nobj_indexed > 0) {
1016 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1017 (a->last_scaled_size[0] == '\0' ||
1018 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1020 if (strlcpy(a->last_scaled_size, scaled_size,
1021 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1022 return got_error(GOT_ERR_NO_SPACE);
1024 if (nobj_indexed > 0) {
1025 p_indexed = (nobj_indexed * 100) / nobj_total;
1026 if (p_indexed != a->last_p_indexed) {
1027 a->last_p_indexed = p_indexed;
1032 if (nobj_resolved > 0) {
1033 p_resolved = (nobj_resolved * 100) /
1034 (nobj_total - nobj_loose);
1035 if (p_resolved != a->last_p_resolved) {
1036 a->last_p_resolved = p_resolved;
1044 if (print_size || print_indexed || print_resolved)
1047 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1049 printf("; indexing %d%%", p_indexed);
1051 printf("; resolving deltas %d%%", p_resolved);
1052 if (print_size || print_indexed || print_resolved)
1058 static const struct got_error *
1059 create_symref(const char *refname, struct got_reference *target_ref,
1060 int verbosity, struct got_repository *repo)
1062 const struct got_error *err;
1063 struct got_reference *head_symref;
1065 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1069 err = got_ref_write(head_symref, repo);
1070 if (err == NULL && verbosity > 0) {
1071 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1072 got_ref_get_name(target_ref));
1074 got_ref_close(head_symref);
1078 static const struct got_error *
1079 list_remote_refs(struct got_pathlist_head *symrefs,
1080 struct got_pathlist_head *refs)
1082 const struct got_error *err;
1083 struct got_pathlist_entry *pe;
1085 TAILQ_FOREACH(pe, symrefs, entry) {
1086 const char *refname = pe->path;
1087 const char *targetref = pe->data;
1089 printf("%s: %s\n", refname, targetref);
1092 TAILQ_FOREACH(pe, refs, entry) {
1093 const char *refname = pe->path;
1094 struct got_object_id *id = pe->data;
1097 err = got_object_id_str(&id_str, id);
1100 printf("%s: %s\n", refname, id_str);
1107 static const struct got_error *
1108 create_ref(const char *refname, struct got_object_id *id,
1109 int verbosity, struct got_repository *repo)
1111 const struct got_error *err = NULL;
1112 struct got_reference *ref;
1115 err = got_object_id_str(&id_str, id);
1119 err = got_ref_alloc(&ref, refname, id);
1123 err = got_ref_write(ref, repo);
1126 if (err == NULL && verbosity >= 0)
1127 printf("Created reference %s: %s\n", refname, id_str);
1134 match_wanted_ref(const char *refname, const char *wanted_ref)
1136 if (strncmp(refname, "refs/", 5) != 0)
1141 * Prevent fetching of references that won't make any
1142 * sense outside of the remote repository's context.
1144 if (strncmp(refname, "got/", 4) == 0)
1146 if (strncmp(refname, "remotes/", 8) == 0)
1149 if (strncmp(wanted_ref, "refs/", 5) == 0)
1152 /* Allow prefix match. */
1153 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1156 /* Allow exact match. */
1157 return (strcmp(refname, wanted_ref) == 0);
1161 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1163 struct got_pathlist_entry *pe;
1165 TAILQ_FOREACH(pe, wanted_refs, entry) {
1166 if (match_wanted_ref(refname, pe->path))
1173 static const struct got_error *
1174 create_wanted_ref(const char *refname, struct got_object_id *id,
1175 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1177 const struct got_error *err;
1178 char *remote_refname;
1180 if (strncmp("refs/", refname, 5) == 0)
1183 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1184 remote_repo_name, refname) == -1)
1185 return got_error_from_errno("asprintf");
1187 err = create_ref(remote_refname, id, verbosity, repo);
1188 free(remote_refname);
1192 static const struct got_error *
1193 create_gotconfig(const char *proto, const char *host, const char *port,
1194 const char *remote_repo_path, const char *default_branch,
1195 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1196 struct got_pathlist_head *wanted_refs, int mirror_references,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 char *gotconfig_path = NULL;
1201 char *gotconfig = NULL;
1202 FILE *gotconfig_file = NULL;
1203 const char *branchname = NULL;
1204 char *branches = NULL, *refs = NULL;
1207 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1208 struct got_pathlist_entry *pe;
1209 TAILQ_FOREACH(pe, wanted_branches, entry) {
1211 branchname = pe->path;
1212 if (strncmp(branchname, "refs/heads/", 11) == 0)
1214 if (asprintf(&s, "%s\"%s\" ",
1215 branches ? branches : "", branchname) == -1) {
1216 err = got_error_from_errno("asprintf");
1222 } else if (!fetch_all_branches && default_branch) {
1223 branchname = default_branch;
1224 if (strncmp(branchname, "refs/heads/", 11) == 0)
1226 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1227 err = got_error_from_errno("asprintf");
1231 if (!TAILQ_EMPTY(wanted_refs)) {
1232 struct got_pathlist_entry *pe;
1233 TAILQ_FOREACH(pe, wanted_refs, entry) {
1235 const char *refname = pe->path;
1236 if (strncmp(refname, "refs/", 5) == 0)
1238 if (asprintf(&s, "%s\"%s\" ",
1239 refs ? refs : "", refname) == -1) {
1240 err = got_error_from_errno("asprintf");
1248 /* Create got.conf(5). */
1249 gotconfig_path = got_repo_get_path_gotconfig(repo);
1250 if (gotconfig_path == NULL) {
1251 err = got_error_from_errno("got_repo_get_path_gotconfig");
1254 gotconfig_file = fopen(gotconfig_path, "ae");
1255 if (gotconfig_file == NULL) {
1256 err = got_error_from_errno2("fopen", gotconfig_path);
1259 if (asprintf(&gotconfig,
1264 "\trepository \"%s\"\n"
1270 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1271 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1272 remote_repo_path, branches ? "\tbranch { " : "",
1273 branches ? branches : "", branches ? "}\n" : "",
1274 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1275 mirror_references ? "\tmirror-references yes\n" : "",
1276 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1277 err = got_error_from_errno("asprintf");
1280 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1281 if (n != strlen(gotconfig)) {
1282 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1287 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1288 err = got_error_from_errno2("fclose", gotconfig_path);
1289 free(gotconfig_path);
1294 static const struct got_error *
1295 create_gitconfig(const char *git_url, const char *default_branch,
1296 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1297 struct got_pathlist_head *wanted_refs, int mirror_references,
1298 struct got_repository *repo)
1300 const struct got_error *err = NULL;
1301 char *gitconfig_path = NULL;
1302 char *gitconfig = NULL;
1303 FILE *gitconfig_file = NULL;
1304 char *branches = NULL, *refs = NULL;
1305 const char *branchname;
1308 /* Create a config file Git can understand. */
1309 gitconfig_path = got_repo_get_path_gitconfig(repo);
1310 if (gitconfig_path == NULL) {
1311 err = got_error_from_errno("got_repo_get_path_gitconfig");
1314 gitconfig_file = fopen(gitconfig_path, "ae");
1315 if (gitconfig_file == NULL) {
1316 err = got_error_from_errno2("fopen", gitconfig_path);
1319 if (fetch_all_branches) {
1320 if (mirror_references) {
1321 if (asprintf(&branches,
1322 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1323 err = got_error_from_errno("asprintf");
1326 } else if (asprintf(&branches,
1327 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1328 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1329 err = got_error_from_errno("asprintf");
1332 } else if (!TAILQ_EMPTY(wanted_branches)) {
1333 struct got_pathlist_entry *pe;
1334 TAILQ_FOREACH(pe, wanted_branches, entry) {
1336 branchname = pe->path;
1337 if (strncmp(branchname, "refs/heads/", 11) == 0)
1339 if (mirror_references) {
1341 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1342 branches ? branches : "",
1343 branchname, branchname) == -1) {
1344 err = got_error_from_errno("asprintf");
1347 } else if (asprintf(&s,
1348 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1349 branches ? branches : "",
1350 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1351 branchname) == -1) {
1352 err = got_error_from_errno("asprintf");
1360 * If the server specified a default branch, use just that one.
1361 * Otherwise fall back to fetching all branches on next fetch.
1363 if (default_branch) {
1364 branchname = default_branch;
1365 if (strncmp(branchname, "refs/heads/", 11) == 0)
1368 branchname = "*"; /* fall back to all branches */
1369 if (mirror_references) {
1370 if (asprintf(&branches,
1371 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1372 branchname, branchname) == -1) {
1373 err = got_error_from_errno("asprintf");
1376 } else if (asprintf(&branches,
1377 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1378 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1379 branchname) == -1) {
1380 err = got_error_from_errno("asprintf");
1384 if (!TAILQ_EMPTY(wanted_refs)) {
1385 struct got_pathlist_entry *pe;
1386 TAILQ_FOREACH(pe, wanted_refs, entry) {
1388 const char *refname = pe->path;
1389 if (strncmp(refname, "refs/", 5) == 0)
1391 if (mirror_references) {
1393 "%s\tfetch = refs/%s:refs/%s\n",
1394 refs ? refs : "", refname, refname) == -1) {
1395 err = got_error_from_errno("asprintf");
1398 } else if (asprintf(&s,
1399 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1401 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1403 err = got_error_from_errno("asprintf");
1411 if (asprintf(&gitconfig,
1416 "\tfetch = refs/tags/*:refs/tags/*\n",
1417 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1418 refs ? refs : "") == -1) {
1419 err = got_error_from_errno("asprintf");
1422 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1423 if (n != strlen(gitconfig)) {
1424 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1428 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1429 err = got_error_from_errno2("fclose", gitconfig_path);
1430 free(gitconfig_path);
1435 static const struct got_error *
1436 create_config_files(const char *proto, const char *host, const char *port,
1437 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1438 int mirror_references, struct got_pathlist_head *symrefs,
1439 struct got_pathlist_head *wanted_branches,
1440 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 const char *default_branch = NULL;
1444 struct got_pathlist_entry *pe;
1447 * If we asked for a set of wanted branches then use the first
1450 if (!TAILQ_EMPTY(wanted_branches)) {
1451 pe = TAILQ_FIRST(wanted_branches);
1452 default_branch = pe->path;
1454 /* First HEAD ref listed by server is the default branch. */
1455 TAILQ_FOREACH(pe, symrefs, entry) {
1456 const char *refname = pe->path;
1457 const char *target = pe->data;
1459 if (strcmp(refname, GOT_REF_HEAD) != 0)
1462 default_branch = target;
1467 /* Create got.conf(5). */
1468 err = create_gotconfig(proto, host, port, remote_repo_path,
1469 default_branch, fetch_all_branches, wanted_branches,
1470 wanted_refs, mirror_references, repo);
1474 /* Create a config file Git can understand. */
1475 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1476 wanted_branches, wanted_refs, mirror_references, repo);
1479 static const struct got_error *
1480 cmd_clone(int argc, char *argv[])
1482 const struct got_error *error = NULL;
1483 const char *uri, *dirname;
1484 char *proto, *host, *port, *repo_name, *server_path;
1485 char *default_destdir = NULL, *id_str = NULL;
1486 const char *repo_path;
1487 struct got_repository *repo = NULL;
1488 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1489 struct got_pathlist_entry *pe;
1490 struct got_object_id *pack_hash = NULL;
1491 int ch, fetchfd = -1, fetchstatus;
1492 pid_t fetchpid = -1;
1493 struct got_fetch_progress_arg fpa;
1494 char *git_url = NULL;
1495 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1496 int list_refs_only = 0;
1497 int *pack_fds = NULL;
1500 TAILQ_INIT(&symrefs);
1501 TAILQ_INIT(&wanted_branches);
1502 TAILQ_INIT(&wanted_refs);
1504 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1507 fetch_all_branches = 1;
1510 error = got_pathlist_append(&wanted_branches,
1519 mirror_references = 1;
1524 else if (verbosity < 3)
1531 error = got_pathlist_append(&wanted_refs,
1544 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1545 option_conflict('a', 'b');
1546 if (list_refs_only) {
1547 if (!TAILQ_EMPTY(&wanted_branches))
1548 option_conflict('l', 'b');
1549 if (fetch_all_branches)
1550 option_conflict('l', 'a');
1551 if (mirror_references)
1552 option_conflict('l', 'm');
1553 if (!TAILQ_EMPTY(&wanted_refs))
1554 option_conflict('l', 'R');
1566 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1571 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1572 host, port ? ":" : "", port ? port : "",
1573 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1574 error = got_error_from_errno("asprintf");
1578 if (strcmp(proto, "git") == 0) {
1580 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1581 "sendfd dns inet unveil", NULL) == -1)
1584 } else if (strcmp(proto, "git+ssh") == 0 ||
1585 strcmp(proto, "ssh") == 0) {
1587 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1588 "sendfd unveil", NULL) == -1)
1591 } else if (strcmp(proto, "http") == 0 ||
1592 strcmp(proto, "git+http") == 0) {
1593 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1596 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1599 if (dirname == NULL) {
1600 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1601 error = got_error_from_errno("asprintf");
1604 repo_path = default_destdir;
1606 repo_path = dirname;
1608 if (!list_refs_only) {
1609 error = got_path_mkdir(repo_path);
1611 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1612 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1614 if (!got_path_dir_is_empty(repo_path)) {
1615 error = got_error_path(repo_path,
1616 GOT_ERR_DIR_NOT_EMPTY);
1621 error = got_dial_apply_unveil(proto);
1625 error = apply_unveil(repo_path, 0, NULL);
1630 printf("Connecting to %s%s%s\n", host,
1631 port ? ":" : "", port ? port : "");
1633 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1634 server_path, verbosity);
1638 if (!list_refs_only) {
1639 error = got_repo_init(repo_path);
1642 error = got_repo_pack_fds_open(&pack_fds);
1645 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1650 fpa.last_scaled_size[0] = '\0';
1651 fpa.last_p_indexed = -1;
1652 fpa.last_p_resolved = -1;
1653 fpa.verbosity = verbosity;
1654 fpa.create_configs = 1;
1655 fpa.configs_created = 0;
1657 fpa.config_info.symrefs = &symrefs;
1658 fpa.config_info.wanted_branches = &wanted_branches;
1659 fpa.config_info.wanted_refs = &wanted_refs;
1660 fpa.config_info.proto = proto;
1661 fpa.config_info.host = host;
1662 fpa.config_info.port = port;
1663 fpa.config_info.remote_repo_path = server_path;
1664 fpa.config_info.git_url = git_url;
1665 fpa.config_info.fetch_all_branches = fetch_all_branches;
1666 fpa.config_info.mirror_references = mirror_references;
1667 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1668 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1669 fetch_all_branches, &wanted_branches, &wanted_refs,
1670 list_refs_only, verbosity, fetchfd, repo,
1671 fetch_progress, &fpa);
1675 if (list_refs_only) {
1676 error = list_remote_refs(&symrefs, &refs);
1680 if (pack_hash == NULL) {
1681 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1682 "server sent an empty pack file");
1685 error = got_object_id_str(&id_str, pack_hash);
1689 printf("\nFetched %s.pack\n", id_str);
1692 /* Set up references provided with the pack file. */
1693 TAILQ_FOREACH(pe, &refs, entry) {
1694 const char *refname = pe->path;
1695 struct got_object_id *id = pe->data;
1696 char *remote_refname;
1698 if (is_wanted_ref(&wanted_refs, refname) &&
1699 !mirror_references) {
1700 error = create_wanted_ref(refname, id,
1701 GOT_FETCH_DEFAULT_REMOTE_NAME,
1702 verbosity - 1, repo);
1708 error = create_ref(refname, id, verbosity - 1, repo);
1712 if (mirror_references)
1715 if (strncmp("refs/heads/", refname, 11) != 0)
1718 if (asprintf(&remote_refname,
1719 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1720 refname + 11) == -1) {
1721 error = got_error_from_errno("asprintf");
1724 error = create_ref(remote_refname, id, verbosity - 1, repo);
1725 free(remote_refname);
1730 /* Set the HEAD reference if the server provided one. */
1731 TAILQ_FOREACH(pe, &symrefs, entry) {
1732 struct got_reference *target_ref;
1733 const char *refname = pe->path;
1734 const char *target = pe->data;
1735 char *remote_refname = NULL, *remote_target = NULL;
1737 if (strcmp(refname, GOT_REF_HEAD) != 0)
1740 error = got_ref_open(&target_ref, repo, target, 0);
1742 if (error->code == GOT_ERR_NOT_REF) {
1749 error = create_symref(refname, target_ref, verbosity, repo);
1750 got_ref_close(target_ref);
1754 if (mirror_references)
1757 if (strncmp("refs/heads/", target, 11) != 0)
1760 if (asprintf(&remote_refname,
1761 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1763 error = got_error_from_errno("asprintf");
1766 if (asprintf(&remote_target,
1767 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1768 target + 11) == -1) {
1769 error = got_error_from_errno("asprintf");
1770 free(remote_refname);
1773 error = got_ref_open(&target_ref, repo, remote_target, 0);
1775 free(remote_refname);
1776 free(remote_target);
1777 if (error->code == GOT_ERR_NOT_REF) {
1783 error = create_symref(remote_refname, target_ref,
1784 verbosity - 1, repo);
1785 free(remote_refname);
1786 free(remote_target);
1787 got_ref_close(target_ref);
1793 * We failed to set the HEAD reference. If we asked for
1794 * a set of wanted branches use the first of one of those
1795 * which could be fetched instead.
1797 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1798 const char *target = pe->path;
1799 struct got_reference *target_ref;
1801 error = got_ref_open(&target_ref, repo, target, 0);
1803 if (error->code == GOT_ERR_NOT_REF) {
1810 error = create_symref(GOT_REF_HEAD, target_ref,
1812 got_ref_close(target_ref);
1820 printf("Created %s repository '%s'\n",
1821 mirror_references ? "mirrored" : "cloned", repo_path);
1824 const struct got_error *pack_err =
1825 got_repo_pack_fds_close(pack_fds);
1830 if (kill(fetchpid, SIGTERM) == -1)
1831 error = got_error_from_errno("kill");
1832 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1833 error = got_error_from_errno("waitpid");
1835 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1836 error = got_error_from_errno("close");
1838 const struct got_error *close_err = got_repo_close(repo);
1842 TAILQ_FOREACH(pe, &refs, entry) {
1843 free((void *)pe->path);
1846 got_pathlist_free(&refs);
1847 TAILQ_FOREACH(pe, &symrefs, entry) {
1848 free((void *)pe->path);
1851 got_pathlist_free(&symrefs);
1852 got_pathlist_free(&wanted_branches);
1853 got_pathlist_free(&wanted_refs);
1860 free(default_destdir);
1865 static const struct got_error *
1866 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1867 int replace_tags, int verbosity, struct got_repository *repo)
1869 const struct got_error *err = NULL;
1870 char *new_id_str = NULL;
1871 struct got_object_id *old_id = NULL;
1873 err = got_object_id_str(&new_id_str, new_id);
1877 if (!replace_tags &&
1878 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1879 err = got_ref_resolve(&old_id, repo, ref);
1882 if (got_object_id_cmp(old_id, new_id) == 0)
1884 if (verbosity >= 0) {
1885 printf("Rejecting update of existing tag %s: %s\n",
1886 got_ref_get_name(ref), new_id_str);
1891 if (got_ref_is_symbolic(ref)) {
1892 if (verbosity >= 0) {
1893 printf("Replacing reference %s: %s\n",
1894 got_ref_get_name(ref),
1895 got_ref_get_symref_target(ref));
1897 err = got_ref_change_symref_to_ref(ref, new_id);
1900 err = got_ref_write(ref, repo);
1904 err = got_ref_resolve(&old_id, repo, ref);
1907 if (got_object_id_cmp(old_id, new_id) == 0)
1910 err = got_ref_change_ref(ref, new_id);
1913 err = got_ref_write(ref, repo);
1919 printf("Updated %s: %s\n", got_ref_get_name(ref),
1927 static const struct got_error *
1928 update_symref(const char *refname, struct got_reference *target_ref,
1929 int verbosity, struct got_repository *repo)
1931 const struct got_error *err = NULL, *unlock_err;
1932 struct got_reference *symref;
1933 int symref_is_locked = 0;
1935 err = got_ref_open(&symref, repo, refname, 1);
1937 if (err->code != GOT_ERR_NOT_REF)
1939 err = got_ref_alloc_symref(&symref, refname, target_ref);
1943 err = got_ref_write(symref, repo);
1948 printf("Created reference %s: %s\n",
1949 got_ref_get_name(symref),
1950 got_ref_get_symref_target(symref));
1952 symref_is_locked = 1;
1954 if (strcmp(got_ref_get_symref_target(symref),
1955 got_ref_get_name(target_ref)) == 0)
1958 err = got_ref_change_symref(symref,
1959 got_ref_get_name(target_ref));
1963 err = got_ref_write(symref, repo);
1968 printf("Updated %s: %s\n", got_ref_get_name(symref),
1969 got_ref_get_symref_target(symref));
1973 if (symref_is_locked) {
1974 unlock_err = got_ref_unlock(symref);
1975 if (unlock_err && err == NULL)
1978 got_ref_close(symref);
1985 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1986 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1987 "[remote-repository-name]\n",
1992 static const struct got_error *
1993 delete_missing_ref(struct got_reference *ref,
1994 int verbosity, struct got_repository *repo)
1996 const struct got_error *err = NULL;
1997 struct got_object_id *id = NULL;
1998 char *id_str = NULL;
2000 if (got_ref_is_symbolic(ref)) {
2001 err = got_ref_delete(ref, repo);
2004 if (verbosity >= 0) {
2005 printf("Deleted %s: %s\n",
2006 got_ref_get_name(ref),
2007 got_ref_get_symref_target(ref));
2010 err = got_ref_resolve(&id, repo, ref);
2013 err = got_object_id_str(&id_str, id);
2017 err = got_ref_delete(ref, repo);
2020 if (verbosity >= 0) {
2021 printf("Deleted %s: %s\n",
2022 got_ref_get_name(ref), id_str);
2031 static const struct got_error *
2032 delete_missing_refs(struct got_pathlist_head *their_refs,
2033 struct got_pathlist_head *their_symrefs,
2034 const struct got_remote_repo *remote,
2035 int verbosity, struct got_repository *repo)
2037 const struct got_error *err = NULL, *unlock_err;
2038 struct got_reflist_head my_refs;
2039 struct got_reflist_entry *re;
2040 struct got_pathlist_entry *pe;
2041 char *remote_namespace = NULL;
2042 char *local_refname = NULL;
2044 TAILQ_INIT(&my_refs);
2046 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2048 return got_error_from_errno("asprintf");
2050 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2054 TAILQ_FOREACH(re, &my_refs, entry) {
2055 const char *refname = got_ref_get_name(re->ref);
2056 const char *their_refname;
2058 if (remote->mirror_references) {
2059 their_refname = refname;
2061 if (strncmp(refname, remote_namespace,
2062 strlen(remote_namespace)) == 0) {
2063 if (strcmp(refname + strlen(remote_namespace),
2066 if (asprintf(&local_refname, "refs/heads/%s",
2067 refname + strlen(remote_namespace)) == -1) {
2068 err = got_error_from_errno("asprintf");
2071 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2074 their_refname = local_refname;
2077 TAILQ_FOREACH(pe, their_refs, entry) {
2078 if (strcmp(their_refname, pe->path) == 0)
2084 TAILQ_FOREACH(pe, their_symrefs, entry) {
2085 if (strcmp(their_refname, pe->path) == 0)
2091 err = delete_missing_ref(re->ref, verbosity, repo);
2095 if (local_refname) {
2096 struct got_reference *ref;
2097 err = got_ref_open(&ref, repo, local_refname, 1);
2099 if (err->code != GOT_ERR_NOT_REF)
2101 free(local_refname);
2102 local_refname = NULL;
2105 err = delete_missing_ref(ref, verbosity, repo);
2108 unlock_err = got_ref_unlock(ref);
2110 if (unlock_err && err == NULL) {
2115 free(local_refname);
2116 local_refname = NULL;
2120 free(remote_namespace);
2121 free(local_refname);
2125 static const struct got_error *
2126 update_wanted_ref(const char *refname, struct got_object_id *id,
2127 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2129 const struct got_error *err, *unlock_err;
2130 char *remote_refname;
2131 struct got_reference *ref;
2133 if (strncmp("refs/", refname, 5) == 0)
2136 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2137 remote_repo_name, refname) == -1)
2138 return got_error_from_errno("asprintf");
2140 err = got_ref_open(&ref, repo, remote_refname, 1);
2142 if (err->code != GOT_ERR_NOT_REF)
2144 err = create_ref(remote_refname, id, verbosity, repo);
2146 err = update_ref(ref, id, 0, verbosity, repo);
2147 unlock_err = got_ref_unlock(ref);
2148 if (unlock_err && err == NULL)
2153 free(remote_refname);
2157 static const struct got_error *
2158 delete_ref(struct got_repository *repo, struct got_reference *ref)
2160 const struct got_error *err = NULL;
2161 struct got_object_id *id = NULL;
2162 char *id_str = NULL;
2165 if (got_ref_is_symbolic(ref)) {
2166 target = got_ref_get_symref_target(ref);
2168 err = got_ref_resolve(&id, repo, ref);
2171 err = got_object_id_str(&id_str, id);
2177 err = got_ref_delete(ref, repo);
2181 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2188 static const struct got_error *
2189 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2191 const struct got_error *err = NULL;
2192 struct got_reflist_head refs;
2193 struct got_reflist_entry *re;
2198 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2199 err = got_error_from_errno("asprintf");
2202 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2206 TAILQ_FOREACH(re, &refs, entry)
2207 delete_ref(repo, re->ref);
2209 got_ref_list_free(&refs);
2213 static const struct got_error *
2214 cmd_fetch(int argc, char *argv[])
2216 const struct got_error *error = NULL, *unlock_err;
2217 char *cwd = NULL, *repo_path = NULL;
2218 const char *remote_name;
2219 char *proto = NULL, *host = NULL, *port = NULL;
2220 char *repo_name = NULL, *server_path = NULL;
2221 const struct got_remote_repo *remotes, *remote = NULL;
2223 char *id_str = NULL;
2224 struct got_repository *repo = NULL;
2225 struct got_worktree *worktree = NULL;
2226 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2227 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2228 struct got_pathlist_entry *pe;
2229 struct got_object_id *pack_hash = NULL;
2230 int i, ch, fetchfd = -1, fetchstatus;
2231 pid_t fetchpid = -1;
2232 struct got_fetch_progress_arg fpa;
2233 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2234 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2235 int *pack_fds = NULL;
2238 TAILQ_INIT(&symrefs);
2239 TAILQ_INIT(&wanted_branches);
2240 TAILQ_INIT(&wanted_refs);
2242 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2245 fetch_all_branches = 1;
2248 error = got_pathlist_append(&wanted_branches,
2260 repo_path = realpath(optarg, NULL);
2261 if (repo_path == NULL)
2262 return got_error_from_errno2("realpath",
2264 got_path_strip_trailing_slashes(repo_path);
2272 else if (verbosity < 3)
2279 error = got_pathlist_append(&wanted_refs,
2295 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2296 option_conflict('a', 'b');
2297 if (list_refs_only) {
2298 if (!TAILQ_EMPTY(&wanted_branches))
2299 option_conflict('l', 'b');
2300 if (fetch_all_branches)
2301 option_conflict('l', 'a');
2303 option_conflict('l', 'd');
2305 option_conflict('l', 'X');
2307 if (delete_remote) {
2308 if (fetch_all_branches)
2309 option_conflict('X', 'a');
2310 if (!TAILQ_EMPTY(&wanted_branches))
2311 option_conflict('X', 'b');
2313 option_conflict('X', 'd');
2315 option_conflict('X', 't');
2316 if (!TAILQ_EMPTY(&wanted_refs))
2317 option_conflict('X', 'R');
2322 errx(1, "-X option requires a remote name");
2323 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2324 } else if (argc == 1)
2325 remote_name = argv[0];
2329 cwd = getcwd(NULL, 0);
2331 error = got_error_from_errno("getcwd");
2335 error = got_repo_pack_fds_open(&pack_fds);
2339 if (repo_path == NULL) {
2340 error = got_worktree_open(&worktree, cwd);
2341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2347 strdup(got_worktree_get_repo_path(worktree));
2348 if (repo_path == NULL)
2349 error = got_error_from_errno("strdup");
2353 repo_path = strdup(cwd);
2354 if (repo_path == NULL) {
2355 error = got_error_from_errno("strdup");
2361 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2365 if (delete_remote) {
2366 error = delete_refs_for_remote(repo, remote_name);
2367 goto done; /* nothing else to do */
2371 worktree_conf = got_worktree_get_gotconfig(worktree);
2372 if (worktree_conf) {
2373 got_gotconfig_get_remotes(&nremotes, &remotes,
2375 for (i = 0; i < nremotes; i++) {
2376 if (strcmp(remotes[i].name, remote_name) == 0) {
2377 remote = &remotes[i];
2383 if (remote == NULL) {
2384 repo_conf = got_repo_get_gotconfig(repo);
2386 got_gotconfig_get_remotes(&nremotes, &remotes,
2388 for (i = 0; i < nremotes; i++) {
2389 if (strcmp(remotes[i].name, remote_name) == 0) {
2390 remote = &remotes[i];
2396 if (remote == NULL) {
2397 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2398 for (i = 0; i < nremotes; i++) {
2399 if (strcmp(remotes[i].name, remote_name) == 0) {
2400 remote = &remotes[i];
2405 if (remote == NULL) {
2406 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2410 if (TAILQ_EMPTY(&wanted_branches)) {
2411 if (!fetch_all_branches)
2412 fetch_all_branches = remote->fetch_all_branches;
2413 for (i = 0; i < remote->nfetch_branches; i++) {
2414 got_pathlist_append(&wanted_branches,
2415 remote->fetch_branches[i], NULL);
2418 if (TAILQ_EMPTY(&wanted_refs)) {
2419 for (i = 0; i < remote->nfetch_refs; i++) {
2420 got_pathlist_append(&wanted_refs,
2421 remote->fetch_refs[i], NULL);
2425 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2426 &repo_name, remote->fetch_url);
2430 if (strcmp(proto, "git") == 0) {
2432 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2433 "sendfd dns inet unveil", NULL) == -1)
2436 } else if (strcmp(proto, "git+ssh") == 0 ||
2437 strcmp(proto, "ssh") == 0) {
2439 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2440 "sendfd unveil", NULL) == -1)
2443 } else if (strcmp(proto, "http") == 0 ||
2444 strcmp(proto, "git+http") == 0) {
2445 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2448 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2452 error = got_dial_apply_unveil(proto);
2456 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2461 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2462 port ? ":" : "", port ? port : "");
2464 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2465 server_path, verbosity);
2469 fpa.last_scaled_size[0] = '\0';
2470 fpa.last_p_indexed = -1;
2471 fpa.last_p_resolved = -1;
2472 fpa.verbosity = verbosity;
2474 fpa.create_configs = 0;
2475 fpa.configs_created = 0;
2476 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2477 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2478 remote->mirror_references, fetch_all_branches, &wanted_branches,
2479 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2480 fetch_progress, &fpa);
2484 if (list_refs_only) {
2485 error = list_remote_refs(&symrefs, &refs);
2489 if (pack_hash == NULL) {
2491 printf("Already up-to-date\n");
2492 } else if (verbosity >= 0) {
2493 error = got_object_id_str(&id_str, pack_hash);
2496 printf("\nFetched %s.pack\n", id_str);
2501 /* Update references provided with the pack file. */
2502 TAILQ_FOREACH(pe, &refs, entry) {
2503 const char *refname = pe->path;
2504 struct got_object_id *id = pe->data;
2505 struct got_reference *ref;
2506 char *remote_refname;
2508 if (is_wanted_ref(&wanted_refs, refname) &&
2509 !remote->mirror_references) {
2510 error = update_wanted_ref(refname, id,
2511 remote->name, verbosity, repo);
2517 if (remote->mirror_references ||
2518 strncmp("refs/tags/", refname, 10) == 0) {
2519 error = got_ref_open(&ref, repo, refname, 1);
2521 if (error->code != GOT_ERR_NOT_REF)
2523 error = create_ref(refname, id, verbosity,
2528 error = update_ref(ref, id, replace_tags,
2530 unlock_err = got_ref_unlock(ref);
2531 if (unlock_err && error == NULL)
2537 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2538 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2539 remote_name, refname + 11) == -1) {
2540 error = got_error_from_errno("asprintf");
2544 error = got_ref_open(&ref, repo, remote_refname, 1);
2546 if (error->code != GOT_ERR_NOT_REF)
2548 error = create_ref(remote_refname, id,
2553 error = update_ref(ref, id, replace_tags,
2555 unlock_err = got_ref_unlock(ref);
2556 if (unlock_err && error == NULL)
2563 /* Also create a local branch if none exists yet. */
2564 error = got_ref_open(&ref, repo, refname, 1);
2566 if (error->code != GOT_ERR_NOT_REF)
2568 error = create_ref(refname, id, verbosity,
2573 unlock_err = got_ref_unlock(ref);
2574 if (unlock_err && error == NULL)
2581 error = delete_missing_refs(&refs, &symrefs, remote,
2587 if (!remote->mirror_references) {
2588 /* Update remote HEAD reference if the server provided one. */
2589 TAILQ_FOREACH(pe, &symrefs, entry) {
2590 struct got_reference *target_ref;
2591 const char *refname = pe->path;
2592 const char *target = pe->data;
2593 char *remote_refname = NULL, *remote_target = NULL;
2595 if (strcmp(refname, GOT_REF_HEAD) != 0)
2598 if (strncmp("refs/heads/", target, 11) != 0)
2601 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2602 remote->name, refname) == -1) {
2603 error = got_error_from_errno("asprintf");
2606 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2607 remote->name, target + 11) == -1) {
2608 error = got_error_from_errno("asprintf");
2609 free(remote_refname);
2613 error = got_ref_open(&target_ref, repo, remote_target,
2616 free(remote_refname);
2617 free(remote_target);
2618 if (error->code == GOT_ERR_NOT_REF) {
2624 error = update_symref(remote_refname, target_ref,
2626 free(remote_refname);
2627 free(remote_target);
2628 got_ref_close(target_ref);
2635 if (kill(fetchpid, SIGTERM) == -1)
2636 error = got_error_from_errno("kill");
2637 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2638 error = got_error_from_errno("waitpid");
2640 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2641 error = got_error_from_errno("close");
2643 const struct got_error *close_err = got_repo_close(repo);
2648 got_worktree_close(worktree);
2650 const struct got_error *pack_err =
2651 got_repo_pack_fds_close(pack_fds);
2655 TAILQ_FOREACH(pe, &refs, entry) {
2656 free((void *)pe->path);
2659 got_pathlist_free(&refs);
2660 TAILQ_FOREACH(pe, &symrefs, entry) {
2661 free((void *)pe->path);
2664 got_pathlist_free(&symrefs);
2665 got_pathlist_free(&wanted_branches);
2666 got_pathlist_free(&wanted_refs);
2681 usage_checkout(void)
2683 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2684 "[-p prefix] [-q] repository-path [worktree-path]\n",
2690 show_worktree_base_ref_warning(void)
2692 fprintf(stderr, "%s: warning: could not create a reference "
2693 "to the work tree's base commit; the commit could be "
2694 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2695 "repository writable and running 'got update' will prevent this\n",
2699 struct got_checkout_progress_arg {
2700 const char *worktree_path;
2701 int had_base_commit_ref_error;
2705 static const struct got_error *
2706 checkout_progress(void *arg, unsigned char status, const char *path)
2708 struct got_checkout_progress_arg *a = arg;
2710 /* Base commit bump happens silently. */
2711 if (status == GOT_STATUS_BUMP_BASE)
2714 if (status == GOT_STATUS_BASE_REF_ERR) {
2715 a->had_base_commit_ref_error = 1;
2719 while (path[0] == '/')
2722 if (a->verbosity >= 0)
2723 printf("%c %s/%s\n", status, a->worktree_path, path);
2728 static const struct got_error *
2729 check_cancelled(void *arg)
2731 if (sigint_received || sigpipe_received)
2732 return got_error(GOT_ERR_CANCELLED);
2736 static const struct got_error *
2737 check_linear_ancestry(struct got_object_id *commit_id,
2738 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2739 struct got_repository *repo)
2741 const struct got_error *err = NULL;
2742 struct got_object_id *yca_id;
2744 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2745 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2750 return got_error(GOT_ERR_ANCESTRY);
2753 * Require a straight line of history between the target commit
2754 * and the work tree's base commit.
2756 * Non-linear situations such as this require a rebase:
2758 * (commit) D F (base_commit)
2766 * 'got update' only handles linear cases:
2767 * Update forwards in time: A (base/yca) - B - C - D (commit)
2768 * Update backwards in time: D (base) - C - B - A (commit/yca)
2770 if (allow_forwards_in_time_only) {
2771 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2772 return got_error(GOT_ERR_ANCESTRY);
2773 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2774 got_object_id_cmp(base_commit_id, yca_id) != 0)
2775 return got_error(GOT_ERR_ANCESTRY);
2781 static const struct got_error *
2782 check_same_branch(struct got_object_id *commit_id,
2783 struct got_reference *head_ref, struct got_object_id *yca_id,
2784 struct got_repository *repo)
2786 const struct got_error *err = NULL;
2787 struct got_commit_graph *graph = NULL;
2788 struct got_object_id *head_commit_id = NULL;
2789 int is_same_branch = 0;
2791 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2795 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2799 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2804 err = got_commit_graph_open(&graph, "/", 1);
2808 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2809 check_cancelled, NULL);
2814 struct got_object_id *id;
2815 err = got_commit_graph_iter_next(&id, graph, repo,
2816 check_cancelled, NULL);
2818 if (err->code == GOT_ERR_ITER_COMPLETED)
2824 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2826 if (got_object_id_cmp(id, commit_id) == 0) {
2834 got_commit_graph_close(graph);
2835 free(head_commit_id);
2836 if (!err && !is_same_branch)
2837 err = got_error(GOT_ERR_ANCESTRY);
2841 static const struct got_error *
2842 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2844 static char msg[512];
2845 const char *branch_name;
2847 if (got_ref_is_symbolic(ref))
2848 branch_name = got_ref_get_symref_target(ref);
2850 branch_name = got_ref_get_name(ref);
2852 if (strncmp("refs/heads/", branch_name, 11) == 0)
2855 snprintf(msg, sizeof(msg),
2856 "target commit is not contained in branch '%s'; "
2857 "the branch to use must be specified with -b; "
2858 "if necessary a new branch can be created for "
2859 "this commit with 'got branch -c %s BRANCH_NAME'",
2860 branch_name, commit_id_str);
2862 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2865 static const struct got_error *
2866 cmd_checkout(int argc, char *argv[])
2868 const struct got_error *error = NULL;
2869 struct got_repository *repo = NULL;
2870 struct got_reference *head_ref = NULL, *ref = NULL;
2871 struct got_worktree *worktree = NULL;
2872 char *repo_path = NULL;
2873 char *worktree_path = NULL;
2874 const char *path_prefix = "";
2875 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2876 char *commit_id_str = NULL;
2877 struct got_object_id *commit_id = NULL;
2879 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2880 struct got_pathlist_head paths;
2881 struct got_checkout_progress_arg cpa;
2882 int *pack_fds = NULL;
2886 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2889 branch_name = optarg;
2892 commit_id_str = strdup(optarg);
2893 if (commit_id_str == NULL)
2894 return got_error_from_errno("strdup");
2900 path_prefix = optarg;
2915 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2916 "unveil", NULL) == -1)
2920 char *base, *dotgit;
2922 repo_path = realpath(argv[0], NULL);
2923 if (repo_path == NULL)
2924 return got_error_from_errno2("realpath", argv[0]);
2925 cwd = getcwd(NULL, 0);
2927 error = got_error_from_errno("getcwd");
2934 error = got_path_basename(&base, path);
2937 dotgit = strstr(base, ".git");
2940 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2941 error = got_error_from_errno("asprintf");
2946 } else if (argc == 2) {
2947 repo_path = realpath(argv[0], NULL);
2948 if (repo_path == NULL) {
2949 error = got_error_from_errno2("realpath", argv[0]);
2952 worktree_path = realpath(argv[1], NULL);
2953 if (worktree_path == NULL) {
2954 if (errno != ENOENT) {
2955 error = got_error_from_errno2("realpath",
2959 worktree_path = strdup(argv[1]);
2960 if (worktree_path == NULL) {
2961 error = got_error_from_errno("strdup");
2968 got_path_strip_trailing_slashes(repo_path);
2969 got_path_strip_trailing_slashes(worktree_path);
2971 error = got_repo_pack_fds_open(&pack_fds);
2975 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2979 /* Pre-create work tree path for unveil(2) */
2980 error = got_path_mkdir(worktree_path);
2982 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2983 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2985 if (!allow_nonempty &&
2986 !got_path_dir_is_empty(worktree_path)) {
2987 error = got_error_path(worktree_path,
2988 GOT_ERR_DIR_NOT_EMPTY);
2993 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2997 error = got_ref_open(&head_ref, repo, branch_name, 0);
3001 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3002 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3005 error = got_worktree_open(&worktree, worktree_path);
3009 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3013 if (!same_path_prefix) {
3014 error = got_error(GOT_ERR_PATH_PREFIX);
3018 if (commit_id_str) {
3019 struct got_reflist_head refs;
3021 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3025 error = got_repo_match_object_id(&commit_id, NULL,
3026 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3027 got_ref_list_free(&refs);
3030 error = check_linear_ancestry(commit_id,
3031 got_worktree_get_base_commit_id(worktree), 0, repo);
3032 if (error != NULL) {
3033 if (error->code == GOT_ERR_ANCESTRY) {
3034 error = checkout_ancestry_error(
3035 head_ref, commit_id_str);
3039 error = check_same_branch(commit_id, head_ref, NULL, repo);
3041 if (error->code == GOT_ERR_ANCESTRY) {
3042 error = checkout_ancestry_error(
3043 head_ref, commit_id_str);
3047 error = got_worktree_set_base_commit_id(worktree, repo,
3051 /* Expand potentially abbreviated commit ID string. */
3052 free(commit_id_str);
3053 error = got_object_id_str(&commit_id_str, commit_id);
3057 commit_id = got_object_id_dup(
3058 got_worktree_get_base_commit_id(worktree));
3059 if (commit_id == NULL) {
3060 error = got_error_from_errno("got_object_id_dup");
3063 error = got_object_id_str(&commit_id_str, commit_id);
3068 error = got_pathlist_append(&paths, "", NULL);
3071 cpa.worktree_path = worktree_path;
3072 cpa.had_base_commit_ref_error = 0;
3073 cpa.verbosity = verbosity;
3074 error = got_worktree_checkout_files(worktree, &paths, repo,
3075 checkout_progress, &cpa, check_cancelled, NULL);
3079 if (got_ref_is_symbolic(head_ref)) {
3080 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3083 refname = got_ref_get_name(ref);
3085 refname = got_ref_get_name(head_ref);
3086 printf("Checked out %s: %s\n", refname, commit_id_str);
3087 printf("Now shut up and hack\n");
3088 if (cpa.had_base_commit_ref_error)
3089 show_worktree_base_ref_warning();
3092 const struct got_error *pack_err =
3093 got_repo_pack_fds_close(pack_fds);
3098 got_ref_close(head_ref);
3101 got_pathlist_free(&paths);
3102 free(commit_id_str);
3105 free(worktree_path);
3110 struct got_update_progress_arg {
3122 print_update_progress_stats(struct got_update_progress_arg *upa)
3124 if (!upa->did_something)
3127 if (upa->conflicts > 0)
3128 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3129 if (upa->obstructed > 0)
3130 printf("File paths obstructed by a non-regular file: %d\n",
3132 if (upa->not_updated > 0)
3133 printf("Files not updated because of existing merge "
3134 "conflicts: %d\n", upa->not_updated);
3138 * The meaning of some status codes differs between merge-style operations and
3139 * update operations. For example, the ! status code means "file was missing"
3140 * if changes were merged into the work tree, and "missing file was restored"
3141 * if the work tree was updated. This function should be used by any operation
3142 * which merges changes into the work tree without updating the work tree.
3145 print_merge_progress_stats(struct got_update_progress_arg *upa)
3147 if (!upa->did_something)
3150 if (upa->conflicts > 0)
3151 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3152 if (upa->obstructed > 0)
3153 printf("File paths obstructed by a non-regular file: %d\n",
3155 if (upa->missing > 0)
3156 printf("Files which had incoming changes but could not be "
3157 "found in the work tree: %d\n", upa->missing);
3158 if (upa->not_deleted > 0)
3159 printf("Files not deleted due to differences in deleted "
3160 "content: %d\n", upa->not_deleted);
3161 if (upa->unversioned > 0)
3162 printf("Files not merged because an unversioned file was "
3163 "found in the work tree: %d\n", upa->unversioned);
3169 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3175 static const struct got_error *
3176 update_progress(void *arg, unsigned char status, const char *path)
3178 struct got_update_progress_arg *upa = arg;
3180 if (status == GOT_STATUS_EXISTS ||
3181 status == GOT_STATUS_BASE_REF_ERR)
3184 upa->did_something = 1;
3186 /* Base commit bump happens silently. */
3187 if (status == GOT_STATUS_BUMP_BASE)
3190 if (status == GOT_STATUS_CONFLICT)
3192 if (status == GOT_STATUS_OBSTRUCTED)
3194 if (status == GOT_STATUS_CANNOT_UPDATE)
3196 if (status == GOT_STATUS_MISSING)
3198 if (status == GOT_STATUS_CANNOT_DELETE)
3200 if (status == GOT_STATUS_UNVERSIONED)
3203 while (path[0] == '/')
3205 if (upa->verbosity >= 0)
3206 printf("%c %s\n", status, path);
3211 static const struct got_error *
3212 switch_head_ref(struct got_reference *head_ref,
3213 struct got_object_id *commit_id, struct got_worktree *worktree,
3214 struct got_repository *repo)
3216 const struct got_error *err = NULL;
3218 int ref_has_moved = 0;
3220 /* Trivial case: switching between two different references. */
3221 if (strcmp(got_ref_get_name(head_ref),
3222 got_worktree_get_head_ref_name(worktree)) != 0) {
3223 printf("Switching work tree from %s to %s\n",
3224 got_worktree_get_head_ref_name(worktree),
3225 got_ref_get_name(head_ref));
3226 return got_worktree_set_head_ref(worktree, head_ref);
3229 err = check_linear_ancestry(commit_id,
3230 got_worktree_get_base_commit_id(worktree), 0, repo);
3232 if (err->code != GOT_ERR_ANCESTRY)
3239 /* Switching to a rebased branch with the same reference name. */
3240 err = got_object_id_str(&base_id_str,
3241 got_worktree_get_base_commit_id(worktree));
3244 printf("Reference %s now points at a different branch\n",
3245 got_worktree_get_head_ref_name(worktree));
3246 printf("Switching work tree from %s to %s\n", base_id_str,
3247 got_worktree_get_head_ref_name(worktree));
3251 static const struct got_error *
3252 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3254 const struct got_error *err;
3257 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3261 return got_error(GOT_ERR_REBASING);
3263 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3267 return got_error(GOT_ERR_HISTEDIT_BUSY);
3272 static const struct got_error *
3273 check_merge_in_progress(struct got_worktree *worktree,
3274 struct got_repository *repo)
3276 const struct got_error *err;
3279 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3283 return got_error(GOT_ERR_MERGE_BUSY);
3288 static const struct got_error *
3289 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3290 char *argv[], struct got_worktree *worktree)
3292 const struct got_error *err = NULL;
3294 struct got_pathlist_entry *new;
3300 return got_error_from_errno("strdup");
3301 return got_pathlist_append(paths, path, NULL);
3304 for (i = 0; i < argc; i++) {
3305 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3308 err = got_pathlist_insert(&new, paths, path, NULL);
3309 if (err || new == NULL /* duplicate */) {
3319 static const struct got_error *
3320 wrap_not_worktree_error(const struct got_error *orig_err,
3321 const char *cmdname, const char *path)
3323 const struct got_error *err;
3324 struct got_repository *repo;
3325 static char msg[512];
3326 int *pack_fds = NULL;
3328 err = got_repo_pack_fds_open(&pack_fds);
3332 err = got_repo_open(&repo, path, NULL, pack_fds);
3336 snprintf(msg, sizeof(msg),
3337 "'got %s' needs a work tree in addition to a git repository\n"
3338 "Work trees can be checked out from this Git repository with "
3340 "The got(1) manual page contains more information.", cmdname);
3341 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3342 got_repo_close(repo);
3344 const struct got_error *pack_err =
3345 got_repo_pack_fds_close(pack_fds);
3352 static const struct got_error *
3353 cmd_update(int argc, char *argv[])
3355 const struct got_error *error = NULL;
3356 struct got_repository *repo = NULL;
3357 struct got_worktree *worktree = NULL;
3358 char *worktree_path = NULL;
3359 struct got_object_id *commit_id = NULL;
3360 char *commit_id_str = NULL;
3361 const char *branch_name = NULL;
3362 struct got_reference *head_ref = NULL;
3363 struct got_pathlist_head paths;
3364 struct got_pathlist_entry *pe;
3365 int ch, verbosity = 0;
3366 struct got_update_progress_arg upa;
3367 int *pack_fds = NULL;
3371 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3374 branch_name = optarg;
3377 commit_id_str = strdup(optarg);
3378 if (commit_id_str == NULL)
3379 return got_error_from_errno("strdup");
3394 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3395 "unveil", NULL) == -1)
3398 worktree_path = getcwd(NULL, 0);
3399 if (worktree_path == NULL) {
3400 error = got_error_from_errno("getcwd");
3404 error = got_repo_pack_fds_open(&pack_fds);
3408 error = got_worktree_open(&worktree, worktree_path);
3410 if (error->code == GOT_ERR_NOT_WORKTREE)
3411 error = wrap_not_worktree_error(error, "update",
3416 error = check_rebase_or_histedit_in_progress(worktree);
3420 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3425 error = apply_unveil(got_repo_get_path(repo), 0,
3426 got_worktree_get_root_path(worktree));
3430 error = check_merge_in_progress(worktree, repo);
3434 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3438 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3439 got_worktree_get_head_ref_name(worktree), 0);
3442 if (commit_id_str == NULL) {
3443 error = got_ref_resolve(&commit_id, repo, head_ref);
3446 error = got_object_id_str(&commit_id_str, commit_id);
3450 struct got_reflist_head refs;
3452 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3456 error = got_repo_match_object_id(&commit_id, NULL,
3457 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3458 got_ref_list_free(&refs);
3459 free(commit_id_str);
3460 commit_id_str = NULL;
3463 error = got_object_id_str(&commit_id_str, commit_id);
3469 struct got_object_id *head_commit_id;
3470 TAILQ_FOREACH(pe, &paths, entry) {
3471 if (pe->path_len == 0)
3473 error = got_error_msg(GOT_ERR_BAD_PATH,
3474 "switching between branches requires that "
3475 "the entire work tree gets updated");
3478 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3481 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3483 free(head_commit_id);
3486 error = check_same_branch(commit_id, head_ref, NULL, repo);
3489 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3493 error = check_linear_ancestry(commit_id,
3494 got_worktree_get_base_commit_id(worktree), 0, repo);
3495 if (error != NULL) {
3496 if (error->code == GOT_ERR_ANCESTRY)
3497 error = got_error(GOT_ERR_BRANCH_MOVED);
3500 error = check_same_branch(commit_id, head_ref, NULL, repo);
3505 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3507 error = got_worktree_set_base_commit_id(worktree, repo,
3513 memset(&upa, 0, sizeof(upa));
3514 upa.verbosity = verbosity;
3515 error = got_worktree_checkout_files(worktree, &paths, repo,
3516 update_progress, &upa, check_cancelled, NULL);
3520 if (upa.did_something) {
3521 printf("Updated to %s: %s\n",
3522 got_worktree_get_head_ref_name(worktree), commit_id_str);
3524 printf("Already up-to-date\n");
3526 print_update_progress_stats(&upa);
3529 const struct got_error *pack_err =
3530 got_repo_pack_fds_close(pack_fds);
3534 free(worktree_path);
3535 TAILQ_FOREACH(pe, &paths, entry)
3536 free((char *)pe->path);
3537 got_pathlist_free(&paths);
3539 free(commit_id_str);
3543 static const struct got_error *
3544 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3545 const char *path, int diff_context, int ignore_whitespace,
3546 int force_text_diff, struct got_repository *repo, FILE *outfile)
3548 const struct got_error *err = NULL;
3549 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3550 FILE *f1 = NULL, *f2 = NULL;
3551 int fd1 = -1, fd2 = -1;
3553 fd1 = got_opentempfd();
3555 return got_error_from_errno("got_opentempfd");
3556 fd2 = got_opentempfd();
3558 err = got_error_from_errno("got_opentempfd");
3563 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3567 f1 = got_opentemp();
3569 err = got_error_from_errno("got_opentemp");
3574 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3578 f2 = got_opentemp();
3580 err = got_error_from_errno("got_opentemp");
3584 while (path[0] == '/')
3586 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3587 diff_context, ignore_whitespace, force_text_diff, outfile);
3589 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3590 err = got_error_from_errno("close");
3592 got_object_blob_close(blob1);
3593 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3594 err = got_error_from_errno("close");
3595 got_object_blob_close(blob2);
3596 if (f1 && fclose(f1) == EOF && err == NULL)
3597 err = got_error_from_errno("fclose");
3598 if (f2 && fclose(f2) == EOF && err == NULL)
3599 err = got_error_from_errno("fclose");
3603 static const struct got_error *
3604 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3605 const char *path, int diff_context, int ignore_whitespace,
3606 int force_text_diff, struct got_repository *repo, FILE *outfile)
3608 const struct got_error *err = NULL;
3609 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3610 struct got_diff_blob_output_unidiff_arg arg;
3611 FILE *f1 = NULL, *f2 = NULL;
3614 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3617 f1 = got_opentemp();
3619 err = got_error_from_errno("got_opentemp");
3624 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3628 f2 = got_opentemp();
3630 err = got_error_from_errno("got_opentemp");
3634 arg.diff_context = diff_context;
3635 arg.ignore_whitespace = ignore_whitespace;
3636 arg.force_text_diff = force_text_diff;
3637 arg.outfile = outfile;
3638 arg.line_offsets = NULL;
3640 while (path[0] == '/')
3642 err = got_diff_tree(tree1, tree2, f1, f2, path, path, repo,
3643 got_diff_blob_output_unidiff, &arg, 1);
3646 got_object_tree_close(tree1);
3648 got_object_tree_close(tree2);
3649 if (f1 && fclose(f1) == EOF && err == NULL)
3650 err = got_error_from_errno("fclose");
3651 if (f2 && fclose(f2) == EOF && err == NULL)
3652 err = got_error_from_errno("fclose");
3656 static const struct got_error *
3657 get_changed_paths(struct got_pathlist_head *paths,
3658 struct got_commit_object *commit, struct got_repository *repo)
3660 const struct got_error *err = NULL;
3661 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3662 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3663 struct got_object_qid *qid;
3665 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3667 struct got_commit_object *pcommit;
3668 err = got_object_open_as_commit(&pcommit, repo,
3673 tree_id1 = got_object_id_dup(
3674 got_object_commit_get_tree_id(pcommit));
3675 if (tree_id1 == NULL) {
3676 got_object_commit_close(pcommit);
3677 return got_error_from_errno("got_object_id_dup");
3679 got_object_commit_close(pcommit);
3684 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3689 tree_id2 = got_object_commit_get_tree_id(commit);
3690 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3694 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3695 got_diff_tree_collect_changed_paths, paths, 0);
3698 got_object_tree_close(tree1);
3700 got_object_tree_close(tree2);
3705 static const struct got_error *
3706 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3707 const char *path, int diff_context, struct got_repository *repo,
3710 const struct got_error *err = NULL;
3711 struct got_commit_object *pcommit = NULL;
3712 char *id_str1 = NULL, *id_str2 = NULL;
3713 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3714 struct got_object_qid *qid;
3716 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3718 err = got_object_open_as_commit(&pcommit, repo,
3722 err = got_object_id_str(&id_str1, &qid->id);
3727 err = got_object_id_str(&id_str2, id);
3731 if (path && path[0] != '\0') {
3733 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3737 err = got_object_id_by_path(&obj_id1, repo,
3740 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3746 err = got_object_get_type(&obj_type, repo, obj_id2);
3752 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3753 fprintf(outfile, "commit - %s\n",
3754 id_str1 ? id_str1 : "/dev/null");
3755 fprintf(outfile, "commit + %s\n", id_str2);
3757 case GOT_OBJ_TYPE_BLOB:
3758 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3759 0, 0, repo, outfile);
3761 case GOT_OBJ_TYPE_TREE:
3762 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3763 0, 0, repo, outfile);
3766 err = got_error(GOT_ERR_OBJ_TYPE);
3772 obj_id2 = got_object_commit_get_tree_id(commit);
3774 obj_id1 = got_object_commit_get_tree_id(pcommit);
3776 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3777 fprintf(outfile, "commit - %s\n",
3778 id_str1 ? id_str1 : "/dev/null");
3779 fprintf(outfile, "commit + %s\n", id_str2);
3780 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3787 got_object_commit_close(pcommit);
3792 get_datestr(time_t *time, char *datebuf)
3794 struct tm mytm, *tm;
3797 tm = gmtime_r(time, &mytm);
3800 s = asctime_r(tm, datebuf);
3803 p = strchr(s, '\n');
3809 static const struct got_error *
3810 match_commit(int *have_match, struct got_object_id *id,
3811 struct got_commit_object *commit, regex_t *regex)
3813 const struct got_error *err = NULL;
3814 regmatch_t regmatch;
3815 char *id_str = NULL, *logmsg = NULL;
3819 err = got_object_id_str(&id_str, id);
3823 err = got_object_commit_get_logmsg(&logmsg, commit);
3827 if (regexec(regex, got_object_commit_get_author(commit), 1,
3828 ®match, 0) == 0 ||
3829 regexec(regex, got_object_commit_get_committer(commit), 1,
3830 ®match, 0) == 0 ||
3831 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3832 regexec(regex, logmsg, 1, ®match, 0) == 0)
3841 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3844 regmatch_t regmatch;
3845 struct got_pathlist_entry *pe;
3849 TAILQ_FOREACH(pe, changed_paths, entry) {
3850 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3857 static const struct got_error *
3858 match_patch(int *have_match, struct got_commit_object *commit,
3859 struct got_object_id *id, const char *path, int diff_context,
3860 struct got_repository *repo, regex_t *regex, FILE *f)
3862 const struct got_error *err = NULL;
3864 size_t linesize = 0;
3866 regmatch_t regmatch;
3870 err = got_opentemp_truncate(f);
3874 err = print_patch(commit, id, path, diff_context, repo, f);
3878 if (fseeko(f, 0L, SEEK_SET) == -1) {
3879 err = got_error_from_errno("fseeko");
3883 while ((linelen = getline(&line, &linesize, f)) != -1) {
3884 if (regexec(regex, line, 1, ®match, 0) == 0) {
3894 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3896 static const struct got_error*
3897 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3898 struct got_object_id *id, struct got_repository *repo,
3901 static const struct got_error *err = NULL;
3902 struct got_reflist_entry *re;
3908 TAILQ_FOREACH(re, refs, entry) {
3909 struct got_tag_object *tag = NULL;
3910 struct got_object_id *ref_id;
3913 name = got_ref_get_name(re->ref);
3914 if (strcmp(name, GOT_REF_HEAD) == 0)
3916 if (strncmp(name, "refs/", 5) == 0)
3918 if (strncmp(name, "got/", 4) == 0)
3920 if (strncmp(name, "heads/", 6) == 0)
3922 if (strncmp(name, "remotes/", 8) == 0) {
3926 s = strstr(name, "/" GOT_REF_HEAD);
3927 if (s != NULL && s[strlen(s)] == '\0')
3930 err = got_ref_resolve(&ref_id, repo, re->ref);
3933 if (strncmp(name, "tags/", 5) == 0) {
3934 err = got_object_open_as_tag(&tag, repo, ref_id);
3936 if (err->code != GOT_ERR_OBJ_TYPE) {
3940 /* Ref points at something other than a tag. */
3945 cmp = got_object_id_cmp(tag ?
3946 got_object_tag_get_object_id(tag) : ref_id, id);
3949 got_object_tag_close(tag);
3953 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3954 s ? ", " : "", name) == -1) {
3955 err = got_error_from_errno("asprintf");
3966 static const struct got_error *
3967 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3968 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3970 const struct got_error *err = NULL;
3971 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3972 char *comma, *s, *nl;
3973 struct got_reflist_head *refs;
3974 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3976 time_t committer_time;
3978 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3980 err = build_refs_str(&ref_str, refs, id, repo, 1);
3984 /* Display the first matching ref only. */
3985 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3989 if (ref_str == NULL) {
3990 err = got_object_id_str(&id_str, id);
3995 committer_time = got_object_commit_get_committer_time(commit);
3996 if (gmtime_r(&committer_time, &tm) == NULL) {
3997 err = got_error_from_errno("gmtime_r");
4000 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4001 err = got_error(GOT_ERR_NO_SPACE);
4005 err = got_object_commit_get_logmsg(&logmsg0, commit);
4010 while (isspace((unsigned char)s[0]))
4013 nl = strchr(s, '\n');
4019 printf("%s%-7s %s\n", datebuf, ref_str, s);
4021 printf("%s%.7s %s\n", datebuf, id_str, s);
4023 if (fflush(stdout) != 0 && err == NULL)
4024 err = got_error_from_errno("fflush");
4032 static const struct got_error *
4033 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4034 struct got_repository *repo, const char *path,
4035 struct got_pathlist_head *changed_paths, int show_patch,
4036 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4037 const char *custom_refs_str)
4039 const struct got_error *err = NULL;
4040 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4042 time_t committer_time;
4043 const char *author, *committer;
4044 char *refs_str = NULL;
4046 err = got_object_id_str(&id_str, id);
4050 if (custom_refs_str == NULL) {
4051 struct got_reflist_head *refs;
4052 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4054 err = build_refs_str(&refs_str, refs, id, repo, 0);
4060 printf(GOT_COMMIT_SEP_STR);
4061 if (custom_refs_str)
4062 printf("commit %s (%s)\n", id_str, custom_refs_str);
4064 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4065 refs_str ? refs_str : "", refs_str ? ")" : "");
4070 printf("from: %s\n", got_object_commit_get_author(commit));
4071 committer_time = got_object_commit_get_committer_time(commit);
4072 datestr = get_datestr(&committer_time, datebuf);
4074 printf("date: %s UTC\n", datestr);
4075 author = got_object_commit_get_author(commit);
4076 committer = got_object_commit_get_committer(commit);
4077 if (strcmp(author, committer) != 0)
4078 printf("via: %s\n", committer);
4079 if (got_object_commit_get_nparents(commit) > 1) {
4080 const struct got_object_id_queue *parent_ids;
4081 struct got_object_qid *qid;
4083 parent_ids = got_object_commit_get_parent_ids(commit);
4084 STAILQ_FOREACH(qid, parent_ids, entry) {
4085 err = got_object_id_str(&id_str, &qid->id);
4088 printf("parent %d: %s\n", n++, id_str);
4094 err = got_object_commit_get_logmsg(&logmsg0, commit);
4100 line = strsep(&logmsg, "\n");
4102 printf(" %s\n", line);
4106 if (changed_paths) {
4107 struct got_pathlist_entry *pe;
4108 TAILQ_FOREACH(pe, changed_paths, entry) {
4109 struct got_diff_changed_path *cp = pe->data;
4110 printf(" %c %s\n", cp->status, pe->path);
4115 err = print_patch(commit, id, path, diff_context, repo, stdout);
4120 if (fflush(stdout) != 0 && err == NULL)
4121 err = got_error_from_errno("fflush");
4128 static const struct got_error *
4129 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4130 struct got_repository *repo, const char *path, int show_changed_paths,
4131 int show_patch, const char *search_pattern, int diff_context, int limit,
4132 int log_branches, int reverse_display_order,
4133 struct got_reflist_object_id_map *refs_idmap, int one_line,
4136 const struct got_error *err;
4137 struct got_commit_graph *graph;
4140 struct got_object_id_queue reversed_commits;
4141 struct got_object_qid *qid;
4142 struct got_commit_object *commit;
4143 struct got_pathlist_head changed_paths;
4144 struct got_pathlist_entry *pe;
4146 STAILQ_INIT(&reversed_commits);
4147 TAILQ_INIT(&changed_paths);
4149 if (search_pattern && regcomp(®ex, search_pattern,
4150 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4151 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4153 err = got_commit_graph_open(&graph, path, !log_branches);
4156 err = got_commit_graph_iter_start(graph, root_id, repo,
4157 check_cancelled, NULL);
4161 struct got_object_id *id;
4163 if (sigint_received || sigpipe_received)
4166 err = got_commit_graph_iter_next(&id, graph, repo,
4167 check_cancelled, NULL);
4169 if (err->code == GOT_ERR_ITER_COMPLETED)
4176 err = got_object_open_as_commit(&commit, repo, id);
4180 if (show_changed_paths && !reverse_display_order) {
4181 err = get_changed_paths(&changed_paths, commit, repo);
4186 if (search_pattern) {
4187 err = match_commit(&have_match, id, commit, ®ex);
4189 got_object_commit_close(commit);
4192 if (have_match == 0 && show_changed_paths)
4193 match_changed_paths(&have_match,
4194 &changed_paths, ®ex);
4195 if (have_match == 0 && show_patch) {
4196 err = match_patch(&have_match, commit, id,
4197 path, diff_context, repo, ®ex,
4202 if (have_match == 0) {
4203 got_object_commit_close(commit);
4204 TAILQ_FOREACH(pe, &changed_paths, entry) {
4205 free((char *)pe->path);
4208 got_pathlist_free(&changed_paths);
4213 if (reverse_display_order) {
4214 err = got_object_qid_alloc(&qid, id);
4217 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4218 got_object_commit_close(commit);
4221 err = print_commit_oneline(commit, id,
4224 err = print_commit(commit, id, repo, path,
4225 show_changed_paths ? &changed_paths : NULL,
4226 show_patch, diff_context, refs_idmap, NULL);
4227 got_object_commit_close(commit);
4231 if ((limit && --limit == 0) ||
4232 (end_id && got_object_id_cmp(id, end_id) == 0))
4235 TAILQ_FOREACH(pe, &changed_paths, entry) {
4236 free((char *)pe->path);
4239 got_pathlist_free(&changed_paths);
4241 if (reverse_display_order) {
4242 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4243 err = got_object_open_as_commit(&commit, repo,
4247 if (show_changed_paths) {
4248 err = get_changed_paths(&changed_paths,
4254 err = print_commit_oneline(commit, &qid->id,
4257 err = print_commit(commit, &qid->id, repo, path,
4258 show_changed_paths ? &changed_paths : NULL,
4259 show_patch, diff_context, refs_idmap, NULL);
4260 got_object_commit_close(commit);
4263 TAILQ_FOREACH(pe, &changed_paths, entry) {
4264 free((char *)pe->path);
4267 got_pathlist_free(&changed_paths);
4271 while (!STAILQ_EMPTY(&reversed_commits)) {
4272 qid = STAILQ_FIRST(&reversed_commits);
4273 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4274 got_object_qid_free(qid);
4276 TAILQ_FOREACH(pe, &changed_paths, entry) {
4277 free((char *)pe->path);
4280 got_pathlist_free(&changed_paths);
4283 got_commit_graph_close(graph);
4290 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4291 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4292 "[-r repository-path] [-R] [path]\n", getprogname());
4297 get_default_log_limit(void)
4299 const char *got_default_log_limit;
4303 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4304 if (got_default_log_limit == NULL)
4306 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4312 static const struct got_error *
4313 cmd_log(int argc, char *argv[])
4315 const struct got_error *error;
4316 struct got_repository *repo = NULL;
4317 struct got_worktree *worktree = NULL;
4318 struct got_object_id *start_id = NULL, *end_id = NULL;
4319 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4320 const char *start_commit = NULL, *end_commit = NULL;
4321 const char *search_pattern = NULL;
4322 int diff_context = -1, ch;
4323 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4324 int reverse_display_order = 0, one_line = 0;
4326 struct got_reflist_head refs;
4327 struct got_reflist_object_id_map *refs_idmap = NULL;
4328 FILE *tmpfile = NULL;
4329 int *pack_fds = NULL;
4334 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4340 limit = get_default_log_limit();
4342 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4348 show_changed_paths = 1;
4351 start_commit = optarg;
4354 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4357 errx(1, "number of context lines is %s: %s",
4361 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4363 errx(1, "number of commits is %s: %s",
4370 repo_path = realpath(optarg, NULL);
4371 if (repo_path == NULL)
4372 return got_error_from_errno2("realpath",
4374 got_path_strip_trailing_slashes(repo_path);
4377 reverse_display_order = 1;
4383 search_pattern = optarg;
4386 end_commit = optarg;
4397 if (diff_context == -1)
4399 else if (!show_patch)
4400 errx(1, "-C requires -p");
4402 if (one_line && (show_patch || show_changed_paths))
4403 errx(1, "cannot use -s with -p or -P");
4405 cwd = getcwd(NULL, 0);
4407 error = got_error_from_errno("getcwd");
4411 error = got_repo_pack_fds_open(&pack_fds);
4415 if (repo_path == NULL) {
4416 error = got_worktree_open(&worktree, cwd);
4417 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4424 error = got_worktree_resolve_path(&path, worktree,
4429 path = strdup(argv[0]);
4431 error = got_error_from_errno("strdup");
4435 } else if (argc != 0)
4438 if (repo_path == NULL) {
4439 repo_path = worktree ?
4440 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4442 if (repo_path == NULL) {
4443 error = got_error_from_errno("strdup");
4447 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4451 error = apply_unveil(got_repo_get_path(repo), 1,
4452 worktree ? got_worktree_get_root_path(worktree) : NULL);
4456 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4460 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4464 if (start_commit == NULL) {
4465 struct got_reference *head_ref;
4466 struct got_commit_object *commit = NULL;
4467 error = got_ref_open(&head_ref, repo,
4468 worktree ? got_worktree_get_head_ref_name(worktree)
4472 error = got_ref_resolve(&start_id, repo, head_ref);
4473 got_ref_close(head_ref);
4476 error = got_object_open_as_commit(&commit, repo,
4480 got_object_commit_close(commit);
4482 error = got_repo_match_object_id(&start_id, NULL,
4483 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4487 if (end_commit != NULL) {
4488 error = got_repo_match_object_id(&end_id, NULL,
4489 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4496 * If a path was specified on the command line it was resolved
4497 * to a path in the work tree above. Prepend the work tree's
4498 * path prefix to obtain the corresponding in-repository path.
4502 prefix = got_worktree_get_path_prefix(worktree);
4503 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4504 (path[0] != '\0') ? "/" : "", path) == -1) {
4505 error = got_error_from_errno("asprintf");
4510 error = got_repo_map_path(&in_repo_path, repo,
4516 path = in_repo_path;
4520 /* Release work tree lock. */
4521 got_worktree_close(worktree);
4525 if (search_pattern && show_patch) {
4526 tmpfile = got_opentemp();
4527 if (tmpfile == NULL) {
4528 error = got_error_from_errno("got_opentemp");
4533 error = print_commits(start_id, end_id, repo, path ? path : "",
4534 show_changed_paths, show_patch, search_pattern, diff_context,
4535 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4542 got_worktree_close(worktree);
4544 const struct got_error *close_err = got_repo_close(repo);
4549 const struct got_error *pack_err =
4550 got_repo_pack_fds_close(pack_fds);
4555 got_reflist_object_id_map_free(refs_idmap);
4556 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4557 error = got_error_from_errno("fclose");
4558 got_ref_list_free(&refs);
4565 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4566 "[-r repository-path] [-s] [-w] [-P] "
4567 "[object1 object2 | path ...]\n", getprogname());
4571 struct print_diff_arg {
4572 struct got_repository *repo;
4573 struct got_worktree *worktree;
4578 int ignore_whitespace;
4579 int force_text_diff;
4583 * Create a file which contains the target path of a symlink so we can feed
4584 * it as content to the diff engine.
4586 static const struct got_error *
4587 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4588 const char *abspath)
4590 const struct got_error *err = NULL;
4591 char target_path[PATH_MAX];
4592 ssize_t target_len, outlen;
4597 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4598 if (target_len == -1)
4599 return got_error_from_errno2("readlinkat", abspath);
4601 target_len = readlink(abspath, target_path, PATH_MAX);
4602 if (target_len == -1)
4603 return got_error_from_errno2("readlink", abspath);
4606 *fd = got_opentempfd();
4608 return got_error_from_errno("got_opentempfd");
4610 outlen = write(*fd, target_path, target_len);
4612 err = got_error_from_errno("got_opentempfd");
4616 if (lseek(*fd, 0, SEEK_SET) == -1) {
4617 err = got_error_from_errno2("lseek", abspath);
4628 static const struct got_error *
4629 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *path, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct print_diff_arg *a = arg;
4635 const struct got_error *err = NULL;
4636 struct got_blob_object *blob1 = NULL;
4637 int fd = -1, fd1 = -1;
4638 FILE *f1 = NULL, *f2 = NULL;
4639 char *abspath = NULL, *label1 = NULL;
4643 if (a->diff_staged) {
4644 if (staged_status != GOT_STATUS_MODIFY &&
4645 staged_status != GOT_STATUS_ADD &&
4646 staged_status != GOT_STATUS_DELETE)
4649 if (staged_status == GOT_STATUS_DELETE)
4651 if (status == GOT_STATUS_NONEXISTENT)
4652 return got_error_set_errno(ENOENT, path);
4653 if (status != GOT_STATUS_MODIFY &&
4654 status != GOT_STATUS_ADD &&
4655 status != GOT_STATUS_DELETE &&
4656 status != GOT_STATUS_CONFLICT)
4660 if (!a->header_shown) {
4661 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4662 got_worktree_get_root_path(a->worktree));
4663 printf("commit - %s\n", a->id_str);
4664 printf("path + %s%s\n",
4665 got_worktree_get_root_path(a->worktree),
4666 a->diff_staged ? " (staged changes)" : "");
4667 a->header_shown = 1;
4670 if (a->diff_staged) {
4671 const char *label1 = NULL, *label2 = NULL;
4672 switch (staged_status) {
4673 case GOT_STATUS_MODIFY:
4677 case GOT_STATUS_ADD:
4680 case GOT_STATUS_DELETE:
4684 return got_error(GOT_ERR_FILE_STATUS);
4686 f1 = got_opentemp();
4688 err = got_error_from_errno("got_opentemp");
4691 f2 = got_opentemp();
4693 err = got_error_from_errno("got_opentemp");
4696 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4697 blob_id, staged_blob_id, label1, label2, a->diff_context,
4698 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4702 fd1 = got_opentempfd();
4704 err = got_error_from_errno("got_opentempfd");
4708 if (staged_status == GOT_STATUS_ADD ||
4709 staged_status == GOT_STATUS_MODIFY) {
4711 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4715 err = got_object_id_str(&id_str, staged_blob_id);
4718 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4719 err = got_error_from_errno("asprintf");
4724 } else if (status != GOT_STATUS_ADD) {
4725 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4731 if (status != GOT_STATUS_DELETE) {
4732 if (asprintf(&abspath, "%s/%s",
4733 got_worktree_get_root_path(a->worktree), path) == -1) {
4734 err = got_error_from_errno("asprintf");
4739 fd = openat(dirfd, de_name,
4740 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4742 if (!got_err_open_nofollow_on_symlink()) {
4743 err = got_error_from_errno2("openat",
4747 err = get_symlink_target_file(&fd, dirfd,
4753 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4755 if (!got_err_open_nofollow_on_symlink()) {
4756 err = got_error_from_errno2("open",
4760 err = get_symlink_target_file(&fd, dirfd,
4766 if (fstat(fd, &sb) == -1) {
4767 err = got_error_from_errno2("fstat", abspath);
4770 f2 = fdopen(fd, "r");
4772 err = got_error_from_errno2("fdopen", abspath);
4780 f1 = got_opentemp();
4782 err = got_error_from_errno("got_opentemp");
4785 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
4791 err = got_diff_blob_file(blob1, f1, size1, label1, f2, sb.st_size,
4792 path, a->diff_context, a->ignore_whitespace, a->force_text_diff,
4795 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4796 err = got_error_from_errno("close");
4798 got_object_blob_close(blob1);
4799 if (f1 && fclose(f1) == EOF && err == NULL)
4800 err = got_error_from_errno("fclose");
4801 if (f2 && fclose(f2) == EOF && err == NULL)
4802 err = got_error_from_errno("fclose");
4803 if (fd != -1 && close(fd) == -1 && err == NULL)
4804 err = got_error_from_errno("close");
4809 static const struct got_error *
4810 cmd_diff(int argc, char *argv[])
4812 const struct got_error *error;
4813 struct got_repository *repo = NULL;
4814 struct got_worktree *worktree = NULL;
4815 char *cwd = NULL, *repo_path = NULL;
4816 const char *commit_args[2] = { NULL, NULL };
4817 int ncommit_args = 0;
4818 struct got_object_id *ids[2] = { NULL, NULL };
4819 char *labels[2] = { NULL, NULL };
4820 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4821 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4822 int force_text_diff = 0, force_path = 0, rflag = 0;
4824 struct got_reflist_head refs;
4825 struct got_pathlist_head paths;
4826 struct got_pathlist_entry *pe;
4827 FILE *f1 = NULL, *f2 = NULL;
4828 int *pack_fds = NULL;
4834 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4839 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4842 force_text_diff = 1;
4845 if (ncommit_args >= 2)
4846 errx(1, "too many -c options used");
4847 commit_args[ncommit_args++] = optarg;
4850 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4853 errx(1, "number of context lines is %s: %s",
4857 repo_path = realpath(optarg, NULL);
4858 if (repo_path == NULL)
4859 return got_error_from_errno2("realpath",
4861 got_path_strip_trailing_slashes(repo_path);
4868 ignore_whitespace = 1;
4882 cwd = getcwd(NULL, 0);
4884 error = got_error_from_errno("getcwd");
4888 error = got_repo_pack_fds_open(&pack_fds);
4892 if (repo_path == NULL) {
4893 error = got_worktree_open(&worktree, cwd);
4894 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4900 strdup(got_worktree_get_repo_path(worktree));
4901 if (repo_path == NULL) {
4902 error = got_error_from_errno("strdup");
4906 repo_path = strdup(cwd);
4907 if (repo_path == NULL) {
4908 error = got_error_from_errno("strdup");
4914 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4919 if (rflag || worktree == NULL || ncommit_args > 0) {
4921 error = got_error_msg(GOT_ERR_NOT_IMPL,
4922 "-P option can only be used when diffing "
4927 error = got_error_msg(GOT_ERR_NOT_IMPL,
4928 "-s option can only be used when diffing "
4934 error = apply_unveil(got_repo_get_path(repo), 1,
4935 worktree ? got_worktree_get_root_path(worktree) : NULL);
4939 if ((!force_path && argc == 2) || ncommit_args > 0) {
4940 int obj_type = (ncommit_args > 0 ?
4941 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4942 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4946 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4948 if (ncommit_args > 0)
4949 arg = commit_args[i];
4952 error = got_repo_match_object_id(&ids[i], &labels[i],
4953 arg, obj_type, &refs, repo);
4955 if (error->code != GOT_ERR_NOT_REF &&
4956 error->code != GOT_ERR_NO_OBJ)
4958 if (ncommit_args > 0)
4966 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4967 struct print_diff_arg arg;
4970 if (worktree == NULL) {
4971 if (argc == 2 && ids[0] == NULL) {
4972 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4974 } else if (argc == 2 && ids[1] == NULL) {
4975 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4977 } else if (argc > 0) {
4978 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4979 "%s", "specified paths cannot be resolved");
4982 error = got_error(GOT_ERR_NOT_WORKTREE);
4987 error = get_worktree_paths_from_argv(&paths, argc, argv,
4992 error = got_object_id_str(&id_str,
4993 got_worktree_get_base_commit_id(worktree));
4997 arg.worktree = worktree;
4998 arg.diff_context = diff_context;
4999 arg.id_str = id_str;
5000 arg.header_shown = 0;
5001 arg.diff_staged = diff_staged;
5002 arg.ignore_whitespace = ignore_whitespace;
5003 arg.force_text_diff = force_text_diff;
5005 error = got_worktree_status(worktree, &paths, repo, 0,
5006 print_diff, &arg, check_cancelled, NULL);
5011 if (ncommit_args == 1) {
5012 struct got_commit_object *commit;
5013 error = got_object_open_as_commit(&commit, repo, ids[0]);
5017 labels[1] = labels[0];
5019 if (got_object_commit_get_nparents(commit) > 0) {
5020 const struct got_object_id_queue *pids;
5021 struct got_object_qid *pid;
5022 pids = got_object_commit_get_parent_ids(commit);
5023 pid = STAILQ_FIRST(pids);
5024 ids[0] = got_object_id_dup(&pid->id);
5025 if (ids[0] == NULL) {
5026 error = got_error_from_errno(
5027 "got_object_id_dup");
5028 got_object_commit_close(commit);
5031 error = got_object_id_str(&labels[0], ids[0]);
5033 got_object_commit_close(commit);
5038 labels[0] = strdup("/dev/null");
5039 if (labels[0] == NULL) {
5040 error = got_error_from_errno("strdup");
5041 got_object_commit_close(commit);
5046 got_object_commit_close(commit);
5049 if (ncommit_args == 0 && argc > 2) {
5050 error = got_error_msg(GOT_ERR_BAD_PATH,
5051 "path arguments cannot be used when diffing two objects");
5056 error = got_object_get_type(&type1, repo, ids[0]);
5061 error = got_object_get_type(&type2, repo, ids[1]);
5064 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5065 error = got_error(GOT_ERR_OBJ_TYPE);
5068 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5069 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5070 "path arguments cannot be used when diffing blobs");
5074 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5076 struct got_pathlist_entry *new;
5080 error = got_worktree_resolve_path(&p, worktree,
5084 prefix = got_worktree_get_path_prefix(worktree);
5085 while (prefix[0] == '/')
5087 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5088 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5090 error = got_error_from_errno("asprintf");
5096 char *mapped_path, *s;
5097 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5103 in_repo_path = strdup(s);
5104 if (in_repo_path == NULL) {
5105 error = got_error_from_errno("asprintf");
5112 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5113 if (error || new == NULL /* duplicate */)
5120 /* Release work tree lock. */
5121 got_worktree_close(worktree);
5125 f1 = got_opentemp();
5127 error = got_error_from_errno("got_opentemp");
5131 f2 = got_opentemp();
5133 error = got_error_from_errno("got_opentemp");
5137 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5138 case GOT_OBJ_TYPE_BLOB:
5139 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5140 ids[0], ids[1], NULL, NULL, diff_context,
5141 ignore_whitespace, force_text_diff, repo, stdout);
5143 case GOT_OBJ_TYPE_TREE:
5144 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
5145 ids[0], ids[1], &paths, "", "", diff_context,
5146 ignore_whitespace, force_text_diff, repo, stdout);
5148 case GOT_OBJ_TYPE_COMMIT:
5149 printf("diff %s %s\n", labels[0], labels[1]);
5150 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5151 ids[0], ids[1], &paths, diff_context, ignore_whitespace,
5152 force_text_diff, repo, stdout);
5155 error = got_error(GOT_ERR_OBJ_TYPE);
5163 got_worktree_close(worktree);
5165 const struct got_error *close_err = got_repo_close(repo);
5170 const struct got_error *pack_err =
5171 got_repo_pack_fds_close(pack_fds);
5175 TAILQ_FOREACH(pe, &paths, entry)
5176 free((char *)pe->path);
5177 got_pathlist_free(&paths);
5178 got_ref_list_free(&refs);
5179 if (f1 && fclose(f1) == EOF && error == NULL)
5180 error = got_error_from_errno("fclose");
5181 if (f2 && fclose(f2) == EOF && error == NULL)
5182 error = got_error_from_errno("fclose");
5190 "usage: %s blame [-c commit] [-r repository-path] path\n",
5199 char datebuf[11]; /* YYYY-MM-DD + NUL */
5202 struct blame_cb_args {
5203 struct blame_line *lines;
5207 off_t *line_offsets;
5209 struct got_repository *repo;
5212 static const struct got_error *
5213 blame_cb(void *arg, int nlines, int lineno,
5214 struct got_commit_object *commit, struct got_object_id *id)
5216 const struct got_error *err = NULL;
5217 struct blame_cb_args *a = arg;
5218 struct blame_line *bline;
5220 size_t linesize = 0;
5223 time_t committer_time;
5225 if (nlines != a->nlines ||
5226 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5227 return got_error(GOT_ERR_RANGE);
5229 if (sigint_received)
5230 return got_error(GOT_ERR_ITER_COMPLETED);
5233 return NULL; /* no change in this commit */
5235 /* Annotate this line. */
5236 bline = &a->lines[lineno - 1];
5237 if (bline->annotated)
5239 err = got_object_id_str(&bline->id_str, id);
5243 bline->committer = strdup(got_object_commit_get_committer(commit));
5244 if (bline->committer == NULL) {
5245 err = got_error_from_errno("strdup");
5249 committer_time = got_object_commit_get_committer_time(commit);
5250 if (gmtime_r(&committer_time, &tm) == NULL)
5251 return got_error_from_errno("gmtime_r");
5252 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5254 err = got_error(GOT_ERR_NO_SPACE);
5257 bline->annotated = 1;
5259 /* Print lines annotated so far. */
5260 bline = &a->lines[a->lineno_cur - 1];
5261 if (!bline->annotated)
5264 offset = a->line_offsets[a->lineno_cur - 1];
5265 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5266 err = got_error_from_errno("fseeko");
5270 while (bline->annotated) {
5271 char *smallerthan, *at, *nl, *committer;
5274 if (getline(&line, &linesize, a->f) == -1) {
5276 err = got_error_from_errno("getline");
5280 committer = bline->committer;
5281 smallerthan = strchr(committer, '<');
5282 if (smallerthan && smallerthan[1] != '\0')
5283 committer = smallerthan + 1;
5284 at = strchr(committer, '@');
5287 len = strlen(committer);
5289 committer[8] = '\0';
5291 nl = strchr(line, '\n');
5294 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5295 bline->id_str, bline->datebuf, committer, line);
5298 bline = &a->lines[a->lineno_cur - 1];
5305 static const struct got_error *
5306 cmd_blame(int argc, char *argv[])
5308 const struct got_error *error;
5309 struct got_repository *repo = NULL;
5310 struct got_worktree *worktree = NULL;
5311 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5312 char *link_target = NULL;
5313 struct got_object_id *obj_id = NULL;
5314 struct got_object_id *commit_id = NULL;
5315 struct got_commit_object *commit = NULL;
5316 struct got_blob_object *blob = NULL;
5317 char *commit_id_str = NULL;
5318 struct blame_cb_args bca;
5319 int ch, obj_type, i, fd = -1;
5321 int *pack_fds = NULL;
5323 fd = got_opentempfd();
5325 return got_error_from_errno("got_opentempfd");
5327 memset(&bca, 0, sizeof(bca));
5330 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5335 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5338 commit_id_str = optarg;
5341 repo_path = realpath(optarg, NULL);
5342 if (repo_path == NULL)
5343 return got_error_from_errno2("realpath",
5345 got_path_strip_trailing_slashes(repo_path);
5361 cwd = getcwd(NULL, 0);
5363 error = got_error_from_errno("getcwd");
5367 error = got_repo_pack_fds_open(&pack_fds);
5371 if (repo_path == NULL) {
5372 error = got_worktree_open(&worktree, cwd);
5373 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5379 strdup(got_worktree_get_repo_path(worktree));
5380 if (repo_path == NULL) {
5381 error = got_error_from_errno("strdup");
5386 repo_path = strdup(cwd);
5387 if (repo_path == NULL) {
5388 error = got_error_from_errno("strdup");
5394 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5399 const char *prefix = got_worktree_get_path_prefix(worktree);
5402 error = got_worktree_resolve_path(&p, worktree, path);
5405 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5406 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5408 error = got_error_from_errno("asprintf");
5413 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5415 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5418 error = got_repo_map_path(&in_repo_path, repo, path);
5423 if (commit_id_str == NULL) {
5424 struct got_reference *head_ref;
5425 error = got_ref_open(&head_ref, repo, worktree ?
5426 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5429 error = got_ref_resolve(&commit_id, repo, head_ref);
5430 got_ref_close(head_ref);
5434 struct got_reflist_head refs;
5436 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5440 error = got_repo_match_object_id(&commit_id, NULL,
5441 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5442 got_ref_list_free(&refs);
5448 /* Release work tree lock. */
5449 got_worktree_close(worktree);
5453 error = got_object_open_as_commit(&commit, repo, commit_id);
5457 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5462 error = got_object_id_by_path(&obj_id, repo, commit,
5463 link_target ? link_target : in_repo_path);
5467 error = got_object_get_type(&obj_type, repo, obj_id);
5471 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5472 error = got_error_path(link_target ? link_target : in_repo_path,
5477 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
5480 bca.f = got_opentemp();
5481 if (bca.f == NULL) {
5482 error = got_error_from_errno("got_opentemp");
5485 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5486 &bca.line_offsets, bca.f, blob);
5487 if (error || bca.nlines == 0)
5490 /* Don't include \n at EOF in the blame line count. */
5491 if (bca.line_offsets[bca.nlines - 1] == filesize)
5494 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5495 if (bca.lines == NULL) {
5496 error = got_error_from_errno("calloc");
5500 bca.nlines_prec = 0;
5508 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5509 repo, blame_cb, &bca, check_cancelled, NULL);
5518 got_object_commit_close(commit);
5519 if (fd != -1 && close(fd) == -1 && error == NULL)
5520 error = got_error_from_errno("close");
5522 got_object_blob_close(blob);
5524 got_worktree_close(worktree);
5526 const struct got_error *close_err = got_repo_close(repo);
5531 const struct got_error *pack_err =
5532 got_repo_pack_fds_close(pack_fds);
5537 for (i = 0; i < bca.nlines; i++) {
5538 struct blame_line *bline = &bca.lines[i];
5539 free(bline->id_str);
5540 free(bline->committer);
5544 free(bca.line_offsets);
5545 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5546 error = got_error_from_errno("fclose");
5554 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5559 static const struct got_error *
5560 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5561 const char *root_path, struct got_repository *repo)
5563 const struct got_error *err = NULL;
5564 int is_root_path = (strcmp(path, root_path) == 0);
5565 const char *modestr = "";
5566 mode_t mode = got_tree_entry_get_mode(te);
5567 char *link_target = NULL;
5569 path += strlen(root_path);
5570 while (path[0] == '/')
5573 if (got_object_tree_entry_is_submodule(te))
5575 else if (S_ISLNK(mode)) {
5578 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5581 for (i = 0; i < strlen(link_target); i++) {
5582 if (!isprint((unsigned char)link_target[i]))
5583 link_target[i] = '?';
5588 else if (S_ISDIR(mode))
5590 else if (mode & S_IXUSR)
5593 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5594 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5595 link_target ? " -> ": "", link_target ? link_target : "");
5601 static const struct got_error *
5602 print_tree(const char *path, struct got_commit_object *commit,
5603 int show_ids, int recurse, const char *root_path,
5604 struct got_repository *repo)
5606 const struct got_error *err = NULL;
5607 struct got_object_id *tree_id = NULL;
5608 struct got_tree_object *tree = NULL;
5611 err = got_object_id_by_path(&tree_id, repo, commit, path);
5615 err = got_object_open_as_tree(&tree, repo, tree_id);
5618 nentries = got_object_tree_get_nentries(tree);
5619 for (i = 0; i < nentries; i++) {
5620 struct got_tree_entry *te;
5623 if (sigint_received || sigpipe_received)
5626 te = got_object_tree_get_entry(tree, i);
5629 err = got_object_id_str(&id_str,
5630 got_tree_entry_get_id(te));
5633 if (asprintf(&id, "%s ", id_str) == -1) {
5634 err = got_error_from_errno("asprintf");
5640 err = print_entry(te, id, path, root_path, repo);
5645 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5647 if (asprintf(&child_path, "%s%s%s", path,
5648 path[0] == '/' && path[1] == '\0' ? "" : "/",
5649 got_tree_entry_get_name(te)) == -1) {
5650 err = got_error_from_errno("asprintf");
5653 err = print_tree(child_path, commit, show_ids, 1,
5662 got_object_tree_close(tree);
5667 static const struct got_error *
5668 cmd_tree(int argc, char *argv[])
5670 const struct got_error *error;
5671 struct got_repository *repo = NULL;
5672 struct got_worktree *worktree = NULL;
5673 const char *path, *refname = NULL;
5674 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5675 struct got_object_id *commit_id = NULL;
5676 struct got_commit_object *commit = NULL;
5677 char *commit_id_str = NULL;
5678 int show_ids = 0, recurse = 0;
5680 int *pack_fds = NULL;
5683 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5688 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5691 commit_id_str = optarg;
5694 repo_path = realpath(optarg, NULL);
5695 if (repo_path == NULL)
5696 return got_error_from_errno2("realpath",
5698 got_path_strip_trailing_slashes(repo_path);
5722 cwd = getcwd(NULL, 0);
5724 error = got_error_from_errno("getcwd");
5728 error = got_repo_pack_fds_open(&pack_fds);
5732 if (repo_path == NULL) {
5733 error = got_worktree_open(&worktree, cwd);
5734 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5740 strdup(got_worktree_get_repo_path(worktree));
5741 if (repo_path == NULL)
5742 error = got_error_from_errno("strdup");
5746 repo_path = strdup(cwd);
5747 if (repo_path == NULL) {
5748 error = got_error_from_errno("strdup");
5754 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5759 const char *prefix = got_worktree_get_path_prefix(worktree);
5764 error = got_worktree_resolve_path(&p, worktree, path);
5767 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5768 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5770 error = got_error_from_errno("asprintf");
5775 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5779 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5784 error = got_repo_map_path(&in_repo_path, repo, path);
5789 if (commit_id_str == NULL) {
5790 struct got_reference *head_ref;
5792 refname = got_worktree_get_head_ref_name(worktree);
5794 refname = GOT_REF_HEAD;
5795 error = got_ref_open(&head_ref, repo, refname, 0);
5798 error = got_ref_resolve(&commit_id, repo, head_ref);
5799 got_ref_close(head_ref);
5803 struct got_reflist_head refs;
5805 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5809 error = got_repo_match_object_id(&commit_id, NULL,
5810 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5811 got_ref_list_free(&refs);
5817 /* Release work tree lock. */
5818 got_worktree_close(worktree);
5822 error = got_object_open_as_commit(&commit, repo, commit_id);
5826 error = print_tree(in_repo_path, commit, show_ids, recurse,
5827 in_repo_path, repo);
5834 got_object_commit_close(commit);
5836 got_worktree_close(worktree);
5838 const struct got_error *close_err = got_repo_close(repo);
5843 const struct got_error *pack_err =
5844 got_repo_pack_fds_close(pack_fds);
5854 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5855 "[-S status-codes] [path ...]\n", getprogname());
5859 struct got_status_arg {
5864 static const struct got_error *
5865 print_status(void *arg, unsigned char status, unsigned char staged_status,
5866 const char *path, struct got_object_id *blob_id,
5867 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5868 int dirfd, const char *de_name)
5870 struct got_status_arg *st = arg;
5872 if (status == staged_status && (status == GOT_STATUS_DELETE))
5873 status = GOT_STATUS_NO_CHANGE;
5874 if (st != NULL && st->status_codes) {
5875 size_t ncodes = strlen(st->status_codes);
5878 for (i = 0; i < ncodes ; i++) {
5880 if (status == st->status_codes[i] ||
5881 staged_status == st->status_codes[i]) {
5886 if (status == st->status_codes[i] ||
5887 staged_status == st->status_codes[i])
5892 if (st->suppress && j == 0)
5899 printf("%c%c %s\n", status, staged_status, path);
5903 static const struct got_error *
5904 cmd_status(int argc, char *argv[])
5906 const struct got_error *error = NULL;
5907 struct got_repository *repo = NULL;
5908 struct got_worktree *worktree = NULL;
5909 struct got_status_arg st;
5911 struct got_pathlist_head paths;
5912 struct got_pathlist_entry *pe;
5913 int ch, i, no_ignores = 0;
5914 int *pack_fds = NULL;
5918 memset(&st, 0, sizeof(st));
5919 st.status_codes = NULL;
5922 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5928 if (st.status_codes != NULL && st.suppress == 0)
5929 option_conflict('S', 's');
5933 for (i = 0; i < strlen(optarg); i++) {
5934 switch (optarg[i]) {
5935 case GOT_STATUS_MODIFY:
5936 case GOT_STATUS_ADD:
5937 case GOT_STATUS_DELETE:
5938 case GOT_STATUS_CONFLICT:
5939 case GOT_STATUS_MISSING:
5940 case GOT_STATUS_OBSTRUCTED:
5941 case GOT_STATUS_UNVERSIONED:
5942 case GOT_STATUS_MODE_CHANGE:
5943 case GOT_STATUS_NONEXISTENT:
5946 errx(1, "invalid status code '%c'",
5950 if (ch == 's' && st.suppress)
5951 option_conflict('s', 'S');
5952 st.status_codes = optarg;
5964 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5968 cwd = getcwd(NULL, 0);
5970 error = got_error_from_errno("getcwd");
5974 error = got_repo_pack_fds_open(&pack_fds);
5978 error = got_worktree_open(&worktree, cwd);
5980 if (error->code == GOT_ERR_NOT_WORKTREE)
5981 error = wrap_not_worktree_error(error, "status", cwd);
5985 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5990 error = apply_unveil(got_repo_get_path(repo), 1,
5991 got_worktree_get_root_path(worktree));
5995 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5999 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6000 print_status, &st, check_cancelled, NULL);
6003 const struct got_error *pack_err =
6004 got_repo_pack_fds_close(pack_fds);
6009 TAILQ_FOREACH(pe, &paths, entry)
6010 free((char *)pe->path);
6011 got_pathlist_free(&paths);
6020 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6021 "[-s reference] [-d] [name]\n",
6026 static const struct got_error *
6027 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6029 static const struct got_error *err = NULL;
6030 struct got_reflist_head refs;
6031 struct got_reflist_entry *re;
6034 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6035 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6040 TAILQ_FOREACH(re, &refs, entry) {
6042 refstr = got_ref_to_str(re->ref);
6043 if (refstr == NULL) {
6044 err = got_error_from_errno("got_ref_to_str");
6047 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6051 got_ref_list_free(&refs);
6055 static const struct got_error *
6056 delete_ref_by_name(struct got_repository *repo, const char *refname)
6058 const struct got_error *err;
6059 struct got_reference *ref;
6061 err = got_ref_open(&ref, repo, refname, 0);
6065 err = delete_ref(repo, ref);
6070 static const struct got_error *
6071 add_ref(struct got_repository *repo, const char *refname, const char *target)
6073 const struct got_error *err = NULL;
6074 struct got_object_id *id = NULL;
6075 struct got_reference *ref = NULL;
6076 struct got_reflist_head refs;
6079 * Don't let the user create a reference name with a leading '-'.
6080 * While technically a valid reference name, this case is usually
6081 * an unintended typo.
6083 if (refname[0] == '-')
6084 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6087 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6090 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6092 got_ref_list_free(&refs);
6096 err = got_ref_alloc(&ref, refname, id);
6100 err = got_ref_write(ref, repo);
6108 static const struct got_error *
6109 add_symref(struct got_repository *repo, const char *refname, const char *target)
6111 const struct got_error *err = NULL;
6112 struct got_reference *ref = NULL;
6113 struct got_reference *target_ref = NULL;
6116 * Don't let the user create a reference name with a leading '-'.
6117 * While technically a valid reference name, this case is usually
6118 * an unintended typo.
6120 if (refname[0] == '-')
6121 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6123 err = got_ref_open(&target_ref, repo, target, 0);
6127 err = got_ref_alloc_symref(&ref, refname, target_ref);
6131 err = got_ref_write(ref, repo);
6134 got_ref_close(target_ref);
6140 static const struct got_error *
6141 cmd_ref(int argc, char *argv[])
6143 const struct got_error *error = NULL;
6144 struct got_repository *repo = NULL;
6145 struct got_worktree *worktree = NULL;
6146 char *cwd = NULL, *repo_path = NULL;
6147 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6148 const char *obj_arg = NULL, *symref_target= NULL;
6149 char *refname = NULL;
6150 int *pack_fds = NULL;
6152 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6161 repo_path = realpath(optarg, NULL);
6162 if (repo_path == NULL)
6163 return got_error_from_errno2("realpath",
6165 got_path_strip_trailing_slashes(repo_path);
6171 symref_target = optarg;
6182 if (obj_arg && do_list)
6183 option_conflict('c', 'l');
6184 if (obj_arg && do_delete)
6185 option_conflict('c', 'd');
6186 if (obj_arg && symref_target)
6187 option_conflict('c', 's');
6188 if (symref_target && do_delete)
6189 option_conflict('s', 'd');
6190 if (symref_target && do_list)
6191 option_conflict('s', 'l');
6192 if (do_delete && do_list)
6193 option_conflict('d', 'l');
6194 if (sort_by_time && !do_list)
6195 errx(1, "-t option requires -l option");
6201 if (argc != 0 && argc != 1)
6204 refname = strdup(argv[0]);
6205 if (refname == NULL) {
6206 error = got_error_from_errno("strdup");
6213 refname = strdup(argv[0]);
6214 if (refname == NULL) {
6215 error = got_error_from_errno("strdup");
6221 got_path_strip_trailing_slashes(refname);
6224 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6225 "sendfd unveil", NULL) == -1)
6228 cwd = getcwd(NULL, 0);
6230 error = got_error_from_errno("getcwd");
6234 error = got_repo_pack_fds_open(&pack_fds);
6238 if (repo_path == NULL) {
6239 error = got_worktree_open(&worktree, cwd);
6240 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6246 strdup(got_worktree_get_repo_path(worktree));
6247 if (repo_path == NULL)
6248 error = got_error_from_errno("strdup");
6252 repo_path = strdup(cwd);
6253 if (repo_path == NULL) {
6254 error = got_error_from_errno("strdup");
6260 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6266 /* Remove "cpath" promise. */
6267 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6273 error = apply_unveil(got_repo_get_path(repo), do_list,
6274 worktree ? got_worktree_get_root_path(worktree) : NULL);
6279 error = list_refs(repo, refname, sort_by_time);
6281 error = delete_ref_by_name(repo, refname);
6282 else if (symref_target)
6283 error = add_symref(repo, refname, symref_target);
6285 if (obj_arg == NULL)
6287 error = add_ref(repo, refname, obj_arg);
6292 const struct got_error *close_err = got_repo_close(repo);
6297 got_worktree_close(worktree);
6299 const struct got_error *pack_err =
6300 got_repo_pack_fds_close(pack_fds);
6313 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6314 "[-n] [name]\n", getprogname());
6318 static const struct got_error *
6319 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6320 struct got_reference *ref)
6322 const struct got_error *err = NULL;
6323 const char *refname, *marker = " ";
6326 refname = got_ref_get_name(ref);
6327 if (worktree && strcmp(refname,
6328 got_worktree_get_head_ref_name(worktree)) == 0) {
6329 struct got_object_id *id = NULL;
6331 err = got_ref_resolve(&id, repo, ref);
6334 if (got_object_id_cmp(id,
6335 got_worktree_get_base_commit_id(worktree)) == 0)
6342 if (strncmp(refname, "refs/heads/", 11) == 0)
6344 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6346 if (strncmp(refname, "refs/remotes/", 13) == 0)
6349 refstr = got_ref_to_str(ref);
6351 return got_error_from_errno("got_ref_to_str");
6353 printf("%s%s: %s\n", marker, refname, refstr);
6358 static const struct got_error *
6359 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6361 const char *refname;
6363 if (worktree == NULL)
6364 return got_error(GOT_ERR_NOT_WORKTREE);
6366 refname = got_worktree_get_head_ref_name(worktree);
6368 if (strncmp(refname, "refs/heads/", 11) == 0)
6370 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6373 printf("%s\n", refname);
6378 static const struct got_error *
6379 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6382 static const struct got_error *err = NULL;
6383 struct got_reflist_head refs;
6384 struct got_reflist_entry *re;
6385 struct got_reference *temp_ref = NULL;
6386 int rebase_in_progress, histedit_in_progress;
6391 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6396 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6401 if (rebase_in_progress || histedit_in_progress) {
6402 err = got_ref_open(&temp_ref, repo,
6403 got_worktree_get_head_ref_name(worktree), 0);
6406 list_branch(repo, worktree, temp_ref);
6407 got_ref_close(temp_ref);
6411 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6412 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6417 TAILQ_FOREACH(re, &refs, entry)
6418 list_branch(repo, worktree, re->ref);
6420 got_ref_list_free(&refs);
6422 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6423 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6428 TAILQ_FOREACH(re, &refs, entry)
6429 list_branch(repo, worktree, re->ref);
6431 got_ref_list_free(&refs);
6436 static const struct got_error *
6437 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6438 const char *branch_name)
6440 const struct got_error *err = NULL;
6441 struct got_reference *ref = NULL;
6442 char *refname, *remote_refname = NULL;
6444 if (strncmp(branch_name, "refs/", 5) == 0)
6446 if (strncmp(branch_name, "heads/", 6) == 0)
6448 else if (strncmp(branch_name, "remotes/", 8) == 0)
6451 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6452 return got_error_from_errno("asprintf");
6454 if (asprintf(&remote_refname, "refs/remotes/%s",
6455 branch_name) == -1) {
6456 err = got_error_from_errno("asprintf");
6460 err = got_ref_open(&ref, repo, refname, 0);
6462 const struct got_error *err2;
6463 if (err->code != GOT_ERR_NOT_REF)
6466 * Keep 'err' intact such that if neither branch exists
6467 * we report "refs/heads" rather than "refs/remotes" in
6468 * our error message.
6470 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6477 strcmp(got_worktree_get_head_ref_name(worktree),
6478 got_ref_get_name(ref)) == 0) {
6479 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6480 "will not delete this work tree's current branch");
6484 err = delete_ref(repo, ref);
6489 free(remote_refname);
6493 static const struct got_error *
6494 add_branch(struct got_repository *repo, const char *branch_name,
6495 struct got_object_id *base_commit_id)
6497 const struct got_error *err = NULL;
6498 struct got_reference *ref = NULL;
6499 char *base_refname = NULL, *refname = NULL;
6502 * Don't let the user create a branch name with a leading '-'.
6503 * While technically a valid reference name, this case is usually
6504 * an unintended typo.
6506 if (branch_name[0] == '-')
6507 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6509 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6512 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6513 err = got_error_from_errno("asprintf");
6517 err = got_ref_open(&ref, repo, refname, 0);
6519 err = got_error(GOT_ERR_BRANCH_EXISTS);
6521 } else if (err->code != GOT_ERR_NOT_REF)
6524 err = got_ref_alloc(&ref, refname, base_commit_id);
6528 err = got_ref_write(ref, repo);
6537 static const struct got_error *
6538 cmd_branch(int argc, char *argv[])
6540 const struct got_error *error = NULL;
6541 struct got_repository *repo = NULL;
6542 struct got_worktree *worktree = NULL;
6543 char *cwd = NULL, *repo_path = NULL;
6544 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6545 const char *delref = NULL, *commit_id_arg = NULL;
6546 struct got_reference *ref = NULL;
6547 struct got_pathlist_head paths;
6548 struct got_pathlist_entry *pe;
6549 struct got_object_id *commit_id = NULL;
6550 char *commit_id_str = NULL;
6551 int *pack_fds = NULL;
6555 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6558 commit_id_arg = optarg;
6564 repo_path = realpath(optarg, NULL);
6565 if (repo_path == NULL)
6566 return got_error_from_errno2("realpath",
6568 got_path_strip_trailing_slashes(repo_path);
6585 if (do_list && delref)
6586 option_conflict('l', 'd');
6587 if (sort_by_time && !do_list)
6588 errx(1, "-t option requires -l option");
6593 if (!do_list && !delref && argc == 0)
6596 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6597 errx(1, "-c option can only be used when creating a branch");
6599 if (do_list || delref) {
6602 } else if (!do_show && argc != 1)
6606 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6607 "sendfd unveil", NULL) == -1)
6610 cwd = getcwd(NULL, 0);
6612 error = got_error_from_errno("getcwd");
6616 error = got_repo_pack_fds_open(&pack_fds);
6620 if (repo_path == NULL) {
6621 error = got_worktree_open(&worktree, cwd);
6622 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6628 strdup(got_worktree_get_repo_path(worktree));
6629 if (repo_path == NULL)
6630 error = got_error_from_errno("strdup");
6634 repo_path = strdup(cwd);
6635 if (repo_path == NULL) {
6636 error = got_error_from_errno("strdup");
6642 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6647 if (do_list || do_show) {
6648 /* Remove "cpath" promise. */
6649 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6655 error = apply_unveil(got_repo_get_path(repo), do_list,
6656 worktree ? got_worktree_get_root_path(worktree) : NULL);
6661 error = show_current_branch(repo, worktree);
6663 error = list_branches(repo, worktree, sort_by_time);
6665 error = delete_branch(repo, worktree, delref);
6667 struct got_reflist_head refs;
6669 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6673 if (commit_id_arg == NULL)
6674 commit_id_arg = worktree ?
6675 got_worktree_get_head_ref_name(worktree) :
6677 error = got_repo_match_object_id(&commit_id, NULL,
6678 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6679 got_ref_list_free(&refs);
6682 error = add_branch(repo, argv[0], commit_id);
6685 if (worktree && do_update) {
6686 struct got_update_progress_arg upa;
6687 char *branch_refname = NULL;
6689 error = got_object_id_str(&commit_id_str, commit_id);
6692 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6696 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6698 error = got_error_from_errno("asprintf");
6701 error = got_ref_open(&ref, repo, branch_refname, 0);
6702 free(branch_refname);
6705 error = switch_head_ref(ref, commit_id, worktree,
6709 error = got_worktree_set_base_commit_id(worktree, repo,
6713 memset(&upa, 0, sizeof(upa));
6714 error = got_worktree_checkout_files(worktree, &paths,
6715 repo, update_progress, &upa, check_cancelled,
6719 if (upa.did_something) {
6720 printf("Updated to %s: %s\n",
6721 got_worktree_get_head_ref_name(worktree),
6724 print_update_progress_stats(&upa);
6731 const struct got_error *close_err = got_repo_close(repo);
6736 got_worktree_close(worktree);
6738 const struct got_error *pack_err =
6739 got_repo_pack_fds_close(pack_fds);
6746 free(commit_id_str);
6747 TAILQ_FOREACH(pe, &paths, entry)
6748 free((char *)pe->path);
6749 got_pathlist_free(&paths);
6758 "usage: %s tag [-c commit] [-r repository] [-l] "
6759 "[-m message] name\n", getprogname());
6764 static const struct got_error *
6765 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6767 const struct got_error *err = NULL;
6768 struct got_reflist_entry *re, *se, *new;
6769 struct got_object_id *re_id, *se_id;
6770 struct got_tag_object *re_tag, *se_tag;
6771 time_t re_time, se_time;
6773 STAILQ_FOREACH(re, tags, entry) {
6774 se = STAILQ_FIRST(sorted);
6776 err = got_reflist_entry_dup(&new, re);
6779 STAILQ_INSERT_HEAD(sorted, new, entry);
6782 err = got_ref_resolve(&re_id, repo, re->ref);
6785 err = got_object_open_as_tag(&re_tag, repo, re_id);
6789 re_time = got_object_tag_get_tagger_time(re_tag);
6790 got_object_tag_close(re_tag);
6794 err = got_ref_resolve(&se_id, repo, re->ref);
6797 err = got_object_open_as_tag(&se_tag, repo, se_id);
6801 se_time = got_object_tag_get_tagger_time(se_tag);
6802 got_object_tag_close(se_tag);
6804 if (se_time > re_time) {
6805 err = got_reflist_entry_dup(&new, re);
6808 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6811 se = STAILQ_NEXT(se, entry);
6820 static const struct got_error *
6821 list_tags(struct got_repository *repo)
6823 static const struct got_error *err = NULL;
6824 struct got_reflist_head refs;
6825 struct got_reflist_entry *re;
6829 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6833 TAILQ_FOREACH(re, &refs, entry) {
6834 const char *refname;
6835 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6839 struct got_object_id *id;
6840 struct got_tag_object *tag;
6841 struct got_commit_object *commit = NULL;
6843 refname = got_ref_get_name(re->ref);
6844 if (strncmp(refname, "refs/tags/", 10) != 0)
6847 refstr = got_ref_to_str(re->ref);
6848 if (refstr == NULL) {
6849 err = got_error_from_errno("got_ref_to_str");
6852 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6855 err = got_ref_resolve(&id, repo, re->ref);
6858 err = got_object_open_as_tag(&tag, repo, id);
6860 if (err->code != GOT_ERR_OBJ_TYPE) {
6864 /* "lightweight" tag */
6865 err = got_object_open_as_commit(&commit, repo, id);
6870 tagger = got_object_commit_get_committer(commit);
6872 got_object_commit_get_committer_time(commit);
6873 err = got_object_id_str(&id_str, id);
6879 tagger = got_object_tag_get_tagger(tag);
6880 tagger_time = got_object_tag_get_tagger_time(tag);
6881 err = got_object_id_str(&id_str,
6882 got_object_tag_get_object_id(tag));
6886 printf("from: %s\n", tagger);
6887 datestr = get_datestr(&tagger_time, datebuf);
6889 printf("date: %s UTC\n", datestr);
6891 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6893 switch (got_object_tag_get_object_type(tag)) {
6894 case GOT_OBJ_TYPE_BLOB:
6895 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6898 case GOT_OBJ_TYPE_TREE:
6899 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6902 case GOT_OBJ_TYPE_COMMIT:
6903 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6906 case GOT_OBJ_TYPE_TAG:
6907 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6916 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6919 got_object_commit_close(commit);
6921 tagmsg0 = strdup(got_object_tag_get_message(tag));
6922 got_object_tag_close(tag);
6923 if (tagmsg0 == NULL) {
6924 err = got_error_from_errno("strdup");
6931 line = strsep(&tagmsg, "\n");
6933 printf(" %s\n", line);
6938 got_ref_list_free(&refs);
6942 static const struct got_error *
6943 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6944 const char *tag_name, const char *repo_path)
6946 const struct got_error *err = NULL;
6947 char *template = NULL, *initial_content = NULL;
6948 char *editor = NULL;
6949 int initial_content_len;
6952 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6953 err = got_error_from_errno("asprintf");
6957 initial_content_len = asprintf(&initial_content,
6958 "\n# tagging commit %s as %s\n",
6959 commit_id_str, tag_name);
6960 if (initial_content_len == -1) {
6961 err = got_error_from_errno("asprintf");
6965 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6969 if (write(fd, initial_content, initial_content_len) == -1) {
6970 err = got_error_from_errno2("write", *tagmsg_path);
6974 err = get_editor(&editor);
6977 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6978 initial_content_len, 1);
6980 free(initial_content);
6984 if (fd != -1 && close(fd) == -1 && err == NULL)
6985 err = got_error_from_errno2("close", *tagmsg_path);
6987 /* Editor is done; we can now apply unveil(2) */
6989 err = apply_unveil(repo_path, 0, NULL);
6997 static const struct got_error *
6998 add_tag(struct got_repository *repo, const char *tagger,
6999 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
7001 const struct got_error *err = NULL;
7002 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7003 char *label = NULL, *commit_id_str = NULL;
7004 struct got_reference *ref = NULL;
7005 char *refname = NULL, *tagmsg = NULL;
7006 char *tagmsg_path = NULL, *tag_id_str = NULL;
7007 int preserve_tagmsg = 0;
7008 struct got_reflist_head refs;
7013 * Don't let the user create a tag name with a leading '-'.
7014 * While technically a valid reference name, this case is usually
7015 * an unintended typo.
7017 if (tag_name[0] == '-')
7018 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7020 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7024 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7025 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7029 err = got_object_id_str(&commit_id_str, commit_id);
7033 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7034 refname = strdup(tag_name);
7035 if (refname == NULL) {
7036 err = got_error_from_errno("strdup");
7040 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
7041 err = got_error_from_errno("asprintf");
7045 err = got_ref_open(&ref, repo, refname, 0);
7047 err = got_error(GOT_ERR_TAG_EXISTS);
7049 } else if (err->code != GOT_ERR_NOT_REF)
7052 if (tagmsg_arg == NULL) {
7053 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7054 tag_name, got_repo_get_path(repo));
7056 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7057 tagmsg_path != NULL)
7058 preserve_tagmsg = 1;
7063 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7064 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7067 preserve_tagmsg = 1;
7071 err = got_ref_alloc(&ref, refname, tag_id);
7074 preserve_tagmsg = 1;
7078 err = got_ref_write(ref, repo);
7081 preserve_tagmsg = 1;
7085 err = got_object_id_str(&tag_id_str, tag_id);
7088 preserve_tagmsg = 1;
7091 printf("Created tag %s\n", tag_id_str);
7093 if (preserve_tagmsg) {
7094 fprintf(stderr, "%s: tag message preserved in %s\n",
7095 getprogname(), tagmsg_path);
7096 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7097 err = got_error_from_errno2("unlink", tagmsg_path);
7102 free(commit_id_str);
7106 got_ref_list_free(&refs);
7110 static const struct got_error *
7111 cmd_tag(int argc, char *argv[])
7113 const struct got_error *error = NULL;
7114 struct got_repository *repo = NULL;
7115 struct got_worktree *worktree = NULL;
7116 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7117 char *gitconfig_path = NULL, *tagger = NULL;
7118 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
7119 int ch, do_list = 0;
7120 int *pack_fds = NULL;
7122 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7125 commit_id_arg = optarg;
7131 repo_path = realpath(optarg, NULL);
7132 if (repo_path == NULL)
7133 return got_error_from_errno2("realpath",
7135 got_path_strip_trailing_slashes(repo_path);
7150 if (commit_id_arg != NULL)
7152 "-c option can only be used when creating a tag");
7154 option_conflict('l', 'm');
7157 } else if (argc != 1)
7163 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7164 "sendfd unveil", NULL) == -1)
7167 cwd = getcwd(NULL, 0);
7169 error = got_error_from_errno("getcwd");
7173 error = got_repo_pack_fds_open(&pack_fds);
7177 if (repo_path == NULL) {
7178 error = got_worktree_open(&worktree, cwd);
7179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7185 strdup(got_worktree_get_repo_path(worktree));
7186 if (repo_path == NULL)
7187 error = got_error_from_errno("strdup");
7191 repo_path = strdup(cwd);
7192 if (repo_path == NULL) {
7193 error = got_error_from_errno("strdup");
7201 /* Release work tree lock. */
7202 got_worktree_close(worktree);
7205 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7210 /* Remove "cpath" promise. */
7211 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7215 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7218 error = list_tags(repo);
7220 error = get_gitconfig_path(&gitconfig_path);
7223 error = got_repo_open(&repo, repo_path, gitconfig_path,
7228 error = get_author(&tagger, repo, worktree);
7232 /* Release work tree lock. */
7233 got_worktree_close(worktree);
7238 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7243 if (commit_id_arg == NULL) {
7244 struct got_reference *head_ref;
7245 struct got_object_id *commit_id;
7246 error = got_ref_open(&head_ref, repo,
7247 worktree ? got_worktree_get_head_ref_name(worktree)
7251 error = got_ref_resolve(&commit_id, repo, head_ref);
7252 got_ref_close(head_ref);
7255 error = got_object_id_str(&commit_id_str, commit_id);
7261 error = add_tag(repo, tagger, tag_name,
7262 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7266 const struct got_error *close_err = got_repo_close(repo);
7271 got_worktree_close(worktree);
7273 const struct got_error *pack_err =
7274 got_repo_pack_fds_close(pack_fds);
7280 free(gitconfig_path);
7281 free(commit_id_str);
7289 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7294 static const struct got_error *
7295 add_progress(void *arg, unsigned char status, const char *path)
7297 while (path[0] == '/')
7299 printf("%c %s\n", status, path);
7303 static const struct got_error *
7304 cmd_add(int argc, char *argv[])
7306 const struct got_error *error = NULL;
7307 struct got_repository *repo = NULL;
7308 struct got_worktree *worktree = NULL;
7310 struct got_pathlist_head paths;
7311 struct got_pathlist_entry *pe;
7312 int ch, can_recurse = 0, no_ignores = 0;
7313 int *pack_fds = NULL;
7317 while ((ch = getopt(argc, argv, "IR")) != -1) {
7335 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7342 cwd = getcwd(NULL, 0);
7344 error = got_error_from_errno("getcwd");
7348 error = got_repo_pack_fds_open(&pack_fds);
7352 error = got_worktree_open(&worktree, cwd);
7354 if (error->code == GOT_ERR_NOT_WORKTREE)
7355 error = wrap_not_worktree_error(error, "add", cwd);
7359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7364 error = apply_unveil(got_repo_get_path(repo), 1,
7365 got_worktree_get_root_path(worktree));
7369 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7376 TAILQ_FOREACH(pe, &paths, entry) {
7377 if (asprintf(&ondisk_path, "%s/%s",
7378 got_worktree_get_root_path(worktree),
7380 error = got_error_from_errno("asprintf");
7383 if (lstat(ondisk_path, &sb) == -1) {
7384 if (errno == ENOENT) {
7388 error = got_error_from_errno2("lstat",
7394 if (S_ISDIR(sb.st_mode)) {
7395 error = got_error_msg(GOT_ERR_BAD_PATH,
7396 "adding directories requires -R option");
7402 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7403 NULL, repo, no_ignores);
7406 const struct got_error *close_err = got_repo_close(repo);
7411 got_worktree_close(worktree);
7413 const struct got_error *pack_err =
7414 got_repo_pack_fds_close(pack_fds);
7418 TAILQ_FOREACH(pe, &paths, entry)
7419 free((char *)pe->path);
7420 got_pathlist_free(&paths);
7428 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7429 "path ...\n", getprogname());
7433 static const struct got_error *
7434 print_remove_status(void *arg, unsigned char status,
7435 unsigned char staged_status, const char *path)
7437 while (path[0] == '/')
7439 if (status == GOT_STATUS_NONEXISTENT)
7441 if (status == staged_status && (status == GOT_STATUS_DELETE))
7442 status = GOT_STATUS_NO_CHANGE;
7443 printf("%c%c %s\n", status, staged_status, path);
7447 static const struct got_error *
7448 cmd_remove(int argc, char *argv[])
7450 const struct got_error *error = NULL;
7451 struct got_worktree *worktree = NULL;
7452 struct got_repository *repo = NULL;
7453 const char *status_codes = NULL;
7455 struct got_pathlist_head paths;
7456 struct got_pathlist_entry *pe;
7457 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7458 int ignore_missing_paths = 0;
7459 int *pack_fds = NULL;
7463 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7466 delete_local_mods = 1;
7467 ignore_missing_paths = 1;
7476 for (i = 0; i < strlen(optarg); i++) {
7477 switch (optarg[i]) {
7478 case GOT_STATUS_MODIFY:
7479 delete_local_mods = 1;
7481 case GOT_STATUS_MISSING:
7482 ignore_missing_paths = 1;
7485 errx(1, "invalid status code '%c'",
7489 status_codes = optarg;
7501 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7508 cwd = getcwd(NULL, 0);
7510 error = got_error_from_errno("getcwd");
7514 error = got_repo_pack_fds_open(&pack_fds);
7518 error = got_worktree_open(&worktree, cwd);
7520 if (error->code == GOT_ERR_NOT_WORKTREE)
7521 error = wrap_not_worktree_error(error, "remove", cwd);
7525 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7530 error = apply_unveil(got_repo_get_path(repo), 1,
7531 got_worktree_get_root_path(worktree));
7535 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7542 TAILQ_FOREACH(pe, &paths, entry) {
7543 if (asprintf(&ondisk_path, "%s/%s",
7544 got_worktree_get_root_path(worktree),
7546 error = got_error_from_errno("asprintf");
7549 if (lstat(ondisk_path, &sb) == -1) {
7550 if (errno == ENOENT) {
7554 error = got_error_from_errno2("lstat",
7560 if (S_ISDIR(sb.st_mode)) {
7561 error = got_error_msg(GOT_ERR_BAD_PATH,
7562 "removing directories requires -R option");
7568 error = got_worktree_schedule_delete(worktree, &paths,
7569 delete_local_mods, status_codes, print_remove_status, NULL,
7570 repo, keep_on_disk, ignore_missing_paths);
7573 const struct got_error *close_err = got_repo_close(repo);
7578 got_worktree_close(worktree);
7580 const struct got_error *pack_err =
7581 got_repo_pack_fds_close(pack_fds);
7585 TAILQ_FOREACH(pe, &paths, entry)
7586 free((char *)pe->path);
7587 got_pathlist_free(&paths);
7595 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7596 "[-R] [patchfile]\n", getprogname());
7600 static const struct got_error *
7601 patch_from_stdin(int *patchfd)
7603 const struct got_error *err = NULL;
7605 char *path, buf[BUFSIZ];
7606 sig_t sighup, sigint, sigquit;
7608 err = got_opentemp_named_fd(&path, patchfd,
7609 GOT_TMPDIR_STR "/got-patch");
7615 sighup = signal(SIGHUP, SIG_DFL);
7616 sigint = signal(SIGINT, SIG_DFL);
7617 sigquit = signal(SIGQUIT, SIG_DFL);
7620 r = read(0, buf, sizeof(buf));
7622 err = got_error_from_errno("read");
7627 if (write(*patchfd, buf, r) == -1) {
7628 err = got_error_from_errno("write");
7633 signal(SIGHUP, sighup);
7634 signal(SIGINT, sigint);
7635 signal(SIGQUIT, sigquit);
7637 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7638 err = got_error_from_errno("lseek");
7648 static const struct got_error *
7649 patch_progress(void *arg, const char *old, const char *new,
7650 unsigned char status, const struct got_error *error, long old_from,
7651 long old_lines, long new_from, long new_lines, long offset,
7652 const struct got_error *hunk_err)
7654 const char *path = new == NULL ? old : new;
7656 while (*path == '/')
7660 printf("%c %s\n", status, path);
7663 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7665 if (offset != 0 || hunk_err != NULL) {
7666 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7667 old_lines, new_from, new_lines);
7668 if (hunk_err != NULL)
7669 printf("%s\n", hunk_err->msg);
7671 printf("applied with offset %ld\n", offset);
7677 static const struct got_error *
7678 cmd_patch(int argc, char *argv[])
7680 const struct got_error *error = NULL, *close_error = NULL;
7681 struct got_worktree *worktree = NULL;
7682 struct got_repository *repo = NULL;
7685 int ch, nop = 0, strip = -1, reverse = 0;
7687 int *pack_fds = NULL;
7689 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7695 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7697 errx(1, "pathname strip count is %s: %s",
7713 error = patch_from_stdin(&patchfd);
7716 } else if (argc == 1) {
7717 patchfd = open(argv[0], O_RDONLY);
7718 if (patchfd == -1) {
7719 error = got_error_from_errno2("open", argv[0]);
7725 if ((cwd = getcwd(NULL, 0)) == NULL) {
7726 error = got_error_from_errno("getcwd");
7730 error = got_repo_pack_fds_open(&pack_fds);
7734 error = got_worktree_open(&worktree, cwd);
7738 const char *repo_path = got_worktree_get_repo_path(worktree);
7739 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7743 error = apply_unveil(got_repo_get_path(repo), 0,
7744 got_worktree_get_root_path(worktree));
7749 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7754 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7755 &patch_progress, NULL, check_cancelled, NULL);
7759 close_error = got_repo_close(repo);
7761 error = close_error;
7763 if (worktree != NULL) {
7764 close_error = got_worktree_close(worktree);
7766 error = close_error;
7769 const struct got_error *pack_err =
7770 got_repo_pack_fds_close(pack_fds);
7781 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7782 "path ...\n", getprogname());
7786 static const struct got_error *
7787 revert_progress(void *arg, unsigned char status, const char *path)
7789 if (status == GOT_STATUS_UNVERSIONED)
7792 while (path[0] == '/')
7794 printf("%c %s\n", status, path);
7798 struct choose_patch_arg {
7799 FILE *patch_script_file;
7803 static const struct got_error *
7804 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7805 int nchanges, const char *action)
7807 const struct got_error *err;
7809 size_t linesize = 0;
7813 case GOT_STATUS_ADD:
7814 printf("A %s\n%s this addition? [y/n] ", path, action);
7816 case GOT_STATUS_DELETE:
7817 printf("D %s\n%s this deletion? [y/n] ", path, action);
7819 case GOT_STATUS_MODIFY:
7820 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7821 return got_error_from_errno("fseek");
7822 printf(GOT_COMMIT_SEP_STR);
7823 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7825 if (linelen == -1 && ferror(patch_file)) {
7826 err = got_error_from_errno("getline");
7831 printf(GOT_COMMIT_SEP_STR);
7832 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7833 path, n, nchanges, action);
7836 return got_error_path(path, GOT_ERR_FILE_STATUS);
7842 static const struct got_error *
7843 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7844 FILE *patch_file, int n, int nchanges)
7846 const struct got_error *err = NULL;
7848 size_t linesize = 0;
7851 struct choose_patch_arg *a = arg;
7853 *choice = GOT_PATCH_CHOICE_NONE;
7855 if (a->patch_script_file) {
7857 err = show_change(status, path, patch_file, n, nchanges,
7861 linelen = getline(&line, &linesize, a->patch_script_file);
7862 if (linelen == -1) {
7863 if (ferror(a->patch_script_file))
7864 return got_error_from_errno("getline");
7867 nl = strchr(line, '\n');
7870 if (strcmp(line, "y") == 0) {
7871 *choice = GOT_PATCH_CHOICE_YES;
7873 } else if (strcmp(line, "n") == 0) {
7874 *choice = GOT_PATCH_CHOICE_NO;
7876 } else if (strcmp(line, "q") == 0 &&
7877 status == GOT_STATUS_MODIFY) {
7878 *choice = GOT_PATCH_CHOICE_QUIT;
7881 printf("invalid response '%s'\n", line);
7886 while (resp != 'y' && resp != 'n' && resp != 'q') {
7887 err = show_change(status, path, patch_file, n, nchanges,
7894 if (status == GOT_STATUS_MODIFY) {
7895 if (resp != 'y' && resp != 'n' && resp != 'q') {
7896 printf("invalid response '%c'\n", resp);
7899 } else if (resp != 'y' && resp != 'n') {
7900 printf("invalid response '%c'\n", resp);
7906 *choice = GOT_PATCH_CHOICE_YES;
7907 else if (resp == 'n')
7908 *choice = GOT_PATCH_CHOICE_NO;
7909 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7910 *choice = GOT_PATCH_CHOICE_QUIT;
7915 static const struct got_error *
7916 cmd_revert(int argc, char *argv[])
7918 const struct got_error *error = NULL;
7919 struct got_worktree *worktree = NULL;
7920 struct got_repository *repo = NULL;
7921 char *cwd = NULL, *path = NULL;
7922 struct got_pathlist_head paths;
7923 struct got_pathlist_entry *pe;
7924 int ch, can_recurse = 0, pflag = 0;
7925 FILE *patch_script_file = NULL;
7926 const char *patch_script_path = NULL;
7927 struct choose_patch_arg cpa;
7928 int *pack_fds = NULL;
7932 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7938 patch_script_path = optarg;
7953 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7954 "unveil", NULL) == -1)
7959 if (patch_script_path && !pflag)
7960 errx(1, "-F option can only be used together with -p option");
7962 cwd = getcwd(NULL, 0);
7964 error = got_error_from_errno("getcwd");
7968 error = got_repo_pack_fds_open(&pack_fds);
7972 error = got_worktree_open(&worktree, cwd);
7974 if (error->code == GOT_ERR_NOT_WORKTREE)
7975 error = wrap_not_worktree_error(error, "revert", cwd);
7979 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7984 if (patch_script_path) {
7985 patch_script_file = fopen(patch_script_path, "re");
7986 if (patch_script_file == NULL) {
7987 error = got_error_from_errno2("fopen",
7992 error = apply_unveil(got_repo_get_path(repo), 1,
7993 got_worktree_get_root_path(worktree));
7997 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8004 TAILQ_FOREACH(pe, &paths, entry) {
8005 if (asprintf(&ondisk_path, "%s/%s",
8006 got_worktree_get_root_path(worktree),
8008 error = got_error_from_errno("asprintf");
8011 if (lstat(ondisk_path, &sb) == -1) {
8012 if (errno == ENOENT) {
8016 error = got_error_from_errno2("lstat",
8022 if (S_ISDIR(sb.st_mode)) {
8023 error = got_error_msg(GOT_ERR_BAD_PATH,
8024 "reverting directories requires -R option");
8030 cpa.patch_script_file = patch_script_file;
8031 cpa.action = "revert";
8032 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8033 pflag ? choose_patch : NULL, &cpa, repo);
8035 if (patch_script_file && fclose(patch_script_file) == EOF &&
8037 error = got_error_from_errno2("fclose", patch_script_path);
8039 const struct got_error *close_err = got_repo_close(repo);
8044 got_worktree_close(worktree);
8046 const struct got_error *pack_err =
8047 got_repo_pack_fds_close(pack_fds);
8059 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8060 "[path ...]\n", getprogname());
8064 struct collect_commit_logmsg_arg {
8065 const char *cmdline_log;
8066 const char *prepared_log;
8067 int non_interactive;
8069 const char *worktree_path;
8070 const char *branch_name;
8071 const char *repo_path;
8076 static const struct got_error *
8077 read_prepared_logmsg(char **logmsg, const char *path)
8079 const struct got_error *err = NULL;
8085 memset(&sb, 0, sizeof(sb));
8087 f = fopen(path, "re");
8089 return got_error_from_errno2("fopen", path);
8091 if (fstat(fileno(f), &sb) == -1) {
8092 err = got_error_from_errno2("fstat", path);
8095 if (sb.st_size == 0) {
8096 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8100 *logmsg = malloc(sb.st_size + 1);
8101 if (*logmsg == NULL) {
8102 err = got_error_from_errno("malloc");
8106 r = fread(*logmsg, 1, sb.st_size, f);
8107 if (r != sb.st_size) {
8109 err = got_error_from_errno2("fread", path);
8111 err = got_error(GOT_ERR_IO);
8114 (*logmsg)[sb.st_size] = '\0';
8116 if (fclose(f) == EOF && err == NULL)
8117 err = got_error_from_errno2("fclose", path);
8126 static const struct got_error *
8127 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8130 char *initial_content = NULL;
8131 struct got_pathlist_entry *pe;
8132 const struct got_error *err = NULL;
8133 char *template = NULL;
8134 struct collect_commit_logmsg_arg *a = arg;
8135 int initial_content_len;
8139 /* if a message was specified on the command line, just use it */
8140 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8141 len = strlen(a->cmdline_log) + 1;
8142 *logmsg = malloc(len + 1);
8143 if (*logmsg == NULL)
8144 return got_error_from_errno("malloc");
8145 strlcpy(*logmsg, a->cmdline_log, len);
8147 } else if (a->prepared_log != NULL && a->non_interactive)
8148 return read_prepared_logmsg(logmsg, a->prepared_log);
8150 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8151 return got_error_from_errno("asprintf");
8153 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8157 if (a->prepared_log) {
8159 err = read_prepared_logmsg(&msg, a->prepared_log);
8162 if (write(fd, msg, strlen(msg)) == -1) {
8163 err = got_error_from_errno2("write", a->logmsg_path);
8170 initial_content_len = asprintf(&initial_content,
8171 "\n# changes to be committed on branch %s:\n",
8173 if (initial_content_len == -1) {
8174 err = got_error_from_errno("asprintf");
8178 if (write(fd, initial_content, initial_content_len) == -1) {
8179 err = got_error_from_errno2("write", a->logmsg_path);
8183 TAILQ_FOREACH(pe, commitable_paths, entry) {
8184 struct got_commitable *ct = pe->data;
8185 dprintf(fd, "# %c %s\n",
8186 got_commitable_get_status(ct),
8187 got_commitable_get_path(ct));
8190 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8191 initial_content_len, a->prepared_log ? 0 : 1);
8193 free(initial_content);
8196 if (fd != -1 && close(fd) == -1 && err == NULL)
8197 err = got_error_from_errno2("close", a->logmsg_path);
8199 /* Editor is done; we can now apply unveil(2) */
8201 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8209 static const struct got_error *
8210 cmd_commit(int argc, char *argv[])
8212 const struct got_error *error = NULL;
8213 struct got_worktree *worktree = NULL;
8214 struct got_repository *repo = NULL;
8215 char *cwd = NULL, *id_str = NULL;
8216 struct got_object_id *id = NULL;
8217 const char *logmsg = NULL;
8218 char *prepared_logmsg = NULL;
8219 struct collect_commit_logmsg_arg cl_arg;
8220 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8221 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8222 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8223 struct got_pathlist_head paths;
8224 int *pack_fds = NULL;
8227 cl_arg.logmsg_path = NULL;
8229 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8233 option_conflict('F', 'm');
8234 prepared_logmsg = realpath(optarg, NULL);
8235 if (prepared_logmsg == NULL)
8236 return got_error_from_errno2("realpath",
8240 if (prepared_logmsg)
8241 option_conflict('m', 'F');
8245 non_interactive = 1;
8248 allow_bad_symlinks = 1;
8260 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8261 "unveil", NULL) == -1)
8264 cwd = getcwd(NULL, 0);
8266 error = got_error_from_errno("getcwd");
8270 error = got_repo_pack_fds_open(&pack_fds);
8274 error = got_worktree_open(&worktree, cwd);
8276 if (error->code == GOT_ERR_NOT_WORKTREE)
8277 error = wrap_not_worktree_error(error, "commit", cwd);
8281 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8284 if (rebase_in_progress) {
8285 error = got_error(GOT_ERR_REBASING);
8289 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8294 error = get_gitconfig_path(&gitconfig_path);
8297 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8298 gitconfig_path, pack_fds);
8302 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8305 if (merge_in_progress) {
8306 error = got_error(GOT_ERR_MERGE_BUSY);
8310 error = get_author(&author, repo, worktree);
8315 * unveil(2) traverses exec(2); if an editor is used we have
8316 * to apply unveil after the log message has been written.
8318 if (logmsg == NULL || strlen(logmsg) == 0)
8319 error = get_editor(&editor);
8321 error = apply_unveil(got_repo_get_path(repo), 0,
8322 got_worktree_get_root_path(worktree));
8326 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8330 cl_arg.editor = editor;
8331 cl_arg.cmdline_log = logmsg;
8332 cl_arg.prepared_log = prepared_logmsg;
8333 cl_arg.non_interactive = non_interactive;
8334 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8335 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8336 if (!histedit_in_progress) {
8337 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8338 error = got_error(GOT_ERR_COMMIT_BRANCH);
8341 cl_arg.branch_name += 11;
8343 cl_arg.repo_path = got_repo_get_path(repo);
8344 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8345 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8346 print_status, NULL, repo);
8348 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8349 cl_arg.logmsg_path != NULL)
8350 preserve_logmsg = 1;
8354 error = got_object_id_str(&id_str, id);
8357 printf("Created commit %s\n", id_str);
8359 if (preserve_logmsg) {
8360 fprintf(stderr, "%s: log message preserved in %s\n",
8361 getprogname(), cl_arg.logmsg_path);
8362 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8364 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8365 free(cl_arg.logmsg_path);
8367 const struct got_error *close_err = got_repo_close(repo);
8372 got_worktree_close(worktree);
8374 const struct got_error *pack_err =
8375 got_repo_pack_fds_close(pack_fds);
8381 free(gitconfig_path);
8384 free(prepared_logmsg);
8391 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8392 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8393 "[remote-repository]\n", getprogname());
8398 print_load_info(int print_colored, int print_found, int print_trees,
8399 int ncolored, int nfound, int ntrees)
8401 if (print_colored) {
8402 printf("%d commit%s colored", ncolored,
8403 ncolored == 1 ? "" : "s");
8406 printf("%s%d object%s found",
8407 ncolored > 0 ? "; " : "",
8408 nfound, nfound == 1 ? "" : "s");
8411 printf("; %d tree%s scanned", ntrees,
8412 ntrees == 1 ? "" : "s");
8416 struct got_send_progress_arg {
8417 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8424 int last_nobj_total;
8428 int printed_something;
8430 struct got_pathlist_head *delete_branches;
8433 static const struct got_error *
8434 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8435 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8436 int nobj_written, off_t bytes_sent, const char *refname, int success)
8438 struct got_send_progress_arg *a = arg;
8439 char scaled_packsize[FMT_SCALED_STRSIZE];
8440 char scaled_sent[FMT_SCALED_STRSIZE];
8441 int p_deltify = 0, p_written = 0, p_sent = 0;
8442 int print_colored = 0, print_found = 0, print_trees = 0;
8443 int print_searching = 0, print_total = 0;
8444 int print_deltify = 0, print_written = 0, print_sent = 0;
8446 if (a->verbosity < 0)
8450 const char *status = success ? "accepted" : "rejected";
8453 struct got_pathlist_entry *pe;
8454 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8455 const char *branchname = pe->path;
8456 if (got_path_cmp(branchname, refname,
8457 strlen(branchname), strlen(refname)) == 0) {
8459 a->sent_something = 1;
8465 if (a->printed_something)
8467 printf("Server has %s %s", status, refname);
8468 a->printed_something = 1;
8472 if (a->last_ncolored != ncolored) {
8474 a->last_ncolored = ncolored;
8477 if (a->last_nfound != nfound) {
8480 a->last_nfound = nfound;
8483 if (a->last_ntrees != ntrees) {
8487 a->last_ntrees = ntrees;
8490 if ((print_colored || print_found || print_trees) &&
8493 print_load_info(print_colored, print_found, print_trees,
8494 ncolored, nfound, ntrees);
8495 a->printed_something = 1;
8498 } else if (!a->loading_done) {
8500 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8502 a->loading_done = 1;
8505 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8506 return got_error_from_errno("fmt_scaled");
8507 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8508 return got_error_from_errno("fmt_scaled");
8510 if (a->last_ncommits != ncommits) {
8511 print_searching = 1;
8512 a->last_ncommits = ncommits;
8515 if (a->last_nobj_total != nobj_total) {
8516 print_searching = 1;
8518 a->last_nobj_total = nobj_total;
8521 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8522 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8523 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8524 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8525 return got_error(GOT_ERR_NO_SPACE);
8528 if (nobj_deltify > 0 || nobj_written > 0) {
8529 if (nobj_deltify > 0) {
8530 p_deltify = (nobj_deltify * 100) / nobj_total;
8531 if (p_deltify != a->last_p_deltify) {
8532 a->last_p_deltify = p_deltify;
8533 print_searching = 1;
8538 if (nobj_written > 0) {
8539 p_written = (nobj_written * 100) / nobj_total;
8540 if (p_written != a->last_p_written) {
8541 a->last_p_written = p_written;
8542 print_searching = 1;
8550 if (bytes_sent > 0) {
8551 p_sent = (bytes_sent * 100) / packfile_size;
8552 if (p_sent != a->last_p_sent) {
8553 a->last_p_sent = p_sent;
8554 print_searching = 1;
8560 a->sent_something = 1;
8563 if (print_searching || print_total || print_deltify || print_written ||
8566 if (print_searching)
8567 printf("packing %d reference%s", ncommits,
8568 ncommits == 1 ? "" : "s");
8570 printf("; %d object%s", nobj_total,
8571 nobj_total == 1 ? "" : "s");
8573 printf("; deltify: %d%%", p_deltify);
8575 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8576 scaled_packsize, p_sent);
8577 else if (print_written)
8578 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8579 scaled_packsize, p_written);
8580 if (print_searching || print_total || print_deltify ||
8581 print_written || print_sent) {
8582 a->printed_something = 1;
8588 static const struct got_error *
8589 cmd_send(int argc, char *argv[])
8591 const struct got_error *error = NULL;
8592 char *cwd = NULL, *repo_path = NULL;
8593 const char *remote_name;
8594 char *proto = NULL, *host = NULL, *port = NULL;
8595 char *repo_name = NULL, *server_path = NULL;
8596 const struct got_remote_repo *remotes, *remote = NULL;
8597 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8598 struct got_repository *repo = NULL;
8599 struct got_worktree *worktree = NULL;
8600 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8601 struct got_pathlist_head branches;
8602 struct got_pathlist_head tags;
8603 struct got_reflist_head all_branches;
8604 struct got_reflist_head all_tags;
8605 struct got_pathlist_head delete_args;
8606 struct got_pathlist_head delete_branches;
8607 struct got_reflist_entry *re;
8608 struct got_pathlist_entry *pe;
8609 int i, ch, sendfd = -1, sendstatus;
8611 struct got_send_progress_arg spa;
8612 int verbosity = 0, overwrite_refs = 0;
8613 int send_all_branches = 0, send_all_tags = 0;
8614 struct got_reference *ref = NULL;
8615 int *pack_fds = NULL;
8617 TAILQ_INIT(&branches);
8619 TAILQ_INIT(&all_branches);
8620 TAILQ_INIT(&all_tags);
8621 TAILQ_INIT(&delete_args);
8622 TAILQ_INIT(&delete_branches);
8624 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8627 send_all_branches = 1;
8630 error = got_pathlist_append(&branches, optarg, NULL);
8636 error = got_pathlist_append(&delete_args, optarg, NULL);
8644 repo_path = realpath(optarg, NULL);
8645 if (repo_path == NULL)
8646 return got_error_from_errno2("realpath",
8648 got_path_strip_trailing_slashes(repo_path);
8651 error = got_pathlist_append(&tags, optarg, NULL);
8662 else if (verbosity < 3)
8676 if (send_all_branches && !TAILQ_EMPTY(&branches))
8677 option_conflict('a', 'b');
8678 if (send_all_tags && !TAILQ_EMPTY(&tags))
8679 option_conflict('T', 't');
8683 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8685 remote_name = argv[0];
8689 cwd = getcwd(NULL, 0);
8691 error = got_error_from_errno("getcwd");
8695 error = got_repo_pack_fds_open(&pack_fds);
8699 if (repo_path == NULL) {
8700 error = got_worktree_open(&worktree, cwd);
8701 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8707 strdup(got_worktree_get_repo_path(worktree));
8708 if (repo_path == NULL)
8709 error = got_error_from_errno("strdup");
8713 repo_path = strdup(cwd);
8714 if (repo_path == NULL) {
8715 error = got_error_from_errno("strdup");
8721 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8726 worktree_conf = got_worktree_get_gotconfig(worktree);
8727 if (worktree_conf) {
8728 got_gotconfig_get_remotes(&nremotes, &remotes,
8730 for (i = 0; i < nremotes; i++) {
8731 if (strcmp(remotes[i].name, remote_name) == 0) {
8732 remote = &remotes[i];
8738 if (remote == NULL) {
8739 repo_conf = got_repo_get_gotconfig(repo);
8741 got_gotconfig_get_remotes(&nremotes, &remotes,
8743 for (i = 0; i < nremotes; i++) {
8744 if (strcmp(remotes[i].name, remote_name) == 0) {
8745 remote = &remotes[i];
8751 if (remote == NULL) {
8752 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8753 for (i = 0; i < nremotes; i++) {
8754 if (strcmp(remotes[i].name, remote_name) == 0) {
8755 remote = &remotes[i];
8760 if (remote == NULL) {
8761 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8765 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8766 &repo_name, remote->send_url);
8770 if (strcmp(proto, "git") == 0) {
8772 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8773 "sendfd dns inet unveil", NULL) == -1)
8776 } else if (strcmp(proto, "git+ssh") == 0 ||
8777 strcmp(proto, "ssh") == 0) {
8779 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8780 "sendfd unveil", NULL) == -1)
8783 } else if (strcmp(proto, "http") == 0 ||
8784 strcmp(proto, "git+http") == 0) {
8785 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8788 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8792 error = got_dial_apply_unveil(proto);
8796 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8800 if (send_all_branches) {
8801 error = got_ref_list(&all_branches, repo, "refs/heads",
8802 got_ref_cmp_by_name, NULL);
8805 TAILQ_FOREACH(re, &all_branches, entry) {
8806 const char *branchname = got_ref_get_name(re->ref);
8807 error = got_pathlist_append(&branches,
8813 } else if (nbranches == 0) {
8814 for (i = 0; i < remote->nsend_branches; i++) {
8815 got_pathlist_append(&branches,
8816 remote->send_branches[i], NULL);
8820 if (send_all_tags) {
8821 error = got_ref_list(&all_tags, repo, "refs/tags",
8822 got_ref_cmp_by_name, NULL);
8825 TAILQ_FOREACH(re, &all_tags, entry) {
8826 const char *tagname = got_ref_get_name(re->ref);
8827 error = got_pathlist_append(&tags,
8836 * To prevent accidents only branches in refs/heads/ can be deleted
8837 * with 'got send -d'.
8838 * Deleting anything else requires local repository access or Git.
8840 TAILQ_FOREACH(pe, &delete_args, entry) {
8841 const char *branchname = pe->path;
8843 struct got_pathlist_entry *new;
8844 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8845 s = strdup(branchname);
8847 error = got_error_from_errno("strdup");
8851 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8852 error = got_error_from_errno("asprintf");
8856 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8857 if (error || new == NULL /* duplicate */)
8864 if (nbranches == 0 && ndelete_branches == 0) {
8865 struct got_reference *head_ref;
8867 error = got_ref_open(&head_ref, repo,
8868 got_worktree_get_head_ref_name(worktree), 0);
8870 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8873 if (got_ref_is_symbolic(head_ref)) {
8874 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8875 got_ref_close(head_ref);
8880 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8888 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8889 port ? ":" : "", port ? port : "");
8891 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8892 server_path, verbosity);
8896 memset(&spa, 0, sizeof(spa));
8897 spa.last_scaled_packsize[0] = '\0';
8898 spa.last_p_deltify = -1;
8899 spa.last_p_written = -1;
8900 spa.verbosity = verbosity;
8901 spa.delete_branches = &delete_branches;
8902 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8903 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8904 check_cancelled, NULL);
8905 if (spa.printed_something)
8909 if (!spa.sent_something && verbosity >= 0)
8910 printf("Already up-to-date\n");
8913 if (kill(sendpid, SIGTERM) == -1)
8914 error = got_error_from_errno("kill");
8915 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8916 error = got_error_from_errno("waitpid");
8918 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8919 error = got_error_from_errno("close");
8921 const struct got_error *close_err = got_repo_close(repo);
8926 got_worktree_close(worktree);
8928 const struct got_error *pack_err =
8929 got_repo_pack_fds_close(pack_fds);
8935 got_pathlist_free(&branches);
8936 got_pathlist_free(&tags);
8937 got_ref_list_free(&all_branches);
8938 got_ref_list_free(&all_tags);
8939 got_pathlist_free(&delete_args);
8940 TAILQ_FOREACH(pe, &delete_branches, entry)
8941 free((char *)pe->path);
8942 got_pathlist_free(&delete_branches);
8954 usage_cherrypick(void)
8956 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8960 static const struct got_error *
8961 cmd_cherrypick(int argc, char *argv[])
8963 const struct got_error *error = NULL;
8964 struct got_worktree *worktree = NULL;
8965 struct got_repository *repo = NULL;
8966 char *cwd = NULL, *commit_id_str = NULL;
8967 struct got_object_id *commit_id = NULL;
8968 struct got_commit_object *commit = NULL;
8969 struct got_object_qid *pid;
8971 struct got_update_progress_arg upa;
8972 int *pack_fds = NULL;
8974 while ((ch = getopt(argc, argv, "")) != -1) {
8986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8987 "unveil", NULL) == -1)
8993 cwd = getcwd(NULL, 0);
8995 error = got_error_from_errno("getcwd");
8999 error = got_repo_pack_fds_open(&pack_fds);
9003 error = got_worktree_open(&worktree, cwd);
9005 if (error->code == GOT_ERR_NOT_WORKTREE)
9006 error = wrap_not_worktree_error(error, "cherrypick",
9011 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9016 error = apply_unveil(got_repo_get_path(repo), 0,
9017 got_worktree_get_root_path(worktree));
9021 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9022 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9025 error = got_object_id_str(&commit_id_str, commit_id);
9029 error = got_object_open_as_commit(&commit, repo, commit_id);
9032 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9033 memset(&upa, 0, sizeof(upa));
9034 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9035 commit_id, repo, update_progress, &upa, check_cancelled,
9040 if (upa.did_something)
9041 printf("Merged commit %s\n", commit_id_str);
9042 print_merge_progress_stats(&upa);
9045 got_object_commit_close(commit);
9046 free(commit_id_str);
9048 got_worktree_close(worktree);
9050 const struct got_error *close_err = got_repo_close(repo);
9055 const struct got_error *pack_err =
9056 got_repo_pack_fds_close(pack_fds);
9067 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9071 static const struct got_error *
9072 cmd_backout(int argc, char *argv[])
9074 const struct got_error *error = NULL;
9075 struct got_worktree *worktree = NULL;
9076 struct got_repository *repo = NULL;
9077 char *cwd = NULL, *commit_id_str = NULL;
9078 struct got_object_id *commit_id = NULL;
9079 struct got_commit_object *commit = NULL;
9080 struct got_object_qid *pid;
9082 struct got_update_progress_arg upa;
9083 int *pack_fds = NULL;
9085 while ((ch = getopt(argc, argv, "")) != -1) {
9097 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9098 "unveil", NULL) == -1)
9104 cwd = getcwd(NULL, 0);
9106 error = got_error_from_errno("getcwd");
9110 error = got_repo_pack_fds_open(&pack_fds);
9114 error = got_worktree_open(&worktree, cwd);
9116 if (error->code == GOT_ERR_NOT_WORKTREE)
9117 error = wrap_not_worktree_error(error, "backout", cwd);
9121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9126 error = apply_unveil(got_repo_get_path(repo), 0,
9127 got_worktree_get_root_path(worktree));
9131 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9132 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9135 error = got_object_id_str(&commit_id_str, commit_id);
9139 error = got_object_open_as_commit(&commit, repo, commit_id);
9142 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9144 error = got_error(GOT_ERR_ROOT_COMMIT);
9148 memset(&upa, 0, sizeof(upa));
9149 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9150 repo, update_progress, &upa, check_cancelled, NULL);
9154 if (upa.did_something)
9155 printf("Backed out commit %s\n", commit_id_str);
9156 print_merge_progress_stats(&upa);
9159 got_object_commit_close(commit);
9160 free(commit_id_str);
9162 got_worktree_close(worktree);
9164 const struct got_error *close_err = got_repo_close(repo);
9169 const struct got_error *pack_err =
9170 got_repo_pack_fds_close(pack_fds);
9180 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9186 trim_logmsg(char *logmsg, int limit)
9191 len = strlen(logmsg);
9195 nl = strchr(logmsg, '\n');
9200 static const struct got_error *
9201 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9203 const struct got_error *err;
9204 char *logmsg0 = NULL;
9207 err = got_object_commit_get_logmsg(&logmsg0, commit);
9212 while (isspace((unsigned char)s[0]))
9215 *logmsg = strdup(s);
9216 if (*logmsg == NULL) {
9217 err = got_error_from_errno("strdup");
9221 trim_logmsg(*logmsg, limit);
9227 static const struct got_error *
9228 show_rebase_merge_conflict(struct got_object_id *id,
9229 struct got_repository *repo)
9231 const struct got_error *err;
9232 struct got_commit_object *commit = NULL;
9233 char *id_str = NULL, *logmsg = NULL;
9235 err = got_object_open_as_commit(&commit, repo, id);
9239 err = got_object_id_str(&id_str, id);
9245 err = get_short_logmsg(&logmsg, 42, commit);
9249 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9252 got_object_commit_close(commit);
9257 static const struct got_error *
9258 show_rebase_progress(struct got_commit_object *commit,
9259 struct got_object_id *old_id, struct got_object_id *new_id)
9261 const struct got_error *err;
9262 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9264 err = got_object_id_str(&old_id_str, old_id);
9269 err = got_object_id_str(&new_id_str, new_id);
9274 old_id_str[12] = '\0';
9276 new_id_str[12] = '\0';
9278 err = get_short_logmsg(&logmsg, 42, commit);
9282 printf("%s -> %s: %s\n", old_id_str,
9283 new_id_str ? new_id_str : "no-op change", logmsg);
9291 static const struct got_error *
9292 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9293 struct got_reference *branch, struct got_reference *new_base_branch,
9294 struct got_reference *tmp_branch, struct got_repository *repo,
9297 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9298 return got_worktree_rebase_complete(worktree, fileindex,
9299 new_base_branch, tmp_branch, branch, repo, create_backup);
9302 static const struct got_error *
9303 rebase_commit(struct got_pathlist_head *merged_paths,
9304 struct got_worktree *worktree, struct got_fileindex *fileindex,
9305 struct got_reference *tmp_branch,
9306 struct got_object_id *commit_id, struct got_repository *repo)
9308 const struct got_error *error;
9309 struct got_commit_object *commit;
9310 struct got_object_id *new_commit_id;
9312 error = got_object_open_as_commit(&commit, repo, commit_id);
9316 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9317 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9319 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9321 error = show_rebase_progress(commit, commit_id, NULL);
9323 error = show_rebase_progress(commit, commit_id, new_commit_id);
9324 free(new_commit_id);
9327 got_object_commit_close(commit);
9331 struct check_path_prefix_arg {
9332 const char *path_prefix;
9337 static const struct got_error *
9338 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9339 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9340 struct got_object_id *id1, struct got_object_id *id2,
9341 const char *path1, const char *path2,
9342 mode_t mode1, mode_t mode2, struct got_repository *repo)
9344 struct check_path_prefix_arg *a = arg;
9346 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9347 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9348 return got_error(a->errcode);
9353 static const struct got_error *
9354 check_path_prefix(struct got_object_id *parent_id,
9355 struct got_object_id *commit_id, const char *path_prefix,
9356 int errcode, struct got_repository *repo)
9358 const struct got_error *err;
9359 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9360 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9361 struct check_path_prefix_arg cpp_arg;
9363 if (got_path_is_root_dir(path_prefix))
9366 err = got_object_open_as_commit(&commit, repo, commit_id);
9370 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9374 err = got_object_open_as_tree(&tree1, repo,
9375 got_object_commit_get_tree_id(parent_commit));
9379 err = got_object_open_as_tree(&tree2, repo,
9380 got_object_commit_get_tree_id(commit));
9384 cpp_arg.path_prefix = path_prefix;
9385 while (cpp_arg.path_prefix[0] == '/')
9386 cpp_arg.path_prefix++;
9387 cpp_arg.len = strlen(cpp_arg.path_prefix);
9388 cpp_arg.errcode = errcode;
9389 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
9390 check_path_prefix_in_diff, &cpp_arg, 0);
9393 got_object_tree_close(tree1);
9395 got_object_tree_close(tree2);
9397 got_object_commit_close(commit);
9399 got_object_commit_close(parent_commit);
9403 static const struct got_error *
9404 collect_commits(struct got_object_id_queue *commits,
9405 struct got_object_id *initial_commit_id,
9406 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9407 const char *path_prefix, int path_prefix_errcode,
9408 struct got_repository *repo)
9410 const struct got_error *err = NULL;
9411 struct got_commit_graph *graph = NULL;
9412 struct got_object_id *parent_id = NULL;
9413 struct got_object_qid *qid;
9414 struct got_object_id *commit_id = initial_commit_id;
9416 err = got_commit_graph_open(&graph, "/", 1);
9420 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9421 check_cancelled, NULL);
9424 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9425 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9426 check_cancelled, NULL);
9428 if (err->code == GOT_ERR_ITER_COMPLETED) {
9429 err = got_error_msg(GOT_ERR_ANCESTRY,
9430 "ran out of commits to rebase before "
9431 "youngest common ancestor commit has "
9436 err = check_path_prefix(parent_id, commit_id,
9437 path_prefix, path_prefix_errcode, repo);
9441 err = got_object_qid_alloc(&qid, commit_id);
9444 STAILQ_INSERT_HEAD(commits, qid, entry);
9445 commit_id = parent_id;
9449 got_commit_graph_close(graph);
9453 static const struct got_error *
9454 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9456 const struct got_error *err = NULL;
9457 time_t committer_time;
9459 char datebuf[11]; /* YYYY-MM-DD + NUL */
9460 char *author0 = NULL, *author, *smallerthan;
9461 char *logmsg0 = NULL, *logmsg, *newline;
9463 committer_time = got_object_commit_get_committer_time(commit);
9464 if (gmtime_r(&committer_time, &tm) == NULL)
9465 return got_error_from_errno("gmtime_r");
9466 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9467 return got_error(GOT_ERR_NO_SPACE);
9469 author0 = strdup(got_object_commit_get_author(commit));
9470 if (author0 == NULL)
9471 return got_error_from_errno("strdup");
9473 smallerthan = strchr(author, '<');
9474 if (smallerthan && smallerthan[1] != '\0')
9475 author = smallerthan + 1;
9476 author[strcspn(author, "@>")] = '\0';
9478 err = got_object_commit_get_logmsg(&logmsg0, commit);
9482 while (*logmsg == '\n')
9484 newline = strchr(logmsg, '\n');
9488 if (asprintf(brief_str, "%s %s %s",
9489 datebuf, author, logmsg) == -1)
9490 err = got_error_from_errno("asprintf");
9497 static const struct got_error *
9498 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9499 struct got_repository *repo)
9501 const struct got_error *err;
9504 err = got_object_id_str(&id_str, id);
9508 err = got_ref_delete(ref, repo);
9512 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9518 static const struct got_error *
9519 print_backup_ref(const char *branch_name, const char *new_id_str,
9520 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9521 struct got_reflist_object_id_map *refs_idmap,
9522 struct got_repository *repo)
9524 const struct got_error *err = NULL;
9525 struct got_reflist_head *refs;
9526 char *refs_str = NULL;
9527 struct got_object_id *new_commit_id = NULL;
9528 struct got_commit_object *new_commit = NULL;
9529 char *new_commit_brief_str = NULL;
9530 struct got_object_id *yca_id = NULL;
9531 struct got_commit_object *yca_commit = NULL;
9532 char *yca_id_str = NULL, *yca_brief_str = NULL;
9533 char *custom_refs_str;
9535 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9536 return got_error_from_errno("asprintf");
9538 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9539 0, 0, refs_idmap, custom_refs_str);
9543 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9547 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9549 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9554 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9558 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9562 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9563 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9567 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9568 refs_str ? " (" : "", refs_str ? refs_str : "",
9569 refs_str ? ")" : "", new_commit_brief_str);
9570 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9571 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9575 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9579 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9583 err = got_object_id_str(&yca_id_str, yca_id);
9587 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9589 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9593 printf("history forked at %s%s%s%s\n %s\n",
9595 refs_str ? " (" : "", refs_str ? refs_str : "",
9596 refs_str ? ")" : "", yca_brief_str);
9599 free(custom_refs_str);
9600 free(new_commit_id);
9604 free(yca_brief_str);
9606 got_object_commit_close(new_commit);
9608 got_object_commit_close(yca_commit);
9613 static const struct got_error *
9614 process_backup_refs(const char *backup_ref_prefix,
9615 const char *wanted_branch_name,
9616 int delete, struct got_repository *repo)
9618 const struct got_error *err;
9619 struct got_reflist_head refs, backup_refs;
9620 struct got_reflist_entry *re;
9621 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9622 struct got_object_id *old_commit_id = NULL;
9623 char *branch_name = NULL;
9624 struct got_commit_object *old_commit = NULL;
9625 struct got_reflist_object_id_map *refs_idmap = NULL;
9626 int wanted_branch_found = 0;
9629 TAILQ_INIT(&backup_refs);
9631 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9635 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9639 if (wanted_branch_name) {
9640 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9641 wanted_branch_name += 11;
9644 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9645 got_ref_cmp_by_commit_timestamp_descending, repo);
9649 TAILQ_FOREACH(re, &backup_refs, entry) {
9650 const char *refname = got_ref_get_name(re->ref);
9653 err = check_cancelled(NULL);
9657 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9661 err = got_object_open_as_commit(&old_commit, repo,
9666 if (strncmp(backup_ref_prefix, refname,
9667 backup_ref_prefix_len) == 0)
9668 refname += backup_ref_prefix_len;
9670 while (refname[0] == '/')
9673 branch_name = strdup(refname);
9674 if (branch_name == NULL) {
9675 err = got_error_from_errno("strdup");
9678 slash = strrchr(branch_name, '/');
9681 refname += strlen(branch_name) + 1;
9684 if (wanted_branch_name == NULL ||
9685 strcmp(wanted_branch_name, branch_name) == 0) {
9686 wanted_branch_found = 1;
9688 err = delete_backup_ref(re->ref,
9689 old_commit_id, repo);
9691 err = print_backup_ref(branch_name, refname,
9692 old_commit_id, old_commit, refs_idmap,
9699 free(old_commit_id);
9700 old_commit_id = NULL;
9703 got_object_commit_close(old_commit);
9707 if (wanted_branch_name && !wanted_branch_found) {
9708 err = got_error_fmt(GOT_ERR_NOT_REF,
9709 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9713 got_reflist_object_id_map_free(refs_idmap);
9714 got_ref_list_free(&refs);
9715 got_ref_list_free(&backup_refs);
9716 free(old_commit_id);
9719 got_object_commit_close(old_commit);
9723 static const struct got_error *
9724 abort_progress(void *arg, unsigned char status, const char *path)
9727 * Unversioned files should not clutter progress output when
9728 * an operation is aborted.
9730 if (status == GOT_STATUS_UNVERSIONED)
9733 return update_progress(arg, status, path);
9736 static const struct got_error *
9737 cmd_rebase(int argc, char *argv[])
9739 const struct got_error *error = NULL;
9740 struct got_worktree *worktree = NULL;
9741 struct got_repository *repo = NULL;
9742 struct got_fileindex *fileindex = NULL;
9744 struct got_reference *branch = NULL;
9745 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9746 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9747 struct got_object_id *resume_commit_id = NULL;
9748 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9749 struct got_commit_object *commit = NULL;
9750 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9751 int histedit_in_progress = 0, merge_in_progress = 0;
9752 int create_backup = 1, list_backups = 0, delete_backups = 0;
9753 struct got_object_id_queue commits;
9754 struct got_pathlist_head merged_paths;
9755 const struct got_object_id_queue *parent_ids;
9756 struct got_object_qid *qid, *pid;
9757 struct got_update_progress_arg upa;
9758 int *pack_fds = NULL;
9760 STAILQ_INIT(&commits);
9761 TAILQ_INIT(&merged_paths);
9762 memset(&upa, 0, sizeof(upa));
9764 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9770 continue_rebase = 1;
9788 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9789 "unveil", NULL) == -1)
9794 option_conflict('l', 'a');
9795 if (continue_rebase)
9796 option_conflict('l', 'c');
9798 option_conflict('l', 'X');
9799 if (argc != 0 && argc != 1)
9801 } else if (delete_backups) {
9803 option_conflict('X', 'a');
9804 if (continue_rebase)
9805 option_conflict('X', 'c');
9807 option_conflict('l', 'X');
9808 if (argc != 0 && argc != 1)
9811 if (abort_rebase && continue_rebase)
9813 else if (abort_rebase || continue_rebase) {
9816 } else if (argc != 1)
9820 cwd = getcwd(NULL, 0);
9822 error = got_error_from_errno("getcwd");
9826 error = got_repo_pack_fds_open(&pack_fds);
9830 error = got_worktree_open(&worktree, cwd);
9832 if (list_backups || delete_backups) {
9833 if (error->code != GOT_ERR_NOT_WORKTREE)
9836 if (error->code == GOT_ERR_NOT_WORKTREE)
9837 error = wrap_not_worktree_error(error,
9843 error = got_repo_open(&repo,
9844 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9849 error = apply_unveil(got_repo_get_path(repo), 0,
9850 worktree ? got_worktree_get_root_path(worktree) : NULL);
9854 if (list_backups || delete_backups) {
9855 error = process_backup_refs(
9856 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9857 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9858 goto done; /* nothing else to do */
9861 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9865 if (histedit_in_progress) {
9866 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9870 error = got_worktree_merge_in_progress(&merge_in_progress,
9874 if (merge_in_progress) {
9875 error = got_error(GOT_ERR_MERGE_BUSY);
9879 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9884 if (!rebase_in_progress) {
9885 error = got_error(GOT_ERR_NOT_REBASING);
9888 error = got_worktree_rebase_continue(&resume_commit_id,
9889 &new_base_branch, &tmp_branch, &branch, &fileindex,
9893 printf("Switching work tree to %s\n",
9894 got_ref_get_symref_target(new_base_branch));
9895 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9896 new_base_branch, abort_progress, &upa);
9899 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9900 print_merge_progress_stats(&upa);
9901 goto done; /* nothing else to do */
9904 if (continue_rebase) {
9905 if (!rebase_in_progress) {
9906 error = got_error(GOT_ERR_NOT_REBASING);
9909 error = got_worktree_rebase_continue(&resume_commit_id,
9910 &new_base_branch, &tmp_branch, &branch, &fileindex,
9915 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9916 resume_commit_id, repo);
9920 yca_id = got_object_id_dup(resume_commit_id);
9921 if (yca_id == NULL) {
9922 error = got_error_from_errno("got_object_id_dup");
9926 error = got_ref_open(&branch, repo, argv[0], 0);
9931 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9935 if (!continue_rebase) {
9936 struct got_object_id *base_commit_id;
9938 base_commit_id = got_worktree_get_base_commit_id(worktree);
9939 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9940 base_commit_id, branch_head_commit_id, 1, repo,
9941 check_cancelled, NULL);
9944 if (yca_id == NULL) {
9945 error = got_error_msg(GOT_ERR_ANCESTRY,
9946 "specified branch shares no common ancestry "
9947 "with work tree's branch");
9951 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9953 if (error->code != GOT_ERR_ANCESTRY)
9957 struct got_pathlist_head paths;
9958 printf("%s is already based on %s\n",
9959 got_ref_get_name(branch),
9960 got_worktree_get_head_ref_name(worktree));
9961 error = switch_head_ref(branch, branch_head_commit_id,
9965 error = got_worktree_set_base_commit_id(worktree, repo,
9966 branch_head_commit_id);
9970 error = got_pathlist_append(&paths, "", NULL);
9973 error = got_worktree_checkout_files(worktree,
9974 &paths, repo, update_progress, &upa,
9975 check_cancelled, NULL);
9976 got_pathlist_free(&paths);
9979 if (upa.did_something) {
9981 error = got_object_id_str(&id_str,
9982 branch_head_commit_id);
9985 printf("Updated to %s: %s\n",
9986 got_worktree_get_head_ref_name(worktree),
9990 printf("Already up-to-date\n");
9991 print_update_progress_stats(&upa);
9996 commit_id = branch_head_commit_id;
9997 error = got_object_open_as_commit(&commit, repo, commit_id);
10001 parent_ids = got_object_commit_get_parent_ids(commit);
10002 pid = STAILQ_FIRST(parent_ids);
10004 error = got_error(GOT_ERR_EMPTY_REBASE);
10007 error = collect_commits(&commits, commit_id, &pid->id,
10008 yca_id, got_worktree_get_path_prefix(worktree),
10009 GOT_ERR_REBASE_PATH, repo);
10010 got_object_commit_close(commit);
10015 if (!continue_rebase) {
10016 error = got_worktree_rebase_prepare(&new_base_branch,
10017 &tmp_branch, &fileindex, worktree, branch, repo);
10022 if (STAILQ_EMPTY(&commits)) {
10023 if (continue_rebase) {
10024 error = rebase_complete(worktree, fileindex,
10025 branch, new_base_branch, tmp_branch, repo,
10029 /* Fast-forward the reference of the branch. */
10030 struct got_object_id *new_head_commit_id;
10032 error = got_ref_resolve(&new_head_commit_id, repo,
10036 error = got_object_id_str(&id_str, new_head_commit_id);
10037 printf("Forwarding %s to commit %s\n",
10038 got_ref_get_name(branch), id_str);
10040 error = got_ref_change_ref(branch,
10041 new_head_commit_id);
10044 /* No backup needed since objects did not change. */
10050 STAILQ_FOREACH(qid, &commits, entry) {
10052 commit_id = &qid->id;
10053 parent_id = pid ? &pid->id : yca_id;
10056 memset(&upa, 0, sizeof(upa));
10057 error = got_worktree_rebase_merge_files(&merged_paths,
10058 worktree, fileindex, parent_id, commit_id, repo,
10059 update_progress, &upa, check_cancelled, NULL);
10063 print_merge_progress_stats(&upa);
10064 if (upa.conflicts > 0 || upa.missing > 0 ||
10065 upa.not_deleted > 0 || upa.unversioned > 0) {
10066 if (upa.conflicts > 0) {
10067 error = show_rebase_merge_conflict(&qid->id,
10072 got_worktree_rebase_pathlist_free(&merged_paths);
10076 error = rebase_commit(&merged_paths, worktree, fileindex,
10077 tmp_branch, commit_id, repo);
10078 got_worktree_rebase_pathlist_free(&merged_paths);
10083 if (upa.conflicts > 0 || upa.missing > 0 ||
10084 upa.not_deleted > 0 || upa.unversioned > 0) {
10085 error = got_worktree_rebase_postpone(worktree, fileindex);
10088 if (upa.conflicts > 0 && upa.missing == 0 &&
10089 upa.not_deleted == 0 && upa.unversioned == 0) {
10090 error = got_error_msg(GOT_ERR_CONFLICTS,
10091 "conflicts must be resolved before rebasing "
10093 } else if (upa.conflicts > 0) {
10094 error = got_error_msg(GOT_ERR_CONFLICTS,
10095 "conflicts must be resolved before rebasing "
10096 "can continue; changes destined for some "
10097 "files were not yet merged and should be "
10098 "merged manually if required before the "
10099 "rebase operation is continued");
10101 error = got_error_msg(GOT_ERR_CONFLICTS,
10102 "changes destined for some files were not "
10103 "yet merged and should be merged manually "
10104 "if required before the rebase operation "
10108 error = rebase_complete(worktree, fileindex, branch,
10109 new_base_branch, tmp_branch, repo, create_backup);
10111 got_object_id_queue_free(&commits);
10112 free(branch_head_commit_id);
10113 free(resume_commit_id);
10116 got_object_commit_close(commit);
10118 got_ref_close(branch);
10119 if (new_base_branch)
10120 got_ref_close(new_base_branch);
10122 got_ref_close(tmp_branch);
10124 got_worktree_close(worktree);
10126 const struct got_error *close_err = got_repo_close(repo);
10131 const struct got_error *pack_err =
10132 got_repo_pack_fds_close(pack_fds);
10140 usage_histedit(void)
10142 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10143 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10148 #define GOT_HISTEDIT_PICK 'p'
10149 #define GOT_HISTEDIT_EDIT 'e'
10150 #define GOT_HISTEDIT_FOLD 'f'
10151 #define GOT_HISTEDIT_DROP 'd'
10152 #define GOT_HISTEDIT_MESG 'm'
10154 static const struct got_histedit_cmd {
10155 unsigned char code;
10158 } got_histedit_cmds[] = {
10159 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10160 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10161 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10163 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10164 { GOT_HISTEDIT_MESG, "mesg",
10165 "single-line log message for commit above (open editor if empty)" },
10168 struct got_histedit_list_entry {
10169 TAILQ_ENTRY(got_histedit_list_entry) entry;
10170 struct got_object_id *commit_id;
10171 const struct got_histedit_cmd *cmd;
10174 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10176 static const struct got_error *
10177 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10178 FILE *f, struct got_repository *repo)
10180 const struct got_error *err = NULL;
10181 char *logmsg = NULL, *id_str = NULL;
10182 struct got_commit_object *commit = NULL;
10185 err = got_object_open_as_commit(&commit, repo, commit_id);
10189 err = get_short_logmsg(&logmsg, 34, commit);
10193 err = got_object_id_str(&id_str, commit_id);
10197 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10199 err = got_ferror(f, GOT_ERR_IO);
10202 got_object_commit_close(commit);
10208 static const struct got_error *
10209 histedit_write_commit_list(struct got_object_id_queue *commits,
10210 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10211 struct got_repository *repo)
10213 const struct got_error *err = NULL;
10214 struct got_object_qid *qid;
10215 const char *histedit_cmd = NULL;
10217 if (STAILQ_EMPTY(commits))
10218 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10220 STAILQ_FOREACH(qid, commits, entry) {
10221 histedit_cmd = got_histedit_cmds[0].name;
10223 histedit_cmd = "edit";
10224 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10225 histedit_cmd = "fold";
10226 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10229 if (edit_logmsg_only) {
10230 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10232 err = got_ferror(f, GOT_ERR_IO);
10241 static const struct got_error *
10242 write_cmd_list(FILE *f, const char *branch_name,
10243 struct got_object_id_queue *commits)
10245 const struct got_error *err = NULL;
10249 struct got_object_qid *qid;
10251 qid = STAILQ_FIRST(commits);
10252 err = got_object_id_str(&id_str, &qid->id);
10257 "# Editing the history of branch '%s' starting at\n"
10259 "# Commits will be processed in order from top to "
10260 "bottom of this file.\n", branch_name, id_str);
10262 err = got_ferror(f, GOT_ERR_IO);
10266 n = fprintf(f, "# Available histedit commands:\n");
10268 err = got_ferror(f, GOT_ERR_IO);
10272 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10273 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10274 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10277 err = got_ferror(f, GOT_ERR_IO);
10286 static const struct got_error *
10287 histedit_syntax_error(int lineno)
10289 static char msg[42];
10292 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10294 if (ret == -1 || ret >= sizeof(msg))
10295 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10297 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10300 static const struct got_error *
10301 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10302 char *logmsg, struct got_repository *repo)
10304 const struct got_error *err;
10305 struct got_commit_object *folded_commit = NULL;
10306 char *id_str, *folded_logmsg = NULL;
10308 err = got_object_id_str(&id_str, hle->commit_id);
10312 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10316 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10319 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10320 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10321 folded_logmsg) == -1) {
10322 err = got_error_from_errno("asprintf");
10326 got_object_commit_close(folded_commit);
10328 free(folded_logmsg);
10332 static struct got_histedit_list_entry *
10333 get_folded_commits(struct got_histedit_list_entry *hle)
10335 struct got_histedit_list_entry *prev, *folded = NULL;
10337 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10338 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10339 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10340 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10342 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10348 static const struct got_error *
10349 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10350 struct got_repository *repo)
10352 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10353 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10354 const struct got_error *err = NULL;
10355 struct got_commit_object *commit = NULL;
10358 struct got_histedit_list_entry *folded = NULL;
10360 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10364 folded = get_folded_commits(hle);
10366 while (folded != hle) {
10367 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10368 folded = TAILQ_NEXT(folded, entry);
10371 err = append_folded_commit_msg(&new_msg, folded,
10377 folded = TAILQ_NEXT(folded, entry);
10381 err = got_object_id_str(&id_str, hle->commit_id);
10384 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10387 logmsg_len = asprintf(&new_msg,
10388 "%s\n# original log message of commit %s: %s",
10389 logmsg ? logmsg : "", id_str, orig_logmsg);
10390 if (logmsg_len == -1) {
10391 err = got_error_from_errno("asprintf");
10397 err = got_object_id_str(&id_str, hle->commit_id);
10401 err = got_opentemp_named_fd(&logmsg_path, &fd,
10402 GOT_TMPDIR_STR "/got-logmsg");
10406 write(fd, logmsg, logmsg_len);
10409 err = get_editor(&editor);
10413 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10416 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10419 hle->logmsg = strdup(new_msg);
10420 if (hle->logmsg == NULL)
10421 err = got_error_from_errno("strdup");
10424 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10425 err = got_error_from_errno2("unlink", logmsg_path);
10431 got_object_commit_close(commit);
10435 static const struct got_error *
10436 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10437 FILE *f, struct got_repository *repo)
10439 const struct got_error *err = NULL;
10440 char *line = NULL, *p, *end;
10444 const struct got_histedit_cmd *cmd;
10445 struct got_object_id *commit_id = NULL;
10446 struct got_histedit_list_entry *hle = NULL;
10449 len = getline(&line, &size, f);
10451 const struct got_error *getline_err;
10454 getline_err = got_error_from_errno("getline");
10455 err = got_ferror(f, getline_err->code);
10460 while (isspace((unsigned char)p[0]))
10462 if (p[0] == '#' || p[0] == '\0') {
10468 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10469 cmd = &got_histedit_cmds[i];
10470 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10471 isspace((unsigned char)p[strlen(cmd->name)])) {
10472 p += strlen(cmd->name);
10475 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10480 if (i == nitems(got_histedit_cmds)) {
10481 err = histedit_syntax_error(lineno);
10484 while (isspace((unsigned char)p[0]))
10486 if (cmd->code == GOT_HISTEDIT_MESG) {
10487 if (hle == NULL || hle->logmsg != NULL) {
10488 err = got_error(GOT_ERR_HISTEDIT_CMD);
10491 if (p[0] == '\0') {
10492 err = histedit_edit_logmsg(hle, repo);
10496 hle->logmsg = strdup(p);
10497 if (hle->logmsg == NULL) {
10498 err = got_error_from_errno("strdup");
10507 while (end[0] && !isspace((unsigned char)end[0]))
10511 err = got_object_resolve_id_str(&commit_id, repo, p);
10513 /* override error code */
10514 err = histedit_syntax_error(lineno);
10518 hle = malloc(sizeof(*hle));
10520 err = got_error_from_errno("malloc");
10524 hle->commit_id = commit_id;
10525 hle->logmsg = NULL;
10529 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10537 static const struct got_error *
10538 histedit_check_script(struct got_histedit_list *histedit_cmds,
10539 struct got_object_id_queue *commits, struct got_repository *repo)
10541 const struct got_error *err = NULL;
10542 struct got_object_qid *qid;
10543 struct got_histedit_list_entry *hle;
10544 static char msg[92];
10547 if (TAILQ_EMPTY(histedit_cmds))
10548 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10549 "histedit script contains no commands");
10550 if (STAILQ_EMPTY(commits))
10551 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10553 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10554 struct got_histedit_list_entry *hle2;
10555 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10558 if (got_object_id_cmp(hle->commit_id,
10559 hle2->commit_id) != 0)
10561 err = got_object_id_str(&id_str, hle->commit_id);
10564 snprintf(msg, sizeof(msg), "commit %s is listed "
10565 "more than once in histedit script", id_str);
10567 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10571 STAILQ_FOREACH(qid, commits, entry) {
10572 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10573 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10577 err = got_object_id_str(&id_str, &qid->id);
10580 snprintf(msg, sizeof(msg),
10581 "commit %s missing from histedit script", id_str);
10583 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10587 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10588 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10589 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10590 "last commit in histedit script cannot be folded");
10595 static const struct got_error *
10596 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10597 const char *path, struct got_object_id_queue *commits,
10598 struct got_repository *repo)
10600 const struct got_error *err = NULL;
10604 err = get_editor(&editor);
10608 if (spawn_editor(editor, path) == -1) {
10609 err = got_error_from_errno("failed spawning editor");
10613 f = fopen(path, "re");
10615 err = got_error_from_errno("fopen");
10618 err = histedit_parse_list(histedit_cmds, f, repo);
10622 err = histedit_check_script(histedit_cmds, commits, repo);
10624 if (f && fclose(f) == EOF && err == NULL)
10625 err = got_error_from_errno("fclose");
10630 static const struct got_error *
10631 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10632 struct got_object_id_queue *, const char *, const char *,
10633 struct got_repository *);
10635 static const struct got_error *
10636 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10637 struct got_object_id_queue *commits, const char *branch_name,
10638 int edit_logmsg_only, int fold_only, int edit_only,
10639 struct got_repository *repo)
10641 const struct got_error *err;
10645 err = got_opentemp_named(&path, &f, "got-histedit");
10649 err = write_cmd_list(f, branch_name, commits);
10653 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10654 fold_only, edit_only, repo);
10658 if (edit_logmsg_only || fold_only || edit_only) {
10660 err = histedit_parse_list(histedit_cmds, f, repo);
10662 if (fclose(f) == EOF) {
10663 err = got_error_from_errno("fclose");
10667 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10669 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10670 err->code != GOT_ERR_HISTEDIT_CMD)
10672 err = histedit_edit_list_retry(histedit_cmds, err,
10673 commits, path, branch_name, repo);
10677 if (f && fclose(f) == EOF && err == NULL)
10678 err = got_error_from_errno("fclose");
10679 if (path && unlink(path) != 0 && err == NULL)
10680 err = got_error_from_errno2("unlink", path);
10685 static const struct got_error *
10686 histedit_save_list(struct got_histedit_list *histedit_cmds,
10687 struct got_worktree *worktree, struct got_repository *repo)
10689 const struct got_error *err = NULL;
10692 struct got_histedit_list_entry *hle;
10693 struct got_commit_object *commit = NULL;
10695 err = got_worktree_get_histedit_script_path(&path, worktree);
10699 f = fopen(path, "we");
10701 err = got_error_from_errno2("fopen", path);
10704 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10705 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10711 int n = fprintf(f, "%c %s\n",
10712 GOT_HISTEDIT_MESG, hle->logmsg);
10714 err = got_ferror(f, GOT_ERR_IO);
10720 if (f && fclose(f) == EOF && err == NULL)
10721 err = got_error_from_errno("fclose");
10724 got_object_commit_close(commit);
10729 histedit_free_list(struct got_histedit_list *histedit_cmds)
10731 struct got_histedit_list_entry *hle;
10733 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10734 TAILQ_REMOVE(histedit_cmds, hle, entry);
10739 static const struct got_error *
10740 histedit_load_list(struct got_histedit_list *histedit_cmds,
10741 const char *path, struct got_repository *repo)
10743 const struct got_error *err = NULL;
10746 f = fopen(path, "re");
10748 err = got_error_from_errno2("fopen", path);
10752 err = histedit_parse_list(histedit_cmds, f, repo);
10754 if (f && fclose(f) == EOF && err == NULL)
10755 err = got_error_from_errno("fclose");
10759 static const struct got_error *
10760 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10761 const struct got_error *edit_err, struct got_object_id_queue *commits,
10762 const char *path, const char *branch_name, struct got_repository *repo)
10764 const struct got_error *err = NULL, *prev_err = edit_err;
10767 while (resp != 'c' && resp != 'r' && resp != 'a') {
10768 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10769 "or (a)bort: ", getprogname(), prev_err->msg);
10774 histedit_free_list(histedit_cmds);
10775 err = histedit_run_editor(histedit_cmds, path, commits,
10778 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10779 err->code != GOT_ERR_HISTEDIT_CMD)
10786 } else if (resp == 'r') {
10787 histedit_free_list(histedit_cmds);
10788 err = histedit_edit_script(histedit_cmds,
10789 commits, branch_name, 0, 0, 0, repo);
10791 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10792 err->code != GOT_ERR_HISTEDIT_CMD)
10799 } else if (resp == 'a') {
10800 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10803 printf("invalid response '%c'\n", resp);
10809 static const struct got_error *
10810 histedit_complete(struct got_worktree *worktree,
10811 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10812 struct got_reference *branch, struct got_repository *repo)
10814 printf("Switching work tree to %s\n",
10815 got_ref_get_symref_target(branch));
10816 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10820 static const struct got_error *
10821 show_histedit_progress(struct got_commit_object *commit,
10822 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10824 const struct got_error *err;
10825 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10827 err = got_object_id_str(&old_id_str, hle->commit_id);
10832 err = got_object_id_str(&new_id_str, new_id);
10837 old_id_str[12] = '\0';
10839 new_id_str[12] = '\0';
10842 logmsg = strdup(hle->logmsg);
10843 if (logmsg == NULL) {
10844 err = got_error_from_errno("strdup");
10847 trim_logmsg(logmsg, 42);
10849 err = get_short_logmsg(&logmsg, 42, commit);
10854 switch (hle->cmd->code) {
10855 case GOT_HISTEDIT_PICK:
10856 case GOT_HISTEDIT_EDIT:
10857 printf("%s -> %s: %s\n", old_id_str,
10858 new_id_str ? new_id_str : "no-op change", logmsg);
10860 case GOT_HISTEDIT_DROP:
10861 case GOT_HISTEDIT_FOLD:
10862 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10874 static const struct got_error *
10875 histedit_commit(struct got_pathlist_head *merged_paths,
10876 struct got_worktree *worktree, struct got_fileindex *fileindex,
10877 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10878 struct got_repository *repo)
10880 const struct got_error *err;
10881 struct got_commit_object *commit;
10882 struct got_object_id *new_commit_id;
10884 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10885 && hle->logmsg == NULL) {
10886 err = histedit_edit_logmsg(hle, repo);
10891 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10895 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10896 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10897 hle->logmsg, repo);
10899 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10901 err = show_histedit_progress(commit, hle, NULL);
10903 err = show_histedit_progress(commit, hle, new_commit_id);
10904 free(new_commit_id);
10907 got_object_commit_close(commit);
10911 static const struct got_error *
10912 histedit_skip_commit(struct got_histedit_list_entry *hle,
10913 struct got_worktree *worktree, struct got_repository *repo)
10915 const struct got_error *error;
10916 struct got_commit_object *commit;
10918 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10923 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10927 error = show_histedit_progress(commit, hle, NULL);
10928 got_object_commit_close(commit);
10932 static const struct got_error *
10933 check_local_changes(void *arg, unsigned char status,
10934 unsigned char staged_status, const char *path,
10935 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10936 struct got_object_id *commit_id, int dirfd, const char *de_name)
10938 int *have_local_changes = arg;
10941 case GOT_STATUS_ADD:
10942 case GOT_STATUS_DELETE:
10943 case GOT_STATUS_MODIFY:
10944 case GOT_STATUS_CONFLICT:
10945 *have_local_changes = 1;
10946 return got_error(GOT_ERR_CANCELLED);
10951 switch (staged_status) {
10952 case GOT_STATUS_ADD:
10953 case GOT_STATUS_DELETE:
10954 case GOT_STATUS_MODIFY:
10955 *have_local_changes = 1;
10956 return got_error(GOT_ERR_CANCELLED);
10964 static const struct got_error *
10965 cmd_histedit(int argc, char *argv[])
10967 const struct got_error *error = NULL;
10968 struct got_worktree *worktree = NULL;
10969 struct got_fileindex *fileindex = NULL;
10970 struct got_repository *repo = NULL;
10972 struct got_reference *branch = NULL;
10973 struct got_reference *tmp_branch = NULL;
10974 struct got_object_id *resume_commit_id = NULL;
10975 struct got_object_id *base_commit_id = NULL;
10976 struct got_object_id *head_commit_id = NULL;
10977 struct got_commit_object *commit = NULL;
10978 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10979 struct got_update_progress_arg upa;
10980 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10981 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10982 int list_backups = 0, delete_backups = 0;
10983 const char *edit_script_path = NULL;
10984 struct got_object_id_queue commits;
10985 struct got_pathlist_head merged_paths;
10986 const struct got_object_id_queue *parent_ids;
10987 struct got_object_qid *pid;
10988 struct got_histedit_list histedit_cmds;
10989 struct got_histedit_list_entry *hle;
10990 int *pack_fds = NULL;
10992 STAILQ_INIT(&commits);
10993 TAILQ_INIT(&histedit_cmds);
10994 TAILQ_INIT(&merged_paths);
10995 memset(&upa, 0, sizeof(upa));
10997 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11012 edit_script_path = optarg;
11015 edit_logmsg_only = 1;
11021 delete_backups = 1;
11033 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11034 "unveil", NULL) == -1)
11037 if (abort_edit && continue_edit)
11038 option_conflict('a', 'c');
11039 if (edit_script_path && edit_logmsg_only)
11040 option_conflict('F', 'm');
11041 if (abort_edit && edit_logmsg_only)
11042 option_conflict('a', 'm');
11043 if (continue_edit && edit_logmsg_only)
11044 option_conflict('c', 'm');
11045 if (abort_edit && fold_only)
11046 option_conflict('a', 'f');
11047 if (continue_edit && fold_only)
11048 option_conflict('c', 'f');
11049 if (fold_only && edit_logmsg_only)
11050 option_conflict('f', 'm');
11051 if (edit_script_path && fold_only)
11052 option_conflict('F', 'f');
11053 if (abort_edit && edit_only)
11054 option_conflict('a', 'e');
11055 if (continue_edit && edit_only)
11056 option_conflict('c', 'e');
11057 if (edit_only && edit_logmsg_only)
11058 option_conflict('e', 'm');
11059 if (edit_script_path && edit_only)
11060 option_conflict('F', 'e');
11061 if (list_backups) {
11063 option_conflict('l', 'a');
11065 option_conflict('l', 'c');
11066 if (edit_script_path)
11067 option_conflict('l', 'F');
11068 if (edit_logmsg_only)
11069 option_conflict('l', 'm');
11071 option_conflict('l', 'f');
11073 option_conflict('l', 'e');
11074 if (delete_backups)
11075 option_conflict('l', 'X');
11076 if (argc != 0 && argc != 1)
11078 } else if (delete_backups) {
11080 option_conflict('X', 'a');
11082 option_conflict('X', 'c');
11083 if (edit_script_path)
11084 option_conflict('X', 'F');
11085 if (edit_logmsg_only)
11086 option_conflict('X', 'm');
11088 option_conflict('X', 'f');
11090 option_conflict('X', 'e');
11092 option_conflict('X', 'l');
11093 if (argc != 0 && argc != 1)
11095 } else if (argc != 0)
11099 * This command cannot apply unveil(2) in all cases because the
11100 * user may choose to run an editor to edit the histedit script
11101 * and to edit individual commit log messages.
11102 * unveil(2) traverses exec(2); if an editor is used we have to
11103 * apply unveil after edit script and log messages have been written.
11104 * XXX TODO: Make use of unveil(2) where possible.
11107 cwd = getcwd(NULL, 0);
11109 error = got_error_from_errno("getcwd");
11113 error = got_repo_pack_fds_open(&pack_fds);
11117 error = got_worktree_open(&worktree, cwd);
11119 if (list_backups || delete_backups) {
11120 if (error->code != GOT_ERR_NOT_WORKTREE)
11123 if (error->code == GOT_ERR_NOT_WORKTREE)
11124 error = wrap_not_worktree_error(error,
11130 if (list_backups || delete_backups) {
11131 error = got_repo_open(&repo,
11132 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11136 error = apply_unveil(got_repo_get_path(repo), 0,
11137 worktree ? got_worktree_get_root_path(worktree) : NULL);
11140 error = process_backup_refs(
11141 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11142 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11143 goto done; /* nothing else to do */
11146 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11151 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11154 if (rebase_in_progress) {
11155 error = got_error(GOT_ERR_REBASING);
11159 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11163 if (merge_in_progress) {
11164 error = got_error(GOT_ERR_MERGE_BUSY);
11168 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11172 if (edit_in_progress && edit_logmsg_only) {
11173 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11174 "histedit operation is in progress in this "
11175 "work tree and must be continued or aborted "
11176 "before the -m option can be used");
11179 if (edit_in_progress && fold_only) {
11180 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11181 "histedit operation is in progress in this "
11182 "work tree and must be continued or aborted "
11183 "before the -f option can be used");
11186 if (edit_in_progress && edit_only) {
11187 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11188 "histedit operation is in progress in this "
11189 "work tree and must be continued or aborted "
11190 "before the -e option can be used");
11194 if (edit_in_progress && abort_edit) {
11195 error = got_worktree_histedit_continue(&resume_commit_id,
11196 &tmp_branch, &branch, &base_commit_id, &fileindex,
11200 printf("Switching work tree to %s\n",
11201 got_ref_get_symref_target(branch));
11202 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11203 branch, base_commit_id, abort_progress, &upa);
11206 printf("Histedit of %s aborted\n",
11207 got_ref_get_symref_target(branch));
11208 print_merge_progress_stats(&upa);
11209 goto done; /* nothing else to do */
11210 } else if (abort_edit) {
11211 error = got_error(GOT_ERR_NOT_HISTEDIT);
11215 if (continue_edit) {
11218 if (!edit_in_progress) {
11219 error = got_error(GOT_ERR_NOT_HISTEDIT);
11223 error = got_worktree_get_histedit_script_path(&path, worktree);
11227 error = histedit_load_list(&histedit_cmds, path, repo);
11232 error = got_worktree_histedit_continue(&resume_commit_id,
11233 &tmp_branch, &branch, &base_commit_id, &fileindex,
11238 error = got_ref_resolve(&head_commit_id, repo, branch);
11242 error = got_object_open_as_commit(&commit, repo,
11246 parent_ids = got_object_commit_get_parent_ids(commit);
11247 pid = STAILQ_FIRST(parent_ids);
11249 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11252 error = collect_commits(&commits, head_commit_id, &pid->id,
11253 base_commit_id, got_worktree_get_path_prefix(worktree),
11254 GOT_ERR_HISTEDIT_PATH, repo);
11255 got_object_commit_close(commit);
11260 if (edit_in_progress) {
11261 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11265 error = got_ref_open(&branch, repo,
11266 got_worktree_get_head_ref_name(worktree), 0);
11270 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11271 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11272 "will not edit commit history of a branch outside "
11273 "the \"refs/heads/\" reference namespace");
11277 error = got_ref_resolve(&head_commit_id, repo, branch);
11278 got_ref_close(branch);
11283 error = got_object_open_as_commit(&commit, repo,
11287 parent_ids = got_object_commit_get_parent_ids(commit);
11288 pid = STAILQ_FIRST(parent_ids);
11290 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11293 error = collect_commits(&commits, head_commit_id, &pid->id,
11294 got_worktree_get_base_commit_id(worktree),
11295 got_worktree_get_path_prefix(worktree),
11296 GOT_ERR_HISTEDIT_PATH, repo);
11297 got_object_commit_close(commit);
11302 if (STAILQ_EMPTY(&commits)) {
11303 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11307 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11308 &base_commit_id, &fileindex, worktree, repo);
11312 if (edit_script_path) {
11313 error = histedit_load_list(&histedit_cmds,
11314 edit_script_path, repo);
11316 got_worktree_histedit_abort(worktree, fileindex,
11317 repo, branch, base_commit_id,
11318 abort_progress, &upa);
11319 print_merge_progress_stats(&upa);
11323 const char *branch_name;
11324 branch_name = got_ref_get_symref_target(branch);
11325 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11327 error = histedit_edit_script(&histedit_cmds, &commits,
11328 branch_name, edit_logmsg_only, fold_only,
11331 got_worktree_histedit_abort(worktree, fileindex,
11332 repo, branch, base_commit_id,
11333 abort_progress, &upa);
11334 print_merge_progress_stats(&upa);
11340 error = histedit_save_list(&histedit_cmds, worktree,
11343 got_worktree_histedit_abort(worktree, fileindex,
11344 repo, branch, base_commit_id,
11345 abort_progress, &upa);
11346 print_merge_progress_stats(&upa);
11352 error = histedit_check_script(&histedit_cmds, &commits, repo);
11356 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11357 if (resume_commit_id) {
11358 if (got_object_id_cmp(hle->commit_id,
11359 resume_commit_id) != 0)
11362 resume_commit_id = NULL;
11363 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11364 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11365 error = histedit_skip_commit(hle, worktree,
11370 struct got_pathlist_head paths;
11371 int have_changes = 0;
11373 TAILQ_INIT(&paths);
11374 error = got_pathlist_append(&paths, "", NULL);
11377 error = got_worktree_status(worktree, &paths,
11378 repo, 0, check_local_changes, &have_changes,
11379 check_cancelled, NULL);
11380 got_pathlist_free(&paths);
11382 if (error->code != GOT_ERR_CANCELLED)
11384 if (sigint_received || sigpipe_received)
11387 if (have_changes) {
11388 error = histedit_commit(NULL, worktree,
11389 fileindex, tmp_branch, hle, repo);
11393 error = got_object_open_as_commit(
11394 &commit, repo, hle->commit_id);
11397 error = show_histedit_progress(commit,
11399 got_object_commit_close(commit);
11408 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11409 error = histedit_skip_commit(hle, worktree, repo);
11415 error = got_object_open_as_commit(&commit, repo,
11419 parent_ids = got_object_commit_get_parent_ids(commit);
11420 pid = STAILQ_FIRST(parent_ids);
11422 error = got_worktree_histedit_merge_files(&merged_paths,
11423 worktree, fileindex, &pid->id, hle->commit_id, repo,
11424 update_progress, &upa, check_cancelled, NULL);
11427 got_object_commit_close(commit);
11430 print_merge_progress_stats(&upa);
11431 if (upa.conflicts > 0 || upa.missing > 0 ||
11432 upa.not_deleted > 0 || upa.unversioned > 0) {
11433 if (upa.conflicts > 0) {
11434 error = show_rebase_merge_conflict(
11435 hle->commit_id, repo);
11439 got_worktree_rebase_pathlist_free(&merged_paths);
11443 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11445 error = got_object_id_str(&id_str, hle->commit_id);
11448 printf("Stopping histedit for amending commit %s\n",
11451 got_worktree_rebase_pathlist_free(&merged_paths);
11452 error = got_worktree_histedit_postpone(worktree,
11457 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11458 error = histedit_skip_commit(hle, worktree, repo);
11464 error = histedit_commit(&merged_paths, worktree, fileindex,
11465 tmp_branch, hle, repo);
11466 got_worktree_rebase_pathlist_free(&merged_paths);
11471 if (upa.conflicts > 0 || upa.missing > 0 ||
11472 upa.not_deleted > 0 || upa.unversioned > 0) {
11473 error = got_worktree_histedit_postpone(worktree, fileindex);
11476 if (upa.conflicts > 0 && upa.missing == 0 &&
11477 upa.not_deleted == 0 && upa.unversioned == 0) {
11478 error = got_error_msg(GOT_ERR_CONFLICTS,
11479 "conflicts must be resolved before histedit "
11481 } else if (upa.conflicts > 0) {
11482 error = got_error_msg(GOT_ERR_CONFLICTS,
11483 "conflicts must be resolved before histedit "
11484 "can continue; changes destined for some "
11485 "files were not yet merged and should be "
11486 "merged manually if required before the "
11487 "histedit operation is continued");
11489 error = got_error_msg(GOT_ERR_CONFLICTS,
11490 "changes destined for some files were not "
11491 "yet merged and should be merged manually "
11492 "if required before the histedit operation "
11496 error = histedit_complete(worktree, fileindex, tmp_branch,
11499 got_object_id_queue_free(&commits);
11500 histedit_free_list(&histedit_cmds);
11501 free(head_commit_id);
11502 free(base_commit_id);
11503 free(resume_commit_id);
11505 got_object_commit_close(commit);
11507 got_ref_close(branch);
11509 got_ref_close(tmp_branch);
11511 got_worktree_close(worktree);
11513 const struct got_error *close_err = got_repo_close(repo);
11518 const struct got_error *pack_err =
11519 got_repo_pack_fds_close(pack_fds);
11527 usage_integrate(void)
11529 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11533 static const struct got_error *
11534 cmd_integrate(int argc, char *argv[])
11536 const struct got_error *error = NULL;
11537 struct got_repository *repo = NULL;
11538 struct got_worktree *worktree = NULL;
11539 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11540 const char *branch_arg = NULL;
11541 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11542 struct got_fileindex *fileindex = NULL;
11543 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11545 struct got_update_progress_arg upa;
11546 int *pack_fds = NULL;
11548 while ((ch = getopt(argc, argv, "")) != -1) {
11561 branch_arg = argv[0];
11563 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11564 "unveil", NULL) == -1)
11567 cwd = getcwd(NULL, 0);
11569 error = got_error_from_errno("getcwd");
11573 error = got_repo_pack_fds_open(&pack_fds);
11577 error = got_worktree_open(&worktree, cwd);
11579 if (error->code == GOT_ERR_NOT_WORKTREE)
11580 error = wrap_not_worktree_error(error, "integrate",
11585 error = check_rebase_or_histedit_in_progress(worktree);
11589 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11594 error = apply_unveil(got_repo_get_path(repo), 0,
11595 got_worktree_get_root_path(worktree));
11599 error = check_merge_in_progress(worktree, repo);
11603 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11604 error = got_error_from_errno("asprintf");
11608 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11609 &base_branch_ref, worktree, refname, repo);
11613 refname = strdup(got_ref_get_name(branch_ref));
11614 if (refname == NULL) {
11615 error = got_error_from_errno("strdup");
11616 got_worktree_integrate_abort(worktree, fileindex, repo,
11617 branch_ref, base_branch_ref);
11620 base_refname = strdup(got_ref_get_name(base_branch_ref));
11621 if (base_refname == NULL) {
11622 error = got_error_from_errno("strdup");
11623 got_worktree_integrate_abort(worktree, fileindex, repo,
11624 branch_ref, base_branch_ref);
11628 error = got_ref_resolve(&commit_id, repo, branch_ref);
11632 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11636 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11637 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11638 "specified branch has already been integrated");
11639 got_worktree_integrate_abort(worktree, fileindex, repo,
11640 branch_ref, base_branch_ref);
11644 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11646 if (error->code == GOT_ERR_ANCESTRY)
11647 error = got_error(GOT_ERR_REBASE_REQUIRED);
11648 got_worktree_integrate_abort(worktree, fileindex, repo,
11649 branch_ref, base_branch_ref);
11653 memset(&upa, 0, sizeof(upa));
11654 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11655 branch_ref, base_branch_ref, update_progress, &upa,
11656 check_cancelled, NULL);
11660 printf("Integrated %s into %s\n", refname, base_refname);
11661 print_update_progress_stats(&upa);
11664 const struct got_error *close_err = got_repo_close(repo);
11669 got_worktree_close(worktree);
11671 const struct got_error *pack_err =
11672 got_repo_pack_fds_close(pack_fds);
11677 free(base_commit_id);
11680 free(base_refname);
11687 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11692 static const struct got_error *
11693 cmd_merge(int argc, char *argv[])
11695 const struct got_error *error = NULL;
11696 struct got_worktree *worktree = NULL;
11697 struct got_repository *repo = NULL;
11698 struct got_fileindex *fileindex = NULL;
11699 char *cwd = NULL, *id_str = NULL, *author = NULL;
11700 struct got_reference *branch = NULL, *wt_branch = NULL;
11701 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11702 struct got_object_id *wt_branch_tip = NULL;
11703 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11704 int interrupt_merge = 0;
11705 struct got_update_progress_arg upa;
11706 struct got_object_id *merge_commit_id = NULL;
11707 char *branch_name = NULL;
11708 int *pack_fds = NULL;
11710 memset(&upa, 0, sizeof(upa));
11712 while ((ch = getopt(argc, argv, "acn")) != -1) {
11718 continue_merge = 1;
11721 interrupt_merge = 1;
11733 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11734 "unveil", NULL) == -1)
11738 if (abort_merge && continue_merge)
11739 option_conflict('a', 'c');
11740 if (abort_merge || continue_merge) {
11743 } else if (argc != 1)
11746 cwd = getcwd(NULL, 0);
11748 error = got_error_from_errno("getcwd");
11752 error = got_repo_pack_fds_open(&pack_fds);
11756 error = got_worktree_open(&worktree, cwd);
11758 if (error->code == GOT_ERR_NOT_WORKTREE)
11759 error = wrap_not_worktree_error(error,
11764 error = got_repo_open(&repo,
11765 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11770 error = apply_unveil(got_repo_get_path(repo), 0,
11771 worktree ? got_worktree_get_root_path(worktree) : NULL);
11775 error = check_rebase_or_histedit_in_progress(worktree);
11779 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11785 if (!merge_in_progress) {
11786 error = got_error(GOT_ERR_NOT_MERGING);
11789 error = got_worktree_merge_continue(&branch_name,
11790 &branch_tip, &fileindex, worktree, repo);
11793 error = got_worktree_merge_abort(worktree, fileindex, repo,
11794 abort_progress, &upa);
11797 printf("Merge of %s aborted\n", branch_name);
11798 goto done; /* nothing else to do */
11801 error = get_author(&author, repo, worktree);
11805 if (continue_merge) {
11806 if (!merge_in_progress) {
11807 error = got_error(GOT_ERR_NOT_MERGING);
11810 error = got_worktree_merge_continue(&branch_name,
11811 &branch_tip, &fileindex, worktree, repo);
11815 error = got_ref_open(&branch, repo, argv[0], 0);
11818 branch_name = strdup(got_ref_get_name(branch));
11819 if (branch_name == NULL) {
11820 error = got_error_from_errno("strdup");
11823 error = got_ref_resolve(&branch_tip, repo, branch);
11828 error = got_ref_open(&wt_branch, repo,
11829 got_worktree_get_head_ref_name(worktree), 0);
11832 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11835 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11836 wt_branch_tip, branch_tip, 0, repo,
11837 check_cancelled, NULL);
11838 if (error && error->code != GOT_ERR_ANCESTRY)
11841 if (!continue_merge) {
11842 error = check_path_prefix(wt_branch_tip, branch_tip,
11843 got_worktree_get_path_prefix(worktree),
11844 GOT_ERR_MERGE_PATH, repo);
11848 error = check_same_branch(wt_branch_tip, branch,
11851 if (error->code != GOT_ERR_ANCESTRY)
11855 static char msg[512];
11856 snprintf(msg, sizeof(msg),
11857 "cannot create a merge commit because "
11858 "%s is based on %s; %s can be integrated "
11859 "with 'got integrate' instead", branch_name,
11860 got_worktree_get_head_ref_name(worktree),
11862 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11866 error = got_worktree_merge_prepare(&fileindex, worktree,
11871 error = got_worktree_merge_branch(worktree, fileindex,
11872 yca_id, branch_tip, repo, update_progress, &upa,
11873 check_cancelled, NULL);
11876 print_merge_progress_stats(&upa);
11877 if (!upa.did_something) {
11878 error = got_worktree_merge_abort(worktree, fileindex,
11879 repo, abort_progress, &upa);
11882 printf("Already up-to-date\n");
11887 if (interrupt_merge) {
11888 error = got_worktree_merge_postpone(worktree, fileindex);
11891 printf("Merge of %s interrupted on request\n", branch_name);
11892 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11893 upa.not_deleted > 0 || upa.unversioned > 0) {
11894 error = got_worktree_merge_postpone(worktree, fileindex);
11897 if (upa.conflicts > 0 && upa.missing == 0 &&
11898 upa.not_deleted == 0 && upa.unversioned == 0) {
11899 error = got_error_msg(GOT_ERR_CONFLICTS,
11900 "conflicts must be resolved before merging "
11902 } else if (upa.conflicts > 0) {
11903 error = got_error_msg(GOT_ERR_CONFLICTS,
11904 "conflicts must be resolved before merging "
11905 "can continue; changes destined for some "
11906 "files were not yet merged and "
11907 "should be merged manually if required before the "
11908 "merge operation is continued");
11910 error = got_error_msg(GOT_ERR_CONFLICTS,
11911 "changes destined for some "
11912 "files were not yet merged and should be "
11913 "merged manually if required before the "
11914 "merge operation is continued");
11918 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11919 fileindex, author, NULL, 1, branch_tip, branch_name,
11920 repo, continue_merge ? print_status : NULL, NULL);
11923 error = got_worktree_merge_complete(worktree, fileindex, repo);
11926 error = got_object_id_str(&id_str, merge_commit_id);
11929 printf("Merged %s into %s: %s\n", branch_name,
11930 got_worktree_get_head_ref_name(worktree),
11936 free(merge_commit_id);
11942 got_ref_close(branch);
11944 got_ref_close(wt_branch);
11946 got_worktree_close(worktree);
11948 const struct got_error *close_err = got_repo_close(repo);
11953 const struct got_error *pack_err =
11954 got_repo_pack_fds_close(pack_fds);
11964 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11965 "[-S] [file-path ...]\n",
11970 static const struct got_error *
11971 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11972 const char *path, struct got_object_id *blob_id,
11973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11974 int dirfd, const char *de_name)
11976 const struct got_error *err = NULL;
11977 char *id_str = NULL;
11979 if (staged_status != GOT_STATUS_ADD &&
11980 staged_status != GOT_STATUS_MODIFY &&
11981 staged_status != GOT_STATUS_DELETE)
11984 if (staged_status == GOT_STATUS_ADD ||
11985 staged_status == GOT_STATUS_MODIFY)
11986 err = got_object_id_str(&id_str, staged_blob_id);
11988 err = got_object_id_str(&id_str, blob_id);
11992 printf("%s %c %s\n", id_str, staged_status, path);
11997 static const struct got_error *
11998 cmd_stage(int argc, char *argv[])
12000 const struct got_error *error = NULL;
12001 struct got_repository *repo = NULL;
12002 struct got_worktree *worktree = NULL;
12004 struct got_pathlist_head paths;
12005 struct got_pathlist_entry *pe;
12006 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12007 FILE *patch_script_file = NULL;
12008 const char *patch_script_path = NULL;
12009 struct choose_patch_arg cpa;
12010 int *pack_fds = NULL;
12012 TAILQ_INIT(&paths);
12014 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12023 patch_script_path = optarg;
12026 allow_bad_symlinks = 1;
12038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12039 "unveil", NULL) == -1)
12042 if (list_stage && (pflag || patch_script_path))
12043 errx(1, "-l option cannot be used with other options");
12044 if (patch_script_path && !pflag)
12045 errx(1, "-F option can only be used together with -p option");
12047 cwd = getcwd(NULL, 0);
12049 error = got_error_from_errno("getcwd");
12053 error = got_repo_pack_fds_open(&pack_fds);
12057 error = got_worktree_open(&worktree, cwd);
12059 if (error->code == GOT_ERR_NOT_WORKTREE)
12060 error = wrap_not_worktree_error(error, "stage", cwd);
12064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12069 if (patch_script_path) {
12070 patch_script_file = fopen(patch_script_path, "re");
12071 if (patch_script_file == NULL) {
12072 error = got_error_from_errno2("fopen",
12073 patch_script_path);
12077 error = apply_unveil(got_repo_get_path(repo), 0,
12078 got_worktree_get_root_path(worktree));
12082 error = check_merge_in_progress(worktree, repo);
12086 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12091 error = got_worktree_status(worktree, &paths, repo, 0,
12092 print_stage, NULL, check_cancelled, NULL);
12094 cpa.patch_script_file = patch_script_file;
12095 cpa.action = "stage";
12096 error = got_worktree_stage(worktree, &paths,
12097 pflag ? NULL : print_status, NULL,
12098 pflag ? choose_patch : NULL, &cpa,
12099 allow_bad_symlinks, repo);
12102 if (patch_script_file && fclose(patch_script_file) == EOF &&
12104 error = got_error_from_errno2("fclose", patch_script_path);
12106 const struct got_error *close_err = got_repo_close(repo);
12111 got_worktree_close(worktree);
12113 const struct got_error *pack_err =
12114 got_repo_pack_fds_close(pack_fds);
12118 TAILQ_FOREACH(pe, &paths, entry)
12119 free((char *)pe->path);
12120 got_pathlist_free(&paths);
12126 usage_unstage(void)
12128 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12129 "[file-path ...]\n",
12135 static const struct got_error *
12136 cmd_unstage(int argc, char *argv[])
12138 const struct got_error *error = NULL;
12139 struct got_repository *repo = NULL;
12140 struct got_worktree *worktree = NULL;
12142 struct got_pathlist_head paths;
12143 struct got_pathlist_entry *pe;
12145 struct got_update_progress_arg upa;
12146 FILE *patch_script_file = NULL;
12147 const char *patch_script_path = NULL;
12148 struct choose_patch_arg cpa;
12149 int *pack_fds = NULL;
12151 TAILQ_INIT(&paths);
12153 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12159 patch_script_path = optarg;
12171 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12172 "unveil", NULL) == -1)
12175 if (patch_script_path && !pflag)
12176 errx(1, "-F option can only be used together with -p option");
12178 cwd = getcwd(NULL, 0);
12180 error = got_error_from_errno("getcwd");
12184 error = got_repo_pack_fds_open(&pack_fds);
12188 error = got_worktree_open(&worktree, cwd);
12190 if (error->code == GOT_ERR_NOT_WORKTREE)
12191 error = wrap_not_worktree_error(error, "unstage", cwd);
12195 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12200 if (patch_script_path) {
12201 patch_script_file = fopen(patch_script_path, "re");
12202 if (patch_script_file == NULL) {
12203 error = got_error_from_errno2("fopen",
12204 patch_script_path);
12209 error = apply_unveil(got_repo_get_path(repo), 0,
12210 got_worktree_get_root_path(worktree));
12214 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12218 cpa.patch_script_file = patch_script_file;
12219 cpa.action = "unstage";
12220 memset(&upa, 0, sizeof(upa));
12221 error = got_worktree_unstage(worktree, &paths, update_progress,
12222 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12224 print_merge_progress_stats(&upa);
12226 if (patch_script_file && fclose(patch_script_file) == EOF &&
12228 error = got_error_from_errno2("fclose", patch_script_path);
12230 const struct got_error *close_err = got_repo_close(repo);
12235 got_worktree_close(worktree);
12237 const struct got_error *pack_err =
12238 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;
12264 fd = got_opentempfd();
12266 return got_error_from_errno("got_opentempfd");
12268 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12272 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12274 if (fd != -1 && close(fd) == -1 && err == NULL)
12275 err = got_error_from_errno("close");
12277 got_object_blob_close(blob);
12281 static const struct got_error *
12282 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12284 const struct got_error *err;
12285 struct got_tree_object *tree;
12288 err = got_object_open_as_tree(&tree, repo, id);
12292 nentries = got_object_tree_get_nentries(tree);
12293 for (i = 0; i < nentries; i++) {
12294 struct got_tree_entry *te;
12296 if (sigint_received || sigpipe_received)
12298 te = got_object_tree_get_entry(tree, i);
12299 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12302 fprintf(outfile, "%s %.7o %s\n", id_str,
12303 got_tree_entry_get_mode(te),
12304 got_tree_entry_get_name(te));
12308 got_object_tree_close(tree);
12313 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12323 h = (long long)gmtoff / 3600;
12324 m = ((long long)gmtoff - h*3600) / 60;
12325 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12328 static const struct got_error *
12329 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12331 const struct got_error *err;
12332 struct got_commit_object *commit;
12333 const struct got_object_id_queue *parent_ids;
12334 struct got_object_qid *pid;
12335 char *id_str = NULL;
12336 const char *logmsg = NULL;
12339 err = got_object_open_as_commit(&commit, repo, id);
12343 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12347 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12348 parent_ids = got_object_commit_get_parent_ids(commit);
12349 fprintf(outfile, "numparents %d\n",
12350 got_object_commit_get_nparents(commit));
12351 STAILQ_FOREACH(pid, parent_ids, entry) {
12353 err = got_object_id_str(&pid_str, &pid->id);
12356 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12359 format_gmtoff(gmtoff, sizeof(gmtoff),
12360 got_object_commit_get_author_gmtoff(commit));
12361 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12362 got_object_commit_get_author(commit),
12363 (long long)got_object_commit_get_author_time(commit),
12366 format_gmtoff(gmtoff, sizeof(gmtoff),
12367 got_object_commit_get_committer_gmtoff(commit));
12368 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12369 got_object_commit_get_author(commit),
12370 (long long)got_object_commit_get_committer_time(commit),
12373 logmsg = got_object_commit_get_logmsg_raw(commit);
12374 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12375 fprintf(outfile, "%s", logmsg);
12378 got_object_commit_close(commit);
12382 static const struct got_error *
12383 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12385 const struct got_error *err;
12386 struct got_tag_object *tag;
12387 char *id_str = NULL;
12388 const char *tagmsg = NULL;
12391 err = got_object_open_as_tag(&tag, repo, id);
12395 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12399 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12401 switch (got_object_tag_get_object_type(tag)) {
12402 case GOT_OBJ_TYPE_BLOB:
12403 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12404 GOT_OBJ_LABEL_BLOB);
12406 case GOT_OBJ_TYPE_TREE:
12407 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12408 GOT_OBJ_LABEL_TREE);
12410 case GOT_OBJ_TYPE_COMMIT:
12411 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12412 GOT_OBJ_LABEL_COMMIT);
12414 case GOT_OBJ_TYPE_TAG:
12415 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12416 GOT_OBJ_LABEL_TAG);
12422 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12423 got_object_tag_get_name(tag));
12425 format_gmtoff(gmtoff, sizeof(gmtoff),
12426 got_object_tag_get_tagger_gmtoff(tag));
12427 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12428 got_object_tag_get_tagger(tag),
12429 (long long)got_object_tag_get_tagger_time(tag),
12432 tagmsg = got_object_tag_get_message(tag);
12433 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12434 fprintf(outfile, "%s", tagmsg);
12437 got_object_tag_close(tag);
12441 static const struct got_error *
12442 cmd_cat(int argc, char *argv[])
12444 const struct got_error *error;
12445 struct got_repository *repo = NULL;
12446 struct got_worktree *worktree = NULL;
12447 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12448 const char *commit_id_str = NULL;
12449 struct got_object_id *id = NULL, *commit_id = NULL;
12450 struct got_commit_object *commit = NULL;
12451 int ch, obj_type, i, force_path = 0;
12452 struct got_reflist_head refs;
12453 int *pack_fds = NULL;
12458 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12463 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12466 commit_id_str = optarg;
12469 repo_path = realpath(optarg, NULL);
12470 if (repo_path == NULL)
12471 return got_error_from_errno2("realpath",
12473 got_path_strip_trailing_slashes(repo_path);
12487 cwd = getcwd(NULL, 0);
12489 error = got_error_from_errno("getcwd");
12493 error = got_repo_pack_fds_open(&pack_fds);
12497 if (repo_path == NULL) {
12498 error = got_worktree_open(&worktree, cwd);
12499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12502 repo_path = strdup(
12503 got_worktree_get_repo_path(worktree));
12504 if (repo_path == NULL) {
12505 error = got_error_from_errno("strdup");
12509 /* Release work tree lock. */
12510 got_worktree_close(worktree);
12515 if (repo_path == NULL) {
12516 repo_path = strdup(cwd);
12517 if (repo_path == NULL)
12518 return got_error_from_errno("strdup");
12521 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12526 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12530 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12534 if (commit_id_str == NULL)
12535 commit_id_str = GOT_REF_HEAD;
12536 error = got_repo_match_object_id(&commit_id, NULL,
12537 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12541 error = got_object_open_as_commit(&commit, repo, commit_id);
12545 for (i = 0; i < argc; i++) {
12547 error = got_object_id_by_path(&id, repo, commit,
12552 error = got_repo_match_object_id(&id, &label, argv[i],
12553 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12556 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12557 error->code != GOT_ERR_NOT_REF)
12559 error = got_object_id_by_path(&id, repo,
12566 error = got_object_get_type(&obj_type, repo, id);
12570 switch (obj_type) {
12571 case GOT_OBJ_TYPE_BLOB:
12572 error = cat_blob(id, repo, stdout);
12574 case GOT_OBJ_TYPE_TREE:
12575 error = cat_tree(id, repo, stdout);
12577 case GOT_OBJ_TYPE_COMMIT:
12578 error = cat_commit(id, repo, stdout);
12580 case GOT_OBJ_TYPE_TAG:
12581 error = cat_tag(id, repo, stdout);
12584 error = got_error(GOT_ERR_OBJ_TYPE);
12599 got_object_commit_close(commit);
12601 got_worktree_close(worktree);
12603 const struct got_error *close_err = got_repo_close(repo);
12608 const struct got_error *pack_err =
12609 got_repo_pack_fds_close(pack_fds);
12614 got_ref_list_free(&refs);
12621 fprintf(stderr, "usage: %s info [path ...]\n",
12626 static const struct got_error *
12627 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12628 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12629 struct got_object_id *commit_id)
12631 const struct got_error *err = NULL;
12632 char *id_str = NULL;
12634 struct tm mytm, *tm;
12635 struct got_pathlist_head *paths = arg;
12636 struct got_pathlist_entry *pe;
12639 * Clear error indication from any of the path arguments which
12640 * would cause this file index entry to be displayed.
12642 TAILQ_FOREACH(pe, paths, entry) {
12643 if (got_path_cmp(path, pe->path, strlen(path),
12644 pe->path_len) == 0 ||
12645 got_path_is_child(path, pe->path, pe->path_len))
12646 pe->data = NULL; /* no error */
12649 printf(GOT_COMMIT_SEP_STR);
12651 printf("symlink: %s\n", path);
12652 else if (S_ISREG(mode)) {
12653 printf("file: %s\n", path);
12654 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12655 } else if (S_ISDIR(mode))
12656 printf("directory: %s\n", path);
12658 printf("something: %s\n", path);
12660 tm = localtime_r(&mtime, &mytm);
12663 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12664 return got_error(GOT_ERR_NO_SPACE);
12665 printf("timestamp: %s\n", datebuf);
12668 err = got_object_id_str(&id_str, blob_id);
12671 printf("based on blob: %s\n", id_str);
12675 if (staged_blob_id) {
12676 err = got_object_id_str(&id_str, staged_blob_id);
12679 printf("based on staged blob: %s\n", id_str);
12684 err = got_object_id_str(&id_str, commit_id);
12687 printf("based on commit: %s\n", id_str);
12694 static const struct got_error *
12695 cmd_info(int argc, char *argv[])
12697 const struct got_error *error = NULL;
12698 struct got_worktree *worktree = NULL;
12699 char *cwd = NULL, *id_str = NULL;
12700 struct got_pathlist_head paths;
12701 struct got_pathlist_entry *pe;
12702 char *uuidstr = NULL;
12703 int ch, show_files = 0;
12704 int *pack_fds = NULL;
12706 TAILQ_INIT(&paths);
12708 while ((ch = getopt(argc, argv, "")) != -1) {
12720 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12724 cwd = getcwd(NULL, 0);
12726 error = got_error_from_errno("getcwd");
12730 error = got_repo_pack_fds_open(&pack_fds);
12734 error = got_worktree_open(&worktree, cwd);
12736 if (error->code == GOT_ERR_NOT_WORKTREE)
12737 error = wrap_not_worktree_error(error, "info", cwd);
12742 /* Remove "cpath" promise. */
12743 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12747 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12752 error = get_worktree_paths_from_argv(&paths, argc, argv,
12759 error = got_object_id_str(&id_str,
12760 got_worktree_get_base_commit_id(worktree));
12764 error = got_worktree_get_uuid(&uuidstr, worktree);
12768 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12769 printf("work tree base commit: %s\n", id_str);
12770 printf("work tree path prefix: %s\n",
12771 got_worktree_get_path_prefix(worktree));
12772 printf("work tree branch reference: %s\n",
12773 got_worktree_get_head_ref_name(worktree));
12774 printf("work tree UUID: %s\n", uuidstr);
12775 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12778 struct got_pathlist_entry *pe;
12779 TAILQ_FOREACH(pe, &paths, entry) {
12780 if (pe->path_len == 0)
12783 * Assume this path will fail. This will be corrected
12784 * in print_path_info() in case the path does suceeed.
12786 pe->data = (void *)got_error_path(pe->path,
12789 error = got_worktree_path_info(worktree, &paths,
12790 print_path_info, &paths, check_cancelled, NULL);
12793 TAILQ_FOREACH(pe, &paths, entry) {
12794 if (pe->data != NULL) {
12795 error = pe->data; /* bad path */
12802 const struct got_error *pack_err =
12803 got_repo_pack_fds_close(pack_fds);
12807 TAILQ_FOREACH(pe, &paths, entry)
12808 free((char *)pe->path);
12809 got_pathlist_free(&paths);