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 (verbosity == -1)
1528 option_conflict('l', 'q');
1529 if (!TAILQ_EMPTY(&wanted_refs))
1530 option_conflict('l', 'R');
1533 uri = argv[0];
1535 if (argc == 1)
1536 dirname = NULL;
1537 else if (argc == 2)
1538 dirname = argv[1];
1539 else
1540 usage_clone();
1542 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1543 &repo_name, uri);
1544 if (error)
1545 goto done;
1547 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1548 host, port ? ":" : "", port ? port : "",
1549 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1550 error = got_error_from_errno("asprintf");
1551 goto done;
1554 if (strcmp(proto, "git") == 0) {
1555 #ifndef PROFILE
1556 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1557 "sendfd dns inet unveil", NULL) == -1)
1558 err(1, "pledge");
1559 #endif
1560 } else if (strcmp(proto, "git+ssh") == 0 ||
1561 strcmp(proto, "ssh") == 0) {
1562 #ifndef PROFILE
1563 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1564 "sendfd unveil", NULL) == -1)
1565 err(1, "pledge");
1566 #endif
1567 } else if (strcmp(proto, "http") == 0 ||
1568 strcmp(proto, "git+http") == 0) {
1569 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1570 goto done;
1571 } else {
1572 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1573 goto done;
1575 if (dirname == NULL) {
1576 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1577 error = got_error_from_errno("asprintf");
1578 goto done;
1580 repo_path = default_destdir;
1581 } else
1582 repo_path = dirname;
1584 if (!list_refs_only) {
1585 error = got_path_mkdir(repo_path);
1586 if (error &&
1587 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1588 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1589 goto done;
1590 if (!got_path_dir_is_empty(repo_path)) {
1591 error = got_error_path(repo_path,
1592 GOT_ERR_DIR_NOT_EMPTY);
1593 goto done;
1597 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1598 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1599 error = got_error_from_errno2("unveil",
1600 GOT_FETCH_PATH_SSH);
1601 goto done;
1604 error = apply_unveil(repo_path, 0, NULL);
1605 if (error)
1606 goto done;
1608 if (verbosity >= 0)
1609 printf("Connecting to %s%s%s\n", host,
1610 port ? ":" : "", port ? port : "");
1612 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1613 server_path, verbosity);
1614 if (error)
1615 goto done;
1617 if (!list_refs_only) {
1618 error = got_repo_init(repo_path);
1619 if (error)
1620 goto done;
1621 error = got_repo_open(&repo, repo_path, NULL);
1622 if (error)
1623 goto done;
1626 fpa.last_scaled_size[0] = '\0';
1627 fpa.last_p_indexed = -1;
1628 fpa.last_p_resolved = -1;
1629 fpa.verbosity = verbosity;
1630 fpa.create_configs = 1;
1631 fpa.configs_created = 0;
1632 fpa.repo = repo;
1633 fpa.config_info.symrefs = &symrefs;
1634 fpa.config_info.wanted_branches = &wanted_branches;
1635 fpa.config_info.wanted_refs = &wanted_refs;
1636 fpa.config_info.proto = proto;
1637 fpa.config_info.host = host;
1638 fpa.config_info.port = port;
1639 fpa.config_info.remote_repo_path = server_path;
1640 fpa.config_info.git_url = git_url;
1641 fpa.config_info.fetch_all_branches = fetch_all_branches;
1642 fpa.config_info.mirror_references = mirror_references;
1643 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1644 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1645 fetch_all_branches, &wanted_branches, &wanted_refs,
1646 list_refs_only, verbosity, fetchfd, repo,
1647 fetch_progress, &fpa);
1648 if (error)
1649 goto done;
1651 if (list_refs_only) {
1652 error = list_remote_refs(&symrefs, &refs);
1653 goto done;
1656 error = got_object_id_str(&id_str, pack_hash);
1657 if (error)
1658 goto done;
1659 if (verbosity >= 0)
1660 printf("\nFetched %s.pack\n", id_str);
1661 free(id_str);
1663 /* Set up references provided with the pack file. */
1664 TAILQ_FOREACH(pe, &refs, entry) {
1665 const char *refname = pe->path;
1666 struct got_object_id *id = pe->data;
1667 char *remote_refname;
1669 if (is_wanted_ref(&wanted_refs, refname) &&
1670 !mirror_references) {
1671 error = create_wanted_ref(refname, id,
1672 GOT_FETCH_DEFAULT_REMOTE_NAME,
1673 verbosity - 1, repo);
1674 if (error)
1675 goto done;
1676 continue;
1679 error = create_ref(refname, id, verbosity - 1, repo);
1680 if (error)
1681 goto done;
1683 if (mirror_references)
1684 continue;
1686 if (strncmp("refs/heads/", refname, 11) != 0)
1687 continue;
1689 if (asprintf(&remote_refname,
1690 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1691 refname + 11) == -1) {
1692 error = got_error_from_errno("asprintf");
1693 goto done;
1695 error = create_ref(remote_refname, id, verbosity - 1, repo);
1696 free(remote_refname);
1697 if (error)
1698 goto done;
1701 /* Set the HEAD reference if the server provided one. */
1702 TAILQ_FOREACH(pe, &symrefs, entry) {
1703 struct got_reference *target_ref;
1704 const char *refname = pe->path;
1705 const char *target = pe->data;
1706 char *remote_refname = NULL, *remote_target = NULL;
1708 if (strcmp(refname, GOT_REF_HEAD) != 0)
1709 continue;
1711 error = got_ref_open(&target_ref, repo, target, 0);
1712 if (error) {
1713 if (error->code == GOT_ERR_NOT_REF) {
1714 error = NULL;
1715 continue;
1717 goto done;
1720 error = create_symref(refname, target_ref, verbosity, repo);
1721 got_ref_close(target_ref);
1722 if (error)
1723 goto done;
1725 if (mirror_references)
1726 continue;
1728 if (strncmp("refs/heads/", target, 11) != 0)
1729 continue;
1731 if (asprintf(&remote_refname,
1732 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1733 refname) == -1) {
1734 error = got_error_from_errno("asprintf");
1735 goto done;
1737 if (asprintf(&remote_target,
1738 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 target + 11) == -1) {
1740 error = got_error_from_errno("asprintf");
1741 free(remote_refname);
1742 goto done;
1744 error = got_ref_open(&target_ref, repo, remote_target, 0);
1745 if (error) {
1746 free(remote_refname);
1747 free(remote_target);
1748 if (error->code == GOT_ERR_NOT_REF) {
1749 error = NULL;
1750 continue;
1752 goto done;
1754 error = create_symref(remote_refname, target_ref,
1755 verbosity - 1, repo);
1756 free(remote_refname);
1757 free(remote_target);
1758 got_ref_close(target_ref);
1759 if (error)
1760 goto done;
1762 if (pe == NULL) {
1764 * We failed to set the HEAD reference. If we asked for
1765 * a set of wanted branches use the first of one of those
1766 * which could be fetched instead.
1768 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1769 const char *target = pe->path;
1770 struct got_reference *target_ref;
1772 error = got_ref_open(&target_ref, repo, target, 0);
1773 if (error) {
1774 if (error->code == GOT_ERR_NOT_REF) {
1775 error = NULL;
1776 continue;
1778 goto done;
1781 error = create_symref(GOT_REF_HEAD, target_ref,
1782 verbosity, repo);
1783 got_ref_close(target_ref);
1784 if (error)
1785 goto done;
1786 break;
1790 if (verbosity >= 0)
1791 printf("Created %s repository '%s'\n",
1792 mirror_references ? "mirrored" : "cloned", repo_path);
1793 done:
1794 if (fetchpid > 0) {
1795 if (kill(fetchpid, SIGTERM) == -1)
1796 error = got_error_from_errno("kill");
1797 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1798 error = got_error_from_errno("waitpid");
1800 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1801 error = got_error_from_errno("close");
1802 if (repo)
1803 got_repo_close(repo);
1804 TAILQ_FOREACH(pe, &refs, entry) {
1805 free((void *)pe->path);
1806 free(pe->data);
1808 got_pathlist_free(&refs);
1809 TAILQ_FOREACH(pe, &symrefs, entry) {
1810 free((void *)pe->path);
1811 free(pe->data);
1813 got_pathlist_free(&symrefs);
1814 got_pathlist_free(&wanted_branches);
1815 got_pathlist_free(&wanted_refs);
1816 free(pack_hash);
1817 free(proto);
1818 free(host);
1819 free(port);
1820 free(server_path);
1821 free(repo_name);
1822 free(default_destdir);
1823 free(git_url);
1824 return error;
1827 static const struct got_error *
1828 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1829 int replace_tags, int verbosity, struct got_repository *repo)
1831 const struct got_error *err = NULL;
1832 char *new_id_str = NULL;
1833 struct got_object_id *old_id = NULL;
1835 err = got_object_id_str(&new_id_str, new_id);
1836 if (err)
1837 goto done;
1839 if (!replace_tags &&
1840 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1841 err = got_ref_resolve(&old_id, repo, ref);
1842 if (err)
1843 goto done;
1844 if (got_object_id_cmp(old_id, new_id) == 0)
1845 goto done;
1846 if (verbosity >= 0) {
1847 printf("Rejecting update of existing tag %s: %s\n",
1848 got_ref_get_name(ref), new_id_str);
1850 goto done;
1853 if (got_ref_is_symbolic(ref)) {
1854 if (verbosity >= 0) {
1855 printf("Replacing reference %s: %s\n",
1856 got_ref_get_name(ref),
1857 got_ref_get_symref_target(ref));
1859 err = got_ref_change_symref_to_ref(ref, new_id);
1860 if (err)
1861 goto done;
1862 err = got_ref_write(ref, repo);
1863 if (err)
1864 goto done;
1865 } else {
1866 err = got_ref_resolve(&old_id, repo, ref);
1867 if (err)
1868 goto done;
1869 if (got_object_id_cmp(old_id, new_id) == 0)
1870 goto done;
1872 err = got_ref_change_ref(ref, new_id);
1873 if (err)
1874 goto done;
1875 err = got_ref_write(ref, repo);
1876 if (err)
1877 goto done;
1880 if (verbosity >= 0)
1881 printf("Updated %s: %s\n", got_ref_get_name(ref),
1882 new_id_str);
1883 done:
1884 free(old_id);
1885 free(new_id_str);
1886 return err;
1889 static const struct got_error *
1890 update_symref(const char *refname, struct got_reference *target_ref,
1891 int verbosity, struct got_repository *repo)
1893 const struct got_error *err = NULL, *unlock_err;
1894 struct got_reference *symref;
1895 int symref_is_locked = 0;
1897 err = got_ref_open(&symref, repo, refname, 1);
1898 if (err) {
1899 if (err->code != GOT_ERR_NOT_REF)
1900 return err;
1901 err = got_ref_alloc_symref(&symref, refname, target_ref);
1902 if (err)
1903 goto done;
1905 err = got_ref_write(symref, repo);
1906 if (err)
1907 goto done;
1909 if (verbosity >= 0)
1910 printf("Created reference %s: %s\n",
1911 got_ref_get_name(symref),
1912 got_ref_get_symref_target(symref));
1913 } else {
1914 symref_is_locked = 1;
1916 if (strcmp(got_ref_get_symref_target(symref),
1917 got_ref_get_name(target_ref)) == 0)
1918 goto done;
1920 err = got_ref_change_symref(symref,
1921 got_ref_get_name(target_ref));
1922 if (err)
1923 goto done;
1925 err = got_ref_write(symref, repo);
1926 if (err)
1927 goto done;
1929 if (verbosity >= 0)
1930 printf("Updated %s: %s\n", got_ref_get_name(symref),
1931 got_ref_get_symref_target(symref));
1934 done:
1935 if (symref_is_locked) {
1936 unlock_err = got_ref_unlock(symref);
1937 if (unlock_err && err == NULL)
1938 err = unlock_err;
1940 got_ref_close(symref);
1941 return err;
1944 __dead static void
1945 usage_fetch(void)
1947 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1948 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1949 "[remote-repository-name]\n",
1950 getprogname());
1951 exit(1);
1954 static const struct got_error *
1955 delete_missing_ref(struct got_reference *ref,
1956 int verbosity, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_object_id *id = NULL;
1960 char *id_str = NULL;
1962 if (got_ref_is_symbolic(ref)) {
1963 err = got_ref_delete(ref, repo);
1964 if (err)
1965 return err;
1966 if (verbosity >= 0) {
1967 printf("Deleted reference %s: %s\n",
1968 got_ref_get_name(ref),
1969 got_ref_get_symref_target(ref));
1971 } else {
1972 err = got_ref_resolve(&id, repo, ref);
1973 if (err)
1974 return err;
1975 err = got_object_id_str(&id_str, id);
1976 if (err)
1977 goto done;
1979 err = got_ref_delete(ref, repo);
1980 if (err)
1981 goto done;
1982 if (verbosity >= 0) {
1983 printf("Deleted reference %s: %s\n",
1984 got_ref_get_name(ref), id_str);
1987 done:
1988 free(id);
1989 free(id_str);
1990 return NULL;
1993 static const struct got_error *
1994 delete_missing_refs(struct got_pathlist_head *their_refs,
1995 struct got_pathlist_head *their_symrefs,
1996 const struct got_remote_repo *remote,
1997 int verbosity, struct got_repository *repo)
1999 const struct got_error *err = NULL, *unlock_err;
2000 struct got_reflist_head my_refs;
2001 struct got_reflist_entry *re;
2002 struct got_pathlist_entry *pe;
2003 char *remote_namespace = NULL;
2004 char *local_refname = NULL;
2006 TAILQ_INIT(&my_refs);
2008 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2009 == -1)
2010 return got_error_from_errno("asprintf");
2012 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2013 if (err)
2014 goto done;
2016 TAILQ_FOREACH(re, &my_refs, entry) {
2017 const char *refname = got_ref_get_name(re->ref);
2019 if (!remote->mirror_references) {
2020 if (strncmp(refname, remote_namespace,
2021 strlen(remote_namespace)) == 0) {
2022 if (strcmp(refname + strlen(remote_namespace),
2023 GOT_REF_HEAD) == 0)
2024 continue;
2025 if (asprintf(&local_refname, "refs/heads/%s",
2026 refname + strlen(remote_namespace)) == -1) {
2027 err = got_error_from_errno("asprintf");
2028 goto done;
2030 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2031 continue;
2034 TAILQ_FOREACH(pe, their_refs, entry) {
2035 if (strcmp(local_refname, pe->path) == 0)
2036 break;
2038 if (pe != NULL)
2039 continue;
2041 TAILQ_FOREACH(pe, their_symrefs, entry) {
2042 if (strcmp(local_refname, pe->path) == 0)
2043 break;
2045 if (pe != NULL)
2046 continue;
2048 err = delete_missing_ref(re->ref, verbosity, repo);
2049 if (err)
2050 break;
2052 if (local_refname) {
2053 struct got_reference *ref;
2054 err = got_ref_open(&ref, repo, local_refname, 1);
2055 if (err) {
2056 if (err->code != GOT_ERR_NOT_REF)
2057 break;
2058 free(local_refname);
2059 local_refname = NULL;
2060 continue;
2062 err = delete_missing_ref(ref, verbosity, repo);
2063 if (err)
2064 break;
2065 unlock_err = got_ref_unlock(ref);
2066 got_ref_close(ref);
2067 if (unlock_err && err == NULL) {
2068 err = unlock_err;
2069 break;
2072 free(local_refname);
2073 local_refname = NULL;
2076 done:
2077 free(remote_namespace);
2078 free(local_refname);
2079 return err;
2082 static const struct got_error *
2083 update_wanted_ref(const char *refname, struct got_object_id *id,
2084 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2086 const struct got_error *err, *unlock_err;
2087 char *remote_refname;
2088 struct got_reference *ref;
2090 if (strncmp("refs/", refname, 5) == 0)
2091 refname += 5;
2093 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2094 remote_repo_name, refname) == -1)
2095 return got_error_from_errno("asprintf");
2097 err = got_ref_open(&ref, repo, remote_refname, 1);
2098 if (err) {
2099 if (err->code != GOT_ERR_NOT_REF)
2100 goto done;
2101 err = create_ref(remote_refname, id, verbosity, repo);
2102 } else {
2103 err = update_ref(ref, id, 0, verbosity, repo);
2104 unlock_err = got_ref_unlock(ref);
2105 if (unlock_err && err == NULL)
2106 err = unlock_err;
2107 got_ref_close(ref);
2109 done:
2110 free(remote_refname);
2111 return err;
2114 static const struct got_error *
2115 cmd_fetch(int argc, char *argv[])
2117 const struct got_error *error = NULL, *unlock_err;
2118 char *cwd = NULL, *repo_path = NULL;
2119 const char *remote_name;
2120 char *proto = NULL, *host = NULL, *port = NULL;
2121 char *repo_name = NULL, *server_path = NULL;
2122 const struct got_remote_repo *remotes, *remote = NULL;
2123 int nremotes;
2124 char *id_str = NULL;
2125 struct got_repository *repo = NULL;
2126 struct got_worktree *worktree = NULL;
2127 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2128 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2129 struct got_pathlist_entry *pe;
2130 struct got_object_id *pack_hash = NULL;
2131 int i, ch, fetchfd = -1, fetchstatus;
2132 pid_t fetchpid = -1;
2133 struct got_fetch_progress_arg fpa;
2134 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2135 int delete_refs = 0, replace_tags = 0;
2137 TAILQ_INIT(&refs);
2138 TAILQ_INIT(&symrefs);
2139 TAILQ_INIT(&wanted_branches);
2140 TAILQ_INIT(&wanted_refs);
2142 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2143 switch (ch) {
2144 case 'a':
2145 fetch_all_branches = 1;
2146 break;
2147 case 'b':
2148 error = got_pathlist_append(&wanted_branches,
2149 optarg, NULL);
2150 if (error)
2151 return error;
2152 break;
2153 case 'd':
2154 delete_refs = 1;
2155 break;
2156 case 'l':
2157 list_refs_only = 1;
2158 break;
2159 case 'r':
2160 repo_path = realpath(optarg, NULL);
2161 if (repo_path == NULL)
2162 return got_error_from_errno2("realpath",
2163 optarg);
2164 got_path_strip_trailing_slashes(repo_path);
2165 break;
2166 case 't':
2167 replace_tags = 1;
2168 break;
2169 case 'v':
2170 if (verbosity < 0)
2171 verbosity = 0;
2172 else if (verbosity < 3)
2173 verbosity++;
2174 break;
2175 case 'q':
2176 verbosity = -1;
2177 break;
2178 case 'R':
2179 error = got_pathlist_append(&wanted_refs,
2180 optarg, NULL);
2181 if (error)
2182 return error;
2183 break;
2184 default:
2185 usage_fetch();
2186 break;
2189 argc -= optind;
2190 argv += optind;
2192 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2193 option_conflict('a', 'b');
2194 if (list_refs_only) {
2195 if (!TAILQ_EMPTY(&wanted_branches))
2196 option_conflict('l', 'b');
2197 if (fetch_all_branches)
2198 option_conflict('l', 'a');
2199 if (delete_refs)
2200 option_conflict('l', 'd');
2203 if (argc == 0)
2204 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2205 else if (argc == 1)
2206 remote_name = argv[0];
2207 else
2208 usage_fetch();
2210 cwd = getcwd(NULL, 0);
2211 if (cwd == NULL) {
2212 error = got_error_from_errno("getcwd");
2213 goto done;
2216 if (repo_path == NULL) {
2217 error = got_worktree_open(&worktree, cwd);
2218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2219 goto done;
2220 else
2221 error = NULL;
2222 if (worktree) {
2223 repo_path =
2224 strdup(got_worktree_get_repo_path(worktree));
2225 if (repo_path == NULL)
2226 error = got_error_from_errno("strdup");
2227 if (error)
2228 goto done;
2229 } else {
2230 repo_path = strdup(cwd);
2231 if (repo_path == NULL) {
2232 error = got_error_from_errno("strdup");
2233 goto done;
2238 error = got_repo_open(&repo, repo_path, NULL);
2239 if (error)
2240 goto done;
2242 if (worktree) {
2243 worktree_conf = got_worktree_get_gotconfig(worktree);
2244 if (worktree_conf) {
2245 got_gotconfig_get_remotes(&nremotes, &remotes,
2246 worktree_conf);
2247 for (i = 0; i < nremotes; i++) {
2248 if (strcmp(remotes[i].name, remote_name) == 0) {
2249 remote = &remotes[i];
2250 break;
2255 if (remote == NULL) {
2256 repo_conf = got_repo_get_gotconfig(repo);
2257 if (repo_conf) {
2258 got_gotconfig_get_remotes(&nremotes, &remotes,
2259 repo_conf);
2260 for (i = 0; i < nremotes; i++) {
2261 if (strcmp(remotes[i].name, remote_name) == 0) {
2262 remote = &remotes[i];
2263 break;
2268 if (remote == NULL) {
2269 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2270 for (i = 0; i < nremotes; i++) {
2271 if (strcmp(remotes[i].name, remote_name) == 0) {
2272 remote = &remotes[i];
2273 break;
2277 if (remote == NULL) {
2278 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2279 goto done;
2282 if (TAILQ_EMPTY(&wanted_branches)) {
2283 if (!fetch_all_branches)
2284 fetch_all_branches = remote->fetch_all_branches;
2285 for (i = 0; i < remote->nbranches; i++) {
2286 got_pathlist_append(&wanted_branches,
2287 remote->branches[i], NULL);
2290 if (TAILQ_EMPTY(&wanted_refs)) {
2291 for (i = 0; i < remote->nrefs; i++) {
2292 got_pathlist_append(&wanted_refs,
2293 remote->refs[i], NULL);
2297 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2298 &repo_name, remote->url);
2299 if (error)
2300 goto done;
2302 if (strcmp(proto, "git") == 0) {
2303 #ifndef PROFILE
2304 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2305 "sendfd dns inet unveil", NULL) == -1)
2306 err(1, "pledge");
2307 #endif
2308 } else if (strcmp(proto, "git+ssh") == 0 ||
2309 strcmp(proto, "ssh") == 0) {
2310 #ifndef PROFILE
2311 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2312 "sendfd unveil", NULL) == -1)
2313 err(1, "pledge");
2314 #endif
2315 } else if (strcmp(proto, "http") == 0 ||
2316 strcmp(proto, "git+http") == 0) {
2317 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2318 goto done;
2319 } else {
2320 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2321 goto done;
2324 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2325 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2326 error = got_error_from_errno2("unveil",
2327 GOT_FETCH_PATH_SSH);
2328 goto done;
2331 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2332 if (error)
2333 goto done;
2335 if (verbosity >= 0)
2336 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2337 port ? ":" : "", port ? port : "");
2339 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2340 server_path, verbosity);
2341 if (error)
2342 goto done;
2344 fpa.last_scaled_size[0] = '\0';
2345 fpa.last_p_indexed = -1;
2346 fpa.last_p_resolved = -1;
2347 fpa.verbosity = verbosity;
2348 fpa.repo = repo;
2349 fpa.create_configs = 0;
2350 fpa.configs_created = 0;
2351 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2352 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2353 remote->mirror_references, fetch_all_branches, &wanted_branches,
2354 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2355 fetch_progress, &fpa);
2356 if (error)
2357 goto done;
2359 if (list_refs_only) {
2360 error = list_remote_refs(&symrefs, &refs);
2361 goto done;
2364 if (pack_hash == NULL) {
2365 if (verbosity >= 0)
2366 printf("Already up-to-date\n");
2367 } else if (verbosity >= 0) {
2368 error = got_object_id_str(&id_str, pack_hash);
2369 if (error)
2370 goto done;
2371 printf("\nFetched %s.pack\n", id_str);
2372 free(id_str);
2373 id_str = NULL;
2376 /* Update references provided with the pack file. */
2377 TAILQ_FOREACH(pe, &refs, entry) {
2378 const char *refname = pe->path;
2379 struct got_object_id *id = pe->data;
2380 struct got_reference *ref;
2381 char *remote_refname;
2383 if (is_wanted_ref(&wanted_refs, refname) &&
2384 !remote->mirror_references) {
2385 error = update_wanted_ref(refname, id,
2386 remote->name, verbosity, repo);
2387 if (error)
2388 goto done;
2389 continue;
2392 if (remote->mirror_references ||
2393 strncmp("refs/tags/", refname, 10) == 0) {
2394 error = got_ref_open(&ref, repo, refname, 1);
2395 if (error) {
2396 if (error->code != GOT_ERR_NOT_REF)
2397 goto done;
2398 error = create_ref(refname, id, verbosity,
2399 repo);
2400 if (error)
2401 goto done;
2402 } else {
2403 error = update_ref(ref, id, replace_tags,
2404 verbosity, repo);
2405 unlock_err = got_ref_unlock(ref);
2406 if (unlock_err && error == NULL)
2407 error = unlock_err;
2408 got_ref_close(ref);
2409 if (error)
2410 goto done;
2412 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2413 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2414 remote_name, refname + 11) == -1) {
2415 error = got_error_from_errno("asprintf");
2416 goto done;
2419 error = got_ref_open(&ref, repo, remote_refname, 1);
2420 if (error) {
2421 if (error->code != GOT_ERR_NOT_REF)
2422 goto done;
2423 error = create_ref(remote_refname, id,
2424 verbosity, repo);
2425 if (error)
2426 goto done;
2427 } else {
2428 error = update_ref(ref, id, replace_tags,
2429 verbosity, repo);
2430 unlock_err = got_ref_unlock(ref);
2431 if (unlock_err && error == NULL)
2432 error = unlock_err;
2433 got_ref_close(ref);
2434 if (error)
2435 goto done;
2438 /* Also create a local branch if none exists yet. */
2439 error = got_ref_open(&ref, repo, refname, 1);
2440 if (error) {
2441 if (error->code != GOT_ERR_NOT_REF)
2442 goto done;
2443 error = create_ref(refname, id, verbosity,
2444 repo);
2445 if (error)
2446 goto done;
2447 } else {
2448 unlock_err = got_ref_unlock(ref);
2449 if (unlock_err && error == NULL)
2450 error = unlock_err;
2451 got_ref_close(ref);
2455 if (delete_refs) {
2456 error = delete_missing_refs(&refs, &symrefs, remote,
2457 verbosity, repo);
2458 if (error)
2459 goto done;
2462 if (!remote->mirror_references) {
2463 /* Update remote HEAD reference if the server provided one. */
2464 TAILQ_FOREACH(pe, &symrefs, entry) {
2465 struct got_reference *target_ref;
2466 const char *refname = pe->path;
2467 const char *target = pe->data;
2468 char *remote_refname = NULL, *remote_target = NULL;
2470 if (strcmp(refname, GOT_REF_HEAD) != 0)
2471 continue;
2473 if (strncmp("refs/heads/", target, 11) != 0)
2474 continue;
2476 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2477 remote->name, refname) == -1) {
2478 error = got_error_from_errno("asprintf");
2479 goto done;
2481 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2482 remote->name, target + 11) == -1) {
2483 error = got_error_from_errno("asprintf");
2484 free(remote_refname);
2485 goto done;
2488 error = got_ref_open(&target_ref, repo, remote_target,
2489 0);
2490 if (error) {
2491 free(remote_refname);
2492 free(remote_target);
2493 if (error->code == GOT_ERR_NOT_REF) {
2494 error = NULL;
2495 continue;
2497 goto done;
2499 error = update_symref(remote_refname, target_ref,
2500 verbosity, repo);
2501 free(remote_refname);
2502 free(remote_target);
2503 got_ref_close(target_ref);
2504 if (error)
2505 goto done;
2508 done:
2509 if (fetchpid > 0) {
2510 if (kill(fetchpid, SIGTERM) == -1)
2511 error = got_error_from_errno("kill");
2512 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2513 error = got_error_from_errno("waitpid");
2515 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2516 error = got_error_from_errno("close");
2517 if (repo)
2518 got_repo_close(repo);
2519 if (worktree)
2520 got_worktree_close(worktree);
2521 TAILQ_FOREACH(pe, &refs, entry) {
2522 free((void *)pe->path);
2523 free(pe->data);
2525 got_pathlist_free(&refs);
2526 TAILQ_FOREACH(pe, &symrefs, entry) {
2527 free((void *)pe->path);
2528 free(pe->data);
2530 got_pathlist_free(&symrefs);
2531 got_pathlist_free(&wanted_branches);
2532 got_pathlist_free(&wanted_refs);
2533 free(id_str);
2534 free(cwd);
2535 free(repo_path);
2536 free(pack_hash);
2537 free(proto);
2538 free(host);
2539 free(port);
2540 free(server_path);
2541 free(repo_name);
2542 return error;
2546 __dead static void
2547 usage_checkout(void)
2549 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2550 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2551 exit(1);
2554 static void
2555 show_worktree_base_ref_warning(void)
2557 fprintf(stderr, "%s: warning: could not create a reference "
2558 "to the work tree's base commit; the commit could be "
2559 "garbage-collected by Git; making the repository "
2560 "writable and running 'got update' will prevent this\n",
2561 getprogname());
2564 struct got_checkout_progress_arg {
2565 const char *worktree_path;
2566 int had_base_commit_ref_error;
2569 static const struct got_error *
2570 checkout_progress(void *arg, unsigned char status, const char *path)
2572 struct got_checkout_progress_arg *a = arg;
2574 /* Base commit bump happens silently. */
2575 if (status == GOT_STATUS_BUMP_BASE)
2576 return NULL;
2578 if (status == GOT_STATUS_BASE_REF_ERR) {
2579 a->had_base_commit_ref_error = 1;
2580 return NULL;
2583 while (path[0] == '/')
2584 path++;
2586 printf("%c %s/%s\n", status, a->worktree_path, path);
2587 return NULL;
2590 static const struct got_error *
2591 check_cancelled(void *arg)
2593 if (sigint_received || sigpipe_received)
2594 return got_error(GOT_ERR_CANCELLED);
2595 return NULL;
2598 static const struct got_error *
2599 check_linear_ancestry(struct got_object_id *commit_id,
2600 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2601 struct got_repository *repo)
2603 const struct got_error *err = NULL;
2604 struct got_object_id *yca_id;
2606 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2607 commit_id, base_commit_id, repo, check_cancelled, NULL);
2608 if (err)
2609 return err;
2611 if (yca_id == NULL)
2612 return got_error(GOT_ERR_ANCESTRY);
2615 * Require a straight line of history between the target commit
2616 * and the work tree's base commit.
2618 * Non-linear situations such as this require a rebase:
2620 * (commit) D F (base_commit)
2621 * \ /
2622 * C E
2623 * \ /
2624 * B (yca)
2625 * |
2626 * A
2628 * 'got update' only handles linear cases:
2629 * Update forwards in time: A (base/yca) - B - C - D (commit)
2630 * Update backwards in time: D (base) - C - B - A (commit/yca)
2632 if (allow_forwards_in_time_only) {
2633 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2634 return got_error(GOT_ERR_ANCESTRY);
2635 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2636 got_object_id_cmp(base_commit_id, yca_id) != 0)
2637 return got_error(GOT_ERR_ANCESTRY);
2639 free(yca_id);
2640 return NULL;
2643 static const struct got_error *
2644 check_same_branch(struct got_object_id *commit_id,
2645 struct got_reference *head_ref, struct got_object_id *yca_id,
2646 struct got_repository *repo)
2648 const struct got_error *err = NULL;
2649 struct got_commit_graph *graph = NULL;
2650 struct got_object_id *head_commit_id = NULL;
2651 int is_same_branch = 0;
2653 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2654 if (err)
2655 goto done;
2657 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2658 is_same_branch = 1;
2659 goto done;
2661 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2662 is_same_branch = 1;
2663 goto done;
2666 err = got_commit_graph_open(&graph, "/", 1);
2667 if (err)
2668 goto done;
2670 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2671 check_cancelled, NULL);
2672 if (err)
2673 goto done;
2675 for (;;) {
2676 struct got_object_id *id;
2677 err = got_commit_graph_iter_next(&id, graph, repo,
2678 check_cancelled, NULL);
2679 if (err) {
2680 if (err->code == GOT_ERR_ITER_COMPLETED)
2681 err = NULL;
2682 break;
2685 if (id) {
2686 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2687 break;
2688 if (got_object_id_cmp(id, commit_id) == 0) {
2689 is_same_branch = 1;
2690 break;
2694 done:
2695 if (graph)
2696 got_commit_graph_close(graph);
2697 free(head_commit_id);
2698 if (!err && !is_same_branch)
2699 err = got_error(GOT_ERR_ANCESTRY);
2700 return err;
2703 static const struct got_error *
2704 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2706 static char msg[512];
2707 const char *branch_name;
2709 if (got_ref_is_symbolic(ref))
2710 branch_name = got_ref_get_symref_target(ref);
2711 else
2712 branch_name = got_ref_get_name(ref);
2714 if (strncmp("refs/heads/", branch_name, 11) == 0)
2715 branch_name += 11;
2717 snprintf(msg, sizeof(msg),
2718 "target commit is not contained in branch '%s'; "
2719 "the branch to use must be specified with -b; "
2720 "if necessary a new branch can be created for "
2721 "this commit with 'got branch -c %s BRANCH_NAME'",
2722 branch_name, commit_id_str);
2724 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2727 static const struct got_error *
2728 cmd_checkout(int argc, char *argv[])
2730 const struct got_error *error = NULL;
2731 struct got_repository *repo = NULL;
2732 struct got_reference *head_ref = NULL;
2733 struct got_worktree *worktree = NULL;
2734 char *repo_path = NULL;
2735 char *worktree_path = NULL;
2736 const char *path_prefix = "";
2737 const char *branch_name = GOT_REF_HEAD;
2738 char *commit_id_str = NULL;
2739 char *cwd = NULL;
2740 int ch, same_path_prefix, allow_nonempty = 0;
2741 struct got_pathlist_head paths;
2742 struct got_checkout_progress_arg cpa;
2744 TAILQ_INIT(&paths);
2746 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2747 switch (ch) {
2748 case 'b':
2749 branch_name = optarg;
2750 break;
2751 case 'c':
2752 commit_id_str = strdup(optarg);
2753 if (commit_id_str == NULL)
2754 return got_error_from_errno("strdup");
2755 break;
2756 case 'E':
2757 allow_nonempty = 1;
2758 break;
2759 case 'p':
2760 path_prefix = optarg;
2761 break;
2762 default:
2763 usage_checkout();
2764 /* NOTREACHED */
2768 argc -= optind;
2769 argv += optind;
2771 #ifndef PROFILE
2772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2773 "unveil", NULL) == -1)
2774 err(1, "pledge");
2775 #endif
2776 if (argc == 1) {
2777 char *base, *dotgit;
2778 const char *path;
2779 repo_path = realpath(argv[0], NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath", argv[0]);
2782 cwd = getcwd(NULL, 0);
2783 if (cwd == NULL) {
2784 error = got_error_from_errno("getcwd");
2785 goto done;
2787 if (path_prefix[0])
2788 path = path_prefix;
2789 else
2790 path = repo_path;
2791 error = got_path_basename(&base, path);
2792 if (error)
2793 goto done;
2794 dotgit = strstr(base, ".git");
2795 if (dotgit)
2796 *dotgit = '\0';
2797 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2798 error = got_error_from_errno("asprintf");
2799 free(base);
2800 goto done;
2802 free(base);
2803 } else if (argc == 2) {
2804 repo_path = realpath(argv[0], NULL);
2805 if (repo_path == NULL) {
2806 error = got_error_from_errno2("realpath", argv[0]);
2807 goto done;
2809 worktree_path = realpath(argv[1], NULL);
2810 if (worktree_path == NULL) {
2811 if (errno != ENOENT) {
2812 error = got_error_from_errno2("realpath",
2813 argv[1]);
2814 goto done;
2816 worktree_path = strdup(argv[1]);
2817 if (worktree_path == NULL) {
2818 error = got_error_from_errno("strdup");
2819 goto done;
2822 } else
2823 usage_checkout();
2825 got_path_strip_trailing_slashes(repo_path);
2826 got_path_strip_trailing_slashes(worktree_path);
2828 error = got_repo_open(&repo, repo_path, NULL);
2829 if (error != NULL)
2830 goto done;
2832 /* Pre-create work tree path for unveil(2) */
2833 error = got_path_mkdir(worktree_path);
2834 if (error) {
2835 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2836 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2837 goto done;
2838 if (!allow_nonempty &&
2839 !got_path_dir_is_empty(worktree_path)) {
2840 error = got_error_path(worktree_path,
2841 GOT_ERR_DIR_NOT_EMPTY);
2842 goto done;
2846 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2847 if (error)
2848 goto done;
2850 error = got_ref_open(&head_ref, repo, branch_name, 0);
2851 if (error != NULL)
2852 goto done;
2854 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2855 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2856 goto done;
2858 error = got_worktree_open(&worktree, worktree_path);
2859 if (error != NULL)
2860 goto done;
2862 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2863 path_prefix);
2864 if (error != NULL)
2865 goto done;
2866 if (!same_path_prefix) {
2867 error = got_error(GOT_ERR_PATH_PREFIX);
2868 goto done;
2871 if (commit_id_str) {
2872 struct got_object_id *commit_id;
2873 struct got_reflist_head refs;
2874 TAILQ_INIT(&refs);
2875 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2876 NULL);
2877 if (error)
2878 goto done;
2879 error = got_repo_match_object_id(&commit_id, NULL,
2880 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2881 got_ref_list_free(&refs);
2882 if (error)
2883 goto done;
2884 error = check_linear_ancestry(commit_id,
2885 got_worktree_get_base_commit_id(worktree), 0, repo);
2886 if (error != NULL) {
2887 free(commit_id);
2888 if (error->code == GOT_ERR_ANCESTRY) {
2889 error = checkout_ancestry_error(
2890 head_ref, commit_id_str);
2892 goto done;
2894 error = check_same_branch(commit_id, head_ref, NULL, repo);
2895 if (error) {
2896 if (error->code == GOT_ERR_ANCESTRY) {
2897 error = checkout_ancestry_error(
2898 head_ref, commit_id_str);
2900 goto done;
2902 error = got_worktree_set_base_commit_id(worktree, repo,
2903 commit_id);
2904 free(commit_id);
2905 if (error)
2906 goto done;
2909 error = got_pathlist_append(&paths, "", NULL);
2910 if (error)
2911 goto done;
2912 cpa.worktree_path = worktree_path;
2913 cpa.had_base_commit_ref_error = 0;
2914 error = got_worktree_checkout_files(worktree, &paths, repo,
2915 checkout_progress, &cpa, check_cancelled, NULL);
2916 if (error != NULL)
2917 goto done;
2919 printf("Now shut up and hack\n");
2920 if (cpa.had_base_commit_ref_error)
2921 show_worktree_base_ref_warning();
2922 done:
2923 got_pathlist_free(&paths);
2924 free(commit_id_str);
2925 free(repo_path);
2926 free(worktree_path);
2927 free(cwd);
2928 return error;
2931 struct got_update_progress_arg {
2932 int did_something;
2933 int conflicts;
2934 int obstructed;
2935 int not_updated;
2938 void
2939 print_update_progress_stats(struct got_update_progress_arg *upa)
2941 if (!upa->did_something)
2942 return;
2944 if (upa->conflicts > 0)
2945 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2946 if (upa->obstructed > 0)
2947 printf("File paths obstructed by a non-regular file: %d\n",
2948 upa->obstructed);
2949 if (upa->not_updated > 0)
2950 printf("Files not updated because of existing merge "
2951 "conflicts: %d\n", upa->not_updated);
2954 __dead static void
2955 usage_update(void)
2957 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2958 getprogname());
2959 exit(1);
2962 static const struct got_error *
2963 update_progress(void *arg, unsigned char status, const char *path)
2965 struct got_update_progress_arg *upa = arg;
2967 if (status == GOT_STATUS_EXISTS ||
2968 status == GOT_STATUS_BASE_REF_ERR)
2969 return NULL;
2971 upa->did_something = 1;
2973 /* Base commit bump happens silently. */
2974 if (status == GOT_STATUS_BUMP_BASE)
2975 return NULL;
2977 if (status == GOT_STATUS_CONFLICT)
2978 upa->conflicts++;
2979 if (status == GOT_STATUS_OBSTRUCTED)
2980 upa->obstructed++;
2981 if (status == GOT_STATUS_CANNOT_UPDATE)
2982 upa->not_updated++;
2984 while (path[0] == '/')
2985 path++;
2986 printf("%c %s\n", status, path);
2987 return NULL;
2990 static const struct got_error *
2991 switch_head_ref(struct got_reference *head_ref,
2992 struct got_object_id *commit_id, struct got_worktree *worktree,
2993 struct got_repository *repo)
2995 const struct got_error *err = NULL;
2996 char *base_id_str;
2997 int ref_has_moved = 0;
2999 /* Trivial case: switching between two different references. */
3000 if (strcmp(got_ref_get_name(head_ref),
3001 got_worktree_get_head_ref_name(worktree)) != 0) {
3002 printf("Switching work tree from %s to %s\n",
3003 got_worktree_get_head_ref_name(worktree),
3004 got_ref_get_name(head_ref));
3005 return got_worktree_set_head_ref(worktree, head_ref);
3008 err = check_linear_ancestry(commit_id,
3009 got_worktree_get_base_commit_id(worktree), 0, repo);
3010 if (err) {
3011 if (err->code != GOT_ERR_ANCESTRY)
3012 return err;
3013 ref_has_moved = 1;
3015 if (!ref_has_moved)
3016 return NULL;
3018 /* Switching to a rebased branch with the same reference name. */
3019 err = got_object_id_str(&base_id_str,
3020 got_worktree_get_base_commit_id(worktree));
3021 if (err)
3022 return err;
3023 printf("Reference %s now points at a different branch\n",
3024 got_worktree_get_head_ref_name(worktree));
3025 printf("Switching work tree from %s to %s\n", base_id_str,
3026 got_worktree_get_head_ref_name(worktree));
3027 return NULL;
3030 static const struct got_error *
3031 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3033 const struct got_error *err;
3034 int in_progress;
3036 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3037 if (err)
3038 return err;
3039 if (in_progress)
3040 return got_error(GOT_ERR_REBASING);
3042 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3043 if (err)
3044 return err;
3045 if (in_progress)
3046 return got_error(GOT_ERR_HISTEDIT_BUSY);
3048 return NULL;
3051 static const struct got_error *
3052 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3053 char *argv[], struct got_worktree *worktree)
3055 const struct got_error *err = NULL;
3056 char *path;
3057 int i;
3059 if (argc == 0) {
3060 path = strdup("");
3061 if (path == NULL)
3062 return got_error_from_errno("strdup");
3063 return got_pathlist_append(paths, path, NULL);
3066 for (i = 0; i < argc; i++) {
3067 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3068 if (err)
3069 break;
3070 err = got_pathlist_append(paths, path, NULL);
3071 if (err) {
3072 free(path);
3073 break;
3077 return err;
3080 static const struct got_error *
3081 wrap_not_worktree_error(const struct got_error *orig_err,
3082 const char *cmdname, const char *path)
3084 const struct got_error *err;
3085 struct got_repository *repo;
3086 static char msg[512];
3088 err = got_repo_open(&repo, path, NULL);
3089 if (err)
3090 return orig_err;
3092 snprintf(msg, sizeof(msg),
3093 "'got %s' needs a work tree in addition to a git repository\n"
3094 "Work trees can be checked out from this Git repository with "
3095 "'got checkout'.\n"
3096 "The got(1) manual page contains more information.", cmdname);
3097 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3098 got_repo_close(repo);
3099 return err;
3102 static const struct got_error *
3103 cmd_update(int argc, char *argv[])
3105 const struct got_error *error = NULL;
3106 struct got_repository *repo = NULL;
3107 struct got_worktree *worktree = NULL;
3108 char *worktree_path = NULL;
3109 struct got_object_id *commit_id = NULL;
3110 char *commit_id_str = NULL;
3111 const char *branch_name = NULL;
3112 struct got_reference *head_ref = NULL;
3113 struct got_pathlist_head paths;
3114 struct got_pathlist_entry *pe;
3115 int ch;
3116 struct got_update_progress_arg upa;
3118 TAILQ_INIT(&paths);
3120 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3121 switch (ch) {
3122 case 'b':
3123 branch_name = optarg;
3124 break;
3125 case 'c':
3126 commit_id_str = strdup(optarg);
3127 if (commit_id_str == NULL)
3128 return got_error_from_errno("strdup");
3129 break;
3130 default:
3131 usage_update();
3132 /* NOTREACHED */
3136 argc -= optind;
3137 argv += optind;
3139 #ifndef PROFILE
3140 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3141 "unveil", NULL) == -1)
3142 err(1, "pledge");
3143 #endif
3144 worktree_path = getcwd(NULL, 0);
3145 if (worktree_path == NULL) {
3146 error = got_error_from_errno("getcwd");
3147 goto done;
3149 error = got_worktree_open(&worktree, worktree_path);
3150 if (error) {
3151 if (error->code == GOT_ERR_NOT_WORKTREE)
3152 error = wrap_not_worktree_error(error, "update",
3153 worktree_path);
3154 goto done;
3157 error = check_rebase_or_histedit_in_progress(worktree);
3158 if (error)
3159 goto done;
3161 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3162 NULL);
3163 if (error != NULL)
3164 goto done;
3166 error = apply_unveil(got_repo_get_path(repo), 0,
3167 got_worktree_get_root_path(worktree));
3168 if (error)
3169 goto done;
3171 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3172 if (error)
3173 goto done;
3175 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3176 got_worktree_get_head_ref_name(worktree), 0);
3177 if (error != NULL)
3178 goto done;
3179 if (commit_id_str == NULL) {
3180 error = got_ref_resolve(&commit_id, repo, head_ref);
3181 if (error != NULL)
3182 goto done;
3183 error = got_object_id_str(&commit_id_str, commit_id);
3184 if (error != NULL)
3185 goto done;
3186 } else {
3187 struct got_reflist_head refs;
3188 TAILQ_INIT(&refs);
3189 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3190 NULL);
3191 if (error)
3192 goto done;
3193 error = got_repo_match_object_id(&commit_id, NULL,
3194 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3195 got_ref_list_free(&refs);
3196 free(commit_id_str);
3197 commit_id_str = NULL;
3198 if (error)
3199 goto done;
3200 error = got_object_id_str(&commit_id_str, commit_id);
3201 if (error)
3202 goto done;
3205 if (branch_name) {
3206 struct got_object_id *head_commit_id;
3207 TAILQ_FOREACH(pe, &paths, entry) {
3208 if (pe->path_len == 0)
3209 continue;
3210 error = got_error_msg(GOT_ERR_BAD_PATH,
3211 "switching between branches requires that "
3212 "the entire work tree gets updated");
3213 goto done;
3215 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3216 if (error)
3217 goto done;
3218 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3219 repo);
3220 free(head_commit_id);
3221 if (error != NULL)
3222 goto done;
3223 error = check_same_branch(commit_id, head_ref, NULL, repo);
3224 if (error)
3225 goto done;
3226 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3227 if (error)
3228 goto done;
3229 } else {
3230 error = check_linear_ancestry(commit_id,
3231 got_worktree_get_base_commit_id(worktree), 0, repo);
3232 if (error != NULL) {
3233 if (error->code == GOT_ERR_ANCESTRY)
3234 error = got_error(GOT_ERR_BRANCH_MOVED);
3235 goto done;
3237 error = check_same_branch(commit_id, head_ref, NULL, repo);
3238 if (error)
3239 goto done;
3242 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3243 commit_id) != 0) {
3244 error = got_worktree_set_base_commit_id(worktree, repo,
3245 commit_id);
3246 if (error)
3247 goto done;
3250 memset(&upa, 0, sizeof(upa));
3251 error = got_worktree_checkout_files(worktree, &paths, repo,
3252 update_progress, &upa, check_cancelled, NULL);
3253 if (error != NULL)
3254 goto done;
3256 if (upa.did_something)
3257 printf("Updated to commit %s\n", commit_id_str);
3258 else
3259 printf("Already up-to-date\n");
3260 print_update_progress_stats(&upa);
3261 done:
3262 free(worktree_path);
3263 TAILQ_FOREACH(pe, &paths, entry)
3264 free((char *)pe->path);
3265 got_pathlist_free(&paths);
3266 free(commit_id);
3267 free(commit_id_str);
3268 return error;
3271 static const struct got_error *
3272 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3273 const char *path, int diff_context, int ignore_whitespace,
3274 int force_text_diff, struct got_repository *repo)
3276 const struct got_error *err = NULL;
3277 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3279 if (blob_id1) {
3280 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3281 if (err)
3282 goto done;
3285 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3286 if (err)
3287 goto done;
3289 while (path[0] == '/')
3290 path++;
3291 err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3292 diff_context, ignore_whitespace, force_text_diff, stdout);
3293 done:
3294 if (blob1)
3295 got_object_blob_close(blob1);
3296 got_object_blob_close(blob2);
3297 return err;
3300 static const struct got_error *
3301 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3302 const char *path, int diff_context, int ignore_whitespace,
3303 int force_text_diff, struct got_repository *repo)
3305 const struct got_error *err = NULL;
3306 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3307 struct got_diff_blob_output_unidiff_arg arg;
3309 if (tree_id1) {
3310 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3311 if (err)
3312 goto done;
3315 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3316 if (err)
3317 goto done;
3319 arg.diff_context = diff_context;
3320 arg.ignore_whitespace = ignore_whitespace;
3321 arg.force_text_diff = force_text_diff;
3322 arg.outfile = stdout;
3323 arg.line_offsets = NULL;
3324 arg.nlines = 0;
3325 while (path[0] == '/')
3326 path++;
3327 err = got_diff_tree(tree1, tree2, path, path, repo,
3328 got_diff_blob_output_unidiff, &arg, 1);
3329 done:
3330 if (tree1)
3331 got_object_tree_close(tree1);
3332 if (tree2)
3333 got_object_tree_close(tree2);
3334 return err;
3337 static const struct got_error *
3338 get_changed_paths(struct got_pathlist_head *paths,
3339 struct got_commit_object *commit, struct got_repository *repo)
3341 const struct got_error *err = NULL;
3342 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3343 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3344 struct got_object_qid *qid;
3346 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3347 if (qid != NULL) {
3348 struct got_commit_object *pcommit;
3349 err = got_object_open_as_commit(&pcommit, repo,
3350 qid->id);
3351 if (err)
3352 return err;
3354 tree_id1 = got_object_commit_get_tree_id(pcommit);
3355 got_object_commit_close(pcommit);
3359 if (tree_id1) {
3360 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3361 if (err)
3362 goto done;
3365 tree_id2 = got_object_commit_get_tree_id(commit);
3366 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3367 if (err)
3368 goto done;
3370 err = got_diff_tree(tree1, tree2, "", "", repo,
3371 got_diff_tree_collect_changed_paths, paths, 0);
3372 done:
3373 if (tree1)
3374 got_object_tree_close(tree1);
3375 if (tree2)
3376 got_object_tree_close(tree2);
3377 return err;
3380 static const struct got_error *
3381 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3382 const char *path, int diff_context, struct got_repository *repo)
3384 const struct got_error *err = NULL;
3385 struct got_commit_object *pcommit = NULL;
3386 char *id_str1 = NULL, *id_str2 = NULL;
3387 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3388 struct got_object_qid *qid;
3390 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3391 if (qid != NULL) {
3392 err = got_object_open_as_commit(&pcommit, repo,
3393 qid->id);
3394 if (err)
3395 return err;
3398 if (path && path[0] != '\0') {
3399 int obj_type;
3400 err = got_object_id_by_path(&obj_id2, repo, id, path);
3401 if (err)
3402 goto done;
3403 err = got_object_id_str(&id_str2, obj_id2);
3404 if (err) {
3405 free(obj_id2);
3406 goto done;
3408 if (pcommit) {
3409 err = got_object_id_by_path(&obj_id1, repo,
3410 qid->id, path);
3411 if (err) {
3412 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3413 free(obj_id2);
3414 goto done;
3416 } else {
3417 err = got_object_id_str(&id_str1, obj_id1);
3418 if (err) {
3419 free(obj_id2);
3420 goto done;
3424 err = got_object_get_type(&obj_type, repo, obj_id2);
3425 if (err) {
3426 free(obj_id2);
3427 goto done;
3429 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3430 switch (obj_type) {
3431 case GOT_OBJ_TYPE_BLOB:
3432 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3433 0, 0, repo);
3434 break;
3435 case GOT_OBJ_TYPE_TREE:
3436 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3437 0, 0, repo);
3438 break;
3439 default:
3440 err = got_error(GOT_ERR_OBJ_TYPE);
3441 break;
3443 free(obj_id1);
3444 free(obj_id2);
3445 } else {
3446 obj_id2 = got_object_commit_get_tree_id(commit);
3447 err = got_object_id_str(&id_str2, obj_id2);
3448 if (err)
3449 goto done;
3450 if (pcommit) {
3451 obj_id1 = got_object_commit_get_tree_id(pcommit);
3452 err = got_object_id_str(&id_str1, obj_id1);
3453 if (err)
3454 goto done;
3456 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3457 id_str2);
3458 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3459 repo);
3461 done:
3462 free(id_str1);
3463 free(id_str2);
3464 if (pcommit)
3465 got_object_commit_close(pcommit);
3466 return err;
3469 static char *
3470 get_datestr(time_t *time, char *datebuf)
3472 struct tm mytm, *tm;
3473 char *p, *s;
3475 tm = gmtime_r(time, &mytm);
3476 if (tm == NULL)
3477 return NULL;
3478 s = asctime_r(tm, datebuf);
3479 if (s == NULL)
3480 return NULL;
3481 p = strchr(s, '\n');
3482 if (p)
3483 *p = '\0';
3484 return s;
3487 static const struct got_error *
3488 match_logmsg(int *have_match, struct got_object_id *id,
3489 struct got_commit_object *commit, regex_t *regex)
3491 const struct got_error *err = NULL;
3492 regmatch_t regmatch;
3493 char *id_str = NULL, *logmsg = NULL;
3495 *have_match = 0;
3497 err = got_object_id_str(&id_str, id);
3498 if (err)
3499 return err;
3501 err = got_object_commit_get_logmsg(&logmsg, commit);
3502 if (err)
3503 goto done;
3505 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3506 *have_match = 1;
3507 done:
3508 free(id_str);
3509 free(logmsg);
3510 return err;
3513 static void
3514 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3515 regex_t *regex)
3517 regmatch_t regmatch;
3518 struct got_pathlist_entry *pe;
3520 *have_match = 0;
3522 TAILQ_FOREACH(pe, changed_paths, entry) {
3523 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3524 *have_match = 1;
3525 break;
3530 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3532 static const struct got_error*
3533 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3534 struct got_object_id *id, struct got_repository *repo)
3536 static const struct got_error *err = NULL;
3537 struct got_reflist_entry *re;
3538 char *s;
3539 const char *name;
3541 *refs_str = NULL;
3543 TAILQ_FOREACH(re, refs, entry) {
3544 struct got_tag_object *tag = NULL;
3545 struct got_object_id *ref_id;
3546 int cmp;
3548 name = got_ref_get_name(re->ref);
3549 if (strcmp(name, GOT_REF_HEAD) == 0)
3550 continue;
3551 if (strncmp(name, "refs/", 5) == 0)
3552 name += 5;
3553 if (strncmp(name, "got/", 4) == 0)
3554 continue;
3555 if (strncmp(name, "heads/", 6) == 0)
3556 name += 6;
3557 if (strncmp(name, "remotes/", 8) == 0) {
3558 name += 8;
3559 s = strstr(name, "/" GOT_REF_HEAD);
3560 if (s != NULL && s[strlen(s)] == '\0')
3561 continue;
3563 err = got_ref_resolve(&ref_id, repo, re->ref);
3564 if (err)
3565 break;
3566 if (strncmp(name, "tags/", 5) == 0) {
3567 err = got_object_open_as_tag(&tag, repo, ref_id);
3568 if (err) {
3569 if (err->code != GOT_ERR_OBJ_TYPE) {
3570 free(ref_id);
3571 break;
3573 /* Ref points at something other than a tag. */
3574 err = NULL;
3575 tag = NULL;
3578 cmp = got_object_id_cmp(tag ?
3579 got_object_tag_get_object_id(tag) : ref_id, id);
3580 free(ref_id);
3581 if (tag)
3582 got_object_tag_close(tag);
3583 if (cmp != 0)
3584 continue;
3585 s = *refs_str;
3586 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3587 s ? ", " : "", name) == -1) {
3588 err = got_error_from_errno("asprintf");
3589 free(s);
3590 *refs_str = NULL;
3591 break;
3593 free(s);
3596 return err;
3599 static const struct got_error *
3600 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3601 struct got_repository *repo, const char *path,
3602 struct got_pathlist_head *changed_paths, int show_patch,
3603 int diff_context, struct got_reflist_object_id_map *refs_idmap)
3605 const struct got_error *err = NULL;
3606 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3607 char datebuf[26];
3608 time_t committer_time;
3609 const char *author, *committer;
3610 char *refs_str = NULL;
3611 struct got_reflist_head *refs;
3613 err = got_object_id_str(&id_str, id);
3614 if (err)
3615 return err;
3617 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3618 if (refs) {
3619 err = build_refs_str(&refs_str, refs, id, repo);
3620 if (err)
3621 goto done;
3624 printf(GOT_COMMIT_SEP_STR);
3625 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3626 refs_str ? refs_str : "", refs_str ? ")" : "");
3627 free(id_str);
3628 id_str = NULL;
3629 free(refs_str);
3630 refs_str = NULL;
3631 printf("from: %s\n", got_object_commit_get_author(commit));
3632 committer_time = got_object_commit_get_committer_time(commit);
3633 datestr = get_datestr(&committer_time, datebuf);
3634 if (datestr)
3635 printf("date: %s UTC\n", datestr);
3636 author = got_object_commit_get_author(commit);
3637 committer = got_object_commit_get_committer(commit);
3638 if (strcmp(author, committer) != 0)
3639 printf("via: %s\n", committer);
3640 if (got_object_commit_get_nparents(commit) > 1) {
3641 const struct got_object_id_queue *parent_ids;
3642 struct got_object_qid *qid;
3643 int n = 1;
3644 parent_ids = got_object_commit_get_parent_ids(commit);
3645 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3646 err = got_object_id_str(&id_str, qid->id);
3647 if (err)
3648 goto done;
3649 printf("parent %d: %s\n", n++, id_str);
3650 free(id_str);
3651 id_str = NULL;
3655 err = got_object_commit_get_logmsg(&logmsg0, commit);
3656 if (err)
3657 goto done;
3659 logmsg = logmsg0;
3660 do {
3661 line = strsep(&logmsg, "\n");
3662 if (line)
3663 printf(" %s\n", line);
3664 } while (line);
3665 free(logmsg0);
3667 if (changed_paths) {
3668 struct got_pathlist_entry *pe;
3669 TAILQ_FOREACH(pe, changed_paths, entry) {
3670 struct got_diff_changed_path *cp = pe->data;
3671 printf(" %c %s\n", cp->status, pe->path);
3673 printf("\n");
3675 if (show_patch) {
3676 err = print_patch(commit, id, path, diff_context, repo);
3677 if (err == 0)
3678 printf("\n");
3681 if (fflush(stdout) != 0 && err == NULL)
3682 err = got_error_from_errno("fflush");
3683 done:
3684 free(id_str);
3685 free(refs_str);
3686 return err;
3689 static const struct got_error *
3690 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3691 struct got_repository *repo, const char *path, int show_changed_paths,
3692 int show_patch, const char *search_pattern, int diff_context, int limit,
3693 int log_branches, int reverse_display_order,
3694 struct got_reflist_object_id_map *refs_idmap)
3696 const struct got_error *err;
3697 struct got_commit_graph *graph;
3698 regex_t regex;
3699 int have_match;
3700 struct got_object_id_queue reversed_commits;
3701 struct got_object_qid *qid;
3702 struct got_commit_object *commit;
3703 struct got_pathlist_head changed_paths;
3704 struct got_pathlist_entry *pe;
3706 SIMPLEQ_INIT(&reversed_commits);
3707 TAILQ_INIT(&changed_paths);
3709 if (search_pattern && regcomp(&regex, search_pattern,
3710 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3711 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3713 err = got_commit_graph_open(&graph, path, !log_branches);
3714 if (err)
3715 return err;
3716 err = got_commit_graph_iter_start(graph, root_id, repo,
3717 check_cancelled, NULL);
3718 if (err)
3719 goto done;
3720 for (;;) {
3721 struct got_object_id *id;
3723 if (sigint_received || sigpipe_received)
3724 break;
3726 err = got_commit_graph_iter_next(&id, graph, repo,
3727 check_cancelled, NULL);
3728 if (err) {
3729 if (err->code == GOT_ERR_ITER_COMPLETED)
3730 err = NULL;
3731 break;
3733 if (id == NULL)
3734 break;
3736 err = got_object_open_as_commit(&commit, repo, id);
3737 if (err)
3738 break;
3740 if (show_changed_paths && !reverse_display_order) {
3741 err = get_changed_paths(&changed_paths, commit, repo);
3742 if (err)
3743 break;
3746 if (search_pattern) {
3747 err = match_logmsg(&have_match, id, commit, &regex);
3748 if (err) {
3749 got_object_commit_close(commit);
3750 break;
3752 if (have_match == 0 && show_changed_paths)
3753 match_changed_paths(&have_match,
3754 &changed_paths, &regex);
3755 if (have_match == 0) {
3756 got_object_commit_close(commit);
3757 TAILQ_FOREACH(pe, &changed_paths, entry) {
3758 free((char *)pe->path);
3759 free(pe->data);
3761 got_pathlist_free(&changed_paths);
3762 continue;
3766 if (reverse_display_order) {
3767 err = got_object_qid_alloc(&qid, id);
3768 if (err)
3769 break;
3770 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3771 got_object_commit_close(commit);
3772 } else {
3773 err = print_commit(commit, id, repo, path,
3774 show_changed_paths ? &changed_paths : NULL,
3775 show_patch, diff_context, refs_idmap);
3776 got_object_commit_close(commit);
3777 if (err)
3778 break;
3780 if ((limit && --limit == 0) ||
3781 (end_id && got_object_id_cmp(id, end_id) == 0))
3782 break;
3784 TAILQ_FOREACH(pe, &changed_paths, entry) {
3785 free((char *)pe->path);
3786 free(pe->data);
3788 got_pathlist_free(&changed_paths);
3790 if (reverse_display_order) {
3791 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3792 err = got_object_open_as_commit(&commit, repo, qid->id);
3793 if (err)
3794 break;
3795 if (show_changed_paths) {
3796 err = get_changed_paths(&changed_paths,
3797 commit, repo);
3798 if (err)
3799 break;
3801 err = print_commit(commit, qid->id, repo, path,
3802 show_changed_paths ? &changed_paths : NULL,
3803 show_patch, diff_context, refs_idmap);
3804 got_object_commit_close(commit);
3805 if (err)
3806 break;
3807 TAILQ_FOREACH(pe, &changed_paths, entry) {
3808 free((char *)pe->path);
3809 free(pe->data);
3811 got_pathlist_free(&changed_paths);
3814 done:
3815 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3816 qid = SIMPLEQ_FIRST(&reversed_commits);
3817 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3818 got_object_qid_free(qid);
3820 TAILQ_FOREACH(pe, &changed_paths, entry) {
3821 free((char *)pe->path);
3822 free(pe->data);
3824 got_pathlist_free(&changed_paths);
3825 if (search_pattern)
3826 regfree(&regex);
3827 got_commit_graph_close(graph);
3828 return err;
3831 __dead static void
3832 usage_log(void)
3834 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3835 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3836 "[-R] [path]\n", getprogname());
3837 exit(1);
3840 static int
3841 get_default_log_limit(void)
3843 const char *got_default_log_limit;
3844 long long n;
3845 const char *errstr;
3847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3848 if (got_default_log_limit == NULL)
3849 return 0;
3850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3851 if (errstr != NULL)
3852 return 0;
3853 return n;
3856 static const struct got_error *
3857 cmd_log(int argc, char *argv[])
3859 const struct got_error *error;
3860 struct got_repository *repo = NULL;
3861 struct got_worktree *worktree = NULL;
3862 struct got_object_id *start_id = NULL, *end_id = NULL;
3863 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3864 const char *start_commit = NULL, *end_commit = NULL;
3865 const char *search_pattern = NULL;
3866 int diff_context = -1, ch;
3867 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3868 int reverse_display_order = 0;
3869 const char *errstr;
3870 struct got_reflist_head refs;
3871 struct got_reflist_object_id_map *refs_idmap = NULL;
3873 TAILQ_INIT(&refs);
3875 #ifndef PROFILE
3876 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3877 NULL)
3878 == -1)
3879 err(1, "pledge");
3880 #endif
3882 limit = get_default_log_limit();
3884 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3885 switch (ch) {
3886 case 'p':
3887 show_patch = 1;
3888 break;
3889 case 'P':
3890 show_changed_paths = 1;
3891 break;
3892 case 'c':
3893 start_commit = optarg;
3894 break;
3895 case 'C':
3896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3897 &errstr);
3898 if (errstr != NULL)
3899 err(1, "-C option %s", errstr);
3900 break;
3901 case 'l':
3902 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3903 if (errstr != NULL)
3904 err(1, "-l option %s", errstr);
3905 break;
3906 case 'b':
3907 log_branches = 1;
3908 break;
3909 case 'r':
3910 repo_path = realpath(optarg, NULL);
3911 if (repo_path == NULL)
3912 return got_error_from_errno2("realpath",
3913 optarg);
3914 got_path_strip_trailing_slashes(repo_path);
3915 break;
3916 case 'R':
3917 reverse_display_order = 1;
3918 break;
3919 case 's':
3920 search_pattern = optarg;
3921 break;
3922 case 'x':
3923 end_commit = optarg;
3924 break;
3925 default:
3926 usage_log();
3927 /* NOTREACHED */
3931 argc -= optind;
3932 argv += optind;
3934 if (diff_context == -1)
3935 diff_context = 3;
3936 else if (!show_patch)
3937 errx(1, "-C requires -p");
3939 cwd = getcwd(NULL, 0);
3940 if (cwd == NULL) {
3941 error = got_error_from_errno("getcwd");
3942 goto done;
3945 if (repo_path == NULL) {
3946 error = got_worktree_open(&worktree, cwd);
3947 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3948 goto done;
3949 error = NULL;
3952 if (argc == 1) {
3953 if (worktree) {
3954 error = got_worktree_resolve_path(&path, worktree,
3955 argv[0]);
3956 if (error)
3957 goto done;
3958 } else {
3959 path = strdup(argv[0]);
3960 if (path == NULL) {
3961 error = got_error_from_errno("strdup");
3962 goto done;
3965 } else if (argc != 0)
3966 usage_log();
3968 if (repo_path == NULL) {
3969 repo_path = worktree ?
3970 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3972 if (repo_path == NULL) {
3973 error = got_error_from_errno("strdup");
3974 goto done;
3977 error = got_repo_open(&repo, repo_path, NULL);
3978 if (error != NULL)
3979 goto done;
3981 error = apply_unveil(got_repo_get_path(repo), 1,
3982 worktree ? got_worktree_get_root_path(worktree) : NULL);
3983 if (error)
3984 goto done;
3986 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3987 if (error)
3988 goto done;
3990 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3991 if (error)
3992 goto done;
3994 if (start_commit == NULL) {
3995 struct got_reference *head_ref;
3996 struct got_commit_object *commit = NULL;
3997 error = got_ref_open(&head_ref, repo,
3998 worktree ? got_worktree_get_head_ref_name(worktree)
3999 : GOT_REF_HEAD, 0);
4000 if (error != NULL)
4001 goto done;
4002 error = got_ref_resolve(&start_id, repo, head_ref);
4003 got_ref_close(head_ref);
4004 if (error != NULL)
4005 goto done;
4006 error = got_object_open_as_commit(&commit, repo,
4007 start_id);
4008 if (error != NULL)
4009 goto done;
4010 got_object_commit_close(commit);
4011 } else {
4012 error = got_repo_match_object_id(&start_id, NULL,
4013 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4014 if (error != NULL)
4015 goto done;
4017 if (end_commit != NULL) {
4018 error = got_repo_match_object_id(&end_id, NULL,
4019 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4020 if (error != NULL)
4021 goto done;
4024 if (worktree) {
4026 * If a path was specified on the command line it was resolved
4027 * to a path in the work tree above. Prepend the work tree's
4028 * path prefix to obtain the corresponding in-repository path.
4030 if (path) {
4031 const char *prefix;
4032 prefix = got_worktree_get_path_prefix(worktree);
4033 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4034 (path[0] != '\0') ? "/" : "", path) == -1) {
4035 error = got_error_from_errno("asprintf");
4036 goto done;
4039 } else
4040 error = got_repo_map_path(&in_repo_path, repo,
4041 path ? path : "");
4042 if (error != NULL)
4043 goto done;
4044 if (in_repo_path) {
4045 free(path);
4046 path = in_repo_path;
4049 error = print_commits(start_id, end_id, repo, path ? path : "",
4050 show_changed_paths, show_patch, search_pattern, diff_context,
4051 limit, log_branches, reverse_display_order, refs_idmap);
4052 done:
4053 free(path);
4054 free(repo_path);
4055 free(cwd);
4056 if (worktree)
4057 got_worktree_close(worktree);
4058 if (repo) {
4059 const struct got_error *repo_error;
4060 repo_error = got_repo_close(repo);
4061 if (error == NULL)
4062 error = repo_error;
4064 if (refs_idmap)
4065 got_reflist_object_id_map_free(refs_idmap);
4066 got_ref_list_free(&refs);
4067 return error;
4070 __dead static void
4071 usage_diff(void)
4073 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4074 "[-s] [-w] [object1 object2 | path]\n", getprogname());
4075 exit(1);
4078 struct print_diff_arg {
4079 struct got_repository *repo;
4080 struct got_worktree *worktree;
4081 int diff_context;
4082 const char *id_str;
4083 int header_shown;
4084 int diff_staged;
4085 int ignore_whitespace;
4086 int force_text_diff;
4090 * Create a file which contains the target path of a symlink so we can feed
4091 * it as content to the diff engine.
4093 static const struct got_error *
4094 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4095 const char *abspath)
4097 const struct got_error *err = NULL;
4098 char target_path[PATH_MAX];
4099 ssize_t target_len, outlen;
4101 *fd = -1;
4103 if (dirfd != -1) {
4104 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4105 if (target_len == -1)
4106 return got_error_from_errno2("readlinkat", abspath);
4107 } else {
4108 target_len = readlink(abspath, target_path, PATH_MAX);
4109 if (target_len == -1)
4110 return got_error_from_errno2("readlink", abspath);
4113 *fd = got_opentempfd();
4114 if (*fd == -1)
4115 return got_error_from_errno("got_opentempfd");
4117 outlen = write(*fd, target_path, target_len);
4118 if (outlen == -1) {
4119 err = got_error_from_errno("got_opentempfd");
4120 goto done;
4123 if (lseek(*fd, 0, SEEK_SET) == -1) {
4124 err = got_error_from_errno2("lseek", abspath);
4125 goto done;
4127 done:
4128 if (err) {
4129 close(*fd);
4130 *fd = -1;
4132 return err;
4135 static const struct got_error *
4136 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4137 const char *path, struct got_object_id *blob_id,
4138 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4139 int dirfd, const char *de_name)
4141 struct print_diff_arg *a = arg;
4142 const struct got_error *err = NULL;
4143 struct got_blob_object *blob1 = NULL;
4144 int fd = -1;
4145 FILE *f2 = NULL;
4146 char *abspath = NULL, *label1 = NULL;
4147 struct stat sb;
4149 if (a->diff_staged) {
4150 if (staged_status != GOT_STATUS_MODIFY &&
4151 staged_status != GOT_STATUS_ADD &&
4152 staged_status != GOT_STATUS_DELETE)
4153 return NULL;
4154 } else {
4155 if (staged_status == GOT_STATUS_DELETE)
4156 return NULL;
4157 if (status == GOT_STATUS_NONEXISTENT)
4158 return got_error_set_errno(ENOENT, path);
4159 if (status != GOT_STATUS_MODIFY &&
4160 status != GOT_STATUS_ADD &&
4161 status != GOT_STATUS_DELETE &&
4162 status != GOT_STATUS_CONFLICT)
4163 return NULL;
4166 if (!a->header_shown) {
4167 printf("diff %s %s%s\n", a->id_str,
4168 got_worktree_get_root_path(a->worktree),
4169 a->diff_staged ? " (staged changes)" : "");
4170 a->header_shown = 1;
4173 if (a->diff_staged) {
4174 const char *label1 = NULL, *label2 = NULL;
4175 switch (staged_status) {
4176 case GOT_STATUS_MODIFY:
4177 label1 = path;
4178 label2 = path;
4179 break;
4180 case GOT_STATUS_ADD:
4181 label2 = path;
4182 break;
4183 case GOT_STATUS_DELETE:
4184 label1 = path;
4185 break;
4186 default:
4187 return got_error(GOT_ERR_FILE_STATUS);
4189 return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4190 staged_blob_id, label1, label2, a->diff_context,
4191 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4194 if (staged_status == GOT_STATUS_ADD ||
4195 staged_status == GOT_STATUS_MODIFY) {
4196 char *id_str;
4197 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4198 8192);
4199 if (err)
4200 goto done;
4201 err = got_object_id_str(&id_str, staged_blob_id);
4202 if (err)
4203 goto done;
4204 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4205 err = got_error_from_errno("asprintf");
4206 free(id_str);
4207 goto done;
4209 free(id_str);
4210 } else if (status != GOT_STATUS_ADD) {
4211 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4212 if (err)
4213 goto done;
4216 if (status != GOT_STATUS_DELETE) {
4217 if (asprintf(&abspath, "%s/%s",
4218 got_worktree_get_root_path(a->worktree), path) == -1) {
4219 err = got_error_from_errno("asprintf");
4220 goto done;
4223 if (dirfd != -1) {
4224 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4225 if (fd == -1) {
4226 if (errno != ELOOP) {
4227 err = got_error_from_errno2("openat",
4228 abspath);
4229 goto done;
4231 err = get_symlink_target_file(&fd, dirfd,
4232 de_name, abspath);
4233 if (err)
4234 goto done;
4236 } else {
4237 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4238 if (fd == -1) {
4239 if (errno != ELOOP) {
4240 err = got_error_from_errno2("open",
4241 abspath);
4242 goto done;
4244 err = get_symlink_target_file(&fd, dirfd,
4245 de_name, abspath);
4246 if (err)
4247 goto done;
4250 if (fstat(fd, &sb) == -1) {
4251 err = got_error_from_errno2("fstat", abspath);
4252 goto done;
4254 f2 = fdopen(fd, "r");
4255 if (f2 == NULL) {
4256 err = got_error_from_errno2("fdopen", abspath);
4257 goto done;
4259 fd = -1;
4260 } else
4261 sb.st_size = 0;
4263 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4264 a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4265 done:
4266 if (blob1)
4267 got_object_blob_close(blob1);
4268 if (f2 && fclose(f2) == EOF && err == NULL)
4269 err = got_error_from_errno("fclose");
4270 if (fd != -1 && close(fd) == -1 && err == NULL)
4271 err = got_error_from_errno("close");
4272 free(abspath);
4273 return err;
4276 static const struct got_error *
4277 cmd_diff(int argc, char *argv[])
4279 const struct got_error *error;
4280 struct got_repository *repo = NULL;
4281 struct got_worktree *worktree = NULL;
4282 char *cwd = NULL, *repo_path = NULL;
4283 struct got_object_id *id1 = NULL, *id2 = NULL;
4284 const char *id_str1 = NULL, *id_str2 = NULL;
4285 char *label1 = NULL, *label2 = NULL;
4286 int type1, type2;
4287 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4288 int force_text_diff = 0;
4289 const char *errstr;
4290 char *path = NULL;
4291 struct got_reflist_head refs;
4293 TAILQ_INIT(&refs);
4295 #ifndef PROFILE
4296 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4297 NULL) == -1)
4298 err(1, "pledge");
4299 #endif
4301 while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4302 switch (ch) {
4303 case 'a':
4304 force_text_diff = 1;
4305 break;
4306 case 'C':
4307 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4308 &errstr);
4309 if (errstr != NULL)
4310 err(1, "-C option %s", errstr);
4311 break;
4312 case 'r':
4313 repo_path = realpath(optarg, NULL);
4314 if (repo_path == NULL)
4315 return got_error_from_errno2("realpath",
4316 optarg);
4317 got_path_strip_trailing_slashes(repo_path);
4318 break;
4319 case 's':
4320 diff_staged = 1;
4321 break;
4322 case 'w':
4323 ignore_whitespace = 1;
4324 break;
4325 default:
4326 usage_diff();
4327 /* NOTREACHED */
4331 argc -= optind;
4332 argv += optind;
4334 cwd = getcwd(NULL, 0);
4335 if (cwd == NULL) {
4336 error = got_error_from_errno("getcwd");
4337 goto done;
4339 if (argc <= 1) {
4340 if (repo_path)
4341 errx(1,
4342 "-r option can't be used when diffing a work tree");
4343 error = got_worktree_open(&worktree, cwd);
4344 if (error) {
4345 if (error->code == GOT_ERR_NOT_WORKTREE)
4346 error = wrap_not_worktree_error(error, "diff",
4347 cwd);
4348 goto done;
4350 repo_path = strdup(got_worktree_get_repo_path(worktree));
4351 if (repo_path == NULL) {
4352 error = got_error_from_errno("strdup");
4353 goto done;
4355 if (argc == 1) {
4356 error = got_worktree_resolve_path(&path, worktree,
4357 argv[0]);
4358 if (error)
4359 goto done;
4360 } else {
4361 path = strdup("");
4362 if (path == NULL) {
4363 error = got_error_from_errno("strdup");
4364 goto done;
4367 } else if (argc == 2) {
4368 if (diff_staged)
4369 errx(1, "-s option can't be used when diffing "
4370 "objects in repository");
4371 id_str1 = argv[0];
4372 id_str2 = argv[1];
4373 if (repo_path == NULL) {
4374 error = got_worktree_open(&worktree, cwd);
4375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4376 goto done;
4377 if (worktree) {
4378 repo_path = strdup(
4379 got_worktree_get_repo_path(worktree));
4380 if (repo_path == NULL) {
4381 error = got_error_from_errno("strdup");
4382 goto done;
4384 } else {
4385 repo_path = strdup(cwd);
4386 if (repo_path == NULL) {
4387 error = got_error_from_errno("strdup");
4388 goto done;
4392 } else
4393 usage_diff();
4395 error = got_repo_open(&repo, repo_path, NULL);
4396 free(repo_path);
4397 if (error != NULL)
4398 goto done;
4400 error = apply_unveil(got_repo_get_path(repo), 1,
4401 worktree ? got_worktree_get_root_path(worktree) : NULL);
4402 if (error)
4403 goto done;
4405 if (argc <= 1) {
4406 struct print_diff_arg arg;
4407 struct got_pathlist_head paths;
4408 char *id_str;
4410 TAILQ_INIT(&paths);
4412 error = got_object_id_str(&id_str,
4413 got_worktree_get_base_commit_id(worktree));
4414 if (error)
4415 goto done;
4416 arg.repo = repo;
4417 arg.worktree = worktree;
4418 arg.diff_context = diff_context;
4419 arg.id_str = id_str;
4420 arg.header_shown = 0;
4421 arg.diff_staged = diff_staged;
4422 arg.ignore_whitespace = ignore_whitespace;
4423 arg.force_text_diff = force_text_diff;
4425 error = got_pathlist_append(&paths, path, NULL);
4426 if (error)
4427 goto done;
4429 error = got_worktree_status(worktree, &paths, repo, print_diff,
4430 &arg, check_cancelled, NULL);
4431 free(id_str);
4432 got_pathlist_free(&paths);
4433 goto done;
4436 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4437 if (error)
4438 return error;
4440 error = got_repo_match_object_id(&id1, &label1, id_str1,
4441 GOT_OBJ_TYPE_ANY, &refs, repo);
4442 if (error)
4443 goto done;
4445 error = got_repo_match_object_id(&id2, &label2, id_str2,
4446 GOT_OBJ_TYPE_ANY, &refs, repo);
4447 if (error)
4448 goto done;
4450 error = got_object_get_type(&type1, repo, id1);
4451 if (error)
4452 goto done;
4454 error = got_object_get_type(&type2, repo, id2);
4455 if (error)
4456 goto done;
4458 if (type1 != type2) {
4459 error = got_error(GOT_ERR_OBJ_TYPE);
4460 goto done;
4463 switch (type1) {
4464 case GOT_OBJ_TYPE_BLOB:
4465 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4466 NULL, NULL, diff_context, ignore_whitespace,
4467 force_text_diff, repo, stdout);
4468 break;
4469 case GOT_OBJ_TYPE_TREE:
4470 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4471 "", "", diff_context, ignore_whitespace, force_text_diff,
4472 repo, stdout);
4473 break;
4474 case GOT_OBJ_TYPE_COMMIT:
4475 printf("diff %s %s\n", label1, label2);
4476 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4477 diff_context, ignore_whitespace, force_text_diff, repo,
4478 stdout);
4479 break;
4480 default:
4481 error = got_error(GOT_ERR_OBJ_TYPE);
4483 done:
4484 free(label1);
4485 free(label2);
4486 free(id1);
4487 free(id2);
4488 free(path);
4489 if (worktree)
4490 got_worktree_close(worktree);
4491 if (repo) {
4492 const struct got_error *repo_error;
4493 repo_error = got_repo_close(repo);
4494 if (error == NULL)
4495 error = repo_error;
4497 got_ref_list_free(&refs);
4498 return error;
4501 __dead static void
4502 usage_blame(void)
4504 fprintf(stderr,
4505 "usage: %s blame [-c commit] [-r repository-path] path\n",
4506 getprogname());
4507 exit(1);
4510 struct blame_line {
4511 int annotated;
4512 char *id_str;
4513 char *committer;
4514 char datebuf[11]; /* YYYY-MM-DD + NUL */
4517 struct blame_cb_args {
4518 struct blame_line *lines;
4519 int nlines;
4520 int nlines_prec;
4521 int lineno_cur;
4522 off_t *line_offsets;
4523 FILE *f;
4524 struct got_repository *repo;
4527 static const struct got_error *
4528 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4530 const struct got_error *err = NULL;
4531 struct blame_cb_args *a = arg;
4532 struct blame_line *bline;
4533 char *line = NULL;
4534 size_t linesize = 0;
4535 struct got_commit_object *commit = NULL;
4536 off_t offset;
4537 struct tm tm;
4538 time_t committer_time;
4540 if (nlines != a->nlines ||
4541 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4542 return got_error(GOT_ERR_RANGE);
4544 if (sigint_received)
4545 return got_error(GOT_ERR_ITER_COMPLETED);
4547 if (lineno == -1)
4548 return NULL; /* no change in this commit */
4550 /* Annotate this line. */
4551 bline = &a->lines[lineno - 1];
4552 if (bline->annotated)
4553 return NULL;
4554 err = got_object_id_str(&bline->id_str, id);
4555 if (err)
4556 return err;
4558 err = got_object_open_as_commit(&commit, a->repo, id);
4559 if (err)
4560 goto done;
4562 bline->committer = strdup(got_object_commit_get_committer(commit));
4563 if (bline->committer == NULL) {
4564 err = got_error_from_errno("strdup");
4565 goto done;
4568 committer_time = got_object_commit_get_committer_time(commit);
4569 if (localtime_r(&committer_time, &tm) == NULL)
4570 return got_error_from_errno("localtime_r");
4571 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4572 &tm) >= sizeof(bline->datebuf)) {
4573 err = got_error(GOT_ERR_NO_SPACE);
4574 goto done;
4576 bline->annotated = 1;
4578 /* Print lines annotated so far. */
4579 bline = &a->lines[a->lineno_cur - 1];
4580 if (!bline->annotated)
4581 goto done;
4583 offset = a->line_offsets[a->lineno_cur - 1];
4584 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4585 err = got_error_from_errno("fseeko");
4586 goto done;
4589 while (bline->annotated) {
4590 char *smallerthan, *at, *nl, *committer;
4591 size_t len;
4593 if (getline(&line, &linesize, a->f) == -1) {
4594 if (ferror(a->f))
4595 err = got_error_from_errno("getline");
4596 break;
4599 committer = bline->committer;
4600 smallerthan = strchr(committer, '<');
4601 if (smallerthan && smallerthan[1] != '\0')
4602 committer = smallerthan + 1;
4603 at = strchr(committer, '@');
4604 if (at)
4605 *at = '\0';
4606 len = strlen(committer);
4607 if (len >= 9)
4608 committer[8] = '\0';
4610 nl = strchr(line, '\n');
4611 if (nl)
4612 *nl = '\0';
4613 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4614 bline->id_str, bline->datebuf, committer, line);
4616 a->lineno_cur++;
4617 bline = &a->lines[a->lineno_cur - 1];
4619 done:
4620 if (commit)
4621 got_object_commit_close(commit);
4622 free(line);
4623 return err;
4626 static const struct got_error *
4627 cmd_blame(int argc, char *argv[])
4629 const struct got_error *error;
4630 struct got_repository *repo = NULL;
4631 struct got_worktree *worktree = NULL;
4632 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4633 char *link_target = NULL;
4634 struct got_object_id *obj_id = NULL;
4635 struct got_object_id *commit_id = NULL;
4636 struct got_blob_object *blob = NULL;
4637 char *commit_id_str = NULL;
4638 struct blame_cb_args bca;
4639 int ch, obj_type, i;
4640 off_t filesize;
4642 memset(&bca, 0, sizeof(bca));
4644 #ifndef PROFILE
4645 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4646 NULL) == -1)
4647 err(1, "pledge");
4648 #endif
4650 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4651 switch (ch) {
4652 case 'c':
4653 commit_id_str = optarg;
4654 break;
4655 case 'r':
4656 repo_path = realpath(optarg, NULL);
4657 if (repo_path == NULL)
4658 return got_error_from_errno2("realpath",
4659 optarg);
4660 got_path_strip_trailing_slashes(repo_path);
4661 break;
4662 default:
4663 usage_blame();
4664 /* NOTREACHED */
4668 argc -= optind;
4669 argv += optind;
4671 if (argc == 1)
4672 path = argv[0];
4673 else
4674 usage_blame();
4676 cwd = getcwd(NULL, 0);
4677 if (cwd == NULL) {
4678 error = got_error_from_errno("getcwd");
4679 goto done;
4681 if (repo_path == NULL) {
4682 error = got_worktree_open(&worktree, cwd);
4683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4684 goto done;
4685 else
4686 error = NULL;
4687 if (worktree) {
4688 repo_path =
4689 strdup(got_worktree_get_repo_path(worktree));
4690 if (repo_path == NULL) {
4691 error = got_error_from_errno("strdup");
4692 if (error)
4693 goto done;
4695 } else {
4696 repo_path = strdup(cwd);
4697 if (repo_path == NULL) {
4698 error = got_error_from_errno("strdup");
4699 goto done;
4704 error = got_repo_open(&repo, repo_path, NULL);
4705 if (error != NULL)
4706 goto done;
4708 if (worktree) {
4709 const char *prefix = got_worktree_get_path_prefix(worktree);
4710 char *p;
4712 error = got_worktree_resolve_path(&p, worktree, path);
4713 if (error)
4714 goto done;
4715 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4716 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4717 p) == -1) {
4718 error = got_error_from_errno("asprintf");
4719 free(p);
4720 goto done;
4722 free(p);
4723 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4724 } else {
4725 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4726 if (error)
4727 goto done;
4728 error = got_repo_map_path(&in_repo_path, repo, path);
4730 if (error)
4731 goto done;
4733 if (commit_id_str == NULL) {
4734 struct got_reference *head_ref;
4735 error = got_ref_open(&head_ref, repo, worktree ?
4736 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4737 if (error != NULL)
4738 goto done;
4739 error = got_ref_resolve(&commit_id, repo, head_ref);
4740 got_ref_close(head_ref);
4741 if (error != NULL)
4742 goto done;
4743 } else {
4744 struct got_reflist_head refs;
4745 TAILQ_INIT(&refs);
4746 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4747 NULL);
4748 if (error)
4749 goto done;
4750 error = got_repo_match_object_id(&commit_id, NULL,
4751 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4752 got_ref_list_free(&refs);
4753 if (error)
4754 goto done;
4757 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4758 commit_id, repo);
4759 if (error)
4760 goto done;
4762 error = got_object_id_by_path(&obj_id, repo, commit_id,
4763 link_target ? link_target : in_repo_path);
4764 if (error)
4765 goto done;
4767 error = got_object_get_type(&obj_type, repo, obj_id);
4768 if (error)
4769 goto done;
4771 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4772 error = got_error_path(link_target ? link_target : in_repo_path,
4773 GOT_ERR_OBJ_TYPE);
4774 goto done;
4777 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4778 if (error)
4779 goto done;
4780 bca.f = got_opentemp();
4781 if (bca.f == NULL) {
4782 error = got_error_from_errno("got_opentemp");
4783 goto done;
4785 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4786 &bca.line_offsets, bca.f, blob);
4787 if (error || bca.nlines == 0)
4788 goto done;
4790 /* Don't include \n at EOF in the blame line count. */
4791 if (bca.line_offsets[bca.nlines - 1] == filesize)
4792 bca.nlines--;
4794 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4795 if (bca.lines == NULL) {
4796 error = got_error_from_errno("calloc");
4797 goto done;
4799 bca.lineno_cur = 1;
4800 bca.nlines_prec = 0;
4801 i = bca.nlines;
4802 while (i > 0) {
4803 i /= 10;
4804 bca.nlines_prec++;
4806 bca.repo = repo;
4808 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4809 repo, blame_cb, &bca, check_cancelled, NULL);
4810 done:
4811 free(in_repo_path);
4812 free(link_target);
4813 free(repo_path);
4814 free(cwd);
4815 free(commit_id);
4816 free(obj_id);
4817 if (blob)
4818 got_object_blob_close(blob);
4819 if (worktree)
4820 got_worktree_close(worktree);
4821 if (repo) {
4822 const struct got_error *repo_error;
4823 repo_error = got_repo_close(repo);
4824 if (error == NULL)
4825 error = repo_error;
4827 if (bca.lines) {
4828 for (i = 0; i < bca.nlines; i++) {
4829 struct blame_line *bline = &bca.lines[i];
4830 free(bline->id_str);
4831 free(bline->committer);
4833 free(bca.lines);
4835 free(bca.line_offsets);
4836 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4837 error = got_error_from_errno("fclose");
4838 return error;
4841 __dead static void
4842 usage_tree(void)
4844 fprintf(stderr,
4845 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4846 getprogname());
4847 exit(1);
4850 static const struct got_error *
4851 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4852 const char *root_path, struct got_repository *repo)
4854 const struct got_error *err = NULL;
4855 int is_root_path = (strcmp(path, root_path) == 0);
4856 const char *modestr = "";
4857 mode_t mode = got_tree_entry_get_mode(te);
4858 char *link_target = NULL;
4860 path += strlen(root_path);
4861 while (path[0] == '/')
4862 path++;
4864 if (got_object_tree_entry_is_submodule(te))
4865 modestr = "$";
4866 else if (S_ISLNK(mode)) {
4867 int i;
4869 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4870 if (err)
4871 return err;
4872 for (i = 0; i < strlen(link_target); i++) {
4873 if (!isprint((unsigned char)link_target[i]))
4874 link_target[i] = '?';
4877 modestr = "@";
4879 else if (S_ISDIR(mode))
4880 modestr = "/";
4881 else if (mode & S_IXUSR)
4882 modestr = "*";
4884 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4885 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4886 link_target ? " -> ": "", link_target ? link_target : "");
4888 free(link_target);
4889 return NULL;
4892 static const struct got_error *
4893 print_tree(const char *path, struct got_object_id *commit_id,
4894 int show_ids, int recurse, const char *root_path,
4895 struct got_repository *repo)
4897 const struct got_error *err = NULL;
4898 struct got_object_id *tree_id = NULL;
4899 struct got_tree_object *tree = NULL;
4900 int nentries, i;
4902 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4903 if (err)
4904 goto done;
4906 err = got_object_open_as_tree(&tree, repo, tree_id);
4907 if (err)
4908 goto done;
4909 nentries = got_object_tree_get_nentries(tree);
4910 for (i = 0; i < nentries; i++) {
4911 struct got_tree_entry *te;
4912 char *id = NULL;
4914 if (sigint_received || sigpipe_received)
4915 break;
4917 te = got_object_tree_get_entry(tree, i);
4918 if (show_ids) {
4919 char *id_str;
4920 err = got_object_id_str(&id_str,
4921 got_tree_entry_get_id(te));
4922 if (err)
4923 goto done;
4924 if (asprintf(&id, "%s ", id_str) == -1) {
4925 err = got_error_from_errno("asprintf");
4926 free(id_str);
4927 goto done;
4929 free(id_str);
4931 err = print_entry(te, id, path, root_path, repo);
4932 free(id);
4933 if (err)
4934 goto done;
4936 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4937 char *child_path;
4938 if (asprintf(&child_path, "%s%s%s", path,
4939 path[0] == '/' && path[1] == '\0' ? "" : "/",
4940 got_tree_entry_get_name(te)) == -1) {
4941 err = got_error_from_errno("asprintf");
4942 goto done;
4944 err = print_tree(child_path, commit_id, show_ids, 1,
4945 root_path, repo);
4946 free(child_path);
4947 if (err)
4948 goto done;
4951 done:
4952 if (tree)
4953 got_object_tree_close(tree);
4954 free(tree_id);
4955 return err;
4958 static const struct got_error *
4959 cmd_tree(int argc, char *argv[])
4961 const struct got_error *error;
4962 struct got_repository *repo = NULL;
4963 struct got_worktree *worktree = NULL;
4964 const char *path, *refname = NULL;
4965 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4966 struct got_object_id *commit_id = NULL;
4967 char *commit_id_str = NULL;
4968 int show_ids = 0, recurse = 0;
4969 int ch;
4971 #ifndef PROFILE
4972 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4973 NULL) == -1)
4974 err(1, "pledge");
4975 #endif
4977 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4978 switch (ch) {
4979 case 'c':
4980 commit_id_str = optarg;
4981 break;
4982 case 'r':
4983 repo_path = realpath(optarg, NULL);
4984 if (repo_path == NULL)
4985 return got_error_from_errno2("realpath",
4986 optarg);
4987 got_path_strip_trailing_slashes(repo_path);
4988 break;
4989 case 'i':
4990 show_ids = 1;
4991 break;
4992 case 'R':
4993 recurse = 1;
4994 break;
4995 default:
4996 usage_tree();
4997 /* NOTREACHED */
5001 argc -= optind;
5002 argv += optind;
5004 if (argc == 1)
5005 path = argv[0];
5006 else if (argc > 1)
5007 usage_tree();
5008 else
5009 path = NULL;
5011 cwd = getcwd(NULL, 0);
5012 if (cwd == NULL) {
5013 error = got_error_from_errno("getcwd");
5014 goto done;
5016 if (repo_path == NULL) {
5017 error = got_worktree_open(&worktree, cwd);
5018 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5019 goto done;
5020 else
5021 error = NULL;
5022 if (worktree) {
5023 repo_path =
5024 strdup(got_worktree_get_repo_path(worktree));
5025 if (repo_path == NULL)
5026 error = got_error_from_errno("strdup");
5027 if (error)
5028 goto done;
5029 } else {
5030 repo_path = strdup(cwd);
5031 if (repo_path == NULL) {
5032 error = got_error_from_errno("strdup");
5033 goto done;
5038 error = got_repo_open(&repo, repo_path, NULL);
5039 if (error != NULL)
5040 goto done;
5042 if (worktree) {
5043 const char *prefix = got_worktree_get_path_prefix(worktree);
5044 char *p;
5046 if (path == NULL)
5047 path = "";
5048 error = got_worktree_resolve_path(&p, worktree, path);
5049 if (error)
5050 goto done;
5051 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5052 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5053 p) == -1) {
5054 error = got_error_from_errno("asprintf");
5055 free(p);
5056 goto done;
5058 free(p);
5059 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5060 if (error)
5061 goto done;
5062 } else {
5063 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5064 if (error)
5065 goto done;
5066 if (path == NULL)
5067 path = "/";
5068 error = got_repo_map_path(&in_repo_path, repo, path);
5069 if (error != NULL)
5070 goto done;
5073 if (commit_id_str == NULL) {
5074 struct got_reference *head_ref;
5075 if (worktree)
5076 refname = got_worktree_get_head_ref_name(worktree);
5077 else
5078 refname = GOT_REF_HEAD;
5079 error = got_ref_open(&head_ref, repo, refname, 0);
5080 if (error != NULL)
5081 goto done;
5082 error = got_ref_resolve(&commit_id, repo, head_ref);
5083 got_ref_close(head_ref);
5084 if (error != NULL)
5085 goto done;
5086 } else {
5087 struct got_reflist_head refs;
5088 TAILQ_INIT(&refs);
5089 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5090 NULL);
5091 if (error)
5092 goto done;
5093 error = got_repo_match_object_id(&commit_id, NULL,
5094 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5095 got_ref_list_free(&refs);
5096 if (error)
5097 goto done;
5100 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5101 in_repo_path, repo);
5102 done:
5103 free(in_repo_path);
5104 free(repo_path);
5105 free(cwd);
5106 free(commit_id);
5107 if (worktree)
5108 got_worktree_close(worktree);
5109 if (repo) {
5110 const struct got_error *repo_error;
5111 repo_error = got_repo_close(repo);
5112 if (error == NULL)
5113 error = repo_error;
5115 return error;
5118 __dead static void
5119 usage_status(void)
5121 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5122 getprogname());
5123 exit(1);
5126 static const struct got_error *
5127 print_status(void *arg, unsigned char status, unsigned char staged_status,
5128 const char *path, struct got_object_id *blob_id,
5129 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5130 int dirfd, const char *de_name)
5132 if (status == staged_status && (status == GOT_STATUS_DELETE))
5133 status = GOT_STATUS_NO_CHANGE;
5134 if (arg) {
5135 char *status_codes = arg;
5136 size_t ncodes = strlen(status_codes);
5137 int i;
5138 for (i = 0; i < ncodes ; i++) {
5139 if (status == status_codes[i] ||
5140 staged_status == status_codes[i])
5141 break;
5143 if (i == ncodes)
5144 return NULL;
5146 printf("%c%c %s\n", status, staged_status, path);
5147 return NULL;
5150 static const struct got_error *
5151 cmd_status(int argc, char *argv[])
5153 const struct got_error *error = NULL;
5154 struct got_repository *repo = NULL;
5155 struct got_worktree *worktree = NULL;
5156 char *cwd = NULL, *status_codes = NULL;;
5157 struct got_pathlist_head paths;
5158 struct got_pathlist_entry *pe;
5159 int ch, i;
5161 TAILQ_INIT(&paths);
5163 while ((ch = getopt(argc, argv, "s:")) != -1) {
5164 switch (ch) {
5165 case 's':
5166 for (i = 0; i < strlen(optarg); i++) {
5167 switch (optarg[i]) {
5168 case GOT_STATUS_MODIFY:
5169 case GOT_STATUS_ADD:
5170 case GOT_STATUS_DELETE:
5171 case GOT_STATUS_CONFLICT:
5172 case GOT_STATUS_MISSING:
5173 case GOT_STATUS_OBSTRUCTED:
5174 case GOT_STATUS_UNVERSIONED:
5175 case GOT_STATUS_MODE_CHANGE:
5176 case GOT_STATUS_NONEXISTENT:
5177 break;
5178 default:
5179 errx(1, "invalid status code '%c'",
5180 optarg[i]);
5183 status_codes = optarg;
5184 break;
5185 default:
5186 usage_status();
5187 /* NOTREACHED */
5191 argc -= optind;
5192 argv += optind;
5194 #ifndef PROFILE
5195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5196 NULL) == -1)
5197 err(1, "pledge");
5198 #endif
5199 cwd = getcwd(NULL, 0);
5200 if (cwd == NULL) {
5201 error = got_error_from_errno("getcwd");
5202 goto done;
5205 error = got_worktree_open(&worktree, cwd);
5206 if (error) {
5207 if (error->code == GOT_ERR_NOT_WORKTREE)
5208 error = wrap_not_worktree_error(error, "status", cwd);
5209 goto done;
5212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5213 NULL);
5214 if (error != NULL)
5215 goto done;
5217 error = apply_unveil(got_repo_get_path(repo), 1,
5218 got_worktree_get_root_path(worktree));
5219 if (error)
5220 goto done;
5222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5223 if (error)
5224 goto done;
5226 error = got_worktree_status(worktree, &paths, repo, print_status,
5227 status_codes, check_cancelled, NULL);
5228 done:
5229 TAILQ_FOREACH(pe, &paths, entry)
5230 free((char *)pe->path);
5231 got_pathlist_free(&paths);
5232 free(cwd);
5233 return error;
5236 __dead static void
5237 usage_ref(void)
5239 fprintf(stderr,
5240 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5241 "[-d] [name]\n",
5242 getprogname());
5243 exit(1);
5246 static const struct got_error *
5247 list_refs(struct got_repository *repo, const char *refname)
5249 static const struct got_error *err = NULL;
5250 struct got_reflist_head refs;
5251 struct got_reflist_entry *re;
5253 TAILQ_INIT(&refs);
5254 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5255 if (err)
5256 return err;
5258 TAILQ_FOREACH(re, &refs, entry) {
5259 char *refstr;
5260 refstr = got_ref_to_str(re->ref);
5261 if (refstr == NULL)
5262 return got_error_from_errno("got_ref_to_str");
5263 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5264 free(refstr);
5267 got_ref_list_free(&refs);
5268 return NULL;
5271 static const struct got_error *
5272 delete_ref(struct got_repository *repo, const char *refname)
5274 const struct got_error *err = NULL;
5275 struct got_reference *ref;
5277 err = got_ref_open(&ref, repo, refname, 0);
5278 if (err)
5279 return err;
5281 err = got_ref_delete(ref, repo);
5282 got_ref_close(ref);
5283 return err;
5286 static const struct got_error *
5287 add_ref(struct got_repository *repo, const char *refname, const char *target)
5289 const struct got_error *err = NULL;
5290 struct got_object_id *id;
5291 struct got_reference *ref = NULL;
5294 * Don't let the user create a reference name with a leading '-'.
5295 * While technically a valid reference name, this case is usually
5296 * an unintended typo.
5298 if (refname[0] == '-')
5299 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5301 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5302 repo);
5303 if (err) {
5304 struct got_reference *target_ref;
5306 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5307 return err;
5308 err = got_ref_open(&target_ref, repo, target, 0);
5309 if (err)
5310 return err;
5311 err = got_ref_resolve(&id, repo, target_ref);
5312 got_ref_close(target_ref);
5313 if (err)
5314 return err;
5317 err = got_ref_alloc(&ref, refname, id);
5318 if (err)
5319 goto done;
5321 err = got_ref_write(ref, repo);
5322 done:
5323 if (ref)
5324 got_ref_close(ref);
5325 free(id);
5326 return err;
5329 static const struct got_error *
5330 add_symref(struct got_repository *repo, const char *refname, const char *target)
5332 const struct got_error *err = NULL;
5333 struct got_reference *ref = NULL;
5334 struct got_reference *target_ref = NULL;
5337 * Don't let the user create a reference name with a leading '-'.
5338 * While technically a valid reference name, this case is usually
5339 * an unintended typo.
5341 if (refname[0] == '-')
5342 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5344 err = got_ref_open(&target_ref, repo, target, 0);
5345 if (err)
5346 return err;
5348 err = got_ref_alloc_symref(&ref, refname, target_ref);
5349 if (err)
5350 goto done;
5352 err = got_ref_write(ref, repo);
5353 done:
5354 if (target_ref)
5355 got_ref_close(target_ref);
5356 if (ref)
5357 got_ref_close(ref);
5358 return err;
5361 static const struct got_error *
5362 cmd_ref(int argc, char *argv[])
5364 const struct got_error *error = NULL;
5365 struct got_repository *repo = NULL;
5366 struct got_worktree *worktree = NULL;
5367 char *cwd = NULL, *repo_path = NULL;
5368 int ch, do_list = 0, do_delete = 0;
5369 const char *obj_arg = NULL, *symref_target= NULL;
5370 char *refname = NULL;
5372 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5373 switch (ch) {
5374 case 'c':
5375 obj_arg = optarg;
5376 break;
5377 case 'd':
5378 do_delete = 1;
5379 break;
5380 case 'r':
5381 repo_path = realpath(optarg, NULL);
5382 if (repo_path == NULL)
5383 return got_error_from_errno2("realpath",
5384 optarg);
5385 got_path_strip_trailing_slashes(repo_path);
5386 break;
5387 case 'l':
5388 do_list = 1;
5389 break;
5390 case 's':
5391 symref_target = optarg;
5392 break;
5393 default:
5394 usage_ref();
5395 /* NOTREACHED */
5399 if (obj_arg && do_list)
5400 option_conflict('c', 'l');
5401 if (obj_arg && do_delete)
5402 option_conflict('c', 'd');
5403 if (obj_arg && symref_target)
5404 option_conflict('c', 's');
5405 if (symref_target && do_delete)
5406 option_conflict('s', 'd');
5407 if (symref_target && do_list)
5408 option_conflict('s', 'l');
5409 if (do_delete && do_list)
5410 option_conflict('d', 'l');
5412 argc -= optind;
5413 argv += optind;
5415 if (do_list) {
5416 if (argc != 0 && argc != 1)
5417 usage_ref();
5418 if (argc == 1) {
5419 refname = strdup(argv[0]);
5420 if (refname == NULL) {
5421 error = got_error_from_errno("strdup");
5422 goto done;
5425 } else {
5426 if (argc != 1)
5427 usage_ref();
5428 refname = strdup(argv[0]);
5429 if (refname == NULL) {
5430 error = got_error_from_errno("strdup");
5431 goto done;
5435 if (refname)
5436 got_path_strip_trailing_slashes(refname);
5438 #ifndef PROFILE
5439 if (do_list) {
5440 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5441 NULL) == -1)
5442 err(1, "pledge");
5443 } else {
5444 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5445 "sendfd unveil", NULL) == -1)
5446 err(1, "pledge");
5448 #endif
5449 cwd = getcwd(NULL, 0);
5450 if (cwd == NULL) {
5451 error = got_error_from_errno("getcwd");
5452 goto done;
5455 if (repo_path == NULL) {
5456 error = got_worktree_open(&worktree, cwd);
5457 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5458 goto done;
5459 else
5460 error = NULL;
5461 if (worktree) {
5462 repo_path =
5463 strdup(got_worktree_get_repo_path(worktree));
5464 if (repo_path == NULL)
5465 error = got_error_from_errno("strdup");
5466 if (error)
5467 goto done;
5468 } else {
5469 repo_path = strdup(cwd);
5470 if (repo_path == NULL) {
5471 error = got_error_from_errno("strdup");
5472 goto done;
5477 error = got_repo_open(&repo, repo_path, NULL);
5478 if (error != NULL)
5479 goto done;
5481 error = apply_unveil(got_repo_get_path(repo), do_list,
5482 worktree ? got_worktree_get_root_path(worktree) : NULL);
5483 if (error)
5484 goto done;
5486 if (do_list)
5487 error = list_refs(repo, refname);
5488 else if (do_delete)
5489 error = delete_ref(repo, refname);
5490 else if (symref_target)
5491 error = add_symref(repo, refname, symref_target);
5492 else {
5493 if (obj_arg == NULL)
5494 usage_ref();
5495 error = add_ref(repo, refname, obj_arg);
5497 done:
5498 free(refname);
5499 if (repo)
5500 got_repo_close(repo);
5501 if (worktree)
5502 got_worktree_close(worktree);
5503 free(cwd);
5504 free(repo_path);
5505 return error;
5508 __dead static void
5509 usage_branch(void)
5511 fprintf(stderr,
5512 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5513 "[name]\n", getprogname());
5514 exit(1);
5517 static const struct got_error *
5518 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5519 struct got_reference *ref)
5521 const struct got_error *err = NULL;
5522 const char *refname, *marker = " ";
5523 char *refstr;
5525 refname = got_ref_get_name(ref);
5526 if (worktree && strcmp(refname,
5527 got_worktree_get_head_ref_name(worktree)) == 0) {
5528 struct got_object_id *id = NULL;
5530 err = got_ref_resolve(&id, repo, ref);
5531 if (err)
5532 return err;
5533 if (got_object_id_cmp(id,
5534 got_worktree_get_base_commit_id(worktree)) == 0)
5535 marker = "* ";
5536 else
5537 marker = "~ ";
5538 free(id);
5541 if (strncmp(refname, "refs/heads/", 11) == 0)
5542 refname += 11;
5543 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5544 refname += 18;
5546 refstr = got_ref_to_str(ref);
5547 if (refstr == NULL)
5548 return got_error_from_errno("got_ref_to_str");
5550 printf("%s%s: %s\n", marker, refname, refstr);
5551 free(refstr);
5552 return NULL;
5555 static const struct got_error *
5556 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5558 const char *refname;
5560 if (worktree == NULL)
5561 return got_error(GOT_ERR_NOT_WORKTREE);
5563 refname = got_worktree_get_head_ref_name(worktree);
5565 if (strncmp(refname, "refs/heads/", 11) == 0)
5566 refname += 11;
5567 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5568 refname += 18;
5570 printf("%s\n", refname);
5572 return NULL;
5575 static const struct got_error *
5576 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5578 static const struct got_error *err = NULL;
5579 struct got_reflist_head refs;
5580 struct got_reflist_entry *re;
5581 struct got_reference *temp_ref = NULL;
5582 int rebase_in_progress, histedit_in_progress;
5584 TAILQ_INIT(&refs);
5586 if (worktree) {
5587 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5588 worktree);
5589 if (err)
5590 return err;
5592 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5593 worktree);
5594 if (err)
5595 return err;
5597 if (rebase_in_progress || histedit_in_progress) {
5598 err = got_ref_open(&temp_ref, repo,
5599 got_worktree_get_head_ref_name(worktree), 0);
5600 if (err)
5601 return err;
5602 list_branch(repo, worktree, temp_ref);
5603 got_ref_close(temp_ref);
5607 err = got_ref_list(&refs, repo, "refs/heads",
5608 got_ref_cmp_by_name, NULL);
5609 if (err)
5610 return err;
5612 TAILQ_FOREACH(re, &refs, entry)
5613 list_branch(repo, worktree, re->ref);
5615 got_ref_list_free(&refs);
5616 return NULL;
5619 static const struct got_error *
5620 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5621 const char *branch_name)
5623 const struct got_error *err = NULL;
5624 struct got_reference *ref = NULL;
5625 char *refname;
5627 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5628 return got_error_from_errno("asprintf");
5630 err = got_ref_open(&ref, repo, refname, 0);
5631 if (err)
5632 goto done;
5634 if (worktree &&
5635 strcmp(got_worktree_get_head_ref_name(worktree),
5636 got_ref_get_name(ref)) == 0) {
5637 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5638 "will not delete this work tree's current branch");
5639 goto done;
5642 err = got_ref_delete(ref, repo);
5643 done:
5644 if (ref)
5645 got_ref_close(ref);
5646 free(refname);
5647 return err;
5650 static const struct got_error *
5651 add_branch(struct got_repository *repo, const char *branch_name,
5652 struct got_object_id *base_commit_id)
5654 const struct got_error *err = NULL;
5655 struct got_reference *ref = NULL;
5656 char *base_refname = NULL, *refname = NULL;
5659 * Don't let the user create a branch name with a leading '-'.
5660 * While technically a valid reference name, this case is usually
5661 * an unintended typo.
5663 if (branch_name[0] == '-')
5664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5667 err = got_error_from_errno("asprintf");
5668 goto done;
5671 err = got_ref_open(&ref, repo, refname, 0);
5672 if (err == NULL) {
5673 err = got_error(GOT_ERR_BRANCH_EXISTS);
5674 goto done;
5675 } else if (err->code != GOT_ERR_NOT_REF)
5676 goto done;
5678 err = got_ref_alloc(&ref, refname, base_commit_id);
5679 if (err)
5680 goto done;
5682 err = got_ref_write(ref, repo);
5683 done:
5684 if (ref)
5685 got_ref_close(ref);
5686 free(base_refname);
5687 free(refname);
5688 return err;
5691 static const struct got_error *
5692 cmd_branch(int argc, char *argv[])
5694 const struct got_error *error = NULL;
5695 struct got_repository *repo = NULL;
5696 struct got_worktree *worktree = NULL;
5697 char *cwd = NULL, *repo_path = NULL;
5698 int ch, do_list = 0, do_show = 0, do_update = 1;
5699 const char *delref = NULL, *commit_id_arg = NULL;
5700 struct got_reference *ref = NULL;
5701 struct got_pathlist_head paths;
5702 struct got_pathlist_entry *pe;
5703 struct got_object_id *commit_id = NULL;
5704 char *commit_id_str = NULL;
5706 TAILQ_INIT(&paths);
5708 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5709 switch (ch) {
5710 case 'c':
5711 commit_id_arg = optarg;
5712 break;
5713 case 'd':
5714 delref = optarg;
5715 break;
5716 case 'r':
5717 repo_path = realpath(optarg, NULL);
5718 if (repo_path == NULL)
5719 return got_error_from_errno2("realpath",
5720 optarg);
5721 got_path_strip_trailing_slashes(repo_path);
5722 break;
5723 case 'l':
5724 do_list = 1;
5725 break;
5726 case 'n':
5727 do_update = 0;
5728 break;
5729 default:
5730 usage_branch();
5731 /* NOTREACHED */
5735 if (do_list && delref)
5736 option_conflict('l', 'd');
5738 argc -= optind;
5739 argv += optind;
5741 if (!do_list && !delref && argc == 0)
5742 do_show = 1;
5744 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5745 errx(1, "-c option can only be used when creating a branch");
5747 if (do_list || delref) {
5748 if (argc > 0)
5749 usage_branch();
5750 } else if (!do_show && argc != 1)
5751 usage_branch();
5753 #ifndef PROFILE
5754 if (do_list || do_show) {
5755 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5756 NULL) == -1)
5757 err(1, "pledge");
5758 } else {
5759 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5760 "sendfd unveil", NULL) == -1)
5761 err(1, "pledge");
5763 #endif
5764 cwd = getcwd(NULL, 0);
5765 if (cwd == NULL) {
5766 error = got_error_from_errno("getcwd");
5767 goto done;
5770 if (repo_path == NULL) {
5771 error = got_worktree_open(&worktree, cwd);
5772 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5773 goto done;
5774 else
5775 error = NULL;
5776 if (worktree) {
5777 repo_path =
5778 strdup(got_worktree_get_repo_path(worktree));
5779 if (repo_path == NULL)
5780 error = got_error_from_errno("strdup");
5781 if (error)
5782 goto done;
5783 } else {
5784 repo_path = strdup(cwd);
5785 if (repo_path == NULL) {
5786 error = got_error_from_errno("strdup");
5787 goto done;
5792 error = got_repo_open(&repo, repo_path, NULL);
5793 if (error != NULL)
5794 goto done;
5796 error = apply_unveil(got_repo_get_path(repo), do_list,
5797 worktree ? got_worktree_get_root_path(worktree) : NULL);
5798 if (error)
5799 goto done;
5801 if (do_show)
5802 error = show_current_branch(repo, worktree);
5803 else if (do_list)
5804 error = list_branches(repo, worktree);
5805 else if (delref)
5806 error = delete_branch(repo, worktree, delref);
5807 else {
5808 struct got_reflist_head refs;
5809 TAILQ_INIT(&refs);
5810 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5811 NULL);
5812 if (error)
5813 goto done;
5814 if (commit_id_arg == NULL)
5815 commit_id_arg = worktree ?
5816 got_worktree_get_head_ref_name(worktree) :
5817 GOT_REF_HEAD;
5818 error = got_repo_match_object_id(&commit_id, NULL,
5819 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5820 got_ref_list_free(&refs);
5821 if (error)
5822 goto done;
5823 error = add_branch(repo, argv[0], commit_id);
5824 if (error)
5825 goto done;
5826 if (worktree && do_update) {
5827 struct got_update_progress_arg upa;
5828 char *branch_refname = NULL;
5830 error = got_object_id_str(&commit_id_str, commit_id);
5831 if (error)
5832 goto done;
5833 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5834 worktree);
5835 if (error)
5836 goto done;
5837 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5838 == -1) {
5839 error = got_error_from_errno("asprintf");
5840 goto done;
5842 error = got_ref_open(&ref, repo, branch_refname, 0);
5843 free(branch_refname);
5844 if (error)
5845 goto done;
5846 error = switch_head_ref(ref, commit_id, worktree,
5847 repo);
5848 if (error)
5849 goto done;
5850 error = got_worktree_set_base_commit_id(worktree, repo,
5851 commit_id);
5852 if (error)
5853 goto done;
5854 memset(&upa, 0, sizeof(upa));
5855 error = got_worktree_checkout_files(worktree, &paths,
5856 repo, update_progress, &upa, check_cancelled,
5857 NULL);
5858 if (error)
5859 goto done;
5860 if (upa.did_something)
5861 printf("Updated to commit %s\n", commit_id_str);
5862 print_update_progress_stats(&upa);
5865 done:
5866 if (ref)
5867 got_ref_close(ref);
5868 if (repo)
5869 got_repo_close(repo);
5870 if (worktree)
5871 got_worktree_close(worktree);
5872 free(cwd);
5873 free(repo_path);
5874 free(commit_id);
5875 free(commit_id_str);
5876 TAILQ_FOREACH(pe, &paths, entry)
5877 free((char *)pe->path);
5878 got_pathlist_free(&paths);
5879 return error;
5883 __dead static void
5884 usage_tag(void)
5886 fprintf(stderr,
5887 "usage: %s tag [-c commit] [-r repository] [-l] "
5888 "[-m message] name\n", getprogname());
5889 exit(1);
5892 #if 0
5893 static const struct got_error *
5894 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5896 const struct got_error *err = NULL;
5897 struct got_reflist_entry *re, *se, *new;
5898 struct got_object_id *re_id, *se_id;
5899 struct got_tag_object *re_tag, *se_tag;
5900 time_t re_time, se_time;
5902 SIMPLEQ_FOREACH(re, tags, entry) {
5903 se = SIMPLEQ_FIRST(sorted);
5904 if (se == NULL) {
5905 err = got_reflist_entry_dup(&new, re);
5906 if (err)
5907 return err;
5908 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5909 continue;
5910 } else {
5911 err = got_ref_resolve(&re_id, repo, re->ref);
5912 if (err)
5913 break;
5914 err = got_object_open_as_tag(&re_tag, repo, re_id);
5915 free(re_id);
5916 if (err)
5917 break;
5918 re_time = got_object_tag_get_tagger_time(re_tag);
5919 got_object_tag_close(re_tag);
5922 while (se) {
5923 err = got_ref_resolve(&se_id, repo, re->ref);
5924 if (err)
5925 break;
5926 err = got_object_open_as_tag(&se_tag, repo, se_id);
5927 free(se_id);
5928 if (err)
5929 break;
5930 se_time = got_object_tag_get_tagger_time(se_tag);
5931 got_object_tag_close(se_tag);
5933 if (se_time > re_time) {
5934 err = got_reflist_entry_dup(&new, re);
5935 if (err)
5936 return err;
5937 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5938 break;
5940 se = SIMPLEQ_NEXT(se, entry);
5941 continue;
5944 done:
5945 return err;
5947 #endif
5949 static const struct got_error *
5950 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5952 static const struct got_error *err = NULL;
5953 struct got_reflist_head refs;
5954 struct got_reflist_entry *re;
5956 TAILQ_INIT(&refs);
5958 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5959 if (err)
5960 return err;
5962 TAILQ_FOREACH(re, &refs, entry) {
5963 const char *refname;
5964 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5965 char datebuf[26];
5966 const char *tagger;
5967 time_t tagger_time;
5968 struct got_object_id *id;
5969 struct got_tag_object *tag;
5970 struct got_commit_object *commit = NULL;
5972 refname = got_ref_get_name(re->ref);
5973 if (strncmp(refname, "refs/tags/", 10) != 0)
5974 continue;
5975 refname += 10;
5976 refstr = got_ref_to_str(re->ref);
5977 if (refstr == NULL) {
5978 err = got_error_from_errno("got_ref_to_str");
5979 break;
5981 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5982 free(refstr);
5984 err = got_ref_resolve(&id, repo, re->ref);
5985 if (err)
5986 break;
5987 err = got_object_open_as_tag(&tag, repo, id);
5988 if (err) {
5989 if (err->code != GOT_ERR_OBJ_TYPE) {
5990 free(id);
5991 break;
5993 /* "lightweight" tag */
5994 err = got_object_open_as_commit(&commit, repo, id);
5995 if (err) {
5996 free(id);
5997 break;
5999 tagger = got_object_commit_get_committer(commit);
6000 tagger_time =
6001 got_object_commit_get_committer_time(commit);
6002 err = got_object_id_str(&id_str, id);
6003 free(id);
6004 if (err)
6005 break;
6006 } else {
6007 free(id);
6008 tagger = got_object_tag_get_tagger(tag);
6009 tagger_time = got_object_tag_get_tagger_time(tag);
6010 err = got_object_id_str(&id_str,
6011 got_object_tag_get_object_id(tag));
6012 if (err)
6013 break;
6015 printf("from: %s\n", tagger);
6016 datestr = get_datestr(&tagger_time, datebuf);
6017 if (datestr)
6018 printf("date: %s UTC\n", datestr);
6019 if (commit)
6020 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6021 else {
6022 switch (got_object_tag_get_object_type(tag)) {
6023 case GOT_OBJ_TYPE_BLOB:
6024 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6025 id_str);
6026 break;
6027 case GOT_OBJ_TYPE_TREE:
6028 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6029 id_str);
6030 break;
6031 case GOT_OBJ_TYPE_COMMIT:
6032 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6033 id_str);
6034 break;
6035 case GOT_OBJ_TYPE_TAG:
6036 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6037 id_str);
6038 break;
6039 default:
6040 break;
6043 free(id_str);
6044 if (commit) {
6045 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6046 if (err)
6047 break;
6048 got_object_commit_close(commit);
6049 } else {
6050 tagmsg0 = strdup(got_object_tag_get_message(tag));
6051 got_object_tag_close(tag);
6052 if (tagmsg0 == NULL) {
6053 err = got_error_from_errno("strdup");
6054 break;
6058 tagmsg = tagmsg0;
6059 do {
6060 line = strsep(&tagmsg, "\n");
6061 if (line)
6062 printf(" %s\n", line);
6063 } while (line);
6064 free(tagmsg0);
6067 got_ref_list_free(&refs);
6068 return NULL;
6071 static const struct got_error *
6072 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6073 const char *tag_name, const char *repo_path)
6075 const struct got_error *err = NULL;
6076 char *template = NULL, *initial_content = NULL;
6077 char *editor = NULL;
6078 int initial_content_len;
6079 int fd = -1;
6081 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6082 err = got_error_from_errno("asprintf");
6083 goto done;
6086 initial_content_len = asprintf(&initial_content,
6087 "\n# tagging commit %s as %s\n",
6088 commit_id_str, tag_name);
6089 if (initial_content_len == -1) {
6090 err = got_error_from_errno("asprintf");
6091 goto done;
6094 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6095 if (err)
6096 goto done;
6098 if (write(fd, initial_content, initial_content_len) == -1) {
6099 err = got_error_from_errno2("write", *tagmsg_path);
6100 goto done;
6103 err = get_editor(&editor);
6104 if (err)
6105 goto done;
6106 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6107 initial_content_len, 1);
6108 done:
6109 free(initial_content);
6110 free(template);
6111 free(editor);
6113 if (fd != -1 && close(fd) == -1 && err == NULL)
6114 err = got_error_from_errno2("close", *tagmsg_path);
6116 /* Editor is done; we can now apply unveil(2) */
6117 if (err == NULL)
6118 err = apply_unveil(repo_path, 0, NULL);
6119 if (err) {
6120 free(*tagmsg);
6121 *tagmsg = NULL;
6123 return err;
6126 static const struct got_error *
6127 add_tag(struct got_repository *repo, struct got_worktree *worktree,
6128 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6130 const struct got_error *err = NULL;
6131 struct got_object_id *commit_id = NULL, *tag_id = NULL;
6132 char *label = NULL, *commit_id_str = NULL;
6133 struct got_reference *ref = NULL;
6134 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6135 char *tagmsg_path = NULL, *tag_id_str = NULL;
6136 int preserve_tagmsg = 0;
6137 struct got_reflist_head refs;
6139 TAILQ_INIT(&refs);
6142 * Don't let the user create a tag name with a leading '-'.
6143 * While technically a valid reference name, this case is usually
6144 * an unintended typo.
6146 if (tag_name[0] == '-')
6147 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6149 err = get_author(&tagger, repo, worktree);
6150 if (err)
6151 return err;
6153 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6154 if (err)
6155 goto done;
6157 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6158 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6159 if (err)
6160 goto done;
6162 err = got_object_id_str(&commit_id_str, commit_id);
6163 if (err)
6164 goto done;
6166 if (strncmp("refs/tags/", tag_name, 10) == 0) {
6167 refname = strdup(tag_name);
6168 if (refname == NULL) {
6169 err = got_error_from_errno("strdup");
6170 goto done;
6172 tag_name += 10;
6173 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6174 err = got_error_from_errno("asprintf");
6175 goto done;
6178 err = got_ref_open(&ref, repo, refname, 0);
6179 if (err == NULL) {
6180 err = got_error(GOT_ERR_TAG_EXISTS);
6181 goto done;
6182 } else if (err->code != GOT_ERR_NOT_REF)
6183 goto done;
6185 if (tagmsg_arg == NULL) {
6186 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6187 tag_name, got_repo_get_path(repo));
6188 if (err) {
6189 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6190 tagmsg_path != NULL)
6191 preserve_tagmsg = 1;
6192 goto done;
6196 err = got_object_tag_create(&tag_id, tag_name, commit_id,
6197 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6198 if (err) {
6199 if (tagmsg_path)
6200 preserve_tagmsg = 1;
6201 goto done;
6204 err = got_ref_alloc(&ref, refname, tag_id);
6205 if (err) {
6206 if (tagmsg_path)
6207 preserve_tagmsg = 1;
6208 goto done;
6211 err = got_ref_write(ref, repo);
6212 if (err) {
6213 if (tagmsg_path)
6214 preserve_tagmsg = 1;
6215 goto done;
6218 err = got_object_id_str(&tag_id_str, tag_id);
6219 if (err) {
6220 if (tagmsg_path)
6221 preserve_tagmsg = 1;
6222 goto done;
6224 printf("Created tag %s\n", tag_id_str);
6225 done:
6226 if (preserve_tagmsg) {
6227 fprintf(stderr, "%s: tag message preserved in %s\n",
6228 getprogname(), tagmsg_path);
6229 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6230 err = got_error_from_errno2("unlink", tagmsg_path);
6231 free(tag_id_str);
6232 if (ref)
6233 got_ref_close(ref);
6234 free(commit_id);
6235 free(commit_id_str);
6236 free(refname);
6237 free(tagmsg);
6238 free(tagmsg_path);
6239 free(tagger);
6240 got_ref_list_free(&refs);
6241 return err;
6244 static const struct got_error *
6245 cmd_tag(int argc, char *argv[])
6247 const struct got_error *error = NULL;
6248 struct got_repository *repo = NULL;
6249 struct got_worktree *worktree = NULL;
6250 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6251 char *gitconfig_path = NULL;
6252 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6253 int ch, do_list = 0;
6255 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6256 switch (ch) {
6257 case 'c':
6258 commit_id_arg = optarg;
6259 break;
6260 case 'm':
6261 tagmsg = optarg;
6262 break;
6263 case 'r':
6264 repo_path = realpath(optarg, NULL);
6265 if (repo_path == NULL)
6266 return got_error_from_errno2("realpath",
6267 optarg);
6268 got_path_strip_trailing_slashes(repo_path);
6269 break;
6270 case 'l':
6271 do_list = 1;
6272 break;
6273 default:
6274 usage_tag();
6275 /* NOTREACHED */
6279 argc -= optind;
6280 argv += optind;
6282 if (do_list) {
6283 if (commit_id_arg != NULL)
6284 errx(1,
6285 "-c option can only be used when creating a tag");
6286 if (tagmsg)
6287 option_conflict('l', 'm');
6288 if (argc > 0)
6289 usage_tag();
6290 } else if (argc != 1)
6291 usage_tag();
6293 tag_name = argv[0];
6295 #ifndef PROFILE
6296 if (do_list) {
6297 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6298 NULL) == -1)
6299 err(1, "pledge");
6300 } else {
6301 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6302 "sendfd unveil", NULL) == -1)
6303 err(1, "pledge");
6305 #endif
6306 cwd = getcwd(NULL, 0);
6307 if (cwd == NULL) {
6308 error = got_error_from_errno("getcwd");
6309 goto done;
6312 if (repo_path == NULL) {
6313 error = got_worktree_open(&worktree, cwd);
6314 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6315 goto done;
6316 else
6317 error = NULL;
6318 if (worktree) {
6319 repo_path =
6320 strdup(got_worktree_get_repo_path(worktree));
6321 if (repo_path == NULL)
6322 error = got_error_from_errno("strdup");
6323 if (error)
6324 goto done;
6325 } else {
6326 repo_path = strdup(cwd);
6327 if (repo_path == NULL) {
6328 error = got_error_from_errno("strdup");
6329 goto done;
6334 if (do_list) {
6335 error = got_repo_open(&repo, repo_path, NULL);
6336 if (error != NULL)
6337 goto done;
6338 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6339 if (error)
6340 goto done;
6341 error = list_tags(repo, worktree);
6342 } else {
6343 error = get_gitconfig_path(&gitconfig_path);
6344 if (error)
6345 goto done;
6346 error = got_repo_open(&repo, repo_path, gitconfig_path);
6347 if (error != NULL)
6348 goto done;
6350 if (tagmsg) {
6351 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6352 if (error)
6353 goto done;
6356 if (commit_id_arg == NULL) {
6357 struct got_reference *head_ref;
6358 struct got_object_id *commit_id;
6359 error = got_ref_open(&head_ref, repo,
6360 worktree ? got_worktree_get_head_ref_name(worktree)
6361 : GOT_REF_HEAD, 0);
6362 if (error)
6363 goto done;
6364 error = got_ref_resolve(&commit_id, repo, head_ref);
6365 got_ref_close(head_ref);
6366 if (error)
6367 goto done;
6368 error = got_object_id_str(&commit_id_str, commit_id);
6369 free(commit_id);
6370 if (error)
6371 goto done;
6374 error = add_tag(repo, worktree, tag_name,
6375 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6377 done:
6378 if (repo)
6379 got_repo_close(repo);
6380 if (worktree)
6381 got_worktree_close(worktree);
6382 free(cwd);
6383 free(repo_path);
6384 free(gitconfig_path);
6385 free(commit_id_str);
6386 return error;
6389 __dead static void
6390 usage_add(void)
6392 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6393 getprogname());
6394 exit(1);
6397 static const struct got_error *
6398 add_progress(void *arg, unsigned char status, const char *path)
6400 while (path[0] == '/')
6401 path++;
6402 printf("%c %s\n", status, path);
6403 return NULL;
6406 static const struct got_error *
6407 cmd_add(int argc, char *argv[])
6409 const struct got_error *error = NULL;
6410 struct got_repository *repo = NULL;
6411 struct got_worktree *worktree = NULL;
6412 char *cwd = NULL;
6413 struct got_pathlist_head paths;
6414 struct got_pathlist_entry *pe;
6415 int ch, can_recurse = 0, no_ignores = 0;
6417 TAILQ_INIT(&paths);
6419 while ((ch = getopt(argc, argv, "IR")) != -1) {
6420 switch (ch) {
6421 case 'I':
6422 no_ignores = 1;
6423 break;
6424 case 'R':
6425 can_recurse = 1;
6426 break;
6427 default:
6428 usage_add();
6429 /* NOTREACHED */
6433 argc -= optind;
6434 argv += optind;
6436 #ifndef PROFILE
6437 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6438 NULL) == -1)
6439 err(1, "pledge");
6440 #endif
6441 if (argc < 1)
6442 usage_add();
6444 cwd = getcwd(NULL, 0);
6445 if (cwd == NULL) {
6446 error = got_error_from_errno("getcwd");
6447 goto done;
6450 error = got_worktree_open(&worktree, cwd);
6451 if (error) {
6452 if (error->code == GOT_ERR_NOT_WORKTREE)
6453 error = wrap_not_worktree_error(error, "add", cwd);
6454 goto done;
6457 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6458 NULL);
6459 if (error != NULL)
6460 goto done;
6462 error = apply_unveil(got_repo_get_path(repo), 1,
6463 got_worktree_get_root_path(worktree));
6464 if (error)
6465 goto done;
6467 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6468 if (error)
6469 goto done;
6471 if (!can_recurse && no_ignores) {
6472 error = got_error_msg(GOT_ERR_BAD_PATH,
6473 "disregarding ignores requires -R option");
6474 goto done;
6478 if (!can_recurse) {
6479 char *ondisk_path;
6480 struct stat sb;
6481 TAILQ_FOREACH(pe, &paths, entry) {
6482 if (asprintf(&ondisk_path, "%s/%s",
6483 got_worktree_get_root_path(worktree),
6484 pe->path) == -1) {
6485 error = got_error_from_errno("asprintf");
6486 goto done;
6488 if (lstat(ondisk_path, &sb) == -1) {
6489 if (errno == ENOENT) {
6490 free(ondisk_path);
6491 continue;
6493 error = got_error_from_errno2("lstat",
6494 ondisk_path);
6495 free(ondisk_path);
6496 goto done;
6498 free(ondisk_path);
6499 if (S_ISDIR(sb.st_mode)) {
6500 error = got_error_msg(GOT_ERR_BAD_PATH,
6501 "adding directories requires -R option");
6502 goto done;
6507 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6508 NULL, repo, no_ignores);
6509 done:
6510 if (repo)
6511 got_repo_close(repo);
6512 if (worktree)
6513 got_worktree_close(worktree);
6514 TAILQ_FOREACH(pe, &paths, entry)
6515 free((char *)pe->path);
6516 got_pathlist_free(&paths);
6517 free(cwd);
6518 return error;
6521 __dead static void
6522 usage_remove(void)
6524 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6525 "path ...\n", getprogname());
6526 exit(1);
6529 static const struct got_error *
6530 print_remove_status(void *arg, unsigned char status,
6531 unsigned char staged_status, const char *path)
6533 while (path[0] == '/')
6534 path++;
6535 if (status == GOT_STATUS_NONEXISTENT)
6536 return NULL;
6537 if (status == staged_status && (status == GOT_STATUS_DELETE))
6538 status = GOT_STATUS_NO_CHANGE;
6539 printf("%c%c %s\n", status, staged_status, path);
6540 return NULL;
6543 static const struct got_error *
6544 cmd_remove(int argc, char *argv[])
6546 const struct got_error *error = NULL;
6547 struct got_worktree *worktree = NULL;
6548 struct got_repository *repo = NULL;
6549 const char *status_codes = NULL;
6550 char *cwd = NULL;
6551 struct got_pathlist_head paths;
6552 struct got_pathlist_entry *pe;
6553 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6555 TAILQ_INIT(&paths);
6557 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6558 switch (ch) {
6559 case 'f':
6560 delete_local_mods = 1;
6561 break;
6562 case 'k':
6563 keep_on_disk = 1;
6564 break;
6565 case 'R':
6566 can_recurse = 1;
6567 break;
6568 case 's':
6569 for (i = 0; i < strlen(optarg); i++) {
6570 switch (optarg[i]) {
6571 case GOT_STATUS_MODIFY:
6572 delete_local_mods = 1;
6573 break;
6574 case GOT_STATUS_MISSING:
6575 break;
6576 default:
6577 errx(1, "invalid status code '%c'",
6578 optarg[i]);
6581 status_codes = optarg;
6582 break;
6583 default:
6584 usage_remove();
6585 /* NOTREACHED */
6589 argc -= optind;
6590 argv += optind;
6592 #ifndef PROFILE
6593 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6594 NULL) == -1)
6595 err(1, "pledge");
6596 #endif
6597 if (argc < 1)
6598 usage_remove();
6600 cwd = getcwd(NULL, 0);
6601 if (cwd == NULL) {
6602 error = got_error_from_errno("getcwd");
6603 goto done;
6605 error = got_worktree_open(&worktree, cwd);
6606 if (error) {
6607 if (error->code == GOT_ERR_NOT_WORKTREE)
6608 error = wrap_not_worktree_error(error, "remove", cwd);
6609 goto done;
6612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6613 NULL);
6614 if (error)
6615 goto done;
6617 error = apply_unveil(got_repo_get_path(repo), 1,
6618 got_worktree_get_root_path(worktree));
6619 if (error)
6620 goto done;
6622 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6623 if (error)
6624 goto done;
6626 if (!can_recurse) {
6627 char *ondisk_path;
6628 struct stat sb;
6629 TAILQ_FOREACH(pe, &paths, entry) {
6630 if (asprintf(&ondisk_path, "%s/%s",
6631 got_worktree_get_root_path(worktree),
6632 pe->path) == -1) {
6633 error = got_error_from_errno("asprintf");
6634 goto done;
6636 if (lstat(ondisk_path, &sb) == -1) {
6637 if (errno == ENOENT) {
6638 free(ondisk_path);
6639 continue;
6641 error = got_error_from_errno2("lstat",
6642 ondisk_path);
6643 free(ondisk_path);
6644 goto done;
6646 free(ondisk_path);
6647 if (S_ISDIR(sb.st_mode)) {
6648 error = got_error_msg(GOT_ERR_BAD_PATH,
6649 "removing directories requires -R option");
6650 goto done;
6655 error = got_worktree_schedule_delete(worktree, &paths,
6656 delete_local_mods, status_codes, print_remove_status, NULL,
6657 repo, keep_on_disk);
6658 done:
6659 if (repo)
6660 got_repo_close(repo);
6661 if (worktree)
6662 got_worktree_close(worktree);
6663 TAILQ_FOREACH(pe, &paths, entry)
6664 free((char *)pe->path);
6665 got_pathlist_free(&paths);
6666 free(cwd);
6667 return error;
6670 __dead static void
6671 usage_revert(void)
6673 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6674 "path ...\n", getprogname());
6675 exit(1);
6678 static const struct got_error *
6679 revert_progress(void *arg, unsigned char status, const char *path)
6681 if (status == GOT_STATUS_UNVERSIONED)
6682 return NULL;
6684 while (path[0] == '/')
6685 path++;
6686 printf("%c %s\n", status, path);
6687 return NULL;
6690 struct choose_patch_arg {
6691 FILE *patch_script_file;
6692 const char *action;
6695 static const struct got_error *
6696 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6697 int nchanges, const char *action)
6699 char *line = NULL;
6700 size_t linesize = 0;
6701 ssize_t linelen;
6703 switch (status) {
6704 case GOT_STATUS_ADD:
6705 printf("A %s\n%s this addition? [y/n] ", path, action);
6706 break;
6707 case GOT_STATUS_DELETE:
6708 printf("D %s\n%s this deletion? [y/n] ", path, action);
6709 break;
6710 case GOT_STATUS_MODIFY:
6711 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6712 return got_error_from_errno("fseek");
6713 printf(GOT_COMMIT_SEP_STR);
6714 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6715 printf("%s", line);
6716 if (ferror(patch_file))
6717 return got_error_from_errno("getline");
6718 printf(GOT_COMMIT_SEP_STR);
6719 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6720 path, n, nchanges, action);
6721 break;
6722 default:
6723 return got_error_path(path, GOT_ERR_FILE_STATUS);
6726 return NULL;
6729 static const struct got_error *
6730 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6731 FILE *patch_file, int n, int nchanges)
6733 const struct got_error *err = NULL;
6734 char *line = NULL;
6735 size_t linesize = 0;
6736 ssize_t linelen;
6737 int resp = ' ';
6738 struct choose_patch_arg *a = arg;
6740 *choice = GOT_PATCH_CHOICE_NONE;
6742 if (a->patch_script_file) {
6743 char *nl;
6744 err = show_change(status, path, patch_file, n, nchanges,
6745 a->action);
6746 if (err)
6747 return err;
6748 linelen = getline(&line, &linesize, a->patch_script_file);
6749 if (linelen == -1) {
6750 if (ferror(a->patch_script_file))
6751 return got_error_from_errno("getline");
6752 return NULL;
6754 nl = strchr(line, '\n');
6755 if (nl)
6756 *nl = '\0';
6757 if (strcmp(line, "y") == 0) {
6758 *choice = GOT_PATCH_CHOICE_YES;
6759 printf("y\n");
6760 } else if (strcmp(line, "n") == 0) {
6761 *choice = GOT_PATCH_CHOICE_NO;
6762 printf("n\n");
6763 } else if (strcmp(line, "q") == 0 &&
6764 status == GOT_STATUS_MODIFY) {
6765 *choice = GOT_PATCH_CHOICE_QUIT;
6766 printf("q\n");
6767 } else
6768 printf("invalid response '%s'\n", line);
6769 free(line);
6770 return NULL;
6773 while (resp != 'y' && resp != 'n' && resp != 'q') {
6774 err = show_change(status, path, patch_file, n, nchanges,
6775 a->action);
6776 if (err)
6777 return err;
6778 resp = getchar();
6779 if (resp == '\n')
6780 resp = getchar();
6781 if (status == GOT_STATUS_MODIFY) {
6782 if (resp != 'y' && resp != 'n' && resp != 'q') {
6783 printf("invalid response '%c'\n", resp);
6784 resp = ' ';
6786 } else if (resp != 'y' && resp != 'n') {
6787 printf("invalid response '%c'\n", resp);
6788 resp = ' ';
6792 if (resp == 'y')
6793 *choice = GOT_PATCH_CHOICE_YES;
6794 else if (resp == 'n')
6795 *choice = GOT_PATCH_CHOICE_NO;
6796 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6797 *choice = GOT_PATCH_CHOICE_QUIT;
6799 return NULL;
6803 static const struct got_error *
6804 cmd_revert(int argc, char *argv[])
6806 const struct got_error *error = NULL;
6807 struct got_worktree *worktree = NULL;
6808 struct got_repository *repo = NULL;
6809 char *cwd = NULL, *path = NULL;
6810 struct got_pathlist_head paths;
6811 struct got_pathlist_entry *pe;
6812 int ch, can_recurse = 0, pflag = 0;
6813 FILE *patch_script_file = NULL;
6814 const char *patch_script_path = NULL;
6815 struct choose_patch_arg cpa;
6817 TAILQ_INIT(&paths);
6819 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6820 switch (ch) {
6821 case 'p':
6822 pflag = 1;
6823 break;
6824 case 'F':
6825 patch_script_path = optarg;
6826 break;
6827 case 'R':
6828 can_recurse = 1;
6829 break;
6830 default:
6831 usage_revert();
6832 /* NOTREACHED */
6836 argc -= optind;
6837 argv += optind;
6839 #ifndef PROFILE
6840 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6841 "unveil", NULL) == -1)
6842 err(1, "pledge");
6843 #endif
6844 if (argc < 1)
6845 usage_revert();
6846 if (patch_script_path && !pflag)
6847 errx(1, "-F option can only be used together with -p option");
6849 cwd = getcwd(NULL, 0);
6850 if (cwd == NULL) {
6851 error = got_error_from_errno("getcwd");
6852 goto done;
6854 error = got_worktree_open(&worktree, cwd);
6855 if (error) {
6856 if (error->code == GOT_ERR_NOT_WORKTREE)
6857 error = wrap_not_worktree_error(error, "revert", cwd);
6858 goto done;
6861 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6862 NULL);
6863 if (error != NULL)
6864 goto done;
6866 if (patch_script_path) {
6867 patch_script_file = fopen(patch_script_path, "r");
6868 if (patch_script_file == NULL) {
6869 error = got_error_from_errno2("fopen",
6870 patch_script_path);
6871 goto done;
6874 error = apply_unveil(got_repo_get_path(repo), 1,
6875 got_worktree_get_root_path(worktree));
6876 if (error)
6877 goto done;
6879 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6880 if (error)
6881 goto done;
6883 if (!can_recurse) {
6884 char *ondisk_path;
6885 struct stat sb;
6886 TAILQ_FOREACH(pe, &paths, entry) {
6887 if (asprintf(&ondisk_path, "%s/%s",
6888 got_worktree_get_root_path(worktree),
6889 pe->path) == -1) {
6890 error = got_error_from_errno("asprintf");
6891 goto done;
6893 if (lstat(ondisk_path, &sb) == -1) {
6894 if (errno == ENOENT) {
6895 free(ondisk_path);
6896 continue;
6898 error = got_error_from_errno2("lstat",
6899 ondisk_path);
6900 free(ondisk_path);
6901 goto done;
6903 free(ondisk_path);
6904 if (S_ISDIR(sb.st_mode)) {
6905 error = got_error_msg(GOT_ERR_BAD_PATH,
6906 "reverting directories requires -R option");
6907 goto done;
6912 cpa.patch_script_file = patch_script_file;
6913 cpa.action = "revert";
6914 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6915 pflag ? choose_patch : NULL, &cpa, repo);
6916 done:
6917 if (patch_script_file && fclose(patch_script_file) == EOF &&
6918 error == NULL)
6919 error = got_error_from_errno2("fclose", patch_script_path);
6920 if (repo)
6921 got_repo_close(repo);
6922 if (worktree)
6923 got_worktree_close(worktree);
6924 free(path);
6925 free(cwd);
6926 return error;
6929 __dead static void
6930 usage_commit(void)
6932 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6933 getprogname());
6934 exit(1);
6937 struct collect_commit_logmsg_arg {
6938 const char *cmdline_log;
6939 const char *editor;
6940 const char *worktree_path;
6941 const char *branch_name;
6942 const char *repo_path;
6943 char *logmsg_path;
6947 static const struct got_error *
6948 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6949 void *arg)
6951 char *initial_content = NULL;
6952 struct got_pathlist_entry *pe;
6953 const struct got_error *err = NULL;
6954 char *template = NULL;
6955 struct collect_commit_logmsg_arg *a = arg;
6956 int initial_content_len;
6957 int fd = -1;
6958 size_t len;
6960 /* if a message was specified on the command line, just use it */
6961 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6962 len = strlen(a->cmdline_log) + 1;
6963 *logmsg = malloc(len + 1);
6964 if (*logmsg == NULL)
6965 return got_error_from_errno("malloc");
6966 strlcpy(*logmsg, a->cmdline_log, len);
6967 return NULL;
6970 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6971 return got_error_from_errno("asprintf");
6973 initial_content_len = asprintf(&initial_content,
6974 "\n# changes to be committed on branch %s:\n",
6975 a->branch_name);
6976 if (initial_content_len == -1)
6977 return got_error_from_errno("asprintf");
6979 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6980 if (err)
6981 goto done;
6983 if (write(fd, initial_content, initial_content_len) == -1) {
6984 err = got_error_from_errno2("write", a->logmsg_path);
6985 goto done;
6988 TAILQ_FOREACH(pe, commitable_paths, entry) {
6989 struct got_commitable *ct = pe->data;
6990 dprintf(fd, "# %c %s\n",
6991 got_commitable_get_status(ct),
6992 got_commitable_get_path(ct));
6995 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6996 initial_content_len, 1);
6997 done:
6998 free(initial_content);
6999 free(template);
7001 if (fd != -1 && close(fd) == -1 && err == NULL)
7002 err = got_error_from_errno2("close", a->logmsg_path);
7004 /* Editor is done; we can now apply unveil(2) */
7005 if (err == NULL)
7006 err = apply_unveil(a->repo_path, 0, a->worktree_path);
7007 if (err) {
7008 free(*logmsg);
7009 *logmsg = NULL;
7011 return err;
7014 static const struct got_error *
7015 cmd_commit(int argc, char *argv[])
7017 const struct got_error *error = NULL;
7018 struct got_worktree *worktree = NULL;
7019 struct got_repository *repo = NULL;
7020 char *cwd = NULL, *id_str = NULL;
7021 struct got_object_id *id = NULL;
7022 const char *logmsg = NULL;
7023 struct collect_commit_logmsg_arg cl_arg;
7024 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7025 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7026 int allow_bad_symlinks = 0;
7027 struct got_pathlist_head paths;
7029 TAILQ_INIT(&paths);
7030 cl_arg.logmsg_path = NULL;
7032 while ((ch = getopt(argc, argv, "m:S")) != -1) {
7033 switch (ch) {
7034 case 'm':
7035 logmsg = optarg;
7036 break;
7037 case 'S':
7038 allow_bad_symlinks = 1;
7039 break;
7040 default:
7041 usage_commit();
7042 /* NOTREACHED */
7046 argc -= optind;
7047 argv += optind;
7049 #ifndef PROFILE
7050 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7051 "unveil", NULL) == -1)
7052 err(1, "pledge");
7053 #endif
7054 cwd = getcwd(NULL, 0);
7055 if (cwd == NULL) {
7056 error = got_error_from_errno("getcwd");
7057 goto done;
7059 error = got_worktree_open(&worktree, cwd);
7060 if (error) {
7061 if (error->code == GOT_ERR_NOT_WORKTREE)
7062 error = wrap_not_worktree_error(error, "commit", cwd);
7063 goto done;
7066 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7067 if (error)
7068 goto done;
7069 if (rebase_in_progress) {
7070 error = got_error(GOT_ERR_REBASING);
7071 goto done;
7074 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7075 worktree);
7076 if (error)
7077 goto done;
7079 error = get_gitconfig_path(&gitconfig_path);
7080 if (error)
7081 goto done;
7082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7083 gitconfig_path);
7084 if (error != NULL)
7085 goto done;
7087 error = get_author(&author, repo, worktree);
7088 if (error)
7089 return error;
7092 * unveil(2) traverses exec(2); if an editor is used we have
7093 * to apply unveil after the log message has been written.
7095 if (logmsg == NULL || strlen(logmsg) == 0)
7096 error = get_editor(&editor);
7097 else
7098 error = apply_unveil(got_repo_get_path(repo), 0,
7099 got_worktree_get_root_path(worktree));
7100 if (error)
7101 goto done;
7103 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7104 if (error)
7105 goto done;
7107 cl_arg.editor = editor;
7108 cl_arg.cmdline_log = logmsg;
7109 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7110 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7111 if (!histedit_in_progress) {
7112 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7113 error = got_error(GOT_ERR_COMMIT_BRANCH);
7114 goto done;
7116 cl_arg.branch_name += 11;
7118 cl_arg.repo_path = got_repo_get_path(repo);
7119 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7120 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7121 print_status, NULL, repo);
7122 if (error) {
7123 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7124 cl_arg.logmsg_path != NULL)
7125 preserve_logmsg = 1;
7126 goto done;
7129 error = got_object_id_str(&id_str, id);
7130 if (error)
7131 goto done;
7132 printf("Created commit %s\n", id_str);
7133 done:
7134 if (preserve_logmsg) {
7135 fprintf(stderr, "%s: log message preserved in %s\n",
7136 getprogname(), cl_arg.logmsg_path);
7137 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7138 error == NULL)
7139 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7140 free(cl_arg.logmsg_path);
7141 if (repo)
7142 got_repo_close(repo);
7143 if (worktree)
7144 got_worktree_close(worktree);
7145 free(cwd);
7146 free(id_str);
7147 free(gitconfig_path);
7148 free(editor);
7149 free(author);
7150 return error;
7153 __dead static void
7154 usage_cherrypick(void)
7156 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7157 exit(1);
7160 static const struct got_error *
7161 cmd_cherrypick(int argc, char *argv[])
7163 const struct got_error *error = NULL;
7164 struct got_worktree *worktree = NULL;
7165 struct got_repository *repo = NULL;
7166 char *cwd = NULL, *commit_id_str = NULL;
7167 struct got_object_id *commit_id = NULL;
7168 struct got_commit_object *commit = NULL;
7169 struct got_object_qid *pid;
7170 struct got_reference *head_ref = NULL;
7171 int ch;
7172 struct got_update_progress_arg upa;
7174 while ((ch = getopt(argc, argv, "")) != -1) {
7175 switch (ch) {
7176 default:
7177 usage_cherrypick();
7178 /* NOTREACHED */
7182 argc -= optind;
7183 argv += optind;
7185 #ifndef PROFILE
7186 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7187 "unveil", NULL) == -1)
7188 err(1, "pledge");
7189 #endif
7190 if (argc != 1)
7191 usage_cherrypick();
7193 cwd = getcwd(NULL, 0);
7194 if (cwd == NULL) {
7195 error = got_error_from_errno("getcwd");
7196 goto done;
7198 error = got_worktree_open(&worktree, cwd);
7199 if (error) {
7200 if (error->code == GOT_ERR_NOT_WORKTREE)
7201 error = wrap_not_worktree_error(error, "cherrypick",
7202 cwd);
7203 goto done;
7206 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7207 NULL);
7208 if (error != NULL)
7209 goto done;
7211 error = apply_unveil(got_repo_get_path(repo), 0,
7212 got_worktree_get_root_path(worktree));
7213 if (error)
7214 goto done;
7216 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7217 GOT_OBJ_TYPE_COMMIT, repo);
7218 if (error != NULL) {
7219 struct got_reference *ref;
7220 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7221 goto done;
7222 error = got_ref_open(&ref, repo, argv[0], 0);
7223 if (error != NULL)
7224 goto done;
7225 error = got_ref_resolve(&commit_id, repo, ref);
7226 got_ref_close(ref);
7227 if (error != NULL)
7228 goto done;
7230 error = got_object_id_str(&commit_id_str, commit_id);
7231 if (error)
7232 goto done;
7234 error = got_ref_open(&head_ref, repo,
7235 got_worktree_get_head_ref_name(worktree), 0);
7236 if (error != NULL)
7237 goto done;
7239 error = check_same_branch(commit_id, head_ref, NULL, repo);
7240 if (error) {
7241 if (error->code != GOT_ERR_ANCESTRY)
7242 goto done;
7243 error = NULL;
7244 } else {
7245 error = got_error(GOT_ERR_SAME_BRANCH);
7246 goto done;
7249 error = got_object_open_as_commit(&commit, repo, commit_id);
7250 if (error)
7251 goto done;
7252 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7253 memset(&upa, 0, sizeof(upa));
7254 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7255 commit_id, repo, update_progress, &upa, check_cancelled,
7256 NULL);
7257 if (error != NULL)
7258 goto done;
7260 if (upa.did_something)
7261 printf("Merged commit %s\n", commit_id_str);
7262 print_update_progress_stats(&upa);
7263 done:
7264 if (commit)
7265 got_object_commit_close(commit);
7266 free(commit_id_str);
7267 if (head_ref)
7268 got_ref_close(head_ref);
7269 if (worktree)
7270 got_worktree_close(worktree);
7271 if (repo)
7272 got_repo_close(repo);
7273 return error;
7276 __dead static void
7277 usage_backout(void)
7279 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7280 exit(1);
7283 static const struct got_error *
7284 cmd_backout(int argc, char *argv[])
7286 const struct got_error *error = NULL;
7287 struct got_worktree *worktree = NULL;
7288 struct got_repository *repo = NULL;
7289 char *cwd = NULL, *commit_id_str = NULL;
7290 struct got_object_id *commit_id = NULL;
7291 struct got_commit_object *commit = NULL;
7292 struct got_object_qid *pid;
7293 struct got_reference *head_ref = NULL;
7294 int ch;
7295 struct got_update_progress_arg upa;
7297 while ((ch = getopt(argc, argv, "")) != -1) {
7298 switch (ch) {
7299 default:
7300 usage_backout();
7301 /* NOTREACHED */
7305 argc -= optind;
7306 argv += optind;
7308 #ifndef PROFILE
7309 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7310 "unveil", NULL) == -1)
7311 err(1, "pledge");
7312 #endif
7313 if (argc != 1)
7314 usage_backout();
7316 cwd = getcwd(NULL, 0);
7317 if (cwd == NULL) {
7318 error = got_error_from_errno("getcwd");
7319 goto done;
7321 error = got_worktree_open(&worktree, cwd);
7322 if (error) {
7323 if (error->code == GOT_ERR_NOT_WORKTREE)
7324 error = wrap_not_worktree_error(error, "backout", cwd);
7325 goto done;
7328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7329 NULL);
7330 if (error != NULL)
7331 goto done;
7333 error = apply_unveil(got_repo_get_path(repo), 0,
7334 got_worktree_get_root_path(worktree));
7335 if (error)
7336 goto done;
7338 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7339 GOT_OBJ_TYPE_COMMIT, repo);
7340 if (error != NULL) {
7341 struct got_reference *ref;
7342 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7343 goto done;
7344 error = got_ref_open(&ref, repo, argv[0], 0);
7345 if (error != NULL)
7346 goto done;
7347 error = got_ref_resolve(&commit_id, repo, ref);
7348 got_ref_close(ref);
7349 if (error != NULL)
7350 goto done;
7352 error = got_object_id_str(&commit_id_str, commit_id);
7353 if (error)
7354 goto done;
7356 error = got_ref_open(&head_ref, repo,
7357 got_worktree_get_head_ref_name(worktree), 0);
7358 if (error != NULL)
7359 goto done;
7361 error = check_same_branch(commit_id, head_ref, NULL, repo);
7362 if (error)
7363 goto done;
7365 error = got_object_open_as_commit(&commit, repo, commit_id);
7366 if (error)
7367 goto done;
7368 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7369 if (pid == NULL) {
7370 error = got_error(GOT_ERR_ROOT_COMMIT);
7371 goto done;
7374 memset(&upa, 0, sizeof(upa));
7375 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7376 update_progress, &upa, check_cancelled, NULL);
7377 if (error != NULL)
7378 goto done;
7380 if (upa.did_something)
7381 printf("Backed out commit %s\n", commit_id_str);
7382 print_update_progress_stats(&upa);
7383 done:
7384 if (commit)
7385 got_object_commit_close(commit);
7386 free(commit_id_str);
7387 if (head_ref)
7388 got_ref_close(head_ref);
7389 if (worktree)
7390 got_worktree_close(worktree);
7391 if (repo)
7392 got_repo_close(repo);
7393 return error;
7396 __dead static void
7397 usage_rebase(void)
7399 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7400 getprogname());
7401 exit(1);
7404 void
7405 trim_logmsg(char *logmsg, int limit)
7407 char *nl;
7408 size_t len;
7410 len = strlen(logmsg);
7411 if (len > limit)
7412 len = limit;
7413 logmsg[len] = '\0';
7414 nl = strchr(logmsg, '\n');
7415 if (nl)
7416 *nl = '\0';
7419 static const struct got_error *
7420 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7422 const struct got_error *err;
7423 char *logmsg0 = NULL;
7424 const char *s;
7426 err = got_object_commit_get_logmsg(&logmsg0, commit);
7427 if (err)
7428 return err;
7430 s = logmsg0;
7431 while (isspace((unsigned char)s[0]))
7432 s++;
7434 *logmsg = strdup(s);
7435 if (*logmsg == NULL) {
7436 err = got_error_from_errno("strdup");
7437 goto done;
7440 trim_logmsg(*logmsg, limit);
7441 done:
7442 free(logmsg0);
7443 return err;
7446 static const struct got_error *
7447 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7449 const struct got_error *err;
7450 struct got_commit_object *commit = NULL;
7451 char *id_str = NULL, *logmsg = NULL;
7453 err = got_object_open_as_commit(&commit, repo, id);
7454 if (err)
7455 return err;
7457 err = got_object_id_str(&id_str, id);
7458 if (err)
7459 goto done;
7461 id_str[12] = '\0';
7463 err = get_short_logmsg(&logmsg, 42, commit);
7464 if (err)
7465 goto done;
7467 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7468 done:
7469 free(id_str);
7470 got_object_commit_close(commit);
7471 free(logmsg);
7472 return err;
7475 static const struct got_error *
7476 show_rebase_progress(struct got_commit_object *commit,
7477 struct got_object_id *old_id, struct got_object_id *new_id)
7479 const struct got_error *err;
7480 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7482 err = got_object_id_str(&old_id_str, old_id);
7483 if (err)
7484 goto done;
7486 if (new_id) {
7487 err = got_object_id_str(&new_id_str, new_id);
7488 if (err)
7489 goto done;
7492 old_id_str[12] = '\0';
7493 if (new_id_str)
7494 new_id_str[12] = '\0';
7496 err = get_short_logmsg(&logmsg, 42, commit);
7497 if (err)
7498 goto done;
7500 printf("%s -> %s: %s\n", old_id_str,
7501 new_id_str ? new_id_str : "no-op change", logmsg);
7502 done:
7503 free(old_id_str);
7504 free(new_id_str);
7505 free(logmsg);
7506 return err;
7509 static const struct got_error *
7510 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7511 struct got_reference *branch, struct got_reference *new_base_branch,
7512 struct got_reference *tmp_branch, struct got_repository *repo)
7514 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7515 return got_worktree_rebase_complete(worktree, fileindex,
7516 new_base_branch, tmp_branch, branch, repo);
7519 static const struct got_error *
7520 rebase_commit(struct got_pathlist_head *merged_paths,
7521 struct got_worktree *worktree, struct got_fileindex *fileindex,
7522 struct got_reference *tmp_branch,
7523 struct got_object_id *commit_id, struct got_repository *repo)
7525 const struct got_error *error;
7526 struct got_commit_object *commit;
7527 struct got_object_id *new_commit_id;
7529 error = got_object_open_as_commit(&commit, repo, commit_id);
7530 if (error)
7531 return error;
7533 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7534 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7535 if (error) {
7536 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7537 goto done;
7538 error = show_rebase_progress(commit, commit_id, NULL);
7539 } else {
7540 error = show_rebase_progress(commit, commit_id, new_commit_id);
7541 free(new_commit_id);
7543 done:
7544 got_object_commit_close(commit);
7545 return error;
7548 struct check_path_prefix_arg {
7549 const char *path_prefix;
7550 size_t len;
7551 int errcode;
7554 static const struct got_error *
7555 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7556 struct got_blob_object *blob2, struct got_object_id *id1,
7557 struct got_object_id *id2, const char *path1, const char *path2,
7558 mode_t mode1, mode_t mode2, struct got_repository *repo)
7560 struct check_path_prefix_arg *a = arg;
7562 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7563 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7564 return got_error(a->errcode);
7566 return NULL;
7569 static const struct got_error *
7570 check_path_prefix(struct got_object_id *parent_id,
7571 struct got_object_id *commit_id, const char *path_prefix,
7572 int errcode, struct got_repository *repo)
7574 const struct got_error *err;
7575 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7576 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7577 struct check_path_prefix_arg cpp_arg;
7579 if (got_path_is_root_dir(path_prefix))
7580 return NULL;
7582 err = got_object_open_as_commit(&commit, repo, commit_id);
7583 if (err)
7584 goto done;
7586 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7587 if (err)
7588 goto done;
7590 err = got_object_open_as_tree(&tree1, repo,
7591 got_object_commit_get_tree_id(parent_commit));
7592 if (err)
7593 goto done;
7595 err = got_object_open_as_tree(&tree2, repo,
7596 got_object_commit_get_tree_id(commit));
7597 if (err)
7598 goto done;
7600 cpp_arg.path_prefix = path_prefix;
7601 while (cpp_arg.path_prefix[0] == '/')
7602 cpp_arg.path_prefix++;
7603 cpp_arg.len = strlen(cpp_arg.path_prefix);
7604 cpp_arg.errcode = errcode;
7605 err = got_diff_tree(tree1, tree2, "", "", repo,
7606 check_path_prefix_in_diff, &cpp_arg, 0);
7607 done:
7608 if (tree1)
7609 got_object_tree_close(tree1);
7610 if (tree2)
7611 got_object_tree_close(tree2);
7612 if (commit)
7613 got_object_commit_close(commit);
7614 if (parent_commit)
7615 got_object_commit_close(parent_commit);
7616 return err;
7619 static const struct got_error *
7620 collect_commits(struct got_object_id_queue *commits,
7621 struct got_object_id *initial_commit_id,
7622 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7623 const char *path_prefix, int path_prefix_errcode,
7624 struct got_repository *repo)
7626 const struct got_error *err = NULL;
7627 struct got_commit_graph *graph = NULL;
7628 struct got_object_id *parent_id = NULL;
7629 struct got_object_qid *qid;
7630 struct got_object_id *commit_id = initial_commit_id;
7632 err = got_commit_graph_open(&graph, "/", 1);
7633 if (err)
7634 return err;
7636 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7637 check_cancelled, NULL);
7638 if (err)
7639 goto done;
7640 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7641 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7642 check_cancelled, NULL);
7643 if (err) {
7644 if (err->code == GOT_ERR_ITER_COMPLETED) {
7645 err = got_error_msg(GOT_ERR_ANCESTRY,
7646 "ran out of commits to rebase before "
7647 "youngest common ancestor commit has "
7648 "been reached?!?");
7650 goto done;
7651 } else {
7652 err = check_path_prefix(parent_id, commit_id,
7653 path_prefix, path_prefix_errcode, repo);
7654 if (err)
7655 goto done;
7657 err = got_object_qid_alloc(&qid, commit_id);
7658 if (err)
7659 goto done;
7660 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7661 commit_id = parent_id;
7664 done:
7665 got_commit_graph_close(graph);
7666 return err;
7669 static const struct got_error *
7670 cmd_rebase(int argc, char *argv[])
7672 const struct got_error *error = NULL;
7673 struct got_worktree *worktree = NULL;
7674 struct got_repository *repo = NULL;
7675 struct got_fileindex *fileindex = NULL;
7676 char *cwd = NULL;
7677 struct got_reference *branch = NULL;
7678 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7679 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7680 struct got_object_id *resume_commit_id = NULL;
7681 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7682 struct got_commit_object *commit = NULL;
7683 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7684 int histedit_in_progress = 0;
7685 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7686 struct got_object_id_queue commits;
7687 struct got_pathlist_head merged_paths;
7688 const struct got_object_id_queue *parent_ids;
7689 struct got_object_qid *qid, *pid;
7691 SIMPLEQ_INIT(&commits);
7692 TAILQ_INIT(&merged_paths);
7694 while ((ch = getopt(argc, argv, "ac")) != -1) {
7695 switch (ch) {
7696 case 'a':
7697 abort_rebase = 1;
7698 break;
7699 case 'c':
7700 continue_rebase = 1;
7701 break;
7702 default:
7703 usage_rebase();
7704 /* NOTREACHED */
7708 argc -= optind;
7709 argv += optind;
7711 #ifndef PROFILE
7712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7713 "unveil", NULL) == -1)
7714 err(1, "pledge");
7715 #endif
7716 if (abort_rebase && continue_rebase)
7717 usage_rebase();
7718 else if (abort_rebase || continue_rebase) {
7719 if (argc != 0)
7720 usage_rebase();
7721 } else if (argc != 1)
7722 usage_rebase();
7724 cwd = getcwd(NULL, 0);
7725 if (cwd == NULL) {
7726 error = got_error_from_errno("getcwd");
7727 goto done;
7729 error = got_worktree_open(&worktree, cwd);
7730 if (error) {
7731 if (error->code == GOT_ERR_NOT_WORKTREE)
7732 error = wrap_not_worktree_error(error, "rebase", cwd);
7733 goto done;
7736 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7737 NULL);
7738 if (error != NULL)
7739 goto done;
7741 error = apply_unveil(got_repo_get_path(repo), 0,
7742 got_worktree_get_root_path(worktree));
7743 if (error)
7744 goto done;
7746 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7747 worktree);
7748 if (error)
7749 goto done;
7750 if (histedit_in_progress) {
7751 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7752 goto done;
7755 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7756 if (error)
7757 goto done;
7759 if (abort_rebase) {
7760 struct got_update_progress_arg upa;
7761 if (!rebase_in_progress) {
7762 error = got_error(GOT_ERR_NOT_REBASING);
7763 goto done;
7765 error = got_worktree_rebase_continue(&resume_commit_id,
7766 &new_base_branch, &tmp_branch, &branch, &fileindex,
7767 worktree, repo);
7768 if (error)
7769 goto done;
7770 printf("Switching work tree to %s\n",
7771 got_ref_get_symref_target(new_base_branch));
7772 memset(&upa, 0, sizeof(upa));
7773 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7774 new_base_branch, update_progress, &upa);
7775 if (error)
7776 goto done;
7777 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7778 print_update_progress_stats(&upa);
7779 goto done; /* nothing else to do */
7782 if (continue_rebase) {
7783 if (!rebase_in_progress) {
7784 error = got_error(GOT_ERR_NOT_REBASING);
7785 goto done;
7787 error = got_worktree_rebase_continue(&resume_commit_id,
7788 &new_base_branch, &tmp_branch, &branch, &fileindex,
7789 worktree, repo);
7790 if (error)
7791 goto done;
7793 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7794 resume_commit_id, repo);
7795 if (error)
7796 goto done;
7798 yca_id = got_object_id_dup(resume_commit_id);
7799 if (yca_id == NULL) {
7800 error = got_error_from_errno("got_object_id_dup");
7801 goto done;
7803 } else {
7804 error = got_ref_open(&branch, repo, argv[0], 0);
7805 if (error != NULL)
7806 goto done;
7809 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7810 if (error)
7811 goto done;
7813 if (!continue_rebase) {
7814 struct got_object_id *base_commit_id;
7816 base_commit_id = got_worktree_get_base_commit_id(worktree);
7817 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7818 base_commit_id, branch_head_commit_id, repo,
7819 check_cancelled, NULL);
7820 if (error)
7821 goto done;
7822 if (yca_id == NULL) {
7823 error = got_error_msg(GOT_ERR_ANCESTRY,
7824 "specified branch shares no common ancestry "
7825 "with work tree's branch");
7826 goto done;
7829 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7830 if (error) {
7831 if (error->code != GOT_ERR_ANCESTRY)
7832 goto done;
7833 error = NULL;
7834 } else {
7835 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7836 "specified branch resolves to a commit which "
7837 "is already contained in work tree's branch");
7838 goto done;
7840 error = got_worktree_rebase_prepare(&new_base_branch,
7841 &tmp_branch, &fileindex, worktree, branch, repo);
7842 if (error)
7843 goto done;
7846 commit_id = branch_head_commit_id;
7847 error = got_object_open_as_commit(&commit, repo, commit_id);
7848 if (error)
7849 goto done;
7851 parent_ids = got_object_commit_get_parent_ids(commit);
7852 pid = SIMPLEQ_FIRST(parent_ids);
7853 if (pid == NULL) {
7854 if (!continue_rebase) {
7855 struct got_update_progress_arg upa;
7856 memset(&upa, 0, sizeof(upa));
7857 error = got_worktree_rebase_abort(worktree, fileindex,
7858 repo, new_base_branch, update_progress, &upa);
7859 if (error)
7860 goto done;
7861 printf("Rebase of %s aborted\n",
7862 got_ref_get_name(branch));
7863 print_update_progress_stats(&upa);
7866 error = got_error(GOT_ERR_EMPTY_REBASE);
7867 goto done;
7869 error = collect_commits(&commits, commit_id, pid->id,
7870 yca_id, got_worktree_get_path_prefix(worktree),
7871 GOT_ERR_REBASE_PATH, repo);
7872 got_object_commit_close(commit);
7873 commit = NULL;
7874 if (error)
7875 goto done;
7877 if (SIMPLEQ_EMPTY(&commits)) {
7878 if (continue_rebase) {
7879 error = rebase_complete(worktree, fileindex,
7880 branch, new_base_branch, tmp_branch, repo);
7881 goto done;
7882 } else {
7883 /* Fast-forward the reference of the branch. */
7884 struct got_object_id *new_head_commit_id;
7885 char *id_str;
7886 error = got_ref_resolve(&new_head_commit_id, repo,
7887 new_base_branch);
7888 if (error)
7889 goto done;
7890 error = got_object_id_str(&id_str, new_head_commit_id);
7891 printf("Forwarding %s to commit %s\n",
7892 got_ref_get_name(branch), id_str);
7893 free(id_str);
7894 error = got_ref_change_ref(branch,
7895 new_head_commit_id);
7896 if (error)
7897 goto done;
7901 pid = NULL;
7902 SIMPLEQ_FOREACH(qid, &commits, entry) {
7903 struct got_update_progress_arg upa;
7905 commit_id = qid->id;
7906 parent_id = pid ? pid->id : yca_id;
7907 pid = qid;
7909 memset(&upa, 0, sizeof(upa));
7910 error = got_worktree_rebase_merge_files(&merged_paths,
7911 worktree, fileindex, parent_id, commit_id, repo,
7912 update_progress, &upa, check_cancelled, NULL);
7913 if (error)
7914 goto done;
7916 print_update_progress_stats(&upa);
7917 if (upa.conflicts > 0)
7918 rebase_status = GOT_STATUS_CONFLICT;
7920 if (rebase_status == GOT_STATUS_CONFLICT) {
7921 error = show_rebase_merge_conflict(qid->id, repo);
7922 if (error)
7923 goto done;
7924 got_worktree_rebase_pathlist_free(&merged_paths);
7925 break;
7928 error = rebase_commit(&merged_paths, worktree, fileindex,
7929 tmp_branch, commit_id, repo);
7930 got_worktree_rebase_pathlist_free(&merged_paths);
7931 if (error)
7932 goto done;
7935 if (rebase_status == GOT_STATUS_CONFLICT) {
7936 error = got_worktree_rebase_postpone(worktree, fileindex);
7937 if (error)
7938 goto done;
7939 error = got_error_msg(GOT_ERR_CONFLICTS,
7940 "conflicts must be resolved before rebasing can continue");
7941 } else
7942 error = rebase_complete(worktree, fileindex, branch,
7943 new_base_branch, tmp_branch, repo);
7944 done:
7945 got_object_id_queue_free(&commits);
7946 free(branch_head_commit_id);
7947 free(resume_commit_id);
7948 free(yca_id);
7949 if (commit)
7950 got_object_commit_close(commit);
7951 if (branch)
7952 got_ref_close(branch);
7953 if (new_base_branch)
7954 got_ref_close(new_base_branch);
7955 if (tmp_branch)
7956 got_ref_close(tmp_branch);
7957 if (worktree)
7958 got_worktree_close(worktree);
7959 if (repo)
7960 got_repo_close(repo);
7961 return error;
7964 __dead static void
7965 usage_histedit(void)
7967 fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] [-F histedit-script] [-m]\n",
7968 getprogname());
7969 exit(1);
7972 #define GOT_HISTEDIT_PICK 'p'
7973 #define GOT_HISTEDIT_EDIT 'e'
7974 #define GOT_HISTEDIT_FOLD 'f'
7975 #define GOT_HISTEDIT_DROP 'd'
7976 #define GOT_HISTEDIT_MESG 'm'
7978 static struct got_histedit_cmd {
7979 unsigned char code;
7980 const char *name;
7981 const char *desc;
7982 } got_histedit_cmds[] = {
7983 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7984 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7985 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7986 "be used" },
7987 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7988 { GOT_HISTEDIT_MESG, "mesg",
7989 "single-line log message for commit above (open editor if empty)" },
7992 struct got_histedit_list_entry {
7993 TAILQ_ENTRY(got_histedit_list_entry) entry;
7994 struct got_object_id *commit_id;
7995 const struct got_histedit_cmd *cmd;
7996 char *logmsg;
7998 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8000 static const struct got_error *
8001 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8002 FILE *f, struct got_repository *repo)
8004 const struct got_error *err = NULL;
8005 char *logmsg = NULL, *id_str = NULL;
8006 struct got_commit_object *commit = NULL;
8007 int n;
8009 err = got_object_open_as_commit(&commit, repo, commit_id);
8010 if (err)
8011 goto done;
8013 err = get_short_logmsg(&logmsg, 34, commit);
8014 if (err)
8015 goto done;
8017 err = got_object_id_str(&id_str, commit_id);
8018 if (err)
8019 goto done;
8021 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8022 if (n < 0)
8023 err = got_ferror(f, GOT_ERR_IO);
8024 done:
8025 if (commit)
8026 got_object_commit_close(commit);
8027 free(id_str);
8028 free(logmsg);
8029 return err;
8032 static const struct got_error *
8033 histedit_write_commit_list(struct got_object_id_queue *commits,
8034 FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8036 const struct got_error *err = NULL;
8037 struct got_object_qid *qid;
8038 const char *histedit_cmd = NULL;
8040 if (SIMPLEQ_EMPTY(commits))
8041 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8043 SIMPLEQ_FOREACH(qid, commits, entry) {
8044 histedit_cmd = got_histedit_cmds[0].name;
8045 if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8046 histedit_cmd = "fold";
8047 err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8048 if (err)
8049 break;
8050 if (edit_logmsg_only) {
8051 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8052 if (n < 0) {
8053 err = got_ferror(f, GOT_ERR_IO);
8054 break;
8059 return err;
8062 static const struct got_error *
8063 write_cmd_list(FILE *f, const char *branch_name,
8064 struct got_object_id_queue *commits)
8066 const struct got_error *err = NULL;
8067 size_t i;
8068 int n;
8069 char *id_str;
8070 struct got_object_qid *qid;
8072 qid = SIMPLEQ_FIRST(commits);
8073 err = got_object_id_str(&id_str, qid->id);
8074 if (err)
8075 return err;
8077 n = fprintf(f,
8078 "# Editing the history of branch '%s' starting at\n"
8079 "# commit %s\n"
8080 "# Commits will be processed in order from top to "
8081 "bottom of this file.\n", branch_name, id_str);
8082 if (n < 0) {
8083 err = got_ferror(f, GOT_ERR_IO);
8084 goto done;
8087 n = fprintf(f, "# Available histedit commands:\n");
8088 if (n < 0) {
8089 err = got_ferror(f, GOT_ERR_IO);
8090 goto done;
8093 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8094 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8095 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8096 cmd->desc);
8097 if (n < 0) {
8098 err = got_ferror(f, GOT_ERR_IO);
8099 break;
8102 done:
8103 free(id_str);
8104 return err;
8107 static const struct got_error *
8108 histedit_syntax_error(int lineno)
8110 static char msg[42];
8111 int ret;
8113 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8114 lineno);
8115 if (ret == -1 || ret >= sizeof(msg))
8116 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8118 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8121 static const struct got_error *
8122 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8123 char *logmsg, struct got_repository *repo)
8125 const struct got_error *err;
8126 struct got_commit_object *folded_commit = NULL;
8127 char *id_str, *folded_logmsg = NULL;
8129 err = got_object_id_str(&id_str, hle->commit_id);
8130 if (err)
8131 return err;
8133 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8134 if (err)
8135 goto done;
8137 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8138 if (err)
8139 goto done;
8140 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8141 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8142 folded_logmsg) == -1) {
8143 err = got_error_from_errno("asprintf");
8145 done:
8146 if (folded_commit)
8147 got_object_commit_close(folded_commit);
8148 free(id_str);
8149 free(folded_logmsg);
8150 return err;
8153 static struct got_histedit_list_entry *
8154 get_folded_commits(struct got_histedit_list_entry *hle)
8156 struct got_histedit_list_entry *prev, *folded = NULL;
8158 prev = TAILQ_PREV(hle, got_histedit_list, entry);
8159 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8160 prev->cmd->code == GOT_HISTEDIT_DROP)) {
8161 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8162 folded = prev;
8163 prev = TAILQ_PREV(prev, got_histedit_list, entry);
8166 return folded;
8169 static const struct got_error *
8170 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8171 struct got_repository *repo)
8173 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8174 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8175 const struct got_error *err = NULL;
8176 struct got_commit_object *commit = NULL;
8177 int logmsg_len;
8178 int fd;
8179 struct got_histedit_list_entry *folded = NULL;
8181 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8182 if (err)
8183 return err;
8185 folded = get_folded_commits(hle);
8186 if (folded) {
8187 while (folded != hle) {
8188 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8189 folded = TAILQ_NEXT(folded, entry);
8190 continue;
8192 err = append_folded_commit_msg(&new_msg, folded,
8193 logmsg, repo);
8194 if (err)
8195 goto done;
8196 free(logmsg);
8197 logmsg = new_msg;
8198 folded = TAILQ_NEXT(folded, entry);
8202 err = got_object_id_str(&id_str, hle->commit_id);
8203 if (err)
8204 goto done;
8205 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8206 if (err)
8207 goto done;
8208 logmsg_len = asprintf(&new_msg,
8209 "%s\n# original log message of commit %s: %s",
8210 logmsg ? logmsg : "", id_str, orig_logmsg);
8211 if (logmsg_len == -1) {
8212 err = got_error_from_errno("asprintf");
8213 goto done;
8215 free(logmsg);
8216 logmsg = new_msg;
8218 err = got_object_id_str(&id_str, hle->commit_id);
8219 if (err)
8220 goto done;
8222 err = got_opentemp_named_fd(&logmsg_path, &fd,
8223 GOT_TMPDIR_STR "/got-logmsg");
8224 if (err)
8225 goto done;
8227 write(fd, logmsg, logmsg_len);
8228 close(fd);
8230 err = get_editor(&editor);
8231 if (err)
8232 goto done;
8234 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8235 logmsg_len, 0);
8236 if (err) {
8237 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8238 goto done;
8239 err = NULL;
8240 hle->logmsg = strdup(new_msg);
8241 if (hle->logmsg == NULL)
8242 err = got_error_from_errno("strdup");
8244 done:
8245 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8246 err = got_error_from_errno2("unlink", logmsg_path);
8247 free(logmsg_path);
8248 free(logmsg);
8249 free(orig_logmsg);
8250 free(editor);
8251 if (commit)
8252 got_object_commit_close(commit);
8253 return err;
8256 static const struct got_error *
8257 histedit_parse_list(struct got_histedit_list *histedit_cmds,
8258 FILE *f, struct got_repository *repo)
8260 const struct got_error *err = NULL;
8261 char *line = NULL, *p, *end;
8262 size_t i, size;
8263 ssize_t len;
8264 int lineno = 0;
8265 const struct got_histedit_cmd *cmd;
8266 struct got_object_id *commit_id = NULL;
8267 struct got_histedit_list_entry *hle = NULL;
8269 for (;;) {
8270 len = getline(&line, &size, f);
8271 if (len == -1) {
8272 const struct got_error *getline_err;
8273 if (feof(f))
8274 break;
8275 getline_err = got_error_from_errno("getline");
8276 err = got_ferror(f, getline_err->code);
8277 break;
8279 lineno++;
8280 p = line;
8281 while (isspace((unsigned char)p[0]))
8282 p++;
8283 if (p[0] == '#' || p[0] == '\0') {
8284 free(line);
8285 line = NULL;
8286 continue;
8288 cmd = NULL;
8289 for (i = 0; i < nitems(got_histedit_cmds); i++) {
8290 cmd = &got_histedit_cmds[i];
8291 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8292 isspace((unsigned char)p[strlen(cmd->name)])) {
8293 p += strlen(cmd->name);
8294 break;
8296 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8297 p++;
8298 break;
8301 if (i == nitems(got_histedit_cmds)) {
8302 err = histedit_syntax_error(lineno);
8303 break;
8305 while (isspace((unsigned char)p[0]))
8306 p++;
8307 if (cmd->code == GOT_HISTEDIT_MESG) {
8308 if (hle == NULL || hle->logmsg != NULL) {
8309 err = got_error(GOT_ERR_HISTEDIT_CMD);
8310 break;
8312 if (p[0] == '\0') {
8313 err = histedit_edit_logmsg(hle, repo);
8314 if (err)
8315 break;
8316 } else {
8317 hle->logmsg = strdup(p);
8318 if (hle->logmsg == NULL) {
8319 err = got_error_from_errno("strdup");
8320 break;
8323 free(line);
8324 line = NULL;
8325 continue;
8326 } else {
8327 end = p;
8328 while (end[0] && !isspace((unsigned char)end[0]))
8329 end++;
8330 *end = '\0';
8332 err = got_object_resolve_id_str(&commit_id, repo, p);
8333 if (err) {
8334 /* override error code */
8335 err = histedit_syntax_error(lineno);
8336 break;
8339 hle = malloc(sizeof(*hle));
8340 if (hle == NULL) {
8341 err = got_error_from_errno("malloc");
8342 break;
8344 hle->cmd = cmd;
8345 hle->commit_id = commit_id;
8346 hle->logmsg = NULL;
8347 commit_id = NULL;
8348 free(line);
8349 line = NULL;
8350 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8353 free(line);
8354 free(commit_id);
8355 return err;
8358 static const struct got_error *
8359 histedit_check_script(struct got_histedit_list *histedit_cmds,
8360 struct got_object_id_queue *commits, struct got_repository *repo)
8362 const struct got_error *err = NULL;
8363 struct got_object_qid *qid;
8364 struct got_histedit_list_entry *hle;
8365 static char msg[92];
8366 char *id_str;
8368 if (TAILQ_EMPTY(histedit_cmds))
8369 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8370 "histedit script contains no commands");
8371 if (SIMPLEQ_EMPTY(commits))
8372 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8374 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8375 struct got_histedit_list_entry *hle2;
8376 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8377 if (hle == hle2)
8378 continue;
8379 if (got_object_id_cmp(hle->commit_id,
8380 hle2->commit_id) != 0)
8381 continue;
8382 err = got_object_id_str(&id_str, hle->commit_id);
8383 if (err)
8384 return err;
8385 snprintf(msg, sizeof(msg), "commit %s is listed "
8386 "more than once in histedit script", id_str);
8387 free(id_str);
8388 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8392 SIMPLEQ_FOREACH(qid, commits, entry) {
8393 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8394 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8395 break;
8397 if (hle == NULL) {
8398 err = got_object_id_str(&id_str, qid->id);
8399 if (err)
8400 return err;
8401 snprintf(msg, sizeof(msg),
8402 "commit %s missing from histedit script", id_str);
8403 free(id_str);
8404 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8408 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8409 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8410 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8411 "last commit in histedit script cannot be folded");
8413 return NULL;
8416 static const struct got_error *
8417 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8418 const char *path, struct got_object_id_queue *commits,
8419 struct got_repository *repo)
8421 const struct got_error *err = NULL;
8422 char *editor;
8423 FILE *f = NULL;
8425 err = get_editor(&editor);
8426 if (err)
8427 return err;
8429 if (spawn_editor(editor, path) == -1) {
8430 err = got_error_from_errno("failed spawning editor");
8431 goto done;
8434 f = fopen(path, "r");
8435 if (f == NULL) {
8436 err = got_error_from_errno("fopen");
8437 goto done;
8439 err = histedit_parse_list(histedit_cmds, f, repo);
8440 if (err)
8441 goto done;
8443 err = histedit_check_script(histedit_cmds, commits, repo);
8444 done:
8445 if (f && fclose(f) == EOF && err == NULL)
8446 err = got_error_from_errno("fclose");
8447 free(editor);
8448 return err;
8451 static const struct got_error *
8452 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8453 struct got_object_id_queue *, const char *, const char *,
8454 struct got_repository *);
8456 static const struct got_error *
8457 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8458 struct got_object_id_queue *commits, const char *branch_name,
8459 int edit_logmsg_only, int fold_only, struct got_repository *repo)
8461 const struct got_error *err;
8462 FILE *f = NULL;
8463 char *path = NULL;
8465 err = got_opentemp_named(&path, &f, "got-histedit");
8466 if (err)
8467 return err;
8469 err = write_cmd_list(f, branch_name, commits);
8470 if (err)
8471 goto done;
8473 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8474 fold_only, repo);
8475 if (err)
8476 goto done;
8478 if (edit_logmsg_only || fold_only) {
8479 rewind(f);
8480 err = histedit_parse_list(histedit_cmds, f, repo);
8481 } else {
8482 if (fclose(f) == EOF) {
8483 err = got_error_from_errno("fclose");
8484 goto done;
8486 f = NULL;
8487 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8488 if (err) {
8489 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8490 err->code != GOT_ERR_HISTEDIT_CMD)
8491 goto done;
8492 err = histedit_edit_list_retry(histedit_cmds, err,
8493 commits, path, branch_name, repo);
8496 done:
8497 if (f && fclose(f) == EOF && err == NULL)
8498 err = got_error_from_errno("fclose");
8499 if (path && unlink(path) != 0 && err == NULL)
8500 err = got_error_from_errno2("unlink", path);
8501 free(path);
8502 return err;
8505 static const struct got_error *
8506 histedit_save_list(struct got_histedit_list *histedit_cmds,
8507 struct got_worktree *worktree, struct got_repository *repo)
8509 const struct got_error *err = NULL;
8510 char *path = NULL;
8511 FILE *f = NULL;
8512 struct got_histedit_list_entry *hle;
8513 struct got_commit_object *commit = NULL;
8515 err = got_worktree_get_histedit_script_path(&path, worktree);
8516 if (err)
8517 return err;
8519 f = fopen(path, "w");
8520 if (f == NULL) {
8521 err = got_error_from_errno2("fopen", path);
8522 goto done;
8524 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8525 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8526 repo);
8527 if (err)
8528 break;
8530 if (hle->logmsg) {
8531 int n = fprintf(f, "%c %s\n",
8532 GOT_HISTEDIT_MESG, hle->logmsg);
8533 if (n < 0) {
8534 err = got_ferror(f, GOT_ERR_IO);
8535 break;
8539 done:
8540 if (f && fclose(f) == EOF && err == NULL)
8541 err = got_error_from_errno("fclose");
8542 free(path);
8543 if (commit)
8544 got_object_commit_close(commit);
8545 return err;
8548 void
8549 histedit_free_list(struct got_histedit_list *histedit_cmds)
8551 struct got_histedit_list_entry *hle;
8553 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8554 TAILQ_REMOVE(histedit_cmds, hle, entry);
8555 free(hle);
8559 static const struct got_error *
8560 histedit_load_list(struct got_histedit_list *histedit_cmds,
8561 const char *path, struct got_repository *repo)
8563 const struct got_error *err = NULL;
8564 FILE *f = NULL;
8566 f = fopen(path, "r");
8567 if (f == NULL) {
8568 err = got_error_from_errno2("fopen", path);
8569 goto done;
8572 err = histedit_parse_list(histedit_cmds, f, repo);
8573 done:
8574 if (f && fclose(f) == EOF && err == NULL)
8575 err = got_error_from_errno("fclose");
8576 return err;
8579 static const struct got_error *
8580 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8581 const struct got_error *edit_err, struct got_object_id_queue *commits,
8582 const char *path, const char *branch_name, struct got_repository *repo)
8584 const struct got_error *err = NULL, *prev_err = edit_err;
8585 int resp = ' ';
8587 while (resp != 'c' && resp != 'r' && resp != 'a') {
8588 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8589 "or (a)bort: ", getprogname(), prev_err->msg);
8590 resp = getchar();
8591 if (resp == '\n')
8592 resp = getchar();
8593 if (resp == 'c') {
8594 histedit_free_list(histedit_cmds);
8595 err = histedit_run_editor(histedit_cmds, path, commits,
8596 repo);
8597 if (err) {
8598 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8599 err->code != GOT_ERR_HISTEDIT_CMD)
8600 break;
8601 prev_err = err;
8602 resp = ' ';
8603 continue;
8605 break;
8606 } else if (resp == 'r') {
8607 histedit_free_list(histedit_cmds);
8608 err = histedit_edit_script(histedit_cmds,
8609 commits, branch_name, 0, 0, repo);
8610 if (err) {
8611 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8612 err->code != GOT_ERR_HISTEDIT_CMD)
8613 break;
8614 prev_err = err;
8615 resp = ' ';
8616 continue;
8618 break;
8619 } else if (resp == 'a') {
8620 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8621 break;
8622 } else
8623 printf("invalid response '%c'\n", resp);
8626 return err;
8629 static const struct got_error *
8630 histedit_complete(struct got_worktree *worktree,
8631 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8632 struct got_reference *branch, struct got_repository *repo)
8634 printf("Switching work tree to %s\n",
8635 got_ref_get_symref_target(branch));
8636 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8637 branch, repo);
8640 static const struct got_error *
8641 show_histedit_progress(struct got_commit_object *commit,
8642 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8644 const struct got_error *err;
8645 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8647 err = got_object_id_str(&old_id_str, hle->commit_id);
8648 if (err)
8649 goto done;
8651 if (new_id) {
8652 err = got_object_id_str(&new_id_str, new_id);
8653 if (err)
8654 goto done;
8657 old_id_str[12] = '\0';
8658 if (new_id_str)
8659 new_id_str[12] = '\0';
8661 if (hle->logmsg) {
8662 logmsg = strdup(hle->logmsg);
8663 if (logmsg == NULL) {
8664 err = got_error_from_errno("strdup");
8665 goto done;
8667 trim_logmsg(logmsg, 42);
8668 } else {
8669 err = get_short_logmsg(&logmsg, 42, commit);
8670 if (err)
8671 goto done;
8674 switch (hle->cmd->code) {
8675 case GOT_HISTEDIT_PICK:
8676 case GOT_HISTEDIT_EDIT:
8677 printf("%s -> %s: %s\n", old_id_str,
8678 new_id_str ? new_id_str : "no-op change", logmsg);
8679 break;
8680 case GOT_HISTEDIT_DROP:
8681 case GOT_HISTEDIT_FOLD:
8682 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8683 logmsg);
8684 break;
8685 default:
8686 break;
8688 done:
8689 free(old_id_str);
8690 free(new_id_str);
8691 return err;
8694 static const struct got_error *
8695 histedit_commit(struct got_pathlist_head *merged_paths,
8696 struct got_worktree *worktree, struct got_fileindex *fileindex,
8697 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8698 struct got_repository *repo)
8700 const struct got_error *err;
8701 struct got_commit_object *commit;
8702 struct got_object_id *new_commit_id;
8704 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8705 && hle->logmsg == NULL) {
8706 err = histedit_edit_logmsg(hle, repo);
8707 if (err)
8708 return err;
8711 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8712 if (err)
8713 return err;
8715 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8716 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8717 hle->logmsg, repo);
8718 if (err) {
8719 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8720 goto done;
8721 err = show_histedit_progress(commit, hle, NULL);
8722 } else {
8723 err = show_histedit_progress(commit, hle, new_commit_id);
8724 free(new_commit_id);
8726 done:
8727 got_object_commit_close(commit);
8728 return err;
8731 static const struct got_error *
8732 histedit_skip_commit(struct got_histedit_list_entry *hle,
8733 struct got_worktree *worktree, struct got_repository *repo)
8735 const struct got_error *error;
8736 struct got_commit_object *commit;
8738 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8739 repo);
8740 if (error)
8741 return error;
8743 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8744 if (error)
8745 return error;
8747 error = show_histedit_progress(commit, hle, NULL);
8748 got_object_commit_close(commit);
8749 return error;
8752 static const struct got_error *
8753 check_local_changes(void *arg, unsigned char status,
8754 unsigned char staged_status, const char *path,
8755 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8756 struct got_object_id *commit_id, int dirfd, const char *de_name)
8758 int *have_local_changes = arg;
8760 switch (status) {
8761 case GOT_STATUS_ADD:
8762 case GOT_STATUS_DELETE:
8763 case GOT_STATUS_MODIFY:
8764 case GOT_STATUS_CONFLICT:
8765 *have_local_changes = 1;
8766 return got_error(GOT_ERR_CANCELLED);
8767 default:
8768 break;
8771 switch (staged_status) {
8772 case GOT_STATUS_ADD:
8773 case GOT_STATUS_DELETE:
8774 case GOT_STATUS_MODIFY:
8775 *have_local_changes = 1;
8776 return got_error(GOT_ERR_CANCELLED);
8777 default:
8778 break;
8781 return NULL;
8784 static const struct got_error *
8785 cmd_histedit(int argc, char *argv[])
8787 const struct got_error *error = NULL;
8788 struct got_worktree *worktree = NULL;
8789 struct got_fileindex *fileindex = NULL;
8790 struct got_repository *repo = NULL;
8791 char *cwd = NULL;
8792 struct got_reference *branch = NULL;
8793 struct got_reference *tmp_branch = NULL;
8794 struct got_object_id *resume_commit_id = NULL;
8795 struct got_object_id *base_commit_id = NULL;
8796 struct got_object_id *head_commit_id = NULL;
8797 struct got_commit_object *commit = NULL;
8798 int ch, rebase_in_progress = 0;
8799 struct got_update_progress_arg upa;
8800 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8801 int edit_logmsg_only = 0, fold_only = 0;
8802 const char *edit_script_path = NULL;
8803 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8804 struct got_object_id_queue commits;
8805 struct got_pathlist_head merged_paths;
8806 const struct got_object_id_queue *parent_ids;
8807 struct got_object_qid *pid;
8808 struct got_histedit_list histedit_cmds;
8809 struct got_histedit_list_entry *hle;
8811 SIMPLEQ_INIT(&commits);
8812 TAILQ_INIT(&histedit_cmds);
8813 TAILQ_INIT(&merged_paths);
8814 memset(&upa, 0, sizeof(upa));
8816 while ((ch = getopt(argc, argv, "acfF:m")) != -1) {
8817 switch (ch) {
8818 case 'a':
8819 abort_edit = 1;
8820 break;
8821 case 'c':
8822 continue_edit = 1;
8823 break;
8824 case 'f':
8825 fold_only = 1;
8826 break;
8827 case 'F':
8828 edit_script_path = optarg;
8829 break;
8830 case 'm':
8831 edit_logmsg_only = 1;
8832 break;
8833 default:
8834 usage_histedit();
8835 /* NOTREACHED */
8839 argc -= optind;
8840 argv += optind;
8842 #ifndef PROFILE
8843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8844 "unveil", NULL) == -1)
8845 err(1, "pledge");
8846 #endif
8847 if (abort_edit && continue_edit)
8848 option_conflict('a', 'c');
8849 if (edit_script_path && edit_logmsg_only)
8850 option_conflict('F', 'm');
8851 if (abort_edit && edit_logmsg_only)
8852 option_conflict('a', 'm');
8853 if (continue_edit && edit_logmsg_only)
8854 option_conflict('c', 'm');
8855 if (abort_edit && fold_only)
8856 option_conflict('a', 'f');
8857 if (continue_edit && fold_only)
8858 option_conflict('c', 'f');
8859 if (fold_only && edit_logmsg_only)
8860 option_conflict('f', 'm');
8861 if (edit_script_path && fold_only)
8862 option_conflict('F', 'f');
8863 if (argc != 0)
8864 usage_histedit();
8867 * This command cannot apply unveil(2) in all cases because the
8868 * user may choose to run an editor to edit the histedit script
8869 * and to edit individual commit log messages.
8870 * unveil(2) traverses exec(2); if an editor is used we have to
8871 * apply unveil after edit script and log messages have been written.
8872 * XXX TODO: Make use of unveil(2) where possible.
8875 cwd = getcwd(NULL, 0);
8876 if (cwd == NULL) {
8877 error = got_error_from_errno("getcwd");
8878 goto done;
8880 error = got_worktree_open(&worktree, cwd);
8881 if (error) {
8882 if (error->code == GOT_ERR_NOT_WORKTREE)
8883 error = wrap_not_worktree_error(error, "histedit", cwd);
8884 goto done;
8887 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8888 NULL);
8889 if (error != NULL)
8890 goto done;
8892 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8893 if (error)
8894 goto done;
8895 if (rebase_in_progress) {
8896 error = got_error(GOT_ERR_REBASING);
8897 goto done;
8900 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8901 if (error)
8902 goto done;
8904 if (edit_in_progress && edit_logmsg_only) {
8905 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8906 "histedit operation is in progress in this "
8907 "work tree and must be continued or aborted "
8908 "before the -m option can be used");
8909 goto done;
8911 if (edit_in_progress && fold_only) {
8912 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8913 "histedit operation is in progress in this "
8914 "work tree and must be continued or aborted "
8915 "before the -f option can be used");
8916 goto done;
8919 if (edit_in_progress && abort_edit) {
8920 error = got_worktree_histedit_continue(&resume_commit_id,
8921 &tmp_branch, &branch, &base_commit_id, &fileindex,
8922 worktree, repo);
8923 if (error)
8924 goto done;
8925 printf("Switching work tree to %s\n",
8926 got_ref_get_symref_target(branch));
8927 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8928 branch, base_commit_id, update_progress, &upa);
8929 if (error)
8930 goto done;
8931 printf("Histedit of %s aborted\n",
8932 got_ref_get_symref_target(branch));
8933 print_update_progress_stats(&upa);
8934 goto done; /* nothing else to do */
8935 } else if (abort_edit) {
8936 error = got_error(GOT_ERR_NOT_HISTEDIT);
8937 goto done;
8940 if (continue_edit) {
8941 char *path;
8943 if (!edit_in_progress) {
8944 error = got_error(GOT_ERR_NOT_HISTEDIT);
8945 goto done;
8948 error = got_worktree_get_histedit_script_path(&path, worktree);
8949 if (error)
8950 goto done;
8952 error = histedit_load_list(&histedit_cmds, path, repo);
8953 free(path);
8954 if (error)
8955 goto done;
8957 error = got_worktree_histedit_continue(&resume_commit_id,
8958 &tmp_branch, &branch, &base_commit_id, &fileindex,
8959 worktree, repo);
8960 if (error)
8961 goto done;
8963 error = got_ref_resolve(&head_commit_id, repo, branch);
8964 if (error)
8965 goto done;
8967 error = got_object_open_as_commit(&commit, repo,
8968 head_commit_id);
8969 if (error)
8970 goto done;
8971 parent_ids = got_object_commit_get_parent_ids(commit);
8972 pid = SIMPLEQ_FIRST(parent_ids);
8973 if (pid == NULL) {
8974 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8975 goto done;
8977 error = collect_commits(&commits, head_commit_id, pid->id,
8978 base_commit_id, got_worktree_get_path_prefix(worktree),
8979 GOT_ERR_HISTEDIT_PATH, repo);
8980 got_object_commit_close(commit);
8981 commit = NULL;
8982 if (error)
8983 goto done;
8984 } else {
8985 if (edit_in_progress) {
8986 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8987 goto done;
8990 error = got_ref_open(&branch, repo,
8991 got_worktree_get_head_ref_name(worktree), 0);
8992 if (error != NULL)
8993 goto done;
8995 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8996 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8997 "will not edit commit history of a branch outside "
8998 "the \"refs/heads/\" reference namespace");
8999 goto done;
9002 error = got_ref_resolve(&head_commit_id, repo, branch);
9003 got_ref_close(branch);
9004 branch = NULL;
9005 if (error)
9006 goto done;
9008 error = got_object_open_as_commit(&commit, repo,
9009 head_commit_id);
9010 if (error)
9011 goto done;
9012 parent_ids = got_object_commit_get_parent_ids(commit);
9013 pid = SIMPLEQ_FIRST(parent_ids);
9014 if (pid == NULL) {
9015 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9016 goto done;
9018 error = collect_commits(&commits, head_commit_id, pid->id,
9019 got_worktree_get_base_commit_id(worktree),
9020 got_worktree_get_path_prefix(worktree),
9021 GOT_ERR_HISTEDIT_PATH, repo);
9022 got_object_commit_close(commit);
9023 commit = NULL;
9024 if (error)
9025 goto done;
9027 if (SIMPLEQ_EMPTY(&commits)) {
9028 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9029 goto done;
9032 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9033 &base_commit_id, &fileindex, worktree, repo);
9034 if (error)
9035 goto done;
9037 if (edit_script_path) {
9038 error = histedit_load_list(&histedit_cmds,
9039 edit_script_path, repo);
9040 if (error) {
9041 got_worktree_histedit_abort(worktree, fileindex,
9042 repo, branch, base_commit_id,
9043 update_progress, &upa);
9044 print_update_progress_stats(&upa);
9045 goto done;
9047 } else {
9048 const char *branch_name;
9049 branch_name = got_ref_get_symref_target(branch);
9050 if (strncmp(branch_name, "refs/heads/", 11) == 0)
9051 branch_name += 11;
9052 error = histedit_edit_script(&histedit_cmds, &commits,
9053 branch_name, edit_logmsg_only, fold_only, repo);
9054 if (error) {
9055 got_worktree_histedit_abort(worktree, fileindex,
9056 repo, branch, base_commit_id,
9057 update_progress, &upa);
9058 print_update_progress_stats(&upa);
9059 goto done;
9064 error = histedit_save_list(&histedit_cmds, worktree,
9065 repo);
9066 if (error) {
9067 got_worktree_histedit_abort(worktree, fileindex,
9068 repo, branch, base_commit_id,
9069 update_progress, &upa);
9070 print_update_progress_stats(&upa);
9071 goto done;
9076 error = histedit_check_script(&histedit_cmds, &commits, repo);
9077 if (error)
9078 goto done;
9080 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9081 if (resume_commit_id) {
9082 if (got_object_id_cmp(hle->commit_id,
9083 resume_commit_id) != 0)
9084 continue;
9086 resume_commit_id = NULL;
9087 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9088 hle->cmd->code == GOT_HISTEDIT_FOLD) {
9089 error = histedit_skip_commit(hle, worktree,
9090 repo);
9091 if (error)
9092 goto done;
9093 } else {
9094 struct got_pathlist_head paths;
9095 int have_changes = 0;
9097 TAILQ_INIT(&paths);
9098 error = got_pathlist_append(&paths, "", NULL);
9099 if (error)
9100 goto done;
9101 error = got_worktree_status(worktree, &paths,
9102 repo, check_local_changes, &have_changes,
9103 check_cancelled, NULL);
9104 got_pathlist_free(&paths);
9105 if (error) {
9106 if (error->code != GOT_ERR_CANCELLED)
9107 goto done;
9108 if (sigint_received || sigpipe_received)
9109 goto done;
9111 if (have_changes) {
9112 error = histedit_commit(NULL, worktree,
9113 fileindex, tmp_branch, hle, repo);
9114 if (error)
9115 goto done;
9116 } else {
9117 error = got_object_open_as_commit(
9118 &commit, repo, hle->commit_id);
9119 if (error)
9120 goto done;
9121 error = show_histedit_progress(commit,
9122 hle, NULL);
9123 got_object_commit_close(commit);
9124 commit = NULL;
9125 if (error)
9126 goto done;
9129 continue;
9132 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9133 error = histedit_skip_commit(hle, worktree, repo);
9134 if (error)
9135 goto done;
9136 continue;
9139 error = got_object_open_as_commit(&commit, repo,
9140 hle->commit_id);
9141 if (error)
9142 goto done;
9143 parent_ids = got_object_commit_get_parent_ids(commit);
9144 pid = SIMPLEQ_FIRST(parent_ids);
9146 error = got_worktree_histedit_merge_files(&merged_paths,
9147 worktree, fileindex, pid->id, hle->commit_id, repo,
9148 update_progress, &upa, check_cancelled, NULL);
9149 if (error)
9150 goto done;
9151 got_object_commit_close(commit);
9152 commit = NULL;
9154 print_update_progress_stats(&upa);
9155 if (upa.conflicts > 0)
9156 rebase_status = GOT_STATUS_CONFLICT;
9158 if (rebase_status == GOT_STATUS_CONFLICT) {
9159 error = show_rebase_merge_conflict(hle->commit_id,
9160 repo);
9161 if (error)
9162 goto done;
9163 got_worktree_rebase_pathlist_free(&merged_paths);
9164 break;
9167 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9168 char *id_str;
9169 error = got_object_id_str(&id_str, hle->commit_id);
9170 if (error)
9171 goto done;
9172 printf("Stopping histedit for amending commit %s\n",
9173 id_str);
9174 free(id_str);
9175 got_worktree_rebase_pathlist_free(&merged_paths);
9176 error = got_worktree_histedit_postpone(worktree,
9177 fileindex);
9178 goto done;
9181 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9182 error = histedit_skip_commit(hle, worktree, repo);
9183 if (error)
9184 goto done;
9185 continue;
9188 error = histedit_commit(&merged_paths, worktree, fileindex,
9189 tmp_branch, hle, repo);
9190 got_worktree_rebase_pathlist_free(&merged_paths);
9191 if (error)
9192 goto done;
9195 if (rebase_status == GOT_STATUS_CONFLICT) {
9196 error = got_worktree_histedit_postpone(worktree, fileindex);
9197 if (error)
9198 goto done;
9199 error = got_error_msg(GOT_ERR_CONFLICTS,
9200 "conflicts must be resolved before histedit can continue");
9201 } else
9202 error = histedit_complete(worktree, fileindex, tmp_branch,
9203 branch, repo);
9204 done:
9205 got_object_id_queue_free(&commits);
9206 histedit_free_list(&histedit_cmds);
9207 free(head_commit_id);
9208 free(base_commit_id);
9209 free(resume_commit_id);
9210 if (commit)
9211 got_object_commit_close(commit);
9212 if (branch)
9213 got_ref_close(branch);
9214 if (tmp_branch)
9215 got_ref_close(tmp_branch);
9216 if (worktree)
9217 got_worktree_close(worktree);
9218 if (repo)
9219 got_repo_close(repo);
9220 return error;
9223 __dead static void
9224 usage_integrate(void)
9226 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9227 exit(1);
9230 static const struct got_error *
9231 cmd_integrate(int argc, char *argv[])
9233 const struct got_error *error = NULL;
9234 struct got_repository *repo = NULL;
9235 struct got_worktree *worktree = NULL;
9236 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9237 const char *branch_arg = NULL;
9238 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9239 struct got_fileindex *fileindex = NULL;
9240 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9241 int ch;
9242 struct got_update_progress_arg upa;
9244 while ((ch = getopt(argc, argv, "")) != -1) {
9245 switch (ch) {
9246 default:
9247 usage_integrate();
9248 /* NOTREACHED */
9252 argc -= optind;
9253 argv += optind;
9255 if (argc != 1)
9256 usage_integrate();
9257 branch_arg = argv[0];
9258 #ifndef PROFILE
9259 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9260 "unveil", NULL) == -1)
9261 err(1, "pledge");
9262 #endif
9263 cwd = getcwd(NULL, 0);
9264 if (cwd == NULL) {
9265 error = got_error_from_errno("getcwd");
9266 goto done;
9269 error = got_worktree_open(&worktree, cwd);
9270 if (error) {
9271 if (error->code == GOT_ERR_NOT_WORKTREE)
9272 error = wrap_not_worktree_error(error, "integrate",
9273 cwd);
9274 goto done;
9277 error = check_rebase_or_histedit_in_progress(worktree);
9278 if (error)
9279 goto done;
9281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9282 NULL);
9283 if (error != NULL)
9284 goto done;
9286 error = apply_unveil(got_repo_get_path(repo), 0,
9287 got_worktree_get_root_path(worktree));
9288 if (error)
9289 goto done;
9291 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9292 error = got_error_from_errno("asprintf");
9293 goto done;
9296 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9297 &base_branch_ref, worktree, refname, repo);
9298 if (error)
9299 goto done;
9301 refname = strdup(got_ref_get_name(branch_ref));
9302 if (refname == NULL) {
9303 error = got_error_from_errno("strdup");
9304 got_worktree_integrate_abort(worktree, fileindex, repo,
9305 branch_ref, base_branch_ref);
9306 goto done;
9308 base_refname = strdup(got_ref_get_name(base_branch_ref));
9309 if (base_refname == NULL) {
9310 error = got_error_from_errno("strdup");
9311 got_worktree_integrate_abort(worktree, fileindex, repo,
9312 branch_ref, base_branch_ref);
9313 goto done;
9316 error = got_ref_resolve(&commit_id, repo, branch_ref);
9317 if (error)
9318 goto done;
9320 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9321 if (error)
9322 goto done;
9324 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9325 error = got_error_msg(GOT_ERR_SAME_BRANCH,
9326 "specified branch has already been integrated");
9327 got_worktree_integrate_abort(worktree, fileindex, repo,
9328 branch_ref, base_branch_ref);
9329 goto done;
9332 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9333 if (error) {
9334 if (error->code == GOT_ERR_ANCESTRY)
9335 error = got_error(GOT_ERR_REBASE_REQUIRED);
9336 got_worktree_integrate_abort(worktree, fileindex, repo,
9337 branch_ref, base_branch_ref);
9338 goto done;
9341 memset(&upa, 0, sizeof(upa));
9342 error = got_worktree_integrate_continue(worktree, fileindex, repo,
9343 branch_ref, base_branch_ref, update_progress, &upa,
9344 check_cancelled, NULL);
9345 if (error)
9346 goto done;
9348 printf("Integrated %s into %s\n", refname, base_refname);
9349 print_update_progress_stats(&upa);
9350 done:
9351 if (repo)
9352 got_repo_close(repo);
9353 if (worktree)
9354 got_worktree_close(worktree);
9355 free(cwd);
9356 free(base_commit_id);
9357 free(commit_id);
9358 free(refname);
9359 free(base_refname);
9360 return error;
9363 __dead static void
9364 usage_stage(void)
9366 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9367 "[-S] [file-path ...]\n",
9368 getprogname());
9369 exit(1);
9372 static const struct got_error *
9373 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9374 const char *path, struct got_object_id *blob_id,
9375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9376 int dirfd, const char *de_name)
9378 const struct got_error *err = NULL;
9379 char *id_str = NULL;
9381 if (staged_status != GOT_STATUS_ADD &&
9382 staged_status != GOT_STATUS_MODIFY &&
9383 staged_status != GOT_STATUS_DELETE)
9384 return NULL;
9386 if (staged_status == GOT_STATUS_ADD ||
9387 staged_status == GOT_STATUS_MODIFY)
9388 err = got_object_id_str(&id_str, staged_blob_id);
9389 else
9390 err = got_object_id_str(&id_str, blob_id);
9391 if (err)
9392 return err;
9394 printf("%s %c %s\n", id_str, staged_status, path);
9395 free(id_str);
9396 return NULL;
9399 static const struct got_error *
9400 cmd_stage(int argc, char *argv[])
9402 const struct got_error *error = NULL;
9403 struct got_repository *repo = NULL;
9404 struct got_worktree *worktree = NULL;
9405 char *cwd = NULL;
9406 struct got_pathlist_head paths;
9407 struct got_pathlist_entry *pe;
9408 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9409 FILE *patch_script_file = NULL;
9410 const char *patch_script_path = NULL;
9411 struct choose_patch_arg cpa;
9413 TAILQ_INIT(&paths);
9415 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9416 switch (ch) {
9417 case 'l':
9418 list_stage = 1;
9419 break;
9420 case 'p':
9421 pflag = 1;
9422 break;
9423 case 'F':
9424 patch_script_path = optarg;
9425 break;
9426 case 'S':
9427 allow_bad_symlinks = 1;
9428 break;
9429 default:
9430 usage_stage();
9431 /* NOTREACHED */
9435 argc -= optind;
9436 argv += optind;
9438 #ifndef PROFILE
9439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9440 "unveil", NULL) == -1)
9441 err(1, "pledge");
9442 #endif
9443 if (list_stage && (pflag || patch_script_path))
9444 errx(1, "-l option cannot be used with other options");
9445 if (patch_script_path && !pflag)
9446 errx(1, "-F option can only be used together with -p option");
9448 cwd = getcwd(NULL, 0);
9449 if (cwd == NULL) {
9450 error = got_error_from_errno("getcwd");
9451 goto done;
9454 error = got_worktree_open(&worktree, cwd);
9455 if (error) {
9456 if (error->code == GOT_ERR_NOT_WORKTREE)
9457 error = wrap_not_worktree_error(error, "stage", cwd);
9458 goto done;
9461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9462 NULL);
9463 if (error != NULL)
9464 goto done;
9466 if (patch_script_path) {
9467 patch_script_file = fopen(patch_script_path, "r");
9468 if (patch_script_file == NULL) {
9469 error = got_error_from_errno2("fopen",
9470 patch_script_path);
9471 goto done;
9474 error = apply_unveil(got_repo_get_path(repo), 0,
9475 got_worktree_get_root_path(worktree));
9476 if (error)
9477 goto done;
9479 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9480 if (error)
9481 goto done;
9483 if (list_stage)
9484 error = got_worktree_status(worktree, &paths, repo,
9485 print_stage, NULL, check_cancelled, NULL);
9486 else {
9487 cpa.patch_script_file = patch_script_file;
9488 cpa.action = "stage";
9489 error = got_worktree_stage(worktree, &paths,
9490 pflag ? NULL : print_status, NULL,
9491 pflag ? choose_patch : NULL, &cpa,
9492 allow_bad_symlinks, repo);
9494 done:
9495 if (patch_script_file && fclose(patch_script_file) == EOF &&
9496 error == NULL)
9497 error = got_error_from_errno2("fclose", patch_script_path);
9498 if (repo)
9499 got_repo_close(repo);
9500 if (worktree)
9501 got_worktree_close(worktree);
9502 TAILQ_FOREACH(pe, &paths, entry)
9503 free((char *)pe->path);
9504 got_pathlist_free(&paths);
9505 free(cwd);
9506 return error;
9509 __dead static void
9510 usage_unstage(void)
9512 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9513 "[file-path ...]\n",
9514 getprogname());
9515 exit(1);
9519 static const struct got_error *
9520 cmd_unstage(int argc, char *argv[])
9522 const struct got_error *error = NULL;
9523 struct got_repository *repo = NULL;
9524 struct got_worktree *worktree = NULL;
9525 char *cwd = NULL;
9526 struct got_pathlist_head paths;
9527 struct got_pathlist_entry *pe;
9528 int ch, pflag = 0;
9529 struct got_update_progress_arg upa;
9530 FILE *patch_script_file = NULL;
9531 const char *patch_script_path = NULL;
9532 struct choose_patch_arg cpa;
9534 TAILQ_INIT(&paths);
9536 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9537 switch (ch) {
9538 case 'p':
9539 pflag = 1;
9540 break;
9541 case 'F':
9542 patch_script_path = optarg;
9543 break;
9544 default:
9545 usage_unstage();
9546 /* NOTREACHED */
9550 argc -= optind;
9551 argv += optind;
9553 #ifndef PROFILE
9554 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9555 "unveil", NULL) == -1)
9556 err(1, "pledge");
9557 #endif
9558 if (patch_script_path && !pflag)
9559 errx(1, "-F option can only be used together with -p option");
9561 cwd = getcwd(NULL, 0);
9562 if (cwd == NULL) {
9563 error = got_error_from_errno("getcwd");
9564 goto done;
9567 error = got_worktree_open(&worktree, cwd);
9568 if (error) {
9569 if (error->code == GOT_ERR_NOT_WORKTREE)
9570 error = wrap_not_worktree_error(error, "unstage", cwd);
9571 goto done;
9574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9575 NULL);
9576 if (error != NULL)
9577 goto done;
9579 if (patch_script_path) {
9580 patch_script_file = fopen(patch_script_path, "r");
9581 if (patch_script_file == NULL) {
9582 error = got_error_from_errno2("fopen",
9583 patch_script_path);
9584 goto done;
9588 error = apply_unveil(got_repo_get_path(repo), 0,
9589 got_worktree_get_root_path(worktree));
9590 if (error)
9591 goto done;
9593 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9594 if (error)
9595 goto done;
9597 cpa.patch_script_file = patch_script_file;
9598 cpa.action = "unstage";
9599 memset(&upa, 0, sizeof(upa));
9600 error = got_worktree_unstage(worktree, &paths, update_progress,
9601 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9602 if (!error)
9603 print_update_progress_stats(&upa);
9604 done:
9605 if (patch_script_file && fclose(patch_script_file) == EOF &&
9606 error == NULL)
9607 error = got_error_from_errno2("fclose", patch_script_path);
9608 if (repo)
9609 got_repo_close(repo);
9610 if (worktree)
9611 got_worktree_close(worktree);
9612 TAILQ_FOREACH(pe, &paths, entry)
9613 free((char *)pe->path);
9614 got_pathlist_free(&paths);
9615 free(cwd);
9616 return error;
9619 __dead static void
9620 usage_cat(void)
9622 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9623 "arg1 [arg2 ...]\n", getprogname());
9624 exit(1);
9627 static const struct got_error *
9628 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9630 const struct got_error *err;
9631 struct got_blob_object *blob;
9633 err = got_object_open_as_blob(&blob, repo, id, 8192);
9634 if (err)
9635 return err;
9637 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9638 got_object_blob_close(blob);
9639 return err;
9642 static const struct got_error *
9643 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9645 const struct got_error *err;
9646 struct got_tree_object *tree;
9647 int nentries, i;
9649 err = got_object_open_as_tree(&tree, repo, id);
9650 if (err)
9651 return err;
9653 nentries = got_object_tree_get_nentries(tree);
9654 for (i = 0; i < nentries; i++) {
9655 struct got_tree_entry *te;
9656 char *id_str;
9657 if (sigint_received || sigpipe_received)
9658 break;
9659 te = got_object_tree_get_entry(tree, i);
9660 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9661 if (err)
9662 break;
9663 fprintf(outfile, "%s %.7o %s\n", id_str,
9664 got_tree_entry_get_mode(te),
9665 got_tree_entry_get_name(te));
9666 free(id_str);
9669 got_object_tree_close(tree);
9670 return err;
9673 static const struct got_error *
9674 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9676 const struct got_error *err;
9677 struct got_commit_object *commit;
9678 const struct got_object_id_queue *parent_ids;
9679 struct got_object_qid *pid;
9680 char *id_str = NULL;
9681 const char *logmsg = NULL;
9683 err = got_object_open_as_commit(&commit, repo, id);
9684 if (err)
9685 return err;
9687 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9688 if (err)
9689 goto done;
9691 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9692 parent_ids = got_object_commit_get_parent_ids(commit);
9693 fprintf(outfile, "numparents %d\n",
9694 got_object_commit_get_nparents(commit));
9695 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9696 char *pid_str;
9697 err = got_object_id_str(&pid_str, pid->id);
9698 if (err)
9699 goto done;
9700 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9701 free(pid_str);
9703 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9704 got_object_commit_get_author(commit),
9705 (long long)got_object_commit_get_author_time(commit));
9707 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9708 got_object_commit_get_author(commit),
9709 (long long)got_object_commit_get_committer_time(commit));
9711 logmsg = got_object_commit_get_logmsg_raw(commit);
9712 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9713 fprintf(outfile, "%s", logmsg);
9714 done:
9715 free(id_str);
9716 got_object_commit_close(commit);
9717 return err;
9720 static const struct got_error *
9721 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9723 const struct got_error *err;
9724 struct got_tag_object *tag;
9725 char *id_str = NULL;
9726 const char *tagmsg = NULL;
9728 err = got_object_open_as_tag(&tag, repo, id);
9729 if (err)
9730 return err;
9732 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9733 if (err)
9734 goto done;
9736 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9738 switch (got_object_tag_get_object_type(tag)) {
9739 case GOT_OBJ_TYPE_BLOB:
9740 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9741 GOT_OBJ_LABEL_BLOB);
9742 break;
9743 case GOT_OBJ_TYPE_TREE:
9744 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9745 GOT_OBJ_LABEL_TREE);
9746 break;
9747 case GOT_OBJ_TYPE_COMMIT:
9748 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9749 GOT_OBJ_LABEL_COMMIT);
9750 break;
9751 case GOT_OBJ_TYPE_TAG:
9752 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9753 GOT_OBJ_LABEL_TAG);
9754 break;
9755 default:
9756 break;
9759 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9760 got_object_tag_get_name(tag));
9762 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9763 got_object_tag_get_tagger(tag),
9764 (long long)got_object_tag_get_tagger_time(tag));
9766 tagmsg = got_object_tag_get_message(tag);
9767 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9768 fprintf(outfile, "%s", tagmsg);
9769 done:
9770 free(id_str);
9771 got_object_tag_close(tag);
9772 return err;
9775 static const struct got_error *
9776 cmd_cat(int argc, char *argv[])
9778 const struct got_error *error;
9779 struct got_repository *repo = NULL;
9780 struct got_worktree *worktree = NULL;
9781 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9782 const char *commit_id_str = NULL;
9783 struct got_object_id *id = NULL, *commit_id = NULL;
9784 int ch, obj_type, i, force_path = 0;
9785 struct got_reflist_head refs;
9787 TAILQ_INIT(&refs);
9789 #ifndef PROFILE
9790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9791 NULL) == -1)
9792 err(1, "pledge");
9793 #endif
9795 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9796 switch (ch) {
9797 case 'c':
9798 commit_id_str = optarg;
9799 break;
9800 case 'r':
9801 repo_path = realpath(optarg, NULL);
9802 if (repo_path == NULL)
9803 return got_error_from_errno2("realpath",
9804 optarg);
9805 got_path_strip_trailing_slashes(repo_path);
9806 break;
9807 case 'P':
9808 force_path = 1;
9809 break;
9810 default:
9811 usage_cat();
9812 /* NOTREACHED */
9816 argc -= optind;
9817 argv += optind;
9819 cwd = getcwd(NULL, 0);
9820 if (cwd == NULL) {
9821 error = got_error_from_errno("getcwd");
9822 goto done;
9824 error = got_worktree_open(&worktree, cwd);
9825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9826 goto done;
9827 if (worktree) {
9828 if (repo_path == NULL) {
9829 repo_path = strdup(
9830 got_worktree_get_repo_path(worktree));
9831 if (repo_path == NULL) {
9832 error = got_error_from_errno("strdup");
9833 goto done;
9838 if (repo_path == NULL) {
9839 repo_path = getcwd(NULL, 0);
9840 if (repo_path == NULL)
9841 return got_error_from_errno("getcwd");
9844 error = got_repo_open(&repo, repo_path, NULL);
9845 free(repo_path);
9846 if (error != NULL)
9847 goto done;
9849 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9850 if (error)
9851 goto done;
9853 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9854 if (error)
9855 goto done;
9857 if (commit_id_str == NULL)
9858 commit_id_str = GOT_REF_HEAD;
9859 error = got_repo_match_object_id(&commit_id, NULL,
9860 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
9861 if (error)
9862 goto done;
9864 for (i = 0; i < argc; i++) {
9865 if (force_path) {
9866 error = got_object_id_by_path(&id, repo, commit_id,
9867 argv[i]);
9868 if (error)
9869 break;
9870 } else {
9871 error = got_repo_match_object_id(&id, &label, argv[i],
9872 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
9873 repo);
9874 if (error) {
9875 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9876 error->code != GOT_ERR_NOT_REF)
9877 break;
9878 error = got_object_id_by_path(&id, repo,
9879 commit_id, argv[i]);
9880 if (error)
9881 break;
9885 error = got_object_get_type(&obj_type, repo, id);
9886 if (error)
9887 break;
9889 switch (obj_type) {
9890 case GOT_OBJ_TYPE_BLOB:
9891 error = cat_blob(id, repo, stdout);
9892 break;
9893 case GOT_OBJ_TYPE_TREE:
9894 error = cat_tree(id, repo, stdout);
9895 break;
9896 case GOT_OBJ_TYPE_COMMIT:
9897 error = cat_commit(id, repo, stdout);
9898 break;
9899 case GOT_OBJ_TYPE_TAG:
9900 error = cat_tag(id, repo, stdout);
9901 break;
9902 default:
9903 error = got_error(GOT_ERR_OBJ_TYPE);
9904 break;
9906 if (error)
9907 break;
9908 free(label);
9909 label = NULL;
9910 free(id);
9911 id = NULL;
9913 done:
9914 free(label);
9915 free(id);
9916 free(commit_id);
9917 if (worktree)
9918 got_worktree_close(worktree);
9919 if (repo) {
9920 const struct got_error *repo_error;
9921 repo_error = got_repo_close(repo);
9922 if (error == NULL)
9923 error = repo_error;
9925 got_ref_list_free(&refs);
9926 return error;
9929 __dead static void
9930 usage_info(void)
9932 fprintf(stderr, "usage: %s info [path ...]\n",
9933 getprogname());
9934 exit(1);
9937 static const struct got_error *
9938 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9939 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9940 struct got_object_id *commit_id)
9942 const struct got_error *err = NULL;
9943 char *id_str = NULL;
9944 char datebuf[128];
9945 struct tm mytm, *tm;
9946 struct got_pathlist_head *paths = arg;
9947 struct got_pathlist_entry *pe;
9950 * Clear error indication from any of the path arguments which
9951 * would cause this file index entry to be displayed.
9953 TAILQ_FOREACH(pe, paths, entry) {
9954 if (got_path_cmp(path, pe->path, strlen(path),
9955 pe->path_len) == 0 ||
9956 got_path_is_child(path, pe->path, pe->path_len))
9957 pe->data = NULL; /* no error */
9960 printf(GOT_COMMIT_SEP_STR);
9961 if (S_ISLNK(mode))
9962 printf("symlink: %s\n", path);
9963 else if (S_ISREG(mode)) {
9964 printf("file: %s\n", path);
9965 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9966 } else if (S_ISDIR(mode))
9967 printf("directory: %s\n", path);
9968 else
9969 printf("something: %s\n", path);
9971 tm = localtime_r(&mtime, &mytm);
9972 if (tm == NULL)
9973 return NULL;
9974 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9975 return got_error(GOT_ERR_NO_SPACE);
9976 printf("timestamp: %s\n", datebuf);
9978 if (blob_id) {
9979 err = got_object_id_str(&id_str, blob_id);
9980 if (err)
9981 return err;
9982 printf("based on blob: %s\n", id_str);
9983 free(id_str);
9986 if (staged_blob_id) {
9987 err = got_object_id_str(&id_str, staged_blob_id);
9988 if (err)
9989 return err;
9990 printf("based on staged blob: %s\n", id_str);
9991 free(id_str);
9994 if (commit_id) {
9995 err = got_object_id_str(&id_str, commit_id);
9996 if (err)
9997 return err;
9998 printf("based on commit: %s\n", id_str);
9999 free(id_str);
10002 return NULL;
10005 static const struct got_error *
10006 cmd_info(int argc, char *argv[])
10008 const struct got_error *error = NULL;
10009 struct got_worktree *worktree = NULL;
10010 char *cwd = NULL, *id_str = NULL;
10011 struct got_pathlist_head paths;
10012 struct got_pathlist_entry *pe;
10013 char *uuidstr = NULL;
10014 int ch, show_files = 0;
10016 TAILQ_INIT(&paths);
10018 while ((ch = getopt(argc, argv, "")) != -1) {
10019 switch (ch) {
10020 default:
10021 usage_info();
10022 /* NOTREACHED */
10026 argc -= optind;
10027 argv += optind;
10029 #ifndef PROFILE
10030 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10031 NULL) == -1)
10032 err(1, "pledge");
10033 #endif
10034 cwd = getcwd(NULL, 0);
10035 if (cwd == NULL) {
10036 error = got_error_from_errno("getcwd");
10037 goto done;
10040 error = got_worktree_open(&worktree, cwd);
10041 if (error) {
10042 if (error->code == GOT_ERR_NOT_WORKTREE)
10043 error = wrap_not_worktree_error(error, "status", cwd);
10044 goto done;
10047 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10048 if (error)
10049 goto done;
10051 if (argc >= 1) {
10052 error = get_worktree_paths_from_argv(&paths, argc, argv,
10053 worktree);
10054 if (error)
10055 goto done;
10056 show_files = 1;
10059 error = got_object_id_str(&id_str,
10060 got_worktree_get_base_commit_id(worktree));
10061 if (error)
10062 goto done;
10064 error = got_worktree_get_uuid(&uuidstr, worktree);
10065 if (error)
10066 goto done;
10068 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10069 printf("work tree base commit: %s\n", id_str);
10070 printf("work tree path prefix: %s\n",
10071 got_worktree_get_path_prefix(worktree));
10072 printf("work tree branch reference: %s\n",
10073 got_worktree_get_head_ref_name(worktree));
10074 printf("work tree UUID: %s\n", uuidstr);
10075 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10077 if (show_files) {
10078 struct got_pathlist_entry *pe;
10079 TAILQ_FOREACH(pe, &paths, entry) {
10080 if (pe->path_len == 0)
10081 continue;
10083 * Assume this path will fail. This will be corrected
10084 * in print_path_info() in case the path does suceeed.
10086 pe->data = (void *)got_error_path(pe->path,
10087 GOT_ERR_BAD_PATH);
10089 error = got_worktree_path_info(worktree, &paths,
10090 print_path_info, &paths, check_cancelled, NULL);
10091 if (error)
10092 goto done;
10093 TAILQ_FOREACH(pe, &paths, entry) {
10094 if (pe->data != NULL) {
10095 error = pe->data; /* bad path */
10096 break;
10100 done:
10101 TAILQ_FOREACH(pe, &paths, entry)
10102 free((char *)pe->path);
10103 got_pathlist_free(&paths);
10104 free(cwd);
10105 free(id_str);
10106 free(uuidstr);
10107 return error;