Blob


1 /*
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>
5 *
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.
9 *
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.
17 */
19 #include <sys/queue.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libgen.h>
37 #include <time.h>
38 #include <paths.h>
39 #include <regex.h>
40 #include <getopt.h>
41 #include <util.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"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_diff.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"
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 static volatile sig_atomic_t sigint_received;
64 static volatile sig_atomic_t sigpipe_received;
66 static void
67 catch_sigint(int signo)
68 {
69 sigint_received = 1;
70 }
72 static void
73 catch_sigpipe(int signo)
74 {
75 sigpipe_received = 1;
76 }
79 struct got_cmd {
80 const char *cmd_name;
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
83 const char *cmd_alias;
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_init(void);
88 __dead static void usage_import(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_fetch(void);
91 __dead static void usage_checkout(void);
92 __dead static void usage_update(void);
93 __dead static void usage_log(void);
94 __dead static void usage_diff(void);
95 __dead static void usage_blame(void);
96 __dead static void usage_tree(void);
97 __dead static void usage_status(void);
98 __dead static void usage_ref(void);
99 __dead static void usage_branch(void);
100 __dead static void usage_tag(void);
101 __dead static void usage_add(void);
102 __dead static void usage_remove(void);
103 __dead static void usage_revert(void);
104 __dead static void usage_commit(void);
105 __dead static void usage_cherrypick(void);
106 __dead static void usage_backout(void);
107 __dead static void usage_rebase(void);
108 __dead static void usage_histedit(void);
109 __dead static void usage_integrate(void);
110 __dead static void usage_stage(void);
111 __dead static void usage_unstage(void);
112 __dead static void usage_cat(void);
113 __dead static void usage_info(void);
115 static const struct got_error* cmd_init(int, char *[]);
116 static const struct got_error* cmd_import(int, char *[]);
117 static const struct got_error* cmd_clone(int, char *[]);
118 static const struct got_error* cmd_fetch(int, char *[]);
119 static const struct got_error* cmd_checkout(int, char *[]);
120 static const struct got_error* cmd_update(int, char *[]);
121 static const struct got_error* cmd_log(int, char *[]);
122 static const struct got_error* cmd_diff(int, char *[]);
123 static const struct got_error* cmd_blame(int, char *[]);
124 static const struct got_error* cmd_tree(int, char *[]);
125 static const struct got_error* cmd_status(int, char *[]);
126 static const struct got_error* cmd_ref(int, char *[]);
127 static const struct got_error* cmd_branch(int, char *[]);
128 static const struct got_error* cmd_tag(int, char *[]);
129 static const struct got_error* cmd_add(int, char *[]);
130 static const struct got_error* cmd_remove(int, char *[]);
131 static const struct got_error* cmd_revert(int, char *[]);
132 static const struct got_error* cmd_commit(int, char *[]);
133 static const struct got_error* cmd_cherrypick(int, char *[]);
134 static const struct got_error* cmd_backout(int, char *[]);
135 static const struct got_error* cmd_rebase(int, char *[]);
136 static const struct got_error* cmd_histedit(int, char *[]);
137 static const struct got_error* cmd_integrate(int, char *[]);
138 static const struct got_error* cmd_stage(int, char *[]);
139 static const struct got_error* cmd_unstage(int, char *[]);
140 static const struct got_error* cmd_cat(int, char *[]);
141 static const struct got_error* cmd_info(int, char *[]);
143 static struct got_cmd got_commands[] = {
144 { "init", cmd_init, usage_init, "" },
145 { "import", cmd_import, usage_import, "im" },
146 { "clone", cmd_clone, usage_clone, "cl" },
147 { "fetch", cmd_fetch, usage_fetch, "fe" },
148 { "checkout", cmd_checkout, usage_checkout, "co" },
149 { "update", cmd_update, usage_update, "up" },
150 { "log", cmd_log, usage_log, "" },
151 { "diff", cmd_diff, usage_diff, "di" },
152 { "blame", cmd_blame, usage_blame, "bl" },
153 { "tree", cmd_tree, usage_tree, "tr" },
154 { "status", cmd_status, usage_status, "st" },
155 { "ref", cmd_ref, usage_ref, "" },
156 { "branch", cmd_branch, usage_branch, "br" },
157 { "tag", cmd_tag, usage_tag, "" },
158 { "add", cmd_add, usage_add, "" },
159 { "remove", cmd_remove, usage_remove, "rm" },
160 { "revert", cmd_revert, usage_revert, "rv" },
161 { "commit", cmd_commit, usage_commit, "ci" },
162 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 { "backout", cmd_backout, usage_backout, "bo" },
164 { "rebase", cmd_rebase, usage_rebase, "rb" },
165 { "histedit", cmd_histedit, usage_histedit, "he" },
166 { "integrate", cmd_integrate, usage_integrate,"ig" },
167 { "stage", cmd_stage, usage_stage, "sg" },
168 { "unstage", cmd_unstage, usage_unstage, "ug" },
169 { "cat", cmd_cat, usage_cat, "" },
170 { "info", cmd_info, usage_info, "" },
171 };
173 static void
174 list_commands(FILE *fp)
176 size_t i;
178 fprintf(fp, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(fp, " %s", cmd->cmd_name);
183 fputc('\n', fp);
186 __dead static void
187 option_conflict(char a, char b)
189 errx(1, "-%c and -%c options are mutually exclusive", a, b);
192 int
193 main(int argc, char *argv[])
195 struct got_cmd *cmd;
196 size_t i;
197 int ch;
198 int hflag = 0, Vflag = 0;
199 static struct option longopts[] = {
200 { "version", no_argument, NULL, 'V' },
201 { NULL, 0, NULL, 0 }
202 };
204 setlocale(LC_CTYPE, "");
206 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 switch (ch) {
208 case 'h':
209 hflag = 1;
210 break;
211 case 'V':
212 Vflag = 1;
213 break;
214 default:
215 usage(hflag, 1);
216 /* NOTREACHED */
220 argc -= optind;
221 argv += optind;
222 optind = 1;
223 optreset = 1;
225 if (Vflag) {
226 got_version_print_str();
227 return 0;
230 if (argc <= 0)
231 usage(hflag, hflag ? 0 : 1);
233 signal(SIGINT, catch_sigint);
234 signal(SIGPIPE, catch_sigpipe);
236 for (i = 0; i < nitems(got_commands); i++) {
237 const struct got_error *error;
239 cmd = &got_commands[i];
241 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 strcmp(cmd->cmd_alias, argv[0]) != 0)
243 continue;
245 if (hflag)
246 got_commands[i].cmd_usage();
248 error = got_commands[i].cmd_main(argc, argv);
249 if (error && error->code != GOT_ERR_CANCELLED &&
250 error->code != GOT_ERR_PRIVSEP_EXIT &&
251 !(sigpipe_received &&
252 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 !(sigint_received &&
254 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
256 return 1;
259 return 0;
262 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 list_commands(stderr);
264 return 1;
267 __dead static void
268 usage(int hflag, int status)
270 FILE *fp = (status == 0) ? stdout : stderr;
272 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 getprogname());
274 if (hflag)
275 list_commands(fp);
276 exit(status);
279 static const struct got_error *
280 get_editor(char **abspath)
282 const struct got_error *err = NULL;
283 const char *editor;
285 *abspath = NULL;
287 editor = getenv("VISUAL");
288 if (editor == NULL)
289 editor = getenv("EDITOR");
291 if (editor) {
292 err = got_path_find_prog(abspath, editor);
293 if (err)
294 return err;
297 if (*abspath == NULL) {
298 *abspath = strdup("/bin/ed");
299 if (*abspath == NULL)
300 return got_error_from_errno("strdup");
303 return NULL;
306 static const struct got_error *
307 apply_unveil(const char *repo_path, int repo_read_only,
308 const char *worktree_path)
310 const struct got_error *err;
312 #ifdef PROFILE
313 if (unveil("gmon.out", "rwc") != 0)
314 return got_error_from_errno2("unveil", "gmon.out");
315 #endif
316 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 return got_error_from_errno2("unveil", repo_path);
319 if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 return got_error_from_errno2("unveil", worktree_path);
322 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 err = got_privsep_unveil_exec_helpers();
326 if (err != NULL)
327 return err;
329 if (unveil(NULL, NULL) != 0)
330 return got_error_from_errno("unveil");
332 return NULL;
335 __dead static void
336 usage_init(void)
338 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
339 exit(1);
342 static const struct got_error *
343 cmd_init(int argc, char *argv[])
345 const struct got_error *error = NULL;
346 char *repo_path = NULL;
347 int ch;
349 while ((ch = getopt(argc, argv, "")) != -1) {
350 switch (ch) {
351 default:
352 usage_init();
353 /* NOTREACHED */
357 argc -= optind;
358 argv += optind;
360 #ifndef PROFILE
361 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 err(1, "pledge");
363 #endif
364 if (argc != 1)
365 usage_init();
367 repo_path = strdup(argv[0]);
368 if (repo_path == NULL)
369 return got_error_from_errno("strdup");
371 got_path_strip_trailing_slashes(repo_path);
373 error = got_path_mkdir(repo_path);
374 if (error &&
375 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 goto done;
378 error = apply_unveil(repo_path, 0, NULL);
379 if (error)
380 goto done;
382 error = got_repo_init(repo_path);
383 done:
384 free(repo_path);
385 return error;
388 __dead static void
389 usage_import(void)
391 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 "[-r repository-path] [-I pattern] path\n", getprogname());
393 exit(1);
396 int
397 spawn_editor(const char *editor, const char *file)
399 pid_t pid;
400 sig_t sighup, sigint, sigquit;
401 int st = -1;
403 sighup = signal(SIGHUP, SIG_IGN);
404 sigint = signal(SIGINT, SIG_IGN);
405 sigquit = signal(SIGQUIT, SIG_IGN);
407 switch (pid = fork()) {
408 case -1:
409 goto doneediting;
410 case 0:
411 execl(editor, editor, file, (char *)NULL);
412 _exit(127);
415 while (waitpid(pid, &st, 0) == -1)
416 if (errno != EINTR)
417 break;
419 doneediting:
420 (void)signal(SIGHUP, sighup);
421 (void)signal(SIGINT, sigint);
422 (void)signal(SIGQUIT, sigquit);
424 if (!WIFEXITED(st)) {
425 errno = EINTR;
426 return -1;
429 return WEXITSTATUS(st);
432 static const struct got_error *
433 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 const char *initial_content, size_t initial_content_len, int check_comments)
436 const struct got_error *err = NULL;
437 char *line = NULL;
438 size_t linesize = 0;
439 ssize_t linelen;
440 struct stat st, st2;
441 FILE *fp = NULL;
442 size_t len, logmsg_len;
443 char *initial_content_stripped = NULL, *buf = NULL, *s;
445 *logmsg = NULL;
447 if (stat(logmsg_path, &st) == -1)
448 return got_error_from_errno2("stat", logmsg_path);
450 if (spawn_editor(editor, logmsg_path) == -1)
451 return got_error_from_errno("failed spawning editor");
453 if (stat(logmsg_path, &st2) == -1)
454 return got_error_from_errno("stat");
456 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
457 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
458 "no changes made to commit message, aborting");
460 /*
461 * Set up a stripped version of the initial content without comments
462 * and blank lines. We need this in order to check if the message
463 * has in fact been edited.
464 */
465 initial_content_stripped = malloc(initial_content_len + 1);
466 if (initial_content_stripped == NULL)
467 return got_error_from_errno("malloc");
468 initial_content_stripped[0] = '\0';
470 buf = strdup(initial_content);
471 if (buf == NULL) {
472 err = got_error_from_errno("strdup");
473 goto done;
475 s = buf;
476 len = 0;
477 while ((line = strsep(&s, "\n")) != NULL) {
478 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
479 continue; /* remove comments and leading empty lines */
480 len = strlcat(initial_content_stripped, line,
481 initial_content_len + 1);
482 if (len >= initial_content_len + 1) {
483 err = got_error(GOT_ERR_NO_SPACE);
484 goto done;
487 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
488 initial_content_stripped[len - 1] = '\0';
489 len--;
492 logmsg_len = st2.st_size;
493 *logmsg = malloc(logmsg_len + 1);
494 if (*logmsg == NULL)
495 return got_error_from_errno("malloc");
496 (*logmsg)[0] = '\0';
498 fp = fopen(logmsg_path, "r");
499 if (fp == NULL) {
500 err = got_error_from_errno("fopen");
501 goto done;
504 len = 0;
505 while ((linelen = getline(&line, &linesize, fp)) != -1) {
506 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
507 continue; /* remove comments and leading empty lines */
508 len = strlcat(*logmsg, line, logmsg_len + 1);
509 if (len >= logmsg_len + 1) {
510 err = got_error(GOT_ERR_NO_SPACE);
511 goto done;
514 free(line);
515 if (ferror(fp)) {
516 err = got_ferror(fp, GOT_ERR_IO);
517 goto done;
519 while (len > 0 && (*logmsg)[len - 1] == '\n') {
520 (*logmsg)[len - 1] = '\0';
521 len--;
524 if (len == 0) {
525 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
526 "commit message cannot be empty, aborting");
527 goto done;
529 if (check_comments && strcmp(*logmsg, initial_content_stripped) == 0)
530 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 "no changes made to commit message, aborting");
532 done:
533 free(initial_content_stripped);
534 free(buf);
535 if (fp && fclose(fp) == EOF && err == NULL)
536 err = got_error_from_errno("fclose");
537 if (err) {
538 free(*logmsg);
539 *logmsg = NULL;
541 return err;
544 static const struct got_error *
545 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
546 const char *path_dir, const char *branch_name)
548 char *initial_content = NULL;
549 const struct got_error *err = NULL;
550 int initial_content_len;
551 int fd = -1;
553 initial_content_len = asprintf(&initial_content,
554 "\n# %s to be imported to branch %s\n", path_dir,
555 branch_name);
556 if (initial_content_len == -1)
557 return got_error_from_errno("asprintf");
559 err = got_opentemp_named_fd(logmsg_path, &fd,
560 GOT_TMPDIR_STR "/got-importmsg");
561 if (err)
562 goto done;
564 if (write(fd, initial_content, initial_content_len) == -1) {
565 err = got_error_from_errno2("write", *logmsg_path);
566 goto done;
569 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
570 initial_content_len, 1);
571 done:
572 if (fd != -1 && close(fd) == -1 && err == NULL)
573 err = got_error_from_errno2("close", *logmsg_path);
574 free(initial_content);
575 if (err) {
576 free(*logmsg_path);
577 *logmsg_path = NULL;
579 return err;
582 static const struct got_error *
583 import_progress(void *arg, const char *path)
585 printf("A %s\n", path);
586 return NULL;
589 static const struct got_error *
590 get_author(char **author, struct got_repository *repo,
591 struct got_worktree *worktree)
593 const struct got_error *err = NULL;
594 const char *got_author = NULL, *name, *email;
595 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
597 *author = NULL;
599 if (worktree)
600 worktree_conf = got_worktree_get_gotconfig(worktree);
601 repo_conf = got_repo_get_gotconfig(repo);
603 /*
604 * Priority of potential author information sources, from most
605 * significant to least significant:
606 * 1) work tree's .got/got.conf file
607 * 2) repository's got.conf file
608 * 3) repository's git config file
609 * 4) environment variables
610 * 5) global git config files (in user's home directory or /etc)
611 */
613 if (worktree_conf)
614 got_author = got_gotconfig_get_author(worktree_conf);
615 if (got_author == NULL)
616 got_author = got_gotconfig_get_author(repo_conf);
617 if (got_author == NULL) {
618 name = got_repo_get_gitconfig_author_name(repo);
619 email = got_repo_get_gitconfig_author_email(repo);
620 if (name && email) {
621 if (asprintf(author, "%s <%s>", name, email) == -1)
622 return got_error_from_errno("asprintf");
623 return NULL;
626 got_author = getenv("GOT_AUTHOR");
627 if (got_author == NULL) {
628 name = got_repo_get_global_gitconfig_author_name(repo);
629 email = got_repo_get_global_gitconfig_author_email(
630 repo);
631 if (name && email) {
632 if (asprintf(author, "%s <%s>", name, email)
633 == -1)
634 return got_error_from_errno("asprintf");
635 return NULL;
637 /* TODO: Look up user in password database? */
638 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
642 *author = strdup(got_author);
643 if (*author == NULL)
644 return got_error_from_errno("strdup");
646 /*
647 * Really dumb email address check; we're only doing this to
648 * avoid git's object parser breaking on commits we create.
649 */
650 while (*got_author && *got_author != '<')
651 got_author++;
652 if (*got_author != '<') {
653 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
654 goto done;
656 while (*got_author && *got_author != '@')
657 got_author++;
658 if (*got_author != '@') {
659 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
660 goto done;
662 while (*got_author && *got_author != '>')
663 got_author++;
664 if (*got_author != '>')
665 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 done:
667 if (err) {
668 free(*author);
669 *author = NULL;
671 return err;
674 static const struct got_error *
675 get_gitconfig_path(char **gitconfig_path)
677 const char *homedir = getenv("HOME");
679 *gitconfig_path = NULL;
680 if (homedir) {
681 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
682 return got_error_from_errno("asprintf");
685 return NULL;
688 static const struct got_error *
689 cmd_import(int argc, char *argv[])
691 const struct got_error *error = NULL;
692 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
693 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
694 const char *branch_name = "main";
695 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
696 struct got_repository *repo = NULL;
697 struct got_reference *branch_ref = NULL, *head_ref = NULL;
698 struct got_object_id *new_commit_id = NULL;
699 int ch;
700 struct got_pathlist_head ignores;
701 struct got_pathlist_entry *pe;
702 int preserve_logmsg = 0;
704 TAILQ_INIT(&ignores);
706 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
707 switch (ch) {
708 case 'b':
709 branch_name = optarg;
710 break;
711 case 'm':
712 logmsg = strdup(optarg);
713 if (logmsg == NULL) {
714 error = got_error_from_errno("strdup");
715 goto done;
717 break;
718 case 'r':
719 repo_path = realpath(optarg, NULL);
720 if (repo_path == NULL) {
721 error = got_error_from_errno2("realpath",
722 optarg);
723 goto done;
725 break;
726 case 'I':
727 if (optarg[0] == '\0')
728 break;
729 error = got_pathlist_insert(&pe, &ignores, optarg,
730 NULL);
731 if (error)
732 goto done;
733 break;
734 default:
735 usage_import();
736 /* NOTREACHED */
740 argc -= optind;
741 argv += optind;
743 #ifndef PROFILE
744 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
745 "unveil",
746 NULL) == -1)
747 err(1, "pledge");
748 #endif
749 if (argc != 1)
750 usage_import();
752 if (repo_path == NULL) {
753 repo_path = getcwd(NULL, 0);
754 if (repo_path == NULL)
755 return got_error_from_errno("getcwd");
757 got_path_strip_trailing_slashes(repo_path);
758 error = get_gitconfig_path(&gitconfig_path);
759 if (error)
760 goto done;
761 error = got_repo_open(&repo, repo_path, gitconfig_path);
762 if (error)
763 goto done;
765 error = get_author(&author, repo, NULL);
766 if (error)
767 return error;
769 /*
770 * Don't let the user create a branch name with a leading '-'.
771 * While technically a valid reference name, this case is usually
772 * an unintended typo.
773 */
774 if (branch_name[0] == '-')
775 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
777 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
778 error = got_error_from_errno("asprintf");
779 goto done;
782 error = got_ref_open(&branch_ref, repo, refname, 0);
783 if (error) {
784 if (error->code != GOT_ERR_NOT_REF)
785 goto done;
786 } else {
787 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
788 "import target branch already exists");
789 goto done;
792 path_dir = realpath(argv[0], NULL);
793 if (path_dir == NULL) {
794 error = got_error_from_errno2("realpath", argv[0]);
795 goto done;
797 got_path_strip_trailing_slashes(path_dir);
799 /*
800 * unveil(2) traverses exec(2); if an editor is used we have
801 * to apply unveil after the log message has been written.
802 */
803 if (logmsg == NULL || strlen(logmsg) == 0) {
804 error = get_editor(&editor);
805 if (error)
806 goto done;
807 free(logmsg);
808 error = collect_import_msg(&logmsg, &logmsg_path, editor,
809 path_dir, refname);
810 if (error) {
811 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
812 logmsg_path != NULL)
813 preserve_logmsg = 1;
814 goto done;
818 if (unveil(path_dir, "r") != 0) {
819 error = got_error_from_errno2("unveil", path_dir);
820 if (logmsg_path)
821 preserve_logmsg = 1;
822 goto done;
825 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
826 if (error) {
827 if (logmsg_path)
828 preserve_logmsg = 1;
829 goto done;
832 error = got_repo_import(&new_commit_id, path_dir, logmsg,
833 author, &ignores, repo, import_progress, NULL);
834 if (error) {
835 if (logmsg_path)
836 preserve_logmsg = 1;
837 goto done;
840 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
841 if (error) {
842 if (logmsg_path)
843 preserve_logmsg = 1;
844 goto done;
847 error = got_ref_write(branch_ref, repo);
848 if (error) {
849 if (logmsg_path)
850 preserve_logmsg = 1;
851 goto done;
854 error = got_object_id_str(&id_str, new_commit_id);
855 if (error) {
856 if (logmsg_path)
857 preserve_logmsg = 1;
858 goto done;
861 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
862 if (error) {
863 if (error->code != GOT_ERR_NOT_REF) {
864 if (logmsg_path)
865 preserve_logmsg = 1;
866 goto done;
869 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
870 branch_ref);
871 if (error) {
872 if (logmsg_path)
873 preserve_logmsg = 1;
874 goto done;
877 error = got_ref_write(head_ref, repo);
878 if (error) {
879 if (logmsg_path)
880 preserve_logmsg = 1;
881 goto done;
885 printf("Created branch %s with commit %s\n",
886 got_ref_get_name(branch_ref), id_str);
887 done:
888 if (preserve_logmsg) {
889 fprintf(stderr, "%s: log message preserved in %s\n",
890 getprogname(), logmsg_path);
891 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
892 error = got_error_from_errno2("unlink", logmsg_path);
893 free(logmsg);
894 free(logmsg_path);
895 free(repo_path);
896 free(editor);
897 free(refname);
898 free(new_commit_id);
899 free(id_str);
900 free(author);
901 free(gitconfig_path);
902 if (branch_ref)
903 got_ref_close(branch_ref);
904 if (head_ref)
905 got_ref_close(head_ref);
906 return error;
909 __dead static void
910 usage_clone(void)
912 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
913 "[-R reference] repository-url [directory]\n", getprogname());
914 exit(1);
917 struct got_fetch_progress_arg {
918 char last_scaled_size[FMT_SCALED_STRSIZE];
919 int last_p_indexed;
920 int last_p_resolved;
921 int verbosity;
923 struct got_repository *repo;
925 int create_configs;
926 int configs_created;
927 struct {
928 struct got_pathlist_head *symrefs;
929 struct got_pathlist_head *wanted_branches;
930 struct got_pathlist_head *wanted_refs;
931 const char *proto;
932 const char *host;
933 const char *port;
934 const char *remote_repo_path;
935 const char *git_url;
936 int fetch_all_branches;
937 int mirror_references;
938 } config_info;
939 };
941 /* XXX forward declaration */
942 static const struct got_error *
943 create_config_files(const char *proto, const char *host, const char *port,
944 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
945 int mirror_references, struct got_pathlist_head *symrefs,
946 struct got_pathlist_head *wanted_branches,
947 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
949 static const struct got_error *
950 fetch_progress(void *arg, const char *message, off_t packfile_size,
951 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
953 const struct got_error *err = NULL;
954 struct got_fetch_progress_arg *a = arg;
955 char scaled_size[FMT_SCALED_STRSIZE];
956 int p_indexed, p_resolved;
957 int print_size = 0, print_indexed = 0, print_resolved = 0;
959 /*
960 * In order to allow a failed clone to be resumed with 'got fetch'
961 * we try to create configuration files as soon as possible.
962 * Once the server has sent information about its default branch
963 * we have all required information.
964 */
965 if (a->create_configs && !a->configs_created &&
966 !TAILQ_EMPTY(a->config_info.symrefs)) {
967 err = create_config_files(a->config_info.proto,
968 a->config_info.host, a->config_info.port,
969 a->config_info.remote_repo_path,
970 a->config_info.git_url,
971 a->config_info.fetch_all_branches,
972 a->config_info.mirror_references,
973 a->config_info.symrefs,
974 a->config_info.wanted_branches,
975 a->config_info.wanted_refs, a->repo);
976 if (err)
977 return err;
978 a->configs_created = 1;
981 if (a->verbosity < 0)
982 return NULL;
984 if (message && message[0] != '\0') {
985 printf("\rserver: %s", message);
986 fflush(stdout);
987 return NULL;
990 if (packfile_size > 0 || nobj_indexed > 0) {
991 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
992 (a->last_scaled_size[0] == '\0' ||
993 strcmp(scaled_size, a->last_scaled_size)) != 0) {
994 print_size = 1;
995 if (strlcpy(a->last_scaled_size, scaled_size,
996 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
997 return got_error(GOT_ERR_NO_SPACE);
999 if (nobj_indexed > 0) {
1000 p_indexed = (nobj_indexed * 100) / nobj_total;
1001 if (p_indexed != a->last_p_indexed) {
1002 a->last_p_indexed = p_indexed;
1003 print_indexed = 1;
1004 print_size = 1;
1007 if (nobj_resolved > 0) {
1008 p_resolved = (nobj_resolved * 100) /
1009 (nobj_total - nobj_loose);
1010 if (p_resolved != a->last_p_resolved) {
1011 a->last_p_resolved = p_resolved;
1012 print_resolved = 1;
1013 print_indexed = 1;
1014 print_size = 1;
1019 if (print_size || print_indexed || print_resolved)
1020 printf("\r");
1021 if (print_size)
1022 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1023 if (print_indexed)
1024 printf("; indexing %d%%", p_indexed);
1025 if (print_resolved)
1026 printf("; resolving deltas %d%%", p_resolved);
1027 if (print_size || print_indexed || print_resolved)
1028 fflush(stdout);
1030 return NULL;
1033 static const struct got_error *
1034 create_symref(const char *refname, struct got_reference *target_ref,
1035 int verbosity, struct got_repository *repo)
1037 const struct got_error *err;
1038 struct got_reference *head_symref;
1040 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1041 if (err)
1042 return err;
1044 err = got_ref_write(head_symref, repo);
1045 if (err == NULL && verbosity > 0) {
1046 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1047 got_ref_get_name(target_ref));
1049 got_ref_close(head_symref);
1050 return err;
1053 static const struct got_error *
1054 list_remote_refs(struct got_pathlist_head *symrefs,
1055 struct got_pathlist_head *refs)
1057 const struct got_error *err;
1058 struct got_pathlist_entry *pe;
1060 TAILQ_FOREACH(pe, symrefs, entry) {
1061 const char *refname = pe->path;
1062 const char *targetref = pe->data;
1064 printf("%s: %s\n", refname, targetref);
1067 TAILQ_FOREACH(pe, refs, entry) {
1068 const char *refname = pe->path;
1069 struct got_object_id *id = pe->data;
1070 char *id_str;
1072 err = got_object_id_str(&id_str, id);
1073 if (err)
1074 return err;
1075 printf("%s: %s\n", refname, id_str);
1076 free(id_str);
1079 return NULL;
1082 static const struct got_error *
1083 create_ref(const char *refname, struct got_object_id *id,
1084 int verbosity, struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_reference *ref;
1088 char *id_str;
1090 err = got_object_id_str(&id_str, id);
1091 if (err)
1092 return err;
1094 err = got_ref_alloc(&ref, refname, id);
1095 if (err)
1096 goto done;
1098 err = got_ref_write(ref, repo);
1099 got_ref_close(ref);
1101 if (err == NULL && verbosity >= 0)
1102 printf("Created reference %s: %s\n", refname, id_str);
1103 done:
1104 free(id_str);
1105 return err;
1108 static int
1109 match_wanted_ref(const char *refname, const char *wanted_ref)
1111 if (strncmp(refname, "refs/", 5) != 0)
1112 return 0;
1113 refname += 5;
1116 * Prevent fetching of references that won't make any
1117 * sense outside of the remote repository's context.
1119 if (strncmp(refname, "got/", 4) == 0)
1120 return 0;
1121 if (strncmp(refname, "remotes/", 8) == 0)
1122 return 0;
1124 if (strncmp(wanted_ref, "refs/", 5) == 0)
1125 wanted_ref += 5;
1127 /* Allow prefix match. */
1128 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1129 return 1;
1131 /* Allow exact match. */
1132 return (strcmp(refname, wanted_ref) == 0);
1135 static int
1136 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1138 struct got_pathlist_entry *pe;
1140 TAILQ_FOREACH(pe, wanted_refs, entry) {
1141 if (match_wanted_ref(refname, pe->path))
1142 return 1;
1145 return 0;
1148 static const struct got_error *
1149 create_wanted_ref(const char *refname, struct got_object_id *id,
1150 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1152 const struct got_error *err;
1153 char *remote_refname;
1155 if (strncmp("refs/", refname, 5) == 0)
1156 refname += 5;
1158 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1159 remote_repo_name, refname) == -1)
1160 return got_error_from_errno("asprintf");
1162 err = create_ref(remote_refname, id, verbosity, repo);
1163 free(remote_refname);
1164 return err;
1167 static const struct got_error *
1168 create_gotconfig(const char *proto, const char *host, const char *port,
1169 const char *remote_repo_path, const char *default_branch,
1170 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1171 struct got_pathlist_head *wanted_refs, int mirror_references,
1172 struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 char *gotconfig_path = NULL;
1176 char *gotconfig = NULL;
1177 FILE *gotconfig_file = NULL;
1178 const char *branchname = NULL;
1179 char *branches = NULL, *refs = NULL;
1180 ssize_t n;
1182 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1183 struct got_pathlist_entry *pe;
1184 TAILQ_FOREACH(pe, wanted_branches, entry) {
1185 char *s;
1186 branchname = pe->path;
1187 if (strncmp(branchname, "refs/heads/", 11) == 0)
1188 branchname += 11;
1189 if (asprintf(&s, "%s\"%s\" ",
1190 branches ? branches : "", branchname) == -1) {
1191 err = got_error_from_errno("asprintf");
1192 goto done;
1194 free(branches);
1195 branches = s;
1197 } else if (!fetch_all_branches && default_branch) {
1198 branchname = default_branch;
1199 if (strncmp(branchname, "refs/heads/", 11) == 0)
1200 branchname += 11;
1201 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1202 err = got_error_from_errno("asprintf");
1203 goto done;
1206 if (!TAILQ_EMPTY(wanted_refs)) {
1207 struct got_pathlist_entry *pe;
1208 TAILQ_FOREACH(pe, wanted_refs, entry) {
1209 char *s;
1210 const char *refname = pe->path;
1211 if (strncmp(refname, "refs/", 5) == 0)
1212 branchname += 5;
1213 if (asprintf(&s, "%s\"%s\" ",
1214 refs ? refs : "", refname) == -1) {
1215 err = got_error_from_errno("asprintf");
1216 goto done;
1218 free(refs);
1219 refs = s;
1223 /* Create got.conf(5). */
1224 gotconfig_path = got_repo_get_path_gotconfig(repo);
1225 if (gotconfig_path == NULL) {
1226 err = got_error_from_errno("got_repo_get_path_gotconfig");
1227 goto done;
1229 gotconfig_file = fopen(gotconfig_path, "a");
1230 if (gotconfig_file == NULL) {
1231 err = got_error_from_errno2("fopen", gotconfig_path);
1232 goto done;
1234 if (asprintf(&gotconfig,
1235 "remote \"%s\" {\n"
1236 "\tserver %s\n"
1237 "\tprotocol %s\n"
1238 "%s%s%s"
1239 "\trepository \"%s\"\n"
1240 "%s%s%s"
1241 "%s%s%s"
1242 "%s"
1243 "%s"
1244 "}\n",
1245 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1246 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1247 remote_repo_path, branches ? "\tbranch { " : "",
1248 branches ? branches : "", branches ? "}\n" : "",
1249 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1250 mirror_references ? "\tmirror-references yes\n" : "",
1251 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1252 err = got_error_from_errno("asprintf");
1253 goto done;
1255 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1256 if (n != strlen(gotconfig)) {
1257 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1258 goto done;
1261 done:
1262 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1263 err = got_error_from_errno2("fclose", gotconfig_path);
1264 free(gotconfig_path);
1265 free(branches);
1266 return err;
1269 static const struct got_error *
1270 create_gitconfig(const char *git_url, const char *default_branch,
1271 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1272 struct got_pathlist_head *wanted_refs, int mirror_references,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 char *gitconfig_path = NULL;
1277 char *gitconfig = NULL;
1278 FILE *gitconfig_file = NULL;
1279 char *branches = NULL, *refs = NULL;
1280 const char *branchname;
1281 ssize_t n;
1283 /* Create a config file Git can understand. */
1284 gitconfig_path = got_repo_get_path_gitconfig(repo);
1285 if (gitconfig_path == NULL) {
1286 err = got_error_from_errno("got_repo_get_path_gitconfig");
1287 goto done;
1289 gitconfig_file = fopen(gitconfig_path, "a");
1290 if (gitconfig_file == NULL) {
1291 err = got_error_from_errno2("fopen", gitconfig_path);
1292 goto done;
1294 if (fetch_all_branches) {
1295 if (mirror_references) {
1296 if (asprintf(&branches,
1297 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1298 err = got_error_from_errno("asprintf");
1299 goto done;
1301 } else if (asprintf(&branches,
1302 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1303 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1304 err = got_error_from_errno("asprintf");
1305 goto done;
1307 } else if (!TAILQ_EMPTY(wanted_branches)) {
1308 struct got_pathlist_entry *pe;
1309 TAILQ_FOREACH(pe, wanted_branches, entry) {
1310 char *s;
1311 branchname = pe->path;
1312 if (strncmp(branchname, "refs/heads/", 11) == 0)
1313 branchname += 11;
1314 if (mirror_references) {
1315 if (asprintf(&s,
1316 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1317 branches ? branches : "",
1318 branchname, branchname) == -1) {
1319 err = got_error_from_errno("asprintf");
1320 goto done;
1322 } else if (asprintf(&s,
1323 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1324 branches ? branches : "",
1325 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1326 branchname) == -1) {
1327 err = got_error_from_errno("asprintf");
1328 goto done;
1330 free(branches);
1331 branches = s;
1333 } else {
1335 * If the server specified a default branch, use just that one.
1336 * Otherwise fall back to fetching all branches on next fetch.
1338 if (default_branch) {
1339 branchname = default_branch;
1340 if (strncmp(branchname, "refs/heads/", 11) == 0)
1341 branchname += 11;
1342 } else
1343 branchname = "*"; /* fall back to all branches */
1344 if (mirror_references) {
1345 if (asprintf(&branches,
1346 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1347 branchname, branchname) == -1) {
1348 err = got_error_from_errno("asprintf");
1349 goto done;
1351 } else if (asprintf(&branches,
1352 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1353 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1354 branchname) == -1) {
1355 err = got_error_from_errno("asprintf");
1356 goto done;
1359 if (!TAILQ_EMPTY(wanted_refs)) {
1360 struct got_pathlist_entry *pe;
1361 TAILQ_FOREACH(pe, wanted_refs, entry) {
1362 char *s;
1363 const char *refname = pe->path;
1364 if (strncmp(refname, "refs/", 5) == 0)
1365 refname += 5;
1366 if (mirror_references) {
1367 if (asprintf(&s,
1368 "%s\tfetch = refs/%s:refs/%s\n",
1369 refs ? refs : "", refname, refname) == -1) {
1370 err = got_error_from_errno("asprintf");
1371 goto done;
1373 } else if (asprintf(&s,
1374 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1375 refs ? refs : "",
1376 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1377 refname) == -1) {
1378 err = got_error_from_errno("asprintf");
1379 goto done;
1381 free(refs);
1382 refs = s;
1386 if (asprintf(&gitconfig,
1387 "[remote \"%s\"]\n"
1388 "\turl = %s\n"
1389 "%s"
1390 "%s"
1391 "\tfetch = refs/tags/*:refs/tags/*\n",
1392 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1393 refs ? refs : "") == -1) {
1394 err = got_error_from_errno("asprintf");
1395 goto done;
1397 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1398 if (n != strlen(gitconfig)) {
1399 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1400 goto done;
1402 done:
1403 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1404 err = got_error_from_errno2("fclose", gitconfig_path);
1405 free(gitconfig_path);
1406 free(branches);
1407 return err;
1410 static const struct got_error *
1411 create_config_files(const char *proto, const char *host, const char *port,
1412 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1413 int mirror_references, struct got_pathlist_head *symrefs,
1414 struct got_pathlist_head *wanted_branches,
1415 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 const char *default_branch = NULL;
1419 struct got_pathlist_entry *pe;
1422 * If we asked for a set of wanted branches then use the first
1423 * one of those.
1425 if (!TAILQ_EMPTY(wanted_branches)) {
1426 pe = TAILQ_FIRST(wanted_branches);
1427 default_branch = pe->path;
1428 } else {
1429 /* First HEAD ref listed by server is the default branch. */
1430 TAILQ_FOREACH(pe, symrefs, entry) {
1431 const char *refname = pe->path;
1432 const char *target = pe->data;
1434 if (strcmp(refname, GOT_REF_HEAD) != 0)
1435 continue;
1437 default_branch = target;
1438 break;
1442 /* Create got.conf(5). */
1443 err = create_gotconfig(proto, host, port, remote_repo_path,
1444 default_branch, fetch_all_branches, wanted_branches,
1445 wanted_refs, mirror_references, repo);
1446 if (err)
1447 return err;
1449 /* Create a config file Git can understand. */
1450 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1451 wanted_branches, wanted_refs, mirror_references, repo);
1454 static const struct got_error *
1455 cmd_clone(int argc, char *argv[])
1457 const struct got_error *error = NULL;
1458 const char *uri, *dirname;
1459 char *proto, *host, *port, *repo_name, *server_path;
1460 char *default_destdir = NULL, *id_str = NULL;
1461 const char *repo_path;
1462 struct got_repository *repo = NULL;
1463 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1464 struct got_pathlist_entry *pe;
1465 struct got_object_id *pack_hash = NULL;
1466 int ch, fetchfd = -1, fetchstatus;
1467 pid_t fetchpid = -1;
1468 struct got_fetch_progress_arg fpa;
1469 char *git_url = NULL;
1470 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1471 int list_refs_only = 0;
1473 TAILQ_INIT(&refs);
1474 TAILQ_INIT(&symrefs);
1475 TAILQ_INIT(&wanted_branches);
1476 TAILQ_INIT(&wanted_refs);
1478 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1479 switch (ch) {
1480 case 'a':
1481 fetch_all_branches = 1;
1482 break;
1483 case 'b':
1484 error = got_pathlist_append(&wanted_branches,
1485 optarg, NULL);
1486 if (error)
1487 return error;
1488 break;
1489 case 'l':
1490 list_refs_only = 1;
1491 break;
1492 case 'm':
1493 mirror_references = 1;
1494 break;
1495 case 'v':
1496 if (verbosity < 0)
1497 verbosity = 0;
1498 else if (verbosity < 3)
1499 verbosity++;
1500 break;
1501 case 'q':
1502 verbosity = -1;
1503 break;
1504 case 'R':
1505 error = got_pathlist_append(&wanted_refs,
1506 optarg, NULL);
1507 if (error)
1508 return error;
1509 break;
1510 default:
1511 usage_clone();
1512 break;
1515 argc -= optind;
1516 argv += optind;
1518 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1519 option_conflict('a', 'b');
1520 if (list_refs_only) {
1521 if (!TAILQ_EMPTY(&wanted_branches))
1522 option_conflict('l', 'b');
1523 if (fetch_all_branches)
1524 option_conflict('l', 'a');
1525 if (mirror_references)
1526 option_conflict('l', 'm');
1527 if (!TAILQ_EMPTY(&wanted_refs))
1528 option_conflict('l', 'R');
1531 uri = argv[0];
1533 if (argc == 1)
1534 dirname = NULL;
1535 else if (argc == 2)
1536 dirname = argv[1];
1537 else
1538 usage_clone();
1540 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1541 &repo_name, uri);
1542 if (error)
1543 goto done;
1545 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1546 host, port ? ":" : "", port ? port : "",
1547 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1548 error = got_error_from_errno("asprintf");
1549 goto done;
1552 if (strcmp(proto, "git") == 0) {
1553 #ifndef PROFILE
1554 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1555 "sendfd dns inet unveil", NULL) == -1)
1556 err(1, "pledge");
1557 #endif
1558 } else if (strcmp(proto, "git+ssh") == 0 ||
1559 strcmp(proto, "ssh") == 0) {
1560 #ifndef PROFILE
1561 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 "sendfd unveil", NULL) == -1)
1563 err(1, "pledge");
1564 #endif
1565 } else if (strcmp(proto, "http") == 0 ||
1566 strcmp(proto, "git+http") == 0) {
1567 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1568 goto done;
1569 } else {
1570 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1571 goto done;
1573 if (dirname == NULL) {
1574 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1575 error = got_error_from_errno("asprintf");
1576 goto done;
1578 repo_path = default_destdir;
1579 } else
1580 repo_path = dirname;
1582 if (!list_refs_only) {
1583 error = got_path_mkdir(repo_path);
1584 if (error &&
1585 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1586 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1587 goto done;
1588 if (!got_path_dir_is_empty(repo_path)) {
1589 error = got_error_path(repo_path,
1590 GOT_ERR_DIR_NOT_EMPTY);
1591 goto done;
1595 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1596 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1597 error = got_error_from_errno2("unveil",
1598 GOT_FETCH_PATH_SSH);
1599 goto done;
1602 error = apply_unveil(repo_path, 0, NULL);
1603 if (error)
1604 goto done;
1606 if (verbosity >= 0)
1607 printf("Connecting to %s%s%s\n", host,
1608 port ? ":" : "", port ? port : "");
1610 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1611 server_path, verbosity);
1612 if (error)
1613 goto done;
1615 if (!list_refs_only) {
1616 error = got_repo_init(repo_path);
1617 if (error)
1618 goto done;
1619 error = got_repo_open(&repo, repo_path, NULL);
1620 if (error)
1621 goto done;
1624 fpa.last_scaled_size[0] = '\0';
1625 fpa.last_p_indexed = -1;
1626 fpa.last_p_resolved = -1;
1627 fpa.verbosity = verbosity;
1628 fpa.create_configs = 1;
1629 fpa.configs_created = 0;
1630 fpa.repo = repo;
1631 fpa.config_info.symrefs = &symrefs;
1632 fpa.config_info.wanted_branches = &wanted_branches;
1633 fpa.config_info.wanted_refs = &wanted_refs;
1634 fpa.config_info.proto = proto;
1635 fpa.config_info.host = host;
1636 fpa.config_info.port = port;
1637 fpa.config_info.remote_repo_path = server_path;
1638 fpa.config_info.git_url = git_url;
1639 fpa.config_info.fetch_all_branches = fetch_all_branches;
1640 fpa.config_info.mirror_references = mirror_references;
1641 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1642 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1643 fetch_all_branches, &wanted_branches, &wanted_refs,
1644 list_refs_only, verbosity, fetchfd, repo,
1645 fetch_progress, &fpa);
1646 if (error)
1647 goto done;
1649 if (list_refs_only) {
1650 error = list_remote_refs(&symrefs, &refs);
1651 goto done;
1654 error = got_object_id_str(&id_str, pack_hash);
1655 if (error)
1656 goto done;
1657 if (verbosity >= 0)
1658 printf("\nFetched %s.pack\n", id_str);
1659 free(id_str);
1661 /* Set up references provided with the pack file. */
1662 TAILQ_FOREACH(pe, &refs, entry) {
1663 const char *refname = pe->path;
1664 struct got_object_id *id = pe->data;
1665 char *remote_refname;
1667 if (is_wanted_ref(&wanted_refs, refname) &&
1668 !mirror_references) {
1669 error = create_wanted_ref(refname, id,
1670 GOT_FETCH_DEFAULT_REMOTE_NAME,
1671 verbosity - 1, repo);
1672 if (error)
1673 goto done;
1674 continue;
1677 error = create_ref(refname, id, verbosity - 1, repo);
1678 if (error)
1679 goto done;
1681 if (mirror_references)
1682 continue;
1684 if (strncmp("refs/heads/", refname, 11) != 0)
1685 continue;
1687 if (asprintf(&remote_refname,
1688 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1689 refname + 11) == -1) {
1690 error = got_error_from_errno("asprintf");
1691 goto done;
1693 error = create_ref(remote_refname, id, verbosity - 1, repo);
1694 free(remote_refname);
1695 if (error)
1696 goto done;
1699 /* Set the HEAD reference if the server provided one. */
1700 TAILQ_FOREACH(pe, &symrefs, entry) {
1701 struct got_reference *target_ref;
1702 const char *refname = pe->path;
1703 const char *target = pe->data;
1704 char *remote_refname = NULL, *remote_target = NULL;
1706 if (strcmp(refname, GOT_REF_HEAD) != 0)
1707 continue;
1709 error = got_ref_open(&target_ref, repo, target, 0);
1710 if (error) {
1711 if (error->code == GOT_ERR_NOT_REF) {
1712 error = NULL;
1713 continue;
1715 goto done;
1718 error = create_symref(refname, target_ref, verbosity, repo);
1719 got_ref_close(target_ref);
1720 if (error)
1721 goto done;
1723 if (mirror_references)
1724 continue;
1726 if (strncmp("refs/heads/", target, 11) != 0)
1727 continue;
1729 if (asprintf(&remote_refname,
1730 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1731 refname) == -1) {
1732 error = got_error_from_errno("asprintf");
1733 goto done;
1735 if (asprintf(&remote_target,
1736 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1737 target + 11) == -1) {
1738 error = got_error_from_errno("asprintf");
1739 free(remote_refname);
1740 goto done;
1742 error = got_ref_open(&target_ref, repo, remote_target, 0);
1743 if (error) {
1744 free(remote_refname);
1745 free(remote_target);
1746 if (error->code == GOT_ERR_NOT_REF) {
1747 error = NULL;
1748 continue;
1750 goto done;
1752 error = create_symref(remote_refname, target_ref,
1753 verbosity - 1, repo);
1754 free(remote_refname);
1755 free(remote_target);
1756 got_ref_close(target_ref);
1757 if (error)
1758 goto done;
1760 if (pe == NULL) {
1762 * We failed to set the HEAD reference. If we asked for
1763 * a set of wanted branches use the first of one of those
1764 * which could be fetched instead.
1766 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1767 const char *target = pe->path;
1768 struct got_reference *target_ref;
1770 error = got_ref_open(&target_ref, repo, target, 0);
1771 if (error) {
1772 if (error->code == GOT_ERR_NOT_REF) {
1773 error = NULL;
1774 continue;
1776 goto done;
1779 error = create_symref(GOT_REF_HEAD, target_ref,
1780 verbosity, repo);
1781 got_ref_close(target_ref);
1782 if (error)
1783 goto done;
1784 break;
1788 if (verbosity >= 0)
1789 printf("Created %s repository '%s'\n",
1790 mirror_references ? "mirrored" : "cloned", repo_path);
1791 done:
1792 if (fetchpid > 0) {
1793 if (kill(fetchpid, SIGTERM) == -1)
1794 error = got_error_from_errno("kill");
1795 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1796 error = got_error_from_errno("waitpid");
1798 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1799 error = got_error_from_errno("close");
1800 if (repo)
1801 got_repo_close(repo);
1802 TAILQ_FOREACH(pe, &refs, entry) {
1803 free((void *)pe->path);
1804 free(pe->data);
1806 got_pathlist_free(&refs);
1807 TAILQ_FOREACH(pe, &symrefs, entry) {
1808 free((void *)pe->path);
1809 free(pe->data);
1811 got_pathlist_free(&symrefs);
1812 got_pathlist_free(&wanted_branches);
1813 got_pathlist_free(&wanted_refs);
1814 free(pack_hash);
1815 free(proto);
1816 free(host);
1817 free(port);
1818 free(server_path);
1819 free(repo_name);
1820 free(default_destdir);
1821 free(git_url);
1822 return error;
1825 static const struct got_error *
1826 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1827 int replace_tags, int verbosity, struct got_repository *repo)
1829 const struct got_error *err = NULL;
1830 char *new_id_str = NULL;
1831 struct got_object_id *old_id = NULL;
1833 err = got_object_id_str(&new_id_str, new_id);
1834 if (err)
1835 goto done;
1837 if (!replace_tags &&
1838 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1839 err = got_ref_resolve(&old_id, repo, ref);
1840 if (err)
1841 goto done;
1842 if (got_object_id_cmp(old_id, new_id) == 0)
1843 goto done;
1844 if (verbosity >= 0) {
1845 printf("Rejecting update of existing tag %s: %s\n",
1846 got_ref_get_name(ref), new_id_str);
1848 goto done;
1851 if (got_ref_is_symbolic(ref)) {
1852 if (verbosity >= 0) {
1853 printf("Replacing reference %s: %s\n",
1854 got_ref_get_name(ref),
1855 got_ref_get_symref_target(ref));
1857 err = got_ref_change_symref_to_ref(ref, new_id);
1858 if (err)
1859 goto done;
1860 err = got_ref_write(ref, repo);
1861 if (err)
1862 goto done;
1863 } else {
1864 err = got_ref_resolve(&old_id, repo, ref);
1865 if (err)
1866 goto done;
1867 if (got_object_id_cmp(old_id, new_id) == 0)
1868 goto done;
1870 err = got_ref_change_ref(ref, new_id);
1871 if (err)
1872 goto done;
1873 err = got_ref_write(ref, repo);
1874 if (err)
1875 goto done;
1878 if (verbosity >= 0)
1879 printf("Updated %s: %s\n", got_ref_get_name(ref),
1880 new_id_str);
1881 done:
1882 free(old_id);
1883 free(new_id_str);
1884 return err;
1887 static const struct got_error *
1888 update_symref(const char *refname, struct got_reference *target_ref,
1889 int verbosity, struct got_repository *repo)
1891 const struct got_error *err = NULL, *unlock_err;
1892 struct got_reference *symref;
1893 int symref_is_locked = 0;
1895 err = got_ref_open(&symref, repo, refname, 1);
1896 if (err) {
1897 if (err->code != GOT_ERR_NOT_REF)
1898 return err;
1899 err = got_ref_alloc_symref(&symref, refname, target_ref);
1900 if (err)
1901 goto done;
1903 err = got_ref_write(symref, repo);
1904 if (err)
1905 goto done;
1907 if (verbosity >= 0)
1908 printf("Created reference %s: %s\n",
1909 got_ref_get_name(symref),
1910 got_ref_get_symref_target(symref));
1911 } else {
1912 symref_is_locked = 1;
1914 if (strcmp(got_ref_get_symref_target(symref),
1915 got_ref_get_name(target_ref)) == 0)
1916 goto done;
1918 err = got_ref_change_symref(symref,
1919 got_ref_get_name(target_ref));
1920 if (err)
1921 goto done;
1923 err = got_ref_write(symref, repo);
1924 if (err)
1925 goto done;
1927 if (verbosity >= 0)
1928 printf("Updated %s: %s\n", got_ref_get_name(symref),
1929 got_ref_get_symref_target(symref));
1932 done:
1933 if (symref_is_locked) {
1934 unlock_err = got_ref_unlock(symref);
1935 if (unlock_err && err == NULL)
1936 err = unlock_err;
1938 got_ref_close(symref);
1939 return err;
1942 __dead static void
1943 usage_fetch(void)
1945 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1946 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1947 "[remote-repository-name]\n",
1948 getprogname());
1949 exit(1);
1952 static const struct got_error *
1953 delete_missing_ref(struct got_reference *ref,
1954 int verbosity, struct got_repository *repo)
1956 const struct got_error *err = NULL;
1957 struct got_object_id *id = NULL;
1958 char *id_str = NULL;
1960 if (got_ref_is_symbolic(ref)) {
1961 err = got_ref_delete(ref, repo);
1962 if (err)
1963 return err;
1964 if (verbosity >= 0) {
1965 printf("Deleted reference %s: %s\n",
1966 got_ref_get_name(ref),
1967 got_ref_get_symref_target(ref));
1969 } else {
1970 err = got_ref_resolve(&id, repo, ref);
1971 if (err)
1972 return err;
1973 err = got_object_id_str(&id_str, id);
1974 if (err)
1975 goto done;
1977 err = got_ref_delete(ref, repo);
1978 if (err)
1979 goto done;
1980 if (verbosity >= 0) {
1981 printf("Deleted reference %s: %s\n",
1982 got_ref_get_name(ref), id_str);
1985 done:
1986 free(id);
1987 free(id_str);
1988 return NULL;
1991 static const struct got_error *
1992 delete_missing_refs(struct got_pathlist_head *their_refs,
1993 struct got_pathlist_head *their_symrefs,
1994 const struct got_remote_repo *remote,
1995 int verbosity, struct got_repository *repo)
1997 const struct got_error *err = NULL, *unlock_err;
1998 struct got_reflist_head my_refs;
1999 struct got_reflist_entry *re;
2000 struct got_pathlist_entry *pe;
2001 char *remote_namespace = NULL;
2002 char *local_refname = NULL;
2004 TAILQ_INIT(&my_refs);
2006 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2007 == -1)
2008 return got_error_from_errno("asprintf");
2010 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2011 if (err)
2012 goto done;
2014 TAILQ_FOREACH(re, &my_refs, entry) {
2015 const char *refname = got_ref_get_name(re->ref);
2017 if (!remote->mirror_references) {
2018 if (strncmp(refname, remote_namespace,
2019 strlen(remote_namespace)) == 0) {
2020 if (strcmp(refname + strlen(remote_namespace),
2021 GOT_REF_HEAD) == 0)
2022 continue;
2023 if (asprintf(&local_refname, "refs/heads/%s",
2024 refname + strlen(remote_namespace)) == -1) {
2025 err = got_error_from_errno("asprintf");
2026 goto done;
2028 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2029 continue;
2032 TAILQ_FOREACH(pe, their_refs, entry) {
2033 if (strcmp(local_refname, pe->path) == 0)
2034 break;
2036 if (pe != NULL)
2037 continue;
2039 TAILQ_FOREACH(pe, their_symrefs, entry) {
2040 if (strcmp(local_refname, pe->path) == 0)
2041 break;
2043 if (pe != NULL)
2044 continue;
2046 err = delete_missing_ref(re->ref, verbosity, repo);
2047 if (err)
2048 break;
2050 if (local_refname) {
2051 struct got_reference *ref;
2052 err = got_ref_open(&ref, repo, local_refname, 1);
2053 if (err) {
2054 if (err->code != GOT_ERR_NOT_REF)
2055 break;
2056 free(local_refname);
2057 local_refname = NULL;
2058 continue;
2060 err = delete_missing_ref(ref, verbosity, repo);
2061 if (err)
2062 break;
2063 unlock_err = got_ref_unlock(ref);
2064 got_ref_close(ref);
2065 if (unlock_err && err == NULL) {
2066 err = unlock_err;
2067 break;
2070 free(local_refname);
2071 local_refname = NULL;
2074 done:
2075 free(remote_namespace);
2076 free(local_refname);
2077 return err;
2080 static const struct got_error *
2081 update_wanted_ref(const char *refname, struct got_object_id *id,
2082 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2084 const struct got_error *err, *unlock_err;
2085 char *remote_refname;
2086 struct got_reference *ref;
2088 if (strncmp("refs/", refname, 5) == 0)
2089 refname += 5;
2091 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2092 remote_repo_name, refname) == -1)
2093 return got_error_from_errno("asprintf");
2095 err = got_ref_open(&ref, repo, remote_refname, 1);
2096 if (err) {
2097 if (err->code != GOT_ERR_NOT_REF)
2098 goto done;
2099 err = create_ref(remote_refname, id, verbosity, repo);
2100 } else {
2101 err = update_ref(ref, id, 0, verbosity, repo);
2102 unlock_err = got_ref_unlock(ref);
2103 if (unlock_err && err == NULL)
2104 err = unlock_err;
2105 got_ref_close(ref);
2107 done:
2108 free(remote_refname);
2109 return err;
2112 static const struct got_error *
2113 cmd_fetch(int argc, char *argv[])
2115 const struct got_error *error = NULL, *unlock_err;
2116 char *cwd = NULL, *repo_path = NULL;
2117 const char *remote_name;
2118 char *proto = NULL, *host = NULL, *port = NULL;
2119 char *repo_name = NULL, *server_path = NULL;
2120 const struct got_remote_repo *remotes, *remote = NULL;
2121 int nremotes;
2122 char *id_str = NULL;
2123 struct got_repository *repo = NULL;
2124 struct got_worktree *worktree = NULL;
2125 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2126 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2127 struct got_pathlist_entry *pe;
2128 struct got_object_id *pack_hash = NULL;
2129 int i, ch, fetchfd = -1, fetchstatus;
2130 pid_t fetchpid = -1;
2131 struct got_fetch_progress_arg fpa;
2132 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2133 int delete_refs = 0, replace_tags = 0;
2135 TAILQ_INIT(&refs);
2136 TAILQ_INIT(&symrefs);
2137 TAILQ_INIT(&wanted_branches);
2138 TAILQ_INIT(&wanted_refs);
2140 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2141 switch (ch) {
2142 case 'a':
2143 fetch_all_branches = 1;
2144 break;
2145 case 'b':
2146 error = got_pathlist_append(&wanted_branches,
2147 optarg, NULL);
2148 if (error)
2149 return error;
2150 break;
2151 case 'd':
2152 delete_refs = 1;
2153 break;
2154 case 'l':
2155 list_refs_only = 1;
2156 break;
2157 case 'r':
2158 repo_path = realpath(optarg, NULL);
2159 if (repo_path == NULL)
2160 return got_error_from_errno2("realpath",
2161 optarg);
2162 got_path_strip_trailing_slashes(repo_path);
2163 break;
2164 case 't':
2165 replace_tags = 1;
2166 break;
2167 case 'v':
2168 if (verbosity < 0)
2169 verbosity = 0;
2170 else if (verbosity < 3)
2171 verbosity++;
2172 break;
2173 case 'q':
2174 verbosity = -1;
2175 break;
2176 case 'R':
2177 error = got_pathlist_append(&wanted_refs,
2178 optarg, NULL);
2179 if (error)
2180 return error;
2181 break;
2182 default:
2183 usage_fetch();
2184 break;
2187 argc -= optind;
2188 argv += optind;
2190 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2191 option_conflict('a', 'b');
2192 if (list_refs_only) {
2193 if (!TAILQ_EMPTY(&wanted_branches))
2194 option_conflict('l', 'b');
2195 if (fetch_all_branches)
2196 option_conflict('l', 'a');
2197 if (delete_refs)
2198 option_conflict('l', 'd');
2201 if (argc == 0)
2202 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2203 else if (argc == 1)
2204 remote_name = argv[0];
2205 else
2206 usage_fetch();
2208 cwd = getcwd(NULL, 0);
2209 if (cwd == NULL) {
2210 error = got_error_from_errno("getcwd");
2211 goto done;
2214 if (repo_path == NULL) {
2215 error = got_worktree_open(&worktree, cwd);
2216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2217 goto done;
2218 else
2219 error = NULL;
2220 if (worktree) {
2221 repo_path =
2222 strdup(got_worktree_get_repo_path(worktree));
2223 if (repo_path == NULL)
2224 error = got_error_from_errno("strdup");
2225 if (error)
2226 goto done;
2227 } else {
2228 repo_path = strdup(cwd);
2229 if (repo_path == NULL) {
2230 error = got_error_from_errno("strdup");
2231 goto done;
2236 error = got_repo_open(&repo, repo_path, NULL);
2237 if (error)
2238 goto done;
2240 if (worktree) {
2241 worktree_conf = got_worktree_get_gotconfig(worktree);
2242 if (worktree_conf) {
2243 got_gotconfig_get_remotes(&nremotes, &remotes,
2244 worktree_conf);
2245 for (i = 0; i < nremotes; i++) {
2246 if (strcmp(remotes[i].name, remote_name) == 0) {
2247 remote = &remotes[i];
2248 break;
2253 if (remote == NULL) {
2254 repo_conf = got_repo_get_gotconfig(repo);
2255 if (repo_conf) {
2256 got_gotconfig_get_remotes(&nremotes, &remotes,
2257 repo_conf);
2258 for (i = 0; i < nremotes; i++) {
2259 if (strcmp(remotes[i].name, remote_name) == 0) {
2260 remote = &remotes[i];
2261 break;
2266 if (remote == NULL) {
2267 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2268 for (i = 0; i < nremotes; i++) {
2269 if (strcmp(remotes[i].name, remote_name) == 0) {
2270 remote = &remotes[i];
2271 break;
2275 if (remote == NULL) {
2276 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2277 goto done;
2280 if (TAILQ_EMPTY(&wanted_branches)) {
2281 if (!fetch_all_branches)
2282 fetch_all_branches = remote->fetch_all_branches;
2283 for (i = 0; i < remote->nbranches; i++) {
2284 got_pathlist_append(&wanted_branches,
2285 remote->branches[i], NULL);
2288 if (TAILQ_EMPTY(&wanted_refs)) {
2289 for (i = 0; i < remote->nrefs; i++) {
2290 got_pathlist_append(&wanted_refs,
2291 remote->refs[i], NULL);
2295 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2296 &repo_name, remote->url);
2297 if (error)
2298 goto done;
2300 if (strcmp(proto, "git") == 0) {
2301 #ifndef PROFILE
2302 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2303 "sendfd dns inet unveil", NULL) == -1)
2304 err(1, "pledge");
2305 #endif
2306 } else if (strcmp(proto, "git+ssh") == 0 ||
2307 strcmp(proto, "ssh") == 0) {
2308 #ifndef PROFILE
2309 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2310 "sendfd unveil", NULL) == -1)
2311 err(1, "pledge");
2312 #endif
2313 } else if (strcmp(proto, "http") == 0 ||
2314 strcmp(proto, "git+http") == 0) {
2315 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2316 goto done;
2317 } else {
2318 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2319 goto done;
2322 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2323 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2324 error = got_error_from_errno2("unveil",
2325 GOT_FETCH_PATH_SSH);
2326 goto done;
2329 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2330 if (error)
2331 goto done;
2333 if (verbosity >= 0)
2334 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2335 port ? ":" : "", port ? port : "");
2337 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2338 server_path, verbosity);
2339 if (error)
2340 goto done;
2342 fpa.last_scaled_size[0] = '\0';
2343 fpa.last_p_indexed = -1;
2344 fpa.last_p_resolved = -1;
2345 fpa.verbosity = verbosity;
2346 fpa.repo = repo;
2347 fpa.create_configs = 0;
2348 fpa.configs_created = 0;
2349 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2350 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2351 remote->mirror_references, fetch_all_branches, &wanted_branches,
2352 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2353 fetch_progress, &fpa);
2354 if (error)
2355 goto done;
2357 if (list_refs_only) {
2358 error = list_remote_refs(&symrefs, &refs);
2359 goto done;
2362 if (pack_hash == NULL) {
2363 if (verbosity >= 0)
2364 printf("Already up-to-date\n");
2365 } else if (verbosity >= 0) {
2366 error = got_object_id_str(&id_str, pack_hash);
2367 if (error)
2368 goto done;
2369 printf("\nFetched %s.pack\n", id_str);
2370 free(id_str);
2371 id_str = NULL;
2374 /* Update references provided with the pack file. */
2375 TAILQ_FOREACH(pe, &refs, entry) {
2376 const char *refname = pe->path;
2377 struct got_object_id *id = pe->data;
2378 struct got_reference *ref;
2379 char *remote_refname;
2381 if (is_wanted_ref(&wanted_refs, refname) &&
2382 !remote->mirror_references) {
2383 error = update_wanted_ref(refname, id,
2384 remote->name, verbosity, repo);
2385 if (error)
2386 goto done;
2387 continue;
2390 if (remote->mirror_references ||
2391 strncmp("refs/tags/", refname, 10) == 0) {
2392 error = got_ref_open(&ref, repo, refname, 1);
2393 if (error) {
2394 if (error->code != GOT_ERR_NOT_REF)
2395 goto done;
2396 error = create_ref(refname, id, verbosity,
2397 repo);
2398 if (error)
2399 goto done;
2400 } else {
2401 error = update_ref(ref, id, replace_tags,
2402 verbosity, repo);
2403 unlock_err = got_ref_unlock(ref);
2404 if (unlock_err && error == NULL)
2405 error = unlock_err;
2406 got_ref_close(ref);
2407 if (error)
2408 goto done;
2410 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2411 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2412 remote_name, refname + 11) == -1) {
2413 error = got_error_from_errno("asprintf");
2414 goto done;
2417 error = got_ref_open(&ref, repo, remote_refname, 1);
2418 if (error) {
2419 if (error->code != GOT_ERR_NOT_REF)
2420 goto done;
2421 error = create_ref(remote_refname, id,
2422 verbosity, repo);
2423 if (error)
2424 goto done;
2425 } else {
2426 error = update_ref(ref, id, replace_tags,
2427 verbosity, repo);
2428 unlock_err = got_ref_unlock(ref);
2429 if (unlock_err && error == NULL)
2430 error = unlock_err;
2431 got_ref_close(ref);
2432 if (error)
2433 goto done;
2436 /* Also create a local branch if none exists yet. */
2437 error = got_ref_open(&ref, repo, refname, 1);
2438 if (error) {
2439 if (error->code != GOT_ERR_NOT_REF)
2440 goto done;
2441 error = create_ref(refname, id, verbosity,
2442 repo);
2443 if (error)
2444 goto done;
2445 } else {
2446 unlock_err = got_ref_unlock(ref);
2447 if (unlock_err && error == NULL)
2448 error = unlock_err;
2449 got_ref_close(ref);
2453 if (delete_refs) {
2454 error = delete_missing_refs(&refs, &symrefs, remote,
2455 verbosity, repo);
2456 if (error)
2457 goto done;
2460 if (!remote->mirror_references) {
2461 /* Update remote HEAD reference if the server provided one. */
2462 TAILQ_FOREACH(pe, &symrefs, entry) {
2463 struct got_reference *target_ref;
2464 const char *refname = pe->path;
2465 const char *target = pe->data;
2466 char *remote_refname = NULL, *remote_target = NULL;
2468 if (strcmp(refname, GOT_REF_HEAD) != 0)
2469 continue;
2471 if (strncmp("refs/heads/", target, 11) != 0)
2472 continue;
2474 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2475 remote->name, refname) == -1) {
2476 error = got_error_from_errno("asprintf");
2477 goto done;
2479 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2480 remote->name, target + 11) == -1) {
2481 error = got_error_from_errno("asprintf");
2482 free(remote_refname);
2483 goto done;
2486 error = got_ref_open(&target_ref, repo, remote_target,
2487 0);
2488 if (error) {
2489 free(remote_refname);
2490 free(remote_target);
2491 if (error->code == GOT_ERR_NOT_REF) {
2492 error = NULL;
2493 continue;
2495 goto done;
2497 error = update_symref(remote_refname, target_ref,
2498 verbosity, repo);
2499 free(remote_refname);
2500 free(remote_target);
2501 got_ref_close(target_ref);
2502 if (error)
2503 goto done;
2506 done:
2507 if (fetchpid > 0) {
2508 if (kill(fetchpid, SIGTERM) == -1)
2509 error = got_error_from_errno("kill");
2510 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2511 error = got_error_from_errno("waitpid");
2513 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2514 error = got_error_from_errno("close");
2515 if (repo)
2516 got_repo_close(repo);
2517 if (worktree)
2518 got_worktree_close(worktree);
2519 TAILQ_FOREACH(pe, &refs, entry) {
2520 free((void *)pe->path);
2521 free(pe->data);
2523 got_pathlist_free(&refs);
2524 TAILQ_FOREACH(pe, &symrefs, entry) {
2525 free((void *)pe->path);
2526 free(pe->data);
2528 got_pathlist_free(&symrefs);
2529 got_pathlist_free(&wanted_branches);
2530 got_pathlist_free(&wanted_refs);
2531 free(id_str);
2532 free(cwd);
2533 free(repo_path);
2534 free(pack_hash);
2535 free(proto);
2536 free(host);
2537 free(port);
2538 free(server_path);
2539 free(repo_name);
2540 return error;
2544 __dead static void
2545 usage_checkout(void)
2547 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2548 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2549 exit(1);
2552 static void
2553 show_worktree_base_ref_warning(void)
2555 fprintf(stderr, "%s: warning: could not create a reference "
2556 "to the work tree's base commit; the commit could be "
2557 "garbage-collected by Git; making the repository "
2558 "writable and running 'got update' will prevent this\n",
2559 getprogname());
2562 struct got_checkout_progress_arg {
2563 const char *worktree_path;
2564 int had_base_commit_ref_error;
2567 static const struct got_error *
2568 checkout_progress(void *arg, unsigned char status, const char *path)
2570 struct got_checkout_progress_arg *a = arg;
2572 /* Base commit bump happens silently. */
2573 if (status == GOT_STATUS_BUMP_BASE)
2574 return NULL;
2576 if (status == GOT_STATUS_BASE_REF_ERR) {
2577 a->had_base_commit_ref_error = 1;
2578 return NULL;
2581 while (path[0] == '/')
2582 path++;
2584 printf("%c %s/%s\n", status, a->worktree_path, path);
2585 return NULL;
2588 static const struct got_error *
2589 check_cancelled(void *arg)
2591 if (sigint_received || sigpipe_received)
2592 return got_error(GOT_ERR_CANCELLED);
2593 return NULL;
2596 static const struct got_error *
2597 check_linear_ancestry(struct got_object_id *commit_id,
2598 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2599 struct got_repository *repo)
2601 const struct got_error *err = NULL;
2602 struct got_object_id *yca_id;
2604 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2605 commit_id, base_commit_id, repo, check_cancelled, NULL);
2606 if (err)
2607 return err;
2609 if (yca_id == NULL)
2610 return got_error(GOT_ERR_ANCESTRY);
2613 * Require a straight line of history between the target commit
2614 * and the work tree's base commit.
2616 * Non-linear situations such as this require a rebase:
2618 * (commit) D F (base_commit)
2619 * \ /
2620 * C E
2621 * \ /
2622 * B (yca)
2623 * |
2624 * A
2626 * 'got update' only handles linear cases:
2627 * Update forwards in time: A (base/yca) - B - C - D (commit)
2628 * Update backwards in time: D (base) - C - B - A (commit/yca)
2630 if (allow_forwards_in_time_only) {
2631 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2632 return got_error(GOT_ERR_ANCESTRY);
2633 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2634 got_object_id_cmp(base_commit_id, yca_id) != 0)
2635 return got_error(GOT_ERR_ANCESTRY);
2637 free(yca_id);
2638 return NULL;
2641 static const struct got_error *
2642 check_same_branch(struct got_object_id *commit_id,
2643 struct got_reference *head_ref, struct got_object_id *yca_id,
2644 struct got_repository *repo)
2646 const struct got_error *err = NULL;
2647 struct got_commit_graph *graph = NULL;
2648 struct got_object_id *head_commit_id = NULL;
2649 int is_same_branch = 0;
2651 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2652 if (err)
2653 goto done;
2655 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2656 is_same_branch = 1;
2657 goto done;
2659 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2660 is_same_branch = 1;
2661 goto done;
2664 err = got_commit_graph_open(&graph, "/", 1);
2665 if (err)
2666 goto done;
2668 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2669 check_cancelled, NULL);
2670 if (err)
2671 goto done;
2673 for (;;) {
2674 struct got_object_id *id;
2675 err = got_commit_graph_iter_next(&id, graph, repo,
2676 check_cancelled, NULL);
2677 if (err) {
2678 if (err->code == GOT_ERR_ITER_COMPLETED)
2679 err = NULL;
2680 break;
2683 if (id) {
2684 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2685 break;
2686 if (got_object_id_cmp(id, commit_id) == 0) {
2687 is_same_branch = 1;
2688 break;
2692 done:
2693 if (graph)
2694 got_commit_graph_close(graph);
2695 free(head_commit_id);
2696 if (!err && !is_same_branch)
2697 err = got_error(GOT_ERR_ANCESTRY);
2698 return err;
2701 static const struct got_error *
2702 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2704 static char msg[512];
2705 const char *branch_name;
2707 if (got_ref_is_symbolic(ref))
2708 branch_name = got_ref_get_symref_target(ref);
2709 else
2710 branch_name = got_ref_get_name(ref);
2712 if (strncmp("refs/heads/", branch_name, 11) == 0)
2713 branch_name += 11;
2715 snprintf(msg, sizeof(msg),
2716 "target commit is not contained in branch '%s'; "
2717 "the branch to use must be specified with -b; "
2718 "if necessary a new branch can be created for "
2719 "this commit with 'got branch -c %s BRANCH_NAME'",
2720 branch_name, commit_id_str);
2722 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2725 static const struct got_error *
2726 cmd_checkout(int argc, char *argv[])
2728 const struct got_error *error = NULL;
2729 struct got_repository *repo = NULL;
2730 struct got_reference *head_ref = NULL;
2731 struct got_worktree *worktree = NULL;
2732 char *repo_path = NULL;
2733 char *worktree_path = NULL;
2734 const char *path_prefix = "";
2735 const char *branch_name = GOT_REF_HEAD;
2736 char *commit_id_str = NULL;
2737 char *cwd = NULL;
2738 int ch, same_path_prefix, allow_nonempty = 0;
2739 struct got_pathlist_head paths;
2740 struct got_checkout_progress_arg cpa;
2742 TAILQ_INIT(&paths);
2744 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2745 switch (ch) {
2746 case 'b':
2747 branch_name = optarg;
2748 break;
2749 case 'c':
2750 commit_id_str = strdup(optarg);
2751 if (commit_id_str == NULL)
2752 return got_error_from_errno("strdup");
2753 break;
2754 case 'E':
2755 allow_nonempty = 1;
2756 break;
2757 case 'p':
2758 path_prefix = optarg;
2759 break;
2760 default:
2761 usage_checkout();
2762 /* NOTREACHED */
2766 argc -= optind;
2767 argv += optind;
2769 #ifndef PROFILE
2770 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2771 "unveil", NULL) == -1)
2772 err(1, "pledge");
2773 #endif
2774 if (argc == 1) {
2775 char *base, *dotgit;
2776 const char *path;
2777 repo_path = realpath(argv[0], NULL);
2778 if (repo_path == NULL)
2779 return got_error_from_errno2("realpath", argv[0]);
2780 cwd = getcwd(NULL, 0);
2781 if (cwd == NULL) {
2782 error = got_error_from_errno("getcwd");
2783 goto done;
2785 if (path_prefix[0])
2786 path = path_prefix;
2787 else
2788 path = repo_path;
2789 error = got_path_basename(&base, path);
2790 if (error)
2791 goto done;
2792 dotgit = strstr(base, ".git");
2793 if (dotgit)
2794 *dotgit = '\0';
2795 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2796 error = got_error_from_errno("asprintf");
2797 free(base);
2798 goto done;
2800 free(base);
2801 } else if (argc == 2) {
2802 repo_path = realpath(argv[0], NULL);
2803 if (repo_path == NULL) {
2804 error = got_error_from_errno2("realpath", argv[0]);
2805 goto done;
2807 worktree_path = realpath(argv[1], NULL);
2808 if (worktree_path == NULL) {
2809 if (errno != ENOENT) {
2810 error = got_error_from_errno2("realpath",
2811 argv[1]);
2812 goto done;
2814 worktree_path = strdup(argv[1]);
2815 if (worktree_path == NULL) {
2816 error = got_error_from_errno("strdup");
2817 goto done;
2820 } else
2821 usage_checkout();
2823 got_path_strip_trailing_slashes(repo_path);
2824 got_path_strip_trailing_slashes(worktree_path);
2826 error = got_repo_open(&repo, repo_path, NULL);
2827 if (error != NULL)
2828 goto done;
2830 /* Pre-create work tree path for unveil(2) */
2831 error = got_path_mkdir(worktree_path);
2832 if (error) {
2833 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2834 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2835 goto done;
2836 if (!allow_nonempty &&
2837 !got_path_dir_is_empty(worktree_path)) {
2838 error = got_error_path(worktree_path,
2839 GOT_ERR_DIR_NOT_EMPTY);
2840 goto done;
2844 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2845 if (error)
2846 goto done;
2848 error = got_ref_open(&head_ref, repo, branch_name, 0);
2849 if (error != NULL)
2850 goto done;
2852 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2853 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2854 goto done;
2856 error = got_worktree_open(&worktree, worktree_path);
2857 if (error != NULL)
2858 goto done;
2860 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2861 path_prefix);
2862 if (error != NULL)
2863 goto done;
2864 if (!same_path_prefix) {
2865 error = got_error(GOT_ERR_PATH_PREFIX);
2866 goto done;
2869 if (commit_id_str) {
2870 struct got_object_id *commit_id;
2871 struct got_reflist_head refs;
2872 TAILQ_INIT(&refs);
2873 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2874 NULL);
2875 if (error)
2876 goto done;
2877 error = got_repo_match_object_id(&commit_id, NULL,
2878 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2879 got_ref_list_free(&refs);
2880 if (error)
2881 goto done;
2882 error = check_linear_ancestry(commit_id,
2883 got_worktree_get_base_commit_id(worktree), 0, repo);
2884 if (error != NULL) {
2885 free(commit_id);
2886 if (error->code == GOT_ERR_ANCESTRY) {
2887 error = checkout_ancestry_error(
2888 head_ref, commit_id_str);
2890 goto done;
2892 error = check_same_branch(commit_id, head_ref, NULL, repo);
2893 if (error) {
2894 if (error->code == GOT_ERR_ANCESTRY) {
2895 error = checkout_ancestry_error(
2896 head_ref, commit_id_str);
2898 goto done;
2900 error = got_worktree_set_base_commit_id(worktree, repo,
2901 commit_id);
2902 free(commit_id);
2903 if (error)
2904 goto done;
2907 error = got_pathlist_append(&paths, "", NULL);
2908 if (error)
2909 goto done;
2910 cpa.worktree_path = worktree_path;
2911 cpa.had_base_commit_ref_error = 0;
2912 error = got_worktree_checkout_files(worktree, &paths, repo,
2913 checkout_progress, &cpa, check_cancelled, NULL);
2914 if (error != NULL)
2915 goto done;
2917 printf("Now shut up and hack\n");
2918 if (cpa.had_base_commit_ref_error)
2919 show_worktree_base_ref_warning();
2920 done:
2921 got_pathlist_free(&paths);
2922 free(commit_id_str);
2923 free(repo_path);
2924 free(worktree_path);
2925 free(cwd);
2926 return error;
2929 struct got_update_progress_arg {
2930 int did_something;
2931 int conflicts;
2932 int obstructed;
2933 int not_updated;
2936 void
2937 print_update_progress_stats(struct got_update_progress_arg *upa)
2939 if (!upa->did_something)
2940 return;
2942 if (upa->conflicts > 0)
2943 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2944 if (upa->obstructed > 0)
2945 printf("File paths obstructed by a non-regular file: %d\n",
2946 upa->obstructed);
2947 if (upa->not_updated > 0)
2948 printf("Files not updated because of existing merge "
2949 "conflicts: %d\n", upa->not_updated);
2952 __dead static void
2953 usage_update(void)
2955 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2956 getprogname());
2957 exit(1);
2960 static const struct got_error *
2961 update_progress(void *arg, unsigned char status, const char *path)
2963 struct got_update_progress_arg *upa = arg;
2965 if (status == GOT_STATUS_EXISTS ||
2966 status == GOT_STATUS_BASE_REF_ERR)
2967 return NULL;
2969 upa->did_something = 1;
2971 /* Base commit bump happens silently. */
2972 if (status == GOT_STATUS_BUMP_BASE)
2973 return NULL;
2975 if (status == GOT_STATUS_CONFLICT)
2976 upa->conflicts++;
2977 if (status == GOT_STATUS_OBSTRUCTED)
2978 upa->obstructed++;
2979 if (status == GOT_STATUS_CANNOT_UPDATE)
2980 upa->not_updated++;
2982 while (path[0] == '/')
2983 path++;
2984 printf("%c %s\n", status, path);
2985 return NULL;
2988 static const struct got_error *
2989 switch_head_ref(struct got_reference *head_ref,
2990 struct got_object_id *commit_id, struct got_worktree *worktree,
2991 struct got_repository *repo)
2993 const struct got_error *err = NULL;
2994 char *base_id_str;
2995 int ref_has_moved = 0;
2997 /* Trivial case: switching between two different references. */
2998 if (strcmp(got_ref_get_name(head_ref),
2999 got_worktree_get_head_ref_name(worktree)) != 0) {
3000 printf("Switching work tree from %s to %s\n",
3001 got_worktree_get_head_ref_name(worktree),
3002 got_ref_get_name(head_ref));
3003 return got_worktree_set_head_ref(worktree, head_ref);
3006 err = check_linear_ancestry(commit_id,
3007 got_worktree_get_base_commit_id(worktree), 0, repo);
3008 if (err) {
3009 if (err->code != GOT_ERR_ANCESTRY)
3010 return err;
3011 ref_has_moved = 1;
3013 if (!ref_has_moved)
3014 return NULL;
3016 /* Switching to a rebased branch with the same reference name. */
3017 err = got_object_id_str(&base_id_str,
3018 got_worktree_get_base_commit_id(worktree));
3019 if (err)
3020 return err;
3021 printf("Reference %s now points at a different branch\n",
3022 got_worktree_get_head_ref_name(worktree));
3023 printf("Switching work tree from %s to %s\n", base_id_str,
3024 got_worktree_get_head_ref_name(worktree));
3025 return NULL;
3028 static const struct got_error *
3029 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3031 const struct got_error *err;
3032 int in_progress;
3034 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3035 if (err)
3036 return err;
3037 if (in_progress)
3038 return got_error(GOT_ERR_REBASING);
3040 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3041 if (err)
3042 return err;
3043 if (in_progress)
3044 return got_error(GOT_ERR_HISTEDIT_BUSY);
3046 return NULL;
3049 static const struct got_error *
3050 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3051 char *argv[], struct got_worktree *worktree)
3053 const struct got_error *err = NULL;
3054 char *path;
3055 int i;
3057 if (argc == 0) {
3058 path = strdup("");
3059 if (path == NULL)
3060 return got_error_from_errno("strdup");
3061 return got_pathlist_append(paths, path, NULL);
3064 for (i = 0; i < argc; i++) {
3065 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3066 if (err)
3067 break;
3068 err = got_pathlist_append(paths, path, NULL);
3069 if (err) {
3070 free(path);
3071 break;
3075 return err;
3078 static const struct got_error *
3079 wrap_not_worktree_error(const struct got_error *orig_err,
3080 const char *cmdname, const char *path)
3082 const struct got_error *err;
3083 struct got_repository *repo;
3084 static char msg[512];
3086 err = got_repo_open(&repo, path, NULL);
3087 if (err)
3088 return orig_err;
3090 snprintf(msg, sizeof(msg),
3091 "'got %s' needs a work tree in addition to a git repository\n"
3092 "Work trees can be checked out from this Git repository with "
3093 "'got checkout'.\n"
3094 "The got(1) manual page contains more information.", cmdname);
3095 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3096 got_repo_close(repo);
3097 return err;
3100 static const struct got_error *
3101 cmd_update(int argc, char *argv[])
3103 const struct got_error *error = NULL;
3104 struct got_repository *repo = NULL;
3105 struct got_worktree *worktree = NULL;
3106 char *worktree_path = NULL;
3107 struct got_object_id *commit_id = NULL;
3108 char *commit_id_str = NULL;
3109 const char *branch_name = NULL;
3110 struct got_reference *head_ref = NULL;
3111 struct got_pathlist_head paths;
3112 struct got_pathlist_entry *pe;
3113 int ch;
3114 struct got_update_progress_arg upa;
3116 TAILQ_INIT(&paths);
3118 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3119 switch (ch) {
3120 case 'b':
3121 branch_name = optarg;
3122 break;
3123 case 'c':
3124 commit_id_str = strdup(optarg);
3125 if (commit_id_str == NULL)
3126 return got_error_from_errno("strdup");
3127 break;
3128 default:
3129 usage_update();
3130 /* NOTREACHED */
3134 argc -= optind;
3135 argv += optind;
3137 #ifndef PROFILE
3138 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3139 "unveil", NULL) == -1)
3140 err(1, "pledge");
3141 #endif
3142 worktree_path = getcwd(NULL, 0);
3143 if (worktree_path == NULL) {
3144 error = got_error_from_errno("getcwd");
3145 goto done;
3147 error = got_worktree_open(&worktree, worktree_path);
3148 if (error) {
3149 if (error->code == GOT_ERR_NOT_WORKTREE)
3150 error = wrap_not_worktree_error(error, "update",
3151 worktree_path);
3152 goto done;
3155 error = check_rebase_or_histedit_in_progress(worktree);
3156 if (error)
3157 goto done;
3159 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3160 NULL);
3161 if (error != NULL)
3162 goto done;
3164 error = apply_unveil(got_repo_get_path(repo), 0,
3165 got_worktree_get_root_path(worktree));
3166 if (error)
3167 goto done;
3169 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3170 if (error)
3171 goto done;
3173 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3174 got_worktree_get_head_ref_name(worktree), 0);
3175 if (error != NULL)
3176 goto done;
3177 if (commit_id_str == NULL) {
3178 error = got_ref_resolve(&commit_id, repo, head_ref);
3179 if (error != NULL)
3180 goto done;
3181 error = got_object_id_str(&commit_id_str, commit_id);
3182 if (error != NULL)
3183 goto done;
3184 } else {
3185 struct got_reflist_head refs;
3186 TAILQ_INIT(&refs);
3187 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3188 NULL);
3189 if (error)
3190 goto done;
3191 error = got_repo_match_object_id(&commit_id, NULL,
3192 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3193 got_ref_list_free(&refs);
3194 free(commit_id_str);
3195 commit_id_str = NULL;
3196 if (error)
3197 goto done;
3198 error = got_object_id_str(&commit_id_str, commit_id);
3199 if (error)
3200 goto done;
3203 if (branch_name) {
3204 struct got_object_id *head_commit_id;
3205 TAILQ_FOREACH(pe, &paths, entry) {
3206 if (pe->path_len == 0)
3207 continue;
3208 error = got_error_msg(GOT_ERR_BAD_PATH,
3209 "switching between branches requires that "
3210 "the entire work tree gets updated");
3211 goto done;
3213 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3214 if (error)
3215 goto done;
3216 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3217 repo);
3218 free(head_commit_id);
3219 if (error != NULL)
3220 goto done;
3221 error = check_same_branch(commit_id, head_ref, NULL, repo);
3222 if (error)
3223 goto done;
3224 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3225 if (error)
3226 goto done;
3227 } else {
3228 error = check_linear_ancestry(commit_id,
3229 got_worktree_get_base_commit_id(worktree), 0, repo);
3230 if (error != NULL) {
3231 if (error->code == GOT_ERR_ANCESTRY)
3232 error = got_error(GOT_ERR_BRANCH_MOVED);
3233 goto done;
3235 error = check_same_branch(commit_id, head_ref, NULL, repo);
3236 if (error)
3237 goto done;
3240 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3241 commit_id) != 0) {
3242 error = got_worktree_set_base_commit_id(worktree, repo,
3243 commit_id);
3244 if (error)
3245 goto done;
3248 memset(&upa, 0, sizeof(upa));
3249 error = got_worktree_checkout_files(worktree, &paths, repo,
3250 update_progress, &upa, check_cancelled, NULL);
3251 if (error != NULL)
3252 goto done;
3254 if (upa.did_something)
3255 printf("Updated to commit %s\n", commit_id_str);
3256 else
3257 printf("Already up-to-date\n");
3258 print_update_progress_stats(&upa);
3259 done:
3260 free(worktree_path);
3261 TAILQ_FOREACH(pe, &paths, entry)
3262 free((char *)pe->path);
3263 got_pathlist_free(&paths);
3264 free(commit_id);
3265 free(commit_id_str);
3266 return error;
3269 static const struct got_error *
3270 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3271 const char *path, int diff_context, int ignore_whitespace,
3272 int force_text_diff, struct got_repository *repo)
3274 const struct got_error *err = NULL;
3275 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3277 if (blob_id1) {
3278 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3279 if (err)
3280 goto done;
3283 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3284 if (err)
3285 goto done;
3287 while (path[0] == '/')
3288 path++;
3289 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3290 diff_context, ignore_whitespace, force_text_diff, stdout);
3291 done:
3292 if (blob1)
3293 got_object_blob_close(blob1);
3294 got_object_blob_close(blob2);
3295 return err;
3298 static const struct got_error *
3299 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3300 const char *path, int diff_context, int ignore_whitespace,
3301 int force_text_diff, struct got_repository *repo)
3303 const struct got_error *err = NULL;
3304 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 struct got_diff_blob_output_unidiff_arg arg;
3307 if (tree_id1) {
3308 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3309 if (err)
3310 goto done;
3313 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3314 if (err)
3315 goto done;
3317 arg.diff_context = diff_context;
3318 arg.ignore_whitespace = ignore_whitespace;
3319 arg.force_text_diff = force_text_diff;
3320 arg.outfile = stdout;
3321 arg.line_offsets = NULL;
3322 arg.nlines = 0;
3323 while (path[0] == '/')
3324 path++;
3325 err = got_diff_tree(tree1, tree2, path, path, repo,
3326 got_diff_blob_output_unidiff, &arg, 1);
3327 done:
3328 if (tree1)
3329 got_object_tree_close(tree1);
3330 if (tree2)
3331 got_object_tree_close(tree2);
3332 return err;
3335 static const struct got_error *
3336 get_changed_paths(struct got_pathlist_head *paths,
3337 struct got_commit_object *commit, struct got_repository *repo)
3339 const struct got_error *err = NULL;
3340 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3341 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3342 struct got_object_qid *qid;
3344 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3345 if (qid != NULL) {
3346 struct got_commit_object *pcommit;
3347 err = got_object_open_as_commit(&pcommit, repo,
3348 qid->id);
3349 if (err)
3350 return err;
3352 tree_id1 = got_object_commit_get_tree_id(pcommit);
3353 got_object_commit_close(pcommit);
3357 if (tree_id1) {
3358 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3359 if (err)
3360 goto done;
3363 tree_id2 = got_object_commit_get_tree_id(commit);
3364 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3365 if (err)
3366 goto done;
3368 err = got_diff_tree(tree1, tree2, "", "", repo,
3369 got_diff_tree_collect_changed_paths, paths, 0);
3370 done:
3371 if (tree1)
3372 got_object_tree_close(tree1);
3373 if (tree2)
3374 got_object_tree_close(tree2);
3375 return err;
3378 static const struct got_error *
3379 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3380 const char *path, int diff_context, struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 struct got_commit_object *pcommit = NULL;
3384 char *id_str1 = NULL, *id_str2 = NULL;
3385 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3386 struct got_object_qid *qid;
3388 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3389 if (qid != NULL) {
3390 err = got_object_open_as_commit(&pcommit, repo,
3391 qid->id);
3392 if (err)
3393 return err;
3396 if (path && path[0] != '\0') {
3397 int obj_type;
3398 err = got_object_id_by_path(&obj_id2, repo, id, path);
3399 if (err)
3400 goto done;
3401 err = got_object_id_str(&id_str2, obj_id2);
3402 if (err) {
3403 free(obj_id2);
3404 goto done;
3406 if (pcommit) {
3407 err = got_object_id_by_path(&obj_id1, repo,
3408 qid->id, path);
3409 if (err) {
3410 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3411 free(obj_id2);
3412 goto done;
3414 } else {
3415 err = got_object_id_str(&id_str1, obj_id1);
3416 if (err) {
3417 free(obj_id2);
3418 goto done;
3422 err = got_object_get_type(&obj_type, repo, obj_id2);
3423 if (err) {
3424 free(obj_id2);
3425 goto done;
3427 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3428 switch (obj_type) {
3429 case GOT_OBJ_TYPE_BLOB:
3430 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3431 0, 0, repo);
3432 break;
3433 case GOT_OBJ_TYPE_TREE:
3434 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3435 0, 0, repo);
3436 break;
3437 default:
3438 err = got_error(GOT_ERR_OBJ_TYPE);
3439 break;
3441 free(obj_id1);
3442 free(obj_id2);
3443 } else {
3444 obj_id2 = got_object_commit_get_tree_id(commit);
3445 err = got_object_id_str(&id_str2, obj_id2);
3446 if (err)
3447 goto done;
3448 if (pcommit) {
3449 obj_id1 = got_object_commit_get_tree_id(pcommit);
3450 err = got_object_id_str(&id_str1, obj_id1);
3451 if (err)
3452 goto done;
3454 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3455 id_str2);
3456 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3457 repo);
3459 done:
3460 free(id_str1);
3461 free(id_str2);
3462 if (pcommit)
3463 got_object_commit_close(pcommit);
3464 return err;
3467 static char *
3468 get_datestr(time_t *time, char *datebuf)
3470 struct tm mytm, *tm;
3471 char *p, *s;
3473 tm = gmtime_r(time, &mytm);
3474 if (tm == NULL)
3475 return NULL;
3476 s = asctime_r(tm, datebuf);
3477 if (s == NULL)
3478 return NULL;
3479 p = strchr(s, '\n');
3480 if (p)
3481 *p = '\0';
3482 return s;
3485 static const struct got_error *
3486 match_logmsg(int *have_match, struct got_object_id *id,
3487 struct got_commit_object *commit, regex_t *regex)
3489 const struct got_error *err = NULL;
3490 regmatch_t regmatch;
3491 char *id_str = NULL, *logmsg = NULL;
3493 *have_match = 0;
3495 err = got_object_id_str(&id_str, id);
3496 if (err)
3497 return err;
3499 err = got_object_commit_get_logmsg(&logmsg, commit);
3500 if (err)
3501 goto done;
3503 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3504 *have_match = 1;
3505 done:
3506 free(id_str);
3507 free(logmsg);
3508 return err;
3511 static void
3512 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3513 regex_t *regex)
3515 regmatch_t regmatch;
3516 struct got_pathlist_entry *pe;
3518 *have_match = 0;
3520 TAILQ_FOREACH(pe, changed_paths, entry) {
3521 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3522 *have_match = 1;
3523 break;
3528 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3530 static const struct got_error*
3531 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3532 struct got_object_id *id, struct got_repository *repo)
3534 static const struct got_error *err = NULL;
3535 struct got_reflist_entry *re;
3536 char *s;
3537 const char *name;
3539 *refs_str = NULL;
3541 TAILQ_FOREACH(re, refs, entry) {
3542 struct got_tag_object *tag = NULL;
3543 struct got_object_id *ref_id;
3544 int cmp;
3546 name = got_ref_get_name(re->ref);
3547 if (strcmp(name, GOT_REF_HEAD) == 0)
3548 continue;
3549 if (strncmp(name, "refs/", 5) == 0)
3550 name += 5;
3551 if (strncmp(name, "got/", 4) == 0)
3552 continue;
3553 if (strncmp(name, "heads/", 6) == 0)
3554 name += 6;
3555 if (strncmp(name, "remotes/", 8) == 0) {
3556 name += 8;
3557 s = strstr(name, "/" GOT_REF_HEAD);
3558 if (s != NULL && s[strlen(s)] == '\0')
3559 continue;
3561 err = got_ref_resolve(&ref_id, repo, re->ref);
3562 if (err)
3563 break;
3564 if (strncmp(name, "tags/", 5) == 0) {
3565 err = got_object_open_as_tag(&tag, repo, ref_id);
3566 if (err) {
3567 if (err->code != GOT_ERR_OBJ_TYPE) {
3568 free(ref_id);
3569 break;
3571 /* Ref points at something other than a tag. */
3572 err = NULL;
3573 tag = NULL;
3576 cmp = got_object_id_cmp(tag ?
3577 got_object_tag_get_object_id(tag) : ref_id, id);
3578 free(ref_id);
3579 if (tag)
3580 got_object_tag_close(tag);
3581 if (cmp != 0)
3582 continue;
3583 s = *refs_str;
3584 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3585 s ? ", " : "", name) == -1) {
3586 err = got_error_from_errno("asprintf");
3587 free(s);
3588 *refs_str = NULL;
3589 break;
3591 free(s);
3594 return err;
3597 static const struct got_error *
3598 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3599 struct got_repository *repo, const char *path,
3600 struct got_pathlist_head *changed_paths, int show_patch,
3601 int diff_context, struct got_reflist_object_id_map *refs_idmap)
3603 const struct got_error *err = NULL;
3604 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3605 char datebuf[26];
3606 time_t committer_time;
3607 const char *author, *committer;
3608 char *refs_str = NULL;
3609 struct got_reflist_head *refs;
3611 err = got_object_id_str(&id_str, id);
3612 if (err)
3613 return err;
3615 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3616 if (refs) {
3617 err = build_refs_str(&refs_str, refs, id, repo);
3618 if (err)
3619 goto done;
3622 printf(GOT_COMMIT_SEP_STR);
3623 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3624 refs_str ? refs_str : "", refs_str ? ")" : "");
3625 free(id_str);
3626 id_str = NULL;
3627 free(refs_str);
3628 refs_str = NULL;
3629 printf("from: %s\n", got_object_commit_get_author(commit));
3630 committer_time = got_object_commit_get_committer_time(commit);
3631 datestr = get_datestr(&committer_time, datebuf);
3632 if (datestr)
3633 printf("date: %s UTC\n", datestr);
3634 author = got_object_commit_get_author(commit);
3635 committer = got_object_commit_get_committer(commit);
3636 if (strcmp(author, committer) != 0)
3637 printf("via: %s\n", committer);
3638 if (got_object_commit_get_nparents(commit) > 1) {
3639 const struct got_object_id_queue *parent_ids;
3640 struct got_object_qid *qid;
3641 int n = 1;
3642 parent_ids = got_object_commit_get_parent_ids(commit);
3643 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3644 err = got_object_id_str(&id_str, qid->id);
3645 if (err)
3646 goto done;
3647 printf("parent %d: %s\n", n++, id_str);
3648 free(id_str);
3649 id_str = NULL;
3653 err = got_object_commit_get_logmsg(&logmsg0, commit);
3654 if (err)
3655 goto done;
3657 logmsg = logmsg0;
3658 do {
3659 line = strsep(&logmsg, "\n");
3660 if (line)
3661 printf(" %s\n", line);
3662 } while (line);
3663 free(logmsg0);
3665 if (changed_paths) {
3666 struct got_pathlist_entry *pe;
3667 TAILQ_FOREACH(pe, changed_paths, entry) {
3668 struct got_diff_changed_path *cp = pe->data;
3669 printf(" %c %s\n", cp->status, pe->path);
3671 printf("\n");
3673 if (show_patch) {
3674 err = print_patch(commit, id, path, diff_context, repo);
3675 if (err == 0)
3676 printf("\n");
3679 if (fflush(stdout) != 0 && err == NULL)
3680 err = got_error_from_errno("fflush");
3681 done:
3682 free(id_str);
3683 free(refs_str);
3684 return err;
3687 static const struct got_error *
3688 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3689 struct got_repository *repo, const char *path, int show_changed_paths,
3690 int show_patch, const char *search_pattern, int diff_context, int limit,
3691 int log_branches, int reverse_display_order,
3692 struct got_reflist_object_id_map *refs_idmap)
3694 const struct got_error *err;
3695 struct got_commit_graph *graph;
3696 regex_t regex;
3697 int have_match;
3698 struct got_object_id_queue reversed_commits;
3699 struct got_object_qid *qid;
3700 struct got_commit_object *commit;
3701 struct got_pathlist_head changed_paths;
3702 struct got_pathlist_entry *pe;
3704 SIMPLEQ_INIT(&reversed_commits);
3705 TAILQ_INIT(&changed_paths);
3707 if (search_pattern && regcomp(&regex, search_pattern,
3708 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3709 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3711 err = got_commit_graph_open(&graph, path, !log_branches);
3712 if (err)
3713 return err;
3714 err = got_commit_graph_iter_start(graph, root_id, repo,
3715 check_cancelled, NULL);
3716 if (err)
3717 goto done;
3718 for (;;) {
3719 struct got_object_id *id;
3721 if (sigint_received || sigpipe_received)
3722 break;
3724 err = got_commit_graph_iter_next(&id, graph, repo,
3725 check_cancelled, NULL);
3726 if (err) {
3727 if (err->code == GOT_ERR_ITER_COMPLETED)
3728 err = NULL;
3729 break;
3731 if (id == NULL)
3732 break;
3734 err = got_object_open_as_commit(&commit, repo, id);
3735 if (err)
3736 break;
3738 if (show_changed_paths && !reverse_display_order) {
3739 err = get_changed_paths(&changed_paths, commit, repo);
3740 if (err)
3741 break;
3744 if (search_pattern) {
3745 err = match_logmsg(&have_match, id, commit, &regex);
3746 if (err) {
3747 got_object_commit_close(commit);
3748 break;
3750 if (have_match == 0 && show_changed_paths)
3751 match_changed_paths(&have_match,
3752 &changed_paths, &regex);
3753 if (have_match == 0) {
3754 got_object_commit_close(commit);
3755 TAILQ_FOREACH(pe, &changed_paths, entry) {
3756 free((char *)pe->path);
3757 free(pe->data);
3759 got_pathlist_free(&changed_paths);
3760 continue;
3764 if (reverse_display_order) {
3765 err = got_object_qid_alloc(&qid, id);
3766 if (err)
3767 break;
3768 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3769 got_object_commit_close(commit);
3770 } else {
3771 err = print_commit(commit, id, repo, path,
3772 show_changed_paths ? &changed_paths : NULL,
3773 show_patch, diff_context, refs_idmap);
3774 got_object_commit_close(commit);
3775 if (err)
3776 break;
3778 if ((limit && --limit == 0) ||
3779 (end_id && got_object_id_cmp(id, end_id) == 0))
3780 break;
3782 TAILQ_FOREACH(pe, &changed_paths, entry) {
3783 free((char *)pe->path);
3784 free(pe->data);
3786 got_pathlist_free(&changed_paths);
3788 if (reverse_display_order) {
3789 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3790 err = got_object_open_as_commit(&commit, repo, qid->id);
3791 if (err)
3792 break;
3793 if (show_changed_paths) {
3794 err = get_changed_paths(&changed_paths,
3795 commit, repo);
3796 if (err)
3797 break;
3799 err = print_commit(commit, qid->id, repo, path,
3800 show_changed_paths ? &changed_paths : NULL,
3801 show_patch, diff_context, refs_idmap);
3802 got_object_commit_close(commit);
3803 if (err)
3804 break;
3805 TAILQ_FOREACH(pe, &changed_paths, entry) {
3806 free((char *)pe->path);
3807 free(pe->data);
3809 got_pathlist_free(&changed_paths);
3812 done:
3813 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3814 qid = SIMPLEQ_FIRST(&reversed_commits);
3815 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3816 got_object_qid_free(qid);
3818 TAILQ_FOREACH(pe, &changed_paths, entry) {
3819 free((char *)pe->path);
3820 free(pe->data);
3822 got_pathlist_free(&changed_paths);
3823 if (search_pattern)
3824 regfree(&regex);
3825 got_commit_graph_close(graph);
3826 return err;
3829 __dead static void
3830 usage_log(void)
3832 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3833 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3834 "[-R] [path]\n", getprogname());
3835 exit(1);
3838 static int
3839 get_default_log_limit(void)
3841 const char *got_default_log_limit;
3842 long long n;
3843 const char *errstr;
3845 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3846 if (got_default_log_limit == NULL)
3847 return 0;
3848 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3849 if (errstr != NULL)
3850 return 0;
3851 return n;
3854 static const struct got_error *
3855 cmd_log(int argc, char *argv[])
3857 const struct got_error *error;
3858 struct got_repository *repo = NULL;
3859 struct got_worktree *worktree = NULL;
3860 struct got_object_id *start_id = NULL, *end_id = NULL;
3861 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3862 const char *start_commit = NULL, *end_commit = NULL;
3863 const char *search_pattern = NULL;
3864 int diff_context = -1, ch;
3865 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3866 int reverse_display_order = 0;
3867 const char *errstr;
3868 struct got_reflist_head refs;
3869 struct got_reflist_object_id_map *refs_idmap = NULL;
3871 TAILQ_INIT(&refs);
3873 #ifndef PROFILE
3874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3875 NULL)
3876 == -1)
3877 err(1, "pledge");
3878 #endif
3880 limit = get_default_log_limit();
3882 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3883 switch (ch) {
3884 case 'p':
3885 show_patch = 1;
3886 break;
3887 case 'P':
3888 show_changed_paths = 1;
3889 break;
3890 case 'c':
3891 start_commit = optarg;
3892 break;
3893 case 'C':
3894 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3895 &errstr);
3896 if (errstr != NULL)
3897 err(1, "-C option %s", errstr);
3898 break;
3899 case 'l':
3900 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3901 if (errstr != NULL)
3902 err(1, "-l option %s", errstr);
3903 break;
3904 case 'b':
3905 log_branches = 1;
3906 break;
3907 case 'r':
3908 repo_path = realpath(optarg, NULL);
3909 if (repo_path == NULL)
3910 return got_error_from_errno2("realpath",
3911 optarg);
3912 got_path_strip_trailing_slashes(repo_path);
3913 break;
3914 case 'R':
3915 reverse_display_order = 1;
3916 break;
3917 case 's':
3918 search_pattern = optarg;
3919 break;
3920 case 'x':
3921 end_commit = optarg;
3922 break;
3923 default:
3924 usage_log();
3925 /* NOTREACHED */
3929 argc -= optind;
3930 argv += optind;
3932 if (diff_context == -1)
3933 diff_context = 3;
3934 else if (!show_patch)
3935 errx(1, "-C requires -p");
3937 cwd = getcwd(NULL, 0);
3938 if (cwd == NULL) {
3939 error = got_error_from_errno("getcwd");
3940 goto done;
3943 if (repo_path == NULL) {
3944 error = got_worktree_open(&worktree, cwd);
3945 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3946 goto done;
3947 error = NULL;
3950 if (argc == 1) {
3951 if (worktree) {
3952 error = got_worktree_resolve_path(&path, worktree,
3953 argv[0]);
3954 if (error)
3955 goto done;
3956 } else {
3957 path = strdup(argv[0]);
3958 if (path == NULL) {
3959 error = got_error_from_errno("strdup");
3960 goto done;
3963 } else if (argc != 0)
3964 usage_log();
3966 if (repo_path == NULL) {
3967 repo_path = worktree ?
3968 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3970 if (repo_path == NULL) {
3971 error = got_error_from_errno("strdup");
3972 goto done;
3975 error = got_repo_open(&repo, repo_path, NULL);
3976 if (error != NULL)
3977 goto done;
3979 error = apply_unveil(got_repo_get_path(repo), 1,
3980 worktree ? got_worktree_get_root_path(worktree) : NULL);
3981 if (error)
3982 goto done;
3984 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3985 if (error)
3986 goto done;
3988 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3989 if (error)
3990 goto done;
3992 if (start_commit == NULL) {
3993 struct got_reference *head_ref;
3994 struct got_commit_object *commit = NULL;
3995 error = got_ref_open(&head_ref, repo,
3996 worktree ? got_worktree_get_head_ref_name(worktree)
3997 : GOT_REF_HEAD, 0);
3998 if (error != NULL)
3999 goto done;
4000 error = got_ref_resolve(&start_id, repo, head_ref);
4001 got_ref_close(head_ref);
4002 if (error != NULL)
4003 goto done;
4004 error = got_object_open_as_commit(&commit, repo,
4005 start_id);
4006 if (error != NULL)
4007 goto done;
4008 got_object_commit_close(commit);
4009 } else {
4010 error = got_repo_match_object_id(&start_id, NULL,
4011 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4012 if (error != NULL)
4013 goto done;
4015 if (end_commit != NULL) {
4016 error = got_repo_match_object_id(&end_id, NULL,
4017 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4018 if (error != NULL)
4019 goto done;
4022 if (worktree) {
4024 * If a path was specified on the command line it was resolved
4025 * to a path in the work tree above. Prepend the work tree's
4026 * path prefix to obtain the corresponding in-repository path.
4028 if (path) {
4029 const char *prefix;
4030 prefix = got_worktree_get_path_prefix(worktree);
4031 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4032 (path[0] != '\0') ? "/" : "", path) == -1) {
4033 error = got_error_from_errno("asprintf");
4034 goto done;
4037 } else
4038 error = got_repo_map_path(&in_repo_path, repo,
4039 path ? path : "");
4040 if (error != NULL)
4041 goto done;
4042 if (in_repo_path) {
4043 free(path);
4044 path = in_repo_path;
4047 error = print_commits(start_id, end_id, repo, path ? path : "",
4048 show_changed_paths, show_patch, search_pattern, diff_context,
4049 limit, log_branches, reverse_display_order, refs_idmap);
4050 done:
4051 free(path);
4052 free(repo_path);
4053 free(cwd);
4054 if (worktree)
4055 got_worktree_close(worktree);
4056 if (repo) {
4057 const struct got_error *repo_error;
4058 repo_error = got_repo_close(repo);
4059 if (error == NULL)
4060 error = repo_error;
4062 if (refs_idmap)
4063 got_reflist_object_id_map_free(refs_idmap);
4064 got_ref_list_free(&refs);
4065 return error;
4068 __dead static void
4069 usage_diff(void)
4071 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4072 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4073 exit(1);
4076 struct print_diff_arg {
4077 struct got_repository *repo;
4078 struct got_worktree *worktree;
4079 int diff_context;
4080 const char *id_str;
4081 int header_shown;
4082 int diff_staged;
4083 int ignore_whitespace;
4084 int force_text_diff;
4088 * Create a file which contains the target path of a symlink so we can feed
4089 * it as content to the diff engine.
4091 static const struct got_error *
4092 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4093 const char *abspath)
4095 const struct got_error *err = NULL;
4096 char target_path[PATH_MAX];
4097 ssize_t target_len, outlen;
4099 *fd = -1;
4101 if (dirfd != -1) {
4102 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4103 if (target_len == -1)
4104 return got_error_from_errno2("readlinkat", abspath);
4105 } else {
4106 target_len = readlink(abspath, target_path, PATH_MAX);
4107 if (target_len == -1)
4108 return got_error_from_errno2("readlink", abspath);
4111 *fd = got_opentempfd();
4112 if (*fd == -1)
4113 return got_error_from_errno("got_opentempfd");
4115 outlen = write(*fd, target_path, target_len);
4116 if (outlen == -1) {
4117 err = got_error_from_errno("got_opentempfd");
4118 goto done;
4121 if (lseek(*fd, 0, SEEK_SET) == -1) {
4122 err = got_error_from_errno2("lseek", abspath);
4123 goto done;
4125 done:
4126 if (err) {
4127 close(*fd);
4128 *fd = -1;
4130 return err;
4133 static const struct got_error *
4134 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4135 const char *path, struct got_object_id *blob_id,
4136 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4137 int dirfd, const char *de_name)
4139 struct print_diff_arg *a = arg;
4140 const struct got_error *err = NULL;
4141 struct got_blob_object *blob1 = NULL;
4142 int fd = -1;
4143 FILE *f2 = NULL;
4144 char *abspath = NULL, *label1 = NULL;
4145 struct stat sb;
4147 if (a->diff_staged) {
4148 if (staged_status != GOT_STATUS_MODIFY &&
4149 staged_status != GOT_STATUS_ADD &&
4150 staged_status != GOT_STATUS_DELETE)
4151 return NULL;
4152 } else {
4153 if (staged_status == GOT_STATUS_DELETE)
4154 return NULL;
4155 if (status == GOT_STATUS_NONEXISTENT)
4156 return got_error_set_errno(ENOENT, path);
4157 if (status != GOT_STATUS_MODIFY &&
4158 status != GOT_STATUS_ADD &&
4159 status != GOT_STATUS_DELETE &&
4160 status != GOT_STATUS_CONFLICT)
4161 return NULL;
4164 if (!a->header_shown) {
4165 printf("diff %s %s%s\n", a->id_str,
4166 got_worktree_get_root_path(a->worktree),
4167 a->diff_staged ? " (staged changes)" : "");
4168 a->header_shown = 1;
4171 if (a->diff_staged) {
4172 const char *label1 = NULL, *label2 = NULL;
4173 switch (staged_status) {
4174 case GOT_STATUS_MODIFY:
4175 label1 = path;
4176 label2 = path;
4177 break;
4178 case GOT_STATUS_ADD:
4179 label2 = path;
4180 break;
4181 case GOT_STATUS_DELETE:
4182 label1 = path;
4183 break;
4184 default:
4185 return got_error(GOT_ERR_FILE_STATUS);
4187 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4188 staged_blob_id, label1, label2, a->diff_context,
4189 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4192 if (staged_status == GOT_STATUS_ADD ||
4193 staged_status == GOT_STATUS_MODIFY) {
4194 char *id_str;
4195 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4196 8192);
4197 if (err)
4198 goto done;
4199 err = got_object_id_str(&id_str, staged_blob_id);
4200 if (err)
4201 goto done;
4202 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4203 err = got_error_from_errno("asprintf");
4204 free(id_str);
4205 goto done;
4207 free(id_str);
4208 } else if (status != GOT_STATUS_ADD) {
4209 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4210 if (err)
4211 goto done;
4214 if (status != GOT_STATUS_DELETE) {
4215 if (asprintf(&abspath, "%s/%s",
4216 got_worktree_get_root_path(a->worktree), path) == -1) {
4217 err = got_error_from_errno("asprintf");
4218 goto done;
4221 if (dirfd != -1) {
4222 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4223 if (fd == -1) {
4224 if (errno != ELOOP) {
4225 err = got_error_from_errno2("openat",
4226 abspath);
4227 goto done;
4229 err = get_symlink_target_file(&fd, dirfd,
4230 de_name, abspath);
4231 if (err)
4232 goto done;
4234 } else {
4235 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4236 if (fd == -1) {
4237 if (errno != ELOOP) {
4238 err = got_error_from_errno2("open",
4239 abspath);
4240 goto done;
4242 err = get_symlink_target_file(&fd, dirfd,
4243 de_name, abspath);
4244 if (err)
4245 goto done;
4248 if (fstat(fd, &sb) == -1) {
4249 err = got_error_from_errno2("fstat", abspath);
4250 goto done;
4252 f2 = fdopen(fd, "r");
4253 if (f2 == NULL) {
4254 err = got_error_from_errno2("fdopen", abspath);
4255 goto done;
4257 fd = -1;
4258 } else
4259 sb.st_size = 0;
4261 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4262 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4263 done:
4264 if (blob1)
4265 got_object_blob_close(blob1);
4266 if (f2 && fclose(f2) == EOF && err == NULL)
4267 err = got_error_from_errno("fclose");
4268 if (fd != -1 && close(fd) == -1 && err == NULL)
4269 err = got_error_from_errno("close");
4270 free(abspath);
4271 return err;
4274 static const struct got_error *
4275 cmd_diff(int argc, char *argv[])
4277 const struct got_error *error;
4278 struct got_repository *repo = NULL;
4279 struct got_worktree *worktree = NULL;
4280 char *cwd = NULL, *repo_path = NULL;
4281 struct got_object_id *id1 = NULL, *id2 = NULL;
4282 const char *id_str1 = NULL, *id_str2 = NULL;
4283 char *label1 = NULL, *label2 = NULL;
4284 int type1, type2;
4285 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4286 int force_text_diff = 0;
4287 const char *errstr;
4288 char *path = NULL;
4289 struct got_reflist_head refs;
4291 TAILQ_INIT(&refs);
4293 #ifndef PROFILE
4294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4295 NULL) == -1)
4296 err(1, "pledge");
4297 #endif
4299 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4300 switch (ch) {
4301 case 'a':
4302 force_text_diff = 1;
4303 break;
4304 case 'C':
4305 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4306 &errstr);
4307 if (errstr != NULL)
4308 err(1, "-C option %s", errstr);
4309 break;
4310 case 'r':
4311 repo_path = realpath(optarg, NULL);
4312 if (repo_path == NULL)
4313 return got_error_from_errno2("realpath",
4314 optarg);
4315 got_path_strip_trailing_slashes(repo_path);
4316 break;
4317 case 's':
4318 diff_staged = 1;
4319 break;
4320 case 'w':
4321 ignore_whitespace = 1;
4322 break;
4323 default:
4324 usage_diff();
4325 /* NOTREACHED */
4329 argc -= optind;
4330 argv += optind;
4332 cwd = getcwd(NULL, 0);
4333 if (cwd == NULL) {
4334 error = got_error_from_errno("getcwd");
4335 goto done;
4337 if (argc <= 1) {
4338 if (repo_path)
4339 errx(1,
4340 "-r option can't be used when diffing a work tree");
4341 error = got_worktree_open(&worktree, cwd);
4342 if (error) {
4343 if (error->code == GOT_ERR_NOT_WORKTREE)
4344 error = wrap_not_worktree_error(error, "diff",
4345 cwd);
4346 goto done;
4348 repo_path = strdup(got_worktree_get_repo_path(worktree));
4349 if (repo_path == NULL) {
4350 error = got_error_from_errno("strdup");
4351 goto done;
4353 if (argc == 1) {
4354 error = got_worktree_resolve_path(&path, worktree,
4355 argv[0]);
4356 if (error)
4357 goto done;
4358 } else {
4359 path = strdup("");
4360 if (path == NULL) {
4361 error = got_error_from_errno("strdup");
4362 goto done;
4365 } else if (argc == 2) {
4366 if (diff_staged)
4367 errx(1, "-s option can't be used when diffing "
4368 "objects in repository");
4369 id_str1 = argv[0];
4370 id_str2 = argv[1];
4371 if (repo_path == NULL) {
4372 error = got_worktree_open(&worktree, cwd);
4373 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4374 goto done;
4375 if (worktree) {
4376 repo_path = strdup(
4377 got_worktree_get_repo_path(worktree));
4378 if (repo_path == NULL) {
4379 error = got_error_from_errno("strdup");
4380 goto done;
4382 } else {
4383 repo_path = strdup(cwd);
4384 if (repo_path == NULL) {
4385 error = got_error_from_errno("strdup");
4386 goto done;
4390 } else
4391 usage_diff();
4393 error = got_repo_open(&repo, repo_path, NULL);
4394 free(repo_path);
4395 if (error != NULL)
4396 goto done;
4398 error = apply_unveil(got_repo_get_path(repo), 1,
4399 worktree ? got_worktree_get_root_path(worktree) : NULL);
4400 if (error)
4401 goto done;
4403 if (argc <= 1) {
4404 struct print_diff_arg arg;
4405 struct got_pathlist_head paths;
4406 char *id_str;
4408 TAILQ_INIT(&paths);
4410 error = got_object_id_str(&id_str,
4411 got_worktree_get_base_commit_id(worktree));
4412 if (error)
4413 goto done;
4414 arg.repo = repo;
4415 arg.worktree = worktree;
4416 arg.diff_context = diff_context;
4417 arg.id_str = id_str;
4418 arg.header_shown = 0;
4419 arg.diff_staged = diff_staged;
4420 arg.ignore_whitespace = ignore_whitespace;
4421 arg.force_text_diff = force_text_diff;
4423 error = got_pathlist_append(&paths, path, NULL);
4424 if (error)
4425 goto done;
4427 error = got_worktree_status(worktree, &paths, repo, print_diff,
4428 &arg, check_cancelled, NULL);
4429 free(id_str);
4430 got_pathlist_free(&paths);
4431 goto done;
4434 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4435 if (error)
4436 return error;
4438 error = got_repo_match_object_id(&id1, &label1, id_str1,
4439 GOT_OBJ_TYPE_ANY, &refs, repo);
4440 if (error)
4441 goto done;
4443 error = got_repo_match_object_id(&id2, &label2, id_str2,
4444 GOT_OBJ_TYPE_ANY, &refs, repo);
4445 if (error)
4446 goto done;
4448 error = got_object_get_type(&type1, repo, id1);
4449 if (error)
4450 goto done;
4452 error = got_object_get_type(&type2, repo, id2);
4453 if (error)
4454 goto done;
4456 if (type1 != type2) {
4457 error = got_error(GOT_ERR_OBJ_TYPE);
4458 goto done;
4461 switch (type1) {
4462 case GOT_OBJ_TYPE_BLOB:
4463 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4464 NULL, NULL, diff_context, ignore_whitespace,
4465 force_text_diff, repo, stdout);
4466 break;
4467 case GOT_OBJ_TYPE_TREE:
4468 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4469 "", "", diff_context, ignore_whitespace, force_text_diff,
4470 repo, stdout);
4471 break;
4472 case GOT_OBJ_TYPE_COMMIT:
4473 printf("diff %s %s\n", label1, label2);
4474 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4475 diff_context, ignore_whitespace, force_text_diff, repo,
4476 stdout);
4477 break;
4478 default:
4479 error = got_error(GOT_ERR_OBJ_TYPE);
4481 done:
4482 free(label1);
4483 free(label2);
4484 free(id1);
4485 free(id2);
4486 free(path);
4487 if (worktree)
4488 got_worktree_close(worktree);
4489 if (repo) {
4490 const struct got_error *repo_error;
4491 repo_error = got_repo_close(repo);
4492 if (error == NULL)
4493 error = repo_error;
4495 got_ref_list_free(&refs);
4496 return error;
4499 __dead static void
4500 usage_blame(void)
4502 fprintf(stderr,
4503 "usage: %s blame [-c commit] [-r repository-path] path\n",
4504 getprogname());
4505 exit(1);
4508 struct blame_line {
4509 int annotated;
4510 char *id_str;
4511 char *committer;
4512 char datebuf[11]; /* YYYY-MM-DD + NUL */
4515 struct blame_cb_args {
4516 struct blame_line *lines;
4517 int nlines;
4518 int nlines_prec;
4519 int lineno_cur;
4520 off_t *line_offsets;
4521 FILE *f;
4522 struct got_repository *repo;
4525 static const struct got_error *
4526 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4528 const struct got_error *err = NULL;
4529 struct blame_cb_args *a = arg;
4530 struct blame_line *bline;
4531 char *line = NULL;
4532 size_t linesize = 0;
4533 struct got_commit_object *commit = NULL;
4534 off_t offset;
4535 struct tm tm;
4536 time_t committer_time;
4538 if (nlines != a->nlines ||
4539 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4540 return got_error(GOT_ERR_RANGE);
4542 if (sigint_received)
4543 return got_error(GOT_ERR_ITER_COMPLETED);
4545 if (lineno == -1)
4546 return NULL; /* no change in this commit */
4548 /* Annotate this line. */
4549 bline = &a->lines[lineno - 1];
4550 if (bline->annotated)
4551 return NULL;
4552 err = got_object_id_str(&bline->id_str, id);
4553 if (err)
4554 return err;
4556 err = got_object_open_as_commit(&commit, a->repo, id);
4557 if (err)
4558 goto done;
4560 bline->committer = strdup(got_object_commit_get_committer(commit));
4561 if (bline->committer == NULL) {
4562 err = got_error_from_errno("strdup");
4563 goto done;
4566 committer_time = got_object_commit_get_committer_time(commit);
4567 if (localtime_r(&committer_time, &tm) == NULL)
4568 return got_error_from_errno("localtime_r");
4569 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4570 &tm) >= sizeof(bline->datebuf)) {
4571 err = got_error(GOT_ERR_NO_SPACE);
4572 goto done;
4574 bline->annotated = 1;
4576 /* Print lines annotated so far. */
4577 bline = &a->lines[a->lineno_cur - 1];
4578 if (!bline->annotated)
4579 goto done;
4581 offset = a->line_offsets[a->lineno_cur - 1];
4582 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4583 err = got_error_from_errno("fseeko");
4584 goto done;
4587 while (bline->annotated) {
4588 char *smallerthan, *at, *nl, *committer;
4589 size_t len;
4591 if (getline(&line, &linesize, a->f) == -1) {
4592 if (ferror(a->f))
4593 err = got_error_from_errno("getline");
4594 break;
4597 committer = bline->committer;
4598 smallerthan = strchr(committer, '<');
4599 if (smallerthan && smallerthan[1] != '\0')
4600 committer = smallerthan + 1;
4601 at = strchr(committer, '@');
4602 if (at)
4603 *at = '\0';
4604 len = strlen(committer);
4605 if (len >= 9)
4606 committer[8] = '\0';
4608 nl = strchr(line, '\n');
4609 if (nl)
4610 *nl = '\0';
4611 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4612 bline->id_str, bline->datebuf, committer, line);
4614 a->lineno_cur++;
4615 bline = &a->lines[a->lineno_cur - 1];
4617 done:
4618 if (commit)
4619 got_object_commit_close(commit);
4620 free(line);
4621 return err;
4624 static const struct got_error *
4625 cmd_blame(int argc, char *argv[])
4627 const struct got_error *error;
4628 struct got_repository *repo = NULL;
4629 struct got_worktree *worktree = NULL;
4630 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4631 char *link_target = NULL;
4632 struct got_object_id *obj_id = NULL;
4633 struct got_object_id *commit_id = NULL;
4634 struct got_blob_object *blob = NULL;
4635 char *commit_id_str = NULL;
4636 struct blame_cb_args bca;
4637 int ch, obj_type, i;
4638 off_t filesize;
4640 memset(&bca, 0, sizeof(bca));
4642 #ifndef PROFILE
4643 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4644 NULL) == -1)
4645 err(1, "pledge");
4646 #endif
4648 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4649 switch (ch) {
4650 case 'c':
4651 commit_id_str = optarg;
4652 break;
4653 case 'r':
4654 repo_path = realpath(optarg, NULL);
4655 if (repo_path == NULL)
4656 return got_error_from_errno2("realpath",
4657 optarg);
4658 got_path_strip_trailing_slashes(repo_path);
4659 break;
4660 default:
4661 usage_blame();
4662 /* NOTREACHED */
4666 argc -= optind;
4667 argv += optind;
4669 if (argc == 1)
4670 path = argv[0];
4671 else
4672 usage_blame();
4674 cwd = getcwd(NULL, 0);
4675 if (cwd == NULL) {
4676 error = got_error_from_errno("getcwd");
4677 goto done;
4679 if (repo_path == NULL) {
4680 error = got_worktree_open(&worktree, cwd);
4681 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4682 goto done;
4683 else
4684 error = NULL;
4685 if (worktree) {
4686 repo_path =
4687 strdup(got_worktree_get_repo_path(worktree));
4688 if (repo_path == NULL) {
4689 error = got_error_from_errno("strdup");
4690 if (error)
4691 goto done;
4693 } else {
4694 repo_path = strdup(cwd);
4695 if (repo_path == NULL) {
4696 error = got_error_from_errno("strdup");
4697 goto done;
4702 error = got_repo_open(&repo, repo_path, NULL);
4703 if (error != NULL)
4704 goto done;
4706 if (worktree) {
4707 const char *prefix = got_worktree_get_path_prefix(worktree);
4708 char *p;
4710 error = got_worktree_resolve_path(&p, worktree, path);
4711 if (error)
4712 goto done;
4713 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4714 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4715 p) == -1) {
4716 error = got_error_from_errno("asprintf");
4717 free(p);
4718 goto done;
4720 free(p);
4721 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4722 } else {
4723 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4724 if (error)
4725 goto done;
4726 error = got_repo_map_path(&in_repo_path, repo, path);
4728 if (error)
4729 goto done;
4731 if (commit_id_str == NULL) {
4732 struct got_reference *head_ref;
4733 error = got_ref_open(&head_ref, repo, worktree ?
4734 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4735 if (error != NULL)
4736 goto done;
4737 error = got_ref_resolve(&commit_id, repo, head_ref);
4738 got_ref_close(head_ref);
4739 if (error != NULL)
4740 goto done;
4741 } else {
4742 struct got_reflist_head refs;
4743 TAILQ_INIT(&refs);
4744 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4745 NULL);
4746 if (error)
4747 goto done;
4748 error = got_repo_match_object_id(&commit_id, NULL,
4749 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4750 got_ref_list_free(&refs);
4751 if (error)
4752 goto done;
4755 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4756 commit_id, repo);
4757 if (error)
4758 goto done;
4760 error = got_object_id_by_path(&obj_id, repo, commit_id,
4761 link_target ? link_target : in_repo_path);
4762 if (error)
4763 goto done;
4765 error = got_object_get_type(&obj_type, repo, obj_id);
4766 if (error)
4767 goto done;
4769 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4770 error = got_error_path(link_target ? link_target : in_repo_path,
4771 GOT_ERR_OBJ_TYPE);
4772 goto done;
4775 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4776 if (error)
4777 goto done;
4778 bca.f = got_opentemp();
4779 if (bca.f == NULL) {
4780 error = got_error_from_errno("got_opentemp");
4781 goto done;
4783 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4784 &bca.line_offsets, bca.f, blob);
4785 if (error || bca.nlines == 0)
4786 goto done;
4788 /* Don't include \n at EOF in the blame line count. */
4789 if (bca.line_offsets[bca.nlines - 1] == filesize)
4790 bca.nlines--;
4792 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4793 if (bca.lines == NULL) {
4794 error = got_error_from_errno("calloc");
4795 goto done;
4797 bca.lineno_cur = 1;
4798 bca.nlines_prec = 0;
4799 i = bca.nlines;
4800 while (i > 0) {
4801 i /= 10;
4802 bca.nlines_prec++;
4804 bca.repo = repo;
4806 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4807 repo, blame_cb, &bca, check_cancelled, NULL);
4808 done:
4809 free(in_repo_path);
4810 free(link_target);
4811 free(repo_path);
4812 free(cwd);
4813 free(commit_id);
4814 free(obj_id);
4815 if (blob)
4816 got_object_blob_close(blob);
4817 if (worktree)
4818 got_worktree_close(worktree);
4819 if (repo) {
4820 const struct got_error *repo_error;
4821 repo_error = got_repo_close(repo);
4822 if (error == NULL)
4823 error = repo_error;
4825 if (bca.lines) {
4826 for (i = 0; i < bca.nlines; i++) {
4827 struct blame_line *bline = &bca.lines[i];
4828 free(bline->id_str);
4829 free(bline->committer);
4831 free(bca.lines);
4833 free(bca.line_offsets);
4834 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4835 error = got_error_from_errno("fclose");
4836 return error;
4839 __dead static void
4840 usage_tree(void)
4842 fprintf(stderr,
4843 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4844 getprogname());
4845 exit(1);
4848 static const struct got_error *
4849 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4850 const char *root_path, struct got_repository *repo)
4852 const struct got_error *err = NULL;
4853 int is_root_path = (strcmp(path, root_path) == 0);
4854 const char *modestr = "";
4855 mode_t mode = got_tree_entry_get_mode(te);
4856 char *link_target = NULL;
4858 path += strlen(root_path);
4859 while (path[0] == '/')
4860 path++;
4862 if (got_object_tree_entry_is_submodule(te))
4863 modestr = "$";
4864 else if (S_ISLNK(mode)) {
4865 int i;
4867 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4868 if (err)
4869 return err;
4870 for (i = 0; i < strlen(link_target); i++) {
4871 if (!isprint((unsigned char)link_target[i]))
4872 link_target[i] = '?';
4875 modestr = "@";
4877 else if (S_ISDIR(mode))
4878 modestr = "/";
4879 else if (mode & S_IXUSR)
4880 modestr = "*";
4882 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4883 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4884 link_target ? " -> ": "", link_target ? link_target : "");
4886 free(link_target);
4887 return NULL;
4890 static const struct got_error *
4891 print_tree(const char *path, struct got_object_id *commit_id,
4892 int show_ids, int recurse, const char *root_path,
4893 struct got_repository *repo)
4895 const struct got_error *err = NULL;
4896 struct got_object_id *tree_id = NULL;
4897 struct got_tree_object *tree = NULL;
4898 int nentries, i;
4900 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4901 if (err)
4902 goto done;
4904 err = got_object_open_as_tree(&tree, repo, tree_id);
4905 if (err)
4906 goto done;
4907 nentries = got_object_tree_get_nentries(tree);
4908 for (i = 0; i < nentries; i++) {
4909 struct got_tree_entry *te;
4910 char *id = NULL;
4912 if (sigint_received || sigpipe_received)
4913 break;
4915 te = got_object_tree_get_entry(tree, i);
4916 if (show_ids) {
4917 char *id_str;
4918 err = got_object_id_str(&id_str,
4919 got_tree_entry_get_id(te));
4920 if (err)
4921 goto done;
4922 if (asprintf(&id, "%s ", id_str) == -1) {
4923 err = got_error_from_errno("asprintf");
4924 free(id_str);
4925 goto done;
4927 free(id_str);
4929 err = print_entry(te, id, path, root_path, repo);
4930 free(id);
4931 if (err)
4932 goto done;
4934 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4935 char *child_path;
4936 if (asprintf(&child_path, "%s%s%s", path,
4937 path[0] == '/' && path[1] == '\0' ? "" : "/",
4938 got_tree_entry_get_name(te)) == -1) {
4939 err = got_error_from_errno("asprintf");
4940 goto done;
4942 err = print_tree(child_path, commit_id, show_ids, 1,
4943 root_path, repo);
4944 free(child_path);
4945 if (err)
4946 goto done;
4949 done:
4950 if (tree)
4951 got_object_tree_close(tree);
4952 free(tree_id);
4953 return err;
4956 static const struct got_error *
4957 cmd_tree(int argc, char *argv[])
4959 const struct got_error *error;
4960 struct got_repository *repo = NULL;
4961 struct got_worktree *worktree = NULL;
4962 const char *path, *refname = NULL;
4963 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4964 struct got_object_id *commit_id = NULL;
4965 char *commit_id_str = NULL;
4966 int show_ids = 0, recurse = 0;
4967 int ch;
4969 #ifndef PROFILE
4970 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4971 NULL) == -1)
4972 err(1, "pledge");
4973 #endif
4975 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4976 switch (ch) {
4977 case 'c':
4978 commit_id_str = optarg;
4979 break;
4980 case 'r':
4981 repo_path = realpath(optarg, NULL);
4982 if (repo_path == NULL)
4983 return got_error_from_errno2("realpath",
4984 optarg);
4985 got_path_strip_trailing_slashes(repo_path);
4986 break;
4987 case 'i':
4988 show_ids = 1;
4989 break;
4990 case 'R':
4991 recurse = 1;
4992 break;
4993 default:
4994 usage_tree();
4995 /* NOTREACHED */
4999 argc -= optind;
5000 argv += optind;
5002 if (argc == 1)
5003 path = argv[0];
5004 else if (argc > 1)
5005 usage_tree();
5006 else
5007 path = NULL;
5009 cwd = getcwd(NULL, 0);
5010 if (cwd == NULL) {
5011 error = got_error_from_errno("getcwd");
5012 goto done;
5014 if (repo_path == NULL) {
5015 error = got_worktree_open(&worktree, cwd);
5016 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5017 goto done;
5018 else
5019 error = NULL;
5020 if (worktree) {
5021 repo_path =
5022 strdup(got_worktree_get_repo_path(worktree));
5023 if (repo_path == NULL)
5024 error = got_error_from_errno("strdup");
5025 if (error)
5026 goto done;
5027 } else {
5028 repo_path = strdup(cwd);
5029 if (repo_path == NULL) {
5030 error = got_error_from_errno("strdup");
5031 goto done;
5036 error = got_repo_open(&repo, repo_path, NULL);
5037 if (error != NULL)
5038 goto done;
5040 if (worktree) {
5041 const char *prefix = got_worktree_get_path_prefix(worktree);
5042 char *p;
5044 if (path == NULL)
5045 path = "";
5046 error = got_worktree_resolve_path(&p, worktree, path);
5047 if (error)
5048 goto done;
5049 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5050 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5051 p) == -1) {
5052 error = got_error_from_errno("asprintf");
5053 free(p);
5054 goto done;
5056 free(p);
5057 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5058 if (error)
5059 goto done;
5060 } else {
5061 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5062 if (error)
5063 goto done;
5064 if (path == NULL)
5065 path = "/";
5066 error = got_repo_map_path(&in_repo_path, repo, path);
5067 if (error != NULL)
5068 goto done;
5071 if (commit_id_str == NULL) {
5072 struct got_reference *head_ref;
5073 if (worktree)
5074 refname = got_worktree_get_head_ref_name(worktree);
5075 else
5076 refname = GOT_REF_HEAD;
5077 error = got_ref_open(&head_ref, repo, refname, 0);
5078 if (error != NULL)
5079 goto done;
5080 error = got_ref_resolve(&commit_id, repo, head_ref);
5081 got_ref_close(head_ref);
5082 if (error != NULL)
5083 goto done;
5084 } else {
5085 struct got_reflist_head refs;
5086 TAILQ_INIT(&refs);
5087 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5088 NULL);
5089 if (error)
5090 goto done;
5091 error = got_repo_match_object_id(&commit_id, NULL,
5092 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5093 got_ref_list_free(&refs);
5094 if (error)
5095 goto done;
5098 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5099 in_repo_path, repo);
5100 done:
5101 free(in_repo_path);
5102 free(repo_path);
5103 free(cwd);
5104 free(commit_id);
5105 if (worktree)
5106 got_worktree_close(worktree);
5107 if (repo) {
5108 const struct got_error *repo_error;
5109 repo_error = got_repo_close(repo);
5110 if (error == NULL)
5111 error = repo_error;
5113 return error;
5116 __dead static void
5117 usage_status(void)
5119 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5120 getprogname());
5121 exit(1);
5124 static const struct got_error *
5125 print_status(void *arg, unsigned char status, unsigned char staged_status,
5126 const char *path, struct got_object_id *blob_id,
5127 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5128 int dirfd, const char *de_name)
5130 if (status == staged_status && (status == GOT_STATUS_DELETE))
5131 status = GOT_STATUS_NO_CHANGE;
5132 if (arg) {
5133 char *status_codes = arg;
5134 size_t ncodes = strlen(status_codes);
5135 int i;
5136 for (i = 0; i < ncodes ; i++) {
5137 if (status == status_codes[i] ||
5138 staged_status == status_codes[i])
5139 break;
5141 if (i == ncodes)
5142 return NULL;
5144 printf("%c%c %s\n", status, staged_status, path);
5145 return NULL;
5148 static const struct got_error *
5149 cmd_status(int argc, char *argv[])
5151 const struct got_error *error = NULL;
5152 struct got_repository *repo = NULL;
5153 struct got_worktree *worktree = NULL;
5154 char *cwd = NULL, *status_codes = NULL;;
5155 struct got_pathlist_head paths;
5156 struct got_pathlist_entry *pe;
5157 int ch, i;
5159 TAILQ_INIT(&paths);
5161 while ((ch = getopt(argc, argv, "s:")) != -1) {
5162 switch (ch) {
5163 case 's':
5164 for (i = 0; i < strlen(optarg); i++) {
5165 switch (optarg[i]) {
5166 case GOT_STATUS_MODIFY:
5167 case GOT_STATUS_ADD:
5168 case GOT_STATUS_DELETE:
5169 case GOT_STATUS_CONFLICT:
5170 case GOT_STATUS_MISSING:
5171 case GOT_STATUS_OBSTRUCTED:
5172 case GOT_STATUS_UNVERSIONED:
5173 case GOT_STATUS_MODE_CHANGE:
5174 case GOT_STATUS_NONEXISTENT:
5175 break;
5176 default:
5177 errx(1, "invalid status code '%c'",
5178 optarg[i]);
5181 status_codes = optarg;
5182 break;
5183 default:
5184 usage_status();
5185 /* NOTREACHED */
5189 argc -= optind;
5190 argv += optind;
5192 #ifndef PROFILE
5193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5194 NULL) == -1)
5195 err(1, "pledge");
5196 #endif
5197 cwd = getcwd(NULL, 0);
5198 if (cwd == NULL) {
5199 error = got_error_from_errno("getcwd");
5200 goto done;
5203 error = got_worktree_open(&worktree, cwd);
5204 if (error) {
5205 if (error->code == GOT_ERR_NOT_WORKTREE)
5206 error = wrap_not_worktree_error(error, "status", cwd);
5207 goto done;
5210 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5211 NULL);
5212 if (error != NULL)
5213 goto done;
5215 error = apply_unveil(got_repo_get_path(repo), 1,
5216 got_worktree_get_root_path(worktree));
5217 if (error)
5218 goto done;
5220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5221 if (error)
5222 goto done;
5224 error = got_worktree_status(worktree, &paths, repo, print_status,
5225 status_codes, check_cancelled, NULL);
5226 done:
5227 TAILQ_FOREACH(pe, &paths, entry)
5228 free((char *)pe->path);
5229 got_pathlist_free(&paths);
5230 free(cwd);
5231 return error;
5234 __dead static void
5235 usage_ref(void)
5237 fprintf(stderr,
5238 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5239 "[-d] [name]\n",
5240 getprogname());
5241 exit(1);
5244 static const struct got_error *
5245 list_refs(struct got_repository *repo, const char *refname)
5247 static const struct got_error *err = NULL;
5248 struct got_reflist_head refs;
5249 struct got_reflist_entry *re;
5251 TAILQ_INIT(&refs);
5252 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5253 if (err)
5254 return err;
5256 TAILQ_FOREACH(re, &refs, entry) {
5257 char *refstr;
5258 refstr = got_ref_to_str(re->ref);
5259 if (refstr == NULL)
5260 return got_error_from_errno("got_ref_to_str");
5261 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5262 free(refstr);
5265 got_ref_list_free(&refs);
5266 return NULL;
5269 static const struct got_error *
5270 delete_ref(struct got_repository *repo, const char *refname)
5272 const struct got_error *err = NULL;
5273 struct got_reference *ref;
5275 err = got_ref_open(&ref, repo, refname, 0);
5276 if (err)
5277 return err;
5279 err = got_ref_delete(ref, repo);
5280 got_ref_close(ref);
5281 return err;
5284 static const struct got_error *
5285 add_ref(struct got_repository *repo, const char *refname, const char *target)
5287 const struct got_error *err = NULL;
5288 struct got_object_id *id;
5289 struct got_reference *ref = NULL;
5292 * Don't let the user create a reference name with a leading '-'.
5293 * While technically a valid reference name, this case is usually
5294 * an unintended typo.
5296 if (refname[0] == '-')
5297 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5299 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5300 repo);
5301 if (err) {
5302 struct got_reference *target_ref;
5304 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5305 return err;
5306 err = got_ref_open(&target_ref, repo, target, 0);
5307 if (err)
5308 return err;
5309 err = got_ref_resolve(&id, repo, target_ref);
5310 got_ref_close(target_ref);
5311 if (err)
5312 return err;
5315 err = got_ref_alloc(&ref, refname, id);
5316 if (err)
5317 goto done;
5319 err = got_ref_write(ref, repo);
5320 done:
5321 if (ref)
5322 got_ref_close(ref);
5323 free(id);
5324 return err;
5327 static const struct got_error *
5328 add_symref(struct got_repository *repo, const char *refname, const char *target)
5330 const struct got_error *err = NULL;
5331 struct got_reference *ref = NULL;
5332 struct got_reference *target_ref = NULL;
5335 * Don't let the user create a reference name with a leading '-'.
5336 * While technically a valid reference name, this case is usually
5337 * an unintended typo.
5339 if (refname[0] == '-')
5340 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5342 err = got_ref_open(&target_ref, repo, target, 0);
5343 if (err)
5344 return err;
5346 err = got_ref_alloc_symref(&ref, refname, target_ref);
5347 if (err)
5348 goto done;
5350 err = got_ref_write(ref, repo);
5351 done:
5352 if (target_ref)
5353 got_ref_close(target_ref);
5354 if (ref)
5355 got_ref_close(ref);
5356 return err;
5359 static const struct got_error *
5360 cmd_ref(int argc, char *argv[])
5362 const struct got_error *error = NULL;
5363 struct got_repository *repo = NULL;
5364 struct got_worktree *worktree = NULL;
5365 char *cwd = NULL, *repo_path = NULL;
5366 int ch, do_list = 0, do_delete = 0;
5367 const char *obj_arg = NULL, *symref_target= NULL;
5368 char *refname = NULL;
5370 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5371 switch (ch) {
5372 case 'c':
5373 obj_arg = optarg;
5374 break;
5375 case 'd':
5376 do_delete = 1;
5377 break;
5378 case 'r':
5379 repo_path = realpath(optarg, NULL);
5380 if (repo_path == NULL)
5381 return got_error_from_errno2("realpath",
5382 optarg);
5383 got_path_strip_trailing_slashes(repo_path);
5384 break;
5385 case 'l':
5386 do_list = 1;
5387 break;
5388 case 's':
5389 symref_target = optarg;
5390 break;
5391 default:
5392 usage_ref();
5393 /* NOTREACHED */
5397 if (obj_arg && do_list)
5398 option_conflict('c', 'l');
5399 if (obj_arg && do_delete)
5400 option_conflict('c', 'd');
5401 if (obj_arg && symref_target)
5402 option_conflict('c', 's');
5403 if (symref_target && do_delete)
5404 option_conflict('s', 'd');
5405 if (symref_target && do_list)
5406 option_conflict('s', 'l');
5407 if (do_delete && do_list)
5408 option_conflict('d', 'l');
5410 argc -= optind;
5411 argv += optind;
5413 if (do_list) {
5414 if (argc != 0 && argc != 1)
5415 usage_ref();
5416 if (argc == 1) {
5417 refname = strdup(argv[0]);
5418 if (refname == NULL) {
5419 error = got_error_from_errno("strdup");
5420 goto done;
5423 } else {
5424 if (argc != 1)
5425 usage_ref();
5426 refname = strdup(argv[0]);
5427 if (refname == NULL) {
5428 error = got_error_from_errno("strdup");
5429 goto done;
5433 if (refname)
5434 got_path_strip_trailing_slashes(refname);
5436 #ifndef PROFILE
5437 if (do_list) {
5438 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5439 NULL) == -1)
5440 err(1, "pledge");
5441 } else {
5442 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5443 "sendfd unveil", NULL) == -1)
5444 err(1, "pledge");
5446 #endif
5447 cwd = getcwd(NULL, 0);
5448 if (cwd == NULL) {
5449 error = got_error_from_errno("getcwd");
5450 goto done;
5453 if (repo_path == NULL) {
5454 error = got_worktree_open(&worktree, cwd);
5455 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5456 goto done;
5457 else
5458 error = NULL;
5459 if (worktree) {
5460 repo_path =
5461 strdup(got_worktree_get_repo_path(worktree));
5462 if (repo_path == NULL)
5463 error = got_error_from_errno("strdup");
5464 if (error)
5465 goto done;
5466 } else {
5467 repo_path = strdup(cwd);
5468 if (repo_path == NULL) {
5469 error = got_error_from_errno("strdup");
5470 goto done;
5475 error = got_repo_open(&repo, repo_path, NULL);
5476 if (error != NULL)
5477 goto done;
5479 error = apply_unveil(got_repo_get_path(repo), do_list,
5480 worktree ? got_worktree_get_root_path(worktree) : NULL);
5481 if (error)
5482 goto done;
5484 if (do_list)
5485 error = list_refs(repo, refname);
5486 else if (do_delete)
5487 error = delete_ref(repo, refname);
5488 else if (symref_target)
5489 error = add_symref(repo, refname, symref_target);
5490 else {
5491 if (obj_arg == NULL)
5492 usage_ref();
5493 error = add_ref(repo, refname, obj_arg);
5495 done:
5496 free(refname);
5497 if (repo)
5498 got_repo_close(repo);
5499 if (worktree)
5500 got_worktree_close(worktree);
5501 free(cwd);
5502 free(repo_path);
5503 return error;
5506 __dead static void
5507 usage_branch(void)
5509 fprintf(stderr,
5510 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5511 "[name]\n", getprogname());
5512 exit(1);
5515 static const struct got_error *
5516 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5517 struct got_reference *ref)
5519 const struct got_error *err = NULL;
5520 const char *refname, *marker = " ";
5521 char *refstr;
5523 refname = got_ref_get_name(ref);
5524 if (worktree && strcmp(refname,
5525 got_worktree_get_head_ref_name(worktree)) == 0) {
5526 struct got_object_id *id = NULL;
5528 err = got_ref_resolve(&id, repo, ref);
5529 if (err)
5530 return err;
5531 if (got_object_id_cmp(id,
5532 got_worktree_get_base_commit_id(worktree)) == 0)
5533 marker = "* ";
5534 else
5535 marker = "~ ";
5536 free(id);
5539 if (strncmp(refname, "refs/heads/", 11) == 0)
5540 refname += 11;
5541 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5542 refname += 18;
5544 refstr = got_ref_to_str(ref);
5545 if (refstr == NULL)
5546 return got_error_from_errno("got_ref_to_str");
5548 printf("%s%s: %s\n", marker, refname, refstr);
5549 free(refstr);
5550 return NULL;
5553 static const struct got_error *
5554 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5556 const char *refname;
5558 if (worktree == NULL)
5559 return got_error(GOT_ERR_NOT_WORKTREE);
5561 refname = got_worktree_get_head_ref_name(worktree);
5563 if (strncmp(refname, "refs/heads/", 11) == 0)
5564 refname += 11;
5565 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5566 refname += 18;
5568 printf("%s\n", refname);
5570 return NULL;
5573 static const struct got_error *
5574 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5576 static const struct got_error *err = NULL;
5577 struct got_reflist_head refs;
5578 struct got_reflist_entry *re;
5579 struct got_reference *temp_ref = NULL;
5580 int rebase_in_progress, histedit_in_progress;
5582 TAILQ_INIT(&refs);
5584 if (worktree) {
5585 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5586 worktree);
5587 if (err)
5588 return err;
5590 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5591 worktree);
5592 if (err)
5593 return err;
5595 if (rebase_in_progress || histedit_in_progress) {
5596 err = got_ref_open(&temp_ref, repo,
5597 got_worktree_get_head_ref_name(worktree), 0);
5598 if (err)
5599 return err;
5600 list_branch(repo, worktree, temp_ref);
5601 got_ref_close(temp_ref);
5605 err = got_ref_list(&refs, repo, "refs/heads",
5606 got_ref_cmp_by_name, NULL);
5607 if (err)
5608 return err;
5610 TAILQ_FOREACH(re, &refs, entry)
5611 list_branch(repo, worktree, re->ref);
5613 got_ref_list_free(&refs);
5614 return NULL;
5617 static const struct got_error *
5618 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5619 const char *branch_name)
5621 const struct got_error *err = NULL;
5622 struct got_reference *ref = NULL;
5623 char *refname;
5625 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5626 return got_error_from_errno("asprintf");
5628 err = got_ref_open(&ref, repo, refname, 0);
5629 if (err)
5630 goto done;
5632 if (worktree &&
5633 strcmp(got_worktree_get_head_ref_name(worktree),
5634 got_ref_get_name(ref)) == 0) {
5635 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5636 "will not delete this work tree's current branch");
5637 goto done;
5640 err = got_ref_delete(ref, repo);
5641 done:
5642 if (ref)
5643 got_ref_close(ref);
5644 free(refname);
5645 return err;
5648 static const struct got_error *
5649 add_branch(struct got_repository *repo, const char *branch_name,
5650 struct got_object_id *base_commit_id)
5652 const struct got_error *err = NULL;
5653 struct got_reference *ref = NULL;
5654 char *base_refname = NULL, *refname = NULL;
5657 * Don't let the user create a branch name with a leading '-'.
5658 * While technically a valid reference name, this case is usually
5659 * an unintended typo.
5661 if (branch_name[0] == '-')
5662 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5664 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5665 err = got_error_from_errno("asprintf");
5666 goto done;
5669 err = got_ref_open(&ref, repo, refname, 0);
5670 if (err == NULL) {
5671 err = got_error(GOT_ERR_BRANCH_EXISTS);
5672 goto done;
5673 } else if (err->code != GOT_ERR_NOT_REF)
5674 goto done;
5676 err = got_ref_alloc(&ref, refname, base_commit_id);
5677 if (err)
5678 goto done;
5680 err = got_ref_write(ref, repo);
5681 done:
5682 if (ref)
5683 got_ref_close(ref);
5684 free(base_refname);
5685 free(refname);
5686 return err;
5689 static const struct got_error *
5690 cmd_branch(int argc, char *argv[])
5692 const struct got_error *error = NULL;
5693 struct got_repository *repo = NULL;
5694 struct got_worktree *worktree = NULL;
5695 char *cwd = NULL, *repo_path = NULL;
5696 int ch, do_list = 0, do_show = 0, do_update = 1;
5697 const char *delref = NULL, *commit_id_arg = NULL;
5698 struct got_reference *ref = NULL;
5699 struct got_pathlist_head paths;
5700 struct got_pathlist_entry *pe;
5701 struct got_object_id *commit_id = NULL;
5702 char *commit_id_str = NULL;
5704 TAILQ_INIT(&paths);
5706 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5707 switch (ch) {
5708 case 'c':
5709 commit_id_arg = optarg;
5710 break;
5711 case 'd':
5712 delref = optarg;
5713 break;
5714 case 'r':
5715 repo_path = realpath(optarg, NULL);
5716 if (repo_path == NULL)
5717 return got_error_from_errno2("realpath",
5718 optarg);
5719 got_path_strip_trailing_slashes(repo_path);
5720 break;
5721 case 'l':
5722 do_list = 1;
5723 break;
5724 case 'n':
5725 do_update = 0;
5726 break;
5727 default:
5728 usage_branch();
5729 /* NOTREACHED */
5733 if (do_list && delref)
5734 option_conflict('l', 'd');
5736 argc -= optind;
5737 argv += optind;
5739 if (!do_list && !delref && argc == 0)
5740 do_show = 1;
5742 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5743 errx(1, "-c option can only be used when creating a branch");
5745 if (do_list || delref) {
5746 if (argc > 0)
5747 usage_branch();
5748 } else if (!do_show && argc != 1)
5749 usage_branch();
5751 #ifndef PROFILE
5752 if (do_list || do_show) {
5753 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5754 NULL) == -1)
5755 err(1, "pledge");
5756 } else {
5757 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5758 "sendfd unveil", NULL) == -1)
5759 err(1, "pledge");
5761 #endif
5762 cwd = getcwd(NULL, 0);
5763 if (cwd == NULL) {
5764 error = got_error_from_errno("getcwd");
5765 goto done;
5768 if (repo_path == NULL) {
5769 error = got_worktree_open(&worktree, cwd);
5770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5771 goto done;
5772 else
5773 error = NULL;
5774 if (worktree) {
5775 repo_path =
5776 strdup(got_worktree_get_repo_path(worktree));
5777 if (repo_path == NULL)
5778 error = got_error_from_errno("strdup");
5779 if (error)
5780 goto done;
5781 } else {
5782 repo_path = strdup(cwd);
5783 if (repo_path == NULL) {
5784 error = got_error_from_errno("strdup");
5785 goto done;
5790 error = got_repo_open(&repo, repo_path, NULL);
5791 if (error != NULL)
5792 goto done;
5794 error = apply_unveil(got_repo_get_path(repo), do_list,
5795 worktree ? got_worktree_get_root_path(worktree) : NULL);
5796 if (error)
5797 goto done;
5799 if (do_show)
5800 error = show_current_branch(repo, worktree);
5801 else if (do_list)
5802 error = list_branches(repo, worktree);
5803 else if (delref)
5804 error = delete_branch(repo, worktree, delref);
5805 else {
5806 struct got_reflist_head refs;
5807 TAILQ_INIT(&refs);
5808 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5809 NULL);
5810 if (error)
5811 goto done;
5812 if (commit_id_arg == NULL)
5813 commit_id_arg = worktree ?
5814 got_worktree_get_head_ref_name(worktree) :
5815 GOT_REF_HEAD;
5816 error = got_repo_match_object_id(&commit_id, NULL,
5817 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5818 got_ref_list_free(&refs);
5819 if (error)
5820 goto done;
5821 error = add_branch(repo, argv[0], commit_id);
5822 if (error)
5823 goto done;
5824 if (worktree && do_update) {
5825 struct got_update_progress_arg upa;
5826 char *branch_refname = NULL;
5828 error = got_object_id_str(&commit_id_str, commit_id);
5829 if (error)
5830 goto done;
5831 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5832 worktree);
5833 if (error)
5834 goto done;
5835 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5836 == -1) {
5837 error = got_error_from_errno("asprintf");
5838 goto done;
5840 error = got_ref_open(&ref, repo, branch_refname, 0);
5841 free(branch_refname);
5842 if (error)
5843 goto done;
5844 error = switch_head_ref(ref, commit_id, worktree,
5845 repo);
5846 if (error)
5847 goto done;
5848 error = got_worktree_set_base_commit_id(worktree, repo,
5849 commit_id);
5850 if (error)
5851 goto done;
5852 memset(&upa, 0, sizeof(upa));
5853 error = got_worktree_checkout_files(worktree, &paths,
5854 repo, update_progress, &upa, check_cancelled,
5855 NULL);
5856 if (error)
5857 goto done;
5858 if (upa.did_something)
5859 printf("Updated to commit %s\n", commit_id_str);
5860 print_update_progress_stats(&upa);
5863 done:
5864 if (ref)
5865 got_ref_close(ref);
5866 if (repo)
5867 got_repo_close(repo);
5868 if (worktree)
5869 got_worktree_close(worktree);
5870 free(cwd);
5871 free(repo_path);
5872 free(commit_id);
5873 free(commit_id_str);
5874 TAILQ_FOREACH(pe, &paths, entry)
5875 free((char *)pe->path);
5876 got_pathlist_free(&paths);
5877 return error;
5881 __dead static void
5882 usage_tag(void)
5884 fprintf(stderr,
5885 "usage: %s tag [-c commit] [-r repository] [-l] "
5886 "[-m message] name\n", getprogname());
5887 exit(1);
5890 #if 0
5891 static const struct got_error *
5892 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5894 const struct got_error *err = NULL;
5895 struct got_reflist_entry *re, *se, *new;
5896 struct got_object_id *re_id, *se_id;
5897 struct got_tag_object *re_tag, *se_tag;
5898 time_t re_time, se_time;
5900 SIMPLEQ_FOREACH(re, tags, entry) {
5901 se = SIMPLEQ_FIRST(sorted);
5902 if (se == NULL) {
5903 err = got_reflist_entry_dup(&new, re);
5904 if (err)
5905 return err;
5906 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5907 continue;
5908 } else {
5909 err = got_ref_resolve(&re_id, repo, re->ref);
5910 if (err)
5911 break;
5912 err = got_object_open_as_tag(&re_tag, repo, re_id);
5913 free(re_id);
5914 if (err)
5915 break;
5916 re_time = got_object_tag_get_tagger_time(re_tag);
5917 got_object_tag_close(re_tag);
5920 while (se) {
5921 err = got_ref_resolve(&se_id, repo, re->ref);
5922 if (err)
5923 break;
5924 err = got_object_open_as_tag(&se_tag, repo, se_id);
5925 free(se_id);
5926 if (err)
5927 break;
5928 se_time = got_object_tag_get_tagger_time(se_tag);
5929 got_object_tag_close(se_tag);
5931 if (se_time > re_time) {
5932 err = got_reflist_entry_dup(&new, re);
5933 if (err)
5934 return err;
5935 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5936 break;
5938 se = SIMPLEQ_NEXT(se, entry);
5939 continue;
5942 done:
5943 return err;
5945 #endif
5947 static const struct got_error *
5948 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5950 static const struct got_error *err = NULL;
5951 struct got_reflist_head refs;
5952 struct got_reflist_entry *re;
5954 TAILQ_INIT(&refs);
5956 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5957 if (err)
5958 return err;
5960 TAILQ_FOREACH(re, &refs, entry) {
5961 const char *refname;
5962 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5963 char datebuf[26];
5964 const char *tagger;
5965 time_t tagger_time;
5966 struct got_object_id *id;
5967 struct got_tag_object *tag;
5968 struct got_commit_object *commit = NULL;
5970 refname = got_ref_get_name(re->ref);
5971 if (strncmp(refname, "refs/tags/", 10) != 0)
5972 continue;
5973 refname += 10;
5974 refstr = got_ref_to_str(re->ref);
5975 if (refstr == NULL) {
5976 err = got_error_from_errno("got_ref_to_str");
5977 break;
5979 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5980 free(refstr);
5982 err = got_ref_resolve(&id, repo, re->ref);
5983 if (err)
5984 break;
5985 err = got_object_open_as_tag(&tag, repo, id);
5986 if (err) {
5987 if (err->code != GOT_ERR_OBJ_TYPE) {
5988 free(id);
5989 break;
5991 /* "lightweight" tag */
5992 err = got_object_open_as_commit(&commit, repo, id);
5993 if (err) {
5994 free(id);
5995 break;
5997 tagger = got_object_commit_get_committer(commit);
5998 tagger_time =
5999 got_object_commit_get_committer_time(commit);
6000 err = got_object_id_str(&id_str, id);
6001 free(id);
6002 if (err)
6003 break;
6004 } else {
6005 free(id);
6006 tagger = got_object_tag_get_tagger(tag);
6007 tagger_time = got_object_tag_get_tagger_time(tag);
6008 err = got_object_id_str(&id_str,
6009 got_object_tag_get_object_id(tag));
6010 if (err)
6011 break;
6013 printf("from: %s\n", tagger);
6014 datestr = get_datestr(&tagger_time, datebuf);
6015 if (datestr)
6016 printf("date: %s UTC\n", datestr);
6017 if (commit)
6018 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6019 else {
6020 switch (got_object_tag_get_object_type(tag)) {
6021 case GOT_OBJ_TYPE_BLOB:
6022 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6023 id_str);
6024 break;
6025 case GOT_OBJ_TYPE_TREE:
6026 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6027 id_str);
6028 break;
6029 case GOT_OBJ_TYPE_COMMIT:
6030 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6031 id_str);
6032 break;
6033 case GOT_OBJ_TYPE_TAG:
6034 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6035 id_str);
6036 break;
6037 default:
6038 break;
6041 free(id_str);
6042 if (commit) {
6043 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6044 if (err)
6045 break;
6046 got_object_commit_close(commit);
6047 } else {
6048 tagmsg0 = strdup(got_object_tag_get_message(tag));
6049 got_object_tag_close(tag);
6050 if (tagmsg0 == NULL) {
6051 err = got_error_from_errno("strdup");
6052 break;
6056 tagmsg = tagmsg0;
6057 do {
6058 line = strsep(&tagmsg, "\n");
6059 if (line)
6060 printf(" %s\n", line);
6061 } while (line);
6062 free(tagmsg0);
6065 got_ref_list_free(&refs);
6066 return NULL;
6069 static const struct got_error *
6070 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6071 const char *tag_name, const char *repo_path)
6073 const struct got_error *err = NULL;
6074 char *template = NULL, *initial_content = NULL;
6075 char *editor = NULL;
6076 int initial_content_len;
6077 int fd = -1;
6079 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6080 err = got_error_from_errno("asprintf");
6081 goto done;
6084 initial_content_len = asprintf(&initial_content,
6085 "\n# tagging commit %s as %s\n",
6086 commit_id_str, tag_name);
6087 if (initial_content_len == -1) {
6088 err = got_error_from_errno("asprintf");
6089 goto done;
6092 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6093 if (err)
6094 goto done;
6096 if (write(fd, initial_content, initial_content_len) == -1) {
6097 err = got_error_from_errno2("write", *tagmsg_path);
6098 goto done;
6101 err = get_editor(&editor);
6102 if (err)
6103 goto done;
6104 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6105 initial_content_len, 1);
6106 done:
6107 free(initial_content);
6108 free(template);
6109 free(editor);
6111 if (fd != -1 && close(fd) == -1 && err == NULL)
6112 err = got_error_from_errno2("close", *tagmsg_path);
6114 /* Editor is done; we can now apply unveil(2) */
6115 if (err == NULL)
6116 err = apply_unveil(repo_path, 0, NULL);
6117 if (err) {
6118 free(*tagmsg);
6119 *tagmsg = NULL;
6121 return err;
6124 static const struct got_error *
6125 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6126 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6128 const struct got_error *err = NULL;
6129 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6130 char *label = NULL, *commit_id_str = NULL;
6131 struct got_reference *ref = NULL;
6132 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6133 char *tagmsg_path = NULL, *tag_id_str = NULL;
6134 int preserve_tagmsg = 0;
6135 struct got_reflist_head refs;
6137 TAILQ_INIT(&refs);
6140 * Don't let the user create a tag name with a leading '-'.
6141 * While technically a valid reference name, this case is usually
6142 * an unintended typo.
6144 if (tag_name[0] == '-')
6145 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6147 err = get_author(&tagger, repo, worktree);
6148 if (err)
6149 return err;
6151 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6152 if (err)
6153 goto done;
6155 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6156 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6157 if (err)
6158 goto done;
6160 err = got_object_id_str(&commit_id_str, commit_id);
6161 if (err)
6162 goto done;
6164 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6165 refname = strdup(tag_name);
6166 if (refname == NULL) {
6167 err = got_error_from_errno("strdup");
6168 goto done;
6170 tag_name += 10;
6171 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6172 err = got_error_from_errno("asprintf");
6173 goto done;
6176 err = got_ref_open(&ref, repo, refname, 0);
6177 if (err == NULL) {
6178 err = got_error(GOT_ERR_TAG_EXISTS);
6179 goto done;
6180 } else if (err->code != GOT_ERR_NOT_REF)
6181 goto done;
6183 if (tagmsg_arg == NULL) {
6184 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6185 tag_name, got_repo_get_path(repo));
6186 if (err) {
6187 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6188 tagmsg_path != NULL)
6189 preserve_tagmsg = 1;
6190 goto done;
6194 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6195 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6196 if (err) {
6197 if (tagmsg_path)
6198 preserve_tagmsg = 1;
6199 goto done;
6202 err = got_ref_alloc(&ref, refname, tag_id);
6203 if (err) {
6204 if (tagmsg_path)
6205 preserve_tagmsg = 1;
6206 goto done;
6209 err = got_ref_write(ref, repo);
6210 if (err) {
6211 if (tagmsg_path)
6212 preserve_tagmsg = 1;
6213 goto done;
6216 err = got_object_id_str(&tag_id_str, tag_id);
6217 if (err) {
6218 if (tagmsg_path)
6219 preserve_tagmsg = 1;
6220 goto done;
6222 printf("Created tag %s\n", tag_id_str);
6223 done:
6224 if (preserve_tagmsg) {
6225 fprintf(stderr, "%s: tag message preserved in %s\n",
6226 getprogname(), tagmsg_path);
6227 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6228 err = got_error_from_errno2("unlink", tagmsg_path);
6229 free(tag_id_str);
6230 if (ref)
6231 got_ref_close(ref);
6232 free(commit_id);
6233 free(commit_id_str);
6234 free(refname);
6235 free(tagmsg);
6236 free(tagmsg_path);
6237 free(tagger);
6238 got_ref_list_free(&refs);
6239 return err;
6242 static const struct got_error *
6243 cmd_tag(int argc, char *argv[])
6245 const struct got_error *error = NULL;
6246 struct got_repository *repo = NULL;
6247 struct got_worktree *worktree = NULL;
6248 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6249 char *gitconfig_path = NULL;
6250 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6251 int ch, do_list = 0;
6253 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6254 switch (ch) {
6255 case 'c':
6256 commit_id_arg = optarg;
6257 break;
6258 case 'm':
6259 tagmsg = optarg;
6260 break;
6261 case 'r':
6262 repo_path = realpath(optarg, NULL);
6263 if (repo_path == NULL)
6264 return got_error_from_errno2("realpath",
6265 optarg);
6266 got_path_strip_trailing_slashes(repo_path);
6267 break;
6268 case 'l':
6269 do_list = 1;
6270 break;
6271 default:
6272 usage_tag();
6273 /* NOTREACHED */
6277 argc -= optind;
6278 argv += optind;
6280 if (do_list) {
6281 if (commit_id_arg != NULL)
6282 errx(1,
6283 "-c option can only be used when creating a tag");
6284 if (tagmsg)
6285 option_conflict('l', 'm');
6286 if (argc > 0)
6287 usage_tag();
6288 } else if (argc != 1)
6289 usage_tag();
6291 tag_name = argv[0];
6293 #ifndef PROFILE
6294 if (do_list) {
6295 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6296 NULL) == -1)
6297 err(1, "pledge");
6298 } else {
6299 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6300 "sendfd unveil", NULL) == -1)
6301 err(1, "pledge");
6303 #endif
6304 cwd = getcwd(NULL, 0);
6305 if (cwd == NULL) {
6306 error = got_error_from_errno("getcwd");
6307 goto done;
6310 if (repo_path == NULL) {
6311 error = got_worktree_open(&worktree, cwd);
6312 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6313 goto done;
6314 else
6315 error = NULL;
6316 if (worktree) {
6317 repo_path =
6318 strdup(got_worktree_get_repo_path(worktree));
6319 if (repo_path == NULL)
6320 error = got_error_from_errno("strdup");
6321 if (error)
6322 goto done;
6323 } else {
6324 repo_path = strdup(cwd);
6325 if (repo_path == NULL) {
6326 error = got_error_from_errno("strdup");
6327 goto done;
6332 if (do_list) {
6333 error = got_repo_open(&repo, repo_path, NULL);
6334 if (error != NULL)
6335 goto done;
6336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6337 if (error)
6338 goto done;
6339 error = list_tags(repo, worktree);
6340 } else {
6341 error = get_gitconfig_path(&gitconfig_path);
6342 if (error)
6343 goto done;
6344 error = got_repo_open(&repo, repo_path, gitconfig_path);
6345 if (error != NULL)
6346 goto done;
6348 if (tagmsg) {
6349 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6350 if (error)
6351 goto done;
6354 if (commit_id_arg == NULL) {
6355 struct got_reference *head_ref;
6356 struct got_object_id *commit_id;
6357 error = got_ref_open(&head_ref, repo,
6358 worktree ? got_worktree_get_head_ref_name(worktree)
6359 : GOT_REF_HEAD, 0);
6360 if (error)
6361 goto done;
6362 error = got_ref_resolve(&commit_id, repo, head_ref);
6363 got_ref_close(head_ref);
6364 if (error)
6365 goto done;
6366 error = got_object_id_str(&commit_id_str, commit_id);
6367 free(commit_id);
6368 if (error)
6369 goto done;
6372 error = add_tag(repo, worktree, tag_name,
6373 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6375 done:
6376 if (repo)
6377 got_repo_close(repo);
6378 if (worktree)
6379 got_worktree_close(worktree);
6380 free(cwd);
6381 free(repo_path);
6382 free(gitconfig_path);
6383 free(commit_id_str);
6384 return error;
6387 __dead static void
6388 usage_add(void)
6390 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6391 getprogname());
6392 exit(1);
6395 static const struct got_error *
6396 add_progress(void *arg, unsigned char status, const char *path)
6398 while (path[0] == '/')
6399 path++;
6400 printf("%c %s\n", status, path);
6401 return NULL;
6404 static const struct got_error *
6405 cmd_add(int argc, char *argv[])
6407 const struct got_error *error = NULL;
6408 struct got_repository *repo = NULL;
6409 struct got_worktree *worktree = NULL;
6410 char *cwd = NULL;
6411 struct got_pathlist_head paths;
6412 struct got_pathlist_entry *pe;
6413 int ch, can_recurse = 0, no_ignores = 0;
6415 TAILQ_INIT(&paths);
6417 while ((ch = getopt(argc, argv, "IR")) != -1) {
6418 switch (ch) {
6419 case 'I':
6420 no_ignores = 1;
6421 break;
6422 case 'R':
6423 can_recurse = 1;
6424 break;
6425 default:
6426 usage_add();
6427 /* NOTREACHED */
6431 argc -= optind;
6432 argv += optind;
6434 #ifndef PROFILE
6435 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6436 NULL) == -1)
6437 err(1, "pledge");
6438 #endif
6439 if (argc < 1)
6440 usage_add();
6442 cwd = getcwd(NULL, 0);
6443 if (cwd == NULL) {
6444 error = got_error_from_errno("getcwd");
6445 goto done;
6448 error = got_worktree_open(&worktree, cwd);
6449 if (error) {
6450 if (error->code == GOT_ERR_NOT_WORKTREE)
6451 error = wrap_not_worktree_error(error, "add", cwd);
6452 goto done;
6455 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6456 NULL);
6457 if (error != NULL)
6458 goto done;
6460 error = apply_unveil(got_repo_get_path(repo), 1,
6461 got_worktree_get_root_path(worktree));
6462 if (error)
6463 goto done;
6465 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6466 if (error)
6467 goto done;
6469 if (!can_recurse && no_ignores) {
6470 error = got_error_msg(GOT_ERR_BAD_PATH,
6471 "disregarding ignores requires -R option");
6472 goto done;
6476 if (!can_recurse) {
6477 char *ondisk_path;
6478 struct stat sb;
6479 TAILQ_FOREACH(pe, &paths, entry) {
6480 if (asprintf(&ondisk_path, "%s/%s",
6481 got_worktree_get_root_path(worktree),
6482 pe->path) == -1) {
6483 error = got_error_from_errno("asprintf");
6484 goto done;
6486 if (lstat(ondisk_path, &sb) == -1) {
6487 if (errno == ENOENT) {
6488 free(ondisk_path);
6489 continue;
6491 error = got_error_from_errno2("lstat",
6492 ondisk_path);
6493 free(ondisk_path);
6494 goto done;
6496 free(ondisk_path);
6497 if (S_ISDIR(sb.st_mode)) {
6498 error = got_error_msg(GOT_ERR_BAD_PATH,
6499 "adding directories requires -R option");
6500 goto done;
6505 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6506 NULL, repo, no_ignores);
6507 done:
6508 if (repo)
6509 got_repo_close(repo);
6510 if (worktree)
6511 got_worktree_close(worktree);
6512 TAILQ_FOREACH(pe, &paths, entry)
6513 free((char *)pe->path);
6514 got_pathlist_free(&paths);
6515 free(cwd);
6516 return error;
6519 __dead static void
6520 usage_remove(void)
6522 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6523 "path ...\n", getprogname());
6524 exit(1);
6527 static const struct got_error *
6528 print_remove_status(void *arg, unsigned char status,
6529 unsigned char staged_status, const char *path)
6531 while (path[0] == '/')
6532 path++;
6533 if (status == GOT_STATUS_NONEXISTENT)
6534 return NULL;
6535 if (status == staged_status && (status == GOT_STATUS_DELETE))
6536 status = GOT_STATUS_NO_CHANGE;
6537 printf("%c%c %s\n", status, staged_status, path);
6538 return NULL;
6541 static const struct got_error *
6542 cmd_remove(int argc, char *argv[])
6544 const struct got_error *error = NULL;
6545 struct got_worktree *worktree = NULL;
6546 struct got_repository *repo = NULL;
6547 const char *status_codes = NULL;
6548 char *cwd = NULL;
6549 struct got_pathlist_head paths;
6550 struct got_pathlist_entry *pe;
6551 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6553 TAILQ_INIT(&paths);
6555 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6556 switch (ch) {
6557 case 'f':
6558 delete_local_mods = 1;
6559 break;
6560 case 'k':
6561 keep_on_disk = 1;
6562 break;
6563 case 'R':
6564 can_recurse = 1;
6565 break;
6566 case 's':
6567 for (i = 0; i < strlen(optarg); i++) {
6568 switch (optarg[i]) {
6569 case GOT_STATUS_MODIFY:
6570 delete_local_mods = 1;
6571 break;
6572 case GOT_STATUS_MISSING:
6573 break;
6574 default:
6575 errx(1, "invalid status code '%c'",
6576 optarg[i]);
6579 status_codes = optarg;
6580 break;
6581 default:
6582 usage_remove();
6583 /* NOTREACHED */
6587 argc -= optind;
6588 argv += optind;
6590 #ifndef PROFILE
6591 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6592 NULL) == -1)
6593 err(1, "pledge");
6594 #endif
6595 if (argc < 1)
6596 usage_remove();
6598 cwd = getcwd(NULL, 0);
6599 if (cwd == NULL) {
6600 error = got_error_from_errno("getcwd");
6601 goto done;
6603 error = got_worktree_open(&worktree, cwd);
6604 if (error) {
6605 if (error->code == GOT_ERR_NOT_WORKTREE)
6606 error = wrap_not_worktree_error(error, "remove", cwd);
6607 goto done;
6610 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6611 NULL);
6612 if (error)
6613 goto done;
6615 error = apply_unveil(got_repo_get_path(repo), 1,
6616 got_worktree_get_root_path(worktree));
6617 if (error)
6618 goto done;
6620 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6621 if (error)
6622 goto done;
6624 if (!can_recurse) {
6625 char *ondisk_path;
6626 struct stat sb;
6627 TAILQ_FOREACH(pe, &paths, entry) {
6628 if (asprintf(&ondisk_path, "%s/%s",
6629 got_worktree_get_root_path(worktree),
6630 pe->path) == -1) {
6631 error = got_error_from_errno("asprintf");
6632 goto done;
6634 if (lstat(ondisk_path, &sb) == -1) {
6635 if (errno == ENOENT) {
6636 free(ondisk_path);
6637 continue;
6639 error = got_error_from_errno2("lstat",
6640 ondisk_path);
6641 free(ondisk_path);
6642 goto done;
6644 free(ondisk_path);
6645 if (S_ISDIR(sb.st_mode)) {
6646 error = got_error_msg(GOT_ERR_BAD_PATH,
6647 "removing directories requires -R option");
6648 goto done;
6653 error = got_worktree_schedule_delete(worktree, &paths,
6654 delete_local_mods, status_codes, print_remove_status, NULL,
6655 repo, keep_on_disk);
6656 done:
6657 if (repo)
6658 got_repo_close(repo);
6659 if (worktree)
6660 got_worktree_close(worktree);
6661 TAILQ_FOREACH(pe, &paths, entry)
6662 free((char *)pe->path);
6663 got_pathlist_free(&paths);
6664 free(cwd);
6665 return error;
6668 __dead static void
6669 usage_revert(void)
6671 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6672 "path ...\n", getprogname());
6673 exit(1);
6676 static const struct got_error *
6677 revert_progress(void *arg, unsigned char status, const char *path)
6679 if (status == GOT_STATUS_UNVERSIONED)
6680 return NULL;
6682 while (path[0] == '/')
6683 path++;
6684 printf("%c %s\n", status, path);
6685 return NULL;
6688 struct choose_patch_arg {
6689 FILE *patch_script_file;
6690 const char *action;
6693 static const struct got_error *
6694 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6695 int nchanges, const char *action)
6697 char *line = NULL;
6698 size_t linesize = 0;
6699 ssize_t linelen;
6701 switch (status) {
6702 case GOT_STATUS_ADD:
6703 printf("A %s\n%s this addition? [y/n] ", path, action);
6704 break;
6705 case GOT_STATUS_DELETE:
6706 printf("D %s\n%s this deletion? [y/n] ", path, action);
6707 break;
6708 case GOT_STATUS_MODIFY:
6709 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6710 return got_error_from_errno("fseek");
6711 printf(GOT_COMMIT_SEP_STR);
6712 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6713 printf("%s", line);
6714 if (ferror(patch_file))
6715 return got_error_from_errno("getline");
6716 printf(GOT_COMMIT_SEP_STR);
6717 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6718 path, n, nchanges, action);
6719 break;
6720 default:
6721 return got_error_path(path, GOT_ERR_FILE_STATUS);
6724 return NULL;
6727 static const struct got_error *
6728 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6729 FILE *patch_file, int n, int nchanges)
6731 const struct got_error *err = NULL;
6732 char *line = NULL;
6733 size_t linesize = 0;
6734 ssize_t linelen;
6735 int resp = ' ';
6736 struct choose_patch_arg *a = arg;
6738 *choice = GOT_PATCH_CHOICE_NONE;
6740 if (a->patch_script_file) {
6741 char *nl;
6742 err = show_change(status, path, patch_file, n, nchanges,
6743 a->action);
6744 if (err)
6745 return err;
6746 linelen = getline(&line, &linesize, a->patch_script_file);
6747 if (linelen == -1) {
6748 if (ferror(a->patch_script_file))
6749 return got_error_from_errno("getline");
6750 return NULL;
6752 nl = strchr(line, '\n');
6753 if (nl)
6754 *nl = '\0';
6755 if (strcmp(line, "y") == 0) {
6756 *choice = GOT_PATCH_CHOICE_YES;
6757 printf("y\n");
6758 } else if (strcmp(line, "n") == 0) {
6759 *choice = GOT_PATCH_CHOICE_NO;
6760 printf("n\n");
6761 } else if (strcmp(line, "q") == 0 &&
6762 status == GOT_STATUS_MODIFY) {
6763 *choice = GOT_PATCH_CHOICE_QUIT;
6764 printf("q\n");
6765 } else
6766 printf("invalid response '%s'\n", line);
6767 free(line);
6768 return NULL;
6771 while (resp != 'y' && resp != 'n' && resp != 'q') {
6772 err = show_change(status, path, patch_file, n, nchanges,
6773 a->action);
6774 if (err)
6775 return err;
6776 resp = getchar();
6777 if (resp == '\n')
6778 resp = getchar();
6779 if (status == GOT_STATUS_MODIFY) {
6780 if (resp != 'y' && resp != 'n' && resp != 'q') {
6781 printf("invalid response '%c'\n", resp);
6782 resp = ' ';
6784 } else if (resp != 'y' && resp != 'n') {
6785 printf("invalid response '%c'\n", resp);
6786 resp = ' ';
6790 if (resp == 'y')
6791 *choice = GOT_PATCH_CHOICE_YES;
6792 else if (resp == 'n')
6793 *choice = GOT_PATCH_CHOICE_NO;
6794 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6795 *choice = GOT_PATCH_CHOICE_QUIT;
6797 return NULL;
6801 static const struct got_error *
6802 cmd_revert(int argc, char *argv[])
6804 const struct got_error *error = NULL;
6805 struct got_worktree *worktree = NULL;
6806 struct got_repository *repo = NULL;
6807 char *cwd = NULL, *path = NULL;
6808 struct got_pathlist_head paths;
6809 struct got_pathlist_entry *pe;
6810 int ch, can_recurse = 0, pflag = 0;
6811 FILE *patch_script_file = NULL;
6812 const char *patch_script_path = NULL;
6813 struct choose_patch_arg cpa;
6815 TAILQ_INIT(&paths);
6817 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6818 switch (ch) {
6819 case 'p':
6820 pflag = 1;
6821 break;
6822 case 'F':
6823 patch_script_path = optarg;
6824 break;
6825 case 'R':
6826 can_recurse = 1;
6827 break;
6828 default:
6829 usage_revert();
6830 /* NOTREACHED */
6834 argc -= optind;
6835 argv += optind;
6837 #ifndef PROFILE
6838 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6839 "unveil", NULL) == -1)
6840 err(1, "pledge");
6841 #endif
6842 if (argc < 1)
6843 usage_revert();
6844 if (patch_script_path && !pflag)
6845 errx(1, "-F option can only be used together with -p option");
6847 cwd = getcwd(NULL, 0);
6848 if (cwd == NULL) {
6849 error = got_error_from_errno("getcwd");
6850 goto done;
6852 error = got_worktree_open(&worktree, cwd);
6853 if (error) {
6854 if (error->code == GOT_ERR_NOT_WORKTREE)
6855 error = wrap_not_worktree_error(error, "revert", cwd);
6856 goto done;
6859 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6860 NULL);
6861 if (error != NULL)
6862 goto done;
6864 if (patch_script_path) {
6865 patch_script_file = fopen(patch_script_path, "r");
6866 if (patch_script_file == NULL) {
6867 error = got_error_from_errno2("fopen",
6868 patch_script_path);
6869 goto done;
6872 error = apply_unveil(got_repo_get_path(repo), 1,
6873 got_worktree_get_root_path(worktree));
6874 if (error)
6875 goto done;
6877 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6878 if (error)
6879 goto done;
6881 if (!can_recurse) {
6882 char *ondisk_path;
6883 struct stat sb;
6884 TAILQ_FOREACH(pe, &paths, entry) {
6885 if (asprintf(&ondisk_path, "%s/%s",
6886 got_worktree_get_root_path(worktree),
6887 pe->path) == -1) {
6888 error = got_error_from_errno("asprintf");
6889 goto done;
6891 if (lstat(ondisk_path, &sb) == -1) {
6892 if (errno == ENOENT) {
6893 free(ondisk_path);
6894 continue;
6896 error = got_error_from_errno2("lstat",
6897 ondisk_path);
6898 free(ondisk_path);
6899 goto done;
6901 free(ondisk_path);
6902 if (S_ISDIR(sb.st_mode)) {
6903 error = got_error_msg(GOT_ERR_BAD_PATH,
6904 "reverting directories requires -R option");
6905 goto done;
6910 cpa.patch_script_file = patch_script_file;
6911 cpa.action = "revert";
6912 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6913 pflag ? choose_patch : NULL, &cpa, repo);
6914 done:
6915 if (patch_script_file && fclose(patch_script_file) == EOF &&
6916 error == NULL)
6917 error = got_error_from_errno2("fclose", patch_script_path);
6918 if (repo)
6919 got_repo_close(repo);
6920 if (worktree)
6921 got_worktree_close(worktree);
6922 free(path);
6923 free(cwd);
6924 return error;
6927 __dead static void
6928 usage_commit(void)
6930 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6931 getprogname());
6932 exit(1);
6935 struct collect_commit_logmsg_arg {
6936 const char *cmdline_log;
6937 const char *editor;
6938 const char *worktree_path;
6939 const char *branch_name;
6940 const char *repo_path;
6941 char *logmsg_path;
6945 static const struct got_error *
6946 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6947 void *arg)
6949 char *initial_content = NULL;
6950 struct got_pathlist_entry *pe;
6951 const struct got_error *err = NULL;
6952 char *template = NULL;
6953 struct collect_commit_logmsg_arg *a = arg;
6954 int initial_content_len;
6955 int fd = -1;
6956 size_t len;
6958 /* if a message was specified on the command line, just use it */
6959 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6960 len = strlen(a->cmdline_log) + 1;
6961 *logmsg = malloc(len + 1);
6962 if (*logmsg == NULL)
6963 return got_error_from_errno("malloc");
6964 strlcpy(*logmsg, a->cmdline_log, len);
6965 return NULL;
6968 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6969 return got_error_from_errno("asprintf");
6971 initial_content_len = asprintf(&initial_content,
6972 "\n# changes to be committed on branch %s:\n",
6973 a->branch_name);
6974 if (initial_content_len == -1)
6975 return got_error_from_errno("asprintf");
6977 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6978 if (err)
6979 goto done;
6981 if (write(fd, initial_content, initial_content_len) == -1) {
6982 err = got_error_from_errno2("write", a->logmsg_path);
6983 goto done;
6986 TAILQ_FOREACH(pe, commitable_paths, entry) {
6987 struct got_commitable *ct = pe->data;
6988 dprintf(fd, "# %c %s\n",
6989 got_commitable_get_status(ct),
6990 got_commitable_get_path(ct));
6993 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6994 initial_content_len, 1);
6995 done:
6996 free(initial_content);
6997 free(template);
6999 if (fd != -1 && close(fd) == -1 && err == NULL)
7000 err = got_error_from_errno2("close", a->logmsg_path);
7002 /* Editor is done; we can now apply unveil(2) */
7003 if (err == NULL)
7004 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7005 if (err) {
7006 free(*logmsg);
7007 *logmsg = NULL;
7009 return err;
7012 static const struct got_error *
7013 cmd_commit(int argc, char *argv[])
7015 const struct got_error *error = NULL;
7016 struct got_worktree *worktree = NULL;
7017 struct got_repository *repo = NULL;
7018 char *cwd = NULL, *id_str = NULL;
7019 struct got_object_id *id = NULL;
7020 const char *logmsg = NULL;
7021 struct collect_commit_logmsg_arg cl_arg;
7022 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7023 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7024 int allow_bad_symlinks = 0;
7025 struct got_pathlist_head paths;
7027 TAILQ_INIT(&paths);
7028 cl_arg.logmsg_path = NULL;
7030 while ((ch = getopt(argc, argv, "m:S")) != -1) {
7031 switch (ch) {
7032 case 'm':
7033 logmsg = optarg;
7034 break;
7035 case 'S':
7036 allow_bad_symlinks = 1;
7037 break;
7038 default:
7039 usage_commit();
7040 /* NOTREACHED */
7044 argc -= optind;
7045 argv += optind;
7047 #ifndef PROFILE
7048 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7049 "unveil", NULL) == -1)
7050 err(1, "pledge");
7051 #endif
7052 cwd = getcwd(NULL, 0);
7053 if (cwd == NULL) {
7054 error = got_error_from_errno("getcwd");
7055 goto done;
7057 error = got_worktree_open(&worktree, cwd);
7058 if (error) {
7059 if (error->code == GOT_ERR_NOT_WORKTREE)
7060 error = wrap_not_worktree_error(error, "commit", cwd);
7061 goto done;
7064 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7065 if (error)
7066 goto done;
7067 if (rebase_in_progress) {
7068 error = got_error(GOT_ERR_REBASING);
7069 goto done;
7072 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7073 worktree);
7074 if (error)
7075 goto done;
7077 error = get_gitconfig_path(&gitconfig_path);
7078 if (error)
7079 goto done;
7080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7081 gitconfig_path);
7082 if (error != NULL)
7083 goto done;
7085 error = get_author(&author, repo, worktree);
7086 if (error)
7087 return error;
7090 * unveil(2) traverses exec(2); if an editor is used we have
7091 * to apply unveil after the log message has been written.
7093 if (logmsg == NULL || strlen(logmsg) == 0)
7094 error = get_editor(&editor);
7095 else
7096 error = apply_unveil(got_repo_get_path(repo), 0,
7097 got_worktree_get_root_path(worktree));
7098 if (error)
7099 goto done;
7101 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7102 if (error)
7103 goto done;
7105 cl_arg.editor = editor;
7106 cl_arg.cmdline_log = logmsg;
7107 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7108 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7109 if (!histedit_in_progress) {
7110 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7111 error = got_error(GOT_ERR_COMMIT_BRANCH);
7112 goto done;
7114 cl_arg.branch_name += 11;
7116 cl_arg.repo_path = got_repo_get_path(repo);
7117 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7118 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7119 print_status, NULL, repo);
7120 if (error) {
7121 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7122 cl_arg.logmsg_path != NULL)
7123 preserve_logmsg = 1;
7124 goto done;
7127 error = got_object_id_str(&id_str, id);
7128 if (error)
7129 goto done;
7130 printf("Created commit %s\n", id_str);
7131 done:
7132 if (preserve_logmsg) {
7133 fprintf(stderr, "%s: log message preserved in %s\n",
7134 getprogname(), cl_arg.logmsg_path);
7135 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7136 error == NULL)
7137 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7138 free(cl_arg.logmsg_path);
7139 if (repo)
7140 got_repo_close(repo);
7141 if (worktree)
7142 got_worktree_close(worktree);
7143 free(cwd);
7144 free(id_str);
7145 free(gitconfig_path);
7146 free(editor);
7147 free(author);
7148 return error;
7151 __dead static void
7152 usage_cherrypick(void)
7154 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7155 exit(1);
7158 static const struct got_error *
7159 cmd_cherrypick(int argc, char *argv[])
7161 const struct got_error *error = NULL;
7162 struct got_worktree *worktree = NULL;
7163 struct got_repository *repo = NULL;
7164 char *cwd = NULL, *commit_id_str = NULL;
7165 struct got_object_id *commit_id = NULL;
7166 struct got_commit_object *commit = NULL;
7167 struct got_object_qid *pid;
7168 struct got_reference *head_ref = NULL;
7169 int ch;
7170 struct got_update_progress_arg upa;
7172 while ((ch = getopt(argc, argv, "")) != -1) {
7173 switch (ch) {
7174 default:
7175 usage_cherrypick();
7176 /* NOTREACHED */
7180 argc -= optind;
7181 argv += optind;
7183 #ifndef PROFILE
7184 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7185 "unveil", NULL) == -1)
7186 err(1, "pledge");
7187 #endif
7188 if (argc != 1)
7189 usage_cherrypick();
7191 cwd = getcwd(NULL, 0);
7192 if (cwd == NULL) {
7193 error = got_error_from_errno("getcwd");
7194 goto done;
7196 error = got_worktree_open(&worktree, cwd);
7197 if (error) {
7198 if (error->code == GOT_ERR_NOT_WORKTREE)
7199 error = wrap_not_worktree_error(error, "cherrypick",
7200 cwd);
7201 goto done;
7204 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7205 NULL);
7206 if (error != NULL)
7207 goto done;
7209 error = apply_unveil(got_repo_get_path(repo), 0,
7210 got_worktree_get_root_path(worktree));
7211 if (error)
7212 goto done;
7214 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7215 GOT_OBJ_TYPE_COMMIT, repo);
7216 if (error != NULL) {
7217 struct got_reference *ref;
7218 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7219 goto done;
7220 error = got_ref_open(&ref, repo, argv[0], 0);
7221 if (error != NULL)
7222 goto done;
7223 error = got_ref_resolve(&commit_id, repo, ref);
7224 got_ref_close(ref);
7225 if (error != NULL)
7226 goto done;
7228 error = got_object_id_str(&commit_id_str, commit_id);
7229 if (error)
7230 goto done;
7232 error = got_ref_open(&head_ref, repo,
7233 got_worktree_get_head_ref_name(worktree), 0);
7234 if (error != NULL)
7235 goto done;
7237 error = check_same_branch(commit_id, head_ref, NULL, repo);
7238 if (error) {
7239 if (error->code != GOT_ERR_ANCESTRY)
7240 goto done;
7241 error = NULL;
7242 } else {
7243 error = got_error(GOT_ERR_SAME_BRANCH);
7244 goto done;
7247 error = got_object_open_as_commit(&commit, repo, commit_id);
7248 if (error)
7249 goto done;
7250 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7251 memset(&upa, 0, sizeof(upa));
7252 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7253 commit_id, repo, update_progress, &upa, check_cancelled,
7254 NULL);
7255 if (error != NULL)
7256 goto done;
7258 if (upa.did_something)
7259 printf("Merged commit %s\n", commit_id_str);
7260 print_update_progress_stats(&upa);
7261 done:
7262 if (commit)
7263 got_object_commit_close(commit);
7264 free(commit_id_str);
7265 if (head_ref)
7266 got_ref_close(head_ref);
7267 if (worktree)
7268 got_worktree_close(worktree);
7269 if (repo)
7270 got_repo_close(repo);
7271 return error;
7274 __dead static void
7275 usage_backout(void)
7277 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7278 exit(1);
7281 static const struct got_error *
7282 cmd_backout(int argc, char *argv[])
7284 const struct got_error *error = NULL;
7285 struct got_worktree *worktree = NULL;
7286 struct got_repository *repo = NULL;
7287 char *cwd = NULL, *commit_id_str = NULL;
7288 struct got_object_id *commit_id = NULL;
7289 struct got_commit_object *commit = NULL;
7290 struct got_object_qid *pid;
7291 struct got_reference *head_ref = NULL;
7292 int ch;
7293 struct got_update_progress_arg upa;
7295 while ((ch = getopt(argc, argv, "")) != -1) {
7296 switch (ch) {
7297 default:
7298 usage_backout();
7299 /* NOTREACHED */
7303 argc -= optind;
7304 argv += optind;
7306 #ifndef PROFILE
7307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7308 "unveil", NULL) == -1)
7309 err(1, "pledge");
7310 #endif
7311 if (argc != 1)
7312 usage_backout();
7314 cwd = getcwd(NULL, 0);
7315 if (cwd == NULL) {
7316 error = got_error_from_errno("getcwd");
7317 goto done;
7319 error = got_worktree_open(&worktree, cwd);
7320 if (error) {
7321 if (error->code == GOT_ERR_NOT_WORKTREE)
7322 error = wrap_not_worktree_error(error, "backout", cwd);
7323 goto done;
7326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7327 NULL);
7328 if (error != NULL)
7329 goto done;
7331 error = apply_unveil(got_repo_get_path(repo), 0,
7332 got_worktree_get_root_path(worktree));
7333 if (error)
7334 goto done;
7336 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7337 GOT_OBJ_TYPE_COMMIT, repo);
7338 if (error != NULL) {
7339 struct got_reference *ref;
7340 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7341 goto done;
7342 error = got_ref_open(&ref, repo, argv[0], 0);
7343 if (error != NULL)
7344 goto done;
7345 error = got_ref_resolve(&commit_id, repo, ref);
7346 got_ref_close(ref);
7347 if (error != NULL)
7348 goto done;
7350 error = got_object_id_str(&commit_id_str, commit_id);
7351 if (error)
7352 goto done;
7354 error = got_ref_open(&head_ref, repo,
7355 got_worktree_get_head_ref_name(worktree), 0);
7356 if (error != NULL)
7357 goto done;
7359 error = check_same_branch(commit_id, head_ref, NULL, repo);
7360 if (error)
7361 goto done;
7363 error = got_object_open_as_commit(&commit, repo, commit_id);
7364 if (error)
7365 goto done;
7366 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7367 if (pid == NULL) {
7368 error = got_error(GOT_ERR_ROOT_COMMIT);
7369 goto done;
7372 memset(&upa, 0, sizeof(upa));
7373 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7374 update_progress, &upa, check_cancelled, NULL);
7375 if (error != NULL)
7376 goto done;
7378 if (upa.did_something)
7379 printf("Backed out commit %s\n", commit_id_str);
7380 print_update_progress_stats(&upa);
7381 done:
7382 if (commit)
7383 got_object_commit_close(commit);
7384 free(commit_id_str);
7385 if (head_ref)
7386 got_ref_close(head_ref);
7387 if (worktree)
7388 got_worktree_close(worktree);
7389 if (repo)
7390 got_repo_close(repo);
7391 return error;
7394 __dead static void
7395 usage_rebase(void)
7397 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7398 getprogname());
7399 exit(1);
7402 void
7403 trim_logmsg(char *logmsg, int limit)
7405 char *nl;
7406 size_t len;
7408 len = strlen(logmsg);
7409 if (len > limit)
7410 len = limit;
7411 logmsg[len] = '\0';
7412 nl = strchr(logmsg, '\n');
7413 if (nl)
7414 *nl = '\0';
7417 static const struct got_error *
7418 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7420 const struct got_error *err;
7421 char *logmsg0 = NULL;
7422 const char *s;
7424 err = got_object_commit_get_logmsg(&logmsg0, commit);
7425 if (err)
7426 return err;
7428 s = logmsg0;
7429 while (isspace((unsigned char)s[0]))
7430 s++;
7432 *logmsg = strdup(s);
7433 if (*logmsg == NULL) {
7434 err = got_error_from_errno("strdup");
7435 goto done;
7438 trim_logmsg(*logmsg, limit);
7439 done:
7440 free(logmsg0);
7441 return err;
7444 static const struct got_error *
7445 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7447 const struct got_error *err;
7448 struct got_commit_object *commit = NULL;
7449 char *id_str = NULL, *logmsg = NULL;
7451 err = got_object_open_as_commit(&commit, repo, id);
7452 if (err)
7453 return err;
7455 err = got_object_id_str(&id_str, id);
7456 if (err)
7457 goto done;
7459 id_str[12] = '\0';
7461 err = get_short_logmsg(&logmsg, 42, commit);
7462 if (err)
7463 goto done;
7465 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7466 done:
7467 free(id_str);
7468 got_object_commit_close(commit);
7469 free(logmsg);
7470 return err;
7473 static const struct got_error *
7474 show_rebase_progress(struct got_commit_object *commit,
7475 struct got_object_id *old_id, struct got_object_id *new_id)
7477 const struct got_error *err;
7478 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7480 err = got_object_id_str(&old_id_str, old_id);
7481 if (err)
7482 goto done;
7484 if (new_id) {
7485 err = got_object_id_str(&new_id_str, new_id);
7486 if (err)
7487 goto done;
7490 old_id_str[12] = '\0';
7491 if (new_id_str)
7492 new_id_str[12] = '\0';
7494 err = get_short_logmsg(&logmsg, 42, commit);
7495 if (err)
7496 goto done;
7498 printf("%s -> %s: %s\n", old_id_str,
7499 new_id_str ? new_id_str : "no-op change", logmsg);
7500 done:
7501 free(old_id_str);
7502 free(new_id_str);
7503 free(logmsg);
7504 return err;
7507 static const struct got_error *
7508 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7509 struct got_reference *branch, struct got_reference *new_base_branch,
7510 struct got_reference *tmp_branch, struct got_repository *repo)
7512 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7513 return got_worktree_rebase_complete(worktree, fileindex,
7514 new_base_branch, tmp_branch, branch, repo);
7517 static const struct got_error *
7518 rebase_commit(struct got_pathlist_head *merged_paths,
7519 struct got_worktree *worktree, struct got_fileindex *fileindex,
7520 struct got_reference *tmp_branch,
7521 struct got_object_id *commit_id, struct got_repository *repo)
7523 const struct got_error *error;
7524 struct got_commit_object *commit;
7525 struct got_object_id *new_commit_id;
7527 error = got_object_open_as_commit(&commit, repo, commit_id);
7528 if (error)
7529 return error;
7531 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7532 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7533 if (error) {
7534 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7535 goto done;
7536 error = show_rebase_progress(commit, commit_id, NULL);
7537 } else {
7538 error = show_rebase_progress(commit, commit_id, new_commit_id);
7539 free(new_commit_id);
7541 done:
7542 got_object_commit_close(commit);
7543 return error;
7546 struct check_path_prefix_arg {
7547 const char *path_prefix;
7548 size_t len;
7549 int errcode;
7552 static const struct got_error *
7553 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7554 struct got_blob_object *blob2, struct got_object_id *id1,
7555 struct got_object_id *id2, const char *path1, const char *path2,
7556 mode_t mode1, mode_t mode2, struct got_repository *repo)
7558 struct check_path_prefix_arg *a = arg;
7560 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7561 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7562 return got_error(a->errcode);
7564 return NULL;
7567 static const struct got_error *
7568 check_path_prefix(struct got_object_id *parent_id,
7569 struct got_object_id *commit_id, const char *path_prefix,
7570 int errcode, struct got_repository *repo)
7572 const struct got_error *err;
7573 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7574 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7575 struct check_path_prefix_arg cpp_arg;
7577 if (got_path_is_root_dir(path_prefix))
7578 return NULL;
7580 err = got_object_open_as_commit(&commit, repo, commit_id);
7581 if (err)
7582 goto done;
7584 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7585 if (err)
7586 goto done;
7588 err = got_object_open_as_tree(&tree1, repo,
7589 got_object_commit_get_tree_id(parent_commit));
7590 if (err)
7591 goto done;
7593 err = got_object_open_as_tree(&tree2, repo,
7594 got_object_commit_get_tree_id(commit));
7595 if (err)
7596 goto done;
7598 cpp_arg.path_prefix = path_prefix;
7599 while (cpp_arg.path_prefix[0] == '/')
7600 cpp_arg.path_prefix++;
7601 cpp_arg.len = strlen(cpp_arg.path_prefix);
7602 cpp_arg.errcode = errcode;
7603 err = got_diff_tree(tree1, tree2, "", "", repo,
7604 check_path_prefix_in_diff, &cpp_arg, 0);
7605 done:
7606 if (tree1)
7607 got_object_tree_close(tree1);
7608 if (tree2)
7609 got_object_tree_close(tree2);
7610 if (commit)
7611 got_object_commit_close(commit);
7612 if (parent_commit)
7613 got_object_commit_close(parent_commit);
7614 return err;
7617 static const struct got_error *
7618 collect_commits(struct got_object_id_queue *commits,
7619 struct got_object_id *initial_commit_id,
7620 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7621 const char *path_prefix, int path_prefix_errcode,
7622 struct got_repository *repo)
7624 const struct got_error *err = NULL;
7625 struct got_commit_graph *graph = NULL;
7626 struct got_object_id *parent_id = NULL;
7627 struct got_object_qid *qid;
7628 struct got_object_id *commit_id = initial_commit_id;
7630 err = got_commit_graph_open(&graph, "/", 1);
7631 if (err)
7632 return err;
7634 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7635 check_cancelled, NULL);
7636 if (err)
7637 goto done;
7638 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7639 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7640 check_cancelled, NULL);
7641 if (err) {
7642 if (err->code == GOT_ERR_ITER_COMPLETED) {
7643 err = got_error_msg(GOT_ERR_ANCESTRY,
7644 "ran out of commits to rebase before "
7645 "youngest common ancestor commit has "
7646 "been reached?!?");
7648 goto done;
7649 } else {
7650 err = check_path_prefix(parent_id, commit_id,
7651 path_prefix, path_prefix_errcode, repo);
7652 if (err)
7653 goto done;
7655 err = got_object_qid_alloc(&qid, commit_id);
7656 if (err)
7657 goto done;
7658 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7659 commit_id = parent_id;
7662 done:
7663 got_commit_graph_close(graph);
7664 return err;
7667 static const struct got_error *
7668 cmd_rebase(int argc, char *argv[])
7670 const struct got_error *error = NULL;
7671 struct got_worktree *worktree = NULL;
7672 struct got_repository *repo = NULL;
7673 struct got_fileindex *fileindex = NULL;
7674 char *cwd = NULL;
7675 struct got_reference *branch = NULL;
7676 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7677 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7678 struct got_object_id *resume_commit_id = NULL;
7679 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7680 struct got_commit_object *commit = NULL;
7681 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7682 int histedit_in_progress = 0;
7683 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7684 struct got_object_id_queue commits;
7685 struct got_pathlist_head merged_paths;
7686 const struct got_object_id_queue *parent_ids;
7687 struct got_object_qid *qid, *pid;
7689 SIMPLEQ_INIT(&commits);
7690 TAILQ_INIT(&merged_paths);
7692 while ((ch = getopt(argc, argv, "ac")) != -1) {
7693 switch (ch) {
7694 case 'a':
7695 abort_rebase = 1;
7696 break;
7697 case 'c':
7698 continue_rebase = 1;
7699 break;
7700 default:
7701 usage_rebase();
7702 /* NOTREACHED */
7706 argc -= optind;
7707 argv += optind;
7709 #ifndef PROFILE
7710 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7711 "unveil", NULL) == -1)
7712 err(1, "pledge");
7713 #endif
7714 if (abort_rebase && continue_rebase)
7715 usage_rebase();
7716 else if (abort_rebase || continue_rebase) {
7717 if (argc != 0)
7718 usage_rebase();
7719 } else if (argc != 1)
7720 usage_rebase();
7722 cwd = getcwd(NULL, 0);
7723 if (cwd == NULL) {
7724 error = got_error_from_errno("getcwd");
7725 goto done;
7727 error = got_worktree_open(&worktree, cwd);
7728 if (error) {
7729 if (error->code == GOT_ERR_NOT_WORKTREE)
7730 error = wrap_not_worktree_error(error, "rebase", cwd);
7731 goto done;
7734 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7735 NULL);
7736 if (error != NULL)
7737 goto done;
7739 error = apply_unveil(got_repo_get_path(repo), 0,
7740 got_worktree_get_root_path(worktree));
7741 if (error)
7742 goto done;
7744 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7745 worktree);
7746 if (error)
7747 goto done;
7748 if (histedit_in_progress) {
7749 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7750 goto done;
7753 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7754 if (error)
7755 goto done;
7757 if (abort_rebase) {
7758 struct got_update_progress_arg upa;
7759 if (!rebase_in_progress) {
7760 error = got_error(GOT_ERR_NOT_REBASING);
7761 goto done;
7763 error = got_worktree_rebase_continue(&resume_commit_id,
7764 &new_base_branch, &tmp_branch, &branch, &fileindex,
7765 worktree, repo);
7766 if (error)
7767 goto done;
7768 printf("Switching work tree to %s\n",
7769 got_ref_get_symref_target(new_base_branch));
7770 memset(&upa, 0, sizeof(upa));
7771 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7772 new_base_branch, update_progress, &upa);
7773 if (error)
7774 goto done;
7775 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7776 print_update_progress_stats(&upa);
7777 goto done; /* nothing else to do */
7780 if (continue_rebase) {
7781 if (!rebase_in_progress) {
7782 error = got_error(GOT_ERR_NOT_REBASING);
7783 goto done;
7785 error = got_worktree_rebase_continue(&resume_commit_id,
7786 &new_base_branch, &tmp_branch, &branch, &fileindex,
7787 worktree, repo);
7788 if (error)
7789 goto done;
7791 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7792 resume_commit_id, repo);
7793 if (error)
7794 goto done;
7796 yca_id = got_object_id_dup(resume_commit_id);
7797 if (yca_id == NULL) {
7798 error = got_error_from_errno("got_object_id_dup");
7799 goto done;
7801 } else {
7802 error = got_ref_open(&branch, repo, argv[0], 0);
7803 if (error != NULL)
7804 goto done;
7807 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7808 if (error)
7809 goto done;
7811 if (!continue_rebase) {
7812 struct got_object_id *base_commit_id;
7814 base_commit_id = got_worktree_get_base_commit_id(worktree);
7815 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7816 base_commit_id, branch_head_commit_id, repo,
7817 check_cancelled, NULL);
7818 if (error)
7819 goto done;
7820 if (yca_id == NULL) {
7821 error = got_error_msg(GOT_ERR_ANCESTRY,
7822 "specified branch shares no common ancestry "
7823 "with work tree's branch");
7824 goto done;
7827 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7828 if (error) {
7829 if (error->code != GOT_ERR_ANCESTRY)
7830 goto done;
7831 error = NULL;
7832 } else {
7833 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7834 "specified branch resolves to a commit which "
7835 "is already contained in work tree's branch");
7836 goto done;
7838 error = got_worktree_rebase_prepare(&new_base_branch,
7839 &tmp_branch, &fileindex, worktree, branch, repo);
7840 if (error)
7841 goto done;
7844 commit_id = branch_head_commit_id;
7845 error = got_object_open_as_commit(&commit, repo, commit_id);
7846 if (error)
7847 goto done;
7849 parent_ids = got_object_commit_get_parent_ids(commit);
7850 pid = SIMPLEQ_FIRST(parent_ids);
7851 if (pid == NULL) {
7852 if (!continue_rebase) {
7853 struct got_update_progress_arg upa;
7854 memset(&upa, 0, sizeof(upa));
7855 error = got_worktree_rebase_abort(worktree, fileindex,
7856 repo, new_base_branch, update_progress, &upa);
7857 if (error)
7858 goto done;
7859 printf("Rebase of %s aborted\n",
7860 got_ref_get_name(branch));
7861 print_update_progress_stats(&upa);
7864 error = got_error(GOT_ERR_EMPTY_REBASE);
7865 goto done;
7867 error = collect_commits(&commits, commit_id, pid->id,
7868 yca_id, got_worktree_get_path_prefix(worktree),
7869 GOT_ERR_REBASE_PATH, repo);
7870 got_object_commit_close(commit);
7871 commit = NULL;
7872 if (error)
7873 goto done;
7875 if (SIMPLEQ_EMPTY(&commits)) {
7876 if (continue_rebase) {
7877 error = rebase_complete(worktree, fileindex,
7878 branch, new_base_branch, tmp_branch, repo);
7879 goto done;
7880 } else {
7881 /* Fast-forward the reference of the branch. */
7882 struct got_object_id *new_head_commit_id;
7883 char *id_str;
7884 error = got_ref_resolve(&new_head_commit_id, repo,
7885 new_base_branch);
7886 if (error)
7887 goto done;
7888 error = got_object_id_str(&id_str, new_head_commit_id);
7889 printf("Forwarding %s to commit %s\n",
7890 got_ref_get_name(branch), id_str);
7891 free(id_str);
7892 error = got_ref_change_ref(branch,
7893 new_head_commit_id);
7894 if (error)
7895 goto done;
7899 pid = NULL;
7900 SIMPLEQ_FOREACH(qid, &commits, entry) {
7901 struct got_update_progress_arg upa;
7903 commit_id = qid->id;
7904 parent_id = pid ? pid->id : yca_id;
7905 pid = qid;
7907 memset(&upa, 0, sizeof(upa));
7908 error = got_worktree_rebase_merge_files(&merged_paths,
7909 worktree, fileindex, parent_id, commit_id, repo,
7910 update_progress, &upa, check_cancelled, NULL);
7911 if (error)
7912 goto done;
7914 print_update_progress_stats(&upa);
7915 if (upa.conflicts > 0)
7916 rebase_status = GOT_STATUS_CONFLICT;
7918 if (rebase_status == GOT_STATUS_CONFLICT) {
7919 error = show_rebase_merge_conflict(qid->id, repo);
7920 if (error)
7921 goto done;
7922 got_worktree_rebase_pathlist_free(&merged_paths);
7923 break;
7926 error = rebase_commit(&merged_paths, worktree, fileindex,
7927 tmp_branch, commit_id, repo);
7928 got_worktree_rebase_pathlist_free(&merged_paths);
7929 if (error)
7930 goto done;
7933 if (rebase_status == GOT_STATUS_CONFLICT) {
7934 error = got_worktree_rebase_postpone(worktree, fileindex);
7935 if (error)
7936 goto done;
7937 error = got_error_msg(GOT_ERR_CONFLICTS,
7938 "conflicts must be resolved before rebasing can continue");
7939 } else
7940 error = rebase_complete(worktree, fileindex, branch,
7941 new_base_branch, tmp_branch, repo);
7942 done:
7943 got_object_id_queue_free(&commits);
7944 free(branch_head_commit_id);
7945 free(resume_commit_id);
7946 free(yca_id);
7947 if (commit)
7948 got_object_commit_close(commit);
7949 if (branch)
7950 got_ref_close(branch);
7951 if (new_base_branch)
7952 got_ref_close(new_base_branch);
7953 if (tmp_branch)
7954 got_ref_close(tmp_branch);
7955 if (worktree)
7956 got_worktree_close(worktree);
7957 if (repo)
7958 got_repo_close(repo);
7959 return error;
7962 __dead static void
7963 usage_histedit(void)
7965 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7966 getprogname());
7967 exit(1);
7970 #define GOT_HISTEDIT_PICK 'p'
7971 #define GOT_HISTEDIT_EDIT 'e'
7972 #define GOT_HISTEDIT_FOLD 'f'
7973 #define GOT_HISTEDIT_DROP 'd'
7974 #define GOT_HISTEDIT_MESG 'm'
7976 static struct got_histedit_cmd {
7977 unsigned char code;
7978 const char *name;
7979 const char *desc;
7980 } got_histedit_cmds[] = {
7981 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7982 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7983 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7984 "be used" },
7985 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7986 { GOT_HISTEDIT_MESG, "mesg",
7987 "single-line log message for commit above (open editor if empty)" },
7990 struct got_histedit_list_entry {
7991 TAILQ_ENTRY(got_histedit_list_entry) entry;
7992 struct got_object_id *commit_id;
7993 const struct got_histedit_cmd *cmd;
7994 char *logmsg;
7996 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7998 static const struct got_error *
7999 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8000 FILE *f, struct got_repository *repo)
8002 const struct got_error *err = NULL;
8003 char *logmsg = NULL, *id_str = NULL;
8004 struct got_commit_object *commit = NULL;
8005 int n;
8007 err = got_object_open_as_commit(&commit, repo, commit_id);
8008 if (err)
8009 goto done;
8011 err = get_short_logmsg(&logmsg, 34, commit);
8012 if (err)
8013 goto done;
8015 err = got_object_id_str(&id_str, commit_id);
8016 if (err)
8017 goto done;
8019 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8020 if (n < 0)
8021 err = got_ferror(f, GOT_ERR_IO);
8022 done:
8023 if (commit)
8024 got_object_commit_close(commit);
8025 free(id_str);
8026 free(logmsg);
8027 return err;
8030 static const struct got_error *
8031 histedit_write_commit_list(struct got_object_id_queue *commits,
8032 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8034 const struct got_error *err = NULL;
8035 struct got_object_qid *qid;
8036 const char *histedit_cmd = NULL;
8038 if (SIMPLEQ_EMPTY(commits))
8039 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8041 SIMPLEQ_FOREACH(qid, commits, entry) {
8042 histedit_cmd = got_histedit_cmds[0].name;
8043 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8044 histedit_cmd = "fold";
8045 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8046 if (err)
8047 break;
8048 if (edit_logmsg_only) {
8049 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8050 if (n < 0) {
8051 err = got_ferror(f, GOT_ERR_IO);
8052 break;
8057 return err;
8060 static const struct got_error *
8061 write_cmd_list(FILE *f, const char *branch_name,
8062 struct got_object_id_queue *commits)
8064 const struct got_error *err = NULL;
8065 size_t i;
8066 int n;
8067 char *id_str;
8068 struct got_object_qid *qid;
8070 qid = SIMPLEQ_FIRST(commits);
8071 err = got_object_id_str(&id_str, qid->id);
8072 if (err)
8073 return err;
8075 n = fprintf(f,
8076 "# Editing the history of branch '%s' starting at\n"
8077 "# commit %s\n"
8078 "# Commits will be processed in order from top to "
8079 "bottom of this file.\n", branch_name, id_str);
8080 if (n < 0) {
8081 err = got_ferror(f, GOT_ERR_IO);
8082 goto done;
8085 n = fprintf(f, "# Available histedit commands:\n");
8086 if (n < 0) {
8087 err = got_ferror(f, GOT_ERR_IO);
8088 goto done;
8091 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8092 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8093 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8094 cmd->desc);
8095 if (n < 0) {
8096 err = got_ferror(f, GOT_ERR_IO);
8097 break;
8100 done:
8101 free(id_str);
8102 return err;
8105 static const struct got_error *
8106 histedit_syntax_error(int lineno)
8108 static char msg[42];
8109 int ret;
8111 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8112 lineno);
8113 if (ret == -1 || ret >= sizeof(msg))
8114 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8116 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8119 static const struct got_error *
8120 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8121 char *logmsg, struct got_repository *repo)
8123 const struct got_error *err;
8124 struct got_commit_object *folded_commit = NULL;
8125 char *id_str, *folded_logmsg = NULL;
8127 err = got_object_id_str(&id_str, hle->commit_id);
8128 if (err)
8129 return err;
8131 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8132 if (err)
8133 goto done;
8135 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8136 if (err)
8137 goto done;
8138 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8139 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8140 folded_logmsg) == -1) {
8141 err = got_error_from_errno("asprintf");
8143 done:
8144 if (folded_commit)
8145 got_object_commit_close(folded_commit);
8146 free(id_str);
8147 free(folded_logmsg);
8148 return err;
8151 static struct got_histedit_list_entry *
8152 get_folded_commits(struct got_histedit_list_entry *hle)
8154 struct got_histedit_list_entry *prev, *folded = NULL;
8156 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8157 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8158 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8159 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8160 folded = prev;
8161 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8164 return folded;
8167 static const struct got_error *
8168 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8169 struct got_repository *repo)
8171 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8172 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8173 const struct got_error *err = NULL;
8174 struct got_commit_object *commit = NULL;
8175 int logmsg_len;
8176 int fd;
8177 struct got_histedit_list_entry *folded = NULL;
8179 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8180 if (err)
8181 return err;
8183 folded = get_folded_commits(hle);
8184 if (folded) {
8185 while (folded != hle) {
8186 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8187 folded = TAILQ_NEXT(folded, entry);
8188 continue;
8190 err = append_folded_commit_msg(&new_msg, folded,
8191 logmsg, repo);
8192 if (err)
8193 goto done;
8194 free(logmsg);
8195 logmsg = new_msg;
8196 folded = TAILQ_NEXT(folded, entry);
8200 err = got_object_id_str(&id_str, hle->commit_id);
8201 if (err)
8202 goto done;
8203 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8204 if (err)
8205 goto done;
8206 logmsg_len = asprintf(&new_msg,
8207 "%s\n# original log message of commit %s: %s",
8208 logmsg ? logmsg : "", id_str, orig_logmsg);
8209 if (logmsg_len == -1) {
8210 err = got_error_from_errno("asprintf");
8211 goto done;
8213 free(logmsg);
8214 logmsg = new_msg;
8216 err = got_object_id_str(&id_str, hle->commit_id);
8217 if (err)
8218 goto done;
8220 err = got_opentemp_named_fd(&logmsg_path, &fd,
8221 GOT_TMPDIR_STR "/got-logmsg");
8222 if (err)
8223 goto done;
8225 write(fd, logmsg, logmsg_len);
8226 close(fd);
8228 err = get_editor(&editor);
8229 if (err)
8230 goto done;
8232 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8233 logmsg_len, 0);
8234 if (err) {
8235 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8236 goto done;
8237 err = NULL;
8238 hle->logmsg = strdup(new_msg);
8239 if (hle->logmsg == NULL)
8240 err = got_error_from_errno("strdup");
8242 done:
8243 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8244 err = got_error_from_errno2("unlink", logmsg_path);
8245 free(logmsg_path);
8246 free(logmsg);
8247 free(orig_logmsg);
8248 free(editor);
8249 if (commit)
8250 got_object_commit_close(commit);
8251 return err;
8254 static const struct got_error *
8255 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8256 FILE *f, struct got_repository *repo)
8258 const struct got_error *err = NULL;
8259 char *line = NULL, *p, *end;
8260 size_t i, size;
8261 ssize_t len;
8262 int lineno = 0;
8263 const struct got_histedit_cmd *cmd;
8264 struct got_object_id *commit_id = NULL;
8265 struct got_histedit_list_entry *hle = NULL;
8267 for (;;) {
8268 len = getline(&line, &size, f);
8269 if (len == -1) {
8270 const struct got_error *getline_err;
8271 if (feof(f))
8272 break;
8273 getline_err = got_error_from_errno("getline");
8274 err = got_ferror(f, getline_err->code);
8275 break;
8277 lineno++;
8278 p = line;
8279 while (isspace((unsigned char)p[0]))
8280 p++;
8281 if (p[0] == '#' || p[0] == '\0') {
8282 free(line);
8283 line = NULL;
8284 continue;
8286 cmd = NULL;
8287 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8288 cmd = &got_histedit_cmds[i];
8289 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8290 isspace((unsigned char)p[strlen(cmd->name)])) {
8291 p += strlen(cmd->name);
8292 break;
8294 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8295 p++;
8296 break;
8299 if (i == nitems(got_histedit_cmds)) {
8300 err = histedit_syntax_error(lineno);
8301 break;
8303 while (isspace((unsigned char)p[0]))
8304 p++;
8305 if (cmd->code == GOT_HISTEDIT_MESG) {
8306 if (hle == NULL || hle->logmsg != NULL) {
8307 err = got_error(GOT_ERR_HISTEDIT_CMD);
8308 break;
8310 if (p[0] == '\0') {
8311 err = histedit_edit_logmsg(hle, repo);
8312 if (err)
8313 break;
8314 } else {
8315 hle->logmsg = strdup(p);
8316 if (hle->logmsg == NULL) {
8317 err = got_error_from_errno("strdup");
8318 break;
8321 free(line);
8322 line = NULL;
8323 continue;
8324 } else {
8325 end = p;
8326 while (end[0] && !isspace((unsigned char)end[0]))
8327 end++;
8328 *end = '\0';
8330 err = got_object_resolve_id_str(&commit_id, repo, p);
8331 if (err) {
8332 /* override error code */
8333 err = histedit_syntax_error(lineno);
8334 break;
8337 hle = malloc(sizeof(*hle));
8338 if (hle == NULL) {
8339 err = got_error_from_errno("malloc");
8340 break;
8342 hle->cmd = cmd;
8343 hle->commit_id = commit_id;
8344 hle->logmsg = NULL;
8345 commit_id = NULL;
8346 free(line);
8347 line = NULL;
8348 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8351 free(line);
8352 free(commit_id);
8353 return err;
8356 static const struct got_error *
8357 histedit_check_script(struct got_histedit_list *histedit_cmds,
8358 struct got_object_id_queue *commits, struct got_repository *repo)
8360 const struct got_error *err = NULL;
8361 struct got_object_qid *qid;
8362 struct got_histedit_list_entry *hle;
8363 static char msg[92];
8364 char *id_str;
8366 if (TAILQ_EMPTY(histedit_cmds))
8367 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8368 "histedit script contains no commands");
8369 if (SIMPLEQ_EMPTY(commits))
8370 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8372 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8373 struct got_histedit_list_entry *hle2;
8374 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8375 if (hle == hle2)
8376 continue;
8377 if (got_object_id_cmp(hle->commit_id,
8378 hle2->commit_id) != 0)
8379 continue;
8380 err = got_object_id_str(&id_str, hle->commit_id);
8381 if (err)
8382 return err;
8383 snprintf(msg, sizeof(msg), "commit %s is listed "
8384 "more than once in histedit script", id_str);
8385 free(id_str);
8386 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8390 SIMPLEQ_FOREACH(qid, commits, entry) {
8391 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8392 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8393 break;
8395 if (hle == NULL) {
8396 err = got_object_id_str(&id_str, qid->id);
8397 if (err)
8398 return err;
8399 snprintf(msg, sizeof(msg),
8400 "commit %s missing from histedit script", id_str);
8401 free(id_str);
8402 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8406 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8407 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8408 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8409 "last commit in histedit script cannot be folded");
8411 return NULL;
8414 static const struct got_error *
8415 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8416 const char *path, struct got_object_id_queue *commits,
8417 struct got_repository *repo)
8419 const struct got_error *err = NULL;
8420 char *editor;
8421 FILE *f = NULL;
8423 err = get_editor(&editor);
8424 if (err)
8425 return err;
8427 if (spawn_editor(editor, path) == -1) {
8428 err = got_error_from_errno("failed spawning editor");
8429 goto done;
8432 f = fopen(path, "r");
8433 if (f == NULL) {
8434 err = got_error_from_errno("fopen");
8435 goto done;
8437 err = histedit_parse_list(histedit_cmds, f, repo);
8438 if (err)
8439 goto done;
8441 err = histedit_check_script(histedit_cmds, commits, repo);
8442 done:
8443 if (f && fclose(f) == EOF && err == NULL)
8444 err = got_error_from_errno("fclose");
8445 free(editor);
8446 return err;
8449 static const struct got_error *
8450 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8451 struct got_object_id_queue *, const char *, const char *,
8452 struct got_repository *);
8454 static const struct got_error *
8455 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8456 struct got_object_id_queue *commits, const char *branch_name,
8457 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8459 const struct got_error *err;
8460 FILE *f = NULL;
8461 char *path = NULL;
8463 err = got_opentemp_named(&path, &f, "got-histedit");
8464 if (err)
8465 return err;
8467 err = write_cmd_list(f, branch_name, commits);
8468 if (err)
8469 goto done;
8471 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8472 fold_only, repo);
8473 if (err)
8474 goto done;
8476 if (edit_logmsg_only || fold_only) {
8477 rewind(f);
8478 err = histedit_parse_list(histedit_cmds, f, repo);
8479 } else {
8480 if (fclose(f) == EOF) {
8481 err = got_error_from_errno("fclose");
8482 goto done;
8484 f = NULL;
8485 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8486 if (err) {
8487 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8488 err->code != GOT_ERR_HISTEDIT_CMD)
8489 goto done;
8490 err = histedit_edit_list_retry(histedit_cmds, err,
8491 commits, path, branch_name, repo);
8494 done:
8495 if (f && fclose(f) == EOF && err == NULL)
8496 err = got_error_from_errno("fclose");
8497 if (path && unlink(path) != 0 && err == NULL)
8498 err = got_error_from_errno2("unlink", path);
8499 free(path);
8500 return err;
8503 static const struct got_error *
8504 histedit_save_list(struct got_histedit_list *histedit_cmds,
8505 struct got_worktree *worktree, struct got_repository *repo)
8507 const struct got_error *err = NULL;
8508 char *path = NULL;
8509 FILE *f = NULL;
8510 struct got_histedit_list_entry *hle;
8511 struct got_commit_object *commit = NULL;
8513 err = got_worktree_get_histedit_script_path(&path, worktree);
8514 if (err)
8515 return err;
8517 f = fopen(path, "w");
8518 if (f == NULL) {
8519 err = got_error_from_errno2("fopen", path);
8520 goto done;
8522 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8523 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8524 repo);
8525 if (err)
8526 break;
8528 if (hle->logmsg) {
8529 int n = fprintf(f, "%c %s\n",
8530 GOT_HISTEDIT_MESG, hle->logmsg);
8531 if (n < 0) {
8532 err = got_ferror(f, GOT_ERR_IO);
8533 break;
8537 done:
8538 if (f && fclose(f) == EOF && err == NULL)
8539 err = got_error_from_errno("fclose");
8540 free(path);
8541 if (commit)
8542 got_object_commit_close(commit);
8543 return err;
8546 void
8547 histedit_free_list(struct got_histedit_list *histedit_cmds)
8549 struct got_histedit_list_entry *hle;
8551 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8552 TAILQ_REMOVE(histedit_cmds, hle, entry);
8553 free(hle);
8557 static const struct got_error *
8558 histedit_load_list(struct got_histedit_list *histedit_cmds,
8559 const char *path, struct got_repository *repo)
8561 const struct got_error *err = NULL;
8562 FILE *f = NULL;
8564 f = fopen(path, "r");
8565 if (f == NULL) {
8566 err = got_error_from_errno2("fopen", path);
8567 goto done;
8570 err = histedit_parse_list(histedit_cmds, f, repo);
8571 done:
8572 if (f && fclose(f) == EOF && err == NULL)
8573 err = got_error_from_errno("fclose");
8574 return err;
8577 static const struct got_error *
8578 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8579 const struct got_error *edit_err, struct got_object_id_queue *commits,
8580 const char *path, const char *branch_name, struct got_repository *repo)
8582 const struct got_error *err = NULL, *prev_err = edit_err;
8583 int resp = ' ';
8585 while (resp != 'c' && resp != 'r' && resp != 'a') {
8586 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8587 "or (a)bort: ", getprogname(), prev_err->msg);
8588 resp = getchar();
8589 if (resp == '\n')
8590 resp = getchar();
8591 if (resp == 'c') {
8592 histedit_free_list(histedit_cmds);
8593 err = histedit_run_editor(histedit_cmds, path, commits,
8594 repo);
8595 if (err) {
8596 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8597 err->code != GOT_ERR_HISTEDIT_CMD)
8598 break;
8599 prev_err = err;
8600 resp = ' ';
8601 continue;
8603 break;
8604 } else if (resp == 'r') {
8605 histedit_free_list(histedit_cmds);
8606 err = histedit_edit_script(histedit_cmds,
8607 commits, branch_name, 0, 0, repo);
8608 if (err) {
8609 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8610 err->code != GOT_ERR_HISTEDIT_CMD)
8611 break;
8612 prev_err = err;
8613 resp = ' ';
8614 continue;
8616 break;
8617 } else if (resp == 'a') {
8618 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8619 break;
8620 } else
8621 printf("invalid response '%c'\n", resp);
8624 return err;
8627 static const struct got_error *
8628 histedit_complete(struct got_worktree *worktree,
8629 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8630 struct got_reference *branch, struct got_repository *repo)
8632 printf("Switching work tree to %s\n",
8633 got_ref_get_symref_target(branch));
8634 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8635 branch, repo);
8638 static const struct got_error *
8639 show_histedit_progress(struct got_commit_object *commit,
8640 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8642 const struct got_error *err;
8643 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8645 err = got_object_id_str(&old_id_str, hle->commit_id);
8646 if (err)
8647 goto done;
8649 if (new_id) {
8650 err = got_object_id_str(&new_id_str, new_id);
8651 if (err)
8652 goto done;
8655 old_id_str[12] = '\0';
8656 if (new_id_str)
8657 new_id_str[12] = '\0';
8659 if (hle->logmsg) {
8660 logmsg = strdup(hle->logmsg);
8661 if (logmsg == NULL) {
8662 err = got_error_from_errno("strdup");
8663 goto done;
8665 trim_logmsg(logmsg, 42);
8666 } else {
8667 err = get_short_logmsg(&logmsg, 42, commit);
8668 if (err)
8669 goto done;
8672 switch (hle->cmd->code) {
8673 case GOT_HISTEDIT_PICK:
8674 case GOT_HISTEDIT_EDIT:
8675 printf("%s -> %s: %s\n", old_id_str,
8676 new_id_str ? new_id_str : "no-op change", logmsg);
8677 break;
8678 case GOT_HISTEDIT_DROP:
8679 case GOT_HISTEDIT_FOLD:
8680 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8681 logmsg);
8682 break;
8683 default:
8684 break;
8686 done:
8687 free(old_id_str);
8688 free(new_id_str);
8689 return err;
8692 static const struct got_error *
8693 histedit_commit(struct got_pathlist_head *merged_paths,
8694 struct got_worktree *worktree, struct got_fileindex *fileindex,
8695 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8696 struct got_repository *repo)
8698 const struct got_error *err;
8699 struct got_commit_object *commit;
8700 struct got_object_id *new_commit_id;
8702 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8703 && hle->logmsg == NULL) {
8704 err = histedit_edit_logmsg(hle, repo);
8705 if (err)
8706 return err;
8709 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8710 if (err)
8711 return err;
8713 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8714 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8715 hle->logmsg, repo);
8716 if (err) {
8717 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8718 goto done;
8719 err = show_histedit_progress(commit, hle, NULL);
8720 } else {
8721 err = show_histedit_progress(commit, hle, new_commit_id);
8722 free(new_commit_id);
8724 done:
8725 got_object_commit_close(commit);
8726 return err;
8729 static const struct got_error *
8730 histedit_skip_commit(struct got_histedit_list_entry *hle,
8731 struct got_worktree *worktree, struct got_repository *repo)
8733 const struct got_error *error;
8734 struct got_commit_object *commit;
8736 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8737 repo);
8738 if (error)
8739 return error;
8741 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8742 if (error)
8743 return error;
8745 error = show_histedit_progress(commit, hle, NULL);
8746 got_object_commit_close(commit);
8747 return error;
8750 static const struct got_error *
8751 check_local_changes(void *arg, unsigned char status,
8752 unsigned char staged_status, const char *path,
8753 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8754 struct got_object_id *commit_id, int dirfd, const char *de_name)
8756 int *have_local_changes = arg;
8758 switch (status) {
8759 case GOT_STATUS_ADD:
8760 case GOT_STATUS_DELETE:
8761 case GOT_STATUS_MODIFY:
8762 case GOT_STATUS_CONFLICT:
8763 *have_local_changes = 1;
8764 return got_error(GOT_ERR_CANCELLED);
8765 default:
8766 break;
8769 switch (staged_status) {
8770 case GOT_STATUS_ADD:
8771 case GOT_STATUS_DELETE:
8772 case GOT_STATUS_MODIFY:
8773 *have_local_changes = 1;
8774 return got_error(GOT_ERR_CANCELLED);
8775 default:
8776 break;
8779 return NULL;
8782 static const struct got_error *
8783 cmd_histedit(int argc, char *argv[])
8785 const struct got_error *error = NULL;
8786 struct got_worktree *worktree = NULL;
8787 struct got_fileindex *fileindex = NULL;
8788 struct got_repository *repo = NULL;
8789 char *cwd = NULL;
8790 struct got_reference *branch = NULL;
8791 struct got_reference *tmp_branch = NULL;
8792 struct got_object_id *resume_commit_id = NULL;
8793 struct got_object_id *base_commit_id = NULL;
8794 struct got_object_id *head_commit_id = NULL;
8795 struct got_commit_object *commit = NULL;
8796 int ch, rebase_in_progress = 0;
8797 struct got_update_progress_arg upa;
8798 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8799 int edit_logmsg_only = 0, fold_only = 0;
8800 const char *edit_script_path = NULL;
8801 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8802 struct got_object_id_queue commits;
8803 struct got_pathlist_head merged_paths;
8804 const struct got_object_id_queue *parent_ids;
8805 struct got_object_qid *pid;
8806 struct got_histedit_list histedit_cmds;
8807 struct got_histedit_list_entry *hle;
8809 SIMPLEQ_INIT(&commits);
8810 TAILQ_INIT(&histedit_cmds);
8811 TAILQ_INIT(&merged_paths);
8812 memset(&upa, 0, sizeof(upa));
8814 while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8815 switch (ch) {
8816 case 'a':
8817 abort_edit = 1;
8818 break;
8819 case 'c':
8820 continue_edit = 1;
8821 break;
8822 case 'f':
8823 fold_only = 1;
8824 break;
8825 case 'F':
8826 edit_script_path = optarg;
8827 break;
8828 case 'm':
8829 edit_logmsg_only = 1;
8830 break;
8831 default:
8832 usage_histedit();
8833 /* NOTREACHED */
8837 argc -= optind;
8838 argv += optind;
8840 #ifndef PROFILE
8841 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8842 "unveil", NULL) == -1)
8843 err(1, "pledge");
8844 #endif
8845 if (abort_edit && continue_edit)
8846 option_conflict('a', 'c');
8847 if (edit_script_path && edit_logmsg_only)
8848 option_conflict('F', 'm');
8849 if (abort_edit && edit_logmsg_only)
8850 option_conflict('a', 'm');
8851 if (continue_edit && edit_logmsg_only)
8852 option_conflict('c', 'm');
8853 if (abort_edit && fold_only)
8854 option_conflict('a', 'f');
8855 if (continue_edit && fold_only)
8856 option_conflict('c', 'f');
8857 if (fold_only && edit_logmsg_only)
8858 option_conflict('f', 'm');
8859 if (edit_script_path && fold_only)
8860 option_conflict('F', 'f');
8861 if (argc != 0)
8862 usage_histedit();
8865 * This command cannot apply unveil(2) in all cases because the
8866 * user may choose to run an editor to edit the histedit script
8867 * and to edit individual commit log messages.
8868 * unveil(2) traverses exec(2); if an editor is used we have to
8869 * apply unveil after edit script and log messages have been written.
8870 * XXX TODO: Make use of unveil(2) where possible.
8873 cwd = getcwd(NULL, 0);
8874 if (cwd == NULL) {
8875 error = got_error_from_errno("getcwd");
8876 goto done;
8878 error = got_worktree_open(&worktree, cwd);
8879 if (error) {
8880 if (error->code == GOT_ERR_NOT_WORKTREE)
8881 error = wrap_not_worktree_error(error, "histedit", cwd);
8882 goto done;
8885 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8886 NULL);
8887 if (error != NULL)
8888 goto done;
8890 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8891 if (error)
8892 goto done;
8893 if (rebase_in_progress) {
8894 error = got_error(GOT_ERR_REBASING);
8895 goto done;
8898 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8899 if (error)
8900 goto done;
8902 if (edit_in_progress && edit_logmsg_only) {
8903 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8904 "histedit operation is in progress in this "
8905 "work tree and must be continued or aborted "
8906 "before the -m option can be used");
8907 goto done;
8909 if (edit_in_progress && fold_only) {
8910 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8911 "histedit operation is in progress in this "
8912 "work tree and must be continued or aborted "
8913 "before the -f option can be used");
8914 goto done;
8917 if (edit_in_progress && abort_edit) {
8918 error = got_worktree_histedit_continue(&resume_commit_id,
8919 &tmp_branch, &branch, &base_commit_id, &fileindex,
8920 worktree, repo);
8921 if (error)
8922 goto done;
8923 printf("Switching work tree to %s\n",
8924 got_ref_get_symref_target(branch));
8925 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8926 branch, base_commit_id, update_progress, &upa);
8927 if (error)
8928 goto done;
8929 printf("Histedit of %s aborted\n",
8930 got_ref_get_symref_target(branch));
8931 print_update_progress_stats(&upa);
8932 goto done; /* nothing else to do */
8933 } else if (abort_edit) {
8934 error = got_error(GOT_ERR_NOT_HISTEDIT);
8935 goto done;
8938 if (continue_edit) {
8939 char *path;
8941 if (!edit_in_progress) {
8942 error = got_error(GOT_ERR_NOT_HISTEDIT);
8943 goto done;
8946 error = got_worktree_get_histedit_script_path(&path, worktree);
8947 if (error)
8948 goto done;
8950 error = histedit_load_list(&histedit_cmds, path, repo);
8951 free(path);
8952 if (error)
8953 goto done;
8955 error = got_worktree_histedit_continue(&resume_commit_id,
8956 &tmp_branch, &branch, &base_commit_id, &fileindex,
8957 worktree, repo);
8958 if (error)
8959 goto done;
8961 error = got_ref_resolve(&head_commit_id, repo, branch);
8962 if (error)
8963 goto done;
8965 error = got_object_open_as_commit(&commit, repo,
8966 head_commit_id);
8967 if (error)
8968 goto done;
8969 parent_ids = got_object_commit_get_parent_ids(commit);
8970 pid = SIMPLEQ_FIRST(parent_ids);
8971 if (pid == NULL) {
8972 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8973 goto done;
8975 error = collect_commits(&commits, head_commit_id, pid->id,
8976 base_commit_id, got_worktree_get_path_prefix(worktree),
8977 GOT_ERR_HISTEDIT_PATH, repo);
8978 got_object_commit_close(commit);
8979 commit = NULL;
8980 if (error)
8981 goto done;
8982 } else {
8983 if (edit_in_progress) {
8984 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8985 goto done;
8988 error = got_ref_open(&branch, repo,
8989 got_worktree_get_head_ref_name(worktree), 0);
8990 if (error != NULL)
8991 goto done;
8993 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8994 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8995 "will not edit commit history of a branch outside "
8996 "the \"refs/heads/\" reference namespace");
8997 goto done;
9000 error = got_ref_resolve(&head_commit_id, repo, branch);
9001 got_ref_close(branch);
9002 branch = NULL;
9003 if (error)
9004 goto done;
9006 error = got_object_open_as_commit(&commit, repo,
9007 head_commit_id);
9008 if (error)
9009 goto done;
9010 parent_ids = got_object_commit_get_parent_ids(commit);
9011 pid = SIMPLEQ_FIRST(parent_ids);
9012 if (pid == NULL) {
9013 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9014 goto done;
9016 error = collect_commits(&commits, head_commit_id, pid->id,
9017 got_worktree_get_base_commit_id(worktree),
9018 got_worktree_get_path_prefix(worktree),
9019 GOT_ERR_HISTEDIT_PATH, repo);
9020 got_object_commit_close(commit);
9021 commit = NULL;
9022 if (error)
9023 goto done;
9025 if (SIMPLEQ_EMPTY(&commits)) {
9026 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9027 goto done;
9030 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9031 &base_commit_id, &fileindex, worktree, repo);
9032 if (error)
9033 goto done;
9035 if (edit_script_path) {
9036 error = histedit_load_list(&histedit_cmds,
9037 edit_script_path, repo);
9038 if (error) {
9039 got_worktree_histedit_abort(worktree, fileindex,
9040 repo, branch, base_commit_id,
9041 update_progress, &upa);
9042 print_update_progress_stats(&upa);
9043 goto done;
9045 } else {
9046 const char *branch_name;
9047 branch_name = got_ref_get_symref_target(branch);
9048 if (strncmp(branch_name, "refs/heads/", 11) == 0)
9049 branch_name += 11;
9050 error = histedit_edit_script(&histedit_cmds, &commits,
9051 branch_name, edit_logmsg_only, fold_only, repo);
9052 if (error) {
9053 got_worktree_histedit_abort(worktree, fileindex,
9054 repo, branch, base_commit_id,
9055 update_progress, &upa);
9056 print_update_progress_stats(&upa);
9057 goto done;
9062 error = histedit_save_list(&histedit_cmds, worktree,
9063 repo);
9064 if (error) {
9065 got_worktree_histedit_abort(worktree, fileindex,
9066 repo, branch, base_commit_id,
9067 update_progress, &upa);
9068 print_update_progress_stats(&upa);
9069 goto done;
9074 error = histedit_check_script(&histedit_cmds, &commits, repo);
9075 if (error)
9076 goto done;
9078 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9079 if (resume_commit_id) {
9080 if (got_object_id_cmp(hle->commit_id,
9081 resume_commit_id) != 0)
9082 continue;
9084 resume_commit_id = NULL;
9085 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9086 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9087 error = histedit_skip_commit(hle, worktree,
9088 repo);
9089 if (error)
9090 goto done;
9091 } else {
9092 struct got_pathlist_head paths;
9093 int have_changes = 0;
9095 TAILQ_INIT(&paths);
9096 error = got_pathlist_append(&paths, "", NULL);
9097 if (error)
9098 goto done;
9099 error = got_worktree_status(worktree, &paths,
9100 repo, check_local_changes, &have_changes,
9101 check_cancelled, NULL);
9102 got_pathlist_free(&paths);
9103 if (error) {
9104 if (error->code != GOT_ERR_CANCELLED)
9105 goto done;
9106 if (sigint_received || sigpipe_received)
9107 goto done;
9109 if (have_changes) {
9110 error = histedit_commit(NULL, worktree,
9111 fileindex, tmp_branch, hle, repo);
9112 if (error)
9113 goto done;
9114 } else {
9115 error = got_object_open_as_commit(
9116 &commit, repo, hle->commit_id);
9117 if (error)
9118 goto done;
9119 error = show_histedit_progress(commit,
9120 hle, NULL);
9121 got_object_commit_close(commit);
9122 commit = NULL;
9123 if (error)
9124 goto done;
9127 continue;
9130 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9131 error = histedit_skip_commit(hle, worktree, repo);
9132 if (error)
9133 goto done;
9134 continue;
9137 error = got_object_open_as_commit(&commit, repo,
9138 hle->commit_id);
9139 if (error)
9140 goto done;
9141 parent_ids = got_object_commit_get_parent_ids(commit);
9142 pid = SIMPLEQ_FIRST(parent_ids);
9144 error = got_worktree_histedit_merge_files(&merged_paths,
9145 worktree, fileindex, pid->id, hle->commit_id, repo,
9146 update_progress, &upa, check_cancelled, NULL);
9147 if (error)
9148 goto done;
9149 got_object_commit_close(commit);
9150 commit = NULL;
9152 print_update_progress_stats(&upa);
9153 if (upa.conflicts > 0)
9154 rebase_status = GOT_STATUS_CONFLICT;
9156 if (rebase_status == GOT_STATUS_CONFLICT) {
9157 error = show_rebase_merge_conflict(hle->commit_id,
9158 repo);
9159 if (error)
9160 goto done;
9161 got_worktree_rebase_pathlist_free(&merged_paths);
9162 break;
9165 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9166 char *id_str;
9167 error = got_object_id_str(&id_str, hle->commit_id);
9168 if (error)
9169 goto done;
9170 printf("Stopping histedit for amending commit %s\n",
9171 id_str);
9172 free(id_str);
9173 got_worktree_rebase_pathlist_free(&merged_paths);
9174 error = got_worktree_histedit_postpone(worktree,
9175 fileindex);
9176 goto done;
9179 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9180 error = histedit_skip_commit(hle, worktree, repo);
9181 if (error)
9182 goto done;
9183 continue;
9186 error = histedit_commit(&merged_paths, worktree, fileindex,
9187 tmp_branch, hle, repo);
9188 got_worktree_rebase_pathlist_free(&merged_paths);
9189 if (error)
9190 goto done;
9193 if (rebase_status == GOT_STATUS_CONFLICT) {
9194 error = got_worktree_histedit_postpone(worktree, fileindex);
9195 if (error)
9196 goto done;
9197 error = got_error_msg(GOT_ERR_CONFLICTS,
9198 "conflicts must be resolved before histedit can continue");
9199 } else
9200 error = histedit_complete(worktree, fileindex, tmp_branch,
9201 branch, repo);
9202 done:
9203 got_object_id_queue_free(&commits);
9204 histedit_free_list(&histedit_cmds);
9205 free(head_commit_id);
9206 free(base_commit_id);
9207 free(resume_commit_id);
9208 if (commit)
9209 got_object_commit_close(commit);
9210 if (branch)
9211 got_ref_close(branch);
9212 if (tmp_branch)
9213 got_ref_close(tmp_branch);
9214 if (worktree)
9215 got_worktree_close(worktree);
9216 if (repo)
9217 got_repo_close(repo);
9218 return error;
9221 __dead static void
9222 usage_integrate(void)
9224 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9225 exit(1);
9228 static const struct got_error *
9229 cmd_integrate(int argc, char *argv[])
9231 const struct got_error *error = NULL;
9232 struct got_repository *repo = NULL;
9233 struct got_worktree *worktree = NULL;
9234 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9235 const char *branch_arg = NULL;
9236 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9237 struct got_fileindex *fileindex = NULL;
9238 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9239 int ch;
9240 struct got_update_progress_arg upa;
9242 while ((ch = getopt(argc, argv, "")) != -1) {
9243 switch (ch) {
9244 default:
9245 usage_integrate();
9246 /* NOTREACHED */
9250 argc -= optind;
9251 argv += optind;
9253 if (argc != 1)
9254 usage_integrate();
9255 branch_arg = argv[0];
9256 #ifndef PROFILE
9257 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9258 "unveil", NULL) == -1)
9259 err(1, "pledge");
9260 #endif
9261 cwd = getcwd(NULL, 0);
9262 if (cwd == NULL) {
9263 error = got_error_from_errno("getcwd");
9264 goto done;
9267 error = got_worktree_open(&worktree, cwd);
9268 if (error) {
9269 if (error->code == GOT_ERR_NOT_WORKTREE)
9270 error = wrap_not_worktree_error(error, "integrate",
9271 cwd);
9272 goto done;
9275 error = check_rebase_or_histedit_in_progress(worktree);
9276 if (error)
9277 goto done;
9279 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9280 NULL);
9281 if (error != NULL)
9282 goto done;
9284 error = apply_unveil(got_repo_get_path(repo), 0,
9285 got_worktree_get_root_path(worktree));
9286 if (error)
9287 goto done;
9289 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9290 error = got_error_from_errno("asprintf");
9291 goto done;
9294 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9295 &base_branch_ref, worktree, refname, repo);
9296 if (error)
9297 goto done;
9299 refname = strdup(got_ref_get_name(branch_ref));
9300 if (refname == NULL) {
9301 error = got_error_from_errno("strdup");
9302 got_worktree_integrate_abort(worktree, fileindex, repo,
9303 branch_ref, base_branch_ref);
9304 goto done;
9306 base_refname = strdup(got_ref_get_name(base_branch_ref));
9307 if (base_refname == NULL) {
9308 error = got_error_from_errno("strdup");
9309 got_worktree_integrate_abort(worktree, fileindex, repo,
9310 branch_ref, base_branch_ref);
9311 goto done;
9314 error = got_ref_resolve(&commit_id, repo, branch_ref);
9315 if (error)
9316 goto done;
9318 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9319 if (error)
9320 goto done;
9322 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9323 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9324 "specified branch has already been integrated");
9325 got_worktree_integrate_abort(worktree, fileindex, repo,
9326 branch_ref, base_branch_ref);
9327 goto done;
9330 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9331 if (error) {
9332 if (error->code == GOT_ERR_ANCESTRY)
9333 error = got_error(GOT_ERR_REBASE_REQUIRED);
9334 got_worktree_integrate_abort(worktree, fileindex, repo,
9335 branch_ref, base_branch_ref);
9336 goto done;
9339 memset(&upa, 0, sizeof(upa));
9340 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9341 branch_ref, base_branch_ref, update_progress, &upa,
9342 check_cancelled, NULL);
9343 if (error)
9344 goto done;
9346 printf("Integrated %s into %s\n", refname, base_refname);
9347 print_update_progress_stats(&upa);
9348 done:
9349 if (repo)
9350 got_repo_close(repo);
9351 if (worktree)
9352 got_worktree_close(worktree);
9353 free(cwd);
9354 free(base_commit_id);
9355 free(commit_id);
9356 free(refname);
9357 free(base_refname);
9358 return error;
9361 __dead static void
9362 usage_stage(void)
9364 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9365 "[-S] [file-path ...]\n",
9366 getprogname());
9367 exit(1);
9370 static const struct got_error *
9371 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9372 const char *path, struct got_object_id *blob_id,
9373 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9374 int dirfd, const char *de_name)
9376 const struct got_error *err = NULL;
9377 char *id_str = NULL;
9379 if (staged_status != GOT_STATUS_ADD &&
9380 staged_status != GOT_STATUS_MODIFY &&
9381 staged_status != GOT_STATUS_DELETE)
9382 return NULL;
9384 if (staged_status == GOT_STATUS_ADD ||
9385 staged_status == GOT_STATUS_MODIFY)
9386 err = got_object_id_str(&id_str, staged_blob_id);
9387 else
9388 err = got_object_id_str(&id_str, blob_id);
9389 if (err)
9390 return err;
9392 printf("%s %c %s\n", id_str, staged_status, path);
9393 free(id_str);
9394 return NULL;
9397 static const struct got_error *
9398 cmd_stage(int argc, char *argv[])
9400 const struct got_error *error = NULL;
9401 struct got_repository *repo = NULL;
9402 struct got_worktree *worktree = NULL;
9403 char *cwd = NULL;
9404 struct got_pathlist_head paths;
9405 struct got_pathlist_entry *pe;
9406 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9407 FILE *patch_script_file = NULL;
9408 const char *patch_script_path = NULL;
9409 struct choose_patch_arg cpa;
9411 TAILQ_INIT(&paths);
9413 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9414 switch (ch) {
9415 case 'l':
9416 list_stage = 1;
9417 break;
9418 case 'p':
9419 pflag = 1;
9420 break;
9421 case 'F':
9422 patch_script_path = optarg;
9423 break;
9424 case 'S':
9425 allow_bad_symlinks = 1;
9426 break;
9427 default:
9428 usage_stage();
9429 /* NOTREACHED */
9433 argc -= optind;
9434 argv += optind;
9436 #ifndef PROFILE
9437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9438 "unveil", NULL) == -1)
9439 err(1, "pledge");
9440 #endif
9441 if (list_stage && (pflag || patch_script_path))
9442 errx(1, "-l option cannot be used with other options");
9443 if (patch_script_path && !pflag)
9444 errx(1, "-F option can only be used together with -p option");
9446 cwd = getcwd(NULL, 0);
9447 if (cwd == NULL) {
9448 error = got_error_from_errno("getcwd");
9449 goto done;
9452 error = got_worktree_open(&worktree, cwd);
9453 if (error) {
9454 if (error->code == GOT_ERR_NOT_WORKTREE)
9455 error = wrap_not_worktree_error(error, "stage", cwd);
9456 goto done;
9459 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9460 NULL);
9461 if (error != NULL)
9462 goto done;
9464 if (patch_script_path) {
9465 patch_script_file = fopen(patch_script_path, "r");
9466 if (patch_script_file == NULL) {
9467 error = got_error_from_errno2("fopen",
9468 patch_script_path);
9469 goto done;
9472 error = apply_unveil(got_repo_get_path(repo), 0,
9473 got_worktree_get_root_path(worktree));
9474 if (error)
9475 goto done;
9477 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9478 if (error)
9479 goto done;
9481 if (list_stage)
9482 error = got_worktree_status(worktree, &paths, repo,
9483 print_stage, NULL, check_cancelled, NULL);
9484 else {
9485 cpa.patch_script_file = patch_script_file;
9486 cpa.action = "stage";
9487 error = got_worktree_stage(worktree, &paths,
9488 pflag ? NULL : print_status, NULL,
9489 pflag ? choose_patch : NULL, &cpa,
9490 allow_bad_symlinks, repo);
9492 done:
9493 if (patch_script_file && fclose(patch_script_file) == EOF &&
9494 error == NULL)
9495 error = got_error_from_errno2("fclose", patch_script_path);
9496 if (repo)
9497 got_repo_close(repo);
9498 if (worktree)
9499 got_worktree_close(worktree);
9500 TAILQ_FOREACH(pe, &paths, entry)
9501 free((char *)pe->path);
9502 got_pathlist_free(&paths);
9503 free(cwd);
9504 return error;
9507 __dead static void
9508 usage_unstage(void)
9510 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9511 "[file-path ...]\n",
9512 getprogname());
9513 exit(1);
9517 static const struct got_error *
9518 cmd_unstage(int argc, char *argv[])
9520 const struct got_error *error = NULL;
9521 struct got_repository *repo = NULL;
9522 struct got_worktree *worktree = NULL;
9523 char *cwd = NULL;
9524 struct got_pathlist_head paths;
9525 struct got_pathlist_entry *pe;
9526 int ch, pflag = 0;
9527 struct got_update_progress_arg upa;
9528 FILE *patch_script_file = NULL;
9529 const char *patch_script_path = NULL;
9530 struct choose_patch_arg cpa;
9532 TAILQ_INIT(&paths);
9534 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9535 switch (ch) {
9536 case 'p':
9537 pflag = 1;
9538 break;
9539 case 'F':
9540 patch_script_path = optarg;
9541 break;
9542 default:
9543 usage_unstage();
9544 /* NOTREACHED */
9548 argc -= optind;
9549 argv += optind;
9551 #ifndef PROFILE
9552 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9553 "unveil", NULL) == -1)
9554 err(1, "pledge");
9555 #endif
9556 if (patch_script_path && !pflag)
9557 errx(1, "-F option can only be used together with -p option");
9559 cwd = getcwd(NULL, 0);
9560 if (cwd == NULL) {
9561 error = got_error_from_errno("getcwd");
9562 goto done;
9565 error = got_worktree_open(&worktree, cwd);
9566 if (error) {
9567 if (error->code == GOT_ERR_NOT_WORKTREE)
9568 error = wrap_not_worktree_error(error, "unstage", cwd);
9569 goto done;
9572 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9573 NULL);
9574 if (error != NULL)
9575 goto done;
9577 if (patch_script_path) {
9578 patch_script_file = fopen(patch_script_path, "r");
9579 if (patch_script_file == NULL) {
9580 error = got_error_from_errno2("fopen",
9581 patch_script_path);
9582 goto done;
9586 error = apply_unveil(got_repo_get_path(repo), 0,
9587 got_worktree_get_root_path(worktree));
9588 if (error)
9589 goto done;
9591 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9592 if (error)
9593 goto done;
9595 cpa.patch_script_file = patch_script_file;
9596 cpa.action = "unstage";
9597 memset(&upa, 0, sizeof(upa));
9598 error = got_worktree_unstage(worktree, &paths, update_progress,
9599 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9600 if (!error)
9601 print_update_progress_stats(&upa);
9602 done:
9603 if (patch_script_file && fclose(patch_script_file) == EOF &&
9604 error == NULL)
9605 error = got_error_from_errno2("fclose", patch_script_path);
9606 if (repo)
9607 got_repo_close(repo);
9608 if (worktree)
9609 got_worktree_close(worktree);
9610 TAILQ_FOREACH(pe, &paths, entry)
9611 free((char *)pe->path);
9612 got_pathlist_free(&paths);
9613 free(cwd);
9614 return error;
9617 __dead static void
9618 usage_cat(void)
9620 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9621 "arg1 [arg2 ...]\n", getprogname());
9622 exit(1);
9625 static const struct got_error *
9626 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9628 const struct got_error *err;
9629 struct got_blob_object *blob;
9631 err = got_object_open_as_blob(&blob, repo, id, 8192);
9632 if (err)
9633 return err;
9635 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9636 got_object_blob_close(blob);
9637 return err;
9640 static const struct got_error *
9641 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9643 const struct got_error *err;
9644 struct got_tree_object *tree;
9645 int nentries, i;
9647 err = got_object_open_as_tree(&tree, repo, id);
9648 if (err)
9649 return err;
9651 nentries = got_object_tree_get_nentries(tree);
9652 for (i = 0; i < nentries; i++) {
9653 struct got_tree_entry *te;
9654 char *id_str;
9655 if (sigint_received || sigpipe_received)
9656 break;
9657 te = got_object_tree_get_entry(tree, i);
9658 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9659 if (err)
9660 break;
9661 fprintf(outfile, "%s %.7o %s\n", id_str,
9662 got_tree_entry_get_mode(te),
9663 got_tree_entry_get_name(te));
9664 free(id_str);
9667 got_object_tree_close(tree);
9668 return err;
9671 static const struct got_error *
9672 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9674 const struct got_error *err;
9675 struct got_commit_object *commit;
9676 const struct got_object_id_queue *parent_ids;
9677 struct got_object_qid *pid;
9678 char *id_str = NULL;
9679 const char *logmsg = NULL;
9681 err = got_object_open_as_commit(&commit, repo, id);
9682 if (err)
9683 return err;
9685 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9686 if (err)
9687 goto done;
9689 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9690 parent_ids = got_object_commit_get_parent_ids(commit);
9691 fprintf(outfile, "numparents %d\n",
9692 got_object_commit_get_nparents(commit));
9693 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9694 char *pid_str;
9695 err = got_object_id_str(&pid_str, pid->id);
9696 if (err)
9697 goto done;
9698 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9699 free(pid_str);
9701 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9702 got_object_commit_get_author(commit),
9703 (long long)got_object_commit_get_author_time(commit));
9705 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9706 got_object_commit_get_author(commit),
9707 (long long)got_object_commit_get_committer_time(commit));
9709 logmsg = got_object_commit_get_logmsg_raw(commit);
9710 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9711 fprintf(outfile, "%s", logmsg);
9712 done:
9713 free(id_str);
9714 got_object_commit_close(commit);
9715 return err;
9718 static const struct got_error *
9719 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9721 const struct got_error *err;
9722 struct got_tag_object *tag;
9723 char *id_str = NULL;
9724 const char *tagmsg = NULL;
9726 err = got_object_open_as_tag(&tag, repo, id);
9727 if (err)
9728 return err;
9730 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9731 if (err)
9732 goto done;
9734 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9736 switch (got_object_tag_get_object_type(tag)) {
9737 case GOT_OBJ_TYPE_BLOB:
9738 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9739 GOT_OBJ_LABEL_BLOB);
9740 break;
9741 case GOT_OBJ_TYPE_TREE:
9742 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9743 GOT_OBJ_LABEL_TREE);
9744 break;
9745 case GOT_OBJ_TYPE_COMMIT:
9746 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9747 GOT_OBJ_LABEL_COMMIT);
9748 break;
9749 case GOT_OBJ_TYPE_TAG:
9750 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9751 GOT_OBJ_LABEL_TAG);
9752 break;
9753 default:
9754 break;
9757 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9758 got_object_tag_get_name(tag));
9760 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9761 got_object_tag_get_tagger(tag),
9762 (long long)got_object_tag_get_tagger_time(tag));
9764 tagmsg = got_object_tag_get_message(tag);
9765 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9766 fprintf(outfile, "%s", tagmsg);
9767 done:
9768 free(id_str);
9769 got_object_tag_close(tag);
9770 return err;
9773 static const struct got_error *
9774 cmd_cat(int argc, char *argv[])
9776 const struct got_error *error;
9777 struct got_repository *repo = NULL;
9778 struct got_worktree *worktree = NULL;
9779 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9780 const char *commit_id_str = NULL;
9781 struct got_object_id *id = NULL, *commit_id = NULL;
9782 int ch, obj_type, i, force_path = 0;
9783 struct got_reflist_head refs;
9785 TAILQ_INIT(&refs);
9787 #ifndef PROFILE
9788 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9789 NULL) == -1)
9790 err(1, "pledge");
9791 #endif
9793 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9794 switch (ch) {
9795 case 'c':
9796 commit_id_str = optarg;
9797 break;
9798 case 'r':
9799 repo_path = realpath(optarg, NULL);
9800 if (repo_path == NULL)
9801 return got_error_from_errno2("realpath",
9802 optarg);
9803 got_path_strip_trailing_slashes(repo_path);
9804 break;
9805 case 'P':
9806 force_path = 1;
9807 break;
9808 default:
9809 usage_cat();
9810 /* NOTREACHED */
9814 argc -= optind;
9815 argv += optind;
9817 cwd = getcwd(NULL, 0);
9818 if (cwd == NULL) {
9819 error = got_error_from_errno("getcwd");
9820 goto done;
9822 error = got_worktree_open(&worktree, cwd);
9823 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9824 goto done;
9825 if (worktree) {
9826 if (repo_path == NULL) {
9827 repo_path = strdup(
9828 got_worktree_get_repo_path(worktree));
9829 if (repo_path == NULL) {
9830 error = got_error_from_errno("strdup");
9831 goto done;
9836 if (repo_path == NULL) {
9837 repo_path = getcwd(NULL, 0);
9838 if (repo_path == NULL)
9839 return got_error_from_errno("getcwd");
9842 error = got_repo_open(&repo, repo_path, NULL);
9843 free(repo_path);
9844 if (error != NULL)
9845 goto done;
9847 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9848 if (error)
9849 goto done;
9851 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9852 if (error)
9853 goto done;
9855 if (commit_id_str == NULL)
9856 commit_id_str = GOT_REF_HEAD;
9857 error = got_repo_match_object_id(&commit_id, NULL,
9858 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9859 if (error)
9860 goto done;
9862 for (i = 0; i < argc; i++) {
9863 if (force_path) {
9864 error = got_object_id_by_path(&id, repo, commit_id,
9865 argv[i]);
9866 if (error)
9867 break;
9868 } else {
9869 error = got_repo_match_object_id(&id, &label, argv[i],
9870 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9871 repo);
9872 if (error) {
9873 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9874 error->code != GOT_ERR_NOT_REF)
9875 break;
9876 error = got_object_id_by_path(&id, repo,
9877 commit_id, argv[i]);
9878 if (error)
9879 break;
9883 error = got_object_get_type(&obj_type, repo, id);
9884 if (error)
9885 break;
9887 switch (obj_type) {
9888 case GOT_OBJ_TYPE_BLOB:
9889 error = cat_blob(id, repo, stdout);
9890 break;
9891 case GOT_OBJ_TYPE_TREE:
9892 error = cat_tree(id, repo, stdout);
9893 break;
9894 case GOT_OBJ_TYPE_COMMIT:
9895 error = cat_commit(id, repo, stdout);
9896 break;
9897 case GOT_OBJ_TYPE_TAG:
9898 error = cat_tag(id, repo, stdout);
9899 break;
9900 default:
9901 error = got_error(GOT_ERR_OBJ_TYPE);
9902 break;
9904 if (error)
9905 break;
9906 free(label);
9907 label = NULL;
9908 free(id);
9909 id = NULL;
9911 done:
9912 free(label);
9913 free(id);
9914 free(commit_id);
9915 if (worktree)
9916 got_worktree_close(worktree);
9917 if (repo) {
9918 const struct got_error *repo_error;
9919 repo_error = got_repo_close(repo);
9920 if (error == NULL)
9921 error = repo_error;
9923 got_ref_list_free(&refs);
9924 return error;
9927 __dead static void
9928 usage_info(void)
9930 fprintf(stderr, "usage: %s info [path ...]\n",
9931 getprogname());
9932 exit(1);
9935 static const struct got_error *
9936 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9937 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9938 struct got_object_id *commit_id)
9940 const struct got_error *err = NULL;
9941 char *id_str = NULL;
9942 char datebuf[128];
9943 struct tm mytm, *tm;
9944 struct got_pathlist_head *paths = arg;
9945 struct got_pathlist_entry *pe;
9948 * Clear error indication from any of the path arguments which
9949 * would cause this file index entry to be displayed.
9951 TAILQ_FOREACH(pe, paths, entry) {
9952 if (got_path_cmp(path, pe->path, strlen(path),
9953 pe->path_len) == 0 ||
9954 got_path_is_child(path, pe->path, pe->path_len))
9955 pe->data = NULL; /* no error */
9958 printf(GOT_COMMIT_SEP_STR);
9959 if (S_ISLNK(mode))
9960 printf("symlink: %s\n", path);
9961 else if (S_ISREG(mode)) {
9962 printf("file: %s\n", path);
9963 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9964 } else if (S_ISDIR(mode))
9965 printf("directory: %s\n", path);
9966 else
9967 printf("something: %s\n", path);
9969 tm = localtime_r(&mtime, &mytm);
9970 if (tm == NULL)
9971 return NULL;
9972 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9973 return got_error(GOT_ERR_NO_SPACE);
9974 printf("timestamp: %s\n", datebuf);
9976 if (blob_id) {
9977 err = got_object_id_str(&id_str, blob_id);
9978 if (err)
9979 return err;
9980 printf("based on blob: %s\n", id_str);
9981 free(id_str);
9984 if (staged_blob_id) {
9985 err = got_object_id_str(&id_str, staged_blob_id);
9986 if (err)
9987 return err;
9988 printf("based on staged blob: %s\n", id_str);
9989 free(id_str);
9992 if (commit_id) {
9993 err = got_object_id_str(&id_str, commit_id);
9994 if (err)
9995 return err;
9996 printf("based on commit: %s\n", id_str);
9997 free(id_str);
10000 return NULL;
10003 static const struct got_error *
10004 cmd_info(int argc, char *argv[])
10006 const struct got_error *error = NULL;
10007 struct got_worktree *worktree = NULL;
10008 char *cwd = NULL, *id_str = NULL;
10009 struct got_pathlist_head paths;
10010 struct got_pathlist_entry *pe;
10011 char *uuidstr = NULL;
10012 int ch, show_files = 0;
10014 TAILQ_INIT(&paths);
10016 while ((ch = getopt(argc, argv, "")) != -1) {
10017 switch (ch) {
10018 default:
10019 usage_info();
10020 /* NOTREACHED */
10024 argc -= optind;
10025 argv += optind;
10027 #ifndef PROFILE
10028 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10029 NULL) == -1)
10030 err(1, "pledge");
10031 #endif
10032 cwd = getcwd(NULL, 0);
10033 if (cwd == NULL) {
10034 error = got_error_from_errno("getcwd");
10035 goto done;
10038 error = got_worktree_open(&worktree, cwd);
10039 if (error) {
10040 if (error->code == GOT_ERR_NOT_WORKTREE)
10041 error = wrap_not_worktree_error(error, "status", cwd);
10042 goto done;
10045 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10046 if (error)
10047 goto done;
10049 if (argc >= 1) {
10050 error = get_worktree_paths_from_argv(&paths, argc, argv,
10051 worktree);
10052 if (error)
10053 goto done;
10054 show_files = 1;
10057 error = got_object_id_str(&id_str,
10058 got_worktree_get_base_commit_id(worktree));
10059 if (error)
10060 goto done;
10062 error = got_worktree_get_uuid(&uuidstr, worktree);
10063 if (error)
10064 goto done;
10066 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10067 printf("work tree base commit: %s\n", id_str);
10068 printf("work tree path prefix: %s\n",
10069 got_worktree_get_path_prefix(worktree));
10070 printf("work tree branch reference: %s\n",
10071 got_worktree_get_head_ref_name(worktree));
10072 printf("work tree UUID: %s\n", uuidstr);
10073 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10075 if (show_files) {
10076 struct got_pathlist_entry *pe;
10077 TAILQ_FOREACH(pe, &paths, entry) {
10078 if (pe->path_len == 0)
10079 continue;
10081 * Assume this path will fail. This will be corrected
10082 * in print_path_info() in case the path does suceeed.
10084 pe->data = (void *)got_error_path(pe->path,
10085 GOT_ERR_BAD_PATH);
10087 error = got_worktree_path_info(worktree, &paths,
10088 print_path_info, &paths, check_cancelled, NULL);
10089 if (error)
10090 goto done;
10091 TAILQ_FOREACH(pe, &paths, entry) {
10092 if (pe->data != NULL) {
10093 error = pe->data; /* bad path */
10094 break;
10098 done:
10099 TAILQ_FOREACH(pe, &paths, entry)
10100 free((char *)pe->path);
10101 got_pathlist_free(&paths);
10102 free(cwd);
10103 free(id_str);
10104 free(uuidstr);
10105 return error;