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>
22 #include <sys/param.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"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static volatile sig_atomic_t sigint_received;
64 static volatile sig_atomic_t sigpipe_received;
67 catch_sigint(int signo)
73 catch_sigpipe(int signo)
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
83 const char *cmd_alias;
86 __dead static void usage(int, int);
87 __dead static void usage_init(void);
88 __dead static void usage_import(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_fetch(void);
91 __dead static void usage_checkout(void);
92 __dead static void usage_update(void);
93 __dead static void usage_log(void);
94 __dead static void usage_diff(void);
95 __dead static void usage_blame(void);
96 __dead static void usage_tree(void);
97 __dead static void usage_status(void);
98 __dead static void usage_ref(void);
99 __dead static void usage_branch(void);
100 __dead static void usage_tag(void);
101 __dead static void usage_add(void);
102 __dead static void usage_remove(void);
103 __dead static void usage_revert(void);
104 __dead static void usage_commit(void);
105 __dead static void usage_cherrypick(void);
106 __dead static void usage_backout(void);
107 __dead static void usage_rebase(void);
108 __dead static void usage_histedit(void);
109 __dead static void usage_integrate(void);
110 __dead static void usage_stage(void);
111 __dead static void usage_unstage(void);
112 __dead static void usage_cat(void);
113 __dead static void usage_info(void);
115 static const struct got_error* cmd_init(int, char *[]);
116 static const struct got_error* cmd_import(int, char *[]);
117 static const struct got_error* cmd_clone(int, char *[]);
118 static const struct got_error* cmd_fetch(int, char *[]);
119 static const struct got_error* cmd_checkout(int, char *[]);
120 static const struct got_error* cmd_update(int, char *[]);
121 static const struct got_error* cmd_log(int, char *[]);
122 static const struct got_error* cmd_diff(int, char *[]);
123 static const struct got_error* cmd_blame(int, char *[]);
124 static const struct got_error* cmd_tree(int, char *[]);
125 static const struct got_error* cmd_status(int, char *[]);
126 static const struct got_error* cmd_ref(int, char *[]);
127 static const struct got_error* cmd_branch(int, char *[]);
128 static const struct got_error* cmd_tag(int, char *[]);
129 static const struct got_error* cmd_add(int, char *[]);
130 static const struct got_error* cmd_remove(int, char *[]);
131 static const struct got_error* cmd_revert(int, char *[]);
132 static const struct got_error* cmd_commit(int, char *[]);
133 static const struct got_error* cmd_cherrypick(int, char *[]);
134 static const struct got_error* cmd_backout(int, char *[]);
135 static const struct got_error* cmd_rebase(int, char *[]);
136 static const struct got_error* cmd_histedit(int, char *[]);
137 static const struct got_error* cmd_integrate(int, char *[]);
138 static const struct got_error* cmd_stage(int, char *[]);
139 static const struct got_error* cmd_unstage(int, char *[]);
140 static const struct got_error* cmd_cat(int, char *[]);
141 static const struct got_error* cmd_info(int, char *[]);
143 static struct got_cmd got_commands[] = {
144 { "init", cmd_init, usage_init, "" },
145 { "import", cmd_import, usage_import, "im" },
146 { "clone", cmd_clone, usage_clone, "cl" },
147 { "fetch", cmd_fetch, usage_fetch, "fe" },
148 { "checkout", cmd_checkout, usage_checkout, "co" },
149 { "update", cmd_update, usage_update, "up" },
150 { "log", cmd_log, usage_log, "" },
151 { "diff", cmd_diff, usage_diff, "di" },
152 { "blame", cmd_blame, usage_blame, "bl" },
153 { "tree", cmd_tree, usage_tree, "tr" },
154 { "status", cmd_status, usage_status, "st" },
155 { "ref", cmd_ref, usage_ref, "" },
156 { "branch", cmd_branch, usage_branch, "br" },
157 { "tag", cmd_tag, usage_tag, "" },
158 { "add", cmd_add, usage_add, "" },
159 { "remove", cmd_remove, usage_remove, "rm" },
160 { "revert", cmd_revert, usage_revert, "rv" },
161 { "commit", cmd_commit, usage_commit, "ci" },
162 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 { "backout", cmd_backout, usage_backout, "bo" },
164 { "rebase", cmd_rebase, usage_rebase, "rb" },
165 { "histedit", cmd_histedit, usage_histedit, "he" },
166 { "integrate", cmd_integrate, usage_integrate,"ig" },
167 { "stage", cmd_stage, usage_stage, "sg" },
168 { "unstage", cmd_unstage, usage_unstage, "ug" },
169 { "cat", cmd_cat, usage_cat, "" },
170 { "info", cmd_info, usage_info, "" },
174 list_commands(FILE *fp)
178 fprintf(fp, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(fp, " %s", cmd->cmd_name);
187 option_conflict(char a, char b)
189 errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 main(int argc, char *argv[])
198 int hflag = 0, Vflag = 0;
199 static struct option longopts[] = {
200 { "version", no_argument, NULL, 'V' },
204 setlocale(LC_CTYPE, "");
206 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
226 got_version_print_str();
231 usage(hflag, hflag ? 0 : 1);
233 signal(SIGINT, catch_sigint);
234 signal(SIGPIPE, catch_sigpipe);
236 for (i = 0; i < nitems(got_commands); i++) {
237 const struct got_error *error;
239 cmd = &got_commands[i];
241 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 strcmp(cmd->cmd_alias, argv[0]) != 0)
246 got_commands[i].cmd_usage();
248 error = got_commands[i].cmd_main(argc, argv);
249 if (error && error->code != GOT_ERR_CANCELLED &&
250 error->code != GOT_ERR_PRIVSEP_EXIT &&
251 !(sigpipe_received &&
252 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
254 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
262 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 list_commands(stderr);
268 usage(int hflag, int status)
270 FILE *fp = (status == 0) ? stdout : stderr;
272 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
279 static const struct got_error *
280 get_editor(char **abspath)
282 const struct got_error *err = NULL;
287 editor = getenv("VISUAL");
289 editor = getenv("EDITOR");
292 err = got_path_find_prog(abspath, editor);
297 if (*abspath == NULL) {
298 *abspath = strdup("/bin/ed");
299 if (*abspath == NULL)
300 return got_error_from_errno("strdup");
306 static const struct got_error *
307 apply_unveil(const char *repo_path, int repo_read_only,
308 const char *worktree_path)
310 const struct got_error *err;
313 if (unveil("gmon.out", "rwc") != 0)
314 return got_error_from_errno2("unveil", "gmon.out");
316 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 return got_error_from_errno2("unveil", repo_path);
319 if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 return got_error_from_errno2("unveil", worktree_path);
322 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 err = got_privsep_unveil_exec_helpers();
329 if (unveil(NULL, NULL) != 0)
330 return got_error_from_errno("unveil");
338 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 static const struct got_error *
343 cmd_init(int argc, char *argv[])
345 const struct got_error *error = NULL;
346 char *repo_path = NULL;
349 while ((ch = getopt(argc, argv, "")) != -1) {
361 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
367 repo_path = strdup(argv[0]);
368 if (repo_path == NULL)
369 return got_error_from_errno("strdup");
371 got_path_strip_trailing_slashes(repo_path);
373 error = got_path_mkdir(repo_path);
375 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 error = apply_unveil(repo_path, 0, NULL);
382 error = got_repo_init(repo_path);
391 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 "[-r repository-path] [-I pattern] path\n", getprogname());
397 spawn_editor(const char *editor, const char *file)
400 sig_t sighup, sigint, sigquit;
403 sighup = signal(SIGHUP, SIG_IGN);
404 sigint = signal(SIGINT, SIG_IGN);
405 sigquit = signal(SIGQUIT, SIG_IGN);
407 switch (pid = fork()) {
411 execl(editor, editor, file, (char *)NULL);
415 while (waitpid(pid, &st, 0) == -1)
420 (void)signal(SIGHUP, sighup);
421 (void)signal(SIGINT, sigint);
422 (void)signal(SIGQUIT, sigquit);
424 if (!WIFEXITED(st)) {
429 return WEXITSTATUS(st);
432 static const struct got_error *
433 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 const char *initial_content, size_t initial_content_len,
435 int require_modification)
437 const struct got_error *err = NULL;
443 size_t len, logmsg_len;
444 char *initial_content_stripped = NULL, *buf = NULL, *s;
448 if (stat(logmsg_path, &st) == -1)
449 return got_error_from_errno2("stat", logmsg_path);
451 if (spawn_editor(editor, logmsg_path) == -1)
452 return got_error_from_errno("failed spawning editor");
454 if (stat(logmsg_path, &st2) == -1)
455 return got_error_from_errno("stat");
457 if (require_modification &&
458 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
459 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 "no changes made to commit message, aborting");
463 * Set up a stripped version of the initial content without comments
464 * and blank lines. We need this in order to check if the message
465 * has in fact been edited.
467 initial_content_stripped = malloc(initial_content_len + 1);
468 if (initial_content_stripped == NULL)
469 return got_error_from_errno("malloc");
470 initial_content_stripped[0] = '\0';
472 buf = strdup(initial_content);
474 err = got_error_from_errno("strdup");
479 while ((line = strsep(&s, "\n")) != NULL) {
480 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
481 continue; /* remove comments and leading empty lines */
482 len = strlcat(initial_content_stripped, line,
483 initial_content_len + 1);
484 if (len >= initial_content_len + 1) {
485 err = got_error(GOT_ERR_NO_SPACE);
489 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
490 initial_content_stripped[len - 1] = '\0';
494 logmsg_len = st2.st_size;
495 *logmsg = malloc(logmsg_len + 1);
497 return got_error_from_errno("malloc");
500 fp = fopen(logmsg_path, "r");
502 err = got_error_from_errno("fopen");
507 while ((linelen = getline(&line, &linesize, fp)) != -1) {
508 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
509 continue; /* remove comments and leading empty lines */
510 len = strlcat(*logmsg, line, logmsg_len + 1);
511 if (len >= logmsg_len + 1) {
512 err = got_error(GOT_ERR_NO_SPACE);
518 err = got_ferror(fp, GOT_ERR_IO);
521 while (len > 0 && (*logmsg)[len - 1] == '\n') {
522 (*logmsg)[len - 1] = '\0';
527 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
528 "commit message cannot be empty, aborting");
531 if (require_modification &&
532 strcmp(*logmsg, initial_content_stripped) == 0)
533 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
534 "no changes made to commit message, aborting");
536 free(initial_content_stripped);
538 if (fp && fclose(fp) == EOF && err == NULL)
539 err = got_error_from_errno("fclose");
547 static const struct got_error *
548 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
549 const char *path_dir, const char *branch_name)
551 char *initial_content = NULL;
552 const struct got_error *err = NULL;
553 int initial_content_len;
556 initial_content_len = asprintf(&initial_content,
557 "\n# %s to be imported to branch %s\n", path_dir,
559 if (initial_content_len == -1)
560 return got_error_from_errno("asprintf");
562 err = got_opentemp_named_fd(logmsg_path, &fd,
563 GOT_TMPDIR_STR "/got-importmsg");
567 if (write(fd, initial_content, initial_content_len) == -1) {
568 err = got_error_from_errno2("write", *logmsg_path);
572 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
573 initial_content_len, 1);
575 if (fd != -1 && close(fd) == -1 && err == NULL)
576 err = got_error_from_errno2("close", *logmsg_path);
577 free(initial_content);
585 static const struct got_error *
586 import_progress(void *arg, const char *path)
588 printf("A %s\n", path);
592 static const struct got_error *
593 get_author(char **author, struct got_repository *repo,
594 struct got_worktree *worktree)
596 const struct got_error *err = NULL;
597 const char *got_author = NULL, *name, *email;
598 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
603 worktree_conf = got_worktree_get_gotconfig(worktree);
604 repo_conf = got_repo_get_gotconfig(repo);
607 * Priority of potential author information sources, from most
608 * significant to least significant:
609 * 1) work tree's .got/got.conf file
610 * 2) repository's got.conf file
611 * 3) repository's git config file
612 * 4) environment variables
613 * 5) global git config files (in user's home directory or /etc)
617 got_author = got_gotconfig_get_author(worktree_conf);
618 if (got_author == NULL)
619 got_author = got_gotconfig_get_author(repo_conf);
620 if (got_author == NULL) {
621 name = got_repo_get_gitconfig_author_name(repo);
622 email = got_repo_get_gitconfig_author_email(repo);
624 if (asprintf(author, "%s <%s>", name, email) == -1)
625 return got_error_from_errno("asprintf");
629 got_author = getenv("GOT_AUTHOR");
630 if (got_author == NULL) {
631 name = got_repo_get_global_gitconfig_author_name(repo);
632 email = got_repo_get_global_gitconfig_author_email(
635 if (asprintf(author, "%s <%s>", name, email)
637 return got_error_from_errno("asprintf");
640 /* TODO: Look up user in password database? */
641 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
645 *author = strdup(got_author);
647 return got_error_from_errno("strdup");
650 * Really dumb email address check; we're only doing this to
651 * avoid git's object parser breaking on commits we create.
653 while (*got_author && *got_author != '<')
655 if (*got_author != '<') {
656 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
659 while (*got_author && *got_author != '@')
661 if (*got_author != '@') {
662 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
665 while (*got_author && *got_author != '>')
667 if (*got_author != '>')
668 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
677 static const struct got_error *
678 get_gitconfig_path(char **gitconfig_path)
680 const char *homedir = getenv("HOME");
682 *gitconfig_path = NULL;
684 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
685 return got_error_from_errno("asprintf");
691 static const struct got_error *
692 cmd_import(int argc, char *argv[])
694 const struct got_error *error = NULL;
695 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
696 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
697 const char *branch_name = "main";
698 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
699 struct got_repository *repo = NULL;
700 struct got_reference *branch_ref = NULL, *head_ref = NULL;
701 struct got_object_id *new_commit_id = NULL;
703 struct got_pathlist_head ignores;
704 struct got_pathlist_entry *pe;
705 int preserve_logmsg = 0;
707 TAILQ_INIT(&ignores);
709 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
712 branch_name = optarg;
715 logmsg = strdup(optarg);
716 if (logmsg == NULL) {
717 error = got_error_from_errno("strdup");
722 repo_path = realpath(optarg, NULL);
723 if (repo_path == NULL) {
724 error = got_error_from_errno2("realpath",
730 if (optarg[0] == '\0')
732 error = got_pathlist_insert(&pe, &ignores, optarg,
747 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
755 if (repo_path == NULL) {
756 repo_path = getcwd(NULL, 0);
757 if (repo_path == NULL)
758 return got_error_from_errno("getcwd");
760 got_path_strip_trailing_slashes(repo_path);
761 error = get_gitconfig_path(&gitconfig_path);
764 error = got_repo_open(&repo, repo_path, gitconfig_path);
768 error = get_author(&author, repo, NULL);
773 * Don't let the user create a branch name with a leading '-'.
774 * While technically a valid reference name, this case is usually
775 * an unintended typo.
777 if (branch_name[0] == '-')
778 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
780 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
781 error = got_error_from_errno("asprintf");
785 error = got_ref_open(&branch_ref, repo, refname, 0);
787 if (error->code != GOT_ERR_NOT_REF)
790 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
791 "import target branch already exists");
795 path_dir = realpath(argv[0], NULL);
796 if (path_dir == NULL) {
797 error = got_error_from_errno2("realpath", argv[0]);
800 got_path_strip_trailing_slashes(path_dir);
803 * unveil(2) traverses exec(2); if an editor is used we have
804 * to apply unveil after the log message has been written.
806 if (logmsg == NULL || strlen(logmsg) == 0) {
807 error = get_editor(&editor);
811 error = collect_import_msg(&logmsg, &logmsg_path, editor,
814 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
821 if (unveil(path_dir, "r") != 0) {
822 error = got_error_from_errno2("unveil", path_dir);
828 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
835 error = got_repo_import(&new_commit_id, path_dir, logmsg,
836 author, &ignores, repo, import_progress, NULL);
843 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
850 error = got_ref_write(branch_ref, repo);
857 error = got_object_id_str(&id_str, new_commit_id);
864 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
866 if (error->code != GOT_ERR_NOT_REF) {
872 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
880 error = got_ref_write(head_ref, repo);
888 printf("Created branch %s with commit %s\n",
889 got_ref_get_name(branch_ref), id_str);
891 if (preserve_logmsg) {
892 fprintf(stderr, "%s: log message preserved in %s\n",
893 getprogname(), logmsg_path);
894 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
895 error = got_error_from_errno2("unlink", logmsg_path);
904 free(gitconfig_path);
906 got_ref_close(branch_ref);
908 got_ref_close(head_ref);
915 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
916 "[-R reference] repository-url [directory]\n", getprogname());
920 struct got_fetch_progress_arg {
921 char last_scaled_size[FMT_SCALED_STRSIZE];
926 struct got_repository *repo;
931 struct got_pathlist_head *symrefs;
932 struct got_pathlist_head *wanted_branches;
933 struct got_pathlist_head *wanted_refs;
937 const char *remote_repo_path;
939 int fetch_all_branches;
940 int mirror_references;
944 /* XXX forward declaration */
945 static const struct got_error *
946 create_config_files(const char *proto, const char *host, const char *port,
947 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
948 int mirror_references, struct got_pathlist_head *symrefs,
949 struct got_pathlist_head *wanted_branches,
950 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
952 static const struct got_error *
953 fetch_progress(void *arg, const char *message, off_t packfile_size,
954 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
956 const struct got_error *err = NULL;
957 struct got_fetch_progress_arg *a = arg;
958 char scaled_size[FMT_SCALED_STRSIZE];
959 int p_indexed, p_resolved;
960 int print_size = 0, print_indexed = 0, print_resolved = 0;
963 * In order to allow a failed clone to be resumed with 'got fetch'
964 * we try to create configuration files as soon as possible.
965 * Once the server has sent information about its default branch
966 * we have all required information.
968 if (a->create_configs && !a->configs_created &&
969 !TAILQ_EMPTY(a->config_info.symrefs)) {
970 err = create_config_files(a->config_info.proto,
971 a->config_info.host, a->config_info.port,
972 a->config_info.remote_repo_path,
973 a->config_info.git_url,
974 a->config_info.fetch_all_branches,
975 a->config_info.mirror_references,
976 a->config_info.symrefs,
977 a->config_info.wanted_branches,
978 a->config_info.wanted_refs, a->repo);
981 a->configs_created = 1;
984 if (a->verbosity < 0)
987 if (message && message[0] != '\0') {
988 printf("\rserver: %s", message);
993 if (packfile_size > 0 || nobj_indexed > 0) {
994 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
995 (a->last_scaled_size[0] == '\0' ||
996 strcmp(scaled_size, a->last_scaled_size)) != 0) {
998 if (strlcpy(a->last_scaled_size, scaled_size,
999 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1000 return got_error(GOT_ERR_NO_SPACE);
1002 if (nobj_indexed > 0) {
1003 p_indexed = (nobj_indexed * 100) / nobj_total;
1004 if (p_indexed != a->last_p_indexed) {
1005 a->last_p_indexed = p_indexed;
1010 if (nobj_resolved > 0) {
1011 p_resolved = (nobj_resolved * 100) /
1012 (nobj_total - nobj_loose);
1013 if (p_resolved != a->last_p_resolved) {
1014 a->last_p_resolved = p_resolved;
1022 if (print_size || print_indexed || print_resolved)
1025 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1027 printf("; indexing %d%%", p_indexed);
1029 printf("; resolving deltas %d%%", p_resolved);
1030 if (print_size || print_indexed || print_resolved)
1036 static const struct got_error *
1037 create_symref(const char *refname, struct got_reference *target_ref,
1038 int verbosity, struct got_repository *repo)
1040 const struct got_error *err;
1041 struct got_reference *head_symref;
1043 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1047 err = got_ref_write(head_symref, repo);
1048 if (err == NULL && verbosity > 0) {
1049 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1050 got_ref_get_name(target_ref));
1052 got_ref_close(head_symref);
1056 static const struct got_error *
1057 list_remote_refs(struct got_pathlist_head *symrefs,
1058 struct got_pathlist_head *refs)
1060 const struct got_error *err;
1061 struct got_pathlist_entry *pe;
1063 TAILQ_FOREACH(pe, symrefs, entry) {
1064 const char *refname = pe->path;
1065 const char *targetref = pe->data;
1067 printf("%s: %s\n", refname, targetref);
1070 TAILQ_FOREACH(pe, refs, entry) {
1071 const char *refname = pe->path;
1072 struct got_object_id *id = pe->data;
1075 err = got_object_id_str(&id_str, id);
1078 printf("%s: %s\n", refname, id_str);
1085 static const struct got_error *
1086 create_ref(const char *refname, struct got_object_id *id,
1087 int verbosity, struct got_repository *repo)
1089 const struct got_error *err = NULL;
1090 struct got_reference *ref;
1093 err = got_object_id_str(&id_str, id);
1097 err = got_ref_alloc(&ref, refname, id);
1101 err = got_ref_write(ref, repo);
1104 if (err == NULL && verbosity >= 0)
1105 printf("Created reference %s: %s\n", refname, id_str);
1112 match_wanted_ref(const char *refname, const char *wanted_ref)
1114 if (strncmp(refname, "refs/", 5) != 0)
1119 * Prevent fetching of references that won't make any
1120 * sense outside of the remote repository's context.
1122 if (strncmp(refname, "got/", 4) == 0)
1124 if (strncmp(refname, "remotes/", 8) == 0)
1127 if (strncmp(wanted_ref, "refs/", 5) == 0)
1130 /* Allow prefix match. */
1131 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1134 /* Allow exact match. */
1135 return (strcmp(refname, wanted_ref) == 0);
1139 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1141 struct got_pathlist_entry *pe;
1143 TAILQ_FOREACH(pe, wanted_refs, entry) {
1144 if (match_wanted_ref(refname, pe->path))
1151 static const struct got_error *
1152 create_wanted_ref(const char *refname, struct got_object_id *id,
1153 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1155 const struct got_error *err;
1156 char *remote_refname;
1158 if (strncmp("refs/", refname, 5) == 0)
1161 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1162 remote_repo_name, refname) == -1)
1163 return got_error_from_errno("asprintf");
1165 err = create_ref(remote_refname, id, verbosity, repo);
1166 free(remote_refname);
1170 static const struct got_error *
1171 create_gotconfig(const char *proto, const char *host, const char *port,
1172 const char *remote_repo_path, const char *default_branch,
1173 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1174 struct got_pathlist_head *wanted_refs, int mirror_references,
1175 struct got_repository *repo)
1177 const struct got_error *err = NULL;
1178 char *gotconfig_path = NULL;
1179 char *gotconfig = NULL;
1180 FILE *gotconfig_file = NULL;
1181 const char *branchname = NULL;
1182 char *branches = NULL, *refs = NULL;
1185 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1186 struct got_pathlist_entry *pe;
1187 TAILQ_FOREACH(pe, wanted_branches, entry) {
1189 branchname = pe->path;
1190 if (strncmp(branchname, "refs/heads/", 11) == 0)
1192 if (asprintf(&s, "%s\"%s\" ",
1193 branches ? branches : "", branchname) == -1) {
1194 err = got_error_from_errno("asprintf");
1200 } else if (!fetch_all_branches && default_branch) {
1201 branchname = default_branch;
1202 if (strncmp(branchname, "refs/heads/", 11) == 0)
1204 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1205 err = got_error_from_errno("asprintf");
1209 if (!TAILQ_EMPTY(wanted_refs)) {
1210 struct got_pathlist_entry *pe;
1211 TAILQ_FOREACH(pe, wanted_refs, entry) {
1213 const char *refname = pe->path;
1214 if (strncmp(refname, "refs/", 5) == 0)
1216 if (asprintf(&s, "%s\"%s\" ",
1217 refs ? refs : "", refname) == -1) {
1218 err = got_error_from_errno("asprintf");
1226 /* Create got.conf(5). */
1227 gotconfig_path = got_repo_get_path_gotconfig(repo);
1228 if (gotconfig_path == NULL) {
1229 err = got_error_from_errno("got_repo_get_path_gotconfig");
1232 gotconfig_file = fopen(gotconfig_path, "a");
1233 if (gotconfig_file == NULL) {
1234 err = got_error_from_errno2("fopen", gotconfig_path);
1237 if (asprintf(&gotconfig,
1242 "\trepository \"%s\"\n"
1248 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1249 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1250 remote_repo_path, branches ? "\tbranch { " : "",
1251 branches ? branches : "", branches ? "}\n" : "",
1252 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1253 mirror_references ? "\tmirror-references yes\n" : "",
1254 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1255 err = got_error_from_errno("asprintf");
1258 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1259 if (n != strlen(gotconfig)) {
1260 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1265 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1266 err = got_error_from_errno2("fclose", gotconfig_path);
1267 free(gotconfig_path);
1272 static const struct got_error *
1273 create_gitconfig(const char *git_url, const char *default_branch,
1274 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1275 struct got_pathlist_head *wanted_refs, int mirror_references,
1276 struct got_repository *repo)
1278 const struct got_error *err = NULL;
1279 char *gitconfig_path = NULL;
1280 char *gitconfig = NULL;
1281 FILE *gitconfig_file = NULL;
1282 char *branches = NULL, *refs = NULL;
1283 const char *branchname;
1286 /* Create a config file Git can understand. */
1287 gitconfig_path = got_repo_get_path_gitconfig(repo);
1288 if (gitconfig_path == NULL) {
1289 err = got_error_from_errno("got_repo_get_path_gitconfig");
1292 gitconfig_file = fopen(gitconfig_path, "a");
1293 if (gitconfig_file == NULL) {
1294 err = got_error_from_errno2("fopen", gitconfig_path);
1297 if (fetch_all_branches) {
1298 if (mirror_references) {
1299 if (asprintf(&branches,
1300 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1301 err = got_error_from_errno("asprintf");
1304 } else if (asprintf(&branches,
1305 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1306 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1307 err = got_error_from_errno("asprintf");
1310 } else if (!TAILQ_EMPTY(wanted_branches)) {
1311 struct got_pathlist_entry *pe;
1312 TAILQ_FOREACH(pe, wanted_branches, entry) {
1314 branchname = pe->path;
1315 if (strncmp(branchname, "refs/heads/", 11) == 0)
1317 if (mirror_references) {
1319 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1320 branches ? branches : "",
1321 branchname, branchname) == -1) {
1322 err = got_error_from_errno("asprintf");
1325 } else if (asprintf(&s,
1326 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1327 branches ? branches : "",
1328 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1329 branchname) == -1) {
1330 err = got_error_from_errno("asprintf");
1338 * If the server specified a default branch, use just that one.
1339 * Otherwise fall back to fetching all branches on next fetch.
1341 if (default_branch) {
1342 branchname = default_branch;
1343 if (strncmp(branchname, "refs/heads/", 11) == 0)
1346 branchname = "*"; /* fall back to all branches */
1347 if (mirror_references) {
1348 if (asprintf(&branches,
1349 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1350 branchname, branchname) == -1) {
1351 err = got_error_from_errno("asprintf");
1354 } else if (asprintf(&branches,
1355 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1356 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1357 branchname) == -1) {
1358 err = got_error_from_errno("asprintf");
1362 if (!TAILQ_EMPTY(wanted_refs)) {
1363 struct got_pathlist_entry *pe;
1364 TAILQ_FOREACH(pe, wanted_refs, entry) {
1366 const char *refname = pe->path;
1367 if (strncmp(refname, "refs/", 5) == 0)
1369 if (mirror_references) {
1371 "%s\tfetch = refs/%s:refs/%s\n",
1372 refs ? refs : "", refname, refname) == -1) {
1373 err = got_error_from_errno("asprintf");
1376 } else if (asprintf(&s,
1377 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1379 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1381 err = got_error_from_errno("asprintf");
1389 if (asprintf(&gitconfig,
1394 "\tfetch = refs/tags/*:refs/tags/*\n",
1395 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1396 refs ? refs : "") == -1) {
1397 err = got_error_from_errno("asprintf");
1400 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1401 if (n != strlen(gitconfig)) {
1402 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1406 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1407 err = got_error_from_errno2("fclose", gitconfig_path);
1408 free(gitconfig_path);
1413 static const struct got_error *
1414 create_config_files(const char *proto, const char *host, const char *port,
1415 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1416 int mirror_references, struct got_pathlist_head *symrefs,
1417 struct got_pathlist_head *wanted_branches,
1418 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1420 const struct got_error *err = NULL;
1421 const char *default_branch = NULL;
1422 struct got_pathlist_entry *pe;
1425 * If we asked for a set of wanted branches then use the first
1428 if (!TAILQ_EMPTY(wanted_branches)) {
1429 pe = TAILQ_FIRST(wanted_branches);
1430 default_branch = pe->path;
1432 /* First HEAD ref listed by server is the default branch. */
1433 TAILQ_FOREACH(pe, symrefs, entry) {
1434 const char *refname = pe->path;
1435 const char *target = pe->data;
1437 if (strcmp(refname, GOT_REF_HEAD) != 0)
1440 default_branch = target;
1445 /* Create got.conf(5). */
1446 err = create_gotconfig(proto, host, port, remote_repo_path,
1447 default_branch, fetch_all_branches, wanted_branches,
1448 wanted_refs, mirror_references, repo);
1452 /* Create a config file Git can understand. */
1453 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1454 wanted_branches, wanted_refs, mirror_references, repo);
1457 static const struct got_error *
1458 cmd_clone(int argc, char *argv[])
1460 const struct got_error *error = NULL;
1461 const char *uri, *dirname;
1462 char *proto, *host, *port, *repo_name, *server_path;
1463 char *default_destdir = NULL, *id_str = NULL;
1464 const char *repo_path;
1465 struct got_repository *repo = NULL;
1466 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1467 struct got_pathlist_entry *pe;
1468 struct got_object_id *pack_hash = NULL;
1469 int ch, fetchfd = -1, fetchstatus;
1470 pid_t fetchpid = -1;
1471 struct got_fetch_progress_arg fpa;
1472 char *git_url = NULL;
1473 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1474 int list_refs_only = 0;
1477 TAILQ_INIT(&symrefs);
1478 TAILQ_INIT(&wanted_branches);
1479 TAILQ_INIT(&wanted_refs);
1481 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1484 fetch_all_branches = 1;
1487 error = got_pathlist_append(&wanted_branches,
1496 mirror_references = 1;
1501 else if (verbosity < 3)
1508 error = got_pathlist_append(&wanted_refs,
1521 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1522 option_conflict('a', 'b');
1523 if (list_refs_only) {
1524 if (!TAILQ_EMPTY(&wanted_branches))
1525 option_conflict('l', 'b');
1526 if (fetch_all_branches)
1527 option_conflict('l', 'a');
1528 if (mirror_references)
1529 option_conflict('l', 'm');
1530 if (!TAILQ_EMPTY(&wanted_refs))
1531 option_conflict('l', 'R');
1543 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1548 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1549 host, port ? ":" : "", port ? port : "",
1550 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1551 error = got_error_from_errno("asprintf");
1555 if (strcmp(proto, "git") == 0) {
1557 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1558 "sendfd dns inet unveil", NULL) == -1)
1561 } else if (strcmp(proto, "git+ssh") == 0 ||
1562 strcmp(proto, "ssh") == 0) {
1564 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 "sendfd unveil", NULL) == -1)
1568 } else if (strcmp(proto, "http") == 0 ||
1569 strcmp(proto, "git+http") == 0) {
1570 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1573 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1576 if (dirname == NULL) {
1577 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1578 error = got_error_from_errno("asprintf");
1581 repo_path = default_destdir;
1583 repo_path = dirname;
1585 if (!list_refs_only) {
1586 error = got_path_mkdir(repo_path);
1588 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1589 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1591 if (!got_path_dir_is_empty(repo_path)) {
1592 error = got_error_path(repo_path,
1593 GOT_ERR_DIR_NOT_EMPTY);
1598 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1599 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1600 error = got_error_from_errno2("unveil",
1601 GOT_FETCH_PATH_SSH);
1605 error = apply_unveil(repo_path, 0, NULL);
1610 printf("Connecting to %s%s%s\n", host,
1611 port ? ":" : "", port ? port : "");
1613 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1614 server_path, verbosity);
1618 if (!list_refs_only) {
1619 error = got_repo_init(repo_path);
1622 error = got_repo_open(&repo, repo_path, NULL);
1627 fpa.last_scaled_size[0] = '\0';
1628 fpa.last_p_indexed = -1;
1629 fpa.last_p_resolved = -1;
1630 fpa.verbosity = verbosity;
1631 fpa.create_configs = 1;
1632 fpa.configs_created = 0;
1634 fpa.config_info.symrefs = &symrefs;
1635 fpa.config_info.wanted_branches = &wanted_branches;
1636 fpa.config_info.wanted_refs = &wanted_refs;
1637 fpa.config_info.proto = proto;
1638 fpa.config_info.host = host;
1639 fpa.config_info.port = port;
1640 fpa.config_info.remote_repo_path = server_path;
1641 fpa.config_info.git_url = git_url;
1642 fpa.config_info.fetch_all_branches = fetch_all_branches;
1643 fpa.config_info.mirror_references = mirror_references;
1644 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1645 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1646 fetch_all_branches, &wanted_branches, &wanted_refs,
1647 list_refs_only, verbosity, fetchfd, repo,
1648 fetch_progress, &fpa);
1652 if (list_refs_only) {
1653 error = list_remote_refs(&symrefs, &refs);
1657 error = got_object_id_str(&id_str, pack_hash);
1661 printf("\nFetched %s.pack\n", id_str);
1664 /* Set up references provided with the pack file. */
1665 TAILQ_FOREACH(pe, &refs, entry) {
1666 const char *refname = pe->path;
1667 struct got_object_id *id = pe->data;
1668 char *remote_refname;
1670 if (is_wanted_ref(&wanted_refs, refname) &&
1671 !mirror_references) {
1672 error = create_wanted_ref(refname, id,
1673 GOT_FETCH_DEFAULT_REMOTE_NAME,
1674 verbosity - 1, repo);
1680 error = create_ref(refname, id, verbosity - 1, repo);
1684 if (mirror_references)
1687 if (strncmp("refs/heads/", refname, 11) != 0)
1690 if (asprintf(&remote_refname,
1691 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1692 refname + 11) == -1) {
1693 error = got_error_from_errno("asprintf");
1696 error = create_ref(remote_refname, id, verbosity - 1, repo);
1697 free(remote_refname);
1702 /* Set the HEAD reference if the server provided one. */
1703 TAILQ_FOREACH(pe, &symrefs, entry) {
1704 struct got_reference *target_ref;
1705 const char *refname = pe->path;
1706 const char *target = pe->data;
1707 char *remote_refname = NULL, *remote_target = NULL;
1709 if (strcmp(refname, GOT_REF_HEAD) != 0)
1712 error = got_ref_open(&target_ref, repo, target, 0);
1714 if (error->code == GOT_ERR_NOT_REF) {
1721 error = create_symref(refname, target_ref, verbosity, repo);
1722 got_ref_close(target_ref);
1726 if (mirror_references)
1729 if (strncmp("refs/heads/", target, 11) != 0)
1732 if (asprintf(&remote_refname,
1733 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1735 error = got_error_from_errno("asprintf");
1738 if (asprintf(&remote_target,
1739 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1740 target + 11) == -1) {
1741 error = got_error_from_errno("asprintf");
1742 free(remote_refname);
1745 error = got_ref_open(&target_ref, repo, remote_target, 0);
1747 free(remote_refname);
1748 free(remote_target);
1749 if (error->code == GOT_ERR_NOT_REF) {
1755 error = create_symref(remote_refname, target_ref,
1756 verbosity - 1, repo);
1757 free(remote_refname);
1758 free(remote_target);
1759 got_ref_close(target_ref);
1765 * We failed to set the HEAD reference. If we asked for
1766 * a set of wanted branches use the first of one of those
1767 * which could be fetched instead.
1769 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1770 const char *target = pe->path;
1771 struct got_reference *target_ref;
1773 error = got_ref_open(&target_ref, repo, target, 0);
1775 if (error->code == GOT_ERR_NOT_REF) {
1782 error = create_symref(GOT_REF_HEAD, target_ref,
1784 got_ref_close(target_ref);
1792 printf("Created %s repository '%s'\n",
1793 mirror_references ? "mirrored" : "cloned", repo_path);
1796 if (kill(fetchpid, SIGTERM) == -1)
1797 error = got_error_from_errno("kill");
1798 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1799 error = got_error_from_errno("waitpid");
1801 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1802 error = got_error_from_errno("close");
1804 const struct got_error *close_err = got_repo_close(repo);
1808 TAILQ_FOREACH(pe, &refs, entry) {
1809 free((void *)pe->path);
1812 got_pathlist_free(&refs);
1813 TAILQ_FOREACH(pe, &symrefs, entry) {
1814 free((void *)pe->path);
1817 got_pathlist_free(&symrefs);
1818 got_pathlist_free(&wanted_branches);
1819 got_pathlist_free(&wanted_refs);
1826 free(default_destdir);
1831 static const struct got_error *
1832 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1833 int replace_tags, int verbosity, struct got_repository *repo)
1835 const struct got_error *err = NULL;
1836 char *new_id_str = NULL;
1837 struct got_object_id *old_id = NULL;
1839 err = got_object_id_str(&new_id_str, new_id);
1843 if (!replace_tags &&
1844 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1845 err = got_ref_resolve(&old_id, repo, ref);
1848 if (got_object_id_cmp(old_id, new_id) == 0)
1850 if (verbosity >= 0) {
1851 printf("Rejecting update of existing tag %s: %s\n",
1852 got_ref_get_name(ref), new_id_str);
1857 if (got_ref_is_symbolic(ref)) {
1858 if (verbosity >= 0) {
1859 printf("Replacing reference %s: %s\n",
1860 got_ref_get_name(ref),
1861 got_ref_get_symref_target(ref));
1863 err = got_ref_change_symref_to_ref(ref, new_id);
1866 err = got_ref_write(ref, repo);
1870 err = got_ref_resolve(&old_id, repo, ref);
1873 if (got_object_id_cmp(old_id, new_id) == 0)
1876 err = got_ref_change_ref(ref, new_id);
1879 err = got_ref_write(ref, repo);
1885 printf("Updated %s: %s\n", got_ref_get_name(ref),
1893 static const struct got_error *
1894 update_symref(const char *refname, struct got_reference *target_ref,
1895 int verbosity, struct got_repository *repo)
1897 const struct got_error *err = NULL, *unlock_err;
1898 struct got_reference *symref;
1899 int symref_is_locked = 0;
1901 err = got_ref_open(&symref, repo, refname, 1);
1903 if (err->code != GOT_ERR_NOT_REF)
1905 err = got_ref_alloc_symref(&symref, refname, target_ref);
1909 err = got_ref_write(symref, repo);
1914 printf("Created reference %s: %s\n",
1915 got_ref_get_name(symref),
1916 got_ref_get_symref_target(symref));
1918 symref_is_locked = 1;
1920 if (strcmp(got_ref_get_symref_target(symref),
1921 got_ref_get_name(target_ref)) == 0)
1924 err = got_ref_change_symref(symref,
1925 got_ref_get_name(target_ref));
1929 err = got_ref_write(symref, repo);
1934 printf("Updated %s: %s\n", got_ref_get_name(symref),
1935 got_ref_get_symref_target(symref));
1939 if (symref_is_locked) {
1940 unlock_err = got_ref_unlock(symref);
1941 if (unlock_err && err == NULL)
1944 got_ref_close(symref);
1951 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1952 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1953 "[remote-repository-name]\n",
1958 static const struct got_error *
1959 delete_missing_ref(struct got_reference *ref,
1960 int verbosity, struct got_repository *repo)
1962 const struct got_error *err = NULL;
1963 struct got_object_id *id = NULL;
1964 char *id_str = NULL;
1966 if (got_ref_is_symbolic(ref)) {
1967 err = got_ref_delete(ref, repo);
1970 if (verbosity >= 0) {
1971 printf("Deleted reference %s: %s\n",
1972 got_ref_get_name(ref),
1973 got_ref_get_symref_target(ref));
1976 err = got_ref_resolve(&id, repo, ref);
1979 err = got_object_id_str(&id_str, id);
1983 err = got_ref_delete(ref, repo);
1986 if (verbosity >= 0) {
1987 printf("Deleted reference %s: %s\n",
1988 got_ref_get_name(ref), id_str);
1997 static const struct got_error *
1998 delete_missing_refs(struct got_pathlist_head *their_refs,
1999 struct got_pathlist_head *their_symrefs,
2000 const struct got_remote_repo *remote,
2001 int verbosity, struct got_repository *repo)
2003 const struct got_error *err = NULL, *unlock_err;
2004 struct got_reflist_head my_refs;
2005 struct got_reflist_entry *re;
2006 struct got_pathlist_entry *pe;
2007 char *remote_namespace = NULL;
2008 char *local_refname = NULL;
2010 TAILQ_INIT(&my_refs);
2012 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2014 return got_error_from_errno("asprintf");
2016 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2020 TAILQ_FOREACH(re, &my_refs, entry) {
2021 const char *refname = got_ref_get_name(re->ref);
2023 if (!remote->mirror_references) {
2024 if (strncmp(refname, remote_namespace,
2025 strlen(remote_namespace)) == 0) {
2026 if (strcmp(refname + strlen(remote_namespace),
2029 if (asprintf(&local_refname, "refs/heads/%s",
2030 refname + strlen(remote_namespace)) == -1) {
2031 err = got_error_from_errno("asprintf");
2034 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2038 TAILQ_FOREACH(pe, their_refs, entry) {
2039 if (strcmp(local_refname, pe->path) == 0)
2045 TAILQ_FOREACH(pe, their_symrefs, entry) {
2046 if (strcmp(local_refname, pe->path) == 0)
2052 err = delete_missing_ref(re->ref, verbosity, repo);
2056 if (local_refname) {
2057 struct got_reference *ref;
2058 err = got_ref_open(&ref, repo, local_refname, 1);
2060 if (err->code != GOT_ERR_NOT_REF)
2062 free(local_refname);
2063 local_refname = NULL;
2066 err = delete_missing_ref(ref, verbosity, repo);
2069 unlock_err = got_ref_unlock(ref);
2071 if (unlock_err && err == NULL) {
2076 free(local_refname);
2077 local_refname = NULL;
2081 free(remote_namespace);
2082 free(local_refname);
2086 static const struct got_error *
2087 update_wanted_ref(const char *refname, struct got_object_id *id,
2088 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2090 const struct got_error *err, *unlock_err;
2091 char *remote_refname;
2092 struct got_reference *ref;
2094 if (strncmp("refs/", refname, 5) == 0)
2097 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2098 remote_repo_name, refname) == -1)
2099 return got_error_from_errno("asprintf");
2101 err = got_ref_open(&ref, repo, remote_refname, 1);
2103 if (err->code != GOT_ERR_NOT_REF)
2105 err = create_ref(remote_refname, id, verbosity, repo);
2107 err = update_ref(ref, id, 0, verbosity, repo);
2108 unlock_err = got_ref_unlock(ref);
2109 if (unlock_err && err == NULL)
2114 free(remote_refname);
2118 static const struct got_error *
2119 cmd_fetch(int argc, char *argv[])
2121 const struct got_error *error = NULL, *unlock_err;
2122 char *cwd = NULL, *repo_path = NULL;
2123 const char *remote_name;
2124 char *proto = NULL, *host = NULL, *port = NULL;
2125 char *repo_name = NULL, *server_path = NULL;
2126 const struct got_remote_repo *remotes, *remote = NULL;
2128 char *id_str = NULL;
2129 struct got_repository *repo = NULL;
2130 struct got_worktree *worktree = NULL;
2131 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2132 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2133 struct got_pathlist_entry *pe;
2134 struct got_object_id *pack_hash = NULL;
2135 int i, ch, fetchfd = -1, fetchstatus;
2136 pid_t fetchpid = -1;
2137 struct got_fetch_progress_arg fpa;
2138 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2139 int delete_refs = 0, replace_tags = 0;
2142 TAILQ_INIT(&symrefs);
2143 TAILQ_INIT(&wanted_branches);
2144 TAILQ_INIT(&wanted_refs);
2146 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2149 fetch_all_branches = 1;
2152 error = got_pathlist_append(&wanted_branches,
2164 repo_path = realpath(optarg, NULL);
2165 if (repo_path == NULL)
2166 return got_error_from_errno2("realpath",
2168 got_path_strip_trailing_slashes(repo_path);
2176 else if (verbosity < 3)
2183 error = got_pathlist_append(&wanted_refs,
2196 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2197 option_conflict('a', 'b');
2198 if (list_refs_only) {
2199 if (!TAILQ_EMPTY(&wanted_branches))
2200 option_conflict('l', 'b');
2201 if (fetch_all_branches)
2202 option_conflict('l', 'a');
2204 option_conflict('l', 'd');
2208 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2210 remote_name = argv[0];
2214 cwd = getcwd(NULL, 0);
2216 error = got_error_from_errno("getcwd");
2220 if (repo_path == NULL) {
2221 error = got_worktree_open(&worktree, cwd);
2222 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2228 strdup(got_worktree_get_repo_path(worktree));
2229 if (repo_path == NULL)
2230 error = got_error_from_errno("strdup");
2234 repo_path = strdup(cwd);
2235 if (repo_path == NULL) {
2236 error = got_error_from_errno("strdup");
2242 error = got_repo_open(&repo, repo_path, NULL);
2247 worktree_conf = got_worktree_get_gotconfig(worktree);
2248 if (worktree_conf) {
2249 got_gotconfig_get_remotes(&nremotes, &remotes,
2251 for (i = 0; i < nremotes; i++) {
2252 if (strcmp(remotes[i].name, remote_name) == 0) {
2253 remote = &remotes[i];
2259 if (remote == NULL) {
2260 repo_conf = got_repo_get_gotconfig(repo);
2262 got_gotconfig_get_remotes(&nremotes, &remotes,
2264 for (i = 0; i < nremotes; i++) {
2265 if (strcmp(remotes[i].name, remote_name) == 0) {
2266 remote = &remotes[i];
2272 if (remote == NULL) {
2273 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2274 for (i = 0; i < nremotes; i++) {
2275 if (strcmp(remotes[i].name, remote_name) == 0) {
2276 remote = &remotes[i];
2281 if (remote == NULL) {
2282 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2286 if (TAILQ_EMPTY(&wanted_branches)) {
2287 if (!fetch_all_branches)
2288 fetch_all_branches = remote->fetch_all_branches;
2289 for (i = 0; i < remote->nbranches; i++) {
2290 got_pathlist_append(&wanted_branches,
2291 remote->branches[i], NULL);
2294 if (TAILQ_EMPTY(&wanted_refs)) {
2295 for (i = 0; i < remote->nrefs; i++) {
2296 got_pathlist_append(&wanted_refs,
2297 remote->refs[i], NULL);
2301 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2302 &repo_name, remote->url);
2306 if (strcmp(proto, "git") == 0) {
2308 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2309 "sendfd dns inet unveil", NULL) == -1)
2312 } else if (strcmp(proto, "git+ssh") == 0 ||
2313 strcmp(proto, "ssh") == 0) {
2315 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2316 "sendfd unveil", NULL) == -1)
2319 } else if (strcmp(proto, "http") == 0 ||
2320 strcmp(proto, "git+http") == 0) {
2321 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2324 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2328 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2329 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2330 error = got_error_from_errno2("unveil",
2331 GOT_FETCH_PATH_SSH);
2335 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2340 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2341 port ? ":" : "", port ? port : "");
2343 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2344 server_path, verbosity);
2348 fpa.last_scaled_size[0] = '\0';
2349 fpa.last_p_indexed = -1;
2350 fpa.last_p_resolved = -1;
2351 fpa.verbosity = verbosity;
2353 fpa.create_configs = 0;
2354 fpa.configs_created = 0;
2355 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2356 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2357 remote->mirror_references, fetch_all_branches, &wanted_branches,
2358 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2359 fetch_progress, &fpa);
2363 if (list_refs_only) {
2364 error = list_remote_refs(&symrefs, &refs);
2368 if (pack_hash == NULL) {
2370 printf("Already up-to-date\n");
2371 } else if (verbosity >= 0) {
2372 error = got_object_id_str(&id_str, pack_hash);
2375 printf("\nFetched %s.pack\n", id_str);
2380 /* Update references provided with the pack file. */
2381 TAILQ_FOREACH(pe, &refs, entry) {
2382 const char *refname = pe->path;
2383 struct got_object_id *id = pe->data;
2384 struct got_reference *ref;
2385 char *remote_refname;
2387 if (is_wanted_ref(&wanted_refs, refname) &&
2388 !remote->mirror_references) {
2389 error = update_wanted_ref(refname, id,
2390 remote->name, verbosity, repo);
2396 if (remote->mirror_references ||
2397 strncmp("refs/tags/", refname, 10) == 0) {
2398 error = got_ref_open(&ref, repo, refname, 1);
2400 if (error->code != GOT_ERR_NOT_REF)
2402 error = create_ref(refname, id, verbosity,
2407 error = update_ref(ref, id, replace_tags,
2409 unlock_err = got_ref_unlock(ref);
2410 if (unlock_err && error == NULL)
2416 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2417 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2418 remote_name, refname + 11) == -1) {
2419 error = got_error_from_errno("asprintf");
2423 error = got_ref_open(&ref, repo, remote_refname, 1);
2425 if (error->code != GOT_ERR_NOT_REF)
2427 error = create_ref(remote_refname, id,
2432 error = update_ref(ref, id, replace_tags,
2434 unlock_err = got_ref_unlock(ref);
2435 if (unlock_err && error == NULL)
2442 /* Also create a local branch if none exists yet. */
2443 error = got_ref_open(&ref, repo, refname, 1);
2445 if (error->code != GOT_ERR_NOT_REF)
2447 error = create_ref(refname, id, verbosity,
2452 unlock_err = got_ref_unlock(ref);
2453 if (unlock_err && error == NULL)
2460 error = delete_missing_refs(&refs, &symrefs, remote,
2466 if (!remote->mirror_references) {
2467 /* Update remote HEAD reference if the server provided one. */
2468 TAILQ_FOREACH(pe, &symrefs, entry) {
2469 struct got_reference *target_ref;
2470 const char *refname = pe->path;
2471 const char *target = pe->data;
2472 char *remote_refname = NULL, *remote_target = NULL;
2474 if (strcmp(refname, GOT_REF_HEAD) != 0)
2477 if (strncmp("refs/heads/", target, 11) != 0)
2480 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2481 remote->name, refname) == -1) {
2482 error = got_error_from_errno("asprintf");
2485 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2486 remote->name, target + 11) == -1) {
2487 error = got_error_from_errno("asprintf");
2488 free(remote_refname);
2492 error = got_ref_open(&target_ref, repo, remote_target,
2495 free(remote_refname);
2496 free(remote_target);
2497 if (error->code == GOT_ERR_NOT_REF) {
2503 error = update_symref(remote_refname, target_ref,
2505 free(remote_refname);
2506 free(remote_target);
2507 got_ref_close(target_ref);
2514 if (kill(fetchpid, SIGTERM) == -1)
2515 error = got_error_from_errno("kill");
2516 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2517 error = got_error_from_errno("waitpid");
2519 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2520 error = got_error_from_errno("close");
2522 const struct got_error *close_err = got_repo_close(repo);
2527 got_worktree_close(worktree);
2528 TAILQ_FOREACH(pe, &refs, entry) {
2529 free((void *)pe->path);
2532 got_pathlist_free(&refs);
2533 TAILQ_FOREACH(pe, &symrefs, entry) {
2534 free((void *)pe->path);
2537 got_pathlist_free(&symrefs);
2538 got_pathlist_free(&wanted_branches);
2539 got_pathlist_free(&wanted_refs);
2554 usage_checkout(void)
2556 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2557 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2562 show_worktree_base_ref_warning(void)
2564 fprintf(stderr, "%s: warning: could not create a reference "
2565 "to the work tree's base commit; the commit could be "
2566 "garbage-collected by Git; making the repository "
2567 "writable and running 'got update' will prevent this\n",
2571 struct got_checkout_progress_arg {
2572 const char *worktree_path;
2573 int had_base_commit_ref_error;
2576 static const struct got_error *
2577 checkout_progress(void *arg, unsigned char status, const char *path)
2579 struct got_checkout_progress_arg *a = arg;
2581 /* Base commit bump happens silently. */
2582 if (status == GOT_STATUS_BUMP_BASE)
2585 if (status == GOT_STATUS_BASE_REF_ERR) {
2586 a->had_base_commit_ref_error = 1;
2590 while (path[0] == '/')
2593 printf("%c %s/%s\n", status, a->worktree_path, path);
2597 static const struct got_error *
2598 check_cancelled(void *arg)
2600 if (sigint_received || sigpipe_received)
2601 return got_error(GOT_ERR_CANCELLED);
2605 static const struct got_error *
2606 check_linear_ancestry(struct got_object_id *commit_id,
2607 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2608 struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 struct got_object_id *yca_id;
2613 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2614 commit_id, base_commit_id, repo, check_cancelled, NULL);
2619 return got_error(GOT_ERR_ANCESTRY);
2622 * Require a straight line of history between the target commit
2623 * and the work tree's base commit.
2625 * Non-linear situations such as this require a rebase:
2627 * (commit) D F (base_commit)
2635 * 'got update' only handles linear cases:
2636 * Update forwards in time: A (base/yca) - B - C - D (commit)
2637 * Update backwards in time: D (base) - C - B - A (commit/yca)
2639 if (allow_forwards_in_time_only) {
2640 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2641 return got_error(GOT_ERR_ANCESTRY);
2642 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2643 got_object_id_cmp(base_commit_id, yca_id) != 0)
2644 return got_error(GOT_ERR_ANCESTRY);
2650 static const struct got_error *
2651 check_same_branch(struct got_object_id *commit_id,
2652 struct got_reference *head_ref, struct got_object_id *yca_id,
2653 struct got_repository *repo)
2655 const struct got_error *err = NULL;
2656 struct got_commit_graph *graph = NULL;
2657 struct got_object_id *head_commit_id = NULL;
2658 int is_same_branch = 0;
2660 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2664 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2668 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2673 err = got_commit_graph_open(&graph, "/", 1);
2677 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2678 check_cancelled, NULL);
2683 struct got_object_id *id;
2684 err = got_commit_graph_iter_next(&id, graph, repo,
2685 check_cancelled, NULL);
2687 if (err->code == GOT_ERR_ITER_COMPLETED)
2693 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2695 if (got_object_id_cmp(id, commit_id) == 0) {
2703 got_commit_graph_close(graph);
2704 free(head_commit_id);
2705 if (!err && !is_same_branch)
2706 err = got_error(GOT_ERR_ANCESTRY);
2710 static const struct got_error *
2711 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2713 static char msg[512];
2714 const char *branch_name;
2716 if (got_ref_is_symbolic(ref))
2717 branch_name = got_ref_get_symref_target(ref);
2719 branch_name = got_ref_get_name(ref);
2721 if (strncmp("refs/heads/", branch_name, 11) == 0)
2724 snprintf(msg, sizeof(msg),
2725 "target commit is not contained in branch '%s'; "
2726 "the branch to use must be specified with -b; "
2727 "if necessary a new branch can be created for "
2728 "this commit with 'got branch -c %s BRANCH_NAME'",
2729 branch_name, commit_id_str);
2731 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2734 static const struct got_error *
2735 cmd_checkout(int argc, char *argv[])
2737 const struct got_error *error = NULL;
2738 struct got_repository *repo = NULL;
2739 struct got_reference *head_ref = NULL;
2740 struct got_worktree *worktree = NULL;
2741 char *repo_path = NULL;
2742 char *worktree_path = NULL;
2743 const char *path_prefix = "";
2744 const char *branch_name = GOT_REF_HEAD;
2745 char *commit_id_str = NULL;
2747 int ch, same_path_prefix, allow_nonempty = 0;
2748 struct got_pathlist_head paths;
2749 struct got_checkout_progress_arg cpa;
2753 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2756 branch_name = optarg;
2759 commit_id_str = strdup(optarg);
2760 if (commit_id_str == NULL)
2761 return got_error_from_errno("strdup");
2767 path_prefix = optarg;
2779 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2780 "unveil", NULL) == -1)
2784 char *base, *dotgit;
2786 repo_path = realpath(argv[0], NULL);
2787 if (repo_path == NULL)
2788 return got_error_from_errno2("realpath", argv[0]);
2789 cwd = getcwd(NULL, 0);
2791 error = got_error_from_errno("getcwd");
2798 error = got_path_basename(&base, path);
2801 dotgit = strstr(base, ".git");
2804 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2805 error = got_error_from_errno("asprintf");
2810 } else if (argc == 2) {
2811 repo_path = realpath(argv[0], NULL);
2812 if (repo_path == NULL) {
2813 error = got_error_from_errno2("realpath", argv[0]);
2816 worktree_path = realpath(argv[1], NULL);
2817 if (worktree_path == NULL) {
2818 if (errno != ENOENT) {
2819 error = got_error_from_errno2("realpath",
2823 worktree_path = strdup(argv[1]);
2824 if (worktree_path == NULL) {
2825 error = got_error_from_errno("strdup");
2832 got_path_strip_trailing_slashes(repo_path);
2833 got_path_strip_trailing_slashes(worktree_path);
2835 error = got_repo_open(&repo, repo_path, NULL);
2839 /* Pre-create work tree path for unveil(2) */
2840 error = got_path_mkdir(worktree_path);
2842 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2843 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2845 if (!allow_nonempty &&
2846 !got_path_dir_is_empty(worktree_path)) {
2847 error = got_error_path(worktree_path,
2848 GOT_ERR_DIR_NOT_EMPTY);
2853 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2857 error = got_ref_open(&head_ref, repo, branch_name, 0);
2861 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2862 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2865 error = got_worktree_open(&worktree, worktree_path);
2869 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2873 if (!same_path_prefix) {
2874 error = got_error(GOT_ERR_PATH_PREFIX);
2878 if (commit_id_str) {
2879 struct got_object_id *commit_id;
2880 struct got_reflist_head refs;
2882 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2886 error = got_repo_match_object_id(&commit_id, NULL,
2887 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2888 got_ref_list_free(&refs);
2891 error = check_linear_ancestry(commit_id,
2892 got_worktree_get_base_commit_id(worktree), 0, repo);
2893 if (error != NULL) {
2895 if (error->code == GOT_ERR_ANCESTRY) {
2896 error = checkout_ancestry_error(
2897 head_ref, commit_id_str);
2901 error = check_same_branch(commit_id, head_ref, NULL, repo);
2903 if (error->code == GOT_ERR_ANCESTRY) {
2904 error = checkout_ancestry_error(
2905 head_ref, commit_id_str);
2909 error = got_worktree_set_base_commit_id(worktree, repo,
2916 error = got_pathlist_append(&paths, "", NULL);
2919 cpa.worktree_path = worktree_path;
2920 cpa.had_base_commit_ref_error = 0;
2921 error = got_worktree_checkout_files(worktree, &paths, repo,
2922 checkout_progress, &cpa, check_cancelled, NULL);
2926 printf("Now shut up and hack\n");
2927 if (cpa.had_base_commit_ref_error)
2928 show_worktree_base_ref_warning();
2930 got_pathlist_free(&paths);
2931 free(commit_id_str);
2933 free(worktree_path);
2938 struct got_update_progress_arg {
2946 print_update_progress_stats(struct got_update_progress_arg *upa)
2948 if (!upa->did_something)
2951 if (upa->conflicts > 0)
2952 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2953 if (upa->obstructed > 0)
2954 printf("File paths obstructed by a non-regular file: %d\n",
2956 if (upa->not_updated > 0)
2957 printf("Files not updated because of existing merge "
2958 "conflicts: %d\n", upa->not_updated);
2964 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2969 static const struct got_error *
2970 update_progress(void *arg, unsigned char status, const char *path)
2972 struct got_update_progress_arg *upa = arg;
2974 if (status == GOT_STATUS_EXISTS ||
2975 status == GOT_STATUS_BASE_REF_ERR)
2978 upa->did_something = 1;
2980 /* Base commit bump happens silently. */
2981 if (status == GOT_STATUS_BUMP_BASE)
2984 if (status == GOT_STATUS_CONFLICT)
2986 if (status == GOT_STATUS_OBSTRUCTED)
2988 if (status == GOT_STATUS_CANNOT_UPDATE)
2991 while (path[0] == '/')
2993 printf("%c %s\n", status, path);
2997 static const struct got_error *
2998 switch_head_ref(struct got_reference *head_ref,
2999 struct got_object_id *commit_id, struct got_worktree *worktree,
3000 struct got_repository *repo)
3002 const struct got_error *err = NULL;
3004 int ref_has_moved = 0;
3006 /* Trivial case: switching between two different references. */
3007 if (strcmp(got_ref_get_name(head_ref),
3008 got_worktree_get_head_ref_name(worktree)) != 0) {
3009 printf("Switching work tree from %s to %s\n",
3010 got_worktree_get_head_ref_name(worktree),
3011 got_ref_get_name(head_ref));
3012 return got_worktree_set_head_ref(worktree, head_ref);
3015 err = check_linear_ancestry(commit_id,
3016 got_worktree_get_base_commit_id(worktree), 0, repo);
3018 if (err->code != GOT_ERR_ANCESTRY)
3025 /* Switching to a rebased branch with the same reference name. */
3026 err = got_object_id_str(&base_id_str,
3027 got_worktree_get_base_commit_id(worktree));
3030 printf("Reference %s now points at a different branch\n",
3031 got_worktree_get_head_ref_name(worktree));
3032 printf("Switching work tree from %s to %s\n", base_id_str,
3033 got_worktree_get_head_ref_name(worktree));
3037 static const struct got_error *
3038 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3040 const struct got_error *err;
3043 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3047 return got_error(GOT_ERR_REBASING);
3049 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3053 return got_error(GOT_ERR_HISTEDIT_BUSY);
3058 static const struct got_error *
3059 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3060 char *argv[], struct got_worktree *worktree)
3062 const struct got_error *err = NULL;
3069 return got_error_from_errno("strdup");
3070 return got_pathlist_append(paths, path, NULL);
3073 for (i = 0; i < argc; i++) {
3074 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3077 err = got_pathlist_append(paths, path, NULL);
3087 static const struct got_error *
3088 wrap_not_worktree_error(const struct got_error *orig_err,
3089 const char *cmdname, const char *path)
3091 const struct got_error *err;
3092 struct got_repository *repo;
3093 static char msg[512];
3095 err = got_repo_open(&repo, path, NULL);
3099 snprintf(msg, sizeof(msg),
3100 "'got %s' needs a work tree in addition to a git repository\n"
3101 "Work trees can be checked out from this Git repository with "
3103 "The got(1) manual page contains more information.", cmdname);
3104 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3105 got_repo_close(repo);
3109 static const struct got_error *
3110 cmd_update(int argc, char *argv[])
3112 const struct got_error *error = NULL;
3113 struct got_repository *repo = NULL;
3114 struct got_worktree *worktree = NULL;
3115 char *worktree_path = NULL;
3116 struct got_object_id *commit_id = NULL;
3117 char *commit_id_str = NULL;
3118 const char *branch_name = NULL;
3119 struct got_reference *head_ref = NULL;
3120 struct got_pathlist_head paths;
3121 struct got_pathlist_entry *pe;
3123 struct got_update_progress_arg upa;
3127 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3130 branch_name = optarg;
3133 commit_id_str = strdup(optarg);
3134 if (commit_id_str == NULL)
3135 return got_error_from_errno("strdup");
3147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3148 "unveil", NULL) == -1)
3151 worktree_path = getcwd(NULL, 0);
3152 if (worktree_path == NULL) {
3153 error = got_error_from_errno("getcwd");
3156 error = got_worktree_open(&worktree, worktree_path);
3158 if (error->code == GOT_ERR_NOT_WORKTREE)
3159 error = wrap_not_worktree_error(error, "update",
3164 error = check_rebase_or_histedit_in_progress(worktree);
3168 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3173 error = apply_unveil(got_repo_get_path(repo), 0,
3174 got_worktree_get_root_path(worktree));
3178 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3182 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3183 got_worktree_get_head_ref_name(worktree), 0);
3186 if (commit_id_str == NULL) {
3187 error = got_ref_resolve(&commit_id, repo, head_ref);
3190 error = got_object_id_str(&commit_id_str, commit_id);
3194 struct got_reflist_head refs;
3196 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3200 error = got_repo_match_object_id(&commit_id, NULL,
3201 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3202 got_ref_list_free(&refs);
3203 free(commit_id_str);
3204 commit_id_str = NULL;
3207 error = got_object_id_str(&commit_id_str, commit_id);
3213 struct got_object_id *head_commit_id;
3214 TAILQ_FOREACH(pe, &paths, entry) {
3215 if (pe->path_len == 0)
3217 error = got_error_msg(GOT_ERR_BAD_PATH,
3218 "switching between branches requires that "
3219 "the entire work tree gets updated");
3222 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3225 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3227 free(head_commit_id);
3230 error = check_same_branch(commit_id, head_ref, NULL, repo);
3233 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3237 error = check_linear_ancestry(commit_id,
3238 got_worktree_get_base_commit_id(worktree), 0, repo);
3239 if (error != NULL) {
3240 if (error->code == GOT_ERR_ANCESTRY)
3241 error = got_error(GOT_ERR_BRANCH_MOVED);
3244 error = check_same_branch(commit_id, head_ref, NULL, repo);
3249 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3251 error = got_worktree_set_base_commit_id(worktree, repo,
3257 memset(&upa, 0, sizeof(upa));
3258 error = got_worktree_checkout_files(worktree, &paths, repo,
3259 update_progress, &upa, check_cancelled, NULL);
3263 if (upa.did_something)
3264 printf("Updated to commit %s\n", commit_id_str);
3266 printf("Already up-to-date\n");
3267 print_update_progress_stats(&upa);
3269 free(worktree_path);
3270 TAILQ_FOREACH(pe, &paths, entry)
3271 free((char *)pe->path);
3272 got_pathlist_free(&paths);
3274 free(commit_id_str);
3278 static const struct got_error *
3279 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3280 const char *path, int diff_context, int ignore_whitespace,
3281 int force_text_diff, struct got_repository *repo)
3283 const struct got_error *err = NULL;
3284 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3287 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3292 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3296 while (path[0] == '/')
3298 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3299 diff_context, ignore_whitespace, force_text_diff, stdout);
3302 got_object_blob_close(blob1);
3303 got_object_blob_close(blob2);
3307 static const struct got_error *
3308 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3309 const char *path, int diff_context, int ignore_whitespace,
3310 int force_text_diff, struct got_repository *repo)
3312 const struct got_error *err = NULL;
3313 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3314 struct got_diff_blob_output_unidiff_arg arg;
3317 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3322 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3326 arg.diff_context = diff_context;
3327 arg.ignore_whitespace = ignore_whitespace;
3328 arg.force_text_diff = force_text_diff;
3329 arg.outfile = stdout;
3330 arg.line_offsets = NULL;
3332 while (path[0] == '/')
3334 err = got_diff_tree(tree1, tree2, path, path, repo,
3335 got_diff_blob_output_unidiff, &arg, 1);
3338 got_object_tree_close(tree1);
3340 got_object_tree_close(tree2);
3344 static const struct got_error *
3345 get_changed_paths(struct got_pathlist_head *paths,
3346 struct got_commit_object *commit, struct got_repository *repo)
3348 const struct got_error *err = NULL;
3349 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3350 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3351 struct got_object_qid *qid;
3353 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3355 struct got_commit_object *pcommit;
3356 err = got_object_open_as_commit(&pcommit, repo,
3361 tree_id1 = got_object_commit_get_tree_id(pcommit);
3362 got_object_commit_close(pcommit);
3367 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3372 tree_id2 = got_object_commit_get_tree_id(commit);
3373 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3377 err = got_diff_tree(tree1, tree2, "", "", repo,
3378 got_diff_tree_collect_changed_paths, paths, 0);
3381 got_object_tree_close(tree1);
3383 got_object_tree_close(tree2);
3387 static const struct got_error *
3388 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3389 const char *path, int diff_context, struct got_repository *repo)
3391 const struct got_error *err = NULL;
3392 struct got_commit_object *pcommit = NULL;
3393 char *id_str1 = NULL, *id_str2 = NULL;
3394 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3395 struct got_object_qid *qid;
3397 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3399 err = got_object_open_as_commit(&pcommit, repo,
3405 if (path && path[0] != '\0') {
3407 err = got_object_id_by_path(&obj_id2, repo, id, path);
3410 err = got_object_id_str(&id_str2, obj_id2);
3416 err = got_object_id_by_path(&obj_id1, repo,
3419 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3424 err = got_object_id_str(&id_str1, obj_id1);
3431 err = got_object_get_type(&obj_type, repo, obj_id2);
3436 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3438 case GOT_OBJ_TYPE_BLOB:
3439 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3442 case GOT_OBJ_TYPE_TREE:
3443 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3447 err = got_error(GOT_ERR_OBJ_TYPE);
3453 obj_id2 = got_object_commit_get_tree_id(commit);
3454 err = got_object_id_str(&id_str2, obj_id2);
3458 obj_id1 = got_object_commit_get_tree_id(pcommit);
3459 err = got_object_id_str(&id_str1, obj_id1);
3463 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3465 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3472 got_object_commit_close(pcommit);
3477 get_datestr(time_t *time, char *datebuf)
3479 struct tm mytm, *tm;
3482 tm = gmtime_r(time, &mytm);
3485 s = asctime_r(tm, datebuf);
3488 p = strchr(s, '\n');
3494 static const struct got_error *
3495 match_logmsg(int *have_match, struct got_object_id *id,
3496 struct got_commit_object *commit, regex_t *regex)
3498 const struct got_error *err = NULL;
3499 regmatch_t regmatch;
3500 char *id_str = NULL, *logmsg = NULL;
3504 err = got_object_id_str(&id_str, id);
3508 err = got_object_commit_get_logmsg(&logmsg, commit);
3512 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3521 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3524 regmatch_t regmatch;
3525 struct got_pathlist_entry *pe;
3529 TAILQ_FOREACH(pe, changed_paths, entry) {
3530 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3537 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3539 static const struct got_error*
3540 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3541 struct got_object_id *id, struct got_repository *repo)
3543 static const struct got_error *err = NULL;
3544 struct got_reflist_entry *re;
3550 TAILQ_FOREACH(re, refs, entry) {
3551 struct got_tag_object *tag = NULL;
3552 struct got_object_id *ref_id;
3555 name = got_ref_get_name(re->ref);
3556 if (strcmp(name, GOT_REF_HEAD) == 0)
3558 if (strncmp(name, "refs/", 5) == 0)
3560 if (strncmp(name, "got/", 4) == 0)
3562 if (strncmp(name, "heads/", 6) == 0)
3564 if (strncmp(name, "remotes/", 8) == 0) {
3566 s = strstr(name, "/" GOT_REF_HEAD);
3567 if (s != NULL && s[strlen(s)] == '\0')
3570 err = got_ref_resolve(&ref_id, repo, re->ref);
3573 if (strncmp(name, "tags/", 5) == 0) {
3574 err = got_object_open_as_tag(&tag, repo, ref_id);
3576 if (err->code != GOT_ERR_OBJ_TYPE) {
3580 /* Ref points at something other than a tag. */
3585 cmp = got_object_id_cmp(tag ?
3586 got_object_tag_get_object_id(tag) : ref_id, id);
3589 got_object_tag_close(tag);
3593 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3594 s ? ", " : "", name) == -1) {
3595 err = got_error_from_errno("asprintf");
3606 static const struct got_error *
3607 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3608 struct got_repository *repo, const char *path,
3609 struct got_pathlist_head *changed_paths, int show_patch,
3610 int diff_context, struct got_reflist_object_id_map *refs_idmap,
3611 const char *custom_refs_str)
3613 const struct got_error *err = NULL;
3614 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3616 time_t committer_time;
3617 const char *author, *committer;
3618 char *refs_str = NULL;
3620 err = got_object_id_str(&id_str, id);
3624 if (custom_refs_str == NULL) {
3625 struct got_reflist_head *refs;
3626 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3628 err = build_refs_str(&refs_str, refs, id, repo);
3634 printf(GOT_COMMIT_SEP_STR);
3635 if (custom_refs_str)
3636 printf("commit %s (%s)\n", id_str, custom_refs_str);
3638 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3639 refs_str ? refs_str : "", refs_str ? ")" : "");
3644 printf("from: %s\n", got_object_commit_get_author(commit));
3645 committer_time = got_object_commit_get_committer_time(commit);
3646 datestr = get_datestr(&committer_time, datebuf);
3648 printf("date: %s UTC\n", datestr);
3649 author = got_object_commit_get_author(commit);
3650 committer = got_object_commit_get_committer(commit);
3651 if (strcmp(author, committer) != 0)
3652 printf("via: %s\n", committer);
3653 if (got_object_commit_get_nparents(commit) > 1) {
3654 const struct got_object_id_queue *parent_ids;
3655 struct got_object_qid *qid;
3657 parent_ids = got_object_commit_get_parent_ids(commit);
3658 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3659 err = got_object_id_str(&id_str, qid->id);
3662 printf("parent %d: %s\n", n++, id_str);
3668 err = got_object_commit_get_logmsg(&logmsg0, commit);
3674 line = strsep(&logmsg, "\n");
3676 printf(" %s\n", line);
3680 if (changed_paths) {
3681 struct got_pathlist_entry *pe;
3682 TAILQ_FOREACH(pe, changed_paths, entry) {
3683 struct got_diff_changed_path *cp = pe->data;
3684 printf(" %c %s\n", cp->status, pe->path);
3689 err = print_patch(commit, id, path, diff_context, repo);
3694 if (fflush(stdout) != 0 && err == NULL)
3695 err = got_error_from_errno("fflush");
3702 static const struct got_error *
3703 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3704 struct got_repository *repo, const char *path, int show_changed_paths,
3705 int show_patch, const char *search_pattern, int diff_context, int limit,
3706 int log_branches, int reverse_display_order,
3707 struct got_reflist_object_id_map *refs_idmap)
3709 const struct got_error *err;
3710 struct got_commit_graph *graph;
3713 struct got_object_id_queue reversed_commits;
3714 struct got_object_qid *qid;
3715 struct got_commit_object *commit;
3716 struct got_pathlist_head changed_paths;
3717 struct got_pathlist_entry *pe;
3719 SIMPLEQ_INIT(&reversed_commits);
3720 TAILQ_INIT(&changed_paths);
3722 if (search_pattern && regcomp(®ex, search_pattern,
3723 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3724 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3726 err = got_commit_graph_open(&graph, path, !log_branches);
3729 err = got_commit_graph_iter_start(graph, root_id, repo,
3730 check_cancelled, NULL);
3734 struct got_object_id *id;
3736 if (sigint_received || sigpipe_received)
3739 err = got_commit_graph_iter_next(&id, graph, repo,
3740 check_cancelled, NULL);
3742 if (err->code == GOT_ERR_ITER_COMPLETED)
3749 err = got_object_open_as_commit(&commit, repo, id);
3753 if (show_changed_paths && !reverse_display_order) {
3754 err = get_changed_paths(&changed_paths, commit, repo);
3759 if (search_pattern) {
3760 err = match_logmsg(&have_match, id, commit, ®ex);
3762 got_object_commit_close(commit);
3765 if (have_match == 0 && show_changed_paths)
3766 match_changed_paths(&have_match,
3767 &changed_paths, ®ex);
3768 if (have_match == 0) {
3769 got_object_commit_close(commit);
3770 TAILQ_FOREACH(pe, &changed_paths, entry) {
3771 free((char *)pe->path);
3774 got_pathlist_free(&changed_paths);
3779 if (reverse_display_order) {
3780 err = got_object_qid_alloc(&qid, id);
3783 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3784 got_object_commit_close(commit);
3786 err = print_commit(commit, id, repo, path,
3787 show_changed_paths ? &changed_paths : NULL,
3788 show_patch, diff_context, refs_idmap, NULL);
3789 got_object_commit_close(commit);
3793 if ((limit && --limit == 0) ||
3794 (end_id && got_object_id_cmp(id, end_id) == 0))
3797 TAILQ_FOREACH(pe, &changed_paths, entry) {
3798 free((char *)pe->path);
3801 got_pathlist_free(&changed_paths);
3803 if (reverse_display_order) {
3804 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3805 err = got_object_open_as_commit(&commit, repo, qid->id);
3808 if (show_changed_paths) {
3809 err = get_changed_paths(&changed_paths,
3814 err = print_commit(commit, qid->id, repo, path,
3815 show_changed_paths ? &changed_paths : NULL,
3816 show_patch, diff_context, refs_idmap, NULL);
3817 got_object_commit_close(commit);
3820 TAILQ_FOREACH(pe, &changed_paths, entry) {
3821 free((char *)pe->path);
3824 got_pathlist_free(&changed_paths);
3828 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3829 qid = SIMPLEQ_FIRST(&reversed_commits);
3830 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3831 got_object_qid_free(qid);
3833 TAILQ_FOREACH(pe, &changed_paths, entry) {
3834 free((char *)pe->path);
3837 got_pathlist_free(&changed_paths);
3840 got_commit_graph_close(graph);
3847 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3848 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3849 "[-R] [path]\n", getprogname());
3854 get_default_log_limit(void)
3856 const char *got_default_log_limit;
3860 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3861 if (got_default_log_limit == NULL)
3863 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3869 static const struct got_error *
3870 cmd_log(int argc, char *argv[])
3872 const struct got_error *error;
3873 struct got_repository *repo = NULL;
3874 struct got_worktree *worktree = NULL;
3875 struct got_object_id *start_id = NULL, *end_id = NULL;
3876 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3877 const char *start_commit = NULL, *end_commit = NULL;
3878 const char *search_pattern = NULL;
3879 int diff_context = -1, ch;
3880 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3881 int reverse_display_order = 0;
3883 struct got_reflist_head refs;
3884 struct got_reflist_object_id_map *refs_idmap = NULL;
3889 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3895 limit = get_default_log_limit();
3897 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3903 show_changed_paths = 1;
3906 start_commit = optarg;
3909 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3912 err(1, "-C option %s", errstr);
3915 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3917 err(1, "-l option %s", errstr);
3923 repo_path = realpath(optarg, NULL);
3924 if (repo_path == NULL)
3925 return got_error_from_errno2("realpath",
3927 got_path_strip_trailing_slashes(repo_path);
3930 reverse_display_order = 1;
3933 search_pattern = optarg;
3936 end_commit = optarg;
3947 if (diff_context == -1)
3949 else if (!show_patch)
3950 errx(1, "-C requires -p");
3952 cwd = getcwd(NULL, 0);
3954 error = got_error_from_errno("getcwd");
3958 if (repo_path == NULL) {
3959 error = got_worktree_open(&worktree, cwd);
3960 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3967 error = got_worktree_resolve_path(&path, worktree,
3972 path = strdup(argv[0]);
3974 error = got_error_from_errno("strdup");
3978 } else if (argc != 0)
3981 if (repo_path == NULL) {
3982 repo_path = worktree ?
3983 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3985 if (repo_path == NULL) {
3986 error = got_error_from_errno("strdup");
3990 error = got_repo_open(&repo, repo_path, NULL);
3994 error = apply_unveil(got_repo_get_path(repo), 1,
3995 worktree ? got_worktree_get_root_path(worktree) : NULL);
3999 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4003 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4007 if (start_commit == NULL) {
4008 struct got_reference *head_ref;
4009 struct got_commit_object *commit = NULL;
4010 error = got_ref_open(&head_ref, repo,
4011 worktree ? got_worktree_get_head_ref_name(worktree)
4015 error = got_ref_resolve(&start_id, repo, head_ref);
4016 got_ref_close(head_ref);
4019 error = got_object_open_as_commit(&commit, repo,
4023 got_object_commit_close(commit);
4025 error = got_repo_match_object_id(&start_id, NULL,
4026 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4030 if (end_commit != NULL) {
4031 error = got_repo_match_object_id(&end_id, NULL,
4032 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4039 * If a path was specified on the command line it was resolved
4040 * to a path in the work tree above. Prepend the work tree's
4041 * path prefix to obtain the corresponding in-repository path.
4045 prefix = got_worktree_get_path_prefix(worktree);
4046 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4047 (path[0] != '\0') ? "/" : "", path) == -1) {
4048 error = got_error_from_errno("asprintf");
4053 error = got_repo_map_path(&in_repo_path, repo,
4059 path = in_repo_path;
4062 error = print_commits(start_id, end_id, repo, path ? path : "",
4063 show_changed_paths, show_patch, search_pattern, diff_context,
4064 limit, log_branches, reverse_display_order, refs_idmap);
4070 got_worktree_close(worktree);
4072 const struct got_error *close_err = got_repo_close(repo);
4077 got_reflist_object_id_map_free(refs_idmap);
4078 got_ref_list_free(&refs);
4085 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4086 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4090 struct print_diff_arg {
4091 struct got_repository *repo;
4092 struct got_worktree *worktree;
4097 int ignore_whitespace;
4098 int force_text_diff;
4102 * Create a file which contains the target path of a symlink so we can feed
4103 * it as content to the diff engine.
4105 static const struct got_error *
4106 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4107 const char *abspath)
4109 const struct got_error *err = NULL;
4110 char target_path[PATH_MAX];
4111 ssize_t target_len, outlen;
4116 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4117 if (target_len == -1)
4118 return got_error_from_errno2("readlinkat", abspath);
4120 target_len = readlink(abspath, target_path, PATH_MAX);
4121 if (target_len == -1)
4122 return got_error_from_errno2("readlink", abspath);
4125 *fd = got_opentempfd();
4127 return got_error_from_errno("got_opentempfd");
4129 outlen = write(*fd, target_path, target_len);
4131 err = got_error_from_errno("got_opentempfd");
4135 if (lseek(*fd, 0, SEEK_SET) == -1) {
4136 err = got_error_from_errno2("lseek", abspath);
4147 static const struct got_error *
4148 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4149 const char *path, struct got_object_id *blob_id,
4150 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4151 int dirfd, const char *de_name)
4153 struct print_diff_arg *a = arg;
4154 const struct got_error *err = NULL;
4155 struct got_blob_object *blob1 = NULL;
4158 char *abspath = NULL, *label1 = NULL;
4161 if (a->diff_staged) {
4162 if (staged_status != GOT_STATUS_MODIFY &&
4163 staged_status != GOT_STATUS_ADD &&
4164 staged_status != GOT_STATUS_DELETE)
4167 if (staged_status == GOT_STATUS_DELETE)
4169 if (status == GOT_STATUS_NONEXISTENT)
4170 return got_error_set_errno(ENOENT, path);
4171 if (status != GOT_STATUS_MODIFY &&
4172 status != GOT_STATUS_ADD &&
4173 status != GOT_STATUS_DELETE &&
4174 status != GOT_STATUS_CONFLICT)
4178 if (!a->header_shown) {
4179 printf("diff %s %s%s\n", a->id_str,
4180 got_worktree_get_root_path(a->worktree),
4181 a->diff_staged ? " (staged changes)" : "");
4182 a->header_shown = 1;
4185 if (a->diff_staged) {
4186 const char *label1 = NULL, *label2 = NULL;
4187 switch (staged_status) {
4188 case GOT_STATUS_MODIFY:
4192 case GOT_STATUS_ADD:
4195 case GOT_STATUS_DELETE:
4199 return got_error(GOT_ERR_FILE_STATUS);
4201 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4202 staged_blob_id, label1, label2, a->diff_context,
4203 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4206 if (staged_status == GOT_STATUS_ADD ||
4207 staged_status == GOT_STATUS_MODIFY) {
4209 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4213 err = got_object_id_str(&id_str, staged_blob_id);
4216 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4217 err = got_error_from_errno("asprintf");
4222 } else if (status != GOT_STATUS_ADD) {
4223 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4228 if (status != GOT_STATUS_DELETE) {
4229 if (asprintf(&abspath, "%s/%s",
4230 got_worktree_get_root_path(a->worktree), path) == -1) {
4231 err = got_error_from_errno("asprintf");
4236 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4238 if (errno != ELOOP) {
4239 err = got_error_from_errno2("openat",
4243 err = get_symlink_target_file(&fd, dirfd,
4249 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4251 if (errno != ELOOP) {
4252 err = got_error_from_errno2("open",
4256 err = get_symlink_target_file(&fd, dirfd,
4262 if (fstat(fd, &sb) == -1) {
4263 err = got_error_from_errno2("fstat", abspath);
4266 f2 = fdopen(fd, "r");
4268 err = got_error_from_errno2("fdopen", abspath);
4275 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4276 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4279 got_object_blob_close(blob1);
4280 if (f2 && fclose(f2) == EOF && err == NULL)
4281 err = got_error_from_errno("fclose");
4282 if (fd != -1 && close(fd) == -1 && err == NULL)
4283 err = got_error_from_errno("close");
4288 static const struct got_error *
4289 cmd_diff(int argc, char *argv[])
4291 const struct got_error *error;
4292 struct got_repository *repo = NULL;
4293 struct got_worktree *worktree = NULL;
4294 char *cwd = NULL, *repo_path = NULL;
4295 struct got_object_id *id1 = NULL, *id2 = NULL;
4296 const char *id_str1 = NULL, *id_str2 = NULL;
4297 char *label1 = NULL, *label2 = NULL;
4299 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4300 int force_text_diff = 0;
4303 struct got_reflist_head refs;
4308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4313 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4316 force_text_diff = 1;
4319 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4322 err(1, "-C option %s", errstr);
4325 repo_path = realpath(optarg, NULL);
4326 if (repo_path == NULL)
4327 return got_error_from_errno2("realpath",
4329 got_path_strip_trailing_slashes(repo_path);
4335 ignore_whitespace = 1;
4346 cwd = getcwd(NULL, 0);
4348 error = got_error_from_errno("getcwd");
4354 "-r option can't be used when diffing a work tree");
4355 error = got_worktree_open(&worktree, cwd);
4357 if (error->code == GOT_ERR_NOT_WORKTREE)
4358 error = wrap_not_worktree_error(error, "diff",
4362 repo_path = strdup(got_worktree_get_repo_path(worktree));
4363 if (repo_path == NULL) {
4364 error = got_error_from_errno("strdup");
4368 error = got_worktree_resolve_path(&path, worktree,
4375 error = got_error_from_errno("strdup");
4379 } else if (argc == 2) {
4381 errx(1, "-s option can't be used when diffing "
4382 "objects in repository");
4385 if (repo_path == NULL) {
4386 error = got_worktree_open(&worktree, cwd);
4387 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4389 repo_path = strdup(worktree ?
4390 got_worktree_get_repo_path(worktree) : cwd);
4391 if (repo_path == NULL) {
4392 error = got_error_from_errno("strdup");
4399 error = got_repo_open(&repo, repo_path, NULL);
4404 error = apply_unveil(got_repo_get_path(repo), 1,
4405 worktree ? got_worktree_get_root_path(worktree) : NULL);
4410 struct print_diff_arg arg;
4411 struct got_pathlist_head paths;
4416 error = got_object_id_str(&id_str,
4417 got_worktree_get_base_commit_id(worktree));
4421 arg.worktree = worktree;
4422 arg.diff_context = diff_context;
4423 arg.id_str = id_str;
4424 arg.header_shown = 0;
4425 arg.diff_staged = diff_staged;
4426 arg.ignore_whitespace = ignore_whitespace;
4427 arg.force_text_diff = force_text_diff;
4429 error = got_pathlist_append(&paths, path, NULL);
4433 error = got_worktree_status(worktree, &paths, repo, print_diff,
4434 &arg, check_cancelled, NULL);
4436 got_pathlist_free(&paths);
4440 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4444 error = got_repo_match_object_id(&id1, &label1, id_str1,
4445 GOT_OBJ_TYPE_ANY, &refs, repo);
4449 error = got_repo_match_object_id(&id2, &label2, id_str2,
4450 GOT_OBJ_TYPE_ANY, &refs, repo);
4454 error = got_object_get_type(&type1, repo, id1);
4458 error = got_object_get_type(&type2, repo, id2);
4462 if (type1 != type2) {
4463 error = got_error(GOT_ERR_OBJ_TYPE);
4468 case GOT_OBJ_TYPE_BLOB:
4469 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4470 NULL, NULL, diff_context, ignore_whitespace,
4471 force_text_diff, repo, stdout);
4473 case GOT_OBJ_TYPE_TREE:
4474 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4475 "", "", diff_context, ignore_whitespace, force_text_diff,
4478 case GOT_OBJ_TYPE_COMMIT:
4479 printf("diff %s %s\n", label1, label2);
4480 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4481 diff_context, ignore_whitespace, force_text_diff, repo,
4485 error = got_error(GOT_ERR_OBJ_TYPE);
4494 got_worktree_close(worktree);
4496 const struct got_error *close_err = got_repo_close(repo);
4500 got_ref_list_free(&refs);
4508 "usage: %s blame [-c commit] [-r repository-path] path\n",
4517 char datebuf[11]; /* YYYY-MM-DD + NUL */
4520 struct blame_cb_args {
4521 struct blame_line *lines;
4525 off_t *line_offsets;
4527 struct got_repository *repo;
4530 static const struct got_error *
4531 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4533 const struct got_error *err = NULL;
4534 struct blame_cb_args *a = arg;
4535 struct blame_line *bline;
4537 size_t linesize = 0;
4538 struct got_commit_object *commit = NULL;
4541 time_t committer_time;
4543 if (nlines != a->nlines ||
4544 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4545 return got_error(GOT_ERR_RANGE);
4547 if (sigint_received)
4548 return got_error(GOT_ERR_ITER_COMPLETED);
4551 return NULL; /* no change in this commit */
4553 /* Annotate this line. */
4554 bline = &a->lines[lineno - 1];
4555 if (bline->annotated)
4557 err = got_object_id_str(&bline->id_str, id);
4561 err = got_object_open_as_commit(&commit, a->repo, id);
4565 bline->committer = strdup(got_object_commit_get_committer(commit));
4566 if (bline->committer == NULL) {
4567 err = got_error_from_errno("strdup");
4571 committer_time = got_object_commit_get_committer_time(commit);
4572 if (localtime_r(&committer_time, &tm) == NULL)
4573 return got_error_from_errno("localtime_r");
4574 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4576 err = got_error(GOT_ERR_NO_SPACE);
4579 bline->annotated = 1;
4581 /* Print lines annotated so far. */
4582 bline = &a->lines[a->lineno_cur - 1];
4583 if (!bline->annotated)
4586 offset = a->line_offsets[a->lineno_cur - 1];
4587 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4588 err = got_error_from_errno("fseeko");
4592 while (bline->annotated) {
4593 char *smallerthan, *at, *nl, *committer;
4596 if (getline(&line, &linesize, a->f) == -1) {
4598 err = got_error_from_errno("getline");
4602 committer = bline->committer;
4603 smallerthan = strchr(committer, '<');
4604 if (smallerthan && smallerthan[1] != '\0')
4605 committer = smallerthan + 1;
4606 at = strchr(committer, '@');
4609 len = strlen(committer);
4611 committer[8] = '\0';
4613 nl = strchr(line, '\n');
4616 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4617 bline->id_str, bline->datebuf, committer, line);
4620 bline = &a->lines[a->lineno_cur - 1];
4624 got_object_commit_close(commit);
4629 static const struct got_error *
4630 cmd_blame(int argc, char *argv[])
4632 const struct got_error *error;
4633 struct got_repository *repo = NULL;
4634 struct got_worktree *worktree = NULL;
4635 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4636 char *link_target = NULL;
4637 struct got_object_id *obj_id = NULL;
4638 struct got_object_id *commit_id = NULL;
4639 struct got_blob_object *blob = NULL;
4640 char *commit_id_str = NULL;
4641 struct blame_cb_args bca;
4642 int ch, obj_type, i;
4645 memset(&bca, 0, sizeof(bca));
4648 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4653 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4656 commit_id_str = optarg;
4659 repo_path = realpath(optarg, NULL);
4660 if (repo_path == NULL)
4661 return got_error_from_errno2("realpath",
4663 got_path_strip_trailing_slashes(repo_path);
4679 cwd = getcwd(NULL, 0);
4681 error = got_error_from_errno("getcwd");
4684 if (repo_path == NULL) {
4685 error = got_worktree_open(&worktree, cwd);
4686 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4692 strdup(got_worktree_get_repo_path(worktree));
4693 if (repo_path == NULL) {
4694 error = got_error_from_errno("strdup");
4699 repo_path = strdup(cwd);
4700 if (repo_path == NULL) {
4701 error = got_error_from_errno("strdup");
4707 error = got_repo_open(&repo, repo_path, NULL);
4712 const char *prefix = got_worktree_get_path_prefix(worktree);
4715 error = got_worktree_resolve_path(&p, worktree, path);
4718 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4719 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4721 error = got_error_from_errno("asprintf");
4726 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4728 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4731 error = got_repo_map_path(&in_repo_path, repo, path);
4736 if (commit_id_str == NULL) {
4737 struct got_reference *head_ref;
4738 error = got_ref_open(&head_ref, repo, worktree ?
4739 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4742 error = got_ref_resolve(&commit_id, repo, head_ref);
4743 got_ref_close(head_ref);
4747 struct got_reflist_head refs;
4749 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4753 error = got_repo_match_object_id(&commit_id, NULL,
4754 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4755 got_ref_list_free(&refs);
4760 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4765 error = got_object_id_by_path(&obj_id, repo, commit_id,
4766 link_target ? link_target : in_repo_path);
4770 error = got_object_get_type(&obj_type, repo, obj_id);
4774 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4775 error = got_error_path(link_target ? link_target : in_repo_path,
4780 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4783 bca.f = got_opentemp();
4784 if (bca.f == NULL) {
4785 error = got_error_from_errno("got_opentemp");
4788 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4789 &bca.line_offsets, bca.f, blob);
4790 if (error || bca.nlines == 0)
4793 /* Don't include \n at EOF in the blame line count. */
4794 if (bca.line_offsets[bca.nlines - 1] == filesize)
4797 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4798 if (bca.lines == NULL) {
4799 error = got_error_from_errno("calloc");
4803 bca.nlines_prec = 0;
4811 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4812 repo, blame_cb, &bca, check_cancelled, NULL);
4821 got_object_blob_close(blob);
4823 got_worktree_close(worktree);
4825 const struct got_error *close_err = got_repo_close(repo);
4830 for (i = 0; i < bca.nlines; i++) {
4831 struct blame_line *bline = &bca.lines[i];
4832 free(bline->id_str);
4833 free(bline->committer);
4837 free(bca.line_offsets);
4838 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4839 error = got_error_from_errno("fclose");
4847 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4852 static const struct got_error *
4853 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4854 const char *root_path, struct got_repository *repo)
4856 const struct got_error *err = NULL;
4857 int is_root_path = (strcmp(path, root_path) == 0);
4858 const char *modestr = "";
4859 mode_t mode = got_tree_entry_get_mode(te);
4860 char *link_target = NULL;
4862 path += strlen(root_path);
4863 while (path[0] == '/')
4866 if (got_object_tree_entry_is_submodule(te))
4868 else if (S_ISLNK(mode)) {
4871 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4874 for (i = 0; i < strlen(link_target); i++) {
4875 if (!isprint((unsigned char)link_target[i]))
4876 link_target[i] = '?';
4881 else if (S_ISDIR(mode))
4883 else if (mode & S_IXUSR)
4886 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4887 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4888 link_target ? " -> ": "", link_target ? link_target : "");
4894 static const struct got_error *
4895 print_tree(const char *path, struct got_object_id *commit_id,
4896 int show_ids, int recurse, const char *root_path,
4897 struct got_repository *repo)
4899 const struct got_error *err = NULL;
4900 struct got_object_id *tree_id = NULL;
4901 struct got_tree_object *tree = NULL;
4904 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4908 err = got_object_open_as_tree(&tree, repo, tree_id);
4911 nentries = got_object_tree_get_nentries(tree);
4912 for (i = 0; i < nentries; i++) {
4913 struct got_tree_entry *te;
4916 if (sigint_received || sigpipe_received)
4919 te = got_object_tree_get_entry(tree, i);
4922 err = got_object_id_str(&id_str,
4923 got_tree_entry_get_id(te));
4926 if (asprintf(&id, "%s ", id_str) == -1) {
4927 err = got_error_from_errno("asprintf");
4933 err = print_entry(te, id, path, root_path, repo);
4938 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4940 if (asprintf(&child_path, "%s%s%s", path,
4941 path[0] == '/' && path[1] == '\0' ? "" : "/",
4942 got_tree_entry_get_name(te)) == -1) {
4943 err = got_error_from_errno("asprintf");
4946 err = print_tree(child_path, commit_id, show_ids, 1,
4955 got_object_tree_close(tree);
4960 static const struct got_error *
4961 cmd_tree(int argc, char *argv[])
4963 const struct got_error *error;
4964 struct got_repository *repo = NULL;
4965 struct got_worktree *worktree = NULL;
4966 const char *path, *refname = NULL;
4967 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4968 struct got_object_id *commit_id = NULL;
4969 char *commit_id_str = NULL;
4970 int show_ids = 0, recurse = 0;
4974 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4979 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4982 commit_id_str = optarg;
4985 repo_path = realpath(optarg, NULL);
4986 if (repo_path == NULL)
4987 return got_error_from_errno2("realpath",
4989 got_path_strip_trailing_slashes(repo_path);
5013 cwd = getcwd(NULL, 0);
5015 error = got_error_from_errno("getcwd");
5018 if (repo_path == NULL) {
5019 error = got_worktree_open(&worktree, cwd);
5020 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5026 strdup(got_worktree_get_repo_path(worktree));
5027 if (repo_path == NULL)
5028 error = got_error_from_errno("strdup");
5032 repo_path = strdup(cwd);
5033 if (repo_path == NULL) {
5034 error = got_error_from_errno("strdup");
5040 error = got_repo_open(&repo, repo_path, NULL);
5045 const char *prefix = got_worktree_get_path_prefix(worktree);
5050 error = got_worktree_resolve_path(&p, worktree, path);
5053 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5054 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5056 error = got_error_from_errno("asprintf");
5061 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5065 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5070 error = got_repo_map_path(&in_repo_path, repo, path);
5075 if (commit_id_str == NULL) {
5076 struct got_reference *head_ref;
5078 refname = got_worktree_get_head_ref_name(worktree);
5080 refname = GOT_REF_HEAD;
5081 error = got_ref_open(&head_ref, repo, refname, 0);
5084 error = got_ref_resolve(&commit_id, repo, head_ref);
5085 got_ref_close(head_ref);
5089 struct got_reflist_head refs;
5091 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5095 error = got_repo_match_object_id(&commit_id, NULL,
5096 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5097 got_ref_list_free(&refs);
5102 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5103 in_repo_path, repo);
5110 got_worktree_close(worktree);
5112 const struct got_error *close_err = got_repo_close(repo);
5122 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5127 static const struct got_error *
5128 print_status(void *arg, unsigned char status, unsigned char staged_status,
5129 const char *path, struct got_object_id *blob_id,
5130 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5131 int dirfd, const char *de_name)
5133 if (status == staged_status && (status == GOT_STATUS_DELETE))
5134 status = GOT_STATUS_NO_CHANGE;
5136 char *status_codes = arg;
5137 size_t ncodes = strlen(status_codes);
5139 for (i = 0; i < ncodes ; i++) {
5140 if (status == status_codes[i] ||
5141 staged_status == status_codes[i])
5147 printf("%c%c %s\n", status, staged_status, path);
5151 static const struct got_error *
5152 cmd_status(int argc, char *argv[])
5154 const struct got_error *error = NULL;
5155 struct got_repository *repo = NULL;
5156 struct got_worktree *worktree = NULL;
5157 char *cwd = NULL, *status_codes = NULL;;
5158 struct got_pathlist_head paths;
5159 struct got_pathlist_entry *pe;
5164 while ((ch = getopt(argc, argv, "s:")) != -1) {
5167 for (i = 0; i < strlen(optarg); i++) {
5168 switch (optarg[i]) {
5169 case GOT_STATUS_MODIFY:
5170 case GOT_STATUS_ADD:
5171 case GOT_STATUS_DELETE:
5172 case GOT_STATUS_CONFLICT:
5173 case GOT_STATUS_MISSING:
5174 case GOT_STATUS_OBSTRUCTED:
5175 case GOT_STATUS_UNVERSIONED:
5176 case GOT_STATUS_MODE_CHANGE:
5177 case GOT_STATUS_NONEXISTENT:
5180 errx(1, "invalid status code '%c'",
5184 status_codes = optarg;
5196 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5200 cwd = getcwd(NULL, 0);
5202 error = got_error_from_errno("getcwd");
5206 error = got_worktree_open(&worktree, cwd);
5208 if (error->code == GOT_ERR_NOT_WORKTREE)
5209 error = wrap_not_worktree_error(error, "status", cwd);
5213 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5218 error = apply_unveil(got_repo_get_path(repo), 1,
5219 got_worktree_get_root_path(worktree));
5223 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5227 error = got_worktree_status(worktree, &paths, repo, print_status,
5228 status_codes, check_cancelled, NULL);
5230 TAILQ_FOREACH(pe, &paths, entry)
5231 free((char *)pe->path);
5232 got_pathlist_free(&paths);
5241 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5247 static const struct got_error *
5248 list_refs(struct got_repository *repo, const char *refname)
5250 static const struct got_error *err = NULL;
5251 struct got_reflist_head refs;
5252 struct got_reflist_entry *re;
5255 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5259 TAILQ_FOREACH(re, &refs, entry) {
5261 refstr = got_ref_to_str(re->ref);
5263 return got_error_from_errno("got_ref_to_str");
5264 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5268 got_ref_list_free(&refs);
5272 static const struct got_error *
5273 delete_ref(struct got_repository *repo, const char *refname)
5275 const struct got_error *err = NULL;
5276 struct got_reference *ref;
5278 err = got_ref_open(&ref, repo, refname, 0);
5282 err = got_ref_delete(ref, repo);
5287 static const struct got_error *
5288 add_ref(struct got_repository *repo, const char *refname, const char *target)
5290 const struct got_error *err = NULL;
5291 struct got_object_id *id;
5292 struct got_reference *ref = NULL;
5295 * Don't let the user create a reference name with a leading '-'.
5296 * While technically a valid reference name, this case is usually
5297 * an unintended typo.
5299 if (refname[0] == '-')
5300 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5302 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5305 struct got_reference *target_ref;
5307 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5309 err = got_ref_open(&target_ref, repo, target, 0);
5312 err = got_ref_resolve(&id, repo, target_ref);
5313 got_ref_close(target_ref);
5318 err = got_ref_alloc(&ref, refname, id);
5322 err = got_ref_write(ref, repo);
5330 static const struct got_error *
5331 add_symref(struct got_repository *repo, const char *refname, const char *target)
5333 const struct got_error *err = NULL;
5334 struct got_reference *ref = NULL;
5335 struct got_reference *target_ref = NULL;
5338 * Don't let the user create a reference name with a leading '-'.
5339 * While technically a valid reference name, this case is usually
5340 * an unintended typo.
5342 if (refname[0] == '-')
5343 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5345 err = got_ref_open(&target_ref, repo, target, 0);
5349 err = got_ref_alloc_symref(&ref, refname, target_ref);
5353 err = got_ref_write(ref, repo);
5356 got_ref_close(target_ref);
5362 static const struct got_error *
5363 cmd_ref(int argc, char *argv[])
5365 const struct got_error *error = NULL;
5366 struct got_repository *repo = NULL;
5367 struct got_worktree *worktree = NULL;
5368 char *cwd = NULL, *repo_path = NULL;
5369 int ch, do_list = 0, do_delete = 0;
5370 const char *obj_arg = NULL, *symref_target= NULL;
5371 char *refname = NULL;
5373 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5382 repo_path = realpath(optarg, NULL);
5383 if (repo_path == NULL)
5384 return got_error_from_errno2("realpath",
5386 got_path_strip_trailing_slashes(repo_path);
5392 symref_target = optarg;
5400 if (obj_arg && do_list)
5401 option_conflict('c', 'l');
5402 if (obj_arg && do_delete)
5403 option_conflict('c', 'd');
5404 if (obj_arg && symref_target)
5405 option_conflict('c', 's');
5406 if (symref_target && do_delete)
5407 option_conflict('s', 'd');
5408 if (symref_target && do_list)
5409 option_conflict('s', 'l');
5410 if (do_delete && do_list)
5411 option_conflict('d', 'l');
5417 if (argc != 0 && argc != 1)
5420 refname = strdup(argv[0]);
5421 if (refname == NULL) {
5422 error = got_error_from_errno("strdup");
5429 refname = strdup(argv[0]);
5430 if (refname == NULL) {
5431 error = got_error_from_errno("strdup");
5437 got_path_strip_trailing_slashes(refname);
5441 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5445 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5446 "sendfd unveil", NULL) == -1)
5450 cwd = getcwd(NULL, 0);
5452 error = got_error_from_errno("getcwd");
5456 if (repo_path == NULL) {
5457 error = got_worktree_open(&worktree, cwd);
5458 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5464 strdup(got_worktree_get_repo_path(worktree));
5465 if (repo_path == NULL)
5466 error = got_error_from_errno("strdup");
5470 repo_path = strdup(cwd);
5471 if (repo_path == NULL) {
5472 error = got_error_from_errno("strdup");
5478 error = got_repo_open(&repo, repo_path, NULL);
5482 error = apply_unveil(got_repo_get_path(repo), do_list,
5483 worktree ? got_worktree_get_root_path(worktree) : NULL);
5488 error = list_refs(repo, refname);
5490 error = delete_ref(repo, refname);
5491 else if (symref_target)
5492 error = add_symref(repo, refname, symref_target);
5494 if (obj_arg == NULL)
5496 error = add_ref(repo, refname, obj_arg);
5501 const struct got_error *close_err = got_repo_close(repo);
5506 got_worktree_close(worktree);
5516 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5517 "[name]\n", getprogname());
5521 static const struct got_error *
5522 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5523 struct got_reference *ref)
5525 const struct got_error *err = NULL;
5526 const char *refname, *marker = " ";
5529 refname = got_ref_get_name(ref);
5530 if (worktree && strcmp(refname,
5531 got_worktree_get_head_ref_name(worktree)) == 0) {
5532 struct got_object_id *id = NULL;
5534 err = got_ref_resolve(&id, repo, ref);
5537 if (got_object_id_cmp(id,
5538 got_worktree_get_base_commit_id(worktree)) == 0)
5545 if (strncmp(refname, "refs/heads/", 11) == 0)
5547 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5549 if (strncmp(refname, "refs/remotes/", 13) == 0)
5552 refstr = got_ref_to_str(ref);
5554 return got_error_from_errno("got_ref_to_str");
5556 printf("%s%s: %s\n", marker, refname, refstr);
5561 static const struct got_error *
5562 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5564 const char *refname;
5566 if (worktree == NULL)
5567 return got_error(GOT_ERR_NOT_WORKTREE);
5569 refname = got_worktree_get_head_ref_name(worktree);
5571 if (strncmp(refname, "refs/heads/", 11) == 0)
5573 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5576 printf("%s\n", refname);
5581 static const struct got_error *
5582 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5584 static const struct got_error *err = NULL;
5585 struct got_reflist_head refs;
5586 struct got_reflist_entry *re;
5587 struct got_reference *temp_ref = NULL;
5588 int rebase_in_progress, histedit_in_progress;
5593 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5598 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5603 if (rebase_in_progress || histedit_in_progress) {
5604 err = got_ref_open(&temp_ref, repo,
5605 got_worktree_get_head_ref_name(worktree), 0);
5608 list_branch(repo, worktree, temp_ref);
5609 got_ref_close(temp_ref);
5613 err = got_ref_list(&refs, repo, "refs/heads",
5614 got_ref_cmp_by_name, NULL);
5618 TAILQ_FOREACH(re, &refs, entry)
5619 list_branch(repo, worktree, re->ref);
5621 got_ref_list_free(&refs);
5623 err = got_ref_list(&refs, repo, "refs/remotes",
5624 got_ref_cmp_by_name, NULL);
5628 TAILQ_FOREACH(re, &refs, entry)
5629 list_branch(repo, worktree, re->ref);
5631 got_ref_list_free(&refs);
5636 static const struct got_error *
5637 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5638 const char *branch_name)
5640 const struct got_error *err = NULL;
5641 struct got_reference *ref = NULL;
5644 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5645 return got_error_from_errno("asprintf");
5647 err = got_ref_open(&ref, repo, refname, 0);
5652 strcmp(got_worktree_get_head_ref_name(worktree),
5653 got_ref_get_name(ref)) == 0) {
5654 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5655 "will not delete this work tree's current branch");
5659 err = got_ref_delete(ref, repo);
5667 static const struct got_error *
5668 add_branch(struct got_repository *repo, const char *branch_name,
5669 struct got_object_id *base_commit_id)
5671 const struct got_error *err = NULL;
5672 struct got_reference *ref = NULL;
5673 char *base_refname = NULL, *refname = NULL;
5676 * Don't let the user create a branch name with a leading '-'.
5677 * While technically a valid reference name, this case is usually
5678 * an unintended typo.
5680 if (branch_name[0] == '-')
5681 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5683 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5684 err = got_error_from_errno("asprintf");
5688 err = got_ref_open(&ref, repo, refname, 0);
5690 err = got_error(GOT_ERR_BRANCH_EXISTS);
5692 } else if (err->code != GOT_ERR_NOT_REF)
5695 err = got_ref_alloc(&ref, refname, base_commit_id);
5699 err = got_ref_write(ref, repo);
5708 static const struct got_error *
5709 cmd_branch(int argc, char *argv[])
5711 const struct got_error *error = NULL;
5712 struct got_repository *repo = NULL;
5713 struct got_worktree *worktree = NULL;
5714 char *cwd = NULL, *repo_path = NULL;
5715 int ch, do_list = 0, do_show = 0, do_update = 1;
5716 const char *delref = NULL, *commit_id_arg = NULL;
5717 struct got_reference *ref = NULL;
5718 struct got_pathlist_head paths;
5719 struct got_pathlist_entry *pe;
5720 struct got_object_id *commit_id = NULL;
5721 char *commit_id_str = NULL;
5725 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5728 commit_id_arg = optarg;
5734 repo_path = realpath(optarg, NULL);
5735 if (repo_path == NULL)
5736 return got_error_from_errno2("realpath",
5738 got_path_strip_trailing_slashes(repo_path);
5752 if (do_list && delref)
5753 option_conflict('l', 'd');
5758 if (!do_list && !delref && argc == 0)
5761 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5762 errx(1, "-c option can only be used when creating a branch");
5764 if (do_list || delref) {
5767 } else if (!do_show && argc != 1)
5771 if (do_list || do_show) {
5772 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5776 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5777 "sendfd unveil", NULL) == -1)
5781 cwd = getcwd(NULL, 0);
5783 error = got_error_from_errno("getcwd");
5787 if (repo_path == NULL) {
5788 error = got_worktree_open(&worktree, cwd);
5789 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5795 strdup(got_worktree_get_repo_path(worktree));
5796 if (repo_path == NULL)
5797 error = got_error_from_errno("strdup");
5801 repo_path = strdup(cwd);
5802 if (repo_path == NULL) {
5803 error = got_error_from_errno("strdup");
5809 error = got_repo_open(&repo, repo_path, NULL);
5813 error = apply_unveil(got_repo_get_path(repo), do_list,
5814 worktree ? got_worktree_get_root_path(worktree) : NULL);
5819 error = show_current_branch(repo, worktree);
5821 error = list_branches(repo, worktree);
5823 error = delete_branch(repo, worktree, delref);
5825 struct got_reflist_head refs;
5827 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5831 if (commit_id_arg == NULL)
5832 commit_id_arg = worktree ?
5833 got_worktree_get_head_ref_name(worktree) :
5835 error = got_repo_match_object_id(&commit_id, NULL,
5836 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5837 got_ref_list_free(&refs);
5840 error = add_branch(repo, argv[0], commit_id);
5843 if (worktree && do_update) {
5844 struct got_update_progress_arg upa;
5845 char *branch_refname = NULL;
5847 error = got_object_id_str(&commit_id_str, commit_id);
5850 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5854 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5856 error = got_error_from_errno("asprintf");
5859 error = got_ref_open(&ref, repo, branch_refname, 0);
5860 free(branch_refname);
5863 error = switch_head_ref(ref, commit_id, worktree,
5867 error = got_worktree_set_base_commit_id(worktree, repo,
5871 memset(&upa, 0, sizeof(upa));
5872 error = got_worktree_checkout_files(worktree, &paths,
5873 repo, update_progress, &upa, check_cancelled,
5877 if (upa.did_something)
5878 printf("Updated to commit %s\n", commit_id_str);
5879 print_update_progress_stats(&upa);
5886 const struct got_error *close_err = got_repo_close(repo);
5891 got_worktree_close(worktree);
5895 free(commit_id_str);
5896 TAILQ_FOREACH(pe, &paths, entry)
5897 free((char *)pe->path);
5898 got_pathlist_free(&paths);
5907 "usage: %s tag [-c commit] [-r repository] [-l] "
5908 "[-m message] name\n", getprogname());
5913 static const struct got_error *
5914 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5916 const struct got_error *err = NULL;
5917 struct got_reflist_entry *re, *se, *new;
5918 struct got_object_id *re_id, *se_id;
5919 struct got_tag_object *re_tag, *se_tag;
5920 time_t re_time, se_time;
5922 SIMPLEQ_FOREACH(re, tags, entry) {
5923 se = SIMPLEQ_FIRST(sorted);
5925 err = got_reflist_entry_dup(&new, re);
5928 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5931 err = got_ref_resolve(&re_id, repo, re->ref);
5934 err = got_object_open_as_tag(&re_tag, repo, re_id);
5938 re_time = got_object_tag_get_tagger_time(re_tag);
5939 got_object_tag_close(re_tag);
5943 err = got_ref_resolve(&se_id, repo, re->ref);
5946 err = got_object_open_as_tag(&se_tag, repo, se_id);
5950 se_time = got_object_tag_get_tagger_time(se_tag);
5951 got_object_tag_close(se_tag);
5953 if (se_time > re_time) {
5954 err = got_reflist_entry_dup(&new, re);
5957 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5960 se = SIMPLEQ_NEXT(se, entry);
5969 static const struct got_error *
5970 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5972 static const struct got_error *err = NULL;
5973 struct got_reflist_head refs;
5974 struct got_reflist_entry *re;
5978 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5982 TAILQ_FOREACH(re, &refs, entry) {
5983 const char *refname;
5984 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5988 struct got_object_id *id;
5989 struct got_tag_object *tag;
5990 struct got_commit_object *commit = NULL;
5992 refname = got_ref_get_name(re->ref);
5993 if (strncmp(refname, "refs/tags/", 10) != 0)
5996 refstr = got_ref_to_str(re->ref);
5997 if (refstr == NULL) {
5998 err = got_error_from_errno("got_ref_to_str");
6001 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6004 err = got_ref_resolve(&id, repo, re->ref);
6007 err = got_object_open_as_tag(&tag, repo, id);
6009 if (err->code != GOT_ERR_OBJ_TYPE) {
6013 /* "lightweight" tag */
6014 err = got_object_open_as_commit(&commit, repo, id);
6019 tagger = got_object_commit_get_committer(commit);
6021 got_object_commit_get_committer_time(commit);
6022 err = got_object_id_str(&id_str, id);
6028 tagger = got_object_tag_get_tagger(tag);
6029 tagger_time = got_object_tag_get_tagger_time(tag);
6030 err = got_object_id_str(&id_str,
6031 got_object_tag_get_object_id(tag));
6035 printf("from: %s\n", tagger);
6036 datestr = get_datestr(&tagger_time, datebuf);
6038 printf("date: %s UTC\n", datestr);
6040 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6042 switch (got_object_tag_get_object_type(tag)) {
6043 case GOT_OBJ_TYPE_BLOB:
6044 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6047 case GOT_OBJ_TYPE_TREE:
6048 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6051 case GOT_OBJ_TYPE_COMMIT:
6052 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6055 case GOT_OBJ_TYPE_TAG:
6056 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6065 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6068 got_object_commit_close(commit);
6070 tagmsg0 = strdup(got_object_tag_get_message(tag));
6071 got_object_tag_close(tag);
6072 if (tagmsg0 == NULL) {
6073 err = got_error_from_errno("strdup");
6080 line = strsep(&tagmsg, "\n");
6082 printf(" %s\n", line);
6087 got_ref_list_free(&refs);
6091 static const struct got_error *
6092 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6093 const char *tag_name, const char *repo_path)
6095 const struct got_error *err = NULL;
6096 char *template = NULL, *initial_content = NULL;
6097 char *editor = NULL;
6098 int initial_content_len;
6101 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6102 err = got_error_from_errno("asprintf");
6106 initial_content_len = asprintf(&initial_content,
6107 "\n# tagging commit %s as %s\n",
6108 commit_id_str, tag_name);
6109 if (initial_content_len == -1) {
6110 err = got_error_from_errno("asprintf");
6114 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6118 if (write(fd, initial_content, initial_content_len) == -1) {
6119 err = got_error_from_errno2("write", *tagmsg_path);
6123 err = get_editor(&editor);
6126 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6127 initial_content_len, 1);
6129 free(initial_content);
6133 if (fd != -1 && close(fd) == -1 && err == NULL)
6134 err = got_error_from_errno2("close", *tagmsg_path);
6136 /* Editor is done; we can now apply unveil(2) */
6138 err = apply_unveil(repo_path, 0, NULL);
6146 static const struct got_error *
6147 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6148 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6150 const struct got_error *err = NULL;
6151 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6152 char *label = NULL, *commit_id_str = NULL;
6153 struct got_reference *ref = NULL;
6154 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6155 char *tagmsg_path = NULL, *tag_id_str = NULL;
6156 int preserve_tagmsg = 0;
6157 struct got_reflist_head refs;
6162 * Don't let the user create a tag name with a leading '-'.
6163 * While technically a valid reference name, this case is usually
6164 * an unintended typo.
6166 if (tag_name[0] == '-')
6167 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6169 err = get_author(&tagger, repo, worktree);
6173 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6177 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6178 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6182 err = got_object_id_str(&commit_id_str, commit_id);
6186 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6187 refname = strdup(tag_name);
6188 if (refname == NULL) {
6189 err = got_error_from_errno("strdup");
6193 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6194 err = got_error_from_errno("asprintf");
6198 err = got_ref_open(&ref, repo, refname, 0);
6200 err = got_error(GOT_ERR_TAG_EXISTS);
6202 } else if (err->code != GOT_ERR_NOT_REF)
6205 if (tagmsg_arg == NULL) {
6206 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6207 tag_name, got_repo_get_path(repo));
6209 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6210 tagmsg_path != NULL)
6211 preserve_tagmsg = 1;
6216 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6217 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6220 preserve_tagmsg = 1;
6224 err = got_ref_alloc(&ref, refname, tag_id);
6227 preserve_tagmsg = 1;
6231 err = got_ref_write(ref, repo);
6234 preserve_tagmsg = 1;
6238 err = got_object_id_str(&tag_id_str, tag_id);
6241 preserve_tagmsg = 1;
6244 printf("Created tag %s\n", tag_id_str);
6246 if (preserve_tagmsg) {
6247 fprintf(stderr, "%s: tag message preserved in %s\n",
6248 getprogname(), tagmsg_path);
6249 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6250 err = got_error_from_errno2("unlink", tagmsg_path);
6255 free(commit_id_str);
6260 got_ref_list_free(&refs);
6264 static const struct got_error *
6265 cmd_tag(int argc, char *argv[])
6267 const struct got_error *error = NULL;
6268 struct got_repository *repo = NULL;
6269 struct got_worktree *worktree = NULL;
6270 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6271 char *gitconfig_path = NULL;
6272 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6273 int ch, do_list = 0;
6275 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6278 commit_id_arg = optarg;
6284 repo_path = realpath(optarg, NULL);
6285 if (repo_path == NULL)
6286 return got_error_from_errno2("realpath",
6288 got_path_strip_trailing_slashes(repo_path);
6303 if (commit_id_arg != NULL)
6305 "-c option can only be used when creating a tag");
6307 option_conflict('l', 'm');
6310 } else if (argc != 1)
6317 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6321 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6322 "sendfd unveil", NULL) == -1)
6326 cwd = getcwd(NULL, 0);
6328 error = got_error_from_errno("getcwd");
6332 if (repo_path == NULL) {
6333 error = got_worktree_open(&worktree, cwd);
6334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6340 strdup(got_worktree_get_repo_path(worktree));
6341 if (repo_path == NULL)
6342 error = got_error_from_errno("strdup");
6346 repo_path = strdup(cwd);
6347 if (repo_path == NULL) {
6348 error = got_error_from_errno("strdup");
6355 error = got_repo_open(&repo, repo_path, NULL);
6358 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6361 error = list_tags(repo, worktree);
6363 error = get_gitconfig_path(&gitconfig_path);
6366 error = got_repo_open(&repo, repo_path, gitconfig_path);
6371 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6376 if (commit_id_arg == NULL) {
6377 struct got_reference *head_ref;
6378 struct got_object_id *commit_id;
6379 error = got_ref_open(&head_ref, repo,
6380 worktree ? got_worktree_get_head_ref_name(worktree)
6384 error = got_ref_resolve(&commit_id, repo, head_ref);
6385 got_ref_close(head_ref);
6388 error = got_object_id_str(&commit_id_str, commit_id);
6394 error = add_tag(repo, worktree, tag_name,
6395 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6399 const struct got_error *close_err = got_repo_close(repo);
6404 got_worktree_close(worktree);
6407 free(gitconfig_path);
6408 free(commit_id_str);
6415 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6420 static const struct got_error *
6421 add_progress(void *arg, unsigned char status, const char *path)
6423 while (path[0] == '/')
6425 printf("%c %s\n", status, path);
6429 static const struct got_error *
6430 cmd_add(int argc, char *argv[])
6432 const struct got_error *error = NULL;
6433 struct got_repository *repo = NULL;
6434 struct got_worktree *worktree = NULL;
6436 struct got_pathlist_head paths;
6437 struct got_pathlist_entry *pe;
6438 int ch, can_recurse = 0, no_ignores = 0;
6442 while ((ch = getopt(argc, argv, "IR")) != -1) {
6460 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6467 cwd = getcwd(NULL, 0);
6469 error = got_error_from_errno("getcwd");
6473 error = got_worktree_open(&worktree, cwd);
6475 if (error->code == GOT_ERR_NOT_WORKTREE)
6476 error = wrap_not_worktree_error(error, "add", cwd);
6480 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6485 error = apply_unveil(got_repo_get_path(repo), 1,
6486 got_worktree_get_root_path(worktree));
6490 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6494 if (!can_recurse && no_ignores) {
6495 error = got_error_msg(GOT_ERR_BAD_PATH,
6496 "disregarding ignores requires -R option");
6504 TAILQ_FOREACH(pe, &paths, entry) {
6505 if (asprintf(&ondisk_path, "%s/%s",
6506 got_worktree_get_root_path(worktree),
6508 error = got_error_from_errno("asprintf");
6511 if (lstat(ondisk_path, &sb) == -1) {
6512 if (errno == ENOENT) {
6516 error = got_error_from_errno2("lstat",
6522 if (S_ISDIR(sb.st_mode)) {
6523 error = got_error_msg(GOT_ERR_BAD_PATH,
6524 "adding directories requires -R option");
6530 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6531 NULL, repo, no_ignores);
6534 const struct got_error *close_err = got_repo_close(repo);
6539 got_worktree_close(worktree);
6540 TAILQ_FOREACH(pe, &paths, entry)
6541 free((char *)pe->path);
6542 got_pathlist_free(&paths);
6550 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6551 "path ...\n", getprogname());
6555 static const struct got_error *
6556 print_remove_status(void *arg, unsigned char status,
6557 unsigned char staged_status, const char *path)
6559 while (path[0] == '/')
6561 if (status == GOT_STATUS_NONEXISTENT)
6563 if (status == staged_status && (status == GOT_STATUS_DELETE))
6564 status = GOT_STATUS_NO_CHANGE;
6565 printf("%c%c %s\n", status, staged_status, path);
6569 static const struct got_error *
6570 cmd_remove(int argc, char *argv[])
6572 const struct got_error *error = NULL;
6573 struct got_worktree *worktree = NULL;
6574 struct got_repository *repo = NULL;
6575 const char *status_codes = NULL;
6577 struct got_pathlist_head paths;
6578 struct got_pathlist_entry *pe;
6579 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6583 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6586 delete_local_mods = 1;
6595 for (i = 0; i < strlen(optarg); i++) {
6596 switch (optarg[i]) {
6597 case GOT_STATUS_MODIFY:
6598 delete_local_mods = 1;
6600 case GOT_STATUS_MISSING:
6603 errx(1, "invalid status code '%c'",
6607 status_codes = optarg;
6619 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6626 cwd = getcwd(NULL, 0);
6628 error = got_error_from_errno("getcwd");
6631 error = got_worktree_open(&worktree, cwd);
6633 if (error->code == GOT_ERR_NOT_WORKTREE)
6634 error = wrap_not_worktree_error(error, "remove", cwd);
6638 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6643 error = apply_unveil(got_repo_get_path(repo), 1,
6644 got_worktree_get_root_path(worktree));
6648 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6655 TAILQ_FOREACH(pe, &paths, entry) {
6656 if (asprintf(&ondisk_path, "%s/%s",
6657 got_worktree_get_root_path(worktree),
6659 error = got_error_from_errno("asprintf");
6662 if (lstat(ondisk_path, &sb) == -1) {
6663 if (errno == ENOENT) {
6667 error = got_error_from_errno2("lstat",
6673 if (S_ISDIR(sb.st_mode)) {
6674 error = got_error_msg(GOT_ERR_BAD_PATH,
6675 "removing directories requires -R option");
6681 error = got_worktree_schedule_delete(worktree, &paths,
6682 delete_local_mods, status_codes, print_remove_status, NULL,
6683 repo, keep_on_disk);
6686 const struct got_error *close_err = got_repo_close(repo);
6691 got_worktree_close(worktree);
6692 TAILQ_FOREACH(pe, &paths, entry)
6693 free((char *)pe->path);
6694 got_pathlist_free(&paths);
6702 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6703 "path ...\n", getprogname());
6707 static const struct got_error *
6708 revert_progress(void *arg, unsigned char status, const char *path)
6710 if (status == GOT_STATUS_UNVERSIONED)
6713 while (path[0] == '/')
6715 printf("%c %s\n", status, path);
6719 struct choose_patch_arg {
6720 FILE *patch_script_file;
6724 static const struct got_error *
6725 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6726 int nchanges, const char *action)
6729 size_t linesize = 0;
6733 case GOT_STATUS_ADD:
6734 printf("A %s\n%s this addition? [y/n] ", path, action);
6736 case GOT_STATUS_DELETE:
6737 printf("D %s\n%s this deletion? [y/n] ", path, action);
6739 case GOT_STATUS_MODIFY:
6740 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6741 return got_error_from_errno("fseek");
6742 printf(GOT_COMMIT_SEP_STR);
6743 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6745 if (ferror(patch_file))
6746 return got_error_from_errno("getline");
6747 printf(GOT_COMMIT_SEP_STR);
6748 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6749 path, n, nchanges, action);
6752 return got_error_path(path, GOT_ERR_FILE_STATUS);
6758 static const struct got_error *
6759 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6760 FILE *patch_file, int n, int nchanges)
6762 const struct got_error *err = NULL;
6764 size_t linesize = 0;
6767 struct choose_patch_arg *a = arg;
6769 *choice = GOT_PATCH_CHOICE_NONE;
6771 if (a->patch_script_file) {
6773 err = show_change(status, path, patch_file, n, nchanges,
6777 linelen = getline(&line, &linesize, a->patch_script_file);
6778 if (linelen == -1) {
6779 if (ferror(a->patch_script_file))
6780 return got_error_from_errno("getline");
6783 nl = strchr(line, '\n');
6786 if (strcmp(line, "y") == 0) {
6787 *choice = GOT_PATCH_CHOICE_YES;
6789 } else if (strcmp(line, "n") == 0) {
6790 *choice = GOT_PATCH_CHOICE_NO;
6792 } else if (strcmp(line, "q") == 0 &&
6793 status == GOT_STATUS_MODIFY) {
6794 *choice = GOT_PATCH_CHOICE_QUIT;
6797 printf("invalid response '%s'\n", line);
6802 while (resp != 'y' && resp != 'n' && resp != 'q') {
6803 err = show_change(status, path, patch_file, n, nchanges,
6810 if (status == GOT_STATUS_MODIFY) {
6811 if (resp != 'y' && resp != 'n' && resp != 'q') {
6812 printf("invalid response '%c'\n", resp);
6815 } else if (resp != 'y' && resp != 'n') {
6816 printf("invalid response '%c'\n", resp);
6822 *choice = GOT_PATCH_CHOICE_YES;
6823 else if (resp == 'n')
6824 *choice = GOT_PATCH_CHOICE_NO;
6825 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6826 *choice = GOT_PATCH_CHOICE_QUIT;
6832 static const struct got_error *
6833 cmd_revert(int argc, char *argv[])
6835 const struct got_error *error = NULL;
6836 struct got_worktree *worktree = NULL;
6837 struct got_repository *repo = NULL;
6838 char *cwd = NULL, *path = NULL;
6839 struct got_pathlist_head paths;
6840 struct got_pathlist_entry *pe;
6841 int ch, can_recurse = 0, pflag = 0;
6842 FILE *patch_script_file = NULL;
6843 const char *patch_script_path = NULL;
6844 struct choose_patch_arg cpa;
6848 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6854 patch_script_path = optarg;
6869 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6870 "unveil", NULL) == -1)
6875 if (patch_script_path && !pflag)
6876 errx(1, "-F option can only be used together with -p option");
6878 cwd = getcwd(NULL, 0);
6880 error = got_error_from_errno("getcwd");
6883 error = got_worktree_open(&worktree, cwd);
6885 if (error->code == GOT_ERR_NOT_WORKTREE)
6886 error = wrap_not_worktree_error(error, "revert", cwd);
6890 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6895 if (patch_script_path) {
6896 patch_script_file = fopen(patch_script_path, "r");
6897 if (patch_script_file == NULL) {
6898 error = got_error_from_errno2("fopen",
6903 error = apply_unveil(got_repo_get_path(repo), 1,
6904 got_worktree_get_root_path(worktree));
6908 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6915 TAILQ_FOREACH(pe, &paths, entry) {
6916 if (asprintf(&ondisk_path, "%s/%s",
6917 got_worktree_get_root_path(worktree),
6919 error = got_error_from_errno("asprintf");
6922 if (lstat(ondisk_path, &sb) == -1) {
6923 if (errno == ENOENT) {
6927 error = got_error_from_errno2("lstat",
6933 if (S_ISDIR(sb.st_mode)) {
6934 error = got_error_msg(GOT_ERR_BAD_PATH,
6935 "reverting directories requires -R option");
6941 cpa.patch_script_file = patch_script_file;
6942 cpa.action = "revert";
6943 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6944 pflag ? choose_patch : NULL, &cpa, repo);
6946 if (patch_script_file && fclose(patch_script_file) == EOF &&
6948 error = got_error_from_errno2("fclose", patch_script_path);
6950 const struct got_error *close_err = got_repo_close(repo);
6955 got_worktree_close(worktree);
6964 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
6965 "[path ...]\n", getprogname());
6969 struct collect_commit_logmsg_arg {
6970 const char *cmdline_log;
6971 const char *prepared_log;
6972 int non_interactive;
6974 const char *worktree_path;
6975 const char *branch_name;
6976 const char *repo_path;
6981 static const struct got_error *
6982 read_prepared_logmsg(char **logmsg, const char *path)
6984 const struct got_error *err = NULL;
6990 memset(&sb, 0, sizeof(sb));
6992 f = fopen(path, "r");
6994 return got_error_from_errno2("fopen", path);
6996 if (fstat(fileno(f), &sb) == -1) {
6997 err = got_error_from_errno2("fstat", path);
7000 if (sb.st_size == 0) {
7001 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7005 *logmsg = malloc(sb.st_size + 1);
7006 if (*logmsg == NULL) {
7007 err = got_error_from_errno("malloc");
7011 r = fread(*logmsg, 1, sb.st_size, f);
7012 if (r != sb.st_size) {
7014 err = got_error_from_errno2("fread", path);
7016 err = got_error(GOT_ERR_IO);
7019 (*logmsg)[sb.st_size] = '\0';
7021 if (fclose(f) == EOF && err == NULL)
7022 err = got_error_from_errno2("fclose", path);
7031 static const struct got_error *
7032 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7035 char *initial_content = NULL;
7036 struct got_pathlist_entry *pe;
7037 const struct got_error *err = NULL;
7038 char *template = NULL;
7039 struct collect_commit_logmsg_arg *a = arg;
7040 int initial_content_len;
7044 /* if a message was specified on the command line, just use it */
7045 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7046 len = strlen(a->cmdline_log) + 1;
7047 *logmsg = malloc(len + 1);
7048 if (*logmsg == NULL)
7049 return got_error_from_errno("malloc");
7050 strlcpy(*logmsg, a->cmdline_log, len);
7052 } else if (a->prepared_log != NULL && a->non_interactive)
7053 return read_prepared_logmsg(logmsg, a->prepared_log);
7055 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7056 return got_error_from_errno("asprintf");
7058 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7062 if (a->prepared_log) {
7064 err = read_prepared_logmsg(&msg, a->prepared_log);
7067 if (write(fd, msg, strlen(msg)) == -1) {
7068 err = got_error_from_errno2("write", a->logmsg_path);
7075 initial_content_len = asprintf(&initial_content,
7076 "\n# changes to be committed on branch %s:\n",
7078 if (initial_content_len == -1) {
7079 err = got_error_from_errno("asprintf");
7083 if (write(fd, initial_content, initial_content_len) == -1) {
7084 err = got_error_from_errno2("write", a->logmsg_path);
7088 TAILQ_FOREACH(pe, commitable_paths, entry) {
7089 struct got_commitable *ct = pe->data;
7090 dprintf(fd, "# %c %s\n",
7091 got_commitable_get_status(ct),
7092 got_commitable_get_path(ct));
7095 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7096 initial_content_len, a->prepared_log ? 0 : 1);
7098 free(initial_content);
7101 if (fd != -1 && close(fd) == -1 && err == NULL)
7102 err = got_error_from_errno2("close", a->logmsg_path);
7104 /* Editor is done; we can now apply unveil(2) */
7106 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7114 static const struct got_error *
7115 cmd_commit(int argc, char *argv[])
7117 const struct got_error *error = NULL;
7118 struct got_worktree *worktree = NULL;
7119 struct got_repository *repo = NULL;
7120 char *cwd = NULL, *id_str = NULL;
7121 struct got_object_id *id = NULL;
7122 const char *logmsg = NULL;
7123 char *prepared_logmsg = NULL;
7124 struct collect_commit_logmsg_arg cl_arg;
7125 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7126 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7127 int allow_bad_symlinks = 0, non_interactive = 0;
7128 struct got_pathlist_head paths;
7131 cl_arg.logmsg_path = NULL;
7133 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7137 option_conflict('F', 'm');
7138 prepared_logmsg = realpath(optarg, NULL);
7139 if (prepared_logmsg == NULL)
7140 return got_error_from_errno2("realpath",
7144 if (prepared_logmsg)
7145 option_conflict('m', 'F');
7149 non_interactive = 1;
7152 allow_bad_symlinks = 1;
7164 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7165 "unveil", NULL) == -1)
7168 cwd = getcwd(NULL, 0);
7170 error = got_error_from_errno("getcwd");
7173 error = got_worktree_open(&worktree, cwd);
7175 if (error->code == GOT_ERR_NOT_WORKTREE)
7176 error = wrap_not_worktree_error(error, "commit", cwd);
7180 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7183 if (rebase_in_progress) {
7184 error = got_error(GOT_ERR_REBASING);
7188 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7193 error = get_gitconfig_path(&gitconfig_path);
7196 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7201 error = get_author(&author, repo, worktree);
7206 * unveil(2) traverses exec(2); if an editor is used we have
7207 * to apply unveil after the log message has been written.
7209 if (logmsg == NULL || strlen(logmsg) == 0)
7210 error = get_editor(&editor);
7212 error = apply_unveil(got_repo_get_path(repo), 0,
7213 got_worktree_get_root_path(worktree));
7217 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7221 cl_arg.editor = editor;
7222 cl_arg.cmdline_log = logmsg;
7223 cl_arg.prepared_log = prepared_logmsg;
7224 cl_arg.non_interactive = non_interactive;
7225 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7226 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7227 if (!histedit_in_progress) {
7228 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7229 error = got_error(GOT_ERR_COMMIT_BRANCH);
7232 cl_arg.branch_name += 11;
7234 cl_arg.repo_path = got_repo_get_path(repo);
7235 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7236 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7237 print_status, NULL, repo);
7239 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7240 cl_arg.logmsg_path != NULL)
7241 preserve_logmsg = 1;
7245 error = got_object_id_str(&id_str, id);
7248 printf("Created commit %s\n", id_str);
7250 if (preserve_logmsg) {
7251 fprintf(stderr, "%s: log message preserved in %s\n",
7252 getprogname(), cl_arg.logmsg_path);
7253 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7255 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7256 free(cl_arg.logmsg_path);
7258 const struct got_error *close_err = got_repo_close(repo);
7263 got_worktree_close(worktree);
7266 free(gitconfig_path);
7269 free(prepared_logmsg);
7274 usage_cherrypick(void)
7276 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7280 static const struct got_error *
7281 cmd_cherrypick(int argc, char *argv[])
7283 const struct got_error *error = NULL;
7284 struct got_worktree *worktree = NULL;
7285 struct got_repository *repo = NULL;
7286 char *cwd = NULL, *commit_id_str = NULL;
7287 struct got_object_id *commit_id = NULL;
7288 struct got_commit_object *commit = NULL;
7289 struct got_object_qid *pid;
7290 struct got_reference *head_ref = NULL;
7292 struct got_update_progress_arg upa;
7294 while ((ch = getopt(argc, argv, "")) != -1) {
7306 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7307 "unveil", NULL) == -1)
7313 cwd = getcwd(NULL, 0);
7315 error = got_error_from_errno("getcwd");
7318 error = got_worktree_open(&worktree, cwd);
7320 if (error->code == GOT_ERR_NOT_WORKTREE)
7321 error = wrap_not_worktree_error(error, "cherrypick",
7326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7331 error = apply_unveil(got_repo_get_path(repo), 0,
7332 got_worktree_get_root_path(worktree));
7336 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7337 GOT_OBJ_TYPE_COMMIT, repo);
7338 if (error != NULL) {
7339 struct got_reference *ref;
7340 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7342 error = got_ref_open(&ref, repo, argv[0], 0);
7345 error = got_ref_resolve(&commit_id, repo, ref);
7350 error = got_object_id_str(&commit_id_str, commit_id);
7354 error = got_ref_open(&head_ref, repo,
7355 got_worktree_get_head_ref_name(worktree), 0);
7359 error = check_same_branch(commit_id, head_ref, NULL, repo);
7361 if (error->code != GOT_ERR_ANCESTRY)
7365 error = got_error(GOT_ERR_SAME_BRANCH);
7369 error = got_object_open_as_commit(&commit, repo, commit_id);
7372 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7373 memset(&upa, 0, sizeof(upa));
7374 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7375 commit_id, repo, update_progress, &upa, check_cancelled,
7380 if (upa.did_something)
7381 printf("Merged commit %s\n", commit_id_str);
7382 print_update_progress_stats(&upa);
7385 got_object_commit_close(commit);
7386 free(commit_id_str);
7388 got_ref_close(head_ref);
7390 got_worktree_close(worktree);
7392 const struct got_error *close_err = got_repo_close(repo);
7402 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7406 static const struct got_error *
7407 cmd_backout(int argc, char *argv[])
7409 const struct got_error *error = NULL;
7410 struct got_worktree *worktree = NULL;
7411 struct got_repository *repo = NULL;
7412 char *cwd = NULL, *commit_id_str = NULL;
7413 struct got_object_id *commit_id = NULL;
7414 struct got_commit_object *commit = NULL;
7415 struct got_object_qid *pid;
7416 struct got_reference *head_ref = NULL;
7418 struct got_update_progress_arg upa;
7420 while ((ch = getopt(argc, argv, "")) != -1) {
7432 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7433 "unveil", NULL) == -1)
7439 cwd = getcwd(NULL, 0);
7441 error = got_error_from_errno("getcwd");
7444 error = got_worktree_open(&worktree, cwd);
7446 if (error->code == GOT_ERR_NOT_WORKTREE)
7447 error = wrap_not_worktree_error(error, "backout", cwd);
7451 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7456 error = apply_unveil(got_repo_get_path(repo), 0,
7457 got_worktree_get_root_path(worktree));
7461 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7462 GOT_OBJ_TYPE_COMMIT, repo);
7463 if (error != NULL) {
7464 struct got_reference *ref;
7465 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7467 error = got_ref_open(&ref, repo, argv[0], 0);
7470 error = got_ref_resolve(&commit_id, repo, ref);
7475 error = got_object_id_str(&commit_id_str, commit_id);
7479 error = got_ref_open(&head_ref, repo,
7480 got_worktree_get_head_ref_name(worktree), 0);
7484 error = check_same_branch(commit_id, head_ref, NULL, repo);
7488 error = got_object_open_as_commit(&commit, repo, commit_id);
7491 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7493 error = got_error(GOT_ERR_ROOT_COMMIT);
7497 memset(&upa, 0, sizeof(upa));
7498 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7499 update_progress, &upa, check_cancelled, NULL);
7503 if (upa.did_something)
7504 printf("Backed out commit %s\n", commit_id_str);
7505 print_update_progress_stats(&upa);
7508 got_object_commit_close(commit);
7509 free(commit_id_str);
7511 got_ref_close(head_ref);
7513 got_worktree_close(worktree);
7515 const struct got_error *close_err = got_repo_close(repo);
7525 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [branch]\n",
7531 trim_logmsg(char *logmsg, int limit)
7536 len = strlen(logmsg);
7540 nl = strchr(logmsg, '\n');
7545 static const struct got_error *
7546 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7548 const struct got_error *err;
7549 char *logmsg0 = NULL;
7552 err = got_object_commit_get_logmsg(&logmsg0, commit);
7557 while (isspace((unsigned char)s[0]))
7560 *logmsg = strdup(s);
7561 if (*logmsg == NULL) {
7562 err = got_error_from_errno("strdup");
7566 trim_logmsg(*logmsg, limit);
7572 static const struct got_error *
7573 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7575 const struct got_error *err;
7576 struct got_commit_object *commit = NULL;
7577 char *id_str = NULL, *logmsg = NULL;
7579 err = got_object_open_as_commit(&commit, repo, id);
7583 err = got_object_id_str(&id_str, id);
7589 err = get_short_logmsg(&logmsg, 42, commit);
7593 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7596 got_object_commit_close(commit);
7601 static const struct got_error *
7602 show_rebase_progress(struct got_commit_object *commit,
7603 struct got_object_id *old_id, struct got_object_id *new_id)
7605 const struct got_error *err;
7606 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7608 err = got_object_id_str(&old_id_str, old_id);
7613 err = got_object_id_str(&new_id_str, new_id);
7618 old_id_str[12] = '\0';
7620 new_id_str[12] = '\0';
7622 err = get_short_logmsg(&logmsg, 42, commit);
7626 printf("%s -> %s: %s\n", old_id_str,
7627 new_id_str ? new_id_str : "no-op change", logmsg);
7635 static const struct got_error *
7636 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7637 struct got_reference *branch, struct got_reference *new_base_branch,
7638 struct got_reference *tmp_branch, struct got_repository *repo,
7641 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7642 return got_worktree_rebase_complete(worktree, fileindex,
7643 new_base_branch, tmp_branch, branch, repo, create_backup);
7646 static const struct got_error *
7647 rebase_commit(struct got_pathlist_head *merged_paths,
7648 struct got_worktree *worktree, struct got_fileindex *fileindex,
7649 struct got_reference *tmp_branch,
7650 struct got_object_id *commit_id, struct got_repository *repo)
7652 const struct got_error *error;
7653 struct got_commit_object *commit;
7654 struct got_object_id *new_commit_id;
7656 error = got_object_open_as_commit(&commit, repo, commit_id);
7660 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7661 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7663 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7665 error = show_rebase_progress(commit, commit_id, NULL);
7667 error = show_rebase_progress(commit, commit_id, new_commit_id);
7668 free(new_commit_id);
7671 got_object_commit_close(commit);
7675 struct check_path_prefix_arg {
7676 const char *path_prefix;
7681 static const struct got_error *
7682 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7683 struct got_blob_object *blob2, struct got_object_id *id1,
7684 struct got_object_id *id2, const char *path1, const char *path2,
7685 mode_t mode1, mode_t mode2, struct got_repository *repo)
7687 struct check_path_prefix_arg *a = arg;
7689 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7690 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7691 return got_error(a->errcode);
7696 static const struct got_error *
7697 check_path_prefix(struct got_object_id *parent_id,
7698 struct got_object_id *commit_id, const char *path_prefix,
7699 int errcode, struct got_repository *repo)
7701 const struct got_error *err;
7702 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7703 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7704 struct check_path_prefix_arg cpp_arg;
7706 if (got_path_is_root_dir(path_prefix))
7709 err = got_object_open_as_commit(&commit, repo, commit_id);
7713 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7717 err = got_object_open_as_tree(&tree1, repo,
7718 got_object_commit_get_tree_id(parent_commit));
7722 err = got_object_open_as_tree(&tree2, repo,
7723 got_object_commit_get_tree_id(commit));
7727 cpp_arg.path_prefix = path_prefix;
7728 while (cpp_arg.path_prefix[0] == '/')
7729 cpp_arg.path_prefix++;
7730 cpp_arg.len = strlen(cpp_arg.path_prefix);
7731 cpp_arg.errcode = errcode;
7732 err = got_diff_tree(tree1, tree2, "", "", repo,
7733 check_path_prefix_in_diff, &cpp_arg, 0);
7736 got_object_tree_close(tree1);
7738 got_object_tree_close(tree2);
7740 got_object_commit_close(commit);
7742 got_object_commit_close(parent_commit);
7746 static const struct got_error *
7747 collect_commits(struct got_object_id_queue *commits,
7748 struct got_object_id *initial_commit_id,
7749 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7750 const char *path_prefix, int path_prefix_errcode,
7751 struct got_repository *repo)
7753 const struct got_error *err = NULL;
7754 struct got_commit_graph *graph = NULL;
7755 struct got_object_id *parent_id = NULL;
7756 struct got_object_qid *qid;
7757 struct got_object_id *commit_id = initial_commit_id;
7759 err = got_commit_graph_open(&graph, "/", 1);
7763 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7764 check_cancelled, NULL);
7767 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7768 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7769 check_cancelled, NULL);
7771 if (err->code == GOT_ERR_ITER_COMPLETED) {
7772 err = got_error_msg(GOT_ERR_ANCESTRY,
7773 "ran out of commits to rebase before "
7774 "youngest common ancestor commit has "
7779 err = check_path_prefix(parent_id, commit_id,
7780 path_prefix, path_prefix_errcode, repo);
7784 err = got_object_qid_alloc(&qid, commit_id);
7787 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7788 commit_id = parent_id;
7792 got_commit_graph_close(graph);
7796 static const struct got_error *
7797 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
7799 const struct got_error *err = NULL;
7800 time_t committer_time;
7802 char datebuf[11]; /* YYYY-MM-DD + NUL */
7803 char *author0 = NULL, *author, *smallerthan;
7804 char *logmsg0 = NULL, *logmsg, *newline;
7806 committer_time = got_object_commit_get_committer_time(commit);
7807 if (localtime_r(&committer_time, &tm) == NULL)
7808 return got_error_from_errno("localtime_r");
7809 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
7810 return got_error(GOT_ERR_NO_SPACE);
7812 author0 = strdup(got_object_commit_get_author(commit));
7813 if (author0 == NULL)
7814 return got_error_from_errno("strdup");
7816 smallerthan = strchr(author, '<');
7817 if (smallerthan && smallerthan[1] != '\0')
7818 author = smallerthan + 1;
7819 author[strcspn(author, "@>")] = '\0';
7821 err = got_object_commit_get_logmsg(&logmsg0, commit);
7825 while (*logmsg == '\n')
7827 newline = strchr(logmsg, '\n');
7831 if (asprintf(brief_str, "%s %s %s",
7832 datebuf, author, logmsg) == -1)
7833 err = got_error_from_errno("asprintf");
7840 static const struct got_error *
7841 print_backup_ref(const char *branch_name, const char *new_id_str,
7842 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
7843 struct got_reflist_object_id_map *refs_idmap,
7844 struct got_repository *repo)
7846 const struct got_error *err = NULL;
7847 struct got_reflist_head *refs;
7848 char *refs_str = NULL;
7849 struct got_object_id *new_commit_id = NULL;
7850 struct got_commit_object *new_commit = NULL;
7851 char *new_commit_brief_str = NULL;
7852 struct got_object_id *yca_id = NULL;
7853 struct got_commit_object *yca_commit = NULL;
7854 char *yca_id_str = NULL, *yca_brief_str = NULL;
7855 char *custom_refs_str;
7857 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
7858 return got_error_from_errno("asprintf");
7860 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
7861 0, 0, refs_idmap, custom_refs_str);
7865 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
7869 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
7871 err = build_refs_str(&refs_str, refs, new_commit_id, repo);
7876 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
7880 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
7884 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7885 old_commit_id, new_commit_id, repo, check_cancelled, NULL);
7889 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
7890 refs_str ? " (" : "", refs_str ? refs_str : "",
7891 refs_str ? ")" : "", new_commit_brief_str);
7892 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
7893 got_object_id_cmp(yca_id, old_commit_id) != 0) {
7897 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
7901 err = get_commit_brief_str(&yca_brief_str, yca_commit);
7905 err = got_object_id_str(&yca_id_str, yca_id);
7909 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
7911 err = build_refs_str(&refs_str, refs, yca_id, repo);
7915 printf("history forked at %s%s%s%s\n %s\n",
7917 refs_str ? " (" : "", refs_str ? refs_str : "",
7918 refs_str ? ")" : "", yca_brief_str);
7921 free(custom_refs_str);
7922 free(new_commit_id);
7926 free(yca_brief_str);
7928 got_object_commit_close(new_commit);
7930 got_object_commit_close(yca_commit);
7935 static const struct got_error *
7936 list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
7937 struct got_repository *repo)
7939 const struct got_error *err;
7940 struct got_reflist_head refs, backup_refs;
7941 struct got_reflist_entry *re;
7942 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
7943 struct got_object_id *old_commit_id = NULL;
7944 char *branch_name = NULL;
7945 struct got_commit_object *old_commit = NULL;
7946 struct got_reflist_object_id_map *refs_idmap = NULL;
7947 int wanted_branch_found = 0;
7950 TAILQ_INIT(&backup_refs);
7952 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7956 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
7960 if (wanted_branch_name) {
7961 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
7962 wanted_branch_name += 11;
7965 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
7966 got_ref_cmp_by_commit_timestamp_descending, repo);
7970 TAILQ_FOREACH(re, &backup_refs, entry) {
7971 const char *refname = got_ref_get_name(re->ref);
7974 err = got_ref_resolve(&old_commit_id, repo, re->ref);
7978 err = got_object_open_as_commit(&old_commit, repo,
7983 if (strncmp(backup_ref_prefix, refname,
7984 backup_ref_prefix_len) == 0)
7985 refname += backup_ref_prefix_len;
7987 while (refname[0] == '/')
7990 branch_name = strdup(refname);
7991 if (branch_name == NULL) {
7992 err = got_error_from_errno("strdup");
7995 slash = strrchr(branch_name, '/');
7998 refname += strlen(branch_name) + 1;
8001 if (wanted_branch_name == NULL ||
8002 strcmp(wanted_branch_name, branch_name) == 0) {
8003 wanted_branch_found = 1;
8004 err = print_backup_ref(branch_name, refname,
8005 old_commit_id, old_commit, refs_idmap, repo);
8010 free(old_commit_id);
8011 old_commit_id = NULL;
8014 got_object_commit_close(old_commit);
8018 if (wanted_branch_name && !wanted_branch_found) {
8019 err = got_error_fmt(GOT_ERR_NOT_REF,
8020 "%s/%s/", backup_ref_prefix, wanted_branch_name);
8024 got_reflist_object_id_map_free(refs_idmap);
8025 got_ref_list_free(&refs);
8026 got_ref_list_free(&backup_refs);
8027 free(old_commit_id);
8030 got_object_commit_close(old_commit);
8034 static const struct got_error *
8035 cmd_rebase(int argc, char *argv[])
8037 const struct got_error *error = NULL;
8038 struct got_worktree *worktree = NULL;
8039 struct got_repository *repo = NULL;
8040 struct got_fileindex *fileindex = NULL;
8042 struct got_reference *branch = NULL;
8043 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8044 struct got_object_id *commit_id = NULL, *parent_id = NULL;
8045 struct got_object_id *resume_commit_id = NULL;
8046 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8047 struct got_commit_object *commit = NULL;
8048 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8049 int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8050 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8051 struct got_object_id_queue commits;
8052 struct got_pathlist_head merged_paths;
8053 const struct got_object_id_queue *parent_ids;
8054 struct got_object_qid *qid, *pid;
8056 SIMPLEQ_INIT(&commits);
8057 TAILQ_INIT(&merged_paths);
8059 while ((ch = getopt(argc, argv, "acl")) != -1) {
8065 continue_rebase = 1;
8080 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8081 "unveil", NULL) == -1)
8086 option_conflict('l', 'a');
8087 if (continue_rebase)
8088 option_conflict('l', 'c');
8089 if (argc != 0 && argc != 1)
8092 if (abort_rebase && continue_rebase)
8094 else if (abort_rebase || continue_rebase) {
8097 } else if (argc != 1)
8101 cwd = getcwd(NULL, 0);
8103 error = got_error_from_errno("getcwd");
8106 error = got_worktree_open(&worktree, cwd);
8109 if (error->code != GOT_ERR_NOT_WORKTREE)
8112 if (error->code == GOT_ERR_NOT_WORKTREE)
8113 error = wrap_not_worktree_error(error,
8119 error = got_repo_open(&repo,
8120 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8124 error = apply_unveil(got_repo_get_path(repo), 0,
8125 worktree ? got_worktree_get_root_path(worktree) : NULL);
8130 error = list_backup_refs(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8131 argc == 1 ? argv[0] : NULL, repo);
8132 goto done; /* nothing else to do */
8135 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8139 if (histedit_in_progress) {
8140 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8144 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8149 struct got_update_progress_arg upa;
8150 if (!rebase_in_progress) {
8151 error = got_error(GOT_ERR_NOT_REBASING);
8154 error = got_worktree_rebase_continue(&resume_commit_id,
8155 &new_base_branch, &tmp_branch, &branch, &fileindex,
8159 printf("Switching work tree to %s\n",
8160 got_ref_get_symref_target(new_base_branch));
8161 memset(&upa, 0, sizeof(upa));
8162 error = got_worktree_rebase_abort(worktree, fileindex, repo,
8163 new_base_branch, update_progress, &upa);
8166 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8167 print_update_progress_stats(&upa);
8168 goto done; /* nothing else to do */
8171 if (continue_rebase) {
8172 if (!rebase_in_progress) {
8173 error = got_error(GOT_ERR_NOT_REBASING);
8176 error = got_worktree_rebase_continue(&resume_commit_id,
8177 &new_base_branch, &tmp_branch, &branch, &fileindex,
8182 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8183 resume_commit_id, repo);
8187 yca_id = got_object_id_dup(resume_commit_id);
8188 if (yca_id == NULL) {
8189 error = got_error_from_errno("got_object_id_dup");
8193 error = got_ref_open(&branch, repo, argv[0], 0);
8198 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8202 if (!continue_rebase) {
8203 struct got_object_id *base_commit_id;
8205 base_commit_id = got_worktree_get_base_commit_id(worktree);
8206 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8207 base_commit_id, branch_head_commit_id, repo,
8208 check_cancelled, NULL);
8211 if (yca_id == NULL) {
8212 error = got_error_msg(GOT_ERR_ANCESTRY,
8213 "specified branch shares no common ancestry "
8214 "with work tree's branch");
8218 error = check_same_branch(base_commit_id, branch, yca_id, repo);
8220 if (error->code != GOT_ERR_ANCESTRY)
8224 static char msg[128];
8225 snprintf(msg, sizeof(msg),
8226 "%s is already based on %s",
8227 got_ref_get_name(branch),
8228 got_worktree_get_head_ref_name(worktree));
8229 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8232 error = got_worktree_rebase_prepare(&new_base_branch,
8233 &tmp_branch, &fileindex, worktree, branch, repo);
8238 commit_id = branch_head_commit_id;
8239 error = got_object_open_as_commit(&commit, repo, commit_id);
8243 parent_ids = got_object_commit_get_parent_ids(commit);
8244 pid = SIMPLEQ_FIRST(parent_ids);
8246 if (!continue_rebase) {
8247 struct got_update_progress_arg upa;
8248 memset(&upa, 0, sizeof(upa));
8249 error = got_worktree_rebase_abort(worktree, fileindex,
8250 repo, new_base_branch, update_progress, &upa);
8253 printf("Rebase of %s aborted\n",
8254 got_ref_get_name(branch));
8255 print_update_progress_stats(&upa);
8258 error = got_error(GOT_ERR_EMPTY_REBASE);
8261 error = collect_commits(&commits, commit_id, pid->id,
8262 yca_id, got_worktree_get_path_prefix(worktree),
8263 GOT_ERR_REBASE_PATH, repo);
8264 got_object_commit_close(commit);
8269 if (SIMPLEQ_EMPTY(&commits)) {
8270 if (continue_rebase) {
8271 error = rebase_complete(worktree, fileindex,
8272 branch, new_base_branch, tmp_branch, repo,
8276 /* Fast-forward the reference of the branch. */
8277 struct got_object_id *new_head_commit_id;
8279 error = got_ref_resolve(&new_head_commit_id, repo,
8283 error = got_object_id_str(&id_str, new_head_commit_id);
8284 printf("Forwarding %s to commit %s\n",
8285 got_ref_get_name(branch), id_str);
8287 error = got_ref_change_ref(branch,
8288 new_head_commit_id);
8291 /* No backup needed since objects did not change. */
8297 SIMPLEQ_FOREACH(qid, &commits, entry) {
8298 struct got_update_progress_arg upa;
8300 commit_id = qid->id;
8301 parent_id = pid ? pid->id : yca_id;
8304 memset(&upa, 0, sizeof(upa));
8305 error = got_worktree_rebase_merge_files(&merged_paths,
8306 worktree, fileindex, parent_id, commit_id, repo,
8307 update_progress, &upa, check_cancelled, NULL);
8311 print_update_progress_stats(&upa);
8312 if (upa.conflicts > 0)
8313 rebase_status = GOT_STATUS_CONFLICT;
8315 if (rebase_status == GOT_STATUS_CONFLICT) {
8316 error = show_rebase_merge_conflict(qid->id, repo);
8319 got_worktree_rebase_pathlist_free(&merged_paths);
8323 error = rebase_commit(&merged_paths, worktree, fileindex,
8324 tmp_branch, commit_id, repo);
8325 got_worktree_rebase_pathlist_free(&merged_paths);
8330 if (rebase_status == GOT_STATUS_CONFLICT) {
8331 error = got_worktree_rebase_postpone(worktree, fileindex);
8334 error = got_error_msg(GOT_ERR_CONFLICTS,
8335 "conflicts must be resolved before rebasing can continue");
8337 error = rebase_complete(worktree, fileindex, branch,
8338 new_base_branch, tmp_branch, repo, create_backup);
8340 got_object_id_queue_free(&commits);
8341 free(branch_head_commit_id);
8342 free(resume_commit_id);
8345 got_object_commit_close(commit);
8347 got_ref_close(branch);
8348 if (new_base_branch)
8349 got_ref_close(new_base_branch);
8351 got_ref_close(tmp_branch);
8353 got_worktree_close(worktree);
8355 const struct got_error *close_err = got_repo_close(repo);
8363 usage_histedit(void)
8365 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8366 "[-F histedit-script] [-m] [-l] [branch]\n", getprogname());
8370 #define GOT_HISTEDIT_PICK 'p'
8371 #define GOT_HISTEDIT_EDIT 'e'
8372 #define GOT_HISTEDIT_FOLD 'f'
8373 #define GOT_HISTEDIT_DROP 'd'
8374 #define GOT_HISTEDIT_MESG 'm'
8376 static struct got_histedit_cmd {
8380 } got_histedit_cmds[] = {
8381 { GOT_HISTEDIT_PICK, "pick", "use commit" },
8382 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
8383 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
8385 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
8386 { GOT_HISTEDIT_MESG, "mesg",
8387 "single-line log message for commit above (open editor if empty)" },
8390 struct got_histedit_list_entry {
8391 TAILQ_ENTRY(got_histedit_list_entry) entry;
8392 struct got_object_id *commit_id;
8393 const struct got_histedit_cmd *cmd;
8396 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8398 static const struct got_error *
8399 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8400 FILE *f, struct got_repository *repo)
8402 const struct got_error *err = NULL;
8403 char *logmsg = NULL, *id_str = NULL;
8404 struct got_commit_object *commit = NULL;
8407 err = got_object_open_as_commit(&commit, repo, commit_id);
8411 err = get_short_logmsg(&logmsg, 34, commit);
8415 err = got_object_id_str(&id_str, commit_id);
8419 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8421 err = got_ferror(f, GOT_ERR_IO);
8424 got_object_commit_close(commit);
8430 static const struct got_error *
8431 histedit_write_commit_list(struct got_object_id_queue *commits,
8432 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8434 const struct got_error *err = NULL;
8435 struct got_object_qid *qid;
8436 const char *histedit_cmd = NULL;
8438 if (SIMPLEQ_EMPTY(commits))
8439 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8441 SIMPLEQ_FOREACH(qid, commits, entry) {
8442 histedit_cmd = got_histedit_cmds[0].name;
8443 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8444 histedit_cmd = "fold";
8445 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8448 if (edit_logmsg_only) {
8449 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8451 err = got_ferror(f, GOT_ERR_IO);
8460 static const struct got_error *
8461 write_cmd_list(FILE *f, const char *branch_name,
8462 struct got_object_id_queue *commits)
8464 const struct got_error *err = NULL;
8468 struct got_object_qid *qid;
8470 qid = SIMPLEQ_FIRST(commits);
8471 err = got_object_id_str(&id_str, qid->id);
8476 "# Editing the history of branch '%s' starting at\n"
8478 "# Commits will be processed in order from top to "
8479 "bottom of this file.\n", branch_name, id_str);
8481 err = got_ferror(f, GOT_ERR_IO);
8485 n = fprintf(f, "# Available histedit commands:\n");
8487 err = got_ferror(f, GOT_ERR_IO);
8491 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8492 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8493 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8496 err = got_ferror(f, GOT_ERR_IO);
8505 static const struct got_error *
8506 histedit_syntax_error(int lineno)
8508 static char msg[42];
8511 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8513 if (ret == -1 || ret >= sizeof(msg))
8514 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8516 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8519 static const struct got_error *
8520 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8521 char *logmsg, struct got_repository *repo)
8523 const struct got_error *err;
8524 struct got_commit_object *folded_commit = NULL;
8525 char *id_str, *folded_logmsg = NULL;
8527 err = got_object_id_str(&id_str, hle->commit_id);
8531 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8535 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8538 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8539 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8540 folded_logmsg) == -1) {
8541 err = got_error_from_errno("asprintf");
8545 got_object_commit_close(folded_commit);
8547 free(folded_logmsg);
8551 static struct got_histedit_list_entry *
8552 get_folded_commits(struct got_histedit_list_entry *hle)
8554 struct got_histedit_list_entry *prev, *folded = NULL;
8556 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8557 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8558 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8559 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8561 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8567 static const struct got_error *
8568 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8569 struct got_repository *repo)
8571 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8572 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8573 const struct got_error *err = NULL;
8574 struct got_commit_object *commit = NULL;
8577 struct got_histedit_list_entry *folded = NULL;
8579 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8583 folded = get_folded_commits(hle);
8585 while (folded != hle) {
8586 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8587 folded = TAILQ_NEXT(folded, entry);
8590 err = append_folded_commit_msg(&new_msg, folded,
8596 folded = TAILQ_NEXT(folded, entry);
8600 err = got_object_id_str(&id_str, hle->commit_id);
8603 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8606 logmsg_len = asprintf(&new_msg,
8607 "%s\n# original log message of commit %s: %s",
8608 logmsg ? logmsg : "", id_str, orig_logmsg);
8609 if (logmsg_len == -1) {
8610 err = got_error_from_errno("asprintf");
8616 err = got_object_id_str(&id_str, hle->commit_id);
8620 err = got_opentemp_named_fd(&logmsg_path, &fd,
8621 GOT_TMPDIR_STR "/got-logmsg");
8625 write(fd, logmsg, logmsg_len);
8628 err = get_editor(&editor);
8632 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8635 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8638 hle->logmsg = strdup(new_msg);
8639 if (hle->logmsg == NULL)
8640 err = got_error_from_errno("strdup");
8643 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8644 err = got_error_from_errno2("unlink", logmsg_path);
8650 got_object_commit_close(commit);
8654 static const struct got_error *
8655 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8656 FILE *f, struct got_repository *repo)
8658 const struct got_error *err = NULL;
8659 char *line = NULL, *p, *end;
8663 const struct got_histedit_cmd *cmd;
8664 struct got_object_id *commit_id = NULL;
8665 struct got_histedit_list_entry *hle = NULL;
8668 len = getline(&line, &size, f);
8670 const struct got_error *getline_err;
8673 getline_err = got_error_from_errno("getline");
8674 err = got_ferror(f, getline_err->code);
8679 while (isspace((unsigned char)p[0]))
8681 if (p[0] == '#' || p[0] == '\0') {
8687 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8688 cmd = &got_histedit_cmds[i];
8689 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8690 isspace((unsigned char)p[strlen(cmd->name)])) {
8691 p += strlen(cmd->name);
8694 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8699 if (i == nitems(got_histedit_cmds)) {
8700 err = histedit_syntax_error(lineno);
8703 while (isspace((unsigned char)p[0]))
8705 if (cmd->code == GOT_HISTEDIT_MESG) {
8706 if (hle == NULL || hle->logmsg != NULL) {
8707 err = got_error(GOT_ERR_HISTEDIT_CMD);
8711 err = histedit_edit_logmsg(hle, repo);
8715 hle->logmsg = strdup(p);
8716 if (hle->logmsg == NULL) {
8717 err = got_error_from_errno("strdup");
8726 while (end[0] && !isspace((unsigned char)end[0]))
8730 err = got_object_resolve_id_str(&commit_id, repo, p);
8732 /* override error code */
8733 err = histedit_syntax_error(lineno);
8737 hle = malloc(sizeof(*hle));
8739 err = got_error_from_errno("malloc");
8743 hle->commit_id = commit_id;
8748 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8756 static const struct got_error *
8757 histedit_check_script(struct got_histedit_list *histedit_cmds,
8758 struct got_object_id_queue *commits, struct got_repository *repo)
8760 const struct got_error *err = NULL;
8761 struct got_object_qid *qid;
8762 struct got_histedit_list_entry *hle;
8763 static char msg[92];
8766 if (TAILQ_EMPTY(histedit_cmds))
8767 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8768 "histedit script contains no commands");
8769 if (SIMPLEQ_EMPTY(commits))
8770 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8772 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8773 struct got_histedit_list_entry *hle2;
8774 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8777 if (got_object_id_cmp(hle->commit_id,
8778 hle2->commit_id) != 0)
8780 err = got_object_id_str(&id_str, hle->commit_id);
8783 snprintf(msg, sizeof(msg), "commit %s is listed "
8784 "more than once in histedit script", id_str);
8786 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8790 SIMPLEQ_FOREACH(qid, commits, entry) {
8791 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8792 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8796 err = got_object_id_str(&id_str, qid->id);
8799 snprintf(msg, sizeof(msg),
8800 "commit %s missing from histedit script", id_str);
8802 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8806 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8807 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8808 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8809 "last commit in histedit script cannot be folded");
8814 static const struct got_error *
8815 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8816 const char *path, struct got_object_id_queue *commits,
8817 struct got_repository *repo)
8819 const struct got_error *err = NULL;
8823 err = get_editor(&editor);
8827 if (spawn_editor(editor, path) == -1) {
8828 err = got_error_from_errno("failed spawning editor");
8832 f = fopen(path, "r");
8834 err = got_error_from_errno("fopen");
8837 err = histedit_parse_list(histedit_cmds, f, repo);
8841 err = histedit_check_script(histedit_cmds, commits, repo);
8843 if (f && fclose(f) == EOF && err == NULL)
8844 err = got_error_from_errno("fclose");
8849 static const struct got_error *
8850 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8851 struct got_object_id_queue *, const char *, const char *,
8852 struct got_repository *);
8854 static const struct got_error *
8855 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8856 struct got_object_id_queue *commits, const char *branch_name,
8857 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8859 const struct got_error *err;
8863 err = got_opentemp_named(&path, &f, "got-histedit");
8867 err = write_cmd_list(f, branch_name, commits);
8871 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8876 if (edit_logmsg_only || fold_only) {
8878 err = histedit_parse_list(histedit_cmds, f, repo);
8880 if (fclose(f) == EOF) {
8881 err = got_error_from_errno("fclose");
8885 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8887 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8888 err->code != GOT_ERR_HISTEDIT_CMD)
8890 err = histedit_edit_list_retry(histedit_cmds, err,
8891 commits, path, branch_name, repo);
8895 if (f && fclose(f) == EOF && err == NULL)
8896 err = got_error_from_errno("fclose");
8897 if (path && unlink(path) != 0 && err == NULL)
8898 err = got_error_from_errno2("unlink", path);
8903 static const struct got_error *
8904 histedit_save_list(struct got_histedit_list *histedit_cmds,
8905 struct got_worktree *worktree, struct got_repository *repo)
8907 const struct got_error *err = NULL;
8910 struct got_histedit_list_entry *hle;
8911 struct got_commit_object *commit = NULL;
8913 err = got_worktree_get_histedit_script_path(&path, worktree);
8917 f = fopen(path, "w");
8919 err = got_error_from_errno2("fopen", path);
8922 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8923 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8929 int n = fprintf(f, "%c %s\n",
8930 GOT_HISTEDIT_MESG, hle->logmsg);
8932 err = got_ferror(f, GOT_ERR_IO);
8938 if (f && fclose(f) == EOF && err == NULL)
8939 err = got_error_from_errno("fclose");
8942 got_object_commit_close(commit);
8947 histedit_free_list(struct got_histedit_list *histedit_cmds)
8949 struct got_histedit_list_entry *hle;
8951 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8952 TAILQ_REMOVE(histedit_cmds, hle, entry);
8957 static const struct got_error *
8958 histedit_load_list(struct got_histedit_list *histedit_cmds,
8959 const char *path, struct got_repository *repo)
8961 const struct got_error *err = NULL;
8964 f = fopen(path, "r");
8966 err = got_error_from_errno2("fopen", path);
8970 err = histedit_parse_list(histedit_cmds, f, repo);
8972 if (f && fclose(f) == EOF && err == NULL)
8973 err = got_error_from_errno("fclose");
8977 static const struct got_error *
8978 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8979 const struct got_error *edit_err, struct got_object_id_queue *commits,
8980 const char *path, const char *branch_name, struct got_repository *repo)
8982 const struct got_error *err = NULL, *prev_err = edit_err;
8985 while (resp != 'c' && resp != 'r' && resp != 'a') {
8986 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8987 "or (a)bort: ", getprogname(), prev_err->msg);
8992 histedit_free_list(histedit_cmds);
8993 err = histedit_run_editor(histedit_cmds, path, commits,
8996 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8997 err->code != GOT_ERR_HISTEDIT_CMD)
9004 } else if (resp == 'r') {
9005 histedit_free_list(histedit_cmds);
9006 err = histedit_edit_script(histedit_cmds,
9007 commits, branch_name, 0, 0, repo);
9009 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9010 err->code != GOT_ERR_HISTEDIT_CMD)
9017 } else if (resp == 'a') {
9018 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9021 printf("invalid response '%c'\n", resp);
9027 static const struct got_error *
9028 histedit_complete(struct got_worktree *worktree,
9029 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9030 struct got_reference *branch, struct got_repository *repo)
9032 printf("Switching work tree to %s\n",
9033 got_ref_get_symref_target(branch));
9034 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9038 static const struct got_error *
9039 show_histedit_progress(struct got_commit_object *commit,
9040 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9042 const struct got_error *err;
9043 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9045 err = got_object_id_str(&old_id_str, hle->commit_id);
9050 err = got_object_id_str(&new_id_str, new_id);
9055 old_id_str[12] = '\0';
9057 new_id_str[12] = '\0';
9060 logmsg = strdup(hle->logmsg);
9061 if (logmsg == NULL) {
9062 err = got_error_from_errno("strdup");
9065 trim_logmsg(logmsg, 42);
9067 err = get_short_logmsg(&logmsg, 42, commit);
9072 switch (hle->cmd->code) {
9073 case GOT_HISTEDIT_PICK:
9074 case GOT_HISTEDIT_EDIT:
9075 printf("%s -> %s: %s\n", old_id_str,
9076 new_id_str ? new_id_str : "no-op change", logmsg);
9078 case GOT_HISTEDIT_DROP:
9079 case GOT_HISTEDIT_FOLD:
9080 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9092 static const struct got_error *
9093 histedit_commit(struct got_pathlist_head *merged_paths,
9094 struct got_worktree *worktree, struct got_fileindex *fileindex,
9095 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9096 struct got_repository *repo)
9098 const struct got_error *err;
9099 struct got_commit_object *commit;
9100 struct got_object_id *new_commit_id;
9102 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9103 && hle->logmsg == NULL) {
9104 err = histedit_edit_logmsg(hle, repo);
9109 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9113 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9114 worktree, fileindex, tmp_branch, commit, hle->commit_id,
9117 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9119 err = show_histedit_progress(commit, hle, NULL);
9121 err = show_histedit_progress(commit, hle, new_commit_id);
9122 free(new_commit_id);
9125 got_object_commit_close(commit);
9129 static const struct got_error *
9130 histedit_skip_commit(struct got_histedit_list_entry *hle,
9131 struct got_worktree *worktree, struct got_repository *repo)
9133 const struct got_error *error;
9134 struct got_commit_object *commit;
9136 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9141 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9145 error = show_histedit_progress(commit, hle, NULL);
9146 got_object_commit_close(commit);
9150 static const struct got_error *
9151 check_local_changes(void *arg, unsigned char status,
9152 unsigned char staged_status, const char *path,
9153 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9154 struct got_object_id *commit_id, int dirfd, const char *de_name)
9156 int *have_local_changes = arg;
9159 case GOT_STATUS_ADD:
9160 case GOT_STATUS_DELETE:
9161 case GOT_STATUS_MODIFY:
9162 case GOT_STATUS_CONFLICT:
9163 *have_local_changes = 1;
9164 return got_error(GOT_ERR_CANCELLED);
9169 switch (staged_status) {
9170 case GOT_STATUS_ADD:
9171 case GOT_STATUS_DELETE:
9172 case GOT_STATUS_MODIFY:
9173 *have_local_changes = 1;
9174 return got_error(GOT_ERR_CANCELLED);
9182 static const struct got_error *
9183 cmd_histedit(int argc, char *argv[])
9185 const struct got_error *error = NULL;
9186 struct got_worktree *worktree = NULL;
9187 struct got_fileindex *fileindex = NULL;
9188 struct got_repository *repo = NULL;
9190 struct got_reference *branch = NULL;
9191 struct got_reference *tmp_branch = NULL;
9192 struct got_object_id *resume_commit_id = NULL;
9193 struct got_object_id *base_commit_id = NULL;
9194 struct got_object_id *head_commit_id = NULL;
9195 struct got_commit_object *commit = NULL;
9196 int ch, rebase_in_progress = 0;
9197 struct got_update_progress_arg upa;
9198 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9199 int edit_logmsg_only = 0, fold_only = 0;
9200 int list_backups = 0;
9201 const char *edit_script_path = NULL;
9202 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9203 struct got_object_id_queue commits;
9204 struct got_pathlist_head merged_paths;
9205 const struct got_object_id_queue *parent_ids;
9206 struct got_object_qid *pid;
9207 struct got_histedit_list histedit_cmds;
9208 struct got_histedit_list_entry *hle;
9210 SIMPLEQ_INIT(&commits);
9211 TAILQ_INIT(&histedit_cmds);
9212 TAILQ_INIT(&merged_paths);
9213 memset(&upa, 0, sizeof(upa));
9215 while ((ch = getopt(argc, argv, "acfF:ml")) != -1) {
9227 edit_script_path = optarg;
9230 edit_logmsg_only = 1;
9245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9246 "unveil", NULL) == -1)
9249 if (abort_edit && continue_edit)
9250 option_conflict('a', 'c');
9251 if (edit_script_path && edit_logmsg_only)
9252 option_conflict('F', 'm');
9253 if (abort_edit && edit_logmsg_only)
9254 option_conflict('a', 'm');
9255 if (continue_edit && edit_logmsg_only)
9256 option_conflict('c', 'm');
9257 if (abort_edit && fold_only)
9258 option_conflict('a', 'f');
9259 if (continue_edit && fold_only)
9260 option_conflict('c', 'f');
9261 if (fold_only && edit_logmsg_only)
9262 option_conflict('f', 'm');
9263 if (edit_script_path && fold_only)
9264 option_conflict('F', 'f');
9267 option_conflict('l', 'a');
9269 option_conflict('l', 'c');
9270 if (edit_script_path)
9271 option_conflict('l', 'F');
9272 if (edit_logmsg_only)
9273 option_conflict('l', 'm');
9275 option_conflict('l', 'f');
9276 if (argc != 0 && argc != 1)
9278 } else if (argc != 0)
9282 * This command cannot apply unveil(2) in all cases because the
9283 * user may choose to run an editor to edit the histedit script
9284 * and to edit individual commit log messages.
9285 * unveil(2) traverses exec(2); if an editor is used we have to
9286 * apply unveil after edit script and log messages have been written.
9287 * XXX TODO: Make use of unveil(2) where possible.
9290 cwd = getcwd(NULL, 0);
9292 error = got_error_from_errno("getcwd");
9295 error = got_worktree_open(&worktree, cwd);
9298 if (error->code != GOT_ERR_NOT_WORKTREE)
9301 if (error->code == GOT_ERR_NOT_WORKTREE)
9302 error = wrap_not_worktree_error(error,
9309 error = got_repo_open(&repo,
9310 worktree ? got_worktree_get_repo_path(worktree) : cwd,
9314 error = apply_unveil(got_repo_get_path(repo), 0,
9315 worktree ? got_worktree_get_root_path(worktree) : NULL);
9318 error = list_backup_refs(
9319 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9320 argc == 1 ? argv[0] : NULL, repo);
9321 goto done; /* nothing else to do */
9324 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9329 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9332 if (rebase_in_progress) {
9333 error = got_error(GOT_ERR_REBASING);
9337 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9341 if (edit_in_progress && edit_logmsg_only) {
9342 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9343 "histedit operation is in progress in this "
9344 "work tree and must be continued or aborted "
9345 "before the -m option can be used");
9348 if (edit_in_progress && fold_only) {
9349 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9350 "histedit operation is in progress in this "
9351 "work tree and must be continued or aborted "
9352 "before the -f option can be used");
9356 if (edit_in_progress && abort_edit) {
9357 error = got_worktree_histedit_continue(&resume_commit_id,
9358 &tmp_branch, &branch, &base_commit_id, &fileindex,
9362 printf("Switching work tree to %s\n",
9363 got_ref_get_symref_target(branch));
9364 error = got_worktree_histedit_abort(worktree, fileindex, repo,
9365 branch, base_commit_id, update_progress, &upa);
9368 printf("Histedit of %s aborted\n",
9369 got_ref_get_symref_target(branch));
9370 print_update_progress_stats(&upa);
9371 goto done; /* nothing else to do */
9372 } else if (abort_edit) {
9373 error = got_error(GOT_ERR_NOT_HISTEDIT);
9377 if (continue_edit) {
9380 if (!edit_in_progress) {
9381 error = got_error(GOT_ERR_NOT_HISTEDIT);
9385 error = got_worktree_get_histedit_script_path(&path, worktree);
9389 error = histedit_load_list(&histedit_cmds, path, repo);
9394 error = got_worktree_histedit_continue(&resume_commit_id,
9395 &tmp_branch, &branch, &base_commit_id, &fileindex,
9400 error = got_ref_resolve(&head_commit_id, repo, branch);
9404 error = got_object_open_as_commit(&commit, repo,
9408 parent_ids = got_object_commit_get_parent_ids(commit);
9409 pid = SIMPLEQ_FIRST(parent_ids);
9411 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9414 error = collect_commits(&commits, head_commit_id, pid->id,
9415 base_commit_id, got_worktree_get_path_prefix(worktree),
9416 GOT_ERR_HISTEDIT_PATH, repo);
9417 got_object_commit_close(commit);
9422 if (edit_in_progress) {
9423 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9427 error = got_ref_open(&branch, repo,
9428 got_worktree_get_head_ref_name(worktree), 0);
9432 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
9433 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
9434 "will not edit commit history of a branch outside "
9435 "the \"refs/heads/\" reference namespace");
9439 error = got_ref_resolve(&head_commit_id, repo, branch);
9440 got_ref_close(branch);
9445 error = got_object_open_as_commit(&commit, repo,
9449 parent_ids = got_object_commit_get_parent_ids(commit);
9450 pid = SIMPLEQ_FIRST(parent_ids);
9452 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9455 error = collect_commits(&commits, head_commit_id, pid->id,
9456 got_worktree_get_base_commit_id(worktree),
9457 got_worktree_get_path_prefix(worktree),
9458 GOT_ERR_HISTEDIT_PATH, repo);
9459 got_object_commit_close(commit);
9464 if (SIMPLEQ_EMPTY(&commits)) {
9465 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9469 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9470 &base_commit_id, &fileindex, worktree, repo);
9474 if (edit_script_path) {
9475 error = histedit_load_list(&histedit_cmds,
9476 edit_script_path, repo);
9478 got_worktree_histedit_abort(worktree, fileindex,
9479 repo, branch, base_commit_id,
9480 update_progress, &upa);
9481 print_update_progress_stats(&upa);
9485 const char *branch_name;
9486 branch_name = got_ref_get_symref_target(branch);
9487 if (strncmp(branch_name, "refs/heads/", 11) == 0)
9489 error = histedit_edit_script(&histedit_cmds, &commits,
9490 branch_name, edit_logmsg_only, fold_only, repo);
9492 got_worktree_histedit_abort(worktree, fileindex,
9493 repo, branch, base_commit_id,
9494 update_progress, &upa);
9495 print_update_progress_stats(&upa);
9501 error = histedit_save_list(&histedit_cmds, worktree,
9504 got_worktree_histedit_abort(worktree, fileindex,
9505 repo, branch, base_commit_id,
9506 update_progress, &upa);
9507 print_update_progress_stats(&upa);
9513 error = histedit_check_script(&histedit_cmds, &commits, repo);
9517 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9518 if (resume_commit_id) {
9519 if (got_object_id_cmp(hle->commit_id,
9520 resume_commit_id) != 0)
9523 resume_commit_id = NULL;
9524 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9525 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9526 error = histedit_skip_commit(hle, worktree,
9531 struct got_pathlist_head paths;
9532 int have_changes = 0;
9535 error = got_pathlist_append(&paths, "", NULL);
9538 error = got_worktree_status(worktree, &paths,
9539 repo, check_local_changes, &have_changes,
9540 check_cancelled, NULL);
9541 got_pathlist_free(&paths);
9543 if (error->code != GOT_ERR_CANCELLED)
9545 if (sigint_received || sigpipe_received)
9549 error = histedit_commit(NULL, worktree,
9550 fileindex, tmp_branch, hle, repo);
9554 error = got_object_open_as_commit(
9555 &commit, repo, hle->commit_id);
9558 error = show_histedit_progress(commit,
9560 got_object_commit_close(commit);
9569 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9570 error = histedit_skip_commit(hle, worktree, repo);
9576 error = got_object_open_as_commit(&commit, repo,
9580 parent_ids = got_object_commit_get_parent_ids(commit);
9581 pid = SIMPLEQ_FIRST(parent_ids);
9583 error = got_worktree_histedit_merge_files(&merged_paths,
9584 worktree, fileindex, pid->id, hle->commit_id, repo,
9585 update_progress, &upa, check_cancelled, NULL);
9588 got_object_commit_close(commit);
9591 print_update_progress_stats(&upa);
9592 if (upa.conflicts > 0)
9593 rebase_status = GOT_STATUS_CONFLICT;
9595 if (rebase_status == GOT_STATUS_CONFLICT) {
9596 error = show_rebase_merge_conflict(hle->commit_id,
9600 got_worktree_rebase_pathlist_free(&merged_paths);
9604 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9606 error = got_object_id_str(&id_str, hle->commit_id);
9609 printf("Stopping histedit for amending commit %s\n",
9612 got_worktree_rebase_pathlist_free(&merged_paths);
9613 error = got_worktree_histedit_postpone(worktree,
9618 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9619 error = histedit_skip_commit(hle, worktree, repo);
9625 error = histedit_commit(&merged_paths, worktree, fileindex,
9626 tmp_branch, hle, repo);
9627 got_worktree_rebase_pathlist_free(&merged_paths);
9632 if (rebase_status == GOT_STATUS_CONFLICT) {
9633 error = got_worktree_histedit_postpone(worktree, fileindex);
9636 error = got_error_msg(GOT_ERR_CONFLICTS,
9637 "conflicts must be resolved before histedit can continue");
9639 error = histedit_complete(worktree, fileindex, tmp_branch,
9642 got_object_id_queue_free(&commits);
9643 histedit_free_list(&histedit_cmds);
9644 free(head_commit_id);
9645 free(base_commit_id);
9646 free(resume_commit_id);
9648 got_object_commit_close(commit);
9650 got_ref_close(branch);
9652 got_ref_close(tmp_branch);
9654 got_worktree_close(worktree);
9656 const struct got_error *close_err = got_repo_close(repo);
9664 usage_integrate(void)
9666 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9670 static const struct got_error *
9671 cmd_integrate(int argc, char *argv[])
9673 const struct got_error *error = NULL;
9674 struct got_repository *repo = NULL;
9675 struct got_worktree *worktree = NULL;
9676 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9677 const char *branch_arg = NULL;
9678 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9679 struct got_fileindex *fileindex = NULL;
9680 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9682 struct got_update_progress_arg upa;
9684 while ((ch = getopt(argc, argv, "")) != -1) {
9697 branch_arg = argv[0];
9699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9700 "unveil", NULL) == -1)
9703 cwd = getcwd(NULL, 0);
9705 error = got_error_from_errno("getcwd");
9709 error = got_worktree_open(&worktree, cwd);
9711 if (error->code == GOT_ERR_NOT_WORKTREE)
9712 error = wrap_not_worktree_error(error, "integrate",
9717 error = check_rebase_or_histedit_in_progress(worktree);
9721 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9726 error = apply_unveil(got_repo_get_path(repo), 0,
9727 got_worktree_get_root_path(worktree));
9731 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9732 error = got_error_from_errno("asprintf");
9736 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9737 &base_branch_ref, worktree, refname, repo);
9741 refname = strdup(got_ref_get_name(branch_ref));
9742 if (refname == NULL) {
9743 error = got_error_from_errno("strdup");
9744 got_worktree_integrate_abort(worktree, fileindex, repo,
9745 branch_ref, base_branch_ref);
9748 base_refname = strdup(got_ref_get_name(base_branch_ref));
9749 if (base_refname == NULL) {
9750 error = got_error_from_errno("strdup");
9751 got_worktree_integrate_abort(worktree, fileindex, repo,
9752 branch_ref, base_branch_ref);
9756 error = got_ref_resolve(&commit_id, repo, branch_ref);
9760 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9764 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9765 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9766 "specified branch has already been integrated");
9767 got_worktree_integrate_abort(worktree, fileindex, repo,
9768 branch_ref, base_branch_ref);
9772 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9774 if (error->code == GOT_ERR_ANCESTRY)
9775 error = got_error(GOT_ERR_REBASE_REQUIRED);
9776 got_worktree_integrate_abort(worktree, fileindex, repo,
9777 branch_ref, base_branch_ref);
9781 memset(&upa, 0, sizeof(upa));
9782 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9783 branch_ref, base_branch_ref, update_progress, &upa,
9784 check_cancelled, NULL);
9788 printf("Integrated %s into %s\n", refname, base_refname);
9789 print_update_progress_stats(&upa);
9792 const struct got_error *close_err = got_repo_close(repo);
9797 got_worktree_close(worktree);
9799 free(base_commit_id);
9809 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9810 "[-S] [file-path ...]\n",
9815 static const struct got_error *
9816 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9817 const char *path, struct got_object_id *blob_id,
9818 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9819 int dirfd, const char *de_name)
9821 const struct got_error *err = NULL;
9822 char *id_str = NULL;
9824 if (staged_status != GOT_STATUS_ADD &&
9825 staged_status != GOT_STATUS_MODIFY &&
9826 staged_status != GOT_STATUS_DELETE)
9829 if (staged_status == GOT_STATUS_ADD ||
9830 staged_status == GOT_STATUS_MODIFY)
9831 err = got_object_id_str(&id_str, staged_blob_id);
9833 err = got_object_id_str(&id_str, blob_id);
9837 printf("%s %c %s\n", id_str, staged_status, path);
9842 static const struct got_error *
9843 cmd_stage(int argc, char *argv[])
9845 const struct got_error *error = NULL;
9846 struct got_repository *repo = NULL;
9847 struct got_worktree *worktree = NULL;
9849 struct got_pathlist_head paths;
9850 struct got_pathlist_entry *pe;
9851 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9852 FILE *patch_script_file = NULL;
9853 const char *patch_script_path = NULL;
9854 struct choose_patch_arg cpa;
9858 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9867 patch_script_path = optarg;
9870 allow_bad_symlinks = 1;
9882 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9883 "unveil", NULL) == -1)
9886 if (list_stage && (pflag || patch_script_path))
9887 errx(1, "-l option cannot be used with other options");
9888 if (patch_script_path && !pflag)
9889 errx(1, "-F option can only be used together with -p option");
9891 cwd = getcwd(NULL, 0);
9893 error = got_error_from_errno("getcwd");
9897 error = got_worktree_open(&worktree, cwd);
9899 if (error->code == GOT_ERR_NOT_WORKTREE)
9900 error = wrap_not_worktree_error(error, "stage", cwd);
9904 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9909 if (patch_script_path) {
9910 patch_script_file = fopen(patch_script_path, "r");
9911 if (patch_script_file == NULL) {
9912 error = got_error_from_errno2("fopen",
9917 error = apply_unveil(got_repo_get_path(repo), 0,
9918 got_worktree_get_root_path(worktree));
9922 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9927 error = got_worktree_status(worktree, &paths, repo,
9928 print_stage, NULL, check_cancelled, NULL);
9930 cpa.patch_script_file = patch_script_file;
9931 cpa.action = "stage";
9932 error = got_worktree_stage(worktree, &paths,
9933 pflag ? NULL : print_status, NULL,
9934 pflag ? choose_patch : NULL, &cpa,
9935 allow_bad_symlinks, repo);
9938 if (patch_script_file && fclose(patch_script_file) == EOF &&
9940 error = got_error_from_errno2("fclose", patch_script_path);
9942 const struct got_error *close_err = got_repo_close(repo);
9947 got_worktree_close(worktree);
9948 TAILQ_FOREACH(pe, &paths, entry)
9949 free((char *)pe->path);
9950 got_pathlist_free(&paths);
9958 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9959 "[file-path ...]\n",
9965 static const struct got_error *
9966 cmd_unstage(int argc, char *argv[])
9968 const struct got_error *error = NULL;
9969 struct got_repository *repo = NULL;
9970 struct got_worktree *worktree = NULL;
9972 struct got_pathlist_head paths;
9973 struct got_pathlist_entry *pe;
9975 struct got_update_progress_arg upa;
9976 FILE *patch_script_file = NULL;
9977 const char *patch_script_path = NULL;
9978 struct choose_patch_arg cpa;
9982 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9988 patch_script_path = optarg;
10000 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10001 "unveil", NULL) == -1)
10004 if (patch_script_path && !pflag)
10005 errx(1, "-F option can only be used together with -p option");
10007 cwd = getcwd(NULL, 0);
10009 error = got_error_from_errno("getcwd");
10013 error = got_worktree_open(&worktree, cwd);
10015 if (error->code == GOT_ERR_NOT_WORKTREE)
10016 error = wrap_not_worktree_error(error, "unstage", cwd);
10020 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10025 if (patch_script_path) {
10026 patch_script_file = fopen(patch_script_path, "r");
10027 if (patch_script_file == NULL) {
10028 error = got_error_from_errno2("fopen",
10029 patch_script_path);
10034 error = apply_unveil(got_repo_get_path(repo), 0,
10035 got_worktree_get_root_path(worktree));
10039 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10043 cpa.patch_script_file = patch_script_file;
10044 cpa.action = "unstage";
10045 memset(&upa, 0, sizeof(upa));
10046 error = got_worktree_unstage(worktree, &paths, update_progress,
10047 &upa, pflag ? choose_patch : NULL, &cpa, repo);
10049 print_update_progress_stats(&upa);
10051 if (patch_script_file && fclose(patch_script_file) == EOF &&
10053 error = got_error_from_errno2("fclose", patch_script_path);
10055 const struct got_error *close_err = got_repo_close(repo);
10060 got_worktree_close(worktree);
10061 TAILQ_FOREACH(pe, &paths, entry)
10062 free((char *)pe->path);
10063 got_pathlist_free(&paths);
10071 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10072 "arg1 [arg2 ...]\n", getprogname());
10076 static const struct got_error *
10077 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10079 const struct got_error *err;
10080 struct got_blob_object *blob;
10082 err = got_object_open_as_blob(&blob, repo, id, 8192);
10086 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10087 got_object_blob_close(blob);
10091 static const struct got_error *
10092 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10094 const struct got_error *err;
10095 struct got_tree_object *tree;
10098 err = got_object_open_as_tree(&tree, repo, id);
10102 nentries = got_object_tree_get_nentries(tree);
10103 for (i = 0; i < nentries; i++) {
10104 struct got_tree_entry *te;
10106 if (sigint_received || sigpipe_received)
10108 te = got_object_tree_get_entry(tree, i);
10109 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10112 fprintf(outfile, "%s %.7o %s\n", id_str,
10113 got_tree_entry_get_mode(te),
10114 got_tree_entry_get_name(te));
10118 got_object_tree_close(tree);
10122 static const struct got_error *
10123 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10125 const struct got_error *err;
10126 struct got_commit_object *commit;
10127 const struct got_object_id_queue *parent_ids;
10128 struct got_object_qid *pid;
10129 char *id_str = NULL;
10130 const char *logmsg = NULL;
10132 err = got_object_open_as_commit(&commit, repo, id);
10136 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10140 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10141 parent_ids = got_object_commit_get_parent_ids(commit);
10142 fprintf(outfile, "numparents %d\n",
10143 got_object_commit_get_nparents(commit));
10144 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
10146 err = got_object_id_str(&pid_str, pid->id);
10149 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10152 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10153 got_object_commit_get_author(commit),
10154 (long long)got_object_commit_get_author_time(commit));
10156 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10157 got_object_commit_get_author(commit),
10158 (long long)got_object_commit_get_committer_time(commit));
10160 logmsg = got_object_commit_get_logmsg_raw(commit);
10161 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10162 fprintf(outfile, "%s", logmsg);
10165 got_object_commit_close(commit);
10169 static const struct got_error *
10170 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10172 const struct got_error *err;
10173 struct got_tag_object *tag;
10174 char *id_str = NULL;
10175 const char *tagmsg = NULL;
10177 err = got_object_open_as_tag(&tag, repo, id);
10181 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10185 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10187 switch (got_object_tag_get_object_type(tag)) {
10188 case GOT_OBJ_TYPE_BLOB:
10189 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10190 GOT_OBJ_LABEL_BLOB);
10192 case GOT_OBJ_TYPE_TREE:
10193 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10194 GOT_OBJ_LABEL_TREE);
10196 case GOT_OBJ_TYPE_COMMIT:
10197 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10198 GOT_OBJ_LABEL_COMMIT);
10200 case GOT_OBJ_TYPE_TAG:
10201 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10202 GOT_OBJ_LABEL_TAG);
10208 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10209 got_object_tag_get_name(tag));
10211 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10212 got_object_tag_get_tagger(tag),
10213 (long long)got_object_tag_get_tagger_time(tag));
10215 tagmsg = got_object_tag_get_message(tag);
10216 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10217 fprintf(outfile, "%s", tagmsg);
10220 got_object_tag_close(tag);
10224 static const struct got_error *
10225 cmd_cat(int argc, char *argv[])
10227 const struct got_error *error;
10228 struct got_repository *repo = NULL;
10229 struct got_worktree *worktree = NULL;
10230 char *cwd = NULL, *repo_path = NULL, *label = NULL;
10231 const char *commit_id_str = NULL;
10232 struct got_object_id *id = NULL, *commit_id = NULL;
10233 int ch, obj_type, i, force_path = 0;
10234 struct got_reflist_head refs;
10239 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10244 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10247 commit_id_str = optarg;
10250 repo_path = realpath(optarg, NULL);
10251 if (repo_path == NULL)
10252 return got_error_from_errno2("realpath",
10254 got_path_strip_trailing_slashes(repo_path);
10268 cwd = getcwd(NULL, 0);
10270 error = got_error_from_errno("getcwd");
10273 error = got_worktree_open(&worktree, cwd);
10274 if (error && error->code != GOT_ERR_NOT_WORKTREE)
10277 if (repo_path == NULL) {
10278 repo_path = strdup(
10279 got_worktree_get_repo_path(worktree));
10280 if (repo_path == NULL) {
10281 error = got_error_from_errno("strdup");
10287 if (repo_path == NULL) {
10288 repo_path = getcwd(NULL, 0);
10289 if (repo_path == NULL)
10290 return got_error_from_errno("getcwd");
10293 error = got_repo_open(&repo, repo_path, NULL);
10298 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10302 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10306 if (commit_id_str == NULL)
10307 commit_id_str = GOT_REF_HEAD;
10308 error = got_repo_match_object_id(&commit_id, NULL,
10309 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10313 for (i = 0; i < argc; i++) {
10315 error = got_object_id_by_path(&id, repo, commit_id,
10320 error = got_repo_match_object_id(&id, &label, argv[i],
10321 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10324 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10325 error->code != GOT_ERR_NOT_REF)
10327 error = got_object_id_by_path(&id, repo,
10328 commit_id, argv[i]);
10334 error = got_object_get_type(&obj_type, repo, id);
10338 switch (obj_type) {
10339 case GOT_OBJ_TYPE_BLOB:
10340 error = cat_blob(id, repo, stdout);
10342 case GOT_OBJ_TYPE_TREE:
10343 error = cat_tree(id, repo, stdout);
10345 case GOT_OBJ_TYPE_COMMIT:
10346 error = cat_commit(id, repo, stdout);
10348 case GOT_OBJ_TYPE_TAG:
10349 error = cat_tag(id, repo, stdout);
10352 error = got_error(GOT_ERR_OBJ_TYPE);
10367 got_worktree_close(worktree);
10369 const struct got_error *close_err = got_repo_close(repo);
10373 got_ref_list_free(&refs);
10380 fprintf(stderr, "usage: %s info [path ...]\n",
10385 static const struct got_error *
10386 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
10387 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10388 struct got_object_id *commit_id)
10390 const struct got_error *err = NULL;
10391 char *id_str = NULL;
10393 struct tm mytm, *tm;
10394 struct got_pathlist_head *paths = arg;
10395 struct got_pathlist_entry *pe;
10398 * Clear error indication from any of the path arguments which
10399 * would cause this file index entry to be displayed.
10401 TAILQ_FOREACH(pe, paths, entry) {
10402 if (got_path_cmp(path, pe->path, strlen(path),
10403 pe->path_len) == 0 ||
10404 got_path_is_child(path, pe->path, pe->path_len))
10405 pe->data = NULL; /* no error */
10408 printf(GOT_COMMIT_SEP_STR);
10410 printf("symlink: %s\n", path);
10411 else if (S_ISREG(mode)) {
10412 printf("file: %s\n", path);
10413 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
10414 } else if (S_ISDIR(mode))
10415 printf("directory: %s\n", path);
10417 printf("something: %s\n", path);
10419 tm = localtime_r(&mtime, &mytm);
10422 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
10423 return got_error(GOT_ERR_NO_SPACE);
10424 printf("timestamp: %s\n", datebuf);
10427 err = got_object_id_str(&id_str, blob_id);
10430 printf("based on blob: %s\n", id_str);
10434 if (staged_blob_id) {
10435 err = got_object_id_str(&id_str, staged_blob_id);
10438 printf("based on staged blob: %s\n", id_str);
10443 err = got_object_id_str(&id_str, commit_id);
10446 printf("based on commit: %s\n", id_str);
10453 static const struct got_error *
10454 cmd_info(int argc, char *argv[])
10456 const struct got_error *error = NULL;
10457 struct got_worktree *worktree = NULL;
10458 char *cwd = NULL, *id_str = NULL;
10459 struct got_pathlist_head paths;
10460 struct got_pathlist_entry *pe;
10461 char *uuidstr = NULL;
10462 int ch, show_files = 0;
10464 TAILQ_INIT(&paths);
10466 while ((ch = getopt(argc, argv, "")) != -1) {
10478 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10482 cwd = getcwd(NULL, 0);
10484 error = got_error_from_errno("getcwd");
10488 error = got_worktree_open(&worktree, cwd);
10490 if (error->code == GOT_ERR_NOT_WORKTREE)
10491 error = wrap_not_worktree_error(error, "info", cwd);
10495 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10500 error = get_worktree_paths_from_argv(&paths, argc, argv,
10507 error = got_object_id_str(&id_str,
10508 got_worktree_get_base_commit_id(worktree));
10512 error = got_worktree_get_uuid(&uuidstr, worktree);
10516 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10517 printf("work tree base commit: %s\n", id_str);
10518 printf("work tree path prefix: %s\n",
10519 got_worktree_get_path_prefix(worktree));
10520 printf("work tree branch reference: %s\n",
10521 got_worktree_get_head_ref_name(worktree));
10522 printf("work tree UUID: %s\n", uuidstr);
10523 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10526 struct got_pathlist_entry *pe;
10527 TAILQ_FOREACH(pe, &paths, entry) {
10528 if (pe->path_len == 0)
10531 * Assume this path will fail. This will be corrected
10532 * in print_path_info() in case the path does suceeed.
10534 pe->data = (void *)got_error_path(pe->path,
10537 error = got_worktree_path_info(worktree, &paths,
10538 print_path_info, &paths, check_cancelled, NULL);
10541 TAILQ_FOREACH(pe, &paths, entry) {
10542 if (pe->data != NULL) {
10543 error = pe->data; /* bad path */
10549 TAILQ_FOREACH(pe, &paths, entry)
10550 free((char *)pe->path);
10551 got_pathlist_free(&paths);