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;
3612 int fd1 = -1, fd2 = -1;
3615 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3618 f1 = got_opentemp();
3620 err = got_error_from_errno("got_opentemp");
3624 fd1 = got_opentempfd();
3626 err = got_error_from_errno("got_opentempfd");
3631 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3635 f2 = got_opentemp();
3637 err = got_error_from_errno("got_opentemp");
3640 fd2 = got_opentempfd();
3642 err = got_error_from_errno("got_opentempfd");
3645 arg.diff_context = diff_context;
3646 arg.ignore_whitespace = ignore_whitespace;
3647 arg.force_text_diff = force_text_diff;
3648 arg.outfile = outfile;
3649 arg.line_offsets = NULL;
3651 while (path[0] == '/')
3653 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3654 got_diff_blob_output_unidiff, &arg, 1);
3657 got_object_tree_close(tree1);
3659 got_object_tree_close(tree2);
3660 if (f1 && fclose(f1) == EOF && err == NULL)
3661 err = got_error_from_errno("fclose");
3662 if (f2 && fclose(f2) == EOF && err == NULL)
3663 err = got_error_from_errno("fclose");
3664 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3665 err = got_error_from_errno("close");
3666 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3667 err = got_error_from_errno("close");
3671 static const struct got_error *
3672 get_changed_paths(struct got_pathlist_head *paths,
3673 struct got_commit_object *commit, struct got_repository *repo)
3675 const struct got_error *err = NULL;
3676 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3677 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3678 struct got_object_qid *qid;
3680 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3682 struct got_commit_object *pcommit;
3683 err = got_object_open_as_commit(&pcommit, repo,
3688 tree_id1 = got_object_id_dup(
3689 got_object_commit_get_tree_id(pcommit));
3690 if (tree_id1 == NULL) {
3691 got_object_commit_close(pcommit);
3692 return got_error_from_errno("got_object_id_dup");
3694 got_object_commit_close(pcommit);
3699 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3704 tree_id2 = got_object_commit_get_tree_id(commit);
3705 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3709 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3710 got_diff_tree_collect_changed_paths, paths, 0);
3713 got_object_tree_close(tree1);
3715 got_object_tree_close(tree2);
3720 static const struct got_error *
3721 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3722 const char *path, int diff_context, struct got_repository *repo,
3725 const struct got_error *err = NULL;
3726 struct got_commit_object *pcommit = NULL;
3727 char *id_str1 = NULL, *id_str2 = NULL;
3728 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3729 struct got_object_qid *qid;
3731 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3733 err = got_object_open_as_commit(&pcommit, repo,
3737 err = got_object_id_str(&id_str1, &qid->id);
3742 err = got_object_id_str(&id_str2, id);
3746 if (path && path[0] != '\0') {
3748 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3752 err = got_object_id_by_path(&obj_id1, repo,
3755 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3761 err = got_object_get_type(&obj_type, repo, obj_id2);
3767 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3768 fprintf(outfile, "commit - %s\n",
3769 id_str1 ? id_str1 : "/dev/null");
3770 fprintf(outfile, "commit + %s\n", id_str2);
3772 case GOT_OBJ_TYPE_BLOB:
3773 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3774 0, 0, repo, outfile);
3776 case GOT_OBJ_TYPE_TREE:
3777 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3778 0, 0, repo, outfile);
3781 err = got_error(GOT_ERR_OBJ_TYPE);
3787 obj_id2 = got_object_commit_get_tree_id(commit);
3789 obj_id1 = got_object_commit_get_tree_id(pcommit);
3791 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3792 fprintf(outfile, "commit - %s\n",
3793 id_str1 ? id_str1 : "/dev/null");
3794 fprintf(outfile, "commit + %s\n", id_str2);
3795 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3802 got_object_commit_close(pcommit);
3807 get_datestr(time_t *time, char *datebuf)
3809 struct tm mytm, *tm;
3812 tm = gmtime_r(time, &mytm);
3815 s = asctime_r(tm, datebuf);
3818 p = strchr(s, '\n');
3824 static const struct got_error *
3825 match_commit(int *have_match, struct got_object_id *id,
3826 struct got_commit_object *commit, regex_t *regex)
3828 const struct got_error *err = NULL;
3829 regmatch_t regmatch;
3830 char *id_str = NULL, *logmsg = NULL;
3834 err = got_object_id_str(&id_str, id);
3838 err = got_object_commit_get_logmsg(&logmsg, commit);
3842 if (regexec(regex, got_object_commit_get_author(commit), 1,
3843 ®match, 0) == 0 ||
3844 regexec(regex, got_object_commit_get_committer(commit), 1,
3845 ®match, 0) == 0 ||
3846 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3847 regexec(regex, logmsg, 1, ®match, 0) == 0)
3856 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3859 regmatch_t regmatch;
3860 struct got_pathlist_entry *pe;
3864 TAILQ_FOREACH(pe, changed_paths, entry) {
3865 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3872 static const struct got_error *
3873 match_patch(int *have_match, struct got_commit_object *commit,
3874 struct got_object_id *id, const char *path, int diff_context,
3875 struct got_repository *repo, regex_t *regex, FILE *f)
3877 const struct got_error *err = NULL;
3879 size_t linesize = 0;
3881 regmatch_t regmatch;
3885 err = got_opentemp_truncate(f);
3889 err = print_patch(commit, id, path, diff_context, repo, f);
3893 if (fseeko(f, 0L, SEEK_SET) == -1) {
3894 err = got_error_from_errno("fseeko");
3898 while ((linelen = getline(&line, &linesize, f)) != -1) {
3899 if (regexec(regex, line, 1, ®match, 0) == 0) {
3909 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3911 static const struct got_error*
3912 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3913 struct got_object_id *id, struct got_repository *repo,
3916 static const struct got_error *err = NULL;
3917 struct got_reflist_entry *re;
3923 TAILQ_FOREACH(re, refs, entry) {
3924 struct got_tag_object *tag = NULL;
3925 struct got_object_id *ref_id;
3928 name = got_ref_get_name(re->ref);
3929 if (strcmp(name, GOT_REF_HEAD) == 0)
3931 if (strncmp(name, "refs/", 5) == 0)
3933 if (strncmp(name, "got/", 4) == 0)
3935 if (strncmp(name, "heads/", 6) == 0)
3937 if (strncmp(name, "remotes/", 8) == 0) {
3941 s = strstr(name, "/" GOT_REF_HEAD);
3942 if (s != NULL && s[strlen(s)] == '\0')
3945 err = got_ref_resolve(&ref_id, repo, re->ref);
3948 if (strncmp(name, "tags/", 5) == 0) {
3949 err = got_object_open_as_tag(&tag, repo, ref_id);
3951 if (err->code != GOT_ERR_OBJ_TYPE) {
3955 /* Ref points at something other than a tag. */
3960 cmp = got_object_id_cmp(tag ?
3961 got_object_tag_get_object_id(tag) : ref_id, id);
3964 got_object_tag_close(tag);
3968 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3969 s ? ", " : "", name) == -1) {
3970 err = got_error_from_errno("asprintf");
3981 static const struct got_error *
3982 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3983 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3985 const struct got_error *err = NULL;
3986 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3987 char *comma, *s, *nl;
3988 struct got_reflist_head *refs;
3989 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3991 time_t committer_time;
3993 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3995 err = build_refs_str(&ref_str, refs, id, repo, 1);
3999 /* Display the first matching ref only. */
4000 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4004 if (ref_str == NULL) {
4005 err = got_object_id_str(&id_str, id);
4010 committer_time = got_object_commit_get_committer_time(commit);
4011 if (gmtime_r(&committer_time, &tm) == NULL) {
4012 err = got_error_from_errno("gmtime_r");
4015 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4016 err = got_error(GOT_ERR_NO_SPACE);
4020 err = got_object_commit_get_logmsg(&logmsg0, commit);
4025 while (isspace((unsigned char)s[0]))
4028 nl = strchr(s, '\n');
4034 printf("%s%-7s %s\n", datebuf, ref_str, s);
4036 printf("%s%.7s %s\n", datebuf, id_str, s);
4038 if (fflush(stdout) != 0 && err == NULL)
4039 err = got_error_from_errno("fflush");
4047 static const struct got_error *
4048 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4049 struct got_repository *repo, const char *path,
4050 struct got_pathlist_head *changed_paths, int show_patch,
4051 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4052 const char *custom_refs_str)
4054 const struct got_error *err = NULL;
4055 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4057 time_t committer_time;
4058 const char *author, *committer;
4059 char *refs_str = NULL;
4061 err = got_object_id_str(&id_str, id);
4065 if (custom_refs_str == NULL) {
4066 struct got_reflist_head *refs;
4067 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4069 err = build_refs_str(&refs_str, refs, id, repo, 0);
4075 printf(GOT_COMMIT_SEP_STR);
4076 if (custom_refs_str)
4077 printf("commit %s (%s)\n", id_str, custom_refs_str);
4079 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4080 refs_str ? refs_str : "", refs_str ? ")" : "");
4085 printf("from: %s\n", got_object_commit_get_author(commit));
4086 committer_time = got_object_commit_get_committer_time(commit);
4087 datestr = get_datestr(&committer_time, datebuf);
4089 printf("date: %s UTC\n", datestr);
4090 author = got_object_commit_get_author(commit);
4091 committer = got_object_commit_get_committer(commit);
4092 if (strcmp(author, committer) != 0)
4093 printf("via: %s\n", committer);
4094 if (got_object_commit_get_nparents(commit) > 1) {
4095 const struct got_object_id_queue *parent_ids;
4096 struct got_object_qid *qid;
4098 parent_ids = got_object_commit_get_parent_ids(commit);
4099 STAILQ_FOREACH(qid, parent_ids, entry) {
4100 err = got_object_id_str(&id_str, &qid->id);
4103 printf("parent %d: %s\n", n++, id_str);
4109 err = got_object_commit_get_logmsg(&logmsg0, commit);
4115 line = strsep(&logmsg, "\n");
4117 printf(" %s\n", line);
4121 if (changed_paths) {
4122 struct got_pathlist_entry *pe;
4123 TAILQ_FOREACH(pe, changed_paths, entry) {
4124 struct got_diff_changed_path *cp = pe->data;
4125 printf(" %c %s\n", cp->status, pe->path);
4130 err = print_patch(commit, id, path, diff_context, repo, stdout);
4135 if (fflush(stdout) != 0 && err == NULL)
4136 err = got_error_from_errno("fflush");
4143 static const struct got_error *
4144 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4145 struct got_repository *repo, const char *path, int show_changed_paths,
4146 int show_patch, const char *search_pattern, int diff_context, int limit,
4147 int log_branches, int reverse_display_order,
4148 struct got_reflist_object_id_map *refs_idmap, int one_line,
4151 const struct got_error *err;
4152 struct got_commit_graph *graph;
4155 struct got_object_id_queue reversed_commits;
4156 struct got_object_qid *qid;
4157 struct got_commit_object *commit;
4158 struct got_pathlist_head changed_paths;
4159 struct got_pathlist_entry *pe;
4161 STAILQ_INIT(&reversed_commits);
4162 TAILQ_INIT(&changed_paths);
4164 if (search_pattern && regcomp(®ex, search_pattern,
4165 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4166 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4168 err = got_commit_graph_open(&graph, path, !log_branches);
4171 err = got_commit_graph_iter_start(graph, root_id, repo,
4172 check_cancelled, NULL);
4176 struct got_object_id *id;
4178 if (sigint_received || sigpipe_received)
4181 err = got_commit_graph_iter_next(&id, graph, repo,
4182 check_cancelled, NULL);
4184 if (err->code == GOT_ERR_ITER_COMPLETED)
4191 err = got_object_open_as_commit(&commit, repo, id);
4195 if (show_changed_paths && !reverse_display_order) {
4196 err = get_changed_paths(&changed_paths, commit, repo);
4201 if (search_pattern) {
4202 err = match_commit(&have_match, id, commit, ®ex);
4204 got_object_commit_close(commit);
4207 if (have_match == 0 && show_changed_paths)
4208 match_changed_paths(&have_match,
4209 &changed_paths, ®ex);
4210 if (have_match == 0 && show_patch) {
4211 err = match_patch(&have_match, commit, id,
4212 path, diff_context, repo, ®ex,
4217 if (have_match == 0) {
4218 got_object_commit_close(commit);
4219 TAILQ_FOREACH(pe, &changed_paths, entry) {
4220 free((char *)pe->path);
4223 got_pathlist_free(&changed_paths);
4228 if (reverse_display_order) {
4229 err = got_object_qid_alloc(&qid, id);
4232 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4233 got_object_commit_close(commit);
4236 err = print_commit_oneline(commit, id,
4239 err = print_commit(commit, id, repo, path,
4240 show_changed_paths ? &changed_paths : NULL,
4241 show_patch, diff_context, refs_idmap, NULL);
4242 got_object_commit_close(commit);
4246 if ((limit && --limit == 0) ||
4247 (end_id && got_object_id_cmp(id, end_id) == 0))
4250 TAILQ_FOREACH(pe, &changed_paths, entry) {
4251 free((char *)pe->path);
4254 got_pathlist_free(&changed_paths);
4256 if (reverse_display_order) {
4257 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4258 err = got_object_open_as_commit(&commit, repo,
4262 if (show_changed_paths) {
4263 err = get_changed_paths(&changed_paths,
4269 err = print_commit_oneline(commit, &qid->id,
4272 err = print_commit(commit, &qid->id, repo, path,
4273 show_changed_paths ? &changed_paths : NULL,
4274 show_patch, diff_context, refs_idmap, NULL);
4275 got_object_commit_close(commit);
4278 TAILQ_FOREACH(pe, &changed_paths, entry) {
4279 free((char *)pe->path);
4282 got_pathlist_free(&changed_paths);
4286 while (!STAILQ_EMPTY(&reversed_commits)) {
4287 qid = STAILQ_FIRST(&reversed_commits);
4288 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4289 got_object_qid_free(qid);
4291 TAILQ_FOREACH(pe, &changed_paths, entry) {
4292 free((char *)pe->path);
4295 got_pathlist_free(&changed_paths);
4298 got_commit_graph_close(graph);
4305 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4306 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4307 "[-r repository-path] [-R] [path]\n", getprogname());
4312 get_default_log_limit(void)
4314 const char *got_default_log_limit;
4318 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4319 if (got_default_log_limit == NULL)
4321 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4327 static const struct got_error *
4328 cmd_log(int argc, char *argv[])
4330 const struct got_error *error;
4331 struct got_repository *repo = NULL;
4332 struct got_worktree *worktree = NULL;
4333 struct got_object_id *start_id = NULL, *end_id = NULL;
4334 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4335 const char *start_commit = NULL, *end_commit = NULL;
4336 const char *search_pattern = NULL;
4337 int diff_context = -1, ch;
4338 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4339 int reverse_display_order = 0, one_line = 0;
4341 struct got_reflist_head refs;
4342 struct got_reflist_object_id_map *refs_idmap = NULL;
4343 FILE *tmpfile = NULL;
4344 int *pack_fds = NULL;
4349 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4355 limit = get_default_log_limit();
4357 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4363 show_changed_paths = 1;
4366 start_commit = optarg;
4369 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4372 errx(1, "number of context lines is %s: %s",
4376 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4378 errx(1, "number of commits is %s: %s",
4385 repo_path = realpath(optarg, NULL);
4386 if (repo_path == NULL)
4387 return got_error_from_errno2("realpath",
4389 got_path_strip_trailing_slashes(repo_path);
4392 reverse_display_order = 1;
4398 search_pattern = optarg;
4401 end_commit = optarg;
4412 if (diff_context == -1)
4414 else if (!show_patch)
4415 errx(1, "-C requires -p");
4417 if (one_line && (show_patch || show_changed_paths))
4418 errx(1, "cannot use -s with -p or -P");
4420 cwd = getcwd(NULL, 0);
4422 error = got_error_from_errno("getcwd");
4426 error = got_repo_pack_fds_open(&pack_fds);
4430 if (repo_path == NULL) {
4431 error = got_worktree_open(&worktree, cwd);
4432 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4439 error = got_worktree_resolve_path(&path, worktree,
4444 path = strdup(argv[0]);
4446 error = got_error_from_errno("strdup");
4450 } else if (argc != 0)
4453 if (repo_path == NULL) {
4454 repo_path = worktree ?
4455 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4457 if (repo_path == NULL) {
4458 error = got_error_from_errno("strdup");
4462 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4466 error = apply_unveil(got_repo_get_path(repo), 1,
4467 worktree ? got_worktree_get_root_path(worktree) : NULL);
4471 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4475 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4479 if (start_commit == NULL) {
4480 struct got_reference *head_ref;
4481 struct got_commit_object *commit = NULL;
4482 error = got_ref_open(&head_ref, repo,
4483 worktree ? got_worktree_get_head_ref_name(worktree)
4487 error = got_ref_resolve(&start_id, repo, head_ref);
4488 got_ref_close(head_ref);
4491 error = got_object_open_as_commit(&commit, repo,
4495 got_object_commit_close(commit);
4497 error = got_repo_match_object_id(&start_id, NULL,
4498 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4502 if (end_commit != NULL) {
4503 error = got_repo_match_object_id(&end_id, NULL,
4504 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4511 * If a path was specified on the command line it was resolved
4512 * to a path in the work tree above. Prepend the work tree's
4513 * path prefix to obtain the corresponding in-repository path.
4517 prefix = got_worktree_get_path_prefix(worktree);
4518 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4519 (path[0] != '\0') ? "/" : "", path) == -1) {
4520 error = got_error_from_errno("asprintf");
4525 error = got_repo_map_path(&in_repo_path, repo,
4531 path = in_repo_path;
4535 /* Release work tree lock. */
4536 got_worktree_close(worktree);
4540 if (search_pattern && show_patch) {
4541 tmpfile = got_opentemp();
4542 if (tmpfile == NULL) {
4543 error = got_error_from_errno("got_opentemp");
4548 error = print_commits(start_id, end_id, repo, path ? path : "",
4549 show_changed_paths, show_patch, search_pattern, diff_context,
4550 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4557 got_worktree_close(worktree);
4559 const struct got_error *close_err = got_repo_close(repo);
4564 const struct got_error *pack_err =
4565 got_repo_pack_fds_close(pack_fds);
4570 got_reflist_object_id_map_free(refs_idmap);
4571 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4572 error = got_error_from_errno("fclose");
4573 got_ref_list_free(&refs);
4580 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4581 "[-r repository-path] [-s] [-w] [-P] "
4582 "[object1 object2 | path ...]\n", getprogname());
4586 struct print_diff_arg {
4587 struct got_repository *repo;
4588 struct got_worktree *worktree;
4593 int ignore_whitespace;
4594 int force_text_diff;
4598 * Create a file which contains the target path of a symlink so we can feed
4599 * it as content to the diff engine.
4601 static const struct got_error *
4602 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4603 const char *abspath)
4605 const struct got_error *err = NULL;
4606 char target_path[PATH_MAX];
4607 ssize_t target_len, outlen;
4612 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4613 if (target_len == -1)
4614 return got_error_from_errno2("readlinkat", abspath);
4616 target_len = readlink(abspath, target_path, PATH_MAX);
4617 if (target_len == -1)
4618 return got_error_from_errno2("readlink", abspath);
4621 *fd = got_opentempfd();
4623 return got_error_from_errno("got_opentempfd");
4625 outlen = write(*fd, target_path, target_len);
4627 err = got_error_from_errno("got_opentempfd");
4631 if (lseek(*fd, 0, SEEK_SET) == -1) {
4632 err = got_error_from_errno2("lseek", abspath);
4643 static const struct got_error *
4644 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4645 const char *path, struct got_object_id *blob_id,
4646 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4647 int dirfd, const char *de_name)
4649 struct print_diff_arg *a = arg;
4650 const struct got_error *err = NULL;
4651 struct got_blob_object *blob1 = NULL;
4652 int fd = -1, fd1 = -1, fd2 = -1;
4653 FILE *f1 = NULL, *f2 = NULL;
4654 char *abspath = NULL, *label1 = NULL;
4658 if (a->diff_staged) {
4659 if (staged_status != GOT_STATUS_MODIFY &&
4660 staged_status != GOT_STATUS_ADD &&
4661 staged_status != GOT_STATUS_DELETE)
4664 if (staged_status == GOT_STATUS_DELETE)
4666 if (status == GOT_STATUS_NONEXISTENT)
4667 return got_error_set_errno(ENOENT, path);
4668 if (status != GOT_STATUS_MODIFY &&
4669 status != GOT_STATUS_ADD &&
4670 status != GOT_STATUS_DELETE &&
4671 status != GOT_STATUS_CONFLICT)
4675 if (!a->header_shown) {
4676 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4677 got_worktree_get_root_path(a->worktree));
4678 printf("commit - %s\n", a->id_str);
4679 printf("path + %s%s\n",
4680 got_worktree_get_root_path(a->worktree),
4681 a->diff_staged ? " (staged changes)" : "");
4682 a->header_shown = 1;
4685 if (a->diff_staged) {
4686 const char *label1 = NULL, *label2 = NULL;
4687 switch (staged_status) {
4688 case GOT_STATUS_MODIFY:
4692 case GOT_STATUS_ADD:
4695 case GOT_STATUS_DELETE:
4699 return got_error(GOT_ERR_FILE_STATUS);
4701 f1 = got_opentemp();
4703 err = got_error_from_errno("got_opentemp");
4706 f2 = got_opentemp();
4708 err = got_error_from_errno("got_opentemp");
4711 fd1 = got_opentempfd();
4713 err = got_error_from_errno("got_opentempfd");
4716 fd2 = got_opentempfd();
4718 err = got_error_from_errno("got_opentempfd");
4721 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
4722 blob_id, staged_blob_id, label1, label2, a->diff_context,
4723 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4727 fd1 = got_opentempfd();
4729 err = got_error_from_errno("got_opentempfd");
4733 if (staged_status == GOT_STATUS_ADD ||
4734 staged_status == GOT_STATUS_MODIFY) {
4736 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4740 err = got_object_id_str(&id_str, staged_blob_id);
4743 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4744 err = got_error_from_errno("asprintf");
4749 } else if (status != GOT_STATUS_ADD) {
4750 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4756 if (status != GOT_STATUS_DELETE) {
4757 if (asprintf(&abspath, "%s/%s",
4758 got_worktree_get_root_path(a->worktree), path) == -1) {
4759 err = got_error_from_errno("asprintf");
4764 fd = openat(dirfd, de_name,
4765 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4767 if (!got_err_open_nofollow_on_symlink()) {
4768 err = got_error_from_errno2("openat",
4772 err = get_symlink_target_file(&fd, dirfd,
4778 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4780 if (!got_err_open_nofollow_on_symlink()) {
4781 err = got_error_from_errno2("open",
4785 err = get_symlink_target_file(&fd, dirfd,
4791 if (fstat(fd, &sb) == -1) {
4792 err = got_error_from_errno2("fstat", abspath);
4795 f2 = fdopen(fd, "r");
4797 err = got_error_from_errno2("fdopen", abspath);
4805 f1 = got_opentemp();
4807 err = got_error_from_errno("got_opentemp");
4810 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
4816 err = got_diff_blob_file(blob1, f1, size1, label1, f2, sb.st_size,
4817 path, a->diff_context, a->ignore_whitespace, a->force_text_diff,
4820 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4821 err = got_error_from_errno("close");
4822 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4823 err = got_error_from_errno("close");
4825 got_object_blob_close(blob1);
4826 if (f1 && fclose(f1) == EOF && err == NULL)
4827 err = got_error_from_errno("fclose");
4828 if (f2 && fclose(f2) == EOF && err == NULL)
4829 err = got_error_from_errno("fclose");
4830 if (fd != -1 && close(fd) == -1 && err == NULL)
4831 err = got_error_from_errno("close");
4836 static const struct got_error *
4837 cmd_diff(int argc, char *argv[])
4839 const struct got_error *error;
4840 struct got_repository *repo = NULL;
4841 struct got_worktree *worktree = NULL;
4842 char *cwd = NULL, *repo_path = NULL;
4843 const char *commit_args[2] = { NULL, NULL };
4844 int ncommit_args = 0;
4845 struct got_object_id *ids[2] = { NULL, NULL };
4846 char *labels[2] = { NULL, NULL };
4847 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4848 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4849 int force_text_diff = 0, force_path = 0, rflag = 0;
4851 struct got_reflist_head refs;
4852 struct got_pathlist_head paths;
4853 struct got_pathlist_entry *pe;
4854 FILE *f1 = NULL, *f2 = NULL;
4855 int fd1 = -1, fd2 = -1;
4856 int *pack_fds = NULL;
4862 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4867 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4870 force_text_diff = 1;
4873 if (ncommit_args >= 2)
4874 errx(1, "too many -c options used");
4875 commit_args[ncommit_args++] = optarg;
4878 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4881 errx(1, "number of context lines is %s: %s",
4885 repo_path = realpath(optarg, NULL);
4886 if (repo_path == NULL)
4887 return got_error_from_errno2("realpath",
4889 got_path_strip_trailing_slashes(repo_path);
4896 ignore_whitespace = 1;
4910 cwd = getcwd(NULL, 0);
4912 error = got_error_from_errno("getcwd");
4916 error = got_repo_pack_fds_open(&pack_fds);
4920 if (repo_path == NULL) {
4921 error = got_worktree_open(&worktree, cwd);
4922 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4928 strdup(got_worktree_get_repo_path(worktree));
4929 if (repo_path == NULL) {
4930 error = got_error_from_errno("strdup");
4934 repo_path = strdup(cwd);
4935 if (repo_path == NULL) {
4936 error = got_error_from_errno("strdup");
4942 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4947 if (rflag || worktree == NULL || ncommit_args > 0) {
4949 error = got_error_msg(GOT_ERR_NOT_IMPL,
4950 "-P option can only be used when diffing "
4955 error = got_error_msg(GOT_ERR_NOT_IMPL,
4956 "-s option can only be used when diffing "
4962 error = apply_unveil(got_repo_get_path(repo), 1,
4963 worktree ? got_worktree_get_root_path(worktree) : NULL);
4967 if ((!force_path && argc == 2) || ncommit_args > 0) {
4968 int obj_type = (ncommit_args > 0 ?
4969 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4970 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4974 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4976 if (ncommit_args > 0)
4977 arg = commit_args[i];
4980 error = got_repo_match_object_id(&ids[i], &labels[i],
4981 arg, obj_type, &refs, repo);
4983 if (error->code != GOT_ERR_NOT_REF &&
4984 error->code != GOT_ERR_NO_OBJ)
4986 if (ncommit_args > 0)
4994 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4995 struct print_diff_arg arg;
4998 if (worktree == NULL) {
4999 if (argc == 2 && ids[0] == NULL) {
5000 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5002 } else if (argc == 2 && ids[1] == NULL) {
5003 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5005 } else if (argc > 0) {
5006 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5007 "%s", "specified paths cannot be resolved");
5010 error = got_error(GOT_ERR_NOT_WORKTREE);
5015 error = get_worktree_paths_from_argv(&paths, argc, argv,
5020 error = got_object_id_str(&id_str,
5021 got_worktree_get_base_commit_id(worktree));
5025 arg.worktree = worktree;
5026 arg.diff_context = diff_context;
5027 arg.id_str = id_str;
5028 arg.header_shown = 0;
5029 arg.diff_staged = diff_staged;
5030 arg.ignore_whitespace = ignore_whitespace;
5031 arg.force_text_diff = force_text_diff;
5033 error = got_worktree_status(worktree, &paths, repo, 0,
5034 print_diff, &arg, check_cancelled, NULL);
5039 if (ncommit_args == 1) {
5040 struct got_commit_object *commit;
5041 error = got_object_open_as_commit(&commit, repo, ids[0]);
5045 labels[1] = labels[0];
5047 if (got_object_commit_get_nparents(commit) > 0) {
5048 const struct got_object_id_queue *pids;
5049 struct got_object_qid *pid;
5050 pids = got_object_commit_get_parent_ids(commit);
5051 pid = STAILQ_FIRST(pids);
5052 ids[0] = got_object_id_dup(&pid->id);
5053 if (ids[0] == NULL) {
5054 error = got_error_from_errno(
5055 "got_object_id_dup");
5056 got_object_commit_close(commit);
5059 error = got_object_id_str(&labels[0], ids[0]);
5061 got_object_commit_close(commit);
5066 labels[0] = strdup("/dev/null");
5067 if (labels[0] == NULL) {
5068 error = got_error_from_errno("strdup");
5069 got_object_commit_close(commit);
5074 got_object_commit_close(commit);
5077 if (ncommit_args == 0 && argc > 2) {
5078 error = got_error_msg(GOT_ERR_BAD_PATH,
5079 "path arguments cannot be used when diffing two objects");
5084 error = got_object_get_type(&type1, repo, ids[0]);
5089 error = got_object_get_type(&type2, repo, ids[1]);
5092 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5093 error = got_error(GOT_ERR_OBJ_TYPE);
5096 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5097 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5098 "path arguments cannot be used when diffing blobs");
5102 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5104 struct got_pathlist_entry *new;
5108 error = got_worktree_resolve_path(&p, worktree,
5112 prefix = got_worktree_get_path_prefix(worktree);
5113 while (prefix[0] == '/')
5115 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5116 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5118 error = got_error_from_errno("asprintf");
5124 char *mapped_path, *s;
5125 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5131 in_repo_path = strdup(s);
5132 if (in_repo_path == NULL) {
5133 error = got_error_from_errno("asprintf");
5140 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5141 if (error || new == NULL /* duplicate */)
5148 /* Release work tree lock. */
5149 got_worktree_close(worktree);
5153 f1 = got_opentemp();
5155 error = got_error_from_errno("got_opentemp");
5159 f2 = got_opentemp();
5161 error = got_error_from_errno("got_opentemp");
5165 fd1 = got_opentempfd();
5167 error = got_error_from_errno("got_opentempfd");
5171 fd2 = got_opentempfd();
5173 error = got_error_from_errno("got_opentempfd");
5177 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5178 case GOT_OBJ_TYPE_BLOB:
5179 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5180 fd1, fd2, ids[0], ids[1], NULL, NULL, diff_context,
5181 ignore_whitespace, force_text_diff, repo, stdout);
5183 case GOT_OBJ_TYPE_TREE:
5184 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5185 ids[0], ids[1], &paths, "", "", diff_context,
5186 ignore_whitespace, force_text_diff, repo, stdout);
5188 case GOT_OBJ_TYPE_COMMIT:
5189 printf("diff %s %s\n", labels[0], labels[1]);
5190 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5191 fd1, fd2, ids[0], ids[1], &paths, diff_context,
5192 ignore_whitespace, force_text_diff, repo, stdout);
5195 error = got_error(GOT_ERR_OBJ_TYPE);
5203 got_worktree_close(worktree);
5205 const struct got_error *close_err = got_repo_close(repo);
5210 const struct got_error *pack_err =
5211 got_repo_pack_fds_close(pack_fds);
5215 TAILQ_FOREACH(pe, &paths, entry)
5216 free((char *)pe->path);
5217 got_pathlist_free(&paths);
5218 got_ref_list_free(&refs);
5219 if (f1 && fclose(f1) == EOF && error == NULL)
5220 error = got_error_from_errno("fclose");
5221 if (f2 && fclose(f2) == EOF && error == NULL)
5222 error = got_error_from_errno("fclose");
5223 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5224 error = got_error_from_errno("close");
5225 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5226 error = got_error_from_errno("close");
5234 "usage: %s blame [-c commit] [-r repository-path] path\n",
5243 char datebuf[11]; /* YYYY-MM-DD + NUL */
5246 struct blame_cb_args {
5247 struct blame_line *lines;
5251 off_t *line_offsets;
5253 struct got_repository *repo;
5256 static const struct got_error *
5257 blame_cb(void *arg, int nlines, int lineno,
5258 struct got_commit_object *commit, struct got_object_id *id)
5260 const struct got_error *err = NULL;
5261 struct blame_cb_args *a = arg;
5262 struct blame_line *bline;
5264 size_t linesize = 0;
5267 time_t committer_time;
5269 if (nlines != a->nlines ||
5270 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5271 return got_error(GOT_ERR_RANGE);
5273 if (sigint_received)
5274 return got_error(GOT_ERR_ITER_COMPLETED);
5277 return NULL; /* no change in this commit */
5279 /* Annotate this line. */
5280 bline = &a->lines[lineno - 1];
5281 if (bline->annotated)
5283 err = got_object_id_str(&bline->id_str, id);
5287 bline->committer = strdup(got_object_commit_get_committer(commit));
5288 if (bline->committer == NULL) {
5289 err = got_error_from_errno("strdup");
5293 committer_time = got_object_commit_get_committer_time(commit);
5294 if (gmtime_r(&committer_time, &tm) == NULL)
5295 return got_error_from_errno("gmtime_r");
5296 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5298 err = got_error(GOT_ERR_NO_SPACE);
5301 bline->annotated = 1;
5303 /* Print lines annotated so far. */
5304 bline = &a->lines[a->lineno_cur - 1];
5305 if (!bline->annotated)
5308 offset = a->line_offsets[a->lineno_cur - 1];
5309 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5310 err = got_error_from_errno("fseeko");
5314 while (bline->annotated) {
5315 char *smallerthan, *at, *nl, *committer;
5318 if (getline(&line, &linesize, a->f) == -1) {
5320 err = got_error_from_errno("getline");
5324 committer = bline->committer;
5325 smallerthan = strchr(committer, '<');
5326 if (smallerthan && smallerthan[1] != '\0')
5327 committer = smallerthan + 1;
5328 at = strchr(committer, '@');
5331 len = strlen(committer);
5333 committer[8] = '\0';
5335 nl = strchr(line, '\n');
5338 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5339 bline->id_str, bline->datebuf, committer, line);
5342 bline = &a->lines[a->lineno_cur - 1];
5349 static const struct got_error *
5350 cmd_blame(int argc, char *argv[])
5352 const struct got_error *error;
5353 struct got_repository *repo = NULL;
5354 struct got_worktree *worktree = NULL;
5355 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5356 char *link_target = NULL;
5357 struct got_object_id *obj_id = NULL;
5358 struct got_object_id *commit_id = NULL;
5359 struct got_commit_object *commit = NULL;
5360 struct got_blob_object *blob = NULL;
5361 char *commit_id_str = NULL;
5362 struct blame_cb_args bca;
5363 int ch, obj_type, i, fd = -1, fd1 = -1;
5365 int *pack_fds = NULL;
5367 fd = got_opentempfd();
5369 return got_error_from_errno("got_opentempfd");
5371 memset(&bca, 0, sizeof(bca));
5374 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5379 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5382 commit_id_str = optarg;
5385 repo_path = realpath(optarg, NULL);
5386 if (repo_path == NULL)
5387 return got_error_from_errno2("realpath",
5389 got_path_strip_trailing_slashes(repo_path);
5405 cwd = getcwd(NULL, 0);
5407 error = got_error_from_errno("getcwd");
5411 error = got_repo_pack_fds_open(&pack_fds);
5415 if (repo_path == NULL) {
5416 error = got_worktree_open(&worktree, cwd);
5417 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5423 strdup(got_worktree_get_repo_path(worktree));
5424 if (repo_path == NULL) {
5425 error = got_error_from_errno("strdup");
5430 repo_path = strdup(cwd);
5431 if (repo_path == NULL) {
5432 error = got_error_from_errno("strdup");
5438 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5443 const char *prefix = got_worktree_get_path_prefix(worktree);
5446 error = got_worktree_resolve_path(&p, worktree, path);
5449 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5450 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5452 error = got_error_from_errno("asprintf");
5457 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5459 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5462 error = got_repo_map_path(&in_repo_path, repo, path);
5467 if (commit_id_str == NULL) {
5468 struct got_reference *head_ref;
5469 error = got_ref_open(&head_ref, repo, worktree ?
5470 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5473 error = got_ref_resolve(&commit_id, repo, head_ref);
5474 got_ref_close(head_ref);
5478 struct got_reflist_head refs;
5480 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5484 error = got_repo_match_object_id(&commit_id, NULL,
5485 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5486 got_ref_list_free(&refs);
5492 /* Release work tree lock. */
5493 got_worktree_close(worktree);
5497 error = got_object_open_as_commit(&commit, repo, commit_id);
5501 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5506 error = got_object_id_by_path(&obj_id, repo, commit,
5507 link_target ? link_target : in_repo_path);
5511 error = got_object_get_type(&obj_type, repo, obj_id);
5515 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5516 error = got_error_path(link_target ? link_target : in_repo_path,
5521 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
5524 bca.f = got_opentemp();
5525 if (bca.f == NULL) {
5526 error = got_error_from_errno("got_opentemp");
5529 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5530 &bca.line_offsets, bca.f, blob);
5531 if (error || bca.nlines == 0)
5534 /* Don't include \n at EOF in the blame line count. */
5535 if (bca.line_offsets[bca.nlines - 1] == filesize)
5538 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5539 if (bca.lines == NULL) {
5540 error = got_error_from_errno("calloc");
5544 bca.nlines_prec = 0;
5552 fd1 = got_opentempfd();
5554 error = got_error_from_errno("got_opentempfd");
5557 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5558 repo, blame_cb, &bca, check_cancelled, NULL, fd1);
5567 got_object_commit_close(commit);
5568 if (fd != -1 && close(fd) == -1 && error == NULL)
5569 error = got_error_from_errno("close");
5570 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5571 error = got_error_from_errno("close");
5573 got_object_blob_close(blob);
5575 got_worktree_close(worktree);
5577 const struct got_error *close_err = got_repo_close(repo);
5582 const struct got_error *pack_err =
5583 got_repo_pack_fds_close(pack_fds);
5588 for (i = 0; i < bca.nlines; i++) {
5589 struct blame_line *bline = &bca.lines[i];
5590 free(bline->id_str);
5591 free(bline->committer);
5595 free(bca.line_offsets);
5596 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5597 error = got_error_from_errno("fclose");
5605 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5610 static const struct got_error *
5611 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5612 const char *root_path, struct got_repository *repo)
5614 const struct got_error *err = NULL;
5615 int is_root_path = (strcmp(path, root_path) == 0);
5616 const char *modestr = "";
5617 mode_t mode = got_tree_entry_get_mode(te);
5618 char *link_target = NULL;
5620 path += strlen(root_path);
5621 while (path[0] == '/')
5624 if (got_object_tree_entry_is_submodule(te))
5626 else if (S_ISLNK(mode)) {
5629 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5632 for (i = 0; i < strlen(link_target); i++) {
5633 if (!isprint((unsigned char)link_target[i]))
5634 link_target[i] = '?';
5639 else if (S_ISDIR(mode))
5641 else if (mode & S_IXUSR)
5644 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5645 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5646 link_target ? " -> ": "", link_target ? link_target : "");
5652 static const struct got_error *
5653 print_tree(const char *path, struct got_commit_object *commit,
5654 int show_ids, int recurse, const char *root_path,
5655 struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct got_object_id *tree_id = NULL;
5659 struct got_tree_object *tree = NULL;
5662 err = got_object_id_by_path(&tree_id, repo, commit, path);
5666 err = got_object_open_as_tree(&tree, repo, tree_id);
5669 nentries = got_object_tree_get_nentries(tree);
5670 for (i = 0; i < nentries; i++) {
5671 struct got_tree_entry *te;
5674 if (sigint_received || sigpipe_received)
5677 te = got_object_tree_get_entry(tree, i);
5680 err = got_object_id_str(&id_str,
5681 got_tree_entry_get_id(te));
5684 if (asprintf(&id, "%s ", id_str) == -1) {
5685 err = got_error_from_errno("asprintf");
5691 err = print_entry(te, id, path, root_path, repo);
5696 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5698 if (asprintf(&child_path, "%s%s%s", path,
5699 path[0] == '/' && path[1] == '\0' ? "" : "/",
5700 got_tree_entry_get_name(te)) == -1) {
5701 err = got_error_from_errno("asprintf");
5704 err = print_tree(child_path, commit, show_ids, 1,
5713 got_object_tree_close(tree);
5718 static const struct got_error *
5719 cmd_tree(int argc, char *argv[])
5721 const struct got_error *error;
5722 struct got_repository *repo = NULL;
5723 struct got_worktree *worktree = NULL;
5724 const char *path, *refname = NULL;
5725 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5726 struct got_object_id *commit_id = NULL;
5727 struct got_commit_object *commit = NULL;
5728 char *commit_id_str = NULL;
5729 int show_ids = 0, recurse = 0;
5731 int *pack_fds = NULL;
5734 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5739 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5742 commit_id_str = optarg;
5745 repo_path = realpath(optarg, NULL);
5746 if (repo_path == NULL)
5747 return got_error_from_errno2("realpath",
5749 got_path_strip_trailing_slashes(repo_path);
5773 cwd = getcwd(NULL, 0);
5775 error = got_error_from_errno("getcwd");
5779 error = got_repo_pack_fds_open(&pack_fds);
5783 if (repo_path == NULL) {
5784 error = got_worktree_open(&worktree, cwd);
5785 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5791 strdup(got_worktree_get_repo_path(worktree));
5792 if (repo_path == NULL)
5793 error = got_error_from_errno("strdup");
5797 repo_path = strdup(cwd);
5798 if (repo_path == NULL) {
5799 error = got_error_from_errno("strdup");
5805 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5810 const char *prefix = got_worktree_get_path_prefix(worktree);
5815 error = got_worktree_resolve_path(&p, worktree, path);
5818 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5819 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5821 error = got_error_from_errno("asprintf");
5826 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5830 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5835 error = got_repo_map_path(&in_repo_path, repo, path);
5840 if (commit_id_str == NULL) {
5841 struct got_reference *head_ref;
5843 refname = got_worktree_get_head_ref_name(worktree);
5845 refname = GOT_REF_HEAD;
5846 error = got_ref_open(&head_ref, repo, refname, 0);
5849 error = got_ref_resolve(&commit_id, repo, head_ref);
5850 got_ref_close(head_ref);
5854 struct got_reflist_head refs;
5856 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5860 error = got_repo_match_object_id(&commit_id, NULL,
5861 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5862 got_ref_list_free(&refs);
5868 /* Release work tree lock. */
5869 got_worktree_close(worktree);
5873 error = got_object_open_as_commit(&commit, repo, commit_id);
5877 error = print_tree(in_repo_path, commit, show_ids, recurse,
5878 in_repo_path, repo);
5885 got_object_commit_close(commit);
5887 got_worktree_close(worktree);
5889 const struct got_error *close_err = got_repo_close(repo);
5894 const struct got_error *pack_err =
5895 got_repo_pack_fds_close(pack_fds);
5905 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5906 "[-S status-codes] [path ...]\n", getprogname());
5910 struct got_status_arg {
5915 static const struct got_error *
5916 print_status(void *arg, unsigned char status, unsigned char staged_status,
5917 const char *path, struct got_object_id *blob_id,
5918 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5919 int dirfd, const char *de_name)
5921 struct got_status_arg *st = arg;
5923 if (status == staged_status && (status == GOT_STATUS_DELETE))
5924 status = GOT_STATUS_NO_CHANGE;
5925 if (st != NULL && st->status_codes) {
5926 size_t ncodes = strlen(st->status_codes);
5929 for (i = 0; i < ncodes ; i++) {
5931 if (status == st->status_codes[i] ||
5932 staged_status == st->status_codes[i]) {
5937 if (status == st->status_codes[i] ||
5938 staged_status == st->status_codes[i])
5943 if (st->suppress && j == 0)
5950 printf("%c%c %s\n", status, staged_status, path);
5954 static const struct got_error *
5955 cmd_status(int argc, char *argv[])
5957 const struct got_error *error = NULL;
5958 struct got_repository *repo = NULL;
5959 struct got_worktree *worktree = NULL;
5960 struct got_status_arg st;
5962 struct got_pathlist_head paths;
5963 struct got_pathlist_entry *pe;
5964 int ch, i, no_ignores = 0;
5965 int *pack_fds = NULL;
5969 memset(&st, 0, sizeof(st));
5970 st.status_codes = NULL;
5973 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5979 if (st.status_codes != NULL && st.suppress == 0)
5980 option_conflict('S', 's');
5984 for (i = 0; i < strlen(optarg); i++) {
5985 switch (optarg[i]) {
5986 case GOT_STATUS_MODIFY:
5987 case GOT_STATUS_ADD:
5988 case GOT_STATUS_DELETE:
5989 case GOT_STATUS_CONFLICT:
5990 case GOT_STATUS_MISSING:
5991 case GOT_STATUS_OBSTRUCTED:
5992 case GOT_STATUS_UNVERSIONED:
5993 case GOT_STATUS_MODE_CHANGE:
5994 case GOT_STATUS_NONEXISTENT:
5997 errx(1, "invalid status code '%c'",
6001 if (ch == 's' && st.suppress)
6002 option_conflict('s', 'S');
6003 st.status_codes = optarg;
6015 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6019 cwd = getcwd(NULL, 0);
6021 error = got_error_from_errno("getcwd");
6025 error = got_repo_pack_fds_open(&pack_fds);
6029 error = got_worktree_open(&worktree, cwd);
6031 if (error->code == GOT_ERR_NOT_WORKTREE)
6032 error = wrap_not_worktree_error(error, "status", cwd);
6036 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6041 error = apply_unveil(got_repo_get_path(repo), 1,
6042 got_worktree_get_root_path(worktree));
6046 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6050 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6051 print_status, &st, check_cancelled, NULL);
6054 const struct got_error *pack_err =
6055 got_repo_pack_fds_close(pack_fds);
6060 TAILQ_FOREACH(pe, &paths, entry)
6061 free((char *)pe->path);
6062 got_pathlist_free(&paths);
6071 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6072 "[-s reference] [-d] [name]\n",
6077 static const struct got_error *
6078 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6080 static const struct got_error *err = NULL;
6081 struct got_reflist_head refs;
6082 struct got_reflist_entry *re;
6085 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6086 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6091 TAILQ_FOREACH(re, &refs, entry) {
6093 refstr = got_ref_to_str(re->ref);
6094 if (refstr == NULL) {
6095 err = got_error_from_errno("got_ref_to_str");
6098 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6102 got_ref_list_free(&refs);
6106 static const struct got_error *
6107 delete_ref_by_name(struct got_repository *repo, const char *refname)
6109 const struct got_error *err;
6110 struct got_reference *ref;
6112 err = got_ref_open(&ref, repo, refname, 0);
6116 err = delete_ref(repo, ref);
6121 static const struct got_error *
6122 add_ref(struct got_repository *repo, const char *refname, const char *target)
6124 const struct got_error *err = NULL;
6125 struct got_object_id *id = NULL;
6126 struct got_reference *ref = NULL;
6127 struct got_reflist_head refs;
6130 * Don't let the user create a reference name with a leading '-'.
6131 * While technically a valid reference name, this case is usually
6132 * an unintended typo.
6134 if (refname[0] == '-')
6135 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6138 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6141 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6143 got_ref_list_free(&refs);
6147 err = got_ref_alloc(&ref, refname, id);
6151 err = got_ref_write(ref, repo);
6159 static const struct got_error *
6160 add_symref(struct got_repository *repo, const char *refname, const char *target)
6162 const struct got_error *err = NULL;
6163 struct got_reference *ref = NULL;
6164 struct got_reference *target_ref = NULL;
6167 * Don't let the user create a reference name with a leading '-'.
6168 * While technically a valid reference name, this case is usually
6169 * an unintended typo.
6171 if (refname[0] == '-')
6172 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6174 err = got_ref_open(&target_ref, repo, target, 0);
6178 err = got_ref_alloc_symref(&ref, refname, target_ref);
6182 err = got_ref_write(ref, repo);
6185 got_ref_close(target_ref);
6191 static const struct got_error *
6192 cmd_ref(int argc, char *argv[])
6194 const struct got_error *error = NULL;
6195 struct got_repository *repo = NULL;
6196 struct got_worktree *worktree = NULL;
6197 char *cwd = NULL, *repo_path = NULL;
6198 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6199 const char *obj_arg = NULL, *symref_target= NULL;
6200 char *refname = NULL;
6201 int *pack_fds = NULL;
6203 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6212 repo_path = realpath(optarg, NULL);
6213 if (repo_path == NULL)
6214 return got_error_from_errno2("realpath",
6216 got_path_strip_trailing_slashes(repo_path);
6222 symref_target = optarg;
6233 if (obj_arg && do_list)
6234 option_conflict('c', 'l');
6235 if (obj_arg && do_delete)
6236 option_conflict('c', 'd');
6237 if (obj_arg && symref_target)
6238 option_conflict('c', 's');
6239 if (symref_target && do_delete)
6240 option_conflict('s', 'd');
6241 if (symref_target && do_list)
6242 option_conflict('s', 'l');
6243 if (do_delete && do_list)
6244 option_conflict('d', 'l');
6245 if (sort_by_time && !do_list)
6246 errx(1, "-t option requires -l option");
6252 if (argc != 0 && argc != 1)
6255 refname = strdup(argv[0]);
6256 if (refname == NULL) {
6257 error = got_error_from_errno("strdup");
6264 refname = strdup(argv[0]);
6265 if (refname == NULL) {
6266 error = got_error_from_errno("strdup");
6272 got_path_strip_trailing_slashes(refname);
6275 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6276 "sendfd unveil", NULL) == -1)
6279 cwd = getcwd(NULL, 0);
6281 error = got_error_from_errno("getcwd");
6285 error = got_repo_pack_fds_open(&pack_fds);
6289 if (repo_path == NULL) {
6290 error = got_worktree_open(&worktree, cwd);
6291 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6297 strdup(got_worktree_get_repo_path(worktree));
6298 if (repo_path == NULL)
6299 error = got_error_from_errno("strdup");
6303 repo_path = strdup(cwd);
6304 if (repo_path == NULL) {
6305 error = got_error_from_errno("strdup");
6311 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6317 /* Remove "cpath" promise. */
6318 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6324 error = apply_unveil(got_repo_get_path(repo), do_list,
6325 worktree ? got_worktree_get_root_path(worktree) : NULL);
6330 error = list_refs(repo, refname, sort_by_time);
6332 error = delete_ref_by_name(repo, refname);
6333 else if (symref_target)
6334 error = add_symref(repo, refname, symref_target);
6336 if (obj_arg == NULL)
6338 error = add_ref(repo, refname, obj_arg);
6343 const struct got_error *close_err = got_repo_close(repo);
6348 got_worktree_close(worktree);
6350 const struct got_error *pack_err =
6351 got_repo_pack_fds_close(pack_fds);
6364 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6365 "[-n] [name]\n", getprogname());
6369 static const struct got_error *
6370 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6371 struct got_reference *ref)
6373 const struct got_error *err = NULL;
6374 const char *refname, *marker = " ";
6377 refname = got_ref_get_name(ref);
6378 if (worktree && strcmp(refname,
6379 got_worktree_get_head_ref_name(worktree)) == 0) {
6380 struct got_object_id *id = NULL;
6382 err = got_ref_resolve(&id, repo, ref);
6385 if (got_object_id_cmp(id,
6386 got_worktree_get_base_commit_id(worktree)) == 0)
6393 if (strncmp(refname, "refs/heads/", 11) == 0)
6395 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6397 if (strncmp(refname, "refs/remotes/", 13) == 0)
6400 refstr = got_ref_to_str(ref);
6402 return got_error_from_errno("got_ref_to_str");
6404 printf("%s%s: %s\n", marker, refname, refstr);
6409 static const struct got_error *
6410 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6412 const char *refname;
6414 if (worktree == NULL)
6415 return got_error(GOT_ERR_NOT_WORKTREE);
6417 refname = got_worktree_get_head_ref_name(worktree);
6419 if (strncmp(refname, "refs/heads/", 11) == 0)
6421 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6424 printf("%s\n", refname);
6429 static const struct got_error *
6430 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6433 static const struct got_error *err = NULL;
6434 struct got_reflist_head refs;
6435 struct got_reflist_entry *re;
6436 struct got_reference *temp_ref = NULL;
6437 int rebase_in_progress, histedit_in_progress;
6442 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6447 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6452 if (rebase_in_progress || histedit_in_progress) {
6453 err = got_ref_open(&temp_ref, repo,
6454 got_worktree_get_head_ref_name(worktree), 0);
6457 list_branch(repo, worktree, temp_ref);
6458 got_ref_close(temp_ref);
6462 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6463 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6468 TAILQ_FOREACH(re, &refs, entry)
6469 list_branch(repo, worktree, re->ref);
6471 got_ref_list_free(&refs);
6473 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6474 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6479 TAILQ_FOREACH(re, &refs, entry)
6480 list_branch(repo, worktree, re->ref);
6482 got_ref_list_free(&refs);
6487 static const struct got_error *
6488 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6489 const char *branch_name)
6491 const struct got_error *err = NULL;
6492 struct got_reference *ref = NULL;
6493 char *refname, *remote_refname = NULL;
6495 if (strncmp(branch_name, "refs/", 5) == 0)
6497 if (strncmp(branch_name, "heads/", 6) == 0)
6499 else if (strncmp(branch_name, "remotes/", 8) == 0)
6502 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6503 return got_error_from_errno("asprintf");
6505 if (asprintf(&remote_refname, "refs/remotes/%s",
6506 branch_name) == -1) {
6507 err = got_error_from_errno("asprintf");
6511 err = got_ref_open(&ref, repo, refname, 0);
6513 const struct got_error *err2;
6514 if (err->code != GOT_ERR_NOT_REF)
6517 * Keep 'err' intact such that if neither branch exists
6518 * we report "refs/heads" rather than "refs/remotes" in
6519 * our error message.
6521 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6528 strcmp(got_worktree_get_head_ref_name(worktree),
6529 got_ref_get_name(ref)) == 0) {
6530 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6531 "will not delete this work tree's current branch");
6535 err = delete_ref(repo, ref);
6540 free(remote_refname);
6544 static const struct got_error *
6545 add_branch(struct got_repository *repo, const char *branch_name,
6546 struct got_object_id *base_commit_id)
6548 const struct got_error *err = NULL;
6549 struct got_reference *ref = NULL;
6550 char *base_refname = NULL, *refname = NULL;
6553 * Don't let the user create a branch name with a leading '-'.
6554 * While technically a valid reference name, this case is usually
6555 * an unintended typo.
6557 if (branch_name[0] == '-')
6558 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6560 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6563 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6564 err = got_error_from_errno("asprintf");
6568 err = got_ref_open(&ref, repo, refname, 0);
6570 err = got_error(GOT_ERR_BRANCH_EXISTS);
6572 } else if (err->code != GOT_ERR_NOT_REF)
6575 err = got_ref_alloc(&ref, refname, base_commit_id);
6579 err = got_ref_write(ref, repo);
6588 static const struct got_error *
6589 cmd_branch(int argc, char *argv[])
6591 const struct got_error *error = NULL;
6592 struct got_repository *repo = NULL;
6593 struct got_worktree *worktree = NULL;
6594 char *cwd = NULL, *repo_path = NULL;
6595 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6596 const char *delref = NULL, *commit_id_arg = NULL;
6597 struct got_reference *ref = NULL;
6598 struct got_pathlist_head paths;
6599 struct got_pathlist_entry *pe;
6600 struct got_object_id *commit_id = NULL;
6601 char *commit_id_str = NULL;
6602 int *pack_fds = NULL;
6606 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6609 commit_id_arg = optarg;
6615 repo_path = realpath(optarg, NULL);
6616 if (repo_path == NULL)
6617 return got_error_from_errno2("realpath",
6619 got_path_strip_trailing_slashes(repo_path);
6636 if (do_list && delref)
6637 option_conflict('l', 'd');
6638 if (sort_by_time && !do_list)
6639 errx(1, "-t option requires -l option");
6644 if (!do_list && !delref && argc == 0)
6647 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6648 errx(1, "-c option can only be used when creating a branch");
6650 if (do_list || delref) {
6653 } else if (!do_show && argc != 1)
6657 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6658 "sendfd unveil", NULL) == -1)
6661 cwd = getcwd(NULL, 0);
6663 error = got_error_from_errno("getcwd");
6667 error = got_repo_pack_fds_open(&pack_fds);
6671 if (repo_path == NULL) {
6672 error = got_worktree_open(&worktree, cwd);
6673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6679 strdup(got_worktree_get_repo_path(worktree));
6680 if (repo_path == NULL)
6681 error = got_error_from_errno("strdup");
6685 repo_path = strdup(cwd);
6686 if (repo_path == NULL) {
6687 error = got_error_from_errno("strdup");
6693 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6698 if (do_list || do_show) {
6699 /* Remove "cpath" promise. */
6700 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6706 error = apply_unveil(got_repo_get_path(repo), do_list,
6707 worktree ? got_worktree_get_root_path(worktree) : NULL);
6712 error = show_current_branch(repo, worktree);
6714 error = list_branches(repo, worktree, sort_by_time);
6716 error = delete_branch(repo, worktree, delref);
6718 struct got_reflist_head refs;
6720 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6724 if (commit_id_arg == NULL)
6725 commit_id_arg = worktree ?
6726 got_worktree_get_head_ref_name(worktree) :
6728 error = got_repo_match_object_id(&commit_id, NULL,
6729 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6730 got_ref_list_free(&refs);
6733 error = add_branch(repo, argv[0], commit_id);
6736 if (worktree && do_update) {
6737 struct got_update_progress_arg upa;
6738 char *branch_refname = NULL;
6740 error = got_object_id_str(&commit_id_str, commit_id);
6743 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6747 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6749 error = got_error_from_errno("asprintf");
6752 error = got_ref_open(&ref, repo, branch_refname, 0);
6753 free(branch_refname);
6756 error = switch_head_ref(ref, commit_id, worktree,
6760 error = got_worktree_set_base_commit_id(worktree, repo,
6764 memset(&upa, 0, sizeof(upa));
6765 error = got_worktree_checkout_files(worktree, &paths,
6766 repo, update_progress, &upa, check_cancelled,
6770 if (upa.did_something) {
6771 printf("Updated to %s: %s\n",
6772 got_worktree_get_head_ref_name(worktree),
6775 print_update_progress_stats(&upa);
6782 const struct got_error *close_err = got_repo_close(repo);
6787 got_worktree_close(worktree);
6789 const struct got_error *pack_err =
6790 got_repo_pack_fds_close(pack_fds);
6797 free(commit_id_str);
6798 TAILQ_FOREACH(pe, &paths, entry)
6799 free((char *)pe->path);
6800 got_pathlist_free(&paths);
6809 "usage: %s tag [-c commit] [-r repository] [-l] "
6810 "[-m message] name\n", getprogname());
6815 static const struct got_error *
6816 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6818 const struct got_error *err = NULL;
6819 struct got_reflist_entry *re, *se, *new;
6820 struct got_object_id *re_id, *se_id;
6821 struct got_tag_object *re_tag, *se_tag;
6822 time_t re_time, se_time;
6824 STAILQ_FOREACH(re, tags, entry) {
6825 se = STAILQ_FIRST(sorted);
6827 err = got_reflist_entry_dup(&new, re);
6830 STAILQ_INSERT_HEAD(sorted, new, entry);
6833 err = got_ref_resolve(&re_id, repo, re->ref);
6836 err = got_object_open_as_tag(&re_tag, repo, re_id);
6840 re_time = got_object_tag_get_tagger_time(re_tag);
6841 got_object_tag_close(re_tag);
6845 err = got_ref_resolve(&se_id, repo, re->ref);
6848 err = got_object_open_as_tag(&se_tag, repo, se_id);
6852 se_time = got_object_tag_get_tagger_time(se_tag);
6853 got_object_tag_close(se_tag);
6855 if (se_time > re_time) {
6856 err = got_reflist_entry_dup(&new, re);
6859 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6862 se = STAILQ_NEXT(se, entry);
6871 static const struct got_error *
6872 get_tag_refname(char **refname, const char *tag_name)
6874 const struct got_error *err;
6876 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6877 *refname = strdup(tag_name);
6878 if (*refname == NULL)
6879 return got_error_from_errno("strdup");
6880 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
6881 err = got_error_from_errno("asprintf");
6889 static const struct got_error *
6890 list_tags(struct got_repository *repo, const char *tag_name)
6892 static const struct got_error *err = NULL;
6893 struct got_reflist_head refs;
6894 struct got_reflist_entry *re;
6895 char *wanted_refname = NULL;
6899 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6904 struct got_reference *ref;
6905 err = get_tag_refname(&wanted_refname, tag_name);
6908 /* Wanted tag reference should exist. */
6909 err = got_ref_open(&ref, repo, wanted_refname, 0);
6915 TAILQ_FOREACH(re, &refs, entry) {
6916 const char *refname;
6917 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6921 struct got_object_id *id;
6922 struct got_tag_object *tag;
6923 struct got_commit_object *commit = NULL;
6925 refname = got_ref_get_name(re->ref);
6926 if (strncmp(refname, "refs/tags/", 10) != 0 ||
6927 (wanted_refname && strcmp(refname, wanted_refname) != 0))
6930 refstr = got_ref_to_str(re->ref);
6931 if (refstr == NULL) {
6932 err = got_error_from_errno("got_ref_to_str");
6935 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6938 err = got_ref_resolve(&id, repo, re->ref);
6941 err = got_object_open_as_tag(&tag, repo, id);
6943 if (err->code != GOT_ERR_OBJ_TYPE) {
6947 /* "lightweight" tag */
6948 err = got_object_open_as_commit(&commit, repo, id);
6953 tagger = got_object_commit_get_committer(commit);
6955 got_object_commit_get_committer_time(commit);
6956 err = got_object_id_str(&id_str, id);
6962 tagger = got_object_tag_get_tagger(tag);
6963 tagger_time = got_object_tag_get_tagger_time(tag);
6964 err = got_object_id_str(&id_str,
6965 got_object_tag_get_object_id(tag));
6969 printf("from: %s\n", tagger);
6970 datestr = get_datestr(&tagger_time, datebuf);
6972 printf("date: %s UTC\n", datestr);
6974 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6976 switch (got_object_tag_get_object_type(tag)) {
6977 case GOT_OBJ_TYPE_BLOB:
6978 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6981 case GOT_OBJ_TYPE_TREE:
6982 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6985 case GOT_OBJ_TYPE_COMMIT:
6986 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6989 case GOT_OBJ_TYPE_TAG:
6990 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6999 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7002 got_object_commit_close(commit);
7004 tagmsg0 = strdup(got_object_tag_get_message(tag));
7005 got_object_tag_close(tag);
7006 if (tagmsg0 == NULL) {
7007 err = got_error_from_errno("strdup");
7014 line = strsep(&tagmsg, "\n");
7016 printf(" %s\n", line);
7021 got_ref_list_free(&refs);
7022 free(wanted_refname);
7026 static const struct got_error *
7027 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7028 const char *tag_name, const char *repo_path)
7030 const struct got_error *err = NULL;
7031 char *template = NULL, *initial_content = NULL;
7032 char *editor = NULL;
7033 int initial_content_len;
7036 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7037 err = got_error_from_errno("asprintf");
7041 initial_content_len = asprintf(&initial_content,
7042 "\n# tagging commit %s as %s\n",
7043 commit_id_str, tag_name);
7044 if (initial_content_len == -1) {
7045 err = got_error_from_errno("asprintf");
7049 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
7053 if (write(fd, initial_content, initial_content_len) == -1) {
7054 err = got_error_from_errno2("write", *tagmsg_path);
7058 err = get_editor(&editor);
7061 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7062 initial_content_len, 1);
7064 free(initial_content);
7068 if (fd != -1 && close(fd) == -1 && err == NULL)
7069 err = got_error_from_errno2("close", *tagmsg_path);
7071 /* Editor is done; we can now apply unveil(2) */
7073 err = apply_unveil(repo_path, 0, NULL);
7081 static const struct got_error *
7082 add_tag(struct got_repository *repo, const char *tagger,
7083 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
7085 const struct got_error *err = NULL;
7086 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7087 char *label = NULL, *commit_id_str = NULL;
7088 struct got_reference *ref = NULL;
7089 char *refname = NULL, *tagmsg = NULL;
7090 char *tagmsg_path = NULL, *tag_id_str = NULL;
7091 int preserve_tagmsg = 0;
7092 struct got_reflist_head refs;
7097 * Don't let the user create a tag name with a leading '-'.
7098 * While technically a valid reference name, this case is usually
7099 * an unintended typo.
7101 if (tag_name[0] == '-')
7102 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7104 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7108 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7109 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7113 err = got_object_id_str(&commit_id_str, commit_id);
7117 err = get_tag_refname(&refname, tag_name);
7120 if (strncmp("refs/tags/", tag_name, 10) == 0)
7123 err = got_ref_open(&ref, repo, refname, 0);
7125 err = got_error(GOT_ERR_TAG_EXISTS);
7127 } else if (err->code != GOT_ERR_NOT_REF)
7130 if (tagmsg_arg == NULL) {
7131 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7132 tag_name, got_repo_get_path(repo));
7134 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7135 tagmsg_path != NULL)
7136 preserve_tagmsg = 1;
7141 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7142 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7145 preserve_tagmsg = 1;
7149 err = got_ref_alloc(&ref, refname, tag_id);
7152 preserve_tagmsg = 1;
7156 err = got_ref_write(ref, repo);
7159 preserve_tagmsg = 1;
7163 err = got_object_id_str(&tag_id_str, tag_id);
7166 preserve_tagmsg = 1;
7169 printf("Created tag %s\n", tag_id_str);
7171 if (preserve_tagmsg) {
7172 fprintf(stderr, "%s: tag message preserved in %s\n",
7173 getprogname(), tagmsg_path);
7174 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7175 err = got_error_from_errno2("unlink", tagmsg_path);
7180 free(commit_id_str);
7184 got_ref_list_free(&refs);
7188 static const struct got_error *
7189 cmd_tag(int argc, char *argv[])
7191 const struct got_error *error = NULL;
7192 struct got_repository *repo = NULL;
7193 struct got_worktree *worktree = NULL;
7194 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7195 char *gitconfig_path = NULL, *tagger = NULL;
7196 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7197 int ch, do_list = 0;
7198 int *pack_fds = NULL;
7200 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7203 commit_id_arg = optarg;
7209 repo_path = realpath(optarg, NULL);
7210 if (repo_path == NULL)
7211 return got_error_from_errno2("realpath",
7213 got_path_strip_trailing_slashes(repo_path);
7228 if (commit_id_arg != NULL)
7230 "-c option can only be used when creating a tag");
7232 option_conflict('l', 'm');
7235 } else if (argc != 1)
7242 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7243 "sendfd unveil", NULL) == -1)
7246 cwd = getcwd(NULL, 0);
7248 error = got_error_from_errno("getcwd");
7252 error = got_repo_pack_fds_open(&pack_fds);
7256 if (repo_path == NULL) {
7257 error = got_worktree_open(&worktree, cwd);
7258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7264 strdup(got_worktree_get_repo_path(worktree));
7265 if (repo_path == NULL)
7266 error = got_error_from_errno("strdup");
7270 repo_path = strdup(cwd);
7271 if (repo_path == NULL) {
7272 error = got_error_from_errno("strdup");
7280 /* Release work tree lock. */
7281 got_worktree_close(worktree);
7284 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7289 /* Remove "cpath" promise. */
7290 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7294 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7297 error = list_tags(repo, tag_name);
7299 error = get_gitconfig_path(&gitconfig_path);
7302 error = got_repo_open(&repo, repo_path, gitconfig_path,
7307 error = get_author(&tagger, repo, worktree);
7311 /* Release work tree lock. */
7312 got_worktree_close(worktree);
7317 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7322 if (commit_id_arg == NULL) {
7323 struct got_reference *head_ref;
7324 struct got_object_id *commit_id;
7325 error = got_ref_open(&head_ref, repo,
7326 worktree ? got_worktree_get_head_ref_name(worktree)
7330 error = got_ref_resolve(&commit_id, repo, head_ref);
7331 got_ref_close(head_ref);
7334 error = got_object_id_str(&commit_id_str, commit_id);
7340 error = add_tag(repo, tagger, tag_name,
7341 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7345 const struct got_error *close_err = got_repo_close(repo);
7350 got_worktree_close(worktree);
7352 const struct got_error *pack_err =
7353 got_repo_pack_fds_close(pack_fds);
7359 free(gitconfig_path);
7360 free(commit_id_str);
7368 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7373 static const struct got_error *
7374 add_progress(void *arg, unsigned char status, const char *path)
7376 while (path[0] == '/')
7378 printf("%c %s\n", status, path);
7382 static const struct got_error *
7383 cmd_add(int argc, char *argv[])
7385 const struct got_error *error = NULL;
7386 struct got_repository *repo = NULL;
7387 struct got_worktree *worktree = NULL;
7389 struct got_pathlist_head paths;
7390 struct got_pathlist_entry *pe;
7391 int ch, can_recurse = 0, no_ignores = 0;
7392 int *pack_fds = NULL;
7396 while ((ch = getopt(argc, argv, "IR")) != -1) {
7414 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7421 cwd = getcwd(NULL, 0);
7423 error = got_error_from_errno("getcwd");
7427 error = got_repo_pack_fds_open(&pack_fds);
7431 error = got_worktree_open(&worktree, cwd);
7433 if (error->code == GOT_ERR_NOT_WORKTREE)
7434 error = wrap_not_worktree_error(error, "add", cwd);
7438 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7443 error = apply_unveil(got_repo_get_path(repo), 1,
7444 got_worktree_get_root_path(worktree));
7448 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7455 TAILQ_FOREACH(pe, &paths, entry) {
7456 if (asprintf(&ondisk_path, "%s/%s",
7457 got_worktree_get_root_path(worktree),
7459 error = got_error_from_errno("asprintf");
7462 if (lstat(ondisk_path, &sb) == -1) {
7463 if (errno == ENOENT) {
7467 error = got_error_from_errno2("lstat",
7473 if (S_ISDIR(sb.st_mode)) {
7474 error = got_error_msg(GOT_ERR_BAD_PATH,
7475 "adding directories requires -R option");
7481 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7482 NULL, repo, no_ignores);
7485 const struct got_error *close_err = got_repo_close(repo);
7490 got_worktree_close(worktree);
7492 const struct got_error *pack_err =
7493 got_repo_pack_fds_close(pack_fds);
7497 TAILQ_FOREACH(pe, &paths, entry)
7498 free((char *)pe->path);
7499 got_pathlist_free(&paths);
7507 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7508 "path ...\n", getprogname());
7512 static const struct got_error *
7513 print_remove_status(void *arg, unsigned char status,
7514 unsigned char staged_status, const char *path)
7516 while (path[0] == '/')
7518 if (status == GOT_STATUS_NONEXISTENT)
7520 if (status == staged_status && (status == GOT_STATUS_DELETE))
7521 status = GOT_STATUS_NO_CHANGE;
7522 printf("%c%c %s\n", status, staged_status, path);
7526 static const struct got_error *
7527 cmd_remove(int argc, char *argv[])
7529 const struct got_error *error = NULL;
7530 struct got_worktree *worktree = NULL;
7531 struct got_repository *repo = NULL;
7532 const char *status_codes = NULL;
7534 struct got_pathlist_head paths;
7535 struct got_pathlist_entry *pe;
7536 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7537 int ignore_missing_paths = 0;
7538 int *pack_fds = NULL;
7542 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7545 delete_local_mods = 1;
7546 ignore_missing_paths = 1;
7555 for (i = 0; i < strlen(optarg); i++) {
7556 switch (optarg[i]) {
7557 case GOT_STATUS_MODIFY:
7558 delete_local_mods = 1;
7560 case GOT_STATUS_MISSING:
7561 ignore_missing_paths = 1;
7564 errx(1, "invalid status code '%c'",
7568 status_codes = optarg;
7580 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7587 cwd = getcwd(NULL, 0);
7589 error = got_error_from_errno("getcwd");
7593 error = got_repo_pack_fds_open(&pack_fds);
7597 error = got_worktree_open(&worktree, cwd);
7599 if (error->code == GOT_ERR_NOT_WORKTREE)
7600 error = wrap_not_worktree_error(error, "remove", cwd);
7604 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7609 error = apply_unveil(got_repo_get_path(repo), 1,
7610 got_worktree_get_root_path(worktree));
7614 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7621 TAILQ_FOREACH(pe, &paths, entry) {
7622 if (asprintf(&ondisk_path, "%s/%s",
7623 got_worktree_get_root_path(worktree),
7625 error = got_error_from_errno("asprintf");
7628 if (lstat(ondisk_path, &sb) == -1) {
7629 if (errno == ENOENT) {
7633 error = got_error_from_errno2("lstat",
7639 if (S_ISDIR(sb.st_mode)) {
7640 error = got_error_msg(GOT_ERR_BAD_PATH,
7641 "removing directories requires -R option");
7647 error = got_worktree_schedule_delete(worktree, &paths,
7648 delete_local_mods, status_codes, print_remove_status, NULL,
7649 repo, keep_on_disk, ignore_missing_paths);
7652 const struct got_error *close_err = got_repo_close(repo);
7657 got_worktree_close(worktree);
7659 const struct got_error *pack_err =
7660 got_repo_pack_fds_close(pack_fds);
7664 TAILQ_FOREACH(pe, &paths, entry)
7665 free((char *)pe->path);
7666 got_pathlist_free(&paths);
7674 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7675 "[-R] [patchfile]\n", getprogname());
7679 static const struct got_error *
7680 patch_from_stdin(int *patchfd)
7682 const struct got_error *err = NULL;
7684 char *path, buf[BUFSIZ];
7685 sig_t sighup, sigint, sigquit;
7687 err = got_opentemp_named_fd(&path, patchfd,
7688 GOT_TMPDIR_STR "/got-patch");
7694 sighup = signal(SIGHUP, SIG_DFL);
7695 sigint = signal(SIGINT, SIG_DFL);
7696 sigquit = signal(SIGQUIT, SIG_DFL);
7699 r = read(0, buf, sizeof(buf));
7701 err = got_error_from_errno("read");
7706 if (write(*patchfd, buf, r) == -1) {
7707 err = got_error_from_errno("write");
7712 signal(SIGHUP, sighup);
7713 signal(SIGINT, sigint);
7714 signal(SIGQUIT, sigquit);
7716 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7717 err = got_error_from_errno("lseek");
7727 static const struct got_error *
7728 patch_progress(void *arg, const char *old, const char *new,
7729 unsigned char status, const struct got_error *error, long old_from,
7730 long old_lines, long new_from, long new_lines, long offset,
7731 const struct got_error *hunk_err)
7733 const char *path = new == NULL ? old : new;
7735 while (*path == '/')
7739 printf("%c %s\n", status, path);
7742 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7744 if (offset != 0 || hunk_err != NULL) {
7745 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7746 old_lines, new_from, new_lines);
7747 if (hunk_err != NULL)
7748 printf("%s\n", hunk_err->msg);
7750 printf("applied with offset %ld\n", offset);
7756 static const struct got_error *
7757 cmd_patch(int argc, char *argv[])
7759 const struct got_error *error = NULL, *close_error = NULL;
7760 struct got_worktree *worktree = NULL;
7761 struct got_repository *repo = NULL;
7764 int ch, nop = 0, strip = -1, reverse = 0;
7766 int *pack_fds = NULL;
7768 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7774 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7776 errx(1, "pathname strip count is %s: %s",
7792 error = patch_from_stdin(&patchfd);
7795 } else if (argc == 1) {
7796 patchfd = open(argv[0], O_RDONLY);
7797 if (patchfd == -1) {
7798 error = got_error_from_errno2("open", argv[0]);
7804 if ((cwd = getcwd(NULL, 0)) == NULL) {
7805 error = got_error_from_errno("getcwd");
7809 error = got_repo_pack_fds_open(&pack_fds);
7813 error = got_worktree_open(&worktree, cwd);
7817 const char *repo_path = got_worktree_get_repo_path(worktree);
7818 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7822 error = apply_unveil(got_repo_get_path(repo), 0,
7823 got_worktree_get_root_path(worktree));
7828 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7833 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7834 &patch_progress, NULL, check_cancelled, NULL);
7838 close_error = got_repo_close(repo);
7840 error = close_error;
7842 if (worktree != NULL) {
7843 close_error = got_worktree_close(worktree);
7845 error = close_error;
7848 const struct got_error *pack_err =
7849 got_repo_pack_fds_close(pack_fds);
7860 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7861 "path ...\n", getprogname());
7865 static const struct got_error *
7866 revert_progress(void *arg, unsigned char status, const char *path)
7868 if (status == GOT_STATUS_UNVERSIONED)
7871 while (path[0] == '/')
7873 printf("%c %s\n", status, path);
7877 struct choose_patch_arg {
7878 FILE *patch_script_file;
7882 static const struct got_error *
7883 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7884 int nchanges, const char *action)
7886 const struct got_error *err;
7888 size_t linesize = 0;
7892 case GOT_STATUS_ADD:
7893 printf("A %s\n%s this addition? [y/n] ", path, action);
7895 case GOT_STATUS_DELETE:
7896 printf("D %s\n%s this deletion? [y/n] ", path, action);
7898 case GOT_STATUS_MODIFY:
7899 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7900 return got_error_from_errno("fseek");
7901 printf(GOT_COMMIT_SEP_STR);
7902 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7904 if (linelen == -1 && ferror(patch_file)) {
7905 err = got_error_from_errno("getline");
7910 printf(GOT_COMMIT_SEP_STR);
7911 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7912 path, n, nchanges, action);
7915 return got_error_path(path, GOT_ERR_FILE_STATUS);
7921 static const struct got_error *
7922 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7923 FILE *patch_file, int n, int nchanges)
7925 const struct got_error *err = NULL;
7927 size_t linesize = 0;
7930 struct choose_patch_arg *a = arg;
7932 *choice = GOT_PATCH_CHOICE_NONE;
7934 if (a->patch_script_file) {
7936 err = show_change(status, path, patch_file, n, nchanges,
7940 linelen = getline(&line, &linesize, a->patch_script_file);
7941 if (linelen == -1) {
7942 if (ferror(a->patch_script_file))
7943 return got_error_from_errno("getline");
7946 nl = strchr(line, '\n');
7949 if (strcmp(line, "y") == 0) {
7950 *choice = GOT_PATCH_CHOICE_YES;
7952 } else if (strcmp(line, "n") == 0) {
7953 *choice = GOT_PATCH_CHOICE_NO;
7955 } else if (strcmp(line, "q") == 0 &&
7956 status == GOT_STATUS_MODIFY) {
7957 *choice = GOT_PATCH_CHOICE_QUIT;
7960 printf("invalid response '%s'\n", line);
7965 while (resp != 'y' && resp != 'n' && resp != 'q') {
7966 err = show_change(status, path, patch_file, n, nchanges,
7973 if (status == GOT_STATUS_MODIFY) {
7974 if (resp != 'y' && resp != 'n' && resp != 'q') {
7975 printf("invalid response '%c'\n", resp);
7978 } else if (resp != 'y' && resp != 'n') {
7979 printf("invalid response '%c'\n", resp);
7985 *choice = GOT_PATCH_CHOICE_YES;
7986 else if (resp == 'n')
7987 *choice = GOT_PATCH_CHOICE_NO;
7988 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7989 *choice = GOT_PATCH_CHOICE_QUIT;
7994 static const struct got_error *
7995 cmd_revert(int argc, char *argv[])
7997 const struct got_error *error = NULL;
7998 struct got_worktree *worktree = NULL;
7999 struct got_repository *repo = NULL;
8000 char *cwd = NULL, *path = NULL;
8001 struct got_pathlist_head paths;
8002 struct got_pathlist_entry *pe;
8003 int ch, can_recurse = 0, pflag = 0;
8004 FILE *patch_script_file = NULL;
8005 const char *patch_script_path = NULL;
8006 struct choose_patch_arg cpa;
8007 int *pack_fds = NULL;
8011 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
8017 patch_script_path = optarg;
8032 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8033 "unveil", NULL) == -1)
8038 if (patch_script_path && !pflag)
8039 errx(1, "-F option can only be used together with -p option");
8041 cwd = getcwd(NULL, 0);
8043 error = got_error_from_errno("getcwd");
8047 error = got_repo_pack_fds_open(&pack_fds);
8051 error = got_worktree_open(&worktree, cwd);
8053 if (error->code == GOT_ERR_NOT_WORKTREE)
8054 error = wrap_not_worktree_error(error, "revert", cwd);
8058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8063 if (patch_script_path) {
8064 patch_script_file = fopen(patch_script_path, "re");
8065 if (patch_script_file == NULL) {
8066 error = got_error_from_errno2("fopen",
8071 error = apply_unveil(got_repo_get_path(repo), 1,
8072 got_worktree_get_root_path(worktree));
8076 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8083 TAILQ_FOREACH(pe, &paths, entry) {
8084 if (asprintf(&ondisk_path, "%s/%s",
8085 got_worktree_get_root_path(worktree),
8087 error = got_error_from_errno("asprintf");
8090 if (lstat(ondisk_path, &sb) == -1) {
8091 if (errno == ENOENT) {
8095 error = got_error_from_errno2("lstat",
8101 if (S_ISDIR(sb.st_mode)) {
8102 error = got_error_msg(GOT_ERR_BAD_PATH,
8103 "reverting directories requires -R option");
8109 cpa.patch_script_file = patch_script_file;
8110 cpa.action = "revert";
8111 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8112 pflag ? choose_patch : NULL, &cpa, repo);
8114 if (patch_script_file && fclose(patch_script_file) == EOF &&
8116 error = got_error_from_errno2("fclose", patch_script_path);
8118 const struct got_error *close_err = got_repo_close(repo);
8123 got_worktree_close(worktree);
8125 const struct got_error *pack_err =
8126 got_repo_pack_fds_close(pack_fds);
8138 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8139 "[path ...]\n", getprogname());
8143 struct collect_commit_logmsg_arg {
8144 const char *cmdline_log;
8145 const char *prepared_log;
8146 int non_interactive;
8148 const char *worktree_path;
8149 const char *branch_name;
8150 const char *repo_path;
8155 static const struct got_error *
8156 read_prepared_logmsg(char **logmsg, const char *path)
8158 const struct got_error *err = NULL;
8164 memset(&sb, 0, sizeof(sb));
8166 f = fopen(path, "re");
8168 return got_error_from_errno2("fopen", path);
8170 if (fstat(fileno(f), &sb) == -1) {
8171 err = got_error_from_errno2("fstat", path);
8174 if (sb.st_size == 0) {
8175 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8179 *logmsg = malloc(sb.st_size + 1);
8180 if (*logmsg == NULL) {
8181 err = got_error_from_errno("malloc");
8185 r = fread(*logmsg, 1, sb.st_size, f);
8186 if (r != sb.st_size) {
8188 err = got_error_from_errno2("fread", path);
8190 err = got_error(GOT_ERR_IO);
8193 (*logmsg)[sb.st_size] = '\0';
8195 if (fclose(f) == EOF && err == NULL)
8196 err = got_error_from_errno2("fclose", path);
8205 static const struct got_error *
8206 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8209 char *initial_content = NULL;
8210 struct got_pathlist_entry *pe;
8211 const struct got_error *err = NULL;
8212 char *template = NULL;
8213 struct collect_commit_logmsg_arg *a = arg;
8214 int initial_content_len;
8218 /* if a message was specified on the command line, just use it */
8219 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8220 len = strlen(a->cmdline_log) + 1;
8221 *logmsg = malloc(len + 1);
8222 if (*logmsg == NULL)
8223 return got_error_from_errno("malloc");
8224 strlcpy(*logmsg, a->cmdline_log, len);
8226 } else if (a->prepared_log != NULL && a->non_interactive)
8227 return read_prepared_logmsg(logmsg, a->prepared_log);
8229 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8230 return got_error_from_errno("asprintf");
8232 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8236 if (a->prepared_log) {
8238 err = read_prepared_logmsg(&msg, a->prepared_log);
8241 if (write(fd, msg, strlen(msg)) == -1) {
8242 err = got_error_from_errno2("write", a->logmsg_path);
8249 initial_content_len = asprintf(&initial_content,
8250 "\n# changes to be committed on branch %s:\n",
8252 if (initial_content_len == -1) {
8253 err = got_error_from_errno("asprintf");
8257 if (write(fd, initial_content, initial_content_len) == -1) {
8258 err = got_error_from_errno2("write", a->logmsg_path);
8262 TAILQ_FOREACH(pe, commitable_paths, entry) {
8263 struct got_commitable *ct = pe->data;
8264 dprintf(fd, "# %c %s\n",
8265 got_commitable_get_status(ct),
8266 got_commitable_get_path(ct));
8269 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8270 initial_content_len, a->prepared_log ? 0 : 1);
8272 free(initial_content);
8275 if (fd != -1 && close(fd) == -1 && err == NULL)
8276 err = got_error_from_errno2("close", a->logmsg_path);
8278 /* Editor is done; we can now apply unveil(2) */
8280 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8288 static const struct got_error *
8289 cmd_commit(int argc, char *argv[])
8291 const struct got_error *error = NULL;
8292 struct got_worktree *worktree = NULL;
8293 struct got_repository *repo = NULL;
8294 char *cwd = NULL, *id_str = NULL;
8295 struct got_object_id *id = NULL;
8296 const char *logmsg = NULL;
8297 char *prepared_logmsg = NULL;
8298 struct collect_commit_logmsg_arg cl_arg;
8299 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8300 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8301 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8302 struct got_pathlist_head paths;
8303 int *pack_fds = NULL;
8306 cl_arg.logmsg_path = NULL;
8308 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8312 option_conflict('F', 'm');
8313 prepared_logmsg = realpath(optarg, NULL);
8314 if (prepared_logmsg == NULL)
8315 return got_error_from_errno2("realpath",
8319 if (prepared_logmsg)
8320 option_conflict('m', 'F');
8324 non_interactive = 1;
8327 allow_bad_symlinks = 1;
8339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8340 "unveil", NULL) == -1)
8343 cwd = getcwd(NULL, 0);
8345 error = got_error_from_errno("getcwd");
8349 error = got_repo_pack_fds_open(&pack_fds);
8353 error = got_worktree_open(&worktree, cwd);
8355 if (error->code == GOT_ERR_NOT_WORKTREE)
8356 error = wrap_not_worktree_error(error, "commit", cwd);
8360 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8363 if (rebase_in_progress) {
8364 error = got_error(GOT_ERR_REBASING);
8368 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8373 error = get_gitconfig_path(&gitconfig_path);
8376 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8377 gitconfig_path, pack_fds);
8381 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8384 if (merge_in_progress) {
8385 error = got_error(GOT_ERR_MERGE_BUSY);
8389 error = get_author(&author, repo, worktree);
8394 * unveil(2) traverses exec(2); if an editor is used we have
8395 * to apply unveil after the log message has been written.
8397 if (logmsg == NULL || strlen(logmsg) == 0)
8398 error = get_editor(&editor);
8400 error = apply_unveil(got_repo_get_path(repo), 0,
8401 got_worktree_get_root_path(worktree));
8405 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8409 cl_arg.editor = editor;
8410 cl_arg.cmdline_log = logmsg;
8411 cl_arg.prepared_log = prepared_logmsg;
8412 cl_arg.non_interactive = non_interactive;
8413 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8414 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8415 if (!histedit_in_progress) {
8416 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8417 error = got_error(GOT_ERR_COMMIT_BRANCH);
8420 cl_arg.branch_name += 11;
8422 cl_arg.repo_path = got_repo_get_path(repo);
8423 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8424 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8425 print_status, NULL, repo);
8427 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8428 cl_arg.logmsg_path != NULL)
8429 preserve_logmsg = 1;
8433 error = got_object_id_str(&id_str, id);
8436 printf("Created commit %s\n", id_str);
8438 if (preserve_logmsg) {
8439 fprintf(stderr, "%s: log message preserved in %s\n",
8440 getprogname(), cl_arg.logmsg_path);
8441 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8443 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8444 free(cl_arg.logmsg_path);
8446 const struct got_error *close_err = got_repo_close(repo);
8451 got_worktree_close(worktree);
8453 const struct got_error *pack_err =
8454 got_repo_pack_fds_close(pack_fds);
8460 free(gitconfig_path);
8463 free(prepared_logmsg);
8470 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8471 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8472 "[remote-repository]\n", getprogname());
8477 print_load_info(int print_colored, int print_found, int print_trees,
8478 int ncolored, int nfound, int ntrees)
8480 if (print_colored) {
8481 printf("%d commit%s colored", ncolored,
8482 ncolored == 1 ? "" : "s");
8485 printf("%s%d object%s found",
8486 ncolored > 0 ? "; " : "",
8487 nfound, nfound == 1 ? "" : "s");
8490 printf("; %d tree%s scanned", ntrees,
8491 ntrees == 1 ? "" : "s");
8495 struct got_send_progress_arg {
8496 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8503 int last_nobj_total;
8507 int printed_something;
8509 struct got_pathlist_head *delete_branches;
8512 static const struct got_error *
8513 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8514 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8515 int nobj_written, off_t bytes_sent, const char *refname, int success)
8517 struct got_send_progress_arg *a = arg;
8518 char scaled_packsize[FMT_SCALED_STRSIZE];
8519 char scaled_sent[FMT_SCALED_STRSIZE];
8520 int p_deltify = 0, p_written = 0, p_sent = 0;
8521 int print_colored = 0, print_found = 0, print_trees = 0;
8522 int print_searching = 0, print_total = 0;
8523 int print_deltify = 0, print_written = 0, print_sent = 0;
8525 if (a->verbosity < 0)
8529 const char *status = success ? "accepted" : "rejected";
8532 struct got_pathlist_entry *pe;
8533 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8534 const char *branchname = pe->path;
8535 if (got_path_cmp(branchname, refname,
8536 strlen(branchname), strlen(refname)) == 0) {
8538 a->sent_something = 1;
8544 if (a->printed_something)
8546 printf("Server has %s %s", status, refname);
8547 a->printed_something = 1;
8551 if (a->last_ncolored != ncolored) {
8553 a->last_ncolored = ncolored;
8556 if (a->last_nfound != nfound) {
8559 a->last_nfound = nfound;
8562 if (a->last_ntrees != ntrees) {
8566 a->last_ntrees = ntrees;
8569 if ((print_colored || print_found || print_trees) &&
8572 print_load_info(print_colored, print_found, print_trees,
8573 ncolored, nfound, ntrees);
8574 a->printed_something = 1;
8577 } else if (!a->loading_done) {
8579 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8581 a->loading_done = 1;
8584 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8585 return got_error_from_errno("fmt_scaled");
8586 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8587 return got_error_from_errno("fmt_scaled");
8589 if (a->last_ncommits != ncommits) {
8590 print_searching = 1;
8591 a->last_ncommits = ncommits;
8594 if (a->last_nobj_total != nobj_total) {
8595 print_searching = 1;
8597 a->last_nobj_total = nobj_total;
8600 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8601 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8602 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8603 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8604 return got_error(GOT_ERR_NO_SPACE);
8607 if (nobj_deltify > 0 || nobj_written > 0) {
8608 if (nobj_deltify > 0) {
8609 p_deltify = (nobj_deltify * 100) / nobj_total;
8610 if (p_deltify != a->last_p_deltify) {
8611 a->last_p_deltify = p_deltify;
8612 print_searching = 1;
8617 if (nobj_written > 0) {
8618 p_written = (nobj_written * 100) / nobj_total;
8619 if (p_written != a->last_p_written) {
8620 a->last_p_written = p_written;
8621 print_searching = 1;
8629 if (bytes_sent > 0) {
8630 p_sent = (bytes_sent * 100) / packfile_size;
8631 if (p_sent != a->last_p_sent) {
8632 a->last_p_sent = p_sent;
8633 print_searching = 1;
8639 a->sent_something = 1;
8642 if (print_searching || print_total || print_deltify || print_written ||
8645 if (print_searching)
8646 printf("packing %d reference%s", ncommits,
8647 ncommits == 1 ? "" : "s");
8649 printf("; %d object%s", nobj_total,
8650 nobj_total == 1 ? "" : "s");
8652 printf("; deltify: %d%%", p_deltify);
8654 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8655 scaled_packsize, p_sent);
8656 else if (print_written)
8657 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8658 scaled_packsize, p_written);
8659 if (print_searching || print_total || print_deltify ||
8660 print_written || print_sent) {
8661 a->printed_something = 1;
8667 static const struct got_error *
8668 cmd_send(int argc, char *argv[])
8670 const struct got_error *error = NULL;
8671 char *cwd = NULL, *repo_path = NULL;
8672 const char *remote_name;
8673 char *proto = NULL, *host = NULL, *port = NULL;
8674 char *repo_name = NULL, *server_path = NULL;
8675 const struct got_remote_repo *remotes, *remote = NULL;
8676 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8677 struct got_repository *repo = NULL;
8678 struct got_worktree *worktree = NULL;
8679 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8680 struct got_pathlist_head branches;
8681 struct got_pathlist_head tags;
8682 struct got_reflist_head all_branches;
8683 struct got_reflist_head all_tags;
8684 struct got_pathlist_head delete_args;
8685 struct got_pathlist_head delete_branches;
8686 struct got_reflist_entry *re;
8687 struct got_pathlist_entry *pe;
8688 int i, ch, sendfd = -1, sendstatus;
8690 struct got_send_progress_arg spa;
8691 int verbosity = 0, overwrite_refs = 0;
8692 int send_all_branches = 0, send_all_tags = 0;
8693 struct got_reference *ref = NULL;
8694 int *pack_fds = NULL;
8696 TAILQ_INIT(&branches);
8698 TAILQ_INIT(&all_branches);
8699 TAILQ_INIT(&all_tags);
8700 TAILQ_INIT(&delete_args);
8701 TAILQ_INIT(&delete_branches);
8703 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8706 send_all_branches = 1;
8709 error = got_pathlist_append(&branches, optarg, NULL);
8715 error = got_pathlist_append(&delete_args, optarg, NULL);
8723 repo_path = realpath(optarg, NULL);
8724 if (repo_path == NULL)
8725 return got_error_from_errno2("realpath",
8727 got_path_strip_trailing_slashes(repo_path);
8730 error = got_pathlist_append(&tags, optarg, NULL);
8741 else if (verbosity < 3)
8755 if (send_all_branches && !TAILQ_EMPTY(&branches))
8756 option_conflict('a', 'b');
8757 if (send_all_tags && !TAILQ_EMPTY(&tags))
8758 option_conflict('T', 't');
8762 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8764 remote_name = argv[0];
8768 cwd = getcwd(NULL, 0);
8770 error = got_error_from_errno("getcwd");
8774 error = got_repo_pack_fds_open(&pack_fds);
8778 if (repo_path == NULL) {
8779 error = got_worktree_open(&worktree, cwd);
8780 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8786 strdup(got_worktree_get_repo_path(worktree));
8787 if (repo_path == NULL)
8788 error = got_error_from_errno("strdup");
8792 repo_path = strdup(cwd);
8793 if (repo_path == NULL) {
8794 error = got_error_from_errno("strdup");
8800 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8805 worktree_conf = got_worktree_get_gotconfig(worktree);
8806 if (worktree_conf) {
8807 got_gotconfig_get_remotes(&nremotes, &remotes,
8809 for (i = 0; i < nremotes; i++) {
8810 if (strcmp(remotes[i].name, remote_name) == 0) {
8811 remote = &remotes[i];
8817 if (remote == NULL) {
8818 repo_conf = got_repo_get_gotconfig(repo);
8820 got_gotconfig_get_remotes(&nremotes, &remotes,
8822 for (i = 0; i < nremotes; i++) {
8823 if (strcmp(remotes[i].name, remote_name) == 0) {
8824 remote = &remotes[i];
8830 if (remote == NULL) {
8831 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8832 for (i = 0; i < nremotes; i++) {
8833 if (strcmp(remotes[i].name, remote_name) == 0) {
8834 remote = &remotes[i];
8839 if (remote == NULL) {
8840 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8844 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8845 &repo_name, remote->send_url);
8849 if (strcmp(proto, "git") == 0) {
8851 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8852 "sendfd dns inet unveil", NULL) == -1)
8855 } else if (strcmp(proto, "git+ssh") == 0 ||
8856 strcmp(proto, "ssh") == 0) {
8858 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8859 "sendfd unveil", NULL) == -1)
8862 } else if (strcmp(proto, "http") == 0 ||
8863 strcmp(proto, "git+http") == 0) {
8864 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8867 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8871 error = got_dial_apply_unveil(proto);
8875 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8879 if (send_all_branches) {
8880 error = got_ref_list(&all_branches, repo, "refs/heads",
8881 got_ref_cmp_by_name, NULL);
8884 TAILQ_FOREACH(re, &all_branches, entry) {
8885 const char *branchname = got_ref_get_name(re->ref);
8886 error = got_pathlist_append(&branches,
8892 } else if (nbranches == 0) {
8893 for (i = 0; i < remote->nsend_branches; i++) {
8894 got_pathlist_append(&branches,
8895 remote->send_branches[i], NULL);
8899 if (send_all_tags) {
8900 error = got_ref_list(&all_tags, repo, "refs/tags",
8901 got_ref_cmp_by_name, NULL);
8904 TAILQ_FOREACH(re, &all_tags, entry) {
8905 const char *tagname = got_ref_get_name(re->ref);
8906 error = got_pathlist_append(&tags,
8915 * To prevent accidents only branches in refs/heads/ can be deleted
8916 * with 'got send -d'.
8917 * Deleting anything else requires local repository access or Git.
8919 TAILQ_FOREACH(pe, &delete_args, entry) {
8920 const char *branchname = pe->path;
8922 struct got_pathlist_entry *new;
8923 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8924 s = strdup(branchname);
8926 error = got_error_from_errno("strdup");
8930 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8931 error = got_error_from_errno("asprintf");
8935 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8936 if (error || new == NULL /* duplicate */)
8943 if (nbranches == 0 && ndelete_branches == 0) {
8944 struct got_reference *head_ref;
8946 error = got_ref_open(&head_ref, repo,
8947 got_worktree_get_head_ref_name(worktree), 0);
8949 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8952 if (got_ref_is_symbolic(head_ref)) {
8953 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8954 got_ref_close(head_ref);
8959 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8967 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8968 port ? ":" : "", port ? port : "");
8970 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8971 server_path, verbosity);
8975 memset(&spa, 0, sizeof(spa));
8976 spa.last_scaled_packsize[0] = '\0';
8977 spa.last_p_deltify = -1;
8978 spa.last_p_written = -1;
8979 spa.verbosity = verbosity;
8980 spa.delete_branches = &delete_branches;
8981 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8982 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8983 check_cancelled, NULL);
8984 if (spa.printed_something)
8988 if (!spa.sent_something && verbosity >= 0)
8989 printf("Already up-to-date\n");
8992 if (kill(sendpid, SIGTERM) == -1)
8993 error = got_error_from_errno("kill");
8994 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8995 error = got_error_from_errno("waitpid");
8997 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8998 error = got_error_from_errno("close");
9000 const struct got_error *close_err = got_repo_close(repo);
9005 got_worktree_close(worktree);
9007 const struct got_error *pack_err =
9008 got_repo_pack_fds_close(pack_fds);
9014 got_pathlist_free(&branches);
9015 got_pathlist_free(&tags);
9016 got_ref_list_free(&all_branches);
9017 got_ref_list_free(&all_tags);
9018 got_pathlist_free(&delete_args);
9019 TAILQ_FOREACH(pe, &delete_branches, entry)
9020 free((char *)pe->path);
9021 got_pathlist_free(&delete_branches);
9033 usage_cherrypick(void)
9035 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
9039 static const struct got_error *
9040 cmd_cherrypick(int argc, char *argv[])
9042 const struct got_error *error = NULL;
9043 struct got_worktree *worktree = NULL;
9044 struct got_repository *repo = NULL;
9045 char *cwd = NULL, *commit_id_str = NULL;
9046 struct got_object_id *commit_id = NULL;
9047 struct got_commit_object *commit = NULL;
9048 struct got_object_qid *pid;
9050 struct got_update_progress_arg upa;
9051 int *pack_fds = NULL;
9053 while ((ch = getopt(argc, argv, "")) != -1) {
9065 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9066 "unveil", NULL) == -1)
9072 cwd = getcwd(NULL, 0);
9074 error = got_error_from_errno("getcwd");
9078 error = got_repo_pack_fds_open(&pack_fds);
9082 error = got_worktree_open(&worktree, cwd);
9084 if (error->code == GOT_ERR_NOT_WORKTREE)
9085 error = wrap_not_worktree_error(error, "cherrypick",
9090 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9095 error = apply_unveil(got_repo_get_path(repo), 0,
9096 got_worktree_get_root_path(worktree));
9100 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9101 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9104 error = got_object_id_str(&commit_id_str, commit_id);
9108 error = got_object_open_as_commit(&commit, repo, commit_id);
9111 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9112 memset(&upa, 0, sizeof(upa));
9113 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9114 commit_id, repo, update_progress, &upa, check_cancelled,
9119 if (upa.did_something)
9120 printf("Merged commit %s\n", commit_id_str);
9121 print_merge_progress_stats(&upa);
9124 got_object_commit_close(commit);
9125 free(commit_id_str);
9127 got_worktree_close(worktree);
9129 const struct got_error *close_err = got_repo_close(repo);
9134 const struct got_error *pack_err =
9135 got_repo_pack_fds_close(pack_fds);
9146 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9150 static const struct got_error *
9151 cmd_backout(int argc, char *argv[])
9153 const struct got_error *error = NULL;
9154 struct got_worktree *worktree = NULL;
9155 struct got_repository *repo = NULL;
9156 char *cwd = NULL, *commit_id_str = NULL;
9157 struct got_object_id *commit_id = NULL;
9158 struct got_commit_object *commit = NULL;
9159 struct got_object_qid *pid;
9161 struct got_update_progress_arg upa;
9162 int *pack_fds = NULL;
9164 while ((ch = getopt(argc, argv, "")) != -1) {
9176 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9177 "unveil", NULL) == -1)
9183 cwd = getcwd(NULL, 0);
9185 error = got_error_from_errno("getcwd");
9189 error = got_repo_pack_fds_open(&pack_fds);
9193 error = got_worktree_open(&worktree, cwd);
9195 if (error->code == GOT_ERR_NOT_WORKTREE)
9196 error = wrap_not_worktree_error(error, "backout", cwd);
9200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9205 error = apply_unveil(got_repo_get_path(repo), 0,
9206 got_worktree_get_root_path(worktree));
9210 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9211 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9214 error = got_object_id_str(&commit_id_str, commit_id);
9218 error = got_object_open_as_commit(&commit, repo, commit_id);
9221 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9223 error = got_error(GOT_ERR_ROOT_COMMIT);
9227 memset(&upa, 0, sizeof(upa));
9228 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9229 repo, update_progress, &upa, check_cancelled, NULL);
9233 if (upa.did_something)
9234 printf("Backed out commit %s\n", commit_id_str);
9235 print_merge_progress_stats(&upa);
9238 got_object_commit_close(commit);
9239 free(commit_id_str);
9241 got_worktree_close(worktree);
9243 const struct got_error *close_err = got_repo_close(repo);
9248 const struct got_error *pack_err =
9249 got_repo_pack_fds_close(pack_fds);
9259 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9265 trim_logmsg(char *logmsg, int limit)
9270 len = strlen(logmsg);
9274 nl = strchr(logmsg, '\n');
9279 static const struct got_error *
9280 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9282 const struct got_error *err;
9283 char *logmsg0 = NULL;
9286 err = got_object_commit_get_logmsg(&logmsg0, commit);
9291 while (isspace((unsigned char)s[0]))
9294 *logmsg = strdup(s);
9295 if (*logmsg == NULL) {
9296 err = got_error_from_errno("strdup");
9300 trim_logmsg(*logmsg, limit);
9306 static const struct got_error *
9307 show_rebase_merge_conflict(struct got_object_id *id,
9308 struct got_repository *repo)
9310 const struct got_error *err;
9311 struct got_commit_object *commit = NULL;
9312 char *id_str = NULL, *logmsg = NULL;
9314 err = got_object_open_as_commit(&commit, repo, id);
9318 err = got_object_id_str(&id_str, id);
9324 err = get_short_logmsg(&logmsg, 42, commit);
9328 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9331 got_object_commit_close(commit);
9336 static const struct got_error *
9337 show_rebase_progress(struct got_commit_object *commit,
9338 struct got_object_id *old_id, struct got_object_id *new_id)
9340 const struct got_error *err;
9341 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9343 err = got_object_id_str(&old_id_str, old_id);
9348 err = got_object_id_str(&new_id_str, new_id);
9353 old_id_str[12] = '\0';
9355 new_id_str[12] = '\0';
9357 err = get_short_logmsg(&logmsg, 42, commit);
9361 printf("%s -> %s: %s\n", old_id_str,
9362 new_id_str ? new_id_str : "no-op change", logmsg);
9370 static const struct got_error *
9371 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9372 struct got_reference *branch, struct got_reference *new_base_branch,
9373 struct got_reference *tmp_branch, struct got_repository *repo,
9376 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9377 return got_worktree_rebase_complete(worktree, fileindex,
9378 new_base_branch, tmp_branch, branch, repo, create_backup);
9381 static const struct got_error *
9382 rebase_commit(struct got_pathlist_head *merged_paths,
9383 struct got_worktree *worktree, struct got_fileindex *fileindex,
9384 struct got_reference *tmp_branch,
9385 struct got_object_id *commit_id, struct got_repository *repo)
9387 const struct got_error *error;
9388 struct got_commit_object *commit;
9389 struct got_object_id *new_commit_id;
9391 error = got_object_open_as_commit(&commit, repo, commit_id);
9395 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9396 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9398 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9400 error = show_rebase_progress(commit, commit_id, NULL);
9402 error = show_rebase_progress(commit, commit_id, new_commit_id);
9403 free(new_commit_id);
9406 got_object_commit_close(commit);
9410 struct check_path_prefix_arg {
9411 const char *path_prefix;
9416 static const struct got_error *
9417 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9418 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9419 struct got_object_id *id1, struct got_object_id *id2,
9420 const char *path1, const char *path2,
9421 mode_t mode1, mode_t mode2, struct got_repository *repo)
9423 struct check_path_prefix_arg *a = arg;
9425 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9426 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9427 return got_error(a->errcode);
9432 static const struct got_error *
9433 check_path_prefix(struct got_object_id *parent_id,
9434 struct got_object_id *commit_id, const char *path_prefix,
9435 int errcode, struct got_repository *repo)
9437 const struct got_error *err;
9438 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9439 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9440 struct check_path_prefix_arg cpp_arg;
9442 if (got_path_is_root_dir(path_prefix))
9445 err = got_object_open_as_commit(&commit, repo, commit_id);
9449 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9453 err = got_object_open_as_tree(&tree1, repo,
9454 got_object_commit_get_tree_id(parent_commit));
9458 err = got_object_open_as_tree(&tree2, repo,
9459 got_object_commit_get_tree_id(commit));
9463 cpp_arg.path_prefix = path_prefix;
9464 while (cpp_arg.path_prefix[0] == '/')
9465 cpp_arg.path_prefix++;
9466 cpp_arg.len = strlen(cpp_arg.path_prefix);
9467 cpp_arg.errcode = errcode;
9468 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
9469 check_path_prefix_in_diff, &cpp_arg, 0);
9472 got_object_tree_close(tree1);
9474 got_object_tree_close(tree2);
9476 got_object_commit_close(commit);
9478 got_object_commit_close(parent_commit);
9482 static const struct got_error *
9483 collect_commits(struct got_object_id_queue *commits,
9484 struct got_object_id *initial_commit_id,
9485 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9486 const char *path_prefix, int path_prefix_errcode,
9487 struct got_repository *repo)
9489 const struct got_error *err = NULL;
9490 struct got_commit_graph *graph = NULL;
9491 struct got_object_id *parent_id = NULL;
9492 struct got_object_qid *qid;
9493 struct got_object_id *commit_id = initial_commit_id;
9495 err = got_commit_graph_open(&graph, "/", 1);
9499 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9500 check_cancelled, NULL);
9503 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9504 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9505 check_cancelled, NULL);
9507 if (err->code == GOT_ERR_ITER_COMPLETED) {
9508 err = got_error_msg(GOT_ERR_ANCESTRY,
9509 "ran out of commits to rebase before "
9510 "youngest common ancestor commit has "
9515 err = check_path_prefix(parent_id, commit_id,
9516 path_prefix, path_prefix_errcode, repo);
9520 err = got_object_qid_alloc(&qid, commit_id);
9523 STAILQ_INSERT_HEAD(commits, qid, entry);
9524 commit_id = parent_id;
9528 got_commit_graph_close(graph);
9532 static const struct got_error *
9533 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9535 const struct got_error *err = NULL;
9536 time_t committer_time;
9538 char datebuf[11]; /* YYYY-MM-DD + NUL */
9539 char *author0 = NULL, *author, *smallerthan;
9540 char *logmsg0 = NULL, *logmsg, *newline;
9542 committer_time = got_object_commit_get_committer_time(commit);
9543 if (gmtime_r(&committer_time, &tm) == NULL)
9544 return got_error_from_errno("gmtime_r");
9545 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9546 return got_error(GOT_ERR_NO_SPACE);
9548 author0 = strdup(got_object_commit_get_author(commit));
9549 if (author0 == NULL)
9550 return got_error_from_errno("strdup");
9552 smallerthan = strchr(author, '<');
9553 if (smallerthan && smallerthan[1] != '\0')
9554 author = smallerthan + 1;
9555 author[strcspn(author, "@>")] = '\0';
9557 err = got_object_commit_get_logmsg(&logmsg0, commit);
9561 while (*logmsg == '\n')
9563 newline = strchr(logmsg, '\n');
9567 if (asprintf(brief_str, "%s %s %s",
9568 datebuf, author, logmsg) == -1)
9569 err = got_error_from_errno("asprintf");
9576 static const struct got_error *
9577 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9578 struct got_repository *repo)
9580 const struct got_error *err;
9583 err = got_object_id_str(&id_str, id);
9587 err = got_ref_delete(ref, repo);
9591 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9597 static const struct got_error *
9598 print_backup_ref(const char *branch_name, const char *new_id_str,
9599 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9600 struct got_reflist_object_id_map *refs_idmap,
9601 struct got_repository *repo)
9603 const struct got_error *err = NULL;
9604 struct got_reflist_head *refs;
9605 char *refs_str = NULL;
9606 struct got_object_id *new_commit_id = NULL;
9607 struct got_commit_object *new_commit = NULL;
9608 char *new_commit_brief_str = NULL;
9609 struct got_object_id *yca_id = NULL;
9610 struct got_commit_object *yca_commit = NULL;
9611 char *yca_id_str = NULL, *yca_brief_str = NULL;
9612 char *custom_refs_str;
9614 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9615 return got_error_from_errno("asprintf");
9617 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9618 0, 0, refs_idmap, custom_refs_str);
9622 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9626 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9628 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9633 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9637 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9641 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9642 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9646 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9647 refs_str ? " (" : "", refs_str ? refs_str : "",
9648 refs_str ? ")" : "", new_commit_brief_str);
9649 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9650 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9654 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9658 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9662 err = got_object_id_str(&yca_id_str, yca_id);
9666 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9668 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9672 printf("history forked at %s%s%s%s\n %s\n",
9674 refs_str ? " (" : "", refs_str ? refs_str : "",
9675 refs_str ? ")" : "", yca_brief_str);
9678 free(custom_refs_str);
9679 free(new_commit_id);
9683 free(yca_brief_str);
9685 got_object_commit_close(new_commit);
9687 got_object_commit_close(yca_commit);
9692 static const struct got_error *
9693 process_backup_refs(const char *backup_ref_prefix,
9694 const char *wanted_branch_name,
9695 int delete, struct got_repository *repo)
9697 const struct got_error *err;
9698 struct got_reflist_head refs, backup_refs;
9699 struct got_reflist_entry *re;
9700 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9701 struct got_object_id *old_commit_id = NULL;
9702 char *branch_name = NULL;
9703 struct got_commit_object *old_commit = NULL;
9704 struct got_reflist_object_id_map *refs_idmap = NULL;
9705 int wanted_branch_found = 0;
9708 TAILQ_INIT(&backup_refs);
9710 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9714 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9718 if (wanted_branch_name) {
9719 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9720 wanted_branch_name += 11;
9723 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9724 got_ref_cmp_by_commit_timestamp_descending, repo);
9728 TAILQ_FOREACH(re, &backup_refs, entry) {
9729 const char *refname = got_ref_get_name(re->ref);
9732 err = check_cancelled(NULL);
9736 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9740 err = got_object_open_as_commit(&old_commit, repo,
9745 if (strncmp(backup_ref_prefix, refname,
9746 backup_ref_prefix_len) == 0)
9747 refname += backup_ref_prefix_len;
9749 while (refname[0] == '/')
9752 branch_name = strdup(refname);
9753 if (branch_name == NULL) {
9754 err = got_error_from_errno("strdup");
9757 slash = strrchr(branch_name, '/');
9760 refname += strlen(branch_name) + 1;
9763 if (wanted_branch_name == NULL ||
9764 strcmp(wanted_branch_name, branch_name) == 0) {
9765 wanted_branch_found = 1;
9767 err = delete_backup_ref(re->ref,
9768 old_commit_id, repo);
9770 err = print_backup_ref(branch_name, refname,
9771 old_commit_id, old_commit, refs_idmap,
9778 free(old_commit_id);
9779 old_commit_id = NULL;
9782 got_object_commit_close(old_commit);
9786 if (wanted_branch_name && !wanted_branch_found) {
9787 err = got_error_fmt(GOT_ERR_NOT_REF,
9788 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9792 got_reflist_object_id_map_free(refs_idmap);
9793 got_ref_list_free(&refs);
9794 got_ref_list_free(&backup_refs);
9795 free(old_commit_id);
9798 got_object_commit_close(old_commit);
9802 static const struct got_error *
9803 abort_progress(void *arg, unsigned char status, const char *path)
9806 * Unversioned files should not clutter progress output when
9807 * an operation is aborted.
9809 if (status == GOT_STATUS_UNVERSIONED)
9812 return update_progress(arg, status, path);
9815 static const struct got_error *
9816 cmd_rebase(int argc, char *argv[])
9818 const struct got_error *error = NULL;
9819 struct got_worktree *worktree = NULL;
9820 struct got_repository *repo = NULL;
9821 struct got_fileindex *fileindex = NULL;
9823 struct got_reference *branch = NULL;
9824 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9825 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9826 struct got_object_id *resume_commit_id = NULL;
9827 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9828 struct got_commit_object *commit = NULL;
9829 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9830 int histedit_in_progress = 0, merge_in_progress = 0;
9831 int create_backup = 1, list_backups = 0, delete_backups = 0;
9832 struct got_object_id_queue commits;
9833 struct got_pathlist_head merged_paths;
9834 const struct got_object_id_queue *parent_ids;
9835 struct got_object_qid *qid, *pid;
9836 struct got_update_progress_arg upa;
9837 int *pack_fds = NULL;
9839 STAILQ_INIT(&commits);
9840 TAILQ_INIT(&merged_paths);
9841 memset(&upa, 0, sizeof(upa));
9843 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9849 continue_rebase = 1;
9867 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9868 "unveil", NULL) == -1)
9873 option_conflict('l', 'a');
9874 if (continue_rebase)
9875 option_conflict('l', 'c');
9877 option_conflict('l', 'X');
9878 if (argc != 0 && argc != 1)
9880 } else if (delete_backups) {
9882 option_conflict('X', 'a');
9883 if (continue_rebase)
9884 option_conflict('X', 'c');
9886 option_conflict('l', 'X');
9887 if (argc != 0 && argc != 1)
9890 if (abort_rebase && continue_rebase)
9892 else if (abort_rebase || continue_rebase) {
9895 } else if (argc != 1)
9899 cwd = getcwd(NULL, 0);
9901 error = got_error_from_errno("getcwd");
9905 error = got_repo_pack_fds_open(&pack_fds);
9909 error = got_worktree_open(&worktree, cwd);
9911 if (list_backups || delete_backups) {
9912 if (error->code != GOT_ERR_NOT_WORKTREE)
9915 if (error->code == GOT_ERR_NOT_WORKTREE)
9916 error = wrap_not_worktree_error(error,
9922 error = got_repo_open(&repo,
9923 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9928 error = apply_unveil(got_repo_get_path(repo), 0,
9929 worktree ? got_worktree_get_root_path(worktree) : NULL);
9933 if (list_backups || delete_backups) {
9934 error = process_backup_refs(
9935 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9936 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9937 goto done; /* nothing else to do */
9940 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9944 if (histedit_in_progress) {
9945 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9949 error = got_worktree_merge_in_progress(&merge_in_progress,
9953 if (merge_in_progress) {
9954 error = got_error(GOT_ERR_MERGE_BUSY);
9958 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9963 if (!rebase_in_progress) {
9964 error = got_error(GOT_ERR_NOT_REBASING);
9967 error = got_worktree_rebase_continue(&resume_commit_id,
9968 &new_base_branch, &tmp_branch, &branch, &fileindex,
9972 printf("Switching work tree to %s\n",
9973 got_ref_get_symref_target(new_base_branch));
9974 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9975 new_base_branch, abort_progress, &upa);
9978 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9979 print_merge_progress_stats(&upa);
9980 goto done; /* nothing else to do */
9983 if (continue_rebase) {
9984 if (!rebase_in_progress) {
9985 error = got_error(GOT_ERR_NOT_REBASING);
9988 error = got_worktree_rebase_continue(&resume_commit_id,
9989 &new_base_branch, &tmp_branch, &branch, &fileindex,
9994 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9995 resume_commit_id, repo);
9999 yca_id = got_object_id_dup(resume_commit_id);
10000 if (yca_id == NULL) {
10001 error = got_error_from_errno("got_object_id_dup");
10005 error = got_ref_open(&branch, repo, argv[0], 0);
10010 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
10014 if (!continue_rebase) {
10015 struct got_object_id *base_commit_id;
10017 base_commit_id = got_worktree_get_base_commit_id(worktree);
10018 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10019 base_commit_id, branch_head_commit_id, 1, repo,
10020 check_cancelled, NULL);
10023 if (yca_id == NULL) {
10024 error = got_error_msg(GOT_ERR_ANCESTRY,
10025 "specified branch shares no common ancestry "
10026 "with work tree's branch");
10030 error = check_same_branch(base_commit_id, branch, yca_id, repo);
10032 if (error->code != GOT_ERR_ANCESTRY)
10036 struct got_pathlist_head paths;
10037 printf("%s is already based on %s\n",
10038 got_ref_get_name(branch),
10039 got_worktree_get_head_ref_name(worktree));
10040 error = switch_head_ref(branch, branch_head_commit_id,
10044 error = got_worktree_set_base_commit_id(worktree, repo,
10045 branch_head_commit_id);
10048 TAILQ_INIT(&paths);
10049 error = got_pathlist_append(&paths, "", NULL);
10052 error = got_worktree_checkout_files(worktree,
10053 &paths, repo, update_progress, &upa,
10054 check_cancelled, NULL);
10055 got_pathlist_free(&paths);
10058 if (upa.did_something) {
10060 error = got_object_id_str(&id_str,
10061 branch_head_commit_id);
10064 printf("Updated to %s: %s\n",
10065 got_worktree_get_head_ref_name(worktree),
10069 printf("Already up-to-date\n");
10070 print_update_progress_stats(&upa);
10075 commit_id = branch_head_commit_id;
10076 error = got_object_open_as_commit(&commit, repo, commit_id);
10080 parent_ids = got_object_commit_get_parent_ids(commit);
10081 pid = STAILQ_FIRST(parent_ids);
10083 error = got_error(GOT_ERR_EMPTY_REBASE);
10086 error = collect_commits(&commits, commit_id, &pid->id,
10087 yca_id, got_worktree_get_path_prefix(worktree),
10088 GOT_ERR_REBASE_PATH, repo);
10089 got_object_commit_close(commit);
10094 if (!continue_rebase) {
10095 error = got_worktree_rebase_prepare(&new_base_branch,
10096 &tmp_branch, &fileindex, worktree, branch, repo);
10101 if (STAILQ_EMPTY(&commits)) {
10102 if (continue_rebase) {
10103 error = rebase_complete(worktree, fileindex,
10104 branch, new_base_branch, tmp_branch, repo,
10108 /* Fast-forward the reference of the branch. */
10109 struct got_object_id *new_head_commit_id;
10111 error = got_ref_resolve(&new_head_commit_id, repo,
10115 error = got_object_id_str(&id_str, new_head_commit_id);
10116 printf("Forwarding %s to commit %s\n",
10117 got_ref_get_name(branch), id_str);
10119 error = got_ref_change_ref(branch,
10120 new_head_commit_id);
10123 /* No backup needed since objects did not change. */
10129 STAILQ_FOREACH(qid, &commits, entry) {
10131 commit_id = &qid->id;
10132 parent_id = pid ? &pid->id : yca_id;
10135 memset(&upa, 0, sizeof(upa));
10136 error = got_worktree_rebase_merge_files(&merged_paths,
10137 worktree, fileindex, parent_id, commit_id, repo,
10138 update_progress, &upa, check_cancelled, NULL);
10142 print_merge_progress_stats(&upa);
10143 if (upa.conflicts > 0 || upa.missing > 0 ||
10144 upa.not_deleted > 0 || upa.unversioned > 0) {
10145 if (upa.conflicts > 0) {
10146 error = show_rebase_merge_conflict(&qid->id,
10151 got_worktree_rebase_pathlist_free(&merged_paths);
10155 error = rebase_commit(&merged_paths, worktree, fileindex,
10156 tmp_branch, commit_id, repo);
10157 got_worktree_rebase_pathlist_free(&merged_paths);
10162 if (upa.conflicts > 0 || upa.missing > 0 ||
10163 upa.not_deleted > 0 || upa.unversioned > 0) {
10164 error = got_worktree_rebase_postpone(worktree, fileindex);
10167 if (upa.conflicts > 0 && upa.missing == 0 &&
10168 upa.not_deleted == 0 && upa.unversioned == 0) {
10169 error = got_error_msg(GOT_ERR_CONFLICTS,
10170 "conflicts must be resolved before rebasing "
10172 } else if (upa.conflicts > 0) {
10173 error = got_error_msg(GOT_ERR_CONFLICTS,
10174 "conflicts must be resolved before rebasing "
10175 "can continue; changes destined for some "
10176 "files were not yet merged and should be "
10177 "merged manually if required before the "
10178 "rebase operation is continued");
10180 error = got_error_msg(GOT_ERR_CONFLICTS,
10181 "changes destined for some files were not "
10182 "yet merged and should be merged manually "
10183 "if required before the rebase operation "
10187 error = rebase_complete(worktree, fileindex, branch,
10188 new_base_branch, tmp_branch, repo, create_backup);
10190 got_object_id_queue_free(&commits);
10191 free(branch_head_commit_id);
10192 free(resume_commit_id);
10195 got_object_commit_close(commit);
10197 got_ref_close(branch);
10198 if (new_base_branch)
10199 got_ref_close(new_base_branch);
10201 got_ref_close(tmp_branch);
10203 got_worktree_close(worktree);
10205 const struct got_error *close_err = got_repo_close(repo);
10210 const struct got_error *pack_err =
10211 got_repo_pack_fds_close(pack_fds);
10219 usage_histedit(void)
10221 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10222 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10227 #define GOT_HISTEDIT_PICK 'p'
10228 #define GOT_HISTEDIT_EDIT 'e'
10229 #define GOT_HISTEDIT_FOLD 'f'
10230 #define GOT_HISTEDIT_DROP 'd'
10231 #define GOT_HISTEDIT_MESG 'm'
10233 static const struct got_histedit_cmd {
10234 unsigned char code;
10237 } got_histedit_cmds[] = {
10238 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10239 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10240 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10242 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10243 { GOT_HISTEDIT_MESG, "mesg",
10244 "single-line log message for commit above (open editor if empty)" },
10247 struct got_histedit_list_entry {
10248 TAILQ_ENTRY(got_histedit_list_entry) entry;
10249 struct got_object_id *commit_id;
10250 const struct got_histedit_cmd *cmd;
10253 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10255 static const struct got_error *
10256 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10257 FILE *f, struct got_repository *repo)
10259 const struct got_error *err = NULL;
10260 char *logmsg = NULL, *id_str = NULL;
10261 struct got_commit_object *commit = NULL;
10264 err = got_object_open_as_commit(&commit, repo, commit_id);
10268 err = get_short_logmsg(&logmsg, 34, commit);
10272 err = got_object_id_str(&id_str, commit_id);
10276 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10278 err = got_ferror(f, GOT_ERR_IO);
10281 got_object_commit_close(commit);
10287 static const struct got_error *
10288 histedit_write_commit_list(struct got_object_id_queue *commits,
10289 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10290 struct got_repository *repo)
10292 const struct got_error *err = NULL;
10293 struct got_object_qid *qid;
10294 const char *histedit_cmd = NULL;
10296 if (STAILQ_EMPTY(commits))
10297 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10299 STAILQ_FOREACH(qid, commits, entry) {
10300 histedit_cmd = got_histedit_cmds[0].name;
10302 histedit_cmd = "edit";
10303 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10304 histedit_cmd = "fold";
10305 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10308 if (edit_logmsg_only) {
10309 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10311 err = got_ferror(f, GOT_ERR_IO);
10320 static const struct got_error *
10321 write_cmd_list(FILE *f, const char *branch_name,
10322 struct got_object_id_queue *commits)
10324 const struct got_error *err = NULL;
10328 struct got_object_qid *qid;
10330 qid = STAILQ_FIRST(commits);
10331 err = got_object_id_str(&id_str, &qid->id);
10336 "# Editing the history of branch '%s' starting at\n"
10338 "# Commits will be processed in order from top to "
10339 "bottom of this file.\n", branch_name, id_str);
10341 err = got_ferror(f, GOT_ERR_IO);
10345 n = fprintf(f, "# Available histedit commands:\n");
10347 err = got_ferror(f, GOT_ERR_IO);
10351 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10352 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10353 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10356 err = got_ferror(f, GOT_ERR_IO);
10365 static const struct got_error *
10366 histedit_syntax_error(int lineno)
10368 static char msg[42];
10371 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10373 if (ret == -1 || ret >= sizeof(msg))
10374 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10376 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10379 static const struct got_error *
10380 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10381 char *logmsg, struct got_repository *repo)
10383 const struct got_error *err;
10384 struct got_commit_object *folded_commit = NULL;
10385 char *id_str, *folded_logmsg = NULL;
10387 err = got_object_id_str(&id_str, hle->commit_id);
10391 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10395 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10398 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10399 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10400 folded_logmsg) == -1) {
10401 err = got_error_from_errno("asprintf");
10405 got_object_commit_close(folded_commit);
10407 free(folded_logmsg);
10411 static struct got_histedit_list_entry *
10412 get_folded_commits(struct got_histedit_list_entry *hle)
10414 struct got_histedit_list_entry *prev, *folded = NULL;
10416 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10417 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10418 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10419 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10421 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10427 static const struct got_error *
10428 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10429 struct got_repository *repo)
10431 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10432 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10433 const struct got_error *err = NULL;
10434 struct got_commit_object *commit = NULL;
10437 struct got_histedit_list_entry *folded = NULL;
10439 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10443 folded = get_folded_commits(hle);
10445 while (folded != hle) {
10446 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10447 folded = TAILQ_NEXT(folded, entry);
10450 err = append_folded_commit_msg(&new_msg, folded,
10456 folded = TAILQ_NEXT(folded, entry);
10460 err = got_object_id_str(&id_str, hle->commit_id);
10463 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10466 logmsg_len = asprintf(&new_msg,
10467 "%s\n# original log message of commit %s: %s",
10468 logmsg ? logmsg : "", id_str, orig_logmsg);
10469 if (logmsg_len == -1) {
10470 err = got_error_from_errno("asprintf");
10476 err = got_object_id_str(&id_str, hle->commit_id);
10480 err = got_opentemp_named_fd(&logmsg_path, &fd,
10481 GOT_TMPDIR_STR "/got-logmsg");
10485 write(fd, logmsg, logmsg_len);
10488 err = get_editor(&editor);
10492 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10495 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10498 hle->logmsg = strdup(new_msg);
10499 if (hle->logmsg == NULL)
10500 err = got_error_from_errno("strdup");
10503 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10504 err = got_error_from_errno2("unlink", logmsg_path);
10510 got_object_commit_close(commit);
10514 static const struct got_error *
10515 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10516 FILE *f, struct got_repository *repo)
10518 const struct got_error *err = NULL;
10519 char *line = NULL, *p, *end;
10523 const struct got_histedit_cmd *cmd;
10524 struct got_object_id *commit_id = NULL;
10525 struct got_histedit_list_entry *hle = NULL;
10528 len = getline(&line, &size, f);
10530 const struct got_error *getline_err;
10533 getline_err = got_error_from_errno("getline");
10534 err = got_ferror(f, getline_err->code);
10539 while (isspace((unsigned char)p[0]))
10541 if (p[0] == '#' || p[0] == '\0') {
10547 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10548 cmd = &got_histedit_cmds[i];
10549 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10550 isspace((unsigned char)p[strlen(cmd->name)])) {
10551 p += strlen(cmd->name);
10554 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10559 if (i == nitems(got_histedit_cmds)) {
10560 err = histedit_syntax_error(lineno);
10563 while (isspace((unsigned char)p[0]))
10565 if (cmd->code == GOT_HISTEDIT_MESG) {
10566 if (hle == NULL || hle->logmsg != NULL) {
10567 err = got_error(GOT_ERR_HISTEDIT_CMD);
10570 if (p[0] == '\0') {
10571 err = histedit_edit_logmsg(hle, repo);
10575 hle->logmsg = strdup(p);
10576 if (hle->logmsg == NULL) {
10577 err = got_error_from_errno("strdup");
10586 while (end[0] && !isspace((unsigned char)end[0]))
10590 err = got_object_resolve_id_str(&commit_id, repo, p);
10592 /* override error code */
10593 err = histedit_syntax_error(lineno);
10597 hle = malloc(sizeof(*hle));
10599 err = got_error_from_errno("malloc");
10603 hle->commit_id = commit_id;
10604 hle->logmsg = NULL;
10608 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10616 static const struct got_error *
10617 histedit_check_script(struct got_histedit_list *histedit_cmds,
10618 struct got_object_id_queue *commits, struct got_repository *repo)
10620 const struct got_error *err = NULL;
10621 struct got_object_qid *qid;
10622 struct got_histedit_list_entry *hle;
10623 static char msg[92];
10626 if (TAILQ_EMPTY(histedit_cmds))
10627 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10628 "histedit script contains no commands");
10629 if (STAILQ_EMPTY(commits))
10630 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10632 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10633 struct got_histedit_list_entry *hle2;
10634 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10637 if (got_object_id_cmp(hle->commit_id,
10638 hle2->commit_id) != 0)
10640 err = got_object_id_str(&id_str, hle->commit_id);
10643 snprintf(msg, sizeof(msg), "commit %s is listed "
10644 "more than once in histedit script", id_str);
10646 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10650 STAILQ_FOREACH(qid, commits, entry) {
10651 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10652 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10656 err = got_object_id_str(&id_str, &qid->id);
10659 snprintf(msg, sizeof(msg),
10660 "commit %s missing from histedit script", id_str);
10662 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10666 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10667 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10668 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10669 "last commit in histedit script cannot be folded");
10674 static const struct got_error *
10675 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10676 const char *path, struct got_object_id_queue *commits,
10677 struct got_repository *repo)
10679 const struct got_error *err = NULL;
10683 err = get_editor(&editor);
10687 if (spawn_editor(editor, path) == -1) {
10688 err = got_error_from_errno("failed spawning editor");
10692 f = fopen(path, "re");
10694 err = got_error_from_errno("fopen");
10697 err = histedit_parse_list(histedit_cmds, f, repo);
10701 err = histedit_check_script(histedit_cmds, commits, repo);
10703 if (f && fclose(f) == EOF && err == NULL)
10704 err = got_error_from_errno("fclose");
10709 static const struct got_error *
10710 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10711 struct got_object_id_queue *, const char *, const char *,
10712 struct got_repository *);
10714 static const struct got_error *
10715 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10716 struct got_object_id_queue *commits, const char *branch_name,
10717 int edit_logmsg_only, int fold_only, int edit_only,
10718 struct got_repository *repo)
10720 const struct got_error *err;
10724 err = got_opentemp_named(&path, &f, "got-histedit");
10728 err = write_cmd_list(f, branch_name, commits);
10732 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10733 fold_only, edit_only, repo);
10737 if (edit_logmsg_only || fold_only || edit_only) {
10739 err = histedit_parse_list(histedit_cmds, f, repo);
10741 if (fclose(f) == EOF) {
10742 err = got_error_from_errno("fclose");
10746 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10748 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10749 err->code != GOT_ERR_HISTEDIT_CMD)
10751 err = histedit_edit_list_retry(histedit_cmds, err,
10752 commits, path, branch_name, repo);
10756 if (f && fclose(f) == EOF && err == NULL)
10757 err = got_error_from_errno("fclose");
10758 if (path && unlink(path) != 0 && err == NULL)
10759 err = got_error_from_errno2("unlink", path);
10764 static const struct got_error *
10765 histedit_save_list(struct got_histedit_list *histedit_cmds,
10766 struct got_worktree *worktree, struct got_repository *repo)
10768 const struct got_error *err = NULL;
10771 struct got_histedit_list_entry *hle;
10772 struct got_commit_object *commit = NULL;
10774 err = got_worktree_get_histedit_script_path(&path, worktree);
10778 f = fopen(path, "we");
10780 err = got_error_from_errno2("fopen", path);
10783 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10784 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10790 int n = fprintf(f, "%c %s\n",
10791 GOT_HISTEDIT_MESG, hle->logmsg);
10793 err = got_ferror(f, GOT_ERR_IO);
10799 if (f && fclose(f) == EOF && err == NULL)
10800 err = got_error_from_errno("fclose");
10803 got_object_commit_close(commit);
10808 histedit_free_list(struct got_histedit_list *histedit_cmds)
10810 struct got_histedit_list_entry *hle;
10812 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10813 TAILQ_REMOVE(histedit_cmds, hle, entry);
10818 static const struct got_error *
10819 histedit_load_list(struct got_histedit_list *histedit_cmds,
10820 const char *path, struct got_repository *repo)
10822 const struct got_error *err = NULL;
10825 f = fopen(path, "re");
10827 err = got_error_from_errno2("fopen", path);
10831 err = histedit_parse_list(histedit_cmds, f, repo);
10833 if (f && fclose(f) == EOF && err == NULL)
10834 err = got_error_from_errno("fclose");
10838 static const struct got_error *
10839 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10840 const struct got_error *edit_err, struct got_object_id_queue *commits,
10841 const char *path, const char *branch_name, struct got_repository *repo)
10843 const struct got_error *err = NULL, *prev_err = edit_err;
10846 while (resp != 'c' && resp != 'r' && resp != 'a') {
10847 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10848 "or (a)bort: ", getprogname(), prev_err->msg);
10853 histedit_free_list(histedit_cmds);
10854 err = histedit_run_editor(histedit_cmds, path, commits,
10857 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10858 err->code != GOT_ERR_HISTEDIT_CMD)
10865 } else if (resp == 'r') {
10866 histedit_free_list(histedit_cmds);
10867 err = histedit_edit_script(histedit_cmds,
10868 commits, branch_name, 0, 0, 0, repo);
10870 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10871 err->code != GOT_ERR_HISTEDIT_CMD)
10878 } else if (resp == 'a') {
10879 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10882 printf("invalid response '%c'\n", resp);
10888 static const struct got_error *
10889 histedit_complete(struct got_worktree *worktree,
10890 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10891 struct got_reference *branch, struct got_repository *repo)
10893 printf("Switching work tree to %s\n",
10894 got_ref_get_symref_target(branch));
10895 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10899 static const struct got_error *
10900 show_histedit_progress(struct got_commit_object *commit,
10901 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10903 const struct got_error *err;
10904 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10906 err = got_object_id_str(&old_id_str, hle->commit_id);
10911 err = got_object_id_str(&new_id_str, new_id);
10916 old_id_str[12] = '\0';
10918 new_id_str[12] = '\0';
10921 logmsg = strdup(hle->logmsg);
10922 if (logmsg == NULL) {
10923 err = got_error_from_errno("strdup");
10926 trim_logmsg(logmsg, 42);
10928 err = get_short_logmsg(&logmsg, 42, commit);
10933 switch (hle->cmd->code) {
10934 case GOT_HISTEDIT_PICK:
10935 case GOT_HISTEDIT_EDIT:
10936 printf("%s -> %s: %s\n", old_id_str,
10937 new_id_str ? new_id_str : "no-op change", logmsg);
10939 case GOT_HISTEDIT_DROP:
10940 case GOT_HISTEDIT_FOLD:
10941 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10953 static const struct got_error *
10954 histedit_commit(struct got_pathlist_head *merged_paths,
10955 struct got_worktree *worktree, struct got_fileindex *fileindex,
10956 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10957 struct got_repository *repo)
10959 const struct got_error *err;
10960 struct got_commit_object *commit;
10961 struct got_object_id *new_commit_id;
10963 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10964 && hle->logmsg == NULL) {
10965 err = histedit_edit_logmsg(hle, repo);
10970 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10974 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10975 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10976 hle->logmsg, repo);
10978 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10980 err = show_histedit_progress(commit, hle, NULL);
10982 err = show_histedit_progress(commit, hle, new_commit_id);
10983 free(new_commit_id);
10986 got_object_commit_close(commit);
10990 static const struct got_error *
10991 histedit_skip_commit(struct got_histedit_list_entry *hle,
10992 struct got_worktree *worktree, struct got_repository *repo)
10994 const struct got_error *error;
10995 struct got_commit_object *commit;
10997 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
11002 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
11006 error = show_histedit_progress(commit, hle, NULL);
11007 got_object_commit_close(commit);
11011 static const struct got_error *
11012 check_local_changes(void *arg, unsigned char status,
11013 unsigned char staged_status, const char *path,
11014 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11015 struct got_object_id *commit_id, int dirfd, const char *de_name)
11017 int *have_local_changes = arg;
11020 case GOT_STATUS_ADD:
11021 case GOT_STATUS_DELETE:
11022 case GOT_STATUS_MODIFY:
11023 case GOT_STATUS_CONFLICT:
11024 *have_local_changes = 1;
11025 return got_error(GOT_ERR_CANCELLED);
11030 switch (staged_status) {
11031 case GOT_STATUS_ADD:
11032 case GOT_STATUS_DELETE:
11033 case GOT_STATUS_MODIFY:
11034 *have_local_changes = 1;
11035 return got_error(GOT_ERR_CANCELLED);
11043 static const struct got_error *
11044 cmd_histedit(int argc, char *argv[])
11046 const struct got_error *error = NULL;
11047 struct got_worktree *worktree = NULL;
11048 struct got_fileindex *fileindex = NULL;
11049 struct got_repository *repo = NULL;
11051 struct got_reference *branch = NULL;
11052 struct got_reference *tmp_branch = NULL;
11053 struct got_object_id *resume_commit_id = NULL;
11054 struct got_object_id *base_commit_id = NULL;
11055 struct got_object_id *head_commit_id = NULL;
11056 struct got_commit_object *commit = NULL;
11057 int ch, rebase_in_progress = 0, merge_in_progress = 0;
11058 struct got_update_progress_arg upa;
11059 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
11060 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
11061 int list_backups = 0, delete_backups = 0;
11062 const char *edit_script_path = NULL;
11063 struct got_object_id_queue commits;
11064 struct got_pathlist_head merged_paths;
11065 const struct got_object_id_queue *parent_ids;
11066 struct got_object_qid *pid;
11067 struct got_histedit_list histedit_cmds;
11068 struct got_histedit_list_entry *hle;
11069 int *pack_fds = NULL;
11071 STAILQ_INIT(&commits);
11072 TAILQ_INIT(&histedit_cmds);
11073 TAILQ_INIT(&merged_paths);
11074 memset(&upa, 0, sizeof(upa));
11076 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
11091 edit_script_path = optarg;
11094 edit_logmsg_only = 1;
11100 delete_backups = 1;
11112 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11113 "unveil", NULL) == -1)
11116 if (abort_edit && continue_edit)
11117 option_conflict('a', 'c');
11118 if (edit_script_path && edit_logmsg_only)
11119 option_conflict('F', 'm');
11120 if (abort_edit && edit_logmsg_only)
11121 option_conflict('a', 'm');
11122 if (continue_edit && edit_logmsg_only)
11123 option_conflict('c', 'm');
11124 if (abort_edit && fold_only)
11125 option_conflict('a', 'f');
11126 if (continue_edit && fold_only)
11127 option_conflict('c', 'f');
11128 if (fold_only && edit_logmsg_only)
11129 option_conflict('f', 'm');
11130 if (edit_script_path && fold_only)
11131 option_conflict('F', 'f');
11132 if (abort_edit && edit_only)
11133 option_conflict('a', 'e');
11134 if (continue_edit && edit_only)
11135 option_conflict('c', 'e');
11136 if (edit_only && edit_logmsg_only)
11137 option_conflict('e', 'm');
11138 if (edit_script_path && edit_only)
11139 option_conflict('F', 'e');
11140 if (list_backups) {
11142 option_conflict('l', 'a');
11144 option_conflict('l', 'c');
11145 if (edit_script_path)
11146 option_conflict('l', 'F');
11147 if (edit_logmsg_only)
11148 option_conflict('l', 'm');
11150 option_conflict('l', 'f');
11152 option_conflict('l', 'e');
11153 if (delete_backups)
11154 option_conflict('l', 'X');
11155 if (argc != 0 && argc != 1)
11157 } else if (delete_backups) {
11159 option_conflict('X', 'a');
11161 option_conflict('X', 'c');
11162 if (edit_script_path)
11163 option_conflict('X', 'F');
11164 if (edit_logmsg_only)
11165 option_conflict('X', 'm');
11167 option_conflict('X', 'f');
11169 option_conflict('X', 'e');
11171 option_conflict('X', 'l');
11172 if (argc != 0 && argc != 1)
11174 } else if (argc != 0)
11178 * This command cannot apply unveil(2) in all cases because the
11179 * user may choose to run an editor to edit the histedit script
11180 * and to edit individual commit log messages.
11181 * unveil(2) traverses exec(2); if an editor is used we have to
11182 * apply unveil after edit script and log messages have been written.
11183 * XXX TODO: Make use of unveil(2) where possible.
11186 cwd = getcwd(NULL, 0);
11188 error = got_error_from_errno("getcwd");
11192 error = got_repo_pack_fds_open(&pack_fds);
11196 error = got_worktree_open(&worktree, cwd);
11198 if (list_backups || delete_backups) {
11199 if (error->code != GOT_ERR_NOT_WORKTREE)
11202 if (error->code == GOT_ERR_NOT_WORKTREE)
11203 error = wrap_not_worktree_error(error,
11209 if (list_backups || delete_backups) {
11210 error = got_repo_open(&repo,
11211 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11215 error = apply_unveil(got_repo_get_path(repo), 0,
11216 worktree ? got_worktree_get_root_path(worktree) : NULL);
11219 error = process_backup_refs(
11220 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11221 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11222 goto done; /* nothing else to do */
11225 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11230 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11233 if (rebase_in_progress) {
11234 error = got_error(GOT_ERR_REBASING);
11238 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11242 if (merge_in_progress) {
11243 error = got_error(GOT_ERR_MERGE_BUSY);
11247 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11251 if (edit_in_progress && edit_logmsg_only) {
11252 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11253 "histedit operation is in progress in this "
11254 "work tree and must be continued or aborted "
11255 "before the -m option can be used");
11258 if (edit_in_progress && fold_only) {
11259 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11260 "histedit operation is in progress in this "
11261 "work tree and must be continued or aborted "
11262 "before the -f option can be used");
11265 if (edit_in_progress && edit_only) {
11266 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11267 "histedit operation is in progress in this "
11268 "work tree and must be continued or aborted "
11269 "before the -e option can be used");
11273 if (edit_in_progress && abort_edit) {
11274 error = got_worktree_histedit_continue(&resume_commit_id,
11275 &tmp_branch, &branch, &base_commit_id, &fileindex,
11279 printf("Switching work tree to %s\n",
11280 got_ref_get_symref_target(branch));
11281 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11282 branch, base_commit_id, abort_progress, &upa);
11285 printf("Histedit of %s aborted\n",
11286 got_ref_get_symref_target(branch));
11287 print_merge_progress_stats(&upa);
11288 goto done; /* nothing else to do */
11289 } else if (abort_edit) {
11290 error = got_error(GOT_ERR_NOT_HISTEDIT);
11294 if (continue_edit) {
11297 if (!edit_in_progress) {
11298 error = got_error(GOT_ERR_NOT_HISTEDIT);
11302 error = got_worktree_get_histedit_script_path(&path, worktree);
11306 error = histedit_load_list(&histedit_cmds, path, repo);
11311 error = got_worktree_histedit_continue(&resume_commit_id,
11312 &tmp_branch, &branch, &base_commit_id, &fileindex,
11317 error = got_ref_resolve(&head_commit_id, repo, branch);
11321 error = got_object_open_as_commit(&commit, repo,
11325 parent_ids = got_object_commit_get_parent_ids(commit);
11326 pid = STAILQ_FIRST(parent_ids);
11328 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11331 error = collect_commits(&commits, head_commit_id, &pid->id,
11332 base_commit_id, got_worktree_get_path_prefix(worktree),
11333 GOT_ERR_HISTEDIT_PATH, repo);
11334 got_object_commit_close(commit);
11339 if (edit_in_progress) {
11340 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11344 error = got_ref_open(&branch, repo,
11345 got_worktree_get_head_ref_name(worktree), 0);
11349 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11350 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11351 "will not edit commit history of a branch outside "
11352 "the \"refs/heads/\" reference namespace");
11356 error = got_ref_resolve(&head_commit_id, repo, branch);
11357 got_ref_close(branch);
11362 error = got_object_open_as_commit(&commit, repo,
11366 parent_ids = got_object_commit_get_parent_ids(commit);
11367 pid = STAILQ_FIRST(parent_ids);
11369 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11372 error = collect_commits(&commits, head_commit_id, &pid->id,
11373 got_worktree_get_base_commit_id(worktree),
11374 got_worktree_get_path_prefix(worktree),
11375 GOT_ERR_HISTEDIT_PATH, repo);
11376 got_object_commit_close(commit);
11381 if (STAILQ_EMPTY(&commits)) {
11382 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11386 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11387 &base_commit_id, &fileindex, worktree, repo);
11391 if (edit_script_path) {
11392 error = histedit_load_list(&histedit_cmds,
11393 edit_script_path, repo);
11395 got_worktree_histedit_abort(worktree, fileindex,
11396 repo, branch, base_commit_id,
11397 abort_progress, &upa);
11398 print_merge_progress_stats(&upa);
11402 const char *branch_name;
11403 branch_name = got_ref_get_symref_target(branch);
11404 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11406 error = histedit_edit_script(&histedit_cmds, &commits,
11407 branch_name, edit_logmsg_only, fold_only,
11410 got_worktree_histedit_abort(worktree, fileindex,
11411 repo, branch, base_commit_id,
11412 abort_progress, &upa);
11413 print_merge_progress_stats(&upa);
11419 error = histedit_save_list(&histedit_cmds, worktree,
11422 got_worktree_histedit_abort(worktree, fileindex,
11423 repo, branch, base_commit_id,
11424 abort_progress, &upa);
11425 print_merge_progress_stats(&upa);
11431 error = histedit_check_script(&histedit_cmds, &commits, repo);
11435 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11436 if (resume_commit_id) {
11437 if (got_object_id_cmp(hle->commit_id,
11438 resume_commit_id) != 0)
11441 resume_commit_id = NULL;
11442 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11443 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11444 error = histedit_skip_commit(hle, worktree,
11449 struct got_pathlist_head paths;
11450 int have_changes = 0;
11452 TAILQ_INIT(&paths);
11453 error = got_pathlist_append(&paths, "", NULL);
11456 error = got_worktree_status(worktree, &paths,
11457 repo, 0, check_local_changes, &have_changes,
11458 check_cancelled, NULL);
11459 got_pathlist_free(&paths);
11461 if (error->code != GOT_ERR_CANCELLED)
11463 if (sigint_received || sigpipe_received)
11466 if (have_changes) {
11467 error = histedit_commit(NULL, worktree,
11468 fileindex, tmp_branch, hle, repo);
11472 error = got_object_open_as_commit(
11473 &commit, repo, hle->commit_id);
11476 error = show_histedit_progress(commit,
11478 got_object_commit_close(commit);
11487 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11488 error = histedit_skip_commit(hle, worktree, repo);
11494 error = got_object_open_as_commit(&commit, repo,
11498 parent_ids = got_object_commit_get_parent_ids(commit);
11499 pid = STAILQ_FIRST(parent_ids);
11501 error = got_worktree_histedit_merge_files(&merged_paths,
11502 worktree, fileindex, &pid->id, hle->commit_id, repo,
11503 update_progress, &upa, check_cancelled, NULL);
11506 got_object_commit_close(commit);
11509 print_merge_progress_stats(&upa);
11510 if (upa.conflicts > 0 || upa.missing > 0 ||
11511 upa.not_deleted > 0 || upa.unversioned > 0) {
11512 if (upa.conflicts > 0) {
11513 error = show_rebase_merge_conflict(
11514 hle->commit_id, repo);
11518 got_worktree_rebase_pathlist_free(&merged_paths);
11522 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11524 error = got_object_id_str(&id_str, hle->commit_id);
11527 printf("Stopping histedit for amending commit %s\n",
11530 got_worktree_rebase_pathlist_free(&merged_paths);
11531 error = got_worktree_histedit_postpone(worktree,
11536 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11537 error = histedit_skip_commit(hle, worktree, repo);
11543 error = histedit_commit(&merged_paths, worktree, fileindex,
11544 tmp_branch, hle, repo);
11545 got_worktree_rebase_pathlist_free(&merged_paths);
11550 if (upa.conflicts > 0 || upa.missing > 0 ||
11551 upa.not_deleted > 0 || upa.unversioned > 0) {
11552 error = got_worktree_histedit_postpone(worktree, fileindex);
11555 if (upa.conflicts > 0 && upa.missing == 0 &&
11556 upa.not_deleted == 0 && upa.unversioned == 0) {
11557 error = got_error_msg(GOT_ERR_CONFLICTS,
11558 "conflicts must be resolved before histedit "
11560 } else if (upa.conflicts > 0) {
11561 error = got_error_msg(GOT_ERR_CONFLICTS,
11562 "conflicts must be resolved before histedit "
11563 "can continue; changes destined for some "
11564 "files were not yet merged and should be "
11565 "merged manually if required before the "
11566 "histedit operation is continued");
11568 error = got_error_msg(GOT_ERR_CONFLICTS,
11569 "changes destined for some files were not "
11570 "yet merged and should be merged manually "
11571 "if required before the histedit operation "
11575 error = histedit_complete(worktree, fileindex, tmp_branch,
11578 got_object_id_queue_free(&commits);
11579 histedit_free_list(&histedit_cmds);
11580 free(head_commit_id);
11581 free(base_commit_id);
11582 free(resume_commit_id);
11584 got_object_commit_close(commit);
11586 got_ref_close(branch);
11588 got_ref_close(tmp_branch);
11590 got_worktree_close(worktree);
11592 const struct got_error *close_err = got_repo_close(repo);
11597 const struct got_error *pack_err =
11598 got_repo_pack_fds_close(pack_fds);
11606 usage_integrate(void)
11608 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11612 static const struct got_error *
11613 cmd_integrate(int argc, char *argv[])
11615 const struct got_error *error = NULL;
11616 struct got_repository *repo = NULL;
11617 struct got_worktree *worktree = NULL;
11618 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11619 const char *branch_arg = NULL;
11620 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11621 struct got_fileindex *fileindex = NULL;
11622 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11624 struct got_update_progress_arg upa;
11625 int *pack_fds = NULL;
11627 while ((ch = getopt(argc, argv, "")) != -1) {
11640 branch_arg = argv[0];
11642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11643 "unveil", NULL) == -1)
11646 cwd = getcwd(NULL, 0);
11648 error = got_error_from_errno("getcwd");
11652 error = got_repo_pack_fds_open(&pack_fds);
11656 error = got_worktree_open(&worktree, cwd);
11658 if (error->code == GOT_ERR_NOT_WORKTREE)
11659 error = wrap_not_worktree_error(error, "integrate",
11664 error = check_rebase_or_histedit_in_progress(worktree);
11668 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11673 error = apply_unveil(got_repo_get_path(repo), 0,
11674 got_worktree_get_root_path(worktree));
11678 error = check_merge_in_progress(worktree, repo);
11682 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11683 error = got_error_from_errno("asprintf");
11687 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11688 &base_branch_ref, worktree, refname, repo);
11692 refname = strdup(got_ref_get_name(branch_ref));
11693 if (refname == NULL) {
11694 error = got_error_from_errno("strdup");
11695 got_worktree_integrate_abort(worktree, fileindex, repo,
11696 branch_ref, base_branch_ref);
11699 base_refname = strdup(got_ref_get_name(base_branch_ref));
11700 if (base_refname == NULL) {
11701 error = got_error_from_errno("strdup");
11702 got_worktree_integrate_abort(worktree, fileindex, repo,
11703 branch_ref, base_branch_ref);
11707 error = got_ref_resolve(&commit_id, repo, branch_ref);
11711 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11715 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11716 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11717 "specified branch has already been integrated");
11718 got_worktree_integrate_abort(worktree, fileindex, repo,
11719 branch_ref, base_branch_ref);
11723 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11725 if (error->code == GOT_ERR_ANCESTRY)
11726 error = got_error(GOT_ERR_REBASE_REQUIRED);
11727 got_worktree_integrate_abort(worktree, fileindex, repo,
11728 branch_ref, base_branch_ref);
11732 memset(&upa, 0, sizeof(upa));
11733 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11734 branch_ref, base_branch_ref, update_progress, &upa,
11735 check_cancelled, NULL);
11739 printf("Integrated %s into %s\n", refname, base_refname);
11740 print_update_progress_stats(&upa);
11743 const struct got_error *close_err = got_repo_close(repo);
11748 got_worktree_close(worktree);
11750 const struct got_error *pack_err =
11751 got_repo_pack_fds_close(pack_fds);
11756 free(base_commit_id);
11759 free(base_refname);
11766 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11771 static const struct got_error *
11772 cmd_merge(int argc, char *argv[])
11774 const struct got_error *error = NULL;
11775 struct got_worktree *worktree = NULL;
11776 struct got_repository *repo = NULL;
11777 struct got_fileindex *fileindex = NULL;
11778 char *cwd = NULL, *id_str = NULL, *author = NULL;
11779 struct got_reference *branch = NULL, *wt_branch = NULL;
11780 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11781 struct got_object_id *wt_branch_tip = NULL;
11782 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11783 int interrupt_merge = 0;
11784 struct got_update_progress_arg upa;
11785 struct got_object_id *merge_commit_id = NULL;
11786 char *branch_name = NULL;
11787 int *pack_fds = NULL;
11789 memset(&upa, 0, sizeof(upa));
11791 while ((ch = getopt(argc, argv, "acn")) != -1) {
11797 continue_merge = 1;
11800 interrupt_merge = 1;
11812 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11813 "unveil", NULL) == -1)
11817 if (abort_merge && continue_merge)
11818 option_conflict('a', 'c');
11819 if (abort_merge || continue_merge) {
11822 } else if (argc != 1)
11825 cwd = getcwd(NULL, 0);
11827 error = got_error_from_errno("getcwd");
11831 error = got_repo_pack_fds_open(&pack_fds);
11835 error = got_worktree_open(&worktree, cwd);
11837 if (error->code == GOT_ERR_NOT_WORKTREE)
11838 error = wrap_not_worktree_error(error,
11843 error = got_repo_open(&repo,
11844 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11849 error = apply_unveil(got_repo_get_path(repo), 0,
11850 worktree ? got_worktree_get_root_path(worktree) : NULL);
11854 error = check_rebase_or_histedit_in_progress(worktree);
11858 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11864 if (!merge_in_progress) {
11865 error = got_error(GOT_ERR_NOT_MERGING);
11868 error = got_worktree_merge_continue(&branch_name,
11869 &branch_tip, &fileindex, worktree, repo);
11872 error = got_worktree_merge_abort(worktree, fileindex, repo,
11873 abort_progress, &upa);
11876 printf("Merge of %s aborted\n", branch_name);
11877 goto done; /* nothing else to do */
11880 error = get_author(&author, repo, worktree);
11884 if (continue_merge) {
11885 if (!merge_in_progress) {
11886 error = got_error(GOT_ERR_NOT_MERGING);
11889 error = got_worktree_merge_continue(&branch_name,
11890 &branch_tip, &fileindex, worktree, repo);
11894 error = got_ref_open(&branch, repo, argv[0], 0);
11897 branch_name = strdup(got_ref_get_name(branch));
11898 if (branch_name == NULL) {
11899 error = got_error_from_errno("strdup");
11902 error = got_ref_resolve(&branch_tip, repo, branch);
11907 error = got_ref_open(&wt_branch, repo,
11908 got_worktree_get_head_ref_name(worktree), 0);
11911 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11914 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11915 wt_branch_tip, branch_tip, 0, repo,
11916 check_cancelled, NULL);
11917 if (error && error->code != GOT_ERR_ANCESTRY)
11920 if (!continue_merge) {
11921 error = check_path_prefix(wt_branch_tip, branch_tip,
11922 got_worktree_get_path_prefix(worktree),
11923 GOT_ERR_MERGE_PATH, repo);
11927 error = check_same_branch(wt_branch_tip, branch,
11930 if (error->code != GOT_ERR_ANCESTRY)
11934 static char msg[512];
11935 snprintf(msg, sizeof(msg),
11936 "cannot create a merge commit because "
11937 "%s is based on %s; %s can be integrated "
11938 "with 'got integrate' instead", branch_name,
11939 got_worktree_get_head_ref_name(worktree),
11941 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11945 error = got_worktree_merge_prepare(&fileindex, worktree,
11950 error = got_worktree_merge_branch(worktree, fileindex,
11951 yca_id, branch_tip, repo, update_progress, &upa,
11952 check_cancelled, NULL);
11955 print_merge_progress_stats(&upa);
11956 if (!upa.did_something) {
11957 error = got_worktree_merge_abort(worktree, fileindex,
11958 repo, abort_progress, &upa);
11961 printf("Already up-to-date\n");
11966 if (interrupt_merge) {
11967 error = got_worktree_merge_postpone(worktree, fileindex);
11970 printf("Merge of %s interrupted on request\n", branch_name);
11971 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11972 upa.not_deleted > 0 || upa.unversioned > 0) {
11973 error = got_worktree_merge_postpone(worktree, fileindex);
11976 if (upa.conflicts > 0 && upa.missing == 0 &&
11977 upa.not_deleted == 0 && upa.unversioned == 0) {
11978 error = got_error_msg(GOT_ERR_CONFLICTS,
11979 "conflicts must be resolved before merging "
11981 } else if (upa.conflicts > 0) {
11982 error = got_error_msg(GOT_ERR_CONFLICTS,
11983 "conflicts must be resolved before merging "
11984 "can continue; changes destined for some "
11985 "files were not yet merged and "
11986 "should be merged manually if required before the "
11987 "merge operation is continued");
11989 error = got_error_msg(GOT_ERR_CONFLICTS,
11990 "changes destined for some "
11991 "files were not yet merged and should be "
11992 "merged manually if required before the "
11993 "merge operation is continued");
11997 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11998 fileindex, author, NULL, 1, branch_tip, branch_name,
11999 repo, continue_merge ? print_status : NULL, NULL);
12002 error = got_worktree_merge_complete(worktree, fileindex, repo);
12005 error = got_object_id_str(&id_str, merge_commit_id);
12008 printf("Merged %s into %s: %s\n", branch_name,
12009 got_worktree_get_head_ref_name(worktree),
12015 free(merge_commit_id);
12021 got_ref_close(branch);
12023 got_ref_close(wt_branch);
12025 got_worktree_close(worktree);
12027 const struct got_error *close_err = got_repo_close(repo);
12032 const struct got_error *pack_err =
12033 got_repo_pack_fds_close(pack_fds);
12043 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
12044 "[-S] [file-path ...]\n",
12049 static const struct got_error *
12050 print_stage(void *arg, unsigned char status, unsigned char staged_status,
12051 const char *path, struct got_object_id *blob_id,
12052 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
12053 int dirfd, const char *de_name)
12055 const struct got_error *err = NULL;
12056 char *id_str = NULL;
12058 if (staged_status != GOT_STATUS_ADD &&
12059 staged_status != GOT_STATUS_MODIFY &&
12060 staged_status != GOT_STATUS_DELETE)
12063 if (staged_status == GOT_STATUS_ADD ||
12064 staged_status == GOT_STATUS_MODIFY)
12065 err = got_object_id_str(&id_str, staged_blob_id);
12067 err = got_object_id_str(&id_str, blob_id);
12071 printf("%s %c %s\n", id_str, staged_status, path);
12076 static const struct got_error *
12077 cmd_stage(int argc, char *argv[])
12079 const struct got_error *error = NULL;
12080 struct got_repository *repo = NULL;
12081 struct got_worktree *worktree = NULL;
12083 struct got_pathlist_head paths;
12084 struct got_pathlist_entry *pe;
12085 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12086 FILE *patch_script_file = NULL;
12087 const char *patch_script_path = NULL;
12088 struct choose_patch_arg cpa;
12089 int *pack_fds = NULL;
12091 TAILQ_INIT(&paths);
12093 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12102 patch_script_path = optarg;
12105 allow_bad_symlinks = 1;
12117 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12118 "unveil", NULL) == -1)
12121 if (list_stage && (pflag || patch_script_path))
12122 errx(1, "-l option cannot be used with other options");
12123 if (patch_script_path && !pflag)
12124 errx(1, "-F option can only be used together with -p option");
12126 cwd = getcwd(NULL, 0);
12128 error = got_error_from_errno("getcwd");
12132 error = got_repo_pack_fds_open(&pack_fds);
12136 error = got_worktree_open(&worktree, cwd);
12138 if (error->code == GOT_ERR_NOT_WORKTREE)
12139 error = wrap_not_worktree_error(error, "stage", cwd);
12143 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12148 if (patch_script_path) {
12149 patch_script_file = fopen(patch_script_path, "re");
12150 if (patch_script_file == NULL) {
12151 error = got_error_from_errno2("fopen",
12152 patch_script_path);
12156 error = apply_unveil(got_repo_get_path(repo), 0,
12157 got_worktree_get_root_path(worktree));
12161 error = check_merge_in_progress(worktree, repo);
12165 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12170 error = got_worktree_status(worktree, &paths, repo, 0,
12171 print_stage, NULL, check_cancelled, NULL);
12173 cpa.patch_script_file = patch_script_file;
12174 cpa.action = "stage";
12175 error = got_worktree_stage(worktree, &paths,
12176 pflag ? NULL : print_status, NULL,
12177 pflag ? choose_patch : NULL, &cpa,
12178 allow_bad_symlinks, repo);
12181 if (patch_script_file && fclose(patch_script_file) == EOF &&
12183 error = got_error_from_errno2("fclose", patch_script_path);
12185 const struct got_error *close_err = got_repo_close(repo);
12190 got_worktree_close(worktree);
12192 const struct got_error *pack_err =
12193 got_repo_pack_fds_close(pack_fds);
12197 TAILQ_FOREACH(pe, &paths, entry)
12198 free((char *)pe->path);
12199 got_pathlist_free(&paths);
12205 usage_unstage(void)
12207 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12208 "[file-path ...]\n",
12214 static const struct got_error *
12215 cmd_unstage(int argc, char *argv[])
12217 const struct got_error *error = NULL;
12218 struct got_repository *repo = NULL;
12219 struct got_worktree *worktree = NULL;
12221 struct got_pathlist_head paths;
12222 struct got_pathlist_entry *pe;
12224 struct got_update_progress_arg upa;
12225 FILE *patch_script_file = NULL;
12226 const char *patch_script_path = NULL;
12227 struct choose_patch_arg cpa;
12228 int *pack_fds = NULL;
12230 TAILQ_INIT(&paths);
12232 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12238 patch_script_path = optarg;
12250 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12251 "unveil", NULL) == -1)
12254 if (patch_script_path && !pflag)
12255 errx(1, "-F option can only be used together with -p option");
12257 cwd = getcwd(NULL, 0);
12259 error = got_error_from_errno("getcwd");
12263 error = got_repo_pack_fds_open(&pack_fds);
12267 error = got_worktree_open(&worktree, cwd);
12269 if (error->code == GOT_ERR_NOT_WORKTREE)
12270 error = wrap_not_worktree_error(error, "unstage", cwd);
12274 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12279 if (patch_script_path) {
12280 patch_script_file = fopen(patch_script_path, "re");
12281 if (patch_script_file == NULL) {
12282 error = got_error_from_errno2("fopen",
12283 patch_script_path);
12288 error = apply_unveil(got_repo_get_path(repo), 0,
12289 got_worktree_get_root_path(worktree));
12293 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12297 cpa.patch_script_file = patch_script_file;
12298 cpa.action = "unstage";
12299 memset(&upa, 0, sizeof(upa));
12300 error = got_worktree_unstage(worktree, &paths, update_progress,
12301 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12303 print_merge_progress_stats(&upa);
12305 if (patch_script_file && fclose(patch_script_file) == EOF &&
12307 error = got_error_from_errno2("fclose", patch_script_path);
12309 const struct got_error *close_err = got_repo_close(repo);
12314 got_worktree_close(worktree);
12316 const struct got_error *pack_err =
12317 got_repo_pack_fds_close(pack_fds);
12321 TAILQ_FOREACH(pe, &paths, entry)
12322 free((char *)pe->path);
12323 got_pathlist_free(&paths);
12331 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12332 "arg1 [arg2 ...]\n", getprogname());
12336 static const struct got_error *
12337 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12339 const struct got_error *err;
12340 struct got_blob_object *blob;
12343 fd = got_opentempfd();
12345 return got_error_from_errno("got_opentempfd");
12347 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12351 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12353 if (fd != -1 && close(fd) == -1 && err == NULL)
12354 err = got_error_from_errno("close");
12356 got_object_blob_close(blob);
12360 static const struct got_error *
12361 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12363 const struct got_error *err;
12364 struct got_tree_object *tree;
12367 err = got_object_open_as_tree(&tree, repo, id);
12371 nentries = got_object_tree_get_nentries(tree);
12372 for (i = 0; i < nentries; i++) {
12373 struct got_tree_entry *te;
12375 if (sigint_received || sigpipe_received)
12377 te = got_object_tree_get_entry(tree, i);
12378 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12381 fprintf(outfile, "%s %.7o %s\n", id_str,
12382 got_tree_entry_get_mode(te),
12383 got_tree_entry_get_name(te));
12387 got_object_tree_close(tree);
12392 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12402 h = (long long)gmtoff / 3600;
12403 m = ((long long)gmtoff - h*3600) / 60;
12404 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12407 static const struct got_error *
12408 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12410 const struct got_error *err;
12411 struct got_commit_object *commit;
12412 const struct got_object_id_queue *parent_ids;
12413 struct got_object_qid *pid;
12414 char *id_str = NULL;
12415 const char *logmsg = NULL;
12418 err = got_object_open_as_commit(&commit, repo, id);
12422 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12426 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12427 parent_ids = got_object_commit_get_parent_ids(commit);
12428 fprintf(outfile, "numparents %d\n",
12429 got_object_commit_get_nparents(commit));
12430 STAILQ_FOREACH(pid, parent_ids, entry) {
12432 err = got_object_id_str(&pid_str, &pid->id);
12435 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12438 format_gmtoff(gmtoff, sizeof(gmtoff),
12439 got_object_commit_get_author_gmtoff(commit));
12440 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12441 got_object_commit_get_author(commit),
12442 (long long)got_object_commit_get_author_time(commit),
12445 format_gmtoff(gmtoff, sizeof(gmtoff),
12446 got_object_commit_get_committer_gmtoff(commit));
12447 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12448 got_object_commit_get_author(commit),
12449 (long long)got_object_commit_get_committer_time(commit),
12452 logmsg = got_object_commit_get_logmsg_raw(commit);
12453 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12454 fprintf(outfile, "%s", logmsg);
12457 got_object_commit_close(commit);
12461 static const struct got_error *
12462 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12464 const struct got_error *err;
12465 struct got_tag_object *tag;
12466 char *id_str = NULL;
12467 const char *tagmsg = NULL;
12470 err = got_object_open_as_tag(&tag, repo, id);
12474 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12478 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12480 switch (got_object_tag_get_object_type(tag)) {
12481 case GOT_OBJ_TYPE_BLOB:
12482 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12483 GOT_OBJ_LABEL_BLOB);
12485 case GOT_OBJ_TYPE_TREE:
12486 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12487 GOT_OBJ_LABEL_TREE);
12489 case GOT_OBJ_TYPE_COMMIT:
12490 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12491 GOT_OBJ_LABEL_COMMIT);
12493 case GOT_OBJ_TYPE_TAG:
12494 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12495 GOT_OBJ_LABEL_TAG);
12501 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12502 got_object_tag_get_name(tag));
12504 format_gmtoff(gmtoff, sizeof(gmtoff),
12505 got_object_tag_get_tagger_gmtoff(tag));
12506 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12507 got_object_tag_get_tagger(tag),
12508 (long long)got_object_tag_get_tagger_time(tag),
12511 tagmsg = got_object_tag_get_message(tag);
12512 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12513 fprintf(outfile, "%s", tagmsg);
12516 got_object_tag_close(tag);
12520 static const struct got_error *
12521 cmd_cat(int argc, char *argv[])
12523 const struct got_error *error;
12524 struct got_repository *repo = NULL;
12525 struct got_worktree *worktree = NULL;
12526 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12527 const char *commit_id_str = NULL;
12528 struct got_object_id *id = NULL, *commit_id = NULL;
12529 struct got_commit_object *commit = NULL;
12530 int ch, obj_type, i, force_path = 0;
12531 struct got_reflist_head refs;
12532 int *pack_fds = NULL;
12537 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12542 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12545 commit_id_str = optarg;
12548 repo_path = realpath(optarg, NULL);
12549 if (repo_path == NULL)
12550 return got_error_from_errno2("realpath",
12552 got_path_strip_trailing_slashes(repo_path);
12566 cwd = getcwd(NULL, 0);
12568 error = got_error_from_errno("getcwd");
12572 error = got_repo_pack_fds_open(&pack_fds);
12576 if (repo_path == NULL) {
12577 error = got_worktree_open(&worktree, cwd);
12578 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12581 repo_path = strdup(
12582 got_worktree_get_repo_path(worktree));
12583 if (repo_path == NULL) {
12584 error = got_error_from_errno("strdup");
12588 /* Release work tree lock. */
12589 got_worktree_close(worktree);
12594 if (repo_path == NULL) {
12595 repo_path = strdup(cwd);
12596 if (repo_path == NULL)
12597 return got_error_from_errno("strdup");
12600 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12605 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12609 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12613 if (commit_id_str == NULL)
12614 commit_id_str = GOT_REF_HEAD;
12615 error = got_repo_match_object_id(&commit_id, NULL,
12616 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12620 error = got_object_open_as_commit(&commit, repo, commit_id);
12624 for (i = 0; i < argc; i++) {
12626 error = got_object_id_by_path(&id, repo, commit,
12631 error = got_repo_match_object_id(&id, &label, argv[i],
12632 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12635 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12636 error->code != GOT_ERR_NOT_REF)
12638 error = got_object_id_by_path(&id, repo,
12645 error = got_object_get_type(&obj_type, repo, id);
12649 switch (obj_type) {
12650 case GOT_OBJ_TYPE_BLOB:
12651 error = cat_blob(id, repo, stdout);
12653 case GOT_OBJ_TYPE_TREE:
12654 error = cat_tree(id, repo, stdout);
12656 case GOT_OBJ_TYPE_COMMIT:
12657 error = cat_commit(id, repo, stdout);
12659 case GOT_OBJ_TYPE_TAG:
12660 error = cat_tag(id, repo, stdout);
12663 error = got_error(GOT_ERR_OBJ_TYPE);
12678 got_object_commit_close(commit);
12680 got_worktree_close(worktree);
12682 const struct got_error *close_err = got_repo_close(repo);
12687 const struct got_error *pack_err =
12688 got_repo_pack_fds_close(pack_fds);
12693 got_ref_list_free(&refs);
12700 fprintf(stderr, "usage: %s info [path ...]\n",
12705 static const struct got_error *
12706 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12707 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12708 struct got_object_id *commit_id)
12710 const struct got_error *err = NULL;
12711 char *id_str = NULL;
12713 struct tm mytm, *tm;
12714 struct got_pathlist_head *paths = arg;
12715 struct got_pathlist_entry *pe;
12718 * Clear error indication from any of the path arguments which
12719 * would cause this file index entry to be displayed.
12721 TAILQ_FOREACH(pe, paths, entry) {
12722 if (got_path_cmp(path, pe->path, strlen(path),
12723 pe->path_len) == 0 ||
12724 got_path_is_child(path, pe->path, pe->path_len))
12725 pe->data = NULL; /* no error */
12728 printf(GOT_COMMIT_SEP_STR);
12730 printf("symlink: %s\n", path);
12731 else if (S_ISREG(mode)) {
12732 printf("file: %s\n", path);
12733 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12734 } else if (S_ISDIR(mode))
12735 printf("directory: %s\n", path);
12737 printf("something: %s\n", path);
12739 tm = localtime_r(&mtime, &mytm);
12742 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12743 return got_error(GOT_ERR_NO_SPACE);
12744 printf("timestamp: %s\n", datebuf);
12747 err = got_object_id_str(&id_str, blob_id);
12750 printf("based on blob: %s\n", id_str);
12754 if (staged_blob_id) {
12755 err = got_object_id_str(&id_str, staged_blob_id);
12758 printf("based on staged blob: %s\n", id_str);
12763 err = got_object_id_str(&id_str, commit_id);
12766 printf("based on commit: %s\n", id_str);
12773 static const struct got_error *
12774 cmd_info(int argc, char *argv[])
12776 const struct got_error *error = NULL;
12777 struct got_worktree *worktree = NULL;
12778 char *cwd = NULL, *id_str = NULL;
12779 struct got_pathlist_head paths;
12780 struct got_pathlist_entry *pe;
12781 char *uuidstr = NULL;
12782 int ch, show_files = 0;
12783 int *pack_fds = NULL;
12785 TAILQ_INIT(&paths);
12787 while ((ch = getopt(argc, argv, "")) != -1) {
12799 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12803 cwd = getcwd(NULL, 0);
12805 error = got_error_from_errno("getcwd");
12809 error = got_repo_pack_fds_open(&pack_fds);
12813 error = got_worktree_open(&worktree, cwd);
12815 if (error->code == GOT_ERR_NOT_WORKTREE)
12816 error = wrap_not_worktree_error(error, "info", cwd);
12821 /* Remove "cpath" promise. */
12822 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12826 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12831 error = get_worktree_paths_from_argv(&paths, argc, argv,
12838 error = got_object_id_str(&id_str,
12839 got_worktree_get_base_commit_id(worktree));
12843 error = got_worktree_get_uuid(&uuidstr, worktree);
12847 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12848 printf("work tree base commit: %s\n", id_str);
12849 printf("work tree path prefix: %s\n",
12850 got_worktree_get_path_prefix(worktree));
12851 printf("work tree branch reference: %s\n",
12852 got_worktree_get_head_ref_name(worktree));
12853 printf("work tree UUID: %s\n", uuidstr);
12854 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12857 struct got_pathlist_entry *pe;
12858 TAILQ_FOREACH(pe, &paths, entry) {
12859 if (pe->path_len == 0)
12862 * Assume this path will fail. This will be corrected
12863 * in print_path_info() in case the path does suceeed.
12865 pe->data = (void *)got_error_path(pe->path,
12868 error = got_worktree_path_info(worktree, &paths,
12869 print_path_info, &paths, check_cancelled, NULL);
12872 TAILQ_FOREACH(pe, &paths, entry) {
12873 if (pe->data != NULL) {
12874 error = pe->data; /* bad path */
12881 const struct got_error *pack_err =
12882 got_repo_pack_fds_close(pack_fds);
12886 TAILQ_FOREACH(pe, &paths, entry)
12887 free((char *)pe->path);
12888 got_pathlist_free(&paths);