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);
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, "" },
178 fprintf(stderr, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(stderr, " %s", cmd->cmd_name);
187 main(int argc, char *argv[])
192 int hflag = 0, Vflag = 0;
193 static struct option longopts[] = {
194 { "version", no_argument, NULL, 'V' },
198 setlocale(LC_CTYPE, "");
200 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
219 got_version_print_str();
226 signal(SIGINT, catch_sigint);
227 signal(SIGPIPE, catch_sigpipe);
229 for (i = 0; i < nitems(got_commands); i++) {
230 const struct got_error *error;
232 cmd = &got_commands[i];
234 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 strcmp(cmd->cmd_alias, argv[0]) != 0)
239 got_commands[i].cmd_usage();
241 error = got_commands[i].cmd_main(argc, argv);
242 if (error && error->code != GOT_ERR_CANCELLED &&
243 error->code != GOT_ERR_PRIVSEP_EXIT &&
244 !(sigpipe_received &&
245 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
247 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
270 static const struct got_error *
271 get_editor(char **abspath)
273 const struct got_error *err = NULL;
278 editor = getenv("VISUAL");
280 editor = getenv("EDITOR");
283 err = got_path_find_prog(abspath, editor);
288 if (*abspath == NULL) {
289 *abspath = strdup("/bin/ed");
290 if (*abspath == NULL)
291 return got_error_from_errno("strdup");
297 static const struct got_error *
298 apply_unveil(const char *repo_path, int repo_read_only,
299 const char *worktree_path)
301 const struct got_error *err;
304 if (unveil("gmon.out", "rwc") != 0)
305 return got_error_from_errno2("unveil", "gmon.out");
307 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 return got_error_from_errno2("unveil", worktree_path);
313 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 err = got_privsep_unveil_exec_helpers();
320 if (unveil(NULL, NULL) != 0)
321 return got_error_from_errno("unveil");
329 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 static const struct got_error *
334 cmd_init(int argc, char *argv[])
336 const struct got_error *error = NULL;
337 char *repo_path = NULL;
340 while ((ch = getopt(argc, argv, "")) != -1) {
352 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
358 repo_path = strdup(argv[0]);
359 if (repo_path == NULL)
360 return got_error_from_errno("strdup");
362 got_path_strip_trailing_slashes(repo_path);
364 error = got_path_mkdir(repo_path);
366 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 error = apply_unveil(repo_path, 0, NULL);
373 error = got_repo_init(repo_path);
382 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 "[-r repository-path] [-I pattern] path\n", getprogname());
388 spawn_editor(const char *editor, const char *file)
391 sig_t sighup, sigint, sigquit;
394 sighup = signal(SIGHUP, SIG_IGN);
395 sigint = signal(SIGINT, SIG_IGN);
396 sigquit = signal(SIGQUIT, SIG_IGN);
398 switch (pid = fork()) {
402 execl(editor, editor, file, (char *)NULL);
406 while (waitpid(pid, &st, 0) == -1)
411 (void)signal(SIGHUP, sighup);
412 (void)signal(SIGINT, sigint);
413 (void)signal(SIGQUIT, sigquit);
415 if (!WIFEXITED(st)) {
420 return WEXITSTATUS(st);
423 static const struct got_error *
424 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 const char *initial_content)
427 const struct got_error *err = NULL;
431 int content_changed = 0;
436 if (stat(logmsg_path, &st) == -1)
437 return got_error_from_errno2("stat", logmsg_path);
439 if (spawn_editor(editor, logmsg_path) == -1)
440 return got_error_from_errno("failed spawning editor");
442 if (stat(logmsg_path, &st2) == -1)
443 return got_error_from_errno("stat");
445 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 "no changes made to commit message, aborting");
449 *logmsg = malloc(st2.st_size + 1);
451 return got_error_from_errno("malloc");
455 fp = fopen(logmsg_path, "r");
457 err = got_error_from_errno("fopen");
460 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 if (!content_changed && strcmp(buf, initial_content) != 0)
463 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 continue; /* remove comments and leading empty lines */
465 len = strlcat(*logmsg, buf, st2.st_size);
469 while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 (*logmsg)[len - 1] = '\0';
474 if (len == 0 || !content_changed)
475 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 "commit message cannot be empty, aborting");
485 static const struct got_error *
486 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 const char *path_dir, const char *branch_name)
489 char *initial_content = NULL;
490 const struct got_error *err = NULL;
491 int initial_content_len;
494 initial_content_len = asprintf(&initial_content,
495 "\n# %s to be imported to branch %s\n", path_dir,
497 if (initial_content_len == -1)
498 return got_error_from_errno("asprintf");
500 err = got_opentemp_named_fd(logmsg_path, &fd,
501 GOT_TMPDIR_STR "/got-importmsg");
505 if (write(fd, initial_content, initial_content_len) == -1) {
506 err = got_error_from_errno2("write", *logmsg_path);
510 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 if (fd != -1 && close(fd) == -1 && err == NULL)
513 err = got_error_from_errno2("close", *logmsg_path);
514 free(initial_content);
518 static const struct got_error *
519 import_progress(void *arg, const char *path)
521 printf("A %s\n", path);
525 static const struct got_error *
526 get_author(char **author, struct got_repository *repo,
527 struct got_worktree *worktree)
529 const struct got_error *err = NULL;
530 const char *got_author = NULL, *name, *email;
531 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
536 worktree_conf = got_worktree_get_gotconfig(worktree);
537 repo_conf = got_repo_get_gotconfig(repo);
540 * Priority of potential author information sources, from most
541 * significant to least significant:
542 * 1) work tree's .got/got.conf file
543 * 2) repository's got.conf file
544 * 3) repository's git config file
545 * 4) environment variables
546 * 5) global git config files (in user's home directory or /etc)
550 got_author = got_gotconfig_get_author(worktree_conf);
551 if (got_author == NULL)
552 got_author = got_gotconfig_get_author(repo_conf);
553 if (got_author == NULL) {
554 name = got_repo_get_gitconfig_author_name(repo);
555 email = got_repo_get_gitconfig_author_email(repo);
557 if (asprintf(author, "%s <%s>", name, email) == -1)
558 return got_error_from_errno("asprintf");
562 got_author = getenv("GOT_AUTHOR");
563 if (got_author == NULL) {
564 name = got_repo_get_global_gitconfig_author_name(repo);
565 email = got_repo_get_global_gitconfig_author_email(
568 if (asprintf(author, "%s <%s>", name, email)
570 return got_error_from_errno("asprintf");
573 /* TODO: Look up user in password database? */
574 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
578 *author = strdup(got_author);
580 return got_error_from_errno("strdup");
583 * Really dumb email address check; we're only doing this to
584 * avoid git's object parser breaking on commits we create.
586 while (*got_author && *got_author != '<')
588 if (*got_author != '<') {
589 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
592 while (*got_author && *got_author != '@')
594 if (*got_author != '@') {
595 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
598 while (*got_author && *got_author != '>')
600 if (*got_author != '>')
601 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
610 static const struct got_error *
611 get_gitconfig_path(char **gitconfig_path)
613 const char *homedir = getenv("HOME");
615 *gitconfig_path = NULL;
617 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
618 return got_error_from_errno("asprintf");
624 static const struct got_error *
625 cmd_import(int argc, char *argv[])
627 const struct got_error *error = NULL;
628 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
629 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
630 const char *branch_name = "main";
631 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
632 struct got_repository *repo = NULL;
633 struct got_reference *branch_ref = NULL, *head_ref = NULL;
634 struct got_object_id *new_commit_id = NULL;
636 struct got_pathlist_head ignores;
637 struct got_pathlist_entry *pe;
638 int preserve_logmsg = 0;
640 TAILQ_INIT(&ignores);
642 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
645 branch_name = optarg;
648 logmsg = strdup(optarg);
649 if (logmsg == NULL) {
650 error = got_error_from_errno("strdup");
655 repo_path = realpath(optarg, NULL);
656 if (repo_path == NULL) {
657 error = got_error_from_errno2("realpath",
663 if (optarg[0] == '\0')
665 error = got_pathlist_insert(&pe, &ignores, optarg,
680 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
688 if (repo_path == NULL) {
689 repo_path = getcwd(NULL, 0);
690 if (repo_path == NULL)
691 return got_error_from_errno("getcwd");
693 got_path_strip_trailing_slashes(repo_path);
694 error = get_gitconfig_path(&gitconfig_path);
697 error = got_repo_open(&repo, repo_path, gitconfig_path);
701 error = get_author(&author, repo, NULL);
706 * Don't let the user create a branch name with a leading '-'.
707 * While technically a valid reference name, this case is usually
708 * an unintended typo.
710 if (branch_name[0] == '-')
711 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
713 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
714 error = got_error_from_errno("asprintf");
718 error = got_ref_open(&branch_ref, repo, refname, 0);
720 if (error->code != GOT_ERR_NOT_REF)
723 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
724 "import target branch already exists");
728 path_dir = realpath(argv[0], NULL);
729 if (path_dir == NULL) {
730 error = got_error_from_errno2("realpath", argv[0]);
733 got_path_strip_trailing_slashes(path_dir);
736 * unveil(2) traverses exec(2); if an editor is used we have
737 * to apply unveil after the log message has been written.
739 if (logmsg == NULL || strlen(logmsg) == 0) {
740 error = get_editor(&editor);
744 error = collect_import_msg(&logmsg, &logmsg_path, editor,
747 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
754 if (unveil(path_dir, "r") != 0) {
755 error = got_error_from_errno2("unveil", path_dir);
761 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
768 error = got_repo_import(&new_commit_id, path_dir, logmsg,
769 author, &ignores, repo, import_progress, NULL);
776 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
783 error = got_ref_write(branch_ref, repo);
790 error = got_object_id_str(&id_str, new_commit_id);
797 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
799 if (error->code != GOT_ERR_NOT_REF) {
805 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
813 error = got_ref_write(head_ref, repo);
821 printf("Created branch %s with commit %s\n",
822 got_ref_get_name(branch_ref), id_str);
824 if (preserve_logmsg) {
825 fprintf(stderr, "%s: log message preserved in %s\n",
826 getprogname(), logmsg_path);
827 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
828 error = got_error_from_errno2("unlink", logmsg_path);
837 free(gitconfig_path);
839 got_ref_close(branch_ref);
841 got_ref_close(head_ref);
848 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
849 "[-R reference] repository-url [directory]\n", getprogname());
853 struct got_fetch_progress_arg {
854 char last_scaled_size[FMT_SCALED_STRSIZE];
860 static const struct got_error *
861 fetch_progress(void *arg, const char *message, off_t packfile_size,
862 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
864 struct got_fetch_progress_arg *a = arg;
865 char scaled_size[FMT_SCALED_STRSIZE];
866 int p_indexed, p_resolved;
867 int print_size = 0, print_indexed = 0, print_resolved = 0;
869 if (a->verbosity < 0)
872 if (message && message[0] != '\0') {
873 printf("\rserver: %s", message);
878 if (packfile_size > 0 || nobj_indexed > 0) {
879 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
880 (a->last_scaled_size[0] == '\0' ||
881 strcmp(scaled_size, a->last_scaled_size)) != 0) {
883 if (strlcpy(a->last_scaled_size, scaled_size,
884 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
885 return got_error(GOT_ERR_NO_SPACE);
887 if (nobj_indexed > 0) {
888 p_indexed = (nobj_indexed * 100) / nobj_total;
889 if (p_indexed != a->last_p_indexed) {
890 a->last_p_indexed = p_indexed;
895 if (nobj_resolved > 0) {
896 p_resolved = (nobj_resolved * 100) /
897 (nobj_total - nobj_loose);
898 if (p_resolved != a->last_p_resolved) {
899 a->last_p_resolved = p_resolved;
907 if (print_size || print_indexed || print_resolved)
910 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
912 printf("; indexing %d%%", p_indexed);
914 printf("; resolving deltas %d%%", p_resolved);
915 if (print_size || print_indexed || print_resolved)
921 static const struct got_error *
922 create_symref(const char *refname, struct got_reference *target_ref,
923 int verbosity, struct got_repository *repo)
925 const struct got_error *err;
926 struct got_reference *head_symref;
928 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
932 err = got_ref_write(head_symref, repo);
933 got_ref_close(head_symref);
934 if (err == NULL && verbosity > 0) {
935 printf("Created reference %s: %s\n", GOT_REF_HEAD,
936 got_ref_get_name(target_ref));
941 static const struct got_error *
942 list_remote_refs(struct got_pathlist_head *symrefs,
943 struct got_pathlist_head *refs)
945 const struct got_error *err;
946 struct got_pathlist_entry *pe;
948 TAILQ_FOREACH(pe, symrefs, entry) {
949 const char *refname = pe->path;
950 const char *targetref = pe->data;
952 printf("%s: %s\n", refname, targetref);
955 TAILQ_FOREACH(pe, refs, entry) {
956 const char *refname = pe->path;
957 struct got_object_id *id = pe->data;
960 err = got_object_id_str(&id_str, id);
963 printf("%s: %s\n", refname, id_str);
970 static const struct got_error *
971 create_ref(const char *refname, struct got_object_id *id,
972 int verbosity, struct got_repository *repo)
974 const struct got_error *err = NULL;
975 struct got_reference *ref;
978 err = got_object_id_str(&id_str, id);
982 err = got_ref_alloc(&ref, refname, id);
986 err = got_ref_write(ref, repo);
989 if (err == NULL && verbosity >= 0)
990 printf("Created reference %s: %s\n", refname, id_str);
997 match_wanted_ref(const char *refname, const char *wanted_ref)
999 if (strncmp(refname, "refs/", 5) != 0)
1004 * Prevent fetching of references that won't make any
1005 * sense outside of the remote repository's context.
1007 if (strncmp(refname, "got/", 4) == 0)
1009 if (strncmp(refname, "remotes/", 8) == 0)
1012 if (strncmp(wanted_ref, "refs/", 5) == 0)
1015 /* Allow prefix match. */
1016 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1019 /* Allow exact match. */
1020 return (strcmp(refname, wanted_ref) == 0);
1024 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1026 struct got_pathlist_entry *pe;
1028 TAILQ_FOREACH(pe, wanted_refs, entry) {
1029 if (match_wanted_ref(refname, pe->path))
1036 static const struct got_error *
1037 create_wanted_ref(const char *refname, struct got_object_id *id,
1038 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1040 const struct got_error *err;
1041 char *remote_refname;
1043 if (strncmp("refs/", refname, 5) == 0)
1046 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1047 remote_repo_name, refname) == -1)
1048 return got_error_from_errno("asprintf");
1050 err = create_ref(remote_refname, id, verbosity, repo);
1051 free(remote_refname);
1055 static const struct got_error *
1056 cmd_clone(int argc, char *argv[])
1058 const struct got_error *error = NULL;
1059 const char *uri, *dirname;
1060 char *proto, *host, *port, *repo_name, *server_path;
1061 char *default_destdir = NULL, *id_str = NULL;
1062 const char *repo_path;
1063 struct got_repository *repo = NULL;
1064 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1065 struct got_pathlist_entry *pe;
1066 struct got_object_id *pack_hash = NULL;
1067 int ch, fetchfd = -1, fetchstatus;
1068 pid_t fetchpid = -1;
1069 struct got_fetch_progress_arg fpa;
1070 char *git_url = NULL;
1071 char *gitconfig_path = NULL, *gotconfig_path = NULL;
1072 char *gitconfig = NULL, *gotconfig = NULL;
1073 FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1075 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1076 int list_refs_only = 0;
1077 struct got_reference *head_symref = NULL;
1080 TAILQ_INIT(&symrefs);
1081 TAILQ_INIT(&wanted_branches);
1082 TAILQ_INIT(&wanted_refs);
1084 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1087 fetch_all_branches = 1;
1090 error = got_pathlist_append(&wanted_branches,
1099 mirror_references = 1;
1104 else if (verbosity < 3)
1111 error = got_pathlist_append(&wanted_refs,
1124 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1125 errx(1, "-a and -b options are mutually exclusive");
1126 if (list_refs_only) {
1127 if (!TAILQ_EMPTY(&wanted_branches))
1128 errx(1, "-l and -b options are mutually exclusive");
1129 if (fetch_all_branches)
1130 errx(1, "-l and -a options are mutually exclusive");
1131 if (mirror_references)
1132 errx(1, "-l and -m options are mutually exclusive");
1133 if (verbosity == -1)
1134 errx(1, "-l and -q options are mutually exclusive");
1135 if (!TAILQ_EMPTY(&wanted_refs))
1136 errx(1, "-l and -R options are mutually exclusive");
1148 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1154 host, port ? ":" : "", port ? port : "",
1155 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1156 error = got_error_from_errno("asprintf");
1160 if (strcmp(proto, "git") == 0) {
1162 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1163 "sendfd dns inet unveil", NULL) == -1)
1166 } else if (strcmp(proto, "git+ssh") == 0 ||
1167 strcmp(proto, "ssh") == 0) {
1169 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1170 "sendfd unveil", NULL) == -1)
1173 } else if (strcmp(proto, "http") == 0 ||
1174 strcmp(proto, "git+http") == 0) {
1175 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1178 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1181 if (dirname == NULL) {
1182 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1183 error = got_error_from_errno("asprintf");
1186 repo_path = default_destdir;
1188 repo_path = dirname;
1190 if (!list_refs_only) {
1191 error = got_path_mkdir(repo_path);
1195 error = got_repo_init(repo_path);
1198 error = got_repo_open(&repo, repo_path, NULL);
1203 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1204 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1205 error = got_error_from_errno2("unveil",
1206 GOT_FETCH_PATH_SSH);
1210 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 printf("Connecting to %s%s%s\n", host,
1216 port ? ":" : "", port ? port : "");
1218 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1219 server_path, verbosity);
1223 fpa.last_scaled_size[0] = '\0';
1224 fpa.last_p_indexed = -1;
1225 fpa.last_p_resolved = -1;
1226 fpa.verbosity = verbosity;
1227 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1228 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1229 fetch_all_branches, &wanted_branches, &wanted_refs,
1230 list_refs_only, verbosity, fetchfd, repo,
1231 fetch_progress, &fpa);
1235 if (list_refs_only) {
1236 error = list_remote_refs(&symrefs, &refs);
1240 error = got_object_id_str(&id_str, pack_hash);
1244 printf("\nFetched %s.pack\n", id_str);
1247 /* Set up references provided with the pack file. */
1248 TAILQ_FOREACH(pe, &refs, entry) {
1249 const char *refname = pe->path;
1250 struct got_object_id *id = pe->data;
1251 char *remote_refname;
1253 if (is_wanted_ref(&wanted_refs, refname) &&
1254 !mirror_references) {
1255 error = create_wanted_ref(refname, id,
1256 GOT_FETCH_DEFAULT_REMOTE_NAME,
1257 verbosity - 1, repo);
1263 error = create_ref(refname, id, verbosity - 1, repo);
1267 if (mirror_references)
1270 if (strncmp("refs/heads/", refname, 11) != 0)
1273 if (asprintf(&remote_refname,
1274 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1275 refname + 11) == -1) {
1276 error = got_error_from_errno("asprintf");
1279 error = create_ref(remote_refname, id, verbosity - 1, repo);
1280 free(remote_refname);
1285 /* Set the HEAD reference if the server provided one. */
1286 TAILQ_FOREACH(pe, &symrefs, entry) {
1287 struct got_reference *target_ref;
1288 const char *refname = pe->path;
1289 const char *target = pe->data;
1290 char *remote_refname = NULL, *remote_target = NULL;
1292 if (strcmp(refname, GOT_REF_HEAD) != 0)
1295 error = got_ref_open(&target_ref, repo, target, 0);
1297 if (error->code == GOT_ERR_NOT_REF) {
1304 error = create_symref(refname, target_ref, verbosity, repo);
1305 got_ref_close(target_ref);
1309 if (mirror_references)
1312 if (strncmp("refs/heads/", target, 11) != 0)
1315 if (asprintf(&remote_refname,
1316 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1318 error = got_error_from_errno("asprintf");
1321 if (asprintf(&remote_target,
1322 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1323 target + 11) == -1) {
1324 error = got_error_from_errno("asprintf");
1325 free(remote_refname);
1328 error = got_ref_open(&target_ref, repo, remote_target, 0);
1330 free(remote_refname);
1331 free(remote_target);
1332 if (error->code == GOT_ERR_NOT_REF) {
1338 error = create_symref(remote_refname, target_ref,
1339 verbosity - 1, repo);
1340 free(remote_refname);
1341 free(remote_target);
1342 got_ref_close(target_ref);
1348 * We failed to set the HEAD reference. If we asked for
1349 * a set of wanted branches use the first of one of those
1350 * which could be fetched instead.
1352 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1353 const char *target = pe->path;
1354 struct got_reference *target_ref;
1356 error = got_ref_open(&target_ref, repo, target, 0);
1358 if (error->code == GOT_ERR_NOT_REF) {
1365 error = create_symref(GOT_REF_HEAD, target_ref,
1367 got_ref_close(target_ref);
1374 /* Create got.conf(5). */
1375 gotconfig_path = got_repo_get_path_gotconfig(repo);
1376 if (gotconfig_path == NULL) {
1377 error = got_error_from_errno("got_repo_get_path_gotconfig");
1380 gotconfig_file = fopen(gotconfig_path, "a");
1381 if (gotconfig_file == NULL) {
1382 error = got_error_from_errno2("fopen", gotconfig_path);
1385 got_path_strip_trailing_slashes(server_path);
1386 if (asprintf(&gotconfig,
1391 "\trepository \"%s\"\n"
1394 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1395 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1397 mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1398 error = got_error_from_errno("asprintf");
1401 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1402 if (n != strlen(gotconfig)) {
1403 error = got_ferror(gotconfig_file, GOT_ERR_IO);
1407 /* Create a config file Git can understand. */
1408 gitconfig_path = got_repo_get_path_gitconfig(repo);
1409 if (gitconfig_path == NULL) {
1410 error = got_error_from_errno("got_repo_get_path_gitconfig");
1413 gitconfig_file = fopen(gitconfig_path, "a");
1414 if (gitconfig_file == NULL) {
1415 error = got_error_from_errno2("fopen", gitconfig_path);
1418 if (mirror_references) {
1419 if (asprintf(&gitconfig,
1422 "\tfetch = +refs/*:refs/*\n"
1423 "\tmirror = true\n",
1424 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1425 error = got_error_from_errno("asprintf");
1428 } else if (fetch_all_branches) {
1429 if (asprintf(&gitconfig,
1432 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1433 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1434 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1435 error = got_error_from_errno("asprintf");
1439 const char *branchname;
1442 * If the server specified a default branch, use just that one.
1443 * Otherwise fall back to fetching all branches on next fetch.
1446 branchname = got_ref_get_symref_target(head_symref);
1447 if (strncmp(branchname, "refs/heads/", 11) == 0)
1450 branchname = "*"; /* fall back to all branches */
1451 if (asprintf(&gitconfig,
1454 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1455 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1456 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1457 branchname) == -1) {
1458 error = got_error_from_errno("asprintf");
1462 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1463 if (n != strlen(gitconfig)) {
1464 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 printf("Created %s repository '%s'\n",
1470 mirror_references ? "mirrored" : "cloned", repo_path);
1473 if (kill(fetchpid, SIGTERM) == -1)
1474 error = got_error_from_errno("kill");
1475 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1476 error = got_error_from_errno("waitpid");
1478 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1479 error = got_error_from_errno("close");
1480 if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1481 error = got_error_from_errno("fclose");
1482 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1483 error = got_error_from_errno("fclose");
1485 got_repo_close(repo);
1487 got_ref_close(head_symref);
1488 TAILQ_FOREACH(pe, &refs, entry) {
1489 free((void *)pe->path);
1492 got_pathlist_free(&refs);
1493 TAILQ_FOREACH(pe, &symrefs, entry) {
1494 free((void *)pe->path);
1497 got_pathlist_free(&symrefs);
1498 got_pathlist_free(&wanted_branches);
1499 got_pathlist_free(&wanted_refs);
1506 free(default_destdir);
1509 free(gotconfig_path);
1510 free(gitconfig_path);
1515 static const struct got_error *
1516 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1517 int replace_tags, int verbosity, struct got_repository *repo)
1519 const struct got_error *err = NULL;
1520 char *new_id_str = NULL;
1521 struct got_object_id *old_id = NULL;
1523 err = got_object_id_str(&new_id_str, new_id);
1527 if (!replace_tags &&
1528 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1529 err = got_ref_resolve(&old_id, repo, ref);
1532 if (got_object_id_cmp(old_id, new_id) == 0)
1534 if (verbosity >= 0) {
1535 printf("Rejecting update of existing tag %s: %s\n",
1536 got_ref_get_name(ref), new_id_str);
1541 if (got_ref_is_symbolic(ref)) {
1542 if (verbosity >= 0) {
1543 printf("Replacing reference %s: %s\n",
1544 got_ref_get_name(ref),
1545 got_ref_get_symref_target(ref));
1547 err = got_ref_change_symref_to_ref(ref, new_id);
1550 err = got_ref_write(ref, repo);
1554 err = got_ref_resolve(&old_id, repo, ref);
1557 if (got_object_id_cmp(old_id, new_id) == 0)
1560 err = got_ref_change_ref(ref, new_id);
1563 err = got_ref_write(ref, repo);
1569 printf("Updated %s: %s\n", got_ref_get_name(ref),
1577 static const struct got_error *
1578 update_symref(const char *refname, struct got_reference *target_ref,
1579 int verbosity, struct got_repository *repo)
1581 const struct got_error *err = NULL, *unlock_err;
1582 struct got_reference *symref;
1583 int symref_is_locked = 0;
1585 err = got_ref_open(&symref, repo, refname, 1);
1587 if (err->code != GOT_ERR_NOT_REF)
1589 err = got_ref_alloc_symref(&symref, refname, target_ref);
1593 err = got_ref_write(symref, repo);
1598 printf("Created reference %s: %s\n",
1599 got_ref_get_name(symref),
1600 got_ref_get_symref_target(symref));
1602 symref_is_locked = 1;
1604 if (strcmp(got_ref_get_symref_target(symref),
1605 got_ref_get_name(target_ref)) == 0)
1608 err = got_ref_change_symref(symref,
1609 got_ref_get_name(target_ref));
1613 err = got_ref_write(symref, repo);
1618 printf("Updated %s: %s\n", got_ref_get_name(symref),
1619 got_ref_get_symref_target(symref));
1623 if (symref_is_locked) {
1624 unlock_err = got_ref_unlock(symref);
1625 if (unlock_err && err == NULL)
1628 got_ref_close(symref);
1635 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1636 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1637 "[remote-repository-name]\n",
1642 static const struct got_error *
1643 delete_missing_ref(struct got_reference *ref,
1644 int verbosity, struct got_repository *repo)
1646 const struct got_error *err = NULL;
1647 struct got_object_id *id = NULL;
1648 char *id_str = NULL;
1650 if (got_ref_is_symbolic(ref)) {
1651 err = got_ref_delete(ref, repo);
1654 if (verbosity >= 0) {
1655 printf("Deleted reference %s: %s\n",
1656 got_ref_get_name(ref),
1657 got_ref_get_symref_target(ref));
1660 err = got_ref_resolve(&id, repo, ref);
1663 err = got_object_id_str(&id_str, id);
1667 err = got_ref_delete(ref, repo);
1670 if (verbosity >= 0) {
1671 printf("Deleted reference %s: %s\n",
1672 got_ref_get_name(ref), id_str);
1681 static const struct got_error *
1682 delete_missing_refs(struct got_pathlist_head *their_refs,
1683 struct got_pathlist_head *their_symrefs,
1684 const struct got_remote_repo *remote,
1685 int verbosity, struct got_repository *repo)
1687 const struct got_error *err = NULL, *unlock_err;
1688 struct got_reflist_head my_refs;
1689 struct got_reflist_entry *re;
1690 struct got_pathlist_entry *pe;
1691 char *remote_namespace = NULL;
1692 char *local_refname = NULL;
1694 SIMPLEQ_INIT(&my_refs);
1696 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1698 return got_error_from_errno("asprintf");
1700 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1704 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1705 const char *refname = got_ref_get_name(re->ref);
1707 if (!remote->mirror_references) {
1708 if (strncmp(refname, remote_namespace,
1709 strlen(remote_namespace)) == 0) {
1710 if (strcmp(refname + strlen(remote_namespace),
1713 if (asprintf(&local_refname, "refs/heads/%s",
1714 refname + strlen(remote_namespace)) == -1) {
1715 err = got_error_from_errno("asprintf");
1718 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1722 TAILQ_FOREACH(pe, their_refs, entry) {
1723 if (strcmp(local_refname, pe->path) == 0)
1729 TAILQ_FOREACH(pe, their_symrefs, entry) {
1730 if (strcmp(local_refname, pe->path) == 0)
1736 err = delete_missing_ref(re->ref, verbosity, repo);
1740 if (local_refname) {
1741 struct got_reference *ref;
1742 err = got_ref_open(&ref, repo, local_refname, 1);
1744 if (err->code != GOT_ERR_NOT_REF)
1746 free(local_refname);
1747 local_refname = NULL;
1750 err = delete_missing_ref(ref, verbosity, repo);
1753 unlock_err = got_ref_unlock(ref);
1755 if (unlock_err && err == NULL) {
1760 free(local_refname);
1761 local_refname = NULL;
1765 free(remote_namespace);
1766 free(local_refname);
1770 static const struct got_error *
1771 update_wanted_ref(const char *refname, struct got_object_id *id,
1772 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1774 const struct got_error *err, *unlock_err;
1775 char *remote_refname;
1776 struct got_reference *ref;
1778 if (strncmp("refs/", refname, 5) == 0)
1781 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1782 remote_repo_name, refname) == -1)
1783 return got_error_from_errno("asprintf");
1785 err = got_ref_open(&ref, repo, remote_refname, 1);
1787 if (err->code != GOT_ERR_NOT_REF)
1789 err = create_ref(remote_refname, id, verbosity, repo);
1791 err = update_ref(ref, id, 0, verbosity, repo);
1792 unlock_err = got_ref_unlock(ref);
1793 if (unlock_err && err == NULL)
1798 free(remote_refname);
1802 static const struct got_error *
1803 cmd_fetch(int argc, char *argv[])
1805 const struct got_error *error = NULL, *unlock_err;
1806 char *cwd = NULL, *repo_path = NULL;
1807 const char *remote_name;
1808 char *proto = NULL, *host = NULL, *port = NULL;
1809 char *repo_name = NULL, *server_path = NULL;
1810 const struct got_remote_repo *remotes, *remote = NULL;
1812 char *id_str = NULL;
1813 struct got_repository *repo = NULL;
1814 struct got_worktree *worktree = NULL;
1815 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1816 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1817 struct got_pathlist_entry *pe;
1818 struct got_object_id *pack_hash = NULL;
1819 int i, ch, fetchfd = -1, fetchstatus;
1820 pid_t fetchpid = -1;
1821 struct got_fetch_progress_arg fpa;
1822 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1823 int delete_refs = 0, replace_tags = 0;
1826 TAILQ_INIT(&symrefs);
1827 TAILQ_INIT(&wanted_branches);
1828 TAILQ_INIT(&wanted_refs);
1830 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1833 fetch_all_branches = 1;
1836 error = got_pathlist_append(&wanted_branches,
1848 repo_path = realpath(optarg, NULL);
1849 if (repo_path == NULL)
1850 return got_error_from_errno2("realpath",
1852 got_path_strip_trailing_slashes(repo_path);
1860 else if (verbosity < 3)
1867 error = got_pathlist_append(&wanted_refs,
1880 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1881 errx(1, "-a and -b options are mutually exclusive");
1882 if (list_refs_only) {
1883 if (!TAILQ_EMPTY(&wanted_branches))
1884 errx(1, "-l and -b options are mutually exclusive");
1885 if (fetch_all_branches)
1886 errx(1, "-l and -a options are mutually exclusive");
1888 errx(1, "-l and -d options are mutually exclusive");
1889 if (verbosity == -1)
1890 errx(1, "-l and -q options are mutually exclusive");
1894 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1896 remote_name = argv[0];
1900 cwd = getcwd(NULL, 0);
1902 error = got_error_from_errno("getcwd");
1906 if (repo_path == NULL) {
1907 error = got_worktree_open(&worktree, cwd);
1908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1914 strdup(got_worktree_get_repo_path(worktree));
1915 if (repo_path == NULL)
1916 error = got_error_from_errno("strdup");
1920 repo_path = strdup(cwd);
1921 if (repo_path == NULL) {
1922 error = got_error_from_errno("strdup");
1928 error = got_repo_open(&repo, repo_path, NULL);
1933 worktree_conf = got_worktree_get_gotconfig(worktree);
1934 if (worktree_conf) {
1935 got_gotconfig_get_remotes(&nremotes, &remotes,
1937 for (i = 0; i < nremotes; i++) {
1938 remote = &remotes[i];
1939 if (strcmp(remote->name, remote_name) == 0)
1944 if (remote == NULL) {
1945 repo_conf = got_repo_get_gotconfig(repo);
1947 got_gotconfig_get_remotes(&nremotes, &remotes,
1949 for (i = 0; i < nremotes; i++) {
1950 remote = &remotes[i];
1951 if (strcmp(remote->name, remote_name) == 0)
1956 if (remote == NULL) {
1957 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1958 for (i = 0; i < nremotes; i++) {
1959 remote = &remotes[i];
1960 if (strcmp(remote->name, remote_name) == 0)
1964 if (remote == NULL) {
1965 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1969 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1970 &repo_name, remote->url);
1974 if (strcmp(proto, "git") == 0) {
1976 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1977 "sendfd dns inet unveil", NULL) == -1)
1980 } else if (strcmp(proto, "git+ssh") == 0 ||
1981 strcmp(proto, "ssh") == 0) {
1983 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1984 "sendfd unveil", NULL) == -1)
1987 } else if (strcmp(proto, "http") == 0 ||
1988 strcmp(proto, "git+http") == 0) {
1989 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1992 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1996 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1997 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1998 error = got_error_from_errno2("unveil",
1999 GOT_FETCH_PATH_SSH);
2003 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2009 port ? ":" : "", port ? port : "");
2011 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2012 server_path, verbosity);
2016 fpa.last_scaled_size[0] = '\0';
2017 fpa.last_p_indexed = -1;
2018 fpa.last_p_resolved = -1;
2019 fpa.verbosity = verbosity;
2020 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2021 remote->mirror_references, fetch_all_branches, &wanted_branches,
2022 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2023 fetch_progress, &fpa);
2027 if (list_refs_only) {
2028 error = list_remote_refs(&symrefs, &refs);
2032 if (pack_hash == NULL) {
2034 printf("Already up-to-date\n");
2035 } else if (verbosity >= 0) {
2036 error = got_object_id_str(&id_str, pack_hash);
2039 printf("\nFetched %s.pack\n", id_str);
2044 /* Update references provided with the pack file. */
2045 TAILQ_FOREACH(pe, &refs, entry) {
2046 const char *refname = pe->path;
2047 struct got_object_id *id = pe->data;
2048 struct got_reference *ref;
2049 char *remote_refname;
2051 if (is_wanted_ref(&wanted_refs, refname) &&
2052 !remote->mirror_references) {
2053 error = update_wanted_ref(refname, id,
2054 remote->name, verbosity, repo);
2060 if (remote->mirror_references ||
2061 strncmp("refs/tags/", refname, 10) == 0) {
2062 error = got_ref_open(&ref, repo, refname, 1);
2064 if (error->code != GOT_ERR_NOT_REF)
2066 error = create_ref(refname, id, verbosity,
2071 error = update_ref(ref, id, replace_tags,
2073 unlock_err = got_ref_unlock(ref);
2074 if (unlock_err && error == NULL)
2080 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2081 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2082 remote_name, refname + 11) == -1) {
2083 error = got_error_from_errno("asprintf");
2087 error = got_ref_open(&ref, repo, remote_refname, 1);
2089 if (error->code != GOT_ERR_NOT_REF)
2091 error = create_ref(remote_refname, id,
2096 error = update_ref(ref, id, replace_tags,
2098 unlock_err = got_ref_unlock(ref);
2099 if (unlock_err && error == NULL)
2106 /* Also create a local branch if none exists yet. */
2107 error = got_ref_open(&ref, repo, refname, 1);
2109 if (error->code != GOT_ERR_NOT_REF)
2111 error = create_ref(refname, id, verbosity,
2116 unlock_err = got_ref_unlock(ref);
2117 if (unlock_err && error == NULL)
2124 error = delete_missing_refs(&refs, &symrefs, remote,
2130 if (!remote->mirror_references) {
2131 /* Update remote HEAD reference if the server provided one. */
2132 TAILQ_FOREACH(pe, &symrefs, entry) {
2133 struct got_reference *target_ref;
2134 const char *refname = pe->path;
2135 const char *target = pe->data;
2136 char *remote_refname = NULL, *remote_target = NULL;
2138 if (strcmp(refname, GOT_REF_HEAD) != 0)
2141 if (strncmp("refs/heads/", target, 11) != 0)
2144 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2145 remote->name, refname) == -1) {
2146 error = got_error_from_errno("asprintf");
2149 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2150 remote->name, target + 11) == -1) {
2151 error = got_error_from_errno("asprintf");
2152 free(remote_refname);
2156 error = got_ref_open(&target_ref, repo, remote_target,
2159 free(remote_refname);
2160 free(remote_target);
2161 if (error->code == GOT_ERR_NOT_REF) {
2167 error = update_symref(remote_refname, target_ref,
2169 free(remote_refname);
2170 free(remote_target);
2171 got_ref_close(target_ref);
2178 if (kill(fetchpid, SIGTERM) == -1)
2179 error = got_error_from_errno("kill");
2180 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2181 error = got_error_from_errno("waitpid");
2183 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2184 error = got_error_from_errno("close");
2186 got_repo_close(repo);
2188 got_worktree_close(worktree);
2189 TAILQ_FOREACH(pe, &refs, entry) {
2190 free((void *)pe->path);
2193 got_pathlist_free(&refs);
2194 TAILQ_FOREACH(pe, &symrefs, entry) {
2195 free((void *)pe->path);
2198 got_pathlist_free(&symrefs);
2199 got_pathlist_free(&wanted_branches);
2200 got_pathlist_free(&wanted_refs);
2215 usage_checkout(void)
2217 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2218 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2223 show_worktree_base_ref_warning(void)
2225 fprintf(stderr, "%s: warning: could not create a reference "
2226 "to the work tree's base commit; the commit could be "
2227 "garbage-collected by Git; making the repository "
2228 "writable and running 'got update' will prevent this\n",
2232 struct got_checkout_progress_arg {
2233 const char *worktree_path;
2234 int had_base_commit_ref_error;
2237 static const struct got_error *
2238 checkout_progress(void *arg, unsigned char status, const char *path)
2240 struct got_checkout_progress_arg *a = arg;
2242 /* Base commit bump happens silently. */
2243 if (status == GOT_STATUS_BUMP_BASE)
2246 if (status == GOT_STATUS_BASE_REF_ERR) {
2247 a->had_base_commit_ref_error = 1;
2251 while (path[0] == '/')
2254 printf("%c %s/%s\n", status, a->worktree_path, path);
2258 static const struct got_error *
2259 check_cancelled(void *arg)
2261 if (sigint_received || sigpipe_received)
2262 return got_error(GOT_ERR_CANCELLED);
2266 static const struct got_error *
2267 check_linear_ancestry(struct got_object_id *commit_id,
2268 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2269 struct got_repository *repo)
2271 const struct got_error *err = NULL;
2272 struct got_object_id *yca_id;
2274 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2275 commit_id, base_commit_id, repo, check_cancelled, NULL);
2280 return got_error(GOT_ERR_ANCESTRY);
2283 * Require a straight line of history between the target commit
2284 * and the work tree's base commit.
2286 * Non-linear situations such as this require a rebase:
2288 * (commit) D F (base_commit)
2296 * 'got update' only handles linear cases:
2297 * Update forwards in time: A (base/yca) - B - C - D (commit)
2298 * Update backwards in time: D (base) - C - B - A (commit/yca)
2300 if (allow_forwards_in_time_only) {
2301 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2302 return got_error(GOT_ERR_ANCESTRY);
2303 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2304 got_object_id_cmp(base_commit_id, yca_id) != 0)
2305 return got_error(GOT_ERR_ANCESTRY);
2311 static const struct got_error *
2312 check_same_branch(struct got_object_id *commit_id,
2313 struct got_reference *head_ref, struct got_object_id *yca_id,
2314 struct got_repository *repo)
2316 const struct got_error *err = NULL;
2317 struct got_commit_graph *graph = NULL;
2318 struct got_object_id *head_commit_id = NULL;
2319 int is_same_branch = 0;
2321 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2325 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2329 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 err = got_commit_graph_open(&graph, "/", 1);
2338 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2339 check_cancelled, NULL);
2344 struct got_object_id *id;
2345 err = got_commit_graph_iter_next(&id, graph, repo,
2346 check_cancelled, NULL);
2348 if (err->code == GOT_ERR_ITER_COMPLETED)
2354 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2356 if (got_object_id_cmp(id, commit_id) == 0) {
2364 got_commit_graph_close(graph);
2365 free(head_commit_id);
2366 if (!err && !is_same_branch)
2367 err = got_error(GOT_ERR_ANCESTRY);
2371 static const struct got_error *
2372 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2374 static char msg[512];
2375 const char *branch_name;
2377 if (got_ref_is_symbolic(ref))
2378 branch_name = got_ref_get_symref_target(ref);
2380 branch_name = got_ref_get_name(ref);
2382 if (strncmp("refs/heads/", branch_name, 11) == 0)
2385 snprintf(msg, sizeof(msg),
2386 "target commit is not contained in branch '%s'; "
2387 "the branch to use must be specified with -b; "
2388 "if necessary a new branch can be created for "
2389 "this commit with 'got branch -c %s BRANCH_NAME'",
2390 branch_name, commit_id_str);
2392 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2395 static const struct got_error *
2396 cmd_checkout(int argc, char *argv[])
2398 const struct got_error *error = NULL;
2399 struct got_repository *repo = NULL;
2400 struct got_reference *head_ref = NULL;
2401 struct got_worktree *worktree = NULL;
2402 char *repo_path = NULL;
2403 char *worktree_path = NULL;
2404 const char *path_prefix = "";
2405 const char *branch_name = GOT_REF_HEAD;
2406 char *commit_id_str = NULL;
2407 int ch, same_path_prefix, allow_nonempty = 0;
2408 struct got_pathlist_head paths;
2409 struct got_checkout_progress_arg cpa;
2413 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2416 branch_name = optarg;
2419 commit_id_str = strdup(optarg);
2420 if (commit_id_str == NULL)
2421 return got_error_from_errno("strdup");
2427 path_prefix = optarg;
2439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2440 "unveil", NULL) == -1)
2444 char *cwd, *base, *dotgit;
2445 repo_path = realpath(argv[0], NULL);
2446 if (repo_path == NULL)
2447 return got_error_from_errno2("realpath", argv[0]);
2448 cwd = getcwd(NULL, 0);
2450 error = got_error_from_errno("getcwd");
2453 if (path_prefix[0]) {
2454 base = basename(path_prefix);
2456 error = got_error_from_errno2("basename",
2461 base = basename(repo_path);
2463 error = got_error_from_errno2("basename",
2468 dotgit = strstr(base, ".git");
2471 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2472 error = got_error_from_errno("asprintf");
2477 } else if (argc == 2) {
2478 repo_path = realpath(argv[0], NULL);
2479 if (repo_path == NULL) {
2480 error = got_error_from_errno2("realpath", argv[0]);
2483 worktree_path = realpath(argv[1], NULL);
2484 if (worktree_path == NULL) {
2485 if (errno != ENOENT) {
2486 error = got_error_from_errno2("realpath",
2490 worktree_path = strdup(argv[1]);
2491 if (worktree_path == NULL) {
2492 error = got_error_from_errno("strdup");
2499 got_path_strip_trailing_slashes(repo_path);
2500 got_path_strip_trailing_slashes(worktree_path);
2502 error = got_repo_open(&repo, repo_path, NULL);
2506 /* Pre-create work tree path for unveil(2) */
2507 error = got_path_mkdir(worktree_path);
2509 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2510 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2512 if (!allow_nonempty &&
2513 !got_path_dir_is_empty(worktree_path)) {
2514 error = got_error_path(worktree_path,
2515 GOT_ERR_DIR_NOT_EMPTY);
2520 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2524 error = got_ref_open(&head_ref, repo, branch_name, 0);
2528 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2529 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2532 error = got_worktree_open(&worktree, worktree_path);
2536 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2540 if (!same_path_prefix) {
2541 error = got_error(GOT_ERR_PATH_PREFIX);
2545 if (commit_id_str) {
2546 struct got_object_id *commit_id;
2547 error = got_repo_match_object_id(&commit_id, NULL,
2548 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2551 error = check_linear_ancestry(commit_id,
2552 got_worktree_get_base_commit_id(worktree), 0, repo);
2553 if (error != NULL) {
2555 if (error->code == GOT_ERR_ANCESTRY) {
2556 error = checkout_ancestry_error(
2557 head_ref, commit_id_str);
2561 error = check_same_branch(commit_id, head_ref, NULL, repo);
2563 if (error->code == GOT_ERR_ANCESTRY) {
2564 error = checkout_ancestry_error(
2565 head_ref, commit_id_str);
2569 error = got_worktree_set_base_commit_id(worktree, repo,
2576 error = got_pathlist_append(&paths, "", NULL);
2579 cpa.worktree_path = worktree_path;
2580 cpa.had_base_commit_ref_error = 0;
2581 error = got_worktree_checkout_files(worktree, &paths, repo,
2582 checkout_progress, &cpa, check_cancelled, NULL);
2586 printf("Now shut up and hack\n");
2587 if (cpa.had_base_commit_ref_error)
2588 show_worktree_base_ref_warning();
2590 got_pathlist_free(&paths);
2591 free(commit_id_str);
2593 free(worktree_path);
2597 struct got_update_progress_arg {
2605 print_update_progress_stats(struct got_update_progress_arg *upa)
2607 if (!upa->did_something)
2610 if (upa->conflicts > 0)
2611 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2612 if (upa->obstructed > 0)
2613 printf("File paths obstructed by a non-regular file: %d\n",
2615 if (upa->not_updated > 0)
2616 printf("Files not updated because of existing merge "
2617 "conflicts: %d\n", upa->not_updated);
2623 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2628 static const struct got_error *
2629 update_progress(void *arg, unsigned char status, const char *path)
2631 struct got_update_progress_arg *upa = arg;
2633 if (status == GOT_STATUS_EXISTS ||
2634 status == GOT_STATUS_BASE_REF_ERR)
2637 upa->did_something = 1;
2639 /* Base commit bump happens silently. */
2640 if (status == GOT_STATUS_BUMP_BASE)
2643 if (status == GOT_STATUS_CONFLICT)
2645 if (status == GOT_STATUS_OBSTRUCTED)
2647 if (status == GOT_STATUS_CANNOT_UPDATE)
2650 while (path[0] == '/')
2652 printf("%c %s\n", status, path);
2656 static const struct got_error *
2657 switch_head_ref(struct got_reference *head_ref,
2658 struct got_object_id *commit_id, struct got_worktree *worktree,
2659 struct got_repository *repo)
2661 const struct got_error *err = NULL;
2663 int ref_has_moved = 0;
2665 /* Trivial case: switching between two different references. */
2666 if (strcmp(got_ref_get_name(head_ref),
2667 got_worktree_get_head_ref_name(worktree)) != 0) {
2668 printf("Switching work tree from %s to %s\n",
2669 got_worktree_get_head_ref_name(worktree),
2670 got_ref_get_name(head_ref));
2671 return got_worktree_set_head_ref(worktree, head_ref);
2674 err = check_linear_ancestry(commit_id,
2675 got_worktree_get_base_commit_id(worktree), 0, repo);
2677 if (err->code != GOT_ERR_ANCESTRY)
2684 /* Switching to a rebased branch with the same reference name. */
2685 err = got_object_id_str(&base_id_str,
2686 got_worktree_get_base_commit_id(worktree));
2689 printf("Reference %s now points at a different branch\n",
2690 got_worktree_get_head_ref_name(worktree));
2691 printf("Switching work tree from %s to %s\n", base_id_str,
2692 got_worktree_get_head_ref_name(worktree));
2696 static const struct got_error *
2697 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2699 const struct got_error *err;
2702 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2706 return got_error(GOT_ERR_REBASING);
2708 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2712 return got_error(GOT_ERR_HISTEDIT_BUSY);
2717 static const struct got_error *
2718 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2719 char *argv[], struct got_worktree *worktree)
2721 const struct got_error *err = NULL;
2728 return got_error_from_errno("strdup");
2729 return got_pathlist_append(paths, path, NULL);
2732 for (i = 0; i < argc; i++) {
2733 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2736 err = got_pathlist_append(paths, path, NULL);
2746 static const struct got_error *
2747 wrap_not_worktree_error(const struct got_error *orig_err,
2748 const char *cmdname, const char *path)
2750 const struct got_error *err;
2751 struct got_repository *repo;
2752 static char msg[512];
2754 err = got_repo_open(&repo, path, NULL);
2758 snprintf(msg, sizeof(msg),
2759 "'got %s' needs a work tree in addition to a git repository\n"
2760 "Work trees can be checked out from this Git repository with "
2762 "The got(1) manual page contains more information.", cmdname);
2763 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2764 got_repo_close(repo);
2768 static const struct got_error *
2769 cmd_update(int argc, char *argv[])
2771 const struct got_error *error = NULL;
2772 struct got_repository *repo = NULL;
2773 struct got_worktree *worktree = NULL;
2774 char *worktree_path = NULL;
2775 struct got_object_id *commit_id = NULL;
2776 char *commit_id_str = NULL;
2777 const char *branch_name = NULL;
2778 struct got_reference *head_ref = NULL;
2779 struct got_pathlist_head paths;
2780 struct got_pathlist_entry *pe;
2782 struct got_update_progress_arg upa;
2786 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2789 branch_name = optarg;
2792 commit_id_str = strdup(optarg);
2793 if (commit_id_str == NULL)
2794 return got_error_from_errno("strdup");
2806 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2807 "unveil", NULL) == -1)
2810 worktree_path = getcwd(NULL, 0);
2811 if (worktree_path == NULL) {
2812 error = got_error_from_errno("getcwd");
2815 error = got_worktree_open(&worktree, worktree_path);
2817 if (error->code == GOT_ERR_NOT_WORKTREE)
2818 error = wrap_not_worktree_error(error, "update",
2823 error = check_rebase_or_histedit_in_progress(worktree);
2827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2832 error = apply_unveil(got_repo_get_path(repo), 0,
2833 got_worktree_get_root_path(worktree));
2837 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2841 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2842 got_worktree_get_head_ref_name(worktree), 0);
2845 if (commit_id_str == NULL) {
2846 error = got_ref_resolve(&commit_id, repo, head_ref);
2849 error = got_object_id_str(&commit_id_str, commit_id);
2853 error = got_repo_match_object_id(&commit_id, NULL,
2854 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2855 free(commit_id_str);
2856 commit_id_str = NULL;
2859 error = got_object_id_str(&commit_id_str, commit_id);
2865 struct got_object_id *head_commit_id;
2866 TAILQ_FOREACH(pe, &paths, entry) {
2867 if (pe->path_len == 0)
2869 error = got_error_msg(GOT_ERR_BAD_PATH,
2870 "switching between branches requires that "
2871 "the entire work tree gets updated");
2874 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2877 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2879 free(head_commit_id);
2882 error = check_same_branch(commit_id, head_ref, NULL, repo);
2885 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2889 error = check_linear_ancestry(commit_id,
2890 got_worktree_get_base_commit_id(worktree), 0, repo);
2891 if (error != NULL) {
2892 if (error->code == GOT_ERR_ANCESTRY)
2893 error = got_error(GOT_ERR_BRANCH_MOVED);
2896 error = check_same_branch(commit_id, head_ref, NULL, repo);
2901 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2903 error = got_worktree_set_base_commit_id(worktree, repo,
2909 memset(&upa, 0, sizeof(upa));
2910 error = got_worktree_checkout_files(worktree, &paths, repo,
2911 update_progress, &upa, check_cancelled, NULL);
2915 if (upa.did_something)
2916 printf("Updated to commit %s\n", commit_id_str);
2918 printf("Already up-to-date\n");
2919 print_update_progress_stats(&upa);
2921 free(worktree_path);
2922 TAILQ_FOREACH(pe, &paths, entry)
2923 free((char *)pe->path);
2924 got_pathlist_free(&paths);
2926 free(commit_id_str);
2930 static const struct got_error *
2931 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2932 const char *path, int diff_context, int ignore_whitespace,
2933 struct got_repository *repo)
2935 const struct got_error *err = NULL;
2936 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2939 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2944 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2948 while (path[0] == '/')
2950 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2951 ignore_whitespace, stdout);
2954 got_object_blob_close(blob1);
2955 got_object_blob_close(blob2);
2959 static const struct got_error *
2960 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2961 const char *path, int diff_context, int ignore_whitespace,
2962 struct got_repository *repo)
2964 const struct got_error *err = NULL;
2965 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2966 struct got_diff_blob_output_unidiff_arg arg;
2969 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2974 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2978 arg.diff_context = diff_context;
2979 arg.ignore_whitespace = ignore_whitespace;
2980 arg.outfile = stdout;
2981 while (path[0] == '/')
2983 err = got_diff_tree(tree1, tree2, path, path, repo,
2984 got_diff_blob_output_unidiff, &arg, 1);
2987 got_object_tree_close(tree1);
2989 got_object_tree_close(tree2);
2993 static const struct got_error *
2994 get_changed_paths(struct got_pathlist_head *paths,
2995 struct got_commit_object *commit, struct got_repository *repo)
2997 const struct got_error *err = NULL;
2998 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2999 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3000 struct got_object_qid *qid;
3002 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3004 struct got_commit_object *pcommit;
3005 err = got_object_open_as_commit(&pcommit, repo,
3010 tree_id1 = got_object_commit_get_tree_id(pcommit);
3011 got_object_commit_close(pcommit);
3016 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3021 tree_id2 = got_object_commit_get_tree_id(commit);
3022 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3026 err = got_diff_tree(tree1, tree2, "", "", repo,
3027 got_diff_tree_collect_changed_paths, paths, 0);
3030 got_object_tree_close(tree1);
3032 got_object_tree_close(tree2);
3036 static const struct got_error *
3037 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3038 const char *path, int diff_context, struct got_repository *repo)
3040 const struct got_error *err = NULL;
3041 struct got_commit_object *pcommit = NULL;
3042 char *id_str1 = NULL, *id_str2 = NULL;
3043 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3044 struct got_object_qid *qid;
3046 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3048 err = got_object_open_as_commit(&pcommit, repo,
3054 if (path && path[0] != '\0') {
3056 err = got_object_id_by_path(&obj_id2, repo, id, path);
3059 err = got_object_id_str(&id_str2, obj_id2);
3065 err = got_object_id_by_path(&obj_id1, repo,
3068 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3073 err = got_object_id_str(&id_str1, obj_id1);
3080 err = got_object_get_type(&obj_type, repo, obj_id2);
3085 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3087 case GOT_OBJ_TYPE_BLOB:
3088 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3091 case GOT_OBJ_TYPE_TREE:
3092 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3096 err = got_error(GOT_ERR_OBJ_TYPE);
3102 obj_id2 = got_object_commit_get_tree_id(commit);
3103 err = got_object_id_str(&id_str2, obj_id2);
3106 obj_id1 = got_object_commit_get_tree_id(pcommit);
3107 err = got_object_id_str(&id_str1, obj_id1);
3110 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3111 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3117 got_object_commit_close(pcommit);
3122 get_datestr(time_t *time, char *datebuf)
3124 struct tm mytm, *tm;
3127 tm = gmtime_r(time, &mytm);
3130 s = asctime_r(tm, datebuf);
3133 p = strchr(s, '\n');
3139 static const struct got_error *
3140 match_logmsg(int *have_match, struct got_object_id *id,
3141 struct got_commit_object *commit, regex_t *regex)
3143 const struct got_error *err = NULL;
3144 regmatch_t regmatch;
3145 char *id_str = NULL, *logmsg = NULL;
3149 err = got_object_id_str(&id_str, id);
3153 err = got_object_commit_get_logmsg(&logmsg, commit);
3157 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3166 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3169 regmatch_t regmatch;
3170 struct got_pathlist_entry *pe;
3174 TAILQ_FOREACH(pe, changed_paths, entry) {
3175 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3182 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3184 static const struct got_error *
3185 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3186 struct got_repository *repo, const char *path,
3187 struct got_pathlist_head *changed_paths, int show_patch,
3188 int diff_context, struct got_reflist_head *refs)
3190 const struct got_error *err = NULL;
3191 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3193 time_t committer_time;
3194 const char *author, *committer;
3195 char *refs_str = NULL;
3196 struct got_reflist_entry *re;
3198 SIMPLEQ_FOREACH(re, refs, entry) {
3201 struct got_tag_object *tag = NULL;
3204 name = got_ref_get_name(re->ref);
3205 if (strcmp(name, GOT_REF_HEAD) == 0)
3207 if (strncmp(name, "refs/", 5) == 0)
3209 if (strncmp(name, "got/", 4) == 0)
3211 if (strncmp(name, "heads/", 6) == 0)
3213 if (strncmp(name, "remotes/", 8) == 0) {
3215 s = strstr(name, "/" GOT_REF_HEAD);
3216 if (s != NULL && s[strlen(s)] == '\0')
3219 if (strncmp(name, "tags/", 5) == 0) {
3220 err = got_object_open_as_tag(&tag, repo, re->id);
3222 if (err->code != GOT_ERR_OBJ_TYPE)
3224 /* Ref points at something other than a tag. */
3229 cmp = got_object_id_cmp(tag ?
3230 got_object_tag_get_object_id(tag) : re->id, id);
3232 got_object_tag_close(tag);
3236 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3238 err = got_error_from_errno("asprintf");
3244 err = got_object_id_str(&id_str, id);
3248 printf(GOT_COMMIT_SEP_STR);
3249 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3250 refs_str ? refs_str : "", refs_str ? ")" : "");
3255 printf("from: %s\n", got_object_commit_get_author(commit));
3256 committer_time = got_object_commit_get_committer_time(commit);
3257 datestr = get_datestr(&committer_time, datebuf);
3259 printf("date: %s UTC\n", datestr);
3260 author = got_object_commit_get_author(commit);
3261 committer = got_object_commit_get_committer(commit);
3262 if (strcmp(author, committer) != 0)
3263 printf("via: %s\n", committer);
3264 if (got_object_commit_get_nparents(commit) > 1) {
3265 const struct got_object_id_queue *parent_ids;
3266 struct got_object_qid *qid;
3268 parent_ids = got_object_commit_get_parent_ids(commit);
3269 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3270 err = got_object_id_str(&id_str, qid->id);
3273 printf("parent %d: %s\n", n++, id_str);
3278 err = got_object_commit_get_logmsg(&logmsg0, commit);
3284 line = strsep(&logmsg, "\n");
3286 printf(" %s\n", line);
3290 if (changed_paths) {
3291 struct got_pathlist_entry *pe;
3292 TAILQ_FOREACH(pe, changed_paths, entry) {
3293 struct got_diff_changed_path *cp = pe->data;
3294 printf(" %c %s\n", cp->status, pe->path);
3299 err = print_patch(commit, id, path, diff_context, repo);
3304 if (fflush(stdout) != 0 && err == NULL)
3305 err = got_error_from_errno("fflush");
3309 static const struct got_error *
3310 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3311 struct got_repository *repo, const char *path, int show_changed_paths,
3312 int show_patch, const char *search_pattern, int diff_context, int limit,
3313 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3315 const struct got_error *err;
3316 struct got_commit_graph *graph;
3319 struct got_object_id_queue reversed_commits;
3320 struct got_object_qid *qid;
3321 struct got_commit_object *commit;
3322 struct got_pathlist_head changed_paths;
3323 struct got_pathlist_entry *pe;
3325 SIMPLEQ_INIT(&reversed_commits);
3326 TAILQ_INIT(&changed_paths);
3328 if (search_pattern && regcomp(®ex, search_pattern,
3329 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3330 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3332 err = got_commit_graph_open(&graph, path, !log_branches);
3335 err = got_commit_graph_iter_start(graph, root_id, repo,
3336 check_cancelled, NULL);
3340 struct got_object_id *id;
3342 if (sigint_received || sigpipe_received)
3345 err = got_commit_graph_iter_next(&id, graph, repo,
3346 check_cancelled, NULL);
3348 if (err->code == GOT_ERR_ITER_COMPLETED)
3355 err = got_object_open_as_commit(&commit, repo, id);
3359 if (show_changed_paths && !reverse_display_order) {
3360 err = get_changed_paths(&changed_paths, commit, repo);
3365 if (search_pattern) {
3366 err = match_logmsg(&have_match, id, commit, ®ex);
3368 got_object_commit_close(commit);
3371 if (have_match == 0 && show_changed_paths)
3372 match_changed_paths(&have_match,
3373 &changed_paths, ®ex);
3374 if (have_match == 0) {
3375 got_object_commit_close(commit);
3376 TAILQ_FOREACH(pe, &changed_paths, entry) {
3377 free((char *)pe->path);
3380 got_pathlist_free(&changed_paths);
3385 if (reverse_display_order) {
3386 err = got_object_qid_alloc(&qid, id);
3389 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3390 got_object_commit_close(commit);
3392 err = print_commit(commit, id, repo, path,
3393 show_changed_paths ? &changed_paths : NULL,
3394 show_patch, diff_context, refs);
3395 got_object_commit_close(commit);
3399 if ((limit && --limit == 0) ||
3400 (end_id && got_object_id_cmp(id, end_id) == 0))
3403 TAILQ_FOREACH(pe, &changed_paths, entry) {
3404 free((char *)pe->path);
3407 got_pathlist_free(&changed_paths);
3409 if (reverse_display_order) {
3410 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3411 err = got_object_open_as_commit(&commit, repo, qid->id);
3414 if (show_changed_paths) {
3415 err = get_changed_paths(&changed_paths,
3420 err = print_commit(commit, qid->id, repo, path,
3421 show_changed_paths ? &changed_paths : NULL,
3422 show_patch, diff_context, refs);
3423 got_object_commit_close(commit);
3426 TAILQ_FOREACH(pe, &changed_paths, entry) {
3427 free((char *)pe->path);
3430 got_pathlist_free(&changed_paths);
3434 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3435 qid = SIMPLEQ_FIRST(&reversed_commits);
3436 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3437 got_object_qid_free(qid);
3439 TAILQ_FOREACH(pe, &changed_paths, entry) {
3440 free((char *)pe->path);
3443 got_pathlist_free(&changed_paths);
3446 got_commit_graph_close(graph);
3453 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3454 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3455 "[-R] [path]\n", getprogname());
3460 get_default_log_limit(void)
3462 const char *got_default_log_limit;
3466 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3467 if (got_default_log_limit == NULL)
3469 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3475 static const struct got_error *
3476 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3477 struct got_repository *repo)
3479 const struct got_error *err = NULL;
3480 struct got_reference *ref;
3484 err = got_ref_open(&ref, repo, commit_arg, 0);
3487 err = got_ref_resolve(id, repo, ref);
3491 err = got_object_get_type(&obj_type, repo, *id);
3494 if (obj_type == GOT_OBJ_TYPE_TAG) {
3495 struct got_tag_object *tag;
3496 err = got_object_open_as_tag(&tag, repo, *id);
3499 if (got_object_tag_get_object_type(tag) !=
3500 GOT_OBJ_TYPE_COMMIT) {
3501 got_object_tag_close(tag);
3502 return got_error(GOT_ERR_OBJ_TYPE);
3505 *id = got_object_id_dup(
3506 got_object_tag_get_object_id(tag));
3508 err = got_error_from_errno(
3509 "got_object_id_dup");
3510 got_object_tag_close(tag);
3513 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3514 return got_error(GOT_ERR_OBJ_TYPE);
3516 err = got_repo_match_object_id_prefix(id, commit_arg,
3517 GOT_OBJ_TYPE_COMMIT, repo);
3523 static const struct got_error *
3524 cmd_log(int argc, char *argv[])
3526 const struct got_error *error;
3527 struct got_repository *repo = NULL;
3528 struct got_worktree *worktree = NULL;
3529 struct got_object_id *start_id = NULL, *end_id = NULL;
3530 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3531 const char *start_commit = NULL, *end_commit = NULL;
3532 const char *search_pattern = NULL;
3533 int diff_context = -1, ch;
3534 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3535 int reverse_display_order = 0;
3537 struct got_reflist_head refs;
3539 SIMPLEQ_INIT(&refs);
3542 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3548 limit = get_default_log_limit();
3550 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3556 show_changed_paths = 1;
3559 start_commit = optarg;
3562 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3565 err(1, "-C option %s", errstr);
3568 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3570 err(1, "-l option %s", errstr);
3576 repo_path = realpath(optarg, NULL);
3577 if (repo_path == NULL)
3578 return got_error_from_errno2("realpath",
3580 got_path_strip_trailing_slashes(repo_path);
3583 reverse_display_order = 1;
3586 search_pattern = optarg;
3589 end_commit = optarg;
3600 if (diff_context == -1)
3602 else if (!show_patch)
3603 errx(1, "-C reguires -p");
3605 cwd = getcwd(NULL, 0);
3607 error = got_error_from_errno("getcwd");
3611 if (repo_path == NULL) {
3612 error = got_worktree_open(&worktree, cwd);
3613 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 error = got_error_from_errno("strdup");
3624 } else if (argc == 1) {
3626 error = got_worktree_resolve_path(&path, worktree,
3631 path = strdup(argv[0]);
3633 error = got_error_from_errno("strdup");
3640 if (repo_path == NULL) {
3641 repo_path = worktree ?
3642 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3644 if (repo_path == NULL) {
3645 error = got_error_from_errno("strdup");
3649 error = got_repo_open(&repo, repo_path, NULL);
3653 error = apply_unveil(got_repo_get_path(repo), 1,
3654 worktree ? got_worktree_get_root_path(worktree) : NULL);
3658 if (start_commit == NULL) {
3659 struct got_reference *head_ref;
3660 struct got_commit_object *commit = NULL;
3661 error = got_ref_open(&head_ref, repo,
3662 worktree ? got_worktree_get_head_ref_name(worktree)
3666 error = got_ref_resolve(&start_id, repo, head_ref);
3667 got_ref_close(head_ref);
3670 error = got_object_open_as_commit(&commit, repo,
3674 got_object_commit_close(commit);
3676 error = resolve_commit_arg(&start_id, start_commit, repo);
3680 if (end_commit != NULL) {
3681 error = resolve_commit_arg(&end_id, end_commit, repo);
3687 const char *prefix = got_worktree_get_path_prefix(worktree);
3689 if (asprintf(&p, "%s%s%s", prefix,
3690 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3691 error = got_error_from_errno("asprintf");
3694 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3697 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3702 path = in_repo_path;
3705 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3709 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3710 show_patch, search_pattern, diff_context, limit, log_branches,
3711 reverse_display_order, &refs);
3717 got_worktree_close(worktree);
3719 const struct got_error *repo_error;
3720 repo_error = got_repo_close(repo);
3724 got_ref_list_free(&refs);
3731 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3732 "[-w] [object1 object2 | path]\n", getprogname());
3736 struct print_diff_arg {
3737 struct got_repository *repo;
3738 struct got_worktree *worktree;
3743 int ignore_whitespace;
3747 * Create a file which contains the target path of a symlink so we can feed
3748 * it as content to the diff engine.
3750 static const struct got_error *
3751 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3752 const char *abspath)
3754 const struct got_error *err = NULL;
3755 char target_path[PATH_MAX];
3756 ssize_t target_len, outlen;
3761 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3762 if (target_len == -1)
3763 return got_error_from_errno2("readlinkat", abspath);
3765 target_len = readlink(abspath, target_path, PATH_MAX);
3766 if (target_len == -1)
3767 return got_error_from_errno2("readlink", abspath);
3770 *fd = got_opentempfd();
3772 return got_error_from_errno("got_opentempfd");
3774 outlen = write(*fd, target_path, target_len);
3776 err = got_error_from_errno("got_opentempfd");
3780 if (lseek(*fd, 0, SEEK_SET) == -1) {
3781 err = got_error_from_errno2("lseek", abspath);
3792 static const struct got_error *
3793 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3794 const char *path, struct got_object_id *blob_id,
3795 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3796 int dirfd, const char *de_name)
3798 struct print_diff_arg *a = arg;
3799 const struct got_error *err = NULL;
3800 struct got_blob_object *blob1 = NULL;
3803 char *abspath = NULL, *label1 = NULL;
3806 if (a->diff_staged) {
3807 if (staged_status != GOT_STATUS_MODIFY &&
3808 staged_status != GOT_STATUS_ADD &&
3809 staged_status != GOT_STATUS_DELETE)
3812 if (staged_status == GOT_STATUS_DELETE)
3814 if (status == GOT_STATUS_NONEXISTENT)
3815 return got_error_set_errno(ENOENT, path);
3816 if (status != GOT_STATUS_MODIFY &&
3817 status != GOT_STATUS_ADD &&
3818 status != GOT_STATUS_DELETE &&
3819 status != GOT_STATUS_CONFLICT)
3823 if (!a->header_shown) {
3824 printf("diff %s %s%s\n", a->id_str,
3825 got_worktree_get_root_path(a->worktree),
3826 a->diff_staged ? " (staged changes)" : "");
3827 a->header_shown = 1;
3830 if (a->diff_staged) {
3831 const char *label1 = NULL, *label2 = NULL;
3832 switch (staged_status) {
3833 case GOT_STATUS_MODIFY:
3837 case GOT_STATUS_ADD:
3840 case GOT_STATUS_DELETE:
3844 return got_error(GOT_ERR_FILE_STATUS);
3846 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3847 label1, label2, a->diff_context, a->ignore_whitespace,
3851 if (staged_status == GOT_STATUS_ADD ||
3852 staged_status == GOT_STATUS_MODIFY) {
3854 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3858 err = got_object_id_str(&id_str, staged_blob_id);
3861 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3862 err = got_error_from_errno("asprintf");
3867 } else if (status != GOT_STATUS_ADD) {
3868 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3873 if (status != GOT_STATUS_DELETE) {
3874 if (asprintf(&abspath, "%s/%s",
3875 got_worktree_get_root_path(a->worktree), path) == -1) {
3876 err = got_error_from_errno("asprintf");
3881 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3883 if (errno != ELOOP) {
3884 err = got_error_from_errno2("openat",
3888 err = get_symlink_target_file(&fd, dirfd,
3894 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3896 if (errno != ELOOP) {
3897 err = got_error_from_errno2("open",
3901 err = get_symlink_target_file(&fd, dirfd,
3907 if (fstat(fd, &sb) == -1) {
3908 err = got_error_from_errno2("fstat", abspath);
3911 f2 = fdopen(fd, "r");
3913 err = got_error_from_errno2("fdopen", abspath);
3920 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3921 a->diff_context, a->ignore_whitespace, stdout);
3924 got_object_blob_close(blob1);
3925 if (f2 && fclose(f2) == EOF && err == NULL)
3926 err = got_error_from_errno("fclose");
3927 if (fd != -1 && close(fd) == -1 && err == NULL)
3928 err = got_error_from_errno("close");
3933 static const struct got_error *
3934 cmd_diff(int argc, char *argv[])
3936 const struct got_error *error;
3937 struct got_repository *repo = NULL;
3938 struct got_worktree *worktree = NULL;
3939 char *cwd = NULL, *repo_path = NULL;
3940 struct got_object_id *id1 = NULL, *id2 = NULL;
3941 const char *id_str1 = NULL, *id_str2 = NULL;
3942 char *label1 = NULL, *label2 = NULL;
3944 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3949 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3954 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3957 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3960 err(1, "-C option %s", errstr);
3963 repo_path = realpath(optarg, NULL);
3964 if (repo_path == NULL)
3965 return got_error_from_errno2("realpath",
3967 got_path_strip_trailing_slashes(repo_path);
3973 ignore_whitespace = 1;
3984 cwd = getcwd(NULL, 0);
3986 error = got_error_from_errno("getcwd");
3992 "-r option can't be used when diffing a work tree");
3993 error = got_worktree_open(&worktree, cwd);
3995 if (error->code == GOT_ERR_NOT_WORKTREE)
3996 error = wrap_not_worktree_error(error, "diff",
4000 repo_path = strdup(got_worktree_get_repo_path(worktree));
4001 if (repo_path == NULL) {
4002 error = got_error_from_errno("strdup");
4006 error = got_worktree_resolve_path(&path, worktree,
4013 error = got_error_from_errno("strdup");
4017 } else if (argc == 2) {
4019 errx(1, "-s option can't be used when diffing "
4020 "objects in repository");
4023 if (repo_path == NULL) {
4024 error = got_worktree_open(&worktree, cwd);
4025 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4029 got_worktree_get_repo_path(worktree));
4030 if (repo_path == NULL) {
4031 error = got_error_from_errno("strdup");
4035 repo_path = strdup(cwd);
4036 if (repo_path == NULL) {
4037 error = got_error_from_errno("strdup");
4045 error = got_repo_open(&repo, repo_path, NULL);
4050 error = apply_unveil(got_repo_get_path(repo), 1,
4051 worktree ? got_worktree_get_root_path(worktree) : NULL);
4056 struct print_diff_arg arg;
4057 struct got_pathlist_head paths;
4062 error = got_object_id_str(&id_str,
4063 got_worktree_get_base_commit_id(worktree));
4067 arg.worktree = worktree;
4068 arg.diff_context = diff_context;
4069 arg.id_str = id_str;
4070 arg.header_shown = 0;
4071 arg.diff_staged = diff_staged;
4072 arg.ignore_whitespace = ignore_whitespace;
4074 error = got_pathlist_append(&paths, path, NULL);
4078 error = got_worktree_status(worktree, &paths, repo, print_diff,
4079 &arg, check_cancelled, NULL);
4081 got_pathlist_free(&paths);
4085 error = got_repo_match_object_id(&id1, &label1, id_str1,
4086 GOT_OBJ_TYPE_ANY, 1, repo);
4090 error = got_repo_match_object_id(&id2, &label2, id_str2,
4091 GOT_OBJ_TYPE_ANY, 1, repo);
4095 error = got_object_get_type(&type1, repo, id1);
4099 error = got_object_get_type(&type2, repo, id2);
4103 if (type1 != type2) {
4104 error = got_error(GOT_ERR_OBJ_TYPE);
4109 case GOT_OBJ_TYPE_BLOB:
4110 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4111 diff_context, ignore_whitespace, repo, stdout);
4113 case GOT_OBJ_TYPE_TREE:
4114 error = got_diff_objects_as_trees(id1, id2, "", "",
4115 diff_context, ignore_whitespace, repo, stdout);
4117 case GOT_OBJ_TYPE_COMMIT:
4118 printf("diff %s %s\n", label1, label2);
4119 error = got_diff_objects_as_commits(id1, id2, diff_context,
4120 ignore_whitespace, repo, stdout);
4123 error = got_error(GOT_ERR_OBJ_TYPE);
4132 got_worktree_close(worktree);
4134 const struct got_error *repo_error;
4135 repo_error = got_repo_close(repo);
4146 "usage: %s blame [-c commit] [-r repository-path] path\n",
4155 char datebuf[11]; /* YYYY-MM-DD + NUL */
4158 struct blame_cb_args {
4159 struct blame_line *lines;
4163 off_t *line_offsets;
4165 struct got_repository *repo;
4168 static const struct got_error *
4169 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4171 const struct got_error *err = NULL;
4172 struct blame_cb_args *a = arg;
4173 struct blame_line *bline;
4175 size_t linesize = 0;
4176 struct got_commit_object *commit = NULL;
4179 time_t committer_time;
4181 if (nlines != a->nlines ||
4182 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4183 return got_error(GOT_ERR_RANGE);
4185 if (sigint_received)
4186 return got_error(GOT_ERR_ITER_COMPLETED);
4189 return NULL; /* no change in this commit */
4191 /* Annotate this line. */
4192 bline = &a->lines[lineno - 1];
4193 if (bline->annotated)
4195 err = got_object_id_str(&bline->id_str, id);
4199 err = got_object_open_as_commit(&commit, a->repo, id);
4203 bline->committer = strdup(got_object_commit_get_committer(commit));
4204 if (bline->committer == NULL) {
4205 err = got_error_from_errno("strdup");
4209 committer_time = got_object_commit_get_committer_time(commit);
4210 if (localtime_r(&committer_time, &tm) == NULL)
4211 return got_error_from_errno("localtime_r");
4212 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4213 &tm) >= sizeof(bline->datebuf)) {
4214 err = got_error(GOT_ERR_NO_SPACE);
4217 bline->annotated = 1;
4219 /* Print lines annotated so far. */
4220 bline = &a->lines[a->lineno_cur - 1];
4221 if (!bline->annotated)
4224 offset = a->line_offsets[a->lineno_cur - 1];
4225 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4226 err = got_error_from_errno("fseeko");
4230 while (bline->annotated) {
4231 char *smallerthan, *at, *nl, *committer;
4234 if (getline(&line, &linesize, a->f) == -1) {
4236 err = got_error_from_errno("getline");
4240 committer = bline->committer;
4241 smallerthan = strchr(committer, '<');
4242 if (smallerthan && smallerthan[1] != '\0')
4243 committer = smallerthan + 1;
4244 at = strchr(committer, '@');
4247 len = strlen(committer);
4249 committer[8] = '\0';
4251 nl = strchr(line, '\n');
4254 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4255 bline->id_str, bline->datebuf, committer, line);
4258 bline = &a->lines[a->lineno_cur - 1];
4262 got_object_commit_close(commit);
4267 static const struct got_error *
4268 cmd_blame(int argc, char *argv[])
4270 const struct got_error *error;
4271 struct got_repository *repo = NULL;
4272 struct got_worktree *worktree = NULL;
4273 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4274 char *link_target = NULL;
4275 struct got_object_id *obj_id = NULL;
4276 struct got_object_id *commit_id = NULL;
4277 struct got_blob_object *blob = NULL;
4278 char *commit_id_str = NULL;
4279 struct blame_cb_args bca;
4280 int ch, obj_type, i;
4283 memset(&bca, 0, sizeof(bca));
4286 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4291 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4294 commit_id_str = optarg;
4297 repo_path = realpath(optarg, NULL);
4298 if (repo_path == NULL)
4299 return got_error_from_errno2("realpath",
4301 got_path_strip_trailing_slashes(repo_path);
4317 cwd = getcwd(NULL, 0);
4319 error = got_error_from_errno("getcwd");
4322 if (repo_path == NULL) {
4323 error = got_worktree_open(&worktree, cwd);
4324 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4330 strdup(got_worktree_get_repo_path(worktree));
4331 if (repo_path == NULL) {
4332 error = got_error_from_errno("strdup");
4337 repo_path = strdup(cwd);
4338 if (repo_path == NULL) {
4339 error = got_error_from_errno("strdup");
4345 error = got_repo_open(&repo, repo_path, NULL);
4349 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4354 const char *prefix = got_worktree_get_path_prefix(worktree);
4355 char *p, *worktree_subdir = cwd +
4356 strlen(got_worktree_get_root_path(worktree));
4357 if (asprintf(&p, "%s%s%s%s%s",
4358 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4359 worktree_subdir, worktree_subdir[0] ? "/" : "",
4361 error = got_error_from_errno("asprintf");
4364 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4367 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4372 if (commit_id_str == NULL) {
4373 struct got_reference *head_ref;
4374 error = got_ref_open(&head_ref, repo, worktree ?
4375 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4378 error = got_ref_resolve(&commit_id, repo, head_ref);
4379 got_ref_close(head_ref);
4383 error = got_repo_match_object_id(&commit_id, NULL,
4384 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4389 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4394 error = got_object_id_by_path(&obj_id, repo, commit_id,
4395 link_target ? link_target : in_repo_path);
4399 error = got_object_get_type(&obj_type, repo, obj_id);
4403 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4404 error = got_error_path(link_target ? link_target : in_repo_path,
4409 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4412 bca.f = got_opentemp();
4413 if (bca.f == NULL) {
4414 error = got_error_from_errno("got_opentemp");
4417 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4418 &bca.line_offsets, bca.f, blob);
4419 if (error || bca.nlines == 0)
4422 /* Don't include \n at EOF in the blame line count. */
4423 if (bca.line_offsets[bca.nlines - 1] == filesize)
4426 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4427 if (bca.lines == NULL) {
4428 error = got_error_from_errno("calloc");
4432 bca.nlines_prec = 0;
4440 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4441 repo, blame_cb, &bca, check_cancelled, NULL);
4450 got_object_blob_close(blob);
4452 got_worktree_close(worktree);
4454 const struct got_error *repo_error;
4455 repo_error = got_repo_close(repo);
4460 for (i = 0; i < bca.nlines; i++) {
4461 struct blame_line *bline = &bca.lines[i];
4462 free(bline->id_str);
4463 free(bline->committer);
4467 free(bca.line_offsets);
4468 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4469 error = got_error_from_errno("fclose");
4477 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4482 static const struct got_error *
4483 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4484 const char *root_path, struct got_repository *repo)
4486 const struct got_error *err = NULL;
4487 int is_root_path = (strcmp(path, root_path) == 0);
4488 const char *modestr = "";
4489 mode_t mode = got_tree_entry_get_mode(te);
4490 char *link_target = NULL;
4492 path += strlen(root_path);
4493 while (path[0] == '/')
4496 if (got_object_tree_entry_is_submodule(te))
4498 else if (S_ISLNK(mode)) {
4501 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4504 for (i = 0; i < strlen(link_target); i++) {
4505 if (!isprint((unsigned char)link_target[i]))
4506 link_target[i] = '?';
4511 else if (S_ISDIR(mode))
4513 else if (mode & S_IXUSR)
4516 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4517 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4518 link_target ? " -> ": "", link_target ? link_target : "");
4524 static const struct got_error *
4525 print_tree(const char *path, struct got_object_id *commit_id,
4526 int show_ids, int recurse, const char *root_path,
4527 struct got_repository *repo)
4529 const struct got_error *err = NULL;
4530 struct got_object_id *tree_id = NULL;
4531 struct got_tree_object *tree = NULL;
4534 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4538 err = got_object_open_as_tree(&tree, repo, tree_id);
4541 nentries = got_object_tree_get_nentries(tree);
4542 for (i = 0; i < nentries; i++) {
4543 struct got_tree_entry *te;
4546 if (sigint_received || sigpipe_received)
4549 te = got_object_tree_get_entry(tree, i);
4552 err = got_object_id_str(&id_str,
4553 got_tree_entry_get_id(te));
4556 if (asprintf(&id, "%s ", id_str) == -1) {
4557 err = got_error_from_errno("asprintf");
4563 err = print_entry(te, id, path, root_path, repo);
4568 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4570 if (asprintf(&child_path, "%s%s%s", path,
4571 path[0] == '/' && path[1] == '\0' ? "" : "/",
4572 got_tree_entry_get_name(te)) == -1) {
4573 err = got_error_from_errno("asprintf");
4576 err = print_tree(child_path, commit_id, show_ids, 1,
4585 got_object_tree_close(tree);
4590 static const struct got_error *
4591 cmd_tree(int argc, char *argv[])
4593 const struct got_error *error;
4594 struct got_repository *repo = NULL;
4595 struct got_worktree *worktree = NULL;
4596 const char *path, *refname = NULL;
4597 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4598 struct got_object_id *commit_id = NULL;
4599 char *commit_id_str = NULL;
4600 int show_ids = 0, recurse = 0;
4604 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4609 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4612 commit_id_str = optarg;
4615 repo_path = realpath(optarg, NULL);
4616 if (repo_path == NULL)
4617 return got_error_from_errno2("realpath",
4619 got_path_strip_trailing_slashes(repo_path);
4643 cwd = getcwd(NULL, 0);
4645 error = got_error_from_errno("getcwd");
4648 if (repo_path == NULL) {
4649 error = got_worktree_open(&worktree, cwd);
4650 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4656 strdup(got_worktree_get_repo_path(worktree));
4657 if (repo_path == NULL)
4658 error = got_error_from_errno("strdup");
4662 repo_path = strdup(cwd);
4663 if (repo_path == NULL) {
4664 error = got_error_from_errno("strdup");
4670 error = got_repo_open(&repo, repo_path, NULL);
4674 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4680 char *p, *worktree_subdir = cwd +
4681 strlen(got_worktree_get_root_path(worktree));
4682 if (asprintf(&p, "%s/%s",
4683 got_worktree_get_path_prefix(worktree),
4684 worktree_subdir) == -1) {
4685 error = got_error_from_errno("asprintf");
4688 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4695 if (in_repo_path == NULL) {
4696 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4701 if (commit_id_str == NULL) {
4702 struct got_reference *head_ref;
4704 refname = got_worktree_get_head_ref_name(worktree);
4706 refname = GOT_REF_HEAD;
4707 error = got_ref_open(&head_ref, repo, refname, 0);
4710 error = got_ref_resolve(&commit_id, repo, head_ref);
4711 got_ref_close(head_ref);
4715 error = got_repo_match_object_id(&commit_id, NULL,
4716 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4721 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4722 in_repo_path, repo);
4729 got_worktree_close(worktree);
4731 const struct got_error *repo_error;
4732 repo_error = got_repo_close(repo);
4742 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4747 static const struct got_error *
4748 print_status(void *arg, unsigned char status, unsigned char staged_status,
4749 const char *path, struct got_object_id *blob_id,
4750 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4751 int dirfd, const char *de_name)
4753 if (status == staged_status && (status == GOT_STATUS_DELETE))
4754 status = GOT_STATUS_NO_CHANGE;
4756 char *status_codes = arg;
4757 size_t ncodes = strlen(status_codes);
4759 for (i = 0; i < ncodes ; i++) {
4760 if (status == status_codes[i] ||
4761 staged_status == status_codes[i])
4767 printf("%c%c %s\n", status, staged_status, path);
4771 static const struct got_error *
4772 cmd_status(int argc, char *argv[])
4774 const struct got_error *error = NULL;
4775 struct got_repository *repo = NULL;
4776 struct got_worktree *worktree = NULL;
4777 char *cwd = NULL, *status_codes = NULL;;
4778 struct got_pathlist_head paths;
4779 struct got_pathlist_entry *pe;
4784 while ((ch = getopt(argc, argv, "s:")) != -1) {
4787 for (i = 0; i < strlen(optarg); i++) {
4788 switch (optarg[i]) {
4789 case GOT_STATUS_MODIFY:
4790 case GOT_STATUS_ADD:
4791 case GOT_STATUS_DELETE:
4792 case GOT_STATUS_CONFLICT:
4793 case GOT_STATUS_MISSING:
4794 case GOT_STATUS_OBSTRUCTED:
4795 case GOT_STATUS_UNVERSIONED:
4796 case GOT_STATUS_MODE_CHANGE:
4797 case GOT_STATUS_NONEXISTENT:
4800 errx(1, "invalid status code '%c'",
4804 status_codes = optarg;
4816 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4820 cwd = getcwd(NULL, 0);
4822 error = got_error_from_errno("getcwd");
4826 error = got_worktree_open(&worktree, cwd);
4828 if (error->code == GOT_ERR_NOT_WORKTREE)
4829 error = wrap_not_worktree_error(error, "status", cwd);
4833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4838 error = apply_unveil(got_repo_get_path(repo), 1,
4839 got_worktree_get_root_path(worktree));
4843 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4847 error = got_worktree_status(worktree, &paths, repo, print_status,
4848 status_codes, check_cancelled, NULL);
4850 TAILQ_FOREACH(pe, &paths, entry)
4851 free((char *)pe->path);
4852 got_pathlist_free(&paths);
4861 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4867 static const struct got_error *
4868 list_refs(struct got_repository *repo, const char *refname)
4870 static const struct got_error *err = NULL;
4871 struct got_reflist_head refs;
4872 struct got_reflist_entry *re;
4874 SIMPLEQ_INIT(&refs);
4875 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4879 SIMPLEQ_FOREACH(re, &refs, entry) {
4881 refstr = got_ref_to_str(re->ref);
4883 return got_error_from_errno("got_ref_to_str");
4884 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4888 got_ref_list_free(&refs);
4892 static const struct got_error *
4893 delete_ref(struct got_repository *repo, const char *refname)
4895 const struct got_error *err = NULL;
4896 struct got_reference *ref;
4898 err = got_ref_open(&ref, repo, refname, 0);
4902 err = got_ref_delete(ref, repo);
4907 static const struct got_error *
4908 add_ref(struct got_repository *repo, const char *refname, const char *target)
4910 const struct got_error *err = NULL;
4911 struct got_object_id *id;
4912 struct got_reference *ref = NULL;
4915 * Don't let the user create a reference name with a leading '-'.
4916 * While technically a valid reference name, this case is usually
4917 * an unintended typo.
4919 if (refname[0] == '-')
4920 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4922 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4925 struct got_reference *target_ref;
4927 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4929 err = got_ref_open(&target_ref, repo, target, 0);
4932 err = got_ref_resolve(&id, repo, target_ref);
4933 got_ref_close(target_ref);
4938 err = got_ref_alloc(&ref, refname, id);
4942 err = got_ref_write(ref, repo);
4950 static const struct got_error *
4951 add_symref(struct got_repository *repo, const char *refname, const char *target)
4953 const struct got_error *err = NULL;
4954 struct got_reference *ref = NULL;
4955 struct got_reference *target_ref = NULL;
4958 * Don't let the user create a reference name with a leading '-'.
4959 * While technically a valid reference name, this case is usually
4960 * an unintended typo.
4962 if (refname[0] == '-')
4963 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4965 err = got_ref_open(&target_ref, repo, target, 0);
4969 err = got_ref_alloc_symref(&ref, refname, target_ref);
4973 err = got_ref_write(ref, repo);
4976 got_ref_close(target_ref);
4982 static const struct got_error *
4983 cmd_ref(int argc, char *argv[])
4985 const struct got_error *error = NULL;
4986 struct got_repository *repo = NULL;
4987 struct got_worktree *worktree = NULL;
4988 char *cwd = NULL, *repo_path = NULL;
4989 int ch, do_list = 0, do_delete = 0;
4990 const char *obj_arg = NULL, *symref_target= NULL;
4991 char *refname = NULL;
4993 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5002 repo_path = realpath(optarg, NULL);
5003 if (repo_path == NULL)
5004 return got_error_from_errno2("realpath",
5006 got_path_strip_trailing_slashes(repo_path);
5012 symref_target = optarg;
5020 if (obj_arg && do_list)
5021 errx(1, "-c and -l options are mutually exclusive");
5022 if (obj_arg && do_delete)
5023 errx(1, "-c and -d options are mutually exclusive");
5024 if (obj_arg && symref_target)
5025 errx(1, "-c and -s options are mutually exclusive");
5026 if (symref_target && do_delete)
5027 errx(1, "-s and -d options are mutually exclusive");
5028 if (symref_target && do_list)
5029 errx(1, "-s and -l options are mutually exclusive");
5030 if (do_delete && do_list)
5031 errx(1, "-d and -l options are mutually exclusive");
5037 if (argc != 0 && argc != 1)
5040 refname = strdup(argv[0]);
5041 if (refname == NULL) {
5042 error = got_error_from_errno("strdup");
5049 refname = strdup(argv[0]);
5050 if (refname == NULL) {
5051 error = got_error_from_errno("strdup");
5057 got_path_strip_trailing_slashes(refname);
5061 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5065 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5066 "sendfd unveil", NULL) == -1)
5070 cwd = getcwd(NULL, 0);
5072 error = got_error_from_errno("getcwd");
5076 if (repo_path == NULL) {
5077 error = got_worktree_open(&worktree, cwd);
5078 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5084 strdup(got_worktree_get_repo_path(worktree));
5085 if (repo_path == NULL)
5086 error = got_error_from_errno("strdup");
5090 repo_path = strdup(cwd);
5091 if (repo_path == NULL) {
5092 error = got_error_from_errno("strdup");
5098 error = got_repo_open(&repo, repo_path, NULL);
5102 error = apply_unveil(got_repo_get_path(repo), do_list,
5103 worktree ? got_worktree_get_root_path(worktree) : NULL);
5108 error = list_refs(repo, refname);
5110 error = delete_ref(repo, refname);
5111 else if (symref_target)
5112 error = add_symref(repo, refname, symref_target);
5114 if (obj_arg == NULL)
5116 error = add_ref(repo, refname, obj_arg);
5121 got_repo_close(repo);
5123 got_worktree_close(worktree);
5133 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5134 "[name]\n", getprogname());
5138 static const struct got_error *
5139 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5140 struct got_reference *ref)
5142 const struct got_error *err = NULL;
5143 const char *refname, *marker = " ";
5146 refname = got_ref_get_name(ref);
5147 if (worktree && strcmp(refname,
5148 got_worktree_get_head_ref_name(worktree)) == 0) {
5149 struct got_object_id *id = NULL;
5151 err = got_ref_resolve(&id, repo, ref);
5154 if (got_object_id_cmp(id,
5155 got_worktree_get_base_commit_id(worktree)) == 0)
5162 if (strncmp(refname, "refs/heads/", 11) == 0)
5164 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5167 refstr = got_ref_to_str(ref);
5169 return got_error_from_errno("got_ref_to_str");
5171 printf("%s%s: %s\n", marker, refname, refstr);
5176 static const struct got_error *
5177 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5179 const char *refname;
5181 if (worktree == NULL)
5182 return got_error(GOT_ERR_NOT_WORKTREE);
5184 refname = got_worktree_get_head_ref_name(worktree);
5186 if (strncmp(refname, "refs/heads/", 11) == 0)
5188 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5191 printf("%s\n", refname);
5196 static const struct got_error *
5197 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5199 static const struct got_error *err = NULL;
5200 struct got_reflist_head refs;
5201 struct got_reflist_entry *re;
5202 struct got_reference *temp_ref = NULL;
5203 int rebase_in_progress, histedit_in_progress;
5205 SIMPLEQ_INIT(&refs);
5208 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5213 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5218 if (rebase_in_progress || histedit_in_progress) {
5219 err = got_ref_open(&temp_ref, repo,
5220 got_worktree_get_head_ref_name(worktree), 0);
5223 list_branch(repo, worktree, temp_ref);
5224 got_ref_close(temp_ref);
5228 err = got_ref_list(&refs, repo, "refs/heads",
5229 got_ref_cmp_by_name, NULL);
5233 SIMPLEQ_FOREACH(re, &refs, entry)
5234 list_branch(repo, worktree, re->ref);
5236 got_ref_list_free(&refs);
5240 static const struct got_error *
5241 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5242 const char *branch_name)
5244 const struct got_error *err = NULL;
5245 struct got_reference *ref = NULL;
5248 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5249 return got_error_from_errno("asprintf");
5251 err = got_ref_open(&ref, repo, refname, 0);
5256 strcmp(got_worktree_get_head_ref_name(worktree),
5257 got_ref_get_name(ref)) == 0) {
5258 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5259 "will not delete this work tree's current branch");
5263 err = got_ref_delete(ref, repo);
5271 static const struct got_error *
5272 add_branch(struct got_repository *repo, const char *branch_name,
5273 struct got_object_id *base_commit_id)
5275 const struct got_error *err = NULL;
5276 struct got_reference *ref = NULL;
5277 char *base_refname = NULL, *refname = NULL;
5280 * Don't let the user create a branch name with a leading '-'.
5281 * While technically a valid reference name, this case is usually
5282 * an unintended typo.
5284 if (branch_name[0] == '-')
5285 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5287 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5288 err = got_error_from_errno("asprintf");
5292 err = got_ref_open(&ref, repo, refname, 0);
5294 err = got_error(GOT_ERR_BRANCH_EXISTS);
5296 } else if (err->code != GOT_ERR_NOT_REF)
5299 err = got_ref_alloc(&ref, refname, base_commit_id);
5303 err = got_ref_write(ref, repo);
5312 static const struct got_error *
5313 cmd_branch(int argc, char *argv[])
5315 const struct got_error *error = NULL;
5316 struct got_repository *repo = NULL;
5317 struct got_worktree *worktree = NULL;
5318 char *cwd = NULL, *repo_path = NULL;
5319 int ch, do_list = 0, do_show = 0, do_update = 1;
5320 const char *delref = NULL, *commit_id_arg = NULL;
5321 struct got_reference *ref = NULL;
5322 struct got_pathlist_head paths;
5323 struct got_pathlist_entry *pe;
5324 struct got_object_id *commit_id = NULL;
5325 char *commit_id_str = NULL;
5329 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5332 commit_id_arg = optarg;
5338 repo_path = realpath(optarg, NULL);
5339 if (repo_path == NULL)
5340 return got_error_from_errno2("realpath",
5342 got_path_strip_trailing_slashes(repo_path);
5356 if (do_list && delref)
5357 errx(1, "-l and -d options are mutually exclusive");
5362 if (!do_list && !delref && argc == 0)
5365 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5366 errx(1, "-c option can only be used when creating a branch");
5368 if (do_list || delref) {
5371 } else if (!do_show && argc != 1)
5375 if (do_list || do_show) {
5376 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5380 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5381 "sendfd unveil", NULL) == -1)
5385 cwd = getcwd(NULL, 0);
5387 error = got_error_from_errno("getcwd");
5391 if (repo_path == NULL) {
5392 error = got_worktree_open(&worktree, cwd);
5393 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5399 strdup(got_worktree_get_repo_path(worktree));
5400 if (repo_path == NULL)
5401 error = got_error_from_errno("strdup");
5405 repo_path = strdup(cwd);
5406 if (repo_path == NULL) {
5407 error = got_error_from_errno("strdup");
5413 error = got_repo_open(&repo, repo_path, NULL);
5417 error = apply_unveil(got_repo_get_path(repo), do_list,
5418 worktree ? got_worktree_get_root_path(worktree) : NULL);
5423 error = show_current_branch(repo, worktree);
5425 error = list_branches(repo, worktree);
5427 error = delete_branch(repo, worktree, delref);
5429 if (commit_id_arg == NULL)
5430 commit_id_arg = worktree ?
5431 got_worktree_get_head_ref_name(worktree) :
5433 error = got_repo_match_object_id(&commit_id, NULL,
5434 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5437 error = add_branch(repo, argv[0], commit_id);
5440 if (worktree && do_update) {
5441 struct got_update_progress_arg upa;
5442 char *branch_refname = NULL;
5444 error = got_object_id_str(&commit_id_str, commit_id);
5447 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5451 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5453 error = got_error_from_errno("asprintf");
5456 error = got_ref_open(&ref, repo, branch_refname, 0);
5457 free(branch_refname);
5460 error = switch_head_ref(ref, commit_id, worktree,
5464 error = got_worktree_set_base_commit_id(worktree, repo,
5468 memset(&upa, 0, sizeof(upa));
5469 error = got_worktree_checkout_files(worktree, &paths,
5470 repo, update_progress, &upa, check_cancelled,
5474 if (upa.did_something)
5475 printf("Updated to commit %s\n", commit_id_str);
5476 print_update_progress_stats(&upa);
5483 got_repo_close(repo);
5485 got_worktree_close(worktree);
5489 free(commit_id_str);
5490 TAILQ_FOREACH(pe, &paths, entry)
5491 free((char *)pe->path);
5492 got_pathlist_free(&paths);
5501 "usage: %s tag [-c commit] [-r repository] [-l] "
5502 "[-m message] name\n", getprogname());
5507 static const struct got_error *
5508 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5510 const struct got_error *err = NULL;
5511 struct got_reflist_entry *re, *se, *new;
5512 struct got_object_id *re_id, *se_id;
5513 struct got_tag_object *re_tag, *se_tag;
5514 time_t re_time, se_time;
5516 SIMPLEQ_FOREACH(re, tags, entry) {
5517 se = SIMPLEQ_FIRST(sorted);
5519 err = got_reflist_entry_dup(&new, re);
5522 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5525 err = got_ref_resolve(&re_id, repo, re->ref);
5528 err = got_object_open_as_tag(&re_tag, repo, re_id);
5532 re_time = got_object_tag_get_tagger_time(re_tag);
5533 got_object_tag_close(re_tag);
5537 err = got_ref_resolve(&se_id, repo, re->ref);
5540 err = got_object_open_as_tag(&se_tag, repo, se_id);
5544 se_time = got_object_tag_get_tagger_time(se_tag);
5545 got_object_tag_close(se_tag);
5547 if (se_time > re_time) {
5548 err = got_reflist_entry_dup(&new, re);
5551 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5554 se = SIMPLEQ_NEXT(se, entry);
5563 static const struct got_error *
5564 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5566 static const struct got_error *err = NULL;
5567 struct got_reflist_head refs;
5568 struct got_reflist_entry *re;
5570 SIMPLEQ_INIT(&refs);
5572 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5576 SIMPLEQ_FOREACH(re, &refs, entry) {
5577 const char *refname;
5578 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5582 struct got_object_id *id;
5583 struct got_tag_object *tag;
5584 struct got_commit_object *commit = NULL;
5586 refname = got_ref_get_name(re->ref);
5587 if (strncmp(refname, "refs/tags/", 10) != 0)
5590 refstr = got_ref_to_str(re->ref);
5591 if (refstr == NULL) {
5592 err = got_error_from_errno("got_ref_to_str");
5595 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5598 err = got_ref_resolve(&id, repo, re->ref);
5601 err = got_object_open_as_tag(&tag, repo, id);
5603 if (err->code != GOT_ERR_OBJ_TYPE) {
5607 /* "lightweight" tag */
5608 err = got_object_open_as_commit(&commit, repo, id);
5613 tagger = got_object_commit_get_committer(commit);
5615 got_object_commit_get_committer_time(commit);
5616 err = got_object_id_str(&id_str, id);
5622 tagger = got_object_tag_get_tagger(tag);
5623 tagger_time = got_object_tag_get_tagger_time(tag);
5624 err = got_object_id_str(&id_str,
5625 got_object_tag_get_object_id(tag));
5629 printf("from: %s\n", tagger);
5630 datestr = get_datestr(&tagger_time, datebuf);
5632 printf("date: %s UTC\n", datestr);
5634 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5636 switch (got_object_tag_get_object_type(tag)) {
5637 case GOT_OBJ_TYPE_BLOB:
5638 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5641 case GOT_OBJ_TYPE_TREE:
5642 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5645 case GOT_OBJ_TYPE_COMMIT:
5646 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5649 case GOT_OBJ_TYPE_TAG:
5650 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5659 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5662 got_object_commit_close(commit);
5664 tagmsg0 = strdup(got_object_tag_get_message(tag));
5665 got_object_tag_close(tag);
5666 if (tagmsg0 == NULL) {
5667 err = got_error_from_errno("strdup");
5674 line = strsep(&tagmsg, "\n");
5676 printf(" %s\n", line);
5681 got_ref_list_free(&refs);
5685 static const struct got_error *
5686 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5687 const char *tag_name, const char *repo_path)
5689 const struct got_error *err = NULL;
5690 char *template = NULL, *initial_content = NULL;
5691 char *editor = NULL;
5692 int initial_content_len;
5695 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5696 err = got_error_from_errno("asprintf");
5700 initial_content_len = asprintf(&initial_content,
5701 "\n# tagging commit %s as %s\n",
5702 commit_id_str, tag_name);
5703 if (initial_content_len == -1) {
5704 err = got_error_from_errno("asprintf");
5708 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5712 if (write(fd, initial_content, initial_content_len) == -1) {
5713 err = got_error_from_errno2("write", *tagmsg_path);
5717 err = get_editor(&editor);
5720 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5722 free(initial_content);
5726 if (fd != -1 && close(fd) == -1 && err == NULL)
5727 err = got_error_from_errno2("close", *tagmsg_path);
5729 /* Editor is done; we can now apply unveil(2) */
5731 err = apply_unveil(repo_path, 0, NULL);
5740 static const struct got_error *
5741 add_tag(struct got_repository *repo, struct got_worktree *worktree,
5742 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5744 const struct got_error *err = NULL;
5745 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5746 char *label = NULL, *commit_id_str = NULL;
5747 struct got_reference *ref = NULL;
5748 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5749 char *tagmsg_path = NULL, *tag_id_str = NULL;
5750 int preserve_tagmsg = 0;
5753 * Don't let the user create a tag name with a leading '-'.
5754 * While technically a valid reference name, this case is usually
5755 * an unintended typo.
5757 if (tag_name[0] == '-')
5758 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5760 err = get_author(&tagger, repo, worktree);
5764 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5765 GOT_OBJ_TYPE_COMMIT, 1, repo);
5769 err = got_object_id_str(&commit_id_str, commit_id);
5773 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5774 refname = strdup(tag_name);
5775 if (refname == NULL) {
5776 err = got_error_from_errno("strdup");
5780 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5781 err = got_error_from_errno("asprintf");
5785 err = got_ref_open(&ref, repo, refname, 0);
5787 err = got_error(GOT_ERR_TAG_EXISTS);
5789 } else if (err->code != GOT_ERR_NOT_REF)
5792 if (tagmsg_arg == NULL) {
5793 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5794 tag_name, got_repo_get_path(repo));
5796 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5797 tagmsg_path != NULL)
5798 preserve_tagmsg = 1;
5803 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5804 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5807 preserve_tagmsg = 1;
5811 err = got_ref_alloc(&ref, refname, tag_id);
5814 preserve_tagmsg = 1;
5818 err = got_ref_write(ref, repo);
5821 preserve_tagmsg = 1;
5825 err = got_object_id_str(&tag_id_str, tag_id);
5828 preserve_tagmsg = 1;
5831 printf("Created tag %s\n", tag_id_str);
5833 if (preserve_tagmsg) {
5834 fprintf(stderr, "%s: tag message preserved in %s\n",
5835 getprogname(), tagmsg_path);
5836 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5837 err = got_error_from_errno2("unlink", tagmsg_path);
5842 free(commit_id_str);
5850 static const struct got_error *
5851 cmd_tag(int argc, char *argv[])
5853 const struct got_error *error = NULL;
5854 struct got_repository *repo = NULL;
5855 struct got_worktree *worktree = NULL;
5856 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5857 char *gitconfig_path = NULL;
5858 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5859 int ch, do_list = 0;
5861 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5864 commit_id_arg = optarg;
5870 repo_path = realpath(optarg, NULL);
5871 if (repo_path == NULL)
5872 return got_error_from_errno2("realpath",
5874 got_path_strip_trailing_slashes(repo_path);
5889 if (commit_id_arg != NULL)
5891 "-c option can only be used when creating a tag");
5893 errx(1, "-l and -m options are mutually exclusive");
5896 } else if (argc != 1)
5903 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5907 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5908 "sendfd unveil", NULL) == -1)
5912 cwd = getcwd(NULL, 0);
5914 error = got_error_from_errno("getcwd");
5918 if (repo_path == NULL) {
5919 error = got_worktree_open(&worktree, cwd);
5920 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5926 strdup(got_worktree_get_repo_path(worktree));
5927 if (repo_path == NULL)
5928 error = got_error_from_errno("strdup");
5932 repo_path = strdup(cwd);
5933 if (repo_path == NULL) {
5934 error = got_error_from_errno("strdup");
5941 error = got_repo_open(&repo, repo_path, NULL);
5944 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5947 error = list_tags(repo, worktree);
5949 error = get_gitconfig_path(&gitconfig_path);
5952 error = got_repo_open(&repo, repo_path, gitconfig_path);
5957 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5962 if (commit_id_arg == NULL) {
5963 struct got_reference *head_ref;
5964 struct got_object_id *commit_id;
5965 error = got_ref_open(&head_ref, repo,
5966 worktree ? got_worktree_get_head_ref_name(worktree)
5970 error = got_ref_resolve(&commit_id, repo, head_ref);
5971 got_ref_close(head_ref);
5974 error = got_object_id_str(&commit_id_str, commit_id);
5980 error = add_tag(repo, worktree, tag_name,
5981 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5985 got_repo_close(repo);
5987 got_worktree_close(worktree);
5990 free(gitconfig_path);
5991 free(commit_id_str);
5998 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6003 static const struct got_error *
6004 add_progress(void *arg, unsigned char status, const char *path)
6006 while (path[0] == '/')
6008 printf("%c %s\n", status, path);
6012 static const struct got_error *
6013 cmd_add(int argc, char *argv[])
6015 const struct got_error *error = NULL;
6016 struct got_repository *repo = NULL;
6017 struct got_worktree *worktree = NULL;
6019 struct got_pathlist_head paths;
6020 struct got_pathlist_entry *pe;
6021 int ch, can_recurse = 0, no_ignores = 0;
6025 while ((ch = getopt(argc, argv, "IR")) != -1) {
6043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6050 cwd = getcwd(NULL, 0);
6052 error = got_error_from_errno("getcwd");
6056 error = got_worktree_open(&worktree, cwd);
6058 if (error->code == GOT_ERR_NOT_WORKTREE)
6059 error = wrap_not_worktree_error(error, "add", cwd);
6063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6068 error = apply_unveil(got_repo_get_path(repo), 1,
6069 got_worktree_get_root_path(worktree));
6073 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6077 if (!can_recurse && no_ignores) {
6078 error = got_error_msg(GOT_ERR_BAD_PATH,
6079 "disregarding ignores requires -R option");
6087 TAILQ_FOREACH(pe, &paths, entry) {
6088 if (asprintf(&ondisk_path, "%s/%s",
6089 got_worktree_get_root_path(worktree),
6091 error = got_error_from_errno("asprintf");
6094 if (lstat(ondisk_path, &sb) == -1) {
6095 if (errno == ENOENT) {
6099 error = got_error_from_errno2("lstat",
6105 if (S_ISDIR(sb.st_mode)) {
6106 error = got_error_msg(GOT_ERR_BAD_PATH,
6107 "adding directories requires -R option");
6113 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6114 NULL, repo, no_ignores);
6117 got_repo_close(repo);
6119 got_worktree_close(worktree);
6120 TAILQ_FOREACH(pe, &paths, entry)
6121 free((char *)pe->path);
6122 got_pathlist_free(&paths);
6130 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6131 "path ...\n", getprogname());
6135 static const struct got_error *
6136 print_remove_status(void *arg, unsigned char status,
6137 unsigned char staged_status, const char *path)
6139 while (path[0] == '/')
6141 if (status == GOT_STATUS_NONEXISTENT)
6143 if (status == staged_status && (status == GOT_STATUS_DELETE))
6144 status = GOT_STATUS_NO_CHANGE;
6145 printf("%c%c %s\n", status, staged_status, path);
6149 static const struct got_error *
6150 cmd_remove(int argc, char *argv[])
6152 const struct got_error *error = NULL;
6153 struct got_worktree *worktree = NULL;
6154 struct got_repository *repo = NULL;
6155 const char *status_codes = NULL;
6157 struct got_pathlist_head paths;
6158 struct got_pathlist_entry *pe;
6159 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6163 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6166 delete_local_mods = 1;
6175 for (i = 0; i < strlen(optarg); i++) {
6176 switch (optarg[i]) {
6177 case GOT_STATUS_MODIFY:
6178 delete_local_mods = 1;
6180 case GOT_STATUS_MISSING:
6183 errx(1, "invalid status code '%c'",
6187 status_codes = optarg;
6199 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6206 cwd = getcwd(NULL, 0);
6208 error = got_error_from_errno("getcwd");
6211 error = got_worktree_open(&worktree, cwd);
6213 if (error->code == GOT_ERR_NOT_WORKTREE)
6214 error = wrap_not_worktree_error(error, "remove", cwd);
6218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6223 error = apply_unveil(got_repo_get_path(repo), 1,
6224 got_worktree_get_root_path(worktree));
6228 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6235 TAILQ_FOREACH(pe, &paths, entry) {
6236 if (asprintf(&ondisk_path, "%s/%s",
6237 got_worktree_get_root_path(worktree),
6239 error = got_error_from_errno("asprintf");
6242 if (lstat(ondisk_path, &sb) == -1) {
6243 if (errno == ENOENT) {
6247 error = got_error_from_errno2("lstat",
6253 if (S_ISDIR(sb.st_mode)) {
6254 error = got_error_msg(GOT_ERR_BAD_PATH,
6255 "removing directories requires -R option");
6261 error = got_worktree_schedule_delete(worktree, &paths,
6262 delete_local_mods, status_codes, print_remove_status, NULL,
6263 repo, keep_on_disk);
6266 got_repo_close(repo);
6268 got_worktree_close(worktree);
6269 TAILQ_FOREACH(pe, &paths, entry)
6270 free((char *)pe->path);
6271 got_pathlist_free(&paths);
6279 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6280 "path ...\n", getprogname());
6284 static const struct got_error *
6285 revert_progress(void *arg, unsigned char status, const char *path)
6287 if (status == GOT_STATUS_UNVERSIONED)
6290 while (path[0] == '/')
6292 printf("%c %s\n", status, path);
6296 struct choose_patch_arg {
6297 FILE *patch_script_file;
6301 static const struct got_error *
6302 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6303 int nchanges, const char *action)
6306 size_t linesize = 0;
6310 case GOT_STATUS_ADD:
6311 printf("A %s\n%s this addition? [y/n] ", path, action);
6313 case GOT_STATUS_DELETE:
6314 printf("D %s\n%s this deletion? [y/n] ", path, action);
6316 case GOT_STATUS_MODIFY:
6317 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6318 return got_error_from_errno("fseek");
6319 printf(GOT_COMMIT_SEP_STR);
6320 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6322 if (ferror(patch_file))
6323 return got_error_from_errno("getline");
6324 printf(GOT_COMMIT_SEP_STR);
6325 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6326 path, n, nchanges, action);
6329 return got_error_path(path, GOT_ERR_FILE_STATUS);
6335 static const struct got_error *
6336 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6337 FILE *patch_file, int n, int nchanges)
6339 const struct got_error *err = NULL;
6341 size_t linesize = 0;
6344 struct choose_patch_arg *a = arg;
6346 *choice = GOT_PATCH_CHOICE_NONE;
6348 if (a->patch_script_file) {
6350 err = show_change(status, path, patch_file, n, nchanges,
6354 linelen = getline(&line, &linesize, a->patch_script_file);
6355 if (linelen == -1) {
6356 if (ferror(a->patch_script_file))
6357 return got_error_from_errno("getline");
6360 nl = strchr(line, '\n');
6363 if (strcmp(line, "y") == 0) {
6364 *choice = GOT_PATCH_CHOICE_YES;
6366 } else if (strcmp(line, "n") == 0) {
6367 *choice = GOT_PATCH_CHOICE_NO;
6369 } else if (strcmp(line, "q") == 0 &&
6370 status == GOT_STATUS_MODIFY) {
6371 *choice = GOT_PATCH_CHOICE_QUIT;
6374 printf("invalid response '%s'\n", line);
6379 while (resp != 'y' && resp != 'n' && resp != 'q') {
6380 err = show_change(status, path, patch_file, n, nchanges,
6387 if (status == GOT_STATUS_MODIFY) {
6388 if (resp != 'y' && resp != 'n' && resp != 'q') {
6389 printf("invalid response '%c'\n", resp);
6392 } else if (resp != 'y' && resp != 'n') {
6393 printf("invalid response '%c'\n", resp);
6399 *choice = GOT_PATCH_CHOICE_YES;
6400 else if (resp == 'n')
6401 *choice = GOT_PATCH_CHOICE_NO;
6402 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6403 *choice = GOT_PATCH_CHOICE_QUIT;
6409 static const struct got_error *
6410 cmd_revert(int argc, char *argv[])
6412 const struct got_error *error = NULL;
6413 struct got_worktree *worktree = NULL;
6414 struct got_repository *repo = NULL;
6415 char *cwd = NULL, *path = NULL;
6416 struct got_pathlist_head paths;
6417 struct got_pathlist_entry *pe;
6418 int ch, can_recurse = 0, pflag = 0;
6419 FILE *patch_script_file = NULL;
6420 const char *patch_script_path = NULL;
6421 struct choose_patch_arg cpa;
6425 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6431 patch_script_path = optarg;
6446 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6447 "unveil", NULL) == -1)
6452 if (patch_script_path && !pflag)
6453 errx(1, "-F option can only be used together with -p option");
6455 cwd = getcwd(NULL, 0);
6457 error = got_error_from_errno("getcwd");
6460 error = got_worktree_open(&worktree, cwd);
6462 if (error->code == GOT_ERR_NOT_WORKTREE)
6463 error = wrap_not_worktree_error(error, "revert", cwd);
6467 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6472 if (patch_script_path) {
6473 patch_script_file = fopen(patch_script_path, "r");
6474 if (patch_script_file == NULL) {
6475 error = got_error_from_errno2("fopen",
6480 error = apply_unveil(got_repo_get_path(repo), 1,
6481 got_worktree_get_root_path(worktree));
6485 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6492 TAILQ_FOREACH(pe, &paths, entry) {
6493 if (asprintf(&ondisk_path, "%s/%s",
6494 got_worktree_get_root_path(worktree),
6496 error = got_error_from_errno("asprintf");
6499 if (lstat(ondisk_path, &sb) == -1) {
6500 if (errno == ENOENT) {
6504 error = got_error_from_errno2("lstat",
6510 if (S_ISDIR(sb.st_mode)) {
6511 error = got_error_msg(GOT_ERR_BAD_PATH,
6512 "reverting directories requires -R option");
6518 cpa.patch_script_file = patch_script_file;
6519 cpa.action = "revert";
6520 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6521 pflag ? choose_patch : NULL, &cpa, repo);
6523 if (patch_script_file && fclose(patch_script_file) == EOF &&
6525 error = got_error_from_errno2("fclose", patch_script_path);
6527 got_repo_close(repo);
6529 got_worktree_close(worktree);
6538 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6543 struct collect_commit_logmsg_arg {
6544 const char *cmdline_log;
6546 const char *worktree_path;
6547 const char *branch_name;
6548 const char *repo_path;
6553 static const struct got_error *
6554 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6557 char *initial_content = NULL;
6558 struct got_pathlist_entry *pe;
6559 const struct got_error *err = NULL;
6560 char *template = NULL;
6561 struct collect_commit_logmsg_arg *a = arg;
6562 int initial_content_len;
6566 /* if a message was specified on the command line, just use it */
6567 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6568 len = strlen(a->cmdline_log) + 1;
6569 *logmsg = malloc(len + 1);
6570 if (*logmsg == NULL)
6571 return got_error_from_errno("malloc");
6572 strlcpy(*logmsg, a->cmdline_log, len);
6576 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6577 return got_error_from_errno("asprintf");
6579 initial_content_len = asprintf(&initial_content,
6580 "\n# changes to be committed on branch %s:\n",
6582 if (initial_content_len == -1)
6583 return got_error_from_errno("asprintf");
6585 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6589 if (write(fd, initial_content, initial_content_len) == -1) {
6590 err = got_error_from_errno2("write", a->logmsg_path);
6594 TAILQ_FOREACH(pe, commitable_paths, entry) {
6595 struct got_commitable *ct = pe->data;
6596 dprintf(fd, "# %c %s\n",
6597 got_commitable_get_status(ct),
6598 got_commitable_get_path(ct));
6601 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6603 free(initial_content);
6606 if (fd != -1 && close(fd) == -1 && err == NULL)
6607 err = got_error_from_errno2("close", a->logmsg_path);
6609 /* Editor is done; we can now apply unveil(2) */
6611 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6620 static const struct got_error *
6621 cmd_commit(int argc, char *argv[])
6623 const struct got_error *error = NULL;
6624 struct got_worktree *worktree = NULL;
6625 struct got_repository *repo = NULL;
6626 char *cwd = NULL, *id_str = NULL;
6627 struct got_object_id *id = NULL;
6628 const char *logmsg = NULL;
6629 struct collect_commit_logmsg_arg cl_arg;
6630 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6631 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6632 int allow_bad_symlinks = 0;
6633 struct got_pathlist_head paths;
6636 cl_arg.logmsg_path = NULL;
6638 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6644 allow_bad_symlinks = 1;
6656 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6657 "unveil", NULL) == -1)
6660 cwd = getcwd(NULL, 0);
6662 error = got_error_from_errno("getcwd");
6665 error = got_worktree_open(&worktree, cwd);
6667 if (error->code == GOT_ERR_NOT_WORKTREE)
6668 error = wrap_not_worktree_error(error, "commit", cwd);
6672 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6675 if (rebase_in_progress) {
6676 error = got_error(GOT_ERR_REBASING);
6680 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6685 error = get_gitconfig_path(&gitconfig_path);
6688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6693 error = get_author(&author, repo, worktree);
6698 * unveil(2) traverses exec(2); if an editor is used we have
6699 * to apply unveil after the log message has been written.
6701 if (logmsg == NULL || strlen(logmsg) == 0)
6702 error = get_editor(&editor);
6704 error = apply_unveil(got_repo_get_path(repo), 0,
6705 got_worktree_get_root_path(worktree));
6709 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6713 cl_arg.editor = editor;
6714 cl_arg.cmdline_log = logmsg;
6715 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6716 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6717 if (!histedit_in_progress) {
6718 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6719 error = got_error(GOT_ERR_COMMIT_BRANCH);
6722 cl_arg.branch_name += 11;
6724 cl_arg.repo_path = got_repo_get_path(repo);
6725 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6726 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6727 print_status, NULL, repo);
6729 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6730 cl_arg.logmsg_path != NULL)
6731 preserve_logmsg = 1;
6735 error = got_object_id_str(&id_str, id);
6738 printf("Created commit %s\n", id_str);
6740 if (preserve_logmsg) {
6741 fprintf(stderr, "%s: log message preserved in %s\n",
6742 getprogname(), cl_arg.logmsg_path);
6743 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6745 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6746 free(cl_arg.logmsg_path);
6748 got_repo_close(repo);
6750 got_worktree_close(worktree);
6753 free(gitconfig_path);
6760 usage_cherrypick(void)
6762 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6766 static const struct got_error *
6767 cmd_cherrypick(int argc, char *argv[])
6769 const struct got_error *error = NULL;
6770 struct got_worktree *worktree = NULL;
6771 struct got_repository *repo = NULL;
6772 char *cwd = NULL, *commit_id_str = NULL;
6773 struct got_object_id *commit_id = NULL;
6774 struct got_commit_object *commit = NULL;
6775 struct got_object_qid *pid;
6776 struct got_reference *head_ref = NULL;
6778 struct got_update_progress_arg upa;
6780 while ((ch = getopt(argc, argv, "")) != -1) {
6792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6793 "unveil", NULL) == -1)
6799 cwd = getcwd(NULL, 0);
6801 error = got_error_from_errno("getcwd");
6804 error = got_worktree_open(&worktree, cwd);
6806 if (error->code == GOT_ERR_NOT_WORKTREE)
6807 error = wrap_not_worktree_error(error, "cherrypick",
6812 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6817 error = apply_unveil(got_repo_get_path(repo), 0,
6818 got_worktree_get_root_path(worktree));
6822 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6823 GOT_OBJ_TYPE_COMMIT, repo);
6824 if (error != NULL) {
6825 struct got_reference *ref;
6826 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6828 error = got_ref_open(&ref, repo, argv[0], 0);
6831 error = got_ref_resolve(&commit_id, repo, ref);
6836 error = got_object_id_str(&commit_id_str, commit_id);
6840 error = got_ref_open(&head_ref, repo,
6841 got_worktree_get_head_ref_name(worktree), 0);
6845 error = check_same_branch(commit_id, head_ref, NULL, repo);
6847 if (error->code != GOT_ERR_ANCESTRY)
6851 error = got_error(GOT_ERR_SAME_BRANCH);
6855 error = got_object_open_as_commit(&commit, repo, commit_id);
6858 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6859 memset(&upa, 0, sizeof(upa));
6860 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6861 commit_id, repo, update_progress, &upa, check_cancelled,
6866 if (upa.did_something)
6867 printf("Merged commit %s\n", commit_id_str);
6868 print_update_progress_stats(&upa);
6871 got_object_commit_close(commit);
6872 free(commit_id_str);
6874 got_ref_close(head_ref);
6876 got_worktree_close(worktree);
6878 got_repo_close(repo);
6885 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6889 static const struct got_error *
6890 cmd_backout(int argc, char *argv[])
6892 const struct got_error *error = NULL;
6893 struct got_worktree *worktree = NULL;
6894 struct got_repository *repo = NULL;
6895 char *cwd = NULL, *commit_id_str = NULL;
6896 struct got_object_id *commit_id = NULL;
6897 struct got_commit_object *commit = NULL;
6898 struct got_object_qid *pid;
6899 struct got_reference *head_ref = NULL;
6901 struct got_update_progress_arg upa;
6903 while ((ch = getopt(argc, argv, "")) != -1) {
6915 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6916 "unveil", NULL) == -1)
6922 cwd = getcwd(NULL, 0);
6924 error = got_error_from_errno("getcwd");
6927 error = got_worktree_open(&worktree, cwd);
6929 if (error->code == GOT_ERR_NOT_WORKTREE)
6930 error = wrap_not_worktree_error(error, "backout", cwd);
6934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6939 error = apply_unveil(got_repo_get_path(repo), 0,
6940 got_worktree_get_root_path(worktree));
6944 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6945 GOT_OBJ_TYPE_COMMIT, repo);
6946 if (error != NULL) {
6947 struct got_reference *ref;
6948 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6950 error = got_ref_open(&ref, repo, argv[0], 0);
6953 error = got_ref_resolve(&commit_id, repo, ref);
6958 error = got_object_id_str(&commit_id_str, commit_id);
6962 error = got_ref_open(&head_ref, repo,
6963 got_worktree_get_head_ref_name(worktree), 0);
6967 error = check_same_branch(commit_id, head_ref, NULL, repo);
6971 error = got_object_open_as_commit(&commit, repo, commit_id);
6974 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6976 error = got_error(GOT_ERR_ROOT_COMMIT);
6980 memset(&upa, 0, sizeof(upa));
6981 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6982 update_progress, &upa, check_cancelled, NULL);
6986 if (upa.did_something)
6987 printf("Backed out commit %s\n", commit_id_str);
6988 print_update_progress_stats(&upa);
6991 got_object_commit_close(commit);
6992 free(commit_id_str);
6994 got_ref_close(head_ref);
6996 got_worktree_close(worktree);
6998 got_repo_close(repo);
7005 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7011 trim_logmsg(char *logmsg, int limit)
7016 len = strlen(logmsg);
7020 nl = strchr(logmsg, '\n');
7025 static const struct got_error *
7026 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7028 const struct got_error *err;
7029 char *logmsg0 = NULL;
7032 err = got_object_commit_get_logmsg(&logmsg0, commit);
7037 while (isspace((unsigned char)s[0]))
7040 *logmsg = strdup(s);
7041 if (*logmsg == NULL) {
7042 err = got_error_from_errno("strdup");
7046 trim_logmsg(*logmsg, limit);
7052 static const struct got_error *
7053 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7055 const struct got_error *err;
7056 struct got_commit_object *commit = NULL;
7057 char *id_str = NULL, *logmsg = NULL;
7059 err = got_object_open_as_commit(&commit, repo, id);
7063 err = got_object_id_str(&id_str, id);
7069 err = get_short_logmsg(&logmsg, 42, commit);
7073 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7076 got_object_commit_close(commit);
7081 static const struct got_error *
7082 show_rebase_progress(struct got_commit_object *commit,
7083 struct got_object_id *old_id, struct got_object_id *new_id)
7085 const struct got_error *err;
7086 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7088 err = got_object_id_str(&old_id_str, old_id);
7093 err = got_object_id_str(&new_id_str, new_id);
7098 old_id_str[12] = '\0';
7100 new_id_str[12] = '\0';
7102 err = get_short_logmsg(&logmsg, 42, commit);
7106 printf("%s -> %s: %s\n", old_id_str,
7107 new_id_str ? new_id_str : "no-op change", logmsg);
7115 static const struct got_error *
7116 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7117 struct got_reference *branch, struct got_reference *new_base_branch,
7118 struct got_reference *tmp_branch, struct got_repository *repo)
7120 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7121 return got_worktree_rebase_complete(worktree, fileindex,
7122 new_base_branch, tmp_branch, branch, repo);
7125 static const struct got_error *
7126 rebase_commit(struct got_pathlist_head *merged_paths,
7127 struct got_worktree *worktree, struct got_fileindex *fileindex,
7128 struct got_reference *tmp_branch,
7129 struct got_object_id *commit_id, struct got_repository *repo)
7131 const struct got_error *error;
7132 struct got_commit_object *commit;
7133 struct got_object_id *new_commit_id;
7135 error = got_object_open_as_commit(&commit, repo, commit_id);
7139 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7140 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7142 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7144 error = show_rebase_progress(commit, commit_id, NULL);
7146 error = show_rebase_progress(commit, commit_id, new_commit_id);
7147 free(new_commit_id);
7150 got_object_commit_close(commit);
7154 struct check_path_prefix_arg {
7155 const char *path_prefix;
7160 static const struct got_error *
7161 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7162 struct got_blob_object *blob2, struct got_object_id *id1,
7163 struct got_object_id *id2, const char *path1, const char *path2,
7164 mode_t mode1, mode_t mode2, struct got_repository *repo)
7166 struct check_path_prefix_arg *a = arg;
7168 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7169 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7170 return got_error(a->errcode);
7175 static const struct got_error *
7176 check_path_prefix(struct got_object_id *parent_id,
7177 struct got_object_id *commit_id, const char *path_prefix,
7178 int errcode, struct got_repository *repo)
7180 const struct got_error *err;
7181 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7182 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7183 struct check_path_prefix_arg cpp_arg;
7185 if (got_path_is_root_dir(path_prefix))
7188 err = got_object_open_as_commit(&commit, repo, commit_id);
7192 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7196 err = got_object_open_as_tree(&tree1, repo,
7197 got_object_commit_get_tree_id(parent_commit));
7201 err = got_object_open_as_tree(&tree2, repo,
7202 got_object_commit_get_tree_id(commit));
7206 cpp_arg.path_prefix = path_prefix;
7207 while (cpp_arg.path_prefix[0] == '/')
7208 cpp_arg.path_prefix++;
7209 cpp_arg.len = strlen(cpp_arg.path_prefix);
7210 cpp_arg.errcode = errcode;
7211 err = got_diff_tree(tree1, tree2, "", "", repo,
7212 check_path_prefix_in_diff, &cpp_arg, 0);
7215 got_object_tree_close(tree1);
7217 got_object_tree_close(tree2);
7219 got_object_commit_close(commit);
7221 got_object_commit_close(parent_commit);
7225 static const struct got_error *
7226 collect_commits(struct got_object_id_queue *commits,
7227 struct got_object_id *initial_commit_id,
7228 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7229 const char *path_prefix, int path_prefix_errcode,
7230 struct got_repository *repo)
7232 const struct got_error *err = NULL;
7233 struct got_commit_graph *graph = NULL;
7234 struct got_object_id *parent_id = NULL;
7235 struct got_object_qid *qid;
7236 struct got_object_id *commit_id = initial_commit_id;
7238 err = got_commit_graph_open(&graph, "/", 1);
7242 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7243 check_cancelled, NULL);
7246 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7247 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7248 check_cancelled, NULL);
7250 if (err->code == GOT_ERR_ITER_COMPLETED) {
7251 err = got_error_msg(GOT_ERR_ANCESTRY,
7252 "ran out of commits to rebase before "
7253 "youngest common ancestor commit has "
7258 err = check_path_prefix(parent_id, commit_id,
7259 path_prefix, path_prefix_errcode, repo);
7263 err = got_object_qid_alloc(&qid, commit_id);
7266 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7267 commit_id = parent_id;
7271 got_commit_graph_close(graph);
7275 static const struct got_error *
7276 cmd_rebase(int argc, char *argv[])
7278 const struct got_error *error = NULL;
7279 struct got_worktree *worktree = NULL;
7280 struct got_repository *repo = NULL;
7281 struct got_fileindex *fileindex = NULL;
7283 struct got_reference *branch = NULL;
7284 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7285 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7286 struct got_object_id *resume_commit_id = NULL;
7287 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7288 struct got_commit_object *commit = NULL;
7289 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7290 int histedit_in_progress = 0;
7291 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7292 struct got_object_id_queue commits;
7293 struct got_pathlist_head merged_paths;
7294 const struct got_object_id_queue *parent_ids;
7295 struct got_object_qid *qid, *pid;
7297 SIMPLEQ_INIT(&commits);
7298 TAILQ_INIT(&merged_paths);
7300 while ((ch = getopt(argc, argv, "ac")) != -1) {
7306 continue_rebase = 1;
7318 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7319 "unveil", NULL) == -1)
7322 if (abort_rebase && continue_rebase)
7324 else if (abort_rebase || continue_rebase) {
7327 } else if (argc != 1)
7330 cwd = getcwd(NULL, 0);
7332 error = got_error_from_errno("getcwd");
7335 error = got_worktree_open(&worktree, cwd);
7337 if (error->code == GOT_ERR_NOT_WORKTREE)
7338 error = wrap_not_worktree_error(error, "rebase", cwd);
7342 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7347 error = apply_unveil(got_repo_get_path(repo), 0,
7348 got_worktree_get_root_path(worktree));
7352 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7356 if (histedit_in_progress) {
7357 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7361 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7366 struct got_update_progress_arg upa;
7367 if (!rebase_in_progress) {
7368 error = got_error(GOT_ERR_NOT_REBASING);
7371 error = got_worktree_rebase_continue(&resume_commit_id,
7372 &new_base_branch, &tmp_branch, &branch, &fileindex,
7376 printf("Switching work tree to %s\n",
7377 got_ref_get_symref_target(new_base_branch));
7378 memset(&upa, 0, sizeof(upa));
7379 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7380 new_base_branch, update_progress, &upa);
7383 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7384 print_update_progress_stats(&upa);
7385 goto done; /* nothing else to do */
7388 if (continue_rebase) {
7389 if (!rebase_in_progress) {
7390 error = got_error(GOT_ERR_NOT_REBASING);
7393 error = got_worktree_rebase_continue(&resume_commit_id,
7394 &new_base_branch, &tmp_branch, &branch, &fileindex,
7399 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7400 resume_commit_id, repo);
7404 yca_id = got_object_id_dup(resume_commit_id);
7405 if (yca_id == NULL) {
7406 error = got_error_from_errno("got_object_id_dup");
7410 error = got_ref_open(&branch, repo, argv[0], 0);
7415 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7419 if (!continue_rebase) {
7420 struct got_object_id *base_commit_id;
7422 base_commit_id = got_worktree_get_base_commit_id(worktree);
7423 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7424 base_commit_id, branch_head_commit_id, repo,
7425 check_cancelled, NULL);
7428 if (yca_id == NULL) {
7429 error = got_error_msg(GOT_ERR_ANCESTRY,
7430 "specified branch shares no common ancestry "
7431 "with work tree's branch");
7435 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7437 if (error->code != GOT_ERR_ANCESTRY)
7441 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7442 "specified branch resolves to a commit which "
7443 "is already contained in work tree's branch");
7446 error = got_worktree_rebase_prepare(&new_base_branch,
7447 &tmp_branch, &fileindex, worktree, branch, repo);
7452 commit_id = branch_head_commit_id;
7453 error = got_object_open_as_commit(&commit, repo, commit_id);
7457 parent_ids = got_object_commit_get_parent_ids(commit);
7458 pid = SIMPLEQ_FIRST(parent_ids);
7460 if (!continue_rebase) {
7461 struct got_update_progress_arg upa;
7462 memset(&upa, 0, sizeof(upa));
7463 error = got_worktree_rebase_abort(worktree, fileindex,
7464 repo, new_base_branch, update_progress, &upa);
7467 printf("Rebase of %s aborted\n",
7468 got_ref_get_name(branch));
7469 print_update_progress_stats(&upa);
7472 error = got_error(GOT_ERR_EMPTY_REBASE);
7475 error = collect_commits(&commits, commit_id, pid->id,
7476 yca_id, got_worktree_get_path_prefix(worktree),
7477 GOT_ERR_REBASE_PATH, repo);
7478 got_object_commit_close(commit);
7483 if (SIMPLEQ_EMPTY(&commits)) {
7484 if (continue_rebase) {
7485 error = rebase_complete(worktree, fileindex,
7486 branch, new_base_branch, tmp_branch, repo);
7489 /* Fast-forward the reference of the branch. */
7490 struct got_object_id *new_head_commit_id;
7492 error = got_ref_resolve(&new_head_commit_id, repo,
7496 error = got_object_id_str(&id_str, new_head_commit_id);
7497 printf("Forwarding %s to commit %s\n",
7498 got_ref_get_name(branch), id_str);
7500 error = got_ref_change_ref(branch,
7501 new_head_commit_id);
7508 SIMPLEQ_FOREACH(qid, &commits, entry) {
7509 struct got_update_progress_arg upa;
7511 commit_id = qid->id;
7512 parent_id = pid ? pid->id : yca_id;
7515 memset(&upa, 0, sizeof(upa));
7516 error = got_worktree_rebase_merge_files(&merged_paths,
7517 worktree, fileindex, parent_id, commit_id, repo,
7518 update_progress, &upa, check_cancelled, NULL);
7522 print_update_progress_stats(&upa);
7523 if (upa.conflicts > 0)
7524 rebase_status = GOT_STATUS_CONFLICT;
7526 if (rebase_status == GOT_STATUS_CONFLICT) {
7527 error = show_rebase_merge_conflict(qid->id, repo);
7530 got_worktree_rebase_pathlist_free(&merged_paths);
7534 error = rebase_commit(&merged_paths, worktree, fileindex,
7535 tmp_branch, commit_id, repo);
7536 got_worktree_rebase_pathlist_free(&merged_paths);
7541 if (rebase_status == GOT_STATUS_CONFLICT) {
7542 error = got_worktree_rebase_postpone(worktree, fileindex);
7545 error = got_error_msg(GOT_ERR_CONFLICTS,
7546 "conflicts must be resolved before rebasing can continue");
7548 error = rebase_complete(worktree, fileindex, branch,
7549 new_base_branch, tmp_branch, repo);
7551 got_object_id_queue_free(&commits);
7552 free(branch_head_commit_id);
7553 free(resume_commit_id);
7556 got_object_commit_close(commit);
7558 got_ref_close(branch);
7559 if (new_base_branch)
7560 got_ref_close(new_base_branch);
7562 got_ref_close(tmp_branch);
7564 got_worktree_close(worktree);
7566 got_repo_close(repo);
7571 usage_histedit(void)
7573 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7578 #define GOT_HISTEDIT_PICK 'p'
7579 #define GOT_HISTEDIT_EDIT 'e'
7580 #define GOT_HISTEDIT_FOLD 'f'
7581 #define GOT_HISTEDIT_DROP 'd'
7582 #define GOT_HISTEDIT_MESG 'm'
7584 static struct got_histedit_cmd {
7588 } got_histedit_cmds[] = {
7589 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7590 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7591 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7593 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7594 { GOT_HISTEDIT_MESG, "mesg",
7595 "single-line log message for commit above (open editor if empty)" },
7598 struct got_histedit_list_entry {
7599 TAILQ_ENTRY(got_histedit_list_entry) entry;
7600 struct got_object_id *commit_id;
7601 const struct got_histedit_cmd *cmd;
7604 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7606 static const struct got_error *
7607 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7608 FILE *f, struct got_repository *repo)
7610 const struct got_error *err = NULL;
7611 char *logmsg = NULL, *id_str = NULL;
7612 struct got_commit_object *commit = NULL;
7615 err = got_object_open_as_commit(&commit, repo, commit_id);
7619 err = get_short_logmsg(&logmsg, 34, commit);
7623 err = got_object_id_str(&id_str, commit_id);
7627 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7629 err = got_ferror(f, GOT_ERR_IO);
7632 got_object_commit_close(commit);
7638 static const struct got_error *
7639 histedit_write_commit_list(struct got_object_id_queue *commits,
7640 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7642 const struct got_error *err = NULL;
7643 struct got_object_qid *qid;
7645 if (SIMPLEQ_EMPTY(commits))
7646 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7648 SIMPLEQ_FOREACH(qid, commits, entry) {
7649 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7653 if (edit_logmsg_only) {
7654 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7656 err = got_ferror(f, GOT_ERR_IO);
7665 static const struct got_error *
7666 write_cmd_list(FILE *f, const char *branch_name,
7667 struct got_object_id_queue *commits)
7669 const struct got_error *err = NULL;
7672 struct got_object_qid *qid;
7674 qid = SIMPLEQ_FIRST(commits);
7675 err = got_object_id_str(&id_str, qid->id);
7680 "# Editing the history of branch '%s' starting at\n"
7682 "# Commits will be processed in order from top to "
7683 "bottom of this file.\n", branch_name, id_str);
7685 err = got_ferror(f, GOT_ERR_IO);
7689 n = fprintf(f, "# Available histedit commands:\n");
7691 err = got_ferror(f, GOT_ERR_IO);
7695 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7696 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7697 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7700 err = got_ferror(f, GOT_ERR_IO);
7709 static const struct got_error *
7710 histedit_syntax_error(int lineno)
7712 static char msg[42];
7715 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7717 if (ret == -1 || ret >= sizeof(msg))
7718 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7720 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7723 static const struct got_error *
7724 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7725 char *logmsg, struct got_repository *repo)
7727 const struct got_error *err;
7728 struct got_commit_object *folded_commit = NULL;
7729 char *id_str, *folded_logmsg = NULL;
7731 err = got_object_id_str(&id_str, hle->commit_id);
7735 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7739 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7742 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7743 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7744 folded_logmsg) == -1) {
7745 err = got_error_from_errno("asprintf");
7749 got_object_commit_close(folded_commit);
7751 free(folded_logmsg);
7755 static struct got_histedit_list_entry *
7756 get_folded_commits(struct got_histedit_list_entry *hle)
7758 struct got_histedit_list_entry *prev, *folded = NULL;
7760 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7761 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7762 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7763 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7765 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7771 static const struct got_error *
7772 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7773 struct got_repository *repo)
7775 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7776 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7777 const struct got_error *err = NULL;
7778 struct got_commit_object *commit = NULL;
7781 struct got_histedit_list_entry *folded = NULL;
7783 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7787 folded = get_folded_commits(hle);
7789 while (folded != hle) {
7790 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7791 folded = TAILQ_NEXT(folded, entry);
7794 err = append_folded_commit_msg(&new_msg, folded,
7800 folded = TAILQ_NEXT(folded, entry);
7804 err = got_object_id_str(&id_str, hle->commit_id);
7807 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7810 logmsg_len = asprintf(&new_msg,
7811 "%s\n# original log message of commit %s: %s",
7812 logmsg ? logmsg : "", id_str, orig_logmsg);
7813 if (logmsg_len == -1) {
7814 err = got_error_from_errno("asprintf");
7820 err = got_object_id_str(&id_str, hle->commit_id);
7824 err = got_opentemp_named_fd(&logmsg_path, &fd,
7825 GOT_TMPDIR_STR "/got-logmsg");
7829 write(fd, logmsg, logmsg_len);
7832 err = get_editor(&editor);
7836 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7838 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7840 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7843 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7844 err = got_error_from_errno2("unlink", logmsg_path);
7850 got_object_commit_close(commit);
7854 static const struct got_error *
7855 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7856 FILE *f, struct got_repository *repo)
7858 const struct got_error *err = NULL;
7859 char *line = NULL, *p, *end;
7863 const struct got_histedit_cmd *cmd;
7864 struct got_object_id *commit_id = NULL;
7865 struct got_histedit_list_entry *hle = NULL;
7868 len = getline(&line, &size, f);
7870 const struct got_error *getline_err;
7873 getline_err = got_error_from_errno("getline");
7874 err = got_ferror(f, getline_err->code);
7879 while (isspace((unsigned char)p[0]))
7881 if (p[0] == '#' || p[0] == '\0') {
7887 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7888 cmd = &got_histedit_cmds[i];
7889 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7890 isspace((unsigned char)p[strlen(cmd->name)])) {
7891 p += strlen(cmd->name);
7894 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7899 if (i == nitems(got_histedit_cmds)) {
7900 err = histedit_syntax_error(lineno);
7903 while (isspace((unsigned char)p[0]))
7905 if (cmd->code == GOT_HISTEDIT_MESG) {
7906 if (hle == NULL || hle->logmsg != NULL) {
7907 err = got_error(GOT_ERR_HISTEDIT_CMD);
7911 err = histedit_edit_logmsg(hle, repo);
7915 hle->logmsg = strdup(p);
7916 if (hle->logmsg == NULL) {
7917 err = got_error_from_errno("strdup");
7926 while (end[0] && !isspace((unsigned char)end[0]))
7930 err = got_object_resolve_id_str(&commit_id, repo, p);
7932 /* override error code */
7933 err = histedit_syntax_error(lineno);
7937 hle = malloc(sizeof(*hle));
7939 err = got_error_from_errno("malloc");
7943 hle->commit_id = commit_id;
7948 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7956 static const struct got_error *
7957 histedit_check_script(struct got_histedit_list *histedit_cmds,
7958 struct got_object_id_queue *commits, struct got_repository *repo)
7960 const struct got_error *err = NULL;
7961 struct got_object_qid *qid;
7962 struct got_histedit_list_entry *hle;
7963 static char msg[92];
7966 if (TAILQ_EMPTY(histedit_cmds))
7967 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7968 "histedit script contains no commands");
7969 if (SIMPLEQ_EMPTY(commits))
7970 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7972 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7973 struct got_histedit_list_entry *hle2;
7974 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7977 if (got_object_id_cmp(hle->commit_id,
7978 hle2->commit_id) != 0)
7980 err = got_object_id_str(&id_str, hle->commit_id);
7983 snprintf(msg, sizeof(msg), "commit %s is listed "
7984 "more than once in histedit script", id_str);
7986 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7990 SIMPLEQ_FOREACH(qid, commits, entry) {
7991 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7992 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7996 err = got_object_id_str(&id_str, qid->id);
7999 snprintf(msg, sizeof(msg),
8000 "commit %s missing from histedit script", id_str);
8002 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8006 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8007 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8008 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8009 "last commit in histedit script cannot be folded");
8014 static const struct got_error *
8015 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8016 const char *path, struct got_object_id_queue *commits,
8017 struct got_repository *repo)
8019 const struct got_error *err = NULL;
8023 err = get_editor(&editor);
8027 if (spawn_editor(editor, path) == -1) {
8028 err = got_error_from_errno("failed spawning editor");
8032 f = fopen(path, "r");
8034 err = got_error_from_errno("fopen");
8037 err = histedit_parse_list(histedit_cmds, f, repo);
8041 err = histedit_check_script(histedit_cmds, commits, repo);
8043 if (f && fclose(f) != 0 && err == NULL)
8044 err = got_error_from_errno("fclose");
8049 static const struct got_error *
8050 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8051 struct got_object_id_queue *, const char *, const char *,
8052 struct got_repository *);
8054 static const struct got_error *
8055 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8056 struct got_object_id_queue *commits, const char *branch_name,
8057 int edit_logmsg_only, struct got_repository *repo)
8059 const struct got_error *err;
8063 err = got_opentemp_named(&path, &f, "got-histedit");
8067 err = write_cmd_list(f, branch_name, commits);
8071 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8075 if (edit_logmsg_only) {
8077 err = histedit_parse_list(histedit_cmds, f, repo);
8079 if (fclose(f) != 0) {
8080 err = got_error_from_errno("fclose");
8084 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8086 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8087 err->code != GOT_ERR_HISTEDIT_CMD)
8089 err = histedit_edit_list_retry(histedit_cmds, err,
8090 commits, path, branch_name, repo);
8094 if (f && fclose(f) != 0 && err == NULL)
8095 err = got_error_from_errno("fclose");
8096 if (path && unlink(path) != 0 && err == NULL)
8097 err = got_error_from_errno2("unlink", path);
8102 static const struct got_error *
8103 histedit_save_list(struct got_histedit_list *histedit_cmds,
8104 struct got_worktree *worktree, struct got_repository *repo)
8106 const struct got_error *err = NULL;
8109 struct got_histedit_list_entry *hle;
8110 struct got_commit_object *commit = NULL;
8112 err = got_worktree_get_histedit_script_path(&path, worktree);
8116 f = fopen(path, "w");
8118 err = got_error_from_errno2("fopen", path);
8121 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8122 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8128 int n = fprintf(f, "%c %s\n",
8129 GOT_HISTEDIT_MESG, hle->logmsg);
8131 err = got_ferror(f, GOT_ERR_IO);
8137 if (f && fclose(f) != 0 && err == NULL)
8138 err = got_error_from_errno("fclose");
8141 got_object_commit_close(commit);
8146 histedit_free_list(struct got_histedit_list *histedit_cmds)
8148 struct got_histedit_list_entry *hle;
8150 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8151 TAILQ_REMOVE(histedit_cmds, hle, entry);
8156 static const struct got_error *
8157 histedit_load_list(struct got_histedit_list *histedit_cmds,
8158 const char *path, struct got_repository *repo)
8160 const struct got_error *err = NULL;
8163 f = fopen(path, "r");
8165 err = got_error_from_errno2("fopen", path);
8169 err = histedit_parse_list(histedit_cmds, f, repo);
8171 if (f && fclose(f) != 0 && err == NULL)
8172 err = got_error_from_errno("fclose");
8176 static const struct got_error *
8177 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8178 const struct got_error *edit_err, struct got_object_id_queue *commits,
8179 const char *path, const char *branch_name, struct got_repository *repo)
8181 const struct got_error *err = NULL, *prev_err = edit_err;
8184 while (resp != 'c' && resp != 'r' && resp != 'a') {
8185 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8186 "or (a)bort: ", getprogname(), prev_err->msg);
8191 histedit_free_list(histedit_cmds);
8192 err = histedit_run_editor(histedit_cmds, path, commits,
8195 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8196 err->code != GOT_ERR_HISTEDIT_CMD)
8203 } else if (resp == 'r') {
8204 histedit_free_list(histedit_cmds);
8205 err = histedit_edit_script(histedit_cmds,
8206 commits, branch_name, 0, repo);
8208 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8209 err->code != GOT_ERR_HISTEDIT_CMD)
8216 } else if (resp == 'a') {
8217 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8220 printf("invalid response '%c'\n", resp);
8226 static const struct got_error *
8227 histedit_complete(struct got_worktree *worktree,
8228 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8229 struct got_reference *branch, struct got_repository *repo)
8231 printf("Switching work tree to %s\n",
8232 got_ref_get_symref_target(branch));
8233 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8237 static const struct got_error *
8238 show_histedit_progress(struct got_commit_object *commit,
8239 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8241 const struct got_error *err;
8242 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8244 err = got_object_id_str(&old_id_str, hle->commit_id);
8249 err = got_object_id_str(&new_id_str, new_id);
8254 old_id_str[12] = '\0';
8256 new_id_str[12] = '\0';
8259 logmsg = strdup(hle->logmsg);
8260 if (logmsg == NULL) {
8261 err = got_error_from_errno("strdup");
8264 trim_logmsg(logmsg, 42);
8266 err = get_short_logmsg(&logmsg, 42, commit);
8271 switch (hle->cmd->code) {
8272 case GOT_HISTEDIT_PICK:
8273 case GOT_HISTEDIT_EDIT:
8274 printf("%s -> %s: %s\n", old_id_str,
8275 new_id_str ? new_id_str : "no-op change", logmsg);
8277 case GOT_HISTEDIT_DROP:
8278 case GOT_HISTEDIT_FOLD:
8279 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8291 static const struct got_error *
8292 histedit_commit(struct got_pathlist_head *merged_paths,
8293 struct got_worktree *worktree, struct got_fileindex *fileindex,
8294 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8295 struct got_repository *repo)
8297 const struct got_error *err;
8298 struct got_commit_object *commit;
8299 struct got_object_id *new_commit_id;
8301 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8302 && hle->logmsg == NULL) {
8303 err = histedit_edit_logmsg(hle, repo);
8308 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8312 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8313 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8316 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8318 err = show_histedit_progress(commit, hle, NULL);
8320 err = show_histedit_progress(commit, hle, new_commit_id);
8321 free(new_commit_id);
8324 got_object_commit_close(commit);
8328 static const struct got_error *
8329 histedit_skip_commit(struct got_histedit_list_entry *hle,
8330 struct got_worktree *worktree, struct got_repository *repo)
8332 const struct got_error *error;
8333 struct got_commit_object *commit;
8335 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8340 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8344 error = show_histedit_progress(commit, hle, NULL);
8345 got_object_commit_close(commit);
8349 static const struct got_error *
8350 check_local_changes(void *arg, unsigned char status,
8351 unsigned char staged_status, const char *path,
8352 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 int *have_local_changes = arg;
8358 case GOT_STATUS_ADD:
8359 case GOT_STATUS_DELETE:
8360 case GOT_STATUS_MODIFY:
8361 case GOT_STATUS_CONFLICT:
8362 *have_local_changes = 1;
8363 return got_error(GOT_ERR_CANCELLED);
8368 switch (staged_status) {
8369 case GOT_STATUS_ADD:
8370 case GOT_STATUS_DELETE:
8371 case GOT_STATUS_MODIFY:
8372 *have_local_changes = 1;
8373 return got_error(GOT_ERR_CANCELLED);
8381 static const struct got_error *
8382 cmd_histedit(int argc, char *argv[])
8384 const struct got_error *error = NULL;
8385 struct got_worktree *worktree = NULL;
8386 struct got_fileindex *fileindex = NULL;
8387 struct got_repository *repo = NULL;
8389 struct got_reference *branch = NULL;
8390 struct got_reference *tmp_branch = NULL;
8391 struct got_object_id *resume_commit_id = NULL;
8392 struct got_object_id *base_commit_id = NULL;
8393 struct got_object_id *head_commit_id = NULL;
8394 struct got_commit_object *commit = NULL;
8395 int ch, rebase_in_progress = 0;
8396 struct got_update_progress_arg upa;
8397 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8398 int edit_logmsg_only = 0;
8399 const char *edit_script_path = NULL;
8400 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8401 struct got_object_id_queue commits;
8402 struct got_pathlist_head merged_paths;
8403 const struct got_object_id_queue *parent_ids;
8404 struct got_object_qid *pid;
8405 struct got_histedit_list histedit_cmds;
8406 struct got_histedit_list_entry *hle;
8408 SIMPLEQ_INIT(&commits);
8409 TAILQ_INIT(&histedit_cmds);
8410 TAILQ_INIT(&merged_paths);
8411 memset(&upa, 0, sizeof(upa));
8413 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8422 edit_script_path = optarg;
8425 edit_logmsg_only = 1;
8437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8438 "unveil", NULL) == -1)
8441 if (abort_edit && continue_edit)
8442 errx(1, "histedit's -a and -c options are mutually exclusive");
8443 if (edit_script_path && edit_logmsg_only)
8444 errx(1, "histedit's -F and -m options are mutually exclusive");
8445 if (abort_edit && edit_logmsg_only)
8446 errx(1, "histedit's -a and -m options are mutually exclusive");
8447 if (continue_edit && edit_logmsg_only)
8448 errx(1, "histedit's -c and -m options are mutually exclusive");
8453 * This command cannot apply unveil(2) in all cases because the
8454 * user may choose to run an editor to edit the histedit script
8455 * and to edit individual commit log messages.
8456 * unveil(2) traverses exec(2); if an editor is used we have to
8457 * apply unveil after edit script and log messages have been written.
8458 * XXX TODO: Make use of unveil(2) where possible.
8461 cwd = getcwd(NULL, 0);
8463 error = got_error_from_errno("getcwd");
8466 error = got_worktree_open(&worktree, cwd);
8468 if (error->code == GOT_ERR_NOT_WORKTREE)
8469 error = wrap_not_worktree_error(error, "histedit", cwd);
8473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8478 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8481 if (rebase_in_progress) {
8482 error = got_error(GOT_ERR_REBASING);
8486 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8490 if (edit_in_progress && edit_logmsg_only) {
8491 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8492 "histedit operation is in progress in this "
8493 "work tree and must be continued or aborted "
8494 "before the -m option can be used");
8498 if (edit_in_progress && abort_edit) {
8499 error = got_worktree_histedit_continue(&resume_commit_id,
8500 &tmp_branch, &branch, &base_commit_id, &fileindex,
8504 printf("Switching work tree to %s\n",
8505 got_ref_get_symref_target(branch));
8506 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8507 branch, base_commit_id, update_progress, &upa);
8510 printf("Histedit of %s aborted\n",
8511 got_ref_get_symref_target(branch));
8512 print_update_progress_stats(&upa);
8513 goto done; /* nothing else to do */
8514 } else if (abort_edit) {
8515 error = got_error(GOT_ERR_NOT_HISTEDIT);
8519 if (continue_edit) {
8522 if (!edit_in_progress) {
8523 error = got_error(GOT_ERR_NOT_HISTEDIT);
8527 error = got_worktree_get_histedit_script_path(&path, worktree);
8531 error = histedit_load_list(&histedit_cmds, path, repo);
8536 error = got_worktree_histedit_continue(&resume_commit_id,
8537 &tmp_branch, &branch, &base_commit_id, &fileindex,
8542 error = got_ref_resolve(&head_commit_id, repo, branch);
8546 error = got_object_open_as_commit(&commit, repo,
8550 parent_ids = got_object_commit_get_parent_ids(commit);
8551 pid = SIMPLEQ_FIRST(parent_ids);
8553 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8556 error = collect_commits(&commits, head_commit_id, pid->id,
8557 base_commit_id, got_worktree_get_path_prefix(worktree),
8558 GOT_ERR_HISTEDIT_PATH, repo);
8559 got_object_commit_close(commit);
8564 if (edit_in_progress) {
8565 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8569 error = got_ref_open(&branch, repo,
8570 got_worktree_get_head_ref_name(worktree), 0);
8574 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8575 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8576 "will not edit commit history of a branch outside "
8577 "the \"refs/heads/\" reference namespace");
8581 error = got_ref_resolve(&head_commit_id, repo, branch);
8582 got_ref_close(branch);
8587 error = got_object_open_as_commit(&commit, repo,
8591 parent_ids = got_object_commit_get_parent_ids(commit);
8592 pid = SIMPLEQ_FIRST(parent_ids);
8594 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8597 error = collect_commits(&commits, head_commit_id, pid->id,
8598 got_worktree_get_base_commit_id(worktree),
8599 got_worktree_get_path_prefix(worktree),
8600 GOT_ERR_HISTEDIT_PATH, repo);
8601 got_object_commit_close(commit);
8606 if (SIMPLEQ_EMPTY(&commits)) {
8607 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8611 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8612 &base_commit_id, &fileindex, worktree, repo);
8616 if (edit_script_path) {
8617 error = histedit_load_list(&histedit_cmds,
8618 edit_script_path, repo);
8620 got_worktree_histedit_abort(worktree, fileindex,
8621 repo, branch, base_commit_id,
8622 update_progress, &upa);
8623 print_update_progress_stats(&upa);
8627 const char *branch_name;
8628 branch_name = got_ref_get_symref_target(branch);
8629 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8631 error = histedit_edit_script(&histedit_cmds, &commits,
8632 branch_name, edit_logmsg_only, repo);
8634 got_worktree_histedit_abort(worktree, fileindex,
8635 repo, branch, base_commit_id,
8636 update_progress, &upa);
8637 print_update_progress_stats(&upa);
8643 error = histedit_save_list(&histedit_cmds, worktree,
8646 got_worktree_histedit_abort(worktree, fileindex,
8647 repo, branch, base_commit_id,
8648 update_progress, &upa);
8649 print_update_progress_stats(&upa);
8655 error = histedit_check_script(&histedit_cmds, &commits, repo);
8659 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8660 if (resume_commit_id) {
8661 if (got_object_id_cmp(hle->commit_id,
8662 resume_commit_id) != 0)
8665 resume_commit_id = NULL;
8666 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8667 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8668 error = histedit_skip_commit(hle, worktree,
8673 struct got_pathlist_head paths;
8674 int have_changes = 0;
8677 error = got_pathlist_append(&paths, "", NULL);
8680 error = got_worktree_status(worktree, &paths,
8681 repo, check_local_changes, &have_changes,
8682 check_cancelled, NULL);
8683 got_pathlist_free(&paths);
8685 if (error->code != GOT_ERR_CANCELLED)
8687 if (sigint_received || sigpipe_received)
8691 error = histedit_commit(NULL, worktree,
8692 fileindex, tmp_branch, hle, repo);
8696 error = got_object_open_as_commit(
8697 &commit, repo, hle->commit_id);
8700 error = show_histedit_progress(commit,
8702 got_object_commit_close(commit);
8711 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8712 error = histedit_skip_commit(hle, worktree, repo);
8718 error = got_object_open_as_commit(&commit, repo,
8722 parent_ids = got_object_commit_get_parent_ids(commit);
8723 pid = SIMPLEQ_FIRST(parent_ids);
8725 error = got_worktree_histedit_merge_files(&merged_paths,
8726 worktree, fileindex, pid->id, hle->commit_id, repo,
8727 update_progress, &upa, check_cancelled, NULL);
8730 got_object_commit_close(commit);
8733 print_update_progress_stats(&upa);
8734 if (upa.conflicts > 0)
8735 rebase_status = GOT_STATUS_CONFLICT;
8737 if (rebase_status == GOT_STATUS_CONFLICT) {
8738 error = show_rebase_merge_conflict(hle->commit_id,
8742 got_worktree_rebase_pathlist_free(&merged_paths);
8746 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8748 error = got_object_id_str(&id_str, hle->commit_id);
8751 printf("Stopping histedit for amending commit %s\n",
8754 got_worktree_rebase_pathlist_free(&merged_paths);
8755 error = got_worktree_histedit_postpone(worktree,
8760 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8761 error = histedit_skip_commit(hle, worktree, repo);
8767 error = histedit_commit(&merged_paths, worktree, fileindex,
8768 tmp_branch, hle, repo);
8769 got_worktree_rebase_pathlist_free(&merged_paths);
8774 if (rebase_status == GOT_STATUS_CONFLICT) {
8775 error = got_worktree_histedit_postpone(worktree, fileindex);
8778 error = got_error_msg(GOT_ERR_CONFLICTS,
8779 "conflicts must be resolved before histedit can continue");
8781 error = histedit_complete(worktree, fileindex, tmp_branch,
8784 got_object_id_queue_free(&commits);
8785 histedit_free_list(&histedit_cmds);
8786 free(head_commit_id);
8787 free(base_commit_id);
8788 free(resume_commit_id);
8790 got_object_commit_close(commit);
8792 got_ref_close(branch);
8794 got_ref_close(tmp_branch);
8796 got_worktree_close(worktree);
8798 got_repo_close(repo);
8803 usage_integrate(void)
8805 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8809 static const struct got_error *
8810 cmd_integrate(int argc, char *argv[])
8812 const struct got_error *error = NULL;
8813 struct got_repository *repo = NULL;
8814 struct got_worktree *worktree = NULL;
8815 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8816 const char *branch_arg = NULL;
8817 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8818 struct got_fileindex *fileindex = NULL;
8819 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8821 struct got_update_progress_arg upa;
8823 while ((ch = getopt(argc, argv, "")) != -1) {
8836 branch_arg = argv[0];
8838 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8839 "unveil", NULL) == -1)
8842 cwd = getcwd(NULL, 0);
8844 error = got_error_from_errno("getcwd");
8848 error = got_worktree_open(&worktree, cwd);
8850 if (error->code == GOT_ERR_NOT_WORKTREE)
8851 error = wrap_not_worktree_error(error, "integrate",
8856 error = check_rebase_or_histedit_in_progress(worktree);
8860 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8865 error = apply_unveil(got_repo_get_path(repo), 0,
8866 got_worktree_get_root_path(worktree));
8870 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8871 error = got_error_from_errno("asprintf");
8875 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8876 &base_branch_ref, worktree, refname, repo);
8880 refname = strdup(got_ref_get_name(branch_ref));
8881 if (refname == NULL) {
8882 error = got_error_from_errno("strdup");
8883 got_worktree_integrate_abort(worktree, fileindex, repo,
8884 branch_ref, base_branch_ref);
8887 base_refname = strdup(got_ref_get_name(base_branch_ref));
8888 if (base_refname == NULL) {
8889 error = got_error_from_errno("strdup");
8890 got_worktree_integrate_abort(worktree, fileindex, repo,
8891 branch_ref, base_branch_ref);
8895 error = got_ref_resolve(&commit_id, repo, branch_ref);
8899 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8903 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8904 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8905 "specified branch has already been integrated");
8906 got_worktree_integrate_abort(worktree, fileindex, repo,
8907 branch_ref, base_branch_ref);
8911 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8913 if (error->code == GOT_ERR_ANCESTRY)
8914 error = got_error(GOT_ERR_REBASE_REQUIRED);
8915 got_worktree_integrate_abort(worktree, fileindex, repo,
8916 branch_ref, base_branch_ref);
8920 memset(&upa, 0, sizeof(upa));
8921 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8922 branch_ref, base_branch_ref, update_progress, &upa,
8923 check_cancelled, NULL);
8927 printf("Integrated %s into %s\n", refname, base_refname);
8928 print_update_progress_stats(&upa);
8931 got_repo_close(repo);
8933 got_worktree_close(worktree);
8935 free(base_commit_id);
8945 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8946 "[-S] [file-path ...]\n",
8951 static const struct got_error *
8952 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8953 const char *path, struct got_object_id *blob_id,
8954 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8955 int dirfd, const char *de_name)
8957 const struct got_error *err = NULL;
8958 char *id_str = NULL;
8960 if (staged_status != GOT_STATUS_ADD &&
8961 staged_status != GOT_STATUS_MODIFY &&
8962 staged_status != GOT_STATUS_DELETE)
8965 if (staged_status == GOT_STATUS_ADD ||
8966 staged_status == GOT_STATUS_MODIFY)
8967 err = got_object_id_str(&id_str, staged_blob_id);
8969 err = got_object_id_str(&id_str, blob_id);
8973 printf("%s %c %s\n", id_str, staged_status, path);
8978 static const struct got_error *
8979 cmd_stage(int argc, char *argv[])
8981 const struct got_error *error = NULL;
8982 struct got_repository *repo = NULL;
8983 struct got_worktree *worktree = NULL;
8985 struct got_pathlist_head paths;
8986 struct got_pathlist_entry *pe;
8987 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8988 FILE *patch_script_file = NULL;
8989 const char *patch_script_path = NULL;
8990 struct choose_patch_arg cpa;
8994 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9003 patch_script_path = optarg;
9006 allow_bad_symlinks = 1;
9018 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9019 "unveil", NULL) == -1)
9022 if (list_stage && (pflag || patch_script_path))
9023 errx(1, "-l option cannot be used with other options");
9024 if (patch_script_path && !pflag)
9025 errx(1, "-F option can only be used together with -p option");
9027 cwd = getcwd(NULL, 0);
9029 error = got_error_from_errno("getcwd");
9033 error = got_worktree_open(&worktree, cwd);
9035 if (error->code == GOT_ERR_NOT_WORKTREE)
9036 error = wrap_not_worktree_error(error, "stage", cwd);
9040 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9045 if (patch_script_path) {
9046 patch_script_file = fopen(patch_script_path, "r");
9047 if (patch_script_file == NULL) {
9048 error = got_error_from_errno2("fopen",
9053 error = apply_unveil(got_repo_get_path(repo), 0,
9054 got_worktree_get_root_path(worktree));
9058 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9063 error = got_worktree_status(worktree, &paths, repo,
9064 print_stage, NULL, check_cancelled, NULL);
9066 cpa.patch_script_file = patch_script_file;
9067 cpa.action = "stage";
9068 error = got_worktree_stage(worktree, &paths,
9069 pflag ? NULL : print_status, NULL,
9070 pflag ? choose_patch : NULL, &cpa,
9071 allow_bad_symlinks, repo);
9074 if (patch_script_file && fclose(patch_script_file) == EOF &&
9076 error = got_error_from_errno2("fclose", patch_script_path);
9078 got_repo_close(repo);
9080 got_worktree_close(worktree);
9081 TAILQ_FOREACH(pe, &paths, entry)
9082 free((char *)pe->path);
9083 got_pathlist_free(&paths);
9091 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9092 "[file-path ...]\n",
9098 static const struct got_error *
9099 cmd_unstage(int argc, char *argv[])
9101 const struct got_error *error = NULL;
9102 struct got_repository *repo = NULL;
9103 struct got_worktree *worktree = NULL;
9105 struct got_pathlist_head paths;
9106 struct got_pathlist_entry *pe;
9108 struct got_update_progress_arg upa;
9109 FILE *patch_script_file = NULL;
9110 const char *patch_script_path = NULL;
9111 struct choose_patch_arg cpa;
9115 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9121 patch_script_path = optarg;
9133 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9134 "unveil", NULL) == -1)
9137 if (patch_script_path && !pflag)
9138 errx(1, "-F option can only be used together with -p option");
9140 cwd = getcwd(NULL, 0);
9142 error = got_error_from_errno("getcwd");
9146 error = got_worktree_open(&worktree, cwd);
9148 if (error->code == GOT_ERR_NOT_WORKTREE)
9149 error = wrap_not_worktree_error(error, "unstage", cwd);
9153 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9158 if (patch_script_path) {
9159 patch_script_file = fopen(patch_script_path, "r");
9160 if (patch_script_file == NULL) {
9161 error = got_error_from_errno2("fopen",
9167 error = apply_unveil(got_repo_get_path(repo), 0,
9168 got_worktree_get_root_path(worktree));
9172 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9176 cpa.patch_script_file = patch_script_file;
9177 cpa.action = "unstage";
9178 memset(&upa, 0, sizeof(upa));
9179 error = got_worktree_unstage(worktree, &paths, update_progress,
9180 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9182 print_update_progress_stats(&upa);
9184 if (patch_script_file && fclose(patch_script_file) == EOF &&
9186 error = got_error_from_errno2("fclose", patch_script_path);
9188 got_repo_close(repo);
9190 got_worktree_close(worktree);
9191 TAILQ_FOREACH(pe, &paths, entry)
9192 free((char *)pe->path);
9193 got_pathlist_free(&paths);
9201 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9202 "arg1 [arg2 ...]\n", getprogname());
9206 static const struct got_error *
9207 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9209 const struct got_error *err;
9210 struct got_blob_object *blob;
9212 err = got_object_open_as_blob(&blob, repo, id, 8192);
9216 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9217 got_object_blob_close(blob);
9221 static const struct got_error *
9222 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9224 const struct got_error *err;
9225 struct got_tree_object *tree;
9228 err = got_object_open_as_tree(&tree, repo, id);
9232 nentries = got_object_tree_get_nentries(tree);
9233 for (i = 0; i < nentries; i++) {
9234 struct got_tree_entry *te;
9236 if (sigint_received || sigpipe_received)
9238 te = got_object_tree_get_entry(tree, i);
9239 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9242 fprintf(outfile, "%s %.7o %s\n", id_str,
9243 got_tree_entry_get_mode(te),
9244 got_tree_entry_get_name(te));
9248 got_object_tree_close(tree);
9252 static const struct got_error *
9253 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9255 const struct got_error *err;
9256 struct got_commit_object *commit;
9257 const struct got_object_id_queue *parent_ids;
9258 struct got_object_qid *pid;
9259 char *id_str = NULL;
9260 const char *logmsg = NULL;
9262 err = got_object_open_as_commit(&commit, repo, id);
9266 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9270 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9271 parent_ids = got_object_commit_get_parent_ids(commit);
9272 fprintf(outfile, "numparents %d\n",
9273 got_object_commit_get_nparents(commit));
9274 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9276 err = got_object_id_str(&pid_str, pid->id);
9279 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9282 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9283 got_object_commit_get_author(commit),
9284 got_object_commit_get_author_time(commit));
9286 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9287 got_object_commit_get_author(commit),
9288 got_object_commit_get_committer_time(commit));
9290 logmsg = got_object_commit_get_logmsg_raw(commit);
9291 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9292 fprintf(outfile, "%s", logmsg);
9295 got_object_commit_close(commit);
9299 static const struct got_error *
9300 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9302 const struct got_error *err;
9303 struct got_tag_object *tag;
9304 char *id_str = NULL;
9305 const char *tagmsg = NULL;
9307 err = got_object_open_as_tag(&tag, repo, id);
9311 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9315 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9317 switch (got_object_tag_get_object_type(tag)) {
9318 case GOT_OBJ_TYPE_BLOB:
9319 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9320 GOT_OBJ_LABEL_BLOB);
9322 case GOT_OBJ_TYPE_TREE:
9323 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9324 GOT_OBJ_LABEL_TREE);
9326 case GOT_OBJ_TYPE_COMMIT:
9327 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9328 GOT_OBJ_LABEL_COMMIT);
9330 case GOT_OBJ_TYPE_TAG:
9331 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9338 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9339 got_object_tag_get_name(tag));
9341 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9342 got_object_tag_get_tagger(tag),
9343 got_object_tag_get_tagger_time(tag));
9345 tagmsg = got_object_tag_get_message(tag);
9346 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9347 fprintf(outfile, "%s", tagmsg);
9350 got_object_tag_close(tag);
9354 static const struct got_error *
9355 cmd_cat(int argc, char *argv[])
9357 const struct got_error *error;
9358 struct got_repository *repo = NULL;
9359 struct got_worktree *worktree = NULL;
9360 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9361 const char *commit_id_str = NULL;
9362 struct got_object_id *id = NULL, *commit_id = NULL;
9363 int ch, obj_type, i, force_path = 0;
9366 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9371 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9374 commit_id_str = optarg;
9377 repo_path = realpath(optarg, NULL);
9378 if (repo_path == NULL)
9379 return got_error_from_errno2("realpath",
9381 got_path_strip_trailing_slashes(repo_path);
9395 cwd = getcwd(NULL, 0);
9397 error = got_error_from_errno("getcwd");
9400 error = got_worktree_open(&worktree, cwd);
9401 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9404 if (repo_path == NULL) {
9406 got_worktree_get_repo_path(worktree));
9407 if (repo_path == NULL) {
9408 error = got_error_from_errno("strdup");
9414 if (repo_path == NULL) {
9415 repo_path = getcwd(NULL, 0);
9416 if (repo_path == NULL)
9417 return got_error_from_errno("getcwd");
9420 error = got_repo_open(&repo, repo_path, NULL);
9425 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9429 if (commit_id_str == NULL)
9430 commit_id_str = GOT_REF_HEAD;
9431 error = got_repo_match_object_id(&commit_id, NULL,
9432 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9436 for (i = 0; i < argc; i++) {
9438 error = got_object_id_by_path(&id, repo, commit_id,
9443 error = got_repo_match_object_id(&id, &label, argv[i],
9444 GOT_OBJ_TYPE_ANY, 0, repo);
9446 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9447 error->code != GOT_ERR_NOT_REF)
9449 error = got_object_id_by_path(&id, repo,
9450 commit_id, argv[i]);
9456 error = got_object_get_type(&obj_type, repo, id);
9461 case GOT_OBJ_TYPE_BLOB:
9462 error = cat_blob(id, repo, stdout);
9464 case GOT_OBJ_TYPE_TREE:
9465 error = cat_tree(id, repo, stdout);
9467 case GOT_OBJ_TYPE_COMMIT:
9468 error = cat_commit(id, repo, stdout);
9470 case GOT_OBJ_TYPE_TAG:
9471 error = cat_tag(id, repo, stdout);
9474 error = got_error(GOT_ERR_OBJ_TYPE);
9489 got_worktree_close(worktree);
9491 const struct got_error *repo_error;
9492 repo_error = got_repo_close(repo);
9502 fprintf(stderr, "usage: %s info [path ...]\n",
9507 static const struct got_error *
9508 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9509 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9510 struct got_object_id *commit_id)
9512 const struct got_error *err = NULL;
9513 char *id_str = NULL;
9515 struct tm mytm, *tm;
9516 struct got_pathlist_head *paths = arg;
9517 struct got_pathlist_entry *pe;
9520 * Clear error indication from any of the path arguments which
9521 * would cause this file index entry to be displayed.
9523 TAILQ_FOREACH(pe, paths, entry) {
9524 if (got_path_cmp(path, pe->path, strlen(path),
9525 pe->path_len) == 0 ||
9526 got_path_is_child(path, pe->path, pe->path_len))
9527 pe->data = NULL; /* no error */
9530 printf(GOT_COMMIT_SEP_STR);
9532 printf("symlink: %s\n", path);
9533 else if (S_ISREG(mode)) {
9534 printf("file: %s\n", path);
9535 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9536 } else if (S_ISDIR(mode))
9537 printf("directory: %s\n", path);
9539 printf("something: %s\n", path);
9541 tm = localtime_r(&mtime, &mytm);
9544 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9545 return got_error(GOT_ERR_NO_SPACE);
9546 printf("timestamp: %s\n", datebuf);
9549 err = got_object_id_str(&id_str, blob_id);
9552 printf("based on blob: %s\n", id_str);
9556 if (staged_blob_id) {
9557 err = got_object_id_str(&id_str, staged_blob_id);
9560 printf("based on staged blob: %s\n", id_str);
9565 err = got_object_id_str(&id_str, commit_id);
9568 printf("based on commit: %s\n", id_str);
9575 static const struct got_error *
9576 cmd_info(int argc, char *argv[])
9578 const struct got_error *error = NULL;
9579 struct got_worktree *worktree = NULL;
9580 char *cwd = NULL, *id_str = NULL;
9581 struct got_pathlist_head paths;
9582 struct got_pathlist_entry *pe;
9583 char *uuidstr = NULL;
9584 int ch, show_files = 0;
9588 while ((ch = getopt(argc, argv, "")) != -1) {
9600 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9604 cwd = getcwd(NULL, 0);
9606 error = got_error_from_errno("getcwd");
9610 error = got_worktree_open(&worktree, cwd);
9612 if (error->code == GOT_ERR_NOT_WORKTREE)
9613 error = wrap_not_worktree_error(error, "status", cwd);
9617 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9622 error = get_worktree_paths_from_argv(&paths, argc, argv,
9629 error = got_object_id_str(&id_str,
9630 got_worktree_get_base_commit_id(worktree));
9634 error = got_worktree_get_uuid(&uuidstr, worktree);
9638 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9639 printf("work tree base commit: %s\n", id_str);
9640 printf("work tree path prefix: %s\n",
9641 got_worktree_get_path_prefix(worktree));
9642 printf("work tree branch reference: %s\n",
9643 got_worktree_get_head_ref_name(worktree));
9644 printf("work tree UUID: %s\n", uuidstr);
9645 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9648 struct got_pathlist_entry *pe;
9649 TAILQ_FOREACH(pe, &paths, entry) {
9650 if (pe->path_len == 0)
9653 * Assume this path will fail. This will be corrected
9654 * in print_path_info() in case the path does suceeed.
9656 pe->data = (void *)got_error_path(pe->path,
9659 error = got_worktree_path_info(worktree, &paths,
9660 print_path_info, &paths, check_cancelled, NULL);
9663 TAILQ_FOREACH(pe, &paths, entry) {
9664 if (pe->data != NULL) {
9665 error = pe->data; /* bad path */
9671 TAILQ_FOREACH(pe, &paths, entry)
9672 free((char *)pe->path);
9673 got_pathlist_free(&paths);